Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/pipi/__acceptance__/cli.ts

Raw
import assert from "node:assert/strict";
import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process";
import { randomUUID } from "node:crypto";
import {
	mkdir,
	mkdtemp,
	readdir,
	readFile,
	rm,
	stat,
	writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { StringDecoder } from "node:string_decoder";
import { setTimeout as delay } from "node:timers/promises";
import { fileURLToPath } from "node:url";
import type { JsonObject, Usage } from "@earendil-works/pi-ai";
import { spawn as spawnPty } from "@lydell/node-pty";
import xterm from "@xterm/headless";

const { Terminal } = xterm;

import type { TaskResult } from "../runtime.ts";

const repo = fileURLToPath(new URL("../../../", import.meta.url));
const root = await mkdtemp(join(tmpdir(), "pipi-cli-"));
const agent = join(root, "agent"),
	sessions = join(root, "sessions");
const pkgPath = fileURLToPath(
	new URL(
		"../package.json",
		import.meta.resolve("@earendil-works/pi-coding-agent"),
	),
);
const pkg = JSON.parse(await readFile(pkgPath, "utf8")) as {
	bin: { pi: string };
};
const cli = resolve(dirname(pkgPath), pkg.bin.pi);
const provider = fileURLToPath(new URL("provider.ts", import.meta.url));
const built = join(repo, "dist", "pipi", "index.ts");
const base = [
	cli,
	"--provider",
	"pipi-test",
	"--model",
	"parent",
	"-e",
	provider,
	"-e",
	built,
	"--session-dir",
	sessions,
	"--offline",
	"--approve",
];
const env = {
	...process.env,
	HOME: join(root, "home"),
	USERPROFILE: join(root, "home"),
	XDG_CONFIG_HOME: join(root, "config"),
	XDG_STATE_HOME: join(root, "state"),
	XDG_CACHE_HOME: join(root, "cache"),
	XDG_DATA_HOME: join(root, "data"),
	PI_CODING_AGENT_DIR: agent,
	PI_PIPI_DEBUG: "0",
	PI_OFFLINE: "1",
};
for (const path of [agent, sessions, env.HOME])
	await mkdir(path, { recursive: true });
await writeFile(
	join(agent, "settings.json"),
	JSON.stringify({
		defaultTools: ["pipi", "codemode", "read", "write"],
		retry: { enabled: false },
		cacheWarming: "off",
		pipi: {
			modelTiers: {
				small: {
					model: "pipi-test/alternate",
					thinkingLevel: "high",
					instructions: "Bounded fixture",
				},
			},
		},
	}),
);
const reports: Array<{ name: string; ok: boolean; detail?: string }> = [];
interface Event {
	type: string;
	isError?: boolean;
	toolName?: string;
	result?: {
		details?: { result?: TaskResult };
		structuredContent?: TaskResult;
	};
	id?: string;
	success?: boolean;
	data?: Record<string, unknown>;
	message?: { role: string; content: unknown };
}
const childPrompt = (value: JsonObject) =>
	"PIPI_CHILD " + JSON.stringify(value);
const parentPrompt = (calls: JsonObject[], script?: string) =>
	"PIPI_PARENT " + JSON.stringify(script ? { script } : { calls });
function parseEvents(text: string): Event[] {
	return text
		.trim()
		.split("\n")
		.filter(Boolean)
		.map((line) => JSON.parse(line) as Event);
}
function taskResults(events: Event[]): TaskResult[] {
	return events
		.filter(
			(event) =>
				event.type === "tool_execution_end" && event.toolName === "pipi",
		)
		.map((event) => {
			const result =
				event.result?.structuredContent ?? event.result?.details?.result;
			assert.ok(result, "pipi completion must carry its structured result");
			return result;
		});
}
async function waitFor(
	check: () => boolean | Promise<boolean>,
	label: string,
	timeout = 15_000,
): Promise<void> {
	const end = Date.now() + timeout;
	while (!(await check())) {
		assert.ok(Date.now() < end, "Deadline: " + label);
		await delay(20);
	}
}
async function stop(child: ChildProcessWithoutNullStreams): Promise<void> {
	if (child.exitCode !== null || child.signalCode !== null) return;
	child.stdin.end();
	try {
		await waitFor(
			() => child.exitCode !== null || child.signalCode !== null,
			"graceful process exit",
			1500,
		);
	} catch {
		child.kill("SIGTERM");
		await waitFor(
			() => child.exitCode !== null || child.signalCode !== null,
			"process exit",
			3000,
		).catch(() => child.kill("SIGKILL"));
	}
}
async function run(
	message: string,
): Promise<{ events: Event[]; parentFile: string }> {
	const id = "acceptance-" + randomUUID();
	const child = spawn(
		process.execPath,
		[...base, "--session-id", id, "--mode", "json", message],
		{ cwd: root, env, stdio: ["pipe", "pipe", "pipe"] },
	);
	let pending = "",
		stderr = "",
		bytes = 0;
	let failure: Error | undefined;
	const events: Event[] = [];
	const decoder = new StringDecoder("utf8");
	child.on("error", (error) => {
		failure = error;
	});
	child.stdout.on("data", (value: Buffer) => {
		if (failure) return;
		try {
			bytes += value.length;
			assert.ok(
				bytes <= 128 * 1024 * 1024,
				"CLI evidence exceeds bounded output budget",
			);
			pending += decoder.write(value);
			while (pending.includes("\n")) {
				const newline = pending.indexOf("\n");
				const line = pending.slice(0, newline);
				pending = pending.slice(newline + 1);
				if (!line.trim()) continue;
				const event = JSON.parse(line) as Event;
				if (!["message_update", "tool_execution_update"].includes(event.type))
					events.push(event);
			}
			assert.ok(pending.length <= 4 * 1024 * 1024, "Oversized JSONL event");
		} catch (error) {
			failure = error instanceof Error ? error : new Error(String(error));
			child.kill("SIGTERM");
		}
	});
	child.stderr.on("data", (value: Buffer) => {
		stderr = (stderr + value.toString()).slice(-16000);
	});
	child.stdin.end();
	try {
		await waitFor(
			() => child.exitCode !== null || child.signalCode !== null,
			"CLI completion",
			30_000,
		);
		if (failure) throw failure;
		assert.equal(child.exitCode, 0, stderr);
		pending += decoder.end();
		if (pending.trim()) events.push(JSON.parse(pending) as Event);
		const parentFile = (await readdir(sessions)).find((path) =>
			path.endsWith("_" + id + ".jsonl"),
		);
		assert.ok(parentFile, "parent session must persist");
		return {
			events,
			parentFile: join(sessions, parentFile),
		};
	} finally {
		await stop(child);
	}
}
async function call(request: JsonObject): Promise<TaskResult> {
	const result = await run(parentPrompt([request]));
	const values = taskResults(result.events);
	assert.equal(values.length, 1, "exactly this invocation's pipi result");
	return values[0] as TaskResult;
}
function answer(result: TaskResult): string {
	assert.equal(result.outputMode, "message");
	return (
		result.output as unknown as {
			content: Array<{ type: string; text?: string }>;
		}
	).content
		.filter((part) => part.type === "text")
		.map((part) => part.text)
		.join("\n");
}
async function entries(path: string): Promise<
	Array<{
		id: string;
		type: string;
		parentSession?: string;
		message?: {
			role: string;
			toolName?: string;
			isError?: boolean;
			usage?: Usage;
			content?: unknown;
		};
		usage?: Usage;
	}>
> {
	return (await readFile(path, "utf8"))
		.trim()
		.split("\n")
		.map((line) => JSON.parse(line));
}
async function caseOf(name: string, fn: () => Promise<void>): Promise<void> {
	const start = performance.now();
	try {
		await fn();
		reports.push({ name, ok: true });
		process.stdout.write(
			`PASS ${name} (${Math.round(performance.now() - start)}ms)\n`,
		);
	} catch (error) {
		const detail =
			error instanceof Error ? (error.stack ?? error.message) : String(error);
		reports.push({ name, ok: false, detail });
		process.stderr.write(`FAIL ${name}: ${detail}\n`);
	}
}
const objectSchema = {
	type: "object",
	properties: { role: { const: "assistant" }, answer: { const: "ok" } },
	required: ["role", "answer"],
	additionalProperties: false,
};
const integerSchema = { type: "integer" };

await caseOf(
	"fresh native message, linked transcript and independent-process continuation",
	async () => {
		const original = await run(
			parentPrompt([
				{ prompt: childPrompt({ text: "remember-7391" }), tools: [] },
			]),
		);
		const first = taskResults(original.events)[0];
		assert.ok(first);
		assert.equal(first.outcome, "completed");
		assert.equal(answer(first), "remember-7391");
		assert.ok(first.sessionFile);
		assert.equal(
			(await entries(first.sessionFile))[0]?.parentSession,
			original.parentFile,
		);
		const second = await call({
			sessionId: first.sessionId,
			prompt: childPrompt({ kind: "history" }),
			tools: [],
		});
		assert.equal(second.outcome, "completed");
		assert.equal(second.sessionId, first.sessionId);
		assert.equal(answer(second), "resumed child answer: remember-7391");
		const fork = await call({
			sessionId: first.sessionFile,
			fork: true,
			prompt: childPrompt({ kind: "history" }),
			tools: [],
		});
		assert.equal(fork.outcome, "completed");
		assert.notEqual(fork.sessionId, first.sessionId);
		assert.equal(answer(fork), "resumed child answer: remember-7391");
	},
);
await caseOf(
	"saved cwd and real file-tool behavior survive continuation",
	async () => {
		const cwd = join(root, "other");
		await mkdir(cwd);
		await writeFile(join(cwd, "evidence.txt"), "cwd-evidence-729");
		const first = await call({
			cwd,
			prompt: childPrompt({
				kind: "tool",
				tool: "read",
				args: { path: "evidence.txt" },
			}),
			tools: ["read"],
		});
		assert.equal(first.outcome, "completed");
		assert.match(answer(first), /cwd-evidence-729/u);
		const second = await call({
			sessionId: first.sessionId,
			prompt: childPrompt({ text: "continued" }),
			tools: [],
		});
		assert.equal(second.outcome, "completed");
		assert.equal(second.effective.cwd, cwd);
		assert.equal(answer(second), "continued");
	},
);
await caseOf(
	"schema opt-in, invalid correction, latest submission and missing-output failure",
	async () => {
		const structured = await call({
			prompt: childPrompt({
				kind: "submit",
				value: { role: "assistant", answer: "ok" },
			}),
			outputSchema: objectSchema,
			tools: [],
		});
		assert.equal(structured.outcome, "completed");
		assert.deepEqual(structured.output, { role: "assistant", answer: "ok" });
		const corrected = await call({
			prompt: childPrompt({ kind: "correct", invalid: "invalid", value: 42 }),
			outputSchema: integerSchema,
			tools: [],
		});
		assert.equal(corrected.outcome, "completed");
		assert.equal(corrected.output, 42);
		assert.equal(corrected.turns, 2);
		const latest = await call({
			prompt: childPrompt({ kind: "last", values: [1, 2] }),
			outputSchema: integerSchema,
			tools: [],
		});
		assert.equal(latest.outcome, "completed");
		assert.equal(latest.output, 2);
		const missing = await call({
			prompt: childPrompt({ kind: "forget" }),
			outputSchema: integerSchema,
			tools: [],
		});
		assert.equal(missing.outcome, "failed");
		assert.equal(missing.turns, 2);
		assert.equal(missing.output, null);
		const again = await call({
			sessionId: structured.sessionId,
			prompt: childPrompt({ kind: "forget" }),
			outputSchema: objectSchema,
			tools: [],
		});
		assert.equal(again.outcome, "failed");
		assert.equal(again.output, null);
	},
);
await caseOf(
	"parallel scripted workflows return current structured values",
	async () => {
		const calls = [1, 2].map((value) => ({
			prompt: childPrompt({ kind: "submit", value }),
			outputSchema: integerSchema,
			tools: [],
		}));
		const script = `const values=await Promise.all(${JSON.stringify(calls)}.map(args=>tools.pipi(args))); text({sum:values.reduce((sum,result)=>sum+result.output,0),ids:values.map(result=>result.sessionId)});`;
		const invocation = await run(parentPrompt([], script));
		const values = taskResults(invocation.events);
		assert.equal(values.length, 2);
		assert.deepEqual(values.map((value) => value.output).sort(), [1, 2]);
		assert.equal(new Set(values.map((value) => value.sessionId)).size, 2);
		const text = JSON.stringify(invocation.events);
		assert.match(text, /\\?"sum\\?":3/u);
	},
);
await caseOf(
	"presets, edits, call section override, schema disable and tier/model selection",
	async () => {
		const dir = join(agent, "pipi", "agents", "reviewer");
		await mkdir(dir, { recursive: true });
		await writeFile(
			join(dir, "config.json"),
			JSON.stringify({
				description: "Fixture reviewer",
				tools: [],
				outputSchema: integerSchema,
				tier: "small",
			}),
		);
		await writeFile(join(dir, "instructions.md"), "PRESET_INSTRUCTIONS");
		const first = await call({
			agent: "reviewer",
			prompt: childPrompt({ kind: "submit", value: 7 }),
		});
		assert.equal(first.outcome, "completed");
		assert.equal(first.output, 7);
		assert.equal(first.effective.model, "pipi-test/alternate");
		assert.equal(first.effective.thinkingLevel, "high");
		await writeFile(join(dir, "instructions.md"), "UPDATED_INSTRUCTIONS");
		const second = await call({
			agent: "reviewer",
			prompt: childPrompt({ text: "ordinary" }),
			outputSchema: null,
			model: "pipi-test/parent",
			sections: { pipi_agent: "CALL_OVERRIDE" },
		});
		assert.equal(second.outcome, "completed");
		assert.equal(second.outputMode, "message");
		assert.equal(answer(second), "ordinary");
		assert.equal(second.effective.model, "pipi-test/parent");
		assert.equal(second.requested.sections?.["pipi_agent"], "CALL_OVERRIDE");
		assert.ok(second.sessionFile);
		const transcript = await readFile(second.sessionFile, "utf8");
		assert.match(transcript, /CALL_OVERRIDE/u);
		assert.doesNotMatch(transcript, /UPDATED_INSTRUCTIONS/u);
		await mkdir(join(agent, "pipi", "agents", "config-only"));
		await writeFile(
			join(agent, "pipi", "agents", "config-only", "config.json"),
			JSON.stringify({ description: "No instructions", tools: [] }),
		);
		assert.equal(
			(
				await call({
					agent: "config-only",
					prompt: childPrompt({ text: "config-only" }),
				})
			).outcome,
			"completed",
		);
		const unknown = await run(
			parentPrompt([
				{ agent: "absent", prompt: childPrompt({ text: "must not run" }) },
			]),
		);
		const completions = unknown.events.filter(
			(event) =>
				event.type === "tool_execution_end" && event.toolName === "pipi",
		);
		assert.equal(completions.length, 1);
		assert.equal(completions[0]?.isError, true);
		assert.equal(completions[0]?.result?.structuredContent, undefined);
		assert.equal(completions[0]?.result?.details?.result, undefined);
		assert.match(JSON.stringify(completions[0]?.result), /preset unavailable/u);
	},
);
await caseOf(
	"dynamic extension and flag values execute real temp side effects",
	async () => {
		const dir = join(agent, "pipi", "dynamic", "marker");
		await mkdir(dir, { recursive: true });
		const path = join(dir, "index.ts"),
			marker = join(root, "marker.txt");
		await writeFile(
			path,
			`import {writeFile} from 'node:fs/promises'; export default pi=>{pi.registerFlag('fixture-toggle',{type:'boolean',description:'Fixture'});pi.registerTool({name:'fixture_marker',label:'Marker',description:'Fixture',parameters:{type:'object',properties:{},additionalProperties:false},async execute(){const value=String(pi.getFlag('fixture-toggle'));await writeFile(${JSON.stringify(marker)},value);return {content:[{type:'text',text:value}],details:{}}}});};`,
		);
		for (const value of [true, false]) {
			const result = await call({
				prompt: childPrompt({ kind: "tool", tool: "fixture_marker" }),
				extensions: [path],
				flags: { "fixture-toggle": value },
				tools: ["fixture_marker"],
			});
			assert.equal(result.outcome, "completed");
			assert.equal(await readFile(marker, "utf8"), String(value));
			assert.equal(answer(result), String(value));
		}
		const excluded = await call({
			prompt: childPrompt({ text: "excluded" }),
			extensions: [path, "-" + path],
			tools: [],
		});
		assert.equal(excluded.outcome, "completed");
		assert.ok(!excluded.effective.extensions.includes(path));
	},
);
await caseOf(
	"new-work usage matches native persisted entries without resumed-history billing",
	async () => {
		const first = await call({
			prompt: childPrompt({ text: "billing first" }),
			tools: [],
		});
		assert.ok(first.sessionFile);
		const old = new Set(
			(await entries(first.sessionFile)).map((entry) => entry.id),
		);
		const next = await call({
			sessionId: first.sessionId,
			prompt: childPrompt({ text: "billing second" }),
			tools: [],
		});
		assert.equal(next.outcome, "completed");
		const total = (await entries(first.sessionFile))
			.filter((entry) => !old.has(entry.id))
			.reduce(
				(sum, entry) =>
					sum +
					(entry.message?.usage?.totalTokens ?? entry.usage?.totalTokens ?? 0),
				0,
			);
		assert.equal(next.usage.totalTokens, total);
		assert.ok(total > 0);
	},
);

await caseOf(
	"native context, skills and prompt templates obey explicit opt-outs",
	async () => {
		const skill = join(agent, "skills", "fixture-skill", "SKILL.md");
		await mkdir(dirname(skill), { recursive: true });
		await mkdir(join(agent, "prompts"), { recursive: true });
		await writeFile(
			skill,
			"---\nname: fixture-skill\ndescription: Fixture skill metadata.\n---\nSKILL_BODY_ON_DEMAND\n",
		);
		await writeFile(join(root, "AGENTS.md"), "WORKSPACE_CONTEXT_832\n");
		await writeFile(
			join(agent, "prompts", "fixture.md"),
			childPrompt({ text: "TEMPLATE_RESULT" }),
		);
		const normal = await call({
			prompt: childPrompt({ text: "context" }),
			tools: ["read"],
		});
		assert.equal(normal.outcome, "completed");
		assert.ok(normal.effective.skills?.includes(skill));
		assert.ok(normal.sessionFile);
		const system = (await entries(normal.sessionFile)).filter(
			(entry) => entry.message?.role === "system",
		);
		assert.match(JSON.stringify(system), /WORKSPACE_CONTEXT_832/u);
		assert.doesNotMatch(JSON.stringify(system), /SKILL_BODY_ON_DEMAND/u);
		const stripped = await call({
			prompt: childPrompt({ text: "stripped" }),
			noSkills: true,
			noContextFiles: true,
			tools: [],
		});
		assert.equal(stripped.outcome, "completed");
		assert.equal(stripped.effective.skills?.length, 0);
		assert.ok(stripped.sessionFile);
		assert.doesNotMatch(
			JSON.stringify(
				(await entries(stripped.sessionFile)).filter(
					(entry) => entry.message?.role === "system",
				),
			),
			/WORKSPACE_CONTEXT_832/u,
		);
		const template = await call({ prompt: "/fixture", tools: [] });
		assert.equal(template.outcome, "completed");
		assert.equal(answer(template), "TEMPLATE_RESULT");
		await rm(join(root, "AGENTS.md"));
	},
);
await caseOf(
	"inherited hook-only permissions survive cwd change; untrusted project code stays unloaded",
	async () => {
		const guard = join(agent, "extensions", "fixture-guard", "index.ts"),
			target = join(root, "guarded"),
			marker = join(root, "untrusted-marker");
		await mkdir(dirname(guard), { recursive: true });
		await mkdir(join(target, ".pi", "extensions"), { recursive: true });
		await writeFile(
			guard,
			"export default pi=>pi.on('tool_call',event=>event.toolName==='write'?{block:true,reason:'GUARD_BLOCKED'}:undefined);",
		);
		await writeFile(
			join(target, ".pi", "extensions", "untrusted.ts"),
			`import {writeFileSync} from 'node:fs';export default()=>writeFileSync(${JSON.stringify(marker)},'loaded');`,
		);
		const value = await call({
			cwd: target,
			prompt: childPrompt({
				kind: "tool",
				tool: "write",
				args: { path: "forbidden.txt", content: "not allowed" },
			}),
			tools: ["write"],
		});
		assert.equal(value.outcome, "completed");
		assert.match(answer(value), /GUARD_BLOCKED/u);
		assert.equal(
			await stat(join(target, "forbidden.txt")).then(
				() => true,
				() => false,
			),
			false,
		);
		assert.equal(
			await stat(marker).then(
				() => true,
				() => false,
			),
			false,
		);
		await rm(guard);
	},
);
await caseOf(
	"native tool modifiers, glob exclusions and unavailable-tool diagnostics",
	async () => {
		const evidence = join(root, "modifier-evidence");
		await writeFile(evidence, "MODIFIER_READ_41");
		const selected = await call({
			prompt: childPrompt({
				kind: "tool",
				tool: "read",
				args: { path: evidence },
			}),
			tools: ["-write", "+read"],
		});
		assert.equal(selected.outcome, "completed");
		assert.ok(!selected.effective.tools.includes("write"));
		assert.match(answer(selected), /MODIFIER_READ_41/u);
		const path = join(agent, "pipi", "dynamic", "marker", "index.ts");
		const excluded = await call({
			prompt: childPrompt({ text: "globbed" }),
			extensions: [path, "!" + join(dirname(path), "*.ts")],
			tools: [],
		});
		assert.equal(excluded.outcome, "completed");
		assert.ok(!excluded.effective.extensions.includes(path));
		const unavailable = await call({
			prompt: childPrompt({ text: "missing tool" }),
			tools: ["deleted_tool_foo"],
		});
		assert.equal(unavailable.outcome, "completed");
		assert.ok(!unavailable.effective.tools.includes("deleted_tool_foo"));
		assert.match(unavailable.diagnostics.join("\n"), /deleted_tool_foo/u);
	},
);
await caseOf(
	"provider errors preserve structured failure payload, including after valid output",
	async () => {
		const request = { prompt: childPrompt({ kind: "error" }), tools: [] };
		const invocation = await run(
			parentPrompt(
				[],
				`const value=await tools.pipi(${JSON.stringify(request)});text({observedOutcome:value.outcome});`,
			),
		);
		const error = taskResults(invocation.events);
		assert.equal(error.length, 1);
		assert.equal(error[0]?.outcome, "error");
		assert.match(
			JSON.stringify(invocation.events),
			/observedOutcome[^}]*error/u,
		);
		const after = await call({
			prompt: childPrompt({ kind: "post-error", value: 9 }),
			outputSchema: integerSchema,
			tools: ["read"],
		});
		assert.equal(after.outcome, "error");
		assert.equal(after.output, 9);
	},
);
await caseOf(
	"large output stays complete for scripts while model-facing text is bounded",
	async () => {
		const text = "large-evidence ".repeat(6000);
		const invocation = await run(
			parentPrompt([{ prompt: childPrompt({ text }), tools: [] }]),
		);
		const value = taskResults(invocation.events)[0];
		assert.ok(value);
		assert.equal(value.outcome, "completed");
		assert.equal(answer(value), text);
		const completion = invocation.events.find(
			(event) =>
				event.type === "tool_execution_end" && event.toolName === "pipi",
		);
		assert.ok(completion?.result);
		const content = (
			completion.result as unknown as {
				content: Array<{ type: string; text?: string }>;
			}
		).content
			.filter((part) => part.type === "text")
			.map((part) => part.text)
			.join("");
		assert.ok(content.length < 55000);
		assert.match(content, /Truncated model-facing result/u);
	},
);

