Repository navigation
fix(backend): window the chat-first materialization health scan - #11949
Merged
Merged
Conversation
`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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 boundon the scan itself. Two consequences, one root cause:
droppedcounted all-time dropped intentsand nothing in
backend/database/chat_first_intents.pyever deletes an intent (verified —no
deletecall anywhere in that file). The first time a single card was ever dropped,every later weekly verdict was
unhealthyforever, with no path back tohealthy. Thatdefeats 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.
run_scheduled_checkcalled the collector withlimit=Noneover an unfilteredcollection_groupscan 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 singlecreated_at >=inequality filter on thecollection_groupquery.summarize()/collect()gained an optional
min_created_atparameter and the report now carries awindow_startfield so a windowed and an all-time report are never confused.
Index question. A single-field filter on a
collection_groupquery needs no explicitindex — this repo already ships exactly that shape unindexed and unregistered:
backend/database/llm_usage.py'sdb.collection_group("llm_usage").where("date", ">=", cutoff_id).The repo's own
firestore_query_coverage.pyratchet only tracks queries with 2+ chainedwhere/order_bycalls (len(state.components) < 2is skipped entirely), which is exactlywhy that
llm_usagequery never appears in the registry or the coverage report. I confirmedthis class of change trips neither the coverage ratchet nor the index-registry composite
requirement by running
python3 backend/scripts/firestore_query_coverage.py --check-ratchetbefore and after (exit 0,
chat_first_materialization_healthnever 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 stillmeans 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_rateis derived from whatever document setit's given, windowing the scan also turns
drop_rateinto a rolling-window figure instead ofan 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.pykeeps its defaultunwindowed, 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-daysflag tolet 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. Ijudged this acceptable because
ProactiveIntent.created_atis a required field(
models/chat_first.py), so an undated document is already outside the normal write path; theCLI's unwindowed default remains the way to audit for that class of corruption. This is called
out in
_documents' andrun_scheduled_check's docstrings and covered bytest_windowed_scan_cannot_see_an_undated_document_no_matter_how_recent.The check still routes a
healthyverdict 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 smallFirestore stand-in that reproduces the missing-field-exclusion behavior so the tests exercise
the real
_documents→summarizepipeline, not a mockedcollect:test_windowed_collect_excludes_a_drop_older_than_the_window)test_unwindowed_collect_still_counts_an_old_drop)run_scheduled_checkderives its own window bound from its own run time (test_scheduled_check_computes_the_window_from_its_own_run_time)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)test_windowed_scan_cannot_see_an_undated_document_no_matter_how_recent)render()showsall-timevs.since <timestamp>correctlyAll existing tests in that file were updated for the new
min_created_atcollector parameterand continue to pass unchanged in behavior.
Failure-Class
backend/utils/task_intelligence/chat_first_materialization_health.pyand its knowncandidates (
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 interactivedeadline 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