Skip to content

Commit fe0179e

Browse files
fix(web): let offline holders join a retained voice session
A holder that mounts while the destination is down now joins the existing session instead of skipping it, so the session survives its last connected holder unmounting. Scope denial uses the shared client-runtime predicate, which counts explicit 401/403 rejections as denials. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e1f9c70 commit fe0179e

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,29 @@ it("keeps a stopped recording local while disconnected and retries it on reconne
437437
expect(mocks.committed).toHaveBeenCalledExactlyOnceWith("dictated", expect.anything());
438438
});
439439

440+
it("keeps a recording alive when its last connected holder unmounts while offline", async () => {
441+
setup({ destination: "env-a" });
442+
mocks.storedDraft = "";
443+
const view = (editor: boolean, indicator: boolean) => (
444+
<>
445+
{editor ? <Probe ownerKey="thread-a" persistent /> : null}
446+
{indicator ? <GlobalVoiceInputIndicator /> : null}
447+
</>
448+
);
449+
await using screen = await render(view(true, false));
450+
await act(() => mocks.voice.start());
451+
mocks.connected.delete("env-a");
452+
mocks.prepared.delete("env-a");
453+
// The indicator mounts offline, then the recording's editor unmounts.
454+
await screen.rerender(view(true, true));
455+
await screen.rerender(view(false, true));
456+
mocks.connected.add("env-a");
457+
await screen.rerender(view(false, true));
458+
await page.getByRole("button", { name: "Finish voice input" }).click();
459+
await vi.waitFor(() => expect(mocks.storedDraft).toBe("dictated"));
460+
expect(mocks.transcribeCalls).toEqual(["env-a"]);
461+
});
462+
440463
it.each(["authorization", "capability"] as const)(
441464
"abandons a recording when connected destination %s is lost",
442465
async (loss) => {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,13 @@ function findSession(key: SessionKey): VoiceInputSession | null {
334334
return null;
335335
}
336336

337-
/** Every holder of a destination shares one session; the last release disposes it. */
338-
function retainVoiceInputSession(key: SessionKey, prepared: PreparedConnection) {
339-
const session = findSession(key) ?? new VoiceInputSession(key, prepared);
337+
/**
338+
* Every holder of a destination shares one session; the last release disposes it. Offline,
339+
* a holder joins an existing session but never creates one.
340+
*/
341+
function retainVoiceInputSession(key: SessionKey, prepared: PreparedConnection | null) {
342+
const session = findSession(key) ?? (prepared ? new VoiceInputSession(key, prepared) : null);
343+
if (!session) return null;
340344
session.updatePrepared(prepared);
341345
sessions.set(session, (sessions.get(session) ?? 0) + 1);
342346
for (const listener of registryListeners) listener();
@@ -377,7 +381,7 @@ export function useRetainedVoiceInputSession(input: {
377381
readonly cloudModel?: CloudSpeechModelId | undefined;
378382
}): VoiceInputSession | null {
379383
const { environmentId, prepared, grantId, retainOnDisconnect, cloudProvider, cloudModel } = input;
380-
const leaseRef = useRef<ReturnType<typeof retainVoiceInputSession> | null>(null);
384+
const leaseRef = useRef<ReturnType<typeof retainVoiceInputSession>>(null);
381385
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- releases the shared recording session on unmount
382386
useEffect(
383387
() => () => {
@@ -396,7 +400,7 @@ export function useRetainedVoiceInputSession(input: {
396400
leaseRef.current = null;
397401
}
398402
if (leaseRef.current) leaseRef.current.session.updatePrepared(prepared);
399-
else if (key && prepared && retainOnDisconnect && typeof navigator !== "undefined")
403+
else if (key && retainOnDisconnect && typeof navigator !== "undefined")
400404
leaseRef.current = retainVoiceInputSession(key, prepared);
401405
}, [environmentId, grantId, cloudProvider, cloudModel, prepared, retainOnDisconnect]);
402406
return useSyncExternalStore(subscribeRegistry, () =>

‎apps/web/src/state/session.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { useAtomValue } from "@effect/atom-react";
2-
import { createEnvironmentSessionAtoms } from "@t3tools/client-runtime/state/session";
2+
import {
3+
createEnvironmentSessionAtoms,
4+
sessionDeniesScope,
5+
} from "@t3tools/client-runtime/state/session";
36
import {
47
type AuthEnvironmentScope,
58
type AuthSessionState,
@@ -69,14 +72,6 @@ export function sessionHasScope(
6972
return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
7073
}
7174

72-
function sessionDeniesScope(
73-
result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
74-
scope: AuthEnvironmentScope,
75-
): boolean {
76-
const session = AsyncResult.getOrElse(result, () => null);
77-
return result._tag !== "Failure" && session !== null && !sessionGrantsScope(session, scope);
78-
}
79-
8075
const environmentsWithScopeAtom = Atom.family((key: string) => {
8176
const [scope, idsKey = ""] = key.split(/\n(.*)/s) as [AuthEnvironmentScope, string?];
8277
const ids = idsKey === "" ? [] : (idsKey.split("\n") as EnvironmentId[]);

0 commit comments

Comments
 (0)