Skip to content

perf(firestore): narrow finalization candidate queries behind WQ13 lever - #23614

Merged
Git-on-my-level merged 3 commits into
mainfrom
lane/firestore-reads-wq13-candidates-rb
Oct 25, 2026
Merged

Git-on-my-level merged 3 commits into
mainfrom
lane/firestore-reads-wq13-candidates-rb

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Firestore reads loop WQ13: rework the conversation_finalization_jobs reconcile candidate queries, which re-scanned users/conversations on every job tick. Predicate-equivalent rewrites replace the full candidate scans (inequality/staleness pushdown) so each tick reads only candidate pages instead of re-collecting the collection.

  • Default-off is byte-identical behavior; the rework activates via the registered lever (rides WQ15 registration; enable is a later config PR, measurement-gated).
  • Review: zcode glm-5.3 SAFE (exec-reunion-pistol-cup-shell-margin-story, 2026-10-23T07:20Z): predicate equivalence vs admission fences verified, all 3 composite index shapes correct (COLLECTION_GROUP conversations entry distinct), no reads-more path; reviewer independently re-ran 86+499+62+176 tests. Five minors addressed or accepted as follow-ups.
  • Re-raise reason: cfj candidate callers surged from ~65k/h (2026-10-23, the original merge-defer) to 25.5M/h at 13Z 2026-10-25 (snapshot 2026-10-25T13:00:02Z) after the singleton/backfill flag-off un-coordinated the reconcile loops. The surge will partly re-collapse when singleton_loops re-ramps, but the per-tick full-collection re-scan remains the underlying amplifier.

Product invariants affected

  • INV-MEM-4 — default-off code path; lever-gated activation, reverts at 0.
  • INV-DATA-1 — candidate selection is predicate-equivalent to the previous scan (review-verified); no conversation is dropped or double-processed; no routing or ownership change.

Line-count exception

Line-Count-Exception: backend/database/conversation_finalization_jobs.py | 2976 -> 3040 | WQ13 adds lever-gated candidate filters while preserving main's content-due scan and admission fences; the gate withholds the pushdown while a lifecycle-v2 serve cohort can exist.
Line-Count-Exception: backend/database/firestore_index_registry.py | 2210 -> 2247 | Registers the three WQ13 candidate-query composite index shapes, including the conversations collection-group index.

Test plan

  • Unit tests for candidate-query predicate equivalence and index shapes (reviewer re-ran 86+499+62+176 on the review commit; CI re-runs on this branch).
  • Post-merge: cfj candidate caller reads/day from omi_firestore_billed_reads_by_caller_total before/after any enable; lever stays 0 until measured.

Rollout

  1. Code-only merge; no config change. Train rides the regular Cloud Run train.
  2. Enable config PR only after a D+2-style baseline at the served revision and a measured caller reads/day.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T20:17:09.205837Z 958b153 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c3c724afa3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2160 to 2165
FINALIZATION_STALE_IN_PROGRESS_QUERY.build(
client.collection_group(CONVERSATIONS_COLLECTION),
{'status': 'in_progress', 'finished_before': finished_before},
field_filter_factory=FieldFilter,
)
.order_by('finished_at')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the admission clock when narrowing recovery candidates

When finjob_candidate_queries is enabled for the optional self-heal sweep, this filter can exclude conversations that the existing admission predicate accepts. recovery_admission_refusal() uses lifecycle_clocks.speech_clock(), which selects last_speech_at for the lifecycle-v2 serving cohort and parses legacy timestamp representations; it does not simply compare the stored finished_at. For example, a conversation with speech three hours ago but a finished_at refreshed by a later empty heartbeat was recoverable through the status-only sweep and now disappears from its candidate window. Preserve a discovery path for rows admitted by the effective speech clock rather than assuming this timestamp predicate is equivalent.

Useful? React with 👍 / 👎.

…hort can exist

Round-2 review finding (exec-worry-wrap-differ-dirt-fault-wolf): upstream
e756385 clocks recovery admission by speech_clock (last_speech_at or
finished_at) for v2-serving uids, and v2 serving suppresses legacy
finished_at writes, so a raw finished_at pushdown would drop rows whose
staleness lives only in last_speech_at. This status-only scan is their only
finished_at-agnostic catch-all, so the candidate shape is now withheld
whenever cohort_configured(LIFECYCLE_V2_MODE) is true (serve mode or a
configured shadow cohort). Dormant shadow (documented default) and off keep
the pushdown: shadow_or_serve would be permanently true under the default
shadow and render the lever inert.

