Skip to content

Commit b732b0d

Browse files
lidge-junalex-jordan547jun
authored
fix(responses): scope the self-named namespace scrub to declared bare tools (carry of #3226) (#3234)
* fix(responses): scope self-named namespace scrub * fix(responses): preserve colliding namespaced functions * fix(responses): honor scrub authorization identity * fix(responses): cover function scrub edge cases * fix(responses): read Chat-shaped function names in the scrub authorization set buildTools accepts the Chat-shaped `{ type: "function", function: { name } }` declaration and the undeclared-tool guard authorizes it, but the scrub's raw-body collector only read `spec.name`. Such a function never entered the raw-body set, the intersection dropped it, and a self-named echo for it reached Codex again. Mirror addWireToolName and read the nested name. Regression: Chat-shaped catalog + upstream `function_call { name: "wait", namespace: "wait" }` is scrubbed; red without this change. --------- Co-authored-by: Alex Jordan <[email protected]> Co-authored-by: jun <[email protected]>
1 parent d23eab4 commit b732b0d

4 files changed

Lines changed: 347 additions & 20 deletions

File tree

‎src/server/responses-self-named-namespace-scrub.ts‎

Lines changed: 130 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,130 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
44
return !!value && typeof value === "object" && !Array.isArray(value);
55
}
66

