@@ -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-
0 commit comments