Skip to content

fix(capture): one live source at a time; a phone pause keeps its conversation - #18839

Merged
Git-on-my-level merged 4 commits into
mainfrom
fix/capture-one-source-at-a-time
Sep 25, 2026
Merged

Git-on-my-level merged 4 commits into
mainfrom
fix/capture-one-source-at-a-time

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

One live source at a time: a phone pause keeps its conversation, and the pendant hands off and comes back

This is PR 1 of 2 for the capture UX David approved on 2026-09-25 (storyboard: "Capture UX: live card + phone button"). It changes capture behaviour only. PR 2 builds the UI on top of it.

What was wrong on main

Each point below was reproduced by a failing test first. The run before the fix had 7 failures, and the output is quoted under Verification.

  • (a) Phone Pause split the conversation. The live page's phone Mute called stopStreamRecording() and Unmute called streamRecording(). That minted a new recording id, which becomes a new client_conversation_id on /v4/listen, so the backend created a second conversation. The first half stayed in_progress, because the server finalizes phone sockets on close only for desktop.
  • (b) Stopping the phone left capture muted. A user stop wrote the single capture-policy mute and never undid it. A pendant that was paused, or connected later, came up paused.
  • (c) Tapping Record with a pendant live mixed both into one socket. streamRecording() → changeAudioRecordProfile() → _resetState() re-opened the pendant's BLE stream. The phone's PCM socket then replaced the pendant's, and the pendant listener kept writing its frames into it. The phone's stop then left the pendant muted.
  • (d) Omi calls never touched capture. A live pendant kept capturing through a call.

The fix

The capture policy stays the single durable mute authority (FC-split-mutation-authority); this PR adds no second mute flag. "Separate pause" (b) holds because only one source is ever live. A stop restores the state its recording started from, and a handed-off pendant is restored explicitly.

  • (a) Real phone pause. pauseCapture() mutes first, which retires the in-flight callbacks. It then flushes the frames captured so far, releases the mic, and keeps the socket and the recording id. resumeCapture() unmutes, reopens the socket only if it closed, and restarts only the mic through _resumeMicRecording, so the conversation continues. A pause that outlasts the server's conversation timeout still splits on the backend's lifecycle loop, as it should.

  • (b) The stop restores the pre-recording state. A user stop of a live phone recording mutes (retiring callbacks) and then restores the policy it started from. Transcribe Later keeps its mute across stop, unchanged.

  • (c) Explicit handoff. An explicit phone start with a realtime pendant streaming or user-paused hands the pendant off:

    • its Dart BLE stream and native background streaming close;
    • its conversation is processed if it has content, and the processing request is awaited before the phone's socket can change the in-progress conversation;
    • the pendant resumes by itself when the phone stops, live or paused as it was before.

    finishCapture() is the one Finish. It stops, processes the phone conversation, and then resumes the pendant, so the processing request cannot reach the pendant's next conversation. Other cases:

    • A pendant that connects during a phone recording waits the same way.
    • A pendant connecting or disconnecting no longer rolls the phone's capture session.
    • A pendant that disconnects mid-handoff is not resumed.
  • (d) Omi calls. PhoneCallProvider.callStateListenable is wired in production composition. Connecting, ringing and active pause a streaming pendant without touching the policy or its conversation, and the pendant resumes when the call ends. A user-paused pendant stays paused. Call-driven switches are serialized.

  • API. pauseCapture / resumeCapture / finishCapture are now the one pause/stop API for the live page, the Home capture card, quick actions and the Home button. These call-site swaps are small; the UI redesign is PR 2. New getters for PR 2: pendantPausedForPhone, pendantPausedForCall, isPhoneMicPaused.

  • Test seam. A device-connection seam (deviceConnectionLoader) replaces the direct ServiceManager…ensureConnection calls inside the controller. ScriptedDeviceConnection lets the replay world stream real BLE packets through the production path.

  • Docs. CAPTURE_POLICY.md has a new "Live sources" section, and its phone-stop rule is updated.

Known gaps (deferred, with reasons)

  • Transcribe Later pendants are not handed off. Their native batch writer is gated only by the shared policy, so pausing the pendant while the phone records needs a per-source native gate (iOS and Android). This is documented in CAPTURE_POLICY.md.
  • Phone live sockets declare no source. This is unchanged; the backend defaults the source to omi, while the keep-alive reconnect sends phone. It is out of scope here, and the tests assert only "not omi" for the phone socket.

