|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { bridgeToResponsesSSE } from "../src/bridge"; |
| 3 | +import type { AdapterEvent } from "../src/types"; |
| 4 | + |
| 5 | +async function drain(stream: ReadableStream<Uint8Array>): Promise<string> { |
| 6 | + const reader = stream.getReader(); |
| 7 | + const decoder = new TextDecoder(); |
| 8 | + let out = ""; |
| 9 | + for (;;) { |
| 10 | + const { done, value } = await reader.read(); |
| 11 | + if (done) break; |
| 12 | + out += decoder.decode(value, { stream: true }); |
| 13 | + } |
| 14 | + return out; |
| 15 | +} |
| 16 | + |
| 17 | +async function* toolTurn(name: string): AsyncGenerator<AdapterEvent> { |
| 18 | + yield { type: "tool_call_start", id: "call-1", name } as AdapterEvent; |
| 19 | + yield { type: "tool_call_delta", id: "call-1", delta: '{"cmd":"ls"}' } as AdapterEvent; |
| 20 | + yield { type: "tool_call_end", id: "call-1" } as AdapterEvent; |
| 21 | + yield { type: "done" } as AdapterEvent; |
| 22 | +} |
| 23 | + |
| 24 | +// #2493: Codex 0.149 declares the shell tool as `exec`, whose own description names the |
| 25 | +// nested `tools.exec_command(...)` helper. Routed models echo the helper name back, and the |
| 26 | +// undeclared-tool guard turned that into a 502 mid-turn. These pin the SSE path the guard |
| 27 | +// actually runs on, which the review flagged as untested. |
| 28 | +describe("bridge normalizes legacy shell names against the declared catalog (#2493)", () => { |
| 29 | + test("exec_command is delivered as the declared exec instead of failing the turn", async () => { |
| 30 | + const sse = await drain(bridgeToResponsesSSE( |
| 31 | + toolTurn("exec_command"), "deepseek-x", undefined, undefined, undefined, undefined, 50_000, |
| 32 | + { declaredToolNames: new Set(["exec"]) }, |
| 33 | + )); |
| 34 | + expect(sse).not.toContain("undeclared client tool"); |
| 35 | + expect(sse).toContain('"name":"exec"'); |
| 36 | + }); |
| 37 | + |
| 38 | + test("shell_command normalizes the same way", async () => { |
| 39 | + const sse = await drain(bridgeToResponsesSSE( |
| 40 | + toolTurn("shell_command"), "deepseek-x", undefined, undefined, undefined, undefined, 50_000, |
| 41 | + { declaredToolNames: new Set(["exec"]) }, |
| 42 | + )); |
| 43 | + expect(sse).not.toContain("undeclared client tool"); |
| 44 | + expect(sse).toContain('"name":"exec"'); |
| 45 | + }); |
| 46 | + |
| 47 | + test("a genuinely undeclared tool still fails the turn", async () => { |
| 48 | + const sse = await drain(bridgeToResponsesSSE( |
| 49 | + toolTurn("apply_patch"), "deepseek-x", undefined, undefined, undefined, undefined, 50_000, |
| 50 | + { declaredToolNames: new Set(["exec"]) }, |
| 51 | + )); |
| 52 | + expect(sse).toContain("undeclared client tool"); |
| 53 | + }); |
| 54 | + |
| 55 | + test("a catalog that declares exec_command itself is never rewritten", async () => { |
| 56 | + const sse = await drain(bridgeToResponsesSSE( |
| 57 | + toolTurn("exec_command"), "deepseek-x", undefined, undefined, undefined, undefined, 50_000, |
| 58 | + { declaredToolNames: new Set(["exec", "exec_command"]) }, |
| 59 | + )); |
| 60 | + expect(sse).not.toContain("undeclared client tool"); |
| 61 | + expect(sse).toContain('"name":"exec_command"'); |
| 62 | + }); |
| 63 | +}); |
0 commit comments