await caseOf(
	"RPC cancellation settles nested work, restores readiness and prevents late side effects",
	async () => {
		const extension = join(root, "delayed.ts"),
			started = join(root, "delay-started"),
			late = join(root, "late-write");
		await writeFile(
			extension,
			`import {writeFile} from 'node:fs/promises';import {setTimeout} from 'node:timers/promises';export default pi=>pi.registerTool({name:'delayed_write',label:'Delay',description:'Fixture',parameters:{type:'object',properties:{}},async execute(id,args,signal){await writeFile(${JSON.stringify(started)},String(Date.now()));await setTimeout(1000,undefined,{signal});await writeFile(${JSON.stringify(late)},'orphan');return {content:[],details:{}};}});`,
		);
		const proc = spawn(
			process.execPath,
			[...base, "--mode", "rpc", "--session-id", "cancel-" + randomUUID()],
			{ cwd: root, env, stdio: ["pipe", "pipe", "pipe"] },
		);
		let output = "";
		proc.stdout.on("data", (value: Buffer) => {
			output += value.toString();
		});
		proc.stderr.on("data", () => {});
		const send = (
			id: string,
			type: string,
			extra: Record<string, unknown> = {},
		) => proc.stdin.write(JSON.stringify({ id, type, ...extra }) + "\n");
		const response = (id: string) =>
			parseEvents(output).find(
				(event) => event.type === "response" && event.id === id,
			);
		try {
			const inner = {
				prompt: childPrompt({ kind: "tool", tool: "delayed_write" }),
				extensions: [extension],
				tools: ["delayed_write"],
			};
			send("start", "prompt", {
				message: parentPrompt([
					{
						prompt: childPrompt({ kind: "nested", request: inner }),
						tools: ["pipi"],
					},
				]),
			});
			await waitFor(
				async () =>
					stat(started).then(
						() => true,
						() => false,
					),
				"nested delayed tool started",
			);
			send("abort", "abort");
			await waitFor(() => !!response("abort"), "abort response");
			send("state", "get_state");
			await waitFor(() => !!response("state"), "idle state response");
			assert.equal(response("state")?.data?.["isStreaming"], false);
			const boundary = output.length;
			send("ready", "prompt", { message: "READY_CHECK" });
			await waitFor(
				() => output.slice(boundary).includes('"text":"READY"'),
				"new prompt completes after cancellation",
			);
			const deadline = Number(await readFile(started, "utf8")) + 1500;
			while (Date.now() < deadline) {
				assert.equal(
					await stat(late).then(
						() => true,
						() => false,
					),
					false,
					"cancelled descendant wrote after abort",
				);
				await delay(25);
			}
			assert.equal(
				await stat(late).then(
					() => true,
					() => false,
				),
				false,
			);
			const persisted = (await readdir(sessions)).filter((file) =>
				file.endsWith(".jsonl"),
			);
			let cancelledTool = false;
			for (const file of persisted)
				for (const entry of await entries(join(sessions, file)))
					if (
						entry.message?.toolName === "delayed_write" &&
						entry.message.isError
					)
						cancelledTool = true;
			assert.ok(
				cancelledTool,
				"native child transcript records cancelled delayed tool",
			);
		} finally {
			await stop(proc);
		}
	},
);

