import assert from "node:assert/strict";
import { EventEmitter } from "node:events";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { PassThrough } from "node:stream";
import { describe, it } from "node:test";
import { readHerdrInspectorBinding } from "../../src/inspectors/herdr/actions.ts";
import { handleInspectorAction } from "../../src/inspectors/actions.ts";
import { createHerdrInspectorPlugin } from "../../src/inspectors/herdr/plugin.ts";
import { createHerdrClient, detectHerdr, parseHerdrVersion, supportsRawPanes, type HerdrClient } from "../../src/inspectors/herdr/client.ts";
import { formatInspectorDashboard, runInspector, submitInspectorControl } from "../../src/inspectors/inspector-runner.ts";
import { createProjectPaneManager, handleHerdrProjectPaneAction, listHerdrProjectPaneRoots, readHerdrProjectPaneBinding, restoreHerdrProjectPaneSnapshots } from "../../src/inspectors/herdr/project-panes.ts";
import { decodeSessionRoots } from "../../src/inspectors/session-roots-codec.ts";
import { consumeSteerRequests, consumeStopRequest } from "../../src/runs/background/control-channel.ts";
import { PI_SUBAGENT_PI_BINARY_ENV } from "../../src/runs/shared/pi-spawn.ts";
import type { AsyncStatus, SubagentState } from "../../src/shared/types.ts";

// `pane run` commands quote the whole --session-roots value (base64, so it has
// no spaces/quotes of its own); pull it back out and decode it the same way
// the inspector runner does, rather than pattern-matching on the raw text.
const INSPECT_ALLOW_FLAGS = /--allow-steer['"]\s+['"]true['"]\s+['"]--allow-stop['"]\s+['"]true['"]/;

function sessionRootsFromRunCommand(command: string): string[] {
	const match = /--session-roots\S*\s+['"]?([A-Za-z0-9+/=]+)/.exec(command);
	assert.ok(match, `--session-roots argument not found in command: ${command}`);
	return decodeSessionRoots(match[1]);
}

function fakeChild(): EventEmitter & { stdout: PassThrough; stderr: PassThrough; kill(): boolean } {
	const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough; kill(): boolean };
	child.stdout = new PassThrough();
	child.stderr = new PassThrough();
	child.kill = () => true;
	return child;
}

function writeRun(root: string, id = "run-123"): { asyncDir: string; status: AsyncStatus } {
	const asyncDir = path.join(root, id);
	fs.mkdirSync(asyncDir, { recursive: true });
	const status: AsyncStatus = {
		runId: id,
		mode: "single",
		state: "running",
		startedAt: Date.now() - 1_000,
		cwd: root,
		steps: [{ agent: "worker", status: "running", recentOutput: ["working"] }],
	};
	fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify(status), "utf-8");
	return { asyncDir, status };
}

function text(result: Awaited<ReturnType<typeof handleInspectorAction>>): string {
	return result.content.find((entry) => entry.type === "text")?.text ?? "";
}

