Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/bak/__tests__/query.test.ts

Raw
import { Value } from "typebox/value";
import { describe, expect, it, vi } from "vitest";
import { BakQuery } from "../query.js";
import { S3Error, S3SizeLimitError } from "../s3.js";
import type { CatalogCache } from "../state.js";
import { BakOutput, type BakParams, type BakResult } from "../tool.js";

const HOST = "12345678-1234-1234-1234-123456789abc";
const OTHER_HOST = "22345678-1234-1234-1234-123456789abc";
const SESSION = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const OTHER_SESSION = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
const CREATED = "2026-01-01T00:00:00.000Z";

function catalog(): CatalogCache {
	const hosts = [
		{ hostId: HOST, alias: "desktop" },
		{ hostId: OTHER_HOST, alias: "laptop" },
	];
	return {
		refreshedAt: CREATED,
		catalog: { version: 1, hosts },
		indexes: hosts.map((host) => ({
			version: 1,
			...host,
			sessions: (host.hostId === HOST
				? [SESSION, OTHER_SESSION]
				: [SESSION]
			).map((id) => ({
				id,
				key: `sessions/${host.hostId}/${id}.jsonl`,
				name: id === SESSION ? "Auth investigation" : "Parser cleanup",
				cwd: "/work/project",
				created: CREATED,
			})),
		})),
	};
}

function transcript(entries: unknown[]): Uint8Array {
	return Buffer.from(
		[
			{
				type: "session",
				version: 3,
				id: SESSION,
				timestamp: CREATED,
				cwd: "/work/project",
			},
			...entries,
		]
			.map((entry) => JSON.stringify(entry))
			.join("\n"),
	);
}

function message(id: string, role: string, content: unknown, extra = {}) {
	return {
		type: "message",
		id,
		parentId: id === "one" ? null : "one",
		timestamp: CREATED,
		message: { role, content, ...extra },
	};
}

function backend(body = transcript([])) {
	return {
		readCatalog: vi.fn(async () => catalog()),
		readSession: vi.fn(async () => body),
		readSessionRange: vi.fn(async () => {
			throw new Error("unexpected raw range read");
		}),
	};
}

function valid(result: BakResult): void {
	expect(Value.Check(BakOutput, result)).toBe(true);
	expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(
		48 * 1024,
	);
}

