diff --git a/packages/bus/src/runtime.mjs b/packages/bus/src/runtime.mjs index 25eeda65e..8a80b5466 100644 --- a/packages/bus/src/runtime.mjs +++ b/packages/bus/src/runtime.mjs @@ -27,6 +27,7 @@ import { readSessions, sessionsFile, stopSession } from '../../seat/src/session. import { compactionCount, storeDir } from '../../seat/src/compactions.mjs'; import { publishRemote, workingRepo, WorkingError } from '../../seat/src/working.mjs'; import { createLoader, hasModule } from '../../plugins/src/loader.mjs'; +import { readWatchView } from '../../plugins/src/harness-watch.mjs'; import { pluginSettings } from '../../organization/src/plugin-settings.mjs'; import { loadManagementClient } from '../../model-proxy/src/client.mjs'; import { ProxyPage } from './proxy-page.mjs'; @@ -1393,7 +1394,9 @@ export async function startBroker({ throw new TemplateError('invalid-request', 'template.save takes preview: true or the preview digest'); } if(verb==='limits')return withTokens(id,settings.read(id)); - if(verb==='plugins')return {...plugins.read(id), available:{harness:levels.names ?? []}}; + // #1873 W3: harnessWatch is what the host's harness watch last wrote under the org's data root, + // read only; a missing or bad file gives each harness "not checked yet" (readWatchView). + if(verb==='plugins')return {...plugins.read(id), available:{harness:levels.names ?? []}, harnessWatch:readWatchView({dataRoot, names:levels.names ?? [], paths:[instanceRoot]})}; // Row 256 (#1761): the actor is the reader, so endProtected is the delete check for that reader. if(verb==='instances')return instances.read(id,actor); // Row 65 (mos-0840): the Console's routing table. diff --git a/packages/bus/tests/plugins-verb.test.mjs b/packages/bus/tests/plugins-verb.test.mjs index 88eadbd55..f1907275f 100644 --- a/packages/bus/tests/plugins-verb.test.mjs +++ b/packages/bus/tests/plugins-verb.test.mjs @@ -102,3 +102,67 @@ test('plugins read advertises loadable harnesses even when switched off', async const view=host.broker.request(human,{verb:'plugins'}); assert.deepEqual(view.plugins.harness,['claude-code']);assert.ok(view.available.harness.includes('pi'));assert.ok(view.available.harness.includes('claude-code')); }); + +// #1873 harness watch W3: the plugins answer carries what the host's watch last wrote under the +// organization's data root (state/harness-watch.json), read only, redacted again, and "not checked +// yet" for every harness when the file is missing, cut short or not version 1's shape. +test('W3 runtime: the plugins answer carries the harness watch record per harness, and a missing, truncated or wrong-shape file gives not checked yet', async (t) => { + const { startBroker } = await import('../src/runtime.mjs'); + const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs'); + const { writeFileSync } = await import('node:fs'); + const root = mkdtempSync(join(tmpdir(), 'plugins-watch-')); + const rolesDir = rolesCopy(root); + writeJson(join(root, 'businesses/demo.json'), businessDoc(root, 'demo')); + const dataRoot = join(root, 'data'); + mkdirSync(join(dataRoot, 'state'), { recursive: true, mode: 0o700 }); + const planted = 'w3-planted-env-value'; + process.env.W3_PLANTED = planted; + const bus = await startBroker({ dataRoot, businesses: { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: { authority: { withinRole: [], crossRole: [] } } } } }, organization: { dir: root, rolesDir, system: systemFor(root) } }); + t.after(async () => { delete process.env.W3_PLANTED; await bus.close(); rmSync(root, { recursive: true, force: true }); }); + const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true }); + const file = join(dataRoot, 'state', 'harness-watch.json'); + const read = () => bus.broker.request(human, { verb: 'plugins' }); + const names = read().available.harness; + assert.ok(names.length > 0, 'the tree gives harnesses'); + const unchecked = (name) => ({ name, state: 'unchecked', version: null, checkedAt: null, changedAt: null, failed: [], line: 'not checked yet' }); + + // No file: every harness the loader gives reads not checked yet, in the loader's order. + assert.deepEqual(read().harnessWatch, names.map(unchecked)); + + const at = '2026-11-20T10:00:00.000Z', moved = '2026-11-19T08:00:00.000Z'; + const rec = (harness, over) => ({ harness, state: 'fit', via: 'inspect', found: true, source: 'PATH', version: '1.0.0', digest: 'a'.repeat(64), lastDigest: 'a'.repeat(64), fit: 'pass', definite: true, failed: [], line: 'ok', checkedAt: at, changedAt: moved, ...over }); + const doc = { version: 1, checkedAt: at, refused: [], gone: [], harnesses: [ + rec(names[0], { version: '2.1.323', line: 'every flag the adapter passes is in --help' }), + rec('w3-red', { state: 'red', fit: 'fail', failed: ['--restricted'], line: '--help lacks --restricted' }), + rec('w3-missing', { state: 'missing', found: false, version: null, digest: null, fit: 'unknown', definite: false, line: 'no binary from MOSAIC_PI_BIN, PATH or the repo copy' }), + rec('w3-unknown', { state: 'unknown', fit: 'unknown', definite: false, line: 'inspect did not answer in time' }), + // A path and an env value planted in the file: the broker redacts them again before answering. + rec('w3-leak', { state: 'red', fit: 'fail', version: `9.9 ${planted}`, failed: [planted, '--ok'], line: `failed under ${dataRoot}/x/y and ${join(root, 'businesses')}/demo.json with ${planted}` }), + ] }; + writeFileSync(file, JSON.stringify(doc), { mode: 0o600 }); + const view = read().harnessWatch; + const by = Object.fromEntries(view.map((e) => [e.name, e])); + assert.deepEqual(view.map((e) => e.name), [...names, 'w3-red', 'w3-missing', 'w3-unknown', 'w3-leak'], 'the loader\'s harnesses first, then the file\'s others'); + assert.deepEqual(by[names[0]], { name: names[0], state: 'fit', version: '2.1.323', checkedAt: at, changedAt: moved, failed: [], line: 'every flag the adapter passes is in --help' }); + assert.deepEqual(by['w3-red'], { name: 'w3-red', state: 'red', version: '1.0.0', checkedAt: at, changedAt: moved, failed: ['--restricted'], line: '--help lacks --restricted' }); + assert.deepEqual(by['w3-missing'], { name: 'w3-missing', state: 'missing', version: null, checkedAt: at, changedAt: moved, failed: [], line: 'no binary from MOSAIC_PI_BIN, PATH or the repo copy' }); + assert.deepEqual(by['w3-unknown'], { name: 'w3-unknown', state: 'unknown', version: '1.0.0', checkedAt: at, changedAt: moved, failed: [], line: 'inspect did not answer in time' }); + for (const name of names.slice(1)) assert.deepEqual(by[name], unchecked(name), 'a harness with no record is not checked yet'); + const leak = JSON.stringify(by['w3-leak']); + for (const needle of [planted, dataRoot, root]) assert.ok(!leak.includes(needle), `the answer holds no ${needle === planted ? 'env value' : 'host path'}: ${leak}`); + assert.deepEqual(by['w3-leak'].failed, ['--ok'], 'a rule id that is an env value is dropped'); + assert.match(by['w3-leak'].line, /^failed under .* with $/); + assert.deepEqual(JSON.parse(readFileSync(file, 'utf8')), doc, 'the broker does not write the file'); + + // A truncated file, a wrong shape and a wrong version each read as not checked yet, never a throw. + for (const bad of [JSON.stringify(doc).slice(0, 80), JSON.stringify({ ...doc, harnesses: { [names[0]]: doc.harnesses[0] } }), JSON.stringify({ ...doc, version: 2 }), JSON.stringify([doc]), 'null', '']) { + writeFileSync(file, bad, { mode: 0o600 }); + assert.deepEqual(read().harnessWatch, names.map(unchecked), bad.slice(0, 40)); + } + // A record of the wrong shape reads as not checked yet for that harness only. + writeFileSync(file, JSON.stringify({ ...doc, harnesses: [rec(names[0], { state: 'green' }), rec('w3-red', { state: 'red', fit: 'fail' }), 'x', null, rec('../x', {})] }), { mode: 0o600 }); + const odd = Object.fromEntries(read().harnessWatch.map((e) => [e.name, e])); + assert.deepEqual(odd[names[0]], unchecked(names[0])); + assert.equal(odd['w3-red'].state, 'red'); + assert.equal(odd['../x'], undefined, 'a name that is not a harness name is dropped'); +}); diff --git a/packages/cli/src/harness-watch-host.mjs b/packages/cli/src/harness-watch-host.mjs index dcbe52f1b..ca5c82880 100644 --- a/packages/cli/src/harness-watch-host.mjs +++ b/packages/cli/src/harness-watch-host.mjs @@ -33,7 +33,7 @@ import { appendFileSync, chmodSync, closeSync, fsyncSync, mkdirSync, mkdtempSync 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 { watchHarnesses, watchStateFile } 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"; @@ -41,7 +41,8 @@ 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"); +// The bus's plugins verb reads this file (readWatchView in the same module as watchStateFile). +export const watchFile = watchStateFile; export const watchLog = (dataRoot) => join(dataRoot, "state", "harness-watch.jsonl"); const VALUE_MIN = 6; @@ -72,16 +73,19 @@ export async function harnessKeys({ dataRoot, env }) { return JSON.stringify(out); } -// Starts the timer and its first pass. Returns { stop, settled }: stop() ends it (a promise), and +// Starts the timer and its first pass. dataRoot is the organization's: the state files go under it. +// pluginRoot (W3) is where the harness plugins load from: the instance root when there is one, as +// the launcher loads them (launcher.mjs, system.instanceRoot ?? dataRoot); it defaults to dataRoot. 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, + pluginRoot = dataRoot, env = process.env, log = () => {}, clock = realClock, processing = () => processingStatus(dataRoot), watch = watchHarnesses, - keys = () => harnessKeys({ dataRoot, env }), + keys = () => harnessKeys({ dataRoot: pluginRoot, env }), statMs = STAT_MS, dayMs = DAY_MS, timeoutMs = undefined, @@ -89,6 +93,7 @@ export function startHarnessWatch({ requests = onWatchRequest, } = {}) { if (typeof dataRoot !== "string" || !dataRoot) throw new TypeError("startHarnessWatch needs the organization's dataRoot"); + if (typeof pluginRoot !== "string" || !pluginRoot) throw new TypeError("startHarnessWatch needs a plugin root"); const control = new AbortController(); let stopped = false; let running = null; @@ -114,9 +119,9 @@ export function startHarnessWatch({ 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 } : {}) }); + const result = await watch({ last, env, scratch, dataRoot: pluginRoot, 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() }); + store(dataRoot, result, { env, scratch, pluginRoot, at: new Date(now).toISOString() }); last = result.records; lastKey = key; lastFull = now; @@ -175,11 +180,11 @@ function readRecords(dataRoot) { } } -function store(dataRoot, result, { env, scratch, at }) { +function store(dataRoot, result, { env, scratch, pluginRoot = dataRoot, 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 }); + const leaks = leakChecker({ env, scratch, dataRoot, pluginRoot }); 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(""); @@ -204,10 +209,10 @@ function store(dataRoot, result, { env, scratch, at }) { // 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 }) { +export function leakChecker({ env, scratch, dataRoot, pluginRoot = null }) { 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 p of [scratch, dataRoot, pluginRoot, 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) => { diff --git a/packages/cli/src/host.mjs b/packages/cli/src/host.mjs index 93d5a5212..7f1209bd4 100644 --- a/packages/cli/src/host.mjs +++ b/packages/cli/src/host.mjs @@ -378,7 +378,9 @@ export async function startHost({ boot, business, notifier = null, options = nul }); // #1873 W2: the harness watch's timer, stopped before anything else when the host closes. if (harnessWatch) { - const watch = startHarnessWatch({ log, ...harnessWatch, dataRoot }); + // W3: plugins load from the instance root when there is one, as the launcher loads them; the + // state files stay under the organization's dataRoot. + const watch = startHarnessWatch({ log, pluginRoot: boot.instanceRoot ?? dataRoot, ...harnessWatch, dataRoot }); hooks.unshift(() => watch.stop()); } diff --git a/packages/cli/tests/bus-start-watch.test.mjs b/packages/cli/tests/bus-start-watch.test.mjs new file mode 100644 index 000000000..1d64ab589 --- /dev/null +++ b/packages/cli/tests/bus-start-watch.test.mjs @@ -0,0 +1,83 @@ +// #1873 harness watch, slice W3: `mosaic bus start ORG --internal-host` starts the harness watch in +// the host (through deps.harnessWatch, a stand-in watch), with the instance root as the root the +// harness plugins load from and the organization's root for its state files, as the launcher loads +// them; `--no-launch` starts no watch. The host runs in this process and stops on SIGTERM, as the +// unit's stop does. Nothing is launched and no live port is used. +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { join, resolve } from 'node:path'; +import { tmpdir } from 'node:os'; +import * as org from '../../organization/src/org-service.mjs'; +import { main } from '../src/cli.mjs'; +import { watchFile } from '../src/harness-watch-host.mjs'; +import { humanEnv } from './helpers.mjs'; + +const repo = resolve(import.meta.dirname, '../../..'); + +async function instance(t) { + const home = mkdtempSync(join(tmpdir(), 'w3-')); + t.after(() => rmSync(home, { recursive: true, force: true })); + const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-w3/config.json') }); + const boot = spawnSync(join(repo, 'scripts/bootstrap.sh'), ['--instance', 'w3'], { env, encoding: 'utf8' }); + assert.equal(boot.status, 0, boot.stderr); + const ctx = { configDir: join(home, '.config/mosaic-w3'), dataRoot: join(home, '.mosaic-w3'), instance: 'w3', env, principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } }; + await org.createOrg(ctx, 'alpha', await org.templateRender('alpha', { fixture: 'local' })); + return { ctx, env, orgRoot: join(ctx.dataRoot, 'org-runtime', 'alpha') }; +} + +// Runs the host in this process until `until` says so, then stops it as SIGTERM does. +async function hostRun(args, env, deps, until) { + let out = '', err = ''; + const io = { env, stdin: null, stdout: { write: (s) => { out += s; return true; } }, stderr: { write: (s) => { err += s; return true; } } }; + const listeners = process.listenerCount('SIGTERM'); + const running = main(['bus', 'start', 'alpha', '--internal-host', ...args], io, deps); + let finished = false; + running.finally(() => { finished = true; }); + const end = Date.now() + 30000; + // The host prints its line before bus start takes SIGTERM, so the stop waits for its listener too. + while (!(out.includes('bus host up') && process.listenerCount('SIGTERM') > listeners && until()) && !finished && Date.now() < end) await new Promise((r) => setTimeout(r, 20)); + const up = out.includes('bus host up'); + if (!finished) process.emit('SIGTERM'); + const code = await running; + return { code, out, err, up }; +} + +test('W3 bus start starts the harness watch in the host, loading plugins from the instance root and writing state under the organization root', { timeout: 90000 }, async (t) => { + const { ctx, env, orgRoot } = await instance(t); + const calls = []; + const scratch = mkdtempSync(join(tmpdir(), 'w3-scratch-')); + t.after(() => rmSync(scratch, { recursive: true, force: true })); + const record = { harness: 'pi', state: 'fit', via: 'inspect', found: true, source: 'PATH', version: '1.6.0', digest: 'b'.repeat(64), lastDigest: 'b'.repeat(64), fit: 'pass', definite: true, failed: [], line: 'pi 1.6.0 passed the fit check', checkedAt: '2026-11-20T10:00:00.000Z', changedAt: '2026-11-20T10:00:00.000Z' }; + const harnessWatch = { + watch: async (opts) => { calls.push(opts); return { records: [record], transitions: [], refused: [], gone: [] }; }, + keys: async () => 'k', + processing: () => ({ halted: false }), + scratchRoot: () => scratch, + }; + const run = await hostRun([], env, { harnessWatch }, () => calls.length > 0 && existsSync(watchFile(orgRoot))); + assert.equal(run.up, true, run.err); + assert.match(run.out, /bus host stopped \(0\)/, run.err); + assert.equal(calls.length, 1, 'the start pass ran in the host'); + assert.notEqual(orgRoot, ctx.dataRoot); + assert.equal(calls[0].dataRoot, ctx.dataRoot, 'the watch loads harness plugins from the instance root, as the launcher does'); + assert.equal(calls[0].signal.aborted, true, 'the stop aborted the watch'); + assert.deepEqual(JSON.parse(readFileSync(watchFile(orgRoot), 'utf8')).harnesses.map((r) => r.harness), ['pi'], 'its state file is under the organization root'); + assert.equal(existsSync(watchFile(ctx.dataRoot)), false, 'and not under the instance root'); +}); + +test('W3 bus start --no-launch starts no harness watch', { timeout: 90000 }, async (t) => { + const { env, orgRoot } = await instance(t); + const calls = []; + const harnessWatch = { watch: async (opts) => { calls.push(opts); return { records: [], transitions: [], refused: [], gone: [] }; }, keys: async () => { calls.push('keys'); return 'k'; }, processing: () => ({ halted: false }), scratchRoot: () => tmpdir() }; + // A watch starts with the host and runs its start pass at once, so the host stays up 500 ms after + // its line before the stop. + let upAt = null; + const run = await hostRun(['--no-launch'], env, { harnessWatch }, () => { upAt ??= Date.now(); return Date.now() - upAt >= 500; }); + assert.equal(run.up, true, run.err); + assert.match(run.out, /launches disabled/); + assert.match(run.out, /bus host stopped \(0\)/, run.err); + assert.deepEqual(calls, [], 'no watch, no key read'); + assert.equal(existsSync(watchFile(orgRoot)), false); +}); diff --git a/packages/cli/tests/harness-watch-host.test.mjs b/packages/cli/tests/harness-watch-host.test.mjs index cb1031c1f..e00ea57db 100644 --- a/packages/cli/tests/harness-watch-host.test.mjs +++ b/packages/cli/tests/harness-watch-host.test.mjs @@ -5,13 +5,14 @@ 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 { dirname, 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 { pluginPaths } from "../../plugins/src/pinned.mjs"; import * as claude from "../../plugins/harness/claude-code/index.mjs"; function scratch(t) { @@ -426,3 +427,69 @@ test("W2 startHost with harnessWatch starts the watch in the host, and close sto assert.equal(signal.aborted, true, "close aborted the pass"); assert.deepEqual(clock.cleared, [clock.handle], "and cleared the timer"); }); + +test("W3 host watch: harness plugins load from pluginRoot, the state files stay under the organization's dataRoot, and the backstop knows both", async (t) => { + const dataRoot = scratch(t); + const pluginRoot = scratch(t); + const clock = fakeClock(); + const stub = stubWatch(); + const handle = startHarnessWatch({ dataRoot, pluginRoot, clock, env: {}, watch: stub.watch, keys: async () => "k", processing: () => ({ halted: false }), scratchRoot: () => scratch(t) }); + t.after(() => handle.stop()); + await handle.settled(); + assert.equal(stub.calls.length, 1); + assert.equal(stub.calls[0].dataRoot, pluginRoot, "the watch loads harness plugins from the plugin root"); + assert.ok(existsSync(watchFile(dataRoot)), "the state file is under the organization's dataRoot"); + assert.equal(existsSync(watchFile(pluginRoot)), false, "and not under the plugin root"); + assert.equal(leakChecker({ env: {}, scratch: null, dataRoot, pluginRoot })({ line: `${pluginRoot}/plugins` }), true, "a record naming the plugin root is not written"); + assert.throws(() => startHarnessWatch({ dataRoot, pluginRoot: "", watch: stub.watch }), TypeError); +}); + +test("W3 startHost gives the watch the instance root as its plugin root when the organization has one", 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 instanceRoot = scratch(t); + const clock = fakeClock(); + const stub = stubWatch(); + const host = await startHost({ boot: { ...boot, instanceRoot }, 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))); + for (let n = 0; n < 250 && !existsSync(watchFile(boot.dataRoot)); n++) await new Promise((done) => setTimeout(done, 20)); + assert.equal(stub.calls.length, 1); + assert.equal(stub.calls[0].dataRoot, instanceRoot, "plugins load from the instance root, as the launcher loads them"); + assert.ok(existsSync(watchFile(boot.dataRoot)), "the state file is under the organization's root"); + assert.equal(existsSync(watchFile(instanceRoot)), false); + assert.equal(await host.close(0), 0); + closed = true; +}); + +test("W3 host watch: the default stat key reads the plugin root's plugins, and a record naming the plugin root is not written", async (t) => { + const dataRoot = scratch(t); + // The plugin root sits beside TMPDIR, not in it: TMPDIR is an env value the backstop already + // knows, so a root under it would be caught without the plugin root as a needle of its own. + // When TMPDIR's parent can't take it, the test skips: a root under TMPDIR would pass with the + // plugin root left out of the backstop. + let pluginRoot; + try { pluginRoot = mkdtempSync(join(dirname(tmpdir()), "mosaic-hw-plugins-")); } + catch { t.skip("the plugin root must sit beside TMPDIR, and TMPDIR's parent is not writable"); return; } + t.after(() => rmSync(pluginRoot, { recursive: true, force: true })); + const clock = fakeClock(); + const logs = []; + let line = "ok"; + const stub = stubWatch(() => ({ records: [record({ line })], transitions: [], refused: [], gone: [] })); + const handle = startHarnessWatch({ dataRoot, pluginRoot, clock, env: {}, log: (l) => logs.push(l), watch: stub.watch, processing: () => ({ halted: false }), scratchRoot: () => scratch(t) }); + t.after(() => handle.stop()); + await handle.settled(); + assert.equal(stub.calls.length, 1); + clock.fire(); await handle.settled(); + assert.equal(stub.calls.length, 1, "no change, no full pass"); + // A plugin lock that can't be read changes the plugin root's key (the loader refuses it): a full pass. + writeFileSync(pluginPaths(pluginRoot).lock, "{", { mode: 0o600 }); + line = `${pluginRoot}/plugins/pi`; + clock.fire(); await handle.settled(); + assert.equal(stub.calls.length, 2, "the key read the plugin root, so its change ran a full pass"); + assert.match(logs.join("\n"), /a state line held a host path or an env value/, "and the record that names the plugin root was not written"); + assert.ok(!logs.join("\n").includes(pluginRoot)); + assert.ok(!readFileSync(watchFile(dataRoot), "utf8").includes(pluginRoot)); +}); diff --git a/packages/plugins/src/harness-watch.mjs b/packages/plugins/src/harness-watch.mjs index 142681b5c..ab10cfb05 100644 --- a/packages/plugins/src/harness-watch.mjs +++ b/packages/plugins/src/harness-watch.mjs @@ -21,9 +21,9 @@ // (MOSAIC_PI_BIN, PATH, repo), never a path, and every text field has the scratch directory, // the home directory, each env value and each PATH entry (and their real paths) replaced by a // name for it, then every other absolute path replaced by . -import { realpathSync } from "node:fs"; +import { closeSync, constants, fstatSync, openSync, readSync, realpathSync } from "node:fs"; import { homedir, userInfo } from "node:os"; -import { isAbsolute } from "node:path"; +import { isAbsolute, join } from "node:path"; import { loadInstalled } from "./loader.mjs"; export const STATES = Object.freeze(["fit", "changed", "red", "unknown", "missing"]); @@ -242,7 +242,7 @@ const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // a trailing slash. Longest first, so a path inside another is not left half replaced. The user // name (this process's account, USER and LOGNAME), short as it may be, is replaced as a whole // word. Last, any absolute path left is . -function redactor(env, scratch) { +function redactor(env, scratch, extra = []) { const pairs = new Map(); const put = (value, name) => { if (value.length >= VALUE_MIN && !pairs.has(value)) pairs.set(value, name); }; const add = (value, name) => { @@ -254,6 +254,7 @@ function redactor(env, scratch) { try { put(realpathSync(bare || "/"), name); } catch {} }; if (typeof scratch === "string" && scratch) add(scratch, ""); + for (const p of extra) if (typeof p === "string" && p) add(p, ""); for (const source of [env ?? {}, process.env]) { for (const [k, v] of Object.entries(source)) { if (typeof v !== "string") continue; @@ -275,3 +276,78 @@ function redactor(env, scratch) { return out.replace(FILE_URL, "").replace(TILDE_PATH, "").replace(LOOSE_PATH, ""); }; } + +// #1873 harness watch, slice W3 (reports/harness-watch-design.md section 4, "What the owner sees"): +// the bus's plugins answer reads what the host's watch last wrote. The host (packages/cli/src/ +// harness-watch-host.mjs) is the one writer of this file; this reads it and never writes. +export const watchStateFile = (dataRoot) => join(dataRoot, "state", "harness-watch.json"); +export const NOT_CHECKED = "not checked yet"; +const STATE_VERSION = 1; +const STATE_MAX = 1024 * 1024; +const NAME = /^[a-z0-9][a-z0-9-]{0,63}$/; +const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/; +const FAILED_MAX = 32; +// At most this many entries, and records read: a file the host wrote holds one per harness module. +const ENTRIES_MAX = 64; +const RECORDS_MAX = 256; + +// One entry per harness: each name in names (the harnesses the loader gives, in its order), then +// each other harness the file holds a record for. An entry is { name, state, version, checkedAt, +// changedAt, failed, line }. state is one of STATES, or "unchecked" with line NOT_CHECKED when the +// file is missing, unreadable, larger than STATE_MAX, not JSON or not version 1's shape, or holds no +// usable record for that harness. Never throws. The text fields are redacted again here, with this +// process's environment, env and the paths given (the data roots): the file's writer redacted them +// once, and a file someone else wrote is not trusted to have. A name the loader does not give that is +// an env value is dropped. At most ENTRIES_MAX entries, from the first RECORDS_MAX records. +export function readWatchView({ dataRoot, names = [], env = process.env, paths = [] } = {}) { + const records = readState(dataRoot).slice(0, RECORDS_MAX); + const order = new Set(); + for (const n of Array.isArray(names) ? names : []) if (typeof n === "string" && NAME.test(n) && order.size < ENTRIES_MAX) order.add(n); + const values = envValues(env); + const byName = new Map(); + for (const r of records) { + if (!r || typeof r !== "object" || Array.isArray(r) || typeof r.harness !== "string" || !NAME.test(r.harness) || byName.has(r.harness)) continue; + if (!order.has(r.harness) && (values.has(r.harness) || order.size >= ENTRIES_MAX)) continue; + byName.set(r.harness, r); + order.add(r.harness); + } + let redact = null; + const view = (name) => { + const r = byName.get(name); + if (!r || !STATES.includes(r.state)) return unchecked(name); + redact ??= redactor(env, null, [dataRoot, ...paths]); + const failed = Array.isArray(r.failed) ? [...new Set(r.failed.slice(0, FAILED_MAX).filter((id) => typeof id === "string" && RULE.test(id) && !values.has(id)))] : []; + return { name, state: r.state, version: text(r.version, LABEL_MAX, redact), checkedAt: when(r.checkedAt), changedAt: when(r.changedAt), failed, line: text(r.line, LINE_MAX, redact) }; + }; + return [...order].map(view); +} + +const unchecked = (name) => ({ name, state: "unchecked", version: null, checkedAt: null, changedAt: null, failed: [], line: NOT_CHECKED }); +// A time is kept only when it is a real one: 2026-02-30 is not. +const when = (v) => { + if (typeof v !== "string" || !ISO.test(v)) return null; + const t = Date.parse(v); + return Number.isFinite(t) && new Date(t).toISOString().slice(0, 19) === v.slice(0, 19) ? v : null; +}; + +// The records of the state file, or [] for any problem. A link, a FIFO or a file over STATE_MAX is +// not read. +function readState(dataRoot) { + if (typeof dataRoot !== "string" || !dataRoot) return []; + let fd = null; + try { + fd = openSync(watchStateFile(dataRoot), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); + const st = fstatSync(fd); + if (!st.isFile() || st.size > STATE_MAX) return []; + const buf = Buffer.alloc(STATE_MAX + 1); + let len = 0, n; + while (len <= STATE_MAX && (n = readSync(fd, buf, len, STATE_MAX + 1 - len, null)) > 0) len += n; + if (len > STATE_MAX) return []; + const doc = JSON.parse(buf.subarray(0, len).toString("utf8")); + return doc && typeof doc === "object" && doc.version === STATE_VERSION && Array.isArray(doc.harnesses) ? doc.harnesses : []; + } catch { + return []; + } finally { + if (fd !== null) try { closeSync(fd); } catch {} + } +} diff --git a/packages/plugins/tests/harness-watch-view.test.mjs b/packages/plugins/tests/harness-watch-view.test.mjs new file mode 100644 index 000000000..28a5d047d --- /dev/null +++ b/packages/plugins/tests/harness-watch-view.test.mjs @@ -0,0 +1,129 @@ +// #1873 harness watch, slice W3: readWatchView, the read the bus's plugins answer makes of the +// host's state file (/state/harness-watch.json). It never writes, never throws, and +// gives "not checked yet" for anything it cannot use. +import test from "node:test"; +import assert from "node:assert/strict"; +import { execFileSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; +import { homedir, tmpdir } from "node:os"; +import { join } from "node:path"; +import { NOT_CHECKED, readWatchView, watchStateFile } from "../src/harness-watch.mjs"; +import { watchFile } from "../../cli/src/harness-watch-host.mjs"; + +function root(t) { + const dir = mkdtempSync(join(tmpdir(), "mosaic-hwv-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + mkdirSync(join(dir, "state"), { mode: 0o700 }); + return dir; +} +const at = "2026-11-20T10:00:00.000Z"; +const rec = (harness, over = {}) => ({ harness, state: "fit", via: "inspect", found: true, source: "PATH", version: "1.6.0", digest: "b".repeat(64), lastDigest: "b".repeat(64), fit: "pass", definite: true, failed: [], line: "pi 1.6.0 passed the fit check", checkedAt: at, changedAt: at, ...over }); +const put = (dir, doc) => writeFileSync(watchStateFile(dir), typeof doc === "string" ? doc : JSON.stringify(doc), { mode: 0o600 }); +const unchecked = (name) => ({ name, state: "unchecked", version: null, checkedAt: null, changedAt: null, failed: [], line: NOT_CHECKED }); + +test("W3 the host writes the file the view reads: one path", () => { + assert.equal(watchFile("/d"), watchStateFile("/d")); + assert.equal(watchStateFile("/d"), "/d/state/harness-watch.json"); +}); + +test("W3 readWatchView gives each state's record, the loader's names first, and writes nothing", (t) => { + const dir = root(t); + const doc = { version: 1, checkedAt: at, refused: [], gone: [], harnesses: [rec("pi"), rec("claude-code", { state: "changed", version: "2.1.323", changedAt: "2026-11-20T09:00:00.000Z" }), rec("w3-red", { state: "red", fit: "fail", failed: ["--restricted", "--restricted"] }), rec("w3-missing", { state: "missing", found: false }), rec("w3-unknown", { state: "unknown" })] }; + put(dir, doc); + const before = statSync(watchStateFile(dir)); + const view = readWatchView({ dataRoot: dir, names: ["claude-code", "pi", "codex"] }); + assert.deepEqual(view.map((e) => [e.name, e.state]), [["claude-code", "changed"], ["pi", "fit"], ["codex", "unchecked"], ["w3-red", "red"], ["w3-missing", "missing"], ["w3-unknown", "unknown"]]); + assert.deepEqual(view[0], { name: "claude-code", state: "changed", version: "2.1.323", checkedAt: at, changedAt: "2026-11-20T09:00:00.000Z", failed: [], line: "pi 1.6.0 passed the fit check" }); + assert.deepEqual(view[2], unchecked("codex")); + assert.deepEqual(view[3].failed, ["--restricted"]); + const after = statSync(watchStateFile(dir)); + assert.deepEqual([after.mtimeMs, after.size, after.mode], [before.mtimeMs, before.size, before.mode]); + assert.deepEqual(JSON.parse(readFileSync(watchStateFile(dir), "utf8")), doc); +}); + +test("W3 readWatchView: no file, a truncated file, a wrong shape, a link, a FIFO and an oversized file give not checked yet", (t) => { + const dir = root(t); + const names = ["pi", "claude-code"]; + const none = names.map(unchecked); + assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "no file"); + assert.deepEqual(readWatchView({ dataRoot: join(dir, "nowhere"), names }), none, "no state directory"); + assert.deepEqual(readWatchView({ dataRoot: null, names }), none, "no data root"); + assert.deepEqual(readWatchView({ names }), none, "nothing given"); + assert.deepEqual(readWatchView(), [], "no names, no file"); + const good = JSON.stringify({ version: 1, harnesses: [rec("pi")] }); + for (const bad of [good.slice(0, good.length - 3), "{", "", "null", "7", JSON.stringify({ version: "1", harnesses: [rec("pi")] }), JSON.stringify({ version: 1, harnesses: { pi: rec("pi") } }), JSON.stringify({ harnesses: [rec("pi")] })]) { + put(dir, bad); + assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, bad.slice(0, 40)); + } + // A link to a good file is not followed: the host writes a plain file, renamed into place. + const elsewhere = join(dir, "elsewhere.json"); + writeFileSync(elsewhere, good); + rmSync(watchStateFile(dir), { force: true }); + symlinkSync(elsewhere, watchStateFile(dir)); + assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "a link"); + rmSync(watchStateFile(dir)); + // A FIFO with no writer does not block the read. + execFileSync("mkfifo", [watchStateFile(dir)]); + assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "a FIFO"); + rmSync(watchStateFile(dir)); + mkdirSync(watchStateFile(dir)); + assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "a directory"); + rmSync(watchStateFile(dir), { recursive: true }); + // Over 1 MiB is not read at all. + put(dir, JSON.stringify({ version: 1, harnesses: [rec("pi")], pad: "x".repeat(1024 * 1024) })); + assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "an oversized file"); + put(dir, good); + assert.equal(readWatchView({ dataRoot: dir, names })[0].state, "fit", "and the good file reads"); +}); + +test("W3 readWatchView: a bad record reads as not checked yet for its harness, and bad fields are dropped", (t) => { + const dir = root(t); + put(dir, { version: 1, harnesses: [ + rec("pi", { state: "green" }), + rec("pi", { state: "red" }), + rec("claude-code", { checkedAt: "yesterday", changedAt: "2026-13-45T99:00:00Z", version: "2.1\nsecond line\u0007", line: 5, failed: ["ok-rule", "has space", 7, "x".repeat(80)] }), + rec("Bad Name"), rec("../up"), rec(""), [rec("codex")], "pi", null, { state: "fit" }, + ] }); + const view = readWatchView({ dataRoot: dir, names: ["pi", "claude-code"] }); + assert.deepEqual(view[0], unchecked("pi"), "a state the watch does not give is not a guess, and the first record for a name wins"); + assert.deepEqual(view[1], { name: "claude-code", state: "fit", version: "2.1", checkedAt: null, changedAt: null, failed: ["ok-rule"], line: null }); + assert.equal(view.length, 2, "records with names that are not harness names are dropped"); +}); + +test("W3 readWatchView redacts again: the data roots, the home directory, PATH entries, env values and loose paths", (t) => { + const dir = root(t); + const instance = mkdtempSync(join(tmpdir(), "mosaic-hwv-inst-")); + t.after(() => rmSync(instance, { recursive: true, force: true })); + const env = { MOSAIC_PI_BIN: "/opt/w3/pi-binary", W3_TOKEN: "w3-token-value-xyz", PATH: "/opt/w3-bin:/usr/bin" }; + put(dir, { version: 1, harnesses: [rec("pi", { + version: `1.6.0 from ${env.MOSAIC_PI_BIN}`, + line: `${dir}/state ${instance}/plugins ${homedir()}/x ${env.W3_TOKEN} /opt/w3-bin/pi /srv/other/place file:///etc/x ~/y/z`, + failed: [env.W3_TOKEN, "--tools"], + })] }); + const [e] = readWatchView({ dataRoot: dir, names: ["pi"], env, paths: [instance] }); + const all = JSON.stringify(e); + for (const needle of [dir, instance, homedir(), env.W3_TOKEN, env.MOSAIC_PI_BIN, "/opt/w3-bin", "/srv/other", "/etc/x", "~/y"]) assert.ok(!all.includes(needle), `${needle} in ${all}`); + assert.equal(e.version, "1.6.0 from "); + assert.deepEqual(e.failed, ["--tools"]); + assert.match(e.line, //); +}); + +test("W3 readWatchView: a name that is an env value is dropped unless the loader gives it, entries are capped, and an impossible date is not a time", (t) => { + const dir = root(t); + const env = { W3_NAME: "w3-secret-name" }; + const many = Array.from({ length: 300 }, (_, i) => rec(`w3-h${i}`)); + put(dir, { version: 1, harnesses: [rec("w3-secret-name"), rec("pi", { checkedAt: "2026-02-30T00:00:00Z", changedAt: "2026-11-20T10:00:00Z" }), ...many] }); + const view = readWatchView({ dataRoot: dir, names: ["pi"], env }); + assert.ok(!view.some((e) => e.name === "w3-secret-name"), "a file-only name equal to an env value does not reach the answer"); + assert.equal(view.length, 64, "at most 64 entries"); + assert.deepEqual([view[0].checkedAt, view[0].changedAt], [null, "2026-11-20T10:00:00Z"], "2026-02-30 is not a time; a real one is kept"); + assert.equal(readWatchView({ dataRoot: dir, names: ["w3-secret-name"], env })[0].state, "fit", "a name the loader gives is kept"); +}); + +test("W3 readWatchView reads at most the first 256 records: a usable record after them is not checked yet", (t) => { + const dir = root(t); + put(dir, { version: 1, harnesses: [...Array(255).fill(null), rec("pi")] }); + assert.equal(readWatchView({ dataRoot: dir, names: ["pi"] })[0].state, "fit", "the 256th record is read"); + put(dir, { version: 1, harnesses: [...Array(256).fill(null), rec("pi")] }); + assert.deepEqual(readWatchView({ dataRoot: dir, names: ["pi"] }), [unchecked("pi")], "the 257th is not"); +}); diff --git a/packages/webui/src/public/bus.js b/packages/webui/src/public/bus.js index ec6512db9..8ad5202dd 100644 --- a/packages/webui/src/public/bus.js +++ b/packages/webui/src/public/bus.js @@ -1313,6 +1313,23 @@ if(harnessLoading || harnessSettings !== null || !canSend || isTester)return; harnessLoading=true;const epoch=orgEpoch; fetch('/api/proxy/plugins',{cache:'no-store',redirect:'manual'}).then(async r=>{const v=await r.json();if(epoch!==orgEpoch)return;if(!r.ok || !/^[a-f0-9]{64}$/.test(v.revision??'') || !Array.isArray(v.available?.harness) || !v.available.harness.every(n=>typeof n==='string') || !Array.isArray(v.plugins?.harness??[]))throw Error();harnessSettings=v;}).catch(()=>{if(epoch!==orgEpoch)return;harnessSettings=false;harnessReceipt='Harnesses could not be read.';}).finally(()=>{if(epoch!==orgEpoch)return;harnessLoading=false;if(routeOf().split('?')[0]==='/settings/routing')render(false);}); } + // #1873 W3: one line per harness, from what the bus host's harness watch last wrote (the plugins + // answer's harnessWatch, read only). Red and missing read as a problem, in the danger colour r3 + // gives "can't start" (new launches on them refuse); a harness the watch has + // not checked reads "not checked yet", never fit. A bus that gives no harnessWatch gets one line. + const WATCH_WORD={fit:'fit',changed:'fit',red:'red',unknown:'unknown',missing:'missing'}; + function harnessWatchLine(e){ + const known=Object.hasOwn(WATCH_WORD,e.state), head=txt(e.name)+(known&&typeof e.version==='string'&&e.version?` ${txt(e.version)}`:''); + if(!known)return `
  • ${head} · not checked yet
  • `; + const time=v=>typeof v==='string'&&Number.isFinite(Date.parse(v))?v:null, parts=[head], at=e.state==='changed'&&time(e.changedAt)?`changed ${when(e.changedAt)}`:time(e.checkedAt)?`checked ${when(e.checkedAt)}`:''; + if(at)parts.push(at); + parts.push(WATCH_WORD[e.state]); + if(['red','missing','unknown'].includes(e.state)&&typeof e.line==='string'&&e.line)parts.push(txt(e.line)); + if(e.state==='red')parts.push('new launches refuse'); + return `
  • ${parts.join(' · ')}
  • `; + } + const harnessWatchLines=v=>{const list=Array.isArray(v.harnessWatch)?v.harnessWatch.filter(e=>e&&typeof e==='object'&&typeof e.name==='string'&&e.name):null; + return list===null?'

    What the harness check found could not be read.

    ':list.length?`
      ${list.map(harnessWatchLine).join('')}
    `:'';}; const ROUTE_CELLS=[['harness','Harness'],['model','Model'],['thinking','Thinking'],['fallback.harness','Fallback harness'],['fallback.model','Fallback model'],['fallback.thinking','Fallback thinking']]; const routeCells=r=>({harness:r.harness??'',model:r.model??'',thinking:r.thinking??'','fallback.harness':r.fallback?.harness??'','fallback.model':r.fallback?.model??'','fallback.thinking':r.fallback?.thinking??''}); const sameCells=(a,b)=>ROUTE_CELLS.every(([k])=>a[k]===b[k]); @@ -1429,7 +1446,7 @@ // models after a restart; that notice gets the organization's Restart (not primary: Save routes is). const catalogueInfo=v.catalogue?.status==='ready'?`

    Model catalogue, observed ${txt(window.MosaicLocalTime(v.catalogue.observedAt))}.

    ${(v.catalogue.notices??[]).map(n=>`

    ${txt(n)}${n===window.MosaicConsole?.RESTART_TO_LAUNCH&&window.MosaicOrg?.id?` ${window.MosaicConsole.orgRestartButton(window.MosaicOrg.id,{name:window.MosaicOrg.label||window.MosaicOrg.id})}`:''}

    `).join('')}`:v.catalogue?.status==='unavailable'?catalogueLine(proxyOn,v.catalogue):'

    Model catalogue not enabled.

    '; const providerControl=canSend&&!isTester?modelsChoice(/ data-primary[ >]/.test(catalogueInfo)):''; - const harnessControl=canSend&&!isTester?`
    Harnesses${harnessSettings ? [...new Set([...harnessSettings.available.harness,...(harnessSettings.plugins.harness??[])])].map(name=>``).join('')+` ` : '

    Harnesses unavailable.

    '}

    ${txt(harnessReceipt)}

    `:''; + const harnessControl=canSend&&!isTester?`
    Harnesses${harnessSettings ? [...new Set([...harnessSettings.available.harness,...(harnessSettings.plugins.harness??[])])].map(name=>``).join('')+` `+harnessWatchLines(harnessSettings) : '

    Harnesses unavailable.

    '}

    ${txt(harnessReceipt)}

    `:''; const off=v.harnesses.length?'':'

    No harness is switched on for this organization, so every launch is refused, and so is a save that changes a harness. Switch one on under Harnesses.

    '; return `
    ${title}${catalogueInfo}${providerControl}${harnessControl}${off}
    ${ROUTE_CELLS.map(([,l])=>``).join('')}${rows}
    AgentPrimaryFallback
    ${l.replace('Fallback ','')}

    ${canSend || isTester?'Changes are checked and logged with your identity.':'Saving requires signed-in send mode.'}

    ${txt(routesReceipt)}

    ${routesApplyMarkup()}
    `; } @@ -1472,7 +1489,7 @@ const used=old.filter(n=>!selected.includes(n) && Object.values(routesView.instances).some(r=>r.harness===n || r.fallback?.harness===n)); if(used.length){harnessReceipt=`A saved route uses ${used.join(', ')}. Change the route before switching it off.`;render(false);return;} harnessBusy=true;const button=e.target;button.disabled=true;harnessReceipt='';const epoch=orgEpoch; - try{const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:harnessSettings.revision,plugins:{harness:selected}}),cache:'no-store',redirect:'manual'});const result=await response.json();if(epoch!==orgEpoch)return;if(!response.ok){harnessReceipt=result.message||result.error||'Harness setting was refused.';return;}harnessSettings={...result,available:harnessSettings.available};harnessDraft=null;harnessReceipt='Harnesses saved. The next launch uses the new settings.';routesView=null;last.delete('routes');} + try{const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:harnessSettings.revision,plugins:{harness:selected}}),cache:'no-store',redirect:'manual'});const result=await response.json();if(epoch!==orgEpoch)return;if(!response.ok){harnessReceipt=result.message||result.error||'Harness setting was refused.';return;}harnessSettings={...result,available:harnessSettings.available,harnessWatch:result.harnessWatch??harnessSettings.harnessWatch};harnessDraft=null;harnessReceipt='Harnesses saved. The next launch uses the new settings.';routesView=null;last.delete('routes');} catch{if(epoch!==orgEpoch)return;harnessReceipt='Outcome unknown. Read the harnesses again before trying.';harnessSettings=false;} finally{if(epoch===orgEpoch){harnessBusy=false;if(!out())render(false);}} }); diff --git a/packages/webui/src/public/shell.css b/packages/webui/src/public/shell.css index 4525e39a0..e858a1fc8 100644 --- a/packages/webui/src/public/shell.css +++ b/packages/webui/src/public/shell.css @@ -388,6 +388,9 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed} .settings-content fieldset.harness-control{display:flex;flex-wrap:wrap;align-items:center;gap:6px 12px} .settings-content fieldset.harness-control>legend{flex-basis:100%} .settings-content fieldset.harness-control>:is(button,p){flex-basis:100%} +.settings-content fieldset.harness-control>ul.harness-watch{flex-basis:100%;margin:0;padding:0;list-style:none;display:grid;gap:4px} +.settings-content fieldset.harness-control>ul.harness-watch>li{margin:0;overflow-wrap:anywhere} +.settings-content fieldset.harness-control>ul.harness-watch>li.danger{color:var(--danger);font-weight:600} /* #1873 settings slice 5: an organization's one choice (mockup org-models). A choice not built yet is dashed and disabled. */ /* settings-s5 review P3-4: the choice cards sit in the form column, as .settings-form does. */ .settings-content fieldset.models-source{display:grid;gap:8px;max-width:28rem} diff --git a/packages/webui/tests/routes-harness-watch-browser.test.mjs b/packages/webui/tests/routes-harness-watch-browser.test.mjs new file mode 100644 index 000000000..eb0fff9c8 --- /dev/null +++ b/packages/webui/tests/routes-harness-watch-browser.test.mjs @@ -0,0 +1,104 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { browser } from './browser.mjs'; +import { startServer } from '../src/serve.mjs'; +import { close } from './fixture.mjs'; + +// #1873 harness watch, slice W3: Settings > Routing shows one line per harness from the plugins +// answer's harnessWatch. Red and missing read as a problem, a harness the watch has not checked +// reads "not checked yet" and never fit, and a bus that sends no harnessWatch gets one plain line. +// The page is served on port 0 and the bus answers are stubbed in the page; no live bus is used. +const initial = { business: 'acme', revision: 'a'.repeat(64), harnesses: ['pi'], models: ['first'], providerModels: {}, levels: { pi: ['low'] }, instances: { coder: { harness: 'pi', model: 'first', thinking: 'low', fallback: null } }, catalogue: { status: 'disabled', models: [], notices: [] } }; +const recent = new Date(Date.now() - 4 * 60 * 1000).toISOString(); +const watch = [ + { name: 'pi', state: 'fit', version: '1.6.0', checkedAt: recent, changedAt: recent, failed: [], line: 'pi 1.6.0 passed the fit check' }, + { name: 'claude-code', state: 'changed', version: '2.1.323', checkedAt: recent, changedAt: recent, failed: [], line: 'claude-code 2.1.323 passed the fit check' }, + { name: 'w3-red', state: 'red', version: '2.1.330', checkedAt: recent, changedAt: recent, failed: ['--restricted'], line: '--restricted is gone' }, + { name: 'w3-missing', state: 'missing', version: null, checkedAt: recent, changedAt: recent, failed: [], line: 'no binary from MOSAIC_PI_BIN, PATH or the repo copy' }, + { name: 'w3-unknown', state: 'unknown', version: '0.9.0', checkedAt: recent, changedAt: recent, failed: [], line: 'the fit check could not run' }, + { name: 'w3-new', state: 'unchecked', version: null, checkedAt: null, changedAt: null, failed: [], line: 'not checked yet' }, + { name: 'w3-odd', state: 'green', version: '9.9.9', checkedAt: recent, changedAt: recent, failed: [], line: 'fit' }, +]; +const settings = (extra) => ({ business: 'acme', revision: initial.revision, plugins: { harness: ['pi'] }, available: { harness: ['pi', 'claude-code'] }, ...extra }); + +async function routing(view, shot, act = null) { + const web = await startServer({ port: 0, board: 'http://127.0.0.1:9', bus: { messages: async () => ({ messages: [], unread: 0 }), usage: async () => { throw Error('fixture'); }, routes: async () => initial }, reads: { settings: () => ({ business: 'acme', release: 'fixture', notifier: {} }) }, log() {} }); + let b; + try { + b = await browser(); + const errors = []; + b.on('Runtime.exceptionThrown', x => errors.push(x.exceptionDetails?.exception?.description ?? x.exceptionDetails?.text)); + await b.call('Page.addScriptToEvaluateOnNewDocument', { source: `window.routeView=${JSON.stringify(initial)};window.harnessView=${JSON.stringify(view)};const original=window.fetch;window.fetch=async(u,o={})=>{if(u==='/api/config')return new Response(JSON.stringify({send:true}));if(u==='/api/proxy/plugins')return new Response(JSON.stringify(harnessView));if(u==='/api/bus/routes')return new Response(JSON.stringify({rows:[routeView],at:new Date().toISOString()}));if(u==='/api/proxy/plugins/set')return new Response(JSON.stringify(window.saveAnswer));return original(u,o);};` }); + const wait = expr => b.evaluate(`(async()=>{for(let n=0;n<500;n++){if(${expr})return true;await new Promise(r=>setTimeout(r,20));}throw Error('wait: '+${JSON.stringify(expr)});})()`); + if (shot) await b.viewport(1440, 900); + await b.navigate(`http://127.0.0.1:${web.address().port}/#/settings/routing`); + await wait('!!document.querySelector("#harness-save")'); + if (act) await act(b, wait); + const page = await b.evaluate(`(()=>{const f=document.querySelector("fieldset.harness-control");return { + lines:[...f.querySelectorAll("ul.harness-watch>li")].map(li=>({state:li.dataset.watchState,text:li.textContent,danger:li.classList.contains("danger"),muted:li.classList.contains("muted"),color:getComputedStyle(li).color})), + none:f.querySelector(".harness-watch-none")?.textContent??null, + label:f.querySelector("ul.harness-watch")?.getAttribute("aria-label")??null, + text:f.textContent, + muted:(()=>{const p=document.createElement("p");p.className="small muted";f.append(p);const c=getComputedStyle(p).color;p.remove();return c;})(), + danger:(()=>{const p=document.createElement("p");p.style.color="var(--danger)";f.append(p);const c=getComputedStyle(p).color;p.remove();return c;})(), + };})()`); + if (shot) { + await b.evaluate('document.querySelector("fieldset.harness-control").scrollIntoView({block:"center"});true'); + await b.screenshot(shot); + } + assert.deepEqual(errors, []); + return page; + } finally { await b?.close(); await close(web); } +} + +test('W3 Routing shows one line per harness for each watch state, red and missing as a problem, unchecked never as fit', { timeout: 60000 }, async () => { + const page = await routing(settings({ harnessWatch: watch }), process.env.W3_SHOT || null); + assert.equal(page.none, null); + assert.equal(page.label, 'What the last harness check found'); + assert.deepEqual(page.lines.map(l => l.state), ['fit', 'changed', 'red', 'missing', 'unknown', 'unchecked', 'unchecked']); + const [fit, changed, red, missing, unknown, unchecked, odd] = page.lines; + assert.equal(fit.text, 'pi 1.6.0 · checked 4 min ago · fit'); + assert.equal(changed.text, 'claude-code 2.1.323 · changed 4 min ago · fit'); + assert.equal(red.text, 'w3-red 2.1.330 · checked 4 min ago · red · --restricted is gone · new launches refuse'); + assert.equal(missing.text, 'w3-missing · checked 4 min ago · missing · no binary from MOSAIC_PI_BIN, PATH or the repo copy'); + assert.equal(unknown.text, 'w3-unknown 0.9.0 · checked 4 min ago · unknown · the fit check could not run'); + assert.equal(unchecked.text, 'w3-new · not checked yet'); + assert.equal(odd.text, 'w3-odd · not checked yet', 'a state the page does not know is not checked yet, not its line and not fit'); + for (const l of [red, missing]) assert.ok(l.danger && !l.muted, `${l.state} reads as a problem`); + for (const l of [fit, changed, unknown, unchecked, odd]) assert.ok(!l.danger && l.muted, `${l.state} is not a problem line`); + assert.notEqual(red.color, page.muted, 'a problem line is not drawn in the muted colour'); + for (const l of [red, missing]) assert.equal(l.color, page.danger, `${l.state} is drawn in the danger colour, as r3 gives can't start`); + for (const l of [unknown, unchecked, odd]) assert.doesNotMatch(l.text, /· fit\b/, `${l.state} never reads as fit`); +}); + +test('W3 Routing: a plugins answer with no harnessWatch, or one that is not a list, gives one plain line and no guess', { timeout: 60000 }, async () => { + for (const extra of [{}, { harnessWatch: { pi: { state: 'fit' } } }, { harnessWatch: 'fit' }, { harnessWatch: null }]) { + const page = await routing(settings(extra)); + assert.deepEqual(page.lines, [], JSON.stringify(extra)); + assert.equal(page.none, 'What the harness check found could not be read.', JSON.stringify(extra)); + assert.doesNotMatch(page.text, /\bfit\b/, JSON.stringify(extra)); + } + const empty = await routing(settings({ harnessWatch: [] })); + assert.deepEqual([empty.lines, empty.none], [[], null], 'an empty list (no harness installed) adds nothing'); +}); + +test('W3 Routing: a bus-authored line or version is shown as text, never as markup, and a time that is not a time is not shown', { timeout: 60000 }, async () => { + const page = await routing(settings({ harnessWatch: [{ name: 'pi', state: 'red', version: '1', checkedAt: recent, changedAt: recent, failed: [], line: 'gone' }, { name: 'w3-time', state: 'fit', version: '1', checkedAt: '/srv/host/place', changedAt: '/srv/host/place', failed: [], line: 'x' }] })); + assert.equal(page.lines.length, 2); + assert.equal(page.lines[1].text, 'w3-time 1 · fit', 'a time that is not a time is left out, not shown'); + assert.match(page.lines[0].text, /1<\/b>/); + assert.match(page.lines[0].text, / { + const save = (answer) => async (b, wait) => { + await b.evaluate(`window.saveAnswer=${JSON.stringify(answer)};document.querySelector("[data-harness=claude-code]").click();document.querySelector("#harness-save").click();true`); + await wait('document.querySelector("#harness-status")?.textContent.includes("Harnesses saved")'); + }; + const saved = { business: 'acme', revision: 'c'.repeat(64), plugins: { harness: ['pi', 'claude-code'] }, transaction: 'fixture' }; + const kept = await routing(settings({ harnessWatch: watch.slice(0, 3) }), null, save(saved)); + assert.equal(kept.none, null, 'a save answer with no harnessWatch keeps the lines read before'); + assert.deepEqual(kept.lines.map(l => l.state), ['fit', 'changed', 'red']); + const fresh = await routing(settings({ harnessWatch: watch.slice(0, 3) }), null, save({ ...saved, harnessWatch: [watch[3]] })); + assert.deepEqual(fresh.lines.map(l => l.state), ['missing'], 'a save answer with harnessWatch shows its lines'); +});