Skip to content

fix(dictation): keep a microphone that opens late instead of dropping the press - #503

Merged
GeiserX merged 3 commits into
mainfrom
fix/slow-mic-open
Nov 2, 2026
Merged

GeiserX merged 3 commits into
mainfrom
fix/slow-mic-open

Conversation

@GeiserX

@GeiserX GeiserX commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Problem

A USB microphone that has gone idle can take more than 3 s to wake. When the dictation key was pressed with the window closed, the helper got the key, but its open of the microphone gave up after a fixed 3 s budget. It then closed the stream that was about to arrive and tried again 2 s later. The session waited for a first sample that never came and ended with nothing dictated. Nothing on screen said why; app.log only had mic-open: the microphone did not open in time. A second press worked at once because by then the device was awake. The 3 s wait also blocked the worker loop, so the key tap's queue and paste receipts sat behind it.

Windows and Linux had the same flaw: their dictate microphones wait REBUILD_BUDGET (3 s) through the same worker open.

Fix

  • Device::open now only starts the open and returns. A new Device::opened hands back the outcome when it arrives. The worker checks it every step and as soon as a buffer from that stream shows up. From that moment the session records, for as long as it still wants the mic. Only the pre-roll from before the open is lost.
  • No second stream is opened while one is still opening. When the session no longer wants the mic, an open still in progress is dropped without waiting; its worker drops whatever it opens and exits. macOS (MicWorker::begin_open), Windows (Worker::begin_open) and Linux (Handle::begin_open) share the change. The blocking open(budget) stays for recording, which has its own 15 s start budget and reports failures as a refused start.
  • A new helper line, mic.failed, goes out once per session in three cases: the open failed, the open has run for 3 s, or the session ended before the mic opened. The app shows it in the pill as "The microphone didn't open" for ERROR_MS. If the mic opens later, or the next press works, the session's listening state replaces the message.
  • If the mic opens normally, nothing changes.

Trap T4.39 is added to docs/TRAPS.md, and mic.failed to the protocol table in docs/ux/DICTATION.md.

Tests

  • t4_39_* in native/akou-capture/src/dictate/live.rs use a fake device whose open answers when the test says:
    • opens at 4 s: opened once, records from 4 s to the release, told once
    • opens at once: records from the press, tells nothing
    • never opens: opened once, told once, and the worker keeps running after the key-up
    • session ends at 1 s, before a 2 s open: told at the key-up
  • Positive controls, run as mutations:
    • Giving up the open after 3 s, the old behaviour, fails the late and never-opens cases. Each shows ["open default", "close", "open default", "close"].
    • Removing the key-up message fails the 1 s case.
    • Removing the pill's handler fails the [T4.39] pill unit test and the whole-app test.
  • [T4.39] in tests/dictation-pill.test.ts: the error and when it ends. Controls: a press with nothing after it says nothing, and a session's island is never replaced. There is also a whole-app case with the fake helper's new --mic-never-opens switch, controlled by a normal press.
  • [T4.39] in tests/dictation-session.test.ts, plus the shared helper-lines fixture.
  • Local runs on macOS: cargo test with and without simulate, cargo +1.97.1 clippy -D warnings and cargo fmt --check are clean. bun run check: 3603 pass, 0 fail. The Windows and Linux backends could not be built here, so CI covers them.

The simulator's --mic-open-delay delays the first sample and has no open budget, so it does not reproduce this bug: the slow open lived only in the device follower and the backends. The fake device in live.rs is how the bug is reproduced.

Summary by CodeRabbit

  • New Features
    • Dictation can continue while the microphone starts, including on slower devices.
    • If the microphone fails to open, takes longer than 3 seconds, or the session ends before it opens, a brief error notice appears. A later successful microphone open clears the notice, and a new press retries an open attempt that has taken at least 3 seconds.
  • Documentation
    • Updated dictation guidance to describe microphone startup, retries, and failure notices.

… the press

A microphone waking from idle can take more than 3 s to open. The
dictate helper gave the open up at that budget on every OS, closed the
stream that was about to arrive, and the press ended with nothing
dictated and only a warn line in the log. The wait also held the worker
loop.