describe("bak query", () => {
	it("lists every indexed host, keeping identical session IDs distinct", async () => {
		const source = backend();
		const query = new BakQuery(source);
		const first = await query.execute({ action: "list", limit: 1 });
		valid(first);
		expect(first.action).toBe("list");
		if (first.action === "read") throw new Error("expected metadata");
		expect(first.totalItems).toBe(3);
		expect(first.sessions[0]).toMatchObject({
			hostId: HOST,
			sessionId: SESSION,
		});
		const second = await query.execute({
			action: "list",
			cursor: first.nextCursor!,
			limit: 100,
		});
		valid(second);
		if (second.action === "read") throw new Error("expected metadata");
		expect(
			second.sessions.map((item) => [item.hostId, item.sessionId]),
		).toEqual([
			[HOST, OTHER_SESSION],
			[OTHER_HOST, SESSION],
		]);
		expect(second.nextCursor).toBeNull();
		expect(source.readCatalog).toHaveBeenCalledTimes(1);
		expect(source.readSession).not.toHaveBeenCalled();
		expect(JSON.stringify(first)).not.toContain('"key"');
	});

	it("keeps cursor snapshots stable while fresh queries see remote changes", async () => {
		const source = backend();
		const query = new BakQuery(source);
		const first = await query.execute({ action: "list", limit: 1 });
		source.readCatalog.mockResolvedValue({ ...catalog(), indexes: [] });
		const next = await query.execute({
			action: "list",
			cursor: first.nextCursor!,
		});
		expect(next).toMatchObject({ totalItems: 3 });
		const fresh = await query.execute({ action: "list" });
		expect(fresh).toMatchObject({ totalItems: 0 });
		expect(source.readCatalog).toHaveBeenCalledTimes(2);
	});

	it.each(["auth", "PROJECT", "DESKTOP", HOST, OTHER_SESSION])(
		"searches literal metadata %s",
		async (query) => {
			const result = await new BakQuery(backend()).execute({
				action: "search",
				query,
			});
			valid(result);
			if (!("totalItems" in result)) throw new Error("expected metadata");
			expect(result.totalItems).toBeGreaterThan(0);
		},
	);

	it("searches original metadata beyond its display clipping and filters hosts", async () => {
		const source = backend();
		const cache = catalog();
		cache.indexes[0]!.sessions[0]!.cwd = `/${"x".repeat(600)}/needle`;
		source.readCatalog.mockResolvedValue(cache);
		const result = await new BakQuery(source).execute({
			action: "search",
			query: " needle ",
			hostId: HOST,
		});
		valid(result);
		if (result.action === "read") throw new Error("expected search");
		expect(result.totalItems).toBe(1);
		expect(result.sessions[0]?.metadataTruncated).toBe(true);
		expect(result.sessions[0]?.cwd.length).toBe(512);
	});

	it("does not expose configured credentials through substring-search probes", async () => {
		const fakeCredential = "credential-part-123";
		const source = backend();
		const cache = catalog();
		cache.indexes[0]!.sessions[0]!.name = `ordinary ${fakeCredential}`;
		source.readCatalog.mockResolvedValue(cache);
		const query = new BakQuery(source, [fakeCredential]);
		expect(
			await query.execute({ action: "search", query: "part-123" }),
		).toMatchObject({ totalItems: 0 });
		expect(
			await query.execute({ action: "search", query: "ordinary" }),
		).toMatchObject({ totalItems: 1 });
	});

	it("isolates snapshots from mutations of returned metadata and fragments", async () => {
		const text = "original".repeat(1000);
		const query = new BakQuery(
			backend(transcript([message("one", "user", text)])),
		);
		const read = await query.execute({
			action: "read",
			hostId: HOST,
			sessionId: SESSION,
			limit: 1,
		});
		if (read.action !== "read" || read.view !== "archive-history")
			throw new Error("expected read");
		read.session.cwd = "tampered";
		read.omitted.thinking = 999;
		read.fragments[0]!.text = "tampered";
		const replay = await query.execute({
			action: "read",
			cursor: read.nextCursor!.replace(/\.\d+$/, ".0"),
			limit: 1,
		});
		valid(replay);
		if (replay.action !== "read" || replay.view !== "archive-history")
			throw new Error("expected read");
		expect(replay.session.cwd).toBe("/work/project");
		expect(replay.omitted.thinking).toBe(0);
		expect(replay.fragments[0]?.text).toBe(text.slice(0, 4096));
		const list = await query.execute({ action: "list", limit: 1 });
		if (list.action === "read") throw new Error("expected metadata");
		list.sessions[0]!.name = "tampered";
		const listReplay = await query.execute({
			action: "list",
			cursor: list.nextCursor!.replace(/\.\d+$/, ".0"),
			limit: 1,
		});
		if (listReplay.action === "read") throw new Error("expected metadata");
		expect(listReplay.sessions[0]?.name).toBe("Auth investigation");
	});

	it("includes inline shell history and custom messages hidden from the TUI", async () => {
		const query = new BakQuery(
			backend(
				transcript([
					message("one", "bashExecution", undefined, {
						command: "printf hello",
						output: "hello",
						exitCode: 0,
						cancelled: false,
						excludeFromContext: true,
						fullOutputPath: "PRIVATE_ARTIFACT_PATH",
					}),
					{
						type: "custom_message",
						id: "two",
						parentId: "one",
						timestamp: CREATED,
						content: "hidden context",
						display: false,
						details: { private: "PRIVATE_DETAILS" },
					},
				]),
			),
		);
		const result = await query.execute({
			action: "read",
			hostId: HOST,
			sessionId: SESSION,
		});
		valid(result);
		if (result.action !== "read" || result.view !== "archive-history")
			throw new Error("expected read");
		expect(result.fragments.map((item) => [item.kind, item.text])).toEqual([
			["bashExecution", "$ printf hello\nhello"],
			["custom_message", "hidden context"],
		]);
		expect(JSON.stringify(result)).not.toContain("PRIVATE_ARTIFACT_PATH");
		expect(JSON.stringify(result)).not.toContain("PRIVATE_DETAILS");
	});

	it("rejects blank search and invalid page limits before any remote work", async () => {
		const source = backend();
		const query = new BakQuery(source);
		await expect(
			query.execute({ action: "search", query: " " }),
		).rejects.toThrow("non-empty");
		await expect(query.execute({ action: "list", limit: 101 })).rejects.toThrow(
			"page limit",
		);
		expect(source.readCatalog).not.toHaveBeenCalled();
	});

	it("expires, evicts and clears cursors without silently fetching new pages", async () => {
		let now = 0;
		const source = backend();
		const query = new BakQuery(source, [], () => now);
		const initial = await query.execute({ action: "list", limit: 1 });
		now = 10 * 60 * 1000;
		await expect(
			query.execute({ action: "list", cursor: initial.nextCursor! }),
		).rejects.toThrow("expired");
		const oldest = await query.execute({ action: "list", limit: 1 });
		for (let i = 0; i < 4; i++) await query.execute({ action: "list" });
		await expect(
			query.execute({ action: "list", cursor: oldest.nextCursor! }),
		).rejects.toThrow("expired");
		const last = await query.execute({ action: "list", limit: 1 });
		query.clear();
		await expect(
			query.execute({ action: "list", cursor: last.nextCursor! }),
		).rejects.toThrow("expired");
	});

	it("binds cursors to actions and rejects changed selectors or forged offsets", async () => {
		const query = new BakQuery(backend());
		const page = await query.execute({ action: "list", limit: 1 });
		await expect(
			query.execute({ action: "read", cursor: page.nextCursor! }),
		).rejects.toThrow("action mismatch");
		await expect(
			query.execute({
				action: "list",
				cursor: page.nextCursor!,
				hostId: HOST,
			} as BakParams),
		).rejects.toThrow("selectors");
		await expect(
			query.execute({
				action: "list",
				cursor: page.nextCursor!.replace(/\.1$/, ".999999"),
			}),
		).rejects.toThrow("offset");
		await expect(
			query.execute({ action: "list", cursor: "invalid" }),
		).rejects.toThrow("invalid cursor");
	});

	it("projects all-branch visible history without reasoning, state, media or arguments", async () => {
		const credential = "ACTIVE_ARCHIVE_CREDENTIAL";
		const source = backend(
			transcript([
				message("one", "user", `token ${credential}`),
				message("two", "assistant", [
					{
						type: "thinking",
						thinking: "THINKING_SECRET",
						thinkingSignature: "SIGNATURE_SECRET",
					},
					{ type: "text", text: "visible answer" },
					{
						type: "toolCall",
						name: "read",
						arguments: { path: "ARGUMENT_SECRET" },
					},
				]),
				message(
					"three",
					"toolResult",
					[
						{ type: "text", text: "visible output" },
						{ type: "image", data: "IMAGE_SECRET" },
					],
					{
						toolName: "read",
						isError: true,
						details: { secret: "detail-seed" },
						structuredContent: "STRUCTURED_SECRET",
					},
				),
				message("four", "system", "SYSTEM_SECRET"),
				{ type: "custom", data: { secret: "state-seed" } },
				{
					type: "custom_message",
					id: "five",
					parentId: "one",
					content: "branch context",
					details: "CUSTOM_DETAIL_SECRET",
				},
				{
					type: "compaction",
					summary: "summary",
					systemMessage: "CHECKPOINT_SECRET",
				},
				{ type: "branch_summary", summary: "other branch" },
				{ type: "context_edit", targetId: "one", replacement: null },
			]),
		);
		const cache = catalog();
		cache.indexes[0]!.sessions[0]!.name = `name ${credential}`;
		source.readCatalog.mockResolvedValue(cache);
		const result = await new BakQuery(source, [credential]).execute({
			action: "read",
			hostId: HOST,
			sessionId: SESSION,
		});
		valid(result);
		if (result.action !== "read" || result.view !== "archive-history")
			throw new Error("expected read");
		expect(result.fragments.map((item) => item.kind)).toEqual([
			"user",
			"assistant",
			"toolResult",
			"custom_message",
			"compaction",
			"branch_summary",
		]);
		expect(result.fragments[0]).toMatchObject({
			entryId: "one",
			parentId: null,
			line: 2,
			text: "token [redacted]",
		});
		expect(result.fragments[2]).toMatchObject({
			toolName: "read",
			isError: true,
		});
		expect(result.omitted).toEqual({
			thinking: 1,
			images: 1,
			system: 1,
			customState: 1,
			toolArguments: 1,
			other: 1,
		});
		expect(result).toMatchObject({
			branches: "all",
			order: "append",
			view: "archive-history",
		});
		const json = JSON.stringify(result);
		for (const secret of [
			credential,
			"THINKING_SECRET",
			"SIGNATURE_SECRET",
			"ARGUMENT_SECRET",
			"IMAGE_SECRET",
			"detail-seed",
			"STRUCTURED_SECRET",
			"SYSTEM_SECRET",
			"state-seed",
			"CUSTOM_DETAIL_SECRET",
			"CHECKPOINT_SECRET",
		])
			expect(json).not.toContain(secret);
	});

	it("reassembles Unicode entry fragments without dropping surrogate pairs", async () => {
		const text = `${"a".repeat(4095)}😀${"b".repeat(5000)}`;
		const source = backend(transcript([message("one", "user", text)]));
		const query = new BakQuery(source);
		let page = await query.execute({
			action: "read",
			hostId: HOST,
			sessionId: SESSION,
			limit: 1,
		});
		const parts: string[] = [];
		while (true) {
			valid(page);
			if (page.action !== "read" || page.view !== "archive-history")
				throw new Error("expected read");
			parts.push(...page.fragments.map((item) => item.text));
			if (!page.nextCursor) break;
			page = await query.execute({
				action: "read",
				cursor: page.nextCursor,
				limit: 1,
			});
		}
		expect(parts.every((part) => part.isWellFormed())).toBe(true);
		expect(parts.join("")).toBe(text);
		expect(source.readSession).toHaveBeenCalledTimes(1);
	});

	it("respects JSON escaping byte limits without losing text between pages", async () => {
		const text = "\u0000".repeat(100_000);
		const query = new BakQuery(
			backend(transcript([message("one", "user", text)])),
		);
		let page = await query.execute({
			action: "read",
			hostId: HOST,
			sessionId: SESSION,
			limit: 100,
		});
		let restored = "";
		let pages = 0;
		while (true) {
			valid(page);
			if (page.action !== "read" || page.view !== "archive-history")
				throw new Error("expected read");
			restored += page.fragments.map((item) => item.text).join("");
			pages++;
			if (!page.nextCursor) break;
			const cursor = page.nextCursor;
			page = await query.execute({ action: "read", cursor, limit: 100 });
			expect(page.nextCursor).not.toBe(cursor);
			if (pages > 100) throw new Error("pagination did not advance");
		}
		expect(restored).toBe(text);
		expect(pages).toBeGreaterThan(1);
	});

	it("refuses unindexed reads without requesting an object", async () => {
		const source = backend();
		await expect(
			new BakQuery(source).execute({
				action: "read",
				hostId: OTHER_HOST,
				sessionId: OTHER_SESSION,
			}),
		).rejects.toThrow("not indexed");
		expect(source.readSession).not.toHaveBeenCalled();
	});

	it.each([
		new Error("Bak query: SERVER_CREDENTIAL"),
		new S3Error(403, "SERVER_CREDENTIAL"),
		new S3SizeLimitError(),
	])("sanitizes backend failures %s", async (error) => {
		const source = backend();
		source.readCatalog.mockRejectedValue(error);
		await expect(
			new BakQuery(source).execute({ action: "list" }),
		).rejects.toThrow(/^Bak query:/);
		await expect(
			new BakQuery(source).execute({ action: "list" }),
		).rejects.not.toThrow("SERVER_CREDENTIAL");
	});

	it("honors cancellation during projection without exposing abort reasons", async () => {
		const controller = new AbortController();
		const source = backend(
			transcript(
				Array.from({ length: 1000 }, (_, i) =>
					message(String(i), "user", "hello"),
				),
			),
		);
		const read = source.readSession;
		read.mockImplementation(async () => {
			setImmediate(() => controller.abort(new Error("ABORT_SECRET")));
			return transcript(
				Array.from({ length: 1000 }, (_, i) =>
					message(String(i), "user", "hello"),
				),
			);
		});
		await expect(
			new BakQuery(source).execute(
				{ action: "read", hostId: HOST, sessionId: SESSION },
				controller.signal,
			),
		).rejects.toThrow("cancelled");
	});

	it("bounds concurrent initial requests", async () => {
		const gate = Promise.withResolvers<CatalogCache>();
		const source = backend();
		source.readCatalog.mockImplementation(() => gate.promise);
		const query = new BakQuery(source);
		const first = query.execute({ action: "list" });
		const second = query.execute({ action: "list" });
		await expect(query.execute({ action: "list" })).rejects.toThrow(
			"two reads already running",
		);
		gate.resolve(catalog());
		await Promise.all([first, second]);
		expect(source.readCatalog).toHaveBeenCalledTimes(2);
	});
});