fix(memory): preserve trusted state head across ledger writes - #9739
Merged
Git-on-my-level merged 1 commit intoJul 14, 2026
Merged
Conversation
5 tasks
kodjima33
added a commit
that referenced
this pull request
Jul 15, 2026
… (#9783) 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.
Summary
Root cause and durable guard
The legacy ledger replaced
users/{uid}/memory_state/headwith onlycurrent_head_commit_id, deleting the canonical reader's requireduid, generation, and sequence fields. The V3 reader correctly failed closed with 503 as a result. Both ledger transaction variants now retain a validated canonical header or recover it from canonical apply control, and regression tests execute the ledger transaction followed by the V3 trusted reader.Product invariants affected
Verification
backend/.venv/bin/python -m pytest -q backend/tests/unit/test_memory_ledger.py backend/tests/unit/test_memory_apply_store.py backend/tests/unit/test_v3_account_generation_source.py(45 passed)make preflight(passed)backend/test.shexercised the full runner, but an unrelated existing fast-unit CPU-duration ratchet rejectedtests/unit/test_activate_task_intelligence_dogfood_user.py::test_missing_control_plans_explicit_read_at_default_generationafter its assertions passed (0.22s over 0.12s limit).