Skip to content

fix(backend): bound the memory list page's suppressed-row walk in seconds, not only rows (#11803) - #11805

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

kodjima33 merged 1 commit into
mainfrom
watchdog/issue-11803-memories-scan-deadline

Conversation

@kodjima33

@kodjima33 kodjima33 commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

GET /v3/memories still 504s at the 30s edge timeout after PR #11800's scan row budget reached prod. I dispatched the prod deploy of 922b30db1c this morning (backend-922b30d-32113104414-1 took 100% traffic at ~08:09Z); the 504 rate fell from ~110/h to ~40/h but did not go to zero — still first pages only (offset=0, limit 8/100/500/5000), while successful first pages run at p90 5–11s with maxima of 26–28s.

('8',    'off0', 504) n=4 p50 30.01 max 30.36
('100',  'off0', 504) n=3 p50 30.00
('5000', 'off0', 504) n=3 p50 30.00
('8',    'off0', 200) n=185 p50 0.72 p90 10.84 max 26.59

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. The request 504s anyway, which is exactly the failure class the bound was meant to close.

Fix

Give the shared per-request _ScanRowBudget 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. The clock is injectable so the deadline is asserted without sleeping.

Tested

  • backend/test.sh over all 85 *memor* test files (BACKEND_UNIT_TEST_FILE_LIST) — green, no failures.
  • The two new tests fail on clean origin/main with the source hunk stashed (TypeError: _ScanRowBudget.__init__() got an unexpected keyword argument 'deadline_seconds'; the deadline test otherwise scans past the row budget) and pass with it.
  • make preflight — green with the line-count exception below.
  • Post-merge: I will deploy prod and confirm the /v3/memories 504 rate goes to zero on the new revision.

Product invariants

  • INV-MEM-1, INV-MEM-3 — path-glob match on backend/utils/memory/memory_service.py. The diff changes no tier vocabulary, no collection layout, no exposure policy and no ordering: it only bounds how long one page may walk rows it must not emit before falling back to the read path that already serves the same page.

Failure-Class: FC-bounded-read-exceeds-request-budget

Line-Count-Exception: backend/utils/memory/memory_service.py | 2540 -> 2558 | one wall-clock bound plus its incident comment inside the existing scan-budget class; splitting the file is not in scope for a live-504 fix

Fixes #11803

🤖 automated by hourly watchdog — tested and merged

Review in cubic

…onds, not only rows

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 still 504s after the scan row budget: 4000 skipped rows can eat the whole 30s edge budget

1 participant