Repository navigation
fix(backend): back off conversation memory replacement under control CAS races - #11897
Merged
Merged
Conversation
…CAS races Every same-account canonical memory write races the one account-global memory_state/apply_control document. #11726 fixed that for retraction (cascade delete + merge) with a converging, backed-off loop and explicitly left extraction on its existing contract: three immediate attempts inside replace_conversation_sourced_memories. Extraction has no outer convergence loop. It is called straight from conversation processing (process_conversation -> _extract_memories -> _extract_memories_canonical) and is intentionally fail-closed, so when the three attempts exhaust, ConversationReplacementConflictError propagates out of _emit_derived_effects and the whole enrichment fails: the user's conversation gets no memories and no summary, and the deferred-open path re-arms and retries, feeding the storm. Prod evidence (Cloud Run service `backend`, project based-hardware, 2026-08-19): 11 x "canonical conversation replacement conflicted repeatedly" raised from canonical_memory_adapter.replace_conversation_sourced_memories, each caused by ConversationSourceReplacementConflict("memory control changed during conversation replacement") from the transaction fence in database/memory_apply_store.py. Bursts at 15:45:43-15:51Z (revision backend-79a585b, four distinct instances failing concurrently), 17:33Z, and again at 22:55Z on the current revision backend-920fc55 - so it is live, not a one-off. An immediate retry re-reads the control the peer just advanced, so a burst of same-account writers keeps losing in lockstep. The replacement loop now takes a bounded backoff schedule (0.05 -> 0.5s, five rounds), matching the one the retraction path already proved. The number of attempts is derived from the schedule, exhaustion still raises the same typed error, and nothing is committed on the way out - the #11627 never-orphan-live-memories fence is unchanged. Retraction keeps its inner rounds immediate (it passes the explicit immediate schedule) so the cascade-delete latency budget is not multiplied by its own outer converging loop; its three existing convergence tests pass untouched, which is the proof that path did not move. Verification: - backend/tests/unit/test_conversation_replacement_backoff.py (3 new tests) drives the real production function through the store boundary with a fake Firestore: conflicts that outlast the old immediate budget now converge and commit the memory item; the budget stays bounded at five rounds with the 0.05/0.1/0.25/0.5 schedule and fails closed leaving apply_control at source_generation 1 and no item written; a caller passing an immediate schedule still gets three no-sleep rounds. - All 3 fail against origin/main (2 on the un-backed-off budget, 1 on the absent parameter) and pass on this branch. - backend/tests/unit/test_cascade_retract_convergence.py: 3 passed, unmodified. - 16-file memory/canonical selection: 273 passed, 1 pre-existing ordering failure (test_ws_c_backfill.py::test_stage_all_candidate_can_be_reviewed_ processed_and_routed_by_l2) reproduced identically on origin/main and passing when that file runs alone. Failure-Class: new
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
Every same-account canonical memory write races the one account-global
memory_state/apply_controldocument. #11726 fixed that contention forretraction (cascade delete + merge) with a converging, backed-off loop and
explicitly left extraction on its existing contract: three immediate
attempts inside
replace_conversation_sourced_memories.Extraction has no outer convergence loop. It is called straight from
conversation processing (
process_conversation→_extract_memories→_extract_memories_canonical) and is intentionally fail-closed, so when thethree attempts exhaust,
ConversationReplacementConflictErrorpropagates outof
_emit_derived_effectsand the whole enrichment fails: the user'sconversation gets no memories and no summary, and the deferred-open path
re-arms and retries, feeding the storm.
Prod evidence (read-only)
Cloud Run service
backend, projectbased-hardware, 2026-08-19. 11 ×canonical conversation replacement conflicted repeatedlyraised fromcanonical_memory_adapter.replace_conversation_sourced_memories, each causedby
ConversationSourceReplacementConflict("memory control changed during conversation replacement")from the transaction fence indatabase/memory_apply_store.py.backend-79a585b— four distinct instancesfailing concurrently (
...9569c2b,...7981eb,...b603dbbf16,...a4b96), i.e. genuine same-account contention, not one stuck worker.backend-920fc55— still live.Root cause
An immediate retry re-reads the control a peer just advanced, so a burst of
same-account writers keeps losing in lockstep. The budget is spent on ordinary
contention, not on a real failure.
Fix
replace_conversation_sourced_memoriesnow takes a bounded backoff schedule(
0.05 → 0.5s, five rounds) — the one the retraction path already proved inprod. The attempt count is derived from the schedule so "how many rounds" and
"how far apart" cannot drift. Both conflict sites in the loop (the source-scan
control mismatch and the store fence) back off.
Exhaustion still raises the same typed
ConversationReplacementConflictErrorand nothing is committed on the way out — the #11627 never-orphan-live-memories
fence is unchanged.
Retraction keeps its inner rounds immediate (it passes the explicit
immediate schedule) so the cascade-delete latency budget is not multiplied by
its own outer converging loop. Its three existing convergence tests pass
untouched, which is the proof that path did not move.
What I tested
backend/tests/unit/test_conversation_replacement_backoff.py(3 new tests)drives the real production function through the real store boundary with the
shared fake Firestore:
the memory item (
source_generation1 → 2);0.05/0.1/0.25/0.5scheduleand fails closed —
apply_controlleft atsource_generation: 1, noitem written;
origin/main(2 on the un-backed-off budget, 1 on theabsent parameter) and pass on this branch.
backend/tests/unit/test_cascade_retract_convergence.py: 3 passed, fileunmodified.
failure (
test_ws_c_backfill.py::test_stage_all_candidate_can_be_reviewed_processed_and_routed_by_l2)reproduced identically on
origin/mainand passing when that file runs alone.make preflight(deterministic check manifest, CI lane).Product invariants affected
Failure class
Second fix sharing this cause (#11731 was the first), so it is registered
rather than left as an instance fix, per root
AGENTS.md.Failure-Class: new
🤖 automated by hourly watchdog — tested and merged
Line-Count-Exception: backend/utils/memory/canonical_memory_adapter.py | 2562 -> 2593 | Surgical in-place fix to one existing conflict loop in this file (bounded backoff helper + schedule parameter + the prod-signature docstring). Splitting the canonical adapter is a separate, high-blast-radius refactor and is not appropriate inside a production incident fix.