Skip to content

Commit 37e024a

Browse files
committed
fix(server): serialize Codex voice lifecycle ownership
1 parent c4d865e commit 37e024a

3 files changed

Lines changed: 273 additions & 36 deletions

File tree

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7579,15 +7579,19 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
75797579
Effect.gen(function* () {
75807580
const nativeThreadId = yield* getNativeThreadId(voiceInput.providerThread);
75817581
yield* ensureInitialized;
7582-
yield* Ref.set(voiceSession, owner);
7583-
yield* Ref.set(voiceProviderThread, voiceInput.providerThread);
7584-
return voice.start({
7585-
providerThreadId: nativeThreadId,
7586-
offerSdp: voiceInput.offerSdp,
7587-
...(adapterOptions.settings.voice
7588-
? { voice: adapterOptions.settings.voice }
7589-
: {}),
7590-
});
7582+
return voice.start(
7583+
{
7584+
providerThreadId: nativeThreadId,
7585+
offerSdp: voiceInput.offerSdp,
7586+
...(adapterOptions.settings.voice
7587+
? { voice: adapterOptions.settings.voice }
7588+
: {}),
7589+
},
7590+
Effect.gen(function* () {
7591+
yield* Ref.set(voiceSession, owner);
7592+
yield* Ref.set(voiceProviderThread, voiceInput.providerThread);
7593+
}),
7594+
);
75917595
}),
75927596
).pipe(
75937597
// A replaced conversation must not clear its successor's thread.

‎apps/server/src/provider/CodexVoice.test.ts‎

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import * as Deferred from "effect/Deferred";
66
import * as Effect from "effect/Effect";
77
import * as Exit from "effect/Exit";
88
import * as Fiber from "effect/Fiber";
9+
import * as Logger from "effect/Logger";
910
import * as Schema from "effect/Schema";
1011
import * as Scope from "effect/Scope";
1112
import * as Stream from "effect/Stream";
1213
import type * as CodexClient from "effect-codex-app-server/client";
14+
import * as CodexErrors from "effect-codex-app-server/errors";
1315

1416
import {
1517
CodexVoiceSessionError,
@@ -30,8 +32,11 @@ const makeFakeClient = Effect.gen(function* () {
3032
const handlers = new Map<string, Array<(payload: unknown) => Effect.Effect<void, Error>>>();
3133
const requests: Array<{ readonly method: string; readonly params: unknown }> = [];
3234
let closeOnStop = false;
35+
let onStart: Effect.Effect<void, CodexErrors.CodexAppServerError> = Effect.void;
36+
let onStop: Effect.Effect<void, CodexErrors.CodexAppServerError> = Effect.void;
3337
const startRequested = yield* Deferred.make<void>();
3438
const secondStartRequested = yield* Deferred.make<void>();
39+
const thirdStartRequested = yield* Deferred.make<void>();
3540
const speechRequested = yield* Deferred.make<void>();
3641
const stopRequested = yield* Deferred.make<void>();
3742
const client = {
@@ -50,12 +55,16 @@ const makeFakeClient = Effect.gen(function* () {
5055
yield* Deferred.succeed(startRequested, undefined);
5156
if (requests.filter((request) => request.method === method).length === 2)
5257
yield* Deferred.succeed(secondStartRequested, undefined);
58+
if (requests.filter((request) => request.method === method).length === 3)
59+
yield* Deferred.succeed(thirdStartRequested, undefined);
60+
yield* onStart;
5361
}
5462
if (method === "thread/realtime/appendSpeech") {
5563
yield* Deferred.succeed(speechRequested, undefined);
5664
}
5765
if (method === "thread/realtime/stop") {
5866
yield* Deferred.succeed(stopRequested, undefined);
67+
yield* onStop;
5968
if (closeOnStop)
6069
yield* emit("thread/realtime/closed", {
6170
threadId: PROVIDER_THREAD,
@@ -76,7 +85,14 @@ const makeFakeClient = Effect.gen(function* () {
7685
emit,
7786
startRequested,
7887
secondStartRequested,
88+
thirdStartRequested,
7989
speechRequested,
90+
setStart: (effect: typeof onStart) => {
91+
onStart = effect;
92+
},
93+
setStop: (effect: typeof onStop) => {
94+
onStop = effect;
95+
},
8096
stopRequested,
8197
closePreviousOnStop: () => {
8298
closeOnStop = true;
@@ -228,6 +244,194 @@ describe("makeCodexVoice", () => {
228244
),
229245
);
230246

247+
it.effect("serializes concurrent replacements before routing the successor's SDP", () =>
248+
Effect.scoped(
249+
Effect.gen(function* () {
250+
const fake = yield* makeFakeClient;
251+
const voice = yield* makeCodexVoice(fake.client, yield* Effect.scope);
252+
const owners: Array<string> = [];
253+
const start = (owner: string) =>
254+
voice
255+
.start(
256+
{ providerThreadId: PROVIDER_THREAD, offerSdp: "offer" },
257+
Effect.sync(() => {
258+
owners.push(owner);
259+
}),
260+
)
261+
.pipe(Stream.runCollect, Effect.forkChild({ startImmediately: true }));
262+
const first = yield* start("first");
263+
yield* Deferred.await(fake.startRequested);
264+
const releaseStop = yield* Deferred.make<void>();
265+
fake.setStop(Deferred.await(releaseStop));
266+
const second = yield* start("second");
267+
yield* Deferred.await(fake.stopRequested);
268+
const third = yield* start("third");
269+
const cancelled = yield* start("cancelled");
270+
const cancellation = yield* Fiber.interrupt(cancelled).pipe(Effect.forkChild);
271+
const whileStopping = fake.requests.map(({ method }) => method);
272+
yield* Deferred.succeed(releaseStop, undefined);
273+
NodeAssert.deepEqual(whileStopping, ["thread/realtime/start", "thread/realtime/stop"]);
274+
yield* Deferred.await(fake.thirdStartRequested);
275+
yield* Fiber.join(cancellation);
276+
NodeAssert.deepEqual(owners, ["first", "second", "third"]);
277+
yield* fake.emit("thread/realtime/sdp", { threadId: PROVIDER_THREAD, sdp: "third-answer" });
278+
yield* fake.emit("thread/realtime/closed", { threadId: PROVIDER_THREAD });
279+
NodeAssert.deepEqual(yield* Fiber.join(first), [{ type: "closed", reason: "replaced" }]);
280+
NodeAssert.deepEqual(yield* Fiber.join(second), [{ type: "closed", reason: "replaced" }]);
281+
NodeAssert.deepEqual(yield* Fiber.join(third), [
282+
{ type: "answer", sdp: "third-answer" },
283+
{ type: "closed" },
284+
]);
285+
NodeAssert.deepEqual(
286+
fake.requests.map(({ method }) => method),
287+
[
288+
"thread/realtime/start",
289+
"thread/realtime/stop",
290+
"thread/realtime/start",
291+
"thread/realtime/stop",
292+
"thread/realtime/start",
293+
],
294+
);
295+
}),
296+
),
297+
);
298+
299+
it.effect("waits for unsubscribe cleanup before starting another native thread", () =>
300+
Effect.scoped(
301+
Effect.gen(function* () {
302+
const fake = yield* makeFakeClient;
303+
const voice = yield* makeCodexVoice(fake.client, yield* Effect.scope);
304+
const first = yield* voice
305+
.start({ providerThreadId: PROVIDER_THREAD, offerSdp: "offer" })
306+
.pipe(Stream.runCollect, Effect.forkChild);
307+
yield* Deferred.await(fake.startRequested);
308+
const releaseStop = yield* Deferred.make<void>();
309+
fake.setStop(Deferred.await(releaseStop));
310+
const cleanup = yield* Fiber.interrupt(first).pipe(Effect.forkChild);
311+
yield* Deferred.await(fake.stopRequested);
312+
const replacement = yield* voice
313+
.start({ providerThreadId: "another-thread", offerSdp: "offer" })
314+
.pipe(Stream.runCollect, Effect.forkChild({ startImmediately: true }));
315+
const whileStopping = fake.requests.map(({ method }) => method);
316+
yield* Deferred.succeed(releaseStop, undefined);
317+
NodeAssert.deepEqual(whileStopping, ["thread/realtime/start", "thread/realtime/stop"]);
318+
yield* Fiber.join(cleanup);
319+
yield* Deferred.await(fake.secondStartRequested);
320+
yield* fake.emit("thread/realtime/sdp", { threadId: PROVIDER_THREAD, sdp: "ignored" });
321+
yield* fake.emit("thread/realtime/sdp", { threadId: "another-thread", sdp: "answer" });
322+
yield* fake.emit("thread/realtime/closed", { threadId: "another-thread" });
323+
NodeAssert.deepEqual(yield* Fiber.join(replacement), [
324+
{ type: "answer", sdp: "answer" },
325+
{ type: "closed" },
326+
]);
327+
}),
328+
),
329+
);
330+
331+
it.effect.each(["stop response", "closed notification"] as const)(
332+
"retains failed cleanup until a %s confirms termination",
333+
(confirmation) => {
334+
const messages: Array<unknown> = [];
335+
const logger = Logger.make(({ message }) => {
336+
messages.push(message);
337+
});
338+
return Effect.scoped(
339+
Effect.gen(function* () {
340+
const fake = yield* makeFakeClient;
341+
const voice = yield* makeCodexVoice(fake.client, yield* Effect.scope);
342+
const start = () =>
343+
voice
344+
.start({ providerThreadId: PROVIDER_THREAD, offerSdp: "offer" })
345+
.pipe(Stream.runCollect, Effect.forkChild);
346+
const first = yield* start();
347+
yield* Deferred.await(fake.startRequested);
348+
const stopFailure = new CodexErrors.CodexAppServerRequestError({
349+
code: -32603,
350+
errorMessage: "stop failed",
351+
method: "thread/realtime/stop",
352+
});
353+
fake.setStop(Effect.fail(stopFailure));
354+
fake.setStart(
355+
Effect.fail(
356+
new CodexErrors.CodexAppServerRequestError({
357+
code: -32603,
358+
errorMessage: "unexpected start",
359+
}),
360+
),
361+
);
362+
yield* Fiber.interrupt(first);
363+
const replacementExit = yield* voice
364+
.start({ providerThreadId: PROVIDER_THREAD, offerSdp: "offer" })
365+
.pipe(Stream.runCollect, Effect.exit);
366+
NodeAssert.ok(Exit.isFailure(replacementExit));
367+
NodeAssert.deepEqual(
368+
fake.requests.map(({ method }) => method),
369+
["thread/realtime/start", "thread/realtime/stop", "thread/realtime/stop"],
370+
);
371+
NodeAssert.ok(
372+
messages.flat().includes("Codex voice could not stop the realtime session"),
373+
);
374+
fake.setStop(Effect.void);
375+
fake.setStart(Effect.void);
376+
if (confirmation === "closed notification")
377+
yield* fake.emit("thread/realtime/closed", { threadId: PROVIDER_THREAD });
378+
const recovered = yield* start();
379+
yield* Deferred.await(fake.secondStartRequested);
380+
yield* fake.emit("thread/realtime/closed", { threadId: PROVIDER_THREAD });
381+
yield* Fiber.join(recovered);
382+
NodeAssert.deepEqual(
383+
fake.requests.map(({ method }) => method),
384+
[
385+
"thread/realtime/start",
386+
"thread/realtime/stop",
387+
"thread/realtime/stop",
388+
...(confirmation === "stop response" ? ["thread/realtime/stop"] : []),
389+
"thread/realtime/start",
390+
],
391+
);
392+
}),
393+
).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false })));
394+
},
395+
);
396+
397+
it.effect("retries predecessor cleanup after an interrupted replacement", () =>
398+
Effect.scoped(
399+
Effect.gen(function* () {
400+
const fake = yield* makeFakeClient;
401+
const voice = yield* makeCodexVoice(fake.client, yield* Effect.scope);
402+
const start = () =>
403+
voice
404+
.start({ providerThreadId: PROVIDER_THREAD, offerSdp: "offer" })
405+
.pipe(Stream.runCollect, Effect.forkChild({ startImmediately: true }));
406+
const first = yield* start();
407+
yield* Deferred.await(fake.startRequested);
408+
const stopInterrupted = yield* Deferred.make<void>();
409+
const releaseStop = yield* Deferred.make<void>();
410+
fake.setStop(
411+
Deferred.await(releaseStop).pipe(
412+
Effect.onInterrupt(() => Deferred.succeed(stopInterrupted, undefined)),
413+
),
414+
);
415+
const replacement = yield* start();
416+
yield* Deferred.await(fake.stopRequested);
417+
const interrupted = yield* Fiber.interrupt(replacement).pipe(Effect.forkChild);
418+
yield* Deferred.await(stopInterrupted);
419+
fake.setStop(Effect.void);
420+
yield* Deferred.succeed(releaseStop, undefined);
421+
yield* Fiber.join(interrupted);
422+
yield* Fiber.join(first);
423+
NodeAssert.deepEqual(
424+
fake.requests.map(({ method }) => method),
425+
["thread/realtime/start", "thread/realtime/stop", "thread/realtime/stop"],
426+
);
427+
const recovered = yield* start();
428+
yield* Deferred.await(fake.secondStartRequested);
429+
yield* fake.emit("thread/realtime/closed", { threadId: PROVIDER_THREAD });
430+
yield* Fiber.join(recovered);
431+
}),
432+
),
433+
);
434+
231435
it.effect("stops the realtime session when the client unsubscribes", () =>
232436
Effect.gen(function* () {
233437
const voice = yield* startVoice;

0 commit comments

Comments
 (0)