Skip to content

fix(desktop): await physical mic teardown - #11031

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
peterolkhov:codex/fix-mic-restart-teardown
Aug 4, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
peterolkhov:codex/fix-mic-restart-teardown

Conversation

@peterolkhov

@peterolkhov peterolkhov commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

Fixes the live microphone-switch teardown race tracked in #10921.

Settings previously stopped transcription and guessed that CoreAudio teardown would finish within one second. AudioCaptureService.stopCapture() deliberately performs AudioDeviceStop and IOProc destruction asynchronously, so a slow Bluetooth teardown could still own the device when the replacement capture opened.

  • Expose the existing local-transcription stop task as a completion seam.
  • Await both logical transcription shutdown and waitForPhysicalStop() before reopening capture.
  • Advance and reuse recordingGeneration for both start and stop decisions, preventing an older settings restart from overriding a newer user action.
  • Remove the fixed one-second timer.

No new capture owner, dependency, compatibility path, or polling loop is introduced.

Product invariants affected

none

How it was verified

  • xcrun swiftc -frontend -parse <changed Swift files> — passed.
  • Homebrew swift-format 603.0.0 format --configuration desktop/macos/Desktop/.swift-format over all changed Swift files — no diff. This is a cross-check only; CI remains authoritative for the repository-pinned 602.0.0 formatter.
  • python3 .github/scripts/run_checks.py --lane local --base origin/main --head HEAD --skip-pr-body-checks — all checks through desktop-main-actor-xctest-hooks passed; the runner then stopped at desktop-swift-format-lint because this machine has Command Line Tools only and xcrun --sdk macosx --show-sdk-platform-path cannot resolve a full Xcode platform SDK.
  • python3 desktop/macos/scripts/check_desktop_test_quality.py — passed.
  • python3 desktop/macos/scripts/check-e2e-flow-coverage.py --strict --base origin/main — 3/3 changed production Swift files covered.
  • python3 .github/scripts/desktop-changelog.py validate — passed.

Not locally verified: Swift compilation, XCTest execution, pinned Swift formatting/SwiftLint, named-bundle launch, or physical Bluetooth microphone switching. This PR is draft until the macOS CI lanes pass and the live hardware path is exercised.

Tests

TranscriptionSettingsRestartTests drives the production restart boundary through a suspended fake physical stop on the default on-device STT path. It proves the replacement cannot start before teardown completes and, through a real second stopTranscription() call, that a user stop during teardown cancels the stale restart.

Failure class (fixes)

Failure-Class: none

New guards

No repository check or ratchet is added. The focused behavioral regression test would have caught this instance.

Replace the settings restart timer with the existing physical-stop boundary, wait for on-device STT shutdown, and fence stale restarts with the recording generation.

Verification: Swift parse, diff-scoped portable manifest checks, desktop test-quality, E2E coverage, changelog validation, and swift-format 603 no-diff cross-check passed. Full Xcode compile/tests and pinned 602 formatting were unavailable because this machine has Command Line Tools only.

Failure-Class: none
@peterolkhov
peterolkhov marked this pull request as ready for review August 2, 2026 21:18

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Advance the existing recording generation for stop decisions and exercise the cancellation through the production stop path in the regression test.\n\nVerification:\n- Swift frontend parse passed for changed extension and test files\n- swift-format 603 cross-check produced no diff\n- desktop test-quality check passed\n- desktop E2E flow coverage passed (3/3)\n- desktop changelog validation passed\n\nThe repository-pinned formatter and XCTest remain unavailable without full Xcode on this machine.\n\nFailure-Class: none
@Git-on-my-level

Copy link
Copy Markdown
Member

@/tmp/tmp2xigj0jj.md


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added needs-maintainer-review Needs a human maintainer to sign off before merge macOS labels Aug 3, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Confirmed still broken on main: restartTranscriptionIfNeeded() guesses CoreAudio teardown finishes in 1.0s via asyncAfter. Awaiting waitForPhysicalStop() plus the recordingGeneration fence is the right shape, and TranscriptionSettingsRestartTests covers it. Holding the merge until the Desktop Swift lane finishes — first CI run for this author, and the PR body notes Swift compilation was not verified locally.

@Git-on-my-level

Copy link
Copy Markdown
Member

Good fix and well-structured. Confirming the technical review on the current head (195eed76):

  • The teardown race is real and this is the right shape: exposing stopTranscription() as a @discardableResult completion seam and awaiting waitForPhysicalStop() before reopening capture removes the 1.0s asyncAfter guess, which is the correct fix for slow Bluetooth teardown.
  • The earlier recordingGeneration gap (a user stop during settings-driven teardown wouldn't invalidate the restart) is resolved here — stopTranscription() now advances the counter on every stop, and prepareTranscriptionRestartAfterSettingsChange() snapshots the generation after its own stop and re-checks it after teardown. testSettingsRestartDoesNotReopenAfterUserStopsAgainDuringTeardown pins that path.
  • No new capture owner, permission, data-flow, or polling loop introduced; the change stays within the existing local-STT and cloud paths.
  • Desktop Swift build/test + static/test-contract lanes are green, and the changelog entry is in place.

One thing worth a human maintainer's eye before merge: stopTranscription() now returns nil on the cloud path, so callers that previously relied on fire-and-forget behavior are unaffected, but confirm there's no settings-restart path for cloud STT that should also use the new seam (the current restartTranscriptionIfNeeded only meaningfully gates local capture teardown, which is fine — just flagging for completeness).

Leaving a positive signal; formal merge sign-off stays with the human maintainer track given this is the author's first contribution and it touches the live microphone lifecycle.


by AI on behalf of David — if you need David's attention urgently, please @Git-on-my-level and escalate with need human response.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Confirmed the 1.0s asyncAfter guess is still on main (SettingsContentView+Transcription.swift:325) — awaiting waitForPhysicalStop plus the recordingGeneration fencing is the right fix for a CoreAudio teardown race. Desktop bug fix — merging (owner-override).

@kodjima33
kodjima33 merged commit df465aa into BasedHardware:main Aug 4, 2026
35 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

macOS needs-maintainer-review Needs a human maintainer to sign off before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants