repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ultra/dynamic-catalog.ts

Raw
File outline
import { spawn } from "node:child_process";
import { createHash, randomUUID } from "node:crypto";
import { existsSync } from "node:fs";
import {
	access,
	copyFile,
	mkdir,
	open,
	readdir,
	readFile,
	rename,
	rm,
	stat,
	writeFile,
} from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, join, posix, relative, resolve, sep, win32 } from "node:path";
import { fileURLToPath } from "node:url";
import type { ToolDefinition } from "@earendil-works/pi-coding-agent";
import { Type } from "typebox";
import { artifactFiles, hashArtifacts } from "./dynamic-artifacts.ts";
import {
	type DynamicManifest,
	VALIDATOR_CONTRACT_VERSION,
	VALIDATOR_WATCHDOG_MS,
	type ValidationAttestation,
	type ValidationDiagnostic,
	type WorkerValidationResult,
} from "./dynamic-contract.ts";
import { DYNAMIC_DEBUG_TEMPLATE } from "./dynamic-debug-template.ts";
import type {
	DynamicExtensionResolver,
	ResolvedDynamicExtension,
	UsedDynamicExtension,
} from "./dynamic-types.ts";
import { DYNAMIC_EXTENSION_TOOL_NAMES } from "./spec.ts";

const RETENTION_MS = 7 * 24 * 60 * 60 * 1_000;
const MAX_TOOL_OUTPUT = 12_000;
const NAME_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+){1,3}$/u;

export interface DynamicCatalogPaths {
	catalog: string;
	cache: string;
	runs: string;
	locks: string;
	logs: string;
}

export function dynamicCatalogPaths(options: {
	agentDir: string;
	platform?: NodeJS.Platform;
	env?: NodeJS.ProcessEnv;
	home?: string;
}): DynamicCatalogPaths {
	const platform = options.platform ?? process.platform;
	const env = options.env ?? process.env;
	const home = options.home ?? homedir();
	const platformPath = platform === "win32" ? win32 : posix;
	let platformCache: string;
	if (platform === "win32")
		platformCache =
			env.LOCALAPPDATA ?? platformPath.join(home, "AppData", "Local");
	else if (platform === "darwin")
		platformCache = platformPath.join(home, "Library", "Caches");
	else platformCache = env.XDG_CACHE_HOME ?? platformPath.join(home, ".cache");
	const cache = platformPath.join(
		platformCache,
		"pi",
		"ultra",
		"dynamic-extensions",
	);
	return {
		catalog: platformPath.join(
			options.agentDir,
			"ultra",
			"dynamic-extensions",
			"catalog",
		),
		cache,
		runs: platformPath.join(cache, "runs"),
		locks: platformPath.join(cache, "locks"),
		logs: platformPath.join(cache, "logs"),
	};
}

interface CurrentRevision {
	schemaVersion: 1;
	name: string;
	revision: string;
	description: string;
	topic: string;
	publishedAt: string;
}

interface SearchResult {
	name: string;
	currentHash: string;
	validationStatus: "validated" | "invalid" | "outdated";
	description: string;
	topic: string;
	tags: string[];
	testedPlatforms: string[];
	sourcePath: string;
}

interface BuilderState {
	query: string;
	candidates: string[];
	ownedName?: string;
}

interface DraftMetadata {
	name: string;
	baseHash: string | null;
	createdAt: string;
}

export interface DynamicCoordinatorOptions {
	agentDir: string;
	parentSessionId: string;
	workflowRunId: string;
	availableTools: string[];
	paths?: DynamicCatalogPaths;
	workerPath?: string;
}

export function createDynamicExtensionCoordinator(
	options: DynamicCoordinatorOptions,
): DynamicExtensionResolver {
	return new DynamicCoordinator(options);
}

class DynamicCoordinator implements DynamicExtensionResolver {
	readonly paths: DynamicCatalogPaths;
	readonly runRoot: string;
	readonly workerPath: string;
	private initialized?: Promise<void>;

	constructor(private readonly options: DynamicCoordinatorOptions) {
		this.paths = options.paths ?? dynamicCatalogPaths(options);
		this.runRoot = join(
			this.paths.runs,
			safeSegment(options.parentSessionId),
			safeSegment(options.workflowRunId),
		);
		this.workerPath =
			options.workerPath ??
			fileURLToPath(new URL("./dynamic-validator-worker.ts", import.meta.url));
	}

	private init(): Promise<void> {
		if (!this.initialized) {
			this.initialized = (async () => {
				await Promise.all([
					mkdir(this.paths.catalog, { recursive: true }),
					mkdir(this.paths.runs, { recursive: true }),
					mkdir(this.paths.locks, { recursive: true }),
					mkdir(this.paths.logs, { recursive: true }),
				]);
				await Promise.all([
					pruneOlderThan(this.paths.runs, RETENTION_MS, this.runRoot),
					pruneOlderThan(this.paths.logs, RETENTION_MS),
				]);
			})();
		}
		return this.initialized;
	}

	async resolve(
		names: string[],
		args: { phase: string; agentId: string; signal?: AbortSignal },
	): Promise<ResolvedDynamicExtension[]> {
		await this.init();
		const unique = new Set<string>();
		const resolved: ResolvedDynamicExtension[] = [];
		for (const name of names) {
			assertName(name);
			if (unique.has(name))
				throw new Error(
					`ultra: dynamic extension "${name}" appears more than once in phase "${args.phase}".`,
				);
			unique.add(name);
			resolved.push(
				await withNameLock(this.paths, name, args.signal, async () => {
					const current = await readCurrent(this.paths, name);
					if (!current)
						throw new Error(
							`ultra: dynamic extension "${name}" has no canonical revision. Run a builder with all four dynamic-extension tools.`,
						);
					const sourcePath = join(this.paths.catalog, name, current.revision);
					const verified = await verifyCurrent(sourcePath, current);
					if (!verified.ok)
						throw new Error(
							`ultra: dynamic extension "${name}" is ${verified.status} at ${sourcePath}; call validate_dynamic_extension on that exact path.`,
						);
					const snapshot = join(
						this.runRoot,
						safeSegment(args.agentId),
						"snapshots",
						name,
						current.revision,
					);
					if (!existsSync(snapshot)) {
						await mkdir(dirname(snapshot), { recursive: true });
						await copyArtifacts(sourcePath, snapshot);
						const copiedHash = hashArtifacts(snapshot);
						if (copiedHash !== current.revision) {
							await rm(snapshot, { recursive: true, force: true });
							throw new Error(
								`ultra: snapshot hash mismatch for "${name}": expected ${current.revision}, got ${copiedHash}.`,
							);
						}
					}
					const manifest = await readManifest(sourcePath);
					return {
						name,
						description: manifest.description,
						revision: current.revision,
						selector: `${name}@${current.revision}`,
						canonicalPath: sourcePath,
						sourcePath: snapshot,
						loadPath: join(snapshot, "index.ts"),
						overrides: [...manifest.overrides],
					};
				}),
			);
		}
		return resolved;
	}

	async builder(
		agentId: string,
		signal?: AbortSignal,
	): Promise<{
		tools: ToolDefinition[];
		prompt: string;
		activeTools: string[];
	}> {
		await this.init();
		const workspace = join(this.runRoot, safeSegment(agentId));
		await mkdir(workspace, { recursive: true });
		const tools = makeBuilderTools({
			paths: this.paths,
			workspace,
			workerPath: this.workerPath,
			defaultSignal: signal,
		});
		const prompt = await readFile(
			new URL("./prompts/dynamic-extension-builder.md", import.meta.url),
			"utf8",
		);
		return {
			tools,
			prompt: prompt.trim(),
			activeTools: [
				...new Set([
					...this.options.availableTools.filter(
						(name) => name !== "run_workflow",
					),
					...DYNAMIC_EXTENSION_TOOL_NAMES,
				]),
			],
		};
	}

	loaded(
		extensions: ResolvedDynamicExtension[],
		sessionId: string,
	): UsedDynamicExtension[] {
		return extensions.map((extension) => ({
			name: extension.name,
			description: extension.description,
			selector: extension.selector,
			sourcePath: extension.sourcePath,
			debugLogPaths: [
				join(this.paths.logs, sessionId, `${extension.name}.jsonl`),
			],
		}));
	}

	async complete(): Promise<void> {
		for (const agent of await readdir(this.runRoot, {
			withFileTypes: true,
		}).catch(() => [])) {
			if (!agent.isDirectory()) continue;
			const agentRoot = join(this.runRoot, agent.name);
			await Promise.all([
				rm(join(agentRoot, "drafts"), { recursive: true, force: true }),
				rm(join(agentRoot, "builder-state.json"), { force: true }),
			]);
			if ((await readdir(agentRoot).catch(() => [])).length === 0)
				await rm(agentRoot, { recursive: true, force: true });
		}
		if ((await readdir(this.runRoot).catch(() => [])).length === 0)
			await rm(this.runRoot, { recursive: true, force: true });
	}
}

function safeSegment(value: string): string {
	return value.replace(/[^A-Za-z0-9._-]/gu, "_");
}

function assertName(name: string): void {
	if (!NAME_PATTERN.test(name))
		throw new Error(
			`ultra: invalid dynamic extension name "${name}"; use two-to-four-term kebab-case.`,
		);
}

async function pruneOlderThan(
	root: string,
	ageMs: number,
	preserve?: string,
): Promise<void> {
	const threshold = Date.now() - ageMs;
	for (const entry of await readdir(root, { withFileTypes: true }).catch(
		() => [],
	)) {
		const path = join(root, entry.name);
		if (
			!entry.isDirectory() ||
			(preserve && resolve(path) === resolve(preserve))
		)
			continue;
		const value = await stat(path).catch(() => undefined);
		if (value && value.mtimeMs < threshold)
			await rm(path, { recursive: true, force: true });
	}
}

function lockRoot(paths: DynamicCatalogPaths): string {
	const catalogPath = resolve(paths.catalog).replaceAll("\\", "/");
	const key =
		process.platform === "win32" ? catalogPath.toLowerCase() : catalogPath;
	const hash = createHash("sha256").update(key).digest("hex");
	return join(paths.locks, hash);
}

async function withNameLock<T>(
	paths: DynamicCatalogPaths,
	name: string,
	signal: AbortSignal | undefined,
	work: () => Promise<T>,
): Promise<T> {
	const lock = join(lockRoot(paths), `${name}.lock`);
	await mkdir(dirname(lock), { recursive: true });
	const started = Date.now();
	for (;;) {
		if (signal?.aborted) throw abortError();
		try {
			await mkdir(lock);
			await writeFile(
				join(lock, "owner.json"),
				JSON.stringify({
					pid: process.pid,
					acquiredAt: new Date().toISOString(),
				}),
			);
			break;
		} catch (error) {
			if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
			const value = await stat(lock).catch(() => undefined);
			if (
				value &&
				Date.now() - value.mtimeMs > VALIDATOR_WATCHDOG_MS * 2 &&
				!(await lockOwnerActive(lock))
			) {
				await rm(lock, { recursive: true, force: true });
				continue;
			}
			if (Date.now() - started > VALIDATOR_WATCHDOG_MS)
				throw new Error(
					`ultra: timed out waiting for publication lock for ${name}.`,
				);
			await abortableDelay(40, signal);
		}
	}
	try {
		return await work();
	} finally {
		await rm(lock, { recursive: true, force: true });
	}
}

async function lockOwnerActive(lock: string): Promise<boolean> {
	try {
		const owner = JSON.parse(
			await readFile(join(lock, "owner.json"), "utf8"),
		) as { pid?: unknown };
		if (typeof owner.pid !== "number") return false;
		try {
			process.kill(owner.pid, 0);
			return true;
		} catch (error) {
			return (error as NodeJS.ErrnoException).code === "EPERM";
		}
	} catch {
		return false;
	}
}

function abortError(): Error {
	const error = new Error("aborted");
	error.name = "AbortError";
	return error;
}

function abortableDelay(ms: number, signal?: AbortSignal): Promise<void> {
	if (signal?.aborted) return Promise.reject(abortError());
	return new Promise((resolveDelay, reject) => {
		const timer = setTimeout(resolveDelay, ms);
		const aborted = () => {
			clearTimeout(timer);
			reject(abortError());
		};
		signal?.addEventListener("abort", aborted, { once: true });
		if (signal)
			setTimeout(() => signal.removeEventListener("abort", aborted), ms + 1);
	});
}

async function readCurrent(
	paths: DynamicCatalogPaths,
	name: string,
): Promise<CurrentRevision | undefined> {
	try {
		return JSON.parse(
			await readFile(join(paths.catalog, name, "current.json"), "utf8"),
		) as CurrentRevision;
	} catch {
		return undefined;
	}
}

async function readManifest(path: string): Promise<DynamicManifest> {
	return JSON.parse(await readFile(join(path, "manifest.json"), "utf8"));
}

async function verifyCurrent(
	sourcePath: string,
	current: CurrentRevision,
): Promise<{ ok: boolean; status: "validated" | "invalid" | "outdated" }> {
	let attestation: ValidationAttestation;
	try {
		attestation = JSON.parse(
			await readFile(join(sourcePath, ".ultra-validation.json"), "utf8"),
		);
	} catch {
		return { ok: false, status: "invalid" };
	}
	if (attestation.validatorContractVersion !== VALIDATOR_CONTRACT_VERSION)
		return { ok: false, status: "outdated" };
	try {
		const hash = hashArtifacts(sourcePath);
		return {
			ok:
				hash === current.revision &&
				attestation.sourceHash === current.revision,
			status:
				hash === current.revision && attestation.sourceHash === current.revision
					? "validated"
					: "invalid",
		};
	} catch {
		return { ok: false, status: "invalid" };
	}
}

async function copyArtifacts(
	source: string,
	destination: string,
): Promise<void> {
	for (const file of artifactFiles(source)) {
		const sourceFile = resolve(file);
		const relativePath = relative(source, sourceFile);
		if (relativePath === ".ultra-validation.json") continue;
		const target = join(destination, relativePath);
		await mkdir(dirname(target), { recursive: true });
		await copyFile(sourceFile, target);
	}
}

async function atomicWriteJson(path: string, value: unknown): Promise<void> {
	await mkdir(dirname(path), { recursive: true });
	const temporary = `${path}.${process.pid}.${randomUUID()}.tmp`;
	await writeFile(temporary, `${JSON.stringify(value, null, 2)}\n`, "utf8");
	const handle = await open(temporary, "r+");
	try {
		await handle.sync();
	} finally {
		await handle.close();
	}
	await rename(temporary, path);
}

interface BuilderToolOptions {
	paths: DynamicCatalogPaths;
	workspace: string;
	workerPath: string;
	defaultSignal?: AbortSignal;
}

function makeBuilderTools(options: BuilderToolOptions): ToolDefinition[] {
	const statePath = join(options.workspace, "builder-state.json");
	const readState = async (): Promise<BuilderState | undefined> => {
		try {
			return JSON.parse(await readFile(statePath, "utf8"));
		} catch {
			return undefined;
		}
	};
	const assertOwnership = async (
		name: string,
	): Promise<BuilderState | undefined> => {
		const state = await readState();
		if (state?.ownedName && state.ownedName !== name)
			throw new Error(
				`this builder owns dynamic extension "${state.ownedName}" and cannot modify "${name}".`,
			);
		return state;
	};
	const result = (details: unknown) => ({
		content: [
			{
				type: "text" as const,
				text: bounded(JSON.stringify(details, null, 2)),
			},
		],
		details,
	});
	return [
		{
			name: "search_dynamic_extensions",
			label: "Search dynamic extensions",
			description:
				"Search the validated shared dynamic-extension catalog before creating or improving one.",
			parameters: Type.Object(
				{ query: Type.String({ minLength: 1 }) },
				{ additionalProperties: false },
			),
			async execute(_id, params) {
				const input = params as { query: string };
				const matches = await searchCatalog(options.paths, input.query);
				const previous = await readState();
				await atomicWriteJson(statePath, {
					query: input.query,
					candidates: matches.map((match) => match.name),
					...(previous?.ownedName ? { ownedName: previous.ownedName } : {}),
				} satisfies BuilderState);
				return result({ matches });
			},
		},
		{
			name: "create_dynamic_extension",
			label: "Create dynamic extension",
			description:
				"Scaffold one new topic after search and explicit rejection of every candidate.",
			parameters: Type.Object(
				{
					name: Type.String({ minLength: 1 }),
					description: Type.String({ minLength: 1 }),
					topic: Type.String({ minLength: 1 }),
					scope: Type.String({ minLength: 1 }),
					nonGoals: Type.Array(Type.String({ minLength: 1 }), {
						minItems: 1,
					}),
					tags: Type.Array(Type.String({ minLength: 1 }), { minItems: 1 }),
					consideredCandidates: Type.Array(
						Type.Object(
							{
								name: Type.String({ minLength: 1 }),
								rationale: Type.String({ minLength: 1 }),
							},
							{ additionalProperties: false },
						),
					),
					noMatchRationale: Type.Optional(Type.String({ minLength: 1 })),
				},
				{ additionalProperties: false },
			),
			async execute(_id, params) {
				const input = params as CreateParams;
				assertName(input.name);
				const searched = await assertOwnership(input.name);
				if (!searched)
					throw new Error(
						"create_dynamic_extension requires search_dynamic_extensions first in this builder session.",
					);
				const rejected = new Set(
					input.consideredCandidates
						.filter((candidate) => candidate.rationale.trim().length > 0)
						.map((candidate) => candidate.name),
				);
				const missing = searched.candidates.filter(
					(name) => !rejected.has(name),
				);
				if (missing.length > 0)
					throw new Error(
						`create_dynamic_extension requires rejection rationale for: ${missing.join(", ")}.`,
					);
				if (searched.candidates.length === 0 && !input.noMatchRationale?.trim())
					throw new Error(
						"create_dynamic_extension requires noMatchRationale when search returned no candidates.",
					);
				if (await readCurrent(options.paths, input.name))
					throw new Error(
						`dynamic extension "${input.name}" already exists; checkout and improve it.`,
					);
				const draft = join(options.workspace, "drafts", input.name, "source");
				await rm(dirname(draft), { recursive: true, force: true });
				await scaffoldDraft(draft, input, searched.query);
				await atomicWriteJson(join(dirname(draft), "draft.json"), {
					name: input.name,
					baseHash: null,
					createdAt: new Date().toISOString(),
				} satisfies DraftMetadata);
				await atomicWriteJson(statePath, {
					...searched,
					ownedName: input.name,
				} satisfies BuilderState);
				return result({ name: input.name, draftPath: draft });
			},
		},
		{
			name: "checkout_dynamic_extension",
			label: "Checkout dynamic extension",
			description:
				"Copy one canonical validated extension into an isolated editable draft.",
			parameters: Type.Object(
				{ name: Type.String({ minLength: 1 }) },
				{ additionalProperties: false },
			),
			async execute(_id, params, signal) {
				const input = params as { name: string };
				assertName(input.name);
				const state = await assertOwnership(input.name);
				const effectiveSignal = signal ?? options.defaultSignal;
				const checkout = await withNameLock(
					options.paths,
					input.name,
					effectiveSignal,
					async () => {
						const current = await readCurrent(options.paths, input.name);
						if (!current)
							throw new Error(`unknown dynamic extension: ${input.name}`);
						const source = join(
							options.paths.catalog,
							input.name,
							current.revision,
						);
						const verified = await verifyCurrent(source, current);
						if (!verified.ok)
							throw new Error(
								`canonical source is ${verified.status} at ${source}; validate it before checkout.`,
							);
						const draft = join(
							options.workspace,
							"drafts",
							input.name,
							"source",
						);
						await rm(dirname(draft), { recursive: true, force: true });
						await copyArtifacts(source, draft);
						await atomicWriteJson(join(dirname(draft), "draft.json"), {
							name: input.name,
							baseHash: current.revision,
							createdAt: new Date().toISOString(),
						} satisfies DraftMetadata);
						return { current, draft };
					},
				);
				await atomicWriteJson(statePath, {
					query: state?.query ?? input.name,
					candidates: state?.candidates ?? [input.name],
					ownedName: input.name,
				} satisfies BuilderState);
				return result({
					name: input.name,
					baseHash: checkout.current.revision,
					draftPath: checkout.draft,
				});
			},
		},
		{
			name: "validate_dynamic_extension",
			label: "Validate dynamic extension",
			description:
				"Run mandatory correctness gates and advisory performance checks, then publish atomically only when correctness succeeds.",
			parameters: Type.Object(
				{ path: Type.String({ minLength: 1 }) },
				{ additionalProperties: false },
			),
			async execute(_id, params, signal) {
				const input = params as { path: string };
				const manifest = await readManifest(resolve(input.path)).catch(
					() => undefined,
				);
				if (manifest) await assertOwnership(manifest.name);
				const validation = await validateAndPublish({
					paths: options.paths,
					draftPath: resolve(input.path),
					workerPath: options.workerPath,
					signal: signal ?? options.defaultSignal,
				});
				if (manifest) {
					const state = await readState();
					await atomicWriteJson(statePath, {
						query: state?.query ?? manifest.topic,
						candidates: state?.candidates ?? [manifest.name],
						ownedName: manifest.name,
					} satisfies BuilderState);
				}
				return result(validation);
			},
		},
	] as ToolDefinition[];
}

function bounded(value: string): string {
	return value.length <= MAX_TOOL_OUTPUT
		? value
		: `${value.slice(0, MAX_TOOL_OUTPUT)}\n[truncated]`;
}

function normalizeTokens(value: string): string[] {
	return value
		.replace(/([a-z0-9])([A-Z])/gu, "$1 $2")
		.toLowerCase()
		.split(/[^a-z0-9]+/gu)
		.filter(Boolean)
		.map((token) =>
			token.length > 4 && token.endsWith("ies")
				? `${token.slice(0, -3)}y`
				: token.length > 3 && token.endsWith("s")
					? token.slice(0, -1)
					: token,
		);
}

function readmeIndexText(readme: string): string {
	const wanted = new Set(["purpose", "scope", "usage"]);
	const lines: string[] = [];
	let include = false;
	let inCode = false;
	for (const line of readme.split(/\r?\n/u)) {
		if (line.startsWith("```")) {
			inCode = !inCode;
			continue;
		}
		if (inCode) continue;
		const heading = /^##\s+(.+)$/u.exec(line);
		if (heading) {
			include = wanted.has(heading[1].trim().toLowerCase());
			continue;
		}
		if (include) lines.push(line);
	}
	return lines.join(" ");
}

async function searchCatalog(
	paths: DynamicCatalogPaths,
	query: string,
): Promise<SearchResult[]> {
	await mkdir(paths.catalog, { recursive: true });
	const queryPhrase = query.toLowerCase().trim();
	const queryTokens = normalizeTokens(query);
	const ranked: Array<{ score: number; result: SearchResult }> = [];
	for (const entry of await readdir(paths.catalog, { withFileTypes: true })) {
		if (!entry.isDirectory()) continue;
		const current = await readCurrent(paths, entry.name);
		if (!current) continue;
		const sourcePath = join(paths.catalog, entry.name, current.revision);
		let manifest: DynamicManifest;
		let readme: string;
		try {
			[manifest, readme] = await Promise.all([
				readManifest(sourcePath),
				readFile(join(sourcePath, "README.md"), "utf8"),
			]);
		} catch {
			continue;
		}
		const verified = await verifyCurrent(sourcePath, current);
		const attestation = await readAttestation(sourcePath);
		const nameTopic = `${manifest.name} ${manifest.topic}`;
		const tags = manifest.tags.join(" ");
		const scope = manifest.scope;
		const prose = readmeIndexText(readme);
		const fields = [nameTopic, tags, scope, prose];
		let score = fields.some((field) =>
			field.toLowerCase().includes(queryPhrase),
		)
			? 10_000
			: 0;
		const weights = [100, 40, 15, 4];
		for (const [index, field] of fields.entries()) {
			const tokens = new Set(normalizeTokens(field));
			for (const token of queryTokens)
				if (tokens.has(token)) score += weights[index];
		}
		if (score === 0) continue;
		ranked.push({
			score,
			result: {
				name: manifest.name,
				currentHash: current.revision,
				validationStatus: verified.status,
				description: manifest.description,
				topic: manifest.topic,
				tags: manifest.tags,
				testedPlatforms:
					attestation?.testedPlatforms ??
					(attestation ? [attestation.platform] : []),
				sourcePath,
			},
		});
	}
	return ranked
		.sort(
			(left, right) =>
				right.score - left.score ||
				left.result.name.localeCompare(right.result.name),
		)
		.slice(0, 5)
		.map(({ result }) => result);
}

async function readAttestation(
	sourcePath: string,
): Promise<ValidationAttestation | undefined> {
	try {
		return JSON.parse(
			await readFile(join(sourcePath, ".ultra-validation.json"), "utf8"),
		);
	} catch {
		return undefined;
	}
}

interface CreateParams {
	name: string;
	description: string;
	topic: string;
	scope: string;
	nonGoals: string[];
	tags: string[];
	consideredCandidates: Array<{ name: string; rationale: string }>;
	noMatchRationale?: string;
}

async function scaffoldDraft(
	draft: string,
	params: CreateParams,
	searchQuery: string,
): Promise<void> {
	await mkdir(draft, { recursive: true });
	const capability = `${params.name}-core`;
	const toolName = params.name.replaceAll("-", "_");
	const manifest: DynamicManifest = {
		schemaVersion: 1,
		name: params.name,
		description: params.description,
		topic: params.topic,
		scope: params.scope,
		nonGoals: params.nonGoals,
		tags: [...new Set(params.tags.map((tag) => tag.toLowerCase()))],
		capabilities: [capability],
		requiredExecutables: [],
		optionalExecutables: [],
		overrides: [],
		benchmarkBudgets: { [process.platform]: { hotPathMs: 10 } },
	};
	const alternatives =
		params.consideredCandidates.length > 0
			? params.consideredCandidates
					.map((candidate) => `- **${candidate.name}**: ${candidate.rationale}`)
					.join("\n")
			: `- Search \`${searchQuery}\` returned no suitable candidate: ${params.noMatchRationale}`;
	const files: Record<string, string> = {
		"manifest.json": `${JSON.stringify(manifest, null, 2)}\n`,
		"package.json": `${JSON.stringify(
			{
				name: `@ultra-dynamic/${params.name}`,
				private: true,
				type: "module",
				main: "index.ts",
				peerDependencies: {
					"@earendil-works/pi-coding-agent": "*",
				},
			},
			null,
			2,
		)}\n`,
		"README.md": `# ${params.name}\n\n## Purpose\n\n${params.description}\n\n## Scope\n\n${params.scope}\n\n## Non-goals\n\n${params.nonGoals.map((item) => `- ${item}`).join("\n")}\n\n## Alternatives considered\n\n${alternatives}\n\n## Decision rationale\n\nThis extension owns the single topic **${params.topic}** because encoding it once is cheaper and more deterministic than repeating the reasoning in consumers.\n\n## Usage\n\nLoad the extension through an Ultra step's \`dynamicExtensions\` field, then call \`${toolName}\`.\n\n## Architecture\n\nThe thin entry registers one lazy tool and activates it during \`session_start\`.\nCore behavior remains directly testable without a Pi session.\n\n## Test matrix\n\n| Capability | Test |\n| --- | --- |\n| \`${capability}\` | \`index.test.ts\` |\n\n## Promotion notes\n\nKeep this package private until a human deliberately promotes it.\n`,
		"core.ts": `export function executeCapability(value: string): { value: string } {\n\treturn { value };\n}\n`,
		"index.ts": `import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";\nimport { executeCapability } from "./core.ts";\nimport { dynamicExtensionRevision, logDynamicExtensionEvent } from "./debug.ts";\n\nexport default function dynamicExtension(pi: ExtensionAPI) {\n\tpi.registerTool({\n\t\tname: "${toolName}",\n\t\tlabel: "${params.name}",\n\t\tdescription: ${JSON.stringify(params.description)},\n\t\tparameters: {\n\t\t\ttype: "object",\n\t\t\tproperties: { value: { type: "string" } },\n\t\t\trequired: ["value"],\n\t\t\tadditionalProperties: false,\n\t\t},\n\t\tasync execute(_id, params, _signal, _onUpdate, ctx) {\n\t\t\tconst result = executeCapability(params.value);\n\t\t\tawait logDynamicExtensionEvent({\n\t\t\t\tname: "${params.name}",\n\t\t\t\trevision: dynamicExtensionRevision(import.meta.url),\n\t\t\t\tctx,\n\t\t\t\tevent: "capability_complete",\n\t\t\t\tcontext: { inputLength: params.value.length },\n\t\t\t});\n\t\t\treturn { content: [{ type: "text", text: JSON.stringify(result) }], details: result };\n\t\t},\n\t});\n\tpi.on("session_start", () => {\n\t\tconst active = pi.getActiveTools();\n\t\tif (!active.includes("${toolName}")) pi.setActiveTools([...active, "${toolName}"]);\n\t});\n}\n`,
		"index.test.ts": `import assert from "node:assert/strict";\nimport test from "node:test";\nimport { executeCapability } from "./core.ts";\n\ntest("${capability}: returns deterministic structured output", () => {\n\tassert.deepEqual(executeCapability("value"), { value: "value" });\n});\n`,
		"benchmark.ts": `import { performance } from "node:perf_hooks";\nimport { executeCapability } from "./core.ts";\n\nconst started = performance.now();\nfor (let index = 0; index < 1_000; index++) executeCapability(String(index));\nconst hotPathMs = performance.now() - started;\nconsole.log(JSON.stringify({ hotPathMs }));\n`,
		"debug.ts": DYNAMIC_DEBUG_TEMPLATE,
	};
	await Promise.all(
		Object.entries(files).map(([name, content]) =>
			writeFile(join(draft, name), content, "utf8"),
		),
	);
}

async function readDraftMetadata(
	draftPath: string,
): Promise<DraftMetadata | undefined> {
	try {
		return JSON.parse(
			await readFile(join(dirname(draftPath), "draft.json"), "utf8"),
		);
	} catch {
		return undefined;
	}
}

async function runValidator(args: {
	workerPath: string;
	draftPath: string;
	signal?: AbortSignal;
}): Promise<WorkerValidationResult & { aborted?: boolean }> {
	if (args.signal?.aborted)
		return abortedValidation(
			args.draftPath,
			"validation aborted before launch",
		);
	return new Promise((resolveResult) => {
		const resultPath = join(
			dirname(args.draftPath),
			`.validation-result-${randomUUID()}.json`,
		);
		const child = spawn(process.execPath, [args.workerPath, args.draftPath], {
			cwd: args.draftPath,
			env: {
				...process.env,
				CI: "true",
				ULTRA_VALIDATOR_TEMP: dirname(args.draftPath),
				ULTRA_VALIDATOR_RESULT_PATH: resultPath,
			},
			detached: process.platform !== "win32",
			stdio: ["ignore", "pipe", "pipe"],
		});
		let output = "";
		const append = (chunk: Buffer): void => {
			output = `${output}${chunk.toString("utf8")}`.slice(-64_000);
		};
		child.stdout.on("data", append);
		child.stderr.on("data", append);
		let finished = false;
		let stopping = false;
		const finish = (value: WorkerValidationResult & { aborted?: boolean }) => {
			if (finished) return;
			finished = true;
			clearTimeout(timer);
			args.signal?.removeEventListener("abort", onAbort);
			void rm(resultPath, { force: true });
			resolveResult(value);
		};
		const stop = async (reason: "aborted" | "timeout") => {
			if (finished || stopping) return;
			stopping = true;
			await killProcessTree(child.pid);
			finish(
				reason === "aborted"
					? abortedValidation(args.draftPath, "validation aborted")
					: failedValidation(args.draftPath, {
							gate: "validator",
							code: "validator_timeout",
							status: "error",
							limitMs: VALIDATOR_WATCHDOG_MS,
							output: bounded(output),
							remediation:
								"Fix hanging tests, benchmarks, imports, or leaked handles.",
							draftPath: args.draftPath,
						}),
			);
		};
		const timer = setTimeout(() => void stop("timeout"), VALIDATOR_WATCHDOG_MS);
		const onAbort = () => void stop("aborted");
		args.signal?.addEventListener("abort", onAbort, { once: true });
		child.on("error", (error) =>
			finish(
				failedValidation(args.draftPath, {
					gate: "validator",
					code: "validator_spawn",
					status: "error",
					actual: error.message,
					remediation:
						"Ensure the current Node executable can launch the bundled validator.",
					draftPath: args.draftPath,
				}),
			),
		);
		child.on("close", async () => {
			if (stopping) return;
			try {
				finish(JSON.parse(await readFile(resultPath, "utf8")));
				return;
			} catch {}
			const marker = "ULTRA_VALIDATION_RESULT=";
			const index = output.lastIndexOf(marker);
			if (index < 0) {
				finish(
					failedValidation(args.draftPath, {
						gate: "validator",
						code: "validator_protocol",
						status: "error",
						output: bounded(output),
						remediation:
							"Inspect validator output and repair the worker protocol failure.",
						draftPath: args.draftPath,
					}),
				);
				return;
			}
			try {
				const line = output.slice(index + marker.length).split(/\r?\n/u)[0];
				finish(JSON.parse(line));
			} catch (error) {
				finish(
					failedValidation(args.draftPath, {
						gate: "validator",
						code: "validator_protocol",
						status: "error",
						actual: error instanceof Error ? error.message : String(error),
						output: bounded(output),
						remediation: "Repair malformed validator result output.",
						draftPath: args.draftPath,
					}),
				);
			}
		});
	});
}

async function killProcessTree(pid: number | undefined): Promise<void> {
	if (!pid) return;
	if (process.platform === "win32") {
		await new Promise<void>((resolveKill) => {
			const killer = spawn("taskkill", ["/pid", String(pid), "/T", "/F"], {
				stdio: "ignore",
				windowsHide: true,
			});
			killer.once("error", () => resolveKill());
			killer.once("close", () => resolveKill());
		});
		return;
	}
	try {
		process.kill(-pid, "SIGKILL");
	} catch {
		try {
			process.kill(pid, "SIGKILL");
		} catch {}
	}
}

function failedValidation(
	_draftPath: string,
	diagnostic: ValidationDiagnostic,
): WorkerValidationResult {
	return { ok: false, diagnostics: [diagnostic], durationMs: 0 };
}

function abortedValidation(
	draftPath: string,
	message: string,
): WorkerValidationResult & { aborted: true } {
	return {
		ok: false,
		aborted: true,
		diagnostics: [
			{
				gate: "validator",
				code: "validator_aborted",
				status: "error",
				actual: message,
				remediation: "Resume the retained draft and validate again.",
				draftPath,
			},
		],
		durationMs: 0,
	};
}

function publicationFailure(
	validation: WorkerValidationResult,
	draftPath: string,
	code: string,
	expected: unknown,
	actual: unknown,
	remediation: string,
): Record<string, unknown> {
	return {
		status: "failed",
		diagnostics: [
			...validation.diagnostics,
			{
				gate: "publication",
				code,
				status: "error",
				expected,
				actual,
				path: join(draftPath, "manifest.json"),
				remediation,
				draftPath,
			},
		],
		retainedDraftPath: draftPath,
	};
}

async function validateAndPublish(args: {
	paths: DynamicCatalogPaths;
	draftPath: string;
	workerPath: string;
	signal?: AbortSignal;
}): Promise<Record<string, unknown>> {
	await access(args.draftPath);
	const validation = await runValidator(args);
	if (
		!validation.ok ||
		!validation.manifest ||
		!validation.hash ||
		!validation.attestation
	)
		return {
			status: validation.aborted ? "aborted" : "failed",
			diagnostics: validation.diagnostics,
			retainedDraftPath: args.draftPath,
		};
	const manifest = validation.manifest;
	const revision = validation.hash;
	let attestation = validation.attestation;
	assertName(manifest.name);
	const metadata = await readDraftMetadata(args.draftPath);
	const catalogRelative = relative(args.paths.catalog, args.draftPath);
	const catalogParts = catalogRelative.split(sep);
	const canonicalName =
		!catalogRelative.startsWith("..") && catalogParts.length === 2
			? catalogParts[0]
			: undefined;
	const identityName = metadata?.name ?? canonicalName;
	if (identityName && identityName !== manifest.name)
		return publicationFailure(
			validation,
			args.draftPath,
			"extension_identity_changed",
			identityName,
			manifest.name,
			"Restore the immutable extension name; a distinct topic requires a new extension.",
		);
	return withNameLock(args.paths, manifest.name, args.signal, async () => {
		const current = await readCurrent(args.paths, manifest.name);
		const currentSource = current
			? join(args.paths.catalog, manifest.name, current.revision)
			: undefined;
		const manualCurrent =
			currentSource && resolve(currentSource) === resolve(args.draftPath);
		const baseHash = manualCurrent ? current?.revision : metadata?.baseHash;
		if ((current?.revision ?? null) !== (baseHash ?? null)) {
			const diagnostic: ValidationDiagnostic = {
				gate: "publication",
				code: "stale_base",
				status: "error",
				expected: current?.revision ?? null,
				actual: baseHash ?? null,
				path: currentSource,
				remediation:
					"Checkout the current canonical revision, reapply the intended change, and validate again.",
				draftPath: args.draftPath,
			};
			return {
				status: "stale",
				name: manifest.name,
				baseHash: baseHash ?? null,
				currentHash: current?.revision ?? null,
				currentPath: currentSource,
				diagnostics: [...validation.diagnostics, diagnostic],
				retainedDraftPath: args.draftPath,
			};
		}
		if (currentSource && current) {
			const oldManifest = await readManifest(currentSource).catch(
				() => undefined,
			);
			if (current.topic !== manifest.topic)
				return publicationFailure(
					validation,
					args.draftPath,
					"extension_topic_changed",
					current.topic,
					manifest.topic,
					"Restore the immutable topic; create a new extension for a distinct responsibility.",
				);
			for (const [platform, oldBudget] of Object.entries(
				oldManifest?.benchmarkBudgets ?? {},
			)) {
				const newBudget = manifest.benchmarkBudgets[platform];
				if (!newBudget || newBudget.hotPathMs > oldBudget.hotPathMs)
					return publicationFailure(
						validation,
						args.draftPath,
						"benchmark_budget_loosened",
						{ platform, hotPathMs: oldBudget.hotPathMs },
						newBudget
							? { platform, hotPathMs: newBudget.hotPathMs }
							: { platform, hotPathMs: undefined },
						"Restore or tighten every existing platform budget.",
					);
			}
			const oldAttestation = await readAttestation(currentSource);
			attestation = {
				...attestation,
				testedPlatforms: [
					...new Set([
						...(oldAttestation?.testedPlatforms ??
							(oldAttestation ? [oldAttestation.platform] : [])),
						...attestation.testedPlatforms,
					]),
				],
			};
		}
		const nameRoot = join(args.paths.catalog, manifest.name);
		const target = join(nameRoot, revision);
		if (!existsSync(target)) {
			const staging = `${target}.tmp-${randomUUID()}`;
			await copyArtifacts(args.draftPath, staging);
			await writeFile(
				join(staging, ".ultra-validation.json"),
				`${JSON.stringify(attestation, null, 2)}\n`,
				"utf8",
			);
			await mkdir(nameRoot, { recursive: true });
			await rename(staging, target);
		} else {
			await atomicWriteJson(
				join(target, ".ultra-validation.json"),
				attestation,
			);
		}
		await atomicWriteJson(join(nameRoot, "current.json"), {
			schemaVersion: 1,
			name: manifest.name,
			revision,
			description: manifest.description,
			topic: manifest.topic,
			publishedAt: new Date().toISOString(),
		} satisfies CurrentRevision);
		return {
			status: "validated",
			name: manifest.name,
			revision,
			description: manifest.description,
			sourcePath: target,
			diagnostics: validation.diagnostics,
		};
	});
}