Tests: pushdown withheld under serve/shadow-cohort-percent/shadow-cohort-
digests; active under off and dormant shadow. Targeted shard 509 passed
(test_finjob_candidate_queries, test_selfheal_sweep, test_firestore_query_shapes,
test_firestore_index_oracle, test_firestore_indexes,
test_reconcile_firestore_indexes, test_selfheal_recovery).
@Git-on-my-level
Git-on-my-level merged commit 91ac171 into main Oct 25, 2026
57 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/firestore-reads-wq13-candidates-rb branch October 25, 2026 20:33
Git-on-my-level added a commit that referenced this pull request Oct 26, 2026
…st_index_window to 100 (#23646)

## Summary

Firestore reads loop config: enable the WQ13 `finjob_candidate_queries`
lever at 5%
and graduate `memory_list_index_window` 25 → 100 in one literal (18
keys, generated
with `avo/firestore-reads/set_levers.py`, REPLACE-mode, byte-identical
at all
declaration sites).

- `finjob_candidate_queries`: 0 → 5. The WQ13 rework (#23614, 91ac171)
replaces the
`conversation_finalization_jobs` per-tick full-collection candidate
re-scans with
predicate-equivalent narrow queries behind this default-off lever. The
code is
serving everywhere since tonight's trains (Cloud Run 528f97f 23:17Z,
listen 86a15ac
22:30Z, pusher fca00e4 00:24Z; all contain 91ac171) and all three
composite
indexes it needs are READY live (CICAgJiVqpoK, CICAgJjH44cK,
CICAgPjgiJoK).
Why now: the three finjob candidate scans are the top Firestore read
caller —
~61M reads/h fleet-wide in the 22–23Z hour (snapshot
2026-10-26T00-40Z.json;
  29.0M/h already at 17–18Z pre-serve), concentrated on backend-sync and
backend-sync-backfill. `queue_age_sample_budget` and `singleton_loops`
at 100
since 23:17Z did not collapse them; the re-scan amplifier is the code
path this
  lever narrows.
- `memory_list_index_window`: 25 → 100. verify SUPPORTED at 25 since
2026-10-18
(no FALSIFIED/UNSAFE tick); graduation follows the loop runbook ramp5
rule and
  the c2fs ranked plan (item 5, bundle with the WQ13 enable).

Config only; no code paths change. Off-path for both levers is the
current served
behavior (`finjob_candidate_queries` 0 = byte-identical scans; 25 was
already
SUPPORTED).

## Product invariants affected

- INV-MEM-4 — config-only rollout of default-off code-path levers; every
lever reverts at 0 with no data-path or authority change.
- INV-DATA-1 — value-only edit of the existing `FIRESTORE_READ_LEVERS`
key; no routing or ownership change.

## Test plan

- `backend/tests/unit/test_render_backend_runtime_env.py` +
`backend/tests/unit/test_verify_pusher_config_references.py` via
`backend/test.sh` with `BACKEND_UNIT_TEST_FILE_LIST` (local shard green
on this branch before push).
- Literal verified byte-identical at all declaration sites; regenerated
`runtime_env.yaml`, flag registry, and both pinned contract tests.
- Post-merge watch: verify.py per-lever gates +
backend-sync/backend-sync-backfill 5xx rates hourly; any
UNSAFE/FALSIFIED lever flags off via config PR.

## Rollout

1. Train order **listen → pusher → Cloud Run** (FIRESTORE_READ_LEVERS
order; the Cloud Run candidate check compares GKE env against main's
runtime_env). Handoff to the two-lane lead via bus after merge; the
ramp5 clock starts at the served revision carrying this literal.
2. `finjob_candidate_queries` follows the 5 → 25 → 100 ladder with soaks
between steps (3h soak + verify PASS ×2 per runbook); ramp5 gates decide
each step. memory_list_index_window rides the same literal at 100.
3. D+2 absolute readout per lever vs a fresh post-serve baseline; 0 for
any FALSIFIED/UNSAFE lever.
aryanorastar pushed a commit to Dragonire9/omi that referenced this pull request Oct 26, 2026
…st_index_window to 100

Firestore reads loop config PR (18-key FIRESTORE_READ_LEVERS literal, generated
with avo/firestore-reads/set_levers.py, REPLACE-mode, byte-identical at all
declaration sites):

- finjob_candidate_queries 0 -> 5: first enable of the WQ13 rework from BasedHardware#23614.
  Code serves on all services (Cloud Run 528f97f 23:17Z, listen 86a15ac,
  pusher fca00e4); the three composite indexes are READY (CICAgJiVqpoK,
  CICAgJjH44cK, CICAgPjgiJoK). Pre-enable evidence: finjob candidate scans
  ~61M reads/h fleet-wide at 22-23Z (snapshot 2026-10-26T00-40Z.json), the top
  Firestore read caller; queue_age_sample_budget and singleton_loops at 100
  since 23:17Z did not collapse them.
- memory_list_index_window 25 -> 100: verify SUPPORTED at 25 since 2026-10-18;
  graduation per runbook ramp5 rule and the c2fs ranked plan (bundle with the
  WQ13 enable).

Config only; off-path is the current served behavior for both levers.
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