Repository navigation
Description
Summary
Dev Cloud Run backend request logs on 2026-07-13 confirmed three user-facing 5xx families caused by Firestore failures. The failures predate the unrelated apps-js deletion, and the historical backend revision was Cloud Run Ready; this is not supported as an apps-js or failed-rollout regression.
This issue groups the shared prevention gap: serving Firestore query and transaction contracts are not consistently validated from source through deployed datastore readiness.
Confirmed dev evidence (redacted)
| Route | Status | First failing boundary | Confirmed error class |
|---|---|---|---|
/v1/what-matters-now |
500 | database.task_recommendations.list_active_override_dedupe_keys |
Firestore FailedPrecondition: required composite index missing |
/v1/action-items write path |
500 | database.action_items.create_in_generation |
Firestore Aborted: cross-transaction contention |
/v3/memories/batch legacy path |
503 | database.memory_ledger._append_commit_with_builder_transaction |
Firestore Aborted: cross-transaction contention |
Observed route errors existed before the cutover timestamp across earlier backend revisions. Do not attribute them to apps-js removal without a demonstrated call-path dependency.
Root causes
1. Missing index declaration/deployment contract
The What Matters Now query combines account_generation == with expires_at > on task_attention_overrides. It requires a composite index, but the checked-in firestore.indexes.json contains no task_attention_overrides entry.
firebase.json points to the index manifest, but current repository CI/deploy workflows do not automatically reconcile indexes or wait for required indexes to reach READY before backend traffic shifts. Existing index tests validate selected hand-maintained entries only; the recommendation tests mock the failing datastore helper and therefore do not execute the real query.
2. Unprotected transactional writes
Action-item creation and legacy memory-ledger commits allow retryable Firestore Aborted contention to escape their shared write boundary. A later retry/503-normalization implementation exists in repository history but is absent from the affected dev revision.
Desired durable contract
Build a repository-local Firestore query coverage tool and progressive migration path. It must make query/index coverage visible and prevent new serving-query debt while avoiding a big-bang rewrite.
Query inventory and ratchet
- Add a deterministic AST-based inventory for serving Firestore query shapes. It must report query shape (not raw
.where()call count), file/line, collection scope when known, fields/operators/order, classification, index signature when determinable, and registration status. - Support human and machine output, e.g.
reportandreport --format json, with counts for registered, raw-unregistered, waived, unsupported, and non-serving queries. - Treat migrations, admin scripts, and tests as separately reported non-serving scope; do not let them inflate serving coverage.
- Establish a committed baseline and a CI ratchet: no newly introduced or modified raw serving compound query may merge unregistered; no new waiver without owner/expiry; coverage may not decrease.
- Define 100% honestly: all eligible serving query shapes are registered, with no active raw, waiver, or unsupported serving entries.
Generated index contract
- Introduce a deliberately narrow shared query-spec layer for serving compound queries. It must construct the real Firestore query and expose a normalized query/index requirement.
- Generate the canonical existing Firebase-format
firestore.indexes.jsonfrom registered query specs, and fail CI on manifest drift. - Add a narrow static guard preventing new direct compound Firestore query chains outside the query-spec layer (with explicit, reviewed non-serving exceptions).
- Migrate the failing
task_attention_overrides(account_generation, expires_at)query first, then migrate serving queries incrementally using the inventory/scaffold output.
Deployment and real-path verification
- Before dev traffic shift, reconcile the generated index manifest using the supported Firebase/GCP tooling and poll each required composite index until
READY. - Add a controlled post-deploy smoke for
/v1/what-matters-nowthat exercises the real datastore query and fails on 5xx. - Add the equivalent readiness gate to any future promotion path before traffic moves.
Transaction contract
- Route serving transactional writes through the shared bounded fresh-transaction retry helper.
- Add fault-injection coverage for
google.api_core.exceptions.Aborted: transient abort then success, exhaustion, fresh transaction per retry, and documented client-safe exhausted-retry mapping. - Inventory direct serving transactions and apply the same no-regression ratchet until all are protected.
Acceptance criteria
-
task_attention_overridesrequired composite index is generated/declared and is verifiedREADYbefore the affected dev route receives traffic. - CI catches a new or modified unregistered serving compound query before merge.
- CI fails if generated index output and checked-in manifest diverge.
- Coverage report exposes current counts and a zero-debt path to 100% serving-query registration.
- Dev deployment validates required index readiness before traffic shift, not only Cloud Run readiness.
- The What Matters Now real-query smoke runs after deploy and would fail on the missing-index condition.
- Action-item and legacy memory transaction paths have bounded retry/error-mapping coverage for Firestore contention.
- Existing route behavior remains covered by focused tests; no customer content, IDs, tokens, or request bodies appear in CI artifacts or issue comments.
Scope / non-goals
- Do not make any production configuration, index, IAM, or traffic change as part of initial implementation without separate approval.
- Do not claim production impact from dev-only evidence. Production status needs a separately authorized read-only check.
- Do not add an unmaintained third-party index-generator dependency. Reuse
firebase-tools/Firestore APIs for deployment and readiness; keep source-query contract logic small and repository-owned.
Activity
feat(backend): prevent Firestore query contract regressions (#9668)