Skip to content

Commit 7167433

Browse files
committed
fix(server): Codex /goal resume no longer fails with EmptyInput
Resuming a Codex goal started a native turn with no input, which Codex rejects with EmptyInput. The run failed before the goal was reactivated, and any turn Codex had already started kept running with no run attached. Resuming now starts no turn of its own. The run takes over the turn Codex already started on the thread, or else is held open while the goal is reactivated and adopts the turn Codex starts next. If the goal stays inactive the run settles right away. Fixes #20232
1 parent cfafb4b commit 7167433

2 files changed

Lines changed: 185 additions & 14 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
@@ -8795,6 +8795,132 @@ describe("CodexAdapterV2 post-settle continuation", () => {
87958795
),
87968796
);
87978797

8798+
describe("/goal resume", () => {
8799+
const turnStarted = (id: string) =>
8800+
notification(`${id} started`, "turn/started", {
8801+
threadId: nativeThreadId,
8802+
turn: makeCodexReplayTurn({ id, status: "inProgress" }),
8803+
});
8804+
const turnCompleted = (id: string) =>
8805+
notification(`${id} completed`, "turn/completed", {
8806+
threadId: nativeThreadId,
8807+
turn: makeCodexReplayTurn({ id, status: "completed" }),
8808+
});
8809+
const goalUpdated = (status: string, turnId: string | null) =>
8810+
notification(`goal ${status}`, "thread/goal/updated", {
8811+
threadId: nativeThreadId,
8812+
turnId,
8813+
goal: codexGoal(status, 10),
8814+
});
8815+
const resumeRequests = (goalBefore: string, goalAfter: string) => [
8816+
...request(
8817+
3,
8818+
"thread/goal/get",
8819+
{ threadId: nativeThreadId },
8820+
{ goal: codexGoal(goalBefore, 10) },
8821+
),
8822+
...request(
8823+
4,
8824+
"thread/goal/set",
8825+
{ threadId: nativeThreadId, status: "active" },
8826+
{ goal: codexGoal(goalAfter, 10) },
8827+
),
8828+
];
8829+
const runningTurns = (events: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>) =>
8830+
events.flatMap((event) =>
8831+
event.type === "provider_turn.updated" && event.providerTurn.status === "running"
8832+
? [[event.providerTurn.nativeTurnRef?.nativeId ?? null, event.providerTurn.ordinal]]
8833+
: [],
8834+
);
8835+
8836+
const runResume = (scenario: string, entries: Array<CodexReplay.CodexAppServerReplayEntry>) =>
8837+
Effect.gen(function* () {
8838+
const requests: Array<string> = [];
8839+
const harness = yield* makeCodexReplayHarness(
8840+
makeCodexReplayTranscript({ scenario, entries: [...sessionStart, ...entries] }),
8841+
() => Effect.void,
8842+
(method) =>
8843+
Effect.sync(() => {
8844+
requests.push(method);
8845+
}),
8846+
);
8847+
yield* harness.runtime.startTurn(yield* goalTurnInput(harness, "/goal resume"));
8848+
yield* harness.firstTerminal;
8849+
return { harness, requests };
8850+
});
8851+
const provideServices = Effect.provide(
8852+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
8853+
);
8854+
8855+
it.effect("attaches the run to the turn Codex already started", () =>
8856+
Effect.gen(function* () {
8857+
const { harness, requests } = yield* runResume("goal-resume-started-turn", [
8858+
turnStarted("resumed-turn"),
8859+
...resumeRequests("blocked", "active"),
8860+
goalUpdated("active", "resumed-turn"),
8861+
turnCompleted("resumed-turn"),
8862+
goalUpdated("complete", null),
8863+
]);
8864+
8865+
assert.notInclude(requests, "turn/start");
8866+
assert.deepEqual(runningTurns(harness.events), [["resumed-turn", 1]]);
8867+
const providerTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({
8868+
driver: CodexAdapterV2.CODEX_DRIVER_KIND,
8869+
nativeTurnId: "resumed-turn",
8870+
});
8871+
assert.deepEqual(
8872+
harness.terminalEvents().map((event) => [event.providerTurnId, event.status]),
8873+
[[providerTurnId, "completed"]],
8874+
);
8875+
assert.include(providerGoals(harness.events), "active");
8876+
}).pipe(Effect.scoped, provideServices),
8877+
);
8878+
8879+
it.effect("holds the run for the turn Codex starts once the goal is active", () =>
8880+
Effect.gen(function* () {
8881+
const { harness, requests } = yield* runResume("goal-resume-continuation", [
8882+
// A turn that already finished has nothing left to attach to.
8883+
turnStarted("finished-turn"),
8884+
turnCompleted("finished-turn"),
8885+
...resumeRequests("paused", "active"),
8886+
turnStarted("continuation"),
8887+
goalUpdated("active", "continuation"),
8888+
turnCompleted("continuation"),
8889+
goalUpdated("complete", null),
8890+
]);
8891+
8892+
assert.notInclude(requests, "turn/start");
8893+
assert.deepEqual(runningTurns(harness.events), [
8894+
[null, 1],
8895+
["continuation", 2],
8896+
]);
8897+
const providerTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({
8898+
driver: CodexAdapterV2.CODEX_DRIVER_KIND,
8899+
nativeTurnId: "continuation",
8900+
});
8901+
assert.deepEqual(
8902+
harness.terminalEvents().map((event) => [event.providerTurnId, event.status]),
8903+
[[providerTurnId, "completed"]],
8904+
);
8905+
}).pipe(Effect.scoped, provideServices),
8906+
);
8907+
8908+
it.effect("settles the run when Codex leaves the goal inactive", () =>
8909+
Effect.gen(function* () {
8910+
const { harness, requests } = yield* runResume("goal-resume-inactive", [
8911+
...resumeRequests("budgetLimited", "budgetLimited"),
8912+
]);
8913+
8914+
assert.notInclude(requests, "turn/start");
8915+
assert.deepEqual(
8916+
harness.terminalEvents().map((event) => event.status),
8917+
["completed"],
8918+
);
8919+
assert.deepEqual(runningTurns(harness.events), [[null, 1]]);
8920+
}).pipe(Effect.scoped, provideServices),
8921+
);
8922+
});
8923+
87988924
it.effect("settles a run held for the next goal turn when Stop arrives in between", () =>
87998925
Effect.gen(function* () {
88008926
const prompt = "Keep going";

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

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
19041904
const goalHolds = new Map<string, CodexGoalHold>();
19051905
const goalRuns = new Map<ProviderTurnId, Array<ActiveCodexTurnContext>>();
19061906
const goalActivations = new Map<string, { stopped: boolean }>();
1907+
// Codex can start a turn on its own while no run exists, such as when it
1908+
// resumes a goal with the thread. `/goal resume` adopts it.
1909+
const unownedRootTurns = new Map<
1910+
string,
1911+
{ readonly nativeTurnId: string; readonly startedAt: DateTime.Utc }
1912+
>();
19071913
// Stops in progress by native thread. Until a Stop resolves its target,
19081914
// a held run must not settle as completed.
19091915
const goalStops = new Map<string, number>();
@@ -4466,10 +4472,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
44664472
);
44674473
return;
44684474
}
4475+
const startedAt = codexTimestamp(payload.turn.startedAt);
4476+
unownedRootTurns.set(payload.threadId, { nativeTurnId: payload.turn.id, startedAt });
44694477
yield* rememberSubagentTurnStarted({
44704478
nativeThreadId: payload.threadId,
44714479
nativeTurnId: payload.turn.id,
4472-
startedAt: codexTimestamp(payload.turn.startedAt),
4480+
startedAt,
44734481
});
44744482
}).pipe(Effect.orDie, turnTerminalizationPermit.withPermits(1)),
44754483
);
@@ -5982,6 +5990,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59825990

