fix(conversations): transactional segment-text edits to stop lost updates (#9392) - #9574
Merged
Merged
Conversation
…lost updates
update_conversation_segment_text did a non-transactional read-modify-write:
doc.get() -> mutate one segment in the decoded transcript_segments array ->
doc.update() rewriting the whole array. Two concurrent edits (even to different
segments) both read the pre-edit array, and the later write clobbered the
earlier one -- both returned 200 but one edit was silently lost.
Wrap the read + write in a Firestore transaction using the same
@firestore.transactional inner-function pattern already used by
upsert_conversation in this file, so the read is retried on contention and the
whole-array rewrite is atomic. Return-code contract (ok/not_found/locked/
segment_not_found) is unchanged, so the router behavior is identical.
Root cause: shared read-modify-write over the whole transcript_segments array
without a transaction. Durable guard: atomic transaction + regression tests
asserting the write goes through transaction.update and the untouched segment
is preserved.
Verified: added 4 tests in test_conversation_revision_contract.py exercising the
real db function through a controllable transaction seam (edit lands + other
segment preserved, missing-segment no-write, locked, not_found). Ran with the
backend venv:
ENCRYPTION_SECRET=... .venv/bin/python -m pytest \
tests/unit/test_conversation_revision_contract.py -q -> 14 passed
Scope note: the speaker-assign race (router-side read + update_conversation_
segments) and the reprocess stale-snapshot overwrite from #9392 are larger,
separate surfaces left as follow-ups.
fixes #9392
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.
What
Wrap
update_conversation_segment_text(backend/database/conversations.py) in a Firestore transaction so concurrent transcript-segment text edits can't silently lose-update each other.Why
From #9392:
The web PR (#9353) only serializes saves within a single client; the cross-tab / cross-client race requires this backend fix.
Change
doc_ref.get) and the whole-array write now run inside a@firestore.transactionalinner function — the same pattern already used byupsert_conversationin this file — so the read is retried on write contention and the array rewrite is atomic.ok/not_found/locked/segment_not_found) is unchanged, sopatch_conversation_segment_textinrouters/conversations.pybehaves identically.Root cause & durable guard
transcript_segmentsarray with no transaction.transaction.updatewhile the untouched segment is preserved.Verified
Added 4 tests to
backend/tests/unit/test_conversation_revision_contract.py(edit lands + other segment preserved / missing-segment no-write / locked / not_found), run with the backend venv:(10 pre-existing + 4 new; the new ones deselected-run green under
-k segment.)Scope note (deliberately deferred)
#9392 also covers two larger, separate surfaces left as follow-ups so this PR stays reviewable and revertable:
update_conversation_segments(used by 6 call sites incl. the sync pipeline); fixing it correctly means moving the mutation inside a transaction per endpoint.reprocess_conversationoverwriting the transcript from a stale snapshot (touchesprocess_conversationpersistence).Auto-generated from issue feedback by the mini issues-improver. Review before merge.