Implement a long-running daemon service that continuously polls Claude
Code session logs and exposes usage data via local HTTP endpoints.
Architecture:
- internal/daemon/service.go: Core Service struct managing poll loop,
snapshot computation, event buffering, and HTTP handlers
- cmd/daemon.go: Cobra commands for start/status/stop with detach mode
HTTP Endpoints (default 127.0.0.1:8787):
- GET /healthz - Liveness probe for orchestration
- GET /v1/status - Current aggregate snapshot + daemon runtime info
- GET /v1/events - Recent event buffer as JSON array
- GET /v1/stream - Server-Sent Events for real-time updates
Snapshot model captures:
- Session/prompt/API call counts
- Token totals and estimated cost
- Cache hit rate
- Rolling daily averages (cost/day, tokens/day, sessions/day)
Delta detection emits events only when usage actually changes, keeping
the event stream lean for downstream consumers.
Detach mode (-d, --detach):
- Forks a child process with stdout/stderr redirected to log file
- Writes PID file for process management
- Parent exits after confirming child is running
This daemon serves as the foundation for planned capabilities like
incident replay, runaway detection, and session classification.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>