Skip to content

fix(chat): ignore a stale voice recognizer's late onresult callback - #6510

Merged
pedrofrxncx merged 1 commit into
mainfrom
fix/voice-input-stale-onresult-w4
Aug 27, 2026
Merged

fix(chat): ignore a stale voice recognizer's late onresult callback#6510
pedrofrxncx merged 1 commit into
mainfrom
fix/voice-input-stale-onresult-w4

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Follows #6494, which guarded onerror/onend in use-voice-input.ts against a stale SpeechRecognition instance firing async callbacks after a restart — but missed onresult.

Payoff: finalTranscriptRef/interimTranscriptRef are shared across recognizer instances. If a user stops/cancels and quickly restarts recording, the old (stale) recognizer can still fire a late onresult with a final transcript, which splices into the new session's transcript even though the old recognizer was supposed to be discarded — the exact same bug class #6494 fixed for onerror/onend, just on the one callback that was missed.

Fix: add the same recognitionRef.current !== recognition guard to onresult.

Regression test: use-voice-input.test.ts — starts recording, cancels, restarts (two instances), fires onresult on the stale first instance with a final transcript, and asserts the new session's transcript stays empty (previously it would have picked up the stale words).

Verify: bun test apps/web/src/hooks/use-voice-input.test.ts (6 pass).

Locally ran: bun test apps/web/src/hooks/use-voice-input.test.ts, bunx oxlint apps/web/src/hooks/use-voice-input.ts apps/web/src/hooks/use-voice-input.test.ts, bun run fmt. Full CI validates the rest.


Summary by cubic

Ignore late onresult from a stale SpeechRecognition instance after a restart to prevent stale text from splicing into the new session. Previously, a cancelled recognizer could append a final transcript to the new session; now the hook drops those events.

  • Add recognitionRef.current !== recognition guard to onresult, matching existing onerror/onend guards.
  • Add a regression test that starts, cancels, restarts, then fires the stale instance’s onresult and asserts the new session’s transcript stays empty.
  • No API changes; only stale results are ignored.

Written for commit 3d58328. Summary will update on new commits.

Review in cubic

PR #6494 guarded onerror/onend against a stale SpeechRecognition instance firing async callbacks after a restart, but missed onresult. finalTranscriptRef/interimTranscriptRef are shared across instances, so a cancelled recognizer's late final result still splices into the new recording's transcript.

Adds the same recognitionRef.current !== recognition guard to onresult, plus a regression test that starts, cancels, restarts, then fires the stale instance's onresult and asserts the new session's transcript stays empty.
@pedrofrxncx
pedrofrxncx merged commit 91661fc into main Aug 27, 2026
34 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/voice-input-stale-onresult-w4 branch August 27, 2026 10:31
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