Repository navigation
fix(daemon): make health verdict consult alerts and stop going blind - #3554
Conversation
Problem: an observability audit (polylogue-7eo7) found the daemon health pipeline lying to operators in several ways. `alert_count` counted every FAST-tier check run, including OK ones, so a fully healthy daemon printed "Health: ok (6 alerts)" -- reading as a self-contradiction even with zero real problems. A heartbeat sitting between 1x and 2x the configured interval (observed: 1369.8s against a 900s interval) produced no signal at all, printing flat "Status: running (heartbeat 1369.8s ago)" all the way up to the 2x "vanished" floor. And when the watcher was schema-blocked, `_periodic_health_check()` only started inside the `if not watcher_blocked:` branch, so the daemon ran with no periodic health checks or notifications for the entire blocked duration -- exactly when an operator most needs to see "archive tier layout is not ready". Solution: - `alert_count` in `daemon_status_payload()` now counts only non-OK alerts; `checks_run` carries the previous "every check" total for anyone who wants it (polylogue/daemon/status.py). - New FAST-tier `heartbeat_staleness` check + `DAEMON_HEARTBEAT_STALE_WARN_SECONDS` (1.5x the configured interval, derived from the existing `DAEMON_HEARTBEAT_INTERVAL_SECONDS`, not hardcoded) fills the gap between "fresh" and the pre-existing 2x "vanished" floor with a WARNING-severity "stale" state (polylogue/daemon/lifecycle.py, polylogue/daemon/health.py). `format_daemon_status_lines` renders a `[STALE]` marker on the plain-text status line. - `_periodic_health_check()` moved into the unconditional `maintenance_tasks` list in `run_daemon_services` (polylogue/daemon/cli.py) so it starts regardless of `watcher_blocked`. FAST-tier checks are all read-only (schema_version, disk/WAL space, hook flow, source availability, heartbeat staleness), so this doesn't reintroduce the archive-write concern the schema-block guard exists for. Follow-up scoped, not fixed here: `ops.db.cursor_lag_samples` has 0 rows because its only producer, `record_cursor_lag_sample`, runs inside `_check_cursor_lag_medium` (MEDIUM tier), and `health_check_tiers` defaults to `"fast"` (polylogue/config.py, docs/daemon.md documents this as deliberate -- MEDIUM/EXPENSIVE are "explicit operator diagnostics"). The SLO check reading that table is not dead code, but its producer never runs under default daemon operation. Filing a follow-up bead to either decouple sample recording from the MEDIUM gate or make the gap explicit in status output is the right next step; this PR does not change default tiers, which would be a larger behavior/performance decision. Verification: `devtools test tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_health_contract.py tests/unit/daemon/test_daemon_lifecycle.py tests/unit/daemon/test_daemon_status.py tests/unit/daemon/test_daemon_cli.py` -- 241 passed (includes new tests proving a nonzero-alert scenario no longer reports "ok (N alerts)" for OK-only checks, and a stale-heartbeat scenario producing a WARNING alert + `[STALE]` marker). Also fixed an unrelated pre-existing drift in the same pinned-check-inventory test (`capture_coverage` from #3541 was missing from `EXPECTED_MEDIUM_CHECKS`). `devtools verify --quick` passes (ruff format/check, mypy --strict, render all --check, layering, degrade-loudly, and the rest of the static gate set). A broader `devtools test tests/unit/daemon/` run (2010 tests) shows 2009 passing; the one remaining failure (`test_no_token_in_log_or_print_calls` in `api_auth.py`) is unrelated and pre-existing -- this PR does not touch that file. Ref polylogue-7eo7 Co-Authored-By: Claude <[email protected]>
|
Warning Review limit reached
Next review available in: 32 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (11)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Fixes two of the four observability defects audited in polylogue-7eo7 (alert-count/verdict contradiction, silent heartbeat staleness) plus the schema-blocked health-loop gap, and scopes the fourth (dead cursor-lag SLO check) as a documented follow-up rather than a silent no-op.
Problem
An audit of the daemon health/status pipeline (polylogue-7eo7) found:
polylogued statusprinted"Health: ok (6 alerts)"whilehook_flow [error]fires in the journal -- becausealert_countcounted every FAST-tier check run (including OK ones), not actual problems. A fully healthy daemon always shows exactlylen(fast checks)"alerts", making "ok (N alerts)" read as a contradiction regardless of N."Status: running (heartbeat 1369.8s ago)"on a 15-minute (900s) interval produced no staleness signal at all -- the only existing threshold was the 2x-interval (1800s) "vanished" floor, so anything short of that read as flat "running".ops.db.cursor_lag_sampleshas 0 rows ever: the SLO check reads a table whose only producer runs inside a MEDIUM-tier check, andhealth_check_tiersdefaults to"fast"-- so the producer never executes under default daemon operation._periodic_health_check()only started inside theif not watcher_blocked:branch inrun_daemon_services, so the daemon ran with zero periodic health checks/notifications for the entire blocked duration -- exactly when an operator most needs to see "archive tier layout is not ready".Solution
alert_countindaemon_status_payload()'s"health"dict now counts only non-OK alerts; a newchecks_runfield carries the previous "every check" total for anyone who still wants it (polylogue/daemon/status.py).heartbeat_stalenesscheck plusDAEMON_HEARTBEAT_STALE_WARN_SECONDS(1.5xDAEMON_HEARTBEAT_INTERVAL_SECONDS, derived, not hardcoded) fills the gap between "fresh" and the pre-existing 2x "vanished" floor with a WARNING-severity"stale"lifecycle state (polylogue/daemon/lifecycle.py,polylogue/daemon/health.py).format_daemon_status_linesrenders a[STALE]marker on the plain-text status line so the signal is visible without reading alerts separately._periodic_health_check()moved into the unconditionalmaintenance_taskslist inrun_daemon_services(polylogue/daemon/cli.py), so it now starts regardless ofwatcher_blocked. All FAST-tier checks are read-only (schema_version viamode=roconnections, disk/WAL space stats, hook flow, source availability, heartbeat staleness), so this does not reintroduce the archive-write concern the schema-block guard exists for.record_cursor_lag_sampleis only ever called from_check_cursor_lag_anomaly_layer, invoked by_check_cursor_lag_medium(MEDIUM tier).health_check_tiersdefaults to"fast"(polylogue/config.py), anddocs/daemon.mdalready documents MEDIUM/EXPENSIVE as deliberate "explicit operator diagnostics" -- not accidentally disabled. So the check is not dead code in the sense of being unreachable, but its only producer never runs under default daemon operation, making the table permanently empty in practice. Two real options for a follow-up: (a) decouple sample recording from the MEDIUM gate since it's a cheap read+write, not an expensive scan, or (b) make the gap explicit in status output (e.g. surface "0 samples, producer not scheduled" rather than silence). Left to a follow-up bead since either choice is a behavior decision, not a pure bug fix, and out of the "prioritize Gemini AI Studio exports need one reproducible archive path #1/feat: unify Gemini export rendering and Drive sync in gmd #2" scope this bead itself calls for.Verification
New tests added:
test_health_alert_count_excludes_ok_severity_checksandtest_health_alert_count_reflects_a_real_problem_not_ok(proves a nonzero-alert scenario no longer reports "ok" and an all-OK scenario reports(0 alerts)instead of(6 alerts)),test_heartbeat_staleness_warning_between_warn_and_vanished_floorsandtest_lifecycle_status_reports_stale_between_warn_and_vanished_floors(proves the exact observed 1369.8s/900s scenario now produces a WARNING alert and a"stale"lifecycle state),test_cli_status_marks_a_stale_but_still_running_heartbeat(proves the[STALE]marker renders), andtest_run_daemon_services_schema_block_skips_write_but_starts_health_check(renamed/rewritten from the old..._skips_db_background_worktest, which previously asserted the opposite -- that health checks must NOT start when blocked).Also fixed an unrelated pre-existing drift in the same pinned-check-inventory test:
capture_coverage(added in #3541) was missing fromEXPECTED_MEDIUM_CHECKS, causingtest_medium_tier_inventory_pinnedto fail independent of this change.devtools verify --quickpasses (ruff format/check, mypy --strict, render all --check, layering, degrade-loudly, closure-matrix, and the rest of the static gate set) -- also ran automatically via the pre-push hook.A broader
devtools test tests/unit/daemon/run (2010 tests) shows 2009 passing; the one remaining failure (test_no_token_in_log_or_print_callsintest_daemon_http_security.py, aboutpolylogue/daemon/api_auth.py) is unrelated and pre-existing -- this PR does not touch that file.Not run: full
devtools verify(seed-testmon, non-slow suite) -- started once, killed per operator instruction to avoid cross-turn backgrounding; the focused command above covers every file this PR touches.Ref polylogue-7eo7