Skip to content

perf(dictation): send desktop dictation audio straight to the speech process instead of through main - #39346

Merged
AmoabaKelvin merged 4 commits into
mainfrom
dictation-direct-port
Nov 7, 2026
Merged

AmoabaKelvin merged 4 commits into
mainfrom
dictation-direct-port

Conversation

@AmoabaKelvin

@AmoabaKelvin AmoabaKelvin commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

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.

Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​20 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​19
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​165 $\color{#cf222e}{\Huge{\mathbf{−}}}$​18 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​147

What Changed

Before: every 100 ms frame went renderer → ipcRenderer.invoke('speech:feedAudio') → main → SttService.feedAudio → postMessage to 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:

  • Preload (speech-bridge.ts): for each dictation it makes a MessageChannel. It posts one end to main with ipcRenderer.postMessage('speech:audioPort'), right before the startDictation invoke on the same ordered pipe. It keeps the other end for that session.
  • Main (ipc/speech.ts): holds the port until the start is accepted. Then SttService.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.
  • Preload, after the reply: frames go on the port when directAudio is true. stopDictation posts { type: 'end' } and closes the port, then sends the stop over IPC as before.
  • Speech process (stt-worker.ts): feeds the bound stream from the port. It holds a stop until that port's end (or its close, if the window went away), so the last frames always land before the stream finishes. Main's stop travels a different pipe and could otherwise overtake them.
  • Who gets a port: only an active on-device session in an Electron utility process. These keep the IPC/RPC path they use today:
    • cloud (OpenAI) sessions, which need feedAudio's upload-cap answer
    • orcad's forked child, which can't take ports (attachAudioPort returns false)
    • the web client and phones, which never reach this handler
  • Crash/restart: each start makes a new channel, so a restarted speech process gets a fresh port on the next dictation.

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:

  • A one-way ipcRenderer.send. It saves only the reply, and main still touches every frame.
  • A channel created by main. Its renderer end could arrive after the start resolves, so the first frames would go over IPC and later ones over the port, which can reorder them.

Creating the channel in the preload keeps the renderer API unchanged: DictationController and use-audio-capture don'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 kHz say clip.

Same build, A/B. I removed main's speech:audioPort listener at runtime to force the IPC fallback, then put it back. Four runs each way, 17 s of audio:

IPC (before) Port (after)
main speech:feedAudio handler calls per dictation 172–173 0
main-thread JS in speech code per dictation (V8 profile) 5–11 ms 0
main-thread busy per dictation (V8 profile, includes native) 150–210 ms 135–214 ms (noise)
transcript all four sentences, in order all four sentences, in order

The 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 -9 mid-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/dictation and settings/voice. A scoped tsc over the changed files and their tests. check:code-quality:changed and check:react-doctor:changed are 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 until end" 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 end or close, 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 end first. 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:

  • Electron MessagePortMain behaviour on Linux and Windows. It is the same Electron API on every platform, but I only ran macOS.
  • Nothing is saved, and nothing changes on the wire to phones, SSH or the web client.
  • The renderer-facing window.api.speech API is unchanged.

To back it out, revert the PR.

Level: Medium

AI Disclosure

Review

Two Claude review rounds (Codex is at its usage limit):

  • Round 1: no P0/P1.
  • Round 2: one P1. The speech.ts IPC test's ipcMain mock lacked on. Fixed.

P3s left as is:

  • A stop sent before the start reply lands would leave the port open. DictationController never does that.
  • A second start for an already-active owner would attach a second port. Session ids are unique per run.
  • A pending port whose start never arrives would stay in main's map. The start always follows immediately on the same pipe.
  • The preload doesn't check that samples is a Float32Array. A compromised page could already crash the speech process over IPC.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Cross-platform: Electron utilityProcess and MessageChannel ports exist on all three platforms, with no platform branches. Only macOS was run.
  • SSH and remote: unchanged. Remote hosts and orca serve use the forked child or RPC.
  • Mobile: unchanged. Phones feed through RPC.
  • Folder workspaces: not involved.
  • Seen during testing, not changed here: killing the speech process with kill -9 opens 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.
  • For later items: partial and final transcript events still go through main. The plan's "desktop via MessagePort" for events could reuse this port, sending events back from the speech process. W1.3's {revision, committed, tentative} events are the natural time to do that.

For Kelvin

  • No new or changed user-facing strings.
  • Behaviour: desktop on-device dictation audio no longer passes through the main process. Transcripts are unchanged.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 51323f28-6526-45ea-b0d0-83669ac60ed4
Commits

Reviewing files that changed from the base of the PR and between f29226e and bf98d2b.

Files selected for processing: 10

  • src/main/ipc/speech.test.ts
  • src/main/ipc/speech.ts
  • src/main/speech/stt-service.test.ts
  • src/main/speech/stt-service.ts
  • src/main/speech/stt-worker-parent-port.ts
  • src/main/speech/stt-worker-process.ts
  • src/main/speech/stt-worker-utility-process.ts
  • src/main/speech/stt-worker.ts
  • src/preload/api/speech-bridge.ts
  • src/shared/speech-types.ts
  • 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

@AmoabaKelvin
AmoabaKelvin merged commit d3a24e3 into main Nov 7, 2026
50 checks passed
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