Skip to content

Commit bd2c562

Browse files
committed
fix(tools): scope wait integer coercion
1 parent ed719b5 commit bd2c562

3 files changed

Lines changed: 98 additions & 8 deletions

File tree

‎src/bridge.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ export function bridgeToResponsesSSE(
627627
const argsStr = coerceIntegerToolArguments(
628628
currentToolCall.args || "{}",
629629
options?.toolParameterSchemas?.get(currentToolCall.name),
630+
currentToolCall.namespace === undefined ? currentToolCall.name : undefined,
630631
);
631632
// Finalize streamed function-call arguments so Codex commits the call (incl. MCP / computer_use).
632633
if (!currentToolCall.freeform && !currentToolCall.toolSearch) {
@@ -1659,6 +1660,7 @@ function buildResponseJSONWithBudget(
16591660
const coercedArgs = coerceIntegerToolArguments(
16601661
currentToolCallArgs,
16611662
options?.toolParameterSchemas?.get(currentToolCallName),
1663+
ns === undefined ? realName : undefined,
16621664
);
16631665
// Freeform tools serialize as custom_tool_call without extra_content; remember the
16641666
// signature server-side regardless so the replayed call can be re-signed (#1735).

‎src/lib/tool-argument-integers.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ function declaresString(schema: SchemaNode): boolean {
7777
// against it. It gets its own change when it gets its own reproduction.
7878
const U64_NUMBER_FIELDS = new Set(["timeout_ms"]);
7979

80+
// Issue #2443: Codex Desktop's bare `wait` tool has the same schema/runtime split
81+
// for `yield-time_ms` and `max_tokens`. Scope these names to that bare tool so a
82+
// third-party or namespaced tool can still use fractional values legitimately.
83+
const U64_NUMBER_FIELDS_BY_TOOL = new Map<string, ReadonlySet<string>>([
84+
["wait", new Set(["yield-time_ms", "max_tokens"])],
85+
]);
86+
8087
/** True when the node accepts a JSON number (`integer` or `number`), so a numeric
8188
* value is already schema-valid and must not be rewritten into a string. */
8289
function declaresNumeric(schema: SchemaNode): boolean {
@@ -141,6 +148,7 @@ function coerceValue(
141148
schema: SchemaNode | undefined,
142149
root: SchemaNode,
143150
depth: number,
151+
toolName?: string,
144152
propertyName?: string,
145153
): CoerceResult {
146154
// A hostile or deeply nested schema must not blow the stack.
@@ -153,8 +161,12 @@ function coerceValue(
153161
// #2316: a known Codex-native u64 field counts as integer-declared even when the
154162
// advertised schema says `number`, but only when the field really is numeric —
155163
// an allowlisted name over a string-typed field is a disagreement, not a repair.
156-
const nativeU64Declared = propertyName !== undefined
157-
&& U64_NUMBER_FIELDS.has(propertyName)
164+
const nativeU64Field = propertyName !== undefined
165+
&& (
166+
U64_NUMBER_FIELDS.has(propertyName)
167+
|| U64_NUMBER_FIELDS_BY_TOOL.get(toolName ?? "")?.has(propertyName) === true
168+
);
169+
const nativeU64Declared = nativeU64Field
158170
&& (declaresNumeric(resolved) || branches.some(declaresNumeric));
159171
const integerDeclared = declaresInteger(resolved)
160172
|| branches.some(declaresInteger)
@@ -182,7 +194,7 @@ function coerceValue(
182194
const next = value.map(entry => {
183195
// Array items have no property name of their own; passing the array's key would
184196
// let `timeout_ms: [1.5]` inherit the allowlist. Items are judged by schema only.
185-
const result = coerceValue(entry, itemSchema, root, depth + 1);
197+
const result = coerceValue(entry, itemSchema, root, depth + 1, toolName);
186198
if (result.changed) changed = true;
187199
return result.value;
188200
});
@@ -198,7 +210,7 @@ function coerceValue(
198210
const next: Record<string, unknown> = {};
199211
for (const [key, entry] of Object.entries(object)) {
200212
const childSchema = asSchema(properties?.[key]) ?? additional;
201-
const result = coerceValue(entry, childSchema, root, depth + 1, key);
213+
const result = coerceValue(entry, childSchema, root, depth + 1, toolName, key);
202214
if (result.changed) changed = true;
203215
next[key] = result.value;
204216
}
@@ -216,6 +228,7 @@ function coerceValue(
216228
export function coerceIntegerToolArguments(
217229
args: string,
218230
parameters: Record<string, unknown> | undefined,
231+
toolName?: string,
219232
): string {
220233
if (!parameters || !args) return args;
221234
// Cheap reject: a payload with no digit cannot need either repair (integral-float
@@ -230,7 +243,7 @@ export function coerceIntegerToolArguments(
230243
return args;
231244
}
232245
const root = parameters as SchemaNode;
233-
const result = coerceValue(parsed, root, root, 0);
246+
const result = coerceValue(parsed, root, root, 0, toolName);
234247
if (!result.changed) return args;
235248
return JSON.stringify(result.value);
236249
}

‎tests/tool-argument-integers.test.ts‎

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,15 @@ describe("native u64 fields advertised as number (#2316)", () => {
325325
.toBe('{"timeout_ms":"120000"}');
326326
});
327327

328-
test("a field NOT on the allowlist keeps its float even when integral", () => {
329-
// Deliberate scope proof: only names with a captured u64 rejection are repaired.
328+
test("Cursor's sibling field stays unchanged even with wait identity", () => {
329+
// Cursor uses yield_time_ms (underscore), not wait's yield-time_ms (hyphen).
330+
// Keep this explicit scope proof from #2316: field names are never broadened globally.
330331
const others = {
331332
type: "object",
332333
properties: { yield_time_ms: { type: "number" }, priority: { type: "number" } },
333334
};
334335
const raw = '{"yield_time_ms":60000.0,"priority":2.0}';
335-
expect(coerceIntegerToolArguments(raw, others)).toBe(raw);
336+
expect(coerceIntegerToolArguments(raw, others, "wait")).toBe(raw);
336337
});
337338

338339
test("the namespaced wait_agent call is repaired through the real bridge", async () => {
@@ -363,3 +364,77 @@ describe("native u64 fields advertised as number (#2316)", () => {
363364
});
364365
});
365366

367+
const CODEX_DESKTOP_WAIT_SCHEMA = {
368+
type: "object",
369+
properties: {
370+
"yield-time_ms": { type: "number" },
371+
max_tokens: { type: "number" },
372+
},
373+
};
374+
375+
const WAIT_SCOPE_SCHEMAS = new Map<string, Record<string, unknown>>([
376+
["wait", CODEX_DESKTOP_WAIT_SCHEMA],
377+
["other_tool", CODEX_DESKTOP_WAIT_SCHEMA],
378+
["cursor_wait", CODEX_DESKTOP_WAIT_SCHEMA],
379+
]);
380+
381+
const WAIT_SCOPE_NAMESPACE_MAP = new Map([
382+
["cursor_wait", { namespace: "cursor", name: "wait" }],
383+
]);
384+
385+
const WAIT_SCOPE_EVENTS: AdapterEvent[] = [
386+
{ type: "tool_call_start", id: "call_wait", name: "wait" },
387+
{ type: "tool_call_delta", arguments: '{"yield-time_ms":120000.0,"max_tokens":8000.0}' },
388+
{ type: "tool_call_end", id: "call_wait" },
389+
{ type: "tool_call_start", id: "call_fractional", name: "wait" },
390+
{ type: "tool_call_delta", arguments: '{"yield-time_ms":1.5,"max_tokens":1.5}' },
391+
{ type: "tool_call_end", id: "call_fractional" },
392+
{ type: "tool_call_start", id: "call_other", name: "other_tool" },
393+
{ type: "tool_call_delta", arguments: '{"yield-time_ms":120000.0,"max_tokens":8000.0}' },
394+
{ type: "tool_call_end", id: "call_other" },
395+
{ type: "tool_call_start", id: "call_namespaced", name: "cursor_wait" },
396+
{ type: "tool_call_delta", arguments: '{"yield-time_ms":120000.0,"max_tokens":8000.0}' },
397+
{ type: "tool_call_end", id: "call_namespaced" },
398+
{ type: "done" },
399+
];
400+
401+
describe("Codex Desktop wait native integers (#2443)", () => {
402+
const expectedCalls = [
403+
{ name: "wait", namespace: undefined, arguments: '{"yield-time_ms":120000,"max_tokens":8000}' },
404+
{ name: "wait", namespace: undefined, arguments: '{"yield-time_ms":1.5,"max_tokens":1.5}' },
405+
{ name: "other_tool", namespace: undefined, arguments: '{"yield-time_ms":120000.0,"max_tokens":8000.0}' },
406+
{ name: "wait", namespace: "cursor", arguments: '{"yield-time_ms":120000.0,"max_tokens":8000.0}' },
407+
];
408+
409+
test("streaming bridge scopes the repair to the bare wait tool", async () => {
410+
const frames = await collectSse(bridgeToResponsesSSE(
411+
replay(WAIT_SCOPE_EVENTS),
412+
"grok-4.6",
413+
WAIT_SCOPE_NAMESPACE_MAP,
414+
undefined,
415+
undefined,
416+
undefined,
417+
2_000,
418+
{ toolParameterSchemas: WAIT_SCOPE_SCHEMAS },
419+
));
420+
const calls = frames
421+
.filter(frame => frame.event === "response.output_item.done")
422+
.map(frame => frame.data.item as Record<string, unknown>)
423+
.filter(item => item.type === "function_call")
424+
.map(item => ({ name: item.name, namespace: item.namespace, arguments: item.arguments }));
425+
426+
expect(calls).toEqual(expectedCalls);
427+
});
428+
429+
test("non-streaming bridge scopes the repair to the bare wait tool", () => {
430+
const body = buildResponseJSON(WAIT_SCOPE_EVENTS, "grok-4.6", {
431+
toolNsMap: WAIT_SCOPE_NAMESPACE_MAP,
432+
toolParameterSchemas: WAIT_SCOPE_SCHEMAS,
433+
}) as Record<string, unknown>;
434+
const calls = (body.output as Record<string, unknown>[])
435+
.filter(item => item.type === "function_call")
436+
.map(item => ({ name: item.name, namespace: item.namespace, arguments: item.arguments }));
437+
438+
expect(calls).toEqual(expectedCalls);
439+
});
440+
});

0 commit comments

Comments
 (0)