Skip to content

Commit eb4f0f5

Browse files
fix(voice): keep audio off when browser consent is unreadable
1 parent 3a06d08 commit eb4f0f5

2 files changed

Lines changed: 68 additions & 14 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,47 @@ it("revokes across windows even before the storage event and survives stale pref
394394
}
395395
});
396396

397+
it("recovers from a corrupt choice without enabling cloud dictation before the save completes", async () => {
398+
setup();
399+
window.localStorage.setItem("t3code:voice-audio-grant:v1", "not-json");
400+
const save = pendingCloudAudioChoice({
401+
kind: "cloud",
402+
environmentId: EnvironmentId.make("env-b"),
403+
provider: "openai",
404+
});
405+
await using _screen = await render(<Probe />);
406+
expect(mocks.voice.transcriptionEnvironmentId).toBeNull();
407+
await act(save);
408+
expect(mocks.voice.transcriptionEnvironmentId).toBe("env-b");
409+
});
410+
411+
it("uses neither destination when the cloud save marker cannot be read", async () => {
412+
setup({ destination: "env-a" });
413+
const key = "t3code:voice-audio-grant:v1";
414+
const previous = JSON.parse(window.localStorage.getItem(key)!) as { id: string };
415+
const canSendToPrevious = bindAudioGrant(previous.id);
416+
const save = pendingCloudAudioChoice({
417+
kind: "cloud",
418+
environmentId: EnvironmentId.make("env-b"),
419+
provider: "openai",
420+
});
421+
save();
422+
const getItem = Storage.prototype.getItem;
423+
using _blocked = vi.spyOn(Storage.prototype, "getItem").mockImplementation(function (
424+
this: Storage,
425+
k,
426+
) {
427+
if (k.startsWith(`${key}:saved:`))
428+
throw new DOMException("Storage is blocked", "SecurityError");
429+
return getItem.call(this, k);
430+
});
431+
await using _screen = await render(<Probe />);
432+
expect(mocks.voice.transcriptionEnvironmentId).toBeNull();
433+
expect(canSendToPrevious()).toBe(false);
434+
await act(() => mocks.voice.start());
435+
expectNoAudioSent();
436+
});
437+
397438
it("cleans the transcript only after cleanup is separately enabled", async () => {
398439
setup({ destination: "env-a" });
399440
chooseTranscriptCleanup(true);

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,38 @@ type AudioChoice = typeof AudioChoice.Type;
2323
const savedKey = (id: string) => `${AUDIO_KEY}:saved:${id}`;
2424
const pendingGrant = (choice: AudioChoice) =>
2525
choice && "pending" in choice ? choice.pending : null;
26-
const activeGrant = (choice: AudioChoice, saved: boolean) =>
27-
choice && "pending" in choice ? (saved ? choice.pending : choice.previous) : choice;
2826
const CleanupGrant = Schema.NullOr(Schema.String);
2927
const decodeChoice = Schema.decodeSync(Schema.fromJsonString(AudioChoice));
28+
const readChoice = () => readLocalStorageValue(AUDIO_KEY, null, AudioChoice);
3029

31-
function isSaved(id: string | undefined) {
32-
return !!id && window.localStorage.getItem(savedKey(id)) === "true";
30+
function readSaveMarker(id: string | undefined) {
31+
try {
32+
return id ? window.localStorage.getItem(savedKey(id)) : null;
33+
} catch {
34+
return undefined;
35+
}
36+
}
37+
38+
function activeGrant(choice: AudioChoice, marker: string | null | undefined) {
39+
if (!choice || !("pending" in choice)) return choice;
40+
// Only a confirmed missing marker means the previous grant still applies.
41+
if (marker === null) return choice.previous;
42+
return marker === "true" ? choice.pending : null;
3343
}
3444

3545
export function useAudioGrant() {
3646
const [choice] = useLocalStorage(AUDIO_KEY, null, AudioChoice);
3747
const id = pendingGrant(choice)?.id;
38-
const saved = useSyncExternalStore(
48+
const marker = useSyncExternalStore(
3949
(notify) => (id ? subscribeLocalStorageKey(savedKey(id), notify) : () => {}),
40-
() => isSaved(id),
41-
() => false,
50+
() => readSaveMarker(id),
51+
() => undefined,
4252
);
43-
return activeGrant(choice, saved);
53+
return activeGrant(choice, marker);
4454
}
4555

46-
function replaceChoice(choice: AudioChoice) {
47-
const old = pendingGrant(readLocalStorageValue(AUDIO_KEY, null, AudioChoice));
56+
function replaceChoice(choice: AudioChoice, previous = readChoice()) {
57+
const old = pendingGrant(previous);
4858
writeLocalStorageValue(AUDIO_KEY, choice, null, AudioChoice);
4959
if (old && pendingGrant(getLocalStorageItem(AUDIO_KEY, AudioChoice))?.id !== old.id)
5060
window.localStorage.removeItem(savedKey(old.id));
@@ -57,12 +67,15 @@ export function chooseAudioDestination(destination: VoiceTranscriptionDestinatio
5767
export function pendingCloudAudioChoice(
5868
destination: Extract<VoiceTranscriptionDestination, { kind: "cloud" }>,
5969
) {
60-
const choice = getLocalStorageItem(AUDIO_KEY, AudioChoice);
70+
const choice = readChoice();
6171
const pending = { id: randomUUID(), destination };
62-
replaceChoice({ pending, previous: activeGrant(choice, isSaved(pendingGrant(choice)?.id)) });
72+
replaceChoice(
73+
{ pending, previous: activeGrant(choice, readSaveMarker(pendingGrant(choice)?.id)) },
74+
choice,
75+
);
6376
// A late response can acknowledge only its own choice, never write the destination another tab picked.
6477
return () => {
65-
if (pendingGrant(getLocalStorageItem(AUDIO_KEY, AudioChoice))?.id === pending.id)
78+
if (pendingGrant(readChoice())?.id === pending.id)
6679
writeLocalStorageValue(savedKey(pending.id), true, false, Schema.Boolean);
6780
};
6881
}
@@ -78,7 +91,7 @@ export function bindAudioGrant(id: string) {
7891
choice = raw ? decodeChoice(raw) : null;
7992
cachedRaw = raw;
8093
}
81-
return activeGrant(choice, isSaved(pendingGrant(choice)?.id));
94+
return activeGrant(choice, readSaveMarker(pendingGrant(choice)?.id));
8295
};
8396
const bound = current();
8497
if (bound?.id !== id) return () => false;

0 commit comments

Comments
 (0)