Verification (hermetic, Flutter 3.44.5, TZ=UTC)

  • Before the fix: flutter test test/services/capture/capture_one_source_test.dart gave 2 passed and 7 failed:

    • (a) the state after Pause was stop, not pause, because the recording ended.
    • (b) isPaused was true after a phone stop, and a pendant connected afterwards came up pause.
    • (c) after the phone stopped, isPaused was true (the pendant was left muted), and a user-paused pendant ended in stop, not pause. One (c) assertion was wrong in the first draft: phone sockets on main declare no source. I corrected it to assert "not omi".
    • (d) the pendant's audio subscription stayed open (1) during a call.

    The socket-mixing half of (c) is established by reading the code, not by a failing run: the corrected assertion was not re-run against main. After the fix, the test asserts that no pendant bytes reach the phone socket and that the pendant stream is closed.

  • After the fix: 17 of 17 pass. That covers the 9 above plus Finish ordering, pendant connect and disconnect during a phone recording, the live-source getter, and the Grok-review scenarios (BLE drop during a call, a call that ends while the phone takes over, Transcribe Later toggled while paused, process death during a pause). One more bug was found while writing PR 2 and has a failing test: the phone reused the pendant's recording id, so it would have joined the pendant's conversation.

  • bash app/test.sh: 3053 tests passed, plus 138 in the spine suite, after rebasing on d3d945cce2.

  • The analyzer ratchet passes, and dart analyze on the changed files is clean.

  • This is not verified on a physical device (BLE, native background streaming, OS mic release); those remain device checks.

Cursor Grok review (grok-4.7-high via agentctl, read-only, including misses beyond the diff)

Grok raised 15 findings (1 P0, 8 P1, 4 P2, 2 P3).

Fixed in this PR

  1. P0. A pendant BLE drop and reconnect during an Omi call cleared the call handoff, so the pendant streamed into the call. The call handoff now outlives a BLE drop, and streamDeviceRecording returns early while a call is active. Tested.
  2. P1. streamDeviceRecording during a call handoff wiped the transcript and rolled the session. It now returns early, as above. Tested (same recording id after the reconnect).
  3. P1. A phone Transcribe Later stop (offline auto-batch) returned before resuming a handed-off pendant. The batch branch now resumes it; Transcribe Later's own mute rule is unchanged.
  4. P1. Speech-profile onboarding stopped the phone and resumed the pendant while onboarding needed the mic, and it ignored a paused phone. It now stops with resumeHandedOffPendant: false and treats isPhoneMicPaused as a live phone.
  5. P1. A stall or onConnected during a pause could restart the mic. _onMicStalled, _restartPhoneMicRecording, _resumeMicRecording and onConnected now all respect _phoneMicPaused.
  6. P1. setBatchMode while paused rolled the recording under the mute and stranded it. A paused recording now keeps its mode, and the new mode applies to the next recording. Tested.
  7. P1. onTranscriptionSettingsChanged opened a device socket while the phone or a call owned the capture. It now branches on the owning source.
  8. P1. If the stop's mute write was superseded, finishCapture resumed the pendant anyway. stopStreamRecording now returns whether it stopped, and finishCapture resumes only then.
  9. P1. Phone handoffs were not serialized with call-driven switches. Every handoff and resume now runs on one queue, and each step re-checks its condition when it runs. A phone start over a call-paused pendant converts the handoff to the phone and ends the pendant's recording. Tested.
  10. P2. A raced mute write during resume left the pendant stranded. The resume now continues, and the newer intent decides the pause state.
  11. P2. A process death mid-recording or mid-pause left the next launch muted. A persisted marker now restores the pre-recording policy at startup. Tested with a kill and reconstruct.

Deferred, with reasons

  • P2, finding 11. Phone pause and resume share the policy with a Transcribe Later pendant. That combination is not reachable: Transcribe Later is global, so a phone recording in that mode is batch too, and batch mute sharing is pre-existing. A per-source native gate is the real fix and is listed under known gaps.
  • P2, finding 13. An Omi call does not pause a live phone-mic recording. The OS audio-session interruption already handles the mic (_micInterrupted), and a dedicated phone-mic pause for calls is a follow-up.
  • P3, finding 14. Accessibility labels, the card's local _isPhoneMicPaused, small screens, and the "pendant waiting" state are all in the UI PR (PR 2). Android keeps the foreground hold while paused on purpose, so the session survives. The OS mic indicator goes off because the mic is released.
  • P3, finding 15. Remaining untested paths: stall during pause, superseded finishCapture, and socket drop during pause. They are guarded in code but not covered by a scenario, because the replay world cannot yet drive a native stall or a raced policy write.

Line count

Line-Count-Exception: app/lib/services/capture/capture_controller.dart | 3137 -> 3459 | The source-switch rules (handoff, restore, pause) read and mutate controller-private stream, socket and session state, so extracting them means splitting the controller, which is a separate change. About 80 of the added lines are doc comments on the new rules; about 75 more came from the Grok review fixes.

Product invariants affected

none

Failure class (fixes)

Failure-Class: FC-unowned-session-mutation-tears-down-live-capture

🤖 Generated with Claude Code

Git-on-my-level and others added 3 commits September 25, 2026 11:33
…ersation

- Phone pause mutes, releases the mic and keeps the socket and recording id;
  resume unmutes and restarts only the mic (_resumeMicRecording). Before,
  Pause/Resume stopped and restarted the recording: a new client conversation
  id, so a second conversation, with the first stranded in_progress.
- A user stop of a live phone recording restores the policy it started from,
  so it no longer leaves a (later) pendant muted.
- An explicit phone start hands off a live pendant (BLE streams closed, its
  conversation processed) and the pendant resumes by itself when the phone
  stops, in its prior live/paused state. finishCapture() processes the phone
  conversation before that resume. Before, the phone socket replaced the
  pendant's while the pendant listener kept writing into it.
- A pendant (re)connecting during a phone recording waits and never rolls the
  phone's capture session.
- An Omi phone call pauses a streaming pendant and resumes it after.
- pauseCapture/resumeCapture/finishCapture are the one API for the live page,
  the capture card and quick actions. Device-connection seam for tests.

Failure-Class: FC-unowned-session-mutation-tears-down-live-capture

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

Failure-Class: FC-unowned-session-mutation-tears-down-live-capture

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

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agentic security review for PR 18839: one medium finding on paused phone-listen socket reuse across sign-out.

Open in Web View Automation 

Sent by Cursor Security Agent: Security Reviewer

Comment on lines +3329 to +3336
Future<void> _resumePhoneRecording() async {
final revision = await _setCaptureMuted(false);
if (!_admitsCapture(revision)) return;
_phoneMicPaused = false;
// The socket may have closed during a long pause; reopen it for the same recording.
if (_socket?.state != SocketServiceState.connected) {
await _initiateWebsocket(audioCodec: BleAudioCodec.pcm16, sampleRate: 16000);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Agentic Security Review
Severity: MEDIUM

A phone pause now keeps the authenticated /v4/listen socket and recording id, and resume reuses that socket whenever it is still connected. Sign-out does not stop this socket, and the new phone-owns path skips the session roll that device detach used to run, so the previous account's listen connection can remain current.

Impact: On a shared phone, the next account can resume into the previous account's already-authenticated listen socket. Later PCM frames are not re-bound to the signed-in user, so the new account's microphone audio can be transcribed into the previous account's open conversation.

Fix in Cursor Fix in Web

Reviewed by Cursor Security Reviewer for commit 02b3e8e. Configure here.

- A call handoff outlives a pendant BLE drop; streamDeviceRecording returns
  early while a call is active (no transcript wipe, no session roll).
- Phone handoffs and resumes share the call-switch queue; each step re-reads
  its condition. A phone start over a call-paused pendant takes the handoff.
- The handoff closes the pendant's recording so the phone mints its own id.
- A pause is not undone by stall/reconnect recovery or a Transcribe Later
  toggle; settings refresh follows the owning source; a superseded stop
  reports it, and Finish only resumes the pendant after a real stop.
- A persisted marker restores the pre-recording policy after a process death.
- Speech-profile onboarding keeps a handed-off pendant paused.
- liveCaptureSource / liveCaptureStartedAt for the live card.

Failure-Class: FC-unowned-session-mutation-tears-down-live-capture

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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