Skip to content

Commit 29919b3

Browse files
fix(server): decide a Codex goal's pending wake before a continuation can claim it
The pending-wake lookup ran in a fork after the goal hold was published, so a continuation Codex started meanwhile could adopt the hold and keep the loop going. The lookup now runs under the terminalization permit before the hold exists, and a pending wake claims the hold in the same critical section, so a racing continuation is stopped as an orphan. Inspecting the goal with /goal no longer drops the marker that resumes a goal paused for a delegated task's wake. AI-assisted; verified with the tests and tools listed above.
1 parent e8f46d3 commit 29919b3

2 files changed

Lines changed: 73 additions & 19 deletions

File tree

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9884,6 +9884,8 @@ describe("CodexAdapterV2 post-settle continuation", () => {
98849884

98859885
it.effect("pauses a goal at once while a delegated task will wake the thread", () =>
98869886
Effect.gen(function* () {
9887+
const lookupStarted = yield* Deferred.make<void>();
9888+
const lookupAnswered = yield* Deferred.make<void>();
98879889
const turnStart = (id: number, text: string) =>
98889890
Effect.map(
98899891
CodexAdapterV2.buildCodexTurnStartParams({
@@ -9919,21 +9921,44 @@ describe("CodexAdapterV2 post-settle continuation", () => {
99199921
threadId: nativeThreadId,
99209922
turn: makeCodexReplayTurn({ id: "goal-turn", status: "completed" }),
99219923
}),
9924+
// Codex continues before the wake lookup answers.
9925+
notification("continuation", "turn/started", {
9926+
threadId: nativeThreadId,
9927+
turn: makeCodexReplayTurn({ id: "goal-turn-2", status: "inProgress" }),
9928+
}),
99229929
...request(
99239930
4,
99249931
"thread/goal/set",
99259932
{ threadId: nativeThreadId, status: "paused" },
99269933
{ goal: codexGoal("paused", 10) },
99279934
),
9928-
yield* turnStart(5, "Child finished"),
9935+
...request(
9936+
5,
9937+
"thread/goal/set",
9938+
{ threadId: nativeThreadId, status: "paused" },
9939+
{ goal: codexGoal("paused", 10) },
9940+
),
9941+
...request(
9942+
6,
9943+
"turn/interrupt",
9944+
{ threadId: nativeThreadId, turnId: "goal-turn-2" },
9945+
{},
9946+
),
9947+
...request(
9948+
7,
9949+
"thread/goal/get",
9950+
{ threadId: nativeThreadId },
9951+
{ goal: codexGoal("paused", 10) },
9952+
),
9953+
yield* turnStart(8, "Child finished"),
99299954
...withReplayRequestId(
99309955
codexReplayPreamble({ nativeThreadId, nativeTurnId: "wake-turn", prompt: "" }).slice(
99319956
6,
99329957
),
9933-
5,
9958+
8,
99349959
),
99359960
...request(
9936-
6,
9961+
9,
99379962
"thread/goal/set",
99389963
{ threadId: nativeThreadId, status: "active" },
99399964
{ goal: codexGoal("active", 10) },
@@ -9954,17 +9979,33 @@ describe("CodexAdapterV2 post-settle continuation", () => {
99549979
endpoint: "http://127.0.0.1:43123/mcp",
99559980
authorizationHeader: "Bearer test",
99569981
browserToolsAvailable: true,
9957-
hasPendingWake: Effect.succeed(true),
9982+
hasPendingWake: Deferred.succeed(lookupStarted, undefined).pipe(
9983+
Effect.andThen(Deferred.await(lookupAnswered)),
9984+
Effect.as(true),
9985+
),
99589986
});
99599987
yield* harness.runtime.startTurn(yield* goalTurnInput(harness, "Delegate and wait"));
9988+
yield* Deferred.await(lookupStarted);
9989+
yield* Deferred.succeed(lookupAnswered, undefined);
99609990
// Settles without waiting out the continuation grace.
99619991
yield* harness.firstTerminal;
99629992
assert.deepEqual(providerGoals(harness.events), ["active", "paused"]);
99639993
assert.deepEqual(
99649994
harness.terminalEvents().map((event) => event.status),
99659995
["completed"],
99669996
);
9997+
yield* harness.until(() => harness.terminalEvents().length === 1);
9998+
assert.deepEqual(
9999+
harness.events.flatMap((event) =>
10000+
event.type === "provider_turn.updated" && event.providerTurn.status === "running"
10001+
? [event.providerTurn.ordinal]
10002+
: [],
10003+
),
10004+
[1],
10005+
);
996710006

10007+
// Inspecting the goal must not cancel its resume on the wake.
10008+
yield* harness.runtime.startTurn(yield* goalTurnInput(harness, "/goal"));
996810009
yield* harness.runtime.startTurn(yield* goalTurnInput(harness, "Child finished"));
996910010
yield* harness.until(() => providerGoals(harness.events).at(-1) === "active");
997010011
}).pipe(

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6253,21 +6253,30 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
62536253
event.status === "completed" &&
62546254
nativeThreadId != null
62556255
) {
6256+
const hasPendingWake = (yield* mcpSessions.read(input.context.input.threadId))
6257+
?.hasPendingWake;
6258+
// Decided under the permit before the hold exists, so no continuation can adopt it first.
6259+
const waitsForWake = hasPendingWake === undefined ? false : yield* hasPendingWake;
62566260
goalHolds.set(nativeThreadId, {
62576261
context: input.context,
62586262
event,
62596263
completedTurn: input.goalHoldTurn,
62606264
next: yield* Deferred.make<ActiveCodexTurnContext | undefined>(),
62616265
});
6262-
const hasPendingWake = (yield* mcpSessions.read(input.context.input.threadId))
6263-
?.hasPendingWake;
62646266
// A continuation turn would only report that the delegated task is still running.
6265-
yield* (hasPendingWake ?? Effect.succeed(false)).pipe(
6266-
Effect.tap((waitsForWake) =>
6267-
waitsForWake ? Effect.void : Effect.sleep(CODEX_GOAL_CONTINUATION_GRACE),
6268-
),
6269-
Effect.flatMap((waitsForWake) =>
6270-
pauseUncontinuedGoal(nativeThreadId, event, waitsForWake),
6267+
const claimed = waitsForWake ? claimGoalHold(nativeThreadId, event) : undefined;
6268+
if (claimed !== undefined) {
6269+
yield* pauseClaimedGoal(nativeThreadId, claimed, true).pipe(Effect.forkIn(scope));
6270+
return;
6271+
}
6272+
yield* Effect.sleep(CODEX_GOAL_CONTINUATION_GRACE).pipe(
6273+
Effect.andThen(
6274+
Effect.suspend(() => {
6275+
const hold = claimGoalHold(nativeThreadId, event);
6276+
return hold === undefined
6277+
? Effect.void
6278+
: pauseClaimedGoal(nativeThreadId, hold, false);
6279+
}),
62716280
),
62726281
Effect.andThen(
62736282
turnTerminalizationPermit.withPermits(1)(releaseGoalHold(nativeThreadId, event)),
@@ -6283,22 +6292,26 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
62836292

62846293
// A goal must not read as active once its run ends. Claiming the hold first
62856294
// makes a turn Codex starts during the pause an orphan, which is stopped.
6286-
const pauseUncontinuedGoal = Effect.fnUntraced(function* (
6287-
nativeThreadId: string,
6288-
expected: CodexRootTerminalEvent,
6289-
waitsForWake: boolean,
6290-
) {
6295+
const claimGoalHold = (nativeThreadId: string, expected: CodexRootTerminalEvent) => {
62916296
const hold = goalHolds.get(nativeThreadId);
62926297
if (
62936298
hold?.event !== expected ||
62946299
goalStops.has(nativeThreadId) ||
62956300
goalsByNativeThread.get(nativeThreadId)?.status !== "active"
62966301
) {
6297-
return;
6302+
return undefined;
62986303
}
62996304
goalHolds.delete(nativeThreadId);
63006305
// Keeps a turn Codex starts before settlement an orphan once the goal reads paused.
63016306
goalStops.set(nativeThreadId, (goalStops.get(nativeThreadId) ?? 0) + 1);
6307+
return hold;
6308+
};
6309+
6310+
const pauseClaimedGoal = Effect.fnUntraced(function* (
6311+
nativeThreadId: string,
6312+
hold: CodexGoalHold,
6313+
waitsForWake: boolean,
6314+
) {
63026315
if (!waitsForWake) {
63036316
yield* Effect.logWarning("orchestration-v2.codex-goal-continuation-expired", {
63046317
nativeThreadId,
@@ -7316,7 +7329,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73167329
if (command.type === "set") yield* validateCodexGoalObjective(command.objective);
73177330
const threadId = yield* getNativeThreadId(turnInput.providerThread);
73187331
rootProviderThreads.set(threadId, turnInput.providerThread);
7319-
goalsPausedForWake.delete(threadId);
73207332
const readGoal = client
73217333
.request("thread/goal/get", { threadId })
73227334
.pipe(
@@ -7332,6 +7344,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73327344
current === null ? "No goal is set." : describeCodexGoal(current),
73337345
);
73347346
}
7347+
goalsPausedForWake.delete(threadId);
73357348
if (command.type === "clear") {
73367349
const { cleared } = yield* client.request("thread/goal/clear", { threadId });
73377350
goalsByNativeThread.set(threadId, null);

0 commit comments

Comments
 (0)