Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/pipi/__tests__/contract.test.ts

Raw
import {
	mkdir,
	mkdtemp,
	readdir,
	readFile,
	rm,
	stat,
	writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import {
	createAssistantMessageEventStream,
	type FauxResponseStep,
	fauxAssistantMessage,
	fauxProvider,
	fauxToolCall,
	getCurrentSystemPrompt,
	getCurrentTools,
	type JsonObject,
} from "@earendil-works/pi-ai";
import {
	AssistantMessageComponent,
	createCodemodeExtension,
	initTheme,
	SessionManager,
	Theme,
} from "@earendil-works/pi-coding-agent";
import {
	Box,
	stripTerminalSequences,
	TuiMainScreen,
	visibleWidth,
} from "@earendil-works/pi-tui";
import xterm from "@xterm/headless";
import { afterEach, describe, expect, it } from "vitest";
import {
	createPiSession,
	type PiSession,
} from "../../../test/fixture/create-pi-session.ts";
import { LiveTaskPreview } from "../live.ts";
import { type Progress, runTask, type TaskResult } from "../runtime.ts";

let fixture: PiSession | undefined;
afterEach(async () => {
	await fixture?.dispose();
	fixture = undefined;
});
function previewTui(): TuiMainScreen {
	const noop = () => {};
	const tui = new TuiMainScreen({
		start: noop,
		stop: noop,
		drainInput: async () => {},
		write: noop,
		columns: 120,
		rows: 40,
		kittyProtocolActive: false,
		moveBy: noop,
		hideCursor: noop,
		showCursor: noop,
		clearLine: noop,
		clearFromCursor: noop,
		clearScreen: noop,
		setTitle: noop,
		setProgress: noop,
		setProgramStatus: noop,
	});
	tui.stop();
	return tui;
}
function call(args: JsonObject) {
	return fauxAssistantMessage(fauxToolCall("pipi", args, { id: "task" }));
}
function submit(output: JsonObject) {
	return fauxAssistantMessage(
		fauxToolCall("pipi_output", { output }, { id: "submit" }),
	);
}
async function setup(
	responses: NonNullable<Parameters<typeof createPiSession>[0]["responses"]>,
) {
	fixture = await createPiSession({
		extensionPaths: [resolve("extensions/pipi/index.ts")],
		responses,
		env: { PI_PIPI_DEBUG: "0" },
		userSettings: {
			defaultTools: ["read", "pipi"],
			retry: { enabled: false },
			cacheWarming: "off",
		},
	});
	expect(fixture.errors).toEqual([]);
	return fixture;
}
function result(f: PiSession): TaskResult {
	const message = f.session.messages.findLast(
		(message) => message.role === "toolResult" && message.toolName === "pipi",
	);
	if (message?.role !== "toolResult") throw new Error("Missing pipi result");
	return JSON.parse(
		message.content
			.filter((part) => part.type === "text")
			.map((part) => part.text)
			.join(""),
	) as TaskResult;
}
it.each(["provider", "https://models.example/v1"])(
	"runs exact slash-containing model identities for %s and rejects substitutes",
	async (provider) => {
		const f = await setup([]);
		const id = "org/worker";
		const faux = fauxProvider({ provider, models: [{ id }] });
		faux.setResponses([fauxAssistantMessage("Resolved exact identity")]);
		const ctx = f.session.extensionRunner.createToolContext(
			"model-reference",
			undefined,
		);
		ctx.modelRegistry.registerProvider(faux.provider);
		const invoke = (model: string) =>
			runTask(
				ctx,
				{
					prompt: "Reply",
					model,
					noExtensions: true,
					noSkills: true,
					noContextFiles: true,
				},
				{ tools: [], availableTools: [], extensions: [], prompt: {} },
				{ modelTiers: {}, providerModelTiers: {} },
				14_400_000,
			);
		const value = await invoke(`${provider}/${id}`);
		expect(value.outcome).toBe("completed");
		expect(value.effective.model).toBe(`${provider}/${id}`);
		expect(value.output).toMatchObject({
			provider,
			model: id,
			content: [{ type: "text", text: "Resolved exact identity" }],
		});
		for (const ref of [
			id,
			`${provider}/org/work`,
			`${provider}/${id}:high`,
			`${provider.toUpperCase()}/${id}`,
			`/${provider}/${id}`,
		]) {
			await expect(invoke(ref)).resolves.toMatchObject({
				outcome: "error",
				diagnostics: [
					ref.startsWith("/")
						? "pipi: model must be provider/model"
						: `pipi: model unavailable: ${ref}`,
				],
				turns: 0,
			});
		}
		await expect(invoke("worker")).resolves.toMatchObject({
			outcome: "error",
			diagnostics: ["pipi: model must be provider/model"],
			turns: 0,
		});
	},
);

it("classifies child-hidden, inactive and missing tools after startup", async () => {
	const directory = await mkdtemp(join(tmpdir(), "pipi-tool-visibility-"));
	const extension = join(directory, "visibility.ts");
	try {
		await writeFile(
			extension,
			`export default pi => {
				const tool = name => ({name, label:name, description:name, parameters:{type:"object",properties:{}}, async execute(){return {content:[],details:{}}}});
				pi.registerTool(tool("hidden_probe"));
				pi.registerTool(tool("inactive_probe"));
				pi.on("session_start", (_event, ctx) => {
					if (!ctx.sessionManager.getHeader()?.parentSession) return;
					pi.registerTool({...tool("hidden_probe"), exposure:"hidden"});
					pi.setActiveTools(pi.getActiveTools().filter(name => name !== "inactive_probe"));
				});
			}`,
		);
		fixture = await createPiSession({
			extensionPaths: [resolve("extensions/pipi/index.ts"), extension],
			sessionManager: SessionManager.create(
				directory,
				join(directory, "sessions"),
			),
			userSettings: {
				defaultTools: ["read", "pipi", "hidden_probe", "inactive_probe"],
				retry: { enabled: false },
				cacheWarming: "off",
			},
			env: { PI_PIPI_DEBUG: "0" },
		});
		const f = fixture;
		for (const tools of [
			undefined,
			["+read"],
			["hidden_probe"],
			["+hidden_probe"],
			["inactive_probe", "missing_probe"],
		]) {
			f.setResponses([
				call({ prompt: "Inspect visibility", ...(tools ? { tools } : {}) }),
				fauxAssistantMessage("child"),
				fauxAssistantMessage("parent"),
			]);
			await f.session.prompt("Delegate");
			const value = result(f);
			expect(value.outcome).toBe("completed");
			expect(value.effective.tools).not.toContain("hidden_probe");
			expect(value.effective.tools).not.toContain("inactive_probe");
			const explicitHidden = tools?.some((name) =>
				name.endsWith("hidden_probe"),
			);
			if (!tools || tools[0]?.startsWith("+")) {
				expect(value.diagnostics.join("\n")).toContain("inactive_probe");
			}
			if (explicitHidden) {
				expect(value.effective.excludedTools ?? []).not.toContain(
					"hidden_probe",
				);
				expect(value.diagnostics.join("\n")).toMatch(/hidden.*hidden_probe/);
			} else if (!tools || tools[0] === "+read") {
				expect(value.effective.excludedTools).toEqual(["hidden_probe"]);
				expect(value.diagnostics.join("\n")).not.toContain("hidden_probe");
			} else {
				expect(value.diagnostics.join("\n")).toMatch(
					/inactive.*inactive_probe/,
				);
				expect(value.diagnostics.join("\n")).toMatch(
					/unavailable.*missing_probe/,
				);
			}
		}
		expect(f.errors).toEqual([]);
	} finally {
		await fixture?.dispose();
		fixture = undefined;
		await rm(directory, { recursive: true, force: true });
	}
});

describe("native pipi child task", () => {
	it("loads and binds a named child, returns native assistant output and leaves child usage in the child ledger", async () => {
		const f = await setup([
			call({
				prompt: "Inspect one thing",
				tools: ["read"],
				noExtensions: true,
			}),
			fauxAssistantMessage("child answer"),
			fauxAssistantMessage("parent done"),
		]);
		await f.session.prompt("Delegate the task");
		expect(f.errors).toEqual([]);
		const value = result(f);
		expect(value.outcome).toBe("completed");
		expect(value.output).toMatchObject({
			role: "assistant",
			content: [{ type: "text", text: "child answer" }],
		});
		expect(value.sessionId).toBeTruthy();
		expect(value.sessionFile).toBeTruthy();
		expect(value.turns).toBe(1);
		const transcript = await readFile(value.sessionFile as string, "utf8");
		expect(transcript).toContain("child answer");
		expect(
			SessionManager.open(value.sessionFile as string).getSessionName(),
		).toBe("PiPi · task · Inspect one thing");
		expect(value.usage.totalTokens).toBeGreaterThan(0);
		const tool = f.session.messages.find(
			(message) => message.role === "toolResult" && message.toolName === "pipi",
		);
		if (tool?.role !== "toolResult")
			throw new Error("Missing pipi tool result");
		expect(tool.usage).toBeUndefined();
		const parentTokens = f.session.messages
			.filter((message) => message.role === "assistant")
			.reduce((sum, message) => sum + message.usage.totalTokens, 0);
		expect(f.session.getSessionStats().tokens.total).toBe(parentTokens);
	});
	it("validates output, reminds once and terminates through the native submission tool", async () => {
		const schema = {
			$defs: { integer: { type: "integer" } },
			type: "object",
			properties: { answer: { $ref: "#/$defs/integer" } },
			required: ["answer"],
			additionalProperties: false,
		};
		const f = await setup([
			call({
				prompt: "Return 42",
				tools: [],
				noExtensions: true,
				outputSchema: schema,
			}),
			fauxAssistantMessage(
				fauxToolCall(
					"pipi_output",
					{ output: { answer: "invalid" } },
					{ id: "invalid-ref" },
				),
			),
			submit({ answer: 42 }),
			fauxAssistantMessage("parent done"),
		]);
		await f.session.prompt("Delegate");
		const value = result(f);
		expect(value.outcome).toBe("completed");
		expect(value.output).toEqual({ answer: 42 });
		expect(value.turns).toBe(2);
	});
	it("fails after the one reminder rather than accepting ordinary prose", async () => {
		const f = await setup([
			call({
				prompt: "Return structured",
				tools: [],
				noExtensions: true,
				outputSchema: { type: "string" },
			}),
			fauxAssistantMessage("first"),
			fauxAssistantMessage("second"),
			fauxAssistantMessage("parent done"),
		]);
		await f.session.prompt("Delegate");
		expect(result(f)).toMatchObject({
			outcome: "failed",
			output: null,
			turns: 2,
		});
	});
});

async function preset(
	f: PiSession,
	name: string,
	config: unknown,
	body?: string,
) {
	const directory = join(f.agentDir, "pipi", "agents", name);
	await mkdir(directory, { recursive: true });
	await writeFile(join(directory, "config.json"), JSON.stringify(config));
	if (body !== undefined)
		await writeFile(join(directory, "instructions.md"), body);
}
describe("output settlement, resume and native usage", () => {
	it("excludes resumed and forked history, resets old output submissions and uses current preset", async () => {
		const f = await setup([]);
		await preset(f, "counter", {
			description: "Count evidence",
			tools: [],
			noExtensions: true,
			outputSchema: { type: "integer" },
		});
		f.setResponses([
			call({ prompt: "First", agent: "counter" }),
			{
				...fauxAssistantMessage(
					fauxToolCall("pipi_output", { output: 7 }, { id: "first" }),
				),
			},
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("First task");
		const first = result(f);
		expect(first).toMatchObject({
			outcome: "completed",
			output: 7,
			turns: 1,
			usage: expect.objectContaining({ totalTokens: expect.any(Number) }),
		});
		const file = first.sessionFile as string;
		f.setResponses([
			call({
				prompt: "Continue without submitting",
				agent: "counter",
				sessionId: file,
			}),
			fauxAssistantMessage("one"),
			fauxAssistantMessage("two"),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Continue task");
		expect(result(f)).toMatchObject({
			outcome: "failed",
			output: null,
			sessionId: first.sessionId,
			usage: expect.objectContaining({ totalTokens: expect.any(Number) }),
		});
		const resumed = result(f);
		const resumedEntries = (await readFile(file, "utf8"))
			.trim()
			.split("\n")
			.map((line) => JSON.parse(line));
		const newMessages = resumedEntries
			.filter((entry) => entry.message?.role === "assistant")
			.slice(-2);
		expect(resumed.usage.totalTokens).toBe(
			newMessages.reduce(
				(sum, entry) => sum + entry.message.usage.totalTokens,
				0,
			),
		);
		await preset(f, "counter", {
			description: "Now ordinary output",
			tools: [],
			noExtensions: true,
		});
		f.setResponses([
			call({
				prompt: "Fork with current config",
				agent: "counter",
				sessionId: file,
				fork: true,
			}),
			fauxAssistantMessage("fork answer"),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Fork task");
		const fork = result(f);
		expect(fork.outcome).toBe("completed");
		expect(fork.sessionId).not.toBe(first.sessionId);
		expect(fork.usage.totalTokens).toBeGreaterThan(0);
		const forkEntries = (await readFile(fork.sessionFile as string, "utf8"))
			.trim()
			.split("\n")
			.map((line) => JSON.parse(line));
		const forkAssistant = forkEntries
			.filter((entry) => entry.message?.role === "assistant")
			.at(-1).message;
		expect(fork.usage).toEqual({
			...forkAssistant.usage,
			cost: { ...forkAssistant.usage.cost, totalFromProvider: false },
		});
		expect(fork.output).toMatchObject({
			role: "assistant",
			content: [{ text: "fork answer" }],
		});
	});
	it("returns forked billed work once and keeps new legacy work estimated", async () => {
		const f = await setup([]);
		f.setResponses([
			call({ prompt: "Create fork baseline", tools: [] }),
			fauxAssistantMessage("baseline"),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Create baseline");
		const baseline = result(f);
		expect(baseline.usage.cost.totalFromProvider).toBe(false);
		const provider = f.session.modelRuntime.getRegisteredProviderConfig("faux");
		if (!provider?.streamSimple) throw new Error("Missing faux stream");
		const streamSimple = provider.streamSimple;
		f.session.modelRuntime.registerProvider("faux", {
			...provider,
			streamSimple(model, context, options) {
				const source = streamSimple(model, context, options);
				const output = createAssistantMessageEventStream();
				void (async () => {
					for await (const event of source) {
						if (event.type !== "done") {
							output.push(event);
							continue;
						}
						const message = {
							...event.message,
							usage: {
								...event.message.usage,
								cost: {
									...event.message.usage.cost,
									total: 0.123,
									totalFromProvider: true,
								},
							},
						};
						output.push({ ...event, message });
						output.end(message);
					}
				})();
				return output;
			},
		});
		f.setResponses([
			call({
				prompt: "Fork billed work",
				tools: [],
				sessionId: baseline.sessionFile as string,
				fork: true,
			}),
			fauxAssistantMessage("billed fork"),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Fork billed work");
		const billed = result(f);
		const childEntries = (await readFile(billed.sessionFile as string, "utf8"))
			.trim()
			.split("\n")
			.map((line) => JSON.parse(line));
		const childUsage = childEntries
			.filter((entry) => entry.message?.role === "assistant")
			.at(-1).message.usage;
		expect(billed.usage).toEqual(childUsage);
		expect(
			SessionManager.open(billed.sessionFile as string).getSessionName(),
		).toBe("PiPi · task · Fork billed work");
		expect(billed.usage.cost).toMatchObject({
			total: 0.123,
			totalFromProvider: true,
		});
		const parentTool = f.session.sessionManager
			.getEntries()
			.findLast(
				(entry) =>
					entry.type === "message" &&
					entry.message.role === "toolResult" &&
					entry.message.toolName === "pipi",
			);
		if (
			parentTool?.type !== "message" ||
			parentTool.message.role !== "toolResult"
		)
			throw new Error("Missing persisted pipi tool result");
		expect(parentTool.message.usage).toBeUndefined();
		f.session.modelRuntime.registerProvider("faux", provider);
		f.setResponses([
			call({
				prompt: "Fork legacy work",
				tools: [],
				sessionId: baseline.sessionFile as string,
				fork: true,
			}),
			fauxAssistantMessage("legacy fork"),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Fork legacy work");
		expect(result(f).usage.cost.totalFromProvider).toBe(false);
	});
	it("keeps last valid output across invalid submission then error; never declares success merely for valid JSON", async () => {
		const f = await setup([]);
		const extension = join(f.cwd, "settle.ts");
		await writeFile(
			extension,
			`export default pi => {pi.registerTool({name:"keep_running",label:"Continue",description:"Continue task",parameters:{type:"object",properties:{}},async execute(){return {content:[],details:{}}}})}`,
		);
		f.setResponses([
			call({
				prompt: "Complete",
				tools: ["keep_running"],
				extensions: [extension],
				noExtensions: true,
				outputSchema: { type: "integer" },
			}),
			fauxAssistantMessage([
				fauxToolCall("pipi_output", { output: 1 }, { id: "valid-one" }),
				fauxToolCall("pipi_output", { output: 2 }, { id: "valid-two" }),
				fauxToolCall("pipi_output", { output: "invalid" }, { id: "invalid" }),
				fauxToolCall("keep_running", {}, { id: "continue" }),
			]),
			fauxAssistantMessage("", {
				stopReason: "error",
				errorMessage: "scripted child error",
			}),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Delegate");
		const value = result(f);
		expect(value.outcome).toBe("error");
		expect(value.output).toBe(2);
		const entries = (await readFile(value.sessionFile as string, "utf8"))
			.trim()
			.split("\n")
			.map((line) => JSON.parse(line));
		expect(
			entries.some(
				(entry) =>
					entry.message?.toolCallId === "invalid" && entry.message.isError,
			),
		).toBe(true);
		expect(
			entries.some((entry) => entry.customType === "pipi_output_reminder"),
		).toBe(false);
	});
	it("null explicitly disables preset schema and scalar/array outputs use native wrapped validation", async () => {
		const f = await setup([]);
		await preset(f, "preset", {
			description: "Array response",
			tools: [],
			noExtensions: true,
			outputSchema: { type: "array", items: { type: "integer" } },
		});
		f.setResponses([
			call({ agent: "preset", prompt: "Array" }),
			fauxAssistantMessage(
				fauxToolCall("pipi_output", { output: [1, 2, 3] }, { id: "array" }),
			),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Array task");
		expect(result(f).output).toEqual([1, 2, 3]);
		f.setResponses([
			call({ agent: "preset", prompt: "Ordinary", outputSchema: null }),
			fauxAssistantMessage("ordinary answer"),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Disable schema");
		expect(result(f).output).toMatchObject({
			role: "assistant",
			content: [{ text: "ordinary answer" }],
		});
	});
	it("propagates caller cancellation through recursive children without an output reminder", async () => {
		const f = await setup([]);
		let started!: () => void;
		const running = new Promise<void>((resolve) => {
			started = resolve;
		});
		let childSignal: AbortSignal | undefined;
		const wait: FauxResponseStep = async (_context, options) => {
			childSignal = options?.signal;
			started();
			await new Promise<void>((resolve) => {
				if (options?.signal?.aborted) resolve();
				else
					options?.signal?.addEventListener("abort", () => resolve(), {
						once: true,
					});
			});
			return fauxAssistantMessage("", { stopReason: "aborted" });
		};
		f.setResponses([
			call({
				prompt: "Child delegates",
				tools: ["pipi"],
				outputSchema: { type: "integer" },
			}),
			call({
				prompt: "Grandchild waits",
				tools: [],
				outputSchema: { type: "integer" },
			}),
			wait,
		]);
		const pending = f.session.prompt("Delegate recursively");
		await running;
		await f.session.abort();
		await pending;
		expect(childSignal?.aborted).toBe(true);
		const files = await readdir(join(f.agentDir, "sessions"), {
			recursive: true,
		});
		const transcripts = await Promise.all(
			files
				.filter((file) => file.endsWith(".jsonl"))
				.map((file) => readFile(join(f.agentDir, "sessions", file), "utf8")),
		);
		expect(
			transcripts.some((text) => text.includes('"stopReason":"aborted"')),
		).toBe(true);
		expect(
			transcripts.some((text) =>
				text.includes('"customType":"pipi_output_reminder"'),
			),
		).toBe(false);
	});
	it("aborts the child at the call deadline without a parent abort and preserves its session", async () => {
		const f = await setup([]);
		let childSignal: AbortSignal | undefined;
		const wait: FauxResponseStep = async (_context, options) => {
			childSignal = options?.signal;
			await new Promise<void>((resolve) => {
				if (options?.signal?.aborted) resolve();
				else
					options?.signal?.addEventListener("abort", () => resolve(), {
						once: true,
					});
			});
			return fauxAssistantMessage("", { stopReason: "aborted" });
		};
		f.setResponses([
			call({
				prompt: "Bounded child",
				timeoutMs: 300,
				tools: [],
				outputSchema: { type: "integer" },
			}),
			wait,
			fauxAssistantMessage("parent"),
		]);
		const started = Date.now();
		await f.session.prompt("Delegate with deadline");
		const value = result(f);
		expect(value.outcome).toBe("aborted");
		expect(childSignal?.aborted).toBe(true);
		expect(value.diagnostics.join("\n")).toContain("deadline");
		expect(value.sessionId).toBeTruthy();
		expect(value.sessionFile).toBeTruthy();
		expect(Date.now() - started).toBeLessThan(15_000);
	});
	it("returns bounded when the deadline fires while the child is wedged in a tool", async () => {
		const directory = await mkdtemp(join(tmpdir(), "pipi-deadline-wedge-"));
		const extension = join(directory, "wedged.ts");
		await writeFile(
			extension,
			`export default pi => {
				pi.registerTool({
					name: "wedged_probe",
					label: "wedged_probe",
					description: "never resolves and ignores abort",
					parameters: { type: "object", properties: {} },
					async execute() {
						await new Promise(() => {});
					},
				});
			};`,
		);
		try {
			const f = await setup([]);
			f.setResponses([
				call({
					prompt: "Wedge",
					timeoutMs: 300,
					tools: ["wedged_probe"],
					extensions: [extension],
				}),
				fauxAssistantMessage(fauxToolCall("wedged_probe", {}, { id: "wedge" })),
				fauxAssistantMessage("parent"),
			]);
			await f.session.prompt("Delegate into wedge");
			const value = result(f);
			expect(value.outcome).toBe("aborted");
			expect(value.diagnostics.join("\n")).toContain("deadline");
			expect(value.sessionFile).toBeTruthy();
			expect(value.durationMs).toBeLessThan(20_000);
		} finally {
			await rm(directory, { recursive: true, force: true });
		}
	});
});
describe("discovery, native capabilities and prompt controls", () => {
	it("preserves PFUI-backed inherited sections while applying child overrides and output guidance", async () => {
		fixture = await createPiSession({
			extensionPaths: [
				resolve("extensions/pfui/index.ts"),
				resolve("extensions/pipi/index.ts"),
			],
			extensionFactories: [
				(pi) => {
					pi.on("before_agent_start", (event) => {
						event.systemPromptOptions.sections["parent_only_marker"] =
							"parent-only marker";
					});
				},
			],
			responses: [],
			env: { PI_PIPI_DEBUG: "0" },
			userSettings: {
				defaultTools: ["read", "write", "pipi"],
				retry: { enabled: false },
				cacheWarming: "off",
			},
		});
		const f = fixture;
		expect(f.errors).toEqual([]);
		await preset(
			f,
			"pfui-reviewer",
			{
				description: "Review with PFUI",
				outputSchema: { type: "integer" },
			},
			"preset instruction marker",
		);
		let parentPrompt = "",
			childPrompt = "";
		f.setResponses([
			(context) => {
				parentPrompt = getCurrentSystemPrompt(context.messages);
				return call({
					agent: "pfui-reviewer",
					prompt: "Return integer",
					tools: ["read"],
					sections: {
						pipi_inherited_probe: "call section marker",
						tools: "explicit accessor override marker",
					},
					extensions: [resolve("extensions/pfui/index.ts")],
				});
			},
			(context) => {
				childPrompt = getCurrentSystemPrompt(context.messages);
				return fauxAssistantMessage(
					fauxToolCall("pipi_output", { output: 17 }, { id: "pfui-submit" }),
				);
			},
			fauxAssistantMessage("parent done"),
		]);
		await f.session.prompt("Delegate through PFUI");
		const value = result(f);
		expect(value.outcome).toBe("completed");
		expect(parentPrompt).toContain("parent-only marker");
		expect(childPrompt).toContain("parent-only marker");
		expect(childPrompt).toContain("call section marker");
		expect(childPrompt).toContain("explicit accessor override marker");
		expect(childPrompt).toContain("preset instruction marker");
		expect(childPrompt).toContain("<pipi_output>");
		expect(
			value.diagnostics.filter((item) =>
				item.startsWith("Child extension failed:"),
			),
		).toEqual([]);

		f.setResponses([
			(context) => {
				parentPrompt = getCurrentSystemPrompt(context.messages);
				return call({
					agent: "pfui-reviewer",
					prompt: "Keep inherited sections",
					tools: ["read"],
					extensions: [resolve("extensions/pfui/index.ts")],
				});
			},
			(context) => {
				childPrompt = getCurrentSystemPrompt(context.messages);
				return fauxAssistantMessage(
					fauxToolCall(
						"pipi_output",
						{ output: 23 },
						{ id: "inherited-submit" },
					),
				);
			},
			fauxAssistantMessage("parent done"),
		]);
		await f.session.prompt("Delegate without section overrides");
		const inherited = result(f);
		expect(inherited.outcome).toBe("completed");
		expect(inherited.output).toBe(23);
		expect(inherited.diagnostics).toEqual([]);
		expect(parentPrompt).toContain("- write:");
		const inheritedTools = parentPrompt.match(
			/<tools>\n[\s\S]*?\n<\/tools>/,
		)?.[0];
		expect(inheritedTools).toBeDefined();
		expect(childPrompt).toContain(inheritedTools);
		expect(childPrompt).toContain("parent-only marker");
		expect(childPrompt).toContain("preset instruction marker");
		expect(childPrompt).toContain("<pipi_output>");
	});
	it("retains actual child extension errors in diagnostics", async () => {
		const f = await setup([]);
		const path = join(f.cwd, "throwing-extension.ts");
		await writeFile(
			path,
			`export default pi => pi.on("before_agent_start", () => { throw new Error("fixture extension sentinel") });`,
		);
		f.setResponses([
			call({ prompt: "Continue", extensions: [path], tools: [] }),
			fauxAssistantMessage("child answer"),
			fauxAssistantMessage("parent done"),
		]);
		await f.session.prompt("Delegate with failing extension");
		const value = result(f);
		expect(value.outcome).toBe("completed");
		expect(value.diagnostics).toContain(
			`Child extension failed: ${path} (before_agent_start): fixture extension sentinel`,
		);
	});
	it("discovers descriptions without reading instructions, resolves current preset and loads ordinary dynamic extension with flags", async () => {
		const f = await setup([]);
		await preset(
			f,
			"reviewer",
			{
				description: "Review evidence",
				tools: ["read", "+evidence"],
				noExtensions: true,
				flags: { evidence: true },
				sections: { pipi_agent: "call replaces preset" },
			},
			"preset instructions",
		);
		await preset(f, "broken", {
			description: "Missing valid types",
			tools: 42,
		});
		const root = join(f.agentDir, "pipi", "dynamic", "evidence");
		await mkdir(root, { recursive: true });
		const path = join(root, "index.ts");
		await writeFile(
			path,
			`export default pi=>{pi.registerFlag("evidence",{type:"boolean",description:"Enable evidence"});pi.registerTool({name:"evidence",label:"Evidence",description:"Return evidence",parameters:{type:"object",properties:{}},async execute(){return {content:[{type:"text",text:String(pi.getFlag("evidence"))}],details:{}}}})}`,
		);
		let parentPrompt = "",
			childPrompt = "";
		f.setResponses([
			(context) => {
				parentPrompt = getCurrentSystemPrompt(context.messages);
				return call({
					prompt: "Inspect",
					agent: "reviewer",
					extensions: [path],
					tools: ["read", "+evidence", "-read"],
					sections: { pipi_agent: "replacement instructions" },
				});
			},
			(context) => {
				childPrompt = getCurrentSystemPrompt(context.messages);
				expect(
					getCurrentTools(context.messages).map((tool) => tool.name),
				).toEqual(["evidence"]);
				return fauxAssistantMessage(
					fauxToolCall("evidence", {}, { id: "probe" }),
				);
			},
			fauxAssistantMessage("evidence inspected"),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Use reusable agent");
		const value = result(f);
		expect(value.outcome).toBe("completed");
		expect(parentPrompt).toContain("Review evidence");
		expect(parentPrompt).not.toContain("preset instructions");
		expect(parentPrompt).toContain("evidence/index.ts");
		expect(childPrompt).toContain("replacement instructions");
		expect(childPrompt).not.toContain("preset instructions");
		expect(value.diagnostics).toContain(
			'Preset "broken" unavailable: invalid config.json',
		);
		expect(value.effective.tools).toEqual(["evidence"]);
		const transcript = await readFile(value.sessionFile as string, "utf8");
		const entries = transcript
			.trim()
			.split("\n")
			.map((line) => JSON.parse(line));
		expect(
			entries.some(
				(entry) =>
					entry.message?.toolName === "evidence" &&
					entry.message.content[0].text === "true",
			),
		).toBe(true);
	});
	it("uses native fresh resource instructions and protects output section with forced prompts", async () => {
		const f = await setup([]);
		await writeFile(join(f.cwd, "AGENTS.md"), "Project instruction evidence");
		let system = "";
		f.setResponses([
			call({
				prompt: "Forced",
				tools: [],
				forceSystemPrompt: "Forced task instruction",
				outputSchema: { type: "integer" },
				noExtensions: true,
			}),
			(context) => {
				system = getCurrentSystemPrompt(context.messages);
				return fauxAssistantMessage(
					fauxToolCall("pipi_output", { output: 3 }, { id: "forced" }),
				);
			},
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Delegate");
		expect(result(f)).toMatchObject({ outcome: "completed", output: 3 });
		expect(system).toContain("Forced task instruction");
		expect(system).toContain("<pipi_output>");
	});
	it("rejects unknown preset instead of silently running a fresh generic child", async () => {
		const f = await setup([
			call({ prompt: "Do something", agent: "does-not-exist" }),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Delegate");
		const tool = f.session.messages.find(
			(message) => message.role === "toolResult" && message.toolName === "pipi",
		);
		expect(tool).toMatchObject({ isError: true });
		expect(f.providerState.callCount).toBe(2);
	});
});

describe("independent native calls", () => {
	it("keeps schema submission state local across concurrent tool calls", async () => {
		const f = await setup([]);
		let active = 0;
		let ready!: () => void;
		const barrier = new Promise<void>((resolve) => {
			ready = resolve;
		});
		const respond =
			(answer: number): FauxResponseStep =>
			async () => {
				active++;
				if (active === 2) ready();
				await barrier;
				return fauxAssistantMessage(
					fauxToolCall(
						"pipi_output",
						{ output: answer },
						{ id: "output-" + answer },
					),
				);
			};
		f.setResponses([
			fauxAssistantMessage([
				fauxToolCall(
					"pipi",
					{ prompt: "One", tools: [], outputSchema: { type: "integer" } },
					{ id: "one" },
				),
				fauxToolCall(
					"pipi",
					{ prompt: "Two", tools: [], outputSchema: { type: "integer" } },
					{ id: "two" },
				),
			]),
			respond(1),
			respond(2),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Delegate concurrently");
		const results = f.session.messages
			.filter(
				(message) =>
					message.role === "toolResult" && message.toolName === "pipi",
			)
			.map((message) => {
				if (message.role !== "toolResult")
					throw new Error("Unexpected message");
				return JSON.parse(
					message.content
						.filter((part) => part.type === "text")
						.map((part) => part.text)
						.join(""),
				) as TaskResult;
			});
		expect(active).toBe(2);
		expect(results.map((value) => value.output).sort()).toEqual([1, 2]);
		expect(new Set(results.map((value) => value.sessionId)).size).toBe(2);
		expect(results.every((value) => value.outcome === "completed")).toBe(true);
	});
	it("leaves persisted descendant usage in the descendant session", async () => {
		const f = await setup([
			call({ prompt: "Outer task", tools: ["pipi"] }),
			call({ prompt: "Inner task", tools: [] }),
			fauxAssistantMessage("grandchild result"),
			fauxAssistantMessage("child result"),
			fauxAssistantMessage("parent result"),
		]);
		await f.session.prompt("Delegate recursively");
		const outer = result(f);
		expect(outer.outcome).toBe("completed");
		const entries = (await readFile(outer.sessionFile as string, "utf8"))
			.trim()
			.split("\n")
			.map((line) => JSON.parse(line));
		const messages = entries
			.filter((entry) => entry.message)
			.map((entry) => entry.message);
		const nested = messages.find(
			(message) => message.role === "toolResult" && message.toolName === "pipi",
		);
		expect(nested.usage).toBeUndefined();
		const assistantTokens = messages
			.filter((message) => message.role === "assistant")
			.reduce((sum, message) => sum + message.usage.totalTokens, 0);
		expect(outer.usage.totalTokens).toBe(assistantTokens);
	});
	it("rediscovers cwd instructions and skill resources; custom and appended prompts retain native loader context", async () => {
		const f = await setup([]);
		const workspace = join(f.cwd, "other");
		const skill = join(f.agentDir, "skills", "inspect");
		await mkdir(workspace, { recursive: true });
		await mkdir(skill, { recursive: true });
		await writeFile(join(workspace, "AGENTS.md"), "Child workspace marker");
		await writeFile(
			join(skill, "SKILL.md"),
			"---\nname: inspect\ndescription: Inspect deterministic evidence.\n---\nSkill body read on demand",
		);
		let system = "";
		f.setResponses([
			call({
				prompt: "Inspect",
				cwd: workspace,
				tools: ["read"],
				customPrompt: "Custom prefix marker",
				appendSystemPrompt: "Append marker",
				sections: { assignment: "Section marker" },
			}),
			(context) => {
				system = getCurrentSystemPrompt(context.messages);
				return fauxAssistantMessage("child");
			},
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Delegate into another cwd");
		expect(result(f)).toMatchObject({
			outcome: "completed",
			effective: { cwd: workspace },
		});
		expect(system).toContain("Custom prefix marker");
		expect(system).toContain("Append marker");
		expect(system).toContain("Section marker");
		expect(system).toContain("Child workspace marker");
		expect(system).toContain(join(skill, "SKILL.md"));
		expect(system).not.toContain("Skill body read on demand");
	});
});

it("loads additive resources and applies native call-local selectors", async () => {
	const f = await setup([]);
	const path = join(f.cwd, "resource.ts");
	await writeFile(
		path,
		`export default pi=>{pi.registerTool({name:"resource_probe",label:"Resource",description:"Resource evidence",parameters:{type:"object",properties:{}},async execute(){return {content:[],details:{}}}})}`,
	);
	f.setResponses([
		call({ prompt: "Include", extensions: [path], noExtensions: true }),
		fauxAssistantMessage("included"),
		fauxAssistantMessage("parent"),
	]);
	await f.session.prompt("Include resource");
	expect(result(f).effective.tools).toContain("resource_probe");
	for (const selector of ["-" + path, "!" + join(f.cwd, "*.ts")]) {
		f.setResponses([
			call({
				prompt: "Exclude",
				extensions: [path, selector],
				noExtensions: true,
			}),
			fauxAssistantMessage("excluded"),
			fauxAssistantMessage("parent"),
		]);
		await f.session.prompt("Exclude resource");
		expect(result(f)).toMatchObject({ outcome: "completed" });
		expect(result(f).effective.tools).not.toContain("resource_probe");
	}
});

it("exposes the stable structured envelope through native scripted parent tool calls", async () => {
	fixture = await createPiSession({
		extensionPaths: [resolve("extensions/pipi/index.ts")],
		extensionFactories: [createCodemodeExtension()],
		env: { PI_PIPI_DEBUG: "0" },
		userSettings: {
			defaultTools: ["codemode", "pipi"],
			retry: { enabled: false },
			cacheWarming: "off",
		},
		responses: [
			fauxAssistantMessage(
				fauxToolCall(
					"codemode",
					{
						code: 'const result = await tools.pipi({ prompt: "Child", tools: [] }); text({ outcome: result.outcome, sessionId: result.sessionId, role: result.output.role });',
					},
					{ id: "script" },
				),
			),
			fauxAssistantMessage("child answer"),
			fauxAssistantMessage("parent answer"),
		],
	});
	expect(fixture.errors).toEqual([]);
	await fixture.session.prompt("Delegate from a script");
	const tool = fixture.session.messages.find(
		(message) =>
			message.role === "toolResult" && message.toolName === "codemode",
	);
	expect(tool).toMatchObject({ isError: false });
	if (tool?.role !== "toolResult")
		throw new Error("Missing scripted tool result");
	expect(tool.content).toEqual(
		expect.arrayContaining([
			expect.objectContaining({
				type: "text",
				text: expect.stringContaining('"outcome":"completed"'),
			}),
		]),
	);
	expect(tool.usage).toBeUndefined();
});
it("includes new auxiliary, summary and nested tool usage while excluding historical entries", async () => {
	const f = await setup([]);
	const { newUsage, emptyUsage } = await import("../runtime.ts");
	const manager = SessionManager.inMemory(f.cwd);
	const usage = (tokens: number, totalFromProvider?: boolean) => ({
		...emptyUsage(),
		input: tokens,
		totalTokens: tokens,
		cost: {
			...emptyUsage().cost,
			total: tokens / 100,
			...(totalFromProvider === undefined ? {} : { totalFromProvider }),
		},
	});
	manager.appendMessage({
		...fauxAssistantMessage("old"),
		usage: usage(100, false),
	});
	const before = new Set(manager.getEntries().map((entry) => entry.id));
	manager.appendUsage("test", "faux", "faux-model", usage(3, true));
	manager.appendCompaction(
		"compact",
		null,
		0,
		undefined,
		false,
		usage(5, true),
	);
	manager.branchWithSummary(null, "summary", undefined, false, usage(7, true));
	manager.appendMessage({
		role: "toolResult",
		toolCallId: "nested",
		toolName: "nested",
		content: [],
		isError: false,
		timestamp: Date.now(),
		usage: usage(11, true),
	});
	manager.appendMessage({
		...fauxAssistantMessage("new"),
		usage: usage(13, true),
	});
	expect(newUsage(manager.getEntries(), before)).toMatchObject({
		totalTokens: 39,
		cost: { total: 0.39, totalFromProvider: true },
	});
	const mixed = SessionManager.inMemory(f.cwd);
	const mixedBefore = new Set(mixed.getEntries().map((entry) => entry.id));
	mixed.appendUsage("test", "faux", "faux-model", usage(1, true));
	mixed.appendUsage("test", "faux", "faux-model", usage(2));
	expect(newUsage(mixed.getEntries(), mixedBefore).cost).toMatchObject({
		total: 0.03,
		totalFromProvider: false,
	});
	const zero = SessionManager.inMemory(f.cwd);
	const zeroBefore = new Set(zero.getEntries().map((entry) => entry.id));
	zero.appendUsage("test", "faux", "faux-model", usage(0, true));
	expect(newUsage(zero.getEntries(), zeroBefore).cost).toMatchObject({
		total: 0,
		totalFromProvider: true,
	});
});

it("lets call tiers override preset models and combines additive preset/call resource lists", async () => {
	const f = await setup([]);
	await writeFile(
		join(f.agentDir, "settings.json"),
		JSON.stringify({
			pipi: { modelTiers: { tiny: { model: "faux/faux-model" } } },
			retry: { enabled: false },
			cacheWarming: "off",
		}),
	);
	const paths = [join(f.cwd, "first.ts"), join(f.cwd, "second.ts")] as const;
	for (const [index, path] of paths.entries())
		await writeFile(
			path,
			`export default pi=>{pi.registerTool({name:"resource_${index}",label:"Resource",description:"Resource",parameters:{type:"object",properties:{}},async execute(){return {content:[],details:{}}}})}`,
		);
	await preset(f, "configured", {
		description: "Configured task",
		model: "missing/preset-model",
		extensions: [paths[0]],
		noExtensions: true,
	});
	f.setResponses([
		call({
			prompt: "Override model tier",
			agent: "configured",
			tier: "tiny",
			extensions: [paths[1]],
		}),
		fauxAssistantMessage("child"),
		fauxAssistantMessage("parent"),
	]);
	await f.session.prompt("Delegate");
	expect(result(f)).toMatchObject({
		outcome: "completed",
		effective: { model: "faux/faux-model" },
	});
	expect(result(f).effective.tools).toEqual(
		expect.arrayContaining(["resource_0", "resource_1"]),
	);
});

it("inherits the PiPi outer background for native and fallback tool previews", async () => {
	initTheme("dark", false);
	const f = await setup([]);
	const tui = previewTui();
	const theme = new Theme(
		{
			text: "#dddddd",
			muted: "#888888",
			thinkingXhigh: "#dddddd",
		} as ConstructorParameters<typeof Theme>[0],
		{
			selectedBg: "#000000",
			toolPendingBg: "#112233",
			toolSuccessBg: "#223344",
			toolErrorBg: "#334455",
		} as ConstructorParameters<typeof Theme>[1],
		"truecolor",
	);
	try {
		for (const toolName of ["read", "edit", "unregistered_probe"]) {
			for (const [state, color] of [
				["toolPendingBg", 0x112233],
				["toolSuccessBg", 0x223344],
				["toolErrorBg", 0x334455],
			] as const) {
				for (const expanded of [false, true]) {
					const preview = new LiveTaskPreview(
						() => tui,
						() => true,
					).configure(expanded, theme);
					try {
						preview.update(
							{
								type: "tool_execution_start",
								toolCallId: "background",
								toolName,
								args: { path: "native-preview.txt" },
							},
							f.session,
						);
						if (state !== "toolPendingBg")
							preview.update(
								{
									type: "tool_execution_end",
									toolCallId: "background",
									toolName,
									result: {
										content: [
											{ type: "text", text: "Native output preserved" },
										],
										details:
											toolName === "edit"
												? { diff: "+1 Native output preserved" }
												: {},
									},
									isError: state === "toolErrorBg",
								},
								f.session,
							);
						const box = new Box(1, 0, (line) => theme.bg(state, line));
						box.addChild(preview);
						const lines = box.render(80);
						expect(lines.map(stripTerminalSequences).join("\n")).toContain(
							"native-preview.txt",
						);
						if (expanded && state !== "toolPendingBg")
							expect(lines.map(stripTerminalSequences).join("\n")).toContain(
								"Native output preserved",
							);
						const terminal = new xterm.Terminal({
							cols: 81,
							rows: lines.length + 1,
							allowProposedApi: true,
						});
						try {
							await new Promise<void>((resolve) =>
								terminal.write(lines.join("\r\n"), resolve),
							);
							for (let row = 0; row < lines.length; row++)
								for (let column = 0; column < 80; column++) {
									expect(
										terminal.buffer.active
											.getLine(row)
											?.getCell(column)
											?.getBgColor(),
										`${toolName} ${state} expanded=${expanded} row=${row} column=${column}`,
									).toBe(color);
								}
						} finally {
							terminal.dispose();
						}
					} finally {
						preview.dispose();
					}
				}
			}
		}
	} finally {
		tui.stop();
	}
});

it("renders real child assistant and custom tool activity through native SDK components", async () => {
	initTheme("dark", false);
	const f = await setup([]);
	const workspace = join(f.cwd, "native-preview");
	await mkdir(workspace);
	const extension = join(f.cwd, "native-probe.ts");
	await writeFile(
		extension,
		`import { Text } from "@earendil-works/pi-tui";
		export default pi => {
			pi.registerMarkdownTransformer(markdown => markdown.replace("TRANSFORM_TOKEN", "TRANSFORMED_NATIVE"));
			pi.registerTool({
			name: "native_probe", label: "Probe", description: "Render child activity", parameters: {type:"object",properties:{marker:{type:"string"},broken:{type:"boolean"}}},
			async execute(_id, _args, _signal, onUpdate) {
				onUpdate?.({content:[{type:"text",text:"partial child output"}],details:{label:"PARTIAL_NATIVE"}});
				return {content:[{type:"text",text:"final child output"}],details:{label:"FINAL_NATIVE"}};
			},
			renderCall(args, _theme, ctx) {
				if(args.broken) return {render(){throw new Error("Broken child render");}, invalidate(){throw new Error("Broken child invalidate");}};
				return new Text("CHILD_CALL " + ctx.cwd + (args.marker ?? ""), 0, 0);
			},
			renderResult(result, options) { return new Text(result.details.label + (options.expanded ? " EXPANDED" : " COMPACT"), 0, 0); }
		}); };`,
	);
	const message = fauxAssistantMessage([
		{ type: "thinking", thinking: "PRIVATE_THINKING" },
		{
			type: "text",
			text: "**Native heading**\n\n```ts\nconst value = 42;\n```\n\nTRANSFORM_TOKEN\nFINAL_ASSISTANT_LINE\u001b]52;c;unsafe-payload\u0007",
		},
	]);
	f.setResponses([
		fauxAssistantMessage(
			fauxToolCall(
				"native_probe",
				{ marker: "\u001b]52;c;tool-payload\u0007\u001b[2J VISIBLE_TOOL_TEXT" },
				{ id: "native-call" },
			),
		),
		message,
	]);
	const tui = previewTui();
	let hideThinking = true;
	const preview = new LiveTaskPreview(
		() => tui,
		() => hideThinking,
	);
	const theme = new Theme(
		{
			text: "#dddddd",
			muted: "#888888",
			thinkingXhigh: "#dddddd",
		} as ConstructorParameters<typeof Theme>[0],
		{ selectedBg: "#000000" } as ConstructorParameters<typeof Theme>[1],
		"truecolor",
	);
	const observed: Record<string, string> = {};
	const text = () => preview.render(120).map(stripTerminalSequences).join("\n");
	try {
		const value = await runTask(
			f.session.extensionRunner.createToolContext("native-preview", undefined),
			{
				prompt: "Inspect native child rendering",
				cwd: workspace,
				tools: ["native_probe"],
				extensions: [extension],
				noExtensions: true,
			},
			{ tools: [], availableTools: [], extensions: [], prompt: {} },
			{ modelTiers: {}, providerModelTiers: {} },
			14_400_000,
			undefined,
			undefined,
			(event, child) => {
				if (event.type === "tool_execution_start") {
					// Replay an explicit overlap ordering against the real child's renderers.
					const overlap = new LiveTaskPreview(
						() => tui,
						() => true,
					).configure(true, theme);
					const content = () =>
						overlap.render(120).map(stripTerminalSequences).join("\n");
					try {
						overlap.update(
							{
								...event,
								toolCallId: "overlap-a",
								args: { marker: " ACTIVE_A" },
							},
							child,
						);
						expect(content()).toContain("ACTIVE_A");
						overlap.update(
							{
								...event,
								toolCallId: "overlap-b",
								args: { marker: " ACTIVE_B" },
							},
							child,
						);
						expect(content()).toContain("ACTIVE_B");
						overlap.update(
							{
								type: "tool_execution_update",
								toolName: event.toolName,
								toolCallId: "overlap-a",
								args: {},
								partialResult: {
									content: [],
									details: { label: "A_UPDATING" },
								},
							},
							child,
						);
						expect(content()).toContain("ACTIVE_A");
						expect(content()).toContain("A_UPDATING");
						overlap.update(
							{
								type: "tool_execution_end",
								toolName: event.toolName,
								toolCallId: "overlap-b",
								result: { content: [], details: { label: "B_DONE" } },
								isError: false,
							},
							child,
						);
						expect(content()).toContain("ACTIVE_A");
						expect(content()).toContain("A_UPDATING");
						expect(content()).not.toContain("B_DONE");
						overlap.update(
							{
								type: "tool_execution_end",
								toolName: event.toolName,
								toolCallId: "overlap-a",
								result: { content: [], details: { label: "A_DONE" } },
								isError: false,
							},
							child,
						);
						expect(content()).toContain("A_DONE");
						overlap.update(
							{
								...event,
								toolCallId: "abandoned",
								args: { marker: " ABANDONED" },
							},
							child,
						);
						expect(content()).toContain("ABANDONED");
						overlap.dispose();
						expect(content()).not.toContain("ABANDONED");
					} finally {
						overlap.dispose();
					}
				}
				if (!preview.update(event, child)) return;
				preview.configure(false, theme, {
					keyHint: () => "alt+e expand",
				});
				expect(preview.render(120).length).toBe(3);
				expect(stripTerminalSequences(preview.render(120).at(-1) ?? "")).toBe(
					"alt+e expand",
				);
				const bytes = preview.render(120).join("");
				expect(bytes).not.toContain("\u001b]52;");
				expect(bytes).not.toContain("\u001b[2J");
				if (event.type.startsWith("tool_execution_")) {
					observed[event.type] = text();
					preview.configure(true, theme);
					observed[event.type + "_expanded"] = text();
				}
				if (
					event.type === "message_end" &&
					event.message.role === "assistant" &&
					event.message.content.some(
						(part) =>
							part.type === "text" && part.text.includes("Native heading"),
					)
				) {
					preview.configure(true, theme);
					preview.update(
						{ type: "message_start", message: fauxAssistantMessage("") },
						child,
					);
					expect(text()).toContain("Native heading");
					expect(text()).not.toContain("PRIVATE_THINKING");
					hideThinking = false;
					expect(text()).toContain("Native heading");
					expect(text()).toContain("PRIVATE_THINKING");
					hideThinking = true;
					expect(text()).toContain("Native heading");
					expect(text()).not.toContain("PRIVATE_THINKING");
					preview.update(event, child);
				}
				if (event.type === "tool_execution_end") {
					preview.update(
						{ type: "message_start", message: fauxAssistantMessage("") },
						child,
					);
					expect(text()).toContain("FINAL_NATIVE");
					preview.update(
						{
							type: "message_end",
							message: fauxAssistantMessage(fauxToolCall("native_probe", {})),
						},
						child,
					);
					expect(text()).toContain("FINAL_NATIVE");
				}
			},
		);
		expect(
			value.outcome,
			JSON.stringify({ diagnostics: value.diagnostics, output: value.output }),
		).toBe("completed");
		expect(observed["tool_execution_start"]).toContain(
			"CHILD_CALL " + workspace,
		);
		expect(observed["tool_execution_start"]).toContain("VISIBLE_TOOL_TEXT");
		expect(observed["tool_execution_start"]).not.toContain("tool-payload");
		expect(observed["tool_execution_update"]).toContain(
			"PARTIAL_NATIVE COMPACT",
		);
		expect(observed["tool_execution_update_expanded"]).toContain(
			"PARTIAL_NATIVE EXPANDED",
		);
		expect(observed["tool_execution_end_expanded"]).toContain(
			"FINAL_NATIVE EXPANDED",
		);
		preview.configure(false, theme);
		expect(text()).not.toContain("PRIVATE_THINKING");
		expect(preview.render(120).length).toBeLessThanOrEqual(3);
		preview.configure(true, theme);
		expect(text()).toContain("Native heading");
		expect(text()).not.toContain("**Native heading**");
		expect(text()).toContain("const value = 42;");
		expect(text()).toContain("FINAL_ASSISTANT_LINE");
		expect(text()).toContain("TRANSFORMED_NATIVE");
		expect(text()).not.toContain("unsafe-payload");
		expect(text()).not.toContain("CHILD_CALL");
		expect(preview.render(120).join("")).not.toContain("\u001b]133;");
		for (const width of [1, 2, 8, 40])
			expect(
				preview.render(width).every((line) => visibleWidth(line) <= width),
			).toBe(true);
		hideThinking = false;
		expect(text()).toContain("PRIVATE_THINKING");
		const native = new AssistantMessageComponent(
			message,
			false,
			undefined,
			undefined,
			0,
			[(markdown) => markdown.replace("TRANSFORM_TOKEN", "TRANSFORMED_NATIVE")],
		);
		const nativeText = native
			.render(120)
			.map(stripTerminalSequences)
			.join("\n");
		expect(text().trim()).toBe(nativeText.trim());
		f.setResponses([
			fauxAssistantMessage(
				fauxToolCall("native_probe", { broken: true }, { id: "broken-call" }),
			),
			fauxAssistantMessage(
				fauxToolCall(
					"native_probe",
					{ broken: true },
					{ id: "broken-invalidate" },
				),
			),
			fauxAssistantMessage("Recovered native answer"),
		]);
		let fallbackCount = 0;
		const recovered = await runTask(
			f.session.extensionRunner.createToolContext("broken-preview", undefined),
			{
				prompt: "Check broken child renderer",
				tools: ["native_probe"],
				extensions: [extension],
				noExtensions: true,
			},
			{ tools: [], availableTools: [], extensions: [], prompt: {} },
			{ modelTiers: {}, providerModelTiers: {} },
			14_400_000,
			undefined,
			undefined,
			(event, child) => {
				preview.update(event, child);
				if (event.type === "tool_execution_start") {
					if (event.toolCallId === "broken-invalidate") preview.invalidate();
					if (text().includes("Preview unavailable")) fallbackCount++;
				}
			},
		);
		expect(fallbackCount).toBe(2);
		expect(recovered.outcome).toBe("completed");
		expect(text()).toContain("Recovered native answer");
		expect(f.errors).toEqual([]);
	} finally {
		preview.dispose();
		tui.stop();
	}
	preview.configure(true, theme);
	expect(text()).not.toContain("FINAL_ASSISTANT_LINE");
});

it("keeps a stable hint on registered renderer errors without task details", async () => {
	initTheme("dark", false);
	const f = await setup([]);
	const render = f.session.getToolDefinition("pipi")?.renderResult;
	if (!render) throw new Error("Missing pipi result renderer");
	const theme = new Theme(
		{
			text: "#dddddd",
			muted: "#888888",
			thinkingXhigh: "#dddddd",
			error: "#c84949",
		} as ConstructorParameters<typeof Theme>[0],
		{ selectedBg: "#000000" } as ConstructorParameters<typeof Theme>[1],
		"truecolor",
	);
	const error = {
		content: [
			{
				type: "text" as const,
				text:
					"Detailed failure\u001b]52;c;unsafe-error-payload\u0007\u001b[2J\u0000\n" +
					"x".repeat(6100) +
					"END_BEYOND_6000",
			},
		],
		details: undefined,
	};
	for (const expanded of [false, true]) {
		const context: Parameters<typeof render>[3] = {
			args: { prompt: "Inspect error" },
			toolCallId: "renderer-error",
			invalidate: () => {},
			lastComponent: undefined,
			state: {},
			cwd: f.cwd,
			executionStarted: true,
			argsComplete: true,
			isPartial: false,
			expanded,
			showImages: false,
			isError: true,
			durationMs: undefined,
			outputPad: 0,
			keyText: () => "",
			keyHint: (_key: string, description: string) => description,
		};
		const view = render(error, { expanded, isPartial: false }, theme, context);
		const rows = view.render(120);
		const plain = rows.map(stripTerminalSequences).join("\n");
		if (expanded) {
			expect(plain).toContain("Detailed failure");
			expect(plain).toContain("END_BEYOND_6000");
			expect(rows.join("\n")).toContain(
				(theme.fg("error", "Detailed failure").split("Detailed failure")[0] ??
					"") + "Detailed failure",
			);
		} else {
			expect(rows).toHaveLength(3);
			expect(plain).not.toContain("Detailed failure");
			expect(stripTerminalSequences(rows[2] ?? "")).toBe("to expand");
		}
		expect(plain).not.toContain("unsafe-error-payload");
		expect(rows.join("\n")).not.toContain("\u001b[2J");
		expect(rows.join("\n")).not.toContain("\u0000");
		for (const width of [1, 2, 8, 80])
			expect(
				view.render(width).every((line) => visibleWidth(line) <= width),
			).toBe(true);
	}
});

it("streams child tool name and bounded args only in human details, then replaces them with the answer", async () => {
	const f = await setup([]);
	const path = join(f.cwd, "evidence.txt");
	await writeFile(path, "child evidence");
	f.setResponses([
		call({ prompt: "Read evidence", tools: ["read"], noExtensions: true }),
		fauxAssistantMessage(fauxToolCall("read", { path }, { id: "child-read" })),
		fauxAssistantMessage("fresh child answer"),
		fauxAssistantMessage("parent"),
	]);
	const updates: Progress[] = [];
	const unsubscribe = f.session.subscribe((event) => {
		if (event.type !== "tool_execution_update" || event.toolName !== "pipi")
			return;
		expect(event.partialResult.content).toEqual([]);
		const details = event.partialResult.details as { progress: Progress };
		updates.push(details.progress);
	});
	try {
		await f.session.prompt("Delegate");
	} finally {
		unsubscribe();
	}
	expect(f.errors).toEqual([]);
	expect(result(f).outcome).toBe("completed");
	const tool = updates.find((update) => update.tool?.name === "read");
	expect(tool?.phase).toBe("tool");
	expect(JSON.parse(tool?.tool?.args ?? "null")).toEqual({ path });
	expect(tool?.tool?.args.length).toBeLessThanOrEqual(6000);
	const answer = updates.findLast((update) => update.phase === "answering");
	expect(answer?.preview).toBe("fresh child answer");
	expect(answer?.tool).toBeUndefined();
	expect(f.session.getToolDefinition("pipi")?.label).toBe("π²");
});

it("bounds model-facing final content while retaining full native structured output and no intermediate traffic", async () => {
	const text = "evidence ".repeat(15000);
	const f = await setup([
		call({ prompt: "Large answer", tools: [] }),
		fauxAssistantMessage(text),
		fauxAssistantMessage("parent"),
	]);
	const updates: unknown[] = [];
	const unsubscribe = f.session.subscribe((event) => {
		if (event.type === "tool_execution_update" && event.toolName === "pipi")
			updates.push(event.partialResult.content);
	});
	try {
		await f.session.prompt("Delegate");
	} finally {
		unsubscribe();
	}
	const tool = f.session.messages.find(
		(message) => message.role === "toolResult" && message.toolName === "pipi",
	);
	if (tool?.role !== "toolResult") throw new Error("Missing pipi result");
	const content = tool.content
		.filter((part) => part.type === "text")
		.map((part) => part.text)
		.join("");
	expect(content.length).toBeLessThan(55000);
	expect(tool.details).toMatchObject({
		result: {
			outcome: "completed",
			output: { role: "assistant", content: [{ type: "text", text }] },
		},
	});
	expect(updates.length).toBeGreaterThan(0);
	expect(
		updates.every((update) => Array.isArray(update) && update.length === 0),
	).toBe(true);
});

it("inherits effective prompt controls recursively without leaking parent output submission requirements", async () => {
	const f = await setup([
		call({
			prompt: "Outer",
			tools: ["pipi"],
			forceSystemPrompt: "Forced inherited marker",
			outputSchema: { type: "integer" },
		}),
		fauxAssistantMessage(
			fauxToolCall("pipi", { prompt: "Inner ordinary task" }, { id: "inner" }),
		),
		(context) => {
			expect(
				getCurrentTools(context.messages).map((tool) => tool.name),
			).not.toContain("pipi_output");
			const prompt = getCurrentSystemPrompt(context.messages);
			expect(prompt).toContain("Forced inherited marker");
			expect(prompt).not.toContain("<pipi_output>");
			return fauxAssistantMessage("inner ordinary answer");
		},
		fauxAssistantMessage(
			fauxToolCall("pipi_output", { output: 99 }, { id: "outer-output" }),
		),
		fauxAssistantMessage("parent"),
	]);
	await f.session.prompt("Delegate recursively with a forced prompt");
	expect(result(f)).toMatchObject({
		outcome: "completed",
		output: 99,
		turns: 2,
	});
});

it("continues in saved child cwd when caller omits cwd", async () => {
	const f = await setup([]);
	const cwd = join(f.cwd, "other-workspace");
	await mkdir(cwd);
	f.setResponses([
		call({ prompt: "First", cwd, tools: [], noExtensions: true }),
		fauxAssistantMessage("first"),
		fauxAssistantMessage("parent"),
	]);
	await f.session.prompt("First task");
	const first = result(f);
	expect(first.effective.cwd).toBe(cwd);
	f.setResponses([
		call({
			prompt: "Continue",
			sessionId: first.sessionId,
			tools: [],
			noExtensions: true,
		}),
		fauxAssistantMessage("second"),
		fauxAssistantMessage("parent"),
	]);
	await f.session.prompt("Continue task");
	expect(result(f)).toMatchObject({
		outcome: "completed",
		sessionId: first.sessionId,
		effective: { cwd },
	});
});

it.each(["project", "cli"] as const)(
	"preserves %s hook-only guard across child cwd changes",
	async (source) => {
		const root = await mkdtemp(join(tmpdir(), "pipi-guard-"));
		const originalCwd = process.cwd();
		const originalArgs = process.argv;
		try {
			const parent = join(root, "parent"),
				target = join(root, "target");
			await mkdir(parent);
			await mkdir(target);
			const directory =
				source === "project" ? join(parent, ".pi", "extensions") : parent;
			await mkdir(directory, { recursive: true });
			const guard = join(directory, "guard.ts");
			const body = (message: string) =>
				`export default pi => { pi.on("tool_call", event => event.toolName === "write" ? {block:true,reason:${JSON.stringify(message)}} : undefined); };`;
			await writeFile(guard, body("original-guard"));
			await writeFile(join(target, "guard.ts"), body("wrong-guard"));
			process.chdir(parent);
			if (source === "cli")
				process.argv = [...originalArgs, "-e", "./guard.ts"];
			fixture = await createPiSession({
				cwd: parent,
				projectTrusted: true,
				extensionPaths: [
					resolve(originalCwd, "extensions/pipi/index.ts"),
					guard,
				],
				userSettings: {
					defaultTools: ["pipi", "write"],
					retry: { enabled: false },
					cacheWarming: "off",
				},
				env: { PI_PIPI_DEBUG: "0" },
				responses: [
					call({ prompt: "Attempt write", cwd: target, tools: ["write"] }),
					fauxAssistantMessage(
						fauxToolCall("write", {
							path: "evidence.txt",
							content: "should not write",
						}),
					),
					fauxAssistantMessage("guard prevented write"),
					fauxAssistantMessage("parent"),
				],
			});
			await fixture.session.prompt("Delegate");
			const value = result(fixture);
			expect(value.outcome).toBe("completed");
			const transcript = await readFile(value.sessionFile as string, "utf8");
			expect(transcript).toContain("original-guard");
			expect(transcript).not.toContain("wrong-guard");
			await expect(stat(join(target, "evidence.txt"))).rejects.toMatchObject({
				code: "ENOENT",
			});
		} finally {
			process.argv = originalArgs;
			process.chdir(originalCwd);
			await fixture?.dispose();
			fixture = undefined;
			await rm(root, { recursive: true, force: true });
		}
	},
);

it("inherits parent-only runtime authentication without persisting credentials", async () => {
	const f = await setup([]);
	await f.session.modelRuntime.setRuntimeApiKey(
		"faux",
		"parent-runtime-fixture",
	);
	let observed: string | undefined;
	f.setResponses([
		call({ prompt: "Use inherited auth", tools: [], noExtensions: true }),
		(_context, options) => {
			observed = options?.apiKey;
			return fauxAssistantMessage("authenticated child");
		},
		fauxAssistantMessage("parent"),
	]);
	await f.session.prompt("Delegate");
	expect(result(f).outcome).toBe("completed");
	expect(observed).toBe("parent-runtime-fixture");
	const stored = await readFile(join(f.agentDir, "auth.json"), "utf8").catch(
		(error: NodeJS.ErrnoException) => {
			if (error.code === "ENOENT") return "{}";
			throw error;
		},
	);
	expect(JSON.parse(stored)).toEqual({});
});