fix(backend): read apply_control before writes in memory ledger (#9780) - #9783
Merged
Merged
Conversation
POST /v3/memories returned retryable 503s (Sentry OMI-DESKTOP-2F3, 23 events / 7 users) because the legacy memory-ledger transaction performed a Firestore read after staging writes: `_state_head_write_payload()` (added in #9739) reads `memory_state/apply_control` as a fallback, but it was invoked inside `transaction.set(state_ref, ...)` — after `projection_writer(transaction)` and the commit write. Firestore rejects this with `ReadAfterWriteError`, which `create_memory()` maps to HTTP 503. Root cause / durable guard: all transactional reads must precede the first write. Build the state-head payload (including the apply_control fallback read) before staging any writes, in both `_append_commit_transaction` and `_append_commit_with_builder_transaction`, then reuse it for the final state_ref write. #9739's canonical-head preservation is unchanged. The existing tests used a lenient transaction fake that ignored ordering, so they missed the regression. Added a strict transaction fake that raises ReadAfterWriteError when a get() lands after a set(); the two new tests fail on the pre-fix code and pass with the fix, covering both the direct and builder append paths through the apply_control fallback. Tests: `pytest tests/unit/test_memory_ledger.py` (29 passed); verified the two new tests fail on pre-fix source (ReadAfterWriteError) and pass after. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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 (#9780)
POST /v3/memoriesreturned retryable 503 Service temporarily unavailable for beta/dev Desktop traffic. Sentry groupOMI-DESKTOP-2F3: 23 events across 7 users (2026-07-14 → 2026-07-15). Dev Cloud Run logs at the failure times show:The effect is a failed new-memory write (the route returns before post-processing/vector upsert — no successful persistence, no data corruption claimed).
Root cause
_state_head_write_payload()(added in #9739 to preserve canonical state-head fields) readsmemory_state/apply_controlas a fallback when the state head lacks trusted fields. In both_append_commit_transactionand_append_commit_with_builder_transactionit was invoked inside the finaltransaction.set(state_ref, ...)— i.e. afterprojection_writer(transaction)and the commit write had already staged writes. Firestore forbids a transactional read after a write and raisesReadAfterWriteError, whichrouters/memories.py:create_memory()translates to HTTP 503.The existing unit tests used a lenient transaction fake that did not enforce Firestore's read-before-write ordering, so they never caught it.
Fix
Build the state-head payload — including the
apply_controlfallback read — before staging any write, then reuse it for the finalstate_refwrite. Applied to both append paths. #9739's canonical-head preservation is unchanged; no untrusted synthetic head is fabricated.Durable guard: added a strict transaction fake (
_StrictLedgerTransaction/_StrictLedgerDb) that raisesReadAfterWriteErrorwhen aget()lands after aset(), plus two regression tests covering the direct and builder append paths through theapply_controlfallback.Verification
pytest tests/unit/test_memory_ledger.py→ 29 passed.ReadAfterWriteError) and pass with the fix — they reproduce the exact prod failure.test_memory_ledger.py + test_short_term_promotion_control_state.py + test_v3_account_generation_source.py→ 42 passed.Diff: 2 files, +141/-20.
🤖 automated by hourly watchdog; opened for review. This is a live prod bug (503s on
/v3/memories); if CI is green it may be auto-merged per the prod-bug gate.Closes #9780