Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/__serial__/controller.test.ts

Raw
import type { Model } from "@earendil-works/pi-ai";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
	__test,
	VoiceController,
	type VoiceControllerEvent,
} from "../src/controller.ts";
import { makeTurnClassifier } from "../src/turn-classifier.ts";

const debug = vi.hoisted(() => vi.fn());
vi.mock("../src/debug.ts", () => ({
	dbg: debug,
	span: undefined,
	closeDebug: vi.fn(),
}));

const model = (provider: string, id: string) =>
	({ provider, id }) as Model<any>;

const tick = () => new Promise((resolve) => setTimeout(resolve, 0));

function fixture(
	options: ConstructorParameters<typeof VoiceController>[1] = {},
	pi: ConstructorParameters<typeof VoiceController>[0] = {} as never,
) {
	const listeners: Array<(event: any) => void> = [];
	const listenersByChild = new Map<object, Array<(event: any) => void>>();
	const settles = new Map<object, () => void>();
	const prompts: string[] = [];
	const children: any[] = [];
	const createChild = () => {
		const childListeners: Array<(event: any) => void> = [];
		let promptActive = false;
		const child = {
			isIdle: false,
			setActiveToolsByName: () => undefined,
			abort: async () => settles.get(child)?.(),
			compact: vi.fn(async () => undefined),
			dispose: () => undefined,
			subscribe: (listener: (event: any) => void) => {
				listeners.push(listener);
				childListeners.push(listener);
				return () => undefined;
			},
			prompt: async (text: string) => {
				if (promptActive)
					throw new Error("AgentSession rejects simultaneous prompt calls.");
				promptActive = true;
				prompts.push(text);
				const message = { role: "assistant" };
				for (const listener of childListeners)
					listener({ type: "message_start", message });
				await new Promise<void>((resolve) => {
					settles.set(child, () => {
						promptActive = false;
						for (const listener of childListeners)
							listener({ type: "agent_settled", message });
						resolve();
					});
				});
			},
		};
		children.push(child);
		listenersByChild.set(child, childListeners);
		return child;
	};
	const controller = new VoiceController(pi, options);
	(controller as any).nativeMode = true;
	(controller as any).callActive = true;
	const install = (child: any) => {
		(controller as any).child = child;
		(controller as any).childUnsubscribe = child.subscribe((event: any) =>
			(controller as any).onChildEvent(event, child, 0),
		);
	};
	const child = createChild();
	install(child);
	(controller as any).ensureChild = async () => {
		const current = (controller as any).child;
		if (current) return current;
		const replacement = createChild();
		install(replacement);
		return replacement;
	};
	return {
		child,
		children,
		controller,
		prompts,
		listeners,
		emit: (target: object, event: any) => {
			for (const listener of listenersByChild.get(target) ?? [])
				listener(event);
		},
		settle: (target = children.at(-1)) => settles.get(target)?.(),
	};
}

describe("native voice system prompt contract", () => {
	it("makes live audio, listening, interrupted playback and recognition continuations explicit", () => {
		const instructions = __test.voiceInstructions;
		expect(instructions).toContain("live audio voice call");
		expect(instructions).toContain("human is listening to spoken output");
		expect(instructions).toContain(
			"reply field is intended to be spoken aloud",
		);
		expect(instructions).toContain(
			"heard and hotwords fields are protocol data",
		);
		expect(instructions).toContain("interrupt by speaking (barge-in)");
		expect(instructions).toContain(
			"Never assume generated text was fully heard without playback evidence",
		);
		expect(instructions).toContain(
			"use conversation context rather than treating every chunk as a new request",
		);
	});
	it("acknowledges automatic status without inventing awareness or weakening safety and output constraints", () => {
		const instructions = __test.voiceInstructions;
		expect(instructions).toContain(
			"automatically speak short progress-status commentary",
		);
		expect(instructions).toContain(
			"Those updates may not appear in your model context",
		);
		expect(instructions).toContain(
			"avoid unsolicited duplicate routine updates",
		);
		expect(instructions).toContain(
			"Speak as Pi: one identity answering the human directly",
		);
		expect(instructions).toContain(
			"Never claim, answer, or approve a question on the human's behalf",
		);
		expect(instructions).toContain(
			"never call an accepted request a completed result",
		);
		expect(instructions).toContain("one to three short, simple sentences");
		expect(instructions).toContain(
			"Output exactly one JSON object with keys in this order: heard",
		);
	});
});

