Skip to content

Commit 1057287

Browse files
committed
fix: machine registration before sync, date filter, unique hostnames
1 parent 475fbbb commit 1057287

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

agent/claude_tracker/cli.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ def setup(
130130
elif not config.get("api_key"):
131131
config["api_key"] = generate_api_key()
132132
else:
133-
default_name = existing_config.get("machine_name") if existing_config else platform.node()
133+
if existing_config:
134+
default_name = existing_config.get("machine_name", platform.node())
135+
else:
136+
# Append short UUID to hostname to avoid duplicates across PCs
137+
default_name = f"{platform.node()}-{generate_machine_id()[:4]}"
134138
config["machine_name"] = click.prompt("Machine name", default=default_name)
135139
config["supabase_url"] = click.prompt(
136140
"Supabase URL",
@@ -194,6 +198,20 @@ def sync(verbose: bool, daily_only: bool, force: bool) -> None:
194198

195199
click.echo(f"Syncing machine: {config['machine_name']} ({machine_id[:8]}...)")
196200

201+
# Ensure machine is registered (fixes FK constraint failures)
202+
try:
203+
client.table("machines").upsert({
204+
"id": machine_id,
205+
"name": config["machine_name"],
206+
"api_key": config.get("api_key", ""),
207+
"os": detect_os(),
208+
"hostname": platform.node(),
209+
}, on_conflict="id").execute()
210+
except Exception as e:
211+
click.echo(f"\n ERROR: Cannot register machine in Supabase: {e}", err=True)
212+
click.echo(" Check your supabase_url and supabase_service_key in config.", err=True)
213+
raise SystemExit(1)
214+
197215
# Daily usage
198216
click.echo("\n Collecting daily usage...", nl=False)
199217
since = None

agent/claude_tracker/daemon.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,20 @@ def _run_sync_cycle(config: dict[str, Any]) -> dict[str, int]:
4040
from supabase import create_client
4141
from .sync import sync_daily_usage, sync_sessions, sync_rate_limits, sync_stats_extra
4242

43+
import platform as _platform
44+
4345
machine_id = config["machine_id"]
4446
client = create_client(config["supabase_url"], config["supabase_service_key"])
4547
results: dict[str, int] = {}
4648

49+
# Ensure machine is registered (prevents FK failures)
50+
client.table("machines").upsert({
51+
"id": machine_id,
52+
"name": config.get("machine_name", _platform.node()),
53+
"api_key": config.get("api_key", ""),
54+
"hostname": _platform.node(),
55+
}, on_conflict="id").execute()
56+
4757
# Daily usage
4858
since = None
4959
last = config.get("last_sync", {}).get("daily_usage")

agent/claude_tracker/sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def sync_daily_usage(
3838

3939
rows = []
4040
for record in records:
41-
if last_sync and record.date <= last_sync[:10]:
41+
if last_sync and record.date < last_sync[:10]:
4242
continue
4343
rows.append({
4444
"machine_id": machine_id,

0 commit comments

Comments
 (0)