Skip to content

A self-organizing Obsidian vault that gives AI coding agents persistent memory.

16.1 KB
Raw Blame
/**
 * Unit tests for lib/hook-io — stderr helpers shared across hooks and scripts.
 * readStdinJson is exercised via integration tests; here we lock the tiny
 * stderr formatters so message shape stays consistent.
 */

import { test, describe, afterEach } from "node:test";
import assert from "node:assert/strict";

import { debug, envelopedLimit, fitEncoded, fitWithMeter, HOOK_OUTPUT_MAX_CHARS, parseHookContext, warn, writeHookOutput, writeStopFeedback, writeSystemMessage, type PolicyResult } from "../lib/hook-io.ts";

/**
 * Replace process.stderr.write with a capturer that records calls and returns
 * true (matching the real write() signature). Returns a restorer that
 * reinstates the original, plus the captured lines.
 */
function captureStderr(): {
	lines: string[];
	restore: () => void;
} {
	const lines: string[] = [];
	const original = process.stderr.write.bind(process.stderr);
	process.stderr.write = ((chunk: string | Uint8Array) => {
		lines.push(typeof chunk === "string" ? chunk : chunk.toString());
		return true;
	}) as typeof process.stderr.write;
	return { lines, restore: () => (process.stderr.write = original) };
}

describe("warn", () => {
	let capture: ReturnType<typeof captureStderr>;

	afterEach(() => capture?.restore());

	test("prefixes the message with `  ⚠ ` and appends a newline", () => {
		capture = captureStderr();
		warn("something went wrong");
		assert.deepEqual(capture.lines, ["  ⚠ something went wrong\n"]);
	});

	test("writes nothing else when called with empty string", () => {
		capture = captureStderr();
		warn("");
		assert.deepEqual(capture.lines, ["  ⚠ \n"]);
	});
});

describe("debug", () => {
	const originalFlag = process.env["HOOK_DEBUG"];
	let capture: ReturnType<typeof captureStderr>;

	afterEach(() => {
		capture?.restore();
		if (originalFlag === undefined) {
			delete process.env["HOOK_DEBUG"];
		} else {
			process.env["HOOK_DEBUG"] = originalFlag;
		}
	});

	test("silent when HOOK_DEBUG is unset", () => {
		delete process.env["HOOK_DEBUG"];
		capture = captureStderr();
		debug("should not appear");
		assert.deepEqual(capture.lines, []);
	});

	test("silent when HOOK_DEBUG is not exactly '1'", () => {
		process.env["HOOK_DEBUG"] = "true";
		capture = captureStderr();
		debug("should not appear");
		assert.deepEqual(capture.lines, []);
	});

	test("writes a tagged line to stderr when HOOK_DEBUG=1", () => {
		process.env["HOOK_DEBUG"] = "1";
		capture = captureStderr();
		debug("taking path A");
		assert.equal(capture.lines.length, 1);
		assert.match(
			capture.lines[0] ?? "",
			/^\[hook-debug \d{4}-\d{2}-\d{2}T[^\]]+\] taking path A\n$/,
		);
	});
});

/** Half of a surrogate pair with its other half missing. */
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;

describe("fitEncoded — held under the hook output cap", () => {
	test("text that fits is returned unchanged", () => {
		assert.equal(fitEncoded("short", 100), "short");
	});

	test("counts JSON escaping, which a cut on raw length would miss", () => {
		// 3,000 backslashes encode to 6,000 characters.
		const text = "\\".repeat(3_000);
		const fitted = fitEncoded(text, 4_000);
		assert.ok(JSON.stringify(fitted).length <= 4_000, `got ${JSON.stringify(fitted).length}`);
		assert.match(fitted, /… \(truncated to fit the hook output cap\)$/);
	});

	test("never leaves half of an astral emoji", () => {
		// Each 🚨 is two UTF-16 units, so a cut on raw length would split one
		// at every odd cap. Counting the encoded length is what prevents it.
		for (let max = 200; max < 206; max++) {
			const fitted = fitEncoded("🚨".repeat(500), max);
			assert.doesNotMatch(fitted, LONE_SURROGATE, `max ${max} left a lone surrogate`);
		}
	});
});

/** What `write` printed to stdout. */
function captureStdout(write: () => void): string {
	const chunks: string[] = [];
	const original = process.stdout.write.bind(process.stdout);
	process.stdout.write = ((chunk: string) => {
		chunks.push(chunk);
		return true;
	}) as typeof process.stdout.write;
	try {
		write();
	} finally {
		process.stdout.write = original;
	}
	return chunks.join("");
}

