|
| 1 | +import { mkdtempSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { describe, expect, test } from "bun:test"; |
| 5 | +import { stringTool } from "@intx/agent"; |
| 6 | + |
| 7 | +import { withAuthorizedArchiveResult } from "./archive-tool-result.js"; |
| 8 | +import { createPermissionGate } from "../permission/gate.js"; |
| 9 | +import { createCompactionArchive, type CompactionArchive } from "../session/compaction-archive.js"; |
| 10 | + |
| 11 | +function memoryArchive(sessionId: string): CompactionArchive { |
| 12 | + const dir = mkdtempSync(join(tmpdir(), "archive-tool-result-")); |
| 13 | + const blobs = new Map<string, Uint8Array>(); |
| 14 | + return createCompactionArchive({ |
| 15 | + sessionId, |
| 16 | + contextDir: dir, |
| 17 | + writeBlob: async (key, bytes) => { |
| 18 | + blobs.set(key, bytes); |
| 19 | + }, |
| 20 | + readBlob: async (key) => { |
| 21 | + const bytes = blobs.get(key); |
| 22 | + if (bytes === undefined) throw new Error(`missing blob ${key}`); |
| 23 | + return bytes; |
| 24 | + }, |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +describe("withAuthorizedArchiveResult", () => { |
| 29 | + test("records string-tool results when an archive is present", async () => { |
| 30 | + const archive = memoryArchive("sess-agent"); |
| 31 | + const tool = withAuthorizedArchiveResult( |
| 32 | + stringTool({ |
| 33 | + definition: { |
| 34 | + name: "manage_tasks", |
| 35 | + description: "tasks", |
| 36 | + inputSchema: { type: "object", properties: {} }, |
| 37 | + }, |
| 38 | + handler: async () => "listed 1 task", |
| 39 | + }), |
| 40 | + () => archive, |
| 41 | + ); |
| 42 | + expect(tool.kind).toBe("full"); |
| 43 | + if (tool.kind !== "full") return; |
| 44 | + const result = await tool.handler( |
| 45 | + { id: "call-mt", name: "manage_tasks", arguments: { action: "create", tasks: [] } }, |
| 46 | + new AbortController().signal, |
| 47 | + ); |
| 48 | + expect(result.content).toBe("listed 1 task"); |
| 49 | + const occs = await archive.listOccurrences(); |
| 50 | + const hits = occs.filter((occ) => occ.kind === "tool_result" && occ.callId === "call-mt"); |
| 51 | + expect(hits).toHaveLength(1); |
| 52 | + const recorded = hits[0]; |
| 53 | + expect(recorded?.provenance).toBe("agent:post-policy"); |
| 54 | + expect(recorded).toBeDefined(); |
| 55 | + if (recorded === undefined) return; |
| 56 | + expect(await archive.readAuthorizedPayload(recorded.occurrenceId)).toContain("listed 1 task"); |
| 57 | + }); |
| 58 | + |
| 59 | + test("does not record when the archive getter is omitted", async () => { |
| 60 | + const inner = stringTool({ |
| 61 | + definition: { |
| 62 | + name: "use_skill", |
| 63 | + description: "skill", |
| 64 | + inputSchema: { type: "object", properties: {} }, |
| 65 | + }, |
| 66 | + handler: async () => "ok", |
| 67 | + }); |
| 68 | + expect(withAuthorizedArchiveResult(inner, undefined)).toBe(inner); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("createAgentToolset authorized result capture", () => { |
| 73 | + test("records manage_tasks and does not double-record posix read_file", async () => { |
| 74 | + const cwd = mkdtempSync(join(tmpdir(), "corbits-archive-capture-")); |
| 75 | + writeFileSync(join(cwd, "note.txt"), "hello"); |
| 76 | + const archive = memoryArchive("sess-capture"); |
| 77 | + const { createAgentToolset } = await import("./tools.js"); |
| 78 | + const permissionGate = createPermissionGate({ |
| 79 | + approvals: [], |
| 80 | + interactive: false, |
| 81 | + skipPermissions: true, |
| 82 | + reactorGated: false, |
| 83 | + cwd, |
| 84 | + }); |
| 85 | + const toolset = await createAgentToolset({ |
| 86 | + cwd, |
| 87 | + permissionGate, |
| 88 | + onOperatorGate: async () => ({ kind: "option", index: 0 }), |
| 89 | + getEvidenceArchive: () => archive, |
| 90 | + }); |
| 91 | + try { |
| 92 | + const tasks = await toolset.dynamicRunner.run( |
| 93 | + { |
| 94 | + id: "call-mt", |
| 95 | + name: "manage_tasks", |
| 96 | + arguments: { |
| 97 | + action: "create", |
| 98 | + tasks: [{ id: "t1", title: "one" }], |
| 99 | + }, |
| 100 | + }, |
| 101 | + new AbortController().signal, |
| 102 | + ); |
| 103 | + expect(tasks.isError).toBeFalsy(); |
| 104 | + const posix = await toolset.dynamicRunner.run( |
| 105 | + { |
| 106 | + id: "call-rf", |
| 107 | + name: "read_file", |
| 108 | + arguments: { path: join(cwd, "note.txt") }, |
| 109 | + }, |
| 110 | + new AbortController().signal, |
| 111 | + ); |
| 112 | + expect(posix.isError).toBeFalsy(); |
| 113 | + const occs = await archive.listOccurrences(); |
| 114 | + const mt = occs.filter((occ) => occ.kind === "tool_result" && occ.callId === "call-mt"); |
| 115 | + const rf = occs.filter((occ) => occ.kind === "tool_result" && occ.callId === "call-rf"); |
| 116 | + expect(mt).toHaveLength(1); |
| 117 | + expect(mt[0]?.provenance).toBe("agent:post-policy"); |
| 118 | + expect(rf).toHaveLength(1); |
| 119 | + expect(rf[0]?.provenance).toBe("posix:post-policy-pre-truncation"); |
| 120 | + } finally { |
| 121 | + await toolset.dispose(); |
| 122 | + } |
| 123 | + }); |
| 124 | +}); |
0 commit comments