The open now starts and returns; the worker takes its outcome each step
and uses the stream from the moment it arrives while the session still
wants it. Nothing opens twice, and an open still under way is given up
without waiting when the session ends. A session waiting on a mic that
failed or has been opening for 3 s, or that ends before the mic opened,
gets a new `mic.failed` line, which the pill shows as "The microphone
didn't open" until the next session's island replaces it.

Trap T4.39.
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between e780134 and 9fce55c.

Files selected for processing: 4
  • docs/TRAPS.md
  • docs/ux/DICTATION.md
  • native/akou-capture/src/dictate/live.rs
  • native/akou-capture/src/windows.rs
Files skipped from review as they are similar to previous changes: 1
  • native/akou-capture/src/windows.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

Dictation microphone opening now runs asynchronously across Linux, macOS, and Windows. The live capture flow tracks pending opens and reports mic.failed when an open fails or a press waits too long. The app forwards the event to the pill, which displays a timed error outside listening or transcription states.

Changes: Dictation microphone startup

Layer / File(s) Summary
Asynchronous device open
native/akou-capture/src/linux.rs, native/akou-capture/src/macos/mic.rs, native/akou-capture/src/windows.rs, native/akou-capture/src/dictate/{linux.rs,mac.rs,win.rs}
Microphone open requests now return before the device result is ready. The platform wrappers poll for completion and use a zero-duration close when abandoning a pending open.
Live open and failure reporting
native/akou-capture/src/dictate/{live.rs,mic.rs,session.rs,protocol.rs}, docs/TRAPS.md
The live microphone flow tracks pending opens and waiting presses. It reports mic.failed once when an open fails or a press waits at least 3 seconds. Late success can still start the session. Tests cover delayed, immediate, and incomplete opens, plus a session ending during an open.
App failure event and pill notice
src/main/dictation/{protocol.ts,session.ts,service.ts}, src/main/window/pill.ts, scripts/fake-helper.ts, tests/dictation-*.test.ts, tests/fixtures/akou-dictate/helper-lines.jsonl, docs/ux/DICTATION.md
The app accepts mic.failed, logs a warning, and forwards a mic-failed event. The pill shows “The microphone didn't open” for ERROR_MS, unless it is listening or transcribing. The fake helper, fixture, documentation, and tests include the event.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Possibly related PRs

  • GeiserX/akou#497: Both changes address slow microphone startup before the first sample. PR #497 changes the macOS smoke expectations for session.started; this PR tracks pending opens and handles their eventual result.
Merge Risk: Minimal · up to 9fce5

No actionable merge-blocking issue is established from the supplied evidence; proceed with normal checks, including Windows and Linux CI.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 61.76% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: dictation now keeps a microphone that opens late instead of dropping the press. The changeset makes microphone opening asynchronous, lets a slow open complete while th…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: dictation now keeps a microphone that opens late instead of dropping the press. The changeset makes microphone opening asynchronous, lets a slow open complete while the session waits, and adds a mic.failed notification for opens that fail or exceed 3 seconds. The title is specific, follows conventional commit form, and contains no file lists or vague terms.

Full details: Docstring Coverage

Explanation

Docstring coverage is 61.76% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 34 functions across 18 files. (2 skipped: 2 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai 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.

  • Fix comments: Fix CodeRabbit comments on this PR

Comment thread native/akou-capture/src/dictate/live.rs Resolved
…t while opening

A press now gives up an open that has already run 3 s and starts a fresh
one, so a device stuck in the OS no longer blocks every press of the warm
hold. A Lost the stream posts before its open answers is a failed open:
the press is told and the device is opened again, instead of a dead stream
being taken as live. Tests pin the key-up give-up step and the buffer that
arrives ahead of the open's answer, and the docs say what the worker
guarantees about overlapping opens.
@GeiserX
GeiserX merged commit c0267ec into main Nov 2, 2026
30 checks passed
@GeiserX
GeiserX deleted the fix/slow-mic-open branch November 2, 2026 11:47
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