// "x" encodes to one character, so the cut can land exactly on the cap:
// these tests assert the exact length, which catches an off-by-one either way.
describe("writeStopFeedback — the whole output fits", () => {
	test("a huge report and summary fill the cap exactly, both marked as cut, and never as a block", () => {
		const out = captureStdout(() => writeStopFeedback("x".repeat(20_000), "x".repeat(20_000)));
		assert.equal(out.length, HOOK_OUTPUT_MAX_CHARS);
		const parsed = JSON.parse(out) as { decision?: string; systemMessage: string; hookSpecificOutput: { hookEventName: string; additionalContext: string } };
		assert.equal(parsed.decision, undefined, "a block is printed in full and labelled an error");
		assert.equal(parsed.hookSpecificOutput.hookEventName, "Stop");
		assert.match(parsed.hookSpecificOutput.additionalContext, /truncated to fit the hook output cap\)$/);
		assert.match(parsed.systemMessage, /truncated to fit the hook output cap\)$/);
		assert.ok(parsed.hookSpecificOutput.additionalContext.length > parsed.systemMessage.length, "the agent's copy gets the larger share");
	});
});

describe("writeSystemMessage — the output fits", () => {
	test("a huge message fills the cap exactly, marked as cut", () => {
		const out = captureStdout(() => writeSystemMessage("x".repeat(20_000)));
		assert.equal(out.length, HOOK_OUTPUT_MAX_CHARS);
		assert.match((JSON.parse(out) as { systemMessage: string }).systemMessage, /truncated to fit the hook output cap\)$/);
	});

	test("a short message is written unchanged", () => {
		assert.equal(captureStdout(() => writeSystemMessage("hello")), '{"systemMessage":"hello"}');
	});
});

describe("writeHookOutput — additionalContext fits the hook output cap (#254)", () => {
	test("a huge context is cut with the marker, and the whole stdout fills the cap exactly", () => {
		const out = captureStdout(() => writeHookOutput("PostToolUse", "x".repeat(20_000)));
		assert.equal(out.length, HOOK_OUTPUT_MAX_CHARS);
		const context = (JSON.parse(out) as { hookSpecificOutput: { additionalContext: string } }).hookSpecificOutput.additionalContext;
		assert.match(context, /truncated to fit the hook output cap\)$/);
	});

	test("policy results take their share of the cap, not more of it", () => {
		const policy: PolicyResult[] = Array.from({ length: 3 }, (_, i) => ({ policy_id: `rule-${i}`, path: `notes/${"y".repeat(300)}.md`, classification: "misplaced", action: "warn" }));
		const out = captureStdout(() => writeHookOutput("PostToolUse", "x".repeat(20_000), policy));
		assert.equal(out.length, HOOK_OUTPUT_MAX_CHARS);
		assert.equal((JSON.parse(out) as { hookSpecificOutput: { policyResults: unknown[] } }).hookSpecificOutput.policyResults.length, 3);
	});

	test("at every policy size near the edge, the whole stdout stays within the cap", () => {
		// Sweeps the boundary where the context's room is about the marker's size,
		// which a single large fixture cannot reach.
		const shell = JSON.stringify({ hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: "", policyResults: [{ policy_id: "r", path: "", classification: "c", action: "warn" }] } }).length;
		for (let pathLen = HOOK_OUTPUT_MAX_CHARS - shell - 80; pathLen <= HOOK_OUTPUT_MAX_CHARS - shell + 5; pathLen++) {
			const policy: PolicyResult[] = [{ policy_id: "r", path: "p".repeat(pathLen), classification: "c", action: "warn" }];
			const out = captureStdout(() => writeHookOutput("PostToolUse", "x".repeat(20_000), policy));
			assert.ok(out.length <= HOOK_OUTPUT_MAX_CHARS, `policy path ${pathLen}: stdout is ${out.length} characters`);
		}
	});

	test("policy results too large to fit beside any context are dropped, never carried over the cap", () => {
		const policy: PolicyResult[] = Array.from({ length: 3 }, (_, i) => ({ policy_id: `rule-${i}`, path: `notes/${"y".repeat(4_000)}.md`, classification: "misplaced", action: "warn" }));
		const out = captureStdout(() => writeHookOutput("PostToolUse", "x".repeat(20_000), policy));
		assert.ok(out.length <= HOOK_OUTPUT_MAX_CHARS, `stdout is ${out.length} characters`);
		const parsed = JSON.parse(out) as { hookSpecificOutput: { additionalContext: string; policyResults?: unknown[] } };
		assert.equal(parsed.hookSpecificOutput.policyResults, undefined);
		assert.match(parsed.hookSpecificOutput.additionalContext, /truncated to fit the hook output cap\)$/);
	});

	test("a short context is written unchanged", () => {
		const out = captureStdout(() => writeHookOutput("PostToolUse", "hello"));
		assert.equal(out, '{"hookSpecificOutput":{"hookEventName":"PostToolUse","additionalContext":"hello"}}');
	});
});

