Repository navigation
perf(dictation): send desktop dictation audio straight to the speech process instead of through main - #39346
Merged
Conversation
… the port handoff registers
…re the port capability
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 2 minutes. View limit details
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. |
This was referenced Nov 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ELI5
When you dictate on desktop, the microphone audio used to go through Orca's main process on its way to the speech engine. Now the window sends it straight to the speech engine. Main no longer handles every slice of audio, and the text you get is the same.
What Changed
Before: every 100 ms frame went renderer →
ipcRenderer.invoke('speech:feedAudio')→ main →SttService.feedAudio→postMessageto the speech utility process. That is about 10 IPC round trips a second through main for each dictation.After, for on-device dictation on desktop:
speech-bridge.ts): for each dictation it makes aMessageChannel. It posts one end to main withipcRenderer.postMessage('speech:audioPort'), right before thestartDictationinvoke on the same ordered pipe. It keeps the other end for that session.ipc/speech.ts): holds the port until the start is accepted. ThenSttService.attachAudioPort(owner, port)transfers it to the speech process, bound to that owner's stream. The start replies{ directAudio }. Any port that isn't used gets closed.directAudiois true.stopDictationposts{ type: 'end' }and closes the port, then sends the stop over IPC as before.stt-worker.ts): feeds the bound stream from the port. It holds astopuntil that port'send(or itsclose, if the window went away), so the last frames always land before the stream finishes. Main'sstoptravels a different pipe and could otherwise overtake them.feedAudio's upload-cap answerattachAudioPortreturns false)Why
This is plan item W1.4 part two. Audio no longer queues behind whatever main is doing, and main does no work per frame.
Alternatives I ruled out:
ipcRenderer.send. It saves only the reply, and main still touches every frame.Creating the channel in the preload keeps the renderer API unchanged:
DictationControlleranduse-audio-capturedon't change. Startup audio is already buffered until the start resolves, so a session's frames all take one path.A blind design challenge came back SAME.
Linked Issue
Plan item W1.4 (part two) of the dictation plan; no GitHub issue. Stacked on #39305 (merged).
Visual Proof
N/A: nothing on screen changes. The proof is the measurement and transcript parity under Testing.
Testing
All runs were real dictation in a hidden dev build (
ORCA_BACKGROUND_LAUNCH=1) on macOS arm64, with the local Parakeet TDT v2 model. A fake microphone (--use-file-for-fake-audio-capture) played a 17 s, 48 kHzsayclip.Same build, A/B. I removed main's
speech:audioPortlistener at runtime to force the IPC fallback, then put it back. Four runs each way, 17 s of audio:speech:feedAudiohandler calls per dictationThe per-frame cost in main was small, so total main-thread time differs only within noise. The change removes 10 IPC round trips per second and main's hop from the audio path. Measured before the change on the base branch build: 173–244 handler calls per dictation.
Other cases:
Crash/restart: I killed the speech process with
kill -9mid-dictation. The existing "speech engine stopped unexpectedly" toast showed and dictation ended. The next dictation started a new process, sent audio over a new port (0 IPC feeds) and inserted the full transcript. Re-checked after the last refactor.Stop while the model is loading: a stop at 400 ms and at 900 ms into a cold start. The buffered audio flushed over the port before
end. Port and IPC gave matching text (e.g. "Please refactor the" both ways).IPC fallback: with the port refused, dictation used IPC (173 feeds) and transcribed the same.
Rebased onto main with the new pre-warm (feat(dictation): load the speech model ahead of the first dictation, with an unload setting #39327): two dictations and a stop-while-starting run all used the port (0 IPC feeds) and inserted the full transcript.
Focused tests: 57 files under
src/main/speech,src/main/ipc/speech.test.ts,src/preload,src/renderer/src/components/dictationandsettings/voice. A scopedtscover the changed files and their tests.check:code-quality:changedandcheck:react-doctor:changedare clean.I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
One test was added in
stt-service.test.ts. A port reaches only its own owner's on-device stream, and a cloud session refuses it. It failed when the binding used another stream id. The worker's "hold stop untilend" logic runs only on the native addon (no unit tests exist for the worker), so the real runs above cover it.Risk
The riskiest part is the stop hold in the speech process. If a port-bound stream's stop never got an
endorclose, main's existing 60 s stop timeout would kill the shared speech process, which ends every device's dictation.Every stop the desktop controller sends goes through the preload, which posts
endfirst. Main's own stops either discard (renderer gone, no hold) or follow an error that the renderer answers with its own stop. A window that goes away closes its port, which also releases the hold.What else could break:
MessagePortMainbehaviour on Linux and Windows. It is the same Electron API on every platform, but I only ran macOS.window.api.speechAPI is unchanged.To back it out, revert the PR.
Level: Medium
AI Disclosure
Review
Two Claude review rounds (Codex is at its usage limit):
speech.tsIPC test'sipcMainmock lackedon. Fixed.P3s left as is:
DictationControllernever does that.samplesis a Float32Array. A compromised page could already crash the speech process over IPC.Agent skill upstream boundary
Notes
utilityProcessandMessageChannelports exist on all three platforms, with no platform branches. Only macOS was run.orca serveuse the forked child or RPC.kill -9opens the "Orca closed unexpectedly" crash-report dialog ("Utility killed (exit 9)"). That dialog takes focus, so a later dictation types into its text box. This comes from the crash reporter's handling of the speech utility process (fix(speech): run on-device dictation in its own process so an engine crash or hang can't take Orca down #39297), not this PR.{revision, committed, tentative}events are the natural time to do that.For Kelvin
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)