Skip to content

fix(backend): read the historical memory prefix in one query, not an offset walk (#11803) - #11819

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/issue-11803-historical-prefix-offset-walk
Aug 18, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/issue-11803-historical-prefix-offset-walk

Conversation

@kodjima33

@kodjima33 kodjima33 commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

GET /v3/memories still 504s at the 30s edge on the current prod image (backend-2976cc3, which carries PR #11816): ~20/h between 2026-08-18 13:30Z and 14:20Z, across the Windows desktop app, iOS (Omi/12187) and Python API clients, on both limit=5000&offset=0 (the first page's documented scan-budget fallback) and the offset>0 pages. Continuation of #11803.

Root cause

The remaining cost is what Firestore reads, not what the page returns.

get_memories(sort="updated_or_created_desc") has no index to order by — updated_at is absent from a material slice of the collection — so it streams two candidate windows of limit + offset documents and applies the final order in Python. Every row an offset skips is still read and billed.

HistoricalMemoryAdapter.read walked the prefix in MAX_PAGE_SIZE chunks with a growing db_offset, so each chunk rescanned everything the previous chunks had already scanned: O(prefix² / page) instead of O(prefix).

Measured on a fully suppressed 5,000-row account, one page:

queries documents scanned
before 25 105,000
after 5 25,000

Fix

Ask for the whole prefix in one query. needed is already bounded by MAX_COMPATIBILITY_WINDOW, and each chunked query's candidate window had grown to that size anyway, so a single query reads no more at once than the last chunked query already did. MAX_PAGE_SIZE keeps its real job as the API page cap.

The guard added with #11816 counted only the rows a query returned, which is why a 105,000-read page passed it at 12,500. It now charges the scanned candidate windows, and fails on the chunked walk.

Also a direct contributor to the standing "Firestore Document Reads exceeded 10k/s" alerts (#11282, #10950, #10817).

Tested

  • backend/test.sh over the 84 memory test files: 856 passed, exit 0.
  • The updated cost guard fails on the chunked walk (10,500 > 3,000 scanned at test scale) and passes with the fix (2,500).
  • Page-content equivalence checked against the pre-fix chunked walk over 25 scenarios — suppression density 0 / 33% / 100%, malformed rows forcing over-fetch continuation rounds, offsets 0 through the window edge — identical ids in every case.
  • Not reproducible against the real prod account, so the prod claim is the measured read/round-trip reduction, not an observed sub-30s page. Watchdog re-checks the live 504 rate next hour.

Line-Count-Exception: backend/utils/memory/memory_service.py | 2587 -> 2595 | 8-line comment recording why the prefix must be fetched in one query, so the offset walk is not reintroduced

Product invariants affected: INV-MEM-1, INV-MEM-3 — behavior unchanged (page content proven identical across 25 scenarios); this is a read-cost fix only.
Failure-Class: none

🤖 automated by hourly watchdog — tested and merged

Review in cubic

…offset walk (#11803)

GET /v3/memories still 504s at the 30s edge on the current prod image
(backend-2976cc3, which carries PR #11816): ~20/h over 2026-08-18 13:30Z
to 14:20Z, on the Windows desktop app, iOS and Python API clients, on
both `limit=5000&offset=0` (the first page's documented scan-budget
fallback) and the `offset>0` pages.

The remaining cost is what Firestore *reads*, not what the page returns.
`get_memories(sort="updated_or_created_desc")` has no index to order by —
`updated_at` is absent from a material slice of the collection — so it
streams two candidate windows of `limit + offset` documents and applies
the final order in Python. Every row an offset skips is still read and
billed. `HistoricalMemoryAdapter.read` walked the prefix in MAX_PAGE_SIZE
chunks with a growing `db_offset`, so each chunk rescanned everything the
previous chunks had already scanned: O(prefix^2 / page) instead of
O(prefix).

Measured on a fully suppressed 5,000-row account, one page:

  before  25 queries / 105,000 documents scanned
  after    5 queries /  25,000 documents scanned

Ask for the whole prefix in one query instead. `needed` is already
bounded by MAX_COMPATIBILITY_WINDOW, and each chunked query's candidate
window had grown to that size anyway, so a single query reads no more at
once than the last chunked query already did. MAX_PAGE_SIZE keeps its
real job as the API page cap.

The guard added with #11816 counted only the rows a query returned, which
is why a 105,000-read page passed it at 12,500. It now charges the
scanned candidate windows, and fails on the chunked walk (10,500 scanned
at test scale, budget 3,000).

This is also a direct contributor to the standing "Firestore Document
Reads exceeded 10k/s" alerts (#11282, #10950, #10817).

Failure-Class: none

Verification:
- backend/test.sh over the 84 memory test files: 856 passed, exit 0.
- The updated cost guard fails on the chunked walk (10,500 > 3,000
  scanned) and passes with the fix (2,500).
- Page-content equivalence checked against the pre-fix chunked walk over
  25 scenarios — suppression density 0/33%/100%, malformed rows forcing
  over-fetch continuation rounds, offsets 0 through the window edge —
  identical ids in every case.
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.

1 participant