Skip to content

Commit 95c96c1

Browse files
fix(server): drop Codex sessions whose runtime exited
The Codex adapter forwarded a runtime's session.exited event but left the dead session in its map, so hasSession and listSessions kept reporting the thread as live. Startup reconciliation trusts listSessions and the session reaper skips sessions holding an activeTurnId, so a thread whose Codex process died mid-turn stayed running and showed Working forever.
1 parent 061543e commit 95c96c1

2 files changed

Lines changed: 108 additions & 17 deletions

File tree

‎apps/server/src/provider/Layers/CodexAdapter.test.ts‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
2323
import { it, vi } from "@effect/vitest";
2424

2525
import * as Context from "effect/Context";
26+
import * as Deferred from "effect/Deferred";
2627
import * as Effect from "effect/Effect";
2728
import * as Exit from "effect/Exit";
2829
import * as Fiber from "effect/Fiber";
@@ -75,6 +76,7 @@ class FakeCodexRuntime implements CodexSessionRuntimeShape {
7576
updatedAt: this.now,
7677
} satisfies ProviderSession),
7778
);
79+
public startEffect: Effect.Effect<ProviderSession> | undefined;
7880

7981
public readonly sendTurnImpl = vi.fn(
8082
(_input: CodexSessionRuntimeSendTurnInput): Promise<ProviderTurnStartResult> =>
@@ -127,7 +129,7 @@ class FakeCodexRuntime implements CodexSessionRuntimeShape {
127129
}
128130

129131
start() {
130-
return Effect.promise(() => this.startImpl());
132+
return this.startEffect ?? Effect.promise(() => this.startImpl());
131133
}
132134

133135
getSession = Effect.promise(() => this.startImpl());
@@ -188,13 +190,24 @@ function makeRuntimeFactory() {
188190
function makeScopedRuntimeFactory(options?: { readonly failConstruction?: boolean }) {
189191
const runtimes: Array<FakeCodexRuntime> = [];
190192
const releasedThreadIds: Array<ThreadId> = [];
193+
let exitDuringNextStart:
194+
| {
195+
readonly exitForwarded: Deferred.Deferred<void>;
196+
readonly scopeReleased: Deferred.Deferred<void>;
197+
}
198+
| undefined;
191199

192200
const factory = vi.fn((runtimeOptions: CodexSessionRuntimeOptions) =>
193201
Effect.gen(function* () {
202+
const exitDuringStart = exitDuringNextStart;
203+
exitDuringNextStart = undefined;
194204
yield* Scope.Scope;
195205
yield* Effect.addFinalizer(() =>
196206
Effect.sync(() => {
197207
releasedThreadIds.push(runtimeOptions.threadId);
208+
if (exitDuringStart) {
209+
Deferred.doneUnsafe(exitDuringStart.scopeReleased, Effect.void);
210+
}
198211
}),
199212
);
200213

@@ -206,6 +219,23 @@ function makeScopedRuntimeFactory(options?: { readonly failConstruction?: boolea
206219
}
207220

208221
const runtime = new FakeCodexRuntime(runtimeOptions);
222+
if (exitDuringStart) {
223+
const startImpl = runtime.startImpl.getMockImplementation();
224+
runtime.startEffect = runtime
225+
.emit({
226+
id: asEventId("evt-session-exited-during-start"),
227+
kind: "notification",
228+
provider: ProviderDriverKind.make("codex"),
229+
createdAt: "2026-01-01T00:00:00.000Z",
230+
method: "session/exited",
231+
threadId: runtimeOptions.threadId,
232+
message: "Codex App Server exited with code 1.",
233+
})
234+
.pipe(
235+
Effect.andThen(Deferred.await(exitDuringStart.exitForwarded)),
236+
Effect.andThen(Effect.promise(() => startImpl!())),
237+
);
238+
}
209239
runtimes.push(runtime);
210240
return runtime;
211241
}),
@@ -214,6 +244,13 @@ function makeScopedRuntimeFactory(options?: { readonly failConstruction?: boolea
214244
return {
215245
factory,
216246
releasedThreadIds,
247+
exitDuringNextStart: () => {
248+
exitDuringNextStart = {
249+
exitForwarded: Deferred.makeUnsafe<void>(),
250+
scopeReleased: Deferred.makeUnsafe<void>(),
251+
};
252+
return exitDuringNextStart;
253+
},
217254
get lastRuntime(): FakeCodexRuntime | undefined {
218255
return runtimes.at(-1);
219256
},
@@ -2562,6 +2599,49 @@ const scopedLifecycleLayer = it.layer(
25622599
);
25632600

25642601
scopedLifecycleLayer("CodexAdapterLive scoped lifecycle", (it) => {
2602+
it.effect("cleans up an exit during startup without deleting its replacement", () =>
2603+
Effect.gen(function* () {
2604+
scopedLifecycleRuntimeFactory.releasedThreadIds.length = 0;
2605+
const signals = scopedLifecycleRuntimeFactory.exitDuringNextStart();
2606+
const adapter = yield* CodexAdapter;
2607+
const threadId = asThreadId("thread-exited");
2608+
const exitedFiber = yield* Stream.runHead(
2609+
Stream.filter(adapter.streamEvents, (event) => event.threadId === threadId),
2610+
).pipe(
2611+
Effect.tap(() => Deferred.succeed(signals.exitForwarded, undefined)),
2612+
Effect.forkChild,
2613+
);
2614+
2615+
yield* adapter.startSession({
2616+
provider: ProviderDriverKind.make("codex"),
2617+
threadId,
2618+
runtimeMode: "full-access",
2619+
});
2620+
2621+
const runtime = scopedLifecycleRuntimeFactory.lastRuntime;
2622+
NodeAssert.ok(runtime);
2623+
const exited = yield* Fiber.join(exitedFiber);
2624+
NodeAssert.equal(exited._tag, "Some");
2625+
NodeAssert.equal(yield* adapter.hasSession(threadId), false);
2626+
NodeAssert.deepStrictEqual(yield* adapter.listSessions(), []);
2627+
2628+
yield* adapter.startSession({
2629+
provider: ProviderDriverKind.make("codex"),
2630+
threadId,
2631+
runtimeMode: "full-access",
2632+
});
2633+
const replacement = scopedLifecycleRuntimeFactory.lastRuntime;
2634+
NodeAssert.ok(replacement);
2635+
NodeAssert.notEqual(replacement, runtime);
2636+
2637+
yield* Deferred.await(signals.scopeReleased);
2638+
NodeAssert.equal(yield* adapter.hasSession(threadId), true);
2639+
NodeAssert.equal((yield* adapter.listSessions()).length, 1);
2640+
NodeAssert.equal(runtime.closeImpl.mock.calls.length, 1);
2641+
NodeAssert.deepStrictEqual(scopedLifecycleRuntimeFactory.releasedThreadIds, [threadId]);
2642+
}),
2643+
);
2644+
25652645
it.effect("closes the externally owned session scope on stopSession", () =>
25662646
Effect.gen(function* () {
25672647
scopedLifecycleRuntimeFactory.releasedThreadIds.length = 0;

‎apps/server/src/provider/Layers/CodexAdapter.ts‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,7 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
22312231
: undefined);
22322232
const managedNativeEventLogger =
22332233
options?.nativeEventLogger === undefined ? nativeEventLogger : undefined;
2234+
const adapterScope = yield* Scope.Scope;
22342235
const runtimeEventQueue = yield* Queue.unbounded<ProviderRuntimeEvent>();
22352236
const sessions = new Map<ThreadId, CodexAdapterSessionContext>();
22362237

@@ -2436,9 +2437,31 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
24362437
return;
24372438
}
24382439
yield* Queue.offerAll(runtimeEventQueue, runtimeEvents);
2440+
2441+
// A runtime that reported its own exit is gone, but startup
2442+
// reconciliation and the session reaper keep reading the thread as
2443+
// live until it leaves this map. Teardown is forked because it
2444+
// interrupts this fiber and closes the scope it runs in.
2445+
if (runtimeEvents.some((runtimeEvent) => runtimeEvent.type === "session.exited")) {
2446+
const exited = sessions.get(input.threadId);
2447+
if (exited?.scope === sessionScope) {
2448+
sessions.delete(input.threadId);
2449+
yield* stopSessionInternal(exited).pipe(Effect.forkIn(adapterScope));
2450+
}
2451+
}
24392452
}),
24402453
).pipe(Effect.forkIn(sessionScope));
24412454

2455+
const session: CodexAdapterSessionContext = {
2456+
threadId: input.threadId,
2457+
scope: sessionScope,
2458+
runtime,
2459+
eventFiber,
2460+
turnTokenUsage,
2461+
stopped: false,
2462+
};
2463+
sessions.set(input.threadId, session);
2464+
24422465
const started = yield* runtime.start().pipe(
24432466
Effect.mapError(
24442467
(cause) =>
@@ -2449,23 +2472,9 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
24492472
cause,
24502473
}),
24512474
),
2452-
Effect.onError(() =>
2453-
runtime.close.pipe(
2454-
Effect.andThen(Effect.ignore(Scope.close(sessionScope, Exit.void))),
2455-
Effect.andThen(Fiber.interrupt(eventFiber)),
2456-
Effect.ignore,
2457-
),
2458-
),
2475+
Effect.onError(() => stopSessionInternal(session)),
24592476
);
24602477

2461-
sessions.set(input.threadId, {
2462-
threadId: input.threadId,
2463-
scope: sessionScope,
2464-
runtime,
2465-
eventFiber,
2466-
turnTokenUsage,
2467-
stopped: false,
2468-
});
24692478
sessionScopeTransferred = true;
24702479

24712480
return started;
@@ -2667,7 +2676,9 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
26672676
return;
26682677
}
26692678
session.stopped = true;
2670-
sessions.delete(session.threadId);
2679+
if (sessions.get(session.threadId) === session) {
2680+
sessions.delete(session.threadId);
2681+
}
26712682
yield* session.runtime.close.pipe(Effect.ignore);
26722683
yield* Effect.ignore(Scope.close(session.scope, Exit.void));
26732684
yield* Fiber.interrupt(session.eventFiber).pipe(Effect.ignore);

0 commit comments

Comments
 (0)