Skip to content

Let a realtime finish that beats start open the socket itself - #422

Merged
alexkroman merged 7 commits into
mainfrom
claude/realtime-finish-before-start
Nov 25, 2026
Merged

alexkroman merged 7 commits into
mainfrom
claude/realtime-finish-before-start

Conversation

@alexkroman

@alexkroman alexkroman commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What & why

Builds on #421 ("Make finishing a phase of the realtime transcriber"), whose commits this branch contains. It targets main so CI runs (check.yml only runs on PRs into main); until #421 merges, the diff here includes #421's change. Merge #421 first.

Today, finish() on a session that hasn't started yet closes it and drops the audio queued for it. The Record button's Stop worked around this in 73ae910 with four pieces: an opened task, a Recording.opened field, an applyTurns(opened:) parameter, and await recording.opened.value before finish(). finish()'s doc also told callers to do the same.

This PR fixes it in the transcriber instead:

  • The turn stream exists from init. It is a new public nonisolated let turns, so it can be read before the socket opens. Whichever of start() and finish() comes first opens the socket.
  • finish() on an idle session with audio queued opens the socket itself. It then sends the audio and waits for Termination as before; a later start() does nothing. With nothing queued, finish() still just closes.
  • start() no longer returns the stream. Callers read turns.
  • LiveTranscriptionSession (from Drive the Record button in the Mac UI suite, on offline doubles for its capture and live session #410) follows: it requires turns and a start() that returns nothing. The UI tests' UITestLiveSession builds its stream in init and yields each scripted turn into it, which drops its owed count and isClosed flag: a turn released before the recording reads any waits in the stream, and a session ended before start has already ended its stream.
  • The app workaround is removed (TranscriptsModel+Recording.swift, TranscriptsModel.swift): the opened task, field, parameter and await are gone. applyTurns(from:) reads realtime.turns on any LiveTranscriptionSession.
  • finish()'s doc is rewritten to describe the new behaviour.

Behaviour change: calling finish() before start() no longer loses the queued audio. The socket opens, the audio is sent, and the final turn still arrives on turns. In the app, a Stop that lands before the socket has opened finishes the recording without waiting on a separate task.

How it was tested

  • New test finishBeforeStart: send audio, call finish(), then start(). It checks that the server received the 3,200-byte chunk and was sent Terminate, and that both the partial and the final turn arrived. No wall-clock waits.

  • Drive the Record button in the Mac UI suite, on offline doubles for its capture and live session #410's UI tests (TranscriptionUITests, the live-recording audit) exercise the reworked double end to end.

  • Written in a Linux sandbox with no Swift toolchain: scripts/check.sh --portable (jscpd included) passes, as do check-swift-size.sh and check-test-clock.sh. CI on macOS verifies the build, tests, swiftlint, swift-format and periphery.

  • scripts/check.sh passes (or CI will, if I'm not on a Mac)

  • I read AGENTS.md and this doesn't reintroduce anything
    deliberately removed

  • Docs updated if behavior changed

🤖 Generated with Claude Code

https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC

…n it

RealtimeTranscriber described one lifecycle with three variables: `phase`
(idle/open/closed), an `isFinishing` flag that was really a fourth phase,
and a `task` whose nil-ness repeated `phase` — the kind of split state
2c7e640 had to fix a race in. `Phase` now has `finishing`, and the
WebSocket task lives in the `open` and `finishing` cases, so it can't be
set without the phase that owns it or outlive `close`. `send`, `finish`
and `waitForTermination` switch on the phase; `isFinishing` and `task` are
gone.

Every transition maps 1:1 onto the old one: no behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
finish() on a session not yet started closed it and dropped the audio
queued for it, so the Record button's Stop worked around it: 73ae910
added an `opened` task, a `Recording.opened` field, an `applyTurns(opened:)`
parameter and an `await recording.opened.value` before finish(), and
finish()'s doc told callers to do the same.

The fix now lives in the transcriber. Its turn stream exists from init
(`turns`), so it can be read before the socket opens, and whichever of
start() and finish() comes first opens it: finish() on an idle session
with audio queued opens the socket, sends the audio, and waits for
Termination as usual; start() after that is a no-op. With nothing queued
it still just closes. start() no longer returns the stream; callers read
`turns`. The app drops the `opened` task, field, parameter and await.

Behaviour change: a finish() before start() no longer loses the queued
audio — it is sent and its turns arrive. A new test covers it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
finishBeforeStart repeated audioBeforeStart's setup and session's
expected turns closely enough for jscpd to count two clones. A
queued(on:) helper and a helloTurns constant now hold them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
@alexkroman
alexkroman changed the base branch from claude/realtime-finishing-phase to main November 25, 2026 20:49
One conflict: #410 made RealtimeTranscriber conform to
LiveTranscriptionSession on the same line this PR documents Phase under.
Kept both; this PR's API is unchanged, so the conformance still holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
#410 put the Record button's live session behind LiveTranscriptionSession,
whose start() returned the turn stream, and gave the UI tests an offline
double built the same way. This PR moves the stream to a `turns` property
that exists from init, so:

- LiveTranscriptionSession now requires `turns` and a start() that returns
  nothing, the shape RealtimeTranscriber has here.
- UITestLiveSession builds its stream in init and yields each scripted
  turn straight into it. The `owed` count and `isClosed` flag, which
  covered a Next Turn or a Stop arriving before start, go: a stream that
  exists from the start buffers the one and has already ended for the other.
- applyTurns takes any LiveTranscriptionSession and reads its `turns`.
- The stream's init locals are renamed so they don't shadow the stored
  properties (variable_shadowing, which #410 also hit).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
@alexkroman
alexkroman enabled auto-merge November 25, 2026 21:11
@alexkroman
alexkroman added this pull request to the merge queue Nov 25, 2026
alexkroman pushed a commit that referenced this pull request Nov 25, 2026
… CI run

Recent multi-commit PRs (#410, #413, #414, #416, #420, #422, #423, #425, #426)
mostly spent their extra ~37-minute runs on things nothing caught before CI:

- scripts/check-swift-lint-lite.sh: a toolchain-free text match for the opt-in
  SwiftLint/swift-format rules it can see (modifier_order, no_empty_block,
  NoBlockComments, swift-format's `catch  where`). Run by check.sh (so
  --portable and --lint-only) and the swift-budgets edit hook; would have
  flagged the pre-fix files of #414, #422 and #425.
- .claude/hooks/push-gate.sh: a PreToolUse hook that runs
  `check.sh --portable` (~20 s) before any `git push` and blocks a red one —
  jscpd clones (#420, #422) and post-merge size overruns (#363, #378) were
  pushes that skipped the documented portable run.
- AGENTS.md / CLAUDE.md: a "Rules only CI catches" list for the rest
  (variable_shadowing, one_declaration_per_file, type_contents_order,
  multiline_function_chains, MemberImportVisibility, one argument per line),
  and the new hook and script in the hooks table and permissions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1UDUZXyuF6g7GUmmNu
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 25, 2026
@alexkroman
alexkroman enabled auto-merge November 25, 2026 21:44
#421 landed as a squash of the commits this branch was built on, so git
saw its RealtimeTranscriber, LiveTranscriptionSession and tests changed
twice. main's copies of those three files are exactly #421's, and this
branch's are #421's plus this PR's change, so they are kept from here;
a plain merge would have left two start() functions in the transcriber.
#398's import and TextReader lines come in unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC

Copy link
Copy Markdown
Collaborator Author

The ios-audits (a) failure here isn't from this PR, which changes no iPhone code. testSettings failed on the default-size audit's "Dynamic Type font sizes are unsupported" for the three rows at the foot of Settings' last page ("How fast can you type?", "Powered by AssemblyAI", "AssemblyAI's privacy policy"). That's the intermittent finding #398's ScalingCheck was added to catch. The same failure took #423 out of the merge queue an hour ago. Every other job passed, and I've re-run the failed job once.


Generated by Claude Code

@alexkroman
alexkroman added this pull request to the merge queue Nov 25, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 25, 2026
@alexkroman
alexkroman added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 9016868 Nov 25, 2026
36 of 38 checks passed
@alexkroman
alexkroman deleted the claude/realtime-finish-before-start branch November 25, 2026 23:54
alexkroman pushed a commit that referenced this pull request Nov 25, 2026
Three conflicts:
- DictationLog.swift: #431 moved the log-removal sweep to
  DictationLog+Removal.swift and made the logger internal for it; this PR
  had moved the withheld placeholders to DictationLog+Withholding.swift.
  Each now lives once, in its own file.
- DictationSession+Pipeline.swift: this PR's DictationLog.loggable call,
  followed by #431's sentPrompt for the log.
- DESIGN.md: main's table (#431 renamed SyncSTTLimits), with this PR's
  HopReturn row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
alexkroman pushed a commit that referenced this pull request Nov 26, 2026
#422 rewrote the Record button's live-session double inside
TranscriptsModel+UITest.swift; this branch had moved that double to
UITestLiveSession.swift, so the rewrite lands there and the model file
keeps only its factory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1VPQPwfs6KaTp1BQKW
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.

2 participants