|
| 1 | +import { mkdtempSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { describe, expect, test } from "bun:test"; |
| 5 | + |
| 6 | +import type { AgentTool } from "@intx/agent"; |
| 7 | +import { CATALOG_TOOL_NAMES, CORE_TOOL_NAMES } from "./tool-search.js"; |
| 8 | +import { |
| 9 | + createReadArchiveTool, |
| 10 | + createSearchArchiveTool, |
| 11 | + readArchiveDefinition, |
| 12 | + searchArchiveDefinition, |
| 13 | +} from "./archive-tools.js"; |
| 14 | +import { formatArchiveRef } from "../session/archive-uri.js"; |
| 15 | +import { createCompactionArchive, type CompactionArchive } from "../session/compaction-archive.js"; |
| 16 | + |
| 17 | +function call(tool: AgentTool, args: Record<string, unknown>): Promise<string> { |
| 18 | + if (tool.kind !== "string") throw new Error("expected string tool"); |
| 19 | + return tool.handler(args, new AbortController().signal); |
| 20 | +} |
| 21 | + |
| 22 | +function memoryArchive(sessionId: string): CompactionArchive { |
| 23 | + const dir = mkdtempSync(join(tmpdir(), "archive-tools-")); |
| 24 | + const blobs = new Map<string, Uint8Array>(); |
| 25 | + return createCompactionArchive({ |
| 26 | + sessionId, |
| 27 | + contextDir: dir, |
| 28 | + writeBlob: async (key, bytes) => { |
| 29 | + blobs.set(key, bytes); |
| 30 | + }, |
| 31 | + readBlob: async (key) => { |
| 32 | + const bytes = blobs.get(key); |
| 33 | + if (bytes === undefined) throw new Error(`missing blob ${key}`); |
| 34 | + return bytes; |
| 35 | + }, |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +function wrapReads(archive: CompactionArchive): string[] { |
| 40 | + const ids: string[] = []; |
| 41 | + const orig = archive.readAuthorizedPayload.bind(archive); |
| 42 | + archive.readAuthorizedPayload = async (occurrenceId) => { |
| 43 | + ids.push(occurrenceId); |
| 44 | + return orig(occurrenceId); |
| 45 | + }; |
| 46 | + return ids; |
| 47 | +} |
| 48 | + |
| 49 | +describe("archive tool definitions", () => { |
| 50 | + test("catalog advertises search_archive and read_archive; they are not CORE", () => { |
| 51 | + expect(CORE_TOOL_NAMES).not.toContain("search_archive"); |
| 52 | + expect(CORE_TOOL_NAMES).not.toContain("read_archive"); |
| 53 | + expect(CATALOG_TOOL_NAMES).toContain("search_archive"); |
| 54 | + expect(CATALOG_TOOL_NAMES).toContain("read_archive"); |
| 55 | + expect(searchArchiveDefinition.description).toContain("archive:///"); |
| 56 | + expect(readArchiveDefinition.description).toContain("archive:///"); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("search_archive and read_archive", () => { |
| 61 | + test("search returns archive:/// refs and read returns the payload", async () => { |
| 62 | + const archive = memoryArchive("sess-primary"); |
| 63 | + const occ = await archive.recordAuthorizedPayload({ |
| 64 | + kind: "user_message", |
| 65 | + payload: "unique-payload-alpha", |
| 66 | + provenance: "primary-admission", |
| 67 | + }); |
| 68 | + const search = createSearchArchiveTool(() => archive); |
| 69 | + const read = createReadArchiveTool(() => archive); |
| 70 | + |
| 71 | + const hits = await call(search, { query: "unique-payload-alpha" }); |
| 72 | + expect(hits).toContain(formatArchiveRef(occ.occurrenceId)); |
| 73 | + expect(hits).toContain("user_message"); |
| 74 | + expect(hits).not.toContain(occ.sessionId); |
| 75 | + expect(hits).not.toContain(occ.blobKey); |
| 76 | + |
| 77 | + const body = await call(read, { ref: formatArchiveRef(occ.occurrenceId) }); |
| 78 | + expect(body).toContain("unique-payload-alpha"); |
| 79 | + expect(body).not.toContain(occ.blobKey); |
| 80 | + }); |
| 81 | + |
| 82 | + test("gap rows match metadata only and never load payload", async () => { |
| 83 | + const archive = memoryArchive("sess-gap"); |
| 84 | + const reads = wrapReads(archive); |
| 85 | + const gap = await archive.recordAuthorizedPayload({ |
| 86 | + kind: "attachment", |
| 87 | + payload: { secret: "gap-payload-must-not-search" }, |
| 88 | + provenance: "primary-admission:attachment-missing", |
| 89 | + gap: true, |
| 90 | + }); |
| 91 | + const search = createSearchArchiveTool(() => archive); |
| 92 | + const read = createReadArchiveTool(() => archive); |
| 93 | + |
| 94 | + const payloadHits = await call(search, { query: "gap-payload-must-not-search" }); |
| 95 | + expect(payloadHits).toContain("No evidence-archive occurrences matched"); |
| 96 | + expect(reads).toEqual([]); |
| 97 | + |
| 98 | + const metaHits = await call(search, { query: "attachment-missing" }); |
| 99 | + expect(metaHits).toContain(formatArchiveRef(gap.occurrenceId)); |
| 100 | + expect(metaHits).toContain("gap"); |
| 101 | + expect(reads).toEqual([]); |
| 102 | + |
| 103 | + const body = await call(read, { ref: formatArchiveRef(gap.occurrenceId) }); |
| 104 | + expect(body).toContain("explicit gap"); |
| 105 | + expect(reads).toEqual([gap.occurrenceId]); |
| 106 | + }); |
| 107 | + |
| 108 | + test("forged and other-session refs are not found", async () => { |
| 109 | + const primary = memoryArchive("sess-a"); |
| 110 | + const other = memoryArchive("sess-b"); |
| 111 | + const foreign = await other.recordAuthorizedPayload({ |
| 112 | + kind: "assistant_text", |
| 113 | + payload: "other-session-only", |
| 114 | + }); |
| 115 | + const read = createReadArchiveTool(() => primary); |
| 116 | + const search = createSearchArchiveTool(() => primary); |
| 117 | + |
| 118 | + const forged = await call(read, { ref: "archive:///occ-forged-not-in-index" }); |
| 119 | + expect(forged).toContain("unknown occurrence"); |
| 120 | + |
| 121 | + const cross = await call(read, { ref: formatArchiveRef(foreign.occurrenceId) }); |
| 122 | + expect(cross).toContain("unknown occurrence"); |
| 123 | + |
| 124 | + const hits = await call(search, { query: "other-session-only" }); |
| 125 | + expect(hits).toContain("No evidence-archive occurrences matched"); |
| 126 | + }); |
| 127 | + |
| 128 | + test("rejects sessionId, path, and blobKey locators", async () => { |
| 129 | + const archive = memoryArchive("sess-locators"); |
| 130 | + const occ = await archive.recordAuthorizedPayload({ |
| 131 | + kind: "user_message", |
| 132 | + payload: "locator-payload", |
| 133 | + }); |
| 134 | + const search = createSearchArchiveTool(() => archive); |
| 135 | + const read = createReadArchiveTool(() => archive); |
| 136 | + const ref = formatArchiveRef(occ.occurrenceId); |
| 137 | + |
| 138 | + for (const args of [ |
| 139 | + { query: "locator-payload", sessionId: "sess-locators" }, |
| 140 | + { query: "locator-payload", path: "evidence-archive/index.jsonl" }, |
| 141 | + { query: "locator-payload", blobKey: occ.blobKey }, |
| 142 | + ]) { |
| 143 | + const out = await call(search, args); |
| 144 | + expect(out).toContain("does not accept sessionId, path, or blobKey"); |
| 145 | + } |
| 146 | + |
| 147 | + for (const args of [ |
| 148 | + { ref, sessionId: "sess-locators" }, |
| 149 | + { ref, path: "evidence-archive/index.jsonl" }, |
| 150 | + { ref, blobKey: occ.blobKey }, |
| 151 | + ]) { |
| 152 | + const out = await call(read, args); |
| 153 | + expect(out).toContain("does not accept sessionId, path, or blobKey"); |
| 154 | + } |
| 155 | + |
| 156 | + const badRef = await call(read, { ref: occ.occurrenceId }); |
| 157 | + expect(badRef).toContain("archive:///{occurrenceId}"); |
| 158 | + }); |
| 159 | + |
| 160 | + test("read_archive pages with offset and limit via readBytesBounded", async () => { |
| 161 | + const archive = memoryArchive("sess-page"); |
| 162 | + const lines = Array.from({ length: 8 }, (_, i) => `archive-line-${i}`).join("\n"); |
| 163 | + const occ = await archive.recordAuthorizedPayload({ |
| 164 | + kind: "tool_result", |
| 165 | + payload: lines, |
| 166 | + }); |
| 167 | + const read = createReadArchiveTool(() => archive); |
| 168 | + const body = await call(read, { |
| 169 | + ref: formatArchiveRef(occ.occurrenceId), |
| 170 | + offset: 2, |
| 171 | + limit: 2, |
| 172 | + }); |
| 173 | + expect(body).toContain("archive-line-2"); |
| 174 | + expect(body).toContain("archive-line-3"); |
| 175 | + expect(body).not.toContain("archive-line-0"); |
| 176 | + expect(body).not.toContain("archive-line-4"); |
| 177 | + expect(body).toContain("Use offset="); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +describe("createAgentToolset archive mount", () => { |
| 182 | + test("mounts search_archive and read_archive only when getEvidenceArchive is set", async () => { |
| 183 | + const cwd = mkdtempSync(join(tmpdir(), "corbits-archive-mount-")); |
| 184 | + const { createAgentToolset } = await import("./tools.js"); |
| 185 | + const permissionGate = { |
| 186 | + check: async () => ({ allowed: true }), |
| 187 | + getSkipPermissions: () => false, |
| 188 | + } as never; |
| 189 | + |
| 190 | + const worker = await createAgentToolset({ |
| 191 | + cwd, |
| 192 | + permissionGate, |
| 193 | + onOperatorGate: async () => ({ kind: "option", index: 0 }), |
| 194 | + }); |
| 195 | + const workerNames = worker.dynamicRunner.currentDefinitions().map((d) => d.name); |
| 196 | + expect(workerNames).not.toContain("search_archive"); |
| 197 | + expect(workerNames).not.toContain("read_archive"); |
| 198 | + await worker.dispose(); |
| 199 | + |
| 200 | + const primary = await createAgentToolset({ |
| 201 | + cwd, |
| 202 | + permissionGate, |
| 203 | + onOperatorGate: async () => ({ kind: "option", index: 0 }), |
| 204 | + getEvidenceArchive: () => undefined, |
| 205 | + }); |
| 206 | + const primaryNames = primary.dynamicRunner.currentDefinitions().map((d) => d.name); |
| 207 | + expect(primaryNames).toContain("search_archive"); |
| 208 | + expect(primaryNames).toContain("read_archive"); |
| 209 | + await primary.dispose(); |
| 210 | + }); |
| 211 | +}); |
0 commit comments