Skip to content

fix(episodes): attach batch captures to their episode at admission - #25041

Merged
Git-on-my-level merged 6 commits into
mainfrom
lane/rowflash
Nov 11, 2026
Merged

Git-on-my-level merged 6 commits into
mainfrom
lane/rowflash

Conversation

@Git-on-my-level

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

Copy link
Copy Markdown
Member

What changed and why

A batch_live_v1 capture used to have no episode for the whole recording. The
server attached it only at audio close (attach_closed_batch_capture), so Home
painted 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).

  • Attach at admission. utils/episodes/batch_close.py gains
    attach_admitted_batch_capture. It reuses the existing fenced
    service.attach_recorded transaction (no new attach path). The recorded start
    is 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.
  • Two admission boundaries, both metadata-only and fail-open:
    routers/listen/conversations.py after a server_batch_only conversation is
    created (the dominant class, off the request path), and
    routers/listen/preview_custody.py when a local preview is promoted to batch
    custody. Both pass a clock-carrying row (the create payload / the full claim
    row), not the eligibility-only capture_snapshot.
  • Close finalizes the end. The existing audio-close attach now finds the
    member already owned, writes the real end, and never renews the pending
    deadline (episode_batch_pending.read_patch stamps once).
  • Deadline floor. Admission floors
    episode_transcription_deadline_at at
    started + MAX_EPISODE_SECONDS + SLA_SECONDS, so a capture longer than the
    2h delivery SLA cannot project "Couldn't transcribe" while it is still
    recording. The close path still never renews the deadline.
  • Boundary move is atomic. If the completed window belongs to a different
    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 failed
    detach leaves a duplicate, never an orphaned capture. detach_recorded
    refuses a live episode or the episode_links/current target.
  • Retry idempotency. episode_recorded_components.attach treats a replay of
    the same provisional fact as the same span, so a repeated admission attach
    does not bump the episode's state_version.
  • Kill switch. EPISODE_BATCH_ADMISSION_ATTACH_ENABLED, default on. Off
    restores the pre-admission behaviour exactly: the capture stays standalone and
    joins at audio close. EPISODE_BATCH_CLOSE_ATTACH_ENABLED still governs the
    close path.

New state and its exit: a provisionally attached member is bounded by the
floored episode_transcription_deadline_at and the automatic "Couldn't
transcribe" 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 the
    new paths against StrictFirestore and the real fenced transaction:
    • test_admission_boundaries_pass_a_clock_carrying_row: drives the real
      preview 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: an
      8000-second capture still projects "Transcribing…" at close.
    • test_admission_attaches_a_provisional_member_before_audio_close: the
      capture 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 leaves
      the capture standalone and the close attach still works.
    • test_admission_retry_does_not_attach_twice: a replay adds no membership
      and does not bump state_version.
    • test_late_admission_after_audio_close_cannot_undo_the_close_end: the
      close attach commits first, then the delayed admission task returns
      closed without writing; the finalized end and state_version are
      unchanged and the member keeps its close-time episode.
  • Full episode suite (backend/tests/unit/test_episode_*.py): 897 passed.
  • Flag budget + registry contract (test_flag_budget, test_omi_flags,
    test_graduated_flag_behavior): 113 passed.
  • Firestore query-shape guard (test_firestore_query_shapes.py): 572 passed.
  • scripts/check_feature_flag_registry.py: OK.
  • Full pre-push hook and preflight through bin/omi-heavy: see the push.
  • Not verified: live production attach latency and the fleet-wide standalone
    window. The change is metadata-only and fail-open; the existing
    episode_batch_audio_close_attach fallback telemetry covers a failed attach.

Tests

Feature: backend/tests/unit/test_episode_batch_audio_close.py covers admission
attach, close finalize, boundary move and detach, the transcribed-member
refusal, the kill switch, and retry idempotency. The admission tests fail on
origin/main because 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 the
11-11 fix batch; this batch's predecessor).

