Skip to content

fix(merge): complete merges of conversations that overlap in time - #22320

Merged
kodjima33 merged 4 commits into
BasedHardware:mainfrom
apoorvdarshan:fix/merge-overlap-correction-transfer
Oct 19, 2026
Merged

kodjima33 merged 4 commits into
BasedHardware:mainfrom
apoorvdarshan:fix/merge-overlap-correction-transfer

Conversation

@apoorvdarshan

Copy link
Copy Markdown
Collaborator

What changed and why

Fixes #22319.

Merging two conversations that overlap in time (for example, a short recording made during a longer one) fails on main. perform_merge_async sorts the merged transcript by start, so the sources' lines interleave. The correction transfer added in #21480 (review_overlay_merge.transfer) then looked for each source's lines at offset + i, as if every source were one block in merge order. For an overlapping merge that check raises ReviewConflict('Captured speech changed during merge'), whether or not either conversation has transcript corrections.

perform_merge_async catches it after the merged conversation has already been processed, deletes the merged conversation and resets the sources. _handle_merge_failure sends no failure notification, so the user's merge just undoes itself.

The fix:

  • _merge_transcript_segments_with_placements returns the merged transcript plus, for each source, the merged index of each of its lines. _merge_transcript_segments still returns the same transcript.
  • perform_merge_async passes those placements to transfer, which checks the captured speech and moves correction anchors (and their Undo receipts) through them. The shape check now requires the placements to cover every merged line exactly once.
  • Without placements, transfer keeps the block-per-source layout, which is what merges of non-overlapping conversations produce.

Product invariants affected

none

