Skip to content

Commit a1d7a2f

Browse files
fix(web): changing voice destination cancels a dictation still finishing for a closed composer
A recording finishing into a thread's draft after its composer unmounted kept its transcription request running when the user turned voice off or picked another destination, and held the dictation slot until that request settled, so new dictation showed "Finishing an earlier dictation" for up to the inference timeout. While a recording sits in the detached slot, the slot now watches the audio choice (including a pending cloud choice's save marker) and cancels the recording through its controller as soon as its bound audio grant no longer holds: the request is aborted, the slot is released, and the failure notice shows once. This one watcher replaces the mounted editor's effect-cleanup notice, so a detached recording no longer depends on which editor is mounted. AI-assisted; verified with the tests and tools listed above.
1 parent 099303b commit a1d7a2f

3 files changed

Lines changed: 109 additions & 37 deletions

File tree

‎apps/web/src/speech/useEnvironmentSpeechInput.browser.test.tsx‎

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const mocks = vi.hoisted(() => ({
5151
platforms: [] as string[],
5252
microphoneRequests: [] as string[],
5353
transcribeCalls: [] as string[],
54+
transcribeSignals: [] as AbortSignal[],
5455
cleanupCalls: [] as string[],
5556
cleanupDraft: vi.fn(),
5657
cleanupOptions: [] as SpeechPostProcessingOptions[],
@@ -183,13 +184,20 @@ vi.mock("./browserVoiceInput", () => ({
183184
mocks.recordingOptions.push(input.getTranscriptionOptions());
184185
return {
185186
locale: "en",
186-
transcribe: async () => {
187+
transcribe: async (_uri: string, { signal }: { readonly signal: AbortSignal }) => {
188+
mocks.transcribeSignals.push(signal);
187189
mocks.transcribeCalls.push(
188190
input.cloudProvider
189191
? `${[input.cloudProvider, (input as { cloudModel?: string }).cloudModel].filter(Boolean).join(":")} via ${environmentId}`
190192
: environmentId,
191193
);
192-
const next = await (mocks.transcripts.shift() ?? "dictated");
194+
// Like a real request, an aborted transcription settles at once.
195+
const next = await Promise.race([
196+
mocks.transcripts.shift() ?? "dictated",
197+
new Promise<never>((_, reject) =>
198+
signal.addEventListener("abort", () => reject(signal.reason), { once: true }),
199+
),
200+
]);
193201
if (next instanceof Error) throw next;
194202
return next;
195203
},
@@ -227,6 +235,7 @@ function setup(options: { readonly destination?: string | null } = {}) {
227235
mocks.platforms = [];
228236
mocks.microphoneRequests = [];
229237
mocks.transcribeCalls = [];
238+
mocks.transcribeSignals = [];
230239
mocks.cleanupCalls = [];
231240
mocks.cleanupDraft.mockClear();
232241
mocks.cleanupOptions = [];
@@ -514,19 +523,34 @@ it.each([
514523
},
515524
);
516525

517-
it("discards a detached transcript when voice input is turned off after the editor unmounts", async () => {
518-
setup({ destination: "env-a" });
519-
const resolveTranscript = pendingTranscript();
520-
const drafts: Record<string, string> = { "thread-a": "Fix" };
521-
await using screen = await render(<ThreadProbe owner="thread-a" drafts={drafts} />);
522-
await act(() => mocks.voice.start());
523-
await screen.rerender(<div />);
524-
vi.mocked(toastManager.add).mockClear();
525-
setDestination(null);
526-
await act(async () => resolveTranscript("the login bug"));
527-
await vi.waitFor(() => expect(toastManager.add).toHaveBeenCalled());
528-
expect(drafts["thread-a"]).toBe("Fix");
529-
});
526+
it.each([
527+
["unmounts", true],
528+
["moves to another thread", false],
529+
])(
530+
"cancels a detached transcription at once when the voice destination changes after the editor %s",
531+
async (_, unmount) => {
532+
setup({ destination: "env-a" });
533+
const resolveTranscript = pendingTranscript();
534+
const drafts: Record<string, string> = { "thread-a": "Fix", "thread-b": "" };
535+
await using screen = await render(<ThreadProbe owner="thread-a" drafts={drafts} />);
536+
await act(() => mocks.voice.start());
537+
await screen.rerender(unmount ? <div /> : <ThreadProbe owner="thread-b" drafts={drafts} />);
538+
await vi.waitFor(() => expect(mocks.transcribeSignals).toHaveLength(1));
539+
vi.mocked(toastManager.add).mockClear();
540+
setDestination("env-b");
541+
expect(mocks.transcribeSignals[0]?.aborted).toBe(true);
542+
expect(toastManager.add).toHaveBeenCalledExactlyOnceWith(
543+
expect.objectContaining({ title: "Dictation couldn't be added to its draft" }),
544+
);
545+
await screen.rerender(<ThreadProbe owner="thread-b" drafts={drafts} />);
546+
await act(() => mocks.voice.start());
547+
expect(mocks.voice.state.phase).toBe("recording");
548+
expect(mocks.microphoneRequests).toEqual(["env-a", "env-b"]);
549+
await act(async () => resolveTranscript("the login bug"));
550+
expect(drafts["thread-a"]).toBe("Fix");
551+
expect(toastManager.add).toHaveBeenCalledOnce();
552+
},
553+
);
530554

531555
it("cleans a detached transcript on its own draft's environment", async () => {
532556
setup({ destination: "env-a" });

‎apps/web/src/speech/useEnvironmentSpeechInput.ts‎

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ import { useEnvironmentScope, usePreparedConnection } from "../state/session";
4040
import { createBrowserVoiceInputPlatform } from "./browserVoiceInput";
4141
import { preferredSpeechLanguages } from "./preferredSpeechLanguages";
4242
import { runEnvironmentSpeech } from "./runEnvironmentSpeech";
43-
import { bindAudioGrant, readCleanupGrant, useAudioChoice } from "./voiceConsent";
43+
import {
44+
bindAudioGrant,
45+
readCleanupGrant,
46+
subscribeAudioChoice,
47+
useAudioChoice,
48+
} from "./voiceConsent";
4449

4550
const INITIAL_STATE: VoiceInputState = { phase: "idle", error: null, errorAction: null };
4651
const WAITING_STATE: VoiceInputState = { phase: "preparing", error: null, errorAction: null };
@@ -82,18 +87,42 @@ type DraftAccess = {
8287

8388
type DetachedRecording = {
8489
readonly controller: VoiceInputController;
90+
readonly hasAudioConsent: () => boolean;
8591
readonly snapshot: VoiceDraftSnapshot;
8692
readonly access: DraftAccess;
8793
state: VoiceInputState;
8894
committedText?: string;
8995
};
9096

97+
const DETACHED_CONSENT_LOST =
98+
"Voice input was turned off or changed before the transcript was ready.";
99+
100+
function failDetached(description: string | null) {
101+
toastManager.add({
102+
type: "error",
103+
title: "Dictation couldn't be added to its draft",
104+
description,
105+
});
106+
}
107+
91108
// A recording finishing into its owner's draft after its editor moved to another owner or
92109
// unmounted. Every editor reads it: the owner's shows it, the rest wait for it. The global voice
93110
// session allows one recording at a time, so one slot is enough.
94111
let detachedRecording: DetachedRecording | null = null;
112+
let unwatchDetachedConsent = () => {};
95113
const detachedListeners = new Set<() => void>();
96114
function publishDetached(next: DetachedRecording | null) {
115+
if (next !== detachedRecording) {
116+
unwatchDetachedConsent();
117+
// No editor's effect sees a consent change once the recording is detached.
118+
unwatchDetachedConsent = next
119+
? subscribeAudioChoice(() => {
120+
if (next.hasAudioConsent()) return;
121+
failDetached(DETACHED_CONSENT_LOST);
122+
next.controller.cancel();
123+
})
124+
: () => {};
125+
}
97126
detachedRecording = next;
98127
for (const listener of detachedListeners) listener();
99128
}
@@ -107,6 +136,7 @@ function subscribeDetached(listener: () => void) {
107136
/** Finishes an active recording into its owner's draft while the editor shows another owner, or is gone. */
108137
function detachRecording(
109138
controller: VoiceInputController,
139+
hasAudioConsent: () => boolean,
110140
snapshot: VoiceDraftSnapshot | null,
111141
{ ownerKey, detachDraft }: HookInput,
112142
): boolean {
@@ -119,22 +149,11 @@ function detachRecording(
119149
state.phase !== "post-processing")
120150
)
121151
return false;
122-
publishDetached({ controller, snapshot, access: detachDraft(), state });
152+
publishDetached({ controller, hasAudioConsent, snapshot, access: detachDraft(), state });
123153
void controller.stop();
124154
return true;
125155
}
126156

127-
const DETACHED_CONSENT_LOST =
128-
"Voice input was turned off or changed before the transcript was ready.";
129-
130-
function failDetached(description: string | null) {
131-
toastManager.add({
132-
type: "error",
133-
title: "Dictation couldn't be added to its draft",
134-
description,
135-
});
136-
}
137-
138157
function cleanupContext({ text, selection }: DraftInput): DraftInput {
139158
const limit = 1_024;
140159
const start = Math.max(0, Math.min(selection.start, text.length));
@@ -272,6 +291,7 @@ export function useEnvironmentSpeechInput(input: HookInput) {
272291
: INITIAL_STATE);
273292

274293
const controllerRef = useRef<VoiceInputController | null>(null);
294+
const audioConsentRef = useRef<() => boolean>(() => false);
275295
const startRequestRef = useRef(0);
276296
const pendingSubmissionRef = useRef<{
277297
ownerKey: string;
@@ -458,7 +478,7 @@ export function useEnvironmentSpeechInput(input: HookInput) {
458478
commitDraft: (text, selection) => {
459479
const detached = detachedSelf();
460480
if (detached) {
461-
// Unmounted, consent changes no longer reach this controller through its effect.
481+
// Another window's revocation can land before its storage event reaches the watcher.
462482
if (!hasAudioConsent()) return failDetached(DETACHED_CONSENT_LOST);
463483
detached.access.commitDraft(text, selection);
464484
detached.committedText = text;
@@ -487,19 +507,23 @@ export function useEnvironmentSpeechInput(input: HookInput) {
487507
},
488508
});
489509
controllerRef.current = controller;
510+
audioConsentRef.current = hasAudioConsent;
490511
return () => {
491512
disposed = true;
492513
pendingSubmissionRef.current = null;
493514
startRequestRef.current += 1;
494515
setQueuedStart(null);
495516
if (controllerRef.current === controller) controllerRef.current = null;
496-
if (detachedSelf()) {
497-
if (unmountingRef.current) return;
498-
failDetached(DETACHED_CONSENT_LOST);
499-
publishDetached(null);
500-
} else if (
501-
unmountingRef.current &&
502-
detachRecording(controller, lastSnapshotRef.current, latestRef.current.input)
517+
// A detached recording answers to its own consent watcher, not to this editor's settings.
518+
if (
519+
detachedSelf() ||
520+
(unmountingRef.current &&
521+
detachRecording(
522+
controller,
523+
hasAudioConsent,
524+
lastSnapshotRef.current,
525+
latestRef.current.input,
526+
))
503527
)
504528
return;
505529
controller.dispose();
@@ -561,7 +585,12 @@ export function useEnvironmentSpeechInput(input: HookInput) {
561585
if (!controller || detachedRecording?.controller === controller) return;
562586
if (
563587
previousOwner === input.ownerKey ||
564-
!detachRecording(controller, lastSnapshotRef.current, previousInputRef.current)
588+
!detachRecording(
589+
controller,
590+
audioConsentRef.current,
591+
lastSnapshotRef.current,
592+
previousInputRef.current,
593+
)
565594
)
566595
controller.ownerChanged();
567596
else setControllerState((current) => ({ ...current, value: INITIAL_STATE }));

‎apps/web/src/speech/voiceConsent.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ export function useAudioChoice() {
5555
return activeChoice(choice, marker);
5656
}
5757

58+
/** Notifies on any change that can move the active audio grant, including a pending choice's save marker. */
59+
export function subscribeAudioChoice(onChange: () => void) {
60+
let unsubscribeMarker = () => {};
61+
const watchMarker = () => {
62+
unsubscribeMarker();
63+
const id = pendingGrant(readChoice())?.id;
64+
unsubscribeMarker = id ? subscribeLocalStorageKey(savedKey(id), onChange) : () => {};
65+
};
66+
watchMarker();
67+
const unsubscribeChoice = subscribeLocalStorageKey(AUDIO_KEY, () => {
68+
watchMarker();
69+
onChange();
70+
});
71+
return () => {
72+
unsubscribeChoice();
73+
unsubscribeMarker();
74+
};
75+
}
76+
5877
function replaceChoice(choice: AudioChoice, previous = readChoice()) {
5978
const old = pendingGrant(previous);
6079
writeLocalStorageValue(AUDIO_KEY, choice, null, AudioChoice);

0 commit comments

Comments
 (0)