Repository navigation
fix(backend): fall back to offset read when canonical list scan 503s - #11593
Merged
Merged
Conversation
GET /v3/memories first page 503'd with 'Canonical memory unavailable' whenever the canonical keyset scan failed for any reason, because the #11590 first-page fallback only matched the sibling detail 'Memory cursor unavailable'. The offset read() path does not use the scan, so the first page now falls back to it for both 503 details; unrelated errors still propagate. The scan wrapper also swallowed the underlying exception class/message into the same opaque 503. It now logger.exception's the original error (no uid or memory content) so a Firestore FAILED_PRECONDITION index miss is distinguishable from a uid/cursor ValueError in logs. Co-authored-by: multica-agent <[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.
What changed and why
GET /v3/memoriesfirst page failed with 503Canonical memory unavailableeven though the legacy offsetread()path was healthy. The first-page fallback added in #11590 only matched the detailMemory cursor unavailable; every failure the canonical keyset scan wraps asCanonical memory unavailable(FirestoreFAILED_PRECONDITION, cursor/uidValueError, anything else) bypassed it and failed the whole list endpoint. The fallback now accepts both 503 details, and the scan wrapper (_CanonicalCursorStream._ensure_slots) logs the underlying exception class/message vialogger.exceptionbefore raising the opaque 503, so an index miss is distinguishable from a uid mismatch in dev/prod logs. POST was unaffected because writes do not use the scan.Multica issue: SCA-311.
Product invariants affected
MemoryService.read()— the universal offset read that merges canonical and historical rows through the same suppression/authority checks (_canonical_read+ adaptive historical scan), not a direct legacy reader. A canonical-check failure insideread()itself still fails closed; only the cursor-paging transport degradation (503 cursor/canonical-scan detail) falls back to the offset shape. Same pattern SCA-310: one MEMORY_ENABLED flag for create+list #11590 already shipped for the cursor-secret detail.How it was verified
backend/.venv/bin/python -m pytest tests/unit/test_memories_pagination_clamp.py— 7 passed (4 existing + 3 new).backend/.venv/bin/python -m pytest tests/unit/test_universal_memory_list_cursor.py— 15 passed (14 existing + 1 new).backend/.venv/bin/python -m pytest tests/unit/test_universal_memory_system.py tests/unit/test_memory_service_parity.py tests/unit/test_firestore_indexes.py tests/unit/test_memory_contracts.py tests/unit/test_dev_api_memories_pagination.py— 27 passed.Not verified here: the live dev 503 itself — that needs the deploy plus a real GET once this lands. The new
logger.exceptionwill identify the underlying exception class (FirestoreFAILED_PRECONDITIONvs uid/cursorValueError) the first time the scan path is exercised in dev.Tests
Regression tests that would have caught the bug:
tests/unit/test_memories_pagination_clamp.py::test_first_page_falls_back_to_offset_read_when_canonical_scan_unavailable—read_pageraising 503Canonical memory unavailablenow yields theread()result instead of a 503 (this is exactly the live failure).tests/unit/test_memories_pagination_clamp.py::test_first_page_propagates_unrelated_503_detail— an unrelated 503 detail still propagates; no fallback.tests/unit/test_memories_pagination_clamp.py::test_first_page_propagates_non_503_errors— non-503 errors (e.g. 402) still propagate; no fallback.tests/unit/test_universal_memory_list_cursor.py::test_canonical_scan_failure_logs_underlying_exception_and_503s— the wrap logs the original exception (class + message) and chains it as__cause__on the 503.Failure class (fixes)
Failure-Class: none
No registry class matches the incident shape: a serving fallback that already existed but was keyed to a single sibling 503 detail string, so a same-class degradation failed closed.
FC-undeclared-serving-query-indexwas considered and rejected:UNIVERSAL_CANONICAL_LIST_SCAN_QUERYis deliberately manifest-exempt under the registry's one-non-__name__-field rule (Firestore-managed single-field index + document-ID ordering; same shape as the twomemorieshistorical scans), it is registered in the query inventory contract, and the underlying live exception has not yet been identified — asserting an index class here would be unverified. The newlogger.exceptionmakes that determination possible from dev logs; if it showsFAILED_PRECONDITIONon the scan, a follow-up can declare the class with evidence.Scoped cleanups (optional)
none
Line-Count-Exception: backend/utils/memory/memory_service.py | 2487 -> 2495 | Failure-diagnosis logging (8 lines) at the opaque canonical-scan 503 wrap required by the incident; splitting the 2.5k-line service file is out of scope for this incident fix.