Skip to content

Commit 3352e28

Browse files
committed
fix(voice): isolate sessions and respect client interaction boundaries
1 parent 2bf0203 commit 3352e28

6 files changed

Lines changed: 123 additions & 20 deletions

File tree

‎apps/mobile/src/features/voice-mode/useVoiceMode.test.tsx‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { createRoot, type Root } from "react-dom/client";
33
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
44
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
55

6-
const fixture = vi.hoisted(() => ({ active: true, currentThread: "thread-a", stop: vi.fn() }));
6+
const fixture = vi.hoisted(() => ({
7+
active: true,
8+
currentThread: "thread-a",
9+
currentEnvironment: "environment",
10+
stop: vi.fn(),
11+
}));
712
const RouteName = createContext("Thread");
813
const target = {
914
environmentId: EnvironmentId.make("environment"),
@@ -12,8 +17,18 @@ const target = {
1217
vi.mock("@react-navigation/native", () => ({
1318
useRoute: () => ({ name: useContext(RouteName) }),
1419
useNavigationState: (
15-
select: (state: { routes: Array<{ name: string; params: { threadId: string } }> }) => unknown,
16-
) => select({ routes: [{ name: "Thread", params: { threadId: fixture.currentThread } }] }),
20+
select: (state: {
21+
routes: Array<{ name: string; params: { threadId: string; environmentId: string } }>;
22+
}) => unknown,
23+
) =>
24+
select({
25+
routes: [
26+
{
27+
name: "Thread",
28+
params: { threadId: fixture.currentThread, environmentId: fixture.currentEnvironment },
29+
},
30+
],
31+
}),
1732
}));
1833
vi.mock("react-native", () => ({ Alert: { alert: vi.fn() } }));
1934
vi.mock("./voiceModePresentation", () => ({ resolveVoiceModeNotice: () => null }));
@@ -37,6 +52,7 @@ let root: Root;
3752
beforeEach(() => {
3853
fixture.active = true;
3954
fixture.currentThread = "thread-a";
55+
fixture.currentEnvironment = "environment";
4056
fixture.stop.mockClear();
4157
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
4258
root = createRoot(document.createElement("div"));
@@ -64,12 +80,16 @@ it("keeps voice running when Files closes and stops when the owning Thread leave
6480
act(() => root.unmount());
6581
expect(fixture.stop).toHaveBeenCalledTimes(1);
6682
});
67-
it("stops the previous thread when navigation selects a different thread", () => {
68-
render(false);
69-
fixture.currentThread = "thread-b";
70-
render(false);
71-
expect(fixture.stop).toHaveBeenCalledTimes(1);
72-
});
83+
it.each(["thread", "environment"])(
84+
"stops the previous thread when navigation selects a different %s",
85+
(changed) => {
86+
render(false);
87+
if (changed === "thread") fixture.currentThread = "thread-b";
88+
else fixture.currentEnvironment = "another-environment";
89+
render(false);
90+
expect(fixture.stop).toHaveBeenCalledTimes(1);
91+
},
92+
);
7393

7494
it("stops voice when its fullscreen thread closes over the still-mounted thread", () => {
7595
render(true, "ThreadInspectorFullscreen");

‎apps/mobile/src/features/voice-mode/useVoiceMode.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@ export function useVoiceModePhase(target: VoiceSessionTarget): VoiceModePhase {
3434
export function useVoiceModeThreadLifecycle(target: VoiceSessionTarget): void {
3535
const { environmentId, threadId } = target;
3636
const routeName = useRoute().name;
37-
const visibleThreadId = useNavigationState((state) => {
37+
const isVisibleThread = useNavigationState((state) => {
3838
const route = state.routes.findLast((route) => route.name === "Thread");
3939
const params = route?.params;
40-
return params && "threadId" in params ? String(params.threadId) : null;
40+
return (
41+
params != null &&
42+
"threadId" in params &&
43+
"environmentId" in params &&
44+
String(params.threadId) === threadId &&
45+
String(params.environmentId) === environmentId
46+
);
4147
});
4248
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Synchronize the WebRTC lifetime with the visible native thread.
4349
useEffect(() => {
44-
if (visibleThreadId !== threadId && voiceMode.isActiveFor({ environmentId, threadId }))
45-
voiceMode.stop();
46-
}, [environmentId, threadId, visibleThreadId]);
50+
if (!isVisibleThread && voiceMode.isActiveFor({ environmentId, threadId })) voiceMode.stop();
51+
}, [environmentId, threadId, isVisibleThread]);
4752

4853
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Release the native call when its owning thread screen unmounts.
4954
useEffect(

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,81 @@ describe("CodexAdapterV2 post-settle continuation", () => {
12001200
};
12011201
});
12021202

1203+
it.effect.each(["request", "realtime"] as const)(
1204+
"only displays intentional voice errors from a %s failure",
1205+
(failure) =>
1206+
Effect.scoped(
1207+
Effect.gen(function* () {
1208+
const nativeThreadId = "voice-error-thread";
1209+
const detail =
1210+
failure === "request" ? "private protocol payload" : "Voice service is unavailable";
1211+
const harness = yield* makeCodexReplayHarness(
1212+
makeCodexReplayTranscript({
1213+
scenario: `voice-error-${failure}`,
1214+
entries: [
1215+
...codexReplayPreamble({
1216+
nativeThreadId,
1217+
nativeTurnId: "unused",
1218+
prompt: "",
1219+
}).slice(0, -3),
1220+
{
1221+
type: "expect_outbound",
1222+
label: "thread/realtime/start",
1223+
frame: {
1224+
id: 3,
1225+
method: "thread/realtime/start",
1226+
params: {
1227+
threadId: nativeThreadId,
1228+
outputModality: "audio",
1229+
transport: { type: "webrtc", sdp: "v=offer\r\n" },
1230+
version: "v3",
1231+
clientManagedHandoffs: true,
1232+
includeStartupContext: false,
1233+
},
1234+
},
1235+
},
1236+
{
1237+
type: "emit_inbound",
1238+
label: "thread/realtime/start",
1239+
frame:
1240+
failure === "request"
1241+
? { id: 3, error: { code: -32603, message: detail } }
1242+
: { id: 3, result: {} },
1243+
},
1244+
...(failure === "realtime"
1245+
? [
1246+
{
1247+
type: "emit_inbound" as const,
1248+
label: "thread/realtime/error",
1249+
frame: {
1250+
method: "thread/realtime/error",
1251+
params: { threadId: nativeThreadId, message: detail },
1252+
},
1253+
},
1254+
]
1255+
: []),
1256+
],
1257+
}),
1258+
);
1259+
const error = yield* harness.runtime.startVoiceSession!({
1260+
providerThread: harness.providerThread,
1261+
offerSdp: "v=offer\r\n",
1262+
}).pipe(Stream.runDrain, Effect.flip);
1263+
assert.equal(error._tag, "ProviderAdapterProtocolError");
1264+
if (error._tag === "ProviderAdapterProtocolError") {
1265+
assert.equal(
1266+
error.detail,
1267+
failure === "request" ? "Voice conversation failed." : detail,
1268+
);
1269+
}
1270+
}).pipe(
1271+
Effect.provide(
1272+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
1273+
),
1274+
),
1275+
),
1276+
);
1277+
12031278
it.effect(
12041279
"resubmits a voice handoff Codex started only once its turn is interrupted or failed",
12051280
() =>

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7610,7 +7610,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
76107610
: new ProviderAdapter.ProviderAdapterProtocolError({
76117611
driver: CODEX_PROVIDER,
76127612
detail:
7613-
cause._tag === "CodexVoiceSessionError" ? cause.detail : cause.message,
7613+
cause._tag === "CodexVoiceSessionError"
7614+
? cause.detail
7615+
: "Voice conversation failed.",
76147616
payload: cause,
76157617
}),
76167618
),

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
943943
const voiceEngaged = voicePhase !== "idle";
944944
// A conversation stays stoppable here even if its provider stopped offering voice.
945945
const voiceSupported = voiceAvailability.kind !== "unsupported" || voiceEngaged;
946-
const toggleVoice = () => toggleThreadVoice(routeThreadRef, voiceAvailability);
946+
const toggleVoice = useStableCallback(() => toggleThreadVoice(routeThreadRef, voiceAvailability));
947947
const selectedProviderSkills = selectedProviderStatus
948948
? resolveProviderSkillsForCwd(selectedProviderStatus, gitCwd)
949949
: NO_PROVIDER_SKILLS;
@@ -1111,6 +1111,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
11111111
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- binds voice shortcuts to window keyboard events for the mounted composer
11121112
useEffect(() => {
11131113
const handler = (event: globalThis.KeyboardEvent) => {
1114+
if (isCommandPaletteOpen()) return;
11141115
const command = resolveShortcutCommand(event, keybindings, {
11151116
context: {
11161117
terminalFocus: getTerminalFocusOwner() !== null,
@@ -1130,7 +1131,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
11301131
if (!event.repeat) voiceMode.toggleMuted();
11311132
return;
11321133
}
1133-
if (command !== "voice.toggle" || !voiceSupported || isCommandPaletteOpen()) return;
1134+
if (command !== "voice.toggle" || !voiceSupported) return;
11341135
event.preventDefault();
11351136
event.stopPropagation();
11361137
if (!event.repeat) toggleVoice();

‎apps/web/src/components/chat/VoiceModeStrip.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ export const VoiceModeStrip = memo(function VoiceModeStrip({
115115
<ComposerBanner.Row>
116116
<ComposerBanner.Icon>{isActive ? <AudioLinesIcon /> : <Spinner />}</ComposerBanner.Icon>
117117
<ComposerBanner.Content className="gap-2">
118-
<span className="shrink-0 whitespace-nowrap text-muted-foreground" role="status">
119-
{status}
118+
<span className="sr-only" role="status">
119+
{isActive ? "Voice connected" : "Connecting voice…"}
120120
</span>
121+
<span className="shrink-0 whitespace-nowrap text-muted-foreground">{status}</span>
121122
{isActive ? (
122123
<span className="flex items-center gap-2 text-muted-foreground">
123124
<span className="flex items-center gap-1">
@@ -190,7 +191,6 @@ export function ComposerVoiceButton(props: {
190191
variant="ghost"
191192
size="sm"
192193
// aria-disabled rather than disabled keeps the tooltip explaining why.
193-
className="aria-disabled:cursor-not-allowed"
194194
pressed={props.engaged}
195195
onPointerDown={(event) => event.preventDefault()}
196196
onPressedChange={() => {

0 commit comments

Comments
 (0)