Skip to content

Commit 6e3ada1

Browse files
fix(server): wake Codex callers when restart cancels commands (pingdotgg#24538)
1 parent 8851e30 commit 6e3ada1

5 files changed

Lines changed: 60 additions & 0 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4772,6 +4772,52 @@ describe("CodexAdapterV2 post-settle continuation", () => {
47724772
],
47734773
});
47744774

4775+
it.effect("marks a root command it wakes the agent for once the command finishes", () =>
4776+
Effect.gen(function* () {
4777+
const harness = yield* makeCodexReplayHarness({
4778+
...backgroundExecTranscript,
4779+
entries: backgroundExecTranscript.entries.map((entry) =>
4780+
entry.type === "emit_inbound" && entry.label === "item/completed/command-late"
4781+
? { ...entry, afterMs: 0 }
4782+
: entry,
4783+
),
4784+
});
4785+
yield* harness.runtime.startTurn(
4786+
makeCodexTestTurnInput({
4787+
threadId: harness.threadId,
4788+
providerThread: harness.providerThread,
4789+
now: yield* DateTime.now,
4790+
attemptId: RunAttemptId.make("attempt-command-completion-wake"),
4791+
text: BG_PROMPT,
4792+
}),
4793+
);
4794+
const commands = () =>
4795+
harness.events.flatMap((event) =>
4796+
event.type === "turn_item.updated" && event.turnItem.type === "command_execution"
4797+
? [[event.turnItem.status, event.turnItem.completionWake]]
4798+
: [],
4799+
);
4800+
yield* harness.until(
4801+
() => harness.continuationRequests.length === 1 && commands().length === 2,
4802+
);
4803+
// Restart recovery reads this to keep the wake the restart cut.
4804+
assert.deepEqual(commands(), [
4805+
["running", "always"],
4806+
["completed", "always"],
4807+
]);
4808+
}).pipe(
4809+
Effect.scoped,
4810+
Effect.provide(
4811+
Layer.mergeAll(
4812+
IdAllocator.layer,
4813+
McpProviderSessions.layer,
4814+
NodeServices.layer,
4815+
NodeCrypto.layer,
4816+
),
4817+
),
4818+
),
4819+
);
4820+
47754821
it.effect("wakes with well-formed text when the cuts split an emoji", () =>
47764822
Effect.scoped(
47774823
Effect.gen(function* () {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,6 +3789,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
37893789
...(item.exitCode === null || item.exitCode === undefined
37903790
? {}
37913791
: { exitCode: item.exitCode }),
3792+
// Matches the continuation offered when it completes after the turn.
3793+
...(context.subagent === null && continuationRequests !== undefined
3794+
? { completionWake: "always" as const }
3795+
: {}),
37923796
};
37933797
return { node, turnItem };
37943798
});

‎apps/server/src/orchestration-v2/RestartBackgroundNote.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ it("names a lost subagent by the id the agent resumes it with", () => {
128128
assert.isTrue(
129129
isAwaitedWork(cancelledRosterTaskWork({ kind: "monitor", taskId: "ci", awaited: true })),
130130
);
131+
const codexCommand = { id: "item-2", type: "command_execution", input: "sleep 180" };
132+
assert.isFalse(isAwaitedWork(cancelledTurnItemWork(codexCommand as never)!));
133+
assert.isTrue(
134+
isAwaitedWork(cancelledTurnItemWork({ ...codexCommand, completionWake: "always" } as never)!),
135+
);
131136
assert.equal(
132137
cancelledTurnItemWork({
133138
id: "item-1",

‎apps/server/src/orchestration-v2/RestartBackgroundNote.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function cancelledTurnItemWork(
5656
kind: item.monitor === true ? "monitor" : "shell",
5757
label: compactLabel(item.input) ?? compactLabel(item.title) ?? "background command",
5858
id: item.id,
59+
...(item.completionWake === "always" ? { awaited: true as const } : {}),
5960
};
6061
case "dynamic_tool": {
6162
const monitor =

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,8 @@ export const OrchestrationV2TurnItem = Schema.Union([
17221722
exitCode: Schema.optional(Schema.Int),
17231723
/** Set when the command is a monitor, so background work lists it as a monitor. */
17241724
monitor: Schema.optional(Schema.Boolean),
1725+
/** The provider wakes the agent when this command finishes after its turn. */
1726+
completionWake: Schema.optional(Schema.Literal("always")),
17251727
}),
17261728
Schema.Struct({
17271729
...OrchestrationV2TurnItemBaseFields,
@@ -2566,6 +2568,8 @@ export const OrchestrationV2TurnItemJson = Schema.Union([
25662568
exitCode: Schema.optional(Schema.Int),
25672569
/** Set when the command is a monitor, so background work lists it as a monitor. */
25682570
monitor: Schema.optional(Schema.Boolean),
2571+
/** The provider wakes the agent when this command finishes after its turn. */
2572+
completionWake: Schema.optional(Schema.Literal("always")),
25692573
}),
25702574
Schema.Struct({
25712575
...OrchestrationV2TurnItemJsonBaseFields,

0 commit comments

Comments
 (0)