Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/loop/__tests__/controller.test.ts

Raw
import { execFileSync } from "node:child_process";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { LoopController } from "../src/controller.ts";
import type { GateOutput, GateResult } from "../src/gates.ts";
import { LoopStore } from "../src/store.ts";

let root: string | undefined;
let store: LoopStore | undefined;
afterEach(() => {
	store?.close();
	if (root) rmSync(root, { recursive: true, force: true });
	store = undefined;
	root = undefined;
});

const boundary = {
	outcome: "completed" as const,
	continue: false,
	entries: [],
	context: {
		pendingMessages: [],
		contextEntries: [],
		contextMessages: [],
		llmMessages: [],
		canContinue: true,
	},
};

async function controller(now: () => number) {
	root = mkdtempSync(join(tmpdir(), "loop-controller-"));
	store = await LoopStore.open(join(root, "state.db"));
	return new LoopController(store, now);
}

describe("loop ownership", () => {
	it("fences two owners and stale gate callbacks across replacement", async () => {
		const first = await controller(Date.now);
		const original = first.start({
			goal: "Owned",
			gates: [{ command: process.execPath }],
			cwd: tmpdir(),
		});
		first.interrupt("pause");
		const otherStore = await LoopStore.open(store!.path);
		const other = new LoopController(otherStore);
		let finish!: (result: GateResult) => void;
		let output!: (value: GateOutput) => void;
		try {
			expect(() => other.resume(store!.getRun(original.id)!)).toThrow(
				/owned by live/u,
			);
			expect(() => other.stopRecord(store!.getRun(original.id)!)).toThrow(
				/owned by live/u,
			);
			const child = execFileSync(
				process.execPath,
				[
					"--no-warnings",
					"--input-type=module",
					"-e",
					`
				import {LoopStore} from ${JSON.stringify(new URL("../src/store.ts", import.meta.url).href)};
				import {LoopController} from ${JSON.stringify(new URL("../src/controller.ts", import.meta.url).href)};
				const store=await LoopStore.open(${JSON.stringify(store!.path)});
				try { new LoopController(store).resume(store.getRun(${JSON.stringify(original.id)})); process.exitCode=1; }
				catch(error) { console.log(error.message); } finally { store.close(); }
			`,
				],
				{
					encoding: "utf8",
					env: { ...process.env, PI_LOOP_DEBUG: "0" },
					timeout: 10_000,
				},
			);
			expect(child).toMatch(/owned by live/u);
			await first.release();
			const pending = new LoopController(
				store!,
				Date.now,
				async (_gate, _cwd, _signal, onOutput) => {
					output = onOutput!;
					return new Promise<GateResult>((resolve) => {
						finish = resolve;
					});
				},
			);
			pending.resume(store!.getRun(original.id)!);
			const pass = pending.checkGates();
			await expect(pending.checkGates()).rejects.toThrow(/sequentially/u);
			const released = pending.release();
			expect(() => other.resume(store!.getRun(original.id)!)).toThrow(
				/owned by live/u,
			);
			output({ stdout: "stale", stderr: "", truncated: false });
			finish({
				exitCode: 0,
				error: null,
				stdout: "stale",
				stderr: "",
				truncated: false,
				startedAt: 1,
				endedAt: 2,
			});
			await pass;
			await released;
			expect(store!.attempts(original.id)).toHaveLength(0);
			other.resume(store!.getRun(original.id)!);
			expect(other.status()).toMatchObject({
				status: "active",
				attempts: 1,
				liveOutput: null,
			});
			expect(pending.status()).toBeNull();
			await other.release();
		} finally {
			otherStore.close();
		}
	});
});

describe("loop restore diagnostics", () => {
	it.each([
		{
			scenario: "completed failed baseline",
			startedAttempt: 2,
			startedIndex: 0,
			completed: [0],
			interrupted: false,
		},
		{
			scenario: "interrupted subsequent gate",
			startedAttempt: 2,
			startedIndex: 1,
			completed: [0],
			interrupted: true,
		},
		{
			scenario: "stale prior-attempt start",
			startedAttempt: 1,
			startedIndex: 1,
			completed: [],
			interrupted: false,
		},
		{
			scenario: "completed gate with null exit code",
			startedAttempt: 2,
			startedIndex: 0,
			completed: [0],
			interrupted: false,
			cancelled: true,
		},
	])(
		"restores $scenario without executing gates",
		async ({
			startedAttempt,
			startedIndex,
			completed,
			interrupted,
			cancelled,
		}) => {
			const first = await controller(() => 1000);
			const original = first.start({
				goal: "Recovery",
				gates: [
					{ name: "first", command: process.execPath },
					{ name: "second", command: process.execPath },
				],
				cwd: tmpdir(),
			});
			await first.release();
			const record = { ...original, attempts: 2, updatedAt: 2000 };
			store!.saveRun(record);
			store!.addEvent(
				record.id,
				"gate_started",
				{ index: startedIndex, attempt: startedAttempt },
				1500,
			);
			// Long agent work must not hide the last gate-start lifecycle record.
			for (let n = 0; n < 205; n++)
				store!.addEvent(record.id, "turn", { n }, 1600);
			for (const gateIndex of completed)
				store!.addAttempt({
					runId: record.id,
					attempt: 2,
					gateIndex,
					gateName: "first",
					exitCode: cancelled ? null : 1,
					error: cancelled ? "cancelled" : null,
					stdout: "",
					stderr: "failure",
					truncated: false,
					startedAt: 1500,
					endedAt: 1700,
				});
			const restored = new LoopController(
				store!,
				() => 3000,
				async () => {
					throw new Error("Restore must never execute gates");
				},
			);
			const snapshot = restored.restore(record);
			expect(snapshot.status).toBe("paused");
			expect(snapshot.reason.includes("interrupted gate")).toBe(interrupted);
			if (interrupted)
				expect(snapshot.reason).toContain("interrupted gate second");
			expect(snapshot.updatedAt).toBe(2000);
			expect(snapshot.attempts).toBe(2);
			expect(snapshot.lastPass?.results.length ?? 0).toBe(completed.length);
			await restored.release();
		},
	);
});

describe("loop restore replacement", () => {
	it("releases the previous paused owner and never restores terminal runs", async () => {
		const loop = await controller(Date.now);
		const a = loop.start({
			goal: "A",
			gates: [{ command: process.execPath }],
			cwd: tmpdir(),
		});
		loop.interrupt("pause");
		const b = { ...store!.getRun(a.id)!, id: "legacy-b", goal: "B" };
		store!.saveRun(b);
		store!.recoverLegacy(b.id);
		loop.restore(b);
		const observer = new LoopController(store!);
		observer.resume(store!.getRun(a.id)!);
		expect(observer.active).toBe(true);
		await observer.release();
		expect(() => loop.restore({ ...b, status: "stopped" })).toThrow(
			/cannot recover/u,
		);
		expect(() => loop.restore({ ...b, status: "succeeded" })).toThrow(
			/cannot recover/u,
		);
		await loop.release();
	});
});

describe("loop budgets", () => {
	it("pauses when the time budget is exhausted and restarts it on resume", async () => {
		let now = 1_000;
		const loop = await controller(() => now);
		const run = loop.start(
			{ goal: "Goal", gates: [{ command: process.execPath }], cwd: tmpdir() },
			{ minutes: 1, turns: null, attempts: null },
		);
		expect(loop.settle(boundary, false)?.continue).toBe(true);
		now += 60_000;
		expect(loop.settle(boundary, false)).toEqual({ continue: false });
		expect(loop.status()).toMatchObject({
			status: "paused",
			reason: "Time budget exhausted",
		});
		await expect(loop.checkGates()).rejects.toThrow(/not active/u);

		const record = store?.getRun(run.id);
		if (!record) throw new Error("run not persisted");
		loop.resume(record);
		expect(loop.status()).toMatchObject({
			status: "active",
			deadline: now + 60_000,
		});
	});

	it("bounds the audit queue and reports failed state writes", async () => {
		const loop = await controller(Date.now);
		loop.start(
			{ goal: "Goal", gates: [{ command: process.execPath }], cwd: tmpdir() },
			{ minutes: null, turns: null, attempts: null },
		);
		store?.close();
		for (let index = 0; index < 5_000; index++) loop.turn();
		loop.interrupt("User pause");
		const status = loop.status();
		expect(status?.pendingAudit).toBe(4_096);
		expect(status?.storageError).toBeTruthy();
	});
});