describe("envelopedLimit and fitWithMeter's json unit — a context sent inside the envelope (#340)", () => {
	const meter = (cut: boolean) => (cut ? "_context injected: cut_" : "_context injected_");

	test("a context fitted to the SessionStart limit stays within the cap once enveloped, meter last", () => {
		// Newlines and quotes escape to two characters each: an ASCII body that
		// fits the cap raw is over it once encoded, which a raw count would miss.
		const body = Array.from({ length: 2_000 }, (_, i) => `- "item" ${i}\n`).join("");
		const context = fitWithMeter(body, meter, envelopedLimit("SessionStart"));
		const out = captureStdout(() => writeHookOutput("SessionStart", context));
		assert.ok(out.length <= HOOK_OUTPUT_MAX_CHARS, `stdout is ${out.length} characters`);
		const carried = (JSON.parse(out) as { hookSpecificOutput: { additionalContext: string } }).hookSpecificOutput.additionalContext;
		assert.equal(carried, context, "writeHookOutput did not cut it again");
		assert.match(carried, /_context injected: cut_\n$/);
	});

	test("a body that fits the limit raw but not encoded is cut, not passed whole", () => {
		const limit = envelopedLimit("SessionStart");
		// Every character a quote: raw it is under the limit, encoded about twice it.
		const body = '"'.repeat(limit.max - 200);
		const context = fitWithMeter(body, meter, limit);
		assert.ok(JSON.stringify(context).length <= limit.max, `encoded ${JSON.stringify(context).length} of ${limit.max}`);
		assert.match(context, /_context injected: cut_\n$/, "the cut is in the meter");
	});

	test("the meter is handed the size in the caller's measure, the one its budget decided on", () => {
		const seen: number[] = [];
		const encoded = (t: string) => JSON.stringify(t).length;
		fitWithMeter('a"b\n', (_cut, size) => (seen.push(size), "_m_"), envelopedLimit("SessionStart"), encoded);
		assert.deepEqual(seen, [encoded('a"b\n')], "not the UTF-8 bytes");
	});

	test("the json cut never splits an emoji", () => {
		const body = "😀".repeat(6_000);
		const context = fitWithMeter(body, meter, envelopedLimit("SessionStart"));
		assert.doesNotMatch(context, /[\uD800-\uDBFF](?![\uDC00-\uDFFF])/, "a lone high surrogate");
		assert.ok(JSON.stringify(context).length <= envelopedLimit("SessionStart").max);
	});

	test("an output exactly at the limit once encoded passes whole; one character more is cut", () => {
		const limit = envelopedLimit("SessionStart");
		// The whole output is `${body}\n${meter}\n`: solve for the body that lands
		// on max, quotes (two encoded characters each) plus one "x" for an odd room.
		const room = limit.max - 2 - (JSON.stringify(`\n${meter(false)}\n`).length - 2);
		const body = "x".repeat(room % 2) + '"'.repeat(Math.floor(room / 2));
		const whole = fitWithMeter(body, meter, limit);
		assert.equal(JSON.stringify(whole).length, limit.max);
		assert.match(whole, /_context injected_\n$/, "not cut");
		assert.match(fitWithMeter(body + "x", meter, limit), /_context injected: cut_\n$/);
	});

	test("an emoji straddling the cut after an ASCII prefix is dropped whole, on every alignment", () => {
		const limit = envelopedLimit("SessionStart");
		for (let pad = 0; pad < 4; pad++) {
			const body = "x".repeat(limit.max - 500 + pad) + "😀".repeat(400);
			const context = fitWithMeter(body, meter, limit);
			assert.doesNotMatch(context, /[\uD800-\uDBFF](?![\uDC00-\uDFFF])/, `a lone high surrogate at pad ${pad}`);
			assert.ok(JSON.stringify(context).length <= limit.max, `pad ${pad}`);
		}
	});

	test("a meter too long for the limit on its own is cut to the limit, its newline counted as encoded", () => {
		for (const max of [10, 11, 20, 21]) {
			const limit = { max, unit: "json" as const, name: "a tiny limit" };
			const out = fitWithMeter("body", () => "m".repeat(100), limit);
			assert.ok(JSON.stringify(out).length <= max, `max ${max}: encoded ${JSON.stringify(out).length}`);
		}
	});

	test("without a measure, the meter is sized in the limit's own unit", () => {
		const seen: number[] = [];
		fitWithMeter('a"b\n', (_cut, size) => (seen.push(size), "_m_"), envelopedLimit("SessionStart"));
		assert.deepEqual(seen, [JSON.stringify('a"b\n').length]);
	});

	test("CRLF bodies are measured as encoded, carriage returns included", () => {
		const body = "line\r\n".repeat(3_000);
		const context = fitWithMeter(body, meter, envelopedLimit("SessionStart"));
		assert.ok(JSON.stringify(context).length <= envelopedLimit("SessionStart").max);
	});
});

