import { access } from "node:fs/promises"; import { fileURLToPath } from "node:url"; import { fauxAssistantMessage, fauxToolCall } from "@earendil-works/pi-ai"; import { createCodemodeExtension, estimateTokens, initTheme, } from "@earendil-works/pi-coding-agent"; import { Value } from "typebox/value"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; import { statePaths } from "../state.js"; import { BakOutput, BakParameters } from "../tool.js"; const HOST = "12345678-1234-1234-1234-123456789abc"; const SESSION = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; const OTHER_SESSION = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"; const DATE = "2026-01-01T00:00:00.000Z"; const ACCESS = "TEST_ARCHIVE_ACCESS_CREDENTIAL"; const FAKE_CREDENTIAL = "test-secret"; const archiveEnv = { PI_BAK_AUTO_BACKUP: "false", PI_BAK_DEBUG: "0", PI_BAK_S3_ENDPOINT: "https://example.invalid", PI_BAK_S3_BUCKET: "bucket", PI_BAK_S3_ACCESS_KEY_ID: ACCESS, PI_BAK_S3_SECRET_ACCESS_KEY: FAKE_CREDENTIAL, }; const entry = fileURLToPath(new URL("../index.ts", import.meta.url)); let fixture: PiSession | undefined; function fakeStorage() { const objects = new Map([ [ "catalog/hosts.json", JSON.stringify({ version: 1, hosts: [{ hostId: HOST, alias: "desktop" }], }), ], [ `hosts/${HOST}.json`, JSON.stringify({ version: 1, hostId: HOST, alias: "desktop", sessions: [SESSION, OTHER_SESSION].map((id) => ({ id, key: `sessions/${HOST}/${id}.jsonl`, name: `Archive ${id}`, cwd: "/work/project", created: DATE, })), }), ], [ `sessions/${HOST}/${SESSION}.jsonl`, [ { type: "session", version: 3, id: SESSION, timestamp: DATE, cwd: "/work/project", }, { type: "message", id: "one", parentId: null, timestamp: DATE, message: { role: "user", content: `historical ${FAKE_CREDENTIAL}` }, }, ] .map((value) => JSON.stringify(value)) .join("\n"), ], ]); const calls: string[] = []; const fetch = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { const request = new Request(input, init); expect(request.method).toBe("GET"); const key = decodeURIComponent( new URL(request.url).pathname.slice("/bucket/".length), ); calls.push(key); const body = objects.get(key); const range = request.headers.get("range"); if (body !== undefined && range) { const bytes = Buffer.from(body); const match = /^bytes=(\d+)-(\d+)$/.exec(range)!; const start = Number(match[1]), end = Math.min(Number(match[2]), bytes.length - 1); if (start >= bytes.length) return new Response(null, { status: 416 }); return new Response( Uint8Array.from(bytes.subarray(start, end + 1)).buffer, { status: 206, headers: { etag: '"stable"', "content-range": `bytes ${start}-${end}/${bytes.length}`, }, }, ); } return new Response(body ?? "missing", { status: body === undefined ? 404 : 200, }); }); vi.stubGlobal("fetch", fetch); return { objects, calls, fetch }; } async function start(codemode = false) { initTheme(undefined, false); fixture = await createPiSession({ extensionPaths: [entry], extensionFactories: codemode ? [createCodemodeExtension()] : [], userSettings: { bak: { autoBackup: false }, defaultTools: codemode ? ["+codemode"] : ["read", "bash", "edit", "write"], }, env: archiveEnv, }); const tool = fixture.extensions .find((extension) => extension.path === entry) ?.tools.get("bak")?.definition; if (!tool) throw new Error("missing bak tool"); const invoke = ( params: Parameters[1], signal?: AbortSignal, ) => tool.execute( "query-test", params, signal, undefined, fixture!.session.extensionRunner.createToolContext("query-test", signal), ); return { tool, invoke }; } afterEach(async () => { await fixture?.dispose(); fixture = undefined; vi.unstubAllGlobals(); vi.restoreAllMocks(); }); describe("bak agent query tool", () => { it("registers callable codemode exposure and read-only hints without startup I/O", async () => { const storage = fakeStorage(); const { tool } = await start(); expect(tool.exposure).toBe("codemode"); expect(tool.annotations).toEqual({ readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true, }); expect(tool.outputSchema).toBeDefined(); expect(fixture!.session.getActiveToolNames()).not.toContain("bak"); expect(fixture!.session.getCallableToolNames()).toContain("bak"); expect(storage.fetch).not.toHaveBeenCalled(); }); it("measures first-request heuristic context impact without live provider calls", async () => { const storage = fakeStorage(); const measurements: { tokens: number; tools: string[] }[] = []; for (const withBak of [false, true]) { fixture = await createPiSession({ extensionPaths: withBak ? [entry] : [], extensionFactories: [createCodemodeExtension()], userSettings: { bak: { autoBackup: false }, defaultTools: ["+codemode"], }, env: archiveEnv, responses: [ (context) => { measurements.push({ tokens: context.messages.reduce( (total, message) => total + estimateTokens(message), 0, ), tools: context.messages.flatMap((message) => message.role === "system" ? (message.toolsAdded ?? []).map((tool) => tool.name) : [], ), }); return fauxAssistantMessage("OK"); }, ], }); await fixture.session.prompt("Reply only OK. Do not use tools."); expect(fixture.errors).toEqual([]); await fixture.dispose(); fixture = undefined; } expect(measurements).toHaveLength(2); expect(measurements[1]!.tools).toEqual(measurements[0]!.tools); expect(storage.fetch).not.toHaveBeenCalled(); console.info( "Bak isolated first-request SDK heuristic tokens (not provider-reported)", { withoutBak: measurements[0]!.tokens, withBak: measurements[1]!.tokens, delta: measurements[1]!.tokens - measurements[0]!.tokens, }, ); }); it("lists/searches/reads without cache, identity, restore files or remote writes", async () => { const storage = fakeStorage(); const { invoke } = await start(); const listed = await invoke({ action: "list", limit: 1 }); expect(Value.Check(BakOutput, listed.structuredContent)).toBe(true); const searched = await invoke({ action: "search", query: "project" }); expect(Value.Check(BakOutput, searched.structuredContent)).toBe(true); const read = await invoke({ action: "read", hostId: HOST, sessionId: SESSION, }); expect(Value.Check(BakOutput, read.structuredContent)).toBe(true); const text = JSON.stringify(read); expect(text).toContain("historical [redacted]"); expect(text).not.toContain(FAKE_CREDENTIAL); expect(text).not.toContain(ACCESS); expect(text).not.toContain(`sessions/${HOST}/`); const paths = statePaths(); for (const path of [paths.identity, paths.cache, paths.pendingDir]) await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); expect(storage.calls).toContain(`sessions/${HOST}/${SESSION}.jsonl`); expect(fixture!.errors).toEqual([]); }); it("exposes full archived entries on demand through the registered tool without writes", async () => { const storage = fakeStorage(); const key = `sessions/${HOST}/${SESSION}.jsonl`; const original = storage.objects.get(key)!; storage.objects.set( key, `${original}\n${JSON.stringify({ type: "compaction", details: { decay: { elapsedMs: 1234, atoms: [{ key: "rule" }] } }, summary: "checkpoint" })}\n${JSON.stringify({ type: "custom", customType: "decay-diagnostic", data: { reason: "failure", credential: ACCESS } })}\n${JSON.stringify( { type: "message", message: { role: "assistant", content: [ { type: "thinking", thinking: "reasoning" }, { type: "toolCall", arguments: { value: "x".repeat(20000) } }, ], }, }, )}`, ); const { invoke } = await start(); let result = await invoke({ action: "read", view: "raw-jsonl", hostId: HOST, sessionId: SESSION, }); let restored = ""; while (true) { expect(Value.Check(BakOutput, result.structuredContent)).toBe(true); const page = result.structuredContent as import("../tool.js").BakRawResult; restored += page.text; if (!page.nextCursor) break; result = await invoke({ action: "read", cursor: page.nextCursor }); } const entries = restored.split("\n").map((line) => JSON.parse(line)); expect(entries[2].details.decay).toEqual({ elapsedMs: 1234, atoms: [{ key: "rule" }], }); expect(entries[3].customType).toBe("decay-diagnostic"); expect(entries[4].message.content[0].thinking).toBe("reasoning"); expect(entries[4].message.content[1].arguments.value).toHaveLength(20000); expect(restored).not.toContain(ACCESS); expect(restored).not.toContain(FAKE_CREDENTIAL); const paths = statePaths(); for (const path of [paths.identity, paths.cache, paths.pendingDir]) await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); }); it("returns structured values through a real codemode call", async () => { fakeStorage(); await start(true); fixture!.setResponses([ fauxAssistantMessage( fauxToolCall("codemode", { code: 'const page = await tools.bak({ action: "list", limit: 1 }); if (typeof page === "string") throw new Error("not structured"); text({ total: page.totalItems, id: page.sessions[0].sessionId });', }), { stopReason: "toolUse" }, ), fauxAssistantMessage("done"), ]); await fixture!.session.prompt("query the test archive"); const result = fixture!.session.messages.find( (message) => message.role === "toolResult" && message.toolName === "codemode", ); expect(result?.role).toBe("toolResult"); if (result?.role !== "toolResult") throw new Error("missing codemode result"); expect(result.isError).toBe(false); const text = result.content .filter((item) => item.type === "text") .map((item) => item.text) .join("\n"); expect(text).toContain('"total":2'); expect(text).toContain(SESSION); expect(fixture!.session.getActiveToolNames()).not.toContain("bak"); expect(fixture!.errors).toEqual([]); }); it("rejects unsafe or irrelevant parameters before execution", () => { for (const params of [ { action: "restore" }, { action: "read", hostId: "../other", sessionId: SESSION }, { action: "list", path: "/tmp/restore" }, { action: "list", limit: 101 }, { action: "read", cursor: "x", hostId: HOST }, ]) expect(Value.Check(BakParameters, params)).toBe(false); }); it("clears pagination snapshots on shutdown without automatic backup", async () => { fakeStorage(); const { invoke } = await start(); const result = await invoke({ action: "list", limit: 1 }); const data = result.structuredContent as { nextCursor: string }; await fixture!.session.extensionRunner.emit({ type: "session_shutdown", reason: "reload", }); await expect( invoke({ action: "list", cursor: data.nextCursor }), ).rejects.toThrow("cancelled"); }); it("rejects catalog host limits before fetching any indexes", async () => { const storage = fakeStorage(); storage.objects.set( "catalog/hosts.json", JSON.stringify({ version: 1, hosts: Array.from({ length: 257 }, (_, i) => ({ hostId: `${i.toString(16).padStart(8, "0")}-1234-1234-1234-123456789abc`, alias: `host-${i}`, })), }), ); const { invoke } = await start(); await expect(invoke({ action: "list" })).rejects.toThrow("safety limit"); expect(storage.calls).toEqual(["catalog/hosts.json"]); }); it("rejects oversized transcript lines explicitly", async () => { const storage = fakeStorage(); const key = `sessions/${HOST}/${SESSION}.jsonl`; storage.objects.set( key, `${storage.objects.get(key)!.split("\n")[0]}\n${JSON.stringify({ type: "message", message: { role: "user", content: "x".repeat(1024 * 1024) } })}`, ); const { invoke } = await start(); await expect( invoke({ action: "read", hostId: HOST, sessionId: SESSION }), ).rejects.toThrow("safety limit"); }); it("does not follow object keys supplied by a malicious host index", async () => { const storage = fakeStorage(); storage.objects.set( `hosts/${HOST}.json`, JSON.stringify({ version: 1, hostId: HOST, alias: "desktop", sessions: [ { id: SESSION, key: "private/credential-file", cwd: "/work", created: DATE, }, ], }), ); const { invoke } = await start(); await expect( invoke({ action: "read", hostId: HOST, sessionId: SESSION }), ).rejects.toThrow("unavailable or invalid"); expect(storage.calls).not.toContain("private/credential-file"); }); it.each(["shutdown", "deadline"])( "aborts active archive body reads on %s", async (reason) => { const storage = fakeStorage(); const { invoke } = await start(); const deadline = new AbortController(); const timeout = AbortSignal.timeout.bind(AbortSignal); if (reason === "deadline") vi.spyOn(AbortSignal, "timeout").mockImplementation((ms) => ms === 60_000 ? deadline.signal : timeout(ms), ); const reading = Promise.withResolvers(); let pulls = 0; const stream = new ReadableStream({ pull(controller) { if (++pulls === 1) controller.enqueue(Buffer.from("{")); else reading.resolve(); }, }); storage.fetch.mockImplementation(async () => new Response(stream)); const pending = expect(invoke({ action: "list" })).rejects.toThrow( "cancelled or deadline exceeded", ); await reading.promise; if (reason === "shutdown") await fixture!.session.extensionRunner.emit({ type: "session_shutdown", reason: "reload", }); else deadline.abort(new Error("test deadline")); await pending; }, ); it("keeps transcript content hidden in collapsed tool rows", async () => { fakeStorage(); const { tool, invoke } = await start(); const result = await invoke({ action: "read", hostId: HOST, sessionId: SESSION, }); const theme = fixture!.session.extensionRunner.createContext().ui.theme; const renderContext = { isError: false } as Parameters< NonNullable >[3]; const collapsed = tool .renderResult?.( result, { expanded: false, isPartial: false }, theme, renderContext, ) ?.render(100) .join("\n"); const expanded = tool .renderResult?.( result, { expanded: true, isPartial: false }, theme, renderContext, ) ?.render(100) .join("\n"); expect(collapsed).not.toContain("historical"); expect(collapsed).toContain("to expand"); expect(expanded).toContain("historical"); }); });