Skip to content

Commit 7413951

Browse files
fix(server): clear Codex commands when their native thread closes
1 parent 98bc998 commit 7413951

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

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

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
} from "@t3tools/contracts";
3232
import { assert, describe, it } from "@effect/vitest";
3333
import * as HostProcess from "@t3tools/shared/HostProcess";
34+
import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
3435
import { SpawnExecutableResolution } from "@t3tools/shared/shell";
3536
import * as CodexClient from "effect-codex-app-server/client";
3637
import * as CodexError from "effect-codex-app-server/errors";
@@ -4692,6 +4693,131 @@ describe("CodexAdapterV2 post-settle continuation", () => {
46924693
),
46934694
);
46944695

4696+
it.effect("ends retained commands when Codex closes their native thread", () =>
4697+
Effect.gen(function* () {
4698+
const unsubscribe = (id: number, closedThreadId?: string) => [
4699+
{
4700+
type: "expect_outbound" as const,
4701+
label: `unsubscribe-${id}`,
4702+
frame: {
4703+
id,
4704+
method: "thread/unsubscribe",
4705+
params: { threadId: BG_NATIVE_THREAD },
4706+
},
4707+
},
4708+
...(closedThreadId === undefined
4709+
? []
4710+
: [
4711+
{
4712+
type: "emit_inbound" as const,
4713+
label: `closed-${id}`,
4714+
frame: { method: "thread/closed", params: { threadId: closedThreadId } },
4715+
},
4716+
]),
4717+
{
4718+
type: "emit_inbound" as const,
4719+
label: `unsubscribe-result-${id}`,
4720+
frame: { id, result: { status: "notSubscribed" } },
4721+
},
4722+
];
4723+
const transcript = makeCodexReplayTranscript({
4724+
scenario: "codex-background-thread-closed",
4725+
entries: [
4726+
...backgroundExecTranscript.entries.slice(0, -1),
4727+
...unsubscribe(4),
4728+
...unsubscribe(5, "another-native-thread"),
4729+
...unsubscribe(6, BG_NATIVE_THREAD),
4730+
// A duplicate closure and a delayed item completion must not wake
4731+
// the completed root or restore the command after it was ended.
4732+
...unsubscribe(7, BG_NATIVE_THREAD).slice(0, -1),
4733+
{
4734+
type: "emit_inbound",
4735+
label: "late-command-completion",
4736+
frame: {
4737+
method: "item/completed",
4738+
params: {
4739+
item: backgroundCommandItem("completed"),
4740+
threadId: BG_NATIVE_THREAD,
4741+
turnId: BG_NATIVE_TURN,
4742+
completedAtMs: 1782622465500,
4743+
},
4744+
},
4745+
},
4746+
...unsubscribe(7).slice(-1),
4747+
],
4748+
});
4749+
const interrupted = yield* Deferred.make<void>();
4750+
const harness = yield* makeCodexReplayHarness(transcript, (event) =>
4751+
event.type === "turn_item.updated" &&
4752+
event.turnItem.nativeItemRef?.nativeId === BG_COMMAND_ITEM &&
4753+
event.turnItem.status === "interrupted"
4754+
? Deferred.succeed(interrupted, undefined)
4755+
: Effect.void,
4756+
);
4757+
yield* harness.runtime.startTurn(
4758+
makeCodexTestTurnInput({
4759+
threadId: harness.threadId,
4760+
providerThread: harness.providerThread,
4761+
now: yield* DateTime.now,
4762+
attemptId: RunAttemptId.make("attempt-codex-thread-closed"),
4763+
text: BG_PROMPT,
4764+
}),
4765+
);
4766+
yield* harness.firstTerminal;
4767+
const unload = harness.runtime.unloadThread!({ providerThread: harness.providerThread });
4768+
yield* unload;
4769+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
4770+
// The reply follows the notification on the same input reader, so it
4771+
// also fences notification handling without a sleep or a polling loop.
4772+
yield* unload;
4773+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
4774+
yield* unload;
4775+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
4776+
assert.isFalse(
4777+
yield* harness.runtime.hasPendingBackgroundWorkForThread!(harness.providerThread),
4778+
);
4779+
yield* Deferred.await(interrupted);
4780+
yield* unload;
4781+
assert.lengthOf(harness.continuationRequests, 0);
4782+
assert.deepEqual(
4783+
harness.terminalEvents().map((event) => event.status),
4784+
["completed"],
4785+
);
4786+
const commandUpdates = harness.events.filter(
4787+
(
4788+
event,
4789+
): event is Extract<
4790+
ProviderAdapter.ProviderAdapterV2Event,
4791+
{ type: "turn_item.updated" }
4792+
> =>
4793+
event.type === "turn_item.updated" &&
4794+
event.turnItem.nativeItemRef?.nativeId === BG_COMMAND_ITEM,
4795+
);
4796+
assert.deepEqual(
4797+
commandUpdates.map((event) => event.turnItem.status),
4798+
["running", "interrupted"],
4799+
);
4800+
assert.deepEqual(
4801+
derivePendingBackgroundWork({
4802+
latestRun: { id: RunId.make("completed-root"), ordinal: 1, status: "completed" },
4803+
providerThreads: [],
4804+
turnItems: commandUpdates.slice(-1).map((event) => event.turnItem),
4805+
}),
4806+
[],
4807+
);
4808+
const endedNode = harness.events.findLast(
4809+
(event) =>
4810+
event.type === "node.updated" && event.node.nativeItemRef?.nativeId === BG_COMMAND_ITEM,
4811+
);
4812+
assert.isTrue(endedNode?.type === "node.updated" && endedNode.node.status === "interrupted");
4813+
}).pipe(
4814+
Effect.scoped,
4815+
Effect.provide(
4816+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
4817+
),
4818+
),
4819+
);
4820+
46954821
// "thread_unloaded": the thread was settled, so T3 unsubscribed and Codex
46964822
// unloaded it (killing its terminals) before Stop arrived.
46974823
const backgroundStopCases = [true, false, "still_running", "thread_unloaded"] as const;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4693,6 +4693,30 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
46934693
});
46944694
});
46954695

4696+
// Codex unloads an unsubscribed idle thread after shutting down its
4697+
// terminals, without completing each command item. The shared session
4698+
// stays alive, so its retained commands need their own terminal events.
4699+
yield* client.handleServerNotification("thread/closed", (payload) =>
4700+
Effect.gen(function* () {
4701+
const completedAt = yield* DateTime.now;
4702+
for (const context of (yield* Ref.get(settledTurns)).values()) {
4703+
if (context.providerThread.nativeThreadRef?.nativeId !== payload.threadId) continue;
4704+
yield* terminalizeRunningCommandItems(
4705+
context,
4706+
context.nativeTurnId,
4707+
"interrupted",
4708+
completedAt,
4709+
);
4710+
yield* Ref.update(runningCommandItemsByTurn, (current) => {
4711+
const updated = new Map(current);
4712+
updated.delete(context.nativeTurnId);
4713+
return updated;
4714+
});
4715+
yield* releaseSettledTurnIfIdle(context.nativeTurnId);
4716+
}
4717+
}).pipe(Effect.orDie, turnTerminalizationPermit.withPermits(1)),
4718+
);
4719+
46964720
yield* client.handleServerNotification("item/started", (payload) =>
46974721
Effect.gen(function* () {
46984722
const context = yield* awaitActiveTurn(payload.turnId);

0 commit comments

Comments
 (0)