59835991
yield* client.handleServerNotification("turn/completed", (payload) =>
59845992
Effect.gen(function* () {
5993+
if (unownedRootTurns.get(payload.threadId)?.nativeTurnId === payload.turn.id) {
5994+
unownedRootTurns.delete(payload.threadId);
5995+
}
59855996
const context = (yield* Ref.get(activeTurns)).get(payload.turn.id);
59865997
if (context === undefined) {
59875998
return;
@@ -6011,9 +6022,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
60116022
}),
60126023
);
60136024

6025+
/**
6026+
* With `holdForGoalTurn`, call with the terminalization permit: the run
6027+
* stays open for the turn Codex starts once the goal is active.
6028+
*/
60146029
const completeGoalCommandTurn = Effect.fnUntraced(function* (
60156030
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
60166031
reply: string,
6032+
holdForGoalTurn = false,
60176033
) {
60186034
const now = yield* DateTime.now;
60196035
const nativeId = `goal-command:${turnInput.attemptId}`;
@@ -6076,11 +6092,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
60766092
driver: CODEX_PROVIDER,
60776093
turnItem: artifacts.turnItem,
60786094
});
6095+
const completedTurn = { ...providerTurn, status: "completed", completedAt: now } as const;
6096+
if (holdForGoalTurn) {
6097+
return yield* emitOrDeferRootTerminal({
6098+
context,
6099+
nativeTurnId: nativeId,
6100+
status: "completed",
6101+
goalHoldTurn: completedTurn,
6102+
});
6103+
}
60796104
yield* emitProviderEvent({
60806105
type: "provider_turn.updated",
60816106
driver: CODEX_PROVIDER,
60826107
threadId: turnInput.threadId,
6083-
providerTurn: { ...providerTurn, status: "completed", completedAt: now },
6108+
providerTurn: completedTurn,
60846109
});
60856110
yield* emitRootTerminal(context, {
60866111
type: "turn.terminal",
@@ -6228,9 +6253,27 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
62286253
);
62296254

62306255
/**
6231-
* Setting or resuming a goal starts the first goal turn with this run's
6232-
* turn configuration (approvals, sandbox, model, plan mode), since a
6233-
* turn Codex starts on its own reuses the last turn's settings.
6256+
* Codex rejects a turn/start with no input, so resuming starts no turn of
6257+
* its own: the run takes the turn Codex already started, or else waits
6258+
* for the one it starts once the goal is active.
6259+
*/
6260+
const claimGoalTurn = (turnInput: ProviderAdapter.ProviderAdapterV2TurnInput) =>
6261+
turnTerminalizationPermit.withPermits(1)(
6262+
Effect.gen(function* () {
6263+
const threadId = yield* getNativeThreadId(turnInput.providerThread);
6264+
const started = unownedRootTurns.get(threadId);
6265+
if (started === undefined) {
6266+
return yield* completeGoalCommandTurn(turnInput, "Goal resumed.", true);
6267+
}
6268+
unownedRootTurns.delete(threadId);
6269+
yield* registerRootTurn({ turnInput, ...started });
6270+
}),
6271+
);
6272+
6273+
/**
6274+
* Setting a goal starts its first turn with this run's turn
6275+
* configuration (approvals, sandbox, model, plan mode), since a turn
6276+
* Codex starts on its own reuses the last turn's settings.
62346277
*/
62356278
const runGoalCommand = (
62366279
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
@@ -6291,15 +6334,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
62916334
const activation = { stopped: false };
62926335
goalActivations.set(threadId, activation);
62936336
yield* Effect.gen(function* () {
6294-
yield* startNativeTurn(
6295-
turnInput,
6296-
command.type === "set"
6297-
? yield* toCodexInput({
6298-
...turnInput,
6299-
message: { ...turnInput.message, text: command.objective },
6300-
})
6301-
: [],
6302-
);
6337+
if (command.type === "set") {
6338+
yield* startNativeTurn(
6339+
turnInput,
6340+
yield* toCodexInput({
6341+
...turnInput,
6342+
message: { ...turnInput.message, text: command.objective },
6343+
}),
6344+
);
6345+
} else {
6346+
yield* claimGoalTurn(turnInput);
6347+
}
63036348
if (activation.stopped) return;
63046349
const { goal } = yield* client.request("thread/goal/set", {
63056350
threadId,

0 commit comments

Comments
 (0)