Skip to content

fix(backend): back off conversation memory replacement under control CAS races - #11897

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/issue-11726-extraction-cas-backoff
Aug 20, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/issue-11726-extraction-cas-backoff

Conversation

@kodjima33

@kodjima33 kodjima33 commented Aug 20, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

Every same-account canonical memory write races the one account-global
memory_state/apply_control document. #11726 fixed that contention 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 (read-only)

Cloud Run service backend, project based-hardware, 2026-08-19. 11 ×
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.

  • 15:45:43–15:51Z, revision backend-79a585b — four distinct instances
    failing concurrently (...9569c2b, ...7981eb, ...b603dbbf16,
    ...a4b96), i.e. genuine same-account contention, not one stuck worker.
  • 17:33Z, same revision.
  • 22:55Z on the current revision 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_memories now takes a bounded backoff schedule
(0.05 → 0.5s, five rounds) — the one the retraction path already proved in
prod. 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 ConversationReplacementConflictError
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.

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:
    • conflicts that outlast the old immediate budget now converge and commit
      the memory item
      (source_generation 1 → 2);
    • the budget stays bounded at 5 rounds with the 0.05/0.1/0.25/0.5 schedule
      and fails closed — apply_control left at source_generation: 1, no
      item written;
    • a caller passing an immediate schedule still gets 3 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, file
    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.
  • make preflight (deterministic check manifest, CI lane).

Product invariants affected

  • INV-MEM-1

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.

Review in cubic

…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
@kodjima33
kodjima33 merged commit 7f4ac83 into main Aug 20, 2026
40 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-11726-extraction-cas-backoff branch August 20, 2026 01:32
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