Skip to content

Commit 2085db1

Browse files
committed
fix(responses): unwrap one extra executable freeform body wrapper
1 parent 63de499 commit 2085db1

7 files changed

Lines changed: 140 additions & 6 deletions

File tree

‎docs-site/src/content/docs/guides/codex-integration.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,11 @@ Codex. Native custom calls and converted function calls use the same completion
750750
patch previews are held while their executable form is unresolved. JavaScript that merely
751751
contains patch text and unrelated native custom payloads stay unchanged.
752752

753+
When a routed model double-wraps an executable freeform body, opencodex removes one extra
754+
JSON object containing only an input string. This repair applies to local exec and apply_patch
755+
bodies, leaves other tool grammars and ambiguous objects unchanged, and does not decode recursively.
756+
Possible nested wrappers hold their streamed preview until the completed input is available.
757+
753758
A routed model can also mistakenly send a shell-argument object such as
754759
`{"cmd":"git status --short"}` to code-mode `exec`. For a verified code-mode catalog,
755760
opencodex converts an unambiguous shell object into `tools.exec_command(...)` JavaScript

‎src/responses/apply-patch-envelope.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ function stripMarkdownCodeFence(text: string, toolName: string): string {
3939
return match ? match[1] : text;
4040
}
4141

42+
/** Repair one extra canonical wrapper inside an already unwrapped executable body. */
43+
function repairUnwrappedBody(text: string, toolName: string): string {
44+
if (toolName !== "exec" && toolName !== "apply_patch") return text;
45+
try {
46+
const nested: unknown = JSON.parse(text);
47+
if (nested && typeof nested === "object" && !Array.isArray(nested)) {
48+
const record = nested as Record<string, unknown>;
49+
const keys = Object.keys(record);
50+
if (keys.length === 1 && keys[0] === "input" && typeof record.input === "string") text = record.input;
51+
}
52+
} catch {
53+
// Ordinary JavaScript and patches are not JSON wrappers.
54+
}
55+
return stripMarkdownCodeFence(text, toolName);
56+
}
57+
4258
/** Unwrap the `{input:string}` function-call wrapper used for freeform tools. */
4359
export function unwrapFreeformToolInput(argumentsText: unknown, toolName = ""): string {
4460
if (typeof argumentsText !== "string") return "";
@@ -48,13 +64,13 @@ export function unwrapFreeformToolInput(argumentsText: unknown, toolName = ""):
4864
const record = parsed as Record<string, unknown>;
4965
if (Object.prototype.hasOwnProperty.call(record, "input")) {
5066
return typeof record.input === "string"
51-
? stripMarkdownCodeFence(record.input, toolName)
67+
? repairUnwrappedBody(record.input, toolName)
5268
: argumentsText;
5369
}
5470
const fallbackKeys = freeformFallbackKeys(toolName);
5571
const candidates = fallbackKeys.filter(key => typeof record[key] === "string");
5672
if (candidates.length === 1) {
57-
return stripMarkdownCodeFence(record[candidates[0]] as string, toolName);
73+
return repairUnwrappedBody(record[candidates[0]] as string, toolName);
5874
}
5975
}
6076
} catch {

‎src/responses/progressive-freeform-input.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ function decodeJsonStringPrefix(body: string): string | null {
7777
*
7878
* `input` is decidable from its prefix: `unwrapFreeformToolInput` returns it whenever the
7979
* key is present, whatever else the object carries, so its value can be unescaped
80-
* progressively and never retracted.
80+
* progressively and never retracted, except for executable bodies that may contain one
81+
* extra canonical wrapper. Those object-leading bodies hold until authoritative completion.
8182
*
8283
* One case escapes that claim and is accepted rather than fixed: a duplicate `input` key.
8384
* `JSON.parse` keeps the last one, so `{"input":"a","input":"b"}` streams a and completes with
@@ -111,6 +112,9 @@ export function progressiveFreeformInput(args: string, toolName: string): string
111112
if (scan.kind === "input") {
112113
const decoded = decodeJsonStringPrefix(args.slice(scan.valueStart));
113114
if (decoded === null) return null;
115+
// Completion may remove one extra {input:string} body wrapper. A later member can still
116+
// disqualify it, so hold the whole object-shaped preview rather than decode recursively.
117+
if ((toolName === "exec" || toolName === "apply_patch") && decoded.trimStart().startsWith("{")) return null;
114118
return mayBecomeFencedBody(decoded, toolName) ? null : decoded;
115119
}
116120
if (scan.kind === "raw") return mayBecomeFencedBody(args, toolName) ? null : args;

‎structure/transports/responses.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ Function-call wrappers around freeform bodies are restored by
125125
`exec` and `apply_patch`, one tool-specific alternate field or one complete outer Markdown fence
126126
is recoverable because the wrapper is otherwise unusable; two alternate fields are ambiguous and
127127
therefore remain untouched. Foreign freeform grammars never receive that compatibility rewrite.
128+
An already unwrapped executable body may carry exactly one extra complete JSON object with only
129+
an input string; that layer is removed once, never recursively. Extra members and non-string
130+
values remain literal. A decoded object-leading body holds its preview until completion.
128131

129132
#### Schema-bound flat shell repair
130133

@@ -147,9 +150,9 @@ Which wrapper applies is decided by scanning the prefix as JSON rather than matc
147150
a literal opening. `JSON.parse` decides the completed input, and it cares about neither property
148151
order nor how a name is spelled, so a canonical key arriving after other properties or written
149152
with an escape is the same wrapper and has to preview as one (#5151). The buffering policy that
150-
follows is: an own `input` with a string value streams progressively, because completion gives
151-
it precedence over everything else in the object whatever its position; an `input` with a
152-
non-string value, a text that is not an object, and an object `JSON.parse` can no longer accept
153+
follows is: an own string `input` streams progressively unless its decoded executable body
154+
could still be a nested wrapper; completion gives that field precedence over other properties.
155+
An `input` with a non-string value, a text that is not an object, and an object `JSON.parse` can no longer accept
153156
all publish their own bytes, because that is what completion returns for them; every other
154157
object HOLDS until it closes, because a key that has not arrived yet can still change the
155158
answer. Fallback fields decide at that close rather than from a parse: they only unwrap as the

‎tests/responses/apply-patch-envelope.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
normalizeApplyPatchDelimiters,
66
repairFreeformToolInput,
77
stableCanonicalPatchPrefix,
8+
unwrapFreeformToolInput,
89
} from "../../src/responses/apply-patch-envelope";
910
import { compileCodeModeHelperInput, resolveCodeModeHelperName } from "../../src/responses/code-mode-helper-compat";
1011

@@ -23,6 +24,48 @@ const CANONICAL_PATCH = `*** Begin Patch
2324
*** End Patch`;
2425

2526
describe("apply_patch envelope repair", () => {
27+
test.each([
28+
["exec", 'text("nested wrapper");'],
29+
["apply_patch", CANONICAL_PATCH],
30+
])("unwraps one extra canonical string body for %s", (tool, body) => {
31+
const nested = JSON.stringify({ input: body });
32+
const args = JSON.stringify({ input: nested });
33+
expect(repairFreeformToolInput(args, tool)).toBe(body);
34+
expect(repairFreeformToolInput(args, tool, "functions")).toBe(body);
35+
expect(repairFreeformToolInput(body, tool)).toBe(body);
36+
expect(repairFreeformToolInput(nested, tool)).toBe(body);
37+
// A third layer is deliberately left in place.
38+
expect(repairFreeformToolInput(JSON.stringify({ input: args }), tool)).toBe(nested);
39+
expect(repairFreeformToolInput(args, tool, "mcp")).toBe(nested);
40+
expect(repairFreeformToolInput(args, "render_diagram")).toBe(nested);
41+
});
42+
43+
test("the reported exec body parses only after the extra wrapper is removed", () => {
44+
const body = 'text("nested wrapper");';
45+
const nested = JSON.stringify({ input: body });
46+
expect(() => new Function("text", nested)).toThrow(SyntaxError);
47+
const values: string[] = [];
48+
new Function("text", repairFreeformToolInput(JSON.stringify({ input: nested }), "exec"))(
49+
(value: string) => values.push(value),
50+
);
51+
expect(values).toEqual(["nested wrapper"]);
52+
});
53+
54+
test.each(["exec", "apply_patch"])("preserves noncanonical nested %s bodies", tool => {
55+
for (const nested of [
56+
'{"input":"text(1)","extra":true}',
57+
'{"input":42}', '{"input":null}', '{"input":{}}',
58+
'{"code":"text(1)"}', '["text(1)"]', '{"input":"incomplete',
59+
]) {
60+
expect(repairFreeformToolInput(JSON.stringify({ input: nested }), tool)).toBe(nested);
61+
}
62+
for (const input of [null, 42, true, {}, []]) {
63+
const args = JSON.stringify({ input });
64+
expect(repairFreeformToolInput(args, tool)).toBe(args);
65+
}
66+
expect(unwrapFreeformToolInput(undefined, tool)).toBe("");
67+
});
68+
2669
test("repairs only the outer lines of a complete top-level apply_patch payload", () => {
2770
expect(repairFreeformToolInput(DECORATED_PATCH, "apply_patch")).toBe(CANONICAL_PATCH);
2871
expect(normalizeApplyPatchDelimiters(DECORATED_PATCH)).toBe(CANONICAL_PATCH);
@@ -176,6 +219,12 @@ describe("raw exec patch envelope recognition", () => {
176219
expect(source).not.toContain("*** Begin Patch ***");
177220
});
178221

222+
test("recognizes a double-wrapped patch submitted as exec", () => {
223+
const args = JSON.stringify({ input: JSON.stringify({ input: DECORATED_PATCH }) });
224+
expect(resolveCodeModeHelperName(undefined, "exec", args, undefined, CODE_MODE)).toBe("apply_patch");
225+
expect(compileCodeModeHelperInput(args, "apply_patch", "exec")).toContain(JSON.stringify(CANONICAL_PATCH));
226+
});
227+
179228
test("refuses JavaScript that merely mentions an envelope", () => {
180229
const cases = [
181230
"const sample = \"tools.apply_patch({ input: patchText })\";",

‎tests/responses/responses-custom-tool-stream-consistency.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { describe, expect, test } from "bun:test";
2+
import { bridgeToResponsesSSE } from "../../src/bridge";
3+
import type { AdapterEvent } from "../../src/types";
24
import {
35
createRoutedCustomToolRestoreBlockRewrite,
46
RoutedCustomToolProgressError,
@@ -108,6 +110,39 @@ function restoreExecStream(argumentsText: string, fragments: readonly string[],
108110
}
109111

110112
describe("routed Responses custom-tool stream consistency", () => {
113+
test.each([
114+
["exec", 'text("nested");'],
115+
["apply_patch", "*** Begin Patch\n*** Add File: a.txt\n+x\n*** End Patch"],
116+
])("double-wrapped %s agrees across direct and routed completion", async (tool, input) => {
117+
const args = JSON.stringify({ input: JSON.stringify({ input }) });
118+
for (let split = 0; split <= args.length; split++) {
119+
const fragments = [args.slice(0, split), args.slice(split)];
120+
const routed = restoreExecStream(args, fragments, tool);
121+
expect(routed.preview, "routed split " + split).toBe("");
122+
expect([routed.doneInput, routed.itemInput, routed.terminalInput]).toEqual([input, input, input]);
123+
124+
async function* events(): AsyncGenerator<AdapterEvent> {
125+
yield { type: "tool_call_start", id: "call_nested", name: tool };
126+
for (const argumentsPart of fragments) yield { type: "tool_call_delta", arguments: argumentsPart };
127+
yield { type: "tool_call_end", id: "call_nested" };
128+
yield { type: "done" };
129+
}
130+
const wire = await new Response(bridgeToResponsesSSE(
131+
events(), "routed/model", undefined, new Set([tool]),
132+
)).text();
133+
const frames = wire.split("\n\n")
134+
.filter(block => block.includes("data:") && !block.includes("data: [DONE]"))
135+
.map(dataPayload);
136+
const deltas = frames.filter(frame => frame.type === "response.custom_tool_call_input.delta");
137+
// Both transports hold the ambiguous preview; completed input is authoritative.
138+
expect(deltas, "direct split " + split).toEqual([]);
139+
const done = frames.find(frame => frame.type === "response.custom_tool_call_input.done");
140+
const item = frames.find(frame => frame.type === "response.output_item.done")?.item as { input: string };
141+
const terminal = frames.find(frame => frame.type === "response.completed")?.response as { output: Array<{ input: string }> };
142+
expect([done?.input, item.input, terminal.output[0]?.input]).toEqual([input, input, input]);
143+
}
144+
});
145+
111146
test.each([
112147
["late-invalid wrapper", ['{"input":"*** Begin Patch\\n*** Add File: a\\n+x\\n*** End Patch', '\\q"}']],
113148
["duplicate input key", ['{"input":"*** Begin Patch\\n*** Add File: a\\n+x\\n*** End Patch",', '"input":"replacement"}']],

‎tests/responses/responses-freeform-wrapper-keys.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ const emissions = (body: string, toolName: string) => stream(body, toolName).del
4747
const published = (body: string, toolName: string) => emissions(body, toolName).join("");
4848

4949
describe("freeform wrapper keys the literal matcher could not see", () => {
50+
test.each(["exec", "apply_patch"])("holds a possible extra %s wrapper at every prefix", tool => {
51+
const source = tool === "exec" ? 'text("nested");' : "*** Begin Patch\n*** Add File: a.txt\n+x\n*** End Patch";
52+
for (const body of [
53+
JSON.stringify({ input: JSON.stringify({ input: source }) }),
54+
JSON.stringify({ code: JSON.stringify({ input: source }) }),
55+
JSON.stringify({ input: ' \n { "\\u0069nput": ' + JSON.stringify(source) + " }" }),
56+
]) {
57+
// The alternate field is recognized only by exec; apply_patch uses patch.
58+
const args = tool === "apply_patch" ? body.replace('{"code":', '{"patch":') : body;
59+
expect(unwrapFreeformToolInput(args, tool)).toBe(source);
60+
expect(stream(args, tool)).toEqual({ deltas: [], retractions: [] });
61+
expect(progressiveFreeformInput(args, tool)).toBeNull();
62+
}
63+
const foreign = JSON.stringify({ input: JSON.stringify({ input: source }) });
64+
expect(published(foreign, "")).toBe(JSON.stringify({ input: source }));
65+
// A valid JS block can begin like an object too: only its preview is sacrificed.
66+
const block = "{ let x = 1; return x; }";
67+
const wrappedBlock = JSON.stringify({ input: block });
68+
expect(unwrapFreeformToolInput(wrappedBlock, tool)).toBe(block);
69+
expect(published(wrappedBlock, tool)).toBe("");
70+
});
71+
5072
// `progressive` records whether the value can be previewed before the object closes. It is
5173
// the liveness half of the contract and it is asserted, because holding everything until
5274
// completion would satisfy the agreement half on its own while quietly ending progressive

0 commit comments

Comments
 (0)