Repository navigation
fix(episodes): attach batch captures to their episode at admission - #25041
Merged
Merged
Conversation
A batch_live_v1 capture had no episode for the whole recording: the server attached it only at audio close, so Home painted it as a standalone row for the capture duration (median 8.5 min, p90 50 min) and then folded it in. Attach at admission through the same fenced attach_recorded transaction, with a one-second provisional end and pending_transcription=True. The audio-close attach writes the real end and never renews the pending deadline. A boundary move detaches the provisional member through the recorded-timeline fence before re-placing it. Kill switch EPISODE_BATCH_ADMISSION_ATTACH_ENABLED, default on; off restores the pre-admission behaviour. Server only, no app release. Failure-Class: FC-episode-membership-waits-for-enrichment
One content conflict in backend/utils/episodes/batch_close.py, where main added the late_member.joined paid-work re-arm on the audio-close path and this branch rewrote the same function for admission attach plus close finalize. Both survive: the boundary-move detach still runs first, the close attach still writes the real end, and the placed owner is handed to late_member.joined. The admission path is unchanged. The flag registry and config/feature-flags.yaml auto-merged; the registry check stays OK with the admission row in place. Failure-Class: FC-episode-membership-waits-for-enrichment
Pass a clock-carrying row at both admission boundaries so the attach is no longer a silent no-op. Floor the pending deadline past the longest capture, move the provisional member atomically at close, and refuse to detach a live or current episode.
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 changed and why
A
batch_live_v1capture used to have no episode for the whole recording. Theserver attached it only at audio close (
attach_closed_batch_capture), so Homepainted it as its own row for the capture duration and then folded it into the
episode. Measured on one account: median 8.5 minutes, p90 50 minutes standalone
(
data/run-2026-11-08-pipeline-refactor/findings-rowflash.md).utils/episodes/batch_close.pygainsattach_admitted_batch_capture. It reuses the existing fencedservice.attach_recordedtransaction (no new attach path). The recorded startis known at admission; the end is not, so the member joins with a one-second
provisional window and
pending_transcription=True. Home projects it as"Transcribing…" from its first appearance.
routers/listen/conversations.pyafter aserver_batch_onlyconversation iscreated (the dominant class, off the request path), and
routers/listen/preview_custody.pywhen a local preview is promoted to batchcustody. Both pass a clock-carrying row (the create payload / the full claim
row), not the eligibility-only
capture_snapshot.member already owned, writes the real end, and never renews the pending
deadline (
episode_batch_pending.read_patchstamps once).episode_transcription_deadline_atatstarted + MAX_EPISODE_SECONDS + SLA_SECONDS, so a capture longer than the2h delivery SLA cannot project "Couldn't transcribe" while it is still
recording. The close path still never renews the deadline.
occasion than the admission episode, the close attach re-places the member
and removes the stale provisional membership in the same fenced transaction
(
detach_from). A failed attach leaves the member where it was; a faileddetach leaves a duplicate, never an orphaned capture.
detach_recordedrefuses a live episode or the
episode_links/currenttarget.episode_recorded_components.attachtreats a replay ofthe same provisional fact as the same span, so a repeated admission attach
does not bump the episode's
state_version.EPISODE_BATCH_ADMISSION_ATTACH_ENABLED, default on. Offrestores the pre-admission behaviour exactly: the capture stays standalone and
joins at audio close.
EPISODE_BATCH_CLOSE_ATTACH_ENABLEDstill governs theclose path.
New state and its exit: a provisionally attached member is bounded by the
floored
episode_transcription_deadline_atand the automatic "Couldn'ttranscribe" terminal projection; the audio-close attach finalizes the end, and
the transcript projection clears the pending presentation. A failed admission
attach leaves the capture standalone and the finalize-time attach remains the
repair path.
Server only. No app release; the client hardening rides #25024 separately.
Product invariants affected
none
How it was verified
backend/tests/unit/test_episode_batch_audio_close.py(56 tests) covers thenew paths against
StrictFirestoreand the real fenced transaction:test_admission_boundaries_pass_a_clock_carrying_row: drives the realpreview promotion and the real fresh-create path, then asserts a
provisional member exists. This is the regression guard for the inert
admission attach.
test_admission_deadline_survives_a_capture_longer_than_the_sla: an8000-second capture still projects "Transcribing…" at close.
test_admission_attaches_a_provisional_member_before_audio_close: thecapture is a member with a provisional end and a pending deadline before
audio close, and Home projects "Transcribing…".
test_audio_close_finalizes_the_admitted_end_without_renewing_the_deadline:the close keeps the admission owner, writes the real end, and leaves the
deadline unchanged.
test_boundary_move_moves_the_provisional_member_atomically_at_the_close_attach:the member leaves the admission episode and joins the episode the real
window selects in one transaction.
test_detach_refuses_a_transcribed_member: a projected member never moves.test_admission_kill_switch_off_restores_the_audio_close_attach: off leavesthe capture standalone and the close attach still works.
test_admission_retry_does_not_attach_twice: a replay adds no membershipand does not bump
state_version.test_late_admission_after_audio_close_cannot_undo_the_close_end: theclose attach commits first, then the delayed admission task returns
closedwithout writing; the finalized end andstate_versionareunchanged and the member keeps its close-time episode.
backend/tests/unit/test_episode_*.py): 897 passed.test_flag_budget,test_omi_flags,test_graduated_flag_behavior): 113 passed.test_firestore_query_shapes.py): 572 passed.scripts/check_feature_flag_registry.py: OK.bin/omi-heavy: see the push.window. The change is metadata-only and fail-open; the existing
episode_batch_audio_close_attachfallback telemetry covers a failed attach.Tests
Feature:
backend/tests/unit/test_episode_batch_audio_close.pycovers admissionattach, close finalize, boundary move and detach, the transcribed-member
refusal, the kill switch, and retry idempotency. The admission tests fail on
origin/mainbecause no admission attach exists.Failure class (fixes)
Failure-Class: FC-episode-membership-waits-for-enrichment
UI-Evidence: none -- backend-only membership timing; no client change
Line-Count-Exception: backend/utils/episodes/service.py | 2223 -> 2243 | the admission attach threads the deadline floor, atomic detach and closed-refusal outcome through the existing service signature
Merge-main (2026-11-12)
Earlier head:
6a74b9f26cb4d4b08509222e5f57839bbcf79d78(head reviewed by the11-11 fix batch; this batch's predecessor).
Earlier head:
3cd22f779a386d7c562a5400dfe2e2593fde5733(merge of main7688c61a761d180a3b5fb6cd53c7ecd720e976ddinto the reviewed head).Earlier head:
12af3a8e2b990ae172181bafc20e82950f66e944(the reviewed head;first parent of the merge).
Branch:
lane/rowflash. One push, no merge, no deploy.git merge origin/mainproduced exactly one content conflict,backend/utils/episodes/batch_close.py. Main's0e985a4ccd0added a paid-work preservation hook on the audio-close path(
owner = service.attach_recorded(...)thenlate_member.joined(uid, conversation_id, row, owner, c)); this branch hadrewritten the same function for admission attach plus close finalize. Both
survive — the close attach writes the real end, the atomic boundary move runs
inside the same transaction, and the placed owner is handed to
late_member.joinedexactly once on the close path:The import became
from utils.episodes import service, late_member.backend/docs/feature-flag-registry.mdandconfig/feature-flags.yamlauto-merged;
scripts/check_feature_flag_registry.pyis OK and the admissionrow sits in alphabetical position, so the flag count still matches
len(names).Fix batch (11-12)
Head:
b4e16433541f9ba3318ae5fbd4e50ef2b78b26eaBranch:
lane/rowflash. One push, no merge, no deploy.The delta review returned FIX-FIRST with one P1: a delayed admission task
could overwrite the audio-close result. The admission attach now refuses
transactionally after the close has committed.
Fix batch (11-11)
Both pre-reviews returned FIX-FIRST. This batch applies the agreed blockers and
the cheap P2s in one push.
capture_snapshot, which drops every clock, so_admitted_startreturnedNoneand the attach returned'skipped'.conversations.pypasses{**payload, **batch_record_capture};preview_custody.pypasses the full claimrow.capture_snapshotstays the minimal eligibility snapshot. New boundary test drives both real call sites.started + MAX_EPISODE_SECONDS + SLA_SECONDS; the close path still stamps once and never renews. New 8000-second capture test.late_member.joined.detach_recordedcan mutate a live episode and break the live writer's CAS.status == 'live'and theepisode_links/currenttarget.detach_from). A failed attach leaves the member in place._admitted_startalready prefersserver_batch_audio_start; the full row now carries it when present, andstarted_at/created_atotherwise.detach_recordedleavesrecorded_end/closed_at/compose proof stale.recorded_candidates; the boundary-move test used aliveepisode production never creates.recorded_candidatesfake remains.'waiting'for the pending member, so no paid work; the queue/read cost is a follow-up.is_locked/deferred/relevance_decision).closedwith no writes once the archive is closed orserver_batch_audio_endis real; an admission replay of a closed member is treated as already placed. Newtest_late_admission_after_audio_close_cannot_undo_the_close_end.