repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

scripts/test_goal_expect.mjs

Raw
File outline
#!/usr/bin/env node
import { spawnSync } from "node:child_process";
import { mkdirSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { commandExists, ROOT } from "./lib/common.mjs";

const OUT = join(ROOT, ".demo");
const MODEL = "minimax/MiniMax-M2.7-highspeed";
const IDEAS = [
	"Reply exactly PEXPECT_GOAL_OK and nothing else.",
	"Write exactly one sentence about persistent goals.",
	"List exactly three demo-safe commands a user might try.",
	"Explain goal status in exactly nine words.",
	"Create a tiny three-item checklist for finishing a code review.",
	"Write a haiku about persistent goals.",
	"Name one risk in endless autonomous loops.",
	"Draft a one-line release note for a goal-loop extension.",
	"Say whether a clear command should survive reloads, with one reason.",
	"Produce a two-item TODO list for improving tests.",
];
function arg(name, fallback) {
	const i = process.argv.indexOf(name);
	return i === -1 ? fallback : process.argv[i + 1];
}
function has(name) {
	return process.argv.includes(name);
}
function positionalIdea() {
	return process.argv.slice(2).find((a) => /^\d+$/.test(a));
}
function expectQuote(s) {
	return `{${String(s).replaceAll("}", "\\}")}}`;
}
function piCommand() {
	return `pi --model ${MODEL} --thinking off --no-session --no-context-files --no-skills --no-prompt-templates --no-extensions --no-builtin-tools -e extensions/goal`;
}
function clean(text) {
	return text
		.replace(/\x1b\][^\x07]*\x07/g, "")
		.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, "")
		.replaceAll("\r", "");
}
function run(objective, waitSeconds) {
	if (!commandExists("expect")) throw new Error("missing tool: expect");
	mkdirSync(OUT, { recursive: true });
	const script = `
set timeout ${Math.max(90, Math.ceil(waitSeconds + 30))}
log_user 1
cd ${expectQuote(ROOT)}
spawn -noecho -nottycopy -nottyinit -winsize 28 100 sh -lc ${expectQuote(piCommand())}
after 5000
expect -re {minimax}
expect -re {MiniMax-M2.7-highspeed}
send -- "/goal status\r"
after 3000
expect -re {No active goal}
send -- ${expectQuote(`/goal ${objective}\r`)}
after 1000
expect -re {Goal set}
expect -timeout ${Math.ceil(waitSeconds)} -re {(goal marked complete|goal complete|complete ·)}
send -- "/goal status\r"
after 4000
expect -re ${expectQuote(objective)}
expect -re {complete}
send -- "/goal clear\r"
after 2000
expect -re {Goal cleared}
send \\003
after 500
catch {close}
wait
`;
	const result = spawnSync("expect", ["-c", script], {
		cwd: ROOT,
		encoding: "utf8",
		maxBuffer: 10 * 1024 * 1024,
	});
	const transcript = clean(`${result.stdout ?? ""}\n${result.stderr ?? ""}`);
	const path = join(OUT, "goal_expect_smoke.txt");
	writeFileSync(path, transcript, "utf8");
	if (result.status !== 0)
		throw new Error(`goal expect failed; transcript ${path}`);
	return path;
}
function main() {
	if (has("--list-ideas")) {
		IDEAS.forEach((idea, i) => console.log(`${i + 1}. ${idea}`));
		return 0;
	}
	const idx = Number(positionalIdea() ?? arg("--idea", "1"));
	if (!Number.isInteger(idx) || idx < 1 || idx > IDEAS.length)
		throw new Error(`idea must be 1..${IDEAS.length}`);
	const objective = arg("--objective", IDEAS[idx - 1]);
	const wait = Number(arg("--llm-wait", "45"));
	const transcript = run(objective, wait);
	console.log(`\nOK goal expect smoke via ${MODEL}`);
	console.log(`OK transcript ${transcript}`);
	return 0;
}
try {
	process.exit(main());
} catch (e) {
	console.error(e.message);
	process.exit(1);
}