Earlier head: 3cd22f779a386d7c562a5400dfe2e2593fde5733 (merge of main
7688c61a761d180a3b5fb6cd53c7ecd720e976dd into 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/main produced exactly one content conflict,
backend/utils/episodes/batch_close.py. Main's
0e985a4ccd0 added a paid-work preservation hook on the audio-close path
(owner = service.attach_recorded(...) then
late_member.joined(uid, conversation_id, row, owner, c)); this branch had
rewritten 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.joined exactly once on the close path:

        c = collaborators or service.Collaborators(firestore_client=conversations.db)
        moved_from = _moved_owner(uid, conversation_id, row, started, finished, c)
        owner = service.attach_recorded(
            uid,
            conversation_id,
            row,
            started,
            finished,
            c,
            pending_transcription=True,
            detach_from=moved_from,
        )
        late_member.joined(uid, conversation_id, row, owner, c)
        return 'attached'

The import became from utils.episodes import service, late_member.
backend/docs/feature-flag-registry.md and config/feature-flags.yaml
auto-merged; scripts/check_feature_flag_registry.py is OK and the admission
row sits in alphabetical position, so the flag count still matches
len(names).

Fix batch (11-12)

Head: b4e16433541f9ba3318ae5fbd4e50ef2b78b26ea
Branch: 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.

Finding Disposition
P0 — admission attach inert: both boundaries passed capture_snapshot, which drops every clock, so _admitted_start returned None and the attach returned 'skipped'. Fixed. conversations.py passes {**payload, **batch_record_capture}; preview_custody.py passes the full claim row. capture_snapshot stays the minimal eligibility snapshot. New boundary test drives both real call sites.
P1 — the 2h pending deadline is consumed by the capture, so a >2h capture projects "Couldn't transcribe" while recording. Fixed. Admission floors the deadline at started + MAX_EPISODE_SECONDS + SLA_SECONDS; the close path still stamps once and never renews. New 8000-second capture test.
P1 (merge risk) — the dirty resolution could drop late_member.joined. Verified. Called exactly once, on the close path only, after the attach.
P2-1 — detach_recorded can mutate a live episode and break the live writer's CAS. Fixed. Refuses status == 'live' and the episode_links/current target.
P2-3 — detach-then-attach is not atomic; a failed attach orphans the capture. Fixed. 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 in place.
P2-4 — the provisional start is the server arrival clock, not the recorded start. Fixed by the P0 row. _admitted_start already prefers server_batch_audio_start; the full row now carries it when present, and started_at/created_at otherwise.
P2-2 — detach_recorded leaves recorded_end/closed_at/compose proof stale. Deferred. The atomic move removes the stale membership; recomputing the old episode's derived fields is a separate change and does not affect the moved member.
P2-5 — test fixture fakes recorded_candidates; the boundary-move test used a live episode production never creates. Deferred. The boundary-move fixture now uses the production closed/gap shape required by P2-1; the recorded_candidates fake remains.
P2-6 — a 1-second provisional closed episode queues a throttled compose per Home read. Deferred. The composer returns 'waiting' for the pending member, so no paid work; the queue/read cost is a follow-up.
Snapshot-vs-stored guard gap (is_locked/deferred/relevance_decision). Deferred. The hard erasure fences are re-read in the membership transaction; the metadata-only guard gap is unchanged.
P1 (delta) — a delayed admission task can overwrite the audio-close result. Fixed. The admission attach re-reads the member inside its own transaction and returns closed with no writes once the archive is closed or server_batch_audio_end is real; an admission replay of a closed member is treated as already placed. New test_late_admission_after_audio_close_cannot_undo_the_close_end.

View guided diff Turn on auto-fix

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.
@Git-on-my-level
Git-on-my-level merged commit 8660141 into main Nov 11, 2026
66 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/rowflash branch November 11, 2026 22:12
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