From af8c2f97773eb26b0081dc623ec670e6c71da846 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 04:12:24 -0600 Subject: [PATCH] fix(plugins): harness-watch-w1 review: a record carries lastDigest, the latest digest it knew, so a new binary after an unknown, red, missing, timeout or throw reads changed, and when neither record has a digest the version stands in, so red 2.0 to red 2.1 is a transition; the inspect answer is copied inside the try to own data fields only, so a getter, a Proxy or an inherited field is a failed inspect (unknown) and not a failed pass; redaction also covers realpaths, trailing slashes, the user name as a word () and any absolute path left (), and the loader's refusal messages; a source or rule id equal to an env value is dropped; pi's "can't be checked" match reads only after its lookup label; claude-code skips relative PATH entries and hashes and runs the same resolved file; README updated (#1873, lane harness-watch-w1) --- packages/plugins/README.md | 38 +++-- .../plugins/harness/claude-code/index.mjs | 14 +- packages/plugins/harness/pi/index.mjs | 10 +- packages/plugins/src/harness-watch.mjs | 138 ++++++++++++---- packages/plugins/tests/harness-watch.test.mjs | 154 +++++++++++++++++- 5 files changed, 300 insertions(+), 54 deletions(-) diff --git a/packages/plugins/README.md b/packages/plugins/README.md index d4a3ab5dd..27d6a357c 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -401,15 +401,19 @@ that check's digest of the binary's files; a failed check gives none. A failure whose line says "can't be checked" is `fit: "unknown"`. `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 -with only PATH and a scratch HOME. Its fit stays `"unknown"` until slice W2 -adds the claude fit rule. +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). A module with no `inspect` is watched through `version({ env })`, with fit `"unknown"`. A record's `state` is: -- `fit`: found, the fit check passed, same digest as the last record (or no - last digest); -- `changed`: found, the fit check passed, a new digest; +- `fit`: found, the fit check passed, the last known digest (or none known); +- `changed`: found, the fit check passed, a digest that is not the last known + one. The last known digest is the latest any earlier record saw, kept in + each record's `lastDigest` across unknown, red and missing records, so a + new binary first seen through a timeout still reads `changed`; - `red`: found, and the fit check failed with `definite: true`; - `unknown`: anything else, including no `inspect`, an `inspect` that throws or runs past `timeoutMs` (default 300 s), and an answer that isn't this @@ -417,14 +421,22 @@ A module with no `inspect` is watched through `version({ env })`, with fit - `missing`: `inspect` says `found: false`. Only `found: true` with `fit: "pass"` reads as `fit` or `changed`. The watch -checks each field's shape and drops what doesn't fit (a `source` that isn't -a plain name, a digest that isn't 64 hex characters). In every text field it -replaces the scratch directory, the home directory, every env value of six -characters or more and every PATH entry with a name for it. A record also -carries `via` (`inspect` or `version`), `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 digest is not the last -record's. +copies the answer's own data fields only: a field that is a getter, or an +answer that throws when read (a Proxy), is a failed `inspect`, and an +inherited field is not read. It checks each field's shape and drops what +doesn't fit (a `source` that isn't a plain name, a digest that isn't 64 hex +characters, a `source` or rule id that is an env value). In every text +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` +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. ## Transcript usage source (row 124) diff --git a/packages/plugins/harness/claude-code/index.mjs b/packages/plugins/harness/claude-code/index.mjs index 9f82533cf..3628cfae7 100644 --- a/packages/plugins/harness/claude-code/index.mjs +++ b/packages/plugins/harness/claude-code/index.mjs @@ -93,21 +93,25 @@ export function version({ env }) { // #1873 harness watch W1: 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. Fit stays -// "unknown": the claude fit rule (every flag the adapter passes is in --help) is slice W2. +// 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. const CAPTURE_TIMEOUT = 10000; const CAPTURE_MAX = 1024 * 1024; export async function inspect({ env = {}, scratch = null, signal } = {}) { - const bin = binary(env); + const bin = binary({ ...env, PATH: (env.PATH ?? "").split(delimiter).filter((dir) => isAbsolute(dir)).join(delimiter) }); 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" }; - const digest = await sha256(realpathSync(bin)); + 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 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(bin, ["--version"], runEnv, signal), await capture(bin, ["--help"], runEnv, signal)]; + 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 } }; diff --git a/packages/plugins/harness/pi/index.mjs b/packages/plugins/harness/pi/index.mjs index fbf417297..f7d62c708 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -59,8 +59,8 @@ export async function version({ env = {} } = {}) { // 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 line says "can't be checked" could not -// decide, so its fit is "unknown"; any other failure is definite. source names the lookup step, +// 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. 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 = {} } = {}) { @@ -69,7 +69,11 @@ export async function inspect({ env = {} } = {}) { const source = SOURCES[at.label] ?? null; const fit = await piFit(at, { levels: LEVELS }); 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: {} }; - const undecided = /can't be checked/.test(fit.message); + // Read after pi-fit's "pi (