async function terminalCase(
	cols: number,
	rows: number,
	structured: boolean,
	streaming = false,
): Promise<void> {
	const request = {
		prompt: childPrompt(
			structured
				? { kind: "submit", value: { role: "assistant", answer: "ok" } }
				: streaming
					? { kind: "stream" }
					: { text: "TERMINAL_CHILD_RESULT" },
		),
		tools: [],
		...(structured ? { outputSchema: objectSchema } : {}),
	};
	const terminal = new Terminal({ cols, rows, allowProposedApi: true });
	const pty = spawnPty(
		process.execPath,
		[
			...base,
			"--session-id",
			"terminal-" + randomUUID(),
			parentPrompt([request]),
		],
		{
			cwd: root,
			env: { ...env, ...(streaming ? { PIPI_E2E_STREAM: "1" } : {}) },
			name: "xterm-256color",
			cols,
			rows,
		},
	);
	let exited = false;
	pty.onExit(() => {
		exited = true;
	});
	pty.onData((data) => terminal.write(data));
	const screen = async () => {
		await new Promise<void>((resolveWrite) => terminal.write("", resolveWrite));
		const buffer = terminal.buffer.active;
		return Array.from(
			{ length: rows },
			(_, i) =>
				buffer.getLine(buffer.viewportY + i)?.translateToString(true) ?? "",
		).join("\n");
	};
	try {
		if (streaming) {
			await waitFor(async () => {
				const value = await screen();
				return value.includes("STREAM_CHILD") && !value.includes("PARENT_OK");
			}, "visible intermediate streaming before completion");
		}
		await waitFor(
			async () => (await screen()).includes("PARENT_OK"),
			"terminal completed parent",
		);
		const collapsed = await screen();
		assert.match(collapsed, /completed/u);
		assert.match(collapsed, /tokens/u);
		pty.write("\u000f");
		await waitFor(
			async () => (await screen()).includes("Session"),
			"expanded session metadata",
		);
		const screens: string[] = [await screen()];
		pty.write("\u001b[1;5H");
		await delay(20);
		screens.push(await screen());
		for (let i = 0; i < 8; i++) {
			pty.write("\u001b[6~");
			await delay(20);
			screens.push(await screen());
			if (
				(await screen()).includes("PARENT_OK") &&
				(await screen()).includes("Session")
			)
				break;
		}
		const expanded = screens.join("\n");
		for (const text of [
			"PIPI_CHILD",
			"Config",
			"Model:",
			"Tools:",
			"Result",
			"Session",
		])
			assert.ok(expanded.includes(text), `expanded view missing ${text}`);
		if (structured) {
			assert.match(expanded, /answer/u);
			assert.match(expanded, /ok/u);
		}
		assert.doesNotMatch(
			expanded,
			/Error rendering|renderResult failed|Extension error/u,
		);
	} finally {
		pty.write("\u0004");
		await waitFor(() => exited, "terminal exits", 2000).catch(() =>
			pty.kill("SIGTERM"),
		);
		terminal.dispose();
	}
}
await caseOf("wide terminal collapsed → expanded ordinary result", () =>
	terminalCase(110, 40, false),
);
await caseOf("narrow terminal expanded application JSON role=assistant", () =>
	terminalCase(46, 22, true),
);
await caseOf("human-only streaming visible before completion", () =>
	terminalCase(100, 32, false, true),
);

const failed = reports.filter((report) => !report.ok);
process.stdout.write(
	JSON.stringify(
		{
			passed: reports.length - failed.length,
			failed: failed.length,
			cases: reports,
			artifacts: failed.length ? root : null,
		},
		null,
		2,
	) + "\n",
);
if (failed.length) process.exitCode = 1;
else await rm(root, { recursive: true, force: true });