Skip to content

Commit 8a40403

Browse files
committed
fix(adapters): name the advertised exec tool and keep listed apply_patch callable
CodeRabbit on #1895: use the transformed exec wire name in the shared catalog nudge, and do not forbid apply_patch at the top level when that tool is separately advertised in code mode.
1 parent bc22943 commit 8a40403

4 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎src/adapters/cursor/tool-definitions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ export function buildCursorToolGuidanceSystemNote(
615615
// Code mode: shell/edit/MCP live inside freeform `exec` as nested helpers. Without this the
616616
// model probes for a top-level shell tool that is not there.
617617
codeMode
618-
? `\`${CODEX_UNIFIED_EXEC_TOOL}\` is Codex code mode: its body is JavaScript evaluated in a V8 isolate, not a shell command and not Node. Shell, file edits, and MCP are nested helpers called INSIDE that body as \`await tools.<name>(...)\`, for example \`await tools.exec_command({cmd: \"ls\"})\`. Read the tool description and the isolate global \`ALL_TOOLS\` (not \`tools.ALL_TOOLS\`) for helpers this turn provides; absence from the top-level catalog or from \`exec\`'s description is not absence. Those nested helpers are not themselves top-level tools, so do not call \`exec_command\`, \`shell_command\`, or \`apply_patch\` at the top level here${codeModeOtherTopLevelNames.length > 0 ? `; every other tool this turn lists, including ${quotedNames(codeModeOtherTopLevelNames)}, remains callable at the top level as usual` : ""}.`
618+
? `\`${CODEX_UNIFIED_EXEC_TOOL}\` is Codex code mode: its body is JavaScript evaluated in a V8 isolate, not a shell command and not Node. Shell, file edits, and MCP are nested helpers called INSIDE that body as \`await tools.<name>(...)\`, for example \`await tools.exec_command({cmd: \"ls\"})\`. Read the tool description and the isolate global \`ALL_TOOLS\` (not \`tools.ALL_TOOLS\`) for helpers this turn provides; absence from the top-level catalog or from \`exec\`'s description is not absence. Those nested helpers are not themselves top-level tools, so do not call \`exec_command\` or \`shell_command\` at the top level here${codeModeOtherTopLevelNames.length > 0 ? `; every other tool this turn lists, including ${quotedNames(codeModeOtherTopLevelNames)}, remains callable at the top level as usual` : ""}.`
619619
: undefined,
620620
codeMode
621621
? "In code mode the isolate returns nothing on its own: call `text(...)` (or `notify(...)`) on any value you need to see, or the call completes with empty output. There is no `require`, no `module`, and no filesystem or network globals; reach the host only through the nested helpers."

‎src/adapters/tool-catalog-nudge.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ export function shouldInjectNonOpenAIToolCatalogNudge(provider: Pick<OcxProvider
4141
}
4242
}
4343

