Skip to content

fix(backend): window the chat-first materialization health scan - #11949

Merged
Git-on-my-level merged 2 commits into
mainfrom
fix/materialization-health-window
Aug 20, 2026
Merged

Git-on-my-level merged 2 commits into
mainfrom
fix/materialization-health-window

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

Summary

backend/utils/task_intelligence/chat_first_materialization_health.py (landed in #11873)
had an upper age bound on individual intents (stale_after_hours, 48h) but no lower bound
on the scan itself. Two consequences, one root cause:

  1. The weekly alarm latched permanently. dropped counted all-time dropped intents
    and nothing in backend/database/chat_first_intents.py ever deletes an intent (verified —
    no delete call anywhere in that file). The first time a single card was ever dropped,
    every later weekly verdict was unhealthy forever, with no path back to healthy. That
    defeats the check's own stated purpose (see its docstring): forcing the deferred
    server-materialization decision, which a permanently-red signal gets muted instead of
    acted on.
  2. The scan grew without bound. run_scheduled_check called the collector with
    limit=None over an unfiltered collection_group scan of an ever-growing collection.
    Weekly read cost rose linearly with the collection's all-time size forever.

What changed

Added a lower bound: the scheduled check now windows its Firestore read to the last
HEALTH_CHECK_WINDOW_DAYS (14 days — two weekly report cycles) via a single
created_at >= inequality filter on the collection_group query. summarize()/collect()
gained an optional min_created_at parameter and the report now carries a window_start
field so a windowed and an all-time report are never confused.

Index question. A single-field filter on a collection_group query needs no explicit
index — this repo already ships exactly that shape unindexed and unregistered:
backend/database/llm_usage.py's db.collection_group("llm_usage").where("date", ">=", cutoff_id).
The repo's own firestore_query_coverage.py ratchet only tracks queries with 2+ chained
where/order_by calls (len(state.components) < 2 is skipped entirely), which is exactly
why that llm_usage query never appears in the registry or the coverage report. I confirmed
this class of change trips neither the coverage ratchet nor the index-registry composite
requirement by running python3 backend/scripts/firestore_query_coverage.py --check-ratchet
before and after (exit 0, chat_first_materialization_health never appears in the report).
I rejected registering a composite index (unnecessary — this never becomes a multi-field or
ordered query) and rejected Python-side-only filtering (doesn't reduce Firestore read cost,
which is bug #2).

Window length. 14 days = 2× the weekly cadence. A 7-day window against a 7-day cadence
clips a document right at the boundary under any clock skew; 14 days guarantees every intent
that goes stale is seen by at least one weekly run, and a second cycle so a genuinely-fixed
problem is confirmed clear rather than blinking healthy for exactly one week. It's also
comfortably larger than DEFAULT_STALE_AFTER_HOURS (48h) — that value is unchanged and still
means what it meant before (when an individual intent flips from in-flight to decided); the
window is an unrelated, larger bound on which intents are read at all.

Side effect, deliberately kept. Because drop_rate is derived from whatever document set
it's given, windowing the scan also turns drop_rate into a rolling-window figure instead of
an all-time one that a burst of drops could no longer move once a year of delivered intents
had accumulated — the "also worth considering" note in the task. No separate mechanism needed.

CLI. backend/scripts/chat_first_materialization_drop_rate.py keeps its default
unwindowed, all-time behavior — "what is our all-time drop rate" is a legitimate,
operator-invoked question the scheduled alarm shouldn't have to answer, and it isn't a
permanent signal so it doesn't inherit either bug. Added an opt-in --window-days flag to
let an operator reproduce the scheduled check's own scan for comparison.

Traded away. Firestore excludes a document from a field-filter's results when that
document is missing the field or holds a mismatched type, independent of how recently it was
written. So the windowed scheduled check can no longer detect a document with a missing or
malformed created_at — not "only an old one," none, ever, regardless of window length. I
judged this acceptable because ProactiveIntent.created_at is a required field
(models/chat_first.py), so an undated document is already outside the normal write path; the
CLI's unwindowed default remains the way to audit for that class of corruption. This is called
out in _documents' and run_scheduled_check's docstrings and covered by
test_windowed_scan_cannot_see_an_undated_document_no_matter_how_recent.

The check still routes a healthy verdict weekly (unchanged) — the docstring's own
"silence must not let a clean result drift into 'never'" requirement is untouched.

Tests

Added to backend/tests/unit/test_chat_first_materialization_drop_rate.py, including a small
Firestore stand-in that reproduces the missing-field-exclusion behavior so the tests exercise
the real _documents → summarize pipeline, not a mocked collect:

  • a drop older than the window is excluded (test_windowed_collect_excludes_a_drop_older_than_the_window)
  • an unwindowed run still counts an old drop (test_unwindowed_collect_still_counts_an_old_drop)
  • run_scheduled_check derives its own window bound from its own run time (test_scheduled_check_computes_the_window_from_its_own_run_time)
  • the same dropped intent alarms while inside the window and clears once it ages out — the
    latch-bug regression test (test_scheduled_check_still_alarms_on_a_drop_inside_the_window,
    test_scheduled_check_clears_once_the_same_drop_rolls_past_the_window)
  • the documented undated-detection trade-off (test_windowed_scan_cannot_see_an_undated_document_no_matter_how_recent)
  • render() shows all-time vs. since <timestamp> correctly

All existing tests in that file were updated for the new min_created_at collector parameter
and continue to pass unchanged in behavior.

Failure-Class

backend/utils/task_intelligence/chat_first_materialization_health.py and its known
candidates (FC-hardcoded-absolute-date-bound, FC-unbounded-user-collection-in-prompt,
FC-deadline-bounded-query-without-covering-index, FC-day-bucket-key-recomputed,
FC-unbounded-module-cache) don't match: this isn't an LLM-prompt bound, an interactive
deadline query, a day-bucket key, or a module cache — it's a recurring background health
check whose missing lower bound produces both an unbounded read and a latched alarm at
once. Recorded as a new class,
.github/failure-classes/FC-recurring-check-scans-unbounded-history.json.

Failure-Class: new

🤖 Generated with Claude Code

Review in cubic

Git-on-my-level and others added 2 commits August 20, 2026 15:34
`chat_first_materialization_health.py` bounded intent age on the upper end
(stale_after_hours) but never on the lower end. Since nothing ever deletes a
chat_first_proactive_intents document, that meant two things: the weekly
alarm latched unhealthy forever after the first dropped intent (dropped
counted all-time), and the collection_group scan's read cost grew in
lockstep with the collection's all-time size (limit=None).

Add a 14-day (two report cycles) lower-bound window via a single
`created_at >=` inequality filter on the collection_group query. A
single-field filter needs no explicit Firestore index -- this repo already
ships that exact shape unindexed in llm_usage.get_global_top_features, and
firestore_query_coverage.py's own ratchet only tracks 2+ component chains,
confirmed unaffected by --check-ratchet before and after. The CLI keeps its
unwindowed, all-time default (a legitimate operator question) and gains an
opt-in --window-days flag.

Failure-Class: new

Co-Authored-By: Claude Opus 5 <[email protected]>
The definition was committed with a guessed PR number (the next sequential
ID after the highest one visible via gh at the time) before the PR existed.
Update evidence_prs to the actual PR (#11949) now that gh pr create returned
it.

Co-Authored-By: Claude Opus 5 <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit 7c890ea into main Aug 20, 2026
35 checks passed
@Git-on-my-level
Git-on-my-level deleted the fix/materialization-health-window branch August 20, 2026 20:55
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