Skip to content

fix(daemon): make health verdict consult alerts and stop going blind - #3554

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/daemon-health-verdict-observability
Aug 2, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/daemon-health-verdict-observability

Conversation

@Sinity

@Sinity Sinity commented Aug 2, 2026

Copy link
Copy Markdown
Owner

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:

  1. polylogued status printed "Health: ok (6 alerts)" while hook_flow [error] fires in the journal -- because alert_count counted every FAST-tier check run (including OK ones), not actual problems. A fully healthy daemon always shows exactly len(fast checks) "alerts", making "ok (N alerts)" read as a contradiction regardless of N.
  2. "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".
  3. ops.db.cursor_lag_samples has 0 rows ever: the SLO check reads a table whose only producer runs inside a MEDIUM-tier check, and health_check_tiers defaults to "fast" -- so the producer never executes under default daemon operation.
  4. When the watcher is schema-blocked, _periodic_health_check() only started inside the if not watcher_blocked: branch in run_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

  • Gemini AI Studio exports need one reproducible archive path #1: alert_count in daemon_status_payload()'s "health" dict now counts only non-OK alerts; a new checks_run field carries the previous "every check" total for anyone who still wants it (polylogue/daemon/status.py).
  • feat: unify Gemini export rendering and Drive sync in gmd #2: New FAST-tier heartbeat_staleness check plus DAEMON_HEARTBEAT_STALE_WARN_SECONDS (1.5x DAEMON_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_lines renders a [STALE] marker on the plain-text status line so the signal is visible without reading alerts separately.
  • refactor: split CLI into command modules and add HTML preview output #4: _periodic_health_check() moved into the unconditional maintenance_tasks list in run_daemon_services (polylogue/daemon/cli.py), so it now starts regardless of watcher_blocked. All FAST-tier checks are read-only (schema_version via mode=ro connections, 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.
  • The gmd CLI monolith is blocking richer render surfaces #3 (deferred, findings recorded): record_cursor_lag_sample is only ever called from _check_cursor_lag_anomaly_layer, invoked by _check_cursor_lag_medium (MEDIUM tier). health_check_tiers defaults to "fast" (polylogue/config.py), and docs/daemon.md already 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

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

New tests added: test_health_alert_count_excludes_ok_severity_checks and test_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_floors and test_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), and test_run_daemon_services_schema_block_skips_write_but_starts_health_check (renamed/rewritten from the old ..._skips_db_background_work test, 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 from EXPECTED_MEDIUM_CHECKS, causing test_medium_tier_inventory_pinned to fail independent of this change.

devtools verify --quick passes (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_calls in test_daemon_http_security.py, about polylogue/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

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]>
@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 32 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 5b7d2dd8-8e96-48e4-a91e-256b014b8630

📥 Commits

Reviewing files that changed from the base of the PR and between 9eda89a and bbd0782.

📒 Files selected for processing (11)
  • docs/daemon.md
  • docs/plans/degrade-loudly-allowlist.yaml
  • polylogue/daemon/cli.py
  • polylogue/daemon/health.py
  • polylogue/daemon/lifecycle.py
  • polylogue/daemon/status.py
  • tests/unit/daemon/test_daemon_cli.py
  • tests/unit/daemon/test_daemon_lifecycle.py
  • tests/unit/daemon/test_daemon_status.py
  • tests/unit/daemon/test_health_check_paths.py
  • tests/unit/daemon/test_health_contract.py

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Sinity
Sinity merged commit 51fb1a8 into master Aug 2, 2026
3 checks passed
@Sinity
Sinity deleted the feature/fix/daemon-health-verdict-observability branch August 2, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant