Skip to content

Commit 7c1d5dc

Browse files
authored
fix(inference): preserve run ownership and completion delivery (#117731)
1 parent 16de0c0 commit 7c1d5dc

15 files changed

Lines changed: 549 additions & 244 deletions

‎extensions/codex/src/app-server/run-attempt-notification-controller.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,22 @@ export function createCodexAttemptNotificationController(
216216
};
217217
const waitForActiveNativeTurnCompletion = async () => {
218218
const route = resourceState.turnRoute;
219-
if (!route) {
219+
const activeNativeTurnId =
220+
resourceState.thread.lifecycle.activeTurnIds?.at(-1) ?? route?.observedNativeTurnId;
221+
if (!route || !activeNativeTurnId) {
220222
return false;
221223
}
222-
return await route.waitForTurnCompletion({
224+
const watch = resourceState.turnRouter.watchNativeTurnCompletion({
225+
threadId: route.threadId,
226+
turnId: activeNativeTurnId,
223227
timeoutMs: Math.min(appServer.requestTimeoutMs, CODEX_APP_SERVER_NATIVE_TURN_WAIT_TIMEOUT_MS),
224228
signal: runAbortController.signal,
225229
});
230+
try {
231+
return await watch.completion;
232+
} finally {
233+
watch.cancel();
234+
}
226235
};
227236
const noteNotificationReceived = (
228237
notification: CodexServerNotification,

‎extensions/codex/src/app-server/run-attempt.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3996,7 +3996,7 @@ describe("runCodexAppServerAttempt", () => {
39963996
]);
39973997
});
39983998

3999-
it("waits for an already-active native turn before starting a resumed thread turn", async () => {
3999+
it("waits for the exact active native turn before starting a resumed thread turn", async () => {
40004000
const { sessionFile, workspaceDir } = createRunPaths();
40014001
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
40024002
const harness = createAppServerHarness(async (method) => {
@@ -4022,6 +4022,24 @@ describe("runCodexAppServerAttempt", () => {
40224022
setTimeout(resolve, 20);
40234023
});
40244024
expect(harness.requests.map((request) => request.method)).not.toContain("turn/start");
4025+
await harness.notify({
4026+
method: "turn/completed",
4027+
params: {
4028+
threadId: "thread-existing",
4029+
turn: { id: "stale-turn", status: "completed" },
4030+
},
4031+
});
4032+
expect(harness.requests.map((request) => request.method)).not.toContain("turn/start");
4033+
await harness.notify({
4034+
method: "error",
4035+
params: {
4036+
threadId: "thread-existing",
4037+
turnId: "compact-turn",
4038+
error: { message: "native turn has not completed" },
4039+
willRetry: false,
4040+
},
4041+
});
4042+
expect(harness.requests.map((request) => request.method)).not.toContain("turn/start");
40254043
await harness.notify({
40264044
method: "turn/completed",
40274045
params: {

‎extensions/codex/src/app-server/turn-router.test.ts‎

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -650,53 +650,117 @@ describe("CodexAppServerTurnRouter", () => {
650650
route.release();
651651
});
652652

653-
it("consumes one native completion and clears stale completion when arming", async () => {
653+
it("consumes buffered exact native completions and clears them when arming", async () => {
654654
const harness = createHarness();
655-
const route = getCodexAppServerTurnRouter(harness.client).reserveThread({
655+
const router = getCodexAppServerTurnRouter(harness.client);
656+
const route = router.reserveThread({
656657
threadId: "thread-native",
657-
onNotification: vi.fn(),
658658
});
659659
harness.send({
660660
method: "turn/completed",
661661
params: { threadId: "thread-native", turn: { id: "turn-native", items: [] } },
662662
});
663663
await settleInput();
664-
665-
await expect(route.waitForTurnCompletion({ timeoutMs: 10 })).resolves.toBe(true);
666-
await expect(route.waitForTurnCompletion({ timeoutMs: 1 })).resolves.toBe(false);
664+
await route.activate({ onNotification: vi.fn() });
665+
expect(route.observedNativeTurnId).toBe("turn-native");
666+
const completion = (turnId: string, timeoutMs: number) =>
667+
router.watchNativeTurnCompletion({ threadId: route.threadId, turnId, timeoutMs }).completion;
668+
await expect(completion("turn-native", 10)).resolves.toBe(true);
669+
await expect(completion("turn-native", 1)).resolves.toBe(false);
667670

668671
harness.send({
669672
method: "turn/completed",
670673
params: { threadId: "thread-native", turn: { id: "turn-stale", items: [] } },
671674
});
672675
await settleInput();
673676
route.armTurn();
674-
await expect(route.waitForTurnCompletion({ timeoutMs: 1 })).resolves.toBe(false);
677+
expect(route.observedNativeTurnId).toBeUndefined();
678+
await expect(completion("turn-stale", 1)).resolves.toBe(false);
675679
await route.cancelTurn();
676680
});
677681

678-
it("settles an active native-completion waiter on completion, abort, and release", async () => {
682+
it("keeps an observed active native turn exact across arm and stale completion", async () => {
679683
const harness = createHarness();
680-
const route = getCodexAppServerTurnRouter(harness.client).reserveThread({
684+
const router = getCodexAppServerTurnRouter(harness.client);
685+
const route = router.reserveThread({
686+
threadId: "thread-native-active",
687+
onNotification: vi.fn(),
688+
});
689+
harness.send({
690+
method: "turn/started",
691+
params: {
692+
threadId: "thread-native-active",
693+
turn: { id: "turn-compact", status: "inProgress" },
694+
},
695+
});
696+
await settleInput();
697+
698+
route.armTurn();
699+
expect(route.observedNativeTurnId).toBe("turn-compact");
700+
harness.send({
701+
method: "turn/completed",
702+
params: { threadId: "thread-native-active", turn: { id: "turn-stale", items: [] } },
703+
});
704+
await settleInput();
705+
expect(route.observedNativeTurnId).toBe("turn-compact");
706+
707+
const completed = router.watchNativeTurnCompletion({
708+
threadId: "thread-native-active",
709+
turnId: "turn-compact",
710+
timeoutMs: 100,
711+
});
712+
harness.send({
713+
method: "turn/completed",
714+
params: { threadId: "thread-native-active", turn: { id: "turn-compact", items: [] } },
715+
});
716+
await expect(completed.completion).resolves.toBe(true);
717+
await route.cancelTurn();
718+
});
719+
720+
it("settles exact native-completion watchers on completion, abort, and route release", async () => {
721+
const harness = createHarness();
722+
const router = getCodexAppServerTurnRouter(harness.client);
723+
const route = router.reserveThread({
681724
threadId: "thread-native-wait",
682725
onNotification: vi.fn(),
683726
});
684727

685-
const completed = route.waitForTurnCompletion({ timeoutMs: 100 });
728+
const completed = router.watchNativeTurnCompletion({
729+
threadId: "thread-native-wait",
730+
turnId: "turn-native",
731+
timeoutMs: 100,
732+
});
686733
harness.send({
687734
method: "turn/completed",
688735
params: { threadId: "thread-native-wait", turn: { id: "turn-native", items: [] } },
689736
});
690-
await expect(completed).resolves.toBe(true);
737+
await expect(completed.completion).resolves.toBe(true);
691738

692739
const controller = new AbortController();
693-
const aborted = route.waitForTurnCompletion({ timeoutMs: 100, signal: controller.signal });
740+
const aborted = router.watchNativeTurnCompletion({
741+
threadId: "thread-native-wait",
742+
turnId: "turn-aborted",
743+
timeoutMs: 100,
744+
signal: controller.signal,
745+
});
694746
controller.abort("test");
695-
await expect(aborted).resolves.toBe(false);
747+
await expect(aborted.completion).resolves.toBe(false);
748+
const alreadyAborted = router.watchNativeTurnCompletion({
749+
threadId: "thread-native-wait",
750+
turnId: "turn-aborted",
751+
timeoutMs: 100,
752+
signal: controller.signal,
753+
});
754+
await expect(alreadyAborted.completion).resolves.toBe(false);
696755

697-
const released = route.waitForTurnCompletion({ timeoutMs: 100 });
756+
const released = router.watchNativeTurnCompletion({
757+
threadId: "thread-native-wait",
758+
turnId: "turn-released",
759+
timeoutMs: 100,
760+
signal: route.signal,
761+
});
698762
route.release();
699-
await expect(released).resolves.toBe(false);
763+
await expect(released.completion).resolves.toBe(false);
700764
});
701765

702766
it("watches one exact native turn without reserving its thread", async () => {

0 commit comments

Comments
 (0)