Repository navigation
fix(backend): bound the memory list page's suppressed-row walk (#11798) - #11800
Merged
Merged
Conversation
GET /v3/memories 504'd at the 30s edge timeout in prod on 2026-08-18 — ~100/h from 04:00Z, first pages only (offset=0, limit=8/100/5000), across ~10 client IPs an hour, overwhelmingly the Windows desktop app. Root cause: read_page's keyset streams walk past every row they must not emit before they can fill the page. For an account whose historical rows are all suppressed by canonical, a limit=8 first page scans the entire historical collection at 50 rows per Firestore round trip. Measured on the production route: 8,000 suppressed rows = 161 sequential round trips, so a 30k-row account needs ~600 — past 30s at ordinary Firestore latency. The walk only started serving on 2026-08-17T21:36Z when the `memories` (updated_at DESC, __name__ ASC) composite index went READY; before that the scan failed fast and the route fell back to the offset read, which is why the 504 rate jumped 10x the moment the 503 outage (#11791) cleared. Bound the skipped work with one scan row budget per read_page call, shared by the canonical and historical streams. Exhausting it raises the existing first-page fallback contract ("Memory scan budget exceeded"), so the route serves the page from the offset read — which does not walk suppressed rows — instead of hanging until the edge timeout. Verification: backend/test.sh over 83 memory-suite files = all green. Both new service tests and the new route test fail on clean origin/main (verified by stashing the two source hunks). Drove the real FastAPI route with a fully suppressed 8,000-row account: origin/main takes 161 Firestore round trips and returns an empty list; with this change the walk stops at 81 round trips and the request is served by the offset read. Failure-Class: FC-bounded-read-exceeds-request-budget Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
kodjima33
added a commit
that referenced
this pull request
Aug 18, 2026
…onds, not only rows (#11805) GET /v3/memories still 504'd at the 30s edge timeout after the row budget from #11800 reached prod (deployed 2026-08-18T08:09Z): ~40/h, first pages only (offset=0, limit=8/100/500/5000), while successful first pages ran at p90 5-11s with maxima of 26-28s. Root cause: MEMORY_LIST_SCAN_ROW_BUDGET = 4000 bounds rows, not seconds. 4000 skipped rows is ~80 sequential Firestore round trips, so a slow account can spend most of the 30s request budget inside the bound — and the offset-read fallback the budget exists to reach then has no time left to answer, so the request 504s anyway. Give the shared per-request budget a wall-clock deadline (6s) alongside the row bound. Exhausting either raises the same "Memory scan budget exceeded" 503 the route's first-page fallback already keys on, so the page is served from the offset read — which does not walk suppressed rows — with the bulk of the request budget still unspent. Verification: backend/test.sh over all 85 memory test files = green (BACKEND_UNIT_TEST_FILE_LIST). Both new tests fail on clean origin/main with the source hunk stashed (TypeError: no deadline_seconds; the deadline test scans past the row budget) and pass with it. Failure-Class: FC-bounded-read-exceeds-request-budget Fixes #11803
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.
Fixes #11798.
Bug
GET /v3/memoriesreturned 504 at exactly 30.005s in prod at ~100/hour from 2026-08-18T03:00Z (up ~10x from a ~10/hour baseline). Every one was a first page —offset=0,limit=8/100/5000, no cursor — from ~10 distinct client IPs an hour, overwhelmingly the Windows desktop app. Still reproducing on the current revisionbackend-523d194-32102746612-1, so not the stale image and not the #11791 503 outage (cleared 03:18:40Z). Successful requests on the same endpoint were at p95 11.79s.Root cause
MemoryService.read_pagemerges a canonical and a historical keyset stream. Both walk past every row they must not emit before they can fill the page:_HistoricalRawStream.peek()is awhile Truethat refills 50 rows per Firestore round trip and skips eachcanonical_identity/canonical_statesuppressed row, unbounded. For an account whose historical rows are all suppressed by canonical — the normal end state of the canonical migration — alimit=8first page scans the entire historical collection.Measured against the real route: 8,000 suppressed rows = 161 sequential Firestore round trips, so a ~30k-row account needs ~600 — past 30s at ordinary Firestore latency.
The walk only started serving on 2026-08-17T21:36Z, when the
memories(updated_at DESC, __name__ ASC)composite index went READY. Before that the scan returnedFAILED_PRECONDITION,read_pageraisedHistorical memory unavailable, and the route fell back to the offsetread()— which does not walk suppressed rows. That is why the 504 rate jumped the moment #11791's 503s cleared.Fix
One
_ScanRowBudgetperread_pagecall, shared by the canonical and historical streams and charged once per skipped row. Exhausting it raises the existing first-page fallback contract (Memory scan budget exceeded), sorouters/memories.pyserves the page from the offsetread()instead of hanging until the edge timeout. Accounts under the budget are byte-for-byte unchanged.What I tested
backend/test.shover 83 memory-suite files (tests/unit/test_*memor*plus the two changed suites): all green, exit 0.origin/main(verified by stashing the two source hunks) and pass with the change.get_memoriesroute with the realMemoryServiceover a fully suppressed 8,000-row account.origin/maintakes 161 Firestore round trips and returns an empty list; with this change the walk stops at 81 round trips and the request is served by the offset read in 0.13s.make preflight: pass.Still true after this PR
The budget still allows ~80 sequential round trips (~4s) before falling back, so these accounts stay slow, just no longer past the timeout. The underlying shape — a merged list whose historical half is entirely suppressed — deserves a cleanup of the suppressed rows; tracked on #11798.
Product invariants
INV-MEM-1— tiering and default access policy are untouched; the budget only bounds how many rows a page may skip.INV-MEM-3— the universal authority still fails closed. Budget exhaustion raises a 503 throughMemoryService, and the first page falls back toMemoryService.read, the same authority-owned offset path the three existing scan-failure details already fall back to. No direct legacy reader, no suppression check skipped. Guard suitestest_inv_mem_1_guard.py,test_universal_memory_service.py,test_universal_memory_task_authority.py,test_ws_l_surface_routing.pyall pass.Line-Count-Exception: backend/utils/memory/memory_service.py | 2495 -> 2540 | scan row budget plus the incident comment that explains why the bound exists
Failure-Class: FC-bounded-read-exceeds-request-budget
🤖 automated by hourly watchdog — tested and merged