lab: the lab as a module, running beds when the mesh asks
Five tools on the machine the lab runs on: check, run beds against branches on the forge, a run's status, its log, and stop. A run checks out every repository the lab builds, side by side, and runs the suite; one at a time, answered at once with an id (novox/hq ADR 0172).
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
// lab's tools — the lab, as the mesh asks for it (novox/hq ADR 0172). They run on the machine the
|
||||
// lab is assigned to, and only there: a bed raises virtual machines on that machine's virtualisation.
|
||||
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { registerModuleTools, type ToolDefinition } from "@novox/mesh-sdk/tools";
|
||||
import { listRuns, readStatus, REPOSITORIES, running, start, stop, tail } from "./runs.js";
|
||||
|
||||
export function getLabTools(env: NodeJS.ProcessEnv): ToolDefinition[] {
|
||||
const work = env.MESH_LAB_WORK ?? "/var/lib/mesh-lab-runs";
|
||||
const forge = (env.MESH_LAB_FORGE ?? "").replace(/\/+$/, "");
|
||||
return [
|
||||
{
|
||||
name: "lab_check",
|
||||
description: "Whether this machine can run the lab's beds: the lab's own check, against the forge's main branch.",
|
||||
input: {},
|
||||
run: async () => {
|
||||
if (!forge) return { ok: false, output: "the lab's forge is not set: settings for lab, {\"forge\": \"<url>\"}" };
|
||||
const dir = `${work}/check`;
|
||||
spawnSync("rm", ["-rf", dir]);
|
||||
const clone = spawnSync("git", ["clone", "--quiet", "--depth", "1", `${forge}/novox/mesh-lab.git`, dir], { encoding: "utf8" });
|
||||
if (clone.status !== 0) return { ok: false, output: clone.stderr };
|
||||
spawnSync("npm", ["ci", "--no-audit", "--no-fund", "--loglevel=error"], { cwd: dir, encoding: "utf8" });
|
||||
const check = spawnSync("node", ["--experimental-strip-types", "src/cli.ts", "check"], { cwd: dir, encoding: "utf8" });
|
||||
return { ok: check.status === 0, output: `${check.stdout}${check.stderr}`.trim() };
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "lab_run",
|
||||
description:
|
||||
"Run the lab's beds against branches on the forge: fresh checkouts of every repository the lab builds, " +
|
||||
"side by side, then the suite on the named test files. Answers at once with the run's id; lab_status " +
|
||||
"and lab_log follow it. One run at a time.",
|
||||
input: {
|
||||
tests: { type: "string", description: "the bed test files, comma-separated, relative to mesh-lab (e.g. test/integration/mesh.test.ts)" },
|
||||
refs: {
|
||||
type: "string",
|
||||
description: `a JSON object of repository to branch, for any of ${REPOSITORIES.join(", ")}; the rest run main`,
|
||||
},
|
||||
},
|
||||
run: async (args) => {
|
||||
if (!forge) return { started: false, reason: "the lab's forge is not set: settings for lab, {\"forge\": \"<url>\"}" };
|
||||
const tests = String(args.tests ?? "").split(",").map((s) => s.trim()).filter(Boolean);
|
||||
if (tests.length === 0) return { started: false, reason: "name at least one bed test file" };
|
||||
let refs: Record<string, string> = {};
|
||||
if (args.refs) {
|
||||
try {
|
||||
refs = JSON.parse(String(args.refs)) as Record<string, string>;
|
||||
} catch {
|
||||
return { started: false, reason: "refs is not a JSON object of repository to branch" };
|
||||
}
|
||||
}
|
||||
const stranger = Object.keys(refs).filter((r) => !REPOSITORIES.includes(r));
|
||||
if (stranger.length > 0) return { started: false, reason: `the lab does not build ${stranger.join(", ")}` };
|
||||
const busy = running(work);
|
||||
if (busy) return { started: false, reason: `${busy.id} is still ${busy.state}; one run at a time`, running: busy };
|
||||
return { started: true, run: start(work, forge, tests, refs) };
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "lab_status",
|
||||
description: "A run's state, the commits it tested and how it ended — or every run, newest first, when no id is given.",
|
||||
input: { id: { type: "string", description: "the run's id (optional)" } },
|
||||
run: async (args) => {
|
||||
if (args.id) return readStatus(work, String(args.id)) ?? { found: false, id: String(args.id) };
|
||||
return { runs: listRuns(work).slice(0, 10) };
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "lab_log",
|
||||
description: "The last lines of a run's log.",
|
||||
input: {
|
||||
id: { type: "string", description: "the run's id" },
|
||||
lines: { type: "number", description: "how many lines from the end (default 200)" },
|
||||
},
|
||||
run: async (args) => ({ id: String(args.id), log: tail(work, String(args.id), Number(args.lines ?? 200)) }),
|
||||
},
|
||||
{
|
||||
name: "lab_stop",
|
||||
description: "Stop a run and everything it started.",
|
||||
input: { id: { type: "string", description: "the run's id" } },
|
||||
run: async (args) => stop(work, String(args.id)) ?? { found: false, id: String(args.id) },
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
registerModuleTools("lab", (env) => getLabTools(env));
|
||||
Reference in New Issue
Block a user