How it was verified

  • Traced the merge on origin/main (8ed3ad1b1b): the chronological sort in _merge_transcript_segments (7f47667) and the positional check in transfer (feat(dream): verified shared harness, immutable corrections, and all-user cleanup #21480).
  • Replayed the ordering and both versions of the check in a standalone script (copies of the logic, no repo imports). A 2-line conversation plus a 1-line conversation that starts 30 s in merges as long-early, short-only, long-late. The old check rejects it. The new one accepts it with placements [[0, 2], [1]], so a correction on long-late moves to merged line 2. Non-overlapping sources still get [[0, 1], [2]], the old layout.
  • Local checks only (compute policy on this machine): scripts/backend-python-format --write (no changes), python -m py_compile on the changed Python files, and scripts/failure-class validate on this body.
  • I didn't run pytest locally. The regression test is written to fail without the fix; it and the full backend suites run in Omi's CI (ci:full).

Tests

  • backend/tests/unit/test_merge_overlapping_sources.py::test_overlapping_merge_completes_and_keeps_the_correction_on_its_line runs the real perform_merge_async and the real transfer against the strict in-memory Firestore fixture. Two encrypted conversations overlap, and the owner first corrects the later line of the longer one through edit_user_segment. The test asserts that the merge completes (failure handler not called, one completion message), that the merged transcript reads early line, short line, corrected late line, and that the source is sealed to the merged conversation. On main the transfer rejects this merge and the failure handler runs.
  • backend/tests/unit/test_merge_deleted_source_race.py: two tests stubbed the merge step that perform_merge_async calls. The stubs now target the new helper.

UI evidence

UI-Evidence: none -- backend-only change; the regression test drives the merge end to end.

Failure class (fixes)

Failure-Class: new

Failure-class transition narrative

New class FC-merged-layout-assumed-from-source-order.

  • Violated contract: a step that finds each source's items in a combined sequence must use the layout the combining step produced, not running offsets in source order.
  • Canonical guard: backend/tests/unit/test_merge_overlapping_sources.py, which feeds interleaving sources through the real merge and the real transfer.
  • Evidence: issue Merging conversations that overlap in time fails and silently rolls back #22319 and the merge path on origin/main before this change. evidence_prs is empty because this PR is the first instance.

🤖 Generated with Claude Code

Merging conversations that overlap in time sorts the merged transcript by
start, so the sources' lines interleave. The correction transfer that runs
after the merge expected each source's lines as one block in merge order,
so its captured-speech check failed for every such merge, with or without
transcript corrections. The merge rolled back, the sources came back
unmerged, and no failure message reached the user.

The merge now returns where each source line landed, and the transfer
checks the captured speech and moves correction anchors through that map.
Merges whose sources do not overlap keep the same layout as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@apoorvdarshan apoorvdarshan added the ci:full Maintainer-granted: run full CI on this fork PR label Oct 19, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Git-on-my-level

Copy link
Copy Markdown
Member

Verdict: the fix is correct and nicely scoped — it resolves a real bug where every merge of conversations overlapping in time failed and silently rolled back, and the explicit placement map is the right design. Leaving formal approval to a human maintainer since this touches the correction-transfer transaction.

Findings by file:

  • backend/utils/conversations/merge_conversations.py — _merge_transcript_segments_with_placements returns the merged transcript plus placements[k][i] (the merged index of source k's segment i), derived from the same stable-sort permutation that orders the transcript. Verified the merge loop (lines 502–535) appends every source segment exactly once with no filtering or dedup, so the contiguous-range placement reconstruction is exact; the sort key expression is identical to the previous inline sort, so ordering behavior is unchanged. Keeping _merge_transcript_segments as a thin wrapper preserves the old API.
  • backend/database/review_overlay_merge.py — transfer() now validates each source's captured text at positions[i] instead of assuming block-per-source at offset + i, remaps correction segment_index and Undo receipt anchors through the placement map, and the shape check is strengthened: sorted(placed) == list(range(len(segments))) proves the placements cover every merged segment exactly once (catches gaps/duplicates the old total-length check missed). len(positions) != len(captured) is checked before the zip, so no silent truncation. With placements=None the behavior is the legacy block layout, so non-merge callers are unaffected.
  • backend/tests/unit/test_merge_overlapping_sources.py — proper behavioural test: it drives the real perform_merge_async through the real transfer with a short recording nested inside a longer one, asserts the corrected line lands on its own merged line ("Corrected late line." at index 2) and that sources get sealed with transcript_corrections_moved_to. This is exactly the producer-through-consumer test the failure class calls for.
  • backend/tests/unit/test_merge_deleted_source_race.py — correct mechanical retarget of the two monkeypatches to the new function, returning ([], [[] for _ in convs]) to match the new signature; race scenarios still covered.
  • .github/failure-classes/FC-merged-layout-assumed-from-source-order.json — data-only metadata following the established FC schema; the violated-contract wording accurately describes the bug. No workflow logic.

Security/data integrity: the correction and receipt remapping stays inside the same Firestore transaction with the same ReviewConflict fences (account-deletion fence, deleted/locked source, already-moved anchors). No new trust boundary, no data leaves the transaction. Blast radius if wrong: merges — but the strengthened shape check plus the per-position text check make a mis-bound correction anchor fail closed into a rollback, which is the pre-existing safe path.

Performance: one permutation map per merge on top of the sort that already existed — negligible.

USER IMPACT: merging two recordings that overlap in time now completes and keeps transcript corrections on the right lines, instead of the merge quietly undoing itself.

What I'd still want as a reviewer (non-blocking): a variant with corrections on both overlapping sources, and a pinning test for equal-start segments across sources, where the stable sort keeps source order.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution labels Oct 19, 2026
…start ordering

Review follow-up. Both tests drive the real perform_merge_async and the
real correction transfer with the same fakes as the existing overlap test.

- Corrections on both overlapping sources land on their own merged lines,
  both sources are sealed, and undoing one source's correction through its
  moved receipt restores only that line.
- Lines from different sources with the same merged start keep source order
  (the stable sort), and a correction on the later source's tied line stays
  on that line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@apoorvdarshan

Copy link
Copy Markdown
Collaborator Author

Added both tests to backend/tests/unit/test_merge_overlapping_sources.py, driving the real perform_merge_async and transfer with the same fakes as the existing test. One corrects a line in each overlapping source and checks that both corrections land on their own merged lines (and that undoing one restores only its line); the other pins that lines from the two sources with the same merged start keep source order, with a correction on the later source's tied line staying on that line. Both run in this PR's CI (ci:full).

🤖 Generated with Claude Code

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed bug (overlapping-time merges always fail the correction-transfer positional check, fixes #22319); scoped fix with new failure class + strong regression test, CI green, CLEAN.

@kodjima33
kodjima33 merged commit e6770da into BasedHardware:main Oct 19, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) ci:full Maintainer-granted: run full CI on this fork PR positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merging conversations that overlap in time fails and silently rolls back

3 participants