describe("Herdr inspector", () => {
	it("preserves Herdr detection errors in an active Herdr environment", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-detection-error-"));
		try {
			writeRun(root);
			const client: HerdrClient = {
				run: async () => ({ ok: false, error: { code: "HERDR_UNAVAILABLE", message: "herdr missing" } }),
			};
			const opened = await handleInspectorAction("inspector.open", { id: "run-123" }, {
				cwd: root,
				asyncDirRoot: root,
				env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" },
				plugins: [createHerdrInspectorPlugin({ client })],
			});
			assert.equal(opened.isError, true);
			assert.match(text(opened), /Herdr inspector error \(HERDR_UNAVAILABLE\)/);
			assert.doesNotMatch(text(opened), /No inspector plugin is available/);
		} finally {
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("normalizes missing binaries, timeouts, and supported versions", async () => {
		const missing = createHerdrClient({ spawn: (() => { throw Object.assign(new Error("missing"), { code: "ENOENT" }); }) as never });
		const missingResult = await missing.run(["--version"]);
		assert.equal(missingResult.ok, false);
		if (!missingResult.ok) assert.equal(missingResult.error.code, "HERDR_UNAVAILABLE");

		const timeout = createHerdrClient({ spawn: (() => fakeChild()) as never });
		const timeoutResult = await timeout.run(["pane", "get", "w1:p2"], { timeoutMs: 5 });
		assert.equal(timeoutResult.ok, false);
		if (!timeoutResult.ok) assert.equal(timeoutResult.error.code, "TIMEOUT");

		const gone = createHerdrClient({ spawn: (() => {
			const child = fakeChild();
			queueMicrotask(() => {
				child.stderr.end(JSON.stringify({ error: { code: "no_such_pane", message: "gone" } }));
				child.emit("close", 1);
			});
			return child;
		}) as never });
		const goneResult = await gone.run(["pane", "get", "w1:p2"]);
		assert.equal(goneResult.ok, false);
		if (!goneResult.ok) assert.equal(goneResult.error.code, "NOT_FOUND");

		assert.deepEqual(parseHerdrVersion("herdr 0.7.5"), { major: 0, minor: 7, patch: 5 });
		assert.equal(supportsRawPanes({ major: 0, minor: 7, patch: 4 }), false);
		const old: HerdrClient = { run: async () => ({ ok: true, data: "herdr 0.7.4" }) };
		const oldResult = await detectHerdr(old);
		assert.equal(oldResult.ok, false);
		if (!oldResult.ok) assert.equal(oldResult.error.code, "HERDR_UNSUPPORTED_VERSION");
	});

	it("formats the inspector command for the pane's own shell", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-inspector-shell-"));
		try {
			writeRun(root);
			const deps = (env: NodeJS.ProcessEnv) => ({ cwd: root, asyncDirRoot: root, env, plugins: [], runnerPath: path.join(root, "runner.ts") });
			const posix = await handleInspectorAction("inspector.command", { id: "run-123" }, deps({ SHELL: "/usr/bin/bash.exe" }));
			assert.doesNotMatch(text(posix), /^& /, "a Git Bash pane cannot parse the PowerShell call operator");
			const powershell = await handleInspectorAction("inspector.command", { id: "run-123" }, deps({}));
			assert.equal(text(powershell).startsWith("& "), process.platform === "win32");
		} finally {
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("opens one raw inspector pane, persists its binding, and closes only that pane", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-inspector-"));
		try {
			const { asyncDir } = writeRun(root);
			const sessionRoot = path.join(root, "sessions");
			const missionDir = path.join(root, "other-project", ".pi/subagents", "missions");
			fs.writeFileSync(path.join(asyncDir, "mission.json"), JSON.stringify({
				schemaVersion: 1,
				missionId: "mission-cross-project",
				projectRoot: path.join(root, "other-project"),
				missionDir,
				globalIndexDir: path.join(root, "mission-index"),
				writeGlobalIndex: false,
			}), "utf-8");
			const calls: string[][] = [];
			const client: HerdrClient = {
				run: async <T>(args: string[]) => {
					calls.push(args);
					if (args[0] === "--version") return { ok: true, data: "herdr 0.7.5" as T };
					if (args[0] === "pane" && args[1] === "split") return { ok: true, data: { pane: { pane_id: "w1:p9" } } as T };
					return { ok: true, data: {} as T };
				},
			};
			const commandResult = await handleInspectorAction("inspector.command", { id: "run-123" }, {
				cwd: root,
				asyncDirRoot: root,
				resultsDir: path.join(root, "results"),
				plugins: [createHerdrInspectorPlugin({ client })],
				env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" },
				sessionRoots: [sessionRoot],
				runnerPath: path.join(root, "runner.ts"),
			});
			assert.equal(commandResult.isError, undefined, text(commandResult));
			assert.match(text(commandResult), INSPECT_ALLOW_FLAGS);
			assert.deepEqual(sessionRootsFromRunCommand(text(commandResult)), [sessionRoot]);
			assert.deepEqual(calls, []);

			const opened = await handleInspectorAction("inspector.open", { id: "run-123" }, {
				cwd: root,
				asyncDirRoot: root,
				resultsDir: path.join(root, "results"),
				plugins: [createHerdrInspectorPlugin({ client })],
				env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" },
				sessionRoots: [sessionRoot],
				runnerPath: path.join(root, "runner.ts"),
				now: () => new Date("2026-01-01T00:00:00.000Z"),
			});
			assert.equal(opened.isError, undefined, text(opened));
			assert.match(text(opened), /read-only Herdr inspector pane w1:p9/);
			const binding = readHerdrInspectorBinding(asyncDir);
			assert.equal(binding?.paneId, "w1:p9");
			assert.equal(binding?.openedAt, "2026-01-01T00:00:00.000Z");
			assert.equal(binding?.missionId, "mission-cross-project");
			assert.equal(binding?.missionPath, path.join(missionDir, "mission-cross-project.json"));
			assert.equal(binding?.lastFocusedAt, undefined);
			const splitCall = calls.find((args) => args[0] === "pane" && args[1] === "split");
			assert.deepEqual(splitCall?.slice(-1), ["--no-focus"]);
			const runCall = calls.find((args) => args[0] === "pane" && args[1] === "run" && args[2] === "w1:p9");
			assert.ok(runCall);
			if (process.platform !== "win32") {
				assert.ok(!/^[']/.test(runCall[3] ?? ""), `pane run command must not open with a quoted executable; Nushell parses it as a string expression: ${runCall[3]}`);
			}
			assert.match(runCall[3] ?? "", INSPECT_ALLOW_FLAGS);
			assert.deepEqual(sessionRootsFromRunCommand(runCall[3] ?? ""), [sessionRoot]);

			const reopened = await handleInspectorAction("inspector.open", { id: "run-123" }, {
				cwd: root,
				asyncDirRoot: root,
				resultsDir: path.join(root, "results"),
				plugins: [createHerdrInspectorPlugin({ client })],
				env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" },
				sessionRoots: [sessionRoot],
				runnerPath: path.join(root, "runner.ts"),
			});
			assert.equal(reopened.isError, undefined, text(reopened));
			assert.equal(calls.filter((args) => args[0] === "pane" && args[1] === "split").length, 2, "empty pane get response must not reuse the binding");

			const closed = await handleInspectorAction("inspector.close", { dir: asyncDir }, { cwd: root, asyncDirRoot: root, plugins: [createHerdrInspectorPlugin({ client })], env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" } });
			assert.equal(closed.isError, undefined, text(closed));
			assert.match(text(closed), /subagent run was not stopped/);
			assert.equal(readHerdrInspectorBinding(asyncDir), undefined);
			assert.ok(calls.some((args) => args.join(" ") === "pane close w1:p9"));

			calls.length = 0;
			const focusedOpened = await handleInspectorAction("inspector.open", { id: "run-123", focus: true }, {
				cwd: root,
				asyncDirRoot: root,
				resultsDir: path.join(root, "results"),
				plugins: [createHerdrInspectorPlugin({ client })],
				env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" },
				sessionRoots: [sessionRoot],
				runnerPath: path.join(root, "runner.ts"),
				now: () => new Date("2026-01-01T00:00:00.000Z"),
			});
			assert.equal(focusedOpened.isError, undefined, text(focusedOpened));
			const focusedSplitCall = calls.find((args) => args[0] === "pane" && args[1] === "split");
			assert.deepEqual(focusedSplitCall?.slice(-1), ["--focus"]);
			assert.equal(readHerdrInspectorBinding(asyncDir)?.lastFocusedAt, "2026-01-01T00:00:00.000Z");
		} finally {
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("rejects bindings with mismatched run, child, or async identities", async () => {
		for (const mismatch of ["runId", "childIndex", "asyncDir"] as const) {
			const root = fs.mkdtempSync(path.join(os.tmpdir(), `pi-herdr-binding-${mismatch}-`));
			try {
				const { asyncDir } = writeRun(root);
				const otherDir = path.join(root, "other-run");
				fs.mkdirSync(otherDir);
				const binding = {
					schemaVersion: 1,
					kind: "herdr-inspector",
					runId: mismatch === "runId" ? "stale-run" : "run-123",
					asyncDir: mismatch === "asyncDir" ? otherDir : asyncDir,
					...(mismatch === "childIndex" ? { childIndex: 1 } : {}),
					paneId: "w1:stale",
					openedAt: new Date().toISOString(),
					command: "stale",
				};
				fs.mkdirSync(path.join(asyncDir, "inspectors"), { recursive: true });
				fs.writeFileSync(path.join(asyncDir, "inspectors", `herdr${mismatch === "childIndex" ? "-0" : ""}.json`), JSON.stringify(binding), "utf-8");
				const calls: string[][] = [];
				const client: HerdrClient = { run: async (args) => { calls.push(args); return { ok: true, data: {} }; } };
				const options = {
					cwd: root,
					asyncDirRoot: root,
					plugins: [createHerdrInspectorPlugin({ client })],
					env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" },
				};
				const params = { dir: asyncDir, ...(mismatch === "childIndex" ? { index: 0 } : {}) };
				for (const action of ["inspector.status", "inspector.close"] as const) {
					const result = await handleInspectorAction(action, params, options);
					assert.match(text(result), /No inspector plugin owns this binding/);
				}
				assert.deepEqual(calls, []);
			} finally {
				fs.rmSync(root, { recursive: true, force: true });
			}
		}
	});

	it("passes live parent-owned custom session roots to standalone inspectors", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-custom-session-"));
		try {
			const { asyncDir, status } = writeRun(root);
			const sessionRoot = path.join(root, "custom-sessions");
			status.steps![0] = { ...status.steps![0], sessionFile: path.join(sessionRoot, "run-0", "session.jsonl") };
			fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify(status), "utf-8");
			const calls: string[][] = [];
			const client: HerdrClient = {
				run: async <T>(args: string[]) => {
					calls.push(args);
					if (args[0] === "--version") return { ok: true, data: "herdr 0.7.5" as T };
					if (args[0] === "pane" && args[1] === "split") return { ok: true, data: { pane: { pane_id: "w1:p11" } } as T };
					return { ok: true, data: {} as T };
				},
			};
			const state = {
				asyncJobs: new Map([["run-123", { asyncId: "run-123", asyncDir, status: "running", sessionRoot }]]),
				foregroundControls: new Map(),
				lastForegroundControlId: null,
				baseCwd: root,
				currentSessionId: "session-1",
			} as unknown as SubagentState;

			const opened = await handleInspectorAction("inspector.open", { id: "run-123" }, {
				cwd: root,
				asyncDirRoot: root,
				plugins: [createHerdrInspectorPlugin({ client })],
				env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" },
				state,
				runnerPath: path.join(root, "runner.ts"),
			});
			assert.equal(opened.isError, undefined, text(opened));
			const runCall = calls.find((args) => args[0] === "pane" && args[1] === "run" && args[2] === "w1:p11");
			assert.deepEqual(sessionRootsFromRunCommand(runCall?.[3] ?? ""), [sessionRoot]);

			state.asyncJobs.clear();
			fs.rmSync(path.join(asyncDir, "inspectors"), { recursive: true, force: true });
			calls.length = 0;
			const reopened = await handleInspectorAction("inspector.open", { id: "run-123" }, {
				cwd: root,
				asyncDirRoot: root,
				plugins: [createHerdrInspectorPlugin({ client })],
				env: { HERDR_ENV: "1", HERDR_PANE_ID: "test-pane" },
				state,
				runnerPath: path.join(root, "runner.ts"),
			});
			assert.equal(reopened.isError, undefined, text(reopened));
			const untrustedRunCall = calls.find((args) => args[0] === "pane" && args[1] === "run" && args[2] === "w1:p11");
			assert.deepEqual(sessionRootsFromRunCommand(untrustedRunCall?.[3] ?? ""), []);
		} finally {
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("refuses direct run directories outside trusted roots", async () => {
		const trustedRoot = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-trusted-"));
		const outsideRoot = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-outside-"));
		try {
			const { asyncDir } = writeRun(outsideRoot);
			const inspected = await handleInspectorAction("inspector.status", { dir: asyncDir }, {
				cwd: trustedRoot,
				asyncDirRoot: trustedRoot,
				client: { run: async () => ({ ok: true, data: {} }) },
			});
			assert.equal(inspected.isError, true);
			assert.match(text(inspected), /outside trusted run roots/);
		} finally {
			fs.rmSync(trustedRoot, { recursive: true, force: true });
			fs.rmSync(outsideRoot, { recursive: true, force: true });
		}
	});

	it("renders mission context and reuses existing steer/stop control records", () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-controls-"));
		try {
			const { asyncDir, status } = writeRun(root);
			const dashboard = formatInspectorDashboard({
				status,
				asyncDir,
				mission: {
					schemaVersion: 1,
					id: "mission-1",
					title: "Ship inspector",
					goal: { status: "active" },
					status: "active",
					createdAt: "2026-01-01T00:00:00.000Z",
					updatedAt: "2026-01-01T00:00:00.000Z",
					runs: [],
					decisions: [{ id: "decision-1", status: "open", title: "Choose UX", createdAt: "2026-01-01T00:00:00.000Z" }],
					artifacts: [],
				},
			});
			assert.match(dashboard, /Mission: Ship inspector \(active\)/);
			assert.match(dashboard, /decision-1: Choose UX/);
			assert.match(dashboard, /closing it does not stop the run/i);

			assert.match(submitInspectorControl({ asyncDir, runId: "run-123", refreshMs: 1_500 }, "steer keep going"), /Steering queued for run run-123\.\n\nMessage sent:\n```text\nkeep going\n```/);
			assert.deepEqual(consumeSteerRequests(asyncDir).map((request) => ({ message: request.message, targetIndex: request.targetIndex, source: request.source })), [
				{ message: "keep going", targetIndex: 0, source: "inspector-runner" },
			]);
			assert.match(submitInspectorControl({ asyncDir, runId: "run-123", index: 0, refreshMs: 1_500 }, "keep going without a prefix"), /Steering queued for run run-123/);
			assert.deepEqual(consumeSteerRequests(asyncDir).map((request) => ({ message: request.message, targetIndex: request.targetIndex, source: request.source })), [
				{ message: "keep going without a prefix", targetIndex: 0, source: "inspector-runner" },
			]);
			fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify({ ...status, mode: "parallel", steps: [...status.steps!, { agent: "reviewer", status: "running" }] }), "utf-8");
			const aggregateDashboard = formatInspectorDashboard({ status: { ...status, mode: "parallel", steps: [...status.steps!, { agent: "reviewer", status: "running" }] }, asyncDir });
			assert.match(aggregateDashboard, /Controls: steer <message> \| stop \| status/);
			assert.doesNotMatch(aggregateDashboard, /type guidance/);
			assert.throws(() => submitInspectorControl({ asyncDir, runId: "run-123", refreshMs: 1_500 }, "ambiguous plain guidance"), /Plain guidance requires a child-specific inspector/);
			assert.match(submitInspectorControl({ asyncDir, runId: "run-123", refreshMs: 1_500 }, "steer broadcast guidance"), /Steering queued for run run-123/);
			assert.deepEqual(consumeSteerRequests(asyncDir).map((request) => ({ message: request.message, targetIndexes: request.targetIndexes, source: request.source })), [
				{ message: "broadcast guidance", targetIndexes: [0, 1], source: "inspector-runner" },
			]);
			assert.match(submitInspectorControl({ asyncDir, runId: "run-123", refreshMs: 1_500 }, "stop"), /Stop requested/);
			assert.equal(consumeStopRequest(asyncDir), true);
			assert.throws(() => submitInspectorControl({ asyncDir, runId: "run-123", refreshMs: 1_500 }, "reply decision-1 yes"), /parent Pi session/);
			assert.throws(() => submitInspectorControl({ asyncDir, runId: "run-123", refreshMs: 1_500, allowSteer: false }, "steer bypass"), /Authority policy/);
			assert.throws(() => submitInspectorControl({ asyncDir, runId: "run-123", refreshMs: 1_500, allowStop: false }, "stop"), /Authority policy/);
		} finally {
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("keeps a partly typed control line on screen when the dashboard refreshes", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-typed-line-"));
		const input = new PassThrough();
		const output = Object.assign(new PassThrough(), { isTTY: true, columns: 80 });
		let screen = "";
		output.on("data", (chunk: Buffer) => { screen += chunk.toString(); });
		const nextRedraw = () => new Promise<void>((resolve) => {
			const onData = (chunk: Buffer) => {
				if (!chunk.toString().includes("\x1b[2J")) return;
				output.off("data", onData);
				setImmediate(resolve);
			};
			output.on("data", onData);
		});
		try {
			const { asyncDir, status } = writeRun(root);
			runInspector(["--async-dir", asyncDir, "--run-id", "run-123", "--refresh-ms", "250"], { input, output });
			input.write("steer keep going");
			await nextRedraw();
			assert.match(screen.slice(screen.lastIndexOf("\x1b[2J")), /> steer keep going/);

			fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify({ ...status, state: "complete" }), "utf-8");
			await nextRedraw();
		} finally {
			input.end();
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("renders trusted child session fallback and refuses untrusted roots", () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-session-"));
		try {
			const { asyncDir, status } = writeRun(root);
			const sessionRoot = path.join(root, "sessions");
			const sessionFile = path.join(sessionRoot, "child.jsonl");
			fs.mkdirSync(sessionRoot, { recursive: true });
			fs.writeFileSync(sessionFile, `${JSON.stringify({ role: "assistant", content: "session fallback text" })}\n`, "utf-8");
			status.steps![0] = { ...status.steps![0], recentOutput: [], sessionFile };

			const trusted = formatInspectorDashboard({ status, asyncDir, sessionRoots: [sessionRoot] });
			assert.match(trusted, /assistant: session fallback text/);
			assert.doesNotMatch(trusted, /without a trusted root/);

			const untrusted = formatInspectorDashboard({ status, asyncDir });
			assert.match(untrusted, /without a trusted root/);
		} finally {
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("opens, reports, and closes a project-owned Herdr pane", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi=herdr project pane-"));
		const previousPiBinary = process.env[PI_SUBAGENT_PI_BINARY_ENV];
		const previousShell = process.env.SHELL;
		process.env[PI_SUBAGENT_PI_BINARY_ENV] = path.join(root, "pi-bin");
		delete process.env.SHELL;
		try {
			const projectRoot = fs.realpathSync(root);
			const ownerRoot = path.join(root, "owner");
			fs.mkdirSync(ownerRoot);
			const calls: string[][] = [];
			const client: HerdrClient = {
				run: async <T>(args: string[]) => {
					calls.push(args);
					if (args[0] === "--version") return { ok: true, data: "herdr 0.7.5" as T };
					if (args[0] === "pane" && args[1] === "split") return { ok: true, data: { pane: { pane_id: "w1:p10" } } as T };
					if (args[0] === "pane" && args[1] === "get") return { ok: true, data: { pane: {
						pane_id: "w1:p10", agent: "pi", agent_status: "idle", cwd: projectRoot,
						foreground_cwd: projectRoot, focused: false, terminal_title_stripped: "Pi · project",
					} } as T };
					return { ok: true, data: {} as T };
				},
			};

			const opened = await handleHerdrProjectPaneAction("project.open", { cwd: root, message: "Own this project mission." }, {
				cwd: ownerRoot,
				client,
				now: () => new Date("2026-01-01T00:00:00.000Z"),
			});
			assert.equal(opened.isError, undefined, text(opened));
			assert.match(text(opened), /Opened Herdr project pane w1:p10/);
			const binding = readHerdrProjectPaneBinding(root);
			assert.equal(binding?.paneId, "w1:p10");
			assert.equal(binding?.projectRoot, projectRoot);
			assert.equal(binding?.startupMessage, "Own this project mission.");
			assert.deepEqual(listHerdrProjectPaneRoots(ownerRoot), [projectRoot]);
			const restoredState = { herdrProjectPanes: new Map() } as SubagentState;
			restoreHerdrProjectPaneSnapshots(restoredState, [...listHerdrProjectPaneRoots(ownerRoot), ownerRoot], Date.parse("2026-01-01T00:00:01.000Z"));
			const restored = restoredState.herdrProjectPanes?.get(projectRoot);
			assert.equal(restored?.paneId, "w1:p10");
			assert.equal(restored?.state, "open");
			assert.equal(restored?.ownership, "unknown");
			assert.equal(restored?.agentStatus, "unknown");
			const splitCall = calls.find((args) => args[0] === "pane" && args[1] === "split");
			assert.deepEqual(splitCall, ["pane", "split", "--current", "--direction", "right", "--cwd", projectRoot, "--no-focus"]);
			assert.equal(binding?.lastFocusedAt, undefined);
			const runCall = calls.find((args) => args[0] === "pane" && args[1] === "run" && args[2] === "w1:p10");
			assert.equal(runCall?.[3]?.startsWith("& "), process.platform === "win32");
			if (process.platform !== "win32") assert.match(runCall?.[3] ?? "", /^sh -c 'exec \"\$0\" \"\$@\"' '/);
			assert.match(runCall?.[3] ?? "", /pi-bin/);
			assert.match(runCall?.[3] ?? "", /Own this project mission\./);

			const status = await handleHerdrProjectPaneAction("project.status", { cwd: root }, { cwd: ownerRoot, client });
			assert.equal(status.isError, undefined, text(status));
			assert.match(text(status), /project pane w1:p10 is open/);

			const closed = await handleHerdrProjectPaneAction("project.close", { cwd: root }, { cwd: ownerRoot, client });
			assert.equal(closed.isError, undefined, text(closed));
			assert.match(text(closed), /Closed Herdr project pane w1:p10/);
			assert.equal(readHerdrProjectPaneBinding(root), undefined);
			assert.deepEqual(listHerdrProjectPaneRoots(ownerRoot), []);
			assert.ok(calls.some((args) => args.join(" ") === "pane close w1:p10"));

			calls.length = 0;
			const focused = await handleHerdrProjectPaneAction("project.open", { cwd: root, focus: true }, {
				cwd: ownerRoot,
				client,
				now: () => new Date("2026-01-01T00:00:00.000Z"),
			});
			assert.equal(focused.isError, undefined, text(focused));
			const focusedSplitCall = calls.find((args) => args[0] === "pane" && args[1] === "split");
			assert.deepEqual(focusedSplitCall, ["pane", "split", "--current", "--direction", "right", "--cwd", projectRoot, "--focus"]);
			assert.equal(readHerdrProjectPaneBinding(root)?.lastFocusedAt, "2026-01-01T00:00:00.000Z");
			fs.writeFileSync(path.join(ownerRoot, ".pi/subagents/project-panes/herdr-roots.json"), JSON.stringify({ schemaVersion: 1, kind: "herdr-project-pane-roots", projectRoots: [123] }));
			assert.throws(() => listHerdrProjectPaneRoots(ownerRoot), /Invalid Herdr project pane root index/);
		} finally {
			if (previousPiBinary === undefined) delete process.env[PI_SUBAGENT_PI_BINARY_ENV];
			else process.env[PI_SUBAGENT_PI_BINARY_ENV] = previousPiBinary;
			if (previousShell === undefined) delete process.env.SHELL;
			else process.env.SHELL = previousShell;
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("fails closed when an idle-only project pane close sees active work", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-project-pane-busy-"));
		const previousPiBinary = process.env[PI_SUBAGENT_PI_BINARY_ENV];
		process.env[PI_SUBAGENT_PI_BINARY_ENV] = path.join(root, "pi-bin");
		try {
			const projectRoot = fs.realpathSync(root);
			const calls: string[][] = [];
			let opened = false;
			const client: HerdrClient = {
				run: async <T>(args: string[]) => {
					calls.push(args);
					if (args[0] === "--version") return { ok: true, data: "herdr 0.8.0" as T };
					if (args[0] === "pane" && args[1] === "split") return { ok: true, data: { pane: { pane_id: "w1:p11" } } as T };
					if (args[0] === "pane" && args[1] === "run") { opened = true; return { ok: true, data: {} as T }; }
					if (args[0] === "pane" && args[1] === "get" && opened) return { ok: true, data: { pane: {
						pane_id: "w1:p11", agent: "pi", agent_status: "working", cwd: projectRoot,
					} } as T };
					return { ok: true, data: {} as T };
				},
			};
			const manager = createProjectPaneManager({ client });
			const open = await manager.open({ cwd: root, focus: false });
			assert.equal(open.ok, true);
			const closed = await manager.close({ cwd: root, requireIdle: true });
			assert.equal(closed.ok, false);
			if (!closed.ok) assert.equal(closed.error.code, "PANE_NOT_IDLE");
			assert.equal(calls.some((args) => args.join(" ") === "pane close w1:p11"), false);
			assert.equal(readHerdrProjectPaneBinding(root)?.paneId, "w1:p11");
		} finally {
			if (previousPiBinary === undefined) delete process.env[PI_SUBAGENT_PI_BINARY_ENV];
			else process.env[PI_SUBAGENT_PI_BINARY_ENV] = previousPiBinary;
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("preserves legacy model-facing recovery for transient and opaque pane inspection results", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-project-pane-legacy-"));
		const previousPiBinary = process.env[PI_SUBAGENT_PI_BINARY_ENV];
		process.env[PI_SUBAGENT_PI_BINARY_ENV] = path.join(root, "pi-bin");
		try {
			let splitCount = 0;
			let getMode: "valid" | "timeout" | "opaque" = "valid";
			const client: HerdrClient = {
				run: async <T>(args: string[]) => {
					if (args[0] === "--version") return { ok: true, data: "herdr 0.8.0" as T };
					if (args[0] === "pane" && args[1] === "split") {
						splitCount++;
						return { ok: true, data: { pane: { pane_id: `w1:p3${splitCount}` } } as T };
					}
					if (args[0] === "pane" && args[1] === "get") {
						if (getMode === "timeout") return { ok: false, error: { code: "TIMEOUT", message: "temporary" } };
						if (getMode === "opaque") return { ok: true, data: {} as T };
						return { ok: true, data: { pane: { pane_id: `w1:p3${splitCount}`, agent_status: "idle", cwd: root } } as T };
					}
					return { ok: true, data: {} as T };
				},
			};
			const first = await handleHerdrProjectPaneAction("project.open", { cwd: root }, { cwd: root, client });
			assert.equal(first.isError, undefined, text(first));
			getMode = "timeout";
			const recovered = await handleHerdrProjectPaneAction("project.open", { cwd: root }, { cwd: root, client });
			assert.equal(recovered.isError, undefined, text(recovered));
			assert.equal(splitCount, 2);
			assert.equal(readHerdrProjectPaneBinding(root)?.paneId, "w1:p32");
			getMode = "opaque";
			const status = await handleHerdrProjectPaneAction("project.status", { cwd: root }, { cwd: root, client });
			assert.equal(status.isError, undefined, text(status));
			assert.match(text(status), /w1:p32 is open/);
			const legacyBinding = readHerdrProjectPaneBinding(root)!;
			fs.writeFileSync(path.join(root, ".pi/subagents", "project-panes", "herdr.json"), JSON.stringify({ ...legacyBinding, startupMessage: 42 }));
			const closed = await handleHerdrProjectPaneAction("project.close", { cwd: root }, { cwd: root, client });
			assert.equal(closed.isError, true);
			assert.match(text(closed), /INVALID_PANE_RESPONSE/);
			assert.equal(readHerdrProjectPaneBinding(root)?.paneId, "w1:p32");
		} finally {
			if (previousPiBinary === undefined) delete process.env[PI_SUBAGENT_PI_BINARY_ENV];
			else process.env[PI_SUBAGENT_PI_BINARY_ENV] = previousPiBinary;
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

	it("rejects project pane targets that are not directories", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-herdr-project-pane-missing-"));
		try {
			const missing = path.join(root, "missing");
			const opened = await handleHerdrProjectPaneAction("project.open", { cwd: missing }, {
				cwd: root,
				client: { run: async () => ({ ok: true, data: {} }) },
			});
			assert.equal(opened.isError, true);
			assert.match(text(opened), /unavailable/);
		} finally {
			fs.rmSync(root, { recursive: true, force: true });
		}
	});

});
