Skip to content

Commit cebe005

Browse files
authored
Merge pull request #2724 from lidge-jun/codex/l2-2663-code-mode-exec
fix(responses): bridge bare code-mode helper calls through exec (#2663)
2 parents 64c6d64 + cb9bb9b commit cebe005

13 files changed

Lines changed: 575 additions & 68 deletions
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# wp2 — L1 lane outcome
2+
3+
Landed on `dev` as `2feffbdc3` (PR #2720), which carried four merges:
4+
5+
| PR | author | preserved as | disposition |
6+
|---|---|---|---|
7+
| #2672 | Ingwannu | `2a9c18dc5` | MERGED, no changes needed |
8+
| #2674 | Ingwannu | `e3b136fb7` | MERGED, no changes needed |
9+
| #2671 | DevonGithub | `17aadf88e` | MERGED + one added test (`4a4df12f2`) |
10+
| #2684 | Michael-Z-Freeman | `2c85dd48d` | MERGED, checklist-only CI failures |
11+
12+
All four show `MERGED` on GitHub with a comment naming the landed sha.
13+
14+
## Evidence
15+
16+
- `bun run test` (full suite, local, session 77096): exit 0.
17+
- PR #2720 CI after one rerun: 23 pass, 1 skipping, 0 fail.
18+
- `bun x tsc --noEmit`: clean on the merged tree.
19+
- Focused: forward-prompt-envelope + posit-continuation 9/9, muse-vision 6/6,
20+
azure-model-router 2/2, repo-hygiene 11/11.
21+
22+
## The `test 1/4` failure was a flake, and it was checked rather than assumed
23+
24+
First CI run failed one case: `update stops the running proxy before replacing files >
25+
npm launcher restarts the stopped runtime after a staged update failure`, at 46797ms.
26+
The same case passes locally in 2.6s, this branch touches no update/launcher file
27+
(`git diff --name-only 9b838d062 HEAD | grep -E 'update|launcher'` is empty), and the
28+
test already carries two prior timing-budget repairs (`538a602af`, `34ef53966`). Rerun
29+
of the failed job alone: green.
30+
31+
That is the standard this round applies — a rerun-to-green is only acceptable when the
32+
causal question was actually asked first.
33+
34+
## Discovered constraint: `dev` is protected
35+
36+
A direct `git push origin dev` is rejected: "Changes must be made through a pull
37+
request." So every later lane lands the same way — a `codex/` branch plus a PR, not a
38+
local merge and push. The local merges are still how the work is built and verified;
39+
they simply travel through a PR.
40+
41+
## Carried forward
42+
43+
- #2671's probe-evidence question (007, finding 10) is open on a MERGED change.
44+
- #2690 is now guaranteed to conflict with `dev`, since #2684 landed. That was the
45+
intended ordering, not an accident.
46+
- `src/server/responses/core.ts` evidence for #2663/#2638/#2497/#2694 is unaffected by
47+
this lane: none of the four L1 merges touched it.

‎src/bridge.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { redactSecretString } from "./lib/redact";
1919
import { repairFreeformToolInput } from "./responses/apply-patch-envelope";
2020
import { encodeCompactionSummary } from "./responses/compaction";
21+
import { compileCodeModeHelperInput } from "./responses/code-mode-helper-compat";
2122
import { isTruncatedStopReason, truncationReasonFor } from "./responses/truncated-stop-reason";
2223
import { encodeReasoningEnvelope, type ReasoningEnvelope } from "./responses/reasoning-envelope";
2324
import { rememberReasoningForCall } from "./responses/reasoning-replay-cache";
@@ -244,9 +245,14 @@ export function bridgeToResponsesSSE(
244245
// Freeform/custom tools (apply_patch, code-mode exec) carry their body in `input`; the
245246
// model is given a function with `{input:string}`, so unwrap it here when relaying back
246247
// as a custom_tool_call. Decorated apply_patch envelopes are repaired at this boundary.
247-
const freeformInput = (args: string, toolName: string, namespace?: string): string => (
248-
repairFreeformToolInput(args, toolName, namespace)
249-
);
248+
const freeformInput = (
249+
args: string,
250+
toolName: string,
251+
namespace?: string,
252+
codeModeHelperName?: string,
253+
): string => codeModeHelperName
254+
? compileCodeModeHelperInput(args, codeModeHelperName)
255+
: repairFreeformToolInput(args, toolName, namespace);
250256
// Best-effort unwrap of a PARTIAL freeform arg buffer for live input streaming
251257
// (`response.custom_tool_call_input.delta` — codex-rs uses it for UI preview only;
252258
// the completed custom_tool_call item stays authoritative). Compact `{"input":"...`
@@ -530,7 +536,7 @@ export function bridgeToResponsesSSE(
530536
// synthetic compaction item's payload on done.
531537
let compactionText = "";
532538
let compactionTextBytes = 0;
533-
let currentToolCall: { itemId: string; outputIndex: number; callId: string; name: string; args: string; argsBytes: number; namespace?: string; freeform?: boolean; toolSearch?: boolean; inputEmitted?: string; providerMetadata?: OcxProviderOpaqueToolCallMetadata } | null = null;
539+
let currentToolCall: { itemId: string; outputIndex: number; callId: string; name: string; args: string; argsBytes: number; namespace?: string; freeform?: boolean; toolSearch?: boolean; inputEmitted?: string; codeModeHelperName?: string; providerMetadata?: OcxProviderOpaqueToolCallMetadata } | null = null;
534540
// Open native web-search cell (between begin and end). Holds the output index allocated on
535541
// begin so the matching done reuses it; closed as `failed` if the stream terminates early.
536542
let currentWebSearch: { itemId: string; eventId: string; outputIndex: number } | null = null;
@@ -649,7 +655,7 @@ export function bridgeToResponsesSSE(
649655
emit("response.custom_tool_call_input.done", {
650656
item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex,
651657
...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
652-
input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace),
658+
input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName),
653659
});
654660
}
655661
// Freeform tools serialize as custom_tool_call without extra_content; remember the
@@ -666,7 +672,7 @@ export function bridgeToResponsesSSE(
666672
type: "custom_tool_call", id: currentToolCall.itemId,
667673
call_id: currentToolCall.callId, name: currentToolCall.name,
668674
...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
669-
input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace), status: "completed",
675+
input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName), status: "completed",
670676
}
671677
: {
672678
type: "function_call", id: currentToolCall.itemId,
@@ -706,7 +712,7 @@ export function bridgeToResponsesSSE(
706712
type: "custom_tool_call", id: currentToolCall.itemId,
707713
call_id: currentToolCall.callId, name: currentToolCall.name,
708714
...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
709-
input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace), status: "incomplete",
715+
input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName), status: "incomplete",
710716
}
711717
: {
712718
type: "function_call", id: currentToolCall.itemId,
@@ -1052,6 +1058,9 @@ export function bridgeToResponsesSSE(
10521058
}
10531059
if (currentToolCall) closeCurrentToolCall();
10541060
const effectiveName = normalizeDeclaredToolName(event.name, options?.declaredToolNames);
1061+
const codeModeHelperName = effectiveName === "exec" && event.name !== effectiveName
1062+
? event.name
1063+
: undefined;
10551064
const mapped = toolNsMap?.get(effectiveName);
10561065
const realName = mapped?.name ?? effectiveName;
10571066
if (options?.declaredToolNames && !options.declaredToolNames.has(effectiveName)) {
@@ -1083,7 +1092,7 @@ export function bridgeToResponsesSSE(
10831092
? { type: "custom_tool_call", id: itemId, call_id: event.id, name: realName, ...(ns ? { namespace: ns } : {}), input: "", status: "in_progress" }
10841093
: { type: "function_call", id: itemId, call_id: event.id, name: realName, arguments: "", status: "in_progress", ...(ns ? { namespace: ns } : {}) };
10851094
emit("response.output_item.added", { output_index: outputIndex, item });
1086-
currentToolCall = { itemId, outputIndex, callId: event.id, name: realName, args: "", argsBytes: 0, namespace: ns, freeform, toolSearch, providerMetadata: event.providerMetadata };
1095+
currentToolCall = { itemId, outputIndex, callId: event.id, name: realName, args: "", argsBytes: 0, namespace: ns, freeform, toolSearch, codeModeHelperName, providerMetadata: event.providerMetadata };
10871096
budget?.openCall(event.id);
10881097
break;
10891098
}
@@ -1102,7 +1111,7 @@ export function bridgeToResponsesSSE(
11021111
delta: event.arguments,
11031112
});
11041113
}
1105-
if (currentToolCall.freeform) {
1114+
if (currentToolCall.freeform && !currentToolCall.codeModeHelperName) {
11061115
// Hold while the buffer is still an ambiguous prefix of the JSON wrapper,
11071116
// then stream only the unwrapped input suffix (never rewind on mode flips).
11081117
if (!FREEFORM_WRAP_PREFIX.startsWith(currentToolCall.args)) {
@@ -1587,15 +1596,21 @@ function buildResponseJSONWithBudget(
15871596
let batchKiroRedactedBytes = 0;
15881597
let currentToolCallId = "";
15891598
let currentToolCallName = "";
1599+
let currentToolCallCodeModeHelperName: string | undefined;
15901600
let currentToolCallArgs = "";
15911601
let currentToolCallProviderMetadata: OcxProviderOpaqueToolCallMetadata | undefined;
15921602
let currentToolCallArgsBytes = 0;
15931603
// Web-search citations awaiting the next assistant message (attached as url_citation annotations).
15941604
let pendingWebSources: { url: string; title?: string }[] = [];
15951605

1596-
const freeformInput = (args: string, toolName: string, namespace?: string): string => (
1597-
repairFreeformToolInput(args, toolName, namespace)
1598-
);
1606+
const freeformInput = (
1607+
args: string,
1608+
toolName: string,
1609+
namespace?: string,
1610+
codeModeHelperName?: string,
1611+
): string => codeModeHelperName
1612+
? compileCodeModeHelperInput(args, codeModeHelperName)
1613+
: repairFreeformToolInput(args, toolName, namespace);
15991614
const parseArgsObj = (args: string): Record<string, unknown> => {
16001615
try { const o = JSON.parse(args); return o && typeof o === "object" ? o : {}; } catch { return {}; }
16011616
};
@@ -1697,7 +1712,7 @@ function buildResponseJSONWithBudget(
16971712
type: "custom_tool_call", id: `ctc_${uuid()}`,
16981713
call_id: currentToolCallId, name: realName,
16991714
...(ns ? { namespace: ns } : {}),
1700-
input: freeformInput(currentToolCallArgs, realName, ns), status,
1715+
input: freeformInput(currentToolCallArgs, realName, ns, currentToolCallCodeModeHelperName), status,
17011716
});
17021717
} else {
17031718
pushOutput({
@@ -1711,6 +1726,7 @@ function buildResponseJSONWithBudget(
17111726
budget?.closeCall(currentToolCallId);
17121727
currentToolCallId = "";
17131728
currentToolCallName = "";
1729+
currentToolCallCodeModeHelperName = undefined;
17141730
currentToolCallProviderMetadata = undefined;
17151731
currentToolCallArgs = "";
17161732
currentToolCallArgsBytes = 0;
@@ -1825,6 +1841,9 @@ function buildResponseJSONWithBudget(
18251841
currentToolCallId = e.id;
18261842
budget?.openCall(e.id);
18271843
currentToolCallName = effectiveName;
1844+
currentToolCallCodeModeHelperName = effectiveName === "exec" && e.name !== effectiveName
1845+
? e.name
1846+
: undefined;
18281847
currentToolCallArgs = "";
18291848
currentToolCallArgsBytes = 0;
18301849
currentToolCallProviderMetadata = e.providerMetadata;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { normalizeApplyPatchDelimiters } from "./apply-patch-envelope";
2+
3+
function isPlainObject(value: unknown): value is Record<string, unknown> {
4+
return !!value && typeof value === "object" && !Array.isArray(value);
5+
}
6+
7+
function unwrapPatchInput(value: string): string {
8+
try {
9+
const parsed: unknown = JSON.parse(value);
10+
if (isPlainObject(parsed)) {
11+
if (typeof parsed.input === "string") return parsed.input;
12+
if (typeof parsed.patch === "string") return parsed.patch;
13+
}
14+
} catch {
15+
// Native custom calls carry the patch body directly.
16+
}
17+
return value;
18+
}
19+
20+
/**
21+
* Convert a nested Code Mode helper call into unified-exec JavaScript.
22+
*
23+
* Parsed values are serialized as data, never interpolated as source, so command and patch text
24+
* cannot escape the generated call. Invalid structured shell payloads are also passed as data so
25+
* nested-tool validation can reject them without evaluating provider text as JavaScript.
26+
*/
27+
export function compileCodeModeHelperInput(argumentsText: unknown, toolName: string): string {
28+
if (typeof argumentsText !== "string") return "";
29+
if (toolName === "apply_patch") {
30+
const patch = normalizeApplyPatchDelimiters(unwrapPatchInput(argumentsText));
31+
return `const result = await tools.apply_patch(${JSON.stringify(patch)});\ntext(result);`;
32+
}
33+
let parsed: unknown = argumentsText;
34+
try {
35+
parsed = JSON.parse(argumentsText);
36+
} catch {
37+
// Keep malformed provider text as data rather than executable source.
38+
}
39+
const args: unknown = isPlainObject(parsed) ? { ...parsed } : parsed;
40+
if (
41+
toolName === "shell_command"
42+
&& isPlainObject(args)
43+
&& typeof args.command === "string"
44+
&& args.cmd === undefined
45+
) {
46+
args.cmd = args.command;
47+
delete args.command;
48+
}
49+
return `const result = await tools.exec_command(${JSON.stringify(args)});\ntext(result);`;
50+
}

‎src/responses/custom-tool-compat.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { namespacedToolName } from "../types";
1+
import { namespacedToolName, normalizeDeclaredToolName } from "../types";
22
import {
33
normalizeApplyPatchDelimiters,
44
repairFreeformToolInput,
55
unwrapFreeformToolInput,
66
} from "./apply-patch-envelope";
7+
import { compileCodeModeHelperInput } from "./code-mode-helper-compat";
78
import { collectResponsesToolGroups } from "./tool-groups";
89

910
const ROUTED_CUSTOM_TOOL_PASSTHROUGH = new Set(["apply_patch"]);
@@ -65,6 +66,20 @@ export function routedCustomToolWireName(value: unknown): string | undefined {
6566
);
6667
}
6768

69+
/** Resolve a provider-emitted wire name to the routed custom tool the client declared. */
70+
export function routedCustomToolTargetName(
71+
value: unknown,
72+
names: ReadonlySet<string>,
73+
declaredNames?: ReadonlySet<string>,
74+
): string | undefined {
75+
const wireName = routedCustomToolWireName(value);
76+
if (wireName === undefined) return undefined;
77+
if (names.has(wireName)) return wireName;
78+
if (!isPlainObject(value) || typeof value.namespace === "string") return undefined;
79+
const normalized = normalizeDeclaredToolName(wireName, declaredNames);
80+
return normalized !== wireName && names.has(normalized) ? normalized : undefined;
81+
}
82+
6883
/**
6984
* Names of custom declarations after namespace lowering. The selection flag separates converted
7085
* names from native passthrough names while keeping same-named function and custom children distinct.
@@ -250,29 +265,37 @@ export function restoreRoutedCustomCalls(
250265
value: unknown,
251266
names: ReadonlySet<string>,
252267
repairNames: ReadonlySet<string> = new Set(),
268+
declaredNames?: ReadonlySet<string>,
253269
): { value: unknown; changed: boolean } {
254270
if (!isPlainObject(value)) return { value, changed: false };
255271

256272
const restoreItem = (item: unknown): { value: unknown; changed: boolean } => {
257273
if (!isPlainObject(item)) return { value: item, changed: false };
258274
const wireName = routedCustomToolWireName(item);
275+
const targetName = routedCustomToolTargetName(item, names, declaredNames);
259276
if (
260-
item.type === "function_call"
277+
(item.type === "function_call" || item.type === "custom_tool_call")
261278
&& typeof item.name === "string"
262279
&& wireName !== undefined
263-
&& names.has(wireName)
280+
&& targetName !== undefined
264281
) {
282+
const sourceInput = item.type === "function_call" ? item.arguments : item.input;
283+
const aliased = targetName !== wireName;
265284
const restored: Record<string, unknown> = {
266285
...item,
267286
type: "custom_tool_call",
268287
id: customToolItemId(item.id),
269-
input: repairFreeformToolInput(
270-
item.arguments,
271-
item.name,
272-
typeof item.namespace === "string" ? item.namespace : undefined,
273-
),
288+
name: aliased ? targetName : item.name,
289+
input: aliased && sourceInput !== ""
290+
? compileCodeModeHelperInput(sourceInput, item.name)
291+
: repairFreeformToolInput(
292+
sourceInput,
293+
targetName,
294+
typeof item.namespace === "string" ? item.namespace : undefined,
295+
),
274296
};
275297
delete restored.arguments;
298+
if (aliased) delete restored.namespace;
276299
return { value: restored, changed: true };
277300
}
278301
if (
@@ -323,7 +346,7 @@ export function restoreRoutedCustomCalls(
323346
&& value.type.startsWith("response.")
324347
&& isPlainObject(value.response)
325348
) {
326-
const response = restoreRoutedCustomCalls(value.response, names, repairNames);
349+
const response = restoreRoutedCustomCalls(value.response, names, repairNames, declaredNames);
327350
if (response.changed) {
328351
restored.response = response.value;
329352
changed = true;
@@ -337,6 +360,7 @@ export function restoreRoutedCustomCallsInJson(
337360
text: string,
338361
names: ReadonlySet<string>,
339362
repairNames: ReadonlySet<string> = new Set(),
363+
declaredNames?: ReadonlySet<string>,
340364
): string {
341365
if (names.size === 0 && repairNames.size === 0) return text;
342366
let payload: unknown;
@@ -345,7 +369,7 @@ export function restoreRoutedCustomCallsInJson(
345369
} catch {
346370
return text;
347371
}
348-
const restored = restoreRoutedCustomCalls(payload, names, repairNames);
372+
const restored = restoreRoutedCustomCalls(payload, names, repairNames, declaredNames);
349373
return restored.changed ? JSON.stringify(restored.value) : text;
350374
}
351375

0 commit comments

Comments
 (0)