Skip to content

fix(backend): bound the memory list page's suppressed-row walk (#11798) - #11800

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/issue-memories-scan-row-budget
Aug 18, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/issue-memories-scan-row-budget

Conversation

@kodjima33

@kodjima33 kodjima33 commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #11798.

Bug

GET /v3/memories returned 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 revision backend-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_page merges 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 a while True that refills 50 rows per Firestore round trip and skips each canonical_identity / canonical_state suppressed row, unbounded. For an account whose historical rows are all suppressed by canonical — the normal end state of the canonical migration — a limit=8 first 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 returned FAILED_PRECONDITION, read_page raised Historical memory unavailable, and the route fell back to the offset read() — which does not walk suppressed rows. That is why the 504 rate jumped the moment #11791's 503s cleared.

Fix

One _ScanRowBudget per read_page call, 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), so routers/memories.py serves the page from the offset read() instead of hanging until the edge timeout. Accounts under the budget are byte-for-byte unchanged.

What I tested

  • backend/test.sh over 83 memory-suite files (tests/unit/test_*memor* plus the two changed suites): all green, exit 0.
  • All three new tests fail on clean origin/main (verified by stashing the two source hunks) and pass with the change.
  • Real user-facing path: drove the actual FastAPI get_memories route with the real MemoryService over 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 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 through MemoryService, and the first page falls back to MemoryService.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 suites test_inv_mem_1_guard.py, test_universal_memory_service.py, test_universal_memory_task_authority.py, test_ws_l_surface_routing.py all 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

Review in cubic

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
kodjima33 merged commit 1d874e8 into main Aug 18, 2026
43 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-memories-scan-row-budget branch August 18, 2026 06:40
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GET /v3/memories 504s at the 30s edge timeout: the first page walks every canonical-suppressed historical row

1 participant