7+
export interface SelfNamedNamespaceScrubAuthorization {
8+
customToolCallNames: ReadonlySet<string>;
9+
functionCallNames: ReadonlySet<string>;
10+
}
11+
12+
function collectBareToolSpecs(
13+
bareCustomNames: Set<string>,
14+
bareFunctionNames: Set<string>,
15+
sameNameNamespacedCustomNames: Set<string>,
16+
sameNameNamespacedFunctionNames: Set<string>,
17+
specs: unknown,
18+
): void {
19+
if (!Array.isArray(specs)) return;
20+
for (const spec of specs) {
21+
if (!isPlainObject(spec)) continue;
22+
if (spec.type === "namespace" && Array.isArray(spec.tools)) {
23+
const namespace = typeof spec.name === "string" ? spec.name : undefined;
24+
for (const inner of spec.tools) {
25+
if (!isPlainObject(inner) || typeof inner.name !== "string") continue;
26+
if (namespace !== "functions" && namespace === inner.name) {
27+
if (inner.type === "custom") sameNameNamespacedCustomNames.add(inner.name);
28+
else if (inner.type === "function") sameNameNamespacedFunctionNames.add(inner.name);
29+
}
30+
if (namespace === "functions") {
31+
if (inner.type === "custom") bareCustomNames.add(inner.name);
32+
else if (inner.type === "function") bareFunctionNames.add(inner.name);
33+
}
34+
}
35+
continue;
36+
}
37+
// `buildTools` (parser.ts) also accepts the Chat-shaped `{ type: "function", function: { name } }`
38+
// declaration, and the undeclared-tool guard authorizes it the same way. Reading only
39+
// `spec.name` here left such a function out of the raw-body set, so the intersection dropped
40+
// it and a self-named echo for it reached Codex again.
41+
const nestedFunction = spec.type === "function" && isPlainObject(spec.function) ? spec.function : undefined;
42+
const name = typeof spec.name === "string" && spec.name.length > 0
43+
? spec.name
44+
: typeof nestedFunction?.name === "string" && nestedFunction.name.length > 0
45+
? nestedFunction.name
46+
: undefined;
47+
if (!name) continue;
48+
const namespace = typeof spec.namespace === "string" ? spec.namespace : undefined;
49+
if (namespace !== "functions" && namespace === name) {
50+
if (spec.type === "custom") sameNameNamespacedCustomNames.add(name);
51+
else if (spec.type === "function") sameNameNamespacedFunctionNames.add(name);
52+
}
53+
if (!namespace || namespace === "functions") {
54+
if (spec.type === "custom") bareCustomNames.add(name);
55+
else if (spec.type === "function") bareFunctionNames.add(name);
56+
}
57+
}
58+
}
59+
60+
/** Bare custom tools authorized by this turn, scoped to each response call type. */
61+
export function collectSelfNamedNamespaceScrubAuthorization(
62+
body: unknown,
63+
authorizedBareCustomToolNames: ReadonlySet<string>,
64+
authorizedBareFunctionToolNames: ReadonlySet<string>,
65+
): SelfNamedNamespaceScrubAuthorization {
66+
const bareCustomNames = new Set<string>();
67+
const bareFunctionNames = new Set<string>();
68+
const sameNameNamespacedCustomNames = new Set<string>();
69+
const sameNameNamespacedFunctionNames = new Set<string>();
70+
if (isPlainObject(body)) {
71+
collectBareToolSpecs(
72+
bareCustomNames,
73+
bareFunctionNames,
74+
sameNameNamespacedCustomNames,
75+
sameNameNamespacedFunctionNames,
76+
body.tools,
77+
);
78+
if (Array.isArray(body.input)) {
79+
for (const item of body.input) {
80+
if (
81+
isPlainObject(item)
82+
&& (item.type === "additional_tools" || item.type === "tool_search_output")
83+
) {
84+
collectBareToolSpecs(
85+
bareCustomNames,
86+
bareFunctionNames,
87+
sameNameNamespacedCustomNames,
88+
sameNameNamespacedFunctionNames,
89+
item.tools,
90+
);
91+
}
92+
}
93+
}
94+
}
95+
const authorizedCustomNames = [...bareCustomNames]
96+
.filter(name => authorizedBareCustomToolNames.has(name));
97+
const authorizedFunctionNames = new Set([
98+
...authorizedCustomNames,
99+
...[...bareFunctionNames].filter(name => authorizedBareFunctionToolNames.has(name)),
100+
]);
101+
return {
102+
customToolCallNames: new Set(
103+
authorizedCustomNames.filter(name => !sameNameNamespacedCustomNames.has(name)),
104+
),
105+
functionCallNames: new Set(
106+
[...authorizedFunctionNames].filter(name => !sameNameNamespacedFunctionNames.has(name)),
107+
),
108+
};
109+
}
110+
7111
/**
8112
* Drop a tool-call `namespace` that merely repeats the call's own `name` (#3217).
9113
*
10114
* codex-rs resolves a client tool call as `ToolName::new(namespace, name)` and treats only
11115
* `None | "" | "functions"` as the default namespace; anything else is concatenated into a flat
12116
* name before routing. A backend answer of `{ name: "exec", namespace: "exec" }` therefore
13117
* becomes `execexec`, which no client tool matches, and Codex re-issues the same call forever.
14-
* That shape is never a legitimate identity — an MCP namespace is a server name, not the tool —
15-
* so it is safe to scrub without consulting the declared catalog. The adapter fix that stops
16-
* provoking the answer lives in `stripSparkCompatibility`; this is the belt to that suspender.
118+
* The malformed Spark shape is scrubbed only when the current turn authorized a bare custom tool
119+
* with that name. A genuine namespaced tool may intentionally use the same namespace and name.
120+
* The adapter fix that stops provoking the answer lives in `stripSparkCompatibility`; this is the
121+
* belt to that suspender.
17122
*/
18-
export function scrubSelfNamedToolCallNamespace(value: unknown): { value: unknown; changed: boolean } {
123+
export function scrubSelfNamedToolCallNamespace(
124+
value: unknown,
125+
authorization: SelfNamedNamespaceScrubAuthorization,
126+
): { value: unknown; changed: boolean } {
19127
if (Array.isArray(value)) {
20128
let changed = false;
21129
const out = value.map(entry => {
22-
const result = scrubSelfNamedToolCallNamespace(entry);
130+
const result = scrubSelfNamedToolCallNamespace(entry, authorization);
23131
changed ||= result.changed;
24132
return result.value;
25133
});
@@ -29,35 +137,45 @@ export function scrubSelfNamedToolCallNamespace(value: unknown): { value: unknow
29137
let changed = false;
30138
const out: Record<string, unknown> = {};
31139
for (const [key, entry] of Object.entries(value)) {
32-
const result = scrubSelfNamedToolCallNamespace(entry);
140+
const result = scrubSelfNamedToolCallNamespace(entry, authorization);
33141
out[key] = result.value;
34142
changed ||= result.changed;
35143
}
144+
const authorizedNames = value.type === "custom_tool_call"
145+
? authorization.customToolCallNames
146+
: value.type === "function_call"
147+
? authorization.functionCallNames
148+
: undefined;
36149
if (
37-
(value.type === "custom_tool_call" || value.type === "function_call")
150+
authorizedNames
38151
&& typeof value.name === "string"
39152
&& value.name.length > 0
40153
&& value.namespace === value.name
154+
&& authorizedNames.has(value.name)
41155
) {
42156
delete out.namespace;
43157
changed = true;
44158
}
45159
return changed ? { value: out, changed: true } : { value, changed: false };
46160
}
47161

48-
export function scrubSelfNamedToolCallNamespaceInJson(text: string): string {
162+
export function scrubSelfNamedToolCallNamespaceInJson(
163+
text: string,
164+
authorization: SelfNamedNamespaceScrubAuthorization,
165+
): string {
49166
if (!text.includes("\"namespace\"")) return text;
50167
let payload: unknown;
51168
try {
52169
payload = JSON.parse(text);
53170
} catch {
54171
return text;
55172
}
56-
const result = scrubSelfNamedToolCallNamespace(payload);
173+
const result = scrubSelfNamedToolCallNamespace(payload, authorization);
57174
return result.changed ? JSON.stringify(result.value) : text;
58175
}
59176

60-
export function createSelfNamedToolCallNamespaceScrubRewrite(): SsePayloadRewrite {
61-
return payload => scrubSelfNamedToolCallNamespaceInJson(payload);
177+
export function createSelfNamedToolCallNamespaceScrubRewrite(
178+
authorization: SelfNamedNamespaceScrubAuthorization,
179+
): SsePayloadRewrite {
180+
return payload => scrubSelfNamedToolCallNamespaceInJson(payload, authorization);
62181
}
63-

‎src/server/responses/collaboration.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,16 @@ export function buildToolBridgeMaps(parsed: OcxParsedRequest, budget?: Translato
108108
/** Declared parameter schema per request-visible tool name (#1611 integer repair). */
109109
toolParameterSchemas: Map<string, Record<string, unknown>>;
110110
freeformToolNames: Set<string>;
111+
bareCustomToolNames: Set<string>;
112+
bareFunctionToolNames: Set<string>;
111113
toolSearchToolNames: Set<string>;
112114
} {
113115
const toolNsMap = new Map<string, { namespace: string; name: string; freeform?: true }>();
114116
const declaredToolNames = new Set<string>();
115117
const toolParameterSchemas = new Map<string, Record<string, unknown>>();
116118
const freeformToolNames = new Set<string>();
119+
const bareCustomToolNames = new Set<string>();
120+
const bareFunctionToolNames = new Set<string>();
117121
const toolSearchToolNames = new Set<string>();
118122
const requestedTools = parsed.context.tools ?? [];
119123
const toolAllowed = toolChoiceToolPredicate(parsed.options.toolChoice, requestedTools);
@@ -133,6 +137,19 @@ export function buildToolBridgeMaps(parsed: OcxParsedRequest, budget?: Translato
133137
if (t.freeform) {
134138
budget?.chargeRetained(new TextEncoder().encode(t.name).byteLength, { kind: "retained_collectors" });
135139
freeformToolNames.add(t.name);
140+
if (!t.namespace || t.namespace === "functions") {
141+
budget?.chargeRetained(new TextEncoder().encode(t.name).byteLength, { kind: "retained_collectors" });
142+
bareCustomToolNames.add(t.name);
143+
}
144+
} else if (
145+
!t.toolSearch
146+
&& !t.webSearch
147+
&& !t.imageGeneration
148+
&& !t.videoGeneration
149+
&& (!t.namespace || t.namespace === "functions")
150+
) {
151+
budget?.chargeRetained(new TextEncoder().encode(t.name).byteLength, { kind: "retained_collectors" });
152+
bareFunctionToolNames.add(t.name);
136153
}
137154
if (t.toolSearch) {
138155
budget?.chargeRetained(new TextEncoder().encode(t.name).byteLength, { kind: "retained_collectors" });
@@ -162,7 +179,15 @@ export function buildToolBridgeMaps(parsed: OcxParsedRequest, budget?: Translato
162179
toolParameterSchemas.set(t.name, t.parameters);
163180
}
164181
}
165-
return { toolNsMap, declaredToolNames, toolParameterSchemas, freeformToolNames, toolSearchToolNames };
182+
return {
183+
toolNsMap,
184+
declaredToolNames,
185+
toolParameterSchemas,
186+
freeformToolNames,
187+
bareCustomToolNames,
188+
bareFunctionToolNames,
189+
toolSearchToolNames,
190+
};
166191
}
167192

168193

‎src/server/responses/core.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ import {
327327
} from "../responses-image-gen-repair";
328328
import { createResponsesModelPayloadRewrite, rewriteResponsesModelJson } from "../responses-model-rewrite";
329329
import {
330+
collectSelfNamedNamespaceScrubAuthorization,
330331
createSelfNamedToolCallNamespaceScrubRewrite,
331332
scrubSelfNamedToolCallNamespaceInJson,
332333
} from "../responses-self-named-namespace-scrub";
@@ -3661,6 +3662,11 @@ async function handleResponsesInner(
36613662
parsed._rawBody,
36623663
replayedInputPrefixLength,
36633664
);
3665+
const selfNamedNamespaceScrubAuthorization = collectSelfNamedNamespaceScrubAuthorization(
3666+
clientToolAuthorizationBody,
3667+
toolBridgeMaps.bareCustomToolNames,
3668+
toolBridgeMaps.bareFunctionToolNames,
3669+
);
36643670
const clientExplicitWireToolCatalog = hasExplicitWireToolCatalog(clientToolAuthorizationBody);
36653671
const clientDeclaredWireToolNames = collectDeclaredWireToolNames(clientToolAuthorizationBody);
36663672
const clientDeclaredNamelessCallTypes = collectDeclaredNamelessClientCallTypes(
@@ -4646,7 +4652,7 @@ async function handleResponsesInner(
46464652
const payloadRewrites = [
46474653
createImageGenCallRestoreRewrite(imageGenCallAliases),
46484654
// #3217: a call whose namespace repeats its own name is unroutable in codex-rs.
4649-
createSelfNamedToolCallNamespaceScrubRewrite(),
4655+
createSelfNamedToolCallNamespaceScrubRewrite(selfNamedNamespaceScrubAuthorization),
46504656
routedNamespaceToolAliases.size > 0
46514657
? createRoutedNamespaceCallRestoreRewrite(routedNamespaceToolAliases)
46524658
: undefined,
@@ -4879,7 +4885,10 @@ async function handleResponsesInner(
48794885
inspectResponseLogJson(logCtx, text);
48804886
const clientJson = (() => {
48814887
const restoredNamespace = restoreRoutedNamespaceCallsInJson(
4882-
scrubSelfNamedToolCallNamespaceInJson(restoreImageGenCallsInJson(text, imageGenCallAliases)),
4888+
scrubSelfNamedToolCallNamespaceInJson(
4889+
restoreImageGenCallsInJson(text, imageGenCallAliases),
4890+
selfNamedNamespaceScrubAuthorization,
4891+
),
48834892
routedNamespaceToolAliases,
48844893
);
48854894
const restoredAuthorizedBareNamespace = restoreRoutedNamespaceCallsInJson(

0 commit comments

Comments
 (0)