|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createToolRunner } from "@intx/agent"; |
| 3 | +import { createBlobReader, type ToolCall, type ToolResult } from "@intx/types/runtime"; |
| 4 | + |
| 5 | +import { createCodexToolProxies, type CodexRunManageTasks } from "../agent/codex-tool-proxies.js"; |
| 6 | +import { |
| 7 | + MAX_RESULT_CHARS, |
| 8 | + truncateToolResultContent, |
| 9 | +} from "../plugins/result-truncation-plugin.js"; |
| 10 | +import { createCodexProxyRunTool } from "./run.js"; |
| 11 | + |
| 12 | +function fakeBlobStore() { |
| 13 | + const blobs = new Map<string, { bytes: Uint8Array; contentType: string }>(); |
| 14 | + return { |
| 15 | + blobs, |
| 16 | + writeBlob: async (key: string, bytes: Uint8Array, contentType: string) => { |
| 17 | + blobs.set(key, { bytes, contentType }); |
| 18 | + }, |
| 19 | + readBlob: async (key: string) => { |
| 20 | + const entry = blobs.get(key); |
| 21 | + if (entry === undefined) throw new Error(`Blob not found: ${key}`); |
| 22 | + return entry.bytes; |
| 23 | + }, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +const unusedManageTasks: CodexRunManageTasks = async () => ({ content: "unused" }); |
| 28 | + |
| 29 | +function extractToolOutputURI(content: unknown): string { |
| 30 | + const match = /tool-output:\/\/\/\S+/.exec(String(content)); |
| 31 | + if (match === null) throw new Error(`missing tool-output URI in ${String(content)}`); |
| 32 | + return match[0].replace(/[.\]]+$/, ""); |
| 33 | +} |
| 34 | + |
| 35 | +describe("createCodexProxyRunTool", () => { |
| 36 | + test("oversized proxied shell calls get distinct recoverable spill URIs", async () => { |
| 37 | + const store = fakeBlobStore(); |
| 38 | + const outputs: [string, string] = [ |
| 39 | + `${"a".repeat(MAX_RESULT_CHARS)}FIRST-TAIL`, |
| 40 | + `${"b".repeat(MAX_RESULT_CHARS)}SECOND-TAIL`, |
| 41 | + ]; |
| 42 | + const seenCallIds: string[] = []; |
| 43 | + const posixTools = { |
| 44 | + run: async (call: ToolCall): Promise<ToolResult> => { |
| 45 | + seenCallIds.push(call.id); |
| 46 | + const index = seenCallIds.length - 1; |
| 47 | + return { |
| 48 | + callId: call.id, |
| 49 | + content: await truncateToolResultContent(outputs[index] ?? "", MAX_RESULT_CHARS, { |
| 50 | + callId: call.id, |
| 51 | + writeBlob: store.writeBlob, |
| 52 | + }), |
| 53 | + }; |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + const tools = createCodexToolProxies({ |
| 58 | + isCodex: true, |
| 59 | + runTool: createCodexProxyRunTool(posixTools), |
| 60 | + readRawFile: async () => ({ content: "unused" }), |
| 61 | + runManageTasks: unusedManageTasks, |
| 62 | + }); |
| 63 | + const runner = createToolRunner(tools); |
| 64 | + |
| 65 | + const first = await runner.run( |
| 66 | + { id: "outer-1", name: "shell", arguments: { command: "first" } }, |
| 67 | + new AbortController().signal, |
| 68 | + ); |
| 69 | + const second = await runner.run( |
| 70 | + { id: "outer-2", name: "shell", arguments: { command: "second" } }, |
| 71 | + new AbortController().signal, |
| 72 | + ); |
| 73 | + |
| 74 | + const firstURI = extractToolOutputURI(first.content); |
| 75 | + const secondURI = extractToolOutputURI(second.content); |
| 76 | + expect(firstURI).not.toBe(secondURI); |
| 77 | + expect(seenCallIds).toEqual(["codex-proxy-1", "codex-proxy-2"]); |
| 78 | + |
| 79 | + const reader = createBlobReader(store); |
| 80 | + expect(new TextDecoder().decode(await reader.read(firstURI))).toBe(outputs[0]); |
| 81 | + expect(new TextDecoder().decode(await reader.read(secondURI))).toBe(outputs[1]); |
| 82 | + }); |
| 83 | +}); |
0 commit comments