Skip to content

SCA-4656: Defer finalization while live audio is uncommitted - #26402

Merged
Git-on-my-level merged 4 commits into
mainfrom
agent/omi-control-owners-sol/a4fab207339e
Nov 28, 2026
Merged

Git-on-my-level merged 4 commits into
mainfrom
agent/omi-control-owners-sol/a4fab207339e

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes SCA-4656 (incident SCA-4652, diagnosis SCA-4651). When a user Finish arrives while the live listen session still holds uncommitted audio, POST /v1/conversation-finalization-jobs/run waited 30 s, raised ServerBatchArchiveNotClosed and answered HTTP 500. That spent an attempt, and after five attempts the job was budget-requeued or dead-lettered.

Changes

  • Wait up to 3 s for the live archive to commit (was 30 s). If it is still uncommitted, yield the lease with a non-counting retry and answer HTTP 200 {"status": "deferred"}. attempt_count and the worker claim slot are unchanged.
  • The deferred job becomes due 10 s later. dispatch_requested_at is cleared and a due-time guard in _claim_finalization_replay_txn stops any replay before the due time. The next reconciler pass then mints a new dispatch generation.
  • Bound. The first deferral stamps live_archive_wait_started_at on the job. After LIVE_ARCHIVE_MAX_WAIT (10 min), the deferral transaction raises LiveArchiveWaitExpired and writes nothing. The worker then takes the pre-existing path: a counted ServerBatchArchiveNotClosed retry under the normal attempt budget. A session that never commits therefore cannot replay forever, and the audio stays held.
  • If the row is deleted during the wait, the delivery defers like any other uncommitted delivery. The replay's admission read (_load_admitted_conversation) then applies its existing missing-row and fanout-claim handling. The deferral path adds no separate shortcut for this case.

Custody invariants and the tests that prove them

All tests are in backend/tests/unit/test_user_finish_first_archive_window.py. They use the real admission, claim, retry and replay transactions on the strict Firestore fixture.

Invariant Test
Deferral never drops, deletes or finalizes without the held audio. The conversation row is byte-identical after the delivery. The register, complete and empty-delete writers are not called (the worker double raises on any other writer). The projection deltas balance. test_worker_defers_an_uncommitted_finish_and_replays_without_spending_an_attempt
A deferred job is not replayed before its due time, is replayed once at the due time into generation 2 with attempts unchanged, and, once the session commits, the redelivery hands the recording to batch (register then complete, 200 done). same test
Each redelivery inside the bound defers again without spending an attempt, and the first start stamp is kept. test_each_redelivery_inside_the_bound_defers_again
At the bound, deferral stops and the pre-existing safe path runs: a counted 500 retry with attempt_count 1 to 2, the conversation row unchanged, and no delete or finalize. Mutation check: removing the bound's raise fails this test. test_deferral_is_bounded_then_takes_the_counted_not_closed_retry
A row deleted mid-wait defers (non-counting) to the replay admission read. test_a_deleted_row_ends_the_wait_and_defers_to_the_replay_admission
A natural end (CAPTURE_END) does not wait and keeps the not-closed retry. test_a_natural_end_does_not_wait

Firestore reads per delivery

A deferred delivery makes these reads. Reads this PR adds: 0.

  • Unchanged by this PR: claim transaction (job doc plus account-deletion marker, 2), get_finalization_job (1), conversation admission read (1).
  • Changed by this PR:
    • Archive-wait polls: at most 6 conversation reads (3 s at 0.5 s), down from at most 60 (30 s).
    • The deferral transaction reads the job doc once. It replaces the read that the pre-existing _retry_or_dead_letter retry transaction made.
  • Per replay cycle (unchanged mechanism): 1 candidate row in the reconciler page and 1 job read in the replay transaction.
  • Cycles per job are bounded by LIVE_ARCHIVE_MAX_WAIT: about 10 at the per-minute reconciler pass, and never more than 600 s / 10 s.

