diff --git a/packages/cli/src/cli.mjs b/packages/cli/src/cli.mjs index 8a94d2a11..b87f30c8d 100644 --- a/packages/cli/src/cli.mjs +++ b/packages/cli/src/cli.mjs @@ -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). diff --git a/packages/cli/src/harness-watch-host.mjs b/packages/cli/src/harness-watch-host.mjs new file mode 100644 index 000000000..dcbe52f1b --- /dev/null +++ b/packages/cli/src/harness-watch-host.mjs @@ -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 /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); +} diff --git a/packages/cli/src/host.mjs b/packages/cli/src/host.mjs index 10d215a22..93d5a5212 100644 --- a/packages/cli/src/host.mjs +++ b/packages/cli/src/host.mjs @@ -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 = {}; diff --git a/packages/cli/tests/harness-watch-host.test.mjs b/packages/cli/tests/harness-watch-host.test.mjs new file mode 100644 index 000000000..cb1031c1f --- /dev/null +++ b/packages/cli/tests/harness-watch-host.test.mjs @@ -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 host: "); + 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"); +}); diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 8a2e72b5b..a4f32c22c 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -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 }; diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index f85f0106e..9bb45e8b4 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -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 diff --git a/packages/plugins/README.md b/packages/plugins/README.md index bdb0d1e95..49bab29d6 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -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 ``, then any other absolute path -of two segments or more with ``. The loader's refusals in `refused` +of two segments or more with ``, 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 `--` 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 `/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})`. diff --git a/packages/plugins/harness/claude-code/index.mjs b/packages/plugins/harness/claude-code/index.mjs index 3628cfae7..38aee86b0 100644 --- a/packages/plugins/harness/claude-code/index.mjs +++ b/packages/plugins/harness/claude-code/index.mjs @@ -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 = /(? \"$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")); diff --git a/packages/plugins/harness/pi/binary.mjs b/packages/plugins/harness/pi/binary.mjs index 1b392f81c..d9322107a 100644 --- a/packages/plugins/harness/pi/binary.mjs +++ b/packages/plugins/harness/pi/binary.mjs @@ -119,8 +119,10 @@ const cleanLabel = (s) => s.length > 0 && s.length <= VERSION_MAX && !/[\u0000-\ // The first line ` --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) => { diff --git a/packages/plugins/harness/pi/index.mjs b/packages/plugins/harness/pi/index.mjs index f7d62c708..2f8ae4594 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -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 (