diff --git a/adapters/pi/adapter.sh b/adapters/pi/adapter.sh index 64a4290b9..4ea49eaf7 100644 --- a/adapters/pi/adapter.sh +++ b/adapters/pi/adapter.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Pi adapter: implements the Mosaic adapter contract for the pinned -# @earendil-works/pi-coding-agent CLI. +# Pi adapter: implements the Mosaic adapter contract for the +# @earendil-works/pi-coding-agent CLI. Its behaviour notes are read from 0.85.1. # # Contract: see /opt/mosaic/adapters/README.md. # Headless (default): stdout = response only; stderr = diagnostics; exit 0. @@ -92,6 +92,11 @@ fi THINKING_FLAG="" [ -n "${MOSAIC_THINKING:-}" ] && THINKING_FLAG="--thinking $MOSAIC_THINKING" +# MCP (#1873 S3): the harness module sets MOSAIC_PI_NO_MCP=1 when the binary's --help lists +# --no-mcp, so MCP stays off by its own flag and not only as a side effect of --no-extensions. +NO_MCP_FLAG="" +[ "${MOSAIC_PI_NO_MCP:-}" = "1" ] && NO_MCP_FLAG="--no-mcp" + # Mode (M13): interactive TUI or one-shot print. PRINT_MODE="-p" REQUEST_ARG="" @@ -107,6 +112,7 @@ fi # --system-prompt replace the default prompt with the generated one, given # as the file's absolute path (checked above), never its text # --no-* no ambient context/skills/extensions/templates/themes +# NO_MCP_FLAG --no-mcp when the binary lists it, else nothing # EXT_FLAGS the explicitly provided extensions only (-e) # SESSION_FLAGS ephemeral | persistent | forked (per env) # TOOLS_FLAG per capabilities @@ -118,6 +124,7 @@ set -- \ --offline \ --no-approve \ --no-extensions \ + $NO_MCP_FLAG \ $EXT_FLAGS \ $SKILLS_FLAG \ --no-prompt-templates \ @@ -134,6 +141,7 @@ set -- \ [ "$INTERACTIVE" = "1" ] || set -- "$@" -p "$MOSAIC_REQUEST" if [ -n "${MOSAIC_MODEL_EVENT_MONITOR:-}" ] && [ "$INTERACTIVE" != "1" ]; then STREAM="$(dirname "$0")/../../packages/plugins/harness/pi/model-stream.mjs" - exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" pi "$@" --mode json + exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" "${MOSAIC_PI_BIN:-pi}" "$@" --mode json fi -exec pi "$@" +# #1873 S2: the binary the harness module found (MOSAIC_PI_BIN, its path inside the sandbox). +exec "${MOSAIC_PI_BIN:-pi}" "$@" diff --git a/packages/bus/tests/model-hire-ownership.test.mjs b/packages/bus/tests/model-hire-ownership.test.mjs index 9abdb20c4..525693612 100644 --- a/packages/bus/tests/model-hire-ownership.test.mjs +++ b/packages/bus/tests/model-hire-ownership.test.mjs @@ -6,6 +6,7 @@ import { join } from 'node:path'; import { sandboxProblem } from '../../seat/src/sandbox.mjs'; import { startTimeOf } from '../../seat/src/proc.mjs'; import { ownershipFixture, waitFor } from './model-hire-ownership-fixture.mjs'; +import * as pi from '../../plugins/harness/pi/index.mjs'; const unavailable=sandboxProblem()??false; // Any skip means UNVERIFIED, not green acceptance. const done=async box=>{await box.promise;return box;}; const refused=async(box,code)=>{await done(box);assert.equal(box.ok,false,'real operation refuses');assert.equal(box.code,code,'real operation preserves exact refusal');}; @@ -17,7 +18,12 @@ test('655 listed duplicate with no record refuses before any model or management assert.deepEqual(f.snapshot(),before,'duplicate hire makes no credential or management effects');f.assertHistoryGuard();f.valueFree(); }); -test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:30000},async t=>{ +test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:120000},async t=>{ + // #1873 pi-unpin re-check 9 and 10: the launch's preflight shares pi's fit check, which is cold in + // a fresh process. Wait for it before the fixture starts its launcher, so the launcher's warm-up + // and the launch's preflight hit the cache, the gate's 5 s times the launch, and the launch starts + // at the same point in the launcher's 2 s tick as it did before the fit check existed. + await pi.ready({env:{PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR}}); const f=await ownershipFixture(t),pGet=f.hold('GET'),p=f.launchAide();await pGet.entered(); // P's own acquisition GET is held. Its PUT confirmation GET is ordinal+1; // only an OLD H reuse GET can be ordinal+2. The correct early refusal removes it. diff --git a/packages/cli/src/launcher.mjs b/packages/cli/src/launcher.mjs index dd0b02b21..bd2dbba03 100644 --- a/packages/cli/src/launcher.mjs +++ b/packages/cli/src/launcher.mjs @@ -215,12 +215,19 @@ const relayed = (failed) => failed && (typeof failed.message === "string" ? { .. // and names the module rather than escaping the host. What it threw is named by its system code // or its class, never its message, which the host didn't write (#1873). function call(name, what, fn) { - try { - return fn(); - } catch (e) { + const failed = (e) => { const thrown = quietMessage(e, "a host file") ?? `threw ${/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(String(e?.name)) ? e.name : "a value"} without a system code`; throw new Refusal("harness-failed", `harness ${name} ${what}: ${thrown}`); + }; + let out; + try { + out = fn(); + } catch (e) { + failed(e); } + // #1873 pi-unpin re-check 7: a module may answer with a promise (pi's preflight runs its fit + // check as child processes). The caller awaits it; a rejection is refused as a throw is. + return typeof out?.then === "function" ? Promise.resolve(out).then(undefined, failed) : out; } // host: startHost()'s handle. tracker: true when the broker has task verbs for @@ -454,14 +461,11 @@ export function createLauncher({ for (const m of [primary, fallback].filter(Boolean)) { if (!on.includes(m.harness)) throw new Refusal("harness-off", `harness ${m.harness} isn't switched on in plugins.harness`); } - // One harness through a fresh loader, so a refusal never leaves an earlier load's module in use. const harnessOf = async (m) => { - const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log: (entry) => record({ ...entry, instance }) }); - const { refused } = await loader.load({ harness: [m.harness] }); + const { refused, module } = await freshHarness(m, (entry) => record({ ...entry, instance })); // #1873: a loader message can quote the module's path or its load error; only a // missing-export one, which names the export, is relayed. Any other refusal names its code. if (refused.length) throw new Refusal("harness-refused", `harness ${m.harness}: ${refused.map((r) => r.code === "missing-export" ? `${r.code}: ${relay(r.message)}` : String(r.code)).join("; ")}`); - const module = loader.modules("harness")[0]; const levels = call(m.harness, "levels", () => module.levels()); if (!levels.includes(m.thinking)) throw new Refusal("thinking-unsupported", `harness ${m.harness} takes thinking ${levels.join(", ")}, not ${m.thinking}`); return module; @@ -556,7 +560,7 @@ export function createLauncher({ const pickRoute = async (proxyOf) => { let sessionProxy = await proxyOf(primary); // #1873: a preflight's message is the module's own text, relayed with host paths replaced. - const failed = relayed(call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy }))); + const failed = relayed(await call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy }))); if (failed) { if (!fallback) throw new Refusal(failed.code, failed.message); // The fallback only on a failed primary preflight, and only at launch (row 65). Any refusal of @@ -566,7 +570,7 @@ export function createLauncher({ throw e; }); sessionProxy = await proxyOf(fallback); - const also = relayed(call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy }))); + const also = relayed(await call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy }))); if (also) throw new Refusal(also.code, `fallback ${fallback.harness}: ${also.message} (primary ${primary.harness}: ${failed.message})`); [model, harness, route, routeReason] = [fallback, second, "fallback", failed.code]; } @@ -740,7 +744,7 @@ export function createLauncher({ if (store) compactions(store, model.harness, harness, { instance, thread, session, run }); const toolSocket = join(runDir, "tools.sock"); const turnMarker = join(runDir, "turn.done"); - const version = harnessVersion !== undefined ? harnessVersion : call(model.harness, "version", () => harness.version({ env })); + const version = harnessVersion !== undefined ? harnessVersion : await call(model.harness, "version", () => harness.version({ env })); // Row 137 (#1631): a role and thread holding an approved, unspent merge decision get land_work. // The broker checks the decision again on every land; this only decides what the bundle shows. // Row 156 (#1651): a non-thread instance's land and landing come from one landHolders read, so a @@ -1670,6 +1674,38 @@ export function createLauncher({ clearTimeout(timer); } + // One harness ({ harness: name }) through a fresh loader, so a refusal never leaves an earlier + // load's module in use: { refused, module }, module null when refused. A launch (harnessOf) and + // the start's fit warm-up below (#1873 re-check 8) both load through here. + async function freshHarness(m, log) { + const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log }); + const { refused } = await loader.load({ harness: [m.harness] }); + return { refused, module: refused.length ? null : loader.modules("harness")[0] }; + } + + // #1873 pi-unpin re-check 8: each switched-on harness module with ready() (pi's fit check) starts + // it here, in the background, so the first launch joins the check in flight instead of starting + // it. Nothing waits on it: a launch still awaits its own preflight, which shares this check or + // reads its pass. A warm-up that fails or throws is dropped; it caches nothing, and the launch's + // preflight runs the check again. Re-check 9: a harness plugin the warm-up can't load leaves one + // launch log entry, by its code only (a launch that loads it records the message). + function warmHarnesses() { + const on = Array.isArray(b.plugins?.harness) ? b.plugins.harness : []; + for (const name of on) { + (async () => { + let logged = false; + const refusal = (entry) => { + if (logged || entry?.event !== "plugin-refused") return; + logged = true; + record({ event: "plugin-refused", kind: entry.kind, name: entry.name, code: entry.code, during: "warm-up" }); + }; + const { module } = await freshHarness({ harness: name }, refusal); + if (typeof module?.ready === "function") await module.ready({ env }); + })().catch(() => {}); + } + } + if (launches) warmHarnesses(); + host.beforeClose(close); return { business: b, registry, listen, launchPm, fromSession, tick, close }; } diff --git a/packages/cli/tests/fixtures/request-agent.mjs b/packages/cli/tests/fixtures/request-agent.mjs index 027d5ed04..5ab691be0 100644 --- a/packages/cli/tests/fixtures/request-agent.mjs +++ b/packages/cli/tests/fixtures/request-agent.mjs @@ -16,7 +16,7 @@ import { pushWorking, fetchReview } from '../../../harness/src/git-tools.mjs'; import { callTool } from '../../../harness/src/tools.mjs'; import { toolServer } from '../../../harness/src/runner.mjs'; import { buildBundle } from '../../../harness/src/bundle.mjs'; -import { HARNESS } from '../../../harness/tests/helpers.mjs'; +import { HARNESS, harnessReady } from '../../../harness/tests/helpers.mjs'; import { ensureCheckouts } from '../../../seat/src/checkout.mjs'; import { newRun, Registry, readSessions, writeSessionFile, workspaceDir } from '../../../seat/src/session.mjs'; import { startTimeOf } from '../../src/host.mjs'; @@ -127,6 +127,7 @@ async function runtime() { const cap = bus.bindLaunch(launch_record).cap; roles[role] = { cap, run: id, workspace, runDir }; call(role, 'role.claim'); + await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check const bundle = buildBundle({ dir: join(runDir, 'bundle'), resolved, contract: readFileSync(resolved.contract, 'utf8'), arbiter: role === 'cto', tracker: Boolean(resolved.tracker), credentials: [], skillsDir: join(dataRoot, 'skills-enabled'), harness: HARNESS.pi, harnessVersion: '0.85.1', session: { run: id, workspace, checkouts, toolSocket: join(runDir, 'tools.sock'), turnMarker: join(runDir, 'turn.done') } }); diff --git a/packages/cli/tests/launcher.test.mjs b/packages/cli/tests/launcher.test.mjs index a5e41bc2c..f4f060021 100644 --- a/packages/cli/tests/launcher.test.mjs +++ b/packages/cli/tests/launcher.test.mjs @@ -12,7 +12,8 @@ import { createHash } from "node:crypto"; import { once } from "node:events"; import { createInterface } from "node:readline"; import { connect } from "node:net"; -import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; +import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, watch, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { Client } from "../../bus/src/client.mjs"; @@ -24,6 +25,8 @@ import { bootConfig, loadSystem } from "../src/config.mjs"; import { cmdlineOf, startHost, startTimeOf } from "../src/host.mjs"; import { createLauncher } from "../src/launcher.mjs"; import { mockAnthropic, REPO } from "../../harness/tests/helpers.mjs"; +import { piStandIn } from "../../harness/tests/pi-stand-in.mjs"; +import * as pi from "../../plugins/harness/pi/index.mjs"; import { fakeHarnesses, fixture, HOST_CREDENTIALS, tmp, withoutHostCredentials } from "./helpers.mjs"; const FAKE = fileURLToPath(new URL("../../harness/tests/fixtures/fake-adapter.mjs", import.meta.url)); @@ -266,6 +269,142 @@ test("bus start --pm: the launcher instance (the chief of staff) runs under its assert.equal(end.exitCode, 0); }); +// #1873 pi-unpin S1/S2: the version label comes from the pi the module finds. A pi whose --version +// fails gives a null label, and the launch still goes ahead. +test("pi-unpin: a pi named by MOSAIC_PI_BIN that exits 1 on --version records a null harnessVersion and still launches", { skip, timeout: 30000 }, async (t) => { + const prepared = prepare(t); + const pkg = join(prepared.root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent"); + mkdirSync(join(pkg, "dist", "bundle"), { recursive: true }); + writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } })); + // #1873 S3: the stand-in passes the fit check; only its --version fails. + writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ versionExit: 1 }), { mode: 0o755 }); + mkdirSync(join(prepared.root, "pi-prefix", "bin")); + const bin = join(prepared.root, "pi-prefix", "bin", "pi"); + symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin); + prepared.f.env.MOSAIC_PI_BIN = bin; + // env() reads the bus's own environment, as the launcher's is process.env outside tests. + const before = process.env.MOSAIC_PI_BIN; + process.env.MOSAIC_PI_BIN = bin; + t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; }); + const { f, launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined }); + const chief = await launcher.launchPm(); + assert.equal(chief.harness, "pi"); + const [s] = readSessions(f.dataRoot); + const manifest = JSON.parse(readFileSync(join(s.runDir, "bundle", "manifest.json"), "utf8")); + assert.equal(manifest.harnessVersion, null); + assert.equal(await close(), 0); +}); + +// #1873 re-check 8: a pi stand-in under root/pi-prefix, as an npm prefix holds it. Returns its bin. +function piPrefix(root, knobs) { + const pkg = join(root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent"); + mkdirSync(join(pkg, "dist", "bundle"), { recursive: true }); + writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } })); + writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn(knobs), { mode: 0o755 }); + mkdirSync(join(root, "pi-prefix", "bin")); + const bin = join(root, "pi-prefix", "bin", "pi"); + symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin); + return bin; +} +// Each fit run makes one scratch directory under tmpdir(), so a watch there counts the runs. +// quiet() waits until no run has started for a while, so a check an earlier test left going +// (the real pi's, warmed by its launcher) is not counted. +function fitRuns(t) { + const made = new Set(); + const watcher = watch(tmpdir(), (event, name) => { if (name && name.startsWith("mosaic-pi-fit-")) made.add(name); }); + t.after(() => watcher.close()); + const quiet = async () => { + let n; + do { n = made.size; await new Promise((r) => setTimeout(r, 700)); } while (made.size !== n); + }; + return { made, quiet }; +} +function busPiBin(t, prepared, bin) { + prepared.f.env.MOSAIC_PI_BIN = bin; + const before = process.env.MOSAIC_PI_BIN; + process.env.MOSAIC_PI_BIN = bin; + t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; }); +} + +// #1873 pi-unpin re-check 8: the launcher starts pi's fit check when it starts, before any launch, +// and does not wait for it. The first launch joins that check: the two together make one check's +// runs, not two. +test("pi-unpin re-check 8: the launcher starts pi's fit check when it starts, and the first launch joins it", { skip, timeout: 60000 }, async (t) => { + const { made, quiet } = fitRuns(t); + // One check on its own, for another stand-in: how many runs it makes. + const alone = piPrefix(tmp(t, "mosaic-launch-pi-"), {}); + await quiet(); + made.clear(); + assert.equal(await pi.preflight({ env: { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: alone } }), null); + await quiet(); + const runs = made.size; + assert.ok(runs > 10, `one check made ${runs} runs`); + const prepared = prepare(t); + busPiBin(t, prepared, piPrefix(prepared.root, {})); + made.clear(); + const { launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined }); + // No launch yet, and the check is under way. + await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch")); + const chief = await launcher.launchPm(); + assert.equal(chief.harness, "pi"); + await quiet(); + assert.equal(made.size, runs, "the launch joined the check the launcher started"); + assert.equal(await close(), 0); +}); + +// A warm-up that fails leaves no unhandled rejection and keeps no verdict: the next preflight runs +// the check again and refuses on its own. +test("pi-unpin re-check 8: a fit check the launcher starts that fails is dropped: no unhandled rejection, nothing kept", { skip, timeout: 60000 }, async (t) => { + const { made, quiet } = fitRuns(t); + const unhandled = []; + const seen = (e) => unhandled.push(e); + process.on("unhandledRejection", seen); + t.after(() => process.off("unhandledRejection", seen)); + const prepared = prepare(t); + const bin = piPrefix(prepared.root, { dropFlags: ["--no-themes"] }); + busPiBin(t, prepared, bin); + await quiet(); + made.clear(); + const { close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined }); + await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch")); + // This preflight joins the failing check; then it is gone. + const env = { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: bin }; + const refusal = { code: "harness-feature-missing", message: "pi 9.9.9 (MOSAIC_PI_BIN) lacks --no-themes" }; + assert.deepEqual(await pi.preflight({ env }), refusal); + await quiet(); + await new Promise((r) => setImmediate(r)); + assert.deepEqual(unhandled, []); + made.clear(); + assert.deepEqual(await pi.preflight({ env }), refusal); + await quiet(); + assert.ok(made.size > 0, "the failed check was kept"); + assert.equal(await close(), 0); +}); + +// Re-check 9: a harness plugin the warm-up can't load leaves one launch log entry, by its code +// only, so the message (which may name a host path) stays out of it. +test("pi-unpin re-check 9: a harness plugin the launcher's warm-up can't load leaves one launch log entry, by code only", { skip, timeout: 30000 }, async (t) => { + const given = prepare(t, (doc) => { + doc.plugins = { ...doc.plugins, harness: [...doc.plugins.harness, "broken"] }; + return doc; + }); + const makeLoader = await fakeHarnesses(t, { pi: given.adapter, "claude-code": given.adapter }, (write) => { + write("harness", "broken", `throw new Error("broken at /srv/fit-secret-path");\n`); + }); + const { f, close } = await setup(t, undefined, given, { makeLoader }); + const refused = () => existsSync(launchLogFile(f.dataRoot, "acme")) ? log(f).filter((e) => e.event === "plugin-refused") : []; + await until(() => refused().length > 0, 10000).catch(() => assert.fail("the warm-up left no record of the refused plugin")); + await new Promise((r) => setTimeout(r, 500)); + const entries = refused(); + assert.equal(entries.length, 1, JSON.stringify(entries)); + const [entry] = entries; + assert.deepEqual(Object.keys(entry).sort(), ["at", "code", "during", "event", "kind", "name"]); + assert.deepEqual([entry.kind, entry.name, entry.during], ["harness", "broken", "warm-up"]); + assert.match(entry.code, /^[a-z][a-z-]+$/); + assert.doesNotMatch(readFileSync(launchLogFile(f.dataRoot, "acme"), "utf8"), /fit-secret-path/); + assert.equal(await close(), 0); +}); + test("the chief of staff launches a coder through its launch tool; the coder answers; refusals name their code", { skip, timeout: 30000 }, async (t) => { const { f, launcher, client, cto } = await setup(t); const sock = launchSocketPath(f.dataRoot); diff --git a/packages/cli/tests/path-redaction-labels.test.mjs b/packages/cli/tests/path-redaction-labels.test.mjs index 16a528381..ce6f30bee 100644 --- a/packages/cli/tests/path-redaction-labels.test.mjs +++ b/packages/cli/tests/path-redaction-labels.test.mjs @@ -90,6 +90,8 @@ async function routed(t, harness) { const makeLoader = await fakeHarnesses(t, { pi: null, 'claude-code': null }, write => { // fsthrows: its preflight reads a missing file, so the error's message quotes the path. write('harness', 'fsthrows', `export * from ${PI};\nimport { readFileSync } from "node:fs";\nexport function preflight() { readFileSync(${JSON.stringify(absent)}); }\n`); + // fsrejects (#1873 re-check 8): an async preflight, as pi's is, whose promise rejects with that error. + write('harness', 'fsrejects', `export * from ${PI};\nimport { readFileSync } from "node:fs";\nexport async function preflight() { readFileSync(${JSON.stringify(absent)}); }\n`); // plainthrows: a plain Error whose message quotes a host path, as a module's own text. write('harness', 'plainthrows', `export * from ${PI};\nexport function preflight() { throw new Error(${JSON.stringify(`cannot read ${absent}`)}); }\n`); // pathfails: a preflight that returns a failure whose message quotes a host path. @@ -146,6 +148,15 @@ test('a harness whose preflight throws a system error refuses harness-failed nam clean(JSON.stringify(s.last()), s.root); }); +// #1873 re-check 8 P3-1: a preflight promise that rejects is refused as a throw is, by the call and +// the system code, never the raw error or its path. +test('a harness whose async preflight rejects with a system error refuses harness-failed naming the call and code, never the path', async t => { + const s = await routed(t, 'fsrejects'); + assert.deepEqual(await s.launch(), { ok: false, error: 'harness-failed' }); + assert.equal(s.last().why, 'harness fsrejects preflight: cannot open a host file (ENOENT)'); + clean(JSON.stringify(s.last()), s.root); +}); + test('a harness the loader refuses names its code only, never the loader message or a path', async t => { const s = await routed(t, 'absent'); assert.deepEqual(await s.launch(), { ok: false, error: 'harness-refused' }); diff --git a/packages/harness/README.md b/packages/harness/README.md index 5dc337f35..276454420 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -18,13 +18,103 @@ Every harness layer below is a row S0 line (`docs/plans/2026-10-04_slice-1.md`, "What S6 can rely on"), and each bundle's `manifest.json` names the lines it relies on (`reliesOn`): -| | Pi | Claude Code | -|---|---|---| -| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed | -| 2. gate crash | a throw in the handler blocks | the hook runs as ` \|\| exit 2` | -| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks | -| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 \|\| exit 2`, hook timeout 20 | -| 5. bash | limited only by the tool limit | limited only by the tool limit | +| | Pi | Claude Code | How pi's line is checked | +|---|---|---|---| +| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed | suites, on the repository's pi (`pi-session.test.mjs`) | +| 2. gate crash | a throw in the handler blocks | the hook runs as ` \|\| exit 2` | suites, on the repository's pi | +| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks | at every start, by the fit check, on the pi that runs; and suites | +| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 \|\| exit 2`, hook timeout 20 | suites, on the repository's pi | +| 5. bash | limited only by the tool limit | limited only by the tool limit | suites, on the repository's pi | + +Pi is not pinned (#1873 pi-unpin). Before a session starts, `preflight` +awaits the fit check in `src/pi-fit.mjs` on the binary the module found +(the Triage host and route await the module's `ready` instead), and +`env` reads its pass. It runs that binary in a sandbox with the network +unshared and calls no model. Each run is a child process the check +awaits, up to four at a time, so the bus's event loop keeps running +while it does; callers that ask while a check runs share it. Each run +gets the environment a bus session's pi gets, built by the same code +(the launcher's `ENV_ALLOW` and `sessionEnv`, the sandbox's +`sessionSetenv`, the runner's `adapterEnv` and the pi module's +`harnessEnv`) from the bus process's own environment, less the provider +keys, and starts the binary through `/bin/sh` as the adapter does. So +`HOME` is a scratch home, `TMPDIR` is `/tmp` bound to scratch, a run has +`TERM`, `TZ` or `LC_ALL` only when the bus has it (a bus started by its +unit has none), and no run sets `PI_CODING_AGENT_DIR`, which no session +has: pi's agent directory is `$HOME/.pi/agent`, where the user sources +are planted. A test fails when the fit runs' names, or the values the +code sets, differ from a bus session's, apart from the provider +variables, for a login shell's host and a unit's. It needs +`--help` to list, in its option column, every flag the adapter passes +(with rpc mode and each thinking level), and a missing `-e` file to stop +the start in rpc and in `-p`. The planted ambient sources must stay +unloaded with the seal flags on (skills and prompt templates are read +from `get_commands` in rpc and from the `-e` extension's command list in +`-p`): extensions, MCP +servers, skills and prompt templates (found in their directories, listed +in a `settings.json`, or in a local package a `settings.json` lists), +context files (`AGENTS.md`, `CLAUDE.md`, `SYSTEM.md`) and a trusted +project's `.pi/SYSTEM.md` and `.pi/APPEND_SYSTEM.md`. The project has a +saved trust, so only `--no-approve` keeps those two out. An `-e` +extension writes the system prompt, command names and session id it sees +at `session_start`; the sealed runs must show the `--system-prompt` file +and none of the planted sources. A control run passes the seal flags less every loader flag +(`--no-approve`, `--no-extensions`, `--no-skills`, +`--no-prompt-templates`, `--no-context-files`, and `--no-mcp` when +listed), so it keeps `--offline`, `--no-themes`, `--no-tools` and +`--no-session`, and the same `-e` and `--system-prompt`. It must show +each kind of source (an MCP server only when `--help` lists `--no-mcp`) +and answer `get_state` and `get_commands`, and its `-e` extension must +list the planted skill and prompt template, or the check can't tell and +refuses. For every kind but the trusted project files, only a source +outside the project counts there, since a project source can wait on +trust and `--no-approve` would then hide a seal flag the binary ignores. +Then one sealed rpc run per loader flag leaves out only that flag: its +kind must show, and no other kind (an MCP server may show beside an +extension when `--help` lists no `--no-mcp`, since pi then ties MCP to +`--no-extensions`). So each loader flag is shown to keep its own kind +out, and no other seal flag, nor their order, can stand in for it. Five +sealed `-p` runs have a bus session's shape: the arguments the adapter +passes for a session, in its order, from `sessionShape` in +`src/pi-fit.mjs`, with two `-e` and `--session-dir`. The adapter's four +session branches (a tool list or `--no-tools`, `-c` over a stored +session or none, `--thinking` or none, `--mode json` or none) are spread +over the five runs so every pair of them is passed together. A `-c` run +gets a stored session and must resume it. Each must load both `-e` +files and show no planted source. A test drives the adapter, in a bus +session's environment, through every branch set with no tools, one or +two, `-c` over a pi-named session file or an unrelated file, each +thinking level and each provider a session can name. It runs over a +login shell's host and a unit's, and fails on an argv `sessionShape` +doesn't build, a pair no run passes, or a variable the adapter reads +that no session sets. So a branch on `TZ` or `LC_ALL` being unset, or on +`TERM` missing from the environment, turns it red. (Bash run as +`/bin/sh` sets an unset `TERM` to `dumb` for the shell without exporting +it, so `${TERM:-}` in the adapter is never empty, in a session or here.) +It can't see a branch keyed on a +value it doesn't try (a model, a path, another tool or provider). A last sealed `-p` run uses `--no-session`, as +worker runs do. Each run gets a fresh scratch directory, so no run sees +what another left. Not run: `--fork`, `--skill` and the interactive TUI +(worker containers and `scripts/agent.sh` only), the branches three or +four at a time beyond the five runs, `-c` over a session directory that +holds nothing for the project (one more run costs about 340 ms cold), +and a session's own values (tools beyond `read` and `bash`, thinking +levels beyond `low`, provider, model, key, `-e` files, request). The +runs are offline with no key, so nothing past the start is tried. A pi +built to act one way in the fit runs and another way with those is +outside what a start-time check can catch. A run killed for taking +over 20 s or printing over 4 MB can't be checked and says which. A +`--version` that fails or hangs past 3 s only leaves no label. Pi +0.85.1 appends the agent directory's `APPEND_SYSTEM.md` whatever the +flags, so the check doesn't plant one. Any failure refuses the launch with +`harness-feature-missing` and one line that names the binary by its label, +for example `pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files`. When +`--help` lists `--no-mcp`, the adapter passes it too. A pass is cached in +memory by a digest of the package's files (and npm's lock files, for +the repository's copy) and the names in the runs' environment, so a bus +restarted under other names checks again. Lines 1, 2, 4 and 5 are not +checked at start; a pi other than the repository's copy relies on them +unproved until a scripted-model fit run exists (design section 5, S6). Claude Code also runs with `--restricted`: no user, project or local settings, file tools confined to the working directory, and no `CLAUDE.md` diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs new file mode 100644 index 000000000..5207e7acd --- /dev/null +++ b/packages/harness/src/pi-fit.mjs @@ -0,0 +1,778 @@ +// #1873 pi-unpin S3: the start-time fit check for a pi binary the harness module found. Nothing +// here compares a version. The check runs the binary only inside a sandbox with the network +// unshared, the found package mounted as a session mounts it, and a bus session's environment +// (fitEnv below), so its agent directory is $HOME/.pi/agent under a scratch HOME, as a session's +// is. No model is called and no provider key is passed. It asks: +// +// --version a label for the refusal line and the record; not a gate +// --help lists, in its option column, every flag Mosaic passes, with rpc mode and each +// thinking level +// control rpc, planted ambient sources, a saved trust for the project, the seal flags less +// every loader flag (the ones that each keep one kind of source out: --no-approve, +// --no-extensions, --no-skills, --no-prompt-templates, --no-context-files, and +// --no-mcp when --help lists it), the --system-prompt file the sealed runs pass, and +// one -e extension that writes what it sees at session_start: the system prompt, the +// command names and the session id. The binary answers get_state and get_commands, +// its -e extension lists the planted skill and prompt template (the -p runs see +// those kinds only that way), and it shows each kind of planted source: +// an extension, a skill, a prompt template, a context file, a trusted project file +// (the project's .pi/APPEND_SYSTEM.md, which only the trust --no-approve must undo +// lets in), and an MCP server when --help lists --no-mcp. Only a source outside the +// project counts for the other kinds, since a project source can wait on trust. A +// kind it doesn't show could not be told apart from a sealed one. +// sealed rpc the adapter's seal flags (and --no-mcp when --help lists it), the -e extension and +// --system-prompt. The binary answers both, loads that extension (a session's gate +// comes in that way), uses the --system-prompt file, and shows no planted source. +// one out one more sealed rpc run per loader flag, with every seal flag but that one. The +// kind it keeps out must show, and no other kind (pi without --no-mcp ties MCP to +// --no-extensions, so an MCP server may show beside an extension). So each loader flag +// is shown to keep its own kind out, and no other seal flag, nor the order they come +// in, can stand in for it. +// session five -p runs in a bus session's shape, with the arguments adapters/pi/adapter.sh +// passes for a session, in its order (sessionShape below): two -e, --session-dir in +// place of --no-session, a fixed --provider and --model, -p and a message. The +// adapter's four session branches (a tool list or --no-tools, -c over a stored +// session or none, --thinking or none, --mode json or none) are spread over the five +// runs so each pair of them is passed together (SESSION_RUNS). A -c run gets a +// stored session for the project and must resume it. Each run loads both -e +// extensions, uses the --system-prompt file, and shows no +// planted source. A test drives the adapter through every branch set, tool list +// length and thinking level, and fails on an argv sessionShape doesn't build or a +// pair no run passes. +// sealed -p the seal flags in -p with --no-session, as a worker run passes them. Its -e +// extension must reach session_start, and no planted source may show. +// missing -e a missing -e file, in rpc and in -p, stops the start: exit non-zero, no answer, +// and no session_start for the -e extension given after it +// +// Every run gets a fresh scratch directory with the sources planted again, so no run sees what +// another one left. The directory's name carries the pid, start time and pid namespace of the +// process that made it, and each check first stops the processes and removes the directories a +// process no longer running left (sweepScratch below). Every run dies with the process that runs +// the check (checkArgv in packages/seat/src/sandbox.mjs), at once when unshare runs here and at +// the next check's sweep when it doesn't. +// The runs share no state: up to FIT_CONCURRENCY of them run at once, +// each a child process the check awaits, so the process that runs the check (the bus) keeps +// answering while it runs. --version and --help go first; the rest start in the order above and +// are judged in that order, so the first failure is the one a refusal names, and the runs still +// going are killed. Each run passes the same -e and --system-prompt; the other runs differ only by +// their seal flags, their mode, the -p message and the missing -e file. Each run starts the binary +// through /bin/sh, as the adapter does, with fitEnv's variables: the environment a bus session's pi +// gets, built by the code that builds it (the launcher's ENV_ALLOW and sessionEnv, the sandbox's +// sessionSetenv, the runner's adapterEnv and the pi module's harnessEnv) from the calling process's +// own environment, less the provider variables. So a fit run has a name only when a session in that +// process has it (TERM, TZ and LC_ALL only when the bus has them), no run sets a variable a session +// lacks, such as PI_CODING_AGENT_DIR, and a test holds the two together. +// +// A check that cannot tell refuses: a sandbox that fails, a run that times out or prints more +// than 4 MB, an answer that is missing or doesn't parse, a kind of source the control doesn't +// show, or the kind a one-out run should show. A one-out run that shows another kind, or a +// session-shaped run that shows any, is a leak. A --version that fails or hangs past 3 s only +// leaves no label. A pass is cached in memory by a digest of the files that run (the package's +// package.json, every file under dist/bundle/, every file in the directory tree that holds the +// binary's real file, and, for a copy mounted with the node_modules it sits in, npm's lock files +// there), the mount layout and the names in the fit runs' environment, so a process with other +// names checks again. Callers that ask while a check for the same key is running share it. +// A refusal line names the found binary by its label (MOSAIC_PI_BIN, the pi on PATH, the +// repository's pi), never a host path. +// +// Known limit: pi 0.85.1 appends the agent directory's APPEND_SYSTEM.md whatever the flags. The +// check doesn't plant one; a session's agent directory is the run's own. Not run: --fork, +// --skill with --no-skills dropped, and the interactive TUI, which only worker containers and +// scripts/agent.sh pass; the session branches three or four at a time beyond the session-shaped +// runs; -c over a session directory that holds nothing for the project (the adapter passes -c +// whenever the directory isn't empty), since one more run costs about 340 ms cold; and a +// session's own values: its tool names beyond read and bash, thinking levels beyond low, +// provider, model, key, -e files and request. The runs are offline with no key, so nothing +// past the start is tried. A pi built to act differently with those is outside what a start-time +// check can catch. +import { spawn } from "node:child_process"; +import { createHash } from "node:crypto"; +import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join, relative } from "node:path"; +import { BWRAP, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs"; +import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; +import { adapterEnv } from "./runner.mjs"; +import { FIT_ROUTE, harnessEnv } from "../../plugins/harness/pi/session-env.mjs"; + +// The flags adapters/pi/adapter.sh and the conversation engine pass. +export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", "--session-dir", "--fork", "-c", "--no-extensions", "-e", "--no-skills", "--skill", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-approve", "--offline", "--tools", "--no-tools", "--provider", "--model", "--thinking", "--system-prompt", "-p"]); +// The adapter's seal flags, as it passes them in an ephemeral session with no tools. +const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-tools", "--no-session"]; +// The loader flags: each keeps one kind of planted source out. --no-mcp counts when --help lists it. +const LOADERS = [["--no-approve", "trusted project file"], ["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]]; +// The session-shaped runs' fixed values: a two-tool list, a thinking level, a provider and model. +// Offline with no key, pi stops before any model call. The provider and model are the pi module's +// (session-env.mjs FIT_ROUTE), so no core source names them. +export const SESSION_SHAPE = Object.freeze({ tools: "read,bash", thinking: "low", provider: FIT_ROUTE.provider, model: FIT_ROUTE.model }); +// The branches adapters/pi/adapter.sh takes for a bus session, each run once or not: a tool list or +// --no-tools, -c over a stored session or a fresh one, --thinking or none, --mode json (the model +// event monitor) or none. The five runs pass every pair of those together; pi-fit.test.mjs drives +// the adapter through all sixteen and fails on a pair no run passes. +const T = SESSION_SHAPE.tools; +const L = SESSION_SHAPE.thinking; +export const SESSION_RUNS = Object.freeze([ + { tools: "", cont: false, thinking: "", json: false }, + { tools: "", cont: true, thinking: L, json: true }, + { tools: T, cont: false, thinking: L, json: true }, + { tools: T, cont: true, thinking: "", json: true }, + { tools: T, cont: true, thinking: L, json: false }, +].map((r) => Object.freeze(r))); +// The arguments adapters/pi/adapter.sh builds for a bus session: the given -e files, a tool list +// or none, a persistent session directory with -c when it holds a session, a thinking level or +// none, the --system-prompt file, -p and the request, and --mode json when the model event monitor +// runs pi. No --skill and no --fork: a bus session never passes them. Keep its order: +// pi-fit.test.mjs runs the adapter through every branch and fails when the two differ. +export function sessionShape({ noMcp, extensions, prompt, sessionDir, tools, cont, thinking, json, message }) { + return [ + "--offline", "--no-approve", "--no-extensions", ...(noMcp ? ["--no-mcp"] : []), ...extensions.flatMap((e) => ["-e", e]), + "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", + ...(tools ? ["--tools", tools] : ["--no-tools"]), "--session-dir", sessionDir, ...(cont ? ["-c"] : []), + "--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model, ...(thinking ? ["--thinking", thinking] : []), + "--system-prompt", prompt, "-p", message, ...(json ? ["--mode", "json"] : []), + ]; +} +// What a session-shaped run is named by in a refusal line. +const shapeName = ({ tools, cont, thinking, json }) => `in a session's shape with ${[tools ? "--tools" : "--no-tools", ...(cont ? ["-c"] : []), ...(thinking ? ["--thinking"] : []), ...(json ? ["--mode json"] : [])].join(", ")}`; +const RUN_TIMEOUT = 20000; +// --version gives only a label, so a hang costs this much and leaves no label. +const VERSION_TIMEOUT = 3000; +const OUTPUT_MAX = 4 * 1024 * 1024; +// How long a run's pipes may stay open after it exits or is killed (spawned). +const PIPE_GRACE = 1000; +const DIGEST_FILES_MAX = 20000; + +// At most this many fit runs at once. Each run is one sandboxed pi start in its own scratch +// directory. +export const FIT_CONCURRENCY = 4; + +const passed = new Map(); +const running = new Map(); + +// #1873 re-check 9: each run's scratch directory is named for the process that makes it, by its +// pid and its start time (field 22 of /proc//stat), so a pid used again later is not taken +// for it. A process killed mid-check can't remove its own directories. So a check removes, before +// it starts, every directory here that a process no longer running made: its pid is gone, or now +// belongs to a process that started at another time. A directory whose process can't be read, or +// one this uid doesn't own, or a symlink, is left alone. +// +// #1873 re-check 10: the name also carries the maker's pid namespace (the inode of +// /proc//ns/pid), and a sweep leaves alone every directory made in another one, since it +// can't read that process. And before it removes a directory, the sweep stops the processes that +// name it on their command line: a run's every layer does (the setpriv, shell, unshare and bwrap +// argv, bwrap's child before its exec, pi's own -e and --system-prompt), and so do the sandbox +// probes. Without unshare's outer layer a bwrap child killed in its setup waits for good (see +// checkArgv in sandbox.mjs), and this is what stops it. A process is stopped only when an argument +// is that directory or a path in it, its real and effective uid are this process's, and its start +// time is the same before and after its command line is read. Never by name, never another uid's, +// never one whose directory's maker still runs. +const SCRATCH = /^mosaic-pi-fit-(\d+)-(\d+)-(\d+)-/; +const startTime = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19]; +const pidNamespace = (pid) => /^pid:\[(\d+)\]$/.exec(readlinkSync(`/proc/${pid}/ns/pid`))[1]; +let ownName = null; +const scratchPrefix = () => `mosaic-pi-fit-${ownName ??= `${process.pid}-${startTime(process.pid)}-${pidNamespace("self")}`}-`; +function sweepScratch(dir = tmpdir()) { + let names; + try { + names = readdirSync(dir); + } catch { + return; + } + const uid = process.getuid(); + const ns = pidNamespace("self"); + const stale = []; + for (const name of names) { + const m = SCRATCH.exec(name); + if (!m || m[3] !== ns) continue; + try { + let alive; + try { + alive = startTime(m[1]) === m[2]; + } catch (e) { + if (e.code !== "ENOENT") continue; + alive = false; + } + if (alive) continue; + const at = join(dir, name); + const st = lstatSync(at); + if (st.isDirectory() && st.uid === uid) stale.push(at); + } catch {} + } + if (!stale.length) return; + stopNaming(stale, uid); + for (const at of stale) { + try { + rmSync(at, { recursive: true, force: true }); + } catch {} + } +} +// SIGKILL to every process of uid (real and effective) with an argument that is one of dirs or a +// path in one, other than this one. +function stopNaming(dirs, uid) { + let pids; + try { + pids = readdirSync("/proc").filter((p) => /^\d+$/.test(p) && Number(p) !== process.pid); + } catch { + return; + } + const names = (arg) => dirs.some((d) => arg === d || arg.startsWith(`${d}/`)); + for (const pid of pids) { + try { + const start = startTime(pid); + const ids = /^Uid:\s+(\d+)\s+(\d+)\s/m.exec(readFileSync(`/proc/${pid}/status`, "utf8")); + if (!ids || Number(ids[1]) !== uid || Number(ids[2]) !== uid) continue; + if (!readFileSync(`/proc/${pid}/cmdline`, "utf8").split("\0").some(names)) continue; + if (startTime(pid) !== start) continue; + process.kill(Number(pid), "SIGKILL"); + } catch {} + } +} + +// The provider variables never reach a fit run: no key is passed, since no model is called. +const withoutProviders = (host) => Object.fromEntries(Object.entries(host ?? {}).filter(([k, v]) => typeof v === "string" && !isProviderVariable(k))); +// The names a fit run's host part holds: sessionEnv's answer for the process's environment. +export const fitNames = (host = process.env) => Object.keys(sessionEnv(withoutProviders(host), "fit")).sort(); +const fitKey = (found, { levels, bwrap, unshare, node, host }) => JSON.stringify([filesDigest(found), found.root, found.dest ?? null, found.view, bwrap, unshare, node, levels, fitNames(host)]); + +// found: binary.mjs's answer ({ bin, label, root, dest, view, pkg }). levels: the thinking levels +// 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 +// { 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 } = {}) { + 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` }); + } + if (passed.has(key)) return Promise.resolve(passed.get(key)); + if (running.has(key)) return running.get(key); + 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 (!result.ok) return result; + const passing = { ...result, outer: layers.outer ? { used: true } : { used: false, why: layers.why } }; + passed.set(key, passing); + return passing; + } 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` }; + } + })().finally(() => { if (running.get(key) === job) running.delete(key); }); + running.set(key, job); + return job; +} + +// The passed check for found as piFit would key it now, or null: none passed, or its files can't +// be read. Nothing is run. +export function fitVerdict(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) { + try { + return passed.get(fitKey(found, { levels, bwrap, unshare, node, host })) ?? null; + } catch { + return null; + } +} + +async function check(found, { levels, bwrap, unshare, node, who, digest, host }) { + // 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 begun = []; + let active = 0; + const waiting = []; + const slot = () => active < FIT_CONCURRENCY ? (active++, Promise.resolve()) : new Promise((resolve) => waiting.push(resolve)); + const free = () => { const next = waiting.shift(); if (next) next(); else active--; }; + // args: the arguments, or a function of the run's scratch directory that returns them (it may + // plant more first). The answer is read before the scratch directory goes: the rpc answers, + // whether each -e extension loaded, what the first one saw at session_start (the system prompt, + // the command names, the session id), and the planted sources that showed, through the rpc + // get_commands answer or through the -e extension's command list (listed: through that alone). + // shape: a session-shaped run's branches, which set its tools, thinking level and monitor + // variables as a session's would. A run that stop kept from starting gives null. + const once = async (args, input, { timeout, noMcp, shape }) => { + await slot(); + try { + if (stop.signal.aborted) return null; + const scratch = mkdtempSync(join(tmpdir(), scratchPrefix())); + let ran = null; + try { + plant(scratch); + const env = fitEnv(scratch, { view: found.view, noMcp, shape, host }); + const r = ran = await runIn(found, { bwrap, unshare, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout, signal: stop.signal }); + const answer = answered(r.stdout); + const start = started(scratch); + const listed = start?.commands ?? []; + return { ...r, answer, explicit: marked(scratch, EXPLICIT), second: marked(scratch, SECOND), prompt: start?.prompt ?? null, session: start?.session ?? null, listed: named(listed), seen: seen(scratch, [...(answer.commands ?? []), ...listed], start) }; + } finally { + // #1873 re-check 10: a run this check killed (a timeout, or stop) may have had bwrap in its + // setup, and without unshare's outer layer bwrap's child then waits for good. The run is + // over, so whatever still names its directory is left from it. + if (ran?.stopped) stopNaming([scratch], process.getuid()); + rmSync(scratch, { recursive: true, force: true }); + } + } finally { + free(); + } + }; + const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null } = {}) => { + const p = once(args, input, { timeout, noMcp, shape }); + // Judged in order, a later run can fail before an earlier one is awaited: this keeps that + // failure from counting as unhandled. Awaiting p still throws it. + p.catch(() => {}); + begun.push(p); + return p; + }; + try { + return await judge(found, { levels, who, digest, run }); + } finally { + stop.abort(); + await Promise.allSettled(begun); + } +} + +async function judge(found, { levels, who, digest, run }) { + const helped = run(["--help"]); + const versioned = run(["--version"], "", { timeout: VERSION_TIMEOUT }); + 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"); + 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)) ?? ""; + const missing = FIT_FLAGS.filter((f) => !has(f)); + if (has("--mode") && !word(line("--mode"), "rpc")) missing.push("--mode rpc"); + if (has("--thinking")) for (const l of levels) if (!word(line("--thinking"), l)) missing.push(`--thinking ${l}`); + if (missing.length) return refuse(`lacks ${missing.join(", ")}`); + const noMcp = has("--no-mcp"); + + const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n'; + // Every run passes the same -e and --system-prompt, so the runs differ only by the seal flags. + const given = (scratch) => ["-e", join(scratch, "explicit", "fit.ts"), "--system-prompt", join(scratch, "prompt.md")]; + const seal = [...SEAL, ...(noMcp ? ["--no-mcp"] : [])]; + const loaders = [...LOADERS, ...(noMcp ? [["--no-mcp", "MCP server"]] : [])]; + const without = (...flags) => seal.filter((f) => !flags.includes(f)); + const rpcRun = (flags) => run((s) => ["--mode", "rpc", ...flags, ...given(s)], ask, { noMcp }); + + // Every run is asked for now, in the order they are judged below; none depends on another. + // The control keeps the seal flags no planted source tells apart (--offline, --no-themes, + // --no-tools, --no-session), so one of those can't stand in for a loader flag either. + const controlRun = rpcRun(without(...loaders.map(([f]) => f))); + const rpcSealed = rpcRun(seal); + const oneOut = loaders.map(([flag, kind]) => [flag, kind, rpcRun(without(flag))]); + // A bus session's shapes, in -p as its turns run: --session-dir in place of --no-session, two -e + // as a session passes, and each pair of the adapter's session branches in one run. A -c run gets + // a stored session for the project first and must resume it, since -c with none starts afresh. + const shaped = SESSION_RUNS.map((shape) => [shape, run((s) => { + if (shape.cont) plantSession(s); + return sessionShape({ noMcp, extensions: [join(s, "explicit", "fit.ts"), join(s, "explicit", "second.ts")], prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions"), message: "fit check", ...shape }); + }, "", { noMcp, shape })]); + // -p takes a message; offline with no key, pi stops before any model call. + const pRun = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { noMcp }); + // A missing -e file stops the start. pi 0.85.1 loads the -e files in order and still loads the + // one after the missing one, then exits 1 before session_start, so only session_start tells. + const missingRuns = [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]].map(([mode, args, input]) => + [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"); + // 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`); + // -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`); + + 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.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.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`); + + // Each loader flag on its own: with every other seal flag on, its kind must show. + 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}`); + 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}`); + } + + 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.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 (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: a -p start with the seal flags ${p.stopped}`); + if (p.prompt === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension"); + if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} with the seal flags on in -p`); + + 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.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension"); + } + return { ok: true, version, noMcp, digest }; +} + +// Whether bwrap runs here, as sandbox.mjs's sandboxProblem asks, but as child processes the caller +// awaits: the system part, the network unshared, an empty environment, then /usr/bin/true. And, +// once per check, whether unshare's outer layer runs (checkArgv in sandbox.mjs): first on its own, +// then with bwrap inside it. Returns { problem } when the check can't run, naming the tool that +// failed without its own text, else { outer, why }: outer is the unshare to pass checkArgv, or null +// when the layer can't run here, and why then names unshare and how it failed. A missing, +// too old or refused unshare drops only that layer and is no refusal: bwrap's sandbox is the same +// either way. The probes run in a scratch directory of their own and name it on their command +// line, so a probe process a killed owner leaves is one the next sweep finds. +async function sandboxLayers({ bwrap, unshare, node }) { + if (!existsSync(bwrap)) return { problem: "the sandbox (bwrap) is missing" }; + if (!existsSync(SETPRIV)) return { problem: "the sandbox's parent-death tool (setpriv) is missing" }; + const scratch = mkdtempSync(join(tmpdir(), scratchPrefix())); + let stopped = false; + try { + const probe = async (argv) => { + const r = await spawned(argv, { input: "", timeout: 10000, signal: new AbortController().signal }); + stopped ||= Boolean(r.stopped); + return r.status === 0; + }; + const sandboxed = (outer) => checkArgv([{ kind: "ro", src: scratch }], ["/usr/bin/true"], { chdir: scratch, node, bwrap, unshare: outer }); + let why = "unshare is missing"; + if (existsSync(unshare)) { + if (!await probe(outerArgv(["/usr/bin/true", scratch], { unshare }))) why = "unshare failed"; + else if (await probe(sandboxed(unshare))) return { outer: unshare, why: null }; + else why = "unshare failed with bwrap inside it"; + } + if (!await probe(sandboxed(null))) return { problem: "the sandbox (bwrap) failed" }; + return { outer: null, why }; + } finally { + // As for a run (once, in piFit): a probe killed at its timeout may leave what names its directory. + if (stopped) stopNaming([scratch], process.getuid()); + rmSync(scratch, { recursive: true, force: true }); + } +} + +// The binary, inside the sandbox: the system part, the found package where a session sees it, the +// scratch directory read-write, and its tmp/ as /tmp, as a session has it. The environment is +// fitEnv's. The binary starts through /bin/sh, as the adapter starts it, so it also gets the +// variables the shell sets. The run is a child process the caller awaits; nothing here blocks. +// stopped: why the run was killed ("timed out", "printed more than 4 MB", "was stopped"), or null. +function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout, signal }) { + const list = [ + { kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) }, + { kind: "rw", src: scratch }, + { kind: "rw", src: join(scratch, "tmp"), dest: "/tmp" }, + ]; + return spawned(checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap, unshare }), { input, timeout, signal }); +} + +// One child process with an empty environment, awaited: its exit status, its stdout, and why it was +// killed, or null. It settles PIPE_GRACE after the child exits or is killed even if its pipes stay +// open: a process left behind can hold them (a kill of bwrap's outer process while its child still +// sets up leaves that child waiting until the pid namespace checkArgv makes is killed, or, without +// that layer, until the next check's sweep), and no wait here is unbounded. +function spawned(argv, { input, timeout, signal }) { + return new Promise((resolve) => { + const out = []; + const size = { stdout: 0, stderr: 0 }; + let stopped = null; + let done = false; + let child; + let timer = null; + let grace = null; + let exited = null; + const finish = (status, sig) => { + if (done) return; + done = true; + clearTimeout(timer); + clearTimeout(grace); + child?.stdout?.destroy(); + child?.stderr?.destroy(); + signal.removeEventListener("abort", onAbort); + if (!stopped && sig === "SIGKILL") stopped = "timed out"; + resolve({ status, stdout: Buffer.concat(out).toString("utf8"), stopped }); + }; + const settleSoon = () => { if (!grace) grace = setTimeout(() => finish(exited?.status ?? null, exited?.sig ?? "SIGKILL"), PIPE_GRACE); }; + const kill = (why) => { + if (!stopped) stopped = why; + try { child?.kill("SIGKILL"); } catch {} + settleSoon(); + }; + const onAbort = () => kill("was stopped"); + try { + child = spawn(argv[0], argv.slice(1), { env: {}, stdio: ["pipe", "pipe", "pipe"] }); + } catch { + finish(null, null); + return; + } + timer = setTimeout(() => kill("timed out"), timeout); + if (signal.aborted) onAbort(); + else signal.addEventListener("abort", onAbort, { once: true }); + const count = (stream) => (chunk) => { + size[stream] += chunk.length; + if (size[stream] > OUTPUT_MAX) kill(`printed more than ${OUTPUT_MAX / 1024 / 1024} MB`); + else if (stream === "stdout") out.push(chunk); + }; + child.stdout.on("data", count("stdout")); + child.stderr.on("data", count("stderr")); + child.stdin.on("error", () => {}); + child.on("error", () => finish(null, null)); + child.on("exit", (status, sig) => { exited = { status, sig }; settleSoon(); }); + child.on("close", (status, sig) => finish(status, sig)); + child.stdin.end(input); + }); +} + +// A fit run's environment: what a bus session's pi gets, built the same way from host, the +// environment of the process sessions launch from (the bus's own). The launcher passes host's +// ENV_ALLOW names that are set through sessionEnv (less the provider variables here, since no key +// is passed: no model is called), with host's PATH behind the repository's .bin; the sandbox sets +// sessionSetenv's (HOME is the scratch home, so pi's agent directory is $HOME/.pi/agent); the +// runner adds adapterEnv's with the pi module's harnessEnv, and MOSAIC_REQUEST for each turn. No +// name is added that a session in that process would lack: a bus started by its unit has no TERM, +// TZ or LC_ALL, and then no fit run has them either. shape: a session-shaped run's branches (its +// tool list, thinking level and --mode json, which a proxy session's first turn takes with the +// model event monitor's variables), else none: no tools, no thinking, one -e. noMcp: --help lists +// --no-mcp. pi-fit.test.mjs fails when these names, or the values the code sets, differ from a bus +// session's, for a login shell's host and a unit's. +export function fitEnv(scratch, { view, noMcp = false, shape = null, host = process.env }) { + const runner = { ...sessionEnv(withoutProviders(host), "fit"), ...sessionSetenv(scratch, { business: "fit", role: "fit" }) }; + const { tools = "", thinking = "", json = false } = shape ?? {}; + const session = { provider: SESSION_SHAPE.provider, model: SESSION_SHAPE.model, thinking }; + const extensions = [join(scratch, "explicit", "fit.ts"), ...(shape ? [join(scratch, "explicit", "second.ts")] : [])]; + const env = adapterEnv({ + ...session, + bundle: { prompt: join(scratch, "prompt.md"), policy: join(scratch, "policy.json"), tools: join(scratch, "tools.json") }, + workspace: join(scratch, "project"), sessionDir: join(scratch, "sessions"), toolSocket: join(scratch, "tools.sock"), turnMarker: join(scratch, "turn"), + harnessEnv: harnessEnv({ bin: view, noMcp, tools: tools ? tools.split(",") : [], extensions, session }), + runDir: scratch, proxyFirstTurn: json, + }, runner); + return { ...env, MOSAIC_REQUEST: "fit check" }; +} + +// The options one help line lists in its option column: a line that starts with "-" (after its +// indent), up to the first run of two spaces or a tab, split at commas; the first word of each +// part. " --extension, -e Load an extension" lists --extension and -e. A flag named in a +// sentence is not listed. +function optionsIn(line) { + const t = line.trim(); + if (!t.startsWith("-")) return []; + return t.split(/ {2,}|\t/)[0].split(",").map((part) => part.trim().split(/[ =<[]/)[0]).filter((o) => /^--?[A-Za-z0-9]/.test(o)); +} +const word = (line, w) => line.split(/[^A-Za-z0-9-]+/).includes(w); + +// The version a refusal line may carry: a version-shaped first line, else none. Anything else the +// binary prints stays out of the line. +function versionLabel(stdout) { + const first = String(stdout).split("\n")[0].trim(); + return /^v?[0-9]+(\.[0-9]+){0,3}([-+][0-9A-Za-z.-]{1,32})?$/.test(first) ? first : null; +} + +// The rpc answers in the output: whether get_state succeeded, and the command names get_commands +// listed, or null when it didn't answer with a { commands: [...] } list. +function answered(stdout) { + let state = false; + let commands = null; + for (const l of String(stdout).split("\n")) { + let m; + try { m = JSON.parse(l); } catch { continue; } + if (!m || m.type !== "response" || m.success !== true) continue; + if (m.id === "fit-state" && m.command === "get_state" && m.data && typeof m.data === "object") state = true; + if (m.id === "fit-commands" && m.command === "get_commands" && Array.isArray(m.data?.commands)) { + commands = m.data.commands.filter((c) => typeof c?.name === "string").map((c) => c.name); + } + } + return { state, commands }; +} + +// The planted sources, each with the kind the control must show. Each extension and each MCP +// server touches its own marker; each skill and prompt template has a name get_commands lists; +// each context and system prompt file holds a word the -e extension's view of the system prompt +// shows. "user" sources sit in the agent directory, $HOME/.pi/agent: no fit run sets +// PI_CODING_AGENT_DIR, as no bus session does. "settings" sources are listed by a settings.json; +// "package" sources come from a local package a settings.json lists. "parent" is a directory above +// the project. +const KINDS = ["extension", "MCP server", "skill", "prompt template", "context file", "trusted project file"]; +const MARKED = { + "user extension": "extension", "user settings extension": "extension", "user package extension": "extension", + "project extension": "extension", "project settings extension": "extension", "project package extension": "extension", + "user mcp.json": "MCP server", "project mcp.json": "MCP server", "project .mcp.json": "MCP server", +}; +const NAMED = { + "fit-user-skill": ["user skill", "skill"], "fit-user-set-skill": ["user settings skill", "skill"], "fit-user-pkg-skill": ["user package skill", "skill"], + "fit-project-skill": ["project skill", "skill"], "fit-project-set-skill": ["project settings skill", "skill"], + "fit-project-pkg-skill": ["project package skill", "skill"], "fit-agents-skill": ["project .agents skill", "skill"], "fit-home-skill": ["home .agents skill", "skill"], + "fit-user-prompt": ["user prompt template", "prompt template"], "fit-user-set-prompt": ["user settings prompt template", "prompt template"], + "fit-user-pkg-prompt": ["user package prompt template", "prompt template"], + "fit-project-prompt": ["project prompt template", "prompt template"], "fit-project-set-prompt": ["project settings prompt template", "prompt template"], + "fit-project-pkg-prompt": ["project package prompt template", "prompt template"], +}; +// Planted file (under the scratch directory), the word it holds, the source, the kind. A project +// SYSTEM.md or APPEND_SYSTEM.md loads only in a trusted project: the saved trust makes it trusted, +// and only --no-approve undoes that with the seal flags on. +const TEXTS = [ + ["home/.pi/agent/SYSTEM.md", "FITUSERSYSTEM", "user SYSTEM.md", "context file"], + ["home/.pi/agent/AGENTS.md", "FITUSERAGENTS", "user AGENTS.md", "context file"], + ["project/.pi/SYSTEM.md", "FITPROJECTSYSTEM", "project SYSTEM.md", "trusted project file"], + ["project/.pi/APPEND_SYSTEM.md", "FITPROJECTAPPEND", "project APPEND_SYSTEM.md", "trusted project file"], + ["project/AGENTS.md", "FITPROJECTAGENTS", "project AGENTS.md", "context file"], + ["project/CLAUDE.md", "FITPROJECTCLAUDE", "project CLAUDE.md", "context file"], + ["CLAUDE.md", "FITPARENTCLAUDE", "parent CLAUDE.md", "context file"], +]; +// The --system-prompt file's word, which the sealed runs must show. +const GIVEN = "FITGIVENPROMPT"; +// The -e extension every run passes, which must load, and the second one the session-shaped runs +// pass after it. +const EXPLICIT = "explicit extension"; +const SECOND = "second explicit extension"; +// The id of the session a -c run must resume. +const PLANTED_SESSION = "fit-planted-session"; +const markerName = (source) => source.replace(/[^a-z]/g, "-"); +const marked = (scratch, source) => { try { return readdirSync(join(scratch, "marks")).includes(markerName(source)); } catch { return false; } }; +// What the -e extension saw at session_start ({ prompt, commands, session }), or null when it wrote +// nothing that parses into that shape. +function started(scratch) { + let m; + try { m = JSON.parse(readFileSync(join(scratch, "marks", "start.json"), "utf8")); } catch { return null; } + if (!m || typeof m.prompt !== "string" || !Array.isArray(m.commands)) return null; + return { prompt: m.prompt, commands: m.commands.filter((c) => typeof c === "string"), session: typeof m.session === "string" ? m.session : null }; +} +// The planted skills and prompt templates among command names, as { source, kind }. +const named = (names) => Object.entries(NAMED).filter(([name]) => names.some((c) => c === name || c.endsWith(`:${name}`))).map(([, [source, kind]]) => ({ source, kind })); + +function plant(scratch) { + const marks = join(scratch, "marks"); + const put = (path, text) => { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, text); }; + const ext = (source) => `import { writeFileSync } from "node:fs";\nexport default function (): void { writeFileSync(${JSON.stringify(join(marks, markerName(source)))}, ""); }\n`; + const mcp = (source) => JSON.stringify({ mcpServers: { fit: { command: "/bin/sh", args: ["-c", `: > ${join(marks, markerName(source))}`] } } }); + const skill = (name) => `---\nname: ${name}\ndescription: a planted skill\n---\nA planted skill.\n`; + const prompt = "A planted prompt template.\n"; + const agent = join(scratch, "home", ".pi", "agent"); + const project = join(scratch, "project"); + mkdirSync(marks, { recursive: true }); + mkdirSync(join(scratch, "tmp"), { recursive: true }); + mkdirSync(project, { recursive: true }); + // The session-shaped runs' --session-dir, made as the adapter makes it. + mkdirSync(join(scratch, "sessions"), { recursive: true }); + // Each settings.json lists an extension, a skill directory, a prompt template and a local + // package holding one of each. + const listed = (who) => { + const dir = join(scratch, "listed", who); + put(join(dir, "fit.ts"), ext(`${who} settings extension`)); + put(join(dir, `fit-${who}-set-skill`, "SKILL.md"), skill(`fit-${who}-set-skill`)); + put(join(dir, `fit-${who}-set-prompt.md`), prompt); + const pkg = join(dir, "package"); + put(join(pkg, "package.json"), JSON.stringify({ name: `fit-${who}-package`, version: "1.0.0" })); + put(join(pkg, "extensions", "fit.ts"), ext(`${who} package extension`)); + put(join(pkg, "skills", `fit-${who}-pkg-skill`, "SKILL.md"), skill(`fit-${who}-pkg-skill`)); + put(join(pkg, "prompts", `fit-${who}-pkg-prompt.md`), prompt); + return { extensions: [join(dir, "fit.ts")], skills: [join(dir, `fit-${who}-set-skill`)], prompts: [join(dir, `fit-${who}-set-prompt.md`)], packages: [pkg] }; + }; + // A saved trust for the project, so the control loads its trust-gated files without --approve. + put(join(agent, "settings.json"), JSON.stringify({ ...listed("user"), defaultProjectTrust: "always" })); + put(join(agent, "trust.json"), JSON.stringify({ [realpathSync(project)]: true })); + put(join(agent, "extensions", "fit.ts"), ext("user extension")); + put(join(agent, "mcp.json"), mcp("user mcp.json")); + put(join(agent, "skills", "fit-user-skill", "SKILL.md"), skill("fit-user-skill")); + put(join(agent, "prompts", "fit-user-prompt.md"), prompt); + put(join(project, ".pi", "settings.json"), JSON.stringify(listed("project"))); + put(join(project, ".pi", "extensions", "fit.ts"), ext("project extension")); + put(join(project, ".pi", "mcp.json"), mcp("project mcp.json")); + put(join(project, ".mcp.json"), mcp("project .mcp.json")); + put(join(project, ".pi", "skills", "fit-project-skill", "SKILL.md"), skill("fit-project-skill")); + put(join(project, ".agents", "skills", "fit-agents-skill", "SKILL.md"), skill("fit-agents-skill")); + put(join(project, ".pi", "prompts", "fit-project-prompt.md"), prompt); + put(join(scratch, "home", ".agents", "skills", "fit-home-skill", "SKILL.md"), skill("fit-home-skill")); + for (const [file, text] of TEXTS) put(join(scratch, file), `${text}\n`); + put(join(scratch, "explicit", "fit.ts"), `import { writeFileSync } from "node:fs";\nexport default function (pi: any): void {\n writeFileSync(${JSON.stringify(join(marks, markerName(EXPLICIT)))}, "");\n pi.on("session_start", (_event: any, ctx: any) => { writeFileSync(${JSON.stringify(join(marks, "start.json"))}, JSON.stringify({ prompt: ctx.getSystemPrompt(), commands: pi.getCommands().map((c: any) => c.name), session: ctx.sessionManager.getSessionId() })); });\n}\n`); + put(join(scratch, "explicit", "second.ts"), ext(SECOND)); + put(join(scratch, "prompt.md"), `${GIVEN}\n`); +} + +// A stored session for the project, as pi 0.85.1 writes one: a header whose cwd is the project's +// real path (pi's -c takes only a session for its own cwd) and one user message. +function plantSession(scratch) { + const at = new Date().toISOString(); + writeFileSync(join(scratch, "sessions", `${PLANTED_SESSION}.jsonl`), [ + { type: "session", version: 3, id: PLANTED_SESSION, timestamp: at, cwd: realpathSync(join(scratch, "project")) }, + { type: "message", id: "f17e0001", parentId: null, timestamp: at, message: { role: "user", content: [{ type: "text", text: "an earlier turn" }], timestamp: Date.now() } }, + ].map((e) => `${JSON.stringify(e)}\n`).join("")); +} + +// The planted sources a run loaded, as { source, kind }: markers present, planted names among the +// command names, and planted words in the system prompt the -e extension saw. +function seen(scratch, commands, start) { + let marks = []; + try { marks = readdirSync(join(scratch, "marks")); } catch { marks = []; } + const out = Object.entries(MARKED).filter(([s]) => marks.includes(markerName(s))).map(([source, kind]) => ({ source, kind })); + out.push(...named(commands)); + const text = start?.prompt ?? ""; + for (const [, word, source, kind] of TEXTS) if (text.includes(word)) out.push({ source, kind }); + return out; +} + +// sha256 over the sorted (path, sha256) list of the package's package.json, every regular file +// under dist/bundle/ (design section 4), every regular file in the directory tree that holds the +// binary's real file, so a bin outside dist/bundle/ can't import a file the digest skips, and npm's +// lock files for a copy mounted with its node_modules. A symlink there is read through. +function filesDigest(found) { + const pkg = found.pkg; + const bin = realpathSync(found.bin); + const files = new Set([join(pkg, "package.json"), bin]); + const walk = (dir) => { + let entries; + try { entries = readdirSync(dir, { withFileTypes: true }); } catch (e) { if (e.code === "ENOENT") return; throw e; } + for (const e of entries) { + const p = join(dir, e.name); + if (e.isDirectory()) walk(p); + else if (e.isFile() || (e.isSymbolicLink() && lstatSync(realpathSync(p)).isFile())) files.add(p); + if (files.size > DIGEST_FILES_MAX) throw new Error("too many files"); + } + }; + walk(join(pkg, "dist", "bundle")); + const binDir = dirname(bin); + if (binDir !== pkg && binDir.startsWith(`${pkg}/`)) walk(binDir); + // A copy mounted with the node_modules it sits in imports packages beside it: npm's lock files + // there stand for them. + if (found.root !== pkg) for (const f of [join(found.root, ".package-lock.json"), join(dirname(found.root), "package-lock.json")]) { + try { lstatSync(f); } catch (e) { if (e.code === "ENOENT") continue; throw e; } + files.add(f); + } + const h = createHash("sha256"); + for (const f of [...files].sort()) { + h.update(relative(pkg, f)).update("\0").update(createHash("sha256").update(readFileSync(f)).digest("hex")).update("\n"); + } + return h.digest("hex"); +} diff --git a/packages/harness/tests/bundle.test.mjs b/packages/harness/tests/bundle.test.mjs index 8844e4600..f9a458925 100644 --- a/packages/harness/tests/bundle.test.mjs +++ b/packages/harness/tests/bundle.test.mjs @@ -4,7 +4,8 @@ import { createHash } from "node:crypto"; import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { BundleError, buildBundle, sessionModel, SOURCES } from "../src/bundle.mjs"; -import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs"; +import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs"; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check import { decide } from "../src/gate.mjs"; import { resolveInstance, validateBusinessDocument } from "../../organization/src/index.mjs"; import { businessDoc, REPO_ROLES, systemFor } from "../../organization/tests/helpers.mjs"; diff --git a/packages/harness/tests/creative.test.mjs b/packages/harness/tests/creative.test.mjs index d9ee76af8..0bd8aa76c 100644 --- a/packages/harness/tests/creative.test.mjs +++ b/packages/harness/tests/creative.test.mjs @@ -12,7 +12,8 @@ import { join, relative } from "node:path"; import { buildBundle, PERSONA_CHARS } from "../src/bundle.mjs"; import { decide } from "../src/gate.mjs"; import { adapterEnv } from "../src/runner.mjs"; -import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs"; +import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs"; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check const sha = (data) => createHash("sha256").update(data).digest("hex"); const CREATIVE = ["companion", "ideas", "research", "proofreader", "fact-checker", "continuity"]; @@ -177,7 +178,8 @@ function runAdapter(t, harness, b, { prompt = b.files.prompt, cwd } = {}) { writeFileSync(cli, '#!/bin/sh\nprintf "%s\\0" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n'); chmodSync(cli, 0o755); const s = { harness, provider: harness === "pi" ? "probe" : null, model: "claude-sonnet-5-5", workspace: b.workspace, sessionDir: join(b.dir, "session"), toolSocket: join(b.dir, "tools.sock"), turnMarker: join(b.dir, "turn.done"), bundle: { ...b.files, prompt }, harnessEnv: b.env }; - const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x" }; + // #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found. + const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) }; const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env }); const argv = existsSync(env.ARGV_OUT) ? readFileSync(env.ARGV_OUT, "utf8").split("\0") : null; return { ...r, argv }; diff --git a/packages/harness/tests/helpers.mjs b/packages/harness/tests/helpers.mjs index b99651d15..c34b1b1f9 100644 --- a/packages/harness/tests/helpers.mjs +++ b/packages/harness/tests/helpers.mjs @@ -31,6 +31,12 @@ async function harnessModule(name) { return loader.modules("harness")[0]; } export const HARNESS = Object.freeze({ pi: await harnessModule("pi"), "claude-code": await harnessModule("claude-code") }); +// #1873 re-check 7: a module whose fit check runs async has ready(), which the launcher's preflight +// stands for. A test that calls buildBundle directly awaits this first. It is not run on import: +// request-agent.mjs imports this file in every tool-call process, which builds no bundle. +export async function harnessReady() { + for (const mod of Object.values(HARNESS)) if (typeof mod.ready === "function") await mod.ready(); +} // A session's route is its own three vars (row 65); a role the test doesn't route runs pi on the // test system's old execution.model at thinking high, so the bundle tests keep their models. diff --git a/packages/harness/tests/library-tools.test.mjs b/packages/harness/tests/library-tools.test.mjs index 683c98d6d..02c91dcc4 100644 --- a/packages/harness/tests/library-tools.test.mjs +++ b/packages/harness/tests/library-tools.test.mjs @@ -6,7 +6,8 @@ import { join } from 'node:path'; import { typedTools } from '../src/tools.mjs'; import { buildBundle } from '../src/bundle.mjs'; import { TYPES } from '../../library/src/mapping.mjs'; -import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs'; +import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs'; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check const LOOKUPS = ['library_find', 'library_get', 'library_search', 'library_manuscript', 'library_universe']; const CREATIVE = ['companion', 'research', 'ideas', 'proofreader', 'fact-checker', 'continuity']; diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs new file mode 100644 index 000000000..bc01e6c62 --- /dev/null +++ b/packages/harness/tests/pi-fit.test.mjs @@ -0,0 +1,1054 @@ +// #1873 pi-unpin (design section 5, S3): before a session starts, a fit check proves the found pi +// takes every flag Mosaic passes and keeps the planted ambient sources off with the seal flags. +// Nothing compares a version. A check that cannot tell refuses. A refusal line names the found +// binary by its label, never a host path. +import test from "node:test"; +import assert from "node:assert/strict"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, watch, writeFileSync } from "node:fs"; +import { spawnSync } from "node:child_process"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import * as fitModule from "../src/pi-fit.mjs"; +import { piStandIn } from "./pi-stand-in.mjs"; +import { locate } from "../../plugins/harness/pi/binary.mjs"; +import * as pi from "../../plugins/harness/pi/index.mjs"; +import { BWRAP, UNSHARE, checkArgv, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs"; +import { ENV_ALLOW, isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; +import { adapterEnv } from "../src/runner.mjs"; + +const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url))); +const NAME = "@earendil-works/pi-coding-agent"; +// The module's thinking levels (packages/plugins/harness/pi/index.mjs LEVELS). +const LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"]; +const CREDENTIAL = { ZAI_API_KEY: "fixture-zai" }; +const SKIP = sandboxProblem() ? { skip: "no working bwrap here" } : {}; + +// A stand-in pi in an npm prefix (prefix/lib/node_modules/, prefix/bin/pi a link to its +// bin), named by MOSAIC_PI_BIN. Returns the env that names it and the paths a refusal must not +// carry. +function standIn(t, knobs, version = "2.0.0") { + const root = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-")); + t.after(() => rmSync(root, { recursive: true, force: true })); + const pkg = join(root, "prefix", "lib", "node_modules", NAME); + mkdirSync(join(pkg, "dist", "bundle"), { recursive: true }); + writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: NAME, version, bin: { pi: "dist/bundle/cli.js" } })); + writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ version, ...knobs }), { mode: 0o755 }); + mkdirSync(join(root, "prefix", "bin")); + symlinkSync(`../lib/node_modules/${NAME}/dist/bundle/cli.js`, join(root, "prefix", "bin", "pi")); + const env = { MOSAIC_PI_BIN: join(root, "prefix", "bin", "pi"), PATH: "/usr/bin:/bin" }; + return { env, root, cli: join(pkg, "dist", "bundle", "cli.js"), paths: [root, realpathSync(root), tmpdir(), REPO] }; +} + +const { piFit } = fitModule; +// A fit check of the stand-in env names, from host (default: this process's environment, as the +// bus's own is in a launch). +const fit = (env, host) => piFit(locate(env), { levels: LEVELS, ...(host ? { host } : {}) }); +// Two environments a bus runs under. A login shell's has every ENV_ALLOW name. A bus started by its +// systemd user unit (packages/cli/systemd/mosaic-bus.service.in) has the user manager's HOME, LANG, +// LOGNAME, PATH and USER, the unit's MOSAIC_CLAUDE_BIN and MOSAIC_CONFIG, and a provider key from its +// EnvironmentFile: no TERM, TZ or LC_ALL. +const NODE_PATH = `${dirname(process.execPath)}:/usr/bin:/bin`; +const HOSTS = Object.freeze({ + login: Object.freeze({ ...Object.fromEntries(ENV_ALLOW.map((n) => [n, `host-${n.toLowerCase()}`])), PATH: NODE_PATH }), + unit: Object.freeze({ PATH: NODE_PATH, HOME: "/home/fixture", USER: "fixture", LOGNAME: "fixture", LANG: "C.UTF-8", MOSAIC_CLAUDE_BIN: "/opt/fixture/claude", MOSAIC_CONFIG: "/home/fixture/config.json", ZAI_API_KEY: "fixture-zai" }), +}); +// The longest gap between ticks of a 10 ms timer while work runs, in ms. +async function gapDuring(work) { + let last = performance.now(); + let max = 0; + const tick = () => { const now = performance.now(); max = Math.max(max, now - last); last = now; }; + const timer = setInterval(tick, 10); + try { + const result = await work(); + tick(); + return { result, max }; + } finally { + clearInterval(timer); + } +} +// A refusal names no host path: none of the stand-in's, the scratch's or the repository's, and no +// "/" at all, since every expected line is plain words and flag names. +const noPath = (message, paths) => { + for (const p of paths) assert.ok(!message.includes(p), message); + assert.ok(!message.includes("/"), message); +}; + +test("pi-unpin S3: a stand-in pi reporting another version that implements every check is accepted, and its version is recorded", SKIP, async t => { + const s = standIn(t, { version: "2.0.0" }); + const r = await fit(s.env); + assert.equal(r.ok, true, r.message); + assert.equal(r.version, "2.0.0"); + assert.equal(r.noMcp, false); + assert.match(r.digest, /^[0-9a-f]{64}$/); + assert.equal(await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }), null); + assert.equal(await pi.version({ env: s.env }), "2.0.0", "the launcher records what the binary prints"); +}); + +test("pi-unpin S3: a stand-in missing --no-context-files from --help is refused with that line", SKIP, async t => { + const s = standIn(t, { dropFlags: ["--no-context-files"] }); + const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files" }); + noPath(problem.message, s.paths); + // rpc mode and each thinking level the module applies are part of the check too. + assert.equal((await fit(standIn(t, { modes: ["text", "json"] }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --mode rpc"); + assert.equal((await fit(standIn(t, { levels: ["off", "low"] }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --thinking minimal, --thinking medium, --thinking high, --thinking xhigh, --thinking max"); + assert.equal((await fit(standIn(t, { dropFlags: ["--offline", "-e"] }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks -e, --offline"); +}); + +test("pi-unpin S3: a stand-in that exits 0 on a missing -e is refused", SKIP, async t => { + const s = standIn(t, { missingExtOk: true }); + const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) starts with a missing -e extension" }); + noPath(problem.message, s.paths); + // Review finding 5: the adapter runs -p, so a pi that starts anyway in either mode is refused. + for (const mode of ["print", "rpc"]) assert.equal((await fit(standIn(t, { missingExtOk: mode }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) starts with a missing -e extension", mode); +}); + +test("pi-unpin S3: a stand-in that touches the planted MCP marker under the seal flags is refused", SKIP, async t => { + const s = standIn(t, { sealLeak: ["mcp"] }); + const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) loaded a planted user mcp.json, project mcp.json, project .mcp.json with the seal flags on" }); + noPath(problem.message, s.paths); + // Each other kind of planted source is caught the same way. + const lines = { + extensions: "loaded a planted user extension, user settings extension, user package extension, project extension, project settings extension, project package extension with the seal flags on", + skills: "loaded a planted user skill, user settings skill, user package skill, project skill, project settings skill, project package skill, project .agents skill, home .agents skill with the seal flags on", + prompts: "loaded a planted user prompt template, user settings prompt template, user package prompt template, project prompt template, project settings prompt template, project package prompt template with the seal flags on", + // Review finding 2: the context files reach the system prompt an -e extension sees. + context: "loaded a planted user AGENTS.md, project AGENTS.md, parent CLAUDE.md with the seal flags on", + // A saved trust makes the project trusted, so only --no-approve keeps its APPEND_SYSTEM.md out. + approve: "loaded a planted project APPEND_SYSTEM.md with the seal flags on", + }; + for (const [kind, line] of Object.entries(lines)) assert.equal((await fit(standIn(t, { sealLeak: [kind] }).env)).message, `pi 2.0.0 (MOSAIC_PI_BIN) ${line}`, kind); +}); + +test("pi-unpin S3: a stand-in that ignores the planted sources even without the seal flags fails the positive control and is refused as can't tell", SKIP, async t => { + const s = standIn(t, { ignoreAll: true }); + const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted extension, skill, prompt template, context file, trusted project file without the seal flags" }); + noPath(problem.message, s.paths); +}); + +// Review finding 1: one planted source seen in the control does not prove the others can be seen. +test("pi-unpin S3 review: a get_commands answer that fails or changes shape is refused as can't tell, even when skills and prompts leak", SKIP, async t => { + for (const commands of ["fail", "list"]) { + const s = standIn(t, { commands, sealLeak: ["skills", "prompts"] }); + assert.equal((await fit(s.env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't answer get_commands", commands); + } + // MCP is a kind the control must show only when --help lists --no-mcp. + assert.equal((await fit(standIn(t, { extraFlags: ["--no-mcp"], ignoreAll: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted extension, MCP server, skill, prompt template, context file, trusted project file without the seal flags"); +}); + +// Review finding 2: the sealed runs read the system prompt through the -e extension, so a pi that +// drops the --system-prompt file, or never runs session_start in -p, can't pass. +test("pi-unpin S3 re-check: a project SYSTEM.md doesn't stand in for the APPEND_SYSTEM.md the control must show, so a trust leak the sealed runs can't see is can't tell", SKIP, async t => { + assert.equal((await fit(standIn(t, { ignoreAppend: true, sealLeak: ["approve"] }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted trusted project file without the seal flags"); +}); + +test("pi-unpin S3 re-check 2: the run without the seal flags passes --system-prompt too, so a pi whose --system-prompt hides what the seal flags must is can't tell", SKIP, async t => { + assert.equal((await fit(standIn(t, { sysReplacesAll: true, sealLeak: ["approve", "context"] }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted context file, trusted project file without the seal flags"); +}); + +// The adapter's seal flags and the loader flags among them, as pi-fit.mjs has them. +const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-tools", "--no-session"]; +const LOADERS = ["--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-context-files"]; + +// Re-check 2's rule, re-check 3 item 3: the runs differ only by their seal flags, their mode, the +// -p message and the missing -e file, and each runs in its own scratch directory. A bwrap that +// logs its argv, one file per start since up to four run at once, shows every run the check makes. +test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the runs differ only by their seal flags", SKIP, async t => { + for (const extraFlags of [[], ["--no-mcp"]]) { + const s = standIn(t, { extraFlags }); + const log = join(s.root, "bwrap.log"); + const bwrap = join(s.root, "bwrap"); + writeFileSync(bwrap, `#!/bin/sh\nf=$(/usr/bin/mktemp '${log}.XXXXXX')\nfor a in "$@"; do printf '%s\\037' "$a"; done > "$f"\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 }); + const found = locate(s.env); + const r = await piFit(found, { levels: LEVELS, bwrap }); + assert.equal(r.ok, true, r.message); + const runs = readdirSync(s.root).filter((n) => n.startsWith("bwrap.log.")).map((n) => readFileSync(join(s.root, n), "utf8").split("\x1f").slice(0, -1)).filter((a) => a.includes(found.view)).map((a) => { + const scratch = a[a.indexOf("--chdir") + 1].replace(/\/project$/, ""); + // Re-check 6: the binary starts through /bin/sh, as the adapter starts it. (Its path is also + // MOSAIC_PI_BIN's value in a --setenv before it.) + const bin = a.indexOf('exec "$0" "$@"') + 1; + assert.deepEqual(a.slice(bin - 3, bin + 1), ["/bin/sh", "-c", 'exec "$0" "$@"', found.view]); + return { scratch, args: a.slice(bin + 1).map((x) => x.split(scratch).join("")) }; + }); + const seal = [...SEAL, ...extraFlags]; + const loaders = [...LOADERS, ...extraFlags]; + // --version, --help, the control, the sealed rpc, one per loader flag, the session-shaped runs, + // -p, two missing -e. + const shapes = fitModule.SESSION_RUNS; + assert.equal(runs.length, 2 + 2 + loaders.length + shapes.length + 1 + 2); + assert.equal(new Set(runs.map((x) => x.scratch)).size, runs.length, "each run has its own scratch directory"); + assert.deepEqual(runs.filter((x) => x.args.length === 1).map((x) => x.args[0]).sort(), ["--help", "--version"]); + // Re-check 5 item 1: the session-shaped runs, in -p, each built by the function the adapter + // test below holds to every argv the adapter builds for a session, with two -e as a session + // passes. They run with the others, so they are matched as a set. + const shaped = (args) => args.includes("--session-dir"); + assert.deepEqual(runs.filter((x) => shaped(x.args)).map((x) => JSON.stringify(x.args)).sort(), shapes.map((shape) => JSON.stringify(fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: ["/explicit/fit.ts", "/explicit/second.ts"], prompt: "/prompt.md", sessionDir: "/sessions", message: "fit check", ...shape }))).sort()); + const flagLists = []; + for (const { args } of runs.filter((x) => x.args.length > 1 && !shaped(x.args))) { + const flags = args.filter((a) => seal.includes(a)); + assert.deepEqual(flags, seal.filter((f) => flags.includes(f)), "the seal flags keep their order"); + const rest = []; + for (let i = 0; i < args.length; i++) { + if (args[i] === "--mode") { i++; continue; } + if (args[i] === "-e" && args[i + 1] === "/missing/extension.ts") { i++; continue; } + if (args[i] === "-p" || seal.includes(args[i]) || (args.includes("-p") && i === args.length - 1)) continue; + rest.push(args[i]); + } + assert.deepEqual(rest, ["-e", "/explicit/fit.ts", "--system-prompt", "/prompt.md"], args.join(" ")); + if (args[0] === "--mode" && !args.includes("/missing/extension.ts")) flagLists.push(flags.join(" ")); + } + assert.deepEqual(flagLists.sort(), [seal.filter((f) => !loaders.includes(f)), seal, ...loaders.map((l) => seal.filter((f) => f !== l))].map((f) => f.join(" ")).sort()); + } +}); + +test("pi-unpin S3 re-check 3: --no-tools or --no-themes can't stand in for the loader flags, since the control passes them too", SKIP, async t => { + for (const cue of ["--no-tools", "--no-themes", "order"]) { + assert.equal((await fit(standIn(t, { hideWhen: cue }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted extension, skill, prompt template, context file, trusted project file without the seal flags", cue); + } +}); + +test("pi-unpin S3 re-check 3: each loader flag is shown on its own, so no other seal flag can stand in for it", SKIP, async t => { + const but = (kind, flag) => `pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted ${kind} with every seal flag but ${flag}`; + const leaked = (sources, flag) => `pi 2.0.0 (MOSAIC_PI_BIN) loaded a planted ${sources} with every seal flag but ${flag}`; + // Each stand-in ignores its flag and keeps its kind out with a loader flag whose run comes later + // (re-check 4 item 2 refuses one that leans on an earlier one in that earlier run, below), so + // each is refused in its own flag's run. + for (const [flag, other, kind, extraFlags] of [ + ["--no-approve", "--no-extensions", "trusted project file", []], + ["--no-extensions", "--no-skills", "extension", []], + ["--no-skills", "--no-prompt-templates", "skill", []], + ["--no-prompt-templates", "--no-context-files", "prompt template", []], + ["--no-context-files", "--no-mcp", "context file", ["--no-mcp"]], + ]) assert.equal((await fit(standIn(t, { standIn: [flag, other], extraFlags }).env)).message, but(kind, flag), flag); + // Re-check 4 item 2: a stand-in that leans on an earlier loader flag shows its kind in that + // flag's run, which refuses it for showing a kind not its own. --no-mcp's run is the last. + assert.equal((await fit(standIn(t, { standIn: ["--no-extensions", "--no-approve"] }).env)).message, leaked("user extension, user settings extension, user package extension, project extension, project settings extension, project package extension, user mcp.json, project mcp.json, project .mcp.json", "--no-approve")); + assert.equal((await fit(standIn(t, { extraFlags: ["--no-mcp"], standIn: ["--no-mcp", "--no-approve"] }).env)).message, leaked("user mcp.json, project mcp.json, project .mcp.json", "--no-approve")); + // Re-check 3's dispatchers: a pi that hides everything whenever one always-passed loader flag is on. + // It now leaks every other kind in the --no-approve run, where --no-approve is left out. + assert.match((await fit(standIn(t, { hideWhen: "--no-approve" }).env)).message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted user extension, .* user skill, .* user prompt template, .* user AGENTS\.md, project AGENTS\.md, parent CLAUDE\.md with every seal flag but --no-approve$/); + assert.equal((await fit(standIn(t, { hideWhen: "--no-extensions" }).env)).message, but("trusted project file", "--no-approve")); +}); + +// Re-check 4 item 2: a one-out run must show its own kind and no other. +test("pi-unpin S3 re-check 4: a pi that loads other kinds when one loader flag is left out is refused at that run", SKIP, async t => { + for (const [leakWithout, flag] of [[["--no-skills"], "--no-skills"], [LOADERS, "--no-approve"]]) { + const m = (await fit(standIn(t, { leakWithout }).env)).message; + assert.match(m, new RegExp(`^pi 2\\.0\\.0 \\(MOSAIC_PI_BIN\\) loaded a planted user extension, .* with every seal flag but ${flag}$`), String(leakWithout)); + if (flag === "--no-skills") assert.ok(!/skill/.test(m.replace(/but --no-skills$/, "")), m); + } +}); + +// Re-check 4 item 1: every bus session runs pi with --session-dir, and most with --tools, so the +// session-shaped runs have no --no-session, and most no --no-tools. +test("pi-unpin S3 re-check 4: a pi whose loader flags work only with --no-tools or --no-session is refused in a session's shape", SKIP, async t => { + for (const extraFlags of [[], ["--no-mcp"]]) for (const [needs, shape] of [["--no-tools", "--tools, --thinking, --mode json"], ["--no-session", "--no-tools"]]) { + assert.match((await fit(standIn(t, { needs, extraFlags }).env)).message ?? "passed", new RegExp(`^pi 2\\.0\\.0 \\(MOSAIC_PI_BIN\\) loaded a planted user extension, .* with the seal flags in a session's shape with ${shape}$`), `${needs} ${extraFlags}`); + } +}); + +// Re-check 5 item 1: the reviewer's fakes, each keyed on what only a session passes, and the +// branch pairs no single-flag fake reaches. Each is refused in the first session-shaped run that +// passes what it is keyed on, and the -p runs see skills and prompt templates through the -e +// extension, since -p answers no get_commands. +test("pi-unpin S3 re-check 5: a pi that leaks only with a session's -c, --thinking, -p with --tools, --mode json, or a pair of them, is refused in that shape", SKIP, async t => { + const line = (shape) => new RegExp(`^pi 2\\.0\\.0 \\(MOSAIC_PI_BIN\\) loaded a planted user extension, .*, user skill, .*, user prompt template, .* with the seal flags in a session's shape with ${shape}$`); + for (const [knobs, shape] of [ + [{ leakWith: ["-c"] }, "--no-tools, -c, --thinking, --mode json"], + [{ leakWith: ["--thinking"] }, "--no-tools, -c, --thinking, --mode json"], + [{ leakWith: ["-p", "--tools"] }, "--tools, --thinking, --mode json"], + [{ leakWith: ["--mode", "json"] }, "--no-tools, -c, --thinking, --mode json"], + [{ leakWith: ["--no-tools", "--session-dir"] }, "--no-tools"], + [{ leakWith: ["-c"], leakWithout: ["--thinking"] }, "--tools, -c, --mode json"], + [{ leakWith: ["--tools"], leakWithout: ["-c"] }, "--tools, --thinking, --mode json"], + [{ leakWith: ["--thinking"], leakWithout: ["--mode"] }, "--tools, -c, --thinking"], + [{ leakWith: ["-c", "--tools"], leakWithout: ["--thinking"] }, "--tools, -c, --mode json"], + ]) for (const extraFlags of [[], ["--no-mcp"]]) assert.match((await fit(standIn(t, { ...knobs, extraFlags }).env)).message ?? "passed", line(shape), `${JSON.stringify(knobs)} ${extraFlags}`); +}); + +test("pi-unpin S3 re-check 5: a -c run must resume the planted session, a session run must load both -e extensions, and the control's -e extension must list skills and prompt templates", SKIP, async t => { + assert.equal((await fit(standIn(t, { noResume: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't resume the planted session with -c in a session's shape with --no-tools, -c, --thinking, --mode json"); + assert.equal((await fit(standIn(t, { oneExt: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) didn't load every -e extension in a session's shape with --no-tools"); + assert.equal((await fit(standIn(t, { extNoCommands: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: its -e extension didn't list a planted skill, prompt template without the seal flags"); +}); + +// Re-check 6 P2-1: no bus session has PI_CODING_AGENT_DIR (sessionEnv drops it), so its pi reads +// $HOME/.pi/agent. A pi that leaks only when the variable is unset must be refused. +test("pi-unpin S3 re-check 6: a pi that loads every kind when PI_CODING_AGENT_DIR is unset, as in a bus session, is refused", SKIP, async t => { + const r = await fit(standIn(t, { leakNoAgentDir: true }).env); + assert.equal(r.ok, false); + assert.match(r.message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted /); +}); + +// Re-check 6 P3-2: the adapter passes -c whenever the session directory isn't empty, so -c can +// meet a directory with nothing for this workspace. A run for that costs about 340 ms cold, over +// the bar the Lead set, so pi-fit.mjs names it under "Not run". This test holds the gap open in +// plain sight: when a run for it lands, it goes red and must be turned round. +test("pi-unpin S3 re-check 6: a pi that leaks only under -c over a session directory with nothing for its directory passes: a named gap, not run", SKIP, async t => { + assert.equal((await fit(standIn(t, { leakNoMatch: true }).env)).ok, true); +}); + +// A bus session's environment for pi, built the way the launcher, the sandbox and the runner build +// it: sessionEnv over host (a login shell's or a unit's, HOSTS above) with PI_CODING_AGENT_DIR +// added (sessionEnv drops it), the sandbox's sessionSetenv, adapterEnv with pi.env() as +// harnessEnv, and the runner's MOSAIC_REQUEST. A json run is a proxy session's first turn. A +// non-proxy run's PI_PROVIDER is the session's provider; a proxy run's is the transport's. The run +// id, business, role and run directory layout are the fit's, so the values the code sets can be +// compared with a fit run's. pi.env() reads a passed fit check: await one for s first. +function busEnv(s, dir, { host = HOSTS.login, tools = [], thinking = "", provider = fitModule.SESSION_SHAPE.provider, json = false, prompt = join(dir, "prompt.md") } = {}) { + const session = { provider, model: fitModule.SESSION_SHAPE.model, thinking, toolSocket: join(dir, "tools.sock"), proxyTransport: { piProvider: provider } }; + const harnessEnv = pi.env({ policy: { tools }, tools: [], session, proxy: json ? { key: "fixture-key" } : null, env: s.env }); + const runner = { ...sessionEnv({ ...host, PI_CODING_AGENT_DIR: join(dir, "host-agent") }, "fit", json ? {} : null), ...sessionSetenv(dir, { business: "fit", role: "fit" }) }; + const env = adapterEnv({ ...session, bundle: { prompt, policy: join(dir, "policy.json"), tools: join(dir, "tools.json") }, workspace: join(dir, "project"), sessionDir: join(dir, "sessions"), turnMarker: join(dir, "turn"), harnessEnv, runDir: dir, proxyFirstTurn: json }, runner); + return { ...env, MOSAIC_REQUEST: "x" }; +} + +// Re-check 6 P2-1, one step on: the fit runs' variables are a bus session's, built by the same +// code, so no run sets a name a session lacks or lacks one a session sets. The one named +// exception, with its reason: +const FIT_ENV_EXCEPT = [ + ["provider variables (isProviderVariable)", isProviderVariable, "the fit check passes no key: it calls no model"], +]; +// Re-check 7 P2-1: the names whose values a fit run sets differently from a session, with why. +const FIT_VALUE_EXCEPT = [ + ["MOSAIC_EXTENSIONS", "a session's -e files are the tool gate and the unslop check; a fit run's record what the run saw"], + ["MOSAIC_REQUEST", "a session's request; a fit run's -p message"], +]; +// Re-check 7 P2-1: the fit runs in the bus, so its environment comes from the bus's own. Over a +// login shell's host and a unit's, a fit run has the names a session has, apart from the provider +// variables, and the same values: a host name keeps the host's value, and a name the code sets has +// the value a session's code gives it for the same run directory, apart from FIT_VALUE_EXCEPT. +test("pi-unpin S3 re-check 7: a fit run gets the variable names and values a bus session's pi gets, from a login shell's host and from a unit's, and the runs themselves hold them", SKIP, async t => { + const s = standIn(t, {}); + assert.equal((await fit(s.env)).ok, true); + const view = locate(s.env).view; + const kept = (names) => names.filter((n) => !FIT_ENV_EXCEPT.some(([, drop]) => drop(n))).sort(); + const valueExcept = FIT_VALUE_EXCEPT.map(([n]) => n); + const names = {}; + for (const [label, host] of Object.entries(HOSTS)) { + names[label] = {}; + for (const json of [false, true]) { + const dir = mkdtempSync(join(s.root, "env-")); + const bus = busEnv(s, dir, { host, tools: ["read", "bash"], thinking: "low", json }); + const busNames = kept(Object.keys(bus)); + const fitEnv = fitModule.fitEnv(dir, { view, host, shape: { tools: "read,bash", thinking: "low", json } }); + const fitNames = Object.keys(fitEnv).sort(); + const at = `${label} host, json ${json}`; + assert.deepEqual(fitNames.filter((n) => !busNames.includes(n)), [], `fit names a bus session lacks (${at})`); + assert.deepEqual(busNames.filter((n) => !fitNames.includes(n)), [], `bus names a fit run lacks (${at})`); + for (const n of fitNames) if (!valueExcept.includes(n)) assert.equal(fitEnv[n], bus[n], `${n} (${at})`); + for (const n of valueExcept) assert.ok(fitNames.includes(n), n); + names[label][json ? "json" : "other"] = busNames; + } + // A run that isn't session-shaped gets the same names as one without --mode json. + assert.deepEqual(Object.keys(fitModule.fitEnv(mkdtempSync(join(s.root, "env-")), { view, host })).sort(), names[label].other); + assert.deepEqual(fitModule.fitNames(host), kept(Object.keys(sessionEnv(host, "fit"))), label); + assert.ok(!names[label].other.includes("PI_CODING_AGENT_DIR")); + } + // A unit-started bus's sessions have no TERM, TZ or LC_ALL, so its fit runs have none either. + for (const n of ["TERM", "TZ", "LC_ALL"]) { + assert.ok(names.login.other.includes(n) && names.login.json.includes(n), n); + assert.ok(!names.unit.other.includes(n) && !names.unit.json.includes(n), n); + } + // The env each run's process gets, not only fitEnv's object: this pi loads every kind when its + // variables differ from the bus names. It passes under each host; expecting one name more is + // refused. + for (const [label, host] of Object.entries(HOSTS)) { + const own = names[label]; + assert.equal((await fit(standIn(t, { leakEnv: own }).env, host)).ok, true, label); + for (const extra of [{ json: own.json, other: [...own.other, "PI_CODING_AGENT_DIR"] }, { json: [...own.json, "PI_CODING_AGENT_DIR"], other: own.other }]) { + assert.match((await fit(standIn(t, { leakEnv: extra }).env, host)).message ?? "passed", /loaded a planted /, label); + } + } + for (const [, drop, reason] of FIT_ENV_EXCEPT) assert.ok(typeof drop === "function" && reason.length > 0); + for (const [name, reason] of FIT_VALUE_EXCEPT) assert.ok(typeof name === "string" && reason.length > 0); +}); + +// Re-check 7 P2-1: a pi that leaks only when TERM, TZ or LC_ALL is unset, as in every session of a +// bus started by its unit. Under a login shell's host the same files pass, since its sessions have +// the name; the host's names are in the cache key, so a bus under the unit's host checks again and +// is refused. +test("pi-unpin S3 re-check 7: a pi that loads every kind when TERM, TZ or LC_ALL is unset, as in a unit-started bus, is refused under the unit's host", SKIP, async t => { + for (const name of ["TERM", "TZ", "LC_ALL"]) { + const s = standIn(t, { leakNoVar: [name] }); + assert.equal((await fit(s.env, HOSTS.login)).ok, true, name); + const r = await fit(s.env, HOSTS.unit); + assert.equal(r.ok, false, name); + assert.match(r.message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted user extension, .* with the seal flags on$/, name); + noPath(r.message, s.paths); + assert.equal((await pi.preflight({ env: { ...CREDENTIAL, ...s.env } })).code, process.env[name] === undefined ? "harness-feature-missing" : undefined, `${name} under this process's own environment`); + } +}); + +// Re-check 5 item 2: the --no-mcp run is the only one that shows --no-mcp alone keeps MCP out. +// This pi keeps MCP out with --no-mcp or with --no-context-files, so every other run hides that +// --no-mcp does nothing on its own. +test("pi-unpin S3 re-check 5: a pi whose --no-mcp works only beside another seal flag is can't tell at the --no-mcp run", SKIP, async t => { + assert.equal((await fit(standIn(t, { extraFlags: ["--no-mcp"], mcpAlso: "--no-context-files" }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted MCP server with every seal flag but --no-mcp"); + assert.equal((await fit(standIn(t, { extraFlags: ["--no-mcp"] }).env)).ok, true); +}); + +test("pi-unpin S3 re-check 3: a pi that remembers the control in its scratch directory and hides later is refused", SKIP, async t => { + assert.match((await fit(standIn(t, { remember: true }).env)).message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted user extension, .* with the seal flags on$/); +}); + +test("pi-unpin S3 re-check 2: a kind shown only through a source that waits on project trust is can't tell, so --no-approve can't stand in for another seal flag", SKIP, async t => { + assert.equal((await fit(standIn(t, { trustExtOnly: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted extension without the seal flags"); + assert.equal((await fit(standIn(t, { trustCtxOnly: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted context file without the seal flags"); +}); + +test("pi-unpin S3 re-check 2: a run that prints more than 4 MB is refused as that, not as a timeout", SKIP, async t => { + assert.equal((await fit(standIn(t, { flood: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: the start without the seal flags printed more than 4 MB"); +}); + +test("pi-unpin S3 re-check 2: a --version that hangs costs a few seconds, not the run timeout, and leaves no version", SKIP, async t => { + const start = Date.now(); + const r = await fit(standIn(t, { versionHang: true }).env); + const ms = Date.now() - start; + assert.equal(r.ok, true, r.message); + assert.equal(r.version, null); + assert.ok(ms < 12000, `took ${ms} ms`); +}); + +test("pi-unpin S3 review: a stand-in that drops the --system-prompt file, or runs no session_start in -p, is refused", SKIP, async t => { + assert.equal((await fit(standIn(t, { ignoreSystemPrompt: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) didn't use the --system-prompt file with the seal flags on"); + // The session-shaped runs are -p, so the first of them is where that shows. + assert.equal((await fit(standIn(t, { printNoStart: true }).env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: its -e extension didn't see the system prompt in a session's shape with --no-tools"); +}); + +// Review finding 3: only the option column counts, not a flag named in a sentence. +test("pi-unpin S3 review: a flag named only in help prose is missing", SKIP, async t => { + const s = standIn(t, { dropFlags: ["--no-context-files", "--no-approve"], helpProse: ["Removed: --no-context-files is gone in this release", "Note: --no-approve was removed", " -x, --no-context-files-old a different flag"] }); + assert.equal((await fit(s.env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files, --no-approve"); + // An alias column counts: "-e" listed as "--extension, -e ". + const aliased = standIn(t, { dropFlags: ["-e"], helpProse: [" --extension, -e Load an extension file"] }); + const alias = await fit(aliased.env); + assert.equal(alias.ok, true, alias.message); +}); + +test("pi-unpin S3: a stand-in that ignores an -e extension with the seal flags on is refused, since a session's gate comes in that way", SKIP, async t => { + const s = standIn(t, { dropExplicit: true }); + const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) didn't load an -e extension with the seal flags on" }); + noPath(problem.message, s.paths); +}); + +test("pi-unpin S3: a binary-printed version that is not version-shaped stays out of the line, and a --version failure is no refusal", SKIP, async t => { + const s = standIn(t, { version: "/home/someone/secret 1.0", dropFlags: ["--no-skills"] }); + assert.equal((await fit(s.env)).message, "pi (MOSAIC_PI_BIN) lacks --no-skills"); + const quiet = standIn(t, { versionExit: 1 }); + const r = await fit(quiet.env); + assert.equal(r.ok, true, r.message); + assert.equal(r.version, null); +}); + +test("pi-unpin S3: a pass is cached by the files that run; a changed file is checked again, and a refusal is not cached", SKIP, async t => { + const s = standIn(t, {}); + const first = await fit(s.env); + assert.equal(first.ok, true, first.message); + assert.equal((await fit(s.env)), first, "the second call reuses the pass"); + writeFileSync(s.cli, piStandIn({ version: "2.0.0", dropFlags: ["--no-themes"] }), { mode: 0o755 }); + assert.equal((await fit(s.env)).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-themes"); + writeFileSync(s.cli, piStandIn({ version: "2.0.0" }), { mode: 0o755 }); + assert.equal((await fit(s.env)).ok, true, "the refusal was not kept"); +}); + +// Review finding 7: a copy mounted with the node_modules it sits in (the repository's copy) is +// checked again when npm's lock files there change, since it imports packages beside it. +test("pi-unpin S3 review: for a copy mounted with its node_modules, a changed lock file is checked again", SKIP, async t => { + const s = standIn(t, {}); + const nodeModules = join(s.root, "prefix", "lib", "node_modules"); + const found = { ...locate(s.env), root: nodeModules, dest: null }; + const lock = join(nodeModules, ".package-lock.json"); + writeFileSync(lock, "{}"); + const first = await piFit(found, { levels: LEVELS }); + assert.equal(first.ok, true, first.message); + assert.equal(await piFit(found, { levels: LEVELS }), first); + writeFileSync(lock, '{"packages":{}}'); + const again = await piFit(found, { levels: LEVELS }); + assert.equal(again.ok, true, again.message); + assert.notEqual(again, first, "a changed .package-lock.json is checked again"); + writeFileSync(join(s.root, "prefix", "lib", "package-lock.json"), "{}"); + assert.notEqual(await piFit(found, { levels: LEVELS }), again, "a changed package-lock.json beside node_modules is checked again"); +}); + +test("pi-unpin S3: when --help lists --no-mcp the session env asks the adapter to pass it", SKIP, async t => { + const s = standIn(t, { extraFlags: ["--no-mcp"] }); + assert.equal((await fit(s.env)).noMcp, true); + const env = pi.env({ policy: { tools: ["read"] }, tools: [], session: { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }, env: s.env }); + assert.equal(env.MOSAIC_PI_NO_MCP, "1"); + assert.ok(pi.envNames.includes("MOSAIC_PI_NO_MCP")); + const other = standIn(t, {}); + assert.equal((await fit(other.env)).noMcp, false); + const plain = pi.env({ policy: { tools: ["read"] }, tools: [], session: { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }, env: other.env }); + assert.equal(plain.MOSAIC_PI_NO_MCP, "0"); +}); + +// Re-check 7: env() runs nothing. ready() (for a host that calls env() without preflight, such as +// Triage) awaits the check and throws its refusal line; env() then has no pass to read and throws. +test("pi-unpin S3: ready() refuses a binary that fails the fit check, naming it by its label, and env() has no pass to read", SKIP, async t => { + const s = standIn(t, { dropFlags: ["--no-approve"] }); + const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }; + await assert.rejects(pi.ready({ env: s.env }), { message: "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-approve" }); + assert.throws(() => pi.env({ policy: { tools: ["read"] }, tools: [], session, env: s.env }), { message: "pi (MOSAIC_PI_BIN) has no passed fit check for its files; preflight or ready() runs one first" }); + const good = standIn(t, {}); + await pi.ready({ env: good.env }); + assert.equal(pi.env({ policy: { tools: ["read"] }, tools: [], session, env: good.env }).MOSAIC_PI_NO_MCP, "0"); +}); + +// Re-check 7: a launch never goes ahead on a fit that has not finished. While one runs, env() has +// no pass to read; once it is awaited, env() reads it. +test("pi-unpin S3 re-check 7: env() refuses while the fit check is still running", SKIP, async t => { + const s = standIn(t, {}); + const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }; + const pending = fit(s.env); + assert.throws(() => pi.env({ policy: { tools: ["read"] }, tools: [], session, env: s.env }), /has no passed fit check/); + assert.equal((await pending).ok, true); + assert.equal(pi.env({ policy: { tools: ["read"] }, tools: [], session, env: s.env }).PI_OFFLINE, "1"); +}); + +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" }); + // 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" }); + // An unreadable package is refused before anything runs. + const found = locate(s.env); + rmSync(join(s.root, "prefix", "lib", "node_modules", NAME, "package.json")); + assert.equal((await piFit(found, { levels: LEVELS })).message, "pi (MOSAIC_PI_BIN) can't be checked: its files can't be read"); +}); + +// Re-check 7: a run whose pipes stay open after it exits (a process left behind holds them, as one +// can when a kill lands before setpriv sets the parent-death signal) must not hang the check. The +// bwrap here leaves a sleep holding stdout and stderr; each run waits a short grace, not the sleep. +test("pi-unpin S3 re-check 7: a run whose pipes outlive it doesn't hang the check", SKIP, async t => { + const s = standIn(t, { dropFlags: ["--no-context-files"] }); + const pids = join(s.root, "held.pids"); + const bwrap = join(s.root, "holding-bwrap"); + writeFileSync(bwrap, `#!/bin/sh\n/usr/bin/sleep 10 &\necho $! >> '${pids}'\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 }); + t.after(() => { for (const p of existsSync(pids) ? readFileSync(pids, "utf8").split("\n").filter(Boolean) : []) try { process.kill(Number(p), "SIGKILL"); } catch {} }); + const start = performance.now(); + const r = await piFit(locate(s.env), { levels: LEVELS, bwrap }); + const ms = performance.now() - start; + assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files"); + assert.ok(ms < 8000, `the check took ${Math.round(ms)} ms with its pipes held`); +}); + +test("pi-unpin S3: the fit check leaves no scratch behind", SKIP, async t => { + const before = readdirSync(tmpdir()).filter((n) => n.startsWith("mosaic-pi-fit-") && !n.startsWith("mosaic-pi-fit-t-")); + await fit(standIn(t, { ignoreAll: true }).env); + const after = readdirSync(tmpdir()).filter((n) => n.startsWith("mosaic-pi-fit-") && !n.startsWith("mosaic-pi-fit-t-")); + assert.deepEqual(after, before); +}); + +// Re-check 9 R9-P2-2: a check dies with the process that runs it. A child process runs real pi's +// cold check with its own TMPDIR and is killed with SIGKILL once several runs are under way, as a +// Console stop kills a bus. 250 ms later no process may have that TMPDIR on its command line (every +// layer of a run names its scratch directory there: the setpriv, shell, unshare and bwrap argv and +// pi's own -e and --system-prompt). The next check, in another process, removes the directories the +// killed one left. +const procsNaming = (text) => readdirSync("/proc").filter((p) => /^\d+$/.test(p) && Number(p) !== process.pid).filter((p) => { + try { return readFileSync(`/proc/${p}/cmdline`, "utf8").includes(text); } catch { return false; } +}); +test("pi-unpin S3 re-check 9: a fit check killed with its process leaves no process, and the next check removes its scratch", SKIP, async t => { + const dir = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-")); + t.after(() => { + for (const p of procsNaming(dir)) try { process.kill(Number(p), "SIGKILL"); } catch {} + rmSync(dir, { recursive: true, force: true }); + }); + const fitUrl = new URL("../src/pi-fit.mjs", import.meta.url).href; + const binUrl = new URL("../../plugins/harness/pi/binary.mjs", import.meta.url).href; + const script = (env) => `const { piFit } = await import(${JSON.stringify(fitUrl)}); const { locate } = await import(${JSON.stringify(binUrl)}); + const r = await piFit(locate(${JSON.stringify(env)}), { levels: ${JSON.stringify(LEVELS)} }); console.log(JSON.stringify(r));`; + const childEnv = { PATH: NODE_PATH, HOME: dir, LANG: "C.UTF-8", TMPDIR: dir }; + const { spawn } = await import("node:child_process"); + const owner = spawn(process.execPath, ["--input-type=module", "-e", script({ PATH: "/usr/bin:/bin" })], { env: childEnv, stdio: "ignore" }); + const exited = new Promise((resolve) => owner.on("exit", resolve)); + const seen = new Set(); + const deadline = Date.now() + 30000; + while (seen.size < 6 && Date.now() < deadline) { + for (const n of readdirSync(dir)) if (n.startsWith("mosaic-pi-fit-")) seen.add(n); + await new Promise((resolve) => setTimeout(resolve, 10)); + } + assert.ok(seen.size >= 6, `the check made ${seen.size} runs before the deadline`); + owner.kill("SIGKILL"); + await exited; + await new Promise((resolve) => setTimeout(resolve, 250)); + const left = procsNaming(dir); + assert.deepEqual(left, [], `${left.length} processes from the killed check still run`); + // The next check, in another process: a stand-in refused at --help is enough to start one. + const s = standIn(t, { dropFlags: ["--no-themes"] }); + const next = spawnSync(process.execPath, ["--input-type=module", "-e", script(s.env)], { env: childEnv, encoding: "utf8", timeout: 30000 }); + assert.equal(JSON.parse(next.stdout).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-themes"); + assert.deepEqual(readdirSync(dir).filter((n) => n.startsWith("mosaic-pi-fit-")), [], "the killed check's scratch is still there"); +}); + +// Re-check 9 R9-P2-2: bwrap's child waits for bwrap to write its uid map before it sets its own +// parent-death signal, so a bwrap killed in that window leaves the child waiting for good, and +// --die-with-parent can't reach it. This bwrap stand-in models that: it leaves a sleep that no +// parent-death signal reaches, then becomes a process that dies with its parent, as bwrap with +// --die-with-parent does. The owner is killed while the check's sandbox probe waits on it. The +// sleep must die too: it is in the check's own pid namespace, which dies with its pid 1. +test("pi-unpin S3 re-check 9: a process bwrap leaves behind in its setup dies with the check's owner", SKIP, async t => { + const s = standIn(t, {}); + const stuck = join(s.root, "stuck.pid"); + const bwrap = join(s.root, "stuck-bwrap"); + // The left process records its pid as the host sees it (the pid namespace the check makes keeps + // the host's /proc, so cut's parent pid there is the shell's host pid), then becomes the sleep. + writeFileSync(bwrap, `#!/bin/sh\n/bin/sh -c 'cut -d" " -f4 /proc/self/stat > "$0"; exec /usr/bin/sleep 60' '${stuck}' &\nexec /usr/bin/setpriv --pdeathsig KILL -- /usr/bin/sleep 60\n`, { mode: 0o755 }); + let pid = null; + t.after(() => { if (pid) try { process.kill(pid, "SIGKILL"); } catch {} }); + const fitUrl = new URL("../src/pi-fit.mjs", import.meta.url).href; + const binUrl = new URL("../../plugins/harness/pi/binary.mjs", import.meta.url).href; + const { spawn } = await import("node:child_process"); + const owner = spawn(process.execPath, ["--input-type=module", "-e", `const { piFit } = await import(${JSON.stringify(fitUrl)}); const { locate } = await import(${JSON.stringify(binUrl)}); + await piFit(locate(${JSON.stringify(s.env)}), { levels: [], bwrap: ${JSON.stringify(bwrap)} });`], { env: { PATH: NODE_PATH, TMPDIR: s.root }, stdio: "ignore" }); + const exited = new Promise((resolve) => owner.on("exit", resolve)); + const deadline = Date.now() + 10000; + const written = () => existsSync(stuck) && readFileSync(stuck, "utf8").endsWith("\n"); + while (!written() && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 10)); + pid = written() ? Number(readFileSync(stuck, "utf8")) : null; + assert.ok(pid > 0, "the stand-in left no process"); + assert.match(readFileSync(`/proc/${pid}/cmdline`, "utf8"), /sleep/); + owner.kill("SIGKILL"); + await exited; + await new Promise((resolve) => setTimeout(resolve, 250)); + let alive = true; + try { alive = readFileSync(`/proc/${pid}/cmdline`, "utf8").includes("sleep"); } catch { alive = false; } + assert.equal(alive, false, "the process bwrap left behind outlived the check's owner"); +}); + +// Re-check 9 R9-P2-2: the parent-death signal setpriv sets comes too late if the owner died first, +// so the chain checks, after the signal is set, that its parent is still the process that built +// it. Started by any other process (here a shell between them), it exits before bwrap starts. +test("pi-unpin S3 re-check 9: a fit run starts only under the process that built its command line", SKIP, async t => { + const s = standIn(t, {}); + const marker = join(s.root, "bwrap-ran"); + const bwrap = join(s.root, "marking-bwrap"); + writeFileSync(bwrap, `#!/bin/sh\n: > '${marker}'\n`, { mode: 0o755 }); + const argv = checkArgv([], ["/usr/bin/true"], { chdir: "/", bwrap }); + const other = spawnSync("/bin/sh", ["-c", '"$@"; exit $?', "sh", ...argv], { env: {}, timeout: 10000 }); + assert.equal(other.status, 1); + assert.equal(existsSync(marker), false, "the chain started under another parent"); + const own = spawnSync(argv[0], argv.slice(1), { env: {}, timeout: 10000 }); + assert.equal(own.status, 0, String(own.stderr)); + assert.equal(existsSync(marker), true, "the chain didn't start under its own process"); +}); + +// Re-check 10 R10-P2-1: unshare's outer layer is best effort. Ubuntu 24.04 with +// apparmor_restrict_unprivileged_userns=1 refuses it, and util-linux before 2.36 lacks +// --map-current-user. Without it the sandbox is the same: bwrap's own argv is identical, so only +// the steps before bwrap differ. +test("pi-unpin S3 re-check 10: bwrap's command line is the same with and without unshare's outer layer", () => { + const list = [{ kind: "ro", src: "/srv/fixture" }, { kind: "rw", src: "/srv/fixture/scratch" }]; + const opts = { env: { HOME: "/srv/fixture/home" }, chdir: "/srv/fixture/scratch", node: "/usr/bin/node", bwrap: BWRAP }; + const outer = checkArgv(list, ["/usr/bin/pi", "--help"], { ...opts, unshare: UNSHARE }); + const plain = checkArgv(list, ["/usr/bin/pi", "--help"], { ...opts, unshare: null }); + const at = (argv) => argv.indexOf(BWRAP); + assert.ok(at(plain) > 0 && at(outer) > at(plain)); + assert.deepEqual(outer.slice(at(outer)), plain.slice(at(plain))); + assert.deepEqual(outer.slice(0, at(outer)), [...plain.slice(0, at(plain)), UNSHARE, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"]); + assert.equal(plain.includes(UNSHARE), false); + for (const flag of ["--die-with-parent", "--unshare-user", "--unshare-pid", "--unshare-ipc", "--unshare-net", "--remount-ro", "--clearenv"]) assert.ok(plain.includes(flag), flag); + assert.deepEqual(plain.slice(0, 4), ["/usr/bin/setpriv", "--pdeathsig", "KILL", "--"]); +}); + +// The real pi, in a child process whose /usr/bin/unshare is a stand-in that refuses as Ubuntu's +// does, or is not there at all, still passes the fit check, and its launch preflight refuses +// nothing. The child sees the host through an outer bwrap; for the missing case /usr/bin is a tmpfs +// with only the tools a check runs bound back. +test("pi-unpin S3 re-check 10: with an unshare that is refused or missing, real pi passes the fit check and no launch is refused", SKIP, async t => { + const dir = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + const refusing = join(dir, "refusing-unshare"); + writeFileSync(refusing, `#!/bin/sh\necho "unshare: write failed /proc/self/uid_map: Operation not permitted" >&2\nexit 1\n`, { mode: 0o755 }); + const fitUrl = new URL("../src/pi-fit.mjs", import.meta.url).href; + const binUrl = new URL("../../plugins/harness/pi/binary.mjs", import.meta.url).href; + const piUrl = new URL("../../plugins/harness/pi/index.mjs", import.meta.url).href; + const script = `const pi = await import(${JSON.stringify(piUrl)}); const { piFit } = await import(${JSON.stringify(fitUrl)}); const { locate } = await import(${JSON.stringify(binUrl)}); + const refused = await pi.preflight({ env: { PATH: "/usr/bin:/bin", HOME: process.env.HOME }, proxy: { key: "fixture" } }); + const r = await piFit(locate({ PATH: "/usr/bin:/bin" }), { levels: ${JSON.stringify(LEVELS)} }); + console.log(JSON.stringify({ refused, ok: r.ok, outer: r.outer, message: r.message }));`; + const kept = ["setpriv", "bwrap", "true", "sh", "bash", "env"].map((b) => `/usr/bin/${b}`); + if (process.execPath.startsWith("/usr/bin/")) kept.push(process.execPath); + const views = { + "unshare failed": ["--ro-bind", refusing, "/usr/bin/unshare"], + "unshare is missing": ["--tmpfs", "/usr/bin", ...kept.flatMap((b) => ["--ro-bind", b, b])], + }; + for (const [why, view] of Object.entries(views)) { + const home = mkdtempSync(join(dir, "home-")); + const r = spawnSync(BWRAP, ["--dev-bind", "/", "/", ...view, "--", process.execPath, "--input-type=module", "-e", script], { env: { PATH: NODE_PATH, HOME: home, LANG: "C.UTF-8", TMPDIR: home }, encoding: "utf8", timeout: 60000 }); + assert.equal(r.status, 0, `${why}: ${r.stderr}`); + assert.deepEqual(JSON.parse(r.stdout), { refused: null, ok: true, outer: { used: false, why } }, why); + } +}); + +// R10-P3-1: the line names the tool that failed. An unshare that fails is never a refusal; the +// pass records how it failed. Only a bwrap that fails alone refuses, and that names bwrap. +test("pi-unpin S3 re-check 10: a failed probe names the tool that failed, and only bwrap's failure refuses", SKIP, async t => { + const s = standIn(t, {}); + const tool = (name, body) => { + const p = join(s.root, name); + writeFileSync(p, `#!/bin/sh\n${body}\n`, { mode: 0o755 }); + return p; + }; + const refusing = tool("refusing-unshare", 'echo "unshare: write failed /proc/self/uid_map: Operation not permitted" >&2; exit 1'); + const old = tool("old-unshare", `case " $* " in *" --map-current-user "*) echo "unshare: unrecognized option '--map-current-user'" >&2; exit 1;; esac; exec '${UNSHARE}' "$@"`); + const noBwrapInside = tool("no-bwrap-inside-unshare", `case " $* " in *" ${BWRAP} "*) exit 1;; esac; exec '${UNSHARE}' "$@"`); + const broken = tool("broken-bwrap", `echo "bwrap: Can't find source path ${s.root}/x" >&2; exit 1`); + const fitWith = (opts) => piFit(locate(s.env), { levels: LEVELS, ...opts }); + for (const [unshare, why] of [[refusing, "unshare failed"], [old, "unshare failed"], [noBwrapInside, "unshare failed with bwrap inside it"], [join(s.root, "no-unshare"), "unshare is missing"]]) { + const r = await fitWith({ unshare }); + assert.equal(r.ok, true, `${why}: ${r.message}`); + 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" }); +}); + +// R10-P2-1: without unshare's outer layer, a bwrap killed in its setup leaves its child waiting for +// good, with bwrap's command line. This bwrap stand-in models that child: for each run it forks a +// subshell (no parent-death signal reaches a forked child) that waits on a FIFO no one opens, then +// runs the real bwrap and stops the subshell when bwrap is done. The owner, on the no-unshare path, +// is killed while runs are under way. The next check, in another process, must stop what the +// killed one left and remove its scratch, checked by command line. +test("pi-unpin S3 re-check 10: without unshare, a check killed with its process leaves nothing once the next check has run", SKIP, async t => { + const dir = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-")); + t.after(() => { + for (const p of procsNaming(dir)) try { process.kill(Number(p), "SIGKILL"); } catch {} + rmSync(dir, { recursive: true, force: true }); + }); + const s = standIn(t, {}); + const fifo = join(s.root, "never"); + spawnSync("/usr/bin/mkfifo", [fifo]); + const pids = join(s.root, "left.pids"); + const bwrap = join(s.root, "setup-bwrap"); + writeFileSync(bwrap, `#!/bin/sh +case " $* " in *" /usr/bin/true "*) exec '${BWRAP}' "$@";; esac +( exec >/dev/null 2>&1 > '${pids}' +'${BWRAP}' "$@" +st=$? +kill -9 $left +exit $st +`, { mode: 0o755 }); + const fitUrl = new URL("../src/pi-fit.mjs", import.meta.url).href; + const binUrl = new URL("../../plugins/harness/pi/binary.mjs", import.meta.url).href; + const script = (env, opts) => `const { piFit } = await import(${JSON.stringify(fitUrl)}); const { locate } = await import(${JSON.stringify(binUrl)}); + const r = await piFit(locate(${JSON.stringify(env)}), { levels: ${JSON.stringify(LEVELS)}, ...${JSON.stringify(opts)} }); console.log(JSON.stringify(r));`; + const childEnv = { PATH: NODE_PATH, HOME: dir, LANG: "C.UTF-8", TMPDIR: dir }; + const { spawn } = await import("node:child_process"); + const owner = spawn(process.execPath, ["--input-type=module", "-e", script(s.env, { bwrap, unshare: join(s.root, "no-unshare") })], { env: childEnv, stdio: "ignore" }); + const exited = new Promise((resolve) => owner.on("exit", resolve)); + const count = () => existsSync(pids) ? readFileSync(pids, "utf8").split("\n").filter(Boolean).length : 0; + const deadline = Date.now() + 30000; + while (count() < 2 && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 5)); + assert.ok(count() >= 2, `the check made ${count()} runs before the deadline`); + owner.kill("SIGKILL"); + await exited; + await new Promise((resolve) => setTimeout(resolve, 250)); + assert.ok(procsNaming(dir).length > 0, "the killed check left nothing to stop: the stand-in didn't model bwrap's setup child"); + const next = spawnSync(process.execPath, ["--input-type=module", "-e", script(standIn(t, { dropFlags: ["--no-themes"] }).env, {})], { env: childEnv, encoding: "utf8", timeout: 30000 }); + assert.equal(JSON.parse(next.stdout).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-themes"); + await new Promise((resolve) => setTimeout(resolve, 100)); + const left = procsNaming(dir); + assert.deepEqual(left, [], `${left.length} processes from the killed check still run after the next check`); + assert.deepEqual(readdirSync(dir).filter((n) => n.startsWith("mosaic-pi-fit-")), [], "the killed check's scratch is still there"); +}); + +// R10-P2-1: a run the check itself kills (a timeout, or the stop after a refusal) may have had bwrap +// in its setup, and without unshare that leaves bwrap's child. Its directory is removed when the run +// ends, so no later sweep finds it: the check stops it then. The stand-in models the child as the +// test above does, and holds every sealed run before bwrap starts, so the stand-in pi's refusal at +// the control stops them there. +test("pi-unpin S3 re-check 10: without unshare, a run the check stops leaves no process", SKIP, async t => { + const s = standIn(t, { ignoreAll: true }); + const fifo = join(s.root, "never"); + spawnSync("/usr/bin/mkfifo", [fifo]); + const pids = join(s.root, "left.pids"); + const bwrap = join(s.root, "setup-bwrap"); + t.after(() => { for (const p of procsNaming(bwrap)) try { process.kill(Number(p), "SIGKILL"); } catch {} }); + writeFileSync(bwrap, `#!/bin/sh +case " $* " in *" /usr/bin/true "*) exec '${BWRAP}' "$@";; esac +( exec >/dev/null 2>&1 > '${pids}' +case " $* " in *" --no-extensions "*) /usr/bin/setpriv --pdeathsig KILL -- /usr/bin/sleep 5;; esac +'${BWRAP}' "$@" +st=$? +kill -9 $left +exit $st +`, { mode: 0o755 }); + const r = await piFit(locate(s.env), { levels: LEVELS, bwrap, unshare: join(s.root, "no-unshare") }); + assert.match(r.message, /didn't load a planted extension/); + const started = readFileSync(pids, "utf8").split("\n").filter(Boolean).length; + assert.ok(started >= 3, `only ${started} runs started`); + await new Promise((resolve) => setTimeout(resolve, 100)); + const left = procsNaming(bwrap); + assert.deepEqual(left, [], `${left.length} of ${started} runs left a process`); +}); + +// R10-P2-1: the sweep stops only processes that name a stale directory of its own pid namespace: a +// live owner's processes, a process naming another path, one naming a path that only starts with a +// stale directory's name, and one naming a directory from another pid namespace are left alone, and +// so are those directories. +test("pi-unpin S3 re-check 10: the sweep leaves a live owner's processes and another directory's processes alone", SKIP, async t => { + const dir = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-")); + const kids = []; + t.after(() => { + for (const k of kids) try { k.kill("SIGKILL"); } catch {} + rmSync(dir, { recursive: true, force: true }); + }); + const { spawn } = await import("node:child_process"); + const start = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19]; + const ns = /^pid:\[(\d+)\]$/.exec(readlinkSync("/proc/self/ns/pid"))[1]; + const dead = spawn("/usr/bin/sleep", ["60"], { stdio: "ignore" }); + await new Promise((resolve) => dead.on("spawn", resolve)); + const deadName = `${dead.pid}-${start(dead.pid)}`; + dead.kill("SIGKILL"); + await new Promise((resolve) => dead.on("exit", resolve)); + const made = (name) => { const p = join(dir, name); mkdirSync(p); return p; }; + const live = made(`mosaic-pi-fit-${process.pid}-${start(process.pid)}-${ns}-live01`); + const stale = made(`mosaic-pi-fit-${deadName}-${ns}-stale1`); + const otherNs = made(`mosaic-pi-fit-${deadName}-${BigInt(ns) + 1n}-other1`); + const other = made("other"); + const fifo = join(dir, "never"); + spawnSync("/usr/bin/mkfifo", [fifo]); + const naming = async (path) => { + const k = spawn("/bin/sh", ["-c", `read x < '${fifo}'; :`, path], { stdio: "ignore" }); + kids.push(k); + k.gone = new Promise((resolve) => k.on("exit", (code, signal) => resolve(signal))); + await new Promise((resolve) => k.on("spawn", resolve)); + return k; + }; + const stopped = await naming(join(stale, "x")); + const kept = { live: await naming(join(live, "x")), prefix: await naming(`${stale}x/y`), other: await naming(join(other, "x")), otherNs: await naming(join(otherNs, "x")) }; + const fitUrl = new URL("../src/pi-fit.mjs", import.meta.url).href; + const binUrl = new URL("../../plugins/harness/pi/binary.mjs", import.meta.url).href; + const s = standIn(t, { dropFlags: ["--no-themes"] }); + const next = spawnSync(process.execPath, ["--input-type=module", "-e", `const { piFit } = await import(${JSON.stringify(fitUrl)}); const { locate } = await import(${JSON.stringify(binUrl)}); + console.log(JSON.stringify(await piFit(locate(${JSON.stringify(s.env)}), { levels: [] })));`], { env: { PATH: NODE_PATH, HOME: dir, LANG: "C.UTF-8", TMPDIR: dir }, encoding: "utf8", timeout: 30000 }); + assert.equal(JSON.parse(next.stdout).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-themes"); + assert.equal(await Promise.race([stopped.gone, new Promise((resolve) => setTimeout(() => resolve("still running"), 2000))]), "SIGKILL"); + await new Promise((resolve) => setTimeout(resolve, 100)); + for (const [name, k] of Object.entries(kept)) assert.equal(k.exitCode === null && k.signalCode === null, true, `the sweep stopped the ${name} process`); + assert.equal(existsSync(stale), false, "the stale directory is still there"); + for (const p of [live, otherNs, other]) assert.equal(existsSync(p), true, `the sweep removed ${p}`); +}); + +// Re-check 7 P2-2: the check runs in the bus, so its event loop must keep turning while a cold check +// runs: a 10 ms timer never waits more than 250 ms. +const GAP_MAX = 250; +test("pi-unpin S3: real pi 0.85.1 from node_modules is accepted, and the event loop stays live while its cold check runs", SKIP, async t => { + const found = locate({ PATH: "/usr/bin:/bin" }); + assert.equal(found.label, "the repository's pi"); + let at = process.hrtime.bigint(); + const { result: r, max } = await gapDuring(() => piFit(found, { levels: LEVELS })); + const cold = Number(process.hrtime.bigint() - at) / 1e6; + assert.equal(r.ok, true, r.message); + assert.equal(r.version, "0.85.1"); + assert.equal(r.noMcp, false, "0.85.1 has no MCP and no --no-mcp"); + at = process.hrtime.bigint(); + assert.equal(await piFit(found, { levels: LEVELS }), r); + const cached = Number(process.hrtime.bigint() - at) / 1e6; + t.diagnostic(`real pi 0.85.1 fit check: cold ${cold.toFixed(0)} ms, cached ${cached.toFixed(1)} ms, longest event loop gap ${max.toFixed(0)} ms`); + assert.ok(max <= GAP_MAX, `the event loop waited ${max.toFixed(0)} ms during the cold check`); +}); + +test("pi-unpin S3 re-check 7: the event loop stays live while a stand-in's cold check runs", SKIP, async t => { + const { result, max } = await gapDuring(() => fit(standIn(t, {}).env)); + assert.equal(result.ok, true, result.message); + t.diagnostic(`stand-in cold check: longest event loop gap ${max.toFixed(0)} ms`); + assert.ok(max <= GAP_MAX, `the event loop waited ${max.toFixed(0)} ms during the cold check`); +}); + +// Re-check 7 P2-2: two launches that start while a check is under way share the one check. Each +// fit run makes one scratch directory under tmpdir(), so a watch there counts the runs. +test("pi-unpin S3 re-check 7: two launches that start while a fit check runs share it: one check's runs, one verdict", SKIP, async t => { + const made = new Set(); + const watcher = watch(tmpdir(), (event, name) => { if (name && name.startsWith("mosaic-pi-fit-") && !name.startsWith("mosaic-pi-fit-t-")) made.add(name); }); + t.after(() => watcher.close()); + const settle = () => new Promise((resolve) => setTimeout(resolve, 200)); + // One check on its own: how many runs it makes. + const one = standIn(t, {}); + await settle(); + made.clear(); + assert.equal((await fit(one.env)).ok, true); + await settle(); + const runs = made.size; + assert.ok(runs > 10, `one check made ${runs} runs`); + // Two launches' preflights at once, for another binary: the same number of runs. + const two = standIn(t, {}); + await settle(); + made.clear(); + const both = await Promise.all([pi.preflight({ env: { ...CREDENTIAL, ...two.env } }), pi.preflight({ env: { ...CREDENTIAL, ...two.env } })]); + await settle(); + assert.deepEqual(both, [null, null]); + assert.equal(made.size, runs, "two launches at once ran one check"); + // piFit hands the second caller the first one's promise. + const third = standIn(t, {}); + const a = fit(third.env); + const b = fit(third.env); + assert.equal(a, b); + assert.equal((await a).ok, true); + // A refusal is shared too, and not kept. + const bad = standIn(t, { dropFlags: ["--no-themes"] }); + const [x, y] = await Promise.all([pi.preflight({ env: { ...CREDENTIAL, ...bad.env } }), pi.preflight({ env: { ...CREDENTIAL, ...bad.env } })]); + assert.deepEqual(x, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-themes" }); + assert.deepEqual(y, x); +}); + +// Re-check 8 P3-3: a check that throws can't tell, so preflight refuses it as harness-feature-missing, +// not as a throw the launcher turns into harness-failed. A TMPDIR that does not exist makes every +// run's scratch directory fail. +// Re-check 9: a Claude model is offered, routed and tested only on the claude-code harness, offline +// or not, so the route the fit runs pass names no Claude model and no Anthropic provider. +test("pi-unpin S3 re-check 9: the fit check's route names no Claude model and no Anthropic provider", () => { + const claude = /claude|anthropic|sonnet|opus|haiku|fable/i; + const shape = fitModule.sessionShape({ noMcp: false, extensions: ["a.ts"], prompt: "p.md", sessionDir: "s", tools: "read", cont: false, thinking: "", json: false, message: "x" }); + const route = [shape[shape.indexOf("--provider") + 1], shape[shape.indexOf("--model") + 1]]; + assert.deepEqual(route, [fitModule.SESSION_SHAPE.provider, fitModule.SESSION_SHAPE.model]); + for (const name of route) { + assert.equal(typeof name, "string"); + assert.ok(name.length > 0); + assert.doesNotMatch(name, claude, "the fit route names a Claude model or provider"); + } +}); + +test("pi-unpin S3 re-check 8: a fit check that throws refuses as harness-feature-missing, names no path, and keeps nothing", SKIP, async t => { + const s = standIn(t, {}); + const before = process.env.TMPDIR; + process.env.TMPDIR = join(s.root, "no-such-tmp"); + let refusal; + try { + refusal = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + } finally { + if (before === undefined) delete process.env.TMPDIR; else process.env.TMPDIR = before; + } + assert.deepEqual(refusal, { code: "harness-feature-missing", message: "pi (MOSAIC_PI_BIN) can't be checked: the check could not run" }); + for (const p of s.paths) assert.equal(refusal.message.includes(p), false, "the line names a host path"); + // Nothing was kept: with a TMPDIR that exists, the same binary passes. + assert.equal(await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }), null); +}); + +test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it", SKIP, async t => { + const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }; + const run = async (s) => { + assert.equal((await fit(s.env)).ok, true); + const out = join(s.root, "argv"); + writeFileSync(join(s.root, "prompt.md"), "p"); + const given = pi.env({ policy: { tools: ["read"] }, tools: [], session, env: s.env }); + const r = spawnSync("/bin/sh", [join(REPO, "adapters", "pi", "adapter.sh")], { + encoding: "utf8", + env: { ...given, PATH: "/usr/bin:/bin", ARGV_OUT: out, MOSAIC_SYSTEM_PROMPT_FILE: join(s.root, "prompt.md"), MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(s.root, "ws"), MOSAIC_SESSION_DIR: join(s.root, "session"), MOSAIC_MODEL: "m" }, + }); + assert.equal(r.status, 0, r.stderr); + return readFileSync(out, "utf8").split("\n"); + }; + assert.equal((await run(standIn(t, { extraFlags: ["--no-mcp"] }))).filter((a) => a === "--no-mcp").length, 1); + assert.equal((await run(standIn(t, {}))).includes("--no-mcp"), false); +}); + +// Re-check 5 item 1, narrowed by re-check 6 P3-1. The adapter runs with the environment a bus +// session's pi gets (busEnv: sessionEnv over a host, sessionSetenv, adapterEnv, pi.env()), so +// HOME and the rest are set as a session sets them, not by the shell. Re-check 7 P2-1: every run is +// made under both hosts in HOSTS, a login shell's with every ENV_ALLOW name and a unit's without +// TERM, TZ or LC_ALL, so a branch keyed on one of those being set or unset is seen. (Bash run as +// /bin/sh gives an unset TERM the shell value "dumb" without exporting it, so ${TERM:-} is never +// empty in the adapter; a branch on TERM missing from the environment, printenv TERM, is seen.) Each argv must be one +// sessionShape() builds, and each pair of its branches must be passed together by a session-shaped +// fit run. What it tries: with and without --no-mcp; no tools, one, two; -c over an empty session +// directory, one holding a session file named as pi names them, and one holding an unrelated file; +// with and without --mode json; no thinking level and each level; each provider in PROVIDERS. +// Every combination of the first four, with thinking unset or low and the first provider; then +// each other level and each other provider, with and without --mode json; all of it under each +// host. Every $NAME or ${NAME +// the adapter reads must be set in that environment, assigned by the adapter on an earlier line, +// set by the shell (SHELL_SET), or one no bus session gets (NOT_BUS). What it can't see: a branch +// keyed on a value it doesn't try (a model, a path, a request, a tool other than read and bash, a +// provider not listed), or on a pair of values where neither is from the base set. +// The provider names a session's PI_PROVIDER can hold: the providers whose key the pi module +// checks for (index.mjs), then the cliproxy and fixture transports' piProvider values. +const PROVIDERS = ["anthropic", "openai", "openrouter", "zai", "mosaic-proxy-glm", "mosaic-proxy-codex", "mosaic-proxy-other", "mosaic-fixture"]; +// Set by the shell, not the environment: the adapter saves and restores it around its loops. +const SHELL_SET = ["IFS"]; +test("pi-unpin S3 re-check 6: every argv the adapter builds in a bus session's environment is one sessionShape builds, over each tool count, -c case, thinking level and provider, and each pair of its branches is one a fit run passes", SKIP, async t => { + const adapter = join(REPO, "adapters", "pi", "adapter.sh"); + // Set only by worker containers (packages/processing-control) and the TUI (scripts/agent.sh). + const NOT_BUS = ["MOSAIC_SKILLS", "MOSAIC_SESSION_FORK", "MOSAIC_INTERACTIVE"]; + for (const name of NOT_BUS) assert.ok(!ENV_ALLOW.includes(name), name); + const lines = readFileSync(adapter, "utf8").split("\n"); + const firstSet = new Map(); + lines.forEach((l, i) => { + for (const m of l.matchAll(/(?:^|[\s;])([A-Za-z_][A-Za-z0-9_]*)=|\bfor\s+([A-Za-z_][A-Za-z0-9_]*)\s+in\b/g)) { + const name = m[1] ?? m[2]; + if (!firstSet.has(name)) firstSet.set(name, i); + } + }); + const reads = []; + lines.forEach((l, i) => { for (const m of l.matchAll(/\$\{?([A-Za-z_][A-Za-z0-9_]*)/g)) reads.push([m[1], i]); }); + assert.ok(reads.length > 20, "the adapter's variable reads are found"); + const set = new Set(["MOSAIC_REQUEST"]); + const branchKeys = ["tools", "cont", "thinking", "json"]; + const covered = (b) => fitModule.SESSION_RUNS.some((r) => branchKeys.every((k) => !(k in b) || Boolean(r[k]) === b[k])); + const runs = []; + for (const noMcp of [false, true]) for (const tools of [[], ["read"], ["read", "bash"]]) for (const stored of ["none", "named", "other"]) for (const json of [false, true]) for (const thinking of ["", "low"]) runs.push({ noMcp, tools, stored, json, thinking, provider: PROVIDERS[0] }); + for (const json of [false, true]) { + for (const thinking of LEVELS.filter((l) => l !== "low")) runs.push({ noMcp: false, tools: ["read", "bash"], stored: "named", json, thinking, provider: PROVIDERS[0] }); + for (const provider of PROVIDERS.slice(1)) runs.push({ noMcp: false, tools: ["read", "bash"], stored: "named", json, thinking: "low", provider }); + } + const base = runs.length; + for (const run of runs.splice(0)) for (const host of Object.keys(HOSTS)) runs.push({ ...run, host }); + const stands = new Map([false, true].map((noMcp) => [noMcp, standIn(t, { extraFlags: noMcp ? ["--no-mcp"] : [] })])); + for (const s of stands.values()) assert.equal((await fit(s.env)).ok, true); + const seenLevels = new Set(); + const seenProviders = new Set(); + for (const run of runs) { + const { noMcp, tools, stored, json, thinking, provider, host } = run; + const s = stands.get(noMcp); + const dir = mkdtempSync(join(s.root, "run-")); + const prompt = join(dir, "prompt.md"); + writeFileSync(prompt, "p"); + const env = busEnv(s, dir, { host: HOSTS[host], tools, thinking, provider, json, prompt }); + const sessionDir = env.MOSAIC_SESSION_DIR; + mkdirSync(sessionDir, { recursive: true }); + if (stored === "named") writeFileSync(join(sessionDir, "2026-11-19T00-00-00-000Z_0b7c4f2e-0000-4000-8000-000000000000.jsonl"), "{}\n"); + if (stored === "other") writeFileSync(join(sessionDir, "notes.txt"), "x\n"); + for (const name of NOT_BUS) assert.equal(env[name], undefined, name); + for (const name of Object.keys(env)) set.add(name); + assert.equal(env.PI_PROVIDER, provider); + const out = join(dir, "argv"); + const r = spawnSync("/bin/sh", [adapter], { encoding: "utf8", env: { ...env, ARGV_OUT: out } }); + const label = JSON.stringify(run); + // The monitor reads the stand-in's empty output as no answer; the argv is written first. + if (!json) assert.equal(r.status, 0, r.stderr); + const argv = readFileSync(out, "utf8").split("\n").slice(0, -1); + const extensions = env.MOSAIC_EXTENSIONS.split(","); + assert.equal(extensions.length, 2, "the session-shaped fit runs pass two -e, as a session does"); + const cont = stored !== "none"; + const want = fitModule.sessionShape({ noMcp, extensions, prompt, sessionDir, tools: tools.join(","), cont, thinking, json, message: "x" }); + // The fit runs pass a fixed provider; the adapter passes the session's. + want[want.indexOf("--provider") + 1] = provider; + assert.deepEqual(argv, want, label); + const branch = { tools: tools.length > 0, cont, thinking: thinking !== "", json }; + for (const a of branchKeys) for (const b of branchKeys) if (a < b) assert.ok(covered({ [a]: branch[a], [b]: branch[b] }), `${label}: no fit run passes ${a}=${branch[a]} with ${b}=${branch[b]}`); + seenLevels.add(thinking); + seenProviders.add(provider); + } + assert.equal(base, 2 * 3 * 3 * 2 * 2 + 2 * (LEVELS.length - 1) + 2 * (PROVIDERS.length - 1)); + assert.equal(runs.length, base * Object.keys(HOSTS).length); + assert.deepEqual([...seenLevels].sort(), ["", ...LEVELS].sort()); + assert.deepEqual([...seenProviders].sort(), [...PROVIDERS].sort()); + for (const [name, at] of reads) { + const local = firstSet.has(name) && firstSet.get(name) < at; + assert.ok(set.has(name) || local || SHELL_SET.includes(name) || NOT_BUS.includes(name), `the adapter reads ${name} (line ${at + 1}), which no bus session environment here sets and the adapter doesn't assign first`); + } +}); diff --git a/packages/harness/tests/pi-session.test.mjs b/packages/harness/tests/pi-session.test.mjs index 5577be628..427240865 100644 --- a/packages/harness/tests/pi-session.test.mjs +++ b/packages/harness/tests/pi-session.test.mjs @@ -10,7 +10,8 @@ import { join } from "node:path"; import { buildBundle } from "../src/bundle.mjs"; import { adapterEnv, unslopCounts, unslopLog, unslopNotes } from "../src/runner.mjs"; import { REPLY_CAP } from "../src/tools.mjs"; -import { fakeToolSocket, HARNESS, mockAnthropic, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs"; +import { fakeToolSocket, HARNESS, mockAnthropic, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs"; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check const ADAPTER = join(REPO, "adapters", "pi", "adapter.sh"); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs new file mode 100644 index 000000000..2209d81f6 --- /dev/null +++ b/packages/harness/tests/pi-stand-in.mjs @@ -0,0 +1,241 @@ +// #1873 pi-unpin S3: a stand-in pi that the fit check (src/pi-fit.mjs) can run. It is one sh +// script with its knobs written into it, because the fit check runs it with an empty +// environment. Put it at dist/bundle/cli.js in a package named @earendil-works/pi-coding-agent. +// +// What it does, in order: +// --version prints `version` (default 9.9.9), or exits `versionExit` when that is set, or +// hangs for a minute with `versionHang` +// $ARGV_OUT set: a session. Writes its argv there, prints `sessionOut`, exits 0. This part is +// sh, so it runs in a sandbox that holds no node. +// otherwise runs node (the absolute path, since the fit check gives it no PATH) on the rest: +// --help lists every flag the fit check wants, less `dropFlags`, plus `extraFlags`, then +// the `helpProse` lines as they are +// -e a file that doesn't exist stops it with exit 1, unless `missingExtOk` is true or +// names the mode ("rpc" or "print") it starts in anyway +// --mode rpc loads the planted sources the way pi 0.85.1 finds them, honouring the seal flags, +// or -p runs each -e extension's session_start (one that writes JSON.stringify(...) gets +// the prompt this stand-in builds, the command names and the session id), then +// answers get_state and get_commands +// (rpc), or prints `sessionOut` and exits 0 (-p). +// An -e file that exists loads even with --no-extensions, unless `dropExplicit` +// (then it loads only without --no-extensions). `printNoStart`: -p runs no +// session_start. MCP servers stay out with --no-mcp when `extraFlags` lists it, else +// with --no-extensions. +// `ignoreAll` loads no planted source. `sealLeak` lists kinds (extensions, mcp, +// skills, prompts, context, approve) loaded even when their seal flag is on. +// `ignoreSystemPrompt` drops the --system-prompt file. `ignoreAppend` drops every +// APPEND_SYSTEM.md. `sysReplacesAll`: a --system-prompt file also drops every +// APPEND_SYSTEM.md and context file. `trustExtOnly`: the only extensions are the +// project's .pi/extensions, loaded in a trusted project whatever --no-extensions +// says. `trustCtxOnly`: the only context file is the project's AGENTS.md, loaded the +// same way whatever --no-context-files says. `flood`: prints 8 MB first. +// `hideWhen`: a cue, a flag or "order" (--offline before --no-session). With the cue +// it loads no planted source; without it, every kind even with its seal flag on. +// `standIn`: [flag, other]: it ignores flag, and other keeps flag's kind out instead. +// `remember`: the first run in a scratch directory (it leaves a marker beside the +// -e file's directory) loads every kind whatever the seal flags; a later run loads +// none. `needs`: a flag the loader flags work only with; without it they do +// nothing. `leakWithout`: flags; when any of them is absent, every kind loads. +// `leakWith`: flags; when all of them are given, every kind loads (with +// `leakWithout` too, both must hold). `mcpAlso`: a flag that keeps MCP servers out +// as well as their own flag does. `leakNoAgentDir`: every kind loads when +// PI_CODING_AGENT_DIR is unset, as in a bus session. `leakNoVar`: names; every +// kind loads when any of them is unset (TERM, TZ, LC_ALL in a unit-started bus). +// `leakNoMatch`: with -c, every +// kind loads when no .jsonl in --session-dir names the working directory. +// `leakEnv`: { json, other }, the variable names a run with --mode json and any +// other run should get; every kind loads when the environment holds a name not in +// the list or lacks one, apart from the names /bin/sh sets (PWD, OLDPWD, SHLVL, _). +// An -e extension that writes JSON.stringify({...}) at session_start gets the +// prompt, the command names, the session id (with -c, the id in the header of a +// .jsonl in --session-dir whose cwd is the working directory, else a new one) and +// the --thinking level (else "medium"). `noResume`: -c always starts a new +// session. `oneExt`: only the first -e loads. `extNoCommands`: the extension sees +// no commands. +// `commands` "fail" answers get_commands with success false; "list" answers +// with a bare list. +// otherwise a session with no $ARGV_OUT: prints `sessionOut` and exits 0. +// +// Trust, as pi 0.85.1 has it: the project is trusted with --approve, or with a saved trust and +// no --no-approve. Only the project SYSTEM.md and APPEND_SYSTEM.md wait on it here. The user +// APPEND_SYSTEM.md is appended whatever the flags, as 0.85.1 does when the project has none. +import { FIT_FLAGS } from "../src/pi-fit.mjs"; + +const BODY = String.raw` +const fs = require("node:fs"); +const path = require("node:path"); +const cp = require("node:child_process"); +const argv = process.argv.slice(1); +const has = (f) => argv.includes(f); +const val = (f) => { const i = argv.indexOf(f); return i === -1 ? undefined : argv[i + 1]; }; +if (argv[0] === "--help") { + const drop = new Set(K.dropFlags || []); + const lines = ["Usage: pi [options] [messages...]", "", "Options:"]; + for (const f of FLAGS) { + if (drop.has(f)) continue; + if (f === "--mode") lines.push(" --mode Output mode: " + (K.modes || ["text", "json", "rpc"]).join(", ")); + else if (f === "--thinking") lines.push(" --thinking Thinking level: " + (K.levels || ["off", "minimal", "low", "medium", "high", "xhigh", "max"]).join(", ")); + else lines.push(" " + f); + } + for (const f of K.extraFlags || []) lines.push(" " + f); + for (const l of K.helpProse || []) lines.push(l); + process.stdout.write(lines.join("\n") + "\n"); + process.exit(0); +} +const rpc = val("--mode") === "rpc"; +const print = !rpc && has("-p"); +const startsAnyway = K.missingExtOk === true || (rpc && K.missingExtOk === "rpc") || (print && K.missingExtOk === "print"); +for (let i = 0; i < argv.length; i++) { + if (argv[i] === "-e" && !fs.existsSync(argv[i + 1]) && !startsAnyway) { + process.stderr.write("Failed to load extension: it does not exist\n"); + process.exit(1); + } +} +if (rpc || print) { + if (K.flood) { + const chunk = Buffer.alloc(1024 * 1024, "x"); + for (let n = 0; n < 8; n++) for (let off = 0; off < chunk.length;) { + try { off += fs.writeSync(1, chunk, off); } catch (e) { if (e.code !== "EAGAIN") throw e; } + } + } + const ALL = ["extensions", "mcp", "skills", "prompts", "context", "approve"]; + // The id in the header of the last .jsonl in dir whose cwd is the working directory, or null. + const storedFor = (dir) => { + let id = null; + if (dir) for (const n of (() => { try { return fs.readdirSync(dir); } catch { return []; } })()) { + if (!n.endsWith(".jsonl")) continue; + let h = null; + try { h = JSON.parse(fs.readFileSync(path.join(dir, n), "utf8").split("\n")[0]); } catch { h = null; } + if (h && h.type === "session" && h.cwd === process.cwd()) id = h.id; + } + return id; + }; + let ignoreAll = K.ignoreAll; + let leak = new Set(K.sealLeak || []); + if (K.hideWhen) { + const cued = K.hideWhen === "order" ? argv.indexOf("--offline") !== -1 && argv.indexOf("--offline") < argv.indexOf("--no-session") : has(K.hideWhen); + if (cued) ignoreAll = true; + else leak = new Set(ALL); + } + if (K.remember) { + const marker = path.join(path.dirname(path.dirname(val("-e"))), "seen-before"); + if (fs.existsSync(marker)) ignoreAll = true; + else { fs.writeFileSync(marker, ""); leak = new Set(ALL); } + } + if ((K.leakWith || K.leakWithout) && (K.leakWith || []).every(has) && (!K.leakWithout || K.leakWithout.some((f) => !has(f)))) leak = new Set(ALL); + if (K.leakNoAgentDir && !process.env.PI_CODING_AGENT_DIR) leak = new Set(ALL); + if (K.leakNoVar && K.leakNoVar.some((n) => process.env[n] === undefined)) leak = new Set(ALL); + if (K.leakNoMatch && has("-c") && !storedFor(val("--session-dir"))) leak = new Set(ALL); + if (K.leakEnv) { + const want = new Set(K.leakEnv[val("--mode") === "json" ? "json" : "other"]); + const got = Object.keys(process.env).filter((n) => !["PWD", "OLDPWD", "SHLVL", "_"].includes(n)); + if (got.length !== want.size || got.some((n) => !want.has(n))) leak = new Set(ALL); + } + // Whether a seal flag is on: with standIn, its flag reads as the other flag; with needs, only + // when that flag is given too. + const sealed = (flag) => has(K.standIn && K.standIn[0] === flag ? K.standIn[1] : flag) && (!K.needs || has(K.needs)); + const on = (kind, flag) => !ignoreAll && (!sealed(flag) || leak.has(kind)); + const agent = process.env.PI_CODING_AGENT_DIR || path.join(process.env.HOME || "/", ".pi", "agent"); + const cwd = process.cwd(); + const list = (dir, test) => { try { return fs.readdirSync(dir).filter(test).map((n) => path.join(dir, n)); } catch { return []; } }; + const json = (file) => { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } }; + const read = (file) => { try { return fs.readFileSync(file, "utf8"); } catch { return null; } }; + const settings = [json(path.join(agent, "settings.json")), json(path.join(cwd, ".pi", "settings.json"))].filter(Boolean); + const trust = json(path.join(agent, "trust.json")) || {}; + const saved = settings.some((s) => s.defaultProjectTrust === "always") || trust[cwd] === true; + const trusted = !ignoreAll && (has("--approve") || (saved && (!sealed("--no-approve") || leak.has("approve")))); + const commands = []; + // A planted extension writes one marker on load; an -e extension may also write what it saw at + // session_start. Do what each would do. + const starts = []; + const load = (f) => { + const text = read(f); + if (text === null) return; + for (const m of text.matchAll(/writeFileSync\(("(?:[^"\\]|\\.)*")(, JSON\.stringify\()?/g)) { + if (m[2]) starts.push(JSON.parse(m[1])); + else fs.writeFileSync(JSON.parse(m[1]), ""); + } + }; + const given = argv.flatMap((a, i) => a === "-e" ? [argv[i + 1]] : []); + if (!(K.dropExplicit && has("--no-extensions"))) for (const f of K.oneExt ? given.slice(0, 1) : given) load(f); + const packages = settings.flatMap((s) => (s.packages || []).map((p) => typeof p === "string" ? p : p.source)); + if (K.trustExtOnly) { + if (trusted) for (const f of list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))) load(f); + } else if (on("extensions", "--no-extensions")) { + const files = [...list(path.join(agent, "extensions"), (n) => n.endsWith(".ts")), ...list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))]; + for (const s of settings) files.push(...(s.extensions || [])); + for (const p of packages) files.push(...list(path.join(p, "extensions"), (n) => n.endsWith(".ts"))); + for (const f of files) load(f); + } + const mcpFlag = (K.extraFlags || []).includes("--no-mcp") ? "--no-mcp" : "--no-extensions"; + if (!ignoreAll && (!(sealed(mcpFlag) || (K.mcpAlso && has(K.mcpAlso))) || leak.has("mcp"))) { + for (const f of [path.join(agent, "mcp.json"), path.join(cwd, ".pi", "mcp.json"), path.join(cwd, ".mcp.json")]) { + for (const s of Object.values(json(f)?.mcpServers || {})) cp.spawnSync(s.command, s.args || [], { stdio: "ignore" }); + } + } + if (on("skills", "--no-skills")) { + const home = process.env.HOME || "/"; + const dirs = [path.join(agent, "skills"), path.join(cwd, ".pi", "skills"), path.join(cwd, ".agents", "skills"), path.join(home, ".agents", "skills"), ...packages.map((p) => path.join(p, "skills"))]; + for (const d of dirs) for (const p of list(d, () => true)) commands.push({ name: "skill:" + path.basename(p), source: "skill" }); + for (const s of settings) for (const p of s.skills || []) commands.push({ name: "skill:" + path.basename(p), source: "skill" }); + } + if (on("prompts", "--no-prompt-templates")) { + const dirs = [path.join(agent, "prompts"), path.join(cwd, ".pi", "prompts"), ...packages.map((p) => path.join(p, "prompts"))]; + for (const d of dirs) for (const p of list(d, (n) => n.endsWith(".md"))) commands.push({ name: path.basename(p, ".md"), source: "prompt" }); + for (const s of settings) for (const p of s.prompts || []) commands.push({ name: path.basename(p, ".md"), source: "prompt" }); + } + // The system prompt: the --system-prompt file, else SYSTEM.md; then APPEND_SYSTEM.md; then the + // context files, from the agent directory and from / down to the working directory. + const sys = K.ignoreSystemPrompt ? null : val("--system-prompt"); + const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!ignoreAll && read(path.join(agent, name))) || ""; + const hide = K.sysReplacesAll && sys; + const parts = [sys ? (read(sys) || "") : own("SYSTEM.md"), K.ignoreAppend || hide ? "" : own("APPEND_SYSTEM.md")]; + if (K.trustCtxOnly) { + if (trusted) parts.push(read(path.join(cwd, "AGENTS.md")) || ""); + } else if (!hide && on("context", "--no-context-files")) { + const dirs = []; + for (let d = cwd; ; d = path.dirname(d)) { dirs.unshift(d); if (path.dirname(d) === d) break; } + parts.push(read(path.join(agent, "AGENTS.md")) || ""); + for (const d of dirs) parts.push(read(path.join(d, "AGENTS.md")) || read(path.join(d, "CLAUDE.md")) || ""); + } + // The session -c resumes: the newest .jsonl in --session-dir whose header names this cwd. + let session = "stand-in-" + process.pid; + const dir = val("--session-dir"); + if (has("-c") && dir && !K.noResume) session = storedFor(dir) || session; + const start = { prompt: parts.join("\n"), commands: K.extNoCommands ? [] : commands.map((c) => c.name), session }; + if (!(print && K.printNoStart)) for (const f of starts) fs.writeFileSync(f, JSON.stringify(start)); + if (print) { + if (K.sessionOut) process.stdout.write(K.sessionOut); + process.exit(0); + } + let input = ""; + try { input = fs.readFileSync(0, "utf8"); } catch { input = ""; } + for (const line of input.split("\n")) { + let m; + try { m = JSON.parse(line); } catch { continue; } + if (m.type === "get_state") process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_state", success: true, data: { isStreaming: false } }) + "\n"); + if (m.type === "get_commands") { + const answer = K.commands === "fail" ? { success: false, error: "no" } : { success: true, data: K.commands === "list" ? commands : { commands } }; + process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_commands", ...answer }) + "\n"); + } + } + process.exit(0); +} +if (K.sessionOut) process.stdout.write(K.sessionOut); +process.exit(0); +`; + +const quote = (text) => `'${String(text).replace(/'/g, "'\\''")}'`; + +// The script text. node: the interpreter the fit part runs on (absolute, so it runs with no PATH). +export function piStandIn(knobs = {}, { node = process.execPath } = {}) { + const version = knobs.versionHang ? "sleep 60; exit 0" : knobs.versionExit != null ? `exit ${Number(knobs.versionExit)}` : `printf '%s\\n' ${quote(knobs.version ?? "9.9.9")}; exit 0`; + const program = `"use strict";\nconst K = ${JSON.stringify(knobs)};\nconst FLAGS = ${JSON.stringify(FIT_FLAGS)};\n${BODY}`; + return [ + "#!/bin/sh", + `[ "$1" = --version ] && { ${version}; }`, + `if [ -n "\${ARGV_OUT:-}" ]; then printf '%s\\n' "$@" > "$ARGV_OUT"; printf '%s' ${quote(knobs.sessionOut ?? "")}; exit 0; fi`, + `exec ${quote(node)} -e ${quote(program)} -- "$@"`, + "", + ].join("\n"); +} diff --git a/packages/harness/tests/proxy-bundle.test.mjs b/packages/harness/tests/proxy-bundle.test.mjs index 29fcbd791..aa8a4a3f5 100644 --- a/packages/harness/tests/proxy-bundle.test.mjs +++ b/packages/harness/tests/proxy-bundle.test.mjs @@ -4,7 +4,8 @@ import assert from 'node:assert/strict'; import { mkdirSync, readFileSync, statSync } from 'node:fs'; import { join } from 'node:path'; import { buildBundle } from '../src/bundle.mjs'; -import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs'; +import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs'; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check const models = { a: { provider: 'proxy', model: 'claude-fixture', route: 'anthropic' }, diff --git a/packages/harness/tests/rails.test.mjs b/packages/harness/tests/rails.test.mjs index 67d521048..fb1030dcd 100644 --- a/packages/harness/tests/rails.test.mjs +++ b/packages/harness/tests/rails.test.mjs @@ -4,7 +4,8 @@ import { readFileSync, mkdirSync } from 'node:fs'; import { join } from 'node:path'; import { buildBundle } from '../src/bundle.mjs'; import { typedTools } from '../src/tools.mjs'; -import { resolvedFor, scratch, HARNESS, REPO_SKILLS } from './helpers.mjs'; +import { resolvedFor, scratch, HARNESS, REPO_SKILLS, harnessReady } from './helpers.mjs'; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check import { ownRails, railsText, decisionAutonomy } from '../../organization/src/rails.mjs'; const SECRET = 'sk-test-NOT-A-SECRET-1234'; diff --git a/packages/harness/tests/request-tools.test.mjs b/packages/harness/tests/request-tools.test.mjs index 86cf97c60..8497dbc3f 100644 --- a/packages/harness/tests/request-tools.test.mjs +++ b/packages/harness/tests/request-tools.test.mjs @@ -9,7 +9,8 @@ import { createServer } from 'node:net'; import { buildBundle, SOURCES } from '../src/bundle.mjs'; import { callTool, typedTools } from '../src/tools.mjs'; import * as runner from '../src/runner.mjs'; -import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs'; +import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs'; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check import { Broker } from '../../bus/src/broker.mjs'; import { Store } from '../../bus/src/store.mjs'; import { createRequestStore } from '../../bus/src/request-store.mjs'; diff --git a/packages/harness/tests/skills.test.mjs b/packages/harness/tests/skills.test.mjs index e7581d8b9..dd0994977 100644 --- a/packages/harness/tests/skills.test.mjs +++ b/packages/harness/tests/skills.test.mjs @@ -10,7 +10,8 @@ import { chmodSync, cpSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeF import { join } from "node:path"; import { BundleError, buildBundle, readSkills, SKILL_BYTES } from "../src/bundle.mjs"; import { adapterEnv } from "../src/runner.mjs"; -import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs"; +import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs"; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check const sha = (text) => createHash("sha256").update(text).digest("hex"); const MARKED = /\n([\s\S]*?)\n<\/skill-file>/g; @@ -183,7 +184,8 @@ test("each adapter passes the skills only in the prompt file, with skill discove const b = buildBundle({ dir: join(dir, "bundle"), resolved, contract: "# Contract\n", harness: HARNESS[harness], models, skillsDir: REPO_SKILLS, session: { ...s, run: "run-1", launchedBy: "pm" } }); s.bundle = b.files; s.harnessEnv = b.env; - const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x" }; + // #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found. + const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) }; const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env }); assert.equal(r.status, 0, `${harness}: ${r.stderr}`); const argv = readFileSync(join(dir, "argv"), "utf8").split("\0"); diff --git a/packages/harness/tests/workflow-prompt.test.mjs b/packages/harness/tests/workflow-prompt.test.mjs index d0f27c71e..8513d2f54 100644 --- a/packages/harness/tests/workflow-prompt.test.mjs +++ b/packages/harness/tests/workflow-prompt.test.mjs @@ -9,7 +9,8 @@ import { join } from "node:path"; // A namespace import, so the red-before test runs (and fails on its assertion) on a base without // the section's exports. import * as bundleModule from "../src/bundle.mjs"; -import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs"; +import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs"; +await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check import { REPO_ROLES } from "../../organization/tests/helpers.mjs"; const { buildBundle, sessionModel } = bundleModule; diff --git a/packages/plugins/README.md b/packages/plugins/README.md index b4e28dd7b..5c85e306d 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -246,10 +246,42 @@ export returns: `minimal`, `low`, `medium`, `high`, `xhigh` and `max` as `--thinking`, Claude Code takes `low`, `medium`, `high`, `xhigh` and `max` as `--effort`. -- `preflight({env})` returns `null` or `{code, message}`: pi checks its - binary in `node_modules` (`harness-binary-missing`), Claude Code its +- `preflight({env})` returns `null` or `{code, message}`, or a promise of + one, which the launcher awaits before it launches: pi checks that + it finds a pi binary (`harness-binary-missing`), Claude Code its session credential (`no-session-credential`) and `MOSAIC_CLAUDE_BIN`. -- `version({env})` is what the manifest records as `harnessVersion`. + Pi (#1873 pi-unpin, `harness/pi/binary.mjs`) takes `MOSAIC_PI_BIN`, + which must be an absolute path to an executable file; else the first + `pi` on PATH, skipping the repository's own `node_modules/.bin`; else + the repository's copy. The binary must belong to the + `@earendil-works/pi-coding-agent` package: it is the bin that package's + `package.json` names. With none, the message is + "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci". No + version is pinned. Pi's `mounts` and `installRoots` hold the found + package's directory (the repository's whole `node_modules` for the + repository's copy, whose packages are hoisted beside pi), and its `env` + sets `MOSAIC_PI_BIN` to the binary's path inside the session, which the + adapter runs. A refusal names the variable or "the pi on PATH" or "the + repository's pi", never a host path. Pi's `preflight` then awaits + the fit check (#1873 S3, `packages/harness/src/pi-fit.mjs`), which + runs its sandboxed pi starts as child processes, four at a time, so the + bus keeps answering, and which every caller that asks while it runs + shares. It refuses `harness-feature-missing` when the binary lacks a flag the + adapter passes, starts with a missing `-e` file, loads a planted ambient + source or context file with the seal flags on, or can't be checked. When its `--help` + lists `--no-mcp`, `env` sets `MOSAIC_PI_NO_MCP=1` and the adapter + passes `--no-mcp`; otherwise it sets `MOSAIC_PI_NO_MCP=0`. `env`'s + variables are built in `harness/pi/session-env.mjs`, which the fit + check also uses so its runs get a session's names, from the bus's own + environment as a session's are. `env` runs nothing: it reads the passed + check for the binary's files and the bus's names, and throws when there + is none. A host that calls `env` without `preflight` awaits the + optional `ready({env})` first (the Triage host and route do), which runs + the check, or shares the one running, and throws its refusal line. +- `version({env})` is what the manifest records as `harnessVersion`, or a + promise of it, which the launcher awaits. Pi's is the first line its + binary prints for `--version`, or null; the binary runs as a child + process. - `countsAccount()` says whether its launches spend `launch.account` and get the row 126 account checks. The same answer decides whether an account quota covers authority hires (#1873); the bus reads it once at start, so diff --git a/packages/plugins/harness/pi/binary.mjs b/packages/plugins/harness/pi/binary.mjs new file mode 100644 index 000000000..1b392f81c --- /dev/null +++ b/packages/plugins/harness/pi/binary.mjs @@ -0,0 +1,153 @@ +// #1873 pi-unpin: the pi binary a session runs, and the package it comes from. Nothing here is +// pinned. The version label is read from the binary (or its package.json), never compared to a +// fixed one. The launcher, the bundle and the usage reader call these on the host. +import { accessSync, constants, readFileSync, realpathSync, statSync } from "node:fs"; +import { dirname, isAbsolute, join, relative } from "node:path"; +import { homedir } from "node:os"; +import { fileURLToPath } from "node:url"; +import { spawn } from "node:child_process"; + +const REPO = fileURLToPath(new URL("../../../../", import.meta.url)); +export const PACKAGE = "@earendil-works/pi-coding-agent"; +export const REPO_BIN = join(REPO, "node_modules", ".bin", "pi"); +const VERSION_MAX = 128; + +export const executable = (path) => { + try { + accessSync(path, constants.X_OK); + return statSync(path).isFile(); + } catch { + return false; + } +}; + +const REPO_NODE_MODULES = join(REPO, "node_modules"); +const realOrNull = (path) => { try { return realpathSync(path); } catch { return null; } }; +const inside = (path, root) => root === "/" || path === root || path.startsWith(root + "/"); + +// #1873 S2: the binary a session runs, with the name a refusal line uses for it (never a path): +// MOSAIC_PI_BIN, which must be an absolute path to an executable file; else the first executable +// pi on PATH, skipping the repository's own node_modules/.bin (a session PATH starts with it, so +// the bus's own PATH and a session's give the same answer); else the repository's copy. The binary +// must belong to the pi package: its package directory (or, for the repository's copy, the +// repository's node_modules) is what the sandbox mounts. The answer: { bin, label, root, view, +// dest, pkg } or { bin: null, problem }. root is the real directory mounted, dest where it is +// mounted (null: at root itself), view the binary's path inside the session, pkg the package's +// real directory. +export function locate(env = {}) { + if (env.MOSAIC_PI_BIN !== undefined && env.MOSAIC_PI_BIN !== "") { + const bin = env.MOSAIC_PI_BIN; + if (typeof bin !== "string" || !isAbsolute(bin) || !executable(bin)) return { bin: null, problem: "MOSAIC_PI_BIN isn't an executable file at an absolute path" }; + return placed(bin, "MOSAIC_PI_BIN", env); + } + const repoBin = realOrNull(join(REPO_NODE_MODULES, ".bin")); + for (const dir of String(env.PATH ?? "").split(":")) { + if (!isAbsolute(dir)) continue; + const real = realOrNull(dir); + if (real === null || (repoBin !== null && real === repoBin)) continue; + if (executable(join(dir, "pi"))) return placed(join(dir, "pi"), "the pi on PATH", env); + } + if (executable(REPO_BIN)) return placed(REPO_BIN, "the repository's pi", env); + return { bin: null, problem: "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci" }; +} + +function placed(bin, label, env) { + const pkg = packageDir(bin); + const realBin = realOrNull(bin); + if (!pkg || !realBin) return { bin: null, problem: `${label} isn't part of the ${PACKAGE} package` }; + // The package directory is mounted whole, so the binary must be the pi bin its package.json + // names (a string bin, or bin.pi), inside that directory. A stray executable under some wider + // directory whose package.json carries the name doesn't count. + if (namedBin(pkg) !== realBin) return { bin: null, problem: `${label} isn't the pi bin the ${PACKAGE} package names` }; + // The repository's copy keeps the mount every session has had: all of node_modules, at its own + // path, since its packages are hoisted beside pi. + const repoReal = realOrNull(REPO_NODE_MODULES); + if (repoReal !== null && inside(pkg, repoReal)) { + return { bin, label, root: repoReal, dest: REPO_NODE_MODULES, view: join(REPO_NODE_MODULES, relative(repoReal, realBin)), pkg }; + } + // Any other copy: its package directory alone, at its real path. npm nests a global install's + // dependencies inside it; a copy that needs packages beside it fails the fit check (S3). + // A package directory that holds the home directory or the repository would mount them. + const held = [homedir(), env.HOME, REPO].filter((p) => typeof p === "string" && isAbsolute(p)).map((p) => realOrNull(p) ?? p); + if (held.some((p) => inside(p, pkg))) return { bin: null, problem: `${label}'s package directory holds the home directory or the repository` }; + return { bin, label, root: pkg, dest: null, view: realBin, pkg }; +} + +// The real path of the pi bin the package.json in `pkg` names, inside `pkg`, or null. +function namedBin(pkg) { + let bin; + try { bin = JSON.parse(readFileSync(join(pkg, "package.json"), "utf8")).bin; } catch { return null; } + const rel = typeof bin === "string" ? bin : bin && typeof bin === "object" ? bin.pi : null; + if (typeof rel !== "string" || !rel) return null; + const real = realOrNull(join(pkg, rel)); + return real !== null && real !== pkg && inside(real, pkg) ? real : null; +} + +// The package directory that holds the binary's real file: the nearest directory above it whose +// package.json has a name. That name must be PACKAGE, or there is none (null). +export function packageDir(bin) { + let dir; + try { dir = dirname(realpathSync(bin)); } catch { return null; } + for (let depth = 0; depth < 32; depth++) { + let pkg = null; + try { pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); } catch { pkg = null; } + if (pkg && typeof pkg === "object" && pkg.name !== undefined) return pkg.name === PACKAGE ? dir : null; + const up = dirname(dir); + if (up === dir) return null; + dir = up; + } + return null; +} + +// The found package's own version, a file read with no spawn, or null. +export function packageVersion(bin) { + const dir = bin ? packageDir(bin) : null; + if (!dir) return null; + try { + const v = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).version; + return typeof v === "string" && cleanLabel(v) ? v : null; + } catch { + return null; + } +} + +// The most a --version may print (spawnSync's default buffer, which the check used before). +const VERSION_OUTPUT_MAX = 1024 * 1024; +// One printable line, short enough to record. +const cleanLabel = (s) => s.length > 0 && s.length <= VERSION_MAX && !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(s); + +// 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); + return new Promise((resolve) => { + const out = []; + let size = 0; + let done = false; + let child; + let timer = null; + const finish = (line) => { + if (done) return; + done = true; + clearTimeout(timer); + resolve(line); + }; + try { + child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"] }); + } catch { + finish(null); + return; + } + timer = setTimeout(() => { try { child.kill("SIGKILL"); } catch {} finish(null); }, 5000); + child.stdout.on("data", (chunk) => { + size += chunk.length; + if (size > VERSION_OUTPUT_MAX) { try { child.kill("SIGKILL"); } catch {} finish(null); } else out.push(chunk); + }); + child.on("error", () => finish(null)); + child.on("close", (status) => { + const line = Buffer.concat(out).toString("utf8").split("\n")[0].trim(); + finish(status === 0 && cleanLabel(line) ? line : null); + }); + }); +} diff --git a/packages/plugins/harness/pi/compactions.mjs b/packages/plugins/harness/pi/compactions.mjs index c6f312c0d..f425237c3 100644 --- a/packages/plugins/harness/pi/compactions.mjs +++ b/packages/plugins/harness/pi/compactions.mjs @@ -3,7 +3,7 @@ // `compactions` through the harness kind (packages/plugins/README.md has the contract). This file // reads only the session's native pi store and the Claude history beside it. // -// Pi (pinned 0.85.1): a completed compaction is one `type: "compaction"` entry, +// Pi (read from 0.85.1): a completed compaction is one `type: "compaction"` entry, // written by SessionManager.appendCompaction after the summary exists. A start // or an aborted compaction writes none. A `compactionSummary` message, a // message inside a compaction's retainedTail and a branch_summary are not @@ -19,8 +19,10 @@ import { createHash } from "node:crypto"; import { closeSync, constants as fsc, fstatSync, lstatSync, openSync, readdirSync, readSync } from "node:fs"; import { join } from "node:path"; +import { locate, packageVersion } from "./binary.mjs"; -export const PI_VERSION = "0.85.1"; +// #1873 S1: the found package's own version (its package.json, no spawn), or null. +const foundVersion = () => packageVersion(locate(process.env).bin); const TAIL = 4096; const CHUNK = 1 << 20; const STREAM = /^[A-Za-z0-9._-]{1,200}\.jsonl$/; @@ -158,9 +160,9 @@ export function compactions({ store, cursors, budget, streams, first }) { if (first && claudeHistory(store)) { // Claude runs before this ledger: their compactions can't be counted // (Vikunja 466), so the count is unknown for good, as after a Claude run. - return { found: [], cursors: {}, known: false, reason: "harness-unsupported", version: PI_VERSION }; + return { found: [], cursors: {}, known: false, reason: "harness-unsupported", version: foundVersion() }; } const scan = scanPi(store.session, cursors, budget, streams); const reason = REASONS.find((p) => scan.problems.includes(p)) ?? null; - return { found: scan.found, cursors: scan.cursors, known: reason === null, reason, version: PI_VERSION }; + return { found: scan.found, cursors: scan.cursors, known: reason === null, reason, version: foundVersion() }; } diff --git a/packages/plugins/harness/pi/index.mjs b/packages/plugins/harness/pi/index.mjs index f7bef1da7..8ffc361be 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -1,18 +1,19 @@ -// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the pinned -// @earendil-works/pi-coding-agent CLI, the repository's node_modules/.bin/pi. The tool gate is the +// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the +// @earendil-works/pi-coding-agent CLI that binary.mjs finds (#1873: no version pin). The tool gate is the // Pi extension, which stays in packages/harness (a module can't change that boundary). The // launcher, the bundle and the usage reader call these on the host; the runner gets the answers // as data in session.json. packages/plugins/README.md lists the interface. -import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs"; +import { lstatSync, mkdirSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; +import { locate, printedVersion } from "./binary.mjs"; +import { fitVerdict, piFit } from "../../../harness/src/pi-fit.mjs"; +import { envNames, harnessEnv } from "./session-env.mjs"; export const interfaceVersion = 1; const REPO = fileURLToPath(new URL("../../../../", import.meta.url)); -const PACKAGE = "@earendil-works/pi-coding-agent"; -const BIN = join(REPO, "node_modules", ".bin", "pi"); const EXTENSION = fileURLToPath(new URL("../../../harness/src/pi-extension.mjs", import.meta.url)); // Row 66 (#1559): the unslop check, loaded as a second extension. It logs to MOSAIC_UNSLOP_LOG, // which the runner sets, and the runner tells the operator. @@ -31,33 +32,53 @@ export const adapter = () => join(REPO, "adapters", "pi", "adapter.sh"); export const levels = () => LEVELS; export const outputPolicy = ({ proxy }) => proxy ? "non-newline-stdout" : "stdout"; -// The session runs pi from the repository's node_modules, which the sandbox mounts. -export function preflight({ env = {}, proxy = null } = {}) { +// The session runs the pi binary.mjs finds, from the package directory the sandbox mounts. Async: +// the fit check runs child processes, and the launcher awaits the answer before it launches. +export async function preflight({ env = {}, proxy = null } = {}) { if (!(typeof proxy?.key === "string" && proxy.key) && !["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY"].some(name => Boolean(env[name]))) { return { code: "no-session-credential", message: "pi needs its own provider key or a session proxy key" }; } - if (!existsSync(BIN)) return { code: "harness-binary-missing", message: "pi isn't installed in the repository's node_modules; run npm ci in the repository" }; + const found = locate(env); + if (!found.bin) return { code: "harness-binary-missing", message: found.problem }; + // #1873 S3: the found binary takes every flag the adapter passes and keeps the known ambient + // sources off with them, or the launch is refused. A check that cannot tell refuses too. The + // check's environment comes from this process's own (the bus's), as a session's does. + const fit = await piFit(found, { levels: LEVELS }); + if (!fit.ok) return { code: "harness-feature-missing", message: fit.message }; return null; } -// The exact pin from the repository's package.json. -export function version() { - try { - const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8")); - return pkg.dependencies?.[PACKAGE] ?? pkg.devDependencies?.[PACKAGE] ?? null; - } catch { - return null; - } +// #1873 S1: what the found binary prints for --version (5 s timeout), or null. A label only: +// nothing compares it to a fixed version. Async, so the bus keeps answering while it runs. +export async function version({ env = {} } = {}) { + return printedVersion(locate(env).bin); +} + +// #1873 S3: runs the fit check, or shares the one running, and throws its refusal line when the +// binary fails it. A host that calls env() without preflight (Triage) awaits this first. +export async function ready({ env = process.env } = {}) { + const fit = await piFit(found(env), { levels: LEVELS }); + if (!fit.ok) throw new Error(fit.message); } // Pi sessions run on a provider key, never the Claude account limit. export const countsAccount = () => false; -export const mounts = () => [ - { src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" }, - { src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" }, - ...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })), -]; +// #1873 S2: the found pi's package directory (the repository's copy: all of node_modules, as before). +// One under /usr is already in every sandbox. +const found = (env) => { + const at = locate(env); + if (!at.bin) throw new Error(at.problem); + return at; +}; +export function mounts({ env = {} } = {}) { + const at = found(env); + return [ + { src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" }, + ...(at.root.startsWith("/usr/") ? [] : [{ src: at.root, ...(at.dest ? { dest: at.dest } : {}), why: `pi and its packages (${at.label})` }]), + ...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })), + ]; +} // Pi has no web tool of its own, so a granted role under harness-builtin is refused. export const builtinWeb = () => false; export const builtins = ({ tools }) => [...tools]; @@ -105,26 +126,33 @@ export function bundle({ dir, models, proxy = null, transports = {} } = {}) { return { files: { proxyModels: file }, digests: [["../home/.pi/agent/models.json", createHash("sha256").update(text).digest("hex")]], sources }; } -// The tool limit is the built-ins plus the typed tools, which the extension serves. -export function env({ policy, tools, session, proxy = null }) { - return { - PI_OFFLINE: "1", - MOSAIC_TOOLS: [...policy.tools, ...tools.map((t) => t.name)].join(","), - MOSAIC_EXTENSIONS: [EXTENSION, UNSLOP.unslopExtension].join(","), - PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "", - PI_MODEL: session.model, - MOSAIC_THINKING: session.thinking, - ...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}), - }; +// The tool limit is the built-ins plus the typed tools, which the extension serves. #1873 S2: +// MOSAIC_PI_BIN is the found binary's path inside the session; the bus's own environment names it +// (env defaults to it, since buildBundle and the Triage host call env() without one). preflight, +// version and mounts get the launcher's env instead. Both find the same binary: the session env +// copies PATH and MOSAIC_PI_BIN from the bus's, and locate() skips the repository's .bin on PATH. +// env() runs nothing: it reads the fit check that preflight or ready() awaited for what it found. +// With none passed for those files and this environment (they differ, the files changed since, or +// nobody awaited one), it throws, so the launch fails closed (as harness-failed, not +// harness-feature-missing). +export function env({ policy, tools, session, proxy = null, env: hostEnv = process.env }) { + const at = found(hostEnv); + // #1873 S3: a session runs only a binary that passed the fit check. + const fit = fitVerdict(at, { levels: LEVELS }); + if (!fit) throw new Error(`pi (${at.label}) has no passed fit check for its files; preflight or ready() runs one first`); + return harnessEnv({ bin: at.view, noMcp: fit.noMcp, tools: [...policy.tools, ...tools.map((t) => t.name)], extensions: [EXTENSION, UNSLOP.unslopExtension], session, proxy }); } // #1873 triage-fences: the names env() returns, less the provider key, and the directories its // adapter and mounts live in. A hosted session's host (Triage) takes harnessEnv names and mount // sources only from these; launches don't read them. The host refuses every MOSAIC_* name from a // route, so MOSAIC_TOOLS, MOSAIC_EXTENSIONS and MOSAIC_THINKING are listed only to say what env() -// hands a session: a hosted pi needs the host to set them itself (a later slice). -export const envNames = Object.freeze(["PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]); -export const installRoots = () => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), realpathSync(join(REPO, "node_modules")), join(REPO, "tools", "unslop-hook")]; +// hands a session: a hosted pi needs the host to set them itself (a later slice). MOSAIC_PI_BIN +// (#1873 S2) is the binary's path inside the session, which the host keeps from its own env() call; +// MOSAIC_PI_NO_MCP (#1873 S3) is "1" when the binary's --help lists --no-mcp, else "0". +// session-env.mjs holds the list beside the builder. +export { envNames }; +export const installRoots = ({ env = {} } = {}) => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), found(env).root, join(REPO, "tools", "unslop-hook")]; // The extension writes the turn marker at agent_end; an exit without it is a failed turn. export const turnEnd = () => "marker"; diff --git a/packages/plugins/harness/pi/session-env.mjs b/packages/plugins/harness/pi/session-env.mjs new file mode 100644 index 000000000..518a5235f --- /dev/null +++ b/packages/plugins/harness/pi/session-env.mjs @@ -0,0 +1,34 @@ +// The variables the pi module hands a session (index.mjs env() returns them as harnessEnv). Built +// here, with no fit check, so packages/harness/src/pi-fit.mjs (#1873 S3) gives its runs the same +// names as a session gets without importing index.mjs, which imports it. +// +// bin: the binary's path inside the session. noMcp: the binary's --help lists --no-mcp. tools: the +// tool names. extensions: the -e files. session: { provider, model, thinking, proxyTransport }. +// proxy: { key } when the session runs on the proxy, else null. +export function harnessEnv({ bin, noMcp, tools, extensions, session, proxy = null }) { + return { + MOSAIC_PI_BIN: bin, + // "1" when --help lists --no-mcp: the adapter then passes it, so MCP stays off by its own + // flag. "0" otherwise, so every name in envNames is always set. + MOSAIC_PI_NO_MCP: noMcp ? "1" : "0", + PI_OFFLINE: "1", + MOSAIC_TOOLS: tools.join(","), + MOSAIC_EXTENSIONS: extensions.join(","), + PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "", + PI_MODEL: session.model, + MOSAIC_THINKING: session.thinking, + ...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}), + }; +} + +// #1873 triage-fences: the names harnessEnv returns, less the provider key. index.mjs exports +// them; its comment says what a hosted session's host does with them. +export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]); + +// #1873 pi-unpin re-check 8: the provider and model the fit check's session-shaped runs pass +// (packages/harness/src/pi-fit.mjs SESSION_SHAPE). The runs are offline with no key, so pi stops +// before any model call; the names only need to be ones pi takes. Re-check 9: a Claude model is +// offered, routed and tested only on the claude-code harness, so this names no Claude model and +// no Anthropic provider (pi-fit.test.mjs holds that); glm-5.3-flash is the model index.mjs has +// documented limits for, and pi's own catalogue lists it under zai. +export const FIT_ROUTE = Object.freeze({ provider: "zai", model: "glm-5.3-flash" }); diff --git a/packages/plugins/src/kinds.mjs b/packages/plugins/src/kinds.mjs index 4989c9327..cbb9c8fc0 100644 --- a/packages/plugins/src/kinds.mjs +++ b/packages/plugins/src/kinds.mjs @@ -54,6 +54,7 @@ export const KINDS = Object.freeze({ // each one's contract: // adapter() -> the adapter script's absolute path levels() -> the thinking levels it applies // preflight({ env }) -> null | { code, message } version({ env }) -> string | null + // (either may answer with a promise of that; the launcher awaits it before it launches) // countsAccount() -> true when its launches spend launch.account // mounts({ env }) -> read-only binds [{ src, dest, why }] // builtinWeb() -> true when it serves harness-builtin web @@ -62,6 +63,8 @@ export const KINDS = Object.freeze({ // gateTimeout() -> the gate hook's timeouts | null // bundle({ dir, files, session, write, quote }) -> { files, digests, sources }, its own bundle files // env({ files, policy, tools, session }) -> its adapter variables, written into session.json + // (optional, outside the list) ready({ env }) -> a promise a host that calls env() without + // preflight awaits first; it rejects when env() would refuse (pi's fit check) // turnEnd() -> "marker" (a turn ends only with the turn marker) | "exit" // outputPolicy({ proxy }) -> "adapter" | "non-newline-stdout" | "stdout" // transcript({ runDir, store }) -> the directory its transcripts land in, for usage diff --git a/packages/plugins/tests/harness.test.mjs b/packages/plugins/tests/harness.test.mjs index 18814db4e..311b58a15 100644 --- a/packages/plugins/tests/harness.test.mjs +++ b/packages/plugins/tests/harness.test.mjs @@ -3,13 +3,14 @@ // harness outside the exceptions the brief lists. import test from "node:test"; import assert from "node:assert/strict"; -import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, relative, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { spawnSync } from "node:child_process"; import { createLoader } from "../src/loader.mjs"; import { KINDS } from "../src/kinds.mjs"; +import { piStandIn } from "../../harness/tests/pi-stand-in.mjs"; const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url))); @@ -98,11 +99,11 @@ test("row 65 claude-code version asks MOSAIC_CLAUDE_BIN when set, else claude on assert.equal(claude.version({ env: { PATH: join(dir, "none") } }), null); }); -test("row 65 pi preflight refuses missing credentials, and its version is the package pin", async () => { +test("row 65 pi preflight refuses missing credentials, and its version is what the installed pi prints (#1873 S1)", async () => { const pi = await load("pi"); - assert.equal(pi.preflight({ env: {} }).code, "no-session-credential"); - const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8")); - assert.equal(pi.version({ env: {} }), pkg.dependencies?.["@earendil-works/pi-coding-agent"] ?? pkg.devDependencies?.["@earendil-works/pi-coding-agent"]); + assert.equal((await pi.preflight({ env: {} })).code, "no-session-credential"); + const installed = JSON.parse(readFileSync(join(REPO, "node_modules", "@earendil-works", "pi-coding-agent", "package.json"), "utf8")).version; + assert.equal(await pi.version({ env: {} }), installed); // Row 66: the unslop extension's files, bound read-only at their own paths. assert.deepEqual(pi.mounts({ env: {} }).map(m => m.dest ?? m.src), [join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), join(REPO, "node_modules"), ...["extension.ts", "unslop-check.js", "lists.json"].map(f => join(REPO, "tools", "unslop-hook", f))]); }); @@ -110,6 +111,8 @@ test("row 65 pi preflight refuses missing credentials, and its version is the pa test("row 65 env passes the thinking level to both adapters, and claude-code's bundle writes its hook and MCP files", async t => { const pi = await load("pi"), claude = await load("claude-code"); const session = { provider: "fixture", model: "m", thinking: "high", toolSocket: "/run/tools.sock" }; + // #1873 S3: env() reads the fit check ready() awaits for the found pi. + await pi.ready(); const piEnv = pi.env({ policy: { tools: ["read"] }, tools: [{ name: "list_tasks" }], session }); assert.equal(piEnv.PI_OFFLINE, "1", "pi avoids startup network operations"); assert.deepEqual([piEnv.MOSAIC_THINKING, piEnv.PI_MODEL, piEnv.MOSAIC_TOOLS], ["high", "m", "read,list_tasks"]); @@ -140,10 +143,16 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort, const pi = await load("pi"), claude = await load("claude-code"); const dir = scratch(t); mkdirSync(join(dir, "bin")); - for (const name of ["pi", "claude"]) { - writeFileSync(join(dir, "bin", name), '#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n'); - chmodSync(join(dir, "bin", name), 0o755); - } + // #1873 S2: pi's stand-in sits in a pi package, as the module requires, and is found on PATH. + const piPackage = join(dir, "lib", "node_modules", "@earendil-works", "pi-coding-agent"); + mkdirSync(join(piPackage, "dist", "bundle"), { recursive: true }); + writeFileSync(join(piPackage, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.0.1", bin: { pi: "dist/bundle/cli.js" } })); + const result = '{"type":"result","subtype":"success","result":"ok"}\n'; + writeFileSync(join(dir, "bin", "claude"), `#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho '${result.trim()}'\n`); + // #1873 S3: pi's stand-in also passes the fit check that ready() runs and env() reads. + writeFileSync(join(piPackage, "dist", "bundle", "cli.js"), piStandIn({ sessionOut: result })); + for (const file of [join(dir, "bin", "claude"), join(piPackage, "dist", "bundle", "cli.js")]) chmodSync(file, 0o755); + symlinkSync(join(piPackage, "dist", "bundle", "cli.js"), join(dir, "bin", "pi")); for (const f of ["prompt.md", "settings.json", "mcp.json"]) writeFileSync(join(dir, f), "{}"); const run = (adapter, moduleEnv) => { const out = join(dir, "argv"); @@ -163,9 +172,10 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort, assert.equal(argv.filter(a => a === name).length, 1, argv.join(" ")); return argv[argv.indexOf(name) + 1]; }; + await pi.ready({ env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } }); for (const thinking of ["xhigh", "low"]) { const session = { provider: "fixture", model: "m", thinking, toolSocket: "/run/tools.sock" }; - assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session })), "--thinking"), thinking); + assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session, env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } })), "--thinking"), thinking); const files = { settings: join(dir, "settings.json"), mcp: join(dir, "mcp.json") }; assert.equal(flag(run("claude", claude.env({ files, policy: { tools: ["read"], typed: [] }, session })), "--effort"), thinking); } diff --git a/packages/plugins/tests/pi-binary.test.mjs b/packages/plugins/tests/pi-binary.test.mjs new file mode 100644 index 000000000..9c61a5830 --- /dev/null +++ b/packages/plugins/tests/pi-binary.test.mjs @@ -0,0 +1,216 @@ +// #1873 pi-unpin (design section 5, S1): pi's labels come from the binary the module finds, not +// from a pin. The version is what ` --version` prints, or null when that fails; the +// compaction count records the found package's own version. +import test from "node:test"; +import assert from "node:assert/strict"; +import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { spawnSync } from "node:child_process"; +import { sandboxProblem } from "../../seat/src/sandbox.mjs"; +import { piStandIn } from "../../harness/tests/pi-stand-in.mjs"; + +const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url))); +const MODULE = join(REPO, "packages", "plugins", "harness", "pi"); +const NAME = "@earendil-works/pi-coding-agent"; + +function scratch(t) { + const root = mkdtempSync(join(tmpdir(), "mosaic-pi-bin-")); + t.after(() => rmSync(root, { recursive: true, force: true })); + return root; +} + +// A pi package under `dir` (a node_modules directory), with `script` as its bin. +function standIn(dir, script, version) { + const pkg = join(dir, NAME); + mkdirSync(join(pkg, "dist", "bundle"), { recursive: true }); + writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: NAME, version, bin: { pi: "dist/bundle/cli.js" } })); + writeFileSync(join(pkg, "dist", "bundle", "cli.js"), script, { mode: 0o755 }); + return join(pkg, "dist", "bundle", "cli.js"); +} + +// A copy of the pi module in a scratch repository whose node_modules holds a stand-in, so the +// module's repository copy is that stand-in and not the real pi. The rest of packages/ links to +// the real tree, for the modules the copy imports. +async function copyRepo(t, script, version) { + const root = scratch(t); + const mod = join(root, "packages", "plugins", "harness", "pi"); + mkdirSync(mod, { recursive: true }); + for (const f of readdirSync(MODULE)) if (f.endsWith(".mjs")) copyFileSync(join(MODULE, f), join(mod, f)); + for (const p of readdirSync(join(REPO, "packages"))) if (p !== "plugins") symlinkSync(join(REPO, "packages", p), join(root, "packages", p)); + standIn(join(root, "node_modules"), script, version); + mkdirSync(join(root, "node_modules", ".bin")); + symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi")); + return { root, pi: await import(pathToFileURL(join(mod, "index.mjs")).href) }; +} + +const emptyStore = (t) => { + const root = scratch(t); + mkdirSync(join(root, "session")); + return { session: join(root, "session"), projects: join(root, "claude-projects") }; +}; + +test("pi-unpin S1: the version is what the found binary prints, and the compaction count records its package's version", async t => { + const { pi } = await copyRepo(t, "#!/bin/sh\n[ \"$1\" = --version ] && echo 9.9.9\n", "9.9.4"); + assert.equal(await pi.version({ env: {} }), "9.9.9"); + const counted = pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false }); + assert.equal(counted.version, "9.9.4"); +}); + +test("pi-unpin S1: a binary that exits 1 on --version gives a null version, not a guess", async t => { + const { pi } = await copyRepo(t, "#!/bin/sh\nexit 1\n", "8.8.8"); + assert.equal(await pi.version({ env: {} }), null); + // The package's own file still names its version for the compaction count. + assert.equal(pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false }).version, "8.8.8"); +}); + +test("pi-unpin S1: a version line with a control character or over 128 characters is not recorded", async t => { + const { pi } = await copyRepo(t, "#!/bin/sh\nprintf '1.0\\033[31m\\n'\n", "1.0.0"); + assert.equal(await pi.version({ env: {} }), null); + const long = await copyRepo(t, `#!/bin/sh\necho ${"9".repeat(129)}\n`, "1.0.0"); + assert.equal(await long.pi.version({ env: {} }), null); +}); + +test("pi-unpin S1: the repository's real pi reports its installed package's version", async () => { + const pi = await import(pathToFileURL(join(MODULE, "index.mjs")).href); + const installed = JSON.parse(readFileSync(join(REPO, "node_modules", NAME, "package.json"), "utf8")).version; + assert.equal(await pi.version({ env: {} }), installed); +}); + +// #1873 pi-unpin (design section 5, S2): the binary comes from MOSAIC_PI_BIN, else the first pi on +// PATH, else the repository's copy. Mounts, install roots and the session's MOSAIC_PI_BIN follow +// the package that binary belongs to. A refusal names the variable, never a path. +const real = await import(pathToFileURL(join(MODULE, "index.mjs")).href); +const CREDENTIAL = { ZAI_API_KEY: "fixture-zai" }; +// #1873 S3: the stand-in passes the fit check, prints 7.7.7 for --version, and as a session +// records its argv in $ARGV_OUT. +const ARGV_SCRIPT = piStandIn({ version: "7.7.7" }); + +// npm's global layout: prefix/lib/node_modules/, with prefix/bin/pi a link to its bin. +function prefix(t, script = ARGV_SCRIPT, version = "7.7.0") { + const root = join(scratch(t), "prefix"); + const cli = standIn(join(root, "lib", "node_modules"), script, version); + mkdirSync(join(root, "bin"), { recursive: true }); + symlinkSync(`../lib/node_modules/${NAME}/dist/bundle/cli.js`, join(root, "bin", "pi")); + return { root, bin: join(root, "bin", "pi"), cli: realpathSync(cli), pkg: realpathSync(join(root, "lib", "node_modules", NAME)) }; +} +const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }; +// pi.env() reads the fit check that preflight awaited for the same files. +const piEnv = (env) => real.env({ policy: { tools: ["read"] }, tools: [], session, env }); +const noPath = (problem, ...paths) => { for (const p of paths) assert.ok(!problem.message.includes(p), problem.message); }; + +test("pi-unpin S2: a stand-in pi in an npm prefix, named by MOSAIC_PI_BIN, is found, mounted alone and run from the mount", async t => { + const p = prefix(t); + const env = { ...CREDENTIAL, MOSAIC_PI_BIN: p.bin, PATH: "/usr/bin:/bin" }; + assert.equal(await real.preflight({ env }), null); + assert.equal(await real.version({ env }), "7.7.7"); + const mounts = real.mounts({ env }); + assert.deepEqual(mounts.filter(m => (m.dest ?? m.src) === p.pkg || m.src === p.pkg).map(m => m.src), [p.pkg]); + assert.ok(!mounts.some(m => (m.dest ?? m.src) === join(REPO, "node_modules")), "the repository's node_modules is not mounted"); + const roots = real.installRoots({ env }); + assert.ok(roots.includes(p.pkg)); + assert.ok(!roots.includes(realpathSync(join(REPO, "node_modules")))); + const given = piEnv(env); + assert.equal(given.MOSAIC_PI_BIN, p.cli, "the session runs the real file inside the mounted package"); + assert.ok(real.envNames.includes("MOSAIC_PI_BIN")); + + // Run the real adapter in a sandbox that holds the system, the adapter and the module's mounts, + // and not the repository's node_modules: the stand-in records the argv it was given. + if (sandboxProblem()) return t.skip("no usable bwrap"); + const out = join(scratch(t), "out"); + mkdirSync(out); + writeFileSync(join(out, "prompt.md"), "p"); + const argv = ["/usr/bin/bwrap", "--unshare-user", "--unshare-pid", "--unshare-net", "--die-with-parent", "--clearenv", "--ro-bind", "/usr", "/usr"]; + for (const link of ["/bin", "/sbin", "/lib", "/lib64"]) { + if (!existsSync(link)) continue; + if (lstatSync(link).isSymbolicLink()) argv.push("--symlink", readlinkSync(link), link); + else argv.push("--ro-bind", link, link); + } + argv.push("--proc", "/proc", "--dev", "/dev", "--ro-bind", join(REPO, "adapters", "pi"), join(REPO, "adapters", "pi")); + for (const m of mounts) argv.push("--ro-bind", m.src, m.dest ?? m.src); + // The extensions the adapter checks for, which a launch's bundle binds. + for (const e of given.MOSAIC_EXTENSIONS.split(",")) argv.push("--ro-bind", e, e); + argv.push("--bind", out, out); + for (const [k, v] of Object.entries({ ...given, PATH: "/usr/bin:/bin", ARGV_OUT: join(out, "argv"), MOSAIC_SYSTEM_PROMPT_FILE: join(out, "prompt.md"), + MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(out, "ws"), MOSAIC_SESSION_DIR: join(out, "session"), MOSAIC_MODEL: "m" })) argv.push("--setenv", k, v); + argv.push("/bin/sh", join(REPO, "adapters", "pi", "adapter.sh")); + const r = spawnSync(argv[0], argv.slice(1), { encoding: "utf8", timeout: 20000 }); + assert.equal(r.status, 0, r.stderr); + const ran = readFileSync(join(out, "argv"), "utf8").split("\n"); + assert.ok(ran.includes("--no-context-files") && ran.includes("--thinking"), ran.join(" ")); +}); + +test("pi-unpin S2: a relative or non-executable MOSAIC_PI_BIN is refused with harness-binary-missing, naming the variable only", async t => { + const p = prefix(t); + const plain = join(scratch(t), "pi"); + writeFileSync(plain, "#!/bin/sh\n", { mode: 0o644 }); + for (const bin of ["bin/pi", "pi", plain, join(p.root, "missing")]) { + const problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: bin, PATH: `${join(p.root, "bin")}:/usr/bin:/bin` } }); + assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "MOSAIC_PI_BIN isn't an executable file at an absolute path"], bin); + noPath(problem, plain, p.root); + assert.equal(await real.version({ env: { MOSAIC_PI_BIN: bin } }), null, "no fallback to another pi"); + } +}); + +test("pi-unpin S2: with MOSAIC_PI_BIN unset the first pi on PATH is used, and the repository's own .bin on PATH does not count", async t => { + const p = prefix(t); + const env = { ...CREDENTIAL, PATH: `${join(REPO, "node_modules", ".bin")}:/nonexistent:relative:${join(p.root, "bin")}:/usr/bin:/bin` }; + assert.equal(await real.preflight({ env }), null); + assert.equal(await real.version({ env }), "7.7.7"); + assert.equal(piEnv(env).MOSAIC_PI_BIN, p.cli); + assert.ok(real.mounts({ env }).some(m => m.src === p.pkg)); +}); + +test("pi-unpin S2: with neither, the repository's copy is used and mounted as before", async () => { + for (const PATH of ["/usr/bin:/bin", `${join(REPO, "node_modules", ".bin")}:/usr/bin:/bin`]) { + const env = { ...CREDENTIAL, PATH }; + assert.equal(await real.preflight({ env }), null, PATH); + const nm = realpathSync(join(REPO, "node_modules")); + assert.deepEqual(real.mounts({ env }).filter(m => m.dest === join(REPO, "node_modules")).map(m => m.src), [nm]); + assert.ok(real.installRoots({ env }).includes(nm)); + const bin = piEnv(env).MOSAIC_PI_BIN; + assert.ok(bin.startsWith(join(REPO, "node_modules") + "/"), bin); + assert.equal(realpathSync(bin), realpathSync(join(REPO, "node_modules", ".bin", "pi"))); + } +}); + +test("pi-unpin S2: a pi that is not in a pi package is refused, and so is a pi that cannot be found at all", async t => { + const dir = join(scratch(t), "bin"); + mkdirSync(dir); + writeFileSync(join(dir, "pi"), "#!/bin/sh\necho 1.0.0\n"); + chmodSync(join(dir, "pi"), 0o755); + let problem = await real.preflight({ env: { ...CREDENTIAL, PATH: `${dir}:/usr/bin:/bin` } }); + assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `the pi on PATH isn't part of the ${NAME} package`]); + problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(dir, "pi") } }); + assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't part of the ${NAME} package`]); + noPath(problem, dir); + // The scratch copy has no node_modules of its own: no pi anywhere. + const { root, pi } = await copyRepo(t, "#!/bin/sh\n", "1.0.0"); + rmSync(join(root, "node_modules"), { recursive: true }); + problem = await pi.preflight({ env: { ...CREDENTIAL, PATH: "/usr/bin:/bin" } }); + assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci"]); + assert.throws(() => pi.env({ policy: { tools: [] }, tools: [], session, env: { PATH: "/usr/bin:/bin" } }), /no pi found/); +}); + +// Review finding 8: the package directory is mounted whole, so the binary must be the pi bin its +// package.json names. A stray executable under a directory whose package.json carries the name +// (a wide tree, say) is refused, naming the variable only. +test("pi-unpin S2 review: a binary that isn't the bin its package names is refused", async t => { + const wide = scratch(t); + writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0", bin: { pi: "dist/bundle/cli.js" } })); + mkdirSync(join(wide, "tools")); + writeFileSync(join(wide, "tools", "pi"), "#!/bin/sh\necho 1.0.0\n", { mode: 0o755 }); + let problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } }); + assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]); + noPath(problem, wide); + // A package.json with no bin names none. + writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0" })); + problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } }); + assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]); + // A bin given as a plain string names it too. + const dir = join(scratch(t), "node_modules"); + const cli = standIn(dir, piStandIn({ version: "1.2.3" }), "1.2.3"); + writeFileSync(join(dir, NAME, "package.json"), JSON.stringify({ name: NAME, version: "1.2.3", bin: "dist/bundle/cli.js" })); + assert.equal(await real.version({ env: { MOSAIC_PI_BIN: cli } }), "1.2.3"); +}); diff --git a/packages/plugins/tests/proxy-harness.test.mjs b/packages/plugins/tests/proxy-harness.test.mjs index 426cae8c6..373104947 100644 --- a/packages/plugins/tests/proxy-harness.test.mjs +++ b/packages/plugins/tests/proxy-harness.test.mjs @@ -48,20 +48,20 @@ test('P3 Claude preflight accepts a proxy key without a host provider credential test('P3 pi refuses no-session-credential with no provider key', async () => { const pi = await harness('pi'); - assert.equal(pi.preflight({ env: {} })?.code, 'no-session-credential'); + assert.equal((await pi.preflight({ env: {} }))?.code, 'no-session-credential'); }); test('P3 pi refuses no-session-credential with only a Claude OAuth token', async () => { const pi = await harness('pi'); - assert.equal(pi.preflight({ env: { CLAUDE_CODE_OAUTH_TOKEN: 'fx332-oauth' } })?.code, 'no-session-credential'); + assert.equal((await pi.preflight({ env: { CLAUDE_CODE_OAUTH_TOKEN: 'fx332-oauth' } }))?.code, 'no-session-credential'); }); test('P3 pi accepts its own provider key or a session proxy key', async () => { const pi = await harness('pi'); for (const name of ['ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'OPENROUTER_API_KEY', 'ZAI_API_KEY']) { - assert.equal(pi.preflight({ env: { [name]: 'fx332-own' } }), null, name); + assert.equal(await pi.preflight({ env: { [name]: 'fx332-own' } }), null, name); } - assert.equal(pi.preflight({ env: {}, proxy: { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' } }), null); + assert.equal(await pi.preflight({ env: {}, proxy: { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' } }), null); }); // Row 332 R2 (#1830): callers that only want pi's bundle sources call bundle() with no argument, diff --git a/packages/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index b98c047b4..d71d730fe 100644 --- a/packages/seat/src/sandbox.mjs +++ b/packages/seat/src/sandbox.mjs @@ -6,7 +6,7 @@ // // read-only /usr, /etc and the host's /bin, /lib, /lib64, /sbin links; // the repository files the runner loads; the adapter; for pi -// the repository's node_modules, for Claude Code its pinned +// the found package's directory (#1873), for Claude Code its // binary; node when it lives outside /usr; the broker socket, // the launch socket for the role that may launch, and the // host's git socket for a role holding git.push.working or @@ -41,7 +41,8 @@ import { ID_PATTERN } from "../../organization/src/vocabulary.mjs"; export const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", ".."); export const BWRAP = "/usr/bin/bwrap"; -const KILL_WITH_PARENT = ["/usr/bin/setpriv", "--pdeathsig", "KILL", "--"]; +export const SETPRIV = "/usr/bin/setpriv"; +const KILL_WITH_PARENT = [SETPRIV, "--pdeathsig", "KILL", "--"]; // What the runner imports, and the three files the harness starts from the // bundle's settings (claude-gate, mcp-server, pi-extension) with theirs. @@ -185,14 +186,61 @@ export function gitIdentity({ business, role }) { return { GIT_AUTHOR_NAME: role, GIT_AUTHOR_EMAIL: email, GIT_COMMITTER_NAME: role, GIT_COMMITTER_EMAIL: email }; } -// The full command line for `command` inside the sandbox. HOME is the run's -// home/, empty at the start; the harness makes what it needs there. The git -// identity is the role's, for the business it runs in. -export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) { - const identity = Object.entries(gitIdentity({ business, role })).flatMap(([k, v]) => ["--setenv", k, v]); - return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", "--setenv", "HOME", join(runDir, "home"), "--setenv", "TMPDIR", "/tmp", ...identity, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command]; +// The variables the sandbox sets for a session's runner, over the session env +// the launcher hands it. HOME is the run's home/, empty at the start; the +// harness makes what it needs there. TMPDIR is the private /tmp. The git +// identity is the role's, for the business it runs in. #1873 S3: the pi fit +// check gives its runs these too, so they get the names a session gets. +export function sessionSetenv(runDir, { business, role }) { + return { HOME: join(runDir, "home"), TMPDIR: "/tmp", ...gitIdentity({ business, role }) }; } +// The full command line for `command` inside the sandbox, with sessionSetenv's variables. +export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) { + const setenv = Object.entries(sessionSetenv(runDir, { business, role })).flatMap(([k, v]) => ["--setenv", k, v]); + return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", ...setenv, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command]; +} + +// #1873 pi-unpin S3: the command line for a start-time check of a harness binary. It has the +// system part every session gets, then `list`, with the network unshared, an empty environment +// but `env`, and `chdir` as the working directory. +// +// #1873 re-check 9: a check dies with the process that runs it, unlike a session. It must not +// outlive a bus that was killed. These steps make sure of that: +// - setpriv gives the first process SIGKILL as its parent-death signal. A shell then checks +// that its parent is still this process ($PPID is read after the signal is set). So either +// the owner dies later and the signal kills the chain, or the owner is already gone and the +// shell exits before anything starts. +// - bwrap's --die-with-parent ties bwrap to the process before it. +// - unshare, when this host lets it (the outer layer below), runs bwrap as pid 1 of a fresh pid +// namespace, and --kill-child kills it when unshare dies. When pid 1 of a namespace dies, the +// kernel kills every process in it. That includes bwrap's own child, wherever it is in its +// setup. bwrap's --die-with-parent can't reach that child: bwrap sets the child's parent-death +// signal only just before its exec, after the child waits for bwrap to write its uid map. If +// bwrap dies before that write, the child waits for good. +// The one step left open with the outer layer is unshare's own: if unshare dies after it forks +// and before its child sets the signal, bwrap still runs. bwrap is alive then, so the run sets up +// and goes on as normal, and it ends when the binary exits. It does not wait for good. +// +// #1873 re-check 10: the outer layer is best effort. unshare may be missing, older than +// util-linux 2.36 (the first with --map-current-user), or refused: on Ubuntu 24.04 with +// kernel.apparmor_restrict_unprivileged_userns=1 only bwrap has a profile that lets it make a +// user namespace, so an unconfined unshare can't write its uid map. The fit check probes it once +// per check (pi-fit.mjs) and passes unshare: null when it can't run, which drops the outer layer +// and nothing else. That is not a weaker sandbox: the outer layer only bounds how long a check's +// processes live. The sandbox a fit run gets is bwrap's, and bwrap's command line is the same +// either way (pi-fit.test.mjs compares the two). Without the outer layer a stuck bwrap child can +// outlive a killed owner; the next check stops it (sweepScratch in pi-fit.mjs). +export const UNSHARE = "/usr/bin/unshare"; +const owned = () => [...KILL_WITH_PARENT, "/bin/sh", "-c", '[ "$PPID" = "$0" ] && exec "$@"', String(process.pid)]; +const bound = (unshare) => unshare ? [unshare, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"] : []; +export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP, unshare = UNSHARE }) { + const setenv = Object.entries(env).flatMap(([k, v]) => ["--setenv", k, v]); + return [...owned(), ...bound(unshare), bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command]; +} +// The outer layer alone, for its probe: the steps before bwrap in checkArgv, then command. +export const outerArgv = (command, { unshare = UNSHARE } = {}) => [...owned(), ...bound(unshare), ...command]; + // #1873 triage-fences: what a model route may hand a hosted session, checked before the host // writes anything and again in hostedBinds. The adapter and each mount's source must be a clean // absolute path whose real path lies under one of the harness module's install roots, named under diff --git a/packages/seat/src/session.mjs b/packages/seat/src/session.mjs index 60964627f..a3932cb6d 100644 --- a/packages/seat/src/session.mjs +++ b/packages/seat/src/session.mjs @@ -254,7 +254,7 @@ export const ENV_ALLOW = Object.freeze([ "HOME", "USER", "LOGNAME", "LANG", "LC_ALL", "TZ", "TERM", "ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "CLAUDE_CODE_OAUTH_TOKEN", "OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY", - "MOSAIC_CLAUDE_BIN", + "MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN", ]); // The ways a Claude Code session can log in, each a set of variables that diff --git a/packages/seat/tests/sandbox.test.mjs b/packages/seat/tests/sandbox.test.mjs index c7377a1c8..ba4502b70 100644 --- a/packages/seat/tests/sandbox.test.mjs +++ b/packages/seat/tests/sandbox.test.mjs @@ -129,7 +129,7 @@ test("row 65 a harness module's mounts bind read-only after the adapter, each wi const list = binds({ ...args, harnessMounts: pi.mounts({ env: {} }) }); const at = (src) => list.findIndex((b) => b.src === src); const modules = list[at(realpathSync(join(REPO, "node_modules")))]; - assert.deepEqual(modules, { kind: "ro", src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" }); + assert.deepEqual(modules, { kind: "ro", src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages (the repository's pi)" }); assert.ok(at(join(root, "a.sh")) < at(modules.src) && at(modules.src) < at(join(root, "bus.sock"))); const fixture = binds({ ...args, harnessMounts: [{ src: join(root, "tool"), why: "a fixture harness's tool" }] }); assert.deepEqual(fixture.find((b) => b.src === join(root, "tool")), { kind: "ro", src: join(root, "tool"), why: "a fixture harness's tool" }); diff --git a/packages/seat/tests/session.test.mjs b/packages/seat/tests/session.test.mjs index 08b3d051c..807c68558 100644 --- a/packages/seat/tests/session.test.mjs +++ b/packages/seat/tests/session.test.mjs @@ -58,6 +58,15 @@ test("sessionEnv passes only the allowlist, the repo's bin on PATH, and the run assert.equal(sessionEnv({ MOSAIC_CLAUDE_BIN: "/opt/claude" }, "r0123456789ab").MOSAIC_CLAUDE_BIN, "/opt/claude"); }); +// #1873 pi-unpin S2: the host's MOSAIC_PI_BIN reaches the session, as MOSAIC_CLAUDE_BIN does, and +// it is the only name the slice adds: every other MOSAIC_* and PI_* name from the host stays out. +test("pi-unpin S2: sessionEnv passes MOSAIC_PI_BIN and nothing else new", () => { + const env = sessionEnv({ MOSAIC_PI_BIN: "/opt/pi/bin/pi", PI_CODING_AGENT_DIR: "/h/.pi", PI_OFFLINE: "0", MOSAIC_PI_ROOT: "/x", PATH: "/usr/bin" }, "r0123456789ab"); + assert.deepEqual(Object.keys(env).sort(), ["MOSAIC_PI_BIN", "MOSAIC_RUN_ID", "PATH"]); + assert.equal(env.MOSAIC_PI_BIN, "/opt/pi/bin/pi"); + assert.deepEqual(ENV_ALLOW.filter((k) => k.startsWith("MOSAIC_")), ["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]); +}); + test("ZAI_API_KEY passes the session allowlist, and ZAI_TOKEN does not", () => { assert.ok(ENV_ALLOW.includes("ZAI_API_KEY")); const env = sessionEnv({ ZAI_API_KEY: "fixture-zai", ZAI_TOKEN: "t" }, "r0123456789ab"); diff --git a/packages/webui/src/triage-host.mjs b/packages/webui/src/triage-host.mjs index 71f1b5052..bb28323ad 100644 --- a/packages/webui/src/triage-host.mjs +++ b/packages/webui/src/triage-host.mjs @@ -127,8 +127,8 @@ const CAP = /^[0-9a-f]{64}$/; // The spawn's environment: these names from the Console's own environment, PATH fixed, and the // route's proxy variables. bwrap sets HOME and TMPDIR inside. export const SPAWN_ENV_ALLOW = Object.freeze(["PATH", "LANG", "LC_ALL", "TZ", "TERM"]); -// Passed only as an absolute path: the pinned harness binary (packages/seat/src/session.mjs ENV_ALLOW). -export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN"]); +// Passed only as an absolute path: a harness binary the Console names (packages/seat/src/session.mjs ENV_ALLOW). +export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]); // The names the runner sets for every harness (runner.mjs adapterEnv); the host's own names never replace them. const RUNNER_NAMES = new Set(["MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", "MOSAIC_SESSION_DIR", "MOSAIC_POLICY_FILE", "MOSAIC_TOOLS_FILE", "MOSAIC_TOOL_SOCKET", "MOSAIC_TURN_MARKER", "MOSAIC_PROVIDER", "MOSAIC_MODEL", "MOSAIC_MODEL_ACTIVITY_DIR", "MOSAIC_UNSLOP_LOG", @@ -826,6 +826,16 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async const start = async ({ admitted, installRoots, transcriptRoot, mod }) => { ensureDirs(); + // #1873 pi-unpin re-check 7: a module whose env() reads a check it runs first (pi's fit check) + // has it run and awaited before the bundle, which calls env(). A refusal or a throw refuses the + // route, as a throw from env() does. + if (typeof mod?.ready === "function") { + try { await mod.ready(); } + catch { + log("triage host: route refused: harness-bundle"); + throw new TriageError("triage-route-invalid", "the model route's answer is not usable"); + } + } const { run, harnessFiles } = writeBundle(admitted, mod); // The key this session holds, and anything shaped like a key, never reaches the transcript. const key = routeKey(admitted.env)?.value ?? null; diff --git a/packages/webui/src/triage-route.mjs b/packages/webui/src/triage-route.mjs index e8ce935af..920d355b2 100644 --- a/packages/webui/src/triage-route.mjs +++ b/packages/webui/src/triage-route.mjs @@ -228,6 +228,9 @@ export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catal // name the host's fence refuses (MOSAIC_* and ANTHROPIC_* among them: the host sets those) // and every name the module does not declare. const session = { model: offered.model, provider: offered.provider ?? "", ...(transport ? { proxyTransport: { ...transport } } : {}) }; + // #1873 pi-unpin re-check 7: a module whose env() reads a check it runs first (pi's fit + // check) has it run here and awaited; a refusal or a throw is incompatible, as env()'s is. + if (typeof mod.ready === "function") await mod.ready(); const given = mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: { ...PROXY_PROBE } }); keyName = routeKeyName(given); if (!keyName) return refuse("triage-harness-incompatible"); diff --git a/packages/webui/tests/triage-host.test.mjs b/packages/webui/tests/triage-host.test.mjs index e7c13c9a2..336759ac5 100644 --- a/packages/webui/tests/triage-host.test.mjs +++ b/packages/webui/tests/triage-host.test.mjs @@ -58,9 +58,13 @@ test("triageEnv keeps the fixed names and the route's proxy variables, and nothi assert.equal(triageEnv(CONSOLE_ENV, { ANTHROPIC_API_KEY: "from-the-route" }).ANTHROPIC_API_KEY, "from-the-route", "the route's key under the name its harness takes"); assert.equal(triageEnv(CONSOLE_ENV).ANTHROPIC_API_KEY, undefined, "a key in the Console's environment is not the route's"); // #1873 triage-wiring: the pinned harness binary passes as an absolute path, as a bus session's does. - assert.deepEqual(SPAWN_PATH_ALLOW, ["MOSAIC_CLAUDE_BIN"]); + // #1873 pi-unpin S2: and so does the pi binary the Console names, under the same checks. + assert.deepEqual(SPAWN_PATH_ALLOW, ["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]); assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: "/opt/claude/bin/claude" }).MOSAIC_CLAUDE_BIN, "/opt/claude/bin/claude"); - for (const bad of ["claude", "bin/claude", "/opt/a\nb", "/" + "x".repeat(300)]) assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: bad }).MOSAIC_CLAUDE_BIN, undefined, bad); + assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_PI_BIN: "/opt/pi/bin/pi" }).MOSAIC_PI_BIN, "/opt/pi/bin/pi"); + for (const name of SPAWN_PATH_ALLOW) { + for (const bad of ["claude", "bin/claude", "/opt/a\nb", "/" + "x".repeat(300)]) assert.equal(triageEnv({ ...CONSOLE_ENV, [name]: bad })[name], undefined, `${name} ${bad}`); + } }); test("the relay refuses every verb but a message to the person, and any call without the capability", async (t) => { @@ -275,6 +279,26 @@ test("fence 1: a name the harness module does not declare is refused; a declared assert.deepEqual(s.harnessEnv, { FAKE_OK: "yes" }); }); +// #1873 pi-unpin re-check 8 P3-2: the host awaits a module's ready() (pi's fit check) before the +// bundle. This module's env() works only once its ready() has finished, as pi's reads the check's +// pass; a ready() that rejects refuses the route though its env() would answer. +test("a module's ready() is awaited before the bundle: env() after it works, and a ready() that rejects refuses the route", async (t) => { + const gated = (roots, fail) => async (name) => { + if (name !== "fake") return null; + let done = false; + return { + envNames: DECLARED, installRoots: () => roots, transcript: ({ runDir }) => join(runDir, "session"), + ready: async () => { await new Promise((r) => setTimeout(r, 20)); if (fail) throw new Error("fit refused"); done = true; }, + bundle: () => ({ files: {} }), + // The host wants the route's key under its own name, as the key itself, and the proxy URL. + env: ({ proxy }) => { if (!done && !fail) throw new Error("no passed check"); return { ANTHROPIC_AUTH_TOKEN: proxy.key, ANTHROPIC_BASE_URL: proxy.baseUrl }; }, + }; + }; + await admittedRoute(t, "ready first", () => ({}), (root) => ({ harnessModule: gated([join(root, "install")], false) })); + const logs = await refusedRoute(t, "ready rejects", () => ({}), (root) => ({ harnessModule: gated([join(root, "install")], true) })); + assert.ok(logs.includes("triage host: route refused: harness-bundle"), logs.join(" | ")); +}); + test("fence 1: the bundle holds the copy the host checked; no MOSAIC_ name reaches session.json, even from a getter", async (t) => { let reads = 0; const s = await admittedRoute(t, "getter", (root) => () => { @@ -409,6 +433,8 @@ test("the shipped harness modules declare what their own env() and mounts() hand for (const name of ["pi", "claude-code"]) { const mod = await import(new URL(`../../plugins/harness/${name}/index.mjs`, import.meta.url).href); assert.ok(Array.isArray(mod.envNames) && Object.isFrozen(mod.envNames), name); + // #1873 S3: pi's env() reads the fit check its ready() awaits. + if (typeof mod.ready === "function") await mod.ready(); const keys = Object.keys(mod.env({ files: { settings: "/s", mcp: "/m" }, policy: { tools: ["read"] }, tools: [], session: { model: "m", thinking: "off", provider: "p" }, })); @@ -438,7 +464,11 @@ test("a host whose HOME holds the repository starts the shipped pi harness's own // triage-wiring); the module's PI_PROVIDER is not the host's to set. const own = Object.fromEntries(Object.entries(s.harnessEnv).filter(([k]) => !k.startsWith("MOSAIC_"))); assert.deepEqual(own, { PI_MODEL: "m", PI_OFFLINE: "1" }); - assert.deepEqual(Object.keys(s.harnessEnv).filter((k) => k.startsWith("MOSAIC_")).sort(), ["MOSAIC_EXTENSIONS", "MOSAIC_THINKING", "MOSAIC_TOOLS"]); + assert.deepEqual(Object.keys(s.harnessEnv).filter((k) => k.startsWith("MOSAIC_")).sort(), ["MOSAIC_EXTENSIONS", "MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "MOSAIC_THINKING", "MOSAIC_TOOLS"]); + // #1873 pi-unpin S3: the repository's pi has no --no-mcp, so the module sets it to "0". + assert.equal(s.harnessEnv.MOSAIC_PI_NO_MCP, "0"); + // #1873 pi-unpin S2: the session runs the pi the module found, here the repository's copy. + assert.ok(s.harnessEnv.MOSAIC_PI_BIN.startsWith(join(REPO, "node_modules") + "/"), s.harnessEnv.MOSAIC_PI_BIN); // The same host and module, with HOME's own directory (which holds the repository) as an extra // install root and mount: it is under HOME and not in the code, so it is refused. const wide = { ...pi, installRoots: (o) => [...pi.installRoots(o), home] };