From c9bc0a5fa0b9a2e5ae82bb39c2a67fca02ac817b Mon Sep 17 00:00:00 2001 From: Henry Hu Date: Tue, 20 Oct 2026 14:43:38 +0800 Subject: [PATCH] hooks: drop Electron port-file discovery The Electron shell is gone (#7), so nothing writes ~/Library/Application Support/llm-usage-tracker/server-port anymore. Remove the hook's port-file probe, its short-timeout/stale-file cleanup branch, and the Electron comments. Local discovery is now :3789 (DOCKER_MONITOR_PORT) then :3000, each with the same 3s budget as before; MONITOR_URL and the edge shipper are untouched. Also drop the README tip about deleting the leftover port file, which no longer affects the hook. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- hooks/agent-monitor-hook.py | 44 ++++------------------------------ hooks/claude-hooks-config.json | 2 +- 3 files changed, 7 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index e88c347..a9c9b7d 100644 --- a/README.md +++ b/README.md @@ -366,7 +366,7 @@ You bind-mounted the DB directory on macOS. WAL keeps its wal-index in an mmap'd Something rebuilt it for the wrong runtime. `npm rebuild better-sqlite3` restores the one true state (system-Node ABI), which is what tests, dev and the container all use. **Hooks feel slow / events missing.** -Verify the hub is up first: `curl http://127.0.0.1:3789/api/live`. If an old desktop build ever ran on this machine, delete its leftover port file — `rm "~/Library/Application Support/llm-usage-tracker/server-port"` — so the hook stops probing a port nothing listens on. The hook can be tested any time with `bash hooks/test-hook.sh`. +Verify the hub is up first: `curl http://127.0.0.1:3789/api/live`. The hook can be tested any time with `bash hooks/test-hook.sh`. **Claude card says "Session key expired".** Grab a fresh `sessionKey` cookie from claude.ai (DevTools → Application → Cookies) and paste it in Settings. If Claude Code is installed and logged in, the Keychain OAuth path usually makes this unnecessary. diff --git a/hooks/agent-monitor-hook.py b/hooks/agent-monitor-hook.py index 1e6716b..c639238 100755 --- a/hooks/agent-monitor-hook.py +++ b/hooks/agent-monitor-hook.py @@ -9,30 +9,10 @@ import threading -PORT_FILE = os.path.expanduser( - "~/Library/Application Support/llm-usage-tracker/server-port" -) - - def read_candidate_ports(): - """Yield (port, from_file) pairs. from_file marks the server-port entry: - it is the least trustworthy candidate — Electron killed with SIGKILL used - to leave it pointing at an orphaned server that accepts TCP but never - answers HTTP, so it gets a shorter timeout and stale-file cleanup below.""" - ports = [] - try: - with open(PORT_FILE) as f: - ports.append((int(f.read().strip()), True)) - except (OSError, ValueError): - pass - ports.append((int(os.environ.get("DOCKER_MONITOR_PORT", "3789")), False)) - ports.append((3000, False)) - seen, out = set(), [] - for p, from_file in ports: - if p not in seen: - seen.add(p) - out.append((p, from_file)) - return out + """Local tracker ports to try: the Docker hub, then a `next dev` server.""" + ports = [int(os.environ.get("DOCKER_MONITOR_PORT", "3789")), 3000] + return list(dict.fromkeys(ports)) def post_json(host, port, path, body, timeout, use_https=False): @@ -199,26 +179,12 @@ def _post(): pass return - for port, from_file in read_candidate_ports(): + for port in read_candidate_ports(): # Post to EVERY listening instance — otherwise whichever instance is # open steals the events and the other's history has gaps. A refused # connection fails in ~1ms; only a listening-but-slow server costs time. - # The port-file entry gets a much smaller budget: a healthy embedded - # server on localhost answers in ~10ms, while a wedged orphan (stale - # file after an unclean Electron exit) eats the whole timeout on - # every single hook event. try: - post_json("127.0.0.1", port, "/api/monitor/events", body, - 0.75 if from_file else 3) - except ConnectionRefusedError: - if from_file: - # Nothing listens there at all. The file is definitely stale — - # Electron writes it only after its server passes /api/live — - # so remove it instead of paying this path on every event. - try: - os.unlink(PORT_FILE) - except OSError: - pass + post_json("127.0.0.1", port, "/api/monitor/events", body, 3) except Exception: pass diff --git a/hooks/claude-hooks-config.json b/hooks/claude-hooks-config.json index 447f932..fef8805 100644 --- a/hooks/claude-hooks-config.json +++ b/hooks/claude-hooks-config.json @@ -1,5 +1,5 @@ { - "_comment": "Add these hooks to ~/.claude/settings.json. For local use no extra variables are needed — the hook auto-discovers the tracker on :3789 (Docker), :3000, and the Electron port file. For a remote hub, run the edge shipper on this machine and use hooks_edge (see edge/README.md). For cloud/tunnel sessions, set MONITOR_URL instead.", + "_comment": "Add these hooks to ~/.claude/settings.json. For local use no extra variables are needed — the hook auto-discovers the tracker on :3789 (Docker) and :3000 (dev server). For a remote hub, run the edge shipper on this machine and use hooks_edge (see edge/README.md). For cloud/tunnel sessions, set MONITOR_URL instead.", "hooks_local": { "PreToolUse": [