Skip to content

Commit 384295e

Browse files
committed
fix(server): a resumed Codex goal run stays open until the goal is active
The 5 second wait for Codex to start the next goal turn began when the resume run was held, before thread/goal/set had returned. If reactivating the goal took longer than that, the run settled first, and the turn Codex started afterwards was paused and interrupted as having no run. The wait now starts once the goal activation has finished. The same applies to /goal when its first turn ends before the goal is active.
1 parent 7167433 commit 384295e

2 files changed

Lines changed: 77 additions & 6 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8919,6 +8919,67 @@ describe("CodexAdapterV2 post-settle continuation", () => {
89198919
assert.deepEqual(runningTurns(harness.events), [[null, 1]]);
89208920
}).pipe(Effect.scoped, provideServices),
89218921
);
8922+
8923+
it.effect("keeps the run open while the goal is still being reactivated", () =>
8924+
Effect.gen(function* () {
8925+
const activationRequested = yield* Deferred.make<void>();
8926+
const requests: Array<string> = [];
8927+
const harness = yield* makeCodexReplayHarness(
8928+
makeCodexReplayTranscript({
8929+
scenario: "goal-resume-slow-activation",
8930+
entries: [
8931+
...sessionStart,
8932+
...resumeRequests("paused", "active"),
8933+
turnStarted("continuation"),
8934+
goalUpdated("active", "continuation"),
8935+
turnCompleted("continuation"),
8936+
goalUpdated("complete", null),
8937+
],
8938+
}),
8939+
() => Effect.void,
8940+
(method) =>
8941+
Effect.sync(() => {
8942+
requests.push(method);
8943+
}).pipe(
8944+
Effect.andThen(
8945+
method === "thread/goal/set"
8946+
? Deferred.succeed(activationRequested, undefined).pipe(
8947+
Effect.andThen(Effect.sleep("10 seconds")),
8948+
)
8949+
: Effect.void,
8950+
),
8951+
),
8952+
);
8953+
yield* harness.runtime
8954+
.startTurn(yield* goalTurnInput(harness, "/goal resume"))
8955+
.pipe(Effect.forkChild({ startImmediately: true }));
8956+
yield* Deferred.await(activationRequested);
8957+
for (let tick = 0; tick < 20; tick++) yield* Effect.yieldNow;
8958+
8959+
// The continuation grace is for Codex to start a turn once the goal is
8960+
// active, so it must not run out while the activation is in flight.
8961+
yield* TestClock.adjust("6 seconds");
8962+
for (let tick = 0; tick < 20; tick++) yield* Effect.yieldNow;
8963+
assert.lengthOf(harness.terminalEvents(), 0);
8964+
8965+
yield* TestClock.adjust("5 seconds");
8966+
yield* harness.firstTerminal;
8967+
8968+
const providerTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({
8969+
driver: CodexAdapterV2.CODEX_DRIVER_KIND,
8970+
nativeTurnId: "continuation",
8971+
});
8972+
assert.deepEqual(
8973+
harness.terminalEvents().map((event) => [event.providerTurnId, event.status]),
8974+
[[providerTurnId, "completed"]],
8975+
);
8976+
assert.notInclude(requests, "turn/interrupt");
8977+
assert.deepEqual(runningTurns(harness.events), [
8978+
[null, 1],
8979+
["continuation", 2],
8980+
]);
8981+
}).pipe(Effect.scoped, provideServices),
8982+
);
89228983
});
89238984

89248985
it.effect("settles a run held for the next goal turn when Stop arrives in between", () =>

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5631,6 +5631,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
56315631
}
56325632
});
56335633

5634+
const startGoalHoldGrace = (nativeThreadId: string, event: CodexRootTerminalEvent) =>
5635+
Effect.sleep(CODEX_GOAL_CONTINUATION_GRACE).pipe(
5636+
Effect.andThen(
5637+
turnTerminalizationPermit.withPermits(1)(releaseGoalHold(nativeThreadId, event)),
5638+
),
5639+
Effect.forkIn(scope),
5640+
);
5641+
56345642
const emitOrDeferRootTerminal = Effect.fn("CodexAdapterV2.emitOrDeferRootTerminal")(
56355643
function* (input: {
56365644
readonly context: ActiveCodexTurnContext;
@@ -5665,12 +5673,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
56655673
completedTurn: input.goalHoldTurn,
56665674
next: yield* Deferred.make<ActiveCodexTurnContext | undefined>(),
56675675
});
5668-
yield* Effect.sleep(CODEX_GOAL_CONTINUATION_GRACE).pipe(
5669-
Effect.andThen(
5670-
turnTerminalizationPermit.withPermits(1)(releaseGoalHold(nativeThreadId, event)),
5671-
),
5672-
Effect.forkIn(scope),
5673-
);
5676+
// The grace is for Codex to start a turn once the goal is active,
5677+
// so an activation in flight starts it when it finishes.
5678+
if (!goalActivations.has(nativeThreadId)) {
5679+
yield* startGoalHoldGrace(nativeThreadId, event);
5680+
}
56745681
return;
56755682
}
56765683
yield* emitRootTerminal(input.context, event);
@@ -6367,6 +6374,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
63676374
}
63686375
if (goalsByNativeThread.get(threadId)?.status !== "active") {
63696376
yield* releaseGoalHold(threadId);
6377+
} else if (!goalActivations.has(threadId)) {
6378+
const hold = goalHolds.get(threadId);
6379+
if (hold !== undefined) yield* startGoalHoldGrace(threadId, hold.event);
63706380
}
63716381
}),
63726382
),

0 commit comments

Comments
 (0)