fix(chat): ignore a stale voice recognizer's late onresult callback - #6510
Merged
Conversation
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.
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.
Follows #6494, which guarded
onerror/onendinuse-voice-input.tsagainst a staleSpeechRecognitioninstance firing async callbacks after a restart — but missedonresult.Payoff:
finalTranscriptRef/interimTranscriptRefare shared across recognizer instances. If a user stops/cancels and quickly restarts recording, the old (stale) recognizer can still fire a lateonresultwith 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 foronerror/onend, just on the one callback that was missed.Fix: add the same
recognitionRef.current !== recognitionguard toonresult.Regression test:
use-voice-input.test.ts— starts recording, cancels, restarts (two instances), firesonresulton the stale first instance with a final transcript, and asserts the new session'stranscriptstays 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
SpeechRecognitioninstance 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.recognitionRef.current !== recognitionguard toonresult, matching existingonerror/onendguards.onresultand asserts the new session’stranscriptstays empty.Written for commit 3d58328. Summary will update on new commits.