describe("bounded voice model progress", () => {
	beforeEach(() => debug.mockClear());
	it.each([12_000, 29_000, 31_000, 59_000])(
		"accepts first text after %i ms without premature fallback",
		async (delay) => {
			vi.useFakeTimers();
			const { controller, child, emit, settle } = fixture();
			const events: VoiceControllerEvent[] = [];
			controller.subscribe((event) => events.push(event));
			try {
				(controller as any).receive("hello", "auto");
				await vi.advanceTimersByTimeAsync(delay);
				expect(
					debug.mock.calls.some(
						([event, details]) =>
							event === "voice.response" && details.outcome === "timeout",
					),
				).toBe(false);
				emit(child, {
					type: "message_update",
					message: { role: "assistant" },
					assistantMessageEvent: {
						type: "text_delta",
						delta:
							'{"heard":"hello","reply":"Late but complete.","hotwords":[]}',
					},
				});
				settle(child);
				await vi.advanceTimersByTimeAsync(0);
				expect(controller.nativeSnapshot()).toMatchObject({
					status: "ready",
					error: null,
				});
				expect(controller.nativeSnapshot().transcript.at(-1)?.text).toBe(
					"Late but complete.",
				);
				expect(
					events.filter((event) => event.type === "assistant-complete"),
				).toHaveLength(1);
			} finally {
				controller.close();
				vi.useRealTimers();
			}
		},
	);
	it("still retires silent requests exactly once at the bounded 60-second deadline", async () => {
		vi.useFakeTimers();
		const { controller } = fixture();
		const events: VoiceControllerEvent[] = [];
		controller.subscribe((event) => events.push(event));
		try {
			(controller as any).receive("hello", "auto");
			await vi.advanceTimersByTimeAsync(59_999);
			expect(
				debug.mock.calls.some(
					([event, details]) =>
						event === "voice.response" && details.outcome === "timeout",
				),
			).toBe(false);
			await vi.advanceTimersByTimeAsync(1);
			expect(
				debug.mock.calls.filter(
					([event, details]) =>
						event === "voice.response" && details.outcome === "timeout",
				),
			).toHaveLength(1);
			expect(debug).toHaveBeenCalledWith("voice.child.retire", {
				reason: "first-response",
			});
			expect(
				events.filter((event) => event.type === "assistant-complete"),
			).toHaveLength(1);
		} finally {
			controller.close();
			vi.useRealTimers();
		}
	});
	it("keeps the 15-second stall bound after text progress", async () => {
		vi.useFakeTimers();
		const { controller, child, emit } = fixture();
		try {
			(controller as any).receive("hello", "auto");
			await vi.advanceTimersByTimeAsync(45_000);
			emit(child, {
				type: "message_update",
				message: { role: "assistant" },
				assistantMessageEvent: {
					type: "text_delta",
					delta: '{"heard":"hello","reply":"Partial',
				},
			});
			await vi.advanceTimersByTimeAsync(14_999);
			expect(
				debug.mock.calls.some(
					([event, details]) =>
						event === "voice.response" && details.outcome === "timeout",
				),
			).toBe(false);
			await vi.advanceTimersByTimeAsync(1);
			expect(debug).toHaveBeenCalledWith("voice.response", {
				outcome: "timeout",
				reason: "response-stall",
			});
		} finally {
			controller.close();
			vi.useRealTimers();
		}
	});
	it("keeps real interruption immediate during the extended initial wait", async () => {
		vi.useFakeTimers();
		const { controller, prompts } = fixture();
		try {
			(controller as any).receive("first", "auto");
			await vi.advanceTimersByTimeAsync(45_000);
			controller.interruptNative();
			await vi.advanceTimersByTimeAsync(0);
			expect((controller as any).draining).toBe(false);
			(controller as any).receive("second", "auto");
			await vi.advanceTimersByTimeAsync(0);
			expect(prompts).toHaveLength(2);
			expect(debug).toHaveBeenCalledWith("voice.response", {
				outcome: "cancel",
				reason: "stop",
			});
		} finally {
			controller.close();
			vi.useRealTimers();
		}
	});
	it("does not manually compact or corrupt the eighth short completed reply", async () => {
		const { controller, child, emit, settle } = fixture();
		child.compact.mockRejectedValue(
			new Error("Nothing to compact (session too small)"),
		);
		const events: VoiceControllerEvent[] = [];
		controller.subscribe((event) => events.push(event));
		try {
			for (let index = 0; index < 8; index++) {
				(controller as any).receive(`hello ${index}`, "auto");
				await tick();
				emit(child, {
					type: "message_update",
					message: { role: "assistant" },
					assistantMessageEvent: {
						type: "text_delta",
						delta: JSON.stringify({
							heard: `hello ${index}`,
							reply: `Complete ${index}.`,
							hotwords: [],
						}),
					},
				});
				settle(child);
				await tick();
			}
			expect(child.compact).not.toHaveBeenCalled();
			expect(controller.nativeSnapshot()).toMatchObject({
				status: "ready",
				error: null,
			});
			expect(controller.nativeSnapshot().transcript.at(-1)?.text).toBe(
				"Complete 7.",
			);
			expect(
				events.filter((event) => event.type === "assistant-complete"),
			).toHaveLength(8);
		} finally {
			controller.close();
		}
	});
	it.each(["before", "after"] as const)(
		"preserves the reply when SDK maintenance reports failure %s text",
		async (when) => {
			const { controller, child, emit, settle } = fixture();
			const events: VoiceControllerEvent[] = [];
			controller.subscribe((event) => events.push(event));
			try {
				(controller as any).receive("hello", "auto");
				await tick();
				const maintenance = () => {
					emit(child, { type: "compaction_start", reason: "threshold" });
					emit(child, {
						type: "compaction_end",
						reason: "threshold",
						errorMessage: "maintenance unavailable",
						aborted: false,
					});
				};
				if (when === "before") maintenance();
				emit(child, {
					type: "message_update",
					message: { role: "assistant" },
					assistantMessageEvent: {
						type: "text_delta",
						delta: '{"heard":"hello","reply":"Complete.","hotwords":[]}',
					},
				});
				if (when === "after") maintenance();
				settle(child);
				await tick();
				expect(controller.nativeSnapshot()).toMatchObject({
					status: "ready",
					error: null,
				});
				expect(controller.nativeSnapshot().transcript.at(-1)?.text).toBe(
					"Complete.",
				);
				expect(
					events.filter((event) => event.type === "assistant-complete"),
				).toHaveLength(1);
			} finally {
				controller.close();
			}
		},
	);
});

describe("voice response diagnostics", () => {
	it("does not resurrect a pending notification after hangup", async () => {
		const f = fixture({ firstResponseTimeoutMs: 1_000 });
		(f.controller as any).pendingResults.push({
			text: "Work completed",
			executorMessageId: undefined,
		});
		(f.controller as any).drain("auto");
		await tick();
		expect(f.prompts).toHaveLength(1);
		f.controller.endNative();
		await tick();
		await tick();
		expect(f.prompts).toHaveLength(1);
		expect((f.controller as any).pendingResults).toEqual([]);
		f.controller.close();
	});

	it("invalidates old voice tool closures before replacing the executor session", async () => {
		const sent = vi.fn();
		const aborted = vi.fn();
		const f = fixture({}, { sendUserMessage: sent } as never);
		const context = (id: string) => ({
			cwd: "/fixture",
			isIdle: () => true,
			abort: aborted,
			sessionManager: {
				getSessionId: () => id,
				getSessionName: () => undefined,
				getBranch: () => [],
				buildSessionProjection: () => ({ entries: [] }),
			},
		});
		f.controller.start(context("a") as never);
		const old = (f.controller as any).tools();
		f.controller.onExecutorSessionSwitch();
		f.controller.start(context("b") as never);
		for (const name of [
			"voice_executor_dispatch",
			"voice_executor_steer",
			"voice_executor_cancel",
		]) {
			const result = await old
				.find((tool: any) => tool.name === name)
				.execute("old", { text: "Do not run" });
			expect(result.details.accepted).toBe(false);
		}
		expect(sent).not.toHaveBeenCalled();
		expect(aborted).not.toHaveBeenCalled();
		f.controller.close();
	});

	it("parses the final assistant JSON separately from pre-tool text", async () => {
		const f = fixture({ firstResponseTimeoutMs: 1_000 });
		(f.controller as any).receive("Inspect status", "auto");
		await tick();
		const delta = (text: string) =>
			f.emit(f.child, {
				type: "message_update",
				message: { role: "assistant" },
				assistantMessageEvent: { type: "text_delta", delta: text },
			});
		delta('{"heard":"Inspect status","reply":"Working.","hotwords":[]}');
		f.emit(f.child, { type: "message_start", message: { role: "assistant" } });
		delta(
			'{"heard":"Inspect status","reply":"The work is pending.","hotwords":[]}',
		);
		f.settle();
		await tick();
		expect((f.controller as any).activeTurn.raw).toBe(
			'{"heard":"Inspect status","reply":"The work is pending.","hotwords":[]}',
		);
		expect((f.controller as any).activeTurn.fallbackSpoken).toBe(false);
		f.controller.close();
	});
	beforeEach(() => debug.mockClear());

	it.each(["stop", "controller-close", "new-input"] as const)(
		"distinguishes %s cancellation from timeout",
		async (reason) => {
			const { controller } = fixture({ firstResponseTimeoutMs: 1_000 });
			(controller as any).receive("first", "auto");
			await tick();
			if (reason === "controller-close") controller.close();
			else if (reason === "stop") (controller as any).stopVoice();
			else (controller as any).receive("second", "auto");
			await tick();
			expect(debug).toHaveBeenCalledWith("voice.response", {
				outcome: "cancel",
				reason,
			});
			expect(debug).toHaveBeenCalledWith("voice.child.retire", { reason });
			expect(
				debug.mock.calls.some(
					([event, details]) =>
						event === "voice.response" && details.outcome === "timeout",
				),
			).toBe(false);
			controller.close();
		},
	);

	it("keeps tool-only progress within the existing stall bound", async () => {
		const { controller, child, emit, settle } = fixture({
			firstResponseTimeoutMs: 5,
			responseStallTimeoutMs: 40,
		});
		(controller as any).receive("inspect status", "auto");
		await tick();
		emit(child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: { type: "toolcall_start" },
		});
		await new Promise((resolve) => setTimeout(resolve, 10));
		emit(child, {
			type: "tool_execution_update",
			toolCallId: "tool-1",
			toolName: "voice_executor_dispatch",
			args: {},
			partialResult: {},
		});
		await new Promise((resolve) => setTimeout(resolve, 10));
		expect(
			debug.mock.calls.some(
				([event, details]) =>
					event === "voice.response" && details.outcome === "timeout",
			),
		).toBe(false);
		settle();
		await tick();
		controller.close();
	});

	it.each(["first-response", "response-stall"] as const)(
		"identifies %s deadline",
		async (reason) => {
			const { controller, child, emit } = fixture({
				firstResponseTimeoutMs: reason === "first-response" ? 5 : 1_000,
				responseStallTimeoutMs: 5,
			});
			(controller as any).receive("hello", "auto");
			await tick();
			if (reason === "response-stall")
				emit(child, {
					type: "message_update",
					message: { role: "assistant" },
					assistantMessageEvent: {
						type: "text_delta",
						delta: '{"heard":"hello",',
					},
				});
			await new Promise((resolve) => setTimeout(resolve, 20));
			expect(debug).toHaveBeenCalledWith("voice.response", {
				outcome: "timeout",
				reason,
			});
			expect(debug).toHaveBeenCalledWith("voice.child.retire", { reason });
			expect((controller as any).error).toContain("timed out");
			controller.close();
		},
	);
});

describe("voice response validation", () => {
	beforeEach(() => debug.mockClear());

	it("accepts completed native prose after tool use without speaking a tool preamble or inventing recognition", async () => {
		const sendUserMessage = vi.fn();
		const f = fixture({}, { sendUserMessage } as never);
		f.controller.start({ cwd: "/fixture", isIdle: () => true } as never);
		const events: VoiceControllerEvent[] = [];
		f.controller.subscribe((event) => events.push(event));
		(f.controller as any).receive("Use Nerd Font icons.", "auto");
		await tick();
		f.emit(f.child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: {
				type: "text_delta",
				delta: "I will dispatch this.",
			},
		});
		(f.controller as any).sendToExecutor("Use Nerd Font icons.", "dispatch");
		f.emit(f.child, {
			type: "message_end",
			message: { role: "assistant", stopReason: "toolUse" },
		});
		f.emit(f.child, { type: "message_start", message: { role: "assistant" } });
		(f.controller as any).pendingHotwords = ["stale"];
		const reply = "I've asked to switch the state icons to Nerd Font glyphs.";
		f.emit(f.child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: { type: "text_delta", delta: reply },
		});
		expect(
			events.filter((event) => event.type === "assistant-speech"),
		).toHaveLength(0);
		f.emit(f.child, {
			type: "message_end",
			message: { role: "assistant", stopReason: "stop" },
		});
		expect(
			events.filter((event) => event.type === "assistant-speech"),
		).toHaveLength(0);
		f.settle();
		await tick();
		expect(events.filter((event) => event.type === "assistant")).toHaveLength(
			1,
		);
		expect(
			events
				.filter((event) => event.type === "assistant-speech")
				.map((event: any) => event.text)
				.join(""),
		).toBe(reply);
		expect(
			events.filter((event) => event.type === "assistant-complete"),
		).toHaveLength(1);
		expect(events).toContainEqual(
			expect.objectContaining({ type: "heard", text: "Use Nerd Font icons." }),
		);
		expect((f.controller as any).pendingHotwords).toEqual([]);
		expect((f.controller as any).activeTurn.fallbackSpoken).toBe(false);
		expect(sendUserMessage).toHaveBeenCalledOnce();
		f.controller.close();
	});

	it.each([
		{ raw: "An otherwise valid answer.", stopReason: "error", native: true },
		{ raw: "An otherwise valid answer.", stopReason: "aborted", native: true },
		{ raw: "An otherwise valid answer.", stopReason: "length", native: true },
		{ raw: "An otherwise valid answer.", stopReason: undefined, native: true },
		{ raw: "An otherwise valid answer.", stopReason: "stop", native: false },
		{ raw: '```json\n{"reply":"no"}\n```', stopReason: "stop", native: true },
		{ raw: '{"heard":"request",', stopReason: "stop", native: true },
		{
			raw: "reply: unsafe protocol fragment",
			stopReason: "stop",
			native: true,
		},
		{ raw: "[]", stopReason: "stop", native: true },
		{ raw: "null trailing tokens", stopReason: "stop", native: true },
		{ raw: 'heard":"broken document', stopReason: "stop", native: true },
		{ raw: '"JSON scalar"', stopReason: "stop", native: true },
	])(
		"never promotes incomplete/error/protocol output to native prose: $stopReason $raw",
		async ({ raw, stopReason, native }) => {
			const f = fixture();
			(f.controller as any).nativeMode = native;
			const events: VoiceControllerEvent[] = [];
			f.controller.subscribe((event) => events.push(event));
			(f.controller as any).receive("recognized request", "auto");
			await tick();
			f.emit(f.child, {
				type: "message_update",
				message: { role: "assistant" },
				assistantMessageEvent: { type: "text_delta", delta: raw },
			});
			f.emit(f.child, {
				type: "message_end",
				message: { role: "assistant", stopReason },
			});
			f.settle();
			await tick();
			expect((f.controller as any).activeTurn.fallbackSpoken).toBe(true);
			expect(
				events
					.filter((event) => event.type === "assistant-speech")
					.map((event: any) => event.text)
					.join(""),
			).toBe("I couldn't finish my response.");
			f.controller.close();
		},
	);

	it("does not accept an oversized prose prefix as a complete response", async () => {
		const f = fixture();
		(f.controller as any).receive("recognized request", "auto");
		await tick();
		for (const delta of ["A valid-looking prefix.", "x".repeat(100_000)])
			f.emit(f.child, {
				type: "message_update",
				message: { role: "assistant" },
				assistantMessageEvent: { type: "text_delta", delta },
			});
		f.emit(f.child, {
			type: "message_end",
			message: { role: "assistant", stopReason: "stop" },
		});
		f.settle();
		await tick();
		expect((f.controller as any).activeTurn.fallbackSpoken).toBe(true);
		f.controller.close();
	});

	it.each([
		["", "empty-output"],
		["{", "invalid-json"],
		['{"heard":"recognized request","reply":"hello"}', "invalid-schema"],
		['{"heard":"","reply":"hello","hotwords":[]}', "empty-heard"],
	])(
		"reports %s validation failure without blaming speech recognition",
		async (raw, reason) => {
			const { controller, child, emit, settle } = fixture();
			(controller as any).receive("recognized request", "auto");
			await tick();
			emit(child, {
				type: "message_update",
				message: { role: "assistant" },
				assistantMessageEvent: { type: "text_delta", delta: raw },
			});
			settle();
			await tick();
			expect((controller as any).transcript.at(-1)?.text).toBe(
				"I couldn't finish my response.",
			);
			expect(
				(controller as any).transcript.find(
					(entry: any) => entry.role === "user",
				).text,
			).toBe("recognized request");
			expect(debug).toHaveBeenCalledWith("voice.response", {
				outcome: "rejected",
				reason,
				workAccepted: false,
				bytes: Buffer.byteLength(raw),
			});
			controller.close();
		},
	);

	it.each([false, true])(
		"preserves dispatch acceptance=%s when the reply is malformed",
		async (accepted) => {
			const sendUserMessage = vi.fn(() => {
				if (!accepted) throw new Error("dispatch rejected");
			});
			const { controller, child, emit, settle } = fixture({}, {
				sendUserMessage,
			} as never);
			controller.start({ cwd: "/fixture", isIdle: () => true } as never);
			(controller as any).receive("research this", "auto");
			await tick();
			const dispatch = (controller as any)
				.tools()
				.find((tool: any) => tool.name === "voice_executor_dispatch");
			const result = await dispatch.execute("tool", { text: "research this" });
			expect(result.details.accepted).toBe(accepted);
			emit(child, {
				type: "message_update",
				message: { role: "assistant" },
				assistantMessageEvent: { type: "text_delta", delta: "{" },
			});
			settle();
			await tick();
			expect((controller as any).transcript.at(-1)?.text).toBe(
				accepted
					? "Work is still pending. I couldn't generate a valid response."
					: "I couldn't finish my response.",
			);
			expect(sendUserMessage).toHaveBeenCalledOnce();
			expect((controller as any).executorRequests.size).toBe(accepted ? 1 : 0);
			expect(debug).toHaveBeenCalledWith("voice.response", {
				outcome: "rejected",
				reason: "invalid-json",
				workAccepted: accepted,
				bytes: 1,
			});
			controller.close();
		},
	);

	it("retains accepted work after a subsequent response stall", async () => {
		const sendUserMessage = vi.fn();
		const { controller, child, emit } = fixture(
			{ firstResponseTimeoutMs: 1_000, responseStallTimeoutMs: 5 },
			{ sendUserMessage } as never,
		);
		controller.start({ cwd: "/fixture", isIdle: () => true } as never);
		(controller as any).receive("research this", "auto");
		await tick();
		(controller as any).sendToExecutor("research this", "dispatch");
		emit(child, {
			type: "tool_execution_end",
			toolCallId: "tool",
			toolName: "voice_executor_dispatch",
			result: {},
			isError: false,
		});
		await new Promise((resolve) => setTimeout(resolve, 20));
		expect((controller as any).transcript.at(-1)?.text).toBe(
			"Work is still pending. I couldn't generate a valid response.",
		);
		expect(sendUserMessage).toHaveBeenCalledOnce();
		expect((controller as any).executorRequests.size).toBe(1);
		expect(debug).toHaveBeenCalledWith(
			"voice.response",
			expect.objectContaining({ outcome: "timeout", reason: "response-stall" }),
		);
		controller.close();
	});

	it("accepts deliberate silent human turns without a fallback", async () => {
		const { child, controller, emit, settle } = fixture();
		const events: any[] = [];
		controller.subscribe((event) => events.push(event));
		(controller as any).receive("wait", "auto");
		await tick();
		emit(child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: {
				type: "text_delta",
				delta: '{"heard":"","reply":"","hotwords":[]}',
			},
		});
		settle();
		await tick();
		expect(events.some((event) => event.type === "assistant-speech")).toBe(
			false,
		);
		expect((controller as any).transcript.at(-1)).toMatchObject({
			role: "user",
			text: "wait",
		});
		expect(
			(controller as any).transcript.some(
				(entry: any) => entry.role === "assistant" && !entry.text,
			),
		).toBe(false);
		expect(debug).not.toHaveBeenCalledWith(
			"voice.response",
			expect.objectContaining({ outcome: "rejected" }),
		);
		controller.close();
	});
});

describe("voice model selection", () => {
	it("prefers current-generation fast GPT over legacy and heavyweight choices", () => {
		const luna = model("openai", "gpt-6-luna");
		const selected = __test.selectModel(
			{
				modelRegistry: {
					getAvailable: () => [
						model("openai", "gpt-5.3-codex-spark"),
						model("openai", "gpt-6-pro"),
						luna,
					],
				},
			} as never,
			"auto",
		);
		expect(selected).toMatchObject({ model: luna, kind: "auto" });
	});

	it("prefers configured OpenAI Codex provider and mini over nano on equal generation", () => {
		const preferred = model("openai-codex", "gpt-6-mini");
		const selected = __test.selectModel(
			{
				modelRegistry: {
					getAvailable: () => [
						model("openai", "gpt-6-mini"),
						model("openai-codex", "gpt-6-nano"),
						preferred,
					],
				},
			} as never,
			"auto",
		);
		expect(selected).toMatchObject({ model: preferred, kind: "auto" });
	});

	it("does not arbitrarily fall back when no GPT model is available", () => {
		const selected = __test.selectModel(
			{
				modelRegistry: {
					getAvailable: () => [model("anthropic", "claude-sonnet")],
				},
			} as never,
			"auto",
		);
		expect(selected).toEqual({ model: undefined, kind: "none" });
	});

	it("does not silently fall back when configured model is unavailable", () => {
		const selected = __test.selectModel(
			{
				modelRegistry: { getAvailable: () => [model("openai", "gpt-5-mini")] },
			} as never,
			"openai/gpt-4.1-mini",
		);
		expect(selected).toEqual({ model: undefined, kind: "none" });
	});
});

