|
| 1 | +/** |
| 2 | + * #3217 — a custom_tool_call whose `namespace` repeats its own `name` must not reach Codex. |
| 3 | + * |
| 4 | + * codex-rs resolves `ToolName::new(namespace, name)` and only treats None/""/"functions" as the |
| 5 | + * default namespace; `{ name: "exec", namespace: "exec" }` becomes the flat name `execexec`, |
| 6 | + * which no client tool matches, and Codex re-issues the call every turn. The adapter fix keeps |
| 7 | + * the reserved `functions` group intact so the backend stops answering that way; this scrub is |
| 8 | + * the belt to that suspender on the client-facing passthrough (SSE and bounded JSON). |
| 9 | + */ |
| 10 | +import { afterEach, expect, test } from "bun:test"; |
| 11 | +import { handleResponses } from "../src/server/responses"; |
| 12 | +import { scrubSelfNamedToolCallNamespace } from "../src/server/responses-self-named-namespace-scrub"; |
| 13 | +import type { OcxConfig } from "../src/types"; |
| 14 | + |
| 15 | +const originalFetch = globalThis.fetch; |
| 16 | +afterEach(() => { globalThis.fetch = originalFetch; }); |
| 17 | + |
| 18 | +function forwardConfig(): OcxConfig { |
| 19 | + return { |
| 20 | + port: 0, |
| 21 | + defaultProvider: "openai", |
| 22 | + providers: { |
| 23 | + openai: { |
| 24 | + adapter: "openai-responses", |
| 25 | + baseUrl: "https://chatgpt.com/backend-api/codex", |
| 26 | + authMode: "forward", |
| 27 | + codexAccountMode: "direct", |
| 28 | + }, |
| 29 | + }, |
| 30 | + } as unknown as OcxConfig; |
| 31 | +} |
| 32 | + |
| 33 | +const requestBody = { |
| 34 | + model: "gpt-5.3-codex-spark", |
| 35 | + stream: true, |
| 36 | + store: false, |
| 37 | + instructions: "x", |
| 38 | + input: [ |
| 39 | + { |
| 40 | + type: "additional_tools", |
| 41 | + role: "developer", |
| 42 | + tools: [{ |
| 43 | + type: "namespace", |
| 44 | + name: "functions", |
| 45 | + tools: [ |
| 46 | + { type: "custom", name: "exec", description: "shell" }, |
| 47 | + { type: "function", name: "wait", parameters: { type: "object", properties: {} } }, |
| 48 | + ], |
| 49 | + }], |
| 50 | + }, |
| 51 | + { type: "message", role: "user", content: [{ type: "input_text", text: "run pwd" }] }, |
| 52 | + ], |
| 53 | +}; |
| 54 | + |
| 55 | +function sseFrom(items: Array<Record<string, unknown>>): string { |
| 56 | + const events = [ |
| 57 | + { type: "response.output_item.added", output_index: 0, item: { ...items[0], input: "", status: "in_progress" } }, |
| 58 | + { type: "response.output_item.done", output_index: 0, item: items[0] }, |
| 59 | + { type: "response.completed", response: { id: "r1", status: "completed", output: items } }, |
| 60 | + ]; |
| 61 | + return events.map(e => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join(""); |
| 62 | +} |
| 63 | + |
| 64 | +function request(): Request { |
| 65 | + return new Request("http://localhost/v1/responses", { |
| 66 | + method: "POST", |
| 67 | + headers: { "content-type": "application/json", authorization: "Bearer test", "chatgpt-account-id": "acct" }, |
| 68 | + body: JSON.stringify(requestBody), |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +test("a self-named namespace on a passthrough custom_tool_call is scrubbed before the client (#3217)", async () => { |
| 73 | + const call = { type: "custom_tool_call", id: "ctc_1", call_id: "call_1", name: "exec", namespace: "exec", input: "pwd", status: "completed" }; |
| 74 | + globalThis.fetch = (async () => new Response(sseFrom([call]), { |
| 75 | + status: 200, headers: { "content-type": "text/event-stream" }, |
| 76 | + })) as typeof fetch; |
| 77 | + |
| 78 | + const res = await handleResponses(request(), forwardConfig(), { model: "", provider: "" }); |
| 79 | + expect(res.status).toBe(200); |
| 80 | + const text = await res.text(); |
| 81 | + const payloads = text.split("\n").filter(l => l.startsWith("data: ") && l !== "data: [DONE]").map(l => JSON.parse(l.slice(6)) as Record<string, unknown>); |
| 82 | + const callItems = payloads.flatMap(p => { |
| 83 | + const item = p.item as Record<string, unknown> | undefined; |
| 84 | + const output = (p.response as { output?: Array<Record<string, unknown>> } | undefined)?.output ?? []; |
| 85 | + return [...(item ? [item] : []), ...output]; |
| 86 | + }).filter(i => i.type === "custom_tool_call"); |
| 87 | + expect(callItems.length).toBeGreaterThanOrEqual(3); |
| 88 | + for (const item of callItems) { |
| 89 | + expect(item.name).toBe("exec"); |
| 90 | + expect("namespace" in item).toBe(false); |
| 91 | + } |
| 92 | +}); |
| 93 | + |
| 94 | +test("a genuine MCP namespace on a passthrough call is left alone", async () => { |
| 95 | + const call = { type: "function_call", id: "fc_1", call_id: "call_1", name: "search", namespace: "mcp__docs", arguments: "{}", status: "completed" }; |
| 96 | + globalThis.fetch = (async () => new Response(sseFrom([call]), { |
| 97 | + status: 200, headers: { "content-type": "text/event-stream" }, |
| 98 | + })) as typeof fetch; |
| 99 | + |
| 100 | + const res = await handleResponses(request(), forwardConfig(), { model: "", provider: "" }); |
| 101 | + const text = await res.text(); |
| 102 | + expect(text).toContain('"namespace":"mcp__docs"'); |
| 103 | +}); |
| 104 | + |
| 105 | +test("the bounded JSON (stream:false) passthrough path scrubs the same shape (#3217)", async () => { |
| 106 | + const call = { type: "custom_tool_call", id: "ctc_1", call_id: "call_1", name: "exec", namespace: "exec", input: "pwd", status: "completed" }; |
| 107 | + globalThis.fetch = (async () => new Response(JSON.stringify({ id: "r1", object: "response", status: "completed", output: [call] }), { |
| 108 | + status: 200, headers: { "content-type": "application/json" }, |
| 109 | + })) as typeof fetch; |
| 110 | + |
| 111 | + const req = new Request("http://localhost/v1/responses", { |
| 112 | + method: "POST", |
| 113 | + headers: { "content-type": "application/json", authorization: "Bearer test", "chatgpt-account-id": "acct" }, |
| 114 | + body: JSON.stringify({ ...requestBody, stream: false }), |
| 115 | + }); |
| 116 | + const res = await handleResponses(req, forwardConfig(), { model: "", provider: "" }); |
| 117 | + expect(res.status).toBe(200); |
| 118 | + const body = await res.json() as { output: Array<Record<string, unknown>> }; |
| 119 | + expect(body.output[0]).toMatchObject({ type: "custom_tool_call", name: "exec" }); |
| 120 | + expect("namespace" in body.output[0]).toBe(false); |
| 121 | +}); |
| 122 | + |
| 123 | +test("scrub is recursive, shape-preserving, and a no-op on clean payloads", () => { |
| 124 | + const clean = { type: "response.completed", response: { output: [{ type: "custom_tool_call", name: "exec", input: "" }] } }; |
| 125 | + expect(scrubSelfNamedToolCallNamespace(clean)).toEqual({ value: clean, changed: false }); |
| 126 | + const dirty = { response: { output: [{ type: "function_call", name: "wait", namespace: "wait", arguments: "{}" }, { type: "message" }] } }; |
| 127 | + const result = scrubSelfNamedToolCallNamespace(dirty); |
| 128 | + expect(result.changed).toBe(true); |
| 129 | + expect(result.value).toEqual({ response: { output: [{ type: "function_call", name: "wait", arguments: "{}" }, { type: "message" }] } }); |
| 130 | + // An empty name never matches: a namespace equal to "" is not the self-named shape. |
| 131 | + expect(scrubSelfNamedToolCallNamespace({ type: "custom_tool_call", name: "", namespace: "" }).changed).toBe(false); |
| 132 | +}); |
0 commit comments