Skip to content

Commit 7e7c5d4

Browse files
authored
fix(codex): let collector waits finish their full timeout (#130939)
* fix(codex): let collector waits finish their full timeout * test(codex): consolidate timeout argument cases
1 parent 68fcc9b commit 7e7c5d4

2 files changed

Lines changed: 136 additions & 130 deletions

File tree

‎extensions/codex/src/app-server/dynamic-tool-execution.test.ts‎

Lines changed: 133 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
toCodexDynamicToolProgressResponse,
1414
toCodexDynamicToolProtocolResponse,
1515
} from "./dynamic-tool-execution.js";
16-
import type { CodexDynamicToolCallResponse } from "./protocol.js";
16+
import type { CodexDynamicToolCallParams, CodexDynamicToolCallResponse } from "./protocol.js";
1717

1818
const CODEX_DYNAMIC_TOOL_TIMEOUT_MS = 90_000;
1919
const CODEX_DYNAMIC_TOOL_MAX_TIMEOUT_MS = 600_000;
@@ -27,104 +27,64 @@ describe("dynamic tool execution helpers", () => {
2727
vi.useRealTimers();
2828
});
2929

30-
it("keeps explicit dynamic tool timeouts above the default bridge deadline", () => {
31-
const timeoutMs = CODEX_DYNAMIC_TOOL_TIMEOUT_MS + 1_000;
32-
30+
it.each<{
31+
name: string;
32+
tool: string;
33+
arguments: CodexDynamicToolCallParams["arguments"];
34+
timeoutMs: number;
35+
}>([
36+
{
37+
name: "keeps explicit dynamic tool timeouts above the default bridge deadline",
38+
tool: "image_generate",
39+
arguments: { prompt: "cat", timeoutMs: CODEX_DYNAMIC_TOOL_TIMEOUT_MS + 1_000 },
40+
timeoutMs: CODEX_DYNAMIC_TOOL_TIMEOUT_MS + 1_000,
41+
},
42+
{
43+
name: "ignores partial dynamic tool timeout strings",
44+
tool: "session_status",
45+
arguments: { timeoutMs: "1abc" },
46+
timeoutMs: CODEX_DYNAMIC_TOOL_TIMEOUT_MS,
47+
},
48+
{
49+
name: "honors timeoutSeconds when timeoutMs is absent",
50+
tool: "session_status",
51+
arguments: { timeoutSeconds: 30 },
52+
timeoutMs: 60_000,
53+
},
54+
{
55+
name: "prefers timeoutMs over timeoutSeconds",
56+
tool: "session_status",
57+
arguments: { timeoutMs: 5_000, timeoutSeconds: 30 },
58+
timeoutMs: 5_000,
59+
},
60+
{
61+
name: "ignores non-positive timeoutSeconds",
62+
tool: "session_status",
63+
arguments: { timeoutSeconds: -1 },
64+
timeoutMs: CODEX_DYNAMIC_TOOL_TIMEOUT_MS,
65+
},
66+
{
67+
name: "rejects fractional timeoutSeconds and falls back to the default",
68+
tool: "session_status",
69+
arguments: { timeoutSeconds: 1.5 },
70+
timeoutMs: CODEX_DYNAMIC_TOOL_TIMEOUT_MS,
71+
},
72+
])("$name", ({ tool, arguments: args, timeoutMs }) => {
3373
expect(
3474
resolveDynamicToolCallTimeoutMs({
3575
call: {
3676
threadId: "thread-1",
3777
turnId: "turn-1",
38-
callId: "call-long",
78+
callId: "call-timeout",
3979
namespace: null,
40-
tool: "image_generate",
41-
arguments: { prompt: "cat", timeoutMs },
80+
tool,
81+
arguments: args,
4282
},
4383
config: undefined,
4484
}),
4585
).toBe(timeoutMs);
4686
});
4787

48-
it("ignores partial dynamic tool timeout strings", () => {
49-
expect(
50-
resolveDynamicToolCallTimeoutMs({
51-
call: {
52-
threadId: "thread-1",
53-
turnId: "turn-1",
54-
callId: "call-partial-timeout",
55-
namespace: null,
56-
tool: "session_status",
57-
arguments: { timeoutMs: "1abc" },
58-
},
59-
config: undefined,
60-
}),
61-
).toBe(CODEX_DYNAMIC_TOOL_TIMEOUT_MS);
62-
});
63-
64-
it("honors timeoutSeconds when timeoutMs is absent", () => {
65-
expect(
66-
resolveDynamicToolCallTimeoutMs({
67-
call: {
68-
threadId: "thread-1",
69-
turnId: "turn-1",
70-
callId: "call-seconds",
71-
namespace: null,
72-
tool: "session_status",
73-
arguments: { timeoutSeconds: 30 },
74-
},
75-
config: undefined,
76-
}),
77-
).toBe(60_000);
78-
});
79-
80-
it("prefers timeoutMs over timeoutSeconds", () => {
81-
expect(
82-
resolveDynamicToolCallTimeoutMs({
83-
call: {
84-
threadId: "thread-1",
85-
turnId: "turn-1",
86-
callId: "call-both",
87-
namespace: null,
88-
tool: "session_status",
89-
arguments: { timeoutMs: 5_000, timeoutSeconds: 30 },
90-
},
91-
config: undefined,
92-
}),
93-
).toBe(5_000);
94-
});
95-
96-
it("ignores non-positive timeoutSeconds", () => {
97-
expect(
98-
resolveDynamicToolCallTimeoutMs({
99-
call: {
100-
threadId: "thread-1",
101-
turnId: "turn-1",
102-
callId: "call-bad-seconds",
103-
namespace: null,
104-
tool: "session_status",
105-
arguments: { timeoutSeconds: -1 },
106-
},
107-
config: undefined,
108-
}),
109-
).toBe(CODEX_DYNAMIC_TOOL_TIMEOUT_MS);
110-
});
111-
112-
it("rejects fractional timeoutSeconds and falls back to the default", () => {
113-
expect(
114-
resolveDynamicToolCallTimeoutMs({
115-
call: {
116-
threadId: "thread-1",
117-
turnId: "turn-1",
118-
callId: "call-fractional-seconds",
119-
namespace: null,
120-
tool: "session_status",
121-
arguments: { timeoutSeconds: 1.5 },
122-
},
123-
config: undefined,
124-
}),
125-
).toBe(CODEX_DYNAMIC_TOOL_TIMEOUT_MS);
126-
});
127-
12888
it("uses configured image generation timeouts for Codex dynamic tool calls", () => {
12989
expect(
13090
resolveDynamicToolCallTimeoutMs({
@@ -438,6 +398,40 @@ describe("dynamic tool execution helpers", () => {
438398
});
439399
});
440400

401+
it.each([
402+
{ tool: "session_status", deadlineMs: 600_000 },
403+
{ tool: "agents_wait", deadlineMs: 630_000 },
404+
])("enforces the resolved $tool cap at $deadlineMs ms", async ({ tool, deadlineMs }) => {
405+
vi.useFakeTimers();
406+
const call = {
407+
threadId: "thread-1",
408+
turnId: "turn-1",
409+
callId: "call-capped-timeout",
410+
namespace: null,
411+
tool,
412+
arguments: { timeoutSeconds: 1_000 },
413+
};
414+
const onTimeout = vi.fn();
415+
const response = handleDynamicToolCallWithTimeout({
416+
call,
417+
toolBridge: { handleToolCall: vi.fn(() => new Promise<never>(() => {})) },
418+
signal: new AbortController().signal,
419+
timeoutMs: resolveDynamicToolCallTimeoutMs({ call, config: undefined }),
420+
onTimeout,
421+
});
422+
423+
await vi.advanceTimersByTimeAsync(deadlineMs - 1);
424+
expect(onTimeout).not.toHaveBeenCalled();
425+
await vi.advanceTimersByTimeAsync(1);
426+
427+
await expect(response).resolves.toMatchObject({
428+
success: false,
429+
diagnosticTerminalReason: "timed_out",
430+
});
431+
expect(onTimeout).toHaveBeenCalledOnce();
432+
expect(vi.getTimerCount()).toBe(0);
433+
});
434+
441435
it("marks a timeout during pre-execution hooks as unstarted", async () => {
442436
vi.useFakeTimers();
443437
const observeToolTerminal = vi.fn(() => ({
@@ -558,49 +552,61 @@ describe("dynamic tool execution helpers", () => {
558552
expect((await response).sideEffectEvidence).toBe(true);
559553
});
560554

561-
it("lets a structured sessions_send timeout win after setup work", async () => {
562-
vi.useFakeTimers();
563-
const call = {
564-
threadId: "thread-1",
565-
turnId: "turn-1",
566-
callId: "call-session-send-timeout",
567-
namespace: null,
568-
tool: "sessions_send",
569-
arguments: { sessionKey: "agent:child", message: "ping", timeoutSeconds: 1 },
570-
};
571-
const structuredTimeout: CodexDynamicToolCallResponse = {
572-
success: true,
573-
contentItems: [
574-
{
575-
type: "inputText" as const,
576-
text: JSON.stringify({
577-
runId: "run-child",
578-
status: "timeout",
579-
sentBeforeError: true,
580-
}),
555+
it.each([
556+
{ tool: "sessions_send", timeoutSeconds: 1, completionMs: 6_000 },
557+
{ tool: "agents_wait", timeoutSeconds: 600, completionMs: 600_000 },
558+
{ tool: "agents_wait", timeoutSeconds: 600, completionMs: 605_000 },
559+
])(
560+
"preserves the $tool result after $completionMs ms",
561+
async ({ tool, timeoutSeconds, completionMs }) => {
562+
vi.useFakeTimers();
563+
const call = {
564+
threadId: "thread-1",
565+
turnId: "turn-1",
566+
callId: "call-structured-timeout",
567+
namespace: null,
568+
tool,
569+
arguments: { timeoutSeconds },
570+
};
571+
const structuredTimeout: CodexDynamicToolCallResponse = {
572+
success: true,
573+
contentItems: [
574+
{
575+
type: "inputText" as const,
576+
text: JSON.stringify(
577+
tool === "agents_wait"
578+
? { completed: [], pending: ["run-child"] }
579+
: {
580+
runId: "run-child",
581+
status: "timeout",
582+
sentBeforeError: true,
583+
},
584+
),
585+
},
586+
],
587+
};
588+
const response = handleDynamicToolCallWithTimeout({
589+
call,
590+
toolBridge: {
591+
handleToolCall: vi.fn(
592+
() =>
593+
new Promise<CodexDynamicToolCallResponse>((resolve) => {
594+
// Inner tool deadlines can start after setup; the outer watchdog
595+
// must preserve their result through the completion grace period.
596+
setTimeout(() => resolve(structuredTimeout), completionMs);
597+
}),
598+
),
581599
},
582-
],
583-
};
584-
const response = handleDynamicToolCallWithTimeout({
585-
call,
586-
toolBridge: {
587-
handleToolCall: vi.fn(
588-
() =>
589-
new Promise<CodexDynamicToolCallResponse>((resolve) => {
590-
// sessions_send can spend time resolving/snapshotting the target
591-
// before its own timeoutSeconds wait starts.
592-
setTimeout(() => resolve(structuredTimeout), 6_000);
593-
}),
594-
),
595-
},
596-
signal: new AbortController().signal,
597-
timeoutMs: resolveDynamicToolCallTimeoutMs({ call, config: undefined }),
598-
});
600+
signal: new AbortController().signal,
601+
timeoutMs: resolveDynamicToolCallTimeoutMs({ call, config: undefined }),
602+
});
599603

600-
await vi.advanceTimersByTimeAsync(6_000);
604+
await vi.advanceTimersByTimeAsync(completionMs);
601605

602-
await expect(response).resolves.toEqual(structuredTimeout);
603-
});
606+
await expect(response).resolves.toEqual(structuredTimeout);
607+
expect(vi.getTimerCount()).toBe(0);
608+
},
609+
);
604610

605611
it("reports pre-execution cancellations to the private result observer", async () => {
606612
const controller = new AbortController();

‎extensions/codex/src/app-server/dynamic-tool-execution.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ function formatDynamicToolTimeoutDetails(params: {
141141
}
142142

143143
/**
144-
* Runs a dynamic tool call with run-abort and per-call timeout handling,
145-
* returning a Codex protocol response instead of throwing.
144+
* Runs a dynamic tool call with run-abort and the budget prepared by
145+
* resolveDynamicToolCallTimeoutMs, preserving tool-specific completion grace.
146146
*/
147147
export async function handleDynamicToolCallWithTimeout(params: {
148148
call: CodexDynamicToolCallParams;
@@ -255,7 +255,7 @@ export async function handleDynamicToolCallWithTimeout(params: {
255255
resolveAbort = resolve;
256256
});
257257
const timeoutPromise = new Promise<CodexDynamicToolRuntimeResponse>((resolve) => {
258-
const timeoutMs = clampDynamicToolTimeoutMs(params.timeoutMs);
258+
const { timeoutMs } = params;
259259
timeout = setTimeout(() => {
260260
timedOut = true;
261261
const timeoutDetails = formatDynamicToolTimeoutDetails({ call: params.call, timeoutMs });

0 commit comments

Comments
 (0)