Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions apps/server/src/codexAppServerManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,60 @@ describe("startSession", () => {
manager.stopAll();
}
});

it("supports silent shutdown without emitting session/closed lifecycle events", () => {
const manager = new CodexAppServerManager();
const emitLifecycleEvent = vi.spyOn(
manager as unknown as {
emitLifecycleEvent: (...args: unknown[]) => void;
},
"emitLifecycleEvent",
);
const updateSession = vi.spyOn(
manager as unknown as {
updateSession: (...args: unknown[]) => void;
},
"updateSession",
);
const sessions = (
manager as unknown as {
sessions: Map<string, unknown>;
}
).sessions;

sessions.set("thread-1", {
session: {
provider: "codex",
status: "ready",
threadId: asThreadId("thread-1"),
runtimeMode: "full-access",
createdAt: "2026-03-19T00:00:00.000Z",
updatedAt: "2026-03-19T00:00:00.000Z",
},
account: {
type: "unknown",
planType: null,
sparkEnabled: true,
},
child: {
killed: true,
},
output: {
close: vi.fn(),
},
pending: new Map(),
pendingApprovals: new Map(),
pendingUserInputs: new Map(),
nextRequestId: 1,
stopping: false,
});

manager.stopAll({ emitLifecycleEvent: false });

expect(updateSession).toHaveBeenCalled();
expect(emitLifecycleEvent).not.toHaveBeenCalled();
expect(sessions.size).toBe(0);
});
});

describe("sendTurn", () => {
Expand Down
14 changes: 10 additions & 4 deletions apps/server/src/codexAppServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export interface CodexAppServerStartSessionInput {
readonly runtimeMode: RuntimeMode;
}

interface CodexAppServerStopOptions {
readonly emitLifecycleEvent?: boolean;
}

export interface CodexThreadTurnSnapshot {
id: TurnId;
items: unknown[];
Expand Down Expand Up @@ -895,7 +899,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
});
}

stopSession(threadId: ThreadId): void {
stopSession(threadId: ThreadId, options?: CodexAppServerStopOptions): void {
const context = this.sessions.get(threadId);
if (!context) {
return;
Expand All @@ -921,7 +925,9 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
status: "closed",
activeTurnId: undefined,
});
this.emitLifecycleEvent(context, "session/closed", "Session stopped");
if (options?.emitLifecycleEvent ?? true) {
this.emitLifecycleEvent(context, "session/closed", "Session stopped");
}
this.sessions.delete(threadId);
}

Expand All @@ -935,9 +941,9 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
return this.sessions.has(threadId);
}

stopAll(): void {
stopAll(options?: CodexAppServerStopOptions): void {
for (const threadId of this.sessions.keys()) {
this.stopSession(threadId);
this.stopSession(threadId, options);
}
}

Expand Down
40 changes: 37 additions & 3 deletions apps/server/src/provider/Layers/CodexAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ class FakeCodexManager extends CodexAppServerManager {
): Promise<void> => undefined,
);

public stopAllImpl = vi.fn(() => undefined);
public stopAllImpl = vi.fn(
(_options?: Parameters<CodexAppServerManager["stopAll"]>[0]) => undefined,
);

override startSession(input: CodexAppServerStartSessionInput): Promise<ProviderSession> {
return this.startSessionImpl(input);
Expand Down Expand Up @@ -134,8 +136,8 @@ class FakeCodexManager extends CodexAppServerManager {
return false;
}

override stopAll(): void {
this.stopAllImpl();
override stopAll(options?: Parameters<CodexAppServerManager["stopAll"]>[0]): void {
this.stopAllImpl(options);
}
}

Expand Down Expand Up @@ -431,6 +433,18 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
}),
);

it.effect("suppresses lifecycle events when adapter stopAll tears down sessions", () =>
Effect.gen(function* () {
const adapter = yield* CodexAdapter;

yield* adapter.stopAll();

assert.deepEqual(lifecycleManager.stopAllImpl.mock.calls.at(-1), [
{ emitLifecycleEvent: false },
]);
}),
);

it.effect("maps retryable Codex error notifications to runtime.warning", () =>
Effect.gen(function* () {
const adapter = yield* CodexAdapter;
Expand Down Expand Up @@ -977,6 +991,26 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
);
});

it.effect("suppresses lifecycle events when the adapter scope finalizes", () =>
Effect.gen(function* () {
const manager = new FakeCodexManager();
const layer = makeCodexAdapterLive({ manager }).pipe(
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(providerSessionDirectoryTestLayer),
Layer.provideMerge(NodeServices.layer),
);

yield* Effect.scoped(
Effect.gen(function* () {
yield* CodexAdapter;
}).pipe(Effect.provide(layer)),
);

assert.deepEqual(manager.stopAllImpl.mock.calls, [[{ emitLifecycleEvent: false }]]);
}),
);

afterAll(() => {
if (lifecycleManager.stopAllImpl.mock.calls.length === 0) {
lifecycleManager.stopAll();
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/provider/Layers/CodexAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
const manager = yield* Effect.acquireRelease(acquireManager(), (manager) =>
Effect.sync(() => {
try {
manager.stopAll();
manager.stopAll({ emitLifecycleEvent: false });
} catch {
// Finalizers should never fail and block shutdown.
}
Expand Down Expand Up @@ -1565,7 +1565,7 @@ const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (

const stopAll: CodexAdapterShape["stopAll"] = () =>
Effect.sync(() => {
manager.stopAll();
manager.stopAll({ emitLifecycleEvent: false });
});

const runtimeEventQueue = yield* Queue.unbounded<ProviderRuntimeEvent>();
Expand Down
Loading