Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 5 additions & 39 deletions hooks/agent-monitor-hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment on lines 12 to +15

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove the remaining port-file discovery claim

After this change the hook only tries DOCKER_MONITOR_PORT and port 3000, but the primary hook-setup documentation in README.md:163 still says it also discovers a leftover legacy port file. Users following that section may expect an Electron/legacy instance on a dynamic port to receive events when it no longer can, so update that remaining description alongside the other documentation changes.

Useful? React with 👍 / 👎.



def post_json(host, port, path, body, timeout, use_https=False):
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion hooks/claude-hooks-config.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
Loading