Skip to content

GET /v3/memories 503s in prod: the canonical scan orders updated_at DESC + __name__ ASC, an index Firestore never builds automatically and the registry never declares #11684

Closed

Description

@kodjima33

GET /v3/memories has been answering 503 "Canonical memory unavailable" in prod since the canonical scan shipped on 2026-08-12 (efba1a0, "fix: finish combined proactive QA hardening"). The canonical list scan asks Firestore for an index that has never been declared, so every page-1 read fails at query planning.

Evidence (prod, based-hardware)

gcloud logging read severity>=ERROR over the last 24h: 915 of a 1000-record sample are this one traceback.

File "/app/utils/memory/memory_service.py", line 1105, in _ensure_slots
  raw_slots, stream_exhausted = read_canonical_scan_page(
File "/app/utils/memory/canonical_memory_adapter.py", line 623, in read_canonical_scan_page
  snapshots = list(query.limit(bounded_limit).stream())
google.api_core.exceptions.FailedPrecondition: 400 The query requires an index.

The remediation link in that error decodes to exactly:
memory_items COLLECTION — updated_at DESCENDING, __name__ ASCENDING.

memory_service.py:1116 maps that exception to HTTPException(503, "Canonical memory unavailable"), so users of GET /v3/memories (routers/memories.py:513) get a 503, not a degraded list.

Root cause

read_canonical_scan_page orders updated_at DESC, __name__ ASC (canonical_memory_adapter.py:612). That tie-break direction is deliberate and correct — MemoryService.keyset_sort_key is (-updated_at_us, memory_id) (memory_service.py:1597), so the whole service's public order is updated_at DESC with ascending document id. The emitted-keyset monotonicity filter (memory_service.py:1153) and the canonical/historical merge (:1725) both depend on the Firestore scan matching that order. Flipping __name__ to DESC would be the wrong fix.

The index is missing because the registry believes Firestore provides it automatically:

# backend/database/firestore_index_registry.py:332
# Firestore manages the single-field updated_at index (plus automatic __name__
# tie-break) itself — this spec records the serving query contract only.

Firestore's automatic single-field indexes serve field ASC, __name__ ASC and field DESC, __name__ DESC only. field DESC, __name__ ASC is a different index that is never built automatically. INDEX_REQUIREMENTS (:600) drops any spec with one non-__name__ field from the manifest, so four specs silently never reach firestore.indexes.json:

spec index live in prod?
memory_items_universal_list_scan memory_items updated_at DESC, __name__ ASC missing — this outage
memories_universal_list_scan_updated_at memories updated_at DESC, __name__ ASC missing
memories_universal_list_scan_created_at memories created_at DESC, __name__ ASC missing
memory_review_queue_by_status_id memory_review_queue status ASC, __name__ DESC missing

Verified against gcloud firestore indexes composite list --project based-hardware (89 indexes): none of the four exist.

test_firestore_index_config.py:30 encodes the same wrong assumption ("rejected at deploy time as redundant"), which is why no test caught it.

The assumption is wrong — measured, not argued

I created the exact index in based-hardware-dev and Firestore accepted it (it is not rejected as redundant), then A/B'd the real serving query:

# index CREATING
FAILED_PRECONDITION based-hardware-dev: 400 ... That index is currently building and cannot be used yet
# index READY
OK based-hardware-dev: query planned and ran
# prod control, index still absent
FAILED_PRECONDITION based-hardware: 400 The query requires an index.

Firestore itself matched my created index to the query — confirming both the shape and that it is not automatic.

Fix

Narrow the manifest predicate to what Firestore actually rejects: a single ordered field is automatic only when __name__ tie-breaks in the same direction. Patch (registry + regenerated firestore.indexes.json + the two tests), 4 files / +122 / -13, is ready and green:

  • backend/tests/unit/test_firestore_indexes.py, test_firestore_index_config.py, test_reconcile_firestore_indexes.py — 34 passed
  • backend/tests/unit/test_firestore_query_contract.py — 26 passed
  • backend/scripts/generate_firestore_indexes.py verify — clean; adds exactly the 4 indexes above, removes none

Why this is not a drive-by PR — sequencing needs a human

firestore_readiness runs reconcile_firestore_indexes.py --check-only in both gcp_backend.yml and gcp_backend_auto_dev.yml. Merging the manifest change before the indexes exist blocks backend deploys (prod and auto-dev) until the create-only migration runs. The correct order is:

  1. Apply the four indexes (Reconcile Firestore Indexes → prod → APPLY_FIRESTORE_INDEXES). This alone stops the 503s — the serving code needs no change.
  2. Then merge the registry/manifest patch so the declaration matches the deployed schema and the regression tests hold.

That workflow is workflow_dispatch with an explicit confirmation string and the prod environment requires reviewers — deliberately a human-approved writer — and AGENTS.md puts schema migrations in the explicit-sign-off bucket. So this is filed rather than pushed.

Follow-ups

  • validate_account_deletion_dispatch_configuration-style gap: nothing asserts a declared serving query is actually servable. A startup or readiness probe that plans each QUERY_SPECS entry once would have caught this on the deploy that shipped it.
  • Separate live error, same subsystem, 29 occurrences/24h: ValueError: canonical memory extraction returned evidence without a unique source binding (process_conversation.py:918) escaping _run_enrichment.

🤖 filed by the hourly desktop bug watchdog

Activity

  1. added
    bug
    on Aug 16, 2026
  2. aryanorastar commented on Aug 16, 2026

    @aryanorastar
    Collaborator

    Opened #11687 with the registry predicate fix + regenerated firestore.indexes.json + regression tests (61 unit passed locally).

    Per the sequencing in this issue: APPLY the four indexes in prod before merging the PR, otherwise backend deploy readiness fails closed. Happy to adjust the PR if APPLY needs a different shape.

  3. Git-on-my-level commented on Aug 17, 2026

    @Git-on-my-level
    Member

    Additional customer path from the 2026-08-17 prod 5xx surge

    Read-only prod check on based-hardware, Cloud Run backend revision backend-memon0815 / commit 6316415534fffc4636efdab224a49a3638cf046f (100% traffic).

    The 03:12–03:28Z watchdog tick (342 backend HTTP ≥500s) was mostly DELETE /v1/conversations/{id}?cascade=true → 503 from Dart/Flutter (268 of 345 sampled 5xxs; prior tick 02:56–03:12 had 303 DELETE 503s). Same FailedPrecondition: query requires an index on memory_items updated_at DESC + __name__ ASC via read_canonical_scan_page / _ensure_slots.

    On this serving SHA, cascade delete calls retraction_can_be_skipped → source_retraction_is_a_noop, which can iter_all_live the historical/canonical scan when deciding whether retract is a no-op. That is the same opposite-direction __name__ scan class this issue already describes for GET /v3/memories. Endpoint difference does not change the repair: APPLY the four indexes still stops these 503s.

    Not claiming this issue owns the concurrent retract 500s in the same window (RuntimeError: canonical conversation replacement conflicted repeatedly). Those are a separate CAS/idempotency class and will be filed separately.

    No uid / conversation ids / payloads retained.

  4. Git-on-my-level commented on Aug 17, 2026

    @Git-on-my-level
    Member

    Follow-up for the same window's DELETE 500 CAS/retract class (not this index): #11726

  5. kodjima33 commented on Aug 17, 2026

    @kodjima33
    CollaboratorAuthor

    Ops remediation applied — the outage is over, the registry gap is not.

    The missing serving index was created directly in prod at 2026-08-17T21:39Z and reached READY at 21:44:07Z:

    projects/based-hardware/databases/(default)/collectionGroups/memory_items/indexes/CICAgLiTzYUK
    COLLECTION | updated_at DESCENDING, __name__ ASCENDING | READY
    

    Exactly the shape this issue decoded from the remediation link, and Firestore accepted it (not rejected as redundant), confirming the "automatic single-field index covers it" assumption in firestore_index_registry.py:332 is wrong.

    Live effect, severity>=ERROR on based-hardware:

    window The query requires an index
    21:00:00Z → 21:44:07Z (index building) 500+ (sample cap)
    21:44:10Z → 21:44:54Z (propagation tail) 14
    21:44:54Z → 21:55:00Z 0

    GET /v3/memories stopped 503ing. The three other specs in the table above (memories updated_at DESC, memories created_at DESC, memory_review_queue status ASC/__name__ DESC) produced no prod errors in the last 10h, so I left them alone.

    Still needed: this index now exists only as a hand-created live index and is absent from firestore.indexes.json, which is the FC-undeclared-serving-query-index failure class itself. The registry fix (_query_spec_index_requirements dropping specs with one non-__name__ field) is open in #11687 and #11731 — one of those still has to land, or the next environment rebuilt from the manifest breaks the same way.

    🤖 hourly desktop bug watchdog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions