Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/loop/site.ts

Raw
import { randomBytes, timingSafeEqual } from "node:crypto";
import { readFile } from "node:fs/promises";
import { createServer, type ServerResponse } from "node:http";
import type { LoopController } from "./src/controller.ts";
import { gateLabel } from "./src/gates.ts";
import { createGoalRenderer } from "./src/goal-markdown.ts";
import type { AttemptRecord, EventRecord, RunRecord } from "./src/store.ts";

const OUTPUT_CHARS = 8_192;
const MAX_CLIENTS = 8;
const HISTORY = 50;
const FEED = 60;
const TIMELINE = 400;
const ASSETS = [
	["/", "index.html", "text/html"],
	["/app.css", "app.css", "text/css"],
	["/app.js", "app.js", "text/javascript"],
	["/nugu.css", "nugu.css", "text/css"],
] as const;

export type Site = { url: string; close(): Promise<void> };

function tail(text: string): string {
	return text.length > OUTPUT_CHARS
		? `…${text.slice(text.length - OUTPUT_CHARS)}`
		: text;
}

function payloadOf(event: EventRecord): Record<string, unknown> {
	return event.payload && typeof event.payload === "object"
		? (event.payload as Record<string, unknown>)
		: {};
}

function summarize(event: EventRecord): string | undefined {
	const p = payloadOf(event);
	switch (event.kind) {
		case "start": {
			const b = p["budgets"] as Record<string, number | null> | undefined;
			const cap = (v: number | null | undefined, unit: string) =>
				v == null ? "∞" : `${v}${unit}`;
			return `${p["gates"]} gate(s) · budgets ${cap(b?.["minutes"], "m")}/${cap(b?.["turns"], "")}/${cap(b?.["attempts"], "")}`;
		}
		case "gate_result":
			return p["failedIndex"] === null
				? `attempt ${p["attempt"]} passed every gate`
				: `attempt ${p["attempt"]} failed: ${p["gate"]} exit ${p["exitCode"] ?? "none"}${p["error"] ? ` (${p["error"]})` : ""}`;
		case "continue":
			return `loop prompt injected after turn ${p["turns"]}`;
		case "success":
			return `every gate exited 0 at attempt ${p["attempt"]}`;
		case "paused":
		case "stopped":
			return String(p["reason"] ?? "");
		case "resume":
			return `resumed with ${p["turns"]} turns and ${p["attempts"]} attempts used`;
		case "gate_error":
			return String(p["message"] ?? "");
		case "telemetry_gap":
			return `${p["dropped"]} telemetry record(s) dropped`;
		default:
			// Turn and admission records feed the timeline, not the event list.
			return undefined;
	}
}

function lastPass(run: RunRecord, attempts: AttemptRecord[]) {
	const rows = attempts.filter((row) => row.attempt === run.attempts);
	if (!rows.length) return null;
	const failed = rows.findIndex(
		(row) => row.exitCode !== 0 || row.error !== null,
	);
	return {
		attempt: run.attempts,
		failedIndex: failed < 0 ? null : failed,
		results: rows.map((row, index) => ({
			exitCode: row.exitCode,
			error: row.error,
			durationMs: row.endedAt - row.startedAt,
			endedAt: row.endedAt,
			truncated: row.truncated,
			// Only the failing gate's output is shown in the drawer.
			stdout: index === failed ? tail(row.stdout) : "",
			stderr: index === failed ? tail(row.stderr) : "",
		})),
	};
}

/** Observer snapshot of the live run, or the newest persisted run. */
function siteSnapshot(
	controller: LoopController,
	renderGoal: (goal: string) => string,
) {
	const live = controller.status();
	const history = controller.store.listRuns(HISTORY);
	const run: RunRecord | undefined = live ?? history[0];
	if (!run)
		return {
			observedAt: Date.now(),
			run: null,
			timeline: [],
			events: [],
			history: [],
		};
	const events = controller.store.events(run.id, 1_000);
	const attempts = controller.store.attempts(run.id, 64);
	const timeline = events
		.flatMap((event) => {
			const p = payloadOf(event);
			if (event.kind === "turn")
				return [{ kind: "turn", at: event.at, n: p["turns"] }];
			if (event.kind === "gate_result")
				return [
					{
						kind: p["failedIndex"] === null ? "pass" : "fail",
						at: event.at,
						n: p["attempt"],
						label: p["gate"] ?? null,
						exitCode: p["exitCode"] ?? null,
					},
				];
			return [];
		})
		.slice(-TIMELINE);
	return {
		observedAt: Date.now(),
		run: {
			id: run.id,
			goal: run.goal,
			goalHtml: renderGoal(run.goal),
			status: run.status,
			reason: run.reason,
			cwd: run.cwd,
			budgets: run.budgets,
			turns: run.turns,
			attempts: run.attempts,
			createdAt: run.createdAt,
			updatedAt: run.updatedAt,
			live: Boolean(live),
			pending: live?.pending ?? false,
			deadline: live?.deadline ?? null,
			runningGate: live?.runningGate ?? null,
			runningSince: live?.runningSince ?? null,
			liveOutput: live?.liveOutput ?? null,
			gates: run.gates.map((gate) => ({
				label: gateLabel(gate),
				command: [gate.command, ...(gate.args ?? [])].join(" ").slice(0, 300),
			})),
			lastPass: lastPass(run, attempts),
		},
		timeline,
		events: events
			.flatMap((event) => {
				const summary = summarize(event);
				return summary === undefined
					? []
					: [
							{
								at: event.at,
								kind: event.kind,
								summary: summary.slice(0, 300),
							},
						];
			})
			.slice(-FEED)
			.reverse(),
		history: history.map((row) => ({
			id: row.id,
			status: row.status,
			goal: (row.goal.split("\n", 1)[0] ?? "").slice(0, 120),
			attempts: row.attempts,
			turns: row.turns,
			createdAt: row.createdAt,
			updatedAt: row.updatedAt,
		})),
	};
}

/** Token-protected, read-only local observer with SSE push. */
export async function startSite(
	controller: LoopController,
	options: { pollMs?: number } = {},
): Promise<Site> {
	const token = randomBytes(24).toString("base64url");
	const assets = new Map<string, { body: Buffer; type: string }>();
	for (const [path, file, type] of ASSETS)
		assets.set(path, {
			body: await readFile(new URL(`./site/${file}`, import.meta.url)),
			type,
		});
	const clients = new Set<ServerResponse>();
	const blocked = new Set<ServerResponse>();
	const pending = new Map<ServerResponse, { event: string; data: unknown }>();
	let origin = "";
	let signature = "";
	let closed = false;
	const secure = (value: string) => {
		const a = Buffer.from(value);
		const b = Buffer.from(token);
		return a.length === b.length && timingSafeEqual(a, b);
	};
	const renderGoal = createGoalRenderer();
	const snapshot = () => {
		try {
			return siteSnapshot(controller, renderGoal);
		} catch {
			return { observedAt: Date.now(), error: "State temporarily unavailable" };
		}
	};
	const forget = (res: ServerResponse) => {
		clients.delete(res);
		blocked.delete(res);
		pending.delete(res);
	};
	const write = (res: ServerResponse, event: string, data: unknown) => {
		if (res.destroyed || res.writableEnded) return forget(res);
		if (blocked.has(res)) {
			// Keep only the newest snapshot for a slow client.
			if (event !== "ping" || !pending.has(res))
				pending.set(res, { event, data });
			return;
		}
		if (!res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`)) {
			blocked.add(res);
			res.once("drain", () => {
				blocked.delete(res);
				const next = pending.get(res);
				pending.delete(res);
				if (next) write(res, next.event, next.data);
			});
		}
	};
	const server = createServer((req, res) => {
		res.setHeader("Cache-Control", "no-store");
		res.setHeader("X-Content-Type-Options", "nosniff");
		res.setHeader("Referrer-Policy", "no-referrer");
		res.setHeader(
			"Content-Security-Policy",
			"default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'",
		);
		const fail = (code: number, message: string) => {
			res.writeHead(code, { "Content-Type": "application/json" });
			res.end(JSON.stringify({ error: message }));
		};
		try {
			// Reject DNS rebinding and cross-origin reads.
			if (
				`http://${req.headers.host}` !== origin ||
				(req.headers.origin && req.headers.origin !== origin)
			)
				return fail(403, "Origin rejected");
			if (req.method !== "GET") return fail(405, "Read-only observer");
			const url = new URL(req.url ?? "/", origin);
			const asset = assets.get(url.pathname);
			if (asset) {
				res.writeHead(200, { "Content-Type": `${asset.type}; charset=utf-8` });
				res.end(asset.body);
				return;
			}
			if (
				!secure(
					req.headers.authorization?.replace(/^Bearer /u, "") ??
						url.searchParams.get("token") ??
						"",
				)
			)
				return fail(401, "Open the link from /loop view");
			if (url.pathname === "/events") {
				if (clients.size >= MAX_CLIENTS) return fail(503, "Too many viewers");
				res.writeHead(200, {
					"Content-Type": "text/event-stream",
					Connection: "keep-alive",
					"X-Accel-Buffering": "no",
				});
				clients.add(res);
				write(res, "snapshot", snapshot());
				req.on("close", () => forget(res));
				return;
			}
			if (url.pathname === "/api/snapshot") {
				res.setHeader("Content-Type", "application/json");
				res.end(JSON.stringify(snapshot()));
				return;
			}
			fail(404, "Not found");
		} catch {
			if (!res.headersSent) fail(503, "State temporarily unavailable");
			else res.end();
		}
	});
	await new Promise<void>((resolve, reject) => {
		server.once("error", reject);
		server.listen(0, "127.0.0.1", () => {
			server.off("error", reject);
			resolve();
		});
	});
	const address = server.address();
	if (!address || typeof address === "string") {
		server.close();
		throw new Error("Loop site failed to bind");
	}
	origin = `http://127.0.0.1:${address.port}`;
	let stored = "";
	/** Live changes always rebuild; the poll only rebuilds when another writer touched the store. */
	const broadcast = (live: boolean) => {
		if (closed || !clients.size) return;
		if (!live) {
			let version: string;
			try {
				version = controller.store.version();
			} catch {
				version = "";
			}
			if (version && version === stored) {
				for (const res of clients)
					write(res, "ping", { observedAt: Date.now() });
				return;
			}
			stored = version;
		}
		const value = snapshot();
		const { observedAt, ...comparable } = value;
		const next = JSON.stringify(comparable);
		if (next === signature) {
			for (const res of clients) write(res, "ping", { observedAt });
			return;
		}
		signature = next;
		for (const res of clients) write(res, "snapshot", value);
	};
	const poll = setInterval(() => broadcast(false), options.pollMs ?? 1_000);
	poll.unref();
	let update: ReturnType<typeof setTimeout> | undefined;
	const unsubscribe = controller.subscribe(() => {
		if (closed || update || !clients.size) return;
		update = setTimeout(() => {
			update = undefined;
			broadcast(true);
		}, 80);
		update.unref();
	});
	return {
		url: `${origin}/#${token}`,
		async close() {
			if (closed) return;
			closed = true;
			clearInterval(poll);
			clearTimeout(update);
			unsubscribe();
			for (const res of clients) res.end();
			clients.clear();
			blocked.clear();
			pending.clear();
			const finished = new Promise<void>((resolve) =>
				server.close(() => resolve()),
			);
			server.closeAllConnections();
			await finished;
		},
	};
}