Rollback and kill switch

  • Rollback: revert this PR. The deferral adds no new data shape that needs cleanup. live_archive_wait_started_at is an inert job field after a revert.
  • There is no dedicated flag for the deferral itself. The existing switches remove the whole early-Finish path that reaches this code: CONVERSATIONS_V3_BATCH_MANUAL_END=off restores the no_content answer for an early Finish, and CONVERSATIONS_V3_FAST_FINISH=cohort|off narrows admission.

Verification

Current head f4668b1a4ca5129004d3142f34ebbdc3ff7bb988 (origin/main c0e57162ec0 merged in, no rebase), m1-mac-studio, through bin/omi-heavy with bounded timeouts:

  • tests/unit/test_user_finish_first_archive_window.py: 22 passed.
  • bash test.sh (changed-file selector, the same 651 files CI selects): 25,120 passed, 17 skipped, 2 failed. Both failures are in tests/unit/test_batch_coalescing_completion.py::test_mobile_hold_headers_survive_coalescing_and_publication[merged|individual] (503 sync_batch_unavailable). They fail identically with this PR's three files reverted to origin/main, and on main CI since 92ad82907a (SCA-4372: resolve accepted review findings for the mobile batch upload hold #26519, SCA-4372). This is main drift, not this change.
  • Pre-push gate: passed on push.
  • git diff --check is clean.

After the release lane serves the fix: on the first health tick, the route's 5xx share should be under the tick rule, with no reason=server_batch_archive_not_closed 500 and no cause=worker_failed dead letter from this cause. Look for event=live_archive_wait outcome=expired warnings, which mark sessions that hit the bound.

Earlier head 9d846f4db2983b7c44cb2d7f57b188980bb6dd7e (not current). Five checks were red:

  • PR Metadata Preflight and Hygiene: line-count ratchet, because the exception was missing from the body.
  • Backend unit shard 5/8 and Backend unit suite: the new route test hit UnsupportedFirestoreOperationError. The strict fixture does not model the projection-shard set(merge=True), so the route answered 500. Fixed by stubbing _record_projection_delta, as other finalization suites do.
  • Backend Go Checks: a timing failure in TestScheduledResumeHandsBackClaimWhenStopWinsLaunch, outside this Python-only diff, and green on main.

Line-Count-Exception: backend/database/conversation_finalization_jobs.py | 3765 -> 3794 | Keep the lease-fenced deferral, its bound and the due-time replay guard inside the existing finalization transactions they fence.

🤖 Generated with Claude Code

Git-on-my-level and others added 4 commits November 26, 2026 10:48
Stamp the first uncommitted-archive deferral on the job. After ten
minutes the deferral transaction refuses without writing, and the worker
takes the pre-existing counted not-closed retry, so a session that never
commits cannot replay forever. A row deleted mid-wait now defers to the
replay's admission read, which owns the missing-row handling, instead of
fencing the job directly.

The worker-route test stubs the projection delta the strict fixture does
not model, and now asserts the held audio is untouched, the replay at the
due time, redelivery to batch after the commit, and the bound.

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

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

REVIEW: SHIP 10d7dbd
PANEL: review-20261128t033830-1e41 on #26402 @ 10d7dbd (luna completed 3m, deepseek completed 6m)

F1. [minor] backend/docs/listen_pusher_pipeline/changes/2026-11-24-user-finish-first-archive-window.md:32
The existing note still documents a 30-second wait, up to 60 reads, and a counted retry, while this change uses a 3-second wait, up to 6 reads, and a non-counting deferral bounded at 10 minutes. This is a documentation drift finding, not a runtime blocker. Suggested follow-up: add a 2026-11-28 change note and mark the older sentences superseded.

Panel integrity warning: reviewer checkout was modified under .multica/.

@Git-on-my-level
Git-on-my-level merged commit 85ea82c into main Nov 28, 2026
70 checks passed
@Git-on-my-level
Git-on-my-level deleted the agent/omi-control-owners-sol/a4fab207339e branch November 28, 2026 03:45
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