feat(cli, plugins, harness): the bus host runs the harness watch on a timer, keeps its results, and claude-code refuses a binary that lacks an adapter flag (#1873) #2016
No files matched your search
@@ -677,15 +677,18 @@ async function bus(rest, io, deps) {
|
||||
const priorBoot = readBootStatus(org.root);
|
||||
try { recordBootOk(org.root, { business: businessId, file }); } catch { /* status is advisory */ }
|
||||
const binding = readNotifyConfig(org.root, businessId);
|
||||
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches } }).catch((e) => {
|
||||
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
|
||||
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
||||
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
|
||||
// #1873 W2: the harness watch runs in the host with the env sessions launch from, unless the
|
||||
// host launches nothing (--no-launch). deps.harnessWatch replaces its options in tests.
|
||||
const harnessWatch = launches ? (deps.harnessWatch === undefined ? {} : deps.harnessWatch) : null;
|
||||
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches }, harnessWatch: harnessWatch ? { env: launchEnv, ...harnessWatch } : null }).catch((e) => {
|
||||
// org-defscan: the broker refused a definition that holds a credential it holds; the load was not good after all.
|
||||
if (e.code === "definition-holds-credential") try { recordBootRefused(org.root, { business: businessId, file, reason: e.message, prior: priorBoot }); } catch { /* the refusal still throws */ }
|
||||
throw e;
|
||||
});
|
||||
try {
|
||||
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
|
||||
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
||||
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
|
||||
// U4a (#1873): agents get the task tools when the bus serves task verbs for the organization: a
|
||||
// Vikunja tracker in the boot config, or the built-in store, which an organization with no
|
||||
// tracker gets (the bus runtime makes the same choice, runtime.mjs trackerOf).
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
// #1873 harness watch, slice W2 (reports/harness-watch-design.md section 1, "How often" and "Where
|
||||
// results are kept"): the bus host's timer over the W1 watch (packages/plugins/src/harness-watch.mjs).
|
||||
// It runs in the host process with the env sessions launch from.
|
||||
//
|
||||
// at start one full pass
|
||||
// every statMs (5 minutes) a stat pass: the stat key of every harness module the loader gives
|
||||
// (its optional watchKey({ env }), else its install roots), and a full pass when
|
||||
// the key is not the last full pass's, or when dayMs (a day) has gone by since it
|
||||
// halted/invalid while processing is halted or invalid (processingStatus), no pass at all; the
|
||||
// first tick after it clears runs a full pass
|
||||
//
|
||||
// A harness module may ask for a stat pass now (packages/plugins/src/watch-request.mjs; claude-code's
|
||||
// preflight asks when it has no verdict for its binary): it runs at once, or right after the pass
|
||||
// running, and runs a full pass only when the key changed, as a tick does.
|
||||
//
|
||||
// One pass at a time: a tick that comes while a pass runs does nothing. A pass that throws is
|
||||
// logged, with no path in the line, and the timer goes on; its key is not kept, so the next tick
|
||||
// runs a full pass again. stop() clears the timer, aborts the pass running (the watch aborts the
|
||||
// inspect it is in, which stops what it started) and waits for it; nothing is written after.
|
||||
//
|
||||
// The host is the one writer of two files under <org dataRoot>/state/:
|
||||
// harness-watch.json { version: 1, checkedAt, harnesses: [record], refused, gone }: the current
|
||||
// record per harness, as the watch returns it but without surfaces (their
|
||||
// store is slice W6). Written whole to a temp file in the same directory, then
|
||||
// renamed over the last one, 0600.
|
||||
// harness-watch.jsonl one line per transition the watch reports, appended, never rewritten.
|
||||
// The watch redacts its records (no host path, no env value). Both files are checked again before
|
||||
// they are written: a line that would hold the scratch directory, the home directory, the data
|
||||
// root or an env value of six characters or more is not written, and the pass is logged as failed.
|
||||
// The timer adds no model call and no network call: the stat pass reads file metadata only, and a
|
||||
// full pass is the watch's (its modules run their binaries with no network where the host allows).
|
||||
import { appendFileSync, chmodSync, closeSync, fsyncSync, mkdirSync, mkdtempSync, openSync, readFileSync, renameSync, rmSync, writeSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { loadInstalled } from "../../plugins/src/loader.mjs";
|
||||
import { watchHarnesses } from "../../plugins/src/harness-watch.mjs";
|
||||
import { statKey } from "../../plugins/src/stat-key.mjs";
|
||||
import { onWatchRequest } from "../../plugins/src/watch-request.mjs";
|
||||
import { processingStatus } from "../../processing-control/src/state.mjs";
|
||||
|
||||
export const STAT_MS = 5 * 60 * 1000;
|
||||
export const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
export const WATCH_VERSION = 1;
|
||||
export const watchFile = (dataRoot) => join(dataRoot, "state", "harness-watch.json");
|
||||
export const watchLog = (dataRoot) => join(dataRoot, "state", "harness-watch.jsonl");
|
||||
const VALUE_MIN = 6;
|
||||
|
||||
const realClock = Object.freeze({
|
||||
now: () => Date.now(),
|
||||
setInterval: (fn, ms) => setInterval(fn, ms),
|
||||
clearInterval: (t) => clearInterval(t),
|
||||
});
|
||||
|
||||
// The stat key of every harness the loader gives: per module, [name, its key]. A module's key is
|
||||
// its watchKey({ env }) when it has one, else the stat key of its installRoots({ env }), else null.
|
||||
// A module that comes or goes changes the key too. The key holds paths; it is compared in memory
|
||||
// only.
|
||||
export async function harnessKeys({ dataRoot, env }) {
|
||||
const loaded = await loadInstalled("harness", { dataRoot });
|
||||
const out = [];
|
||||
for (const { name, module } of loaded.modules) {
|
||||
let key = null;
|
||||
try {
|
||||
if (typeof module.watchKey === "function") key = module.watchKey({ env });
|
||||
else if (typeof module.installRoots === "function") key = statKey(module.installRoots({ env }));
|
||||
} catch (e) {
|
||||
key = ["error", typeof e?.code === "string" ? e.code : "threw"];
|
||||
}
|
||||
out.push([name, typeof key === "string" || Array.isArray(key) ? key : null]);
|
||||
}
|
||||
out.push(["refused", loaded.refused.map((r) => r?.name ?? null)]);
|
||||
return JSON.stringify(out);
|
||||
}
|
||||
|
||||
// Starts the timer and its first pass. Returns { stop, settled }: stop() ends it (a promise), and
|
||||
// settled() is the pass running, or a resolved promise, for tests and for stop.
|
||||
export function startHarnessWatch({
|
||||
dataRoot,
|
||||
env = process.env,
|
||||
log = () => {},
|
||||
clock = realClock,
|
||||
processing = () => processingStatus(dataRoot),
|
||||
watch = watchHarnesses,
|
||||
keys = () => harnessKeys({ dataRoot, env }),
|
||||
statMs = STAT_MS,
|
||||
dayMs = DAY_MS,
|
||||
timeoutMs = undefined,
|
||||
scratchRoot = () => tmpdir(),
|
||||
requests = onWatchRequest,
|
||||
} = {}) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot) throw new TypeError("startHarnessWatch needs the organization's dataRoot");
|
||||
const control = new AbortController();
|
||||
let stopped = false;
|
||||
let running = null;
|
||||
let held = false;
|
||||
let again = false;
|
||||
let needFull = true;
|
||||
let lastKey = null;
|
||||
let lastFull = null;
|
||||
let last = readRecords(dataRoot);
|
||||
|
||||
async function pass() {
|
||||
let status;
|
||||
try { status = processing(); } catch { status = { invalid: true }; }
|
||||
if (status?.halted || status?.invalid) {
|
||||
if (!held) log("harness watch: processing is halted or invalid, so passes wait for it");
|
||||
held = true;
|
||||
return;
|
||||
}
|
||||
if (held) { held = false; needFull = true; }
|
||||
const key = await keys();
|
||||
if (stopped) return;
|
||||
const now = clock.now();
|
||||
if (!needFull && key === lastKey && lastFull !== null && now - lastFull < dayMs) return;
|
||||
const scratch = mkdtempSync(join(scratchRoot(), "mosaic-watch-"));
|
||||
try {
|
||||
const result = await watch({ last, env, scratch, dataRoot, now: () => new Date(clock.now()), signal: control.signal, ...(timeoutMs ? { timeoutMs } : {}) });
|
||||
if (stopped || result?.stopped) return;
|
||||
store(dataRoot, result, { env, scratch, at: new Date(now).toISOString() });
|
||||
last = result.records;
|
||||
lastKey = key;
|
||||
lastFull = now;
|
||||
needFull = false;
|
||||
} finally {
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function tick() {
|
||||
if (stopped || running) return;
|
||||
running = pass()
|
||||
.catch((e) => { if (!stopped) log(`harness watch: a pass failed (${failure(e)}); the timer goes on`); })
|
||||
.finally(() => {
|
||||
running = null;
|
||||
if (again && !stopped) { again = false; tick(); }
|
||||
});
|
||||
}
|
||||
|
||||
// A request runs a tick now, or once right after the pass running.
|
||||
function request() {
|
||||
if (stopped) return;
|
||||
if (running) again = true;
|
||||
else tick();
|
||||
}
|
||||
|
||||
const timer = clock.setInterval(tick, statMs);
|
||||
timer?.unref?.();
|
||||
const unlisten = typeof requests === "function" ? requests(request) : null;
|
||||
tick();
|
||||
|
||||
return Object.freeze({
|
||||
settled: () => running ?? Promise.resolve(),
|
||||
async stop() {
|
||||
if (!stopped) {
|
||||
stopped = true;
|
||||
clock.clearInterval(timer);
|
||||
if (typeof unlisten === "function") unlisten();
|
||||
control.abort();
|
||||
}
|
||||
await (running ?? Promise.resolve());
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// What failed, without the error's text, which can name a host path.
|
||||
const failure = (e) => (typeof e?.code === "string" && /^[A-Za-z0-9_-]{1,40}$/.test(e.code) ? e.code : e?.name === "LeakError" ? "a state line held a host path or an env value" : "an error");
|
||||
|
||||
// The records of the last harness-watch.json, or none when it is missing or not this shape.
|
||||
function readRecords(dataRoot) {
|
||||
try {
|
||||
const doc = JSON.parse(readFileSync(watchFile(dataRoot), "utf8"));
|
||||
return doc?.version === WATCH_VERSION && Array.isArray(doc.harnesses) ? doc.harnesses.filter((r) => r && typeof r === "object" && typeof r.harness === "string") : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function store(dataRoot, result, { env, scratch, at }) {
|
||||
const records = (Array.isArray(result?.records) ? result.records : []).map(({ surfaces, ...rest }) => rest);
|
||||
const doc = { version: WATCH_VERSION, checkedAt: at, harnesses: records, refused: Array.isArray(result?.refused) ? result.refused : [], gone: Array.isArray(result?.gone) ? result.gone : [] };
|
||||
const transitions = Array.isArray(result?.transitions) ? result.transitions : [];
|
||||
const leaks = leakChecker({ env, scratch, dataRoot });
|
||||
if (leaks(doc) || transitions.some(leaks)) throw Object.assign(new Error("leak"), { name: "LeakError" });
|
||||
const text = `${JSON.stringify(doc, null, 2)}\n`;
|
||||
const lines = transitions.map((t) => `${JSON.stringify(t)}\n`).join("");
|
||||
const dir = join(dataRoot, "state");
|
||||
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
const file = watchFile(dataRoot);
|
||||
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
||||
try {
|
||||
const fd = openSync(tmp, "wx", 0o600);
|
||||
try { writeSync(fd, text); fsyncSync(fd); } finally { closeSync(fd); }
|
||||
renameSync(tmp, file);
|
||||
} catch (e) {
|
||||
rmSync(tmp, { force: true });
|
||||
throw e;
|
||||
}
|
||||
chmodSync(file, 0o600);
|
||||
if (lines) appendFileSync(watchLog(dataRoot), lines, { mode: 0o600 });
|
||||
}
|
||||
|
||||
// Whether a value holds a path the host knows (the scratch directory, the home directory, the data
|
||||
// root) or an env value of VALUE_MIN characters or more, the host's or the launch env's, in any of
|
||||
// its strings. Names the watch sets itself are not text and are skipped: a harness or module name
|
||||
// (an env value may name the harness), a lookup step, a state, a fit, a via, a time or a digest.
|
||||
const NOT_TEXT = new Set(["harness", "gone", "name", "kept", "source", "code", "state", "fit", "via", "at", "checkedAt", "changedAt", "digest", "lastDigest"]);
|
||||
export function leakChecker({ env, scratch, dataRoot }) {
|
||||
const needles = new Set();
|
||||
const add = (v) => { if (typeof v === "string" && v.length >= VALUE_MIN) needles.add(v); };
|
||||
for (const p of [scratch, dataRoot, homedir()]) add(p);
|
||||
for (const source of [env ?? {}, process.env]) for (const v of Object.values(source)) add(v);
|
||||
const holds = (text) => { for (const n of needles) if (text.includes(n)) return true; return false; };
|
||||
const walk = (v, key) => {
|
||||
if (typeof v === "string") return !NOT_TEXT.has(key) && holds(v);
|
||||
if (Array.isArray(v)) return v.some((x) => walk(x, key));
|
||||
if (v && typeof v === "object") return Object.entries(v).some(([k, x]) => walk(x, k));
|
||||
return false;
|
||||
};
|
||||
return (value) => walk(value, null);
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { CliError } from "./errors.mjs";
|
||||
import { cmdlineOf, startTimeOf } from "../../seat/src/proc.mjs";
|
||||
import { startHarnessWatch } from "./harness-watch-host.mjs";
|
||||
|
||||
const BROKER = fileURLToPath(new URL("../../bus/src/process.mjs", import.meta.url));
|
||||
const NOTIFIER = fileURLToPath(new URL("./notifier-process.mjs", import.meta.url));
|
||||
@@ -175,7 +176,10 @@ export function watchChildren(children, onDeath) {
|
||||
// {binding, base?, pollMs?}; base and pollMs exist for the tests, and the
|
||||
// command line never sets them, nor requestTimeoutMs. Resolves once both
|
||||
// children are up.
|
||||
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`) }) {
|
||||
// harnessWatch (#1873 W2): { env, ...options } starts the harness watch's timer in this process
|
||||
// once the host is up (harness-watch-host.mjs), with env the environment sessions launch from; the
|
||||
// host's close stops it first. null (the default) starts none.
|
||||
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`), harnessWatch = null }) {
|
||||
const dataRoot = boot.dataRoot;
|
||||
// A host started before host.lock existed holds only host.json.
|
||||
const prior = readHostState(dataRoot);
|
||||
@@ -372,6 +376,11 @@ export async function startHost({ boot, business, notifier = null, options = nul
|
||||
log(`${name} exited (${signal ?? code}); stopping the host`);
|
||||
close(1);
|
||||
});
|
||||
// #1873 W2: the harness watch's timer, stopped before anything else when the host closes.
|
||||
if (harnessWatch) {
|
||||
const watch = startHarnessWatch({ log, ...harnessWatch, dataRoot });
|
||||
hooks.unshift(() => watch.stop());
|
||||
}
|
||||
|
||||
// #1873 CB3: the copies the broker's boot proved live, names only; anything malformed counts as none.
|
||||
const copyBound = {};
|
||||
|
||||
@@ -0,0 +1,428 @@
|
||||
// #1873 harness watch, slice W2: the bus host's timer over the watch (src/harness-watch-host.mjs)
|
||||
// and its two state files. A fake clock and stand-in watches; one run goes through the real watch
|
||||
// with stand-in harnesses, and one through startHost.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { DAY_MS, STAT_MS, leakChecker, startHarnessWatch, watchFile, watchLog } from "../src/harness-watch-host.mjs";
|
||||
import { bootConfig, loadSystem } from "../src/config.mjs";
|
||||
import { startHost } from "../src/host.mjs";
|
||||
import { makeDeployment } from "../../discord/tests/helpers.mjs";
|
||||
import { fixture, tmp } from "./helpers.mjs";
|
||||
import { requestWatch } from "../../plugins/src/watch-request.mjs";
|
||||
import * as claude from "../../plugins/harness/claude-code/index.mjs";
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-hw-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// A clock the test moves: fire() runs the interval's callback, as the timer would every statMs.
|
||||
function fakeClock(start = Date.parse("2026-11-20T00:00:00Z")) {
|
||||
const c = { at: start, fn: null, ms: null, cleared: [], handle: { fake: true } };
|
||||
return Object.assign(c, {
|
||||
now: () => c.at,
|
||||
setInterval: (fn, ms) => { c.fn = fn; c.ms = ms; return c.handle; },
|
||||
clearInterval: (h) => c.cleared.push(h),
|
||||
fire: () => c.fn(),
|
||||
});
|
||||
}
|
||||
|
||||
const record = (over = {}) => ({ harness: "claude-code", state: "fit", via: "inspect", found: true, source: "MOSAIC_CLAUDE_BIN", version: "1.0.0", digest: "a".repeat(64), lastDigest: "a".repeat(64), fit: "pass", definite: true, failed: [], line: "ok", surfaces: { help: "usage" }, checkedAt: "2026-11-20T00:00:00.000Z", changedAt: "2026-11-20T00:00:00.000Z", ...over });
|
||||
const transition = (over = {}) => ({ harness: "claude-code", at: "2026-11-20T00:00:00.000Z", from: null, to: { state: "fit", version: "1.0.0", digest: "a".repeat(64) }, failed: [], line: "ok", ...over });
|
||||
|
||||
// A stand-in watch that counts its calls and answers what the test sets next.
|
||||
function stubWatch(answer = () => ({ records: [record()], transitions: [], refused: [], gone: [] })) {
|
||||
const calls = [];
|
||||
const watch = async (opts) => { calls.push(opts); return answer(opts, calls.length); };
|
||||
return { calls, watch };
|
||||
}
|
||||
|
||||
function start(t, over = {}) {
|
||||
const dataRoot = over.dataRoot ?? scratch(t);
|
||||
const clock = over.clock ?? fakeClock();
|
||||
const logs = [];
|
||||
let key = "k1";
|
||||
let status = { halted: false };
|
||||
const keyCalls = [];
|
||||
const w = over.stub ?? stubWatch();
|
||||
const handle = startHarnessWatch({
|
||||
dataRoot, clock, log: (l) => logs.push(l), env: over.env ?? {}, watch: w.watch,
|
||||
keys: async () => { keyCalls.push(key); return key; },
|
||||
processing: () => status,
|
||||
scratchRoot: () => over.scratchRoot ?? scratch(t),
|
||||
});
|
||||
t.after(() => handle.stop());
|
||||
return { dataRoot, clock, logs, handle, w, keyCalls, setKey: (k) => { key = k; }, setStatus: (s) => { status = s; } };
|
||||
}
|
||||
|
||||
async function tick(s) {
|
||||
s.clock.fire();
|
||||
await s.handle.settled();
|
||||
}
|
||||
|
||||
test("W2 host watch: one full pass at start; a stat pass with no change runs none; a changed key runs one; a day runs one", async (t) => {
|
||||
const s = start(t);
|
||||
await s.handle.settled();
|
||||
assert.equal(s.clock.ms, STAT_MS);
|
||||
assert.equal(s.w.calls.length, 1, "the start pass");
|
||||
assert.ok(existsSync(watchFile(s.dataRoot)));
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 2], "a stat pass reads the key and runs no full pass");
|
||||
s.setKey("k2");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "a changed key runs one full pass");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "and only one");
|
||||
s.clock.at += DAY_MS - 2 * STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "less than a day since the last full pass");
|
||||
s.clock.at += 2 * STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 3, "the daily pass");
|
||||
// The watch gets the last records, the clock, a scratch directory and the stop signal.
|
||||
const opts = s.w.calls[2];
|
||||
assert.equal(opts.last[0].harness, "claude-code");
|
||||
assert.equal(opts.now().getTime(), s.clock.at);
|
||||
assert.equal(typeof opts.scratch, "string");
|
||||
assert.equal(existsSync(opts.scratch), false, "its scratch directory is removed after the pass");
|
||||
assert.equal(opts.signal.aborted, false);
|
||||
});
|
||||
|
||||
test("W2 host watch: while processing is halted or invalid no pass runs; the first tick after resume runs one full pass", async (t) => {
|
||||
const s = start(t);
|
||||
await s.handle.settled();
|
||||
s.setStatus({ halted: true });
|
||||
for (let i = 0; i < 3; i++) { s.clock.at += STAT_MS; await tick(s); }
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 1], "halted: no key read, no pass");
|
||||
s.setStatus({ halted: null, invalid: true, code: "processing-state-invalid" });
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 1, "invalid: no pass");
|
||||
assert.equal(s.logs.filter((l) => /halted or invalid/.test(l)).length, 1, "logged once while held");
|
||||
s.setStatus({ halted: false });
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "resume runs a full pass though the key did not change");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "once");
|
||||
});
|
||||
|
||||
test("W2 host watch: a processing state that can't be read holds the start pass", async (t) => {
|
||||
const dataRoot = scratch(t);
|
||||
const clock = fakeClock();
|
||||
const w = stubWatch();
|
||||
const h = startHarnessWatch({ dataRoot, clock, watch: w.watch, keys: async () => "k", processing: () => { throw new Error("unreadable"); }, scratchRoot: () => scratch(t) });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.equal(w.calls.length, 0);
|
||||
});
|
||||
|
||||
test("W2 host watch: passes never overlap; a tick while one runs does nothing", async (t) => {
|
||||
let release;
|
||||
const stub = stubWatch((opts, n) => n === 1 ? new Promise((done) => { release = () => done({ records: [record()], transitions: [], refused: [], gone: [] }); }) : { records: [record()], transitions: [], refused: [], gone: [] });
|
||||
const s = start(t, { stub });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(s.w.calls.length, 1);
|
||||
const running = s.handle.settled();
|
||||
for (let i = 0; i < 5; i++) { s.setKey(`k${i + 2}`); s.clock.at += STAT_MS; s.clock.fire(); }
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 1], "no second pass and no key read while the first runs");
|
||||
release();
|
||||
await running;
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "the next tick after it runs normally");
|
||||
});
|
||||
|
||||
test("W2 host watch: a pass that throws is logged with no path and the timer goes on", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const stub = stubWatch((opts, n) => { if (n === 1) throw Object.assign(new Error(`boom at ${dir}/secret`), {}); if (n === 2) throw Object.assign(new Error(`eacces ${dir}`), { code: "EACCES" }); return { records: [record()], transitions: [], refused: [], gone: [] }; });
|
||||
const s = start(t, { stub });
|
||||
await s.handle.settled();
|
||||
assert.equal(s.logs.length, 1);
|
||||
assert.match(s.logs[0], /harness watch: a pass failed \(an error\); the timer goes on/);
|
||||
assert.ok(!s.logs[0].includes(dir));
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "the key was not kept, so the next tick runs a full pass again");
|
||||
assert.match(s.logs[1], /\(EACCES\)/);
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 3);
|
||||
assert.ok(existsSync(watchFile(s.dataRoot)));
|
||||
assert.deepEqual(s.clock.cleared, [], "the timer was never cleared");
|
||||
// A keys read that throws is a failed pass too.
|
||||
const k = startHarnessWatch({ dataRoot: scratch(t), clock: fakeClock(), log: (l) => s.logs.push(l), watch: stubWatch().watch, keys: async () => { throw new Error(dir); }, processing: () => ({ halted: false }), scratchRoot: () => scratch(t) });
|
||||
t.after(() => k.stop());
|
||||
await k.settled();
|
||||
assert.match(s.logs.at(-1), /a pass failed \(an error\)/);
|
||||
});
|
||||
|
||||
test("W2 host watch: stop clears the timer, aborts the pass running, waits for it, and writes nothing after", async (t) => {
|
||||
let seen = null;
|
||||
let ended = false;
|
||||
const stub = stubWatch((opts) => new Promise((done) => {
|
||||
seen = opts.signal;
|
||||
opts.signal.addEventListener("abort", () => setTimeout(() => { ended = true; done({ records: [record()], transitions: [transition()], refused: [], gone: [], stopped: true }); }, 50));
|
||||
}));
|
||||
const s = start(t, { stub });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(seen.aborted, false);
|
||||
await s.handle.stop();
|
||||
assert.equal(seen.aborted, true);
|
||||
assert.equal(ended, true, "stop waited for the pass to end");
|
||||
assert.deepEqual(s.clock.cleared, [s.clock.handle]);
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
assert.equal(existsSync(watchLog(s.dataRoot)), false);
|
||||
s.clock.fire();
|
||||
await s.handle.settled();
|
||||
assert.equal(s.w.calls.length, 1, "a tick after stop does nothing");
|
||||
assert.deepEqual(readdirSync(stub.calls[0].scratch.replace(/\/[^/]+$/, "")), [], "the pass's scratch directory is gone");
|
||||
// A watch that ignores the signal and answers normally still writes nothing once stopped.
|
||||
const late = start(t, { stub: stubWatch((opts) => new Promise((done) => opts.signal.addEventListener("abort", () => done({ records: [record()], transitions: [transition()], refused: [], gone: [] })))) });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
await late.handle.stop();
|
||||
assert.equal(existsSync(watchFile(late.dataRoot)), false);
|
||||
assert.deepEqual(late.logs, []);
|
||||
});
|
||||
|
||||
test("W2 host watch: the json is replaced whole and holds no surfaces; the jsonl only grows, one line per transition", async (t) => {
|
||||
let answer = { records: [record()], transitions: [transition()], refused: [{ event: "plugin-refused", kind: "harness", name: "odd", code: "plugin-interface", message: "x" }], gone: [] };
|
||||
const s = start(t, { stub: stubWatch(() => answer) });
|
||||
await s.handle.settled();
|
||||
const file = watchFile(s.dataRoot);
|
||||
const first = JSON.parse(readFileSync(file, "utf8"));
|
||||
assert.deepEqual(Object.keys(first).sort(), ["checkedAt", "gone", "harnesses", "refused", "version"]);
|
||||
assert.equal(first.version, 1);
|
||||
assert.equal(first.checkedAt, new Date(s.clock.at).toISOString());
|
||||
assert.equal(first.harnesses.length, 1);
|
||||
assert.equal("surfaces" in first.harnesses[0], false, "surfaces are slice W6's");
|
||||
assert.equal(first.refused[0].name, "odd");
|
||||
assert.equal(statSync(file).mode & 0o777, 0o600);
|
||||
assert.equal(statSync(join(s.dataRoot, "state")).mode & 0o777, 0o700);
|
||||
const log = watchLog(s.dataRoot);
|
||||
assert.equal(readFileSync(log, "utf8").split("\n").filter(Boolean).length, 1);
|
||||
assert.equal(statSync(log).mode & 0o777, 0o600);
|
||||
const ino = statSync(file).ino;
|
||||
const before = readFileSync(log, "utf8");
|
||||
// A second pass with two transitions and one harness gone.
|
||||
answer = { records: [record({ state: "red", fit: "fail", failed: ["--strict-mcp-config"] })], transitions: [transition({ to: { state: "red", version: "1.0.0", digest: "a".repeat(64) } }), transition({ harness: "pi" })], refused: [], gone: ["pi"] };
|
||||
s.setKey("k2");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
const second = JSON.parse(readFileSync(file, "utf8"));
|
||||
assert.notEqual(statSync(file).ino, ino, "renamed over, not written in place");
|
||||
assert.deepEqual([second.harnesses[0].state, second.refused, second.gone], ["red", [], ["pi"]]);
|
||||
const after = readFileSync(log, "utf8");
|
||||
assert.ok(after.startsWith(before), "the jsonl keeps every line it had");
|
||||
assert.equal(after.split("\n").filter(Boolean).length, 3);
|
||||
assert.deepEqual(after.split("\n").filter(Boolean).map((l) => JSON.parse(l).harness), ["claude-code", "claude-code", "pi"]);
|
||||
// A pass with no transition appends nothing.
|
||||
answer = { records: [record()], transitions: [], refused: [], gone: [] };
|
||||
s.setKey("k3");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(readFileSync(log, "utf8"), after);
|
||||
assert.deepEqual(readdirSync(join(s.dataRoot, "state")).sort(), ["harness-watch.json", "harness-watch.jsonl"], "no temp file is left");
|
||||
// A new host reads the records back as the last ones.
|
||||
const next = start(t, { dataRoot: s.dataRoot });
|
||||
await next.handle.settled();
|
||||
assert.equal(next.w.calls[0].last[0].harness, "claude-code");
|
||||
});
|
||||
|
||||
test("W2 host watch: a record or transition holding a host path or an env value is not written, and the log names no path", async (t) => {
|
||||
const secret = "fixture-secret-value-71";
|
||||
const cases = [
|
||||
(o) => ({ records: [record({ line: `found at ${o.scratch}/claude` })], transitions: [], refused: [], gone: [] }),
|
||||
() => ({ records: [record({ version: `1.0 ${homedir()}` })], transitions: [], refused: [], gone: [] }),
|
||||
() => ({ records: [record()], transitions: [transition({ line: `token ${secret}` })], refused: [], gone: [] }),
|
||||
(o) => ({ records: [record({ failed: [o.dataRoot] })], transitions: [], refused: [], gone: [] }),
|
||||
];
|
||||
for (const answer of cases) {
|
||||
const s = start(t, { env: { FIXTURE_TOKEN: secret }, stub: stubWatch(answer) });
|
||||
await s.handle.settled();
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
assert.equal(existsSync(watchLog(s.dataRoot)), false);
|
||||
assert.match(s.logs.join("\n"), /a state line held a host path or an env value/);
|
||||
for (const l of s.logs) assert.ok(!l.includes(s.dataRoot) && !l.includes(secret));
|
||||
}
|
||||
// An env value that is also a harness name or a state word does not block the write.
|
||||
const named = start(t, { env: { MOSAIC_HARNESS: "claude-code", OTHER: "unknown-ish" } });
|
||||
await named.handle.settled();
|
||||
assert.ok(existsSync(watchFile(named.dataRoot)));
|
||||
});
|
||||
|
||||
test("W2 host watch through the real watch: stand-in harnesses that print a scratch path and an env value leave neither in either file", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const secret = "fixture-secret-value-93";
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
// Its --version prints its own path and the secret; its --help lacks every adapter flag.
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.0 $0 ${secret} $HOME";;\n--help) echo "usage: claude [--version] at $0";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", FIXTURE_TOKEN: secret, HOME: join(root, "home") };
|
||||
const clock = fakeClock();
|
||||
const logs = [];
|
||||
const h = startHarnessWatch({ dataRoot, env, clock, log: (l) => logs.push(l), processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.deepEqual(logs, []);
|
||||
const json = readFileSync(watchFile(dataRoot), "utf8");
|
||||
const jsonl = readFileSync(watchLog(dataRoot), "utf8");
|
||||
const doc = JSON.parse(json);
|
||||
const rec = doc.harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.equal(rec.state, "red");
|
||||
assert.ok(rec.failed.includes("--strict-mcp-config"));
|
||||
assert.ok(doc.harnesses.some((r) => r.state === "missing" && r.source === "MOSAIC_PI_BIN"));
|
||||
assert.equal(jsonl.split("\n").filter(Boolean).length, doc.harnesses.length, "the first pass is a transition per harness");
|
||||
for (const text of [json, jsonl]) {
|
||||
for (const needle of [root, secret, homedir(), dataRoot, bin, tmpdir()]) assert.ok(!text.includes(needle), `no ${needle === secret ? "env value" : "host path"} in a state file`);
|
||||
}
|
||||
// Its scratch directories are gone.
|
||||
assert.deepEqual(readdirSync(root).sort(), ["bin", "org"]);
|
||||
// A stat pass with nothing changed runs no full pass: the jsonl and json stay as they are.
|
||||
clock.at += STAT_MS;
|
||||
clock.fire();
|
||||
await h.settled();
|
||||
assert.equal(readFileSync(watchFile(dataRoot), "utf8"), json);
|
||||
// The binary replaced: its key changed, so the next tick runs a full pass.
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.1";;\n--help) echo "usage";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
clock.at += STAT_MS;
|
||||
clock.fire();
|
||||
await h.settled();
|
||||
assert.notEqual(readFileSync(watchFile(dataRoot), "utf8"), json, "a changed key ran a full pass");
|
||||
assert.equal(JSON.parse(readFileSync(watchFile(dataRoot), "utf8")).harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN").version, "1.0.1");
|
||||
});
|
||||
|
||||
// Review P3-6: each needle of the backstop on its own: the data root, the home directory (with HOME
|
||||
// unset, so no env value covers it) and a value only the host process's env holds.
|
||||
test("W2 host watch: the backstop knows the data root, the home directory and the host's own env values", () => {
|
||||
const leaks = leakChecker({ env: {}, scratch: "/fixture-scratch-x", dataRoot: "/fixture-data-root/org" });
|
||||
assert.equal(leaks({ line: "at /fixture-data-root/org/state" }), true);
|
||||
assert.equal(leaks({ line: "at /fixture-scratch-x/home" }), true);
|
||||
assert.equal(leaks({ line: "nothing here" }), false);
|
||||
assert.equal(leaks({ harness: "/fixture-data-root/org" }), false, "a name field is not text");
|
||||
const saved = process.env.HOME;
|
||||
process.env.FIXTURE_HW_ONLY = "fixture-process-only-77";
|
||||
try {
|
||||
delete process.env.HOME;
|
||||
const home = homedir();
|
||||
const bare = leakChecker({ env: {}, scratch: "/fixture-scratch-x", dataRoot: "/fixture-data-root/org" });
|
||||
assert.equal(bare({ version: `1.0 ${home}` }), true, "the home directory");
|
||||
assert.equal(bare({ line: "token fixture-process-only-77" }), true, "a value of the host process's env");
|
||||
} finally {
|
||||
process.env.HOME = saved;
|
||||
delete process.env.FIXTURE_HW_ONLY;
|
||||
}
|
||||
});
|
||||
|
||||
// Review P3-5: a file: URL, a host:/path and a ~/ path a binary prints are paths too, and reach
|
||||
// neither file.
|
||||
test("W2 host watch through the real watch: a file: URL, a host:/path and a ~/ path in a version reach neither file", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0 file:///opt/fixture-tree/bin/claude host:/srv/fixture-a/b ~/.local/share/fixture/claude";;\n--help) echo "usage";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", HOME: join(root, "home") };
|
||||
const h = startHarnessWatch({ dataRoot, env, clock: fakeClock(), log: () => {}, processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
const json = readFileSync(watchFile(dataRoot), "utf8");
|
||||
const jsonl = readFileSync(watchLog(dataRoot), "utf8");
|
||||
assert.equal(JSON.parse(json).harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN").version, "1.0 <path> host:<path> <path>");
|
||||
for (const text of [json, jsonl]) for (const needle of ["fixture-tree", "/srv/", "fixture-a", "~/", "/fixture/claude"]) assert.ok(!text.includes(needle), `no ${needle} in a state file`);
|
||||
});
|
||||
|
||||
// Review P2-4: a module asks for a stat pass now (claude-code's preflight, with no verdict). It runs
|
||||
// at once, or once after the pass running, and a full pass only when the key changed.
|
||||
test("W2 host watch: a request runs a stat pass now, a full pass only on a changed key, once after a running pass, none after stop", async (t) => {
|
||||
let release = null;
|
||||
let hold = false;
|
||||
const stub = stubWatch(() => (hold ? new Promise((done) => { release = () => done({ records: [record()], transitions: [], refused: [], gone: [] }); }) : { records: [record()], transitions: [], refused: [], gone: [] }));
|
||||
const s = start(t, { stub });
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [1, 1]);
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [1, 2], "a request with the same key runs a stat pass and no full pass");
|
||||
s.setKey("k2");
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.equal(stub.calls.length, 2, "a request after the key changed runs a full pass");
|
||||
// Requests while a pass runs: one more stat pass right after it, however many came.
|
||||
hold = true;
|
||||
s.setKey("k3");
|
||||
requestWatch();
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(stub.calls.length, 3);
|
||||
hold = false;
|
||||
s.setKey("k4");
|
||||
requestWatch();
|
||||
requestWatch();
|
||||
assert.equal(s.keyCalls.length, 4, "no stat pass while a pass runs");
|
||||
release();
|
||||
await new Promise((done) => setTimeout(done, 20));
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [4, 5], "one stat pass after it, which saw k4 and ran a full pass");
|
||||
await s.handle.stop();
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [4, 5], "after stop a request does nothing");
|
||||
});
|
||||
|
||||
// Review P2-4, through the real watch: a red claude touched after its check has a new stat key, so
|
||||
// preflight has no verdict for a moment; it asks the watch, whose pass checks it again, and preflight
|
||||
// refuses again within that pass, not at the next tick.
|
||||
test("W2 host watch through the real watch: preflight's request re-checks a red claude touched after its check", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.0";;\n--help) echo "usage: claude [--version]";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", HOME: join(root, "home") };
|
||||
const launch = { ...env, ANTHROPIC_API_KEY: "fixture-api-key" };
|
||||
const clock = fakeClock();
|
||||
const h = startHarnessWatch({ dataRoot, env, clock, log: () => {}, processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.equal(claude.preflight({ env: launch })?.code, "harness-feature-missing");
|
||||
chmodSync(bin, 0o700);
|
||||
utimesSync(bin, new Date(), new Date(Date.now() + 60000));
|
||||
assert.equal(claude.preflight({ env: launch }), null, "no verdict for the new stat key: this launch is not held");
|
||||
await h.settled();
|
||||
assert.equal(claude.preflight({ env: launch })?.code, "harness-feature-missing", "the requested pass checked it again, with no tick");
|
||||
});
|
||||
|
||||
test("W2 startHost with harnessWatch starts the watch in the host, and close stops it", async (t) => {
|
||||
const root = tmp(t);
|
||||
const f = fixture(root);
|
||||
makeDeployment(root);
|
||||
const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: "acme", env: f.env });
|
||||
const clock = fakeClock();
|
||||
let signal = null;
|
||||
const stub = stubWatch((opts) => new Promise((done) => { signal = opts.signal; opts.signal.addEventListener("abort", () => done({ records: [], transitions: [], refused: [], gone: [], stopped: true })); }));
|
||||
const host = await startHost({ boot, business: "acme", log: () => {}, harnessWatch: { env: {}, clock, watch: stub.watch, keys: async () => "k", processing: () => ({ halted: false }), scratchRoot: () => scratch(t) } });
|
||||
let closed = false;
|
||||
t.after(() => (closed ? null : host.close(0)));
|
||||
await new Promise((done) => setTimeout(done, 50));
|
||||
assert.equal(stub.calls.length, 1, "the start pass runs in the host");
|
||||
assert.equal(stub.calls[0].dataRoot, boot.dataRoot);
|
||||
assert.equal(signal.aborted, false);
|
||||
assert.equal(await host.close(0), 0);
|
||||
closed = true;
|
||||
assert.equal(signal.aborted, true, "close aborted the pass");
|
||||
assert.deepEqual(clock.cleared, [clock.handle], "and cleared the timer");
|
||||
});
|
||||
@@ -268,27 +268,39 @@ const fitKey = (found, { levels, bwrap, unshare, node, host }) => JSON.stringify
|
||||
// the module applies. host: the environment of the process sessions launch from (the bus's own),
|
||||
// which a fit run's environment is built from as a session's is. bwrap and unshare: where those
|
||||
// tools are (tests pass stand-ins). Returns a promise of { ok: true, version, noMcp, digest, outer }
|
||||
// or { ok: false, message }. outer: { used: true } when the runs had unshare's outer layer, else
|
||||
// or { ok: false, message, digest, undecided }. outer: { used: true } when the runs had unshare's outer layer, else
|
||||
// { used: false, why } with why naming unshare and how it failed (sandboxLayers below). A call
|
||||
// while a check with the same key runs gets that check's promise. A check that throws (a scratch directory it can't
|
||||
// make, say) can't tell, so it gives { ok: false, message } too and keeps nothing.
|
||||
export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
|
||||
// #1873 harness watch W2: a failure also carries digest, the digest of the files the check ran
|
||||
// (null only when they can't be read), and undecided: true when the check could not tell (its
|
||||
// message then says "can't be checked"), false when the binary definitely failed. A reader takes
|
||||
// these fields and never parses the message.
|
||||
// signal (W2): the caller stops waiting when it aborts. A check is stopped (its runs killed, its
|
||||
// scratch removed) only when every caller that shares it passed a signal and every one of those
|
||||
// aborted: preflight passes none, so a check a launch waits on is never stopped by the watch. A
|
||||
// caller whose signal aborts gets its answer once the check has ended, or at once when a caller
|
||||
// without a signal keeps the check going; that answer can't tell (undecided) unless the check
|
||||
// had already finished.
|
||||
export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env, signal = null } = {}) {
|
||||
const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`;
|
||||
let key;
|
||||
try {
|
||||
key = fitKey(found, { levels, bwrap, unshare, node, host });
|
||||
} catch {
|
||||
return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read` });
|
||||
return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read`, digest: null, undecided: true });
|
||||
}
|
||||
if (passed.has(key)) return Promise.resolve(passed.get(key));
|
||||
if (running.has(key)) return running.get(key);
|
||||
if (running.has(key)) return share(running.get(key), signal, who, key);
|
||||
const cancel = new AbortController();
|
||||
const job = (async () => {
|
||||
try {
|
||||
sweepScratch();
|
||||
// The sandbox's own error text can name a host path, so the line keeps only what failed.
|
||||
const layers = await sandboxLayers({ bwrap, unshare, node });
|
||||
if (layers.problem) return { ok: false, message: `${who(null)} can't be checked: ${layers.problem}` };
|
||||
const result = await check(found, { levels, bwrap, unshare: layers.outer, node, who, digest: JSON.parse(key)[0], host });
|
||||
if (layers.problem) return { ok: false, message: `${who(null)} can't be checked: ${layers.problem}`, digest: JSON.parse(key)[0], undecided: true };
|
||||
if (cancel.signal.aborted) return { ok: false, message: `${who(null)} can't be checked: the check was stopped`, digest: JSON.parse(key)[0], undecided: true };
|
||||
const result = await check(found, { levels, bwrap, unshare: layers.outer, node, who, digest: JSON.parse(key)[0], host, cancel: cancel.signal });
|
||||
if (!result.ok) return result;
|
||||
const passing = { ...result, outer: layers.outer ? { used: true } : { used: false, why: layers.why } };
|
||||
passed.set(key, passing);
|
||||
@@ -296,11 +308,40 @@ export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, no
|
||||
} catch {
|
||||
// #1873 re-check 8: refused as a fit that failed. The error's text can name a host path, so
|
||||
// the line keeps none of it.
|
||||
return { ok: false, message: `${who(null)} can't be checked: the check could not run` };
|
||||
return { ok: false, message: `${who(null)} can't be checked: the check could not run`, digest: JSON.parse(key)[0], undecided: true };
|
||||
}
|
||||
})().finally(() => { if (running.get(key) === job) running.delete(key); });
|
||||
running.set(key, job);
|
||||
return job;
|
||||
})().finally(() => { if (running.get(key)?.job === job) running.delete(key); });
|
||||
const entry = { job, cancel, holds: 0, firm: false };
|
||||
running.set(key, entry);
|
||||
return share(entry, signal, who, key);
|
||||
}
|
||||
|
||||
// One caller's view of a running check: see signal above. A stopped check leaves running at once,
|
||||
// so a caller that comes after (a launch's preflight) starts its own check and never gets the
|
||||
// stopped one's answer.
|
||||
function share(entry, signal, who, key) {
|
||||
if (!signal) {
|
||||
entry.firm = true;
|
||||
return entry.job;
|
||||
}
|
||||
entry.holds++;
|
||||
return new Promise((resolve) => {
|
||||
let left = false;
|
||||
const leave = () => {
|
||||
if (left) return;
|
||||
left = true;
|
||||
entry.holds--;
|
||||
if (!entry.firm && entry.holds === 0) {
|
||||
entry.cancel.abort();
|
||||
if (running.get(key) === entry) running.delete(key);
|
||||
}
|
||||
if (entry.firm) resolve({ ok: false, message: `${who(null)} can't be checked: the watch stopped waiting`, digest: null, undecided: true });
|
||||
else entry.job.then(resolve);
|
||||
};
|
||||
signal.addEventListener("abort", leave, { once: true });
|
||||
entry.job.then((answer) => { signal.removeEventListener("abort", leave); if (!left) { left = true; entry.holds--; resolve(answer); } });
|
||||
if (signal.aborted) leave();
|
||||
});
|
||||
}
|
||||
|
||||
// The passed check for found as piFit would key it now, or null: none passed, or its files can't
|
||||
@@ -313,11 +354,14 @@ export function fitVerdict(found, { levels = [], bwrap = BWRAP, unshare = UNSHAR
|
||||
}
|
||||
}
|
||||
|
||||
async function check(found, { levels, bwrap, unshare, node, who, digest, host }) {
|
||||
async function check(found, { levels, bwrap, unshare, node, who, digest, host, cancel = null }) {
|
||||
// At most FIT_CONCURRENCY runs at once, started in the order they are asked for. stop kills the
|
||||
// runs still going and keeps queued ones from starting; the check waits for every run it began,
|
||||
// so no scratch directory or child outlives it.
|
||||
const stop = new AbortController();
|
||||
const cancelled = () => stop.abort();
|
||||
cancel?.addEventListener("abort", cancelled, { once: true });
|
||||
if (cancel?.aborted) stop.abort();
|
||||
const begun = [];
|
||||
let active = 0;
|
||||
const waiting = [];
|
||||
@@ -366,6 +410,7 @@ async function check(found, { levels, bwrap, unshare, node, who, digest, host })
|
||||
try {
|
||||
return await judge(found, { levels, who, digest, run });
|
||||
} finally {
|
||||
cancel?.removeEventListener("abort", cancelled);
|
||||
stop.abort();
|
||||
await Promise.allSettled(begun);
|
||||
}
|
||||
@@ -377,8 +422,10 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
const help = await helped;
|
||||
const v = await versioned;
|
||||
const version = v.status === 0 ? versionLabel(v.stdout) : null;
|
||||
const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` });
|
||||
if (help.status !== 0) return refuse("can't be checked: --help failed");
|
||||
// refuse: a definite failure. unsure: the check could not tell.
|
||||
const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}`, digest, undecided: false });
|
||||
const unsure = (why) => ({ ok: false, message: `${who(version)} can't be checked: ${why}`, digest, undecided: true });
|
||||
if (help.status !== 0) return unsure("--help failed");
|
||||
const lines = help.stdout.split("\n");
|
||||
const has = (flag) => lines.some((l) => optionsIn(l).includes(flag));
|
||||
const line = (flag) => lines.find((l) => optionsIn(l).includes(flag)) ?? "";
|
||||
@@ -418,28 +465,28 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
[mode, run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input, { noMcp })]);
|
||||
|
||||
const control = await controlRun;
|
||||
if (control.stopped) return refuse(`can't be checked: the start without the seal flags ${control.stopped}`);
|
||||
if (!control.answer.state) return refuse("can't be checked: it didn't answer get_state");
|
||||
if (!control.answer.commands) return refuse("can't be checked: it didn't answer get_commands");
|
||||
if (!control.explicit || control.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags");
|
||||
if (control.stopped) return unsure(`the start without the seal flags ${control.stopped}`);
|
||||
if (!control.answer.state) return unsure("it didn't answer get_state");
|
||||
if (!control.answer.commands) return unsure("it didn't answer get_commands");
|
||||
if (!control.explicit || control.prompt === null) return unsure("its -e extension didn't see the system prompt without the seal flags");
|
||||
// A kind counts only through a source outside the project, except the trusted project files
|
||||
// themselves: a project source can wait on project trust, and then --no-approve alone would keep
|
||||
// it out of the sealed runs and hide a seal flag of its kind that the binary ignores.
|
||||
const shown = new Set(control.seen.filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unshown = KINDS.filter((k) => (k !== "MCP server" || noMcp) && !shown.has(k));
|
||||
if (unshown.length) return refuse(`can't be checked: it didn't load a planted ${unshown.join(", ")} without the seal flags`);
|
||||
if (unshown.length) return unsure(`it didn't load a planted ${unshown.join(", ")} without the seal flags`);
|
||||
// -p answers no get_commands, so the -p runs see skills and prompt templates only through the -e
|
||||
// extension's command list, which must show them here.
|
||||
const listed = new Set(control.listed.filter((s) => !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unlisted = ["skill", "prompt template"].filter((k) => !listed.has(k));
|
||||
if (unlisted.length) return refuse(`can't be checked: its -e extension didn't list a planted ${unlisted.join(", ")} without the seal flags`);
|
||||
if (unlisted.length) return unsure(`its -e extension didn't list a planted ${unlisted.join(", ")} without the seal flags`);
|
||||
|
||||
const rpc = await rpcSealed;
|
||||
if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`);
|
||||
if (!rpc.answer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on");
|
||||
if (!rpc.answer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on");
|
||||
if (rpc.stopped) return unsure(`the start with the seal flags ${rpc.stopped}`);
|
||||
if (!rpc.answer.state) return unsure("it didn't answer get_state with the seal flags on");
|
||||
if (!rpc.answer.commands) return unsure("it didn't answer get_commands with the seal flags on");
|
||||
if (!rpc.explicit) return refuse("didn't load an -e extension with the seal flags on");
|
||||
if (rpc.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on");
|
||||
if (rpc.prompt === null) return unsure("its -e extension didn't see the system prompt with the seal flags on");
|
||||
if (!rpc.prompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on");
|
||||
if (rpc.seen.length) return refuse(`loaded a planted ${rpc.seen.map((s) => s.source).join(", ")} with the seal flags on`);
|
||||
|
||||
@@ -447,10 +494,10 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
for (const [flag, kind, pending] of oneOut) {
|
||||
const r = await pending;
|
||||
const but = `with every seal flag but ${flag}`;
|
||||
if (r.stopped) return refuse(`can't be checked: the start ${but} ${r.stopped}`);
|
||||
if (!r.answer.state || !r.answer.commands) return refuse(`can't be checked: it didn't answer get_state and get_commands ${but}`);
|
||||
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${but}`);
|
||||
if (!r.seen.some((s) => s.kind === kind)) return refuse(`can't be checked: it didn't load a planted ${kind} ${but}`);
|
||||
if (r.stopped) return unsure(`the start ${but} ${r.stopped}`);
|
||||
if (!r.answer.state || !r.answer.commands) return unsure(`it didn't answer get_state and get_commands ${but}`);
|
||||
if (r.prompt === null) return unsure(`its -e extension didn't see the system prompt ${but}`);
|
||||
if (!r.seen.some((s) => s.kind === kind)) return unsure(`it didn't load a planted ${kind} ${but}`);
|
||||
const others = r.seen.filter((s) => s.kind !== kind && !(flag === "--no-extensions" && !noMcp && s.kind === "MCP server"));
|
||||
if (others.length) return refuse(`loaded a planted ${others.map((s) => s.source).join(", ")} ${but}`);
|
||||
}
|
||||
@@ -458,23 +505,23 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
for (const [shape, pending] of shaped) {
|
||||
const r = await pending;
|
||||
const name = shapeName(shape);
|
||||
if (r.stopped) return refuse(`can't be checked: the start ${name} ${r.stopped}`);
|
||||
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${name}`);
|
||||
if (r.stopped) return unsure(`the start ${name} ${r.stopped}`);
|
||||
if (r.prompt === null) return unsure(`its -e extension didn't see the system prompt ${name}`);
|
||||
if (!r.explicit || !r.second) return refuse(`didn't load every -e extension ${name}`);
|
||||
if (!r.prompt.includes(GIVEN)) return refuse(`didn't use the --system-prompt file ${name}`);
|
||||
if (shape.cont && r.session !== PLANTED_SESSION) return refuse(`can't be checked: it didn't resume the planted session with -c ${name}`);
|
||||
if (shape.cont && r.session !== PLANTED_SESSION) return unsure(`it didn't resume the planted session with -c ${name}`);
|
||||
if (r.seen.length) return refuse(`loaded a planted ${r.seen.map((x) => x.source).join(", ")} with the seal flags ${name}`);
|
||||
}
|
||||
|
||||
const p = await pRun;
|
||||
if (p.stopped) return refuse(`can't be checked: the start in a worker's shape ${p.stopped}`);
|
||||
if (p.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt in a worker's shape");
|
||||
if (p.stopped) return unsure(`the start in a worker's shape ${p.stopped}`);
|
||||
if (p.prompt === null) return unsure("its -e extension didn't see the system prompt in a worker's shape");
|
||||
if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} in a worker's shape with --skill`);
|
||||
if (!p.skills) return refuse("didn't load every --skill directory with --no-skills on in a worker's shape");
|
||||
|
||||
for (const [mode, pending] of missingRuns) {
|
||||
const r = await pending;
|
||||
if (r.stopped) return refuse(`can't be checked: a ${mode} start with a missing -e extension ${r.stopped}`);
|
||||
if (r.stopped) return unsure(`a ${mode} start with a missing -e extension ${r.stopped}`);
|
||||
if (r.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension");
|
||||
}
|
||||
return { ok: true, version, noMcp, digest };
|
||||
|
||||
@@ -543,14 +543,21 @@ test("pi-unpin S3 re-check 7: env() refuses while the fit check is still running
|
||||
assert.equal(pi.env({ policy: { tools: ["read"] }, tools: [], session, env: s.env }).PI_OFFLINE, "1");
|
||||
});
|
||||
|
||||
// #1873 W2: a check that could not run is a failure with the digest of the files it would have run
|
||||
// and undecided: true, beside its message.
|
||||
function assertUndecided(r, message) {
|
||||
assert.deepEqual(r, { ok: false, message, digest: r.digest, undecided: true });
|
||||
assert.match(r.digest, /^[0-9a-f]{64}$/);
|
||||
}
|
||||
|
||||
test("pi-unpin S3: a check that cannot run refuses", SKIP, async t => {
|
||||
const s = standIn(t, {});
|
||||
const r = await piFit(locate(s.env), { levels: LEVELS, bwrap: join(s.root, "no-bwrap") });
|
||||
assert.deepEqual(r, { ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) is missing" });
|
||||
assertUndecided(r, "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) is missing");
|
||||
// A sandbox that fails says so without its own error text, which can name a host path.
|
||||
const broken = join(s.root, "broken-bwrap");
|
||||
writeFileSync(broken, `#!/bin/sh\necho "bwrap: Can't find source path ${s.root}/x" >&2\nexit 1\n`, { mode: 0o755 });
|
||||
assert.deepEqual(await piFit(locate(s.env), { levels: LEVELS, bwrap: broken }), { ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed" });
|
||||
assertUndecided(await piFit(locate(s.env), { levels: LEVELS, bwrap: broken }), "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed");
|
||||
// An unreadable package is refused before anything runs.
|
||||
const found = locate(s.env);
|
||||
rmSync(join(s.root, "prefix", "lib", "node_modules", NAME, "package.json"));
|
||||
@@ -742,7 +749,7 @@ test("pi-unpin S3 re-check 10: a failed probe names the tool that failed, and on
|
||||
assert.deepEqual(r.outer, { used: false, why });
|
||||
}
|
||||
assert.deepEqual((await fitWith({})).outer, { used: true });
|
||||
assert.deepEqual(await fitWith({ unshare: refusing, bwrap: broken }), { ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed" });
|
||||
assertUndecided(await fitWith({ unshare: refusing, bwrap: broken }), "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed");
|
||||
});
|
||||
|
||||
// R10-P2-1: without unshare's outer layer, a bwrap killed in its setup leaves its child waiting for
|
||||
|
||||
+91
-11
@@ -371,7 +371,7 @@ the kind's whole setting, so it names every harness that stays on:
|
||||
|
||||
Jason's ruling (2026-11-19): no pins on any harness, a periodic check
|
||||
instead. `src/harness-watch.mjs` is the function that check calls.
|
||||
`watchHarnesses({ last, env, scratch, dataRoot, timeoutMs })` makes one
|
||||
`watchHarnesses({ last, env, scratch, dataRoot, timeoutMs, signal })` makes one
|
||||
pass over every harness `loadInstalled('harness', { dataRoot })` gives,
|
||||
tree or installed. It holds no name list, so a harness added later is
|
||||
watched at once. It writes nothing: it takes the records the last pass
|
||||
@@ -396,16 +396,59 @@ returned and gives back `{ records, transitions, refused, gone }`.
|
||||
a directory the module may use for a scratch HOME; it removes what it makes
|
||||
there. `signal` aborts when the watch stops waiting. `pi` returns its fit
|
||||
check (`packages/harness/src/pi-fit.mjs`), called exactly as `preflight`
|
||||
calls it, so the two share one cache and one running check. Its digest is
|
||||
that check's digest of the binary's files; a failed check gives none. A
|
||||
failure whose line, after pi's lookup label, starts with "can't be checked"
|
||||
is `fit: "unknown"`.
|
||||
calls it, so the two share one cache and one running check (a test may pass
|
||||
its own check as `check`). Its digest is
|
||||
that check's digest of the binary's files, on a pass and (W2) on a failure
|
||||
too. W2: a failed check carries `undecided`, true when the check could not
|
||||
tell and false when the binary definitely failed. `inspect` reads that
|
||||
field: `undecided: false` is `fit: "fail"`, definite; anything else is
|
||||
`fit: "unknown"`. It never parses the message. The check's runs, and the
|
||||
`--version` capture after a failure, stop when `signal` aborts and only the
|
||||
watch waits on them; a check `preflight` also waits on keeps running.
|
||||
`claude-code` returns the sha256 of the binary's real file and the first
|
||||
line of `--version`, and captures `--help` as the `help` surface, both run
|
||||
from that same real file with only PATH and a scratch HOME. Its lookup for
|
||||
the watch skips a relative PATH entry, as pi's does. Its fit stays
|
||||
`"unknown"` until slice W2 adds the claude fit rule (and runs these
|
||||
captures with no network).
|
||||
the watch skips a relative PATH entry, as pi's does. Each capture runs in its
|
||||
own process group: a stop, a timeout or the capture's end kills the whole
|
||||
group, so nothing the binary left in the background outlives it, and a signal
|
||||
already aborted runs nothing.
|
||||
|
||||
W2, the claude fit rule: every flag the claude adapter passes appears in
|
||||
`--help`, and `--version` answers. The flags come from the adapter itself:
|
||||
`inspect` runs `adapters/claude/adapter.sh` twice against a recorder that
|
||||
writes its argv (a first turn, which passes `--session-id`, then a second,
|
||||
which passes `--resume`), so the module holds no list of them. The rule
|
||||
reads the text the binary printed. A flag counts only from an option line
|
||||
(one to four spaces, then `-`), read up to the description; a flag a
|
||||
description only mentions does not count. `--x[-y]` anywhere in the text
|
||||
gives `--x-y` when `--x` has its own option line (claude 2.1.322 names
|
||||
`--system-prompt-file` only as `--system-prompt[-file]` in the `--bare`
|
||||
description, beside a `--system-prompt` option line). A missing flag is `fit: "fail"`, definite, with
|
||||
the flag as the rule id; so is `--version` or `--help` exiting non-zero
|
||||
(rule id `--version` or `--help`). A capture that timed out, was stopped,
|
||||
could not start or printed too much, or an adapter run that failed, is
|
||||
`unknown`. The captures run with no network: `unshare -r -n` when a probe
|
||||
of it runs, else without it, as the pi fit check drops unshare's layer when
|
||||
it is missing or refused, and the line then ends "run with the network on:
|
||||
unshare is missing" (or "unshare failed"). A definite verdict is kept in
|
||||
memory by the stat key (`src/stat-key.mjs`) of the binary's real file and
|
||||
the adapter; `preflight` reads it and refuses `harness-feature-missing` on a
|
||||
fail. A replaced binary or adapter has another key, so an old verdict no
|
||||
longer applies. With no verdict, or an undecided one, `preflight` does not
|
||||
refuse in W2. With no verdict it asks for a watch pass at once
|
||||
(`src/watch-request.mjs`); in the bus host that runs a stat pass now, and a
|
||||
full pass when the key changed, so a binary or adapter touched after its
|
||||
check is checked again within one pass. `preflight` stays synchronous and
|
||||
does not wait for that pass: the launch that missed, any launch before the
|
||||
first full pass at host start ends, and any launch while the pass runs go
|
||||
ahead. With no listener (no bus host) the request does nothing.
|
||||
|
||||
`watchKey({ env })` is a second optional export: a string the bus host's
|
||||
timer compares every five minutes (W2). It changes when anything the module
|
||||
would check changes; `claude-code` and `pi` give the stat key of their
|
||||
binary, adapter and module files. A module without it is keyed by the stat
|
||||
key of its `installRoots({ env })`. The key holds paths and is compared in
|
||||
memory only.
|
||||
|
||||
A module with no `inspect` is watched through `version({ env })`, with fit
|
||||
`"unknown"`. A record's `state` is:
|
||||
@@ -431,13 +474,15 @@ field it replaces the scratch directory, the home directory, every env value
|
||||
of six characters or more and every PATH entry, each also as its real path
|
||||
and without a trailing slash, with a name for it, the user name (USER,
|
||||
LOGNAME, the process's account) with `<user>`, then any other absolute path
|
||||
of two segments or more with `<path>`. The loader's refusals in `refused`
|
||||
of two segments or more with `<path>`, including one after a `word:` (as in
|
||||
`host:/srv/a`), a `file://` URL and a `~/` or `~name/` path. The loader's
|
||||
refusals in `refused`
|
||||
get the same treatment. A record also carries `via` (`inspect` or
|
||||
`version`), `lastDigest`, `checkedAt` and `changedAt`. A transition
|
||||
(`{ harness, at, from, to, failed, line }`, with `from` null on a first
|
||||
sight) is given only when the state or the binary is not the last record's.
|
||||
The digest names the binary; when neither record has one (pi gives none for
|
||||
a failed check), the version label stands in for it, and only when both
|
||||
The digest names the binary; when neither record has one (a module with
|
||||
no digest, or pi's files that can't be read), the version label stands in for it, and only when both
|
||||
records have a version: a timeout or a throw knows none, and is not a new
|
||||
binary. So unknown 2.0, a timeout, then unknown 2.1, all with no digest,
|
||||
gives no transition for the version; W1 keeps no last known version.
|
||||
@@ -448,6 +493,41 @@ stored `help` reads `--<user>` where claude printed `--model`. A fit rule
|
||||
(slice W2's flag check, for one) runs inside the module on the text the
|
||||
binary printed and puts its answer in `fit`, `failed` and `line`.
|
||||
|
||||
W2: rule ids may start with `-` or `--`, so a flag can be one. The watch
|
||||
takes an optional `signal`; when it aborts, the watch aborts the `inspect`
|
||||
it is in (waiting up to 5 s for it to end), asks no further module and
|
||||
returns `stopped: true` with no records.
|
||||
|
||||
### The bus host's timer (#1873, slice W2)
|
||||
|
||||
`packages/cli/src/harness-watch-host.mjs` runs the watch in the bus host
|
||||
process, with the environment sessions launch from. `mosaic bus start`
|
||||
starts it unless `--no-launch` is given; `startHost({ harnessWatch })` is the
|
||||
switch. It runs one full pass at start, then every five minutes a stat pass
|
||||
that reads each module's `watchKey` (or install roots), and a full pass when
|
||||
that key changed or a day has gone by. While processing is halted or
|
||||
invalid it runs nothing; the first tick after resume runs a full pass. A
|
||||
tick while a pass runs does nothing. A pass that throws is logged with no
|
||||
path and the timer goes on; its key is not kept, so the next tick tries
|
||||
again. Stopping the host clears the timer and aborts the pass running. A
|
||||
request from `src/watch-request.mjs` (preflight's, on a miss) runs a tick at
|
||||
once; a request while a pass runs runs one tick after it, and a request
|
||||
after stop does nothing.
|
||||
|
||||
The host is the one writer of two files under `<org dataRoot>/state/`:
|
||||
|
||||
- `harness-watch.json`: `{ version: 1, checkedAt, harnesses, refused, gone }`,
|
||||
the records of the last full pass without `surfaces`, written to a temp
|
||||
file and renamed over the last one, mode 0600;
|
||||
- `harness-watch.jsonl`: one line per transition, appended only.
|
||||
|
||||
Before either is written, every string in them is checked again for the
|
||||
scratch directory, the home directory, the data root and any env value of
|
||||
six characters or more, from the launch environment and the host's own; a
|
||||
hit writes nothing and logs the pass as failed. Name, code, state, digest
|
||||
and time fields (`harness`, `gone`, `name`, `kept`, `source`, `code`,
|
||||
`state`, `fit`, `via`, the digests and the times) are not checked as text.
|
||||
|
||||
## Transcript usage source (row 124)
|
||||
|
||||
`usage-source/transcript` now ships interface 1's `readTurn({harness, root})`.
|
||||
|
||||
@@ -6,11 +6,14 @@
|
||||
// session.json. packages/plugins/README.md lists the interface.
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { accessSync, constants, createReadStream, mkdtempSync, realpathSync, rmSync, statSync } from "node:fs";
|
||||
import { accessSync, constants, createReadStream, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { delimiter, dirname, isAbsolute, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { claudeBuiltins, MCP_PREFIX } from "../../../harness/src/gate.mjs";
|
||||
import { hasSessionCredential, SESSION_CREDENTIALS } from "../../../seat/src/session.mjs";
|
||||
import { UNSHARE } from "../../../seat/src/sandbox.mjs";
|
||||
import { statKey } from "../../src/stat-key.mjs";
|
||||
import { requestWatch } from "../../src/watch-request.mjs";
|
||||
|
||||
export const interfaceVersion = 1;
|
||||
|
||||
@@ -61,13 +64,25 @@ export function preflight({ env, proxy = null }) {
|
||||
const names = SESSION_CREDENTIALS.map((set) => set.join(" with ")).join(", ");
|
||||
return { code: "no-session-credential", message: `the bus host has none of ${names}; the bus unit's EnvironmentFile sets one when the bus starts` };
|
||||
}
|
||||
if (!binary(env)) {
|
||||
const bin = binary(env);
|
||||
if (!bin) {
|
||||
return {
|
||||
code: "harness-binary-missing",
|
||||
// Names the variable, never its value, which is a host path (#1873).
|
||||
message: env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN isn't an executable file at an absolute path" : "no claude on the bus host's PATH",
|
||||
};
|
||||
}
|
||||
// #1873 harness watch W2: the fit verdict the last check of this very file gave (inspect below),
|
||||
// found by the stat key of its real file and the adapter's. Only a definite failure refuses. No
|
||||
// verdict, or a check that could not tell, does not refuse in this slice. With no verdict, the
|
||||
// bus host's watch is asked for a stat pass now (src/watch-request.mjs), so a binary replaced,
|
||||
// touched or moved since the last check is checked within one pass, not at the next tick; this
|
||||
// launch is not held for it.
|
||||
const verdict = verdictFor(bin);
|
||||
if (!verdict) requestWatch();
|
||||
if (verdict?.fit === "fail") {
|
||||
return { code: "harness-feature-missing", message: `the claude from ${env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN" : "PATH"} failed the fit check: ${verdict.why}` };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -90,36 +105,160 @@ export function version({ env }) {
|
||||
return r.status === 0 ? r.stdout.trim().split("\n")[0] || null : null;
|
||||
}
|
||||
|
||||
// #1873 harness watch W1: what the watch records for the binary sessions run: the lookup step that
|
||||
// #1873 harness watch: what the watch records for the binary sessions run: the lookup step that
|
||||
// found it (MOSAIC_CLAUDE_BIN or PATH, never a path), the sha256 of its real file, the first line
|
||||
// of --version and the whole --help as a surface. Both run with an empty environment but PATH and
|
||||
// a fresh HOME under scratch, which is removed after. Without scratch they don't run. The real path
|
||||
// is resolved once, then hashed and run, so the digest and the captures are of one file; a relative
|
||||
// PATH entry is skipped, as pi's lookup skips it. Fit stays "unknown": the claude fit rule (every
|
||||
// flag the adapter passes is in --help) is slice W2, with the no-network wrapper for these runs.
|
||||
// PATH entry is skipped, as pi's lookup skips it.
|
||||
//
|
||||
// W2, the fit rule: every flag the adapter passes is in --help, and --version answers. The flags
|
||||
// come from the adapter itself: adapter.sh runs twice against a stand-in claude that writes its
|
||||
// argv (once fresh, which passes --session-id, and once with a session id kept, which passes
|
||||
// --resume), so there is no second list of them here. --help is read on the text the binary
|
||||
// printed, not on the redacted copy the watch stores. A flag counts only from an option line (one
|
||||
// to four spaces, then -), where it is named before the description: a flag a description only
|
||||
// mentions does not count. --x[-y] anywhere in the text gives --x-y when --x has its own option
|
||||
// line (claude 2.1.322 names --system-prompt-file only as --system-prompt[-file] in the --bare
|
||||
// description, beside a --system-prompt option line). A flag missing from it, or --version or
|
||||
// --help exiting non-zero, is fit "fail", definite, with the flag as the rule id. A capture that
|
||||
// timed out, was stopped, could not start or printed too much, or an adapter run that failed, is
|
||||
// "unknown": the check could not tell. The captures run with no network: unshare -r -n when it
|
||||
// runs here (probed per check), else without it, as the pi fit check drops unshare's layer when it
|
||||
// is missing or refused; the line says so. preflight reads the definite verdicts (verdictFor).
|
||||
const CAPTURE_TIMEOUT = 10000;
|
||||
const CAPTURE_MAX = 1024 * 1024;
|
||||
export async function inspect({ env = {}, scratch = null, signal } = {}) {
|
||||
const bin = binary({ ...env, PATH: (env.PATH ?? "").split(delimiter).filter((dir) => isAbsolute(dir)).join(delimiter) });
|
||||
export async function inspect({ env = {}, scratch = null, signal, unshare = UNSHARE } = {}) {
|
||||
const bin = lookup(env);
|
||||
const source = env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN" : bin ? "PATH" : null;
|
||||
const base = { source, fit: "unknown", definite: false, failed: [], surfaces: {} };
|
||||
if (!bin) return { ...base, found: false, version: null, digest: null, line: env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN isn't an executable file at an absolute path" : "no claude on PATH" };
|
||||
let real;
|
||||
try { real = realpathSync(bin); } catch { return { ...base, found: false, version: null, digest: null, line: `the claude from ${source} can't be resolved` }; }
|
||||
const key = verdictKey(real);
|
||||
const digest = await sha256(real);
|
||||
if (typeof scratch !== "string" || !isAbsolute(scratch)) return { ...base, found: true, version: null, digest, line: "no scratch directory, so --version and --help were not run; fit not checked" };
|
||||
const home = mkdtempSync(join(scratch, "claude-home-"));
|
||||
try {
|
||||
const runEnv = { PATH: "/usr/bin:/bin", HOME: home };
|
||||
const [v, help] = [await capture(real, ["--version"], runEnv, signal), await capture(real, ["--help"], runEnv, signal)];
|
||||
const line = v?.split("\n")[0].trim() ?? "";
|
||||
const version = line && line.length <= 128 ? line : null;
|
||||
return { ...base, found: true, version, digest, line: help === null ? "--help gave no answer; fit not checked" : "fit not checked", surfaces: help === null ? {} : { help } };
|
||||
const fence = await networkFence(unshare, runEnv, signal);
|
||||
const wrap = (args) => (fence.argv ? [...fence.argv, real, ...args] : [real, ...args]);
|
||||
const v = await capture(wrap(["--version"]), runEnv, signal);
|
||||
const help = await capture(wrap(["--help"]), runEnv, signal);
|
||||
const flags = await adapterFlags(scratch, signal);
|
||||
const first = v.why === null ? v.out.split("\n")[0].trim() : "";
|
||||
const version = first && first.length <= 128 ? first : null;
|
||||
const verdict = judge({ v, first, help, flags });
|
||||
// Kept when it is definite, by the key taken before the check: if the binary or the adapter
|
||||
// changed during it, preflight looks up the new key and does not find this one.
|
||||
if (verdict.definite && key) remember(key, { fit: verdict.fit, failed: verdict.failed, why: verdict.why });
|
||||
const line = fence.argv ? verdict.why : `${verdict.why}; run with the network on: ${fence.why}`;
|
||||
return { ...base, found: true, version, digest, fit: verdict.fit, definite: verdict.definite, failed: verdict.failed, line, surfaces: help.why === null ? { help: help.out } : {} };
|
||||
} finally {
|
||||
rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The watch's lookup: MOSAIC_CLAUDE_BIN, else the first claude in an absolute PATH entry.
|
||||
const lookup = (env) => binary({ ...env, PATH: (env.PATH ?? "").split(delimiter).filter((dir) => isAbsolute(dir)).join(delimiter) });
|
||||
|
||||
function judge({ v, first, help, flags }) {
|
||||
const unsure = (why) => ({ fit: "unknown", definite: false, failed: [], why: `${why}; fit not checked` });
|
||||
const fail = (failed, why) => ({ fit: "fail", definite: true, failed, why });
|
||||
if (v.why === "exit" || (v.why === null && !first)) return fail(["--version"], "--version gave no answer");
|
||||
if (v.why !== null) return unsure(`--version ${v.why}`);
|
||||
if (help.why === "exit") return fail(["--help"], "--help exited with an error");
|
||||
if (help.why !== null) return unsure(`--help ${help.why}`);
|
||||
if (!flags) return unsure("the adapter's flags could not be read");
|
||||
const have = helpFlags(help.out);
|
||||
const missing = flags.filter((flag) => !have.has(flag));
|
||||
if (missing.length) return fail(missing, failMessage(missing));
|
||||
return { fit: "pass", definite: true, failed: [], why: "every flag the adapter passes is in --help" };
|
||||
}
|
||||
|
||||
const failMessage = (failed) => (failed.length === 1 && (failed[0] === "--version" || failed[0] === "--help") ? `${failed[0]} gave no answer` : `--help lacks ${failed.join(" ")}`);
|
||||
|
||||
// The flags --help names in its option lines: a line that starts with one to four spaces then -,
|
||||
// read up to the first run of two spaces (the option and its aliases, before the description).
|
||||
// Then --x[-y] anywhere in the text gives --x-y when --x is one of them.
|
||||
const OPTION_LINE = /^ {1,4}-/;
|
||||
const HEAD_FLAG = /(?:^|[\s,])(--?[A-Za-z][A-Za-z0-9-]*)(?=$|[\s,=<[])/g;
|
||||
const BRACKET = /(?<![A-Za-z0-9_-])(--?[A-Za-z][A-Za-z0-9-]*)\[(-[A-Za-z0-9-]+)\]/g;
|
||||
export function helpFlags(text) {
|
||||
const all = String(text ?? "");
|
||||
const out = new Set();
|
||||
for (const line of all.split("\n")) {
|
||||
if (!OPTION_LINE.test(line)) continue;
|
||||
const head = line.trim().split(/\s{2,}/)[0];
|
||||
for (const m of head.matchAll(HEAD_FLAG)) out.add(m[1]);
|
||||
}
|
||||
for (const m of all.matchAll(BRACKET)) if (out.has(m[1])) out.add(m[1] + m[2]);
|
||||
return out;
|
||||
}
|
||||
|
||||
// unshare -r -n, when it runs here: { argv } to put before the binary, else { argv: null, why }.
|
||||
async function networkFence(unshare, runEnv, signal) {
|
||||
if (!unshare || !existsSync(unshare)) return { argv: null, why: "unshare is missing" };
|
||||
const argv = [unshare, "-r", "-n", "--"];
|
||||
const r = await capture([...argv, "/usr/bin/true"], runEnv, signal);
|
||||
return r.why === null ? { argv } : { argv: null, why: "unshare failed" };
|
||||
}
|
||||
|
||||
// The flags adapter.sh passes claude, from its own runs against a stand-in that writes its argv.
|
||||
// Kept per stat key of the adapter and its stream reader; null when a run failed.
|
||||
const FLAG = /^--?[A-Za-z]/;
|
||||
const RECORDER = "#!/bin/sh\nfor a in \"$@\"; do printf '%s\\n' \"$a\"; done > \"$FIT_ARGV_OUT\"\necho '{\"type\":\"result\",\"subtype\":\"success\",\"result\":\"ok\"}'\n";
|
||||
let flagsMemo = null;
|
||||
async function adapterFlags(scratch, signal) {
|
||||
const key = statKey([adapter(), STREAM]);
|
||||
if (flagsMemo?.key === key) return flagsMemo.flags;
|
||||
const dir = mkdtempSync(join(scratch, "claude-flags-"));
|
||||
try {
|
||||
const recorder = join(dir, "claude");
|
||||
const file = join(dir, "input.json");
|
||||
const argvFile = join(dir, "argv");
|
||||
writeFileSync(recorder, RECORDER, { mode: 0o700 });
|
||||
writeFileSync(file, "{}\n");
|
||||
mkdirSync(join(dir, "home"));
|
||||
const runEnv = {
|
||||
PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, HOME: join(dir, "home"), MOSAIC_CLAUDE_BIN: recorder, FIT_ARGV_OUT: argvFile,
|
||||
MOSAIC_SYSTEM_PROMPT_FILE: file, MOSAIC_CLAUDE_SETTINGS: file, MOSAIC_CLAUDE_MCP_CONFIG: file, MOSAIC_REQUEST: "fit check",
|
||||
MOSAIC_WORKSPACE: join(dir, "work"), MOSAIC_SESSION_DIR: join(dir, "session"), MOSAIC_MODEL: "fit-model", MOSAIC_THINKING: "low", MOSAIC_TOOLS: "Read",
|
||||
};
|
||||
const flags = new Set();
|
||||
// The first run keeps the session id it named, so the second passes --resume.
|
||||
for (let round = 0; round < 2; round++) {
|
||||
rmSync(argvFile, { force: true });
|
||||
const r = await capture(["/bin/sh", adapter()], runEnv, signal, dir);
|
||||
if (r.why !== null || !existsSync(argvFile)) return null;
|
||||
const argv = readFileSync(argvFile, "utf8").split("\n").filter(Boolean);
|
||||
if (!argv.length) return null;
|
||||
for (const arg of argv) if (FLAG.test(arg)) flags.add(arg);
|
||||
}
|
||||
const list = [...flags];
|
||||
if (key === statKey([adapter(), STREAM])) flagsMemo = { key, flags: list };
|
||||
return list;
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The verdicts of the last checks, by the stat key of the binary's real file and the adapter's
|
||||
// (src/stat-key.mjs), as the watch compares them. Definite ones only, at most VERDICTS_MAX.
|
||||
const VERDICTS_MAX = 16;
|
||||
const verdicts = new Map();
|
||||
const verdictKey = (real) => statKey([real, adapter(), STREAM]);
|
||||
function remember(key, verdict) {
|
||||
verdicts.delete(key);
|
||||
verdicts.set(key, Object.freeze({ ...verdict, failed: Object.freeze([...verdict.failed]) }));
|
||||
while (verdicts.size > VERDICTS_MAX) verdicts.delete(verdicts.keys().next().value);
|
||||
}
|
||||
function verdictFor(bin) {
|
||||
try { return verdicts.get(verdictKey(realpathSync(bin))) ?? null; } catch { return null; }
|
||||
}
|
||||
|
||||
function sha256(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const h = createHash("sha256");
|
||||
@@ -127,21 +266,32 @@ function sha256(file) {
|
||||
});
|
||||
}
|
||||
|
||||
// stdout of bin args when it exits 0 within CAPTURE_TIMEOUT and prints at most CAPTURE_MAX, else null.
|
||||
function capture(bin, args, runEnv, signal) {
|
||||
// Runs argv in its own process group and returns { out, why }: why is null when it exited 0 within
|
||||
// CAPTURE_TIMEOUT and printed at most CAPTURE_MAX, else "exit" (it exited non-zero: an answer),
|
||||
// "timed out", "was stopped", "could not start" or "printed too much" (no answer). A timeout, a
|
||||
// stop or too much output kills the whole group, and so does its end: nothing it left in the
|
||||
// background outlives the capture. An already aborted signal starts nothing.
|
||||
function capture(argv, runEnv, signal, cwd = runEnv.HOME) {
|
||||
return new Promise((resolve) => {
|
||||
if (signal?.aborted) { resolve({ out: "", why: "was stopped" }); return; }
|
||||
let child;
|
||||
try { child = spawn(bin, args, { env: runEnv, cwd: runEnv.HOME, stdio: ["ignore", "pipe", "ignore"] }); } catch { resolve(null); return; }
|
||||
try { child = spawn(argv[0], argv.slice(1), { env: runEnv, cwd, stdio: ["ignore", "pipe", "ignore"], detached: true }); } catch { resolve({ out: "", why: "could not start" }); return; }
|
||||
const out = [];
|
||||
let size = 0, done = false;
|
||||
const finish = (v) => { if (done) return; done = true; clearTimeout(timer); signal?.removeEventListener?.("abort", kill); resolve(v); };
|
||||
const kill = () => { try { child.kill("SIGKILL"); } catch {} finish(null); };
|
||||
const timer = setTimeout(kill, CAPTURE_TIMEOUT);
|
||||
signal?.addEventListener?.("abort", kill, { once: true });
|
||||
if (signal?.aborted) kill();
|
||||
child.stdout.on("data", (c) => { size += c.length; if (size > CAPTURE_MAX) kill(); else out.push(c); });
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (status) => finish(status === 0 ? Buffer.concat(out).toString("utf8") : null));
|
||||
const finish = (why) => { if (done) return; done = true; clearTimeout(timer); signal?.removeEventListener?.("abort", stopped); resolve({ out: why === null ? Buffer.concat(out).toString("utf8") : "", why }); };
|
||||
const kill = (why) => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch { try { child.kill("SIGKILL"); } catch {} }
|
||||
finish(why);
|
||||
};
|
||||
const stopped = () => kill("was stopped");
|
||||
const timer = setTimeout(() => kill("timed out"), CAPTURE_TIMEOUT);
|
||||
signal?.addEventListener?.("abort", stopped, { once: true });
|
||||
child.stdout.on("data", (c) => { size += c.length; if (size > CAPTURE_MAX) kill("printed too much"); else out.push(c); });
|
||||
child.on("error", () => finish("could not start"));
|
||||
child.on("close", (status) => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch {}
|
||||
finish(status === 0 ? null : "exit");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -211,6 +361,15 @@ export function installRoots({ env = {} } = {}) {
|
||||
return roots;
|
||||
}
|
||||
|
||||
// #1873 harness watch W2: the stat key the host's 5-minute pass compares (src/stat-key.mjs): the
|
||||
// binary the watch's lookup finds and its directory, the adapter and its stream reader (the fit
|
||||
// rule reads their flags) and the unslop hook. No binary found is a key too. The key holds paths
|
||||
// and stays in memory.
|
||||
export function watchKey({ env = {} } = {}) {
|
||||
const bin = lookup(env);
|
||||
return statKey([bin, bin ? dirname(bin) : null, adapter(), STREAM, join(REPO, "adapters", "claude"), join(REPO, "tools", "unslop-hook")]);
|
||||
}
|
||||
|
||||
// A turn ends when claude exits; there is no marker.
|
||||
export const turnEnd = () => "exit";
|
||||
export const transcript = ({ runDir, store = null }) => (store ? store.projects : join(runDir, "home", ".claude", "projects"));
|
||||
|
||||
@@ -119,8 +119,10 @@ const cleanLabel = (s) => s.length > 0 && s.length <= VERSION_MAX && !/[\u0000-\
|
||||
// The first line `<bin> --version` prints, or null when it fails, times out or prints something
|
||||
// that is not one short printable line. The environment holds only a PATH that finds node. A
|
||||
// promise: the binary runs as a child process, so the caller's event loop keeps running.
|
||||
export function printedVersion(bin) {
|
||||
if (!bin) return Promise.resolve(null);
|
||||
// #1873 harness watch W2: it runs in its own process group, and a timeout, too much output or the
|
||||
// optional signal aborting kills the whole group, so no child it started outlives it.
|
||||
export function printedVersion(bin, { signal = null } = {}) {
|
||||
if (!bin || signal?.aborted) return Promise.resolve(null);
|
||||
return new Promise((resolve) => {
|
||||
const out = [];
|
||||
let size = 0;
|
||||
@@ -131,18 +133,25 @@ export function printedVersion(bin) {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
signal?.removeEventListener?.("abort", stop);
|
||||
resolve(line);
|
||||
};
|
||||
const kill = () => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch { try { child.kill("SIGKILL"); } catch {} }
|
||||
finish(null);
|
||||
};
|
||||
const stop = () => kill();
|
||||
try {
|
||||
child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"] });
|
||||
child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"], detached: true });
|
||||
} catch {
|
||||
finish(null);
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(() => { try { child.kill("SIGKILL"); } catch {} finish(null); }, 5000);
|
||||
timer = setTimeout(kill, 5000);
|
||||
signal?.addEventListener?.("abort", stop, { once: true });
|
||||
child.stdout.on("data", (chunk) => {
|
||||
size += chunk.length;
|
||||
if (size > VERSION_OUTPUT_MAX) { try { child.kill("SIGKILL"); } catch {} finish(null); } else out.push(chunk);
|
||||
if (size > VERSION_OUTPUT_MAX) kill(); else out.push(chunk);
|
||||
});
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (status) => {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { dirname, join } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { locate, printedVersion } from "./binary.mjs";
|
||||
import { statKey } from "../../src/stat-key.mjs";
|
||||
import { fitVerdict, piFit } from "../../../harness/src/pi-fit.mjs";
|
||||
import { envNames, harnessEnv } from "./session-env.mjs";
|
||||
|
||||
@@ -58,23 +59,22 @@ export async function version({ env = {} } = {}) {
|
||||
// #1873 harness watch W1: what the watch records for the pi binary sessions run. It calls the fit
|
||||
// check exactly as preflight does (same found binary, same levels, this process's environment), so
|
||||
// it shares preflight's cache and its running check: a binary that passed is not checked again.
|
||||
// digest is the fit check's digest of the binary's files; a check that fails gives none, since
|
||||
// pi-fit.mjs exports no digest of its own. A failure whose reason starts "can't be checked" could
|
||||
// not decide, so its fit is "unknown"; any other failure is definite. source names the lookup step,
|
||||
// never a path. scratch is not used: the fit check makes its own scratch directories.
|
||||
// digest is the fit check's digest of the binary's files, on a pass and (W2) on a failure too, null
|
||||
// only when the files can't be read. A failure pi-fit marks undecided could not decide, so its fit
|
||||
// is "unknown"; one marked undecided: false is definite. A failure without the field reads as
|
||||
// undecided, so it can never read as definite. The message is shown, never parsed. source names
|
||||
// the lookup step, never a path. scratch is not used: the fit check makes its own scratch directories.
|
||||
// signal goes to the fit check (W2): when the watch stops, a check only it waits on is stopped.
|
||||
const SOURCES = Object.freeze({ MOSAIC_PI_BIN: "MOSAIC_PI_BIN", "the pi on PATH": "PATH", "the repository's pi": "repo" });
|
||||
export async function inspect({ env = {} } = {}) {
|
||||
// check: the fit check to run, piFit unless a test gives a stand-in for it.
|
||||
export async function inspect({ env = {}, signal = null, check = piFit } = {}) {
|
||||
const at = locate(env);
|
||||
if (!at.bin) return { found: false, source: env.MOSAIC_PI_BIN ? "MOSAIC_PI_BIN" : null, version: null, digest: null, fit: "unknown", definite: false, failed: [], line: at.problem, surfaces: {} };
|
||||
const source = SOURCES[at.label] ?? null;
|
||||
const fit = await piFit(at, { levels: LEVELS });
|
||||
const fit = await check(at, { levels: LEVELS, signal });
|
||||
if (fit.ok) return { found: true, source, version: fit.version ?? null, digest: fit.digest, fit: "pass", definite: true, failed: [], line: `pi${fit.version ? ` ${fit.version}` : ""} (${at.label}) passed the fit check`, surfaces: {} };
|
||||
// Read after pi-fit's "pi <version> (<label>) " prefix, so a version label cannot decide it. A line
|
||||
// without that prefix keeps the whole-line match: an unknown line must not read as definite.
|
||||
const mark = `(${at.label}) `;
|
||||
const cut = fit.message.indexOf(mark);
|
||||
const undecided = cut === -1 ? /can't be checked/.test(fit.message) : fit.message.slice(cut + mark.length).startsWith("can't be checked");
|
||||
return { found: true, source, version: await printedVersion(at.bin), digest: null, fit: undecided ? "unknown" : "fail", definite: !undecided, failed: [], line: fit.message, surfaces: {} };
|
||||
const undecided = fit.undecided !== false;
|
||||
return { found: true, source, version: await printedVersion(at.bin, { signal }), digest: typeof fit.digest === "string" ? fit.digest : null, fit: undecided ? "unknown" : "fail", definite: !undecided, failed: [], line: fit.message, surfaces: {} };
|
||||
}
|
||||
|
||||
// #1873 S3: runs the fit check, or shares the one running, and throws its refusal line when the
|
||||
@@ -176,6 +176,14 @@ export function env({ policy, tools, session, proxy = null, env: hostEnv = proce
|
||||
export { envNames };
|
||||
export const installRoots = ({ env = {} } = {}) => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), found(env).root, join(REPO, "tools", "unslop-hook")];
|
||||
|
||||
// #1873 harness watch W2: the stat key the host's 5-minute pass compares (src/stat-key.mjs): the
|
||||
// binary the lookup finds and the install roots. No binary found is a key too, so finding one
|
||||
// later changes it. The key holds paths and stays in memory.
|
||||
export function watchKey({ env = {} } = {}) {
|
||||
const at = locate(env);
|
||||
return statKey([at.bin ?? null, join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), at.root ?? null, join(REPO, "tools", "unslop-hook")]);
|
||||
}
|
||||
|
||||
// The extension writes the turn marker at agent_end; an exit without it is a failed turn.
|
||||
export const turnEnd = () => "marker";
|
||||
export const transcript = ({ runDir, store = null }) => (store ? store.session : join(runDir, "session"));
|
||||
|
||||
@@ -31,7 +31,8 @@ const FITS = new Set(["pass", "fail", "unknown"]);
|
||||
// An inspect that takes longer than this gives unknown. pi's fit check runs many sandboxed starts.
|
||||
export const INSPECT_TIMEOUT_MS = 300000;
|
||||
const SOURCE = /^[A-Za-z][A-Za-z0-9_]{0,63}$/;
|
||||
const RULE = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,63}$/;
|
||||
// A rule id may be a flag (W2: claude-code's fit rule names the flag --help lacks).
|
||||
const RULE = /^(?:--?)?[A-Za-z0-9][A-Za-z0-9_.:-]{0,63}$/;
|
||||
const SURFACE = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
const DIGEST = /^[a-f0-9]{64}$/;
|
||||
const LABEL_MAX = 128;
|
||||
@@ -43,8 +44,11 @@ const VALUE_MIN = 6;
|
||||
const CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u2028\u2029]/g;
|
||||
|
||||
// An absolute path of two segments or more that no name replaced. Not one that follows a word,
|
||||
// a colon, a slash, a dot, a dash, a tilde or a name's closing ">" (URLs, ~/x, <home>/x, a/b/c).
|
||||
const LOOSE_PATH = /(?<![\w:/.~>-])\/[^\s/'"`<>()[\]{},;]+(?:\/[^\s'"`<>()[\]{},;]*)+/g;
|
||||
// a slash, a dot, a dash, a tilde or a name's closing ">" (https://x/y, ~/x, <home>/x, a/b/c); one
|
||||
// after a colon is a path (host:/srv/a/b). W2 review P3-5: a file: URL and a ~/ path are paths too.
|
||||
const LOOSE_PATH = /(?<![\w/.~>-])\/[^\s/'"`<>()[\]{},;]+(?:\/[^\s'"`<>()[\]{},;]*)+/g;
|
||||
const FILE_URL = /(?<![A-Za-z0-9_])file:\/\/[^\s'"`<>()[\]{},;]*/gi;
|
||||
const TILDE_PATH = /(?<![\w/.~-])~[A-Za-z0-9._-]*\/[^\s'"`<>()[\]{},;]*/g;
|
||||
|
||||
// One pass. last: the records a previous pass returned (an array; others are ignored). env: the
|
||||
// environment of the process sessions launch from, which the modules look up their binary in.
|
||||
@@ -52,7 +56,9 @@ const LOOSE_PATH = /(?<![\w:/.~>-])\/[^\s/'"`<>()[\]{},;]+(?:\/[^\s'"`<>()[\]{},
|
||||
// refused, gone }: one record per harness the loader gives, a transition for each harness whose
|
||||
// state or digest is not the last record's (or that has no last record), the loader's refusals
|
||||
// as it reports them, and the names of last records whose harness the loader no longer gives.
|
||||
export async function watchHarnesses({ last = [], env = process.env, scratch = null, dataRoot = null, timeoutMs = INSPECT_TIMEOUT_MS, now = () => new Date() } = {}) {
|
||||
// signal (W2): when it aborts, the inspect running is aborted too (its own signal) and no further
|
||||
// module is asked; the result then carries stopped: true and holds only the records made before.
|
||||
export async function watchHarnesses({ last = [], env = process.env, scratch = null, dataRoot = null, timeoutMs = INSPECT_TIMEOUT_MS, now = () => new Date(), signal = null } = {}) {
|
||||
const before = new Map();
|
||||
for (const r of Array.isArray(last) ? last : []) if (r && typeof r.harness === "string") before.set(r.harness, r);
|
||||
const loaded = await loadInstalled("harness", { dataRoot });
|
||||
@@ -63,7 +69,9 @@ export async function watchHarnesses({ last = [], env = process.env, scratch = n
|
||||
const records = [];
|
||||
const transitions = [];
|
||||
for (const { name, module } of modules) {
|
||||
const answer = await ask(module, { env, scratch, timeoutMs });
|
||||
if (signal?.aborted) break;
|
||||
const answer = await ask(module, { env, scratch, timeoutMs, signal });
|
||||
if (signal?.aborted) break;
|
||||
const at = now().toISOString();
|
||||
const prior = before.get(name) ?? null;
|
||||
const record = { harness: name, ...settle(clean(answer, redact, env), prior), checkedAt: at };
|
||||
@@ -74,7 +82,8 @@ export async function watchHarnesses({ last = [], env = process.env, scratch = n
|
||||
if (moved) transitions.push({ harness: name, at, from: prior ? brief(prior) : null, to: brief(record), failed: record.failed, line: record.line });
|
||||
}
|
||||
const seen = new Set(records.map((r) => r.harness));
|
||||
return { records, transitions, refused, gone: [...before.keys()].filter((n) => !seen.has(n)).sort() };
|
||||
const stopped = Boolean(signal?.aborted);
|
||||
return { records, transitions, refused, gone: stopped ? [] : [...before.keys()].filter((n) => !seen.has(n)).sort(), ...(stopped ? { stopped } : {}) };
|
||||
}
|
||||
|
||||
// The latest digest the last record knew: its own, or the one it carried forward.
|
||||
@@ -92,11 +101,9 @@ function sameBinary(prior, record) {
|
||||
const brief = (r) => ({ state: STATES.includes(r.state) ? r.state : "unknown", version: typeof r.version === "string" ? r.version : null, digest: typeof r.digest === "string" ? r.digest : null });
|
||||
|
||||
// The module's answer, or { error } when it threw or ran past timeoutMs. via says which export.
|
||||
async function ask(module, { env, scratch, timeoutMs }) {
|
||||
async function ask(module, { env, scratch, timeoutMs, signal = null }) {
|
||||
const via = typeof module.inspect === "function" ? "inspect" : "version";
|
||||
const stop = new AbortController();
|
||||
let timer = null;
|
||||
const late = new Promise((resolve) => { timer = setTimeout(() => { stop.abort(); resolve({ via, error: "timeout" }); }, timeoutMs); });
|
||||
const asked = (async () => {
|
||||
try {
|
||||
// The answer is copied here, inside the try, to plain own data: a getter, a Proxy or an
|
||||
@@ -109,12 +116,28 @@ async function ask(module, { env, scratch, timeoutMs }) {
|
||||
return { via, error: "threw" };
|
||||
}
|
||||
})();
|
||||
let timer = null, grace = null, cut = null;
|
||||
const late = new Promise((resolve) => {
|
||||
timer = setTimeout(() => { stop.abort(); resolve({ via, error: "timeout" }); }, timeoutMs);
|
||||
// A stop from the caller aborts the inspect, then waits for it to end (what it started is
|
||||
// stopped by then), up to STOP_GRACE_MS for one that ignores its signal.
|
||||
cut = () => {
|
||||
stop.abort();
|
||||
clearTimeout(timer);
|
||||
Promise.race([asked, new Promise((r) => { grace = setTimeout(r, STOP_GRACE_MS); })]).then(() => resolve({ via, error: "stopped" }));
|
||||
};
|
||||
if (signal?.aborted) cut();
|
||||
else signal?.addEventListener?.("abort", cut, { once: true });
|
||||
});
|
||||
try {
|
||||
return await Promise.race([asked, late]);
|
||||
return signal?.aborted ? await late : await Promise.race([asked, late]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
clearTimeout(grace);
|
||||
signal?.removeEventListener?.("abort", cut);
|
||||
}
|
||||
}
|
||||
const STOP_GRACE_MS = 5000;
|
||||
|
||||
// The contract's fields of an answer as own data properties, copied to a plain object whose
|
||||
// values are strings, booleans, null, an array of strings or a map of strings. A getter is not
|
||||
@@ -249,6 +272,6 @@ function redactor(env, scratch) {
|
||||
let out = s;
|
||||
for (const value of order) if (out.includes(value)) out = out.split(value).join(pairs.get(value));
|
||||
if (word) out = out.replace(word, "<user>");
|
||||
return out.replace(LOOSE_PATH, "<path>");
|
||||
return out.replace(FILE_URL, "<path>").replace(TILDE_PATH, "<path>").replace(LOOSE_PATH, "<path>");
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// #1873 harness watch W2 (reports/harness-watch-design.md section 1, "How often"): the stat key of
|
||||
// a list of files and directories. Per path: its real path, dev, ino, size, mtime and ctime. The
|
||||
// real path catches a symlink swap, ctime a copy that keeps mtime. A path that can't be resolved or
|
||||
// stat'ed gives its error code in place, so a file that goes or comes back changes the key too.
|
||||
// The key is compared in memory only. It holds host paths, so it is never written to a file or a log.
|
||||
import { realpathSync, statSync } from "node:fs";
|
||||
|
||||
export function statKey(paths) {
|
||||
return JSON.stringify((Array.isArray(paths) ? paths : []).map((p) => {
|
||||
if (typeof p !== "string" || !p) return null;
|
||||
try {
|
||||
const real = realpathSync(p);
|
||||
const s = statSync(real);
|
||||
return [real, s.dev, s.ino, s.size, s.mtimeMs, s.ctimeMs];
|
||||
} catch (e) {
|
||||
return [p, typeof e?.code === "string" ? e.code : "error"];
|
||||
}
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// #1873 harness watch W2: a harness module asks the bus host's watch for a stat pass now, instead
|
||||
// of at the next five-minute tick. claude-code's preflight asks when it has no verdict for the
|
||||
// binary it found (a binary or adapter replaced, touched or moved since the last check, or a host
|
||||
// that just started). The host's watch (packages/cli/src/harness-watch-host.mjs) listens in the
|
||||
// same process; with no listener (a Console, a test, a launch outside a bus host) the request does
|
||||
// nothing. A request carries nothing: the watch compares its own stat key and runs a full pass
|
||||
// only when it changed, so a flood of requests costs one stat pass each and no extra full pass.
|
||||
const listeners = new Set();
|
||||
|
||||
// Calls fn on each request until the returned function is called.
|
||||
export function onWatchRequest(fn) {
|
||||
listeners.add(fn);
|
||||
return () => { listeners.delete(fn); };
|
||||
}
|
||||
|
||||
export function requestWatch() {
|
||||
for (const fn of [...listeners]) {
|
||||
try { fn(); } catch {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,398 @@
|
||||
// #1873 harness watch, slice W2 (reports/harness-watch-design.md sections 1 and 5): the claude-code
|
||||
// fit rule (every flag the adapter passes is in --help, and --version answers), the verdict
|
||||
// preflight reads, the no-network captures, the watch's outer signal and flag rule ids, and pi's
|
||||
// digest and undecided fields.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { tree } from "./helpers.mjs";
|
||||
import { KINDS } from "../src/kinds.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
import { piFit } from "../../harness/src/pi-fit.mjs";
|
||||
import { sandboxProblem, UNSHARE } from "../../seat/src/sandbox.mjs";
|
||||
import * as pi from "../harness/pi/index.mjs";
|
||||
import { locate, printedVersion } from "../harness/pi/binary.mjs";
|
||||
import * as claude from "../harness/claude-code/index.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const WATCH_SRC = join(REPO, "packages", "plugins", "src", "harness-watch.mjs");
|
||||
const PI_NAME = "@earendil-works/pi-coding-agent";
|
||||
const NO_BWRAP = sandboxProblem() ? { skip: "no working bwrap here" } : {};
|
||||
const CLAUDE = process.env.MOSAIC_CLAUDE_BIN;
|
||||
const CREDENTIAL = { ANTHROPIC_API_KEY: "fixture-api-key" };
|
||||
const DIGEST = /^[a-f0-9]{64}$/;
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-watch-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// The help a claude that has every flag the adapter passes would print (a test fixture; the module
|
||||
// itself reads the flags from adapter.sh). --system-prompt-file shows only as --system-prompt[-file],
|
||||
// as on claude 2.1.322. Descriptions also mention flags (--settings, --mcp-config, --model), as
|
||||
// claude's do; only the option lines count.
|
||||
const HELP_LINES = [
|
||||
"Usage: claude [options] [command] [prompt]", "", "Options:",
|
||||
" -p, --print Print response and exit",
|
||||
" --output-format <format> text, json or stream-json",
|
||||
" --verbose Verbose output",
|
||||
" --include-partial-messages Partial message chunks",
|
||||
" --bare Minimal mode: no --settings files, no --mcp-config servers,",
|
||||
" --settings and --model still apply",
|
||||
" --system-prompt <prompt> System prompt; also --system-prompt[-file]",
|
||||
" --model <model> Model",
|
||||
" --effort <level> Effort",
|
||||
" --restricted Restricted mode",
|
||||
" --tools <tools...> Built-in tools",
|
||||
" --allowedTools, --allowed-tools <tools...> Allowed tools",
|
||||
" --permission-mode <mode> Permission mode",
|
||||
" --settings <file-or-json> Settings",
|
||||
" --strict-mcp-config Only --mcp-config servers",
|
||||
" --mcp-config <configs...> MCP servers",
|
||||
" --disable-slash-commands No skills",
|
||||
" -r, --resume [value] Resume a session",
|
||||
" --session-id <uuid> Session id",
|
||||
" -v, --version Version",
|
||||
" -h, --help Help",
|
||||
];
|
||||
// drop removes the option line that names a flag (its head, not a mention in a description).
|
||||
const help = ({ drop = [], extra = [] } = {}) => [...HELP_LINES.filter((l) => !drop.some((d) => new RegExp(`^ (?:-[A-Za-z], )?${d}(?=[ ,<\\[]|$)`).test(l))), ...extra].join("\n");
|
||||
|
||||
// A stand-in claude: --version prints version (or exits versionExit), --help prints text (or exits
|
||||
// helpExit, or sleeps with helpHang), anything else exits 3. With nsFile it writes the network
|
||||
// namespace it runs in there first.
|
||||
function standIn(dir, { name = "claude", version = "9.9.9 (stand-in)", versionExit = null, helpText = help(), helpExit = null, helpHang = false, nsFile = null } = {}) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
const bin = join(dir, name);
|
||||
writeFileSync(bin, [
|
||||
"#!/bin/sh",
|
||||
nsFile ? `readlink /proc/self/ns/net >> '${nsFile}'` : "",
|
||||
'case "$1" in',
|
||||
`--version) ${versionExit !== null ? `exit ${versionExit}` : `echo '${version}'`};;`,
|
||||
`--help) ${helpExit !== null ? `exit ${helpExit}` : helpHang ? "exec sleep 30" : `cat <<'EOF'\n${helpText}\nEOF\n`};;`,
|
||||
"*) exit 3;;",
|
||||
"esac", "",
|
||||
].join("\n"), { mode: 0o755 });
|
||||
return bin;
|
||||
}
|
||||
|
||||
test("W2 claude-code: a stand-in whose --help lacks an adapter flag is red, and preflight refuses harness-feature-missing", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = standIn(join(dir, "bin"), { helpText: help({ drop: ["--strict-mcp-config"] }) });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, PATH: "/usr/bin:/bin" };
|
||||
// Before any check, preflight has no verdict and does not refuse.
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const r = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.failed, r.version], [true, "fail", true, ["--strict-mcp-config"], "9.9.9 (stand-in)"]);
|
||||
assert.match(r.line, /--help lacks --strict-mcp-config/);
|
||||
const refusal = claude.preflight({ env: { ...env, ...CREDENTIAL } });
|
||||
assert.equal(refusal?.code, "harness-feature-missing");
|
||||
assert.match(refusal.message, /MOSAIC_CLAUDE_BIN/);
|
||||
assert.match(refusal.message, /--strict-mcp-config/);
|
||||
assert.ok(!refusal.message.includes(dir), "the refusal names the variable, not the path");
|
||||
// The same file through PATH is the same verdict, named by its lookup step.
|
||||
const viaPath = claude.preflight({ env: { PATH: join(dir, "bin"), ...CREDENTIAL } });
|
||||
assert.equal(viaPath?.code, "harness-feature-missing");
|
||||
assert.match(viaPath.message, /the claude from PATH/);
|
||||
// The watch keeps the flag as the rule id (W1's rule ids now take a leading dash or two).
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env: { ...env, MOSAIC_PI_BIN: join(dir, "no-pi") }, scratch: scratch(t) });
|
||||
const rec = out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.deepEqual([rec.state, rec.failed], ["red", ["--strict-mcp-config"]]);
|
||||
// The binary replaced in place by one with every flag: its stat key changed, so the old verdict
|
||||
// no longer refuses, before any new check.
|
||||
standIn(join(dir, "bin"), { helpText: help() });
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const again = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([again.fit, again.definite, again.failed], ["pass", true, []], again.line);
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
});
|
||||
|
||||
test("W2 claude-code: --resume, which only a later turn passes, is checked too, and --x[-y] in help counts as --x-y", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(dir, { helpText: help({ drop: ["--resume"] }) }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.failed], ["fail", ["--resume"]]);
|
||||
const prose = help({ drop: ["--system-prompt"], extra: [" --append-system-prompt <prompt> Append; see --system-prompt-files"] });
|
||||
const s = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "b"), { helpText: prose }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([s.fit, s.failed], ["fail", ["--system-prompt-file"]], "a longer token or a token inside a word is not the flag");
|
||||
// Only option lines count; --x[-y] in prose gives --x-y only beside an --x option line.
|
||||
assert.deepEqual([...claude.helpFlags("use -p/--print or --system-prompt[-file]; non-interactive")], []);
|
||||
assert.deepEqual([...claude.helpFlags(" -c, --continue Continue\n --system-prompt <p> Prompt\n see --system-prompt[-file], --zz[-q]\n --settings in prose")].sort(), ["--continue", "--system-prompt", "--system-prompt-file", "-c"]);
|
||||
});
|
||||
|
||||
// Review P2-3: a flag a description mentions is not an option. A claude whose help lost the
|
||||
// --settings and --mcp-config option lines, though other descriptions still name both, is red.
|
||||
test("W2 claude-code: a flag only a description mentions does not count", async (t) => {
|
||||
const text = help({ drop: ["--settings", "--mcp-config"] });
|
||||
assert.match(text, /--settings/);
|
||||
assert.match(text, /--mcp-config/);
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(scratch(t), { helpText: text }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, [...r.failed].sort()], ["fail", true, ["--mcp-config", "--settings"]], r.line);
|
||||
});
|
||||
|
||||
test("W2 claude-code: the flags come from adapter.sh, not a list in the module", async (t) => {
|
||||
// A copy of the module beside an adapter that passes one more flag; the rest of the repository
|
||||
// is linked. The same stand-in passes with the real adapter and fails with the copy.
|
||||
const root = scratch(t);
|
||||
for (const p of ["packages/harness", "packages/seat", "packages/plugins/src", "tools", "node_modules"]) {
|
||||
mkdirSync(join(root, p, ".."), { recursive: true });
|
||||
symlinkSync(join(REPO, p), join(root, p));
|
||||
}
|
||||
mkdirSync(join(root, "packages", "plugins", "harness", "claude-code"), { recursive: true });
|
||||
cpSync(join(REPO, "packages", "plugins", "harness", "claude-code", "index.mjs"), join(root, "packages", "plugins", "harness", "claude-code", "index.mjs"));
|
||||
mkdirSync(join(root, "adapters", "claude"), { recursive: true });
|
||||
cpSync(join(REPO, "adapters", "claude", "stream.mjs"), join(root, "adapters", "claude", "stream.mjs"));
|
||||
const adapter = readFileSync(join(REPO, "adapters", "claude", "adapter.sh"), "utf8");
|
||||
assert.ok(adapter.includes(" --disable-slash-commands \\\n"));
|
||||
writeFileSync(join(root, "adapters", "claude", "adapter.sh"), adapter.replace(" --disable-slash-commands \\\n", " --disable-slash-commands \\\n --fit-probe-flag \\\n"), { mode: 0o755 });
|
||||
const copy = await import(pathToFileURL(join(root, "packages", "plugins", "harness", "claude-code", "index.mjs")).href);
|
||||
const bin = standIn(join(root, "bin"));
|
||||
const real = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.deepEqual([real.fit, real.failed], ["pass", []], real.line);
|
||||
const changed = await copy.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.deepEqual([changed.fit, changed.definite, changed.failed], ["fail", true, ["--fit-probe-flag"]], changed.line);
|
||||
});
|
||||
|
||||
test("W2 claude-code: --version or --help exiting non-zero is red; a check that is stopped is unknown and preflight does not refuse", async (t) => {
|
||||
const dir = scratch(t);
|
||||
let r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "v"), { versionExit: 2 }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed, r.version], ["fail", true, ["--version"], null]);
|
||||
assert.equal(claude.preflight({ env: { MOSAIC_CLAUDE_BIN: join(dir, "v", "claude"), ...CREDENTIAL } })?.code, "harness-feature-missing");
|
||||
r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "h"), { helpExit: 1 }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed], ["fail", true, ["--help"]]);
|
||||
const hang = standIn(join(dir, "s"), { helpHang: true });
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 500);
|
||||
r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: hang }, scratch: scratch(t), signal: stop.signal });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed], ["unknown", false, []]);
|
||||
assert.match(r.line, /--help was stopped; fit not checked/);
|
||||
assert.equal(claude.preflight({ env: { MOSAIC_CLAUDE_BIN: hang, ...CREDENTIAL } }), null, "an undecided check does not refuse in this slice");
|
||||
});
|
||||
|
||||
const unshareRuns = existsSync(UNSHARE) && spawnSync(UNSHARE, ["-r", "-n", "--", "/usr/bin/true"]).status === 0;
|
||||
test("W2 claude-code: --version and --help run in their own network namespace when unshare runs; without unshare they run and the line says so", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const host = readlinkSync("/proc/self/ns/net");
|
||||
const nsFile = join(dir, "ns");
|
||||
const bin = standIn(join(dir, "bin"), { nsFile });
|
||||
if (unshareRuns) {
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.equal(r.fit, "pass", r.line);
|
||||
const seen = readFileSync(nsFile, "utf8").trim().split("\n");
|
||||
assert.equal(seen.length, 2);
|
||||
for (const ns of seen) assert.notEqual(ns, host, "no network: not the host's namespace");
|
||||
assert.doesNotMatch(r.line, /network/);
|
||||
} else t.diagnostic("unshare -r -n does not run here; only the fallback is checked");
|
||||
rmSync(nsFile, { force: true });
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t), unshare: join(dir, "no-unshare") });
|
||||
assert.equal(r.fit, "pass");
|
||||
assert.match(r.line, /run with the network on: unshare is missing$/);
|
||||
assert.deepEqual(readFileSync(nsFile, "utf8").trim().split("\n"), [host, host]);
|
||||
});
|
||||
|
||||
test("W2 the real claude named by MOSAIC_CLAUDE_BIN is fit, and preflight lets it through", CLAUDE ? {} : { skip: "MOSAIC_CLAUDE_BIN is not set" }, async (t) => {
|
||||
const env = { MOSAIC_CLAUDE_BIN: CLAUDE, PATH: "/usr/bin:/bin" };
|
||||
const r = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.failed], [true, "pass", true, []], r.line);
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env: { ...env, MOSAIC_PI_BIN: "/nonexistent/pi" }, scratch: scratch(t) });
|
||||
assert.equal(out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN").state, "fit");
|
||||
});
|
||||
|
||||
test("W2 claude-code watchKey changes when the binary is replaced, and holds no answer of its own", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = standIn(dir);
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin };
|
||||
const a = claude.watchKey({ env });
|
||||
assert.equal(claude.watchKey({ env }), a);
|
||||
standIn(dir, { version: "9.9.10 (stand-in)" });
|
||||
assert.notEqual(claude.watchKey({ env }), a);
|
||||
assert.notEqual(claude.watchKey({ env: {} }), a);
|
||||
});
|
||||
|
||||
// A stand-in pi package named by MOSAIC_PI_BIN, as pi-fit.test.mjs makes one.
|
||||
function piPackage(t, knobs) {
|
||||
const root = scratch(t);
|
||||
const pkg = join(root, "prefix", "lib", "node_modules", PI_NAME);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: PI_NAME, version: "2.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ version: "2.0.0", ...knobs }), { mode: 0o755 });
|
||||
mkdirSync(join(root, "prefix", "bin"));
|
||||
symlinkSync(`../lib/node_modules/${PI_NAME}/dist/bundle/cli.js`, join(root, "prefix", "bin", "pi"));
|
||||
return { root, env: { MOSAIC_PI_BIN: join(root, "prefix", "bin", "pi"), PATH: "/usr/bin:/bin" } };
|
||||
}
|
||||
|
||||
test("W2 a failed pi check carries the digest of the files it ran, and says it was definite", NO_BWRAP, async (t) => {
|
||||
const lacking = piPackage(t, { dropFlags: ["--no-skills"] });
|
||||
const found = locate(lacking.env);
|
||||
const fit = await piFit(found, { levels: pi.levels() });
|
||||
assert.deepEqual([fit.ok, fit.undecided], [false, false], fit.message);
|
||||
assert.match(fit.digest, DIGEST);
|
||||
const r = await pi.inspect({ env: lacking.env });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["fail", true, fit.digest]);
|
||||
// Another file under the package is another digest.
|
||||
writeFileSync(join(lacking.root, "prefix", "lib", "node_modules", PI_NAME, "dist", "bundle", "extra.js"), "// changed\n");
|
||||
const later = await piFit(found, { levels: pi.levels() });
|
||||
assert.match(later.digest, DIGEST);
|
||||
assert.notEqual(later.digest, fit.digest);
|
||||
});
|
||||
|
||||
test("W2 an undecided pi check is unknown from its undecided field; pi's inspect matches no message text", NO_BWRAP, async (t) => {
|
||||
const flood = piPackage(t, { flood: true });
|
||||
const fit = await piFit(locate(flood.env), { levels: pi.levels() });
|
||||
assert.deepEqual([fit.ok, fit.undecided], [false, true], fit.message);
|
||||
assert.match(fit.digest, DIGEST);
|
||||
const r = await pi.inspect({ env: flood.env });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["unknown", false, fit.digest]);
|
||||
const src = readFileSync(join(REPO, "packages", "plugins", "harness", "pi", "index.mjs"), "utf8");
|
||||
assert.doesNotMatch(src, /can't be checked/, "inspect reads fit.undecided, not the message");
|
||||
// A stopped check is undecided too, and its processes are gone with it.
|
||||
const slow = piPackage(t, { versionHang: true });
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 300);
|
||||
const began = Date.now();
|
||||
const stopped = await pi.inspect({ env: slow.env, signal: stop.signal });
|
||||
assert.ok(Date.now() - began < 4000, "the stop ended the check, and its --version capture with it");
|
||||
assert.deepEqual([stopped.fit, stopped.definite], ["unknown", false], stopped.line);
|
||||
});
|
||||
|
||||
// A scratch plugin tree of stand-in harnesses: each inspect writes <MARKS>/<name> when it is
|
||||
// asked, then answers its entry of the JSON file WATCH_ANSWER names; "hang-signal" waits for its
|
||||
// signal and writes <MARKS>/<name>.aborted when it aborts; "slow-stop" ends 300 ms after its signal
|
||||
// aborts and writes <MARKS>/<name>.ended then.
|
||||
const marker = (name) => `import { readFileSync, writeFileSync } from "node:fs";
|
||||
export const interfaceVersion = ${KINDS.harness.interfaceVersion};
|
||||
${KINDS.harness.exports.map((e) => `export function ${e}() { return null; }`).join("\n")}
|
||||
export async function inspect({ env, signal }) {
|
||||
writeFileSync(env.MARKS + "/${name}", "1");
|
||||
const a = JSON.parse(readFileSync(env.WATCH_ANSWER, "utf8"))[${JSON.stringify(name)}];
|
||||
if (a === "hang-signal") return new Promise(() => { signal.addEventListener("abort", () => writeFileSync(env.MARKS + "/${name}.aborted", "1")); });
|
||||
if (a === "slow-stop") return new Promise((done) => { signal.addEventListener("abort", () => setTimeout(() => { writeFileSync(env.MARKS + "/${name}.ended", "1"); done({ found: true }); }, 300)); });
|
||||
return a;
|
||||
}
|
||||
`;
|
||||
async function markerTree(t, names) {
|
||||
const tr = tree(t);
|
||||
for (const name of names) tr.write("harness", name, marker(name));
|
||||
const { watchHarnesses } = await import(pathToFileURL(join(tr.pkg, "src", "harness-watch.mjs")).href);
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "marks"));
|
||||
return { watchHarnesses, dir, env: { WATCH_ANSWER: join(dir, "answer.json"), MARKS: join(dir, "marks") } };
|
||||
}
|
||||
|
||||
test("W2 the watch's signal aborts the inspect it is in and asks no further module", async (t) => {
|
||||
const w = await markerTree(t, ["a-first", "b-second"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ "a-first": "hang-signal", "b-second": { found: true, fit: "pass", definite: true } }));
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 200);
|
||||
const began = Date.now();
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: stop.signal, timeoutMs: 60000 });
|
||||
assert.ok(Date.now() - began < 10000);
|
||||
assert.equal(out.stopped, true);
|
||||
assert.deepEqual(out.records, []);
|
||||
assert.deepEqual(readdirSync(w.env.MARKS).sort(), ["a-first", "a-first.aborted"], "the running inspect saw the abort; the next was not asked");
|
||||
// An already aborted signal asks none.
|
||||
rmSync(w.env.MARKS, { recursive: true });
|
||||
mkdirSync(w.env.MARKS);
|
||||
const none = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: AbortSignal.abort() });
|
||||
assert.deepEqual([none.stopped, readdirSync(w.env.MARKS)], [true, []]);
|
||||
});
|
||||
|
||||
test("W2 the watch keeps flag rule ids and still drops ones that are not names", async (t) => {
|
||||
const w = await markerTree(t, ["flags"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ flags: { found: true, fit: "fail", definite: true, failed: ["--strict-mcp-config", "-p", "C1", "---x", "--", "-", "--a b", "/tmp/x"] } }));
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir });
|
||||
assert.deepEqual(out.records[0].failed, ["--strict-mcp-config", "-p", "C1"]);
|
||||
assert.equal(out.records[0].state, "red");
|
||||
});
|
||||
|
||||
// Review P3-6: a stop waits for the inspect it aborted to end (up to the grace), so what it started
|
||||
// is gone when the watch returns.
|
||||
test("W2 a stopped watch returns after the inspect it aborted has ended", async (t) => {
|
||||
const w = await markerTree(t, ["slow"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ slow: "slow-stop" }));
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 100);
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: stop.signal, timeoutMs: 60000 });
|
||||
assert.equal(out.stopped, true);
|
||||
assert.ok(existsSync(join(w.env.MARKS, "slow.ended")), "the watch waited for the inspect to end");
|
||||
});
|
||||
|
||||
const alive = (pid) => { try { process.kill(pid, 0); return true; } catch { return false; } };
|
||||
const until = async (fn, ms = 3000) => { const end = Date.now() + ms; while (!fn() && Date.now() < end) await new Promise((r) => setTimeout(r, 25)); return fn(); };
|
||||
|
||||
// Review P3-6: a capture's whole process group goes, on a stop and when it ends: a stand-in that
|
||||
// leaves a child in the background leaves nothing.
|
||||
test("W2 claude-code: a capture stops what the binary left in the background, on a stop and when it ends", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = join(dir, "claude");
|
||||
const left = join(dir, "left"), held = join(dir, "held");
|
||||
writeFileSync(bin, [
|
||||
"#!/bin/sh", 'case "$1" in',
|
||||
`--version) sleep 30 >/dev/null 2>&1 & echo $! > '${left}'; echo '9.9.9';;`,
|
||||
`--help) sleep 30 2>/dev/null & echo $! > '${held}' 2>/dev/null; wait;;`,
|
||||
"*) exit 3;;", "esac", "",
|
||||
].join("\n"), { mode: 0o755 });
|
||||
const stop = new AbortController();
|
||||
const r = claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t), signal: stop.signal });
|
||||
assert.ok(await until(() => existsSync(held) && readFileSync(held, "utf8").trim() !== "", 15000), "--help started");
|
||||
stop.abort();
|
||||
const out = await r;
|
||||
assert.match(out.line, /--help was stopped/);
|
||||
const pids = [left, held].map((f) => Number(readFileSync(f, "utf8").trim()));
|
||||
for (const pid of pids) assert.ok(await until(() => !alive(pid)), `process ${pid} left by a capture is gone`);
|
||||
});
|
||||
|
||||
// Re-check P3-3: pi's version capture kills its group on a stop too. The background child keeps
|
||||
// the stdout pipe open, so killing the parent alone would leave it running.
|
||||
test("W2 pi: a stopped version capture stops what the binary left in the background", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = join(dir, "pi"), held = join(dir, "held");
|
||||
writeFileSync(bin, `#!/bin/sh\nsleep 30 2>/dev/null & echo $! > '${held}' 2>/dev/null\nwait\n`, { mode: 0o755 });
|
||||
const stop = new AbortController();
|
||||
const r = printedVersion(bin, { signal: stop.signal });
|
||||
assert.ok(await until(() => existsSync(held) && readFileSync(held, "utf8").trim() !== "", 15000), "--version started");
|
||||
stop.abort();
|
||||
assert.equal(await r, null);
|
||||
const pid = Number(readFileSync(held, "utf8").trim());
|
||||
assert.ok(await until(() => !alive(pid)), `process ${pid} left by the capture is gone`);
|
||||
});
|
||||
|
||||
// Review P3-6: an already aborted signal starts nothing and decides nothing.
|
||||
test("W2 claude-code: an inspect with an already aborted signal runs nothing and is unknown", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const nsFile = join(dir, "ran");
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "bin"), { nsFile }) }, scratch: scratch(t), signal: AbortSignal.abort() });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false], r.line);
|
||||
assert.equal(existsSync(nsFile), false, "the binary was not run");
|
||||
});
|
||||
|
||||
// Review P3-6: at most 16 verdicts are kept; the oldest goes first.
|
||||
test("W2 claude-code: preflight's verdicts are capped, oldest first", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bins = [];
|
||||
for (let i = 0; i < 17; i++) bins.push(standIn(join(dir, `b${i}`), { helpText: help({ drop: ["--restricted"] }) }));
|
||||
for (const bin of bins) assert.equal((await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) })).fit, "fail");
|
||||
const refused = (bin) => claude.preflight({ env: { MOSAIC_CLAUDE_BIN: bin, ...CREDENTIAL } })?.code ?? null;
|
||||
assert.equal(refused(bins[0]), null, "the first verdict was dropped");
|
||||
for (const bin of bins.slice(1)) assert.equal(refused(bin), "harness-feature-missing");
|
||||
});
|
||||
|
||||
// Review P3-6: pi's inspect decides from undecided alone, whatever the message says.
|
||||
test("W2 pi's inspect reads undecided, not the message", async (t) => {
|
||||
const stand = piPackage(t, {});
|
||||
const digest = "b".repeat(64);
|
||||
const run = (fit) => pi.inspect({ env: stand.env, check: async () => fit });
|
||||
let r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: words", digest, undecided: false });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["fail", true, digest]);
|
||||
r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) lacks --no-skills", digest, undecided: true });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false]);
|
||||
r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) lacks --no-skills" });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["unknown", false, null], "a check that gives no undecided field is undecided");
|
||||
});
|
||||
@@ -229,7 +229,10 @@ test("W1 redaction covers real paths, trailing slashes, the user name, rule ids,
|
||||
bin: [`bin ${realBin}/claude`, "bin <PATH entry>/claude", dir],
|
||||
user: ["built by jq on host", "built by <user> on host", dir],
|
||||
loose: ["at /opt/notinenv/bin/tool and /etc/passwd", "at <path> and <path>", dir],
|
||||
kept: ["see https://example.com/a/b, ~/x/y and /model", "see https://example.com/a/b, ~/x/y and /model", dir],
|
||||
// W2 review P3-5: a ~/ path, a file: URL and a path after a colon are paths now; a URL with a
|
||||
// host and a one-segment path stay.
|
||||
kept: ["see https://example.com/a/b, ~/x/y and /model", "see https://example.com/a/b, <path> and /model", dir],
|
||||
forms: ["file:///opt/fixture/bin host:/srv/fixture/b ~me/.local/x", "<path> host:<path> <path>", dir],
|
||||
};
|
||||
const { watchHarnesses } = await import(pathToFileURL(join(tr.pkg, "src", "harness-watch.mjs")).href);
|
||||
for (const [what, [line, want, at]] of Object.entries(lines)) {
|
||||
@@ -318,7 +321,9 @@ test("W1 pi inspect shares preflight's fit check: a binary that passed is not ch
|
||||
test("W1 pi inspect: a definite failure is fail, one that can't be checked is unknown, no binary is not found", NO_BWRAP, async (t) => {
|
||||
const lacking = piPackage(t, { dropFlags: ["--no-skills"] });
|
||||
let r = await pi.inspect({ env: lacking.env });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.digest, r.version, r.line], [true, "fail", true, null, "2.0.0", "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-skills"]);
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.version, r.line], [true, "fail", true, "2.0.0", "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-skills"]);
|
||||
// W2: a failure carries the digest of the files the check ran.
|
||||
assert.match(r.digest, /^[a-f0-9]{64}$/);
|
||||
const flood = piPackage(t, { flood: true });
|
||||
r = await pi.inspect({ env: flood.env });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false], r.line);
|
||||
@@ -337,14 +342,15 @@ test("W1 the real claude binary named by MOSAIC_CLAUDE_BIN gives a version, a sh
|
||||
const dir = scratch(t);
|
||||
const env = { MOSAIC_CLAUDE_BIN: CLAUDE, PATH: "/usr/bin:/bin" };
|
||||
const r = await claude.inspect({ env, scratch: dir });
|
||||
assert.deepEqual([r.found, r.source, r.fit, r.definite], [true, "MOSAIC_CLAUDE_BIN", "unknown", false]);
|
||||
// W2: the fit rule runs, and the real claude passes it.
|
||||
assert.deepEqual([r.found, r.source, r.fit, r.definite], [true, "MOSAIC_CLAUDE_BIN", "pass", true], r.line);
|
||||
assert.match(r.version, /\d+\.\d+\.\d+/);
|
||||
assert.equal(r.digest, await sha256(realpathSync(CLAUDE)));
|
||||
assert.match(r.surfaces.help, /--version/);
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env, scratch: dir });
|
||||
const rec = out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.deepEqual([rec.state, rec.digest, rec.version], ["unknown", r.digest, r.version]);
|
||||
assert.deepEqual([rec.state, rec.digest, rec.version], ["fit", r.digest, r.version]);
|
||||
assert.ok(!JSON.stringify(out).includes(CLAUDE));
|
||||
// The scratch HOME it ran in is gone.
|
||||
assert.deepEqual(readdirNames(dir), []);
|
||||
|
||||
@@ -39,6 +39,8 @@ async function copyRepo(t, script, version) {
|
||||
mkdirSync(mod, { recursive: true });
|
||||
for (const f of readdirSync(MODULE)) if (f.endsWith(".mjs")) copyFileSync(join(MODULE, f), join(mod, f));
|
||||
for (const p of readdirSync(join(REPO, "packages"))) if (p !== "plugins") symlinkSync(join(REPO, "packages", p), join(root, "packages", p));
|
||||
// The module imports packages/plugins/src (the stat key), so that links to the real tree too.
|
||||
symlinkSync(join(REPO, "packages", "plugins", "src"), join(root, "packages", "plugins", "src"));
|
||||
standIn(join(root, "node_modules"), script, version);
|
||||
mkdirSync(join(root, "node_modules", ".bin"));
|
||||
symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi"));
|
||||
|
||||
Reference in new issue
Block a user