describe("writeStopReportData", () => {
	test("a report too large for one pipe write arrives whole", async () => {
		const { spawnSync } = await import("node:child_process");
		const lib = new URL("../lib/hook-io.ts", import.meta.url).href;
		const agentText = "x".repeat(2_000_000);
		const script = `import { writeStopReportData } from ${JSON.stringify(lib)}; writeStopReportData({ key: "k", claims: ["c"], agentText: "x".repeat(${agentText.length}) });`;
		const run = spawnSync(process.execPath, ["--disable-warning=ExperimentalWarning", "--experimental-strip-types", "--input-type=module", "-e", script], { encoding: "utf8", maxBuffer: 16 * 1024 * 1024 });
		assert.equal(run.status, 0, run.stderr);
		const parsed = JSON.parse(run.stdout) as { report: { agentText: string } };
		assert.equal(parsed.report.agentText.length, agentText.length);
	});
});

describe("writeStopReportData on a pipe that writes in parts", () => {
	test("short writes and a full pipe still deliver every byte, in order", async () => {
		const { writeStopReportData } = await import("../lib/hook-io.ts");
		const out: Buffer[] = [];
		let calls = 0;
		const write = (buffer: Buffer, offset: number, length: number): number => {
			calls++;
			if (calls === 2) throw Object.assign(new Error("full"), { code: "EAGAIN" });
			const n = Math.min(length, 7);
			out.push(buffer.subarray(offset, offset + n));
			return n;
		};
		writeStopReportData({ key: "k", claims: ["a claim"], agentText: "the full report, longer than one short write" }, write);
		const parsed = JSON.parse(Buffer.concat(out).toString("utf8")) as { report: { agentText: string } };
		assert.equal(parsed.report.agentText, "the full report, longer than one short write");
	});

	test("any failure but a full pipe throws, so the mod falls back", async () => {
		const { writeStopReportData } = await import("../lib/hook-io.ts");
		const write = (): number => {
			throw Object.assign(new Error("closed"), { code: "EPIPE" });
		};
		assert.throws(() => writeStopReportData({ key: "k", claims: [], agentText: "t" }, write), /closed/);
	});
});

describe("parseHookContext — the context a hook-path envelope carries (#340)", () => {
	test("returns additionalContext from an envelope for the named event", () => {
		const stdout = JSON.stringify({ hookSpecificOutput: { hookEventName: "SessionStart", additionalContext: "## Session Context\n" } });
		assert.equal(parseHookContext(stdout, "SessionStart"), "## Session Context\n");
	});

	test("plain text is refused by name, not with a bare JSON SyntaxError", () => {
		assert.throws(() => parseHookContext("## Session Context\n", "SessionStart"), /not one JSON object/);
	});

	test("an envelope for another event, or with no string context, is refused", () => {
		const stop = JSON.stringify({ hookSpecificOutput: { hookEventName: "Stop", additionalContext: "x" } });
		assert.throws(() => parseHookContext(stop, "SessionStart"), /not a SessionStart envelope/);
		const empty = JSON.stringify({ hookSpecificOutput: { hookEventName: "SessionStart" } });
		assert.throws(() => parseHookContext(empty, "SessionStart"), /not a SessionStart envelope/);
		assert.throws(() => parseHookContext("null", "SessionStart"), /not a SessionStart envelope/);
	});
});