describe("voice turns", () => {
	it("exposes receipt state to model-facing content", () => {
		const result = __test.receiptResult("voice-1", "state", {
			state: "active",
			tool: "read",
		});
		expect(result.content[0]?.text).toContain('"state":"active"');
		expect(result.details).toMatchObject({
			id: "voice-1",
			state: "active",
			tool: "read",
		});
	});

	it("keeps executor receipts pending until a tagged final response settles", () => {
		const completion = __test.executorCompletion({
			cancelRequested: false,
			result: {
				role: "assistant",
				content: [{ type: "text", text: "" }],
				stopReason: "stop",
			},
		} as never);
		expect(completion).toEqual({
			status: "error",
			text: "Pi did not provide a final written result.",
		});
		expect(
			__test.executorCompletion({
				cancelRequested: true,
				result: undefined,
			} as never),
		).toMatchObject({ status: "cancelled" });
		expect(
			__test.executorCompletion({
				cancelRequested: false,
				result: {
					role: "assistant",
					content: [],
					stopReason: "error",
					errorMessage: "network unavailable",
				},
			} as never),
		).toMatchObject({
			status: "error",
			text: "Pi could not complete the work: network unavailable",
		});
	});

	it("backs current executor branch separately, bounds it, and redacts unsafe text", () => {
		const branch = Array.from({ length: 26 }, (_, index) => ({
			type: "message",
			id: `entry-${index}`,
			message:
				index === 2
					? {
							role: "toolResult",
							toolName: "shell",
							content: [
								{
									type: "text",
									text: `<img src=x> token=secret process.env.AUTH AWS_SECRET_ACCESS_KEY=aws-secret\n-----BEGIN PRIVATE KEY-----\nprivate-key-material\n-----END PRIVATE KEY-----`,
								},
							],
						}
					: {
							role: index % 2 ? "user" : "assistant",
							content: [{ type: "text", text: "x".repeat(2_000) }],
						},
		}));
		const messages = __test.executorMessages({
			sessionManager: { getBranch: () => branch },
		} as never);
		expect(messages).toHaveLength(24);
		expect(messages[0]?.id).toBe("entry-2");
		expect(messages.every((message) => message.text.length <= 1_800)).toBe(
			true,
		);
		const visible = messages.map((message) => message.text).join("\n");
		expect(visible).toContain("<img src=x>");
		expect(visible).not.toContain("secret");
		expect(visible).toContain("[environment redacted]");
		expect(visible).not.toContain("aws-secret");
		expect(visible).not.toContain("private-key-material");
	});

	it("redacts quoted credential labels and complete spaced values in shared executor context", () => {
		const messages = __test.executorMessages({
			sessionManager: {
				getBranch: () => [
					{
						type: "message",
						id: "credentials",
						message: {
							role: "toolResult",
							toolName: "nu",
							content: [
								{
									type: "text",
									text: JSON.stringify({
										ACCESS_KEY: "fixture key value",
										SECRET_VALUE: "fixture secret value",
									}),
								},
							],
						},
					},
				],
			},
		} as never);
		expect(messages[0]?.text).toContain("[redacted]");
		expect(messages[0]?.text).not.toContain("fixture key value");
		expect(messages[0]?.text).not.toContain("fixture secret value");
	});

	it("scans only the bounded tail of large executor branches", () => {
		const branch = Array.from({ length: 300 }, (_, index) => ({
			type: "message",
			id: `entry-${index}`,
			message: {
				role: "assistant",
				content: [{ type: "text", text: `message ${index}` }],
			},
		}));
		const messages = __test.executorMessages({
			sessionManager: { getBranch: () => branch },
		} as never);
		expect(messages).toHaveLength(24);
		expect(messages[0]?.id).toBe("entry-276");
		expect(messages.at(-1)?.id).toBe("entry-299");
	});

	it("refreshes only current executor branch and resets it on session switch", () => {
		let branch = [
			{
				type: "message",
				id: "old",
				message: {
					role: "assistant",
					content: [{ type: "text", text: "executor old" }],
				},
			},
		];
		const context = (id: string) =>
			({
				isIdle: () => true,
				sessionManager: {
					getSessionId: () => id,
					getSessionName: () => undefined,
					getBranch: () => branch,
					buildSessionProjection: () => ({ entries: [] }),
				},
			}) as never;
		const controller = new VoiceController({} as never);
		controller.start(context("session-one"));
		expect((controller as any).executor.messages).toMatchObject([
			{ id: "old", text: "executor old" },
		]);
		branch = [
			{
				type: "message",
				id: "new",
				message: {
					role: "user",
					content: [{ type: "text", text: "executor live" }],
				},
			},
		];
		controller.onExecutorMessageUpdate();
		expect((controller as any).executor.messages).toMatchObject([
			{ id: "new", text: "executor live" },
		]);
		branch = [];
		controller.start(context("session-two"));
		expect((controller as any).executor).toMatchObject({
			session: "session-",
			messages: [],
		});
	});

	it("provides current local clock context instead of a hardcoded time", () => {
		const now = new Date("2026-03-30T12:34:56.000Z");
		const clock = __test.localClock(now);
		expect(clock).toContain("2026");
		expect(clock).not.toContain("10:00");
		expect(clock).toMatch(/^Local clock: .+\(.+\)$/u);
	});

	it("drops pending spoken executor updates when a call ends", () => {
		const controller = new VoiceController({} as never);
		(controller as any).callActive = true;
		(controller as any).pendingResults = [
			"<executor-result>done</executor-result>",
		];
		(controller as any).callActive = false;
		(controller as any).pendingResults = [];
		(controller as any).enqueueExecutorResult(
			"voice-late",
			"completed",
			"late result",
		);
		expect((controller as any).pendingResults).toEqual([]);
	});

	it("blocks executor dispatch from a result-notification turn", async () => {
		const sent: string[] = [];
		const controller = new VoiceController({
			sendUserMessage: (text: string) => sent.push(text),
		} as never);
		(controller as any).activeTurn = {
			ticket: 0,
			assistantId: "voice-result",
			kind: "executor-result",
		};
		const dispatch = (controller as any)
			.tools()
			.find(
				(tool: { name: string }) => tool.name === "voice_executor_dispatch",
			);
		const result = await dispatch.execute("tool", { text: "do more work" });
		expect(result.details).toMatchObject({ accepted: false });
		expect(sent).toEqual([]);
	});

	it("keeps one outstanding executor job, steers it by the same id, then settles once", async () => {
		const sent: Array<{ text: string; options?: unknown }> = [];
		const controller = new VoiceController({
			sendUserMessage: (text: string, options?: unknown) =>
				sent.push({ text, options }),
		} as never);
		controller.start({ isIdle: () => false } as never);
		const tools = (controller as any).tools();
		const dispatch = tools.find(
			(tool: { name: string }) => tool.name === "voice_executor_dispatch",
		);
		const steer = tools.find(
			(tool: { name: string }) => tool.name === "voice_executor_steer",
		);
		const first = await dispatch.execute("tool", { text: "research Alpha" });
		const id = first.details.id;
		expect(first.details).toMatchObject({ accepted: true, id });
		expect((controller as any).executor.entries).toMatchObject([
			{ id, status: "queued", summary: "research Alpha" },
		]);
		controller.onExecutorMessageStart({
			role: "user",
			content: `[Voice executor request id=${id}]\nresearch Alpha`,
		});
		const redirected = await steer.execute("tool", { text: "focus on Beta" });
		expect(redirected.details).toMatchObject({ accepted: true, id });
		expect(sent).toHaveLength(2);
		expect(sent[1]?.text).toContain(`id=${id}`);
		expect(sent[1]?.options).toEqual({ deliverAs: "steer" });
		expect((controller as any).executorRequests.size).toBe(1);
		const result = {
			role: "assistant",
			content: [{ type: "text", text: "Beta is ready." }],
			stopReason: "stop",
		};
		controller.onExecutorMessageEnd(result);
		controller.onExecutorSettled();
		expect((controller as any).executor.entries).toMatchObject([
			{
				id,
				status: "completed",
				summary: "focus on Beta",
				result: "Beta is ready.",
			},
		]);
		expect((controller as any).executorRequests.size).toBe(0);
	});

	it("queues a second native dispatch while work is outstanding", async () => {
		const sent: string[] = [];
		const controller = new VoiceController({
			sendUserMessage: (text: string) => sent.push(text),
		} as never);
		controller.start({ isIdle: () => true } as never);
		const dispatch = (controller as any)
			.tools()
			.find(
				(tool: { name: string }) => tool.name === "voice_executor_dispatch",
			);
		expect(
			(await dispatch.execute("tool", { text: "first" })).details,
		).toMatchObject({
			accepted: true,
		});
		expect(
			(await dispatch.execute("tool", { text: "second" })).details,
		).toMatchObject({
			accepted: true,
		});
		expect(sent).toHaveLength(2);
		expect((controller as any).executorRequests.size).toBe(2);
	});

	it("cancels an aborted queued submission instead of leaving it queued", async () => {
		const controller = new VoiceController({
			sendUserMessage: () => undefined,
		} as never);
		controller.start({ isIdle: () => true } as never);
		const dispatch = (controller as any)
			.tools()
			.find(
				(tool: { name: string }) => tool.name === "voice_executor_dispatch",
			);
		const { id } = (await dispatch.execute("tool", { text: "queued work" }))
			.details;
		controller.onExecutorSettled(true);
		expect((controller as any).executor.entries).toMatchObject([
			{ id, status: "cancelled" },
		]);
		expect((controller as any).executorRequests.size).toBe(0);
	});

	it("marks a running job awaiting native Pi input", async () => {
		const controller = new VoiceController({
			sendUserMessage: () => undefined,
		} as never);
		controller.start({ isIdle: () => true } as never);
		const dispatch = (controller as any)
			.tools()
			.find(
				(tool: { name: string }) => tool.name === "voice_executor_dispatch",
			);
		const { id } = (await dispatch.execute("tool", { text: "ask me" })).details;
		controller.onExecutorMessageStart({
			role: "user",
			content: `[Voice executor request id=${id}]\nask me`,
		});
		controller.onExecutorPromptStart("Native confirmation");
		expect((controller as any).executor).toMatchObject({
			waitingForInput: { title: "Native confirmation", source: "pi" },
			entries: [{ id, status: "awaiting-input" }],
		});
		controller.onExecutorPromptEnd();
		expect((controller as any).executor.entries).toMatchObject([
			{ id, status: "running" },
		]);
	});

	it("does not settle voice work at agent_end", async () => {
		const controller = new VoiceController({
			sendUserMessage: () => undefined,
		} as never);
		controller.start({ isIdle: () => true } as never);
		const dispatch = (controller as any)
			.tools()
			.find(
				(tool: { name: string }) => tool.name === "voice_executor_dispatch",
			);
		const { id } = (await dispatch.execute("tool", { text: "pending work" }))
			.details;
		controller.onExecutorMessageStart({
			role: "user",
			content: `[Voice executor request id=${id}]\npending work`,
		});
		controller.onExecutorAgentEnd([
			{
				role: "assistant",
				content: [{ type: "text", text: "not settled yet" }],
				stopReason: "stop",
			},
		]);
		expect((controller as any).executor.entries).toMatchObject([
			{ id, status: "running" },
		]);
		expect((controller as any).executorRequests.size).toBe(1);
	});

	it("resolves a persisted result id when settlement reaches the branch late", async () => {
		let branch: unknown[] = [];
		const controller = new VoiceController({
			sendUserMessage: () => undefined,
		} as never);
		controller.start({
			isIdle: () => true,
			sessionManager: {
				getBranch: () => branch,
				buildSessionProjection: () => ({ entries: [] }),
				getSessionId: () => "fixture-session",
				getSessionName: () => undefined,
			},
		} as never);
		(controller as any).callActive = true;
		const dispatch = (controller as any)
			.tools()
			.find(
				(tool: { name: string }) => tool.name === "voice_executor_dispatch",
			);
		const { id } = (await dispatch.execute("tool", { text: "persist result" }))
			.details;
		controller.onExecutorMessageStart({
			role: "user",
			content: `[Voice executor request id=${id}]\npersist result`,
		});
		const result = {
			role: "assistant",
			content: [{ type: "text", text: "saved result" }],
			stopReason: "stop",
		};
		controller.onExecutorMessageEnd(result);
		branch = [
			{
				type: "message",
				id: "persisted-result-id",
				message: { ...result, content: [...result.content] },
			},
		];
		controller.onExecutorSettled();
		expect((controller as any).pendingResults[0]?.executorMessageId).toBe(
			"persisted-result-id",
		);
	});

	it("holds executor completion during speech, then supplies it at human commit", async () => {
		const { controller, prompts, settle } = fixture();
		(controller as any).callActive = true;
		controller.onHumanSpeech(true);
		(controller as any).enqueueExecutorResult("result-1", "completed", "done");
		await tick();
		expect(prompts).toEqual([]);
		controller.onHumanSpeech(false);
		await tick();
		expect(prompts).toEqual([]);
		(controller as any).receive("what happened?", "auto", "speech");
		await tick();
		expect(prompts).toHaveLength(1);
		expect(prompts[0]).toContain('<work-result id="result-1"');
		settle();
		controller.close();
	});

	it("releases executor results at confirmed empty speech but not during active speech", async () => {
		const { controller, prompts, settle } = fixture();
		(controller as any).callActive = true;
		controller.onHumanSpeech(true);
		(controller as any).enqueueExecutorResult(
			"empty-result",
			"completed",
			"done",
		);
		controller.onEmptySpeechBoundary();
		await tick();
		expect(prompts).toEqual([]);
		controller.onHumanSpeech(false);
		controller.onEmptySpeechBoundary();
		await tick();
		expect(prompts).toHaveLength(1);
		expect(prompts[0]).toContain('<work-result id="empty-result"');
		settle();
		controller.close();
	});

	it("delivers a completion that arrives after acknowledged playback during idle", async () => {
		const { child, controller, emit, prompts, settle } = fixture();
		(controller as any).callActive = true;
		(controller as any).receive("hello", "auto");
		await tick();
		const assistantTurnId = (controller as any).activeTurn.assistantId;
		emit(child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: {
				type: "text_delta",
				delta: '{"heard":"hello","reply":"Hi.","hotwords":[]}',
			},
		});
		settle(child);
		await tick();
		expect(controller.turnBoundary(assistantTurnId)).toBe(true);
		(controller as any).enqueueExecutorResult("late", "completed", "done");
		await tick();
		expect(prompts).toHaveLength(2);
		expect(prompts[1]).toContain('<work-result id="late"');
		controller.close();
	});

	it("silently skips empty executor results with correlated metadata and later drains", async () => {
		const { child, controller, emit, prompts, settle } = fixture();
		const events: any[] = [];
		controller.subscribe((event) => events.push(event));
		(controller as any).callActive = true;
		(controller as any).boundaryReady = true;
		(controller as any).enqueueExecutorResult(
			"first",
			"completed",
			"done",
			"executor-message-1",
		);
		await tick();
		const first = (controller as any).activeTurn;
		expect(first.userId).toMatch(/^voice-/u);
		emit(child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: {
				type: "text_delta",
				delta: '{"heard":"","reply":"","hotwords":[]}',
			},
		});
		settle(child);
		await tick();
		expect(events.some((event) => event.type === "assistant-speech")).toBe(
			false,
		);
		expect(events).toContainEqual(
			expect.objectContaining({
				type: "assistant-complete",
				userTurnId: first.userId,
			}),
		);
		expect(
			(controller as any).transcript.some(
				(entry: any) => entry.id === first.assistantId,
			),
		).toBe(false);
		expect(controller.turnBoundary(first.assistantId)).toBe(true);
		(controller as any).enqueueExecutorResult("second", "completed", "later");
		await tick();
		expect(prompts).toHaveLength(2);
		expect(prompts[1]).toContain('<work-result id="second"');
		controller.close();
	});

	it("switches once at configured playback duration for the matching turn", async () => {
		const { child, controller, emit, settle } = fixture({
			modality: { maxPlaybackMs: 250 },
		});
		const events: any[] = [];
		controller.subscribe((event) => events.push(event));
		(controller as any).receive("hello", "auto");
		await tick();
		emit(child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: {
				type: "text_delta",
				delta: '{"heard":"hello","reply":"A short sentence.","hotwords":[]}',
			},
		});
		settle(child);
		await tick();
		const turn = (controller as any).activeTurn;
		controller.setPlaybackDuration("obsolete-turn", 500);
		expect(events.filter((event) => event.type === "modality")).toHaveLength(0);
		controller.setPlaybackDuration(turn.userId, 251);
		controller.setPlaybackDuration(turn.userId, 500);
		expect(events.filter((event) => event.type === "modality")).toEqual([
			expect.objectContaining({
				userTurnId: turn.userId,
				modality: "transcript",
			}),
		]);
		controller.close();
	});

	it("announces executor results without inventing a human correction", async () => {
		const { child, controller, emit, settle } = fixture();
		const events: any[] = [];
		controller.subscribe((event) => events.push(event));
		(controller as any).callActive = true;
		(controller as any).enqueueExecutorResult(
			"result",
			"completed",
			"done",
			"executor-message-2",
		);
		await tick();
		emit(child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: {
				type: "text_delta",
				delta: '{"heard":"","reply":"The function works now.","hotwords":[]}',
			},
		});
		settle(child);
		await tick();
		expect(
			events
				.filter((event) => event.type === "assistant-speech")
				.map((event) => event.text)
				.join(""),
		).toBe("The function works now.");
		expect((controller as any).transcript.at(-1)).toMatchObject({
			executorMessageId: "executor-message-2",
			text: "The function works now.",
		});
		controller.close();
	});

	it("speaks fallback after model first-response timeout", async () => {
		const { controller } = fixture({ firstResponseTimeoutMs: 5 });
		const events: any[] = [];
		controller.subscribe((event) => events.push(event));
		(controller as any).receive("hello", "auto");
		await new Promise((resolve) => setTimeout(resolve, 20));
		expect((controller as any).error).toContain("timed out");
		expect(events).toContainEqual(
			expect.objectContaining({
				type: "assistant-speech",
				text: "I couldn't finish my response.",
			}),
		);
		controller.close();
	});

	it("unlocks timeout when abort never settles", async () => {
		const { child, controller, prompts } = fixture({
			firstResponseTimeoutMs: 5,
		});
		child.abort = async () => await new Promise<void>(() => undefined);
		(controller as any).receive("first", "auto");
		await new Promise((resolve) => setTimeout(resolve, 20));
		expect((controller as any).draining).toBe(false);
		(controller as any).receive("second", "auto");
		await tick();
		expect(prompts).toHaveLength(2);
		controller.close();
	});

	it("cancels a non-settling prompt on stop, then accepts a new turn", async () => {
		const { child, controller, prompts } = fixture({
			firstResponseTimeoutMs: 1_000,
		});
		child.abort = async () => await new Promise<void>(() => undefined);
		(controller as any).receive("first", "auto");
		await tick();
		(controller as any).stopVoice();
		await tick();
		(controller as any).receive("second", "auto");
		await tick();
		expect(prompts).toHaveLength(2);
		controller.close();
	});

	it("replaces a non-settling child before another prompt and ignores its late events", async () => {
		const { child, children, controller, emit, prompts, settle } = fixture({
			firstResponseTimeoutMs: 1_000,
		});
		const events: VoiceControllerEvent[] = [];
		controller.subscribe((event) => events.push(event));
		child.abort = async () => await new Promise<void>(() => undefined);
		(controller as any).receive("first", "auto");
		await tick();
		(controller as any).receive("second", "auto");
		await tick();
		expect(children).toHaveLength(2);
		expect(prompts).toHaveLength(2);
		const replacement = children[1];
		emit(replacement, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: {
				type: "text_delta",
				delta: '{"heard":"second","reply":"fresh","hotwords":[]}',
			},
		});
		settle(replacement);
		await tick();
		expect((controller as any).transcript.at(-1)?.text).toBe("fresh");
		expect((controller as any).status).toBe("ready");
		expect((controller as any).error).toBeNull();
		const count = events.length;
		emit(child, {
			type: "message_update",
			message: { role: "assistant" },
			assistantMessageEvent: {
				type: "text_delta",
				delta: '{"heard":"first","reply":"stale","hotwords":[]}',
			},
		});
		emit(child, { type: "agent_settled" });
		expect((controller as any).transcript.at(-1)?.text).toBe("fresh");
		expect(events).toHaveLength(count);
		controller.close();
	});

	it("ignores late deltas after timeout without replacing fallback", async () => {
		const { controller, listeners } = fixture({ firstResponseTimeoutMs: 5 });
		const events: any[] = [];
		controller.subscribe((event) => events.push(event));
		(controller as any).receive("hello", "auto");
		await new Promise((resolve) => setTimeout(resolve, 20));
		for (const listener of listeners) {
			listener({
				type: "message_update",
				message: { role: "assistant" },
				assistantMessageEvent: {
					type: "text_delta",
					delta: '{"heard":"hello","reply":"late","hotwords":[]}',
				},
			});
			listener({
				type: "tool_execution_end",
				toolCallId: "tool-late",
				toolName: "voice_executor_dispatch",
				result: {},
				isError: false,
			});
		}
		expect((controller as any).transcript.at(-1)?.text).toBe(
			"I couldn't finish my response.",
		);
		expect(
			events.filter((event) => event.type === "assistant-speech"),
		).toHaveLength(1);
		controller.close();
	});

	it("requeues executor results interrupted after prompt launch", async () => {
		const { controller, prompts } = fixture();
		(controller as any).callActive = true;
		(controller as any).boundaryReady = true;
		(controller as any).enqueueExecutorResult("result-1", "completed", "done");
		await tick();
		(controller as any).receive("what happened?", "auto");
		await tick();
		expect(prompts).toHaveLength(2);
		expect(prompts[1]).toContain('<work-result id="result-1"');
		controller.close();
	});

	it("does not launch result notification while speech begins during child setup", async () => {
		const { child, controller, prompts } = fixture();
		let resolveChild: ((value: typeof child) => void) | undefined;
		(controller as any).ensureChild = () =>
			new Promise<typeof child>((resolve) => {
				resolveChild = resolve;
			});
		(controller as any).callActive = true;
		(controller as any).boundaryReady = true;
		(controller as any).enqueueExecutorResult("result-1", "completed", "done");
		await tick();
		controller.onHumanSpeech(true);
		resolveChild?.(child);
		await tick();
		expect(prompts).toEqual([]);
		expect((controller as any).pendingResults).toHaveLength(1);
		controller.close();
	});

	it("speaks fallback when model initialization fails before a turn exists", async () => {
		const controller = new VoiceController({} as never);
		const events: any[] = [];
		controller.subscribe((event) => events.push(event));
		controller.start({
			isIdle: () => true,
			modelRegistry: { getAvailable: () => [] },
		} as never);
		(controller as any).receive("hello", "auto");
		await tick();
		await tick();
		expect(events).toContainEqual(
			expect.objectContaining({
				type: "assistant-speech",
				text: "I couldn't finish my response.",
			}),
		);
		controller.close();
	});

	it("aborts stale output then coalesces newer speech without steering", async () => {
		const { controller, prompts } = fixture();
		(controller as any).receive("first", "auto");
		await tick();
		(controller as any).receive("second", "auto");
		await tick();
		expect(prompts).toHaveLength(2);
		expect(prompts[1]).toContain("second");
		expect((controller as any).transcript.at(-1)).toMatchObject({
			role: "assistant",
			text: "",
		});
	});

	it("streams heard-first replies and flushes final speech before completion", async () => {
		const { controller, listeners, settle } = fixture();
		const events: any[] = [];
		controller.subscribe((event) => events.push(event));
		(controller as any).receive("recognized words", "auto");
		await tick();
		const message = { role: "assistant" };
		for (const listener of listeners)
			listener({ type: "message_start", message });
		for (const delta of [
			'{"heard":"corrected words","reply":"First sentence.',
			" Second sentence",
			'.","hotwords":["ONNX","Nushell"]}',
		])
			for (const listener of listeners)
				listener({
					type: "message_update",
					message,
					assistantMessageEvent: { type: "text_delta", delta },
				});
		expect(events.filter((event) => event.type === "heard")).toEqual([
			expect.objectContaining({
				type: "heard",
				text: "corrected words",
			}),
		]);
		expect(
			controller
				.nativeSnapshot()
				.transcript.filter((entry) => entry.role === "user"),
		).toEqual([expect.objectContaining({ text: "corrected words" })]);
		expect(events.findIndex((event) => event.type === "heard")).toBeLessThan(
			events.findIndex((event) => event.type === "assistant-speech"),
		);
		expect(events.some((event) => event.type === "hotwords")).toBe(false);
		expect(
			events
				.filter((event) => event.type === "assistant-speech")
				.map((event) => event.text),
		).toEqual(["First sentence.", "Second sentence."]);
		expect(events.some((event) => event.type === "assistant-complete")).toBe(
			false,
		);
		settle?.();
		await tick();
		const speechIndices = events.flatMap((event, index) =>
			event.type === "assistant-speech" ? [index] : [],
		);
		const completeIndex = events.findIndex(
			(event) => event.type === "assistant-complete",
		);
		expect(completeIndex).toBeGreaterThan(Math.max(...speechIndices));
		expect(events.filter((event) => event.type === "hotwords")).toEqual([
			expect.objectContaining({
				type: "hotwords",
				terms: ["ONNX", "Nushell"],
			}),
		]);
		expect(
			events.filter((event) => event.type === "assistant-speech"),
		).toHaveLength(2);
		controller.close();
	});

	it("preserves accepted assistant replies through the structured-output limit", async () => {
		const { controller, listeners, settle } = fixture();
		const reply = "x".repeat(3_000);
		(controller as any).receive("hello", "auto");
		await tick();
		for (const listener of listeners)
			listener({
				type: "message_update",
				message: { role: "assistant" },
				assistantMessageEvent: {
					type: "text_delta",
					delta: JSON.stringify({ heard: "hello", reply, hotwords: [] }),
				},
			});
		settle();
		await tick();
		expect((controller as any).transcript.at(-1)?.text).toBe(reply);
		controller.close();
	});

	it("streams cloned Pi message events into the active transcript", async () => {
		const { controller, listeners } = fixture();
		(controller as any).receive("hello", "auto");
		await tick();
		for (const delta of [
			'{"heard":"hello","reply":"Hello',
			' there","hotwords":[]}',
		]) {
			for (const listener of listeners)
				listener({
					type: "message_update",
					message: { role: "assistant" },
					assistantMessageEvent: { type: "text_delta", delta },
				});
		}
		expect((controller as any).transcript.at(-1).text).toBe("Hello there");
		(controller as any).stopVoice();
		await tick();
	});

	it("stop clears queued speech and rejects late deltas", async () => {
		const { controller, listeners, prompts } = fixture();
		(controller as any).receive("first", "auto");
		await tick();
		const stale = { role: "assistant" };
		for (const listener of listeners)
			listener({ type: "message_start", message: stale });
		(controller as any).stopVoice();
		for (const listener of listeners)
			listener({
				type: "message_update",
				message: stale,
				assistantMessageEvent: { type: "text_delta", delta: "late" },
			});
		await tick();
		expect(prompts).toHaveLength(1);
		expect((controller as any).transcript.at(-1).text).toBe("");
		expect((controller as any).status).toBe("ready");
	});

	it("decodes structured streamed strings across escapes and unicode boundaries", () => {
		expect(
			__test.extractJsonString('{"heard":"Nushell \\u{1f}"', "heard"),
		).toEqual({ value: "Nushell ", complete: false });
		expect(
			__test.extractJsonString('{"heard":"Nushell \\u004e"}', "heard"),
		).toEqual({ value: "Nushell N", complete: true });
		expect(
			__test.extractJsonString('{"reply":"say \\"hi\\\\there"}', "reply"),
		).toEqual({ value: 'say "hi\\there', complete: true });
	});

	it("retains executor results after failed initialization without retrying until another boundary", async () => {
		const controller = new VoiceController({} as never);
		(controller as any).callActive = true;
		(controller as any).activeTurn = {
			assistantId: "assistant-1",
			ticket: 0,
			userId: "user-1",
			kind: "human",
			raw: "",
			workAccepted: false,
		};
		(controller as any).boundaryReady = false;
		for (let index = 0; index < 12; index++)
			(controller as any).enqueueExecutorResult(
				`id-${index}`,
				"completed",
				`result-${index}`,
			);
		expect((controller as any).pendingResults).toHaveLength(12);
		expect(controller.turnBoundary("other")).toBe(false);
		expect(controller.turnBoundary("assistant-1")).toBe(true);
		expect((controller as any).pendingResults).toHaveLength(12);
		await tick();
		expect((controller as any).draining).toBe(false);
		expect((controller as any).boundaryReady).toBe(false);
		expect((controller as any).pendingResults).toHaveLength(12);
		expect((controller as any).error).not.toBeNull();
		expect(controller.turnBoundary("assistant-1")).toBe(true);
		await tick();
		expect((controller as any).draining).toBe(false);
		expect((controller as any).pendingResults).toHaveLength(12);
		controller.close();
	});

	it("calls the host typed classifier and returns calibrated choice scores", async () => {
		let received: any;
		const model = { provider: "host", id: "opaque" };
		const classifier = makeTurnClassifier({
			modelRegistry: {
				getAvailableOfType: async (type: string) => {
					expect(type).toBe("classifier");
					return [model];
				},
				classify: async (
					_model: unknown,
					request: unknown,
					options: unknown,
				) => {
					received = { request, options };
					return {
						stopReason: "stop",
						answers: {
							completion: {
								type: "choice",
								choice: "complete",
								probabilities: {
									complete: 0.81,
									trailing: 0.12,
									"mid-clause": 0.07,
								},
								confidence: 0.81,
							},
						},
					};
				},
			},
		} as never);
		const signal = new AbortController().signal;
		const result = await classifier({
			text: "Which provider?",
			lastAgentTurn: "We were discussing Pi.",
			signal,
		});
		expect(received.request).toMatchObject({
			state: {
				text: "Which provider?",
				lastAgentTurn: "We were discussing Pi.",
			},
			questions: { completion: { type: "choice" } },
		});
		expect(received.options.signal).not.toBe(signal);
		expect(received.options.signal.aborted).toBe(false);
		expect(result).toMatchObject({
			available: true,
			choice: "complete",
			pComplete: 0.81,
			scores: { trailing: 0.12 },
		});
		expect(result).not.toHaveProperty("model");
	});
});