Repository navigation
fix(backend): read the historical memory prefix in one query, not an offset walk (#11803) - #11819
Merged
kodjima33 merged 1 commit intoAug 18, 2026
Merged
Conversation
…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.
kodjima33
deleted the
watchdog/issue-11803-historical-prefix-offset-walk
branch
August 18, 2026 15:00
6 tasks
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.
Bug
GET /v3/memoriesstill 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 bothlimit=5000&offset=0(the first page's documented scan-budget fallback) and theoffset>0pages. 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_atis absent from a material slice of the collection — so it streams two candidate windows oflimit + offsetdocuments and applies the final order in Python. Every row an offset skips is still read and billed.HistoricalMemoryAdapter.readwalked the prefix inMAX_PAGE_SIZEchunks with a growingdb_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:
Fix
Ask for the whole prefix in one query.
neededis already bounded byMAX_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_SIZEkeeps 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.shover the 84 memory test files: 856 passed, exit 0.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