Skip to content

Commit 71d9cbb

Browse files
fix(server): keep a resumed Codex thread guarded when its resume response is lost
If Codex accepts thread/resume but the response fails or does not decode, the thread may still be running goal turns. The failure path no longer drops the root registration, so the orphan goal-turn guard still pauses them, and it releases held goal updates the same way the success path does. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fedfa2e commit 71d9cbb

2 files changed

Lines changed: 96 additions & 87 deletions

File tree

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

Lines changed: 90 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -9617,94 +9617,101 @@ describe("CodexAdapterV2 post-settle continuation", () => {
96179617
);
96189618

96199619
// regression: #21865 goal turns Codex continues after a server restart ran with no UI
9620-
it.effect("pauses a goal turn Codex starts on a resumed thread", () =>
9621-
Effect.gen(function* () {
9622-
const transcript = makeCodexReplayTranscript({
9623-
scenario: "goal-turn-after-resume",
9624-
entries: [
9625-
...sessionStart,
9626-
...request(
9627-
3,
9628-
"thread/resume",
9629-
{
9620+
it.effect.each(["decoded", "undecodable"] as const)(
9621+
"pauses a goal turn Codex starts on a resumed thread whose response is %s",
9622+
(resumeResponse) =>
9623+
Effect.gen(function* () {
9624+
const transcript = makeCodexReplayTranscript({
9625+
scenario: "goal-turn-after-resume",
9626+
entries: [
9627+
...sessionStart,
9628+
...request(
9629+
3,
9630+
"thread/resume",
9631+
{
9632+
threadId: nativeThreadId,
9633+
excludeTurns: true,
9634+
config: CodexAdapterV2.CODEX_THREAD_CONFIG,
9635+
},
9636+
resumeResponse === "decoded"
9637+
? { thread: { id: nativeThreadId, updatedAt: 1782622450 } }
9638+
: { thread: { id: nativeThreadId } },
9639+
),
9640+
notification("goal resumed", "thread/goal/updated", {
96309641
threadId: nativeThreadId,
9631-
excludeTurns: true,
9632-
config: CodexAdapterV2.CODEX_THREAD_CONFIG,
9633-
},
9634-
{ thread: { id: nativeThreadId, updatedAt: 1782622450 } },
9635-
),
9636-
notification("goal resumed", "thread/goal/updated", {
9637-
threadId: nativeThreadId,
9638-
turnId: null,
9639-
goal: codexGoal("active", 10),
9640-
}),
9641-
notification("goal continuation", "turn/started", {
9642-
threadId: nativeThreadId,
9643-
turn: makeCodexReplayTurn({ id: "orphan-goal-turn", status: "inProgress" }),
9644-
}),
9645-
...request(
9646-
4,
9647-
"thread/goal/set",
9648-
{ threadId: nativeThreadId, status: "paused" },
9649-
{ goal: codexGoal("paused", 10) },
9650-
),
9651-
...request(
9652-
5,
9653-
"turn/interrupt",
9654-
{ threadId: nativeThreadId, turnId: "orphan-goal-turn" },
9655-
{},
9656-
),
9657-
notification("interrupted", "turn/completed", {
9658-
threadId: nativeThreadId,
9659-
turn: makeCodexReplayTurn({ id: "orphan-goal-turn", status: "interrupted" }),
9660-
}),
9661-
],
9662-
});
9663-
const requests: Array<[string, unknown]> = [];
9664-
const interrupted = yield* Deferred.make<void>();
9665-
const harness = yield* makeCodexReplayHarness(
9666-
transcript,
9667-
() => Effect.void,
9668-
(method, params) =>
9669-
Effect.sync(() => {
9670-
requests.push([method, params]);
9671-
}).pipe(
9672-
Effect.andThen(
9673-
method === "turn/interrupt"
9674-
? Deferred.succeed(interrupted, undefined)
9675-
: Effect.void,
9642+
turnId: null,
9643+
goal: codexGoal("active", 10),
9644+
}),
9645+
notification("goal continuation", "turn/started", {
9646+
threadId: nativeThreadId,
9647+
turn: makeCodexReplayTurn({ id: "orphan-goal-turn", status: "inProgress" }),
9648+
}),
9649+
...request(
9650+
4,
9651+
"thread/goal/set",
9652+
{ threadId: nativeThreadId, status: "paused" },
9653+
{ goal: codexGoal("paused", 10) },
9654+
),
9655+
...request(
9656+
5,
9657+
"turn/interrupt",
9658+
{ threadId: nativeThreadId, turnId: "orphan-goal-turn" },
9659+
{},
9660+
),
9661+
notification("interrupted", "turn/completed", {
9662+
threadId: nativeThreadId,
9663+
turn: makeCodexReplayTurn({ id: "orphan-goal-turn", status: "interrupted" }),
9664+
}),
9665+
],
9666+
});
9667+
const requests: Array<[string, unknown]> = [];
9668+
const interrupted = yield* Deferred.make<void>();
9669+
const harness = yield* makeCodexReplayHarness(
9670+
transcript,
9671+
() => Effect.void,
9672+
(method, params) =>
9673+
Effect.sync(() => {
9674+
requests.push([method, params]);
9675+
}).pipe(
9676+
Effect.andThen(
9677+
method === "turn/interrupt"
9678+
? Deferred.succeed(interrupted, undefined)
9679+
: Effect.void,
9680+
),
96769681
),
9677-
),
9678-
);
9679-
yield* harness.runtime.resumeThread({
9680-
providerThread: harness.providerThread,
9681-
modelSelection: CODEX_TEST_MODEL_SELECTION,
9682-
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
9683-
});
9684-
yield* Deferred.await(interrupted);
9685-
const goalStatuses = () =>
9686-
harness.events.flatMap((event) =>
9687-
event.type === "provider_thread.updated"
9688-
? [event.providerThread.goal?.status ?? null]
9689-
: [],
96909682
);
9691-
yield* harness.until(() => goalStatuses().length > 0);
9692-
assert.deepEqual(goalStatuses(), ["active"]);
9693-
assert.deepEqual(requests.slice(-2), [
9694-
["thread/goal/set", { threadId: nativeThreadId, status: "paused" }],
9695-
["turn/interrupt", { threadId: nativeThreadId, turnId: "orphan-goal-turn" }],
9696-
]);
9697-
}).pipe(
9698-
Effect.scoped,
9699-
Effect.provide(
9700-
Layer.mergeAll(
9701-
IdAllocator.layer,
9702-
McpProviderSessions.layer,
9703-
NodeServices.layer,
9704-
NodeCrypto.layer,
9683+
const resumed = yield* harness.runtime
9684+
.resumeThread({
9685+
providerThread: harness.providerThread,
9686+
modelSelection: CODEX_TEST_MODEL_SELECTION,
9687+
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
9688+
})
9689+
.pipe(Effect.exit);
9690+
assert.equal(Exit.isSuccess(resumed), resumeResponse === "decoded");
9691+
yield* Deferred.await(interrupted);
9692+
const goalStatuses = () =>
9693+
harness.events.flatMap((event) =>
9694+
event.type === "provider_thread.updated"
9695+
? [event.providerThread.goal?.status ?? null]
9696+
: [],
9697+
);
9698+
yield* harness.until(() => goalStatuses().length > 0);
9699+
assert.deepEqual(goalStatuses(), ["active"]);
9700+
assert.deepEqual(requests.slice(-2), [
9701+
["thread/goal/set", { threadId: nativeThreadId, status: "paused" }],
9702+
["turn/interrupt", { threadId: nativeThreadId, turnId: "orphan-goal-turn" }],
9703+
]);
9704+
}).pipe(
9705+
Effect.scoped,
9706+
Effect.provide(
9707+
Layer.mergeAll(
9708+
IdAllocator.layer,
9709+
McpProviderSessions.layer,
9710+
NodeServices.layer,
9711+
NodeCrypto.layer,
9712+
),
97059713
),
97069714
),
9707-
),
97089715
);
97099716
});
97109717

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7337,11 +7337,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73377337
const response = yield* ensureInitialized.pipe(
73387338
Effect.andThen(resume),
73397339
Effect.flatMap(decodeCodexResumeMetadata),
7340+
// Stay registered: a lost or undecodable response may still have resumed the thread.
73407341
Effect.onError(() =>
7341-
Effect.sync(() => {
7342-
if (previousRoot === undefined) rootProviderThreads.delete(nativeThreadId);
7343-
else rootProviderThreads.set(nativeThreadId, previousRoot);
7344-
goalUpdatesHeld.delete(nativeThreadId);
7342+
Effect.suspend(() => {
7343+
if (previousRoot !== undefined) {
7344+
rootProviderThreads.set(nativeThreadId, previousRoot);
7345+
}
7346+
return releaseGoalUpdates(nativeThreadId);
73457347
}),
73467348
),
73477349
);

0 commit comments

Comments
 (0)