Skip to content

Commit e7d7f7f

Browse files
committed
fix(server): consume capacity continuation when its turn registers
1 parent fa7f4a5 commit e7d7f7f

2 files changed

Lines changed: 121 additions & 84 deletions

File tree

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

Lines changed: 118 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
22232223
"stop-starting",
22242224
"started-first",
22252225
"completed-first",
2226+
"new-run",
22262227
"steer",
22272228
"start-fails",
22282229
] as const)("handles terminal Codex capacity failures when retries %s", (outcome) =>
@@ -2283,7 +2284,8 @@ describe("CodexAdapterV2 post-settle continuation", () => {
22832284
if (
22842285
outcome === "started-first" ||
22852286
outcome === "stop-starting" ||
2286-
outcome === "completed-first"
2287+
outcome === "completed-first" ||
2288+
outcome === "new-run"
22872289
) {
22882290
start.splice(1, 0, ...start.splice(2, 1));
22892291
}
@@ -2308,7 +2310,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
23082310
},
23092311
});
23102312
} else if (entry.type === "emit_inbound" && entry.label === "turn/start") {
2311-
if (outcome === "completed-first")
2313+
if (outcome === "completed-first" || outcome === "new-run")
23122314
entries.push({
23132315
type: "emit_inbound",
23142316
frame: {
@@ -2319,6 +2321,18 @@ describe("CodexAdapterV2 post-settle continuation", () => {
23192321
},
23202322
},
23212323
});
2324+
if (outcome === "new-run") {
2325+
entries.push(
2326+
...codexReplayPreamble({
2327+
nativeThreadId,
2328+
nativeTurnId: nativeTurnId(2),
2329+
prompt: "Next task.",
2330+
})
2331+
.slice(-3)
2332+
.map((next, index) => (index < 2 ? withReplayRequestId(next, 5) : next)),
2333+
...failureEntries(2),
2334+
);
2335+
}
23222336
entries.push(
23232337
outcome === "start-fails"
23242338
? {
@@ -2491,6 +2505,19 @@ describe("CodexAdapterV2 post-settle continuation", () => {
24912505
}
24922506
}
24932507
yield* harness.awaitTerminal(1);
2508+
if (outcome === "new-run") {
2509+
assert.equal((yield* Queue.take(milestones)).type, "turn.terminal");
2510+
yield* harness.runtime.startTurn({
2511+
...turnInput,
2512+
attemptId: RunAttemptId.make("capacity-next-task"),
2513+
message: { ...turnInput.message, text: "Next task." },
2514+
});
2515+
const nextRetry = yield* Queue.take(milestones);
2516+
assert.equal(nextRetry.type, "turn_item.updated");
2517+
if (nextRetry.type !== "turn_item.updated" || nextRetry.turnItem.type !== "error") return;
2518+
assert.equal(nextRetry.turnItem.retry?.attempt, 1);
2519+
return;
2520+
}
24942521
const terminal = harness.terminalEvents()[0];
24952522
assert.equal(
24962523
terminal?.status,
@@ -5410,101 +5437,105 @@ describe("CodexAdapterV2 post-settle continuation", () => {
54105437
};
54115438
};
54125439

5413-
it.effect.each(["completed", "failed", "interrupted", "starting"] as const)(
5440+
it.effect.each(["completed", "failed", "interrupted", "starting", "replied-last"] as const)(
54145441
"handles a late native child after the capacity retry %s",
54155442
(parentStatus) =>
54165443
Effect.scoped(
54175444
Effect.gen(function* () {
54185445
const retriedTurnId = "native-capacity-recovered-parent";
54195446
const retrying = yield* Deferred.make<void>();
5420-
const harness = yield* makeCodexReplayHarness(
5421-
makeCodexReplayTranscript({
5422-
scenario: "capacity-late-child",
5423-
entries: [
5424-
...lateChildPrefix().spawned,
5425-
{
5426-
type: "emit_inbound",
5427-
frame: {
5428-
method: "turn/completed",
5429-
params: {
5430-
threadId: RESUME_NATIVE_THREAD,
5431-
turn: {
5432-
...makeCodexReplayTurn({ id: RESUME_NATIVE_TURN, status: "failed" }),
5433-
error: {
5434-
message: "Selected model is at capacity.",
5435-
codexErrorInfo: "serverOverloaded",
5436-
},
5437-
},
5447+
const entries: Array<CodexReplay.CodexAppServerReplayEntry> = [
5448+
...lateChildPrefix().spawned,
5449+
{
5450+
type: "emit_inbound",
5451+
frame: {
5452+
method: "turn/completed",
5453+
params: {
5454+
threadId: RESUME_NATIVE_THREAD,
5455+
turn: {
5456+
...makeCodexReplayTurn({ id: RESUME_NATIVE_TURN, status: "failed" }),
5457+
error: {
5458+
message: "Selected model is at capacity.",
5459+
codexErrorInfo: "serverOverloaded",
54385460
},
54395461
},
54405462
},
5441-
{
5442-
type: "expect_outbound",
5443-
frame: {
5444-
id: 4,
5445-
method: "turn/start",
5446-
params: {
5447-
threadId: RESUME_NATIVE_THREAD,
5448-
input: [],
5449-
cwd: "/workspace",
5450-
model: "gpt-5.4",
5451-
approvalPolicy: "never",
5452-
approvalsReviewer: "user",
5453-
sandboxPolicy: { type: "dangerFullAccess" },
5454-
summary: "detailed",
5455-
collaborationMode: { mode: "default", settings: { model: "gpt-5.4" } },
5456-
},
5457-
},
5463+
},
5464+
},
5465+
{
5466+
type: "expect_outbound",
5467+
frame: {
5468+
id: 4,
5469+
method: "turn/start",
5470+
params: {
5471+
threadId: RESUME_NATIVE_THREAD,
5472+
input: [],
5473+
cwd: "/workspace",
5474+
model: "gpt-5.4",
5475+
approvalPolicy: "never",
5476+
approvalsReviewer: "user",
5477+
sandboxPolicy: { type: "dangerFullAccess" },
5478+
summary: "detailed",
5479+
collaborationMode: { mode: "default", settings: { model: "gpt-5.4" } },
54585480
},
5459-
...(parentStatus === "starting" ? [childTurnCompleted(RESUME_CHILD_TURN_1)] : []),
5460-
{
5461-
type: "emit_inbound",
5462-
frame: {
5463-
id: 4,
5464-
result: {
5465-
turn: makeCodexReplayTurn({ id: retriedTurnId, status: "inProgress" }),
5466-
},
5467-
},
5481+
},
5482+
},
5483+
...(parentStatus === "starting" ? [childTurnCompleted(RESUME_CHILD_TURN_1)] : []),
5484+
{
5485+
type: "emit_inbound",
5486+
frame: {
5487+
id: 4,
5488+
result: {
5489+
turn: makeCodexReplayTurn({ id: retriedTurnId, status: "inProgress" }),
54685490
},
5469-
{
5470-
type: "emit_inbound",
5471-
frame: {
5472-
method: "turn/started",
5473-
params: {
5474-
threadId: RESUME_NATIVE_THREAD,
5475-
turn: makeCodexReplayTurn({ id: retriedTurnId, status: "inProgress" }),
5476-
},
5477-
},
5491+
},
5492+
},
5493+
{
5494+
type: "emit_inbound",
5495+
frame: {
5496+
method: "turn/started",
5497+
params: {
5498+
threadId: RESUME_NATIVE_THREAD,
5499+
turn: makeCodexReplayTurn({ id: retriedTurnId, status: "inProgress" }),
54785500
},
5479-
{
5480-
type: "emit_inbound",
5481-
frame: {
5482-
method: "turn/completed",
5483-
params: {
5484-
threadId: RESUME_NATIVE_THREAD,
5485-
turn: {
5486-
...makeCodexReplayTurn({
5487-
id: retriedTurnId,
5488-
status: parentStatus === "starting" ? "completed" : parentStatus,
5489-
}),
5490-
error:
5491-
parentStatus === "failed"
5492-
? { message: "Recovery failed.", codexErrorInfo: "internalServerError" }
5493-
: null,
5494-
},
5495-
},
5501+
},
5502+
},
5503+
{
5504+
type: "emit_inbound",
5505+
frame: {
5506+
method: "turn/completed",
5507+
params: {
5508+
threadId: RESUME_NATIVE_THREAD,
5509+
turn: {
5510+
...makeCodexReplayTurn({
5511+
id: retriedTurnId,
5512+
status:
5513+
parentStatus === "starting" || parentStatus === "replied-last"
5514+
? "completed"
5515+
: parentStatus,
5516+
}),
5517+
error:
5518+
parentStatus === "failed"
5519+
? { message: "Recovery failed.", codexErrorInfo: "internalServerError" }
5520+
: null,
54965521
},
54975522
},
5498-
childAgentMessage({
5499-
id: "child-after-capacity",
5500-
text: "CAPACITY_CHILD_DONE",
5501-
turnId: RESUME_CHILD_TURN_1,
5502-
completedAtMs: 1782622480000,
5503-
afterMs: 60_000,
5504-
}),
5505-
childTurnCompleted(RESUME_CHILD_TURN_1),
5506-
],
5523+
},
5524+
},
5525+
childAgentMessage({
5526+
id: "child-after-capacity",
5527+
text: "CAPACITY_CHILD_DONE",
5528+
turnId: RESUME_CHILD_TURN_1,
5529+
completedAtMs: 1782622480000,
5530+
afterMs: 60_000,
55075531
}),
5532+
childTurnCompleted(RESUME_CHILD_TURN_1),
5533+
];
5534+
if (parentStatus === "replied-last") {
5535+
entries.push(...entries.splice(lateChildPrefix().spawned.length + 2, 1));
5536+
}
5537+
const harness = yield* makeCodexReplayHarness(
5538+
makeCodexReplayTranscript({ scenario: "capacity-late-child", entries }),
55085539
(event) =>
55095540
event.type === "turn_item.updated" &&
55105541
event.turnItem.type === "error" &&
@@ -5534,8 +5565,11 @@ describe("CodexAdapterV2 post-settle continuation", () => {
55345565
() => harness.subagentUpdates().at(-1)?.subagent.status === "completed",
55355566
);
55365567
yield* harness.awaitTerminal(1);
5537-
assert.lengthOf(harness.continuationRequests, parentStatus === "completed" ? 1 : 0);
5538-
if (parentStatus === "completed")
5568+
assert.lengthOf(
5569+
harness.continuationRequests,
5570+
parentStatus === "completed" || parentStatus === "replied-last" ? 1 : 0,
5571+
);
5572+
if (parentStatus === "completed" || parentStatus === "replied-last")
55395573
assert.include(harness.continuationRequests[0]?.detail, "CAPACITY_CHILD_DONE");
55405574
}).pipe(
55415575
Effect.provide(

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
19911991
updated.set(input.nativeTurnId, context);
19921992
return updated;
19931993
});
1994+
if (continuation !== undefined && nativeThreadId != null) {
1995+
capacityContinuations.delete(nativeThreadId);
1996+
}
19941997
yield* emitProviderEvent({
19951998
type: "provider_turn.updated",
19961999
driver: CODEX_PROVIDER,

0 commit comments

Comments
 (0)