Repository navigation
perf(backend): prevent maintenance scan storms on lever rollback - #23801
Merged
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Git-on-my-level
added a commit
that referenced
this pull request
Oct 27, 2026
## Summary Retain two unsuperseded structural Firestore query optimizations from orphan #23442; merge current main into the existing lane history. The public-catalog and request-cohort cache changes are removed. No query policy, transcription, speaker labels, Free benefits, legacy price, raw-audio custody or authority checks change. - **Smart merge:** use the declared device composite only for explicit-device, unbounded discarded-barrier lookups. Keep the global finite six-row predecessor window, missing/empty-device behavior, deleted barriers, transaction snapshot and unrelated-error propagation. Missing-index errors fall back to the original query. Device or citation IDs exceeding 1,500 UTF-8 bytes keep their original scans because Firestore truncates indexed field values; no new identifier input restriction is introduced. - **KG pruning:** query native `memory_ids` reverse citations for at most **30 distinct IDs** before mutations. Larger retractions keep one original full scan so overlapping chunks cannot amplify billed reads. Retain the full surviving-node and orphan-edge cleanup, including unrelated dangling edges. No side collection, backfill or new application writes. - Independent switches default on; `FSR_SMART_MERGE_PARTITION=off` and `FSR_KG_CITATION_INDEX=off` restore their original paths. - Main's new donor rebasing, open-loop/retirement behavior, #23797's scoped literals, #23801's maintenance guard and #23803's collector changes are preserved. ## Four-part decision / economics 10-26 fleet caller data already captured in the fsown report; $0.43–0.47 per million reads × 30 days. These are **opportunity ceilings, not forecasts or booked savings**. | Original part | Observed caller reads/day | Monthly evidence | Result | | --- | ---: | --- | --- | | Smart-merge device barrier | 16,749,388 | Entire pool $216.07–236.17; upside capped at $236.17, eligible barrier fraction/off-device selectivity unknown | KEEP; main still scans broadly then filters device in Python | | KG reverse citations | 8,915,616 | Entire pool $115.01–125.71; first-scan-only upside capped at $62.86. Fixture-equivalent $55.83–61.02 only if its graph shape holds fleet-wide | KEEP; main still performs the first full node scan; bulk traffic remains legacy | | Public catalog single-flight | 1,538,759 | Entire pool only $19.85–21.70; incremental miss coalescing unmeasured; $0 assigned | DROP; main already has 30s list single-flight and 600s Redis backing; limited upside does not justify extending shared-cache dependency | | Request cohort snapshot | 2,616,546 (old + renamed callers) | Entire pool $33.75–36.89; $0 additional assigned | DROP; main already shares observations and clamps their original 60s deadline, while this request memo did not recheck expiry | Retained gross upside cap **$299.03/month**, not a point forecast. Composite storage, empty-query floors, fallback overhead and the actual request/graph mix remain unmeasured; net savings may be zero or negative. **New verified/realized savings: $0**, until lead serving plus D+1 net billing against the 10-24/25 baseline. Track old and new callers together: `smart_merge:find_preceding_conversations` → `smart_merge:collect` (including unchanged finite reads); `knowledge_graph:prune_memory_citations_from_kg` plus `knowledge_graph:_citation_nodes`. New origins are `task:smart_merge_partition_lookup` and `task:kg_citation_prune`. Caller renaming is not saving. ## Integration and validation - Merge commit `f923e4862f1240b52c429f5705f091558f2a90f3` parents: existing PR head `789db126e73be7c01465af90e4f7f5277c3fce56` and main `5f2caea10a67fcd2dbc52347aff08c9f0ac5fdab`. Follow-up `f240ea8dbe01b2f061fbbb9aa5ce56be6f75070c` caps bulk KG reads. Final source `4174839ab5f0c0bc06eda0a4e81b96b66931980a` preserves scans for identifiers beyond index bounds, with ASCII and multibyte boundary tests. No rebase/force-push/stash. Original divergent local lane work is preserved. - **1,019 passed**, one standard slow deselection, across an explicit 14-file list through `backend/test.sh`, `BACKEND_UNIT_TEST_FILE_LIST`, `BACKEND_PYTEST_WORKERS=4`, `nice -n10` and `omi-heavy`. Includes all seven Firestore guards, offline SDK query serialization, global-window/deleted-barrier/transaction fallback, finalizer/donor integration, identical KG cleanup, and bulk read non-amplification. - Synthetic mechanisms: indexed barrier 121→1 returned rows with the same relevant barrier; KG 206→106 reads with identical final graph; bulk KG remains 4→4 reads. None establishes fleet savings. - Own Luna review: SHIP on f240; final index-bound delta review and normal pre-push/CI receipts are tracked at the top of `reports/fsown-owner-1027.md` before MERGE READY. ## Lead-owned index / ship gate A ro-prod **metadata-only** check found 221 composites and **no matching device index**. The lead must clear the standard additive index prebuild auth/environment approvals and verify `conversations_smart_merge_device_preceding` READY before serving this source. Production prebuild approval is a lead gate, not lane authority. Do not ship through the missing-index fallback to bypass the readiness gate. Native KG array-index availability also retains the standard guard/fallback contract. Once readiness and source CI are clean, the lead merges and ships through its normal source train, watches missing-index/fallback/query latency, smart-merge barrier equivalence, KG cleanup, 5xx and live p95, and rolls either switch off on a regression. Compare matched caller windows and D+1 total reads/writes/storage before booking dollars. This lane performs no deploy/apply, prod write, approval or access change, and does not close the PR. ## Product invariants affected - INV-MEM-4 — graph pruning remains derived cleanup; privacy/deletion authority and authoritative promotion/funding checks retain their existing paths. Line-Count-Exception: backend/database/firestore_index_registry.py | 2247 -> 2266 | Declare the partition query beside the original query in the single index authority; preserve generated manifest ownership. Failure-Class: FC-merge-across-discarded-rows
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
During the 10-22/25 Firestore surges, maintenance scans multiplied across replicas; the 10-25 spike followed a lever rollback. The 10-22 exact revision trigger remains unproven. Day-aligned counters attribute 576M and 510M extra reads to the queue sampler and three finalization sweeps, rather than sync-job redrive processing. Keep the existing maintenance admission lease on by default in production independently of the
singleton_loopscohort, withFSR_MAINTENANCE_COORDINATION_ENABLED=falseas the explicit coordination kill switch.Redis keys/Lua, scan cadence, takeover bounds, local recovery on Redis failure, transactional job claims and designated queue publisher are unchanged. Non-production, including stage-prod staging, retains legacy cohort qualification unless explicitly overridden. Other Firestore levers retain their current behavior. No runtime-env/config literal, index, workflow, account-data or retention changes.
This is a recurrence guard: current singleton100 already coordinates production, so steady-state new realized savings are $0. Comparable observed scanner regrowth is about $219–271 per incident at $0.43–0.47/M; recurrence frequency and monthly avoided spend are unknown. WQ21 privacy memo and WQ24 funded aggregate shortcuts are excluded because their correctness gates did not pass; WQ22 is already in #23686. No partial authorization/quota cache ships.
Validation
BACKEND_UNIT_TEST_FILE_LIST=/Volumes/Ephemeral/scratch/fsown/scan-tests.txt BACKEND_PYTEST_WORKERS=4 nice -n 10 bash backend/test.sh, through workspacebin/omi-heavy: 68 passed across the scan-storm guard, existing singleton/recovery and Redis Lua suites.make preflight: 36 checks passed (366.85s). Exact-head CI is verified separately before MERGE READY.exec-yard-device-motion-guard-episode-voyage, continuation keyfsown-luna-delta-1027a).Ship and rollback (lead only)
No merge/deploy/apply by this lane. No apply request during the batch entry/soak through ~11:30Z. Lead deploys source in its allowed train, first verifying healthy designated
FSR_QUEUE_AGE_PUBLISHER=1and fresh queue-age samples. Compare lease outcomes, finalization recovery/queue freshness, errors and live p95 before/after; verify the runtime-reported release SHA for each serving artifact. SettingFSR_MAINTENANCE_COORDINATION_ENABLED=falsevia the regular lead-owned runtime procedure restores prior per-replica execution and can raise spend. Seebackend/docs/runbooks/firestore-spend-guard.md.#23797 remains owned by the lead and untouched. Finjob5→100 requires a separate complete-literal lead config change after soak; READY index metadata alone is not a production apply.
Product invariants affected
none
Failure class
Failure-Class: none
CR train #2 integration (2026-10-27)
Merged
origin/mainat8da0ca9e1b7867cb9719a33225c3d9371713356aintolane/fsownwith merge commiteb963fcfca4106bdc8c718f7c308624116c3034a; no rebase or force-push. The two conflicts were documentation/registry additions: retain fleet coordination and main's server-batch documentation, new batch flags and newer default-off Soniox idle-reselect declaration; regenerate the registry. No production-code conflict. #23797's scoped literals and #23803's metrics collector remain identical to main.Affected tests through
backend/test.sh, explicit nine-fileBACKEND_UNIT_TEST_FILE_LIST,BACKEND_PYTEST_WORKERS=4, undernice/omi-heavy: 458 passed, one slow test deselected by the standard harness. Final exact-head CI andmergeable,mergeStateStatus,headRefOidare recorded inreports/fsown-owner-1027.md. Lead retains merge/deploy/apply authority.