#32

Observer watch loop: adaptive backoff + Claude Code lifecycle awareness (gap for task #34)

Openparzival1lopened 7mo ago

Follow-up notes from discussion around kanban task 19 (watch_session, landed in PR #31). No code changes proposed yet — capturing scope so kanban task 34 (background sidecar; partial in PR #29) can plan accordingly.

1. PID stuff — what exists vs what's task 34's job

Already in the DB (migration 004): observation_state.observer_pid, status (idle/running/stopped), started_at. Helpers set_observer_running / set_observer_stopped / get_running_observers exist.

Not yet wired: the liveness check (kill -0 $PID), the SIGTERM stop handler, and "spawn a watcher process & record its PID." All of that is task 34. ADR-019 explicitly calls out stale-PID detection as task 34's responsibility.

2. Adaptive backoff polling — yes, easy fit

Trivial to add to watch_session. Pattern: track consecutive no-growth iterations, scale poll_interval_sec up when idle, reset to default on any growth. Roughly:

  • 0–N idle ticks → 1s (current default)
  • After 2.5 min idle → 60s
  • After more idle → 5 min, etc., capped at some ceiling

The current code already isolates the wait in sleep_fn(poll_interval_sec), so this is a few lines: replace the constant with a function of consecutive_no_growth. Worth doing for the long-idle case — saves a few thousand stat() calls per hour for nothing.

3. PID + reopen scenarios

Three cases when a user closes Claude Code:

CaseWhat happens
User runs claude --resume <sessionId> (same session)Same JSONL is appended to. If the watcher PID is still alive, it auto-detects growth and resumes observation seamlessly. ✅
User starts a fresh claude in the same dirNew sessionId, new JSONL. Old watcher is still polling the now-static old file. The new session is unobserved unless explicitly opted in again. ⚠️
Watcher process was killed (manually, OS reboot)Stale PID in DB. ADR-019's kill -0 \$PID check (in task 34) corrects status to stopped on next read. Re-running /threadhop:observe resumes from the recorded source_byte_offset. ✅

The gap nobody's solved yet

The watcher has no way today to detect "Claude Code exited but I'm still running." If the user just closes the terminal without invoking --stop, the watcher polls forever against a frozen file. Task 34 needs to define the trigger — options:

  • Hook on session end → SIGTERM the watcher
  • Watcher periodically scans ps/lsof for a claude process holding this session
  • "No growth for >X minutes → exit" timeout

Adaptive backoff (item 2) softens the cost of this gap (a forever-polling watcher on a dead session does ~1 stat per 5 min instead of per second), but doesn't fix it.

Action items (for task 34 or follow-up tasks)

  • Implement adaptive poll backoff in watch_session (consecutive-no-growth → scaled sleep_fn interval)
  • Define + implement Claude-Code-exited detection (pick from the three options above)
  • Wire observer_pid / status columns: write on spawn, clear on graceful exit, stale-PID detection on read
  • SIGTERM handler in the watcher: flush state, set status='stopped', exit cleanly
  • Verify "fresh claude after close" path: confirm new sessionId triggers a separate observation_state row, not a hijack of the old watcher

References

  • ADR-015, ADR-019 (in docs/DESIGN-DECISIONS.md)
  • PR #31 — kanban task 19, observer.watch_session (the watch-mode loop this issue builds on)
  • PR #29 — partial kanban task 34 (threadhop observe CLI subcommand, on-demand only)
  • PR #28 — kanban task 18 (observer core function)

Activity

0

No activity yet.