44-
function catalogListsCodeModeExec(
44+
function advertisedCodeModeExecName(
4545
advertised: ReadonlySet<string>,
4646
toWireName: (name: string) => string,
47-
): boolean {
48-
return advertised.has(CODEX_CODE_MODE_EXEC_TOOL) || advertised.has(toWireName(CODEX_CODE_MODE_EXEC_TOOL));
47+
): string | undefined {
48+
const wireName = toWireName(CODEX_CODE_MODE_EXEC_TOOL);
49+
if (advertised.has(wireName)) return wireName;
50+
if (advertised.has(CODEX_CODE_MODE_EXEC_TOOL)) return CODEX_CODE_MODE_EXEC_TOOL;
51+
return undefined;
4952
}
5053

5154
export function buildNonOpenAIToolCatalogNudgeFromNames(
@@ -63,16 +66,16 @@ export function buildNonOpenAIToolCatalogNudgeFromNames(
6366
const unavailableNeighborNames = NEIGHBOR_AGENT_TOOL_NAMES.filter(
6467
name => !advertised.has(name) && !advertised.has(toWireName(name)),
6568
);
66-
const codeMode = catalogListsCodeModeExec(advertised, toWireName);
69+
const codeModeExecName = advertisedCodeModeExecName(advertised, toWireName);
6770

6871
return [
6972
"Tool contract: use the current tool catalog as ground truth.",
7073
"Valid tool names for this turn are exactly " + quoteNames(names) + ".",
7174
"These listed names are the complete top-level tool-call surface for this turn.",
7275
"Call only listed names with their listed argument keys; do not invent, translate, or rename tools.",
7376
"Names mentioned only in instructions, tool descriptions, argument descriptions, or nested helper APIs are not additional top-level tools.",
74-
codeMode
75-
? "If `exec` is listed, it is Codex code mode: its body is JavaScript evaluated in a V8 isolate. Nested helpers are called INSIDE that body as `await tools.<name>(...)`, for example `await tools.exec_command({cmd: \"ls\"})` or `await tools.codex_app__list_threads({})`. Absence from the top-level catalog or from `exec`'s description is not absence: deferred helpers stay callable on `tools.<name>`. Discover them from the isolate global `ALL_TOOLS`, not `tools.ALL_TOOLS`. Do not skip an available nested helper because it is omitted from the listed top-level names."
77+
codeModeExecName
78+
? "If `" + codeModeExecName + "` is listed, it is Codex code mode: its body is JavaScript evaluated in a V8 isolate. Nested helpers are called INSIDE that body as `await tools.<name>(...)`, for example `await tools.exec_command({cmd: \"ls\"})` or `await tools.codex_app__list_threads({})`. Absence from the top-level catalog or from `" + codeModeExecName + "`'s description is not absence: deferred helpers stay callable on `tools.<name>`. Discover them from the isolate global `ALL_TOOLS`, not `tools.ALL_TOOLS`. Do not skip an available nested helper because it is omitted from the listed top-level names."
7679
: "If a listed tool exposes nested helpers such as a tools.* API, call the listed parent tool and use those helpers only inside that tool's input.",
7780
unavailableNeighborNames.length > 0
7881
? "Do not use neighboring-agent tool names " + quoteNames(unavailableNeighborNames) + " unless this turn's catalog lists those exact names."

‎tests/cursor-tool-definitions.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,21 @@ describe("Cursor code mode tool guidance", () => {
458458
expect(note).not.toContain("For file read/search/listing, use");
459459
});
460460

461+
test("does not forbid a separately listed apply_patch in code mode", () => {
462+
const note = buildCursorToolGuidanceSystemNote([
463+
codeModeExec(),
464+
{ name: "apply_patch", description: "Apply a patch", parameters: {}, freeform: true },
465+
]);
466+
expect(note).toBeDefined();
467+
if (!note) throw new Error("Expected Cursor tool guidance note");
468+
469+
expect(note).toContain("is Codex code mode");
470+
expect(note).toContain("remains callable at the top level as usual");
471+
expect(note).toContain("`apply_patch`");
472+
expect(note).not.toContain("do not call `exec_command`, `shell_command`, or `apply_patch` at the top level here");
473+
expect(note).toContain("do not call `exec_command` or `shell_command` at the top level here");
474+
});
475+
461476
test("keeps other visible top-level tools callable in code mode", () => {
462477
// Code mode is about how `exec` works, not a claim that the rest of the catalog is nested.
463478
// A turn can advertise freeform `exec` alongside ordinary top-level tools, and describing

‎tests/tool-catalog-nudge.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ describe("non-OpenAI tool catalog nudge", () => {
7878
name => `cx_${name}`,
7979
);
8080

81-
expect(note).toContain("If `exec` is listed, it is Codex code mode");
81+
expect(note).toContain("If `cx_exec` is listed, it is Codex code mode");
82+
expect(note).toContain("from `cx_exec`'s description is not absence");
8283
expect(note).toContain("isolate global `ALL_TOOLS`, not `tools.ALL_TOOLS`");
84+
expect(note).not.toContain("If `exec` is listed, it is Codex code mode");
8385
});
8486

8587
// `advertised` holds WIRE names. A provider that rewrites them (Claude OAuth `custom_`,

0 commit comments

Comments
 (0)