From 7355b233a850b71d89ec2f3f0d5851e91c7e2dc0 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 15:01:35 -0600 Subject: [PATCH 01/16] feat(pi): pi-unpin S1: pi's labels come from the binary the module finds, not a pin (#1873) version() runs ` --version` with a 5 s timeout and a PATH that finds node, and returns null when that fails or prints something other than one short printable line. The compaction count records the found package's own package.json version, else null. Adapter and module comments say "read from 0.85.1" instead of "pinned". The lookup still honours only the repository's copy; S2 widens it. --- adapters/pi/adapter.sh | 4 +- packages/plugins/harness/pi/binary.mjs | 67 ++++++++++++++++++ packages/plugins/harness/pi/compactions.mjs | 10 +-- packages/plugins/harness/pi/index.mjs | 24 +++---- packages/plugins/tests/harness.test.mjs | 6 +- packages/plugins/tests/pi-binary.test.mjs | 76 +++++++++++++++++++++ 6 files changed, 164 insertions(+), 23 deletions(-) create mode 100644 packages/plugins/harness/pi/binary.mjs create mode 100644 packages/plugins/tests/pi-binary.test.mjs diff --git a/adapters/pi/adapter.sh b/adapters/pi/adapter.sh index 64a4290b9..be228cf56 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. diff --git a/packages/plugins/harness/pi/binary.mjs b/packages/plugins/harness/pi/binary.mjs new file mode 100644 index 000000000..2e69dfe34 --- /dev/null +++ b/packages/plugins/harness/pi/binary.mjs @@ -0,0 +1,67 @@ +// #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, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { spawnSync } 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; + } +}; + +// The binary a session runs, with the name a refusal line uses for it, or why there is none. +export function locate() { + if (executable(REPO_BIN)) return { bin: REPO_BIN, label: "the repository's pi" }; + return { bin: null, problem: "pi isn't installed in the repository's node_modules; run npm ci in the repository" }; +} + +// 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 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; + } +} + +// 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; + } +} + +// 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. +export function printedVersion(bin) { + if (!bin) return null; + const r = spawnSync(bin, ["--version"], { encoding: "utf8", timeout: 5000, killSignal: "SIGKILL", env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" } }); + if (r.status !== 0 || typeof r.stdout !== "string") return null; + const line = r.stdout.split("\n")[0].trim(); + return 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..6cbb638fe 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -1,18 +1,17 @@ -// 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, realpathSync, 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"; 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. @@ -37,18 +36,15 @@ export function preflight({ env = {}, proxy = null } = {}) { && !["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 }; 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. +export function version({ env = {} } = {}) { + return printedVersion(locate(env).bin); } // Pi sessions run on a provider key, never the Claude account limit. diff --git a/packages/plugins/tests/harness.test.mjs b/packages/plugins/tests/harness.test.mjs index 18814db4e..bb32851e4 100644 --- a/packages/plugins/tests/harness.test.mjs +++ b/packages/plugins/tests/harness.test.mjs @@ -98,11 +98,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"]); + const installed = JSON.parse(readFileSync(join(REPO, "node_modules", "@earendil-works", "pi-coding-agent", "package.json"), "utf8")).version; + assert.equal(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))]); }); diff --git a/packages/plugins/tests/pi-binary.test.mjs b/packages/plugins/tests/pi-binary.test.mjs new file mode 100644 index 000000000..598787100 --- /dev/null +++ b/packages/plugins/tests/pi-binary.test.mjs @@ -0,0 +1,76 @@ +// #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 { copyFileSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; + +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(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(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(pi.version({ env: {} }), null); + const long = await copyRepo(t, `#!/bin/sh\necho ${"9".repeat(129)}\n`, "1.0.0"); + assert.equal(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(pi.version({ env: {} }), installed); +}); -- 2.54.0 From f62b1b95184badebb8df5f27a444639b8aeb04f4 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 15:11:57 -0600 Subject: [PATCH 02/16] feat(pi): pi-unpin S2: the session runs the pi the module finds: MOSAIC_PI_BIN, else PATH, else the repository's copy, mounted by its package directory (#1873) binary.mjs locate() takes MOSAIC_PI_BIN (absolute and executable), else the first pi on PATH past the repository's node_modules/.bin, else the repository's copy. The binary must belong to the pi package. mounts() and installRoots() hold that package directory; the repository's copy keeps the whole node_modules mount, since its packages are hoisted beside pi. env() sets MOSAIC_PI_BIN to the binary's path inside the session, and the adapter and the model-stream branch run it. sessionEnv passes MOSAIC_PI_BIN, and Triage's SPAWN_PATH_ALLOW takes it with every host check kept. harness-binary-missing keeps its code; with no pi its message is "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci". A refusal names the variable, never a host path. --- adapters/pi/adapter.sh | 5 +- packages/cli/tests/launcher.test.mjs | 25 +++++ packages/plugins/README.md | 19 +++- packages/plugins/harness/pi/binary.mjs | 55 ++++++++-- packages/plugins/harness/pi/index.mjs | 38 ++++--- packages/plugins/tests/harness.test.mjs | 15 ++- packages/plugins/tests/pi-binary.test.mjs | 118 +++++++++++++++++++++- packages/seat/src/session.mjs | 2 +- packages/seat/tests/session.test.mjs | 9 ++ packages/webui/src/triage-host.mjs | 4 +- packages/webui/tests/triage-host.test.mjs | 12 ++- 11 files changed, 266 insertions(+), 36 deletions(-) diff --git a/adapters/pi/adapter.sh b/adapters/pi/adapter.sh index be228cf56..fd8876773 100644 --- a/adapters/pi/adapter.sh +++ b/adapters/pi/adapter.sh @@ -134,6 +134,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/cli/tests/launcher.test.mjs b/packages/cli/tests/launcher.test.mjs index a5e41bc2c..427898114 100644 --- a/packages/cli/tests/launcher.test.mjs +++ b/packages/cli/tests/launcher.test.mjs @@ -266,6 +266,31 @@ 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" })); + writeFileSync(join(pkg, "dist", "bundle", "cli.js"), "#!/bin/sh\n[ \"$1\" = --version ] && exit 1\nexit 0\n", { 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); +}); + 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/plugins/README.md b/packages/plugins/README.md index b4e28dd7b..2bcac8dc2 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -246,10 +246,23 @@ 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}`: 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. 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. +- `version({env})` is what the manifest records as `harnessVersion`. Pi's + is the first line its binary prints for `--version`, or null. - `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 index 2e69dfe34..e1e34670d 100644 --- a/packages/plugins/harness/pi/binary.mjs +++ b/packages/plugins/harness/pi/binary.mjs @@ -2,7 +2,8 @@ // 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, join } from "node:path"; +import { dirname, isAbsolute, join, relative } from "node:path"; +import { homedir } from "node:os"; import { fileURLToPath } from "node:url"; import { spawnSync } from "node:child_process"; @@ -20,10 +21,51 @@ export const executable = (path) => { } }; -// The binary a session runs, with the name a refusal line uses for it, or why there is none. -export function locate() { - if (executable(REPO_BIN)) return { bin: REPO_BIN, label: "the repository's pi" }; - return { bin: null, problem: "pi isn't installed in the repository's node_modules; run npm ci in the repository" }; +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 } 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. +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 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)) }; + } + // 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 }; } // The package directory that holds the binary's real file: the nearest directory above it whose @@ -31,7 +73,7 @@ export function locate() { export function packageDir(bin) { let dir; try { dir = dirname(realpathSync(bin)); } catch { return null; } - for (;;) { + 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; @@ -39,6 +81,7 @@ export function packageDir(bin) { if (up === dir) return null; dir = up; } + return null; } // The found package's own version, a file read with no spawn, or null. diff --git a/packages/plugins/harness/pi/index.mjs b/packages/plugins/harness/pi/index.mjs index 6cbb638fe..07a08e921 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -3,7 +3,7 @@ // 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 { lstatSync, mkdirSync, 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"; @@ -30,7 +30,7 @@ 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. +// The session runs the pi binary.mjs finds, from the package directory the sandbox mounts. export 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]))) { @@ -49,11 +49,21 @@ export function version({ env = {} } = {}) { // 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]; @@ -101,9 +111,12 @@ 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 }) { +// 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 the launcher and the Triage host call env() without one). +export function env({ policy, tools, session, proxy = null, env: hostEnv = process.env }) { return { + MOSAIC_PI_BIN: found(hostEnv).view, PI_OFFLINE: "1", MOSAIC_TOOLS: [...policy.tools, ...tools.map((t) => t.name)].join(","), MOSAIC_EXTENSIONS: [EXTENSION, UNSLOP.unslopExtension].join(","), @@ -118,9 +131,10 @@ export function env({ policy, tools, session, proxy = null }) { // 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. +export const envNames = Object.freeze(["MOSAIC_PI_BIN", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]); +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/tests/harness.test.mjs b/packages/plugins/tests/harness.test.mjs index bb32851e4..0b24f9aa3 100644 --- a/packages/plugins/tests/harness.test.mjs +++ b/packages/plugins/tests/harness.test.mjs @@ -3,7 +3,7 @@ // 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"; @@ -140,10 +140,15 @@ 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" })); + for (const file of [join(dir, "bin", "claude"), join(piPackage, "dist", "bundle", "cli.js")]) { + writeFileSync(file, '#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n'); + 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"); @@ -165,7 +170,7 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort, }; 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 index 598787100..4bc464a60 100644 --- a/packages/plugins/tests/pi-binary.test.mjs +++ b/packages/plugins/tests/pi-binary.test.mjs @@ -3,10 +3,12 @@ // compaction count records the found package's own version. import test from "node:test"; import assert from "node:assert/strict"; -import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join, resolve } from "node:path"; +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"; const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url))); const MODULE = join(REPO, "packages", "plugins", "harness", "pi"); @@ -74,3 +76,115 @@ test("pi-unpin S1: the repository's real pi reports its installed package's vers const installed = JSON.parse(readFileSync(join(REPO, "node_modules", NAME, "package.json"), "utf8")).version; assert.equal(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" }; +const ARGV_SCRIPT = '#!/bin/sh\n[ "$1" = --version ] && { echo 7.7.7; exit 0; }\nprintf "%s\\n" "$@" > "$ARGV_OUT"\n'; + +// 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" }; +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(real.preflight({ env }), null); + assert.equal(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 = 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(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(real.preflight({ env }), null); + assert.equal(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(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 = 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 = 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 = 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/); +}); 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/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..8fe9c0c4c 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", diff --git a/packages/webui/tests/triage-host.test.mjs b/packages/webui/tests/triage-host.test.mjs index e7c13c9a2..6db74e48f 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) => { @@ -438,7 +442,9 @@ 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_THINKING", "MOSAIC_TOOLS"]); + // #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] }; -- 2.54.0 From b8354e4304bbe816baca71ee78b33d45cd90db15 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 15:29:40 -0600 Subject: [PATCH 03/16] test(pi): pi-unpin S2 follow-up: the adapter tests point MOSAIC_PI_BIN at their stand-in, and the sandbox test reads the new mount label (#1873) --- packages/harness/tests/creative.test.mjs | 3 ++- packages/harness/tests/skills.test.mjs | 3 ++- packages/seat/tests/sandbox.test.mjs | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/harness/tests/creative.test.mjs b/packages/harness/tests/creative.test.mjs index d9ee76af8..5517c3158 100644 --- a/packages/harness/tests/creative.test.mjs +++ b/packages/harness/tests/creative.test.mjs @@ -177,7 +177,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/skills.test.mjs b/packages/harness/tests/skills.test.mjs index e7581d8b9..c10ccf246 100644 --- a/packages/harness/tests/skills.test.mjs +++ b/packages/harness/tests/skills.test.mjs @@ -183,7 +183,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/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" }); -- 2.54.0 From 8908df113ec9e0a5f5fc3bd26e1f51140dde603a Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 15:29:40 -0600 Subject: [PATCH 04/16] feat(pi): pi-unpin S3: every start runs a sandboxed, offline fit check on the pi it found and refuses harness-feature-missing when a flag is missing, a missing -e starts, or a planted source loads with the seal flags on (#1873) --- adapters/pi/adapter.sh | 7 + packages/cli/tests/launcher.test.mjs | 4 +- packages/harness/README.md | 31 ++- packages/harness/src/pi-fit.mjs | 233 ++++++++++++++++++++++ packages/harness/tests/pi-fit.test.mjs | 192 ++++++++++++++++++ packages/harness/tests/pi-stand-in.mjs | 110 ++++++++++ packages/plugins/README.md | 8 +- packages/plugins/harness/pi/binary.mjs | 9 +- packages/plugins/harness/pi/index.mjs | 19 +- packages/plugins/tests/harness.test.mjs | 10 +- packages/plugins/tests/pi-binary.test.mjs | 5 +- packages/seat/src/sandbox.mjs | 10 +- packages/webui/tests/triage-host.test.mjs | 4 +- 13 files changed, 619 insertions(+), 23 deletions(-) create mode 100644 packages/harness/src/pi-fit.mjs create mode 100644 packages/harness/tests/pi-fit.test.mjs create mode 100644 packages/harness/tests/pi-stand-in.mjs diff --git a/adapters/pi/adapter.sh b/adapters/pi/adapter.sh index fd8876773..4ea49eaf7 100644 --- a/adapters/pi/adapter.sh +++ b/adapters/pi/adapter.sh @@ -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 \ diff --git a/packages/cli/tests/launcher.test.mjs b/packages/cli/tests/launcher.test.mjs index 427898114..d89490f37 100644 --- a/packages/cli/tests/launcher.test.mjs +++ b/packages/cli/tests/launcher.test.mjs @@ -24,6 +24,7 @@ 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 { fakeHarnesses, fixture, HOST_CREDENTIALS, tmp, withoutHostCredentials } from "./helpers.mjs"; const FAKE = fileURLToPath(new URL("../../harness/tests/fixtures/fake-adapter.mjs", import.meta.url)); @@ -273,7 +274,8 @@ test("pi-unpin: a pi named by MOSAIC_PI_BIN that exits 1 on --version records a 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" })); - writeFileSync(join(pkg, "dist", "bundle", "cli.js"), "#!/bin/sh\n[ \"$1\" = --version ] && exit 1\nexit 0\n", { mode: 0o755 }); + // #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); diff --git a/packages/harness/README.md b/packages/harness/README.md index 5dc337f35..ac9d590f5 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -18,13 +18,30 @@ 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` +and `env` run the fit check in `src/pi-fit.mjs` on the binary the module +found. It runs that binary in a sandbox with the network unshared, an +empty environment and a scratch home, and calls no model. It needs +`--help` to list every flag the adapter passes (with rpc mode and each +thinking level), a missing `-e` file to stop the start, and the planted +ambient sources (extensions, settings lists, `mcp.json` files, skills, +prompt templates, context files) to stay unloaded with the seal flags on. +A control run without the seal flags must load at least one of them, or +the check can't tell and refuses. 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. 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..7022048ff --- /dev/null +++ b/packages/harness/src/pi-fit.mjs @@ -0,0 +1,233 @@ +// #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, an empty environment, a scratch HOME and agent directory, and the found package +// mounted as a session mounts it. No model is called. It asks: +// +// --version a label for the refusal line and the record; not a gate +// --help lists every flag Mosaic passes, with rpc mode and each thinking level +// -e /missing a missing extension stops the start (exit non-zero) before any answer +// control with planted ambient sources and no seal flags, the binary answers get_state and +// loads at least one planted source, so the sealed run can tell +// sealed with the adapter's seal flags (and --no-mcp when --help lists it) and one -e +// extension, the binary answers get_state, loads that extension (a session's gate +// comes in that way) and loads none of the planted sources +// +// A check that cannot tell refuses: a sandbox that fails, a run that times out, an answer that +// doesn't parse. A pass is cached in memory by a digest of the files that run (the package's +// package.json, every file under dist/bundle/, and every file in the directory tree that holds +// the binary's real file) and the mount layout. +// 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. +import { spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join, relative } from "node:path"; +import { BWRAP, checkArgv, sandboxProblem } from "../../seat/src/sandbox.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"]; +const RUN_TIMEOUT = 20000; +const OUTPUT_MAX = 4 * 1024 * 1024; +const DIGEST_FILES_MAX = 20000; + +const passed = new Map(); + +// found: binary.mjs's answer ({ bin, label, root, dest, view, pkg }). levels: the thinking levels +// the module applies. Returns { ok: true, version, noMcp, digest } or { ok: false, message }. +export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPath } = {}) { + const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`; + let digest; + try { + digest = filesDigest(found); + } catch { + return { ok: false, message: `${who(null)} can't be checked: its files can't be read` }; + } + const key = JSON.stringify([digest, found.root, found.dest ?? null, found.view, bwrap, node, levels]); + if (passed.has(key)) return passed.get(key); + // The sandbox's own error text can name a host path, so the line keeps only what failed. + const problem = sandboxProblem({ bwrap }); + if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem.startsWith("the sandbox (bwrap) is missing") ? "the sandbox (bwrap) is missing" : "the sandbox (bwrap) failed"}` }; + const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-")); + try { + const result = check(found, { levels, bwrap, node, scratch, who, digest }); + if (result.ok) passed.set(key, result); + return result; + } finally { + rmSync(scratch, { recursive: true, force: true }); + } +} + +function check(found, { levels, bwrap, node, scratch, who, digest }) { + const run = (args, input = "") => runIn(found, { bwrap, node, scratch, args, input }); + plant(scratch); + const v = run(["--version"]); + const version = v.status === 0 ? versionLabel(v.stdout) : null; + const help = run(["--help"]); + if (help.status !== 0) return { ok: false, message: `${who(version)} can't be checked: --help failed` }; + const lines = help.stdout.split("\n"); + const has = (flag) => lines.some((l) => flagIn(l, flag)); + const line = (flag) => lines.find((l) => flagIn(l, 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 { ok: false, message: `${who(version)} lacks ${missing.join(", ")}` }; + const noMcp = has("--no-mcp"); + const approve = has("--approve"); + + const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n'; + plant(scratch); + const missingExt = run(["-e", join(scratch, "missing", "extension.ts"), "--mode", "rpc", ...SEAL], ask); + if (missingExt.timedOut) return { ok: false, message: `${who(version)} can't be checked: a start with a missing -e extension timed out` }; + if (missingExt.status === 0 || answered(missingExt.stdout).state) return { ok: false, message: `${who(version)} starts with a missing -e extension` }; + + plant(scratch); + const control = run(["--mode", "rpc", "--no-session", "--offline", ...(approve ? ["--approve"] : [])], ask); + const controlAnswer = answered(control.stdout); + if (!controlAnswer.state) return { ok: false, message: `${who(version)} can't be checked: it didn't answer get_state` }; + if (seen(scratch, controlAnswer).length === 0) return { ok: false, message: `${who(version)} can't be checked: it loaded none of the planted sources without the seal flags` }; + + plant(scratch); + const sealed = run(["--mode", "rpc", ...SEAL, ...(noMcp ? ["--no-mcp"] : []), "-e", join(scratch, "explicit", "fit.ts"), "--system-prompt", join(scratch, "prompt.md")], ask); + const sealedAnswer = answered(sealed.stdout); + if (!sealedAnswer.state) return { ok: false, message: `${who(version)} can't be checked: it didn't answer get_state with the seal flags on` }; + if (!marked(scratch, EXPLICIT)) return { ok: false, message: `${who(version)} didn't load an -e extension with the seal flags on` }; + const leaked = seen(scratch, sealedAnswer); + if (leaked.length) return { ok: false, message: `${who(version)} loaded a planted ${leaked.join(", ")} with the seal flags on` }; + return { ok: true, version, noMcp, digest }; +} + +// The binary, inside the sandbox: the system part, the found package where a session sees it, and +// the scratch directory read-write. The environment holds HOME, the agent directory, TMPDIR, a +// PATH that finds node, and PI_OFFLINE. +function runIn(found, { bwrap, node, scratch, args, input }) { + const list = [ + { kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) }, + { kind: "rw", src: scratch }, + ]; + const env = { HOME: join(scratch, "home"), PI_CODING_AGENT_DIR: join(scratch, "agent"), TMPDIR: join(scratch, "tmp"), PATH: `${dirname(node)}:/usr/bin:/bin`, PI_OFFLINE: "1" }; + const argv = checkArgv(list, [found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap }); + const r = spawnSync(argv[0], argv.slice(1), { input, encoding: "utf8", timeout: RUN_TIMEOUT, killSignal: "SIGKILL", maxBuffer: OUTPUT_MAX, env: {} }); + return { status: r.status, stdout: typeof r.stdout === "string" ? r.stdout : "", timedOut: r.error?.code === "ETIMEDOUT" || r.signal === "SIGKILL" }; +} + +// One flag in one help line: the flag as its own token, at the line's start or after a space or +// a comma, and followed by the end, a space, a comma, "=" or "<". +function flagIn(line, flag) { + const at = (i) => i === 0 || line[i - 1] === " " || line[i - 1] === "," || line[i - 1] === "\t"; + const after = (i) => i === line.length || " ,=<\t".includes(line[i]); + for (let i = line.indexOf(flag); i !== -1; i = line.indexOf(flag, i + 1)) if (at(i) && after(i + flag.length)) return true; + return false; +} +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 (an empty list when it failed). +function answered(stdout) { + let state = false; + const commands = []; + 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)) { + for (const c of m.data.commands) if (typeof c?.name === "string") commands.push(c.name); + } + } + return { state, commands }; +} + +// The planted sources. Each extension and each MCP server touches its own marker; each skill and +// prompt template has a name get_commands would list. Context and system prompt files are planted +// too, so nothing ambient that a session could meet is missing from the tree. +// "default agent ..." sits in $HOME/.pi/agent, where pi looks when PI_CODING_AGENT_DIR is unset. +const MARKED = ["user extension", "user settings extension", "default agent extension", "project extension", "project settings extension", "user mcp.json", "default agent mcp.json", "project mcp.json", "project .mcp.json"]; +const NAMED = { "fit-user-skill": "user skill", "fit-default-skill": "default agent skill", "fit-project-skill": "project skill", "fit-agents-skill": "project .agents skill", "fit-home-skill": "home .agents skill", "fit-user-prompt": "user prompt template", "fit-default-prompt": "default agent prompt template", "fit-project-prompt": "project prompt template" }; +// The -e extension the sealed run passes, which must load. +const EXPLICIT = "explicit extension"; +const markerName = (source) => source.replace(/[^a-z]/g, "-"); +const marked = (scratch, source) => { try { return readdirSync(join(scratch, "marks")).includes(markerName(source)); } catch { return false; } }; + +function plant(scratch) { + for (const d of ["home", "agent", "project", "marks", "tmp", "missing", "explicit"]) rmSync(join(scratch, d), { recursive: true, force: true }); + 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 agent = join(scratch, "agent"); + const project = join(scratch, "project"); + mkdirSync(marks, { recursive: true }); + mkdirSync(join(scratch, "tmp"), { recursive: true }); + put(join(agent, "extensions", "fit.ts"), ext("user extension")); + put(join(agent, "listed", "fit.ts"), ext("user settings extension")); + put(join(agent, "settings.json"), JSON.stringify({ extensions: [join(agent, "listed", "fit.ts")] })); + 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"), "A planted prompt template.\n"); + for (const f of ["SYSTEM.md", "APPEND_SYSTEM.md", "AGENTS.md"]) put(join(agent, f), "A planted file.\n"); + put(join(project, ".pi", "extensions", "fit.ts"), ext("project extension")); + put(join(project, ".pi", "listed", "fit.ts"), ext("project settings extension")); + put(join(project, ".pi", "settings.json"), JSON.stringify({ extensions: [join(project, ".pi", "listed", "fit.ts")] })); + 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"), "A planted prompt template.\n"); + for (const f of [".pi/SYSTEM.md", ".pi/APPEND_SYSTEM.md", "AGENTS.md", "CLAUDE.md"]) put(join(project, f), "A planted file.\n"); + put(join(scratch, "home", ".agents", "skills", "fit-home-skill", "SKILL.md"), skill("fit-home-skill")); + const fallback = join(scratch, "home", ".pi", "agent"); + put(join(fallback, "extensions", "fit.ts"), ext("default agent extension")); + put(join(fallback, "mcp.json"), mcp("default agent mcp.json")); + put(join(fallback, "skills", "fit-default-skill", "SKILL.md"), skill("fit-default-skill")); + put(join(fallback, "prompts", "fit-default-prompt.md"), "A planted prompt template.\n"); + put(join(scratch, "explicit", "fit.ts"), ext(EXPLICIT)); + put(join(scratch, "prompt.md"), "A fit check.\n"); +} + +// The planted sources a run loaded: markers present, and planted names get_commands listed. +function seen(scratch, answer) { + let marks = []; + try { marks = readdirSync(join(scratch, "marks")); } catch { marks = []; } + const out = MARKED.filter((s) => marks.includes(markerName(s))); + for (const [name, source] of Object.entries(NAMED)) if (answer.commands.some((c) => c === name || c.endsWith(`:${name}`))) out.push(source); + return out; +} + +// sha256 over the sorted (path, sha256) list of the package's package.json, every regular file +// under dist/bundle/ (design section 4), and 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. 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); + 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/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs new file mode 100644 index 000000000..b6ec8321e --- /dev/null +++ b/packages/harness/tests/pi-fit.test.mjs @@ -0,0 +1,192 @@ +// #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 { mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { spawnSync } from "node:child_process"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { piFit } 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 { sandboxProblem } from "../../seat/src/sandbox.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 fit = (env) => piFit(locate(env), { levels: LEVELS }); +// 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, t => { + const s = standIn(t, { version: "2.0.0" }); + const r = 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(pi.preflight({ env: { ...CREDENTIAL, ...s.env } }), null); + assert.equal(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, t => { + const s = standIn(t, { dropFlags: ["--no-context-files"] }); + const problem = 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(fit(standIn(t, { modes: ["text", "json"] }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --mode rpc"); + assert.equal(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(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, t => { + const s = standIn(t, { missingExtOk: true }); + const problem = 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); +}); + +test("pi-unpin S3: a stand-in that touches the planted MCP marker under the seal flags is refused", SKIP, t => { + const s = standIn(t, { sealLeak: ["mcp"] }); + const problem = 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, project extension, project settings extension with the seal flags on", + skills: "loaded a planted user skill, project skill, project .agents skill, home .agents skill with the seal flags on", + prompts: "loaded a planted user prompt template, project prompt template with the seal flags on", + }; + for (const [kind, line] of Object.entries(lines)) assert.equal(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, t => { + const s = standIn(t, { ignoreAll: true }); + const problem = 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 loaded none of the planted sources without the seal flags" }); + noPath(problem.message, s.paths); +}); + +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, t => { + const s = standIn(t, { dropExplicit: true }); + const problem = 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, t => { + const s = standIn(t, { version: "/home/someone/secret 1.0", dropFlags: ["--no-skills"] }); + assert.equal(fit(s.env).message, "pi (MOSAIC_PI_BIN) lacks --no-skills"); + const quiet = standIn(t, { versionExit: 1 }); + const r = 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, t => { + const s = standIn(t, {}); + const first = fit(s.env); + assert.equal(first.ok, true, first.message); + assert.equal(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(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(fit(s.env).ok, true, "the refusal was not kept"); +}); + +test("pi-unpin S3: when --help lists --no-mcp the session env asks the adapter to pass it", SKIP, t => { + const s = standIn(t, { extraFlags: ["--no-mcp"] }); + assert.equal(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 plain = pi.env({ policy: { tools: ["read"] }, tools: [], session: { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }, env: standIn(t, {}).env }); + assert.equal(plain.MOSAIC_PI_NO_MCP, "0"); +}); + +test("pi-unpin S3: env() refuses a binary that fails the fit check, naming it by its label", SKIP, t => { + const s = standIn(t, { dropFlags: ["--no-approve"] }); + assert.throws(() => pi.env({ policy: { tools: ["read"] }, tools: [], session: { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }, env: s.env }), { message: "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-approve" }); +}); + +test("pi-unpin S3: a check that cannot run refuses", SKIP, t => { + const s = standIn(t, {}); + const r = 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(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(piFit(found, { levels: LEVELS }).message, "pi (MOSAIC_PI_BIN) can't be checked: its files can't be read"); +}); + +test("pi-unpin S3: the fit check leaves no scratch behind", SKIP, t => { + const before = readdirSync(tmpdir()).filter((n) => n.startsWith("mosaic-pi-fit-") && !n.startsWith("mosaic-pi-fit-t-")); + 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); +}); + +test("pi-unpin S3: real pi 0.85.1 from node_modules is accepted", SKIP, t => { + const found = locate({ PATH: "/usr/bin:/bin" }); + assert.equal(found.label, "the repository's pi"); + let at = process.hrtime.bigint(); + const r = 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(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`); +}); + +test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it", SKIP, t => { + const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }; + const run = (s) => { + 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(run(standIn(t, { extraFlags: ["--no-mcp"] })).filter((a) => a === "--no-mcp").length, 1); + assert.equal(run(standIn(t, {})).includes("--no-mcp"), false); +}); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs new file mode 100644 index 000000000..50d928038 --- /dev/null +++ b/packages/harness/tests/pi-stand-in.mjs @@ -0,0 +1,110 @@ +// #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 +// $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` +// -e a file that doesn't exist stops it with exit 1, unless `missingExtOk`; one that +// exists is loaded even with --no-extensions, unless `dropExplicit` +// --mode rpc loads the planted sources the way pi finds them, honouring the seal flags, +// then answers get_state and get_commands. `ignoreAll` loads nothing. +// `sealLeak` lists kinds (extensions, mcp, skills, prompts) loaded even when +// their seal flag is on. +// otherwise a session with no $ARGV_OUT: prints `sessionOut` and exits 0. +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); + process.stdout.write(lines.join("\n") + "\n"); + process.exit(0); +} +for (let i = 0; i < argv.length; i++) { + if (argv[i] === "-e" && !fs.existsSync(argv[i + 1]) && !K.missingExtOk) { + process.stderr.write("Failed to load extension: it does not exist\n"); + process.exit(1); + } +} +if (val("--mode") === "rpc") { + const leak = new Set(K.sealLeak || []); + const on = (kind, flag) => !K.ignoreAll && (!has(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 commands = []; + // Each planted extension writes one marker; do what it would do on load. + const load = (f) => { + let text = ""; + try { text = fs.readFileSync(f, "utf8"); } catch { return; } + const m = text.match(/writeFileSync\(("(?:[^"\\]|\\.)*")/); + if (m) fs.writeFileSync(JSON.parse(m[1]), ""); + }; + if (!K.dropExplicit) for (let i = 0; i < argv.length; i++) if (argv[i] === "-e") load(argv[i + 1]); + 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 [path.join(agent, "settings.json"), path.join(cwd, ".pi", "settings.json")]) files.push(...(json(s)?.extensions || [])); + for (const f of files) load(f); + } + if (on("mcp", "--no-extensions") && (!has("--no-mcp") || 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 || "/"; + for (const d of [path.join(agent, "skills"), path.join(cwd, ".pi", "skills"), path.join(cwd, ".agents", "skills"), path.join(home, ".agents", "skills")]) { + for (const p of list(d, () => true)) commands.push({ name: "skill:" + path.basename(p), source: "skill" }); + } + } + if (on("prompts", "--no-prompt-templates")) { + for (const d of [path.join(agent, "prompts"), path.join(cwd, ".pi", "prompts")]) { + for (const p of list(d, (n) => n.endsWith(".md"))) commands.push({ name: path.basename(p, ".md"), source: "prompt" }); + } + } + 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") process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_commands", success: true, data: { commands } }) + "\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.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/plugins/README.md b/packages/plugins/README.md index 2bcac8dc2..a2d1358e8 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -260,7 +260,13 @@ export returns: 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. + repository's pi", never a host path. Pi's `preflight` and `env` then run + the fit check (#1873 S3, `packages/harness/src/pi-fit.mjs`), which + refuses `harness-feature-missing` when the binary lacks a flag the + adapter passes, starts with a missing `-e` file, loads a planted ambient + source 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`. - `version({env})` is what the manifest records as `harnessVersion`. Pi's is the first line its binary prints for `--version`, or null. - `countsAccount()` says whether its launches spend `launch.account` and get diff --git a/packages/plugins/harness/pi/binary.mjs b/packages/plugins/harness/pi/binary.mjs index e1e34670d..d4bc0562c 100644 --- a/packages/plugins/harness/pi/binary.mjs +++ b/packages/plugins/harness/pi/binary.mjs @@ -31,8 +31,9 @@ const inside = (path, root) => root === "/" || path === root || path.startsWith( // 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 } 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. +// 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; @@ -58,14 +59,14 @@ function placed(bin, label, env) { // 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)) }; + 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 }; + return { bin, label, root: pkg, dest: null, view: realBin, pkg }; } // The package directory that holds the binary's real file: the nearest directory above it whose diff --git a/packages/plugins/harness/pi/index.mjs b/packages/plugins/harness/pi/index.mjs index 07a08e921..f18642607 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -8,6 +8,7 @@ import { dirname, join } from "node:path"; import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; import { locate, printedVersion } from "./binary.mjs"; +import { piFit } from "../../../harness/src/pi-fit.mjs"; export const interfaceVersion = 1; @@ -38,6 +39,10 @@ export function preflight({ env = {}, proxy = null } = {}) { } 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. + const fit = piFit(found, { levels: LEVELS }); + if (!fit.ok) return { code: "harness-feature-missing", message: fit.message }; return null; } @@ -115,8 +120,15 @@ export function bundle({ dir, models, proxy = null, transports = {} } = {}) { // MOSAIC_PI_BIN is the found binary's path inside the session; the bus's own environment names it // (env defaults to it, since the launcher and the Triage host call env() without one). 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 (cached from preflight). + const fit = piFit(at, { levels: LEVELS }); + if (!fit.ok) throw new Error(fit.message); return { - MOSAIC_PI_BIN: found(hostEnv).view, + MOSAIC_PI_BIN: at.view, + // "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: fit.noMcp ? "1" : "0", PI_OFFLINE: "1", MOSAIC_TOOLS: [...policy.tools, ...tools.map((t) => t.name)].join(","), MOSAIC_EXTENSIONS: [EXTENSION, UNSLOP.unslopExtension].join(","), @@ -132,8 +144,9 @@ export function env({ policy, tools, session, proxy = null, env: hostEnv = proce // 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). MOSAIC_PI_BIN -// (#1873 S2) is the binary's path inside the session, which the host keeps from its own env() call. -export const envNames = Object.freeze(["MOSAIC_PI_BIN", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]); +// (#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". +export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]); 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. diff --git a/packages/plugins/tests/harness.test.mjs b/packages/plugins/tests/harness.test.mjs index 0b24f9aa3..6a78a76d9 100644 --- a/packages/plugins/tests/harness.test.mjs +++ b/packages/plugins/tests/harness.test.mjs @@ -10,6 +10,7 @@ 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))); @@ -144,10 +145,11 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort, 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" })); - for (const file of [join(dir, "bin", "claude"), join(piPackage, "dist", "bundle", "cli.js")]) { - writeFileSync(file, '#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n'); - chmodSync(file, 0o755); - } + 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 env() runs. + 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) => { diff --git a/packages/plugins/tests/pi-binary.test.mjs b/packages/plugins/tests/pi-binary.test.mjs index 4bc464a60..a64d9974f 100644 --- a/packages/plugins/tests/pi-binary.test.mjs +++ b/packages/plugins/tests/pi-binary.test.mjs @@ -9,6 +9,7 @@ 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"); @@ -82,7 +83,9 @@ test("pi-unpin S1: the repository's real pi reports its installed package's vers // 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" }; -const ARGV_SCRIPT = '#!/bin/sh\n[ "$1" = --version ] && { echo 7.7.7; exit 0; }\nprintf "%s\\n" "$@" > "$ARGV_OUT"\n'; +// #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") { diff --git a/packages/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index b98c047b4..00daadffb 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 @@ -193,6 +193,14 @@ export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWR return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", "--setenv", "HOME", join(runDir, "home"), "--setenv", "TMPDIR", "/tmp", ...identity, "--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. +export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP }) { + const setenv = Object.entries(env).flatMap(([k, v]) => ["--setenv", k, v]); + return [bwrap, ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...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/webui/tests/triage-host.test.mjs b/packages/webui/tests/triage-host.test.mjs index 6db74e48f..2dc844efb 100644 --- a/packages/webui/tests/triage-host.test.mjs +++ b/packages/webui/tests/triage-host.test.mjs @@ -442,7 +442,9 @@ 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_PI_BIN", "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 -- 2.54.0 From 7c1b87d1544c8103352da20d9f64f53177e4475d Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 15:51:29 -0600 Subject: [PATCH 05/16] fix(pi): pi-unpin S2 review: a found pi must be the bin its package's package.json names, and the fixtures name it (#1873) --- packages/cli/tests/launcher.test.mjs | 2 +- packages/plugins/harness/pi/binary.mjs | 14 ++++++++++++++ packages/plugins/tests/harness.test.mjs | 2 +- packages/plugins/tests/pi-binary.test.mjs | 22 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/cli/tests/launcher.test.mjs b/packages/cli/tests/launcher.test.mjs index d89490f37..245bff039 100644 --- a/packages/cli/tests/launcher.test.mjs +++ b/packages/cli/tests/launcher.test.mjs @@ -273,7 +273,7 @@ test("pi-unpin: a pi named by MOSAIC_PI_BIN that exits 1 on --version records a 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" })); + 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")); diff --git a/packages/plugins/harness/pi/binary.mjs b/packages/plugins/harness/pi/binary.mjs index d4bc0562c..fb67592f0 100644 --- a/packages/plugins/harness/pi/binary.mjs +++ b/packages/plugins/harness/pi/binary.mjs @@ -55,6 +55,10 @@ 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); @@ -69,6 +73,16 @@ function placed(bin, label, env) { 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) { diff --git a/packages/plugins/tests/harness.test.mjs b/packages/plugins/tests/harness.test.mjs index 6a78a76d9..b301980b4 100644 --- a/packages/plugins/tests/harness.test.mjs +++ b/packages/plugins/tests/harness.test.mjs @@ -144,7 +144,7 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort, // #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" })); + 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 env() runs. diff --git a/packages/plugins/tests/pi-binary.test.mjs b/packages/plugins/tests/pi-binary.test.mjs index a64d9974f..853db3354 100644 --- a/packages/plugins/tests/pi-binary.test.mjs +++ b/packages/plugins/tests/pi-binary.test.mjs @@ -191,3 +191,25 @@ test("pi-unpin S2: a pi that is not in a pi package is refused, and so is a pi t 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 = 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 = 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(real.version({ env: { MOSAIC_PI_BIN: cli } }), "1.2.3"); +}); -- 2.54.0 From df2608530364a9a0ddc019124cc42abb8ea8cb33 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 15:51:29 -0600 Subject: [PATCH 06/16] fix(pi): pi-unpin S3 review: the control must show each planted kind and answer get_commands, the -e extension reads the system prompt so context and trusted project files are checked, settings and package sources are planted, help flags come from the option column, missing -e and the seal run in -p too, and the digest covers npm's lock files (#1873) --- packages/harness/README.md | 25 ++- packages/harness/src/pi-fit.mjs | 225 ++++++++++++++++++------- packages/harness/tests/pi-fit.test.mjs | 59 ++++++- packages/harness/tests/pi-stand-in.mjs | 92 +++++++--- packages/plugins/README.md | 5 +- packages/plugins/harness/pi/index.mjs | 6 +- 6 files changed, 310 insertions(+), 102 deletions(-) diff --git a/packages/harness/README.md b/packages/harness/README.md index ac9d590f5..dc7787413 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -30,16 +30,27 @@ Pi is not pinned (#1873 pi-unpin). Before a session starts, `preflight` and `env` run the fit check in `src/pi-fit.mjs` on the binary the module found. It runs that binary in a sandbox with the network unshared, an empty environment and a scratch home, and calls no model. It needs -`--help` to list every flag the adapter passes (with rpc mode and each -thinking level), a missing `-e` file to stop the start, and the planted -ambient sources (extensions, settings lists, `mcp.json` files, skills, -prompt templates, context files) to stay unloaded with the seal flags on. -A control run without the seal flags must load at least one of them, or -the check can't tell and refuses. Any failure refuses the launch with +`--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, in rpc and 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 it sees at `session_start`; the +sealed runs must show the `--system-prompt` file and none of the planted +files. A control run without the seal flags must show each kind of +source (an MCP server only when `--help` lists `--no-mcp`) and answer +`get_state` and `get_commands`, or the check can't tell and refuses. 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. Lines 1, 2, 4 and 5 are not +memory by a digest of the package's files (and npm's lock files, for +the repository's copy). 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). diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 7022048ff..73cc700fc 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -1,23 +1,35 @@ // #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, an empty environment, a scratch HOME and agent directory, and the found package -// mounted as a session mounts it. No model is called. It asks: +// mounted as a session mounts it. 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 every flag Mosaic passes, with rpc mode and each thinking level -// -e /missing a missing extension stops the start (exit non-zero) before any answer -// control with planted ambient sources and no seal flags, the binary answers get_state and -// loads at least one planted source, so the sealed run can tell -// sealed with the adapter's seal flags (and --no-mcp when --help lists it) and one -e -// extension, the binary answers get_state, loads that extension (a session's gate -// comes in that way) and loads none of the planted sources +// --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, no seal flags and +// one -e extension that writes the system prompt it sees at session_start. The +// binary answers get_state and get_commands, and shows each kind of planted source: +// an extension, a skill, a prompt template, a context file, a trusted project file +// (the trust --no-approve must undo), and an MCP server when --help lists --no-mcp. +// 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. +// sealed -p the same in -p, the mode the adapter runs. 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 // -// A check that cannot tell refuses: a sandbox that fails, a run that times out, an answer that -// doesn't parse. A pass is cached in memory by a digest of the files that run (the package's -// package.json, every file under dist/bundle/, and every file in the directory tree that holds -// the binary's real file) and the mount layout. +// A check that cannot tell refuses: a sandbox that fails, a run that times out, an answer that is +// missing or doesn't parse, a kind of source the control doesn't show. 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) and the mount layout. // 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. import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; @@ -62,40 +74,66 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa function check(found, { levels, bwrap, node, scratch, who, digest }) { const run = (args, input = "") => runIn(found, { bwrap, node, scratch, args, input }); + const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` }); plant(scratch); const v = run(["--version"]); const version = v.status === 0 ? versionLabel(v.stdout) : null; const help = run(["--help"]); - if (help.status !== 0) return { ok: false, message: `${who(version)} can't be checked: --help failed` }; + if (help.status !== 0) return refuse("can't be checked: --help failed"); const lines = help.stdout.split("\n"); - const has = (flag) => lines.some((l) => flagIn(l, flag)); - const line = (flag) => lines.find((l) => flagIn(l, flag)) ?? ""; + 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 { ok: false, message: `${who(version)} lacks ${missing.join(", ")}` }; + if (missing.length) return refuse(`lacks ${missing.join(", ")}`); const noMcp = has("--no-mcp"); - const approve = has("--approve"); const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n'; - plant(scratch); - const missingExt = run(["-e", join(scratch, "missing", "extension.ts"), "--mode", "rpc", ...SEAL], ask); - if (missingExt.timedOut) return { ok: false, message: `${who(version)} can't be checked: a start with a missing -e extension timed out` }; - if (missingExt.status === 0 || answered(missingExt.stdout).state) return { ok: false, message: `${who(version)} starts with a missing -e extension` }; + const explicit = ["-e", join(scratch, "explicit", "fit.ts")]; + const sealed = [...SEAL, ...(noMcp ? ["--no-mcp"] : []), ...explicit, "--system-prompt", join(scratch, "prompt.md")]; + // -p takes a message; offline with no key, pi stops before any model call. + const print = ["-p", ...sealed, "fit check"]; plant(scratch); - const control = run(["--mode", "rpc", "--no-session", "--offline", ...(approve ? ["--approve"] : [])], ask); + const control = run(["--mode", "rpc", "--no-session", "--offline", ...explicit], ask); + if (control.timedOut) return refuse("can't be checked: the start without the seal flags timed out"); const controlAnswer = answered(control.stdout); - if (!controlAnswer.state) return { ok: false, message: `${who(version)} can't be checked: it didn't answer get_state` }; - if (seen(scratch, controlAnswer).length === 0) return { ok: false, message: `${who(version)} can't be checked: it loaded none of the planted sources without the seal flags` }; + if (!controlAnswer.state) return refuse("can't be checked: it didn't answer get_state"); + if (!controlAnswer.commands) return refuse("can't be checked: it didn't answer get_commands"); + if (!marked(scratch, EXPLICIT) || prompted(scratch) === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags"); + const shown = new Set(seen(scratch, controlAnswer).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`); plant(scratch); - const sealed = run(["--mode", "rpc", ...SEAL, ...(noMcp ? ["--no-mcp"] : []), "-e", join(scratch, "explicit", "fit.ts"), "--system-prompt", join(scratch, "prompt.md")], ask); - const sealedAnswer = answered(sealed.stdout); - if (!sealedAnswer.state) return { ok: false, message: `${who(version)} can't be checked: it didn't answer get_state with the seal flags on` }; - if (!marked(scratch, EXPLICIT)) return { ok: false, message: `${who(version)} didn't load an -e extension with the seal flags on` }; - const leaked = seen(scratch, sealedAnswer); - if (leaked.length) return { ok: false, message: `${who(version)} loaded a planted ${leaked.join(", ")} with the seal flags on` }; + const rpc = run(["--mode", "rpc", ...sealed], ask); + if (rpc.timedOut) return refuse("can't be checked: the start with the seal flags timed out"); + const rpcAnswer = answered(rpc.stdout); + if (!rpcAnswer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on"); + if (!rpcAnswer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on"); + if (!marked(scratch, EXPLICIT)) return refuse("didn't load an -e extension with the seal flags on"); + const rpcPrompt = prompted(scratch); + if (rpcPrompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on"); + if (!rpcPrompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on"); + let leaked = seen(scratch, rpcAnswer); + if (leaked.length) return refuse(`loaded a planted ${leaked.map((s) => s.source).join(", ")} with the seal flags on`); + + plant(scratch); + const p = run(print); + if (p.timedOut) return refuse("can't be checked: a -p start with the seal flags timed out"); + if (prompted(scratch) === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension"); + leaked = seen(scratch, { commands: [] }); + if (leaked.length) return refuse(`loaded a planted ${leaked.map((s) => s.source).join(", ")} with the seal flags on in -p`); + + // 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. + for (const [mode, args, input] of [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]]) { + plant(scratch); + const r = run([...args, ...SEAL, "-e", join(scratch, "missing", "extension.ts"), ...explicit, "--system-prompt", join(scratch, "prompt.md"), ...(mode === "-p" ? ["fit check"] : [])], input); + if (r.timedOut) return refuse(`can't be checked: a ${mode} start with a missing -e extension timed out`); + if (r.status === 0 || answered(r.stdout).state || prompted(scratch) !== null) return refuse("starts with a missing -e extension"); + } return { ok: true, version, noMcp, digest }; } @@ -113,13 +151,14 @@ function runIn(found, { bwrap, node, scratch, args, input }) { return { status: r.status, stdout: typeof r.stdout === "string" ? r.stdout : "", timedOut: r.error?.code === "ETIMEDOUT" || r.signal === "SIGKILL" }; } -// One flag in one help line: the flag as its own token, at the line's start or after a space or -// a comma, and followed by the end, a space, a comma, "=" or "<". -function flagIn(line, flag) { - const at = (i) => i === 0 || line[i - 1] === " " || line[i - 1] === "," || line[i - 1] === "\t"; - const after = (i) => i === line.length || " ,=<\t".includes(line[i]); - for (let i = line.indexOf(flag); i !== -1; i = line.indexOf(flag, i + 1)) if (at(i) && after(i + flag.length)) return true; - return false; +// 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); @@ -131,83 +170,133 @@ function versionLabel(stdout) { } // The rpc answers in the output: whether get_state succeeded, and the command names get_commands -// listed (an empty list when it failed). +// listed, or null when it didn't answer with a { commands: [...] } list. function answered(stdout) { let state = false; - const commands = []; + 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)) { - for (const c of m.data.commands) if (typeof c?.name === "string") commands.push(c.name); + commands = m.data.commands.filter((c) => typeof c?.name === "string").map((c) => c.name); } } return { state, commands }; } -// The planted sources. Each extension and each MCP server touches its own marker; each skill and -// prompt template has a name get_commands would list. Context and system prompt files are planted -// too, so nothing ambient that a session could meet is missing from the tree. -// "default agent ..." sits in $HOME/.pi/agent, where pi looks when PI_CODING_AGENT_DIR is unset. -const MARKED = ["user extension", "user settings extension", "default agent extension", "project extension", "project settings extension", "user mcp.json", "default agent mcp.json", "project mcp.json", "project .mcp.json"]; -const NAMED = { "fit-user-skill": "user skill", "fit-default-skill": "default agent skill", "fit-project-skill": "project skill", "fit-agents-skill": "project .agents skill", "fit-home-skill": "home .agents skill", "fit-user-prompt": "user prompt template", "fit-default-prompt": "default agent prompt template", "fit-project-prompt": "project prompt template" }; -// The -e extension the sealed run passes, which must load. +// 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. "default agent ..." sits in $HOME/.pi/agent, where pi looks when PI_CODING_AGENT_DIR is +// unset. "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", "default agent extension": "extension", + "project extension": "extension", "project settings extension": "extension", "project package extension": "extension", + "user mcp.json": "MCP server", "default agent 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-default-skill": ["default agent 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-default-prompt": ["default agent 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 = [ + ["agent/SYSTEM.md", "FITUSERSYSTEM", "user SYSTEM.md", "context file"], + ["agent/AGENTS.md", "FITUSERAGENTS", "user AGENTS.md", "context file"], + ["home/.pi/agent/AGENTS.md", "FITDEFAULTAGENTS", "default agent 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 the control and sealed runs pass, which must load. const EXPLICIT = "explicit extension"; const markerName = (source) => source.replace(/[^a-z]/g, "-"); const marked = (scratch, source) => { try { return readdirSync(join(scratch, "marks")).includes(markerName(source)); } catch { return false; } }; +// The system prompt the -e extension saw at session_start, or null when it wrote none. +const prompted = (scratch) => { try { return readFileSync(join(scratch, "marks", "system-prompt"), "utf8"); } catch { return null; } }; function plant(scratch) { - for (const d of ["home", "agent", "project", "marks", "tmp", "missing", "explicit"]) rmSync(join(scratch, d), { recursive: true, force: true }); + for (const d of ["home", "agent", "project", "marks", "tmp", "missing", "explicit", "listed"]) rmSync(join(scratch, d), { recursive: true, force: true }); 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, "agent"); const project = join(scratch, "project"); mkdirSync(marks, { recursive: true }); mkdirSync(join(scratch, "tmp"), { recursive: true }); + mkdirSync(project, { 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, "listed", "fit.ts"), ext("user settings extension")); - put(join(agent, "settings.json"), JSON.stringify({ extensions: [join(agent, "listed", "fit.ts")] })); 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"), "A planted prompt template.\n"); - for (const f of ["SYSTEM.md", "APPEND_SYSTEM.md", "AGENTS.md"]) put(join(agent, f), "A planted file.\n"); + 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", "listed", "fit.ts"), ext("project settings extension")); - put(join(project, ".pi", "settings.json"), JSON.stringify({ extensions: [join(project, ".pi", "listed", "fit.ts")] })); 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"), "A planted prompt template.\n"); - for (const f of [".pi/SYSTEM.md", ".pi/APPEND_SYSTEM.md", "AGENTS.md", "CLAUDE.md"]) put(join(project, f), "A planted file.\n"); + 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")); const fallback = join(scratch, "home", ".pi", "agent"); put(join(fallback, "extensions", "fit.ts"), ext("default agent extension")); put(join(fallback, "mcp.json"), mcp("default agent mcp.json")); put(join(fallback, "skills", "fit-default-skill", "SKILL.md"), skill("fit-default-skill")); - put(join(fallback, "prompts", "fit-default-prompt.md"), "A planted prompt template.\n"); - put(join(scratch, "explicit", "fit.ts"), ext(EXPLICIT)); - put(join(scratch, "prompt.md"), "A fit check.\n"); + put(join(fallback, "prompts", "fit-default-prompt.md"), prompt); + 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, "system-prompt"))}, ctx.getSystemPrompt()); });\n}\n`); + put(join(scratch, "prompt.md"), `${GIVEN}\n`); } -// The planted sources a run loaded: markers present, and planted names get_commands listed. +// The planted sources a run loaded, as { source, kind }: markers present, planted names +// get_commands listed, and planted words in the system prompt the -e extension saw. function seen(scratch, answer) { let marks = []; try { marks = readdirSync(join(scratch, "marks")); } catch { marks = []; } - const out = MARKED.filter((s) => marks.includes(markerName(s))); - for (const [name, source] of Object.entries(NAMED)) if (answer.commands.some((c) => c === name || c.endsWith(`:${name}`))) out.push(source); + const out = Object.entries(MARKED).filter(([s]) => marks.includes(markerName(s))).map(([source, kind]) => ({ source, kind })); + for (const [name, [source, kind]] of Object.entries(NAMED)) if ((answer.commands ?? []).some((c) => c === name || c.endsWith(`:${name}`))) out.push({ source, kind }); + const text = prompted(scratch) ?? ""; + 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), and 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. A -// symlink there is read through. +// 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); @@ -225,6 +314,12 @@ function filesDigest(found) { 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"); diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index b6ec8321e..2059ba9ec 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -73,6 +73,8 @@ test("pi-unpin S3: a stand-in that exits 0 on a missing -e is refused", SKIP, t const problem = 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(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, t => { @@ -82,9 +84,13 @@ test("pi-unpin S3: a stand-in that touches the planted MCP marker under the seal 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, project extension, project settings extension with the seal flags on", - skills: "loaded a planted user skill, project skill, project .agents skill, home .agents skill with the seal flags on", - prompts: "loaded a planted user prompt template, project prompt template with the seal flags on", + 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(fit(standIn(t, { sealLeak: [kind] }).env).message, `pi 2.0.0 (MOSAIC_PI_BIN) ${line}`, kind); }); @@ -92,10 +98,36 @@ test("pi-unpin S3: a stand-in that touches the planted MCP marker under the seal 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, t => { const s = standIn(t, { ignoreAll: true }); const problem = 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 loaded none of the planted sources without the seal flags" }); + 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, t => { + for (const commands of ["fail", "list"]) { + const s = standIn(t, { commands, sealLeak: ["skills", "prompts"] }); + assert.equal(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(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 review: a stand-in that drops the --system-prompt file, or runs no session_start in -p, is refused", SKIP, t => { + assert.equal(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"); + assert.equal(fit(standIn(t, { printNoStart: true }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: a -p start with the seal flags didn't run its -e extension"); +}); + +// 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, 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(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"] }); + assert.equal(fit(aliased.env).ok, true, fit(aliased.env).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, t => { const s = standIn(t, { dropExplicit: true }); const problem = pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); @@ -123,6 +155,25 @@ test("pi-unpin S3: a pass is cached by the files that run; a changed file is che assert.equal(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, 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 = piFit(found, { levels: LEVELS }); + assert.equal(first.ok, true, first.message); + assert.equal(piFit(found, { levels: LEVELS }), first); + writeFileSync(lock, '{"packages":{}}'); + const again = 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(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, t => { const s = standIn(t, { extraFlags: ["--no-mcp"] }); assert.equal(fit(s.env).noMcp, true); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index 50d928038..374132140 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -7,14 +7,26 @@ // $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` -// -e a file that doesn't exist stops it with exit 1, unless `missingExtOk`; one that -// exists is loaded even with --no-extensions, unless `dropExplicit` -// --mode rpc loads the planted sources the way pi finds them, honouring the seal flags, -// then answers get_state and get_commands. `ignoreAll` loads nothing. -// `sealLeak` lists kinds (extensions, mcp, skills, prompts) loaded even when -// their seal flag is on. +// --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 ctx.getSystemPrompt() +// gets the prompt this stand-in builds), 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. +// `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. `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` @@ -34,34 +46,49 @@ if (argv[0] === "--help") { 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]) && !K.missingExtOk) { + 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 (val("--mode") === "rpc") { +if (rpc || print) { const leak = new Set(K.sealLeak || []); const on = (kind, flag) => !K.ignoreAll && (!has(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 = !K.ignoreAll && (has("--approve") || (saved && (!has("--no-approve") || leak.has("approve")))); const commands = []; - // Each planted extension writes one marker; do what it would do on load. + // A planted extension writes one marker on load; an -e extension may also write the system + // prompt at session_start. Do what each would do. + const starts = []; const load = (f) => { - let text = ""; - try { text = fs.readFileSync(f, "utf8"); } catch { return; } - const m = text.match(/writeFileSync\(("(?:[^"\\]|\\.)*")/); - if (m) fs.writeFileSync(JSON.parse(m[1]), ""); + const text = read(f); + if (text === null) return; + for (const m of text.matchAll(/writeFileSync\(("(?:[^"\\]|\\.)*")(, ctx\.getSystemPrompt\(\))?/g)) { + if (m[2]) starts.push(JSON.parse(m[1])); + else fs.writeFileSync(JSON.parse(m[1]), ""); + } }; - if (!K.dropExplicit) for (let i = 0; i < argv.length; i++) if (argv[i] === "-e") load(argv[i + 1]); + if (!(K.dropExplicit && has("--no-extensions"))) for (let i = 0; i < argv.length; i++) if (argv[i] === "-e") load(argv[i + 1]); + const packages = settings.flatMap((s) => (s.packages || []).map((p) => typeof p === "string" ? p : p.source)); 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 [path.join(agent, "settings.json"), path.join(cwd, ".pi", "settings.json")]) files.push(...(json(s)?.extensions || [])); + 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); } if (on("mcp", "--no-extensions") && (!has("--no-mcp") || leak.has("mcp"))) { @@ -71,14 +98,30 @@ if (val("--mode") === "rpc") { } if (on("skills", "--no-skills")) { const home = process.env.HOME || "/"; - for (const d of [path.join(agent, "skills"), path.join(cwd, ".pi", "skills"), path.join(cwd, ".agents", "skills"), path.join(home, ".agents", "skills")]) { - for (const p of list(d, () => true)) commands.push({ name: "skill:" + path.basename(p), source: "skill" }); - } + 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")) { - for (const d of [path.join(agent, "prompts"), path.join(cwd, ".pi", "prompts")]) { - for (const p of list(d, (n) => n.endsWith(".md"))) commands.push({ name: path.basename(p, ".md"), source: "prompt" }); - } + 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 given = K.ignoreSystemPrompt ? null : val("--system-prompt"); + const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!K.ignoreAll && read(path.join(agent, name))) || ""; + const parts = [given ? (read(given) || "") : own("SYSTEM.md"), own("APPEND_SYSTEM.md")]; + if (!K.ignoreAll && (!has("--no-context-files") || leak.has("context"))) { + 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")) || ""); + } + if (!(print && K.printNoStart)) for (const f of starts) fs.writeFileSync(f, parts.join("\n")); + if (print) { + if (K.sessionOut) process.stdout.write(K.sessionOut); + process.exit(0); } let input = ""; try { input = fs.readFileSync(0, "utf8"); } catch { input = ""; } @@ -86,7 +129,10 @@ if (val("--mode") === "rpc") { 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") process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_commands", success: true, data: { commands } }) + "\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); } diff --git a/packages/plugins/README.md b/packages/plugins/README.md index a2d1358e8..c8da9ad44 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -253,7 +253,8 @@ export returns: 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. With none, the message is + `@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 @@ -264,7 +265,7 @@ export returns: the fit check (#1873 S3, `packages/harness/src/pi-fit.mjs`), which refuses `harness-feature-missing` when the binary lacks a flag the adapter passes, starts with a missing `-e` file, loads a planted ambient - source with the seal flags on, or can't be checked. When its `--help` + 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`. - `version({env})` is what the manifest records as `harnessVersion`. Pi's diff --git a/packages/plugins/harness/pi/index.mjs b/packages/plugins/harness/pi/index.mjs index f18642607..43517da50 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -118,7 +118,11 @@ export function bundle({ dir, models, proxy = null, transports = {} } = {}) { // 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 the launcher and the Triage host call env() without one). +// (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. +// Should they ever differ, env() still runs the fit check on what it found and 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 (cached from preflight). -- 2.54.0 From 2613fb1c4cb17fb46c05cdfea3f20db3a86bd530 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 16:00:15 -0600 Subject: [PATCH 07/16] fix(pi): pi-unpin S3 re-check: the control counts a kind only through a source the sealed runs can show, so a SYSTEM.md no longer stands in for the project APPEND_SYSTEM.md or an AGENTS.md (#1873) --- packages/harness/README.md | 8 ++++++-- packages/harness/src/pi-fit.mjs | 19 ++++++++++++++----- packages/harness/tests/pi-fit.test.mjs | 4 ++++ packages/harness/tests/pi-stand-in.mjs | 5 +++-- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/harness/README.md b/packages/harness/README.md index dc7787413..81293f679 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -33,7 +33,8 @@ empty environment and a scratch home, and calls no model. 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, in rpc and in `-p`: extensions, MCP +unloaded with the seal flags on (skills and prompt templates are read +from `get_commands`, so in rpc only; the rest in rpc and 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 @@ -43,7 +44,10 @@ extension writes the system prompt it sees at `session_start`; the sealed runs must show the `--system-prompt` file and none of the planted files. A control run without the seal flags must show each kind of source (an MCP server only when `--help` lists `--no-mcp`) and answer -`get_state` and `get_commands`, or the check can't tell and refuses. Pi +`get_state` and `get_commands`, or the check can't tell and refuses. A +`SYSTEM.md` doesn't count for its kind there, since the sealed runs' +`--system-prompt` replaces it: the control must show an `AGENTS.md` or +`CLAUDE.md` and the project's `.pi/APPEND_SYSTEM.md`. 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, diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 73cc700fc..a7941700c 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -9,14 +9,18 @@ // control rpc, planted ambient sources, a saved trust for the project, no seal flags and // one -e extension that writes the system prompt it sees at session_start. The // binary answers get_state and get_commands, and shows each kind of planted source: -// an extension, a skill, a prompt template, a context file, a trusted project file -// (the trust --no-approve must undo), and an MCP server when --help lists --no-mcp. -// A kind it doesn't show could not be told apart from a sealed one. +// an extension, a skill, a prompt template, a context file (AGENTS.md or CLAUDE.md), +// 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. A +// SYSTEM.md doesn't count, since the sealed runs' --system-prompt replaces it. 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. // sealed -p the same in -p, the mode the adapter runs. Its -e extension must reach -// session_start, and no planted source may show. +// session_start, and no planted extension, MCP server, context file or trusted +// project file may show. -p answers no get_commands, so skills and prompt templates +// are checked in rpc only. // 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 // @@ -102,7 +106,9 @@ function check(found, { levels, bwrap, node, scratch, who, digest }) { if (!controlAnswer.state) return refuse("can't be checked: it didn't answer get_state"); if (!controlAnswer.commands) return refuse("can't be checked: it didn't answer get_commands"); if (!marked(scratch, EXPLICIT) || prompted(scratch) === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags"); - const shown = new Set(seen(scratch, controlAnswer).map((s) => s.kind)); + // A kind counts only through a source the sealed runs could show: they pass --system-prompt, + // which replaces a SYSTEM.md, so a SYSTEM.md seen here proves nothing about the seal. + const shown = new Set(seen(scratch, controlAnswer).filter((s) => !REPLACED.has(s.source)).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`); @@ -220,6 +226,9 @@ const TEXTS = [ ["project/CLAUDE.md", "FITPROJECTCLAUDE", "project CLAUDE.md", "context file"], ["CLAUDE.md", "FITPARENTCLAUDE", "parent CLAUDE.md", "context file"], ]; +// Sources the sealed runs' --system-prompt replaces. They stay planted, so a leak still refuses, +// but the control doesn't count them toward their kind. +const REPLACED = new Set(["user SYSTEM.md", "project SYSTEM.md"]); // The --system-prompt file's word, which the sealed runs must show. const GIVEN = "FITGIVENPROMPT"; // The -e extension the control and sealed runs pass, which must load. diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 2059ba9ec..6476814ed 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -114,6 +114,10 @@ test("pi-unpin S3 review: a get_commands answer that fails or changes shape is r // 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, t => { + assert.equal(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 review: a stand-in that drops the --system-prompt file, or runs no session_start in -p, is refused", SKIP, t => { assert.equal(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"); assert.equal(fit(standIn(t, { printNoStart: true }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: a -p start with the seal flags didn't run its -e extension"); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index 374132140..849051c77 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -20,7 +20,8 @@ // session_start. // `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. `commands` "fail" answers +// `ignoreSystemPrompt` drops the --system-prompt file. `ignoreAppend` drops every +// APPEND_SYSTEM.md. `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. // @@ -111,7 +112,7 @@ if (rpc || print) { // context files, from the agent directory and from / down to the working directory. const given = K.ignoreSystemPrompt ? null : val("--system-prompt"); const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!K.ignoreAll && read(path.join(agent, name))) || ""; - const parts = [given ? (read(given) || "") : own("SYSTEM.md"), own("APPEND_SYSTEM.md")]; + const parts = [given ? (read(given) || "") : own("SYSTEM.md"), K.ignoreAppend ? "" : own("APPEND_SYSTEM.md")]; if (!K.ignoreAll && (!has("--no-context-files") || leak.has("context"))) { const dirs = []; for (let d = cwd; ; d = path.dirname(d)) { dirs.unshift(d); if (path.dirname(d) === d) break; } -- 2.54.0 From d009ccaec41480a63ba6e28fa5f3e85fe596db12 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 16:18:54 -0600 Subject: [PATCH 08/16] fix(pi): pi-unpin S3 re-check 2: the control passes the sealed runs' -e and --system-prompt so the runs differ only by the seal flags, a project source no longer shows a kind, a run over 4 MB says so, and --version gets 3 s (#1873) --- packages/harness/README.md | 16 ++++--- packages/harness/src/pi-fit.mjs | 61 +++++++++++++++----------- packages/harness/tests/pi-fit.test.mjs | 29 ++++++++++++ packages/harness/tests/pi-stand-in.mjs | 43 +++++++++++++++--- 4 files changed, 111 insertions(+), 38 deletions(-) diff --git a/packages/harness/README.md b/packages/harness/README.md index 81293f679..123343e7a 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -42,12 +42,16 @@ 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 it sees at `session_start`; the sealed runs must show the `--system-prompt` file and none of the planted -files. A control run without the seal flags must show each kind of -source (an MCP server only when `--help` lists `--no-mcp`) and answer -`get_state` and `get_commands`, or the check can't tell and refuses. A -`SYSTEM.md` doesn't count for its kind there, since the sealed runs' -`--system-prompt` replaces it: the control must show an `AGENTS.md` or -`CLAUDE.md` and the project's `.pi/APPEND_SYSTEM.md`. Pi +files. A control run differs from the sealed runs only by the seal +flags (it keeps `--no-session` and `--offline`, and passes 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`, 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. 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, diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index a7941700c..51d027b44 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -6,14 +6,15 @@ // --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, no seal flags and +// control rpc, planted ambient sources, a saved trust for the project, no seal flags but +// --no-session and --offline, the --system-prompt file the sealed runs pass, and // one -e extension that writes the system prompt it sees at session_start. The // binary answers get_state and get_commands, and shows each kind of planted source: -// an extension, a skill, a prompt template, a context file (AGENTS.md or CLAUDE.md), -// 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. A -// SYSTEM.md doesn't count, since the sealed runs' --system-prompt replaces it. A kind -// it doesn't show could not be told apart from a sealed one. +// 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. @@ -24,8 +25,9 @@ // 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 // -// A check that cannot tell refuses: a sandbox that fails, a run that times out, an answer that is -// missing or doesn't parse, a kind of source the control doesn't show. A pass is cached in memory +// 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. 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) and the mount layout. @@ -46,6 +48,8 @@ export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", " // 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"]; 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; const DIGEST_FILES_MAX = 20000; @@ -77,10 +81,10 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa } function check(found, { levels, bwrap, node, scratch, who, digest }) { - const run = (args, input = "") => runIn(found, { bwrap, node, scratch, args, input }); + const run = (args, input = "", timeout = RUN_TIMEOUT) => runIn(found, { bwrap, node, scratch, args, input, timeout }); const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` }); plant(scratch); - const v = run(["--version"]); + const v = run(["--version"], "", VERSION_TIMEOUT); const version = v.status === 0 ? versionLabel(v.stdout) : null; const help = run(["--help"]); if (help.status !== 0) return refuse("can't be checked: --help failed"); @@ -94,27 +98,33 @@ function check(found, { levels, bwrap, node, scratch, who, digest }) { 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 run without the seal flags and the + // sealed runs differ only by the seal flags. --no-session and --offline are seal flags the + // control keeps: it must not save a session or reach a provider either. const explicit = ["-e", join(scratch, "explicit", "fit.ts")]; - const sealed = [...SEAL, ...(noMcp ? ["--no-mcp"] : []), ...explicit, "--system-prompt", join(scratch, "prompt.md")]; + const given = ["--system-prompt", join(scratch, "prompt.md")]; + const seal = [...SEAL, ...(noMcp ? ["--no-mcp"] : [])]; + const sealed = [...seal, ...explicit, ...given]; // -p takes a message; offline with no key, pi stops before any model call. const print = ["-p", ...sealed, "fit check"]; plant(scratch); - const control = run(["--mode", "rpc", "--no-session", "--offline", ...explicit], ask); - if (control.timedOut) return refuse("can't be checked: the start without the seal flags timed out"); + const control = run(["--mode", "rpc", "--no-session", "--offline", ...explicit, ...given], ask); + if (control.stopped) return refuse(`can't be checked: the start without the seal flags ${control.stopped}`); const controlAnswer = answered(control.stdout); if (!controlAnswer.state) return refuse("can't be checked: it didn't answer get_state"); if (!controlAnswer.commands) return refuse("can't be checked: it didn't answer get_commands"); if (!marked(scratch, EXPLICIT) || prompted(scratch) === 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 the sealed runs could show: they pass --system-prompt, - // which replaces a SYSTEM.md, so a SYSTEM.md seen here proves nothing about the seal. - const shown = new Set(seen(scratch, controlAnswer).filter((s) => !REPLACED.has(s.source)).map((s) => s.kind)); + // 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(seen(scratch, controlAnswer).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`); plant(scratch); const rpc = run(["--mode", "rpc", ...sealed], ask); - if (rpc.timedOut) return refuse("can't be checked: the start with the seal flags timed out"); + if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`); const rpcAnswer = answered(rpc.stdout); if (!rpcAnswer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on"); if (!rpcAnswer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on"); @@ -127,7 +137,7 @@ function check(found, { levels, bwrap, node, scratch, who, digest }) { plant(scratch); const p = run(print); - if (p.timedOut) return refuse("can't be checked: a -p start with the seal flags timed out"); + if (p.stopped) return refuse(`can't be checked: a -p start with the seal flags ${p.stopped}`); if (prompted(scratch) === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension"); leaked = seen(scratch, { commands: [] }); if (leaked.length) return refuse(`loaded a planted ${leaked.map((s) => s.source).join(", ")} with the seal flags on in -p`); @@ -136,8 +146,8 @@ function check(found, { levels, bwrap, node, scratch, who, digest }) { // one after the missing one, then exits 1 before session_start, so only session_start tells. for (const [mode, args, input] of [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]]) { plant(scratch); - const r = run([...args, ...SEAL, "-e", join(scratch, "missing", "extension.ts"), ...explicit, "--system-prompt", join(scratch, "prompt.md"), ...(mode === "-p" ? ["fit check"] : [])], input); - if (r.timedOut) return refuse(`can't be checked: a ${mode} start with a missing -e extension timed out`); + const r = run([...args, ...seal, "-e", join(scratch, "missing", "extension.ts"), ...explicit, ...given, ...(mode === "-p" ? ["fit check"] : [])], input); + if (r.stopped) return refuse(`can't be checked: a ${mode} start with a missing -e extension ${r.stopped}`); if (r.status === 0 || answered(r.stdout).state || prompted(scratch) !== null) return refuse("starts with a missing -e extension"); } return { ok: true, version, noMcp, digest }; @@ -146,15 +156,17 @@ function check(found, { levels, bwrap, node, scratch, who, digest }) { // The binary, inside the sandbox: the system part, the found package where a session sees it, and // the scratch directory read-write. The environment holds HOME, the agent directory, TMPDIR, a // PATH that finds node, and PI_OFFLINE. -function runIn(found, { bwrap, node, scratch, args, input }) { +// stopped: why the run was killed ("timed out", "printed more than 4 MB"), or null. +function runIn(found, { bwrap, node, scratch, args, input, timeout }) { const list = [ { kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) }, { kind: "rw", src: scratch }, ]; const env = { HOME: join(scratch, "home"), PI_CODING_AGENT_DIR: join(scratch, "agent"), TMPDIR: join(scratch, "tmp"), PATH: `${dirname(node)}:/usr/bin:/bin`, PI_OFFLINE: "1" }; const argv = checkArgv(list, [found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap }); - const r = spawnSync(argv[0], argv.slice(1), { input, encoding: "utf8", timeout: RUN_TIMEOUT, killSignal: "SIGKILL", maxBuffer: OUTPUT_MAX, env: {} }); - return { status: r.status, stdout: typeof r.stdout === "string" ? r.stdout : "", timedOut: r.error?.code === "ETIMEDOUT" || r.signal === "SIGKILL" }; + const r = spawnSync(argv[0], argv.slice(1), { input, encoding: "utf8", timeout, killSignal: "SIGKILL", maxBuffer: OUTPUT_MAX, env: {} }); + const stopped = r.error?.code === "ENOBUFS" ? `printed more than ${OUTPUT_MAX / 1024 / 1024} MB` : r.error?.code === "ETIMEDOUT" || r.signal === "SIGKILL" ? "timed out" : null; + return { status: r.status, stdout: typeof r.stdout === "string" ? r.stdout : "", stopped }; } // The options one help line lists in its option column: a line that starts with "-" (after its @@ -226,9 +238,6 @@ const TEXTS = [ ["project/CLAUDE.md", "FITPROJECTCLAUDE", "project CLAUDE.md", "context file"], ["CLAUDE.md", "FITPARENTCLAUDE", "parent CLAUDE.md", "context file"], ]; -// Sources the sealed runs' --system-prompt replaces. They stay planted, so a leak still refuses, -// but the control doesn't count them toward their kind. -const REPLACED = new Set(["user SYSTEM.md", "project SYSTEM.md"]); // The --system-prompt file's word, which the sealed runs must show. const GIVEN = "FITGIVENPROMPT"; // The -e extension the control and sealed runs pass, which must load. diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 6476814ed..272ff7b47 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -118,6 +118,35 @@ test("pi-unpin S3 re-check: a project SYSTEM.md doesn't stand in for the APPEND_ assert.equal(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, t => { + assert.equal(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"); +}); + +test("pi-unpin S3 re-check 2: the run without the seal flags and the sealed runs differ only by the seal flags", SKIP, t => { + const plain = fit(standIn(t, { sameRuns: true }).env); + assert.equal(plain.ok, true, plain.message); + const withMcp = fit(standIn(t, { sameRuns: true, extraFlags: ["--no-mcp"] }).env); + assert.equal(withMcp.ok, true, withMcp.message); +}); + +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, t => { + assert.equal(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(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, t => { + assert.equal(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, t => { + const start = Date.now(); + const r = 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, t => { assert.equal(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"); assert.equal(fit(standIn(t, { printNoStart: true }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: a -p start with the seal flags didn't run its -e extension"); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index 849051c77..f572c95d0 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -3,7 +3,8 @@ // 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 +// --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: @@ -21,7 +22,13 @@ // `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. `commands` "fail" answers +// 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. +// `sameRuns`: each run strips its seal flags (--offline and every --no-*), its mode +// and its -p message, and exits 3 when that differs from the first run's. `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. // @@ -61,6 +68,25 @@ for (let i = 0; i < argv.length; i++) { } } 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; } + } + } + if (K.sameRuns) { + const rest = []; + for (let i = 0; i < argv.length; i++) { + if (argv[i] === "--mode") { i++; continue; } + if (argv[i] === "-p" || argv[i] === "--offline" || argv[i].startsWith("--no-")) continue; + if (print && i === argv.length - 1) continue; + rest.push(argv[i]); + } + const log = path.join(path.dirname(path.dirname(val("-e"))), "runs.log"); + const first = fs.existsSync(log) ? fs.readFileSync(log, "utf8") : null; + if (first === null) fs.writeFileSync(log, JSON.stringify(rest)); + else if (first !== JSON.stringify(rest)) process.exit(3); + } const leak = new Set(K.sealLeak || []); const on = (kind, flag) => !K.ignoreAll && (!has(flag) || leak.has(kind)); const agent = process.env.PI_CODING_AGENT_DIR || path.join(process.env.HOME || "/", ".pi", "agent"); @@ -86,7 +112,9 @@ if (rpc || print) { }; if (!(K.dropExplicit && has("--no-extensions"))) for (let i = 0; i < argv.length; i++) if (argv[i] === "-e") load(argv[i + 1]); const packages = settings.flatMap((s) => (s.packages || []).map((p) => typeof p === "string" ? p : p.source)); - if (on("extensions", "--no-extensions")) { + 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"))); @@ -112,8 +140,11 @@ if (rpc || print) { // context files, from the agent directory and from / down to the working directory. const given = K.ignoreSystemPrompt ? null : val("--system-prompt"); const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!K.ignoreAll && read(path.join(agent, name))) || ""; - const parts = [given ? (read(given) || "") : own("SYSTEM.md"), K.ignoreAppend ? "" : own("APPEND_SYSTEM.md")]; - if (!K.ignoreAll && (!has("--no-context-files") || leak.has("context"))) { + const hide = K.sysReplacesAll && given; + const parts = [given ? (read(given) || "") : 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 && !K.ignoreAll && (!has("--no-context-files") || leak.has("context"))) { 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")) || ""); @@ -145,7 +176,7 @@ 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.versionExit != null ? `exit ${Number(knobs.versionExit)}` : `printf '%s\\n' ${quote(knobs.version ?? "9.9.9")}; exit 0`; + 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", -- 2.54.0 From fa5ce3ae6594c08b2dcb62ea397fa17e47fb8dd0 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 16:48:23 -0600 Subject: [PATCH 09/16] fix(pi): pi-unpin S3 re-check 3: the control passes --no-tools and --no-themes, one sealed run per loader flag leaves only that flag out and its kind must show, every run gets a fresh scratch directory, and the README says what argv the check runs (#1873) --- packages/harness/README.md | 26 ++++-- packages/harness/src/pi-fit.mjs | 123 +++++++++++++++---------- packages/harness/tests/pi-fit.test.mjs | 70 ++++++++++++-- packages/harness/tests/pi-stand-in.mjs | 50 +++++----- 4 files changed, 182 insertions(+), 87 deletions(-) diff --git a/packages/harness/README.md b/packages/harness/README.md index 123343e7a..4d1ed13ce 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -42,14 +42,24 @@ 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 it sees at `session_start`; the sealed runs must show the `--system-prompt` file and none of the planted -files. A control run differs from the sealed runs only by the seal -flags (it keeps `--no-session` and `--offline`, and passes 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`, 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. A run killed for taking +files. 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`, 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, and +its kind must show. 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. Each +run gets a fresh scratch directory, so no run sees what another left. +The fit runs do not pass the flags production adds (`--provider`, +`--model`, `--mode json`, `--session-dir`); a pi built to act one way +without them and another way with them 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 diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 51d027b44..d383f955c 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -6,8 +6,10 @@ // --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, no seal flags but -// --no-session and --offline, the --system-prompt file the sealed runs pass, and +// 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 the system prompt it sees at session_start. The // binary answers get_state and get_commands, and shows each kind of planted source: // an extension, a skill, a prompt template, a context file, a trusted project file @@ -18,6 +20,9 @@ // 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. 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. // sealed -p the same in -p, the mode the adapter runs. Its -e extension must reach // session_start, and no planted extension, MCP server, context file or trusted // project file may show. -p answers no get_commands, so skills and prompt templates @@ -25,9 +30,13 @@ // 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. Each run passes the same -e and --system-prompt; runs differ only by their seal +// flags, their mode, the -p message and the missing -e file. +// // 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. A --version that fails or hangs past 3 s only leaves no label. A pass is cached in memory +// show, or the kind a one-out run should show. 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) and the mount layout. @@ -35,7 +44,9 @@ // 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. +// check doesn't plant one; a session's agent directory is the run's own. The runs don't pass the +// flags a session adds (--provider, --model, --mode json, --session-dir), so a pi built to act +// differently with them is outside what this check can catch. import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; @@ -47,6 +58,8 @@ import { BWRAP, checkArgv, sandboxProblem } from "../../seat/src/sandbox.mjs"; 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"]]; const RUN_TIMEOUT = 20000; // --version gives only a label, so a hang costs this much and leaves no label. const VERSION_TIMEOUT = 3000; @@ -70,21 +83,29 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa // The sandbox's own error text can name a host path, so the line keeps only what failed. const problem = sandboxProblem({ bwrap }); if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem.startsWith("the sandbox (bwrap) is missing") ? "the sandbox (bwrap) is missing" : "the sandbox (bwrap) failed"}` }; - const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-")); - try { - const result = check(found, { levels, bwrap, node, scratch, who, digest }); - if (result.ok) passed.set(key, result); - return result; - } finally { - rmSync(scratch, { recursive: true, force: true }); - } + const result = check(found, { levels, bwrap, node, who, digest }); + if (result.ok) passed.set(key, result); + return result; } -function check(found, { levels, bwrap, node, scratch, who, digest }) { - const run = (args, input = "", timeout = RUN_TIMEOUT) => runIn(found, { bwrap, node, scratch, args, input, timeout }); +function check(found, { levels, bwrap, node, who, digest }) { + // args: the arguments, or a function of the run's scratch directory that returns them. The + // answer is read before the scratch directory goes: the rpc answers, whether the -e extension + // loaded, the system prompt it saw, and the planted sources that showed (commands: false leaves + // get_commands out, for -p). + const run = (args, input = "", { timeout = RUN_TIMEOUT, commands = true } = {}) => { + const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-")); + try { + plant(scratch); + const r = runIn(found, { bwrap, node, scratch, args: typeof args === "function" ? args(scratch) : args, input, timeout }); + const answer = answered(r.stdout); + return { ...r, answer, explicit: marked(scratch, EXPLICIT), prompt: prompted(scratch), seen: seen(scratch, commands ? answer : { commands: [] }) }; + } finally { + rmSync(scratch, { recursive: true, force: true }); + } + }; const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` }); - plant(scratch); - const v = run(["--version"], "", VERSION_TIMEOUT); + const v = run(["--version"], "", { timeout: VERSION_TIMEOUT }); const version = v.status === 0 ? versionLabel(v.stdout) : null; const help = run(["--help"]); if (help.status !== 0) return refuse("can't be checked: --help failed"); @@ -98,57 +119,58 @@ function check(found, { levels, bwrap, node, scratch, who, digest }) { 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 run without the seal flags and the - // sealed runs differ only by the seal flags. --no-session and --offline are seal flags the - // control keeps: it must not save a session or reach a provider either. - const explicit = ["-e", join(scratch, "explicit", "fit.ts")]; - const given = ["--system-prompt", join(scratch, "prompt.md")]; + // 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 sealed = [...seal, ...explicit, ...given]; - // -p takes a message; offline with no key, pi stops before any model call. - const print = ["-p", ...sealed, "fit check"]; + 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); - plant(scratch); - const control = run(["--mode", "rpc", "--no-session", "--offline", ...explicit, ...given], ask); + // 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 control = rpcRun(without(...loaders.map(([f]) => f))); if (control.stopped) return refuse(`can't be checked: the start without the seal flags ${control.stopped}`); - const controlAnswer = answered(control.stdout); - if (!controlAnswer.state) return refuse("can't be checked: it didn't answer get_state"); - if (!controlAnswer.commands) return refuse("can't be checked: it didn't answer get_commands"); - if (!marked(scratch, EXPLICIT) || prompted(scratch) === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags"); + 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(seen(scratch, controlAnswer).filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind)); + 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`); - plant(scratch); - const rpc = run(["--mode", "rpc", ...sealed], ask); + const rpc = rpcRun(seal); if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`); - const rpcAnswer = answered(rpc.stdout); - if (!rpcAnswer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on"); - if (!rpcAnswer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on"); - if (!marked(scratch, EXPLICIT)) return refuse("didn't load an -e extension with the seal flags on"); - const rpcPrompt = prompted(scratch); - if (rpcPrompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on"); - if (!rpcPrompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on"); - let leaked = seen(scratch, rpcAnswer); - if (leaked.length) return refuse(`loaded a planted ${leaked.map((s) => s.source).join(", ")} with the seal flags on`); + 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`); - plant(scratch); - const p = run(print); + // Each loader flag on its own: with every other seal flag on, its kind must show. + for (const [flag, kind] of loaders) { + const r = rpcRun(without(flag)); + 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}`); + } + + // -p takes a message; offline with no key, pi stops before any model call. + const p = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { commands: false }); if (p.stopped) return refuse(`can't be checked: a -p start with the seal flags ${p.stopped}`); - if (prompted(scratch) === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension"); - leaked = seen(scratch, { commands: [] }); - if (leaked.length) return refuse(`loaded a planted ${leaked.map((s) => s.source).join(", ")} with the seal flags on in -p`); + 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((s) => s.source).join(", ")} with the seal flags on in -p`); // 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. for (const [mode, args, input] of [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]]) { - plant(scratch); - const r = run([...args, ...seal, "-e", join(scratch, "missing", "extension.ts"), ...explicit, ...given, ...(mode === "-p" ? ["fit check"] : [])], input); + const r = run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input); if (r.stopped) return refuse(`can't be checked: a ${mode} start with a missing -e extension ${r.stopped}`); - if (r.status === 0 || answered(r.stdout).state || prompted(scratch) !== null) return refuse("starts with a missing -e extension"); + if (r.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension"); } return { ok: true, version, noMcp, digest }; } @@ -248,7 +270,6 @@ const marked = (scratch, source) => { try { return readdirSync(join(scratch, "ma const prompted = (scratch) => { try { return readFileSync(join(scratch, "marks", "system-prompt"), "utf8"); } catch { return null; } }; function plant(scratch) { - for (const d of ["home", "agent", "project", "marks", "tmp", "missing", "explicit", "listed"]) rmSync(join(scratch, d), { recursive: true, force: true }); 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`; diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 272ff7b47..54b6c7ccf 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -13,7 +13,7 @@ import { piFit } 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 { sandboxProblem } from "../../seat/src/sandbox.mjs"; +import { BWRAP, sandboxProblem } from "../../seat/src/sandbox.mjs"; const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url))); const NAME = "@earendil-works/pi-coding-agent"; @@ -122,11 +122,69 @@ test("pi-unpin S3 re-check 2: the run without the seal flags passes --system-pro assert.equal(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"); }); -test("pi-unpin S3 re-check 2: the run without the seal flags and the sealed runs differ only by the seal flags", SKIP, t => { - const plain = fit(standIn(t, { sameRuns: true }).env); - assert.equal(plain.ok, true, plain.message); - const withMcp = fit(standIn(t, { sameRuns: true, extraFlags: ["--no-mcp"] }).env); - assert.equal(withMcp.ok, true, withMcp.message); +// 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 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, 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\nfor a in "$@"; do printf '%s\\037' "$a"; done >> '${log}'\nprintf '\\n' >> '${log}'\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 }); + const found = locate(s.env); + const r = piFit(found, { levels: LEVELS, bwrap }); + assert.equal(r.ok, true, r.message); + const runs = readFileSync(log, "utf8").split("\n").filter(Boolean).map((l) => l.split("\x1f").slice(0, -1)).filter((a) => a.includes(found.view)).map((a) => { + const scratch = a[a.indexOf("--chdir") + 1].replace(/\/project$/, ""); + return { scratch, args: a.slice(a.indexOf(found.view) + 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, -p, two missing -e. + assert.equal(runs.length, 2 + 2 + loaders.length + 1 + 2); + assert.equal(new Set(runs.map((x) => x.scratch)).size, runs.length, "each run has its own scratch directory"); + const flagLists = []; + for (const { args } of runs.slice(2)) { + 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, [seal.filter((f) => !loaders.includes(f)), seal, ...loaders.map((l) => seal.filter((f) => f !== l))].map((f) => f.join(" "))); + } +}); + +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, t => { + for (const cue of ["--no-tools", "--no-themes", "order"]) { + assert.equal(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, 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}`; + assert.equal(fit(standIn(t, { standIn: ["--no-approve", "--no-extensions"] }).env).message, but("trusted project file", "--no-approve")); + for (const [flag, kind] of [["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]]) { + assert.equal(fit(standIn(t, { standIn: [flag, "--no-approve"] }).env).message, but(kind, flag), flag); + } + assert.equal(fit(standIn(t, { extraFlags: ["--no-mcp"], standIn: ["--no-mcp", "--no-approve"] }).env).message, but("MCP server", "--no-mcp")); + // Re-check 3's dispatchers: a pi that hides everything whenever one always-passed loader flag is on. + assert.equal(fit(standIn(t, { hideWhen: "--no-approve" }).env).message, but("extension", "--no-extensions")); + assert.equal(fit(standIn(t, { hideWhen: "--no-extensions" }).env).message, but("trusted project file", "--no-approve")); +}); + +test("pi-unpin S3 re-check 3: a pi that remembers the control in its scratch directory and hides later is refused", SKIP, t => { + assert.match(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, t => { diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index f572c95d0..2270e209e 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -18,7 +18,8 @@ // (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. +// 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 @@ -27,9 +28,13 @@ // 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. -// `sameRuns`: each run strips its seal flags (--offline and every --no-*), its mode -// and its -p message, and exits 3 when that differs from the first run's. `commands` "fail" answers -// get_commands with success false; "list" answers with a bare list. +// `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. `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 @@ -74,21 +79,22 @@ if (rpc || print) { try { off += fs.writeSync(1, chunk, off); } catch (e) { if (e.code !== "EAGAIN") throw e; } } } - if (K.sameRuns) { - const rest = []; - for (let i = 0; i < argv.length; i++) { - if (argv[i] === "--mode") { i++; continue; } - if (argv[i] === "-p" || argv[i] === "--offline" || argv[i].startsWith("--no-")) continue; - if (print && i === argv.length - 1) continue; - rest.push(argv[i]); - } - const log = path.join(path.dirname(path.dirname(val("-e"))), "runs.log"); - const first = fs.existsSync(log) ? fs.readFileSync(log, "utf8") : null; - if (first === null) fs.writeFileSync(log, JSON.stringify(rest)); - else if (first !== JSON.stringify(rest)) process.exit(3); + const ALL = ["extensions", "mcp", "skills", "prompts", "context", "approve"]; + 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); } - const leak = new Set(K.sealLeak || []); - const on = (kind, flag) => !K.ignoreAll && (!has(flag) || leak.has(kind)); + 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); } + } + // Whether a seal flag is on: with standIn, its flag reads as the other flag. + const sealed = (flag) => has(K.standIn && K.standIn[0] === flag ? K.standIn[1] : flag); + 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 []; } }; @@ -97,7 +103,7 @@ if (rpc || print) { 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 = !K.ignoreAll && (has("--approve") || (saved && (!has("--no-approve") || leak.has("approve")))); + 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 the system // prompt at session_start. Do what each would do. @@ -120,7 +126,7 @@ if (rpc || print) { for (const p of packages) files.push(...list(path.join(p, "extensions"), (n) => n.endsWith(".ts"))); for (const f of files) load(f); } - if (on("mcp", "--no-extensions") && (!has("--no-mcp") || leak.has("mcp"))) { + if (on("mcp", (K.extraFlags || []).includes("--no-mcp") ? "--no-mcp" : "--no-extensions")) { 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" }); } @@ -139,12 +145,12 @@ if (rpc || print) { // 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 given = K.ignoreSystemPrompt ? null : val("--system-prompt"); - const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!K.ignoreAll && read(path.join(agent, name))) || ""; + const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!ignoreAll && read(path.join(agent, name))) || ""; const hide = K.sysReplacesAll && given; const parts = [given ? (read(given) || "") : 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 && !K.ignoreAll && (!has("--no-context-files") || leak.has("context"))) { + } 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")) || ""); -- 2.54.0 From 749d6d98f57ce7f2660bd2a64029947603f7c8ec Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 17:27:15 -0600 Subject: [PATCH 10/16] fix(pi): pi-unpin S3 re-check 4: one sealed run in a bus session's shape, built by sessionShape, which a test holds to the adapter's argv; a one-out run must show only its own kind; the README and header list what a session passes that no fit run does (#1873) --- packages/harness/README.md | 29 ++++++--- packages/harness/src/pi-fit.mjs | 60 ++++++++++++++++--- packages/harness/tests/pi-fit.test.mjs | 81 ++++++++++++++++++++++---- packages/harness/tests/pi-stand-in.mjs | 10 +++- 4 files changed, 151 insertions(+), 29 deletions(-) diff --git a/packages/harness/README.md b/packages/harness/README.md index 4d1ed13ce..0a0da9769 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -52,14 +52,27 @@ and answer `get_state` and `get_commands`, 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, and -its kind must show. 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. Each -run gets a fresh scratch directory, so no run sees what another left. -The fit runs do not pass the flags production adds (`--provider`, -`--model`, `--mode json`, `--session-dir`); a pi built to act one way -without them and another way with them is outside what a start-time -check can catch. A run killed for taking +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. One +more sealed rpc run has a bus session's shape: the arguments the adapter +passes for a session with tools and a fresh persistent session, in its +order, from `sessionShape` in `src/pi-fit.mjs`, which a test holds to +the adapter's real argv. That is `--tools read` in place of +`--no-tools`, `--session-dir` in place of `--no-session`, a fixed +`--provider` and `--model`, and `-e` right after `--no-extensions`. It +must show no planted source. Each run gets a fresh scratch directory, so +no run sees what another left. What a session can pass that no fit run +does: `--mode json` (the model event monitor) or `-p` with a request in +the session's shape (that run is rpc), `--thinking`, `-c` (a session +directory that already holds a session), `--fork`, `--skill` with +`--no-skills` dropped (worker runs only), more than one `-e`, a tool +list other than `read`, its own provider and model, and a provider key. +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 diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index d383f955c..ed8bcfa9a 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -21,8 +21,16 @@ // --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. 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. +// 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 one more sealed rpc run in a bus session's shape, with the arguments +// adapters/pi/adapter.sh passes for a session with tools and a fresh persistent +// session, in its order (sessionShape below; a test holds it to the adapter's argv): +// --tools read in place of --no-tools, --session-dir in place of --no-session, a fixed +// --provider and --model, and -e right after --no-extensions. It answers both, loads the +// -e extension, uses the --system-prompt file, and shows no planted source. // sealed -p the same in -p, the mode the adapter runs. Its -e extension must reach // session_start, and no planted extension, MCP server, context file or trusted // project file may show. -p answers no get_commands, so skills and prompt templates @@ -31,12 +39,13 @@ // 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. Each run passes the same -e and --system-prompt; runs differ only by their seal -// flags, their mode, the -p message and the missing -e file. +// another one left. 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. // // 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 --version that fails or hangs past 3 s only leaves no label. A pass is cached in memory +// 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) and the mount layout. @@ -44,9 +53,13 @@ // 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. The runs don't pass the -// flags a session adds (--provider, --model, --mode json, --session-dir), so a pi built to act -// differently with them is outside what this check can catch. +// check doesn't plant one; a session's agent directory is the run's own. What a session can pass +// that no run does: --mode json (the model event monitor) or -p with a request in the session's +// shape (that run is rpc), --thinking, -c (a session directory that already holds a session), +// --fork, --skill with --no-skills dropped (worker runs only), more than one -e, a tool list +// other than read, its own provider and model, and a provider key. 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 { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; @@ -60,6 +73,22 @@ export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", " 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 run's fixed tools, provider and model. Offline with no key, pi stops before +// any model call. +export const SESSION_SHAPE = Object.freeze({ tools: "read", provider: "anthropic", model: "claude-sonnet-4-5" }); +// The arguments adapters/pi/adapter.sh builds for a bus session with tools (SESSION_SHAPE.tools), +// a fresh persistent session directory, the given -e files and --system-prompt file, no --thinking +// and no --skill, less the -p and request it appends. Keep its order: pi-fit.test.mjs runs the +// adapter and fails when the two differ. +export function sessionShape({ noMcp, extensions, prompt, sessionDir }) { + 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", SESSION_SHAPE.tools, "--session-dir", sessionDir, + "--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model, + "--system-prompt", prompt, + ]; +} const RUN_TIMEOUT = 20000; // --version gives only a label, so a hang costs this much and leaves no label. const VERSION_TIMEOUT = 3000; @@ -157,8 +186,21 @@ function check(found, { levels, bwrap, node, who, digest }) { 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}`); } + // A bus session's shape: every session passes --tools, and a persistent one --session-dir, so a + // seal that holds only with --no-tools or --no-session, or only in the order above, shows here. + const shaped = run((s) => [...sessionShape({ noMcp, extensions: [join(s, "explicit", "fit.ts")], prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions") }), "--mode", "rpc"], ask); + const shape = "in a session's shape"; + if (shaped.stopped) return refuse(`can't be checked: the start ${shape} ${shaped.stopped}`); + if (!shaped.answer.state || !shaped.answer.commands) return refuse(`can't be checked: it didn't answer get_state and get_commands ${shape}`); + if (!shaped.explicit) return refuse(`didn't load an -e extension ${shape}`); + if (shaped.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${shape}`); + if (!shaped.prompt.includes(GIVEN)) return refuse(`didn't use the --system-prompt file ${shape}`); + if (shaped.seen.length) return refuse(`loaded a planted ${shaped.seen.map((s) => s.source).join(", ")} with the seal flags ${shape}`); + // -p takes a message; offline with no key, pi stops before any model call. const p = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { commands: false }); if (p.stopped) return refuse(`can't be checked: a -p start with the seal flags ${p.stopped}`); @@ -281,6 +323,8 @@ function plant(scratch) { mkdirSync(marks, { recursive: true }); mkdirSync(join(scratch, "tmp"), { recursive: true }); mkdirSync(project, { recursive: true }); + // The session-shaped run's --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) => { diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 54b6c7ccf..50e1f083f 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -9,7 +9,7 @@ import { spawnSync } from "node:child_process"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; -import { piFit } from "../src/pi-fit.mjs"; +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"; @@ -38,6 +38,7 @@ function standIn(t, knobs, version = "2.0.0") { return { env, root, cli: join(pkg, "dist", "bundle", "cli.js"), paths: [root, realpathSync(root), tmpdir(), REPO] }; } +const { piFit } = fitModule; const fit = (env) => piFit(locate(env), { levels: LEVELS }); // 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. @@ -144,11 +145,16 @@ test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the r }); const seal = [...SEAL, ...extraFlags]; const loaders = [...LOADERS, ...extraFlags]; - // --version, --help, the control, the sealed rpc, one per loader flag, -p, two missing -e. - assert.equal(runs.length, 2 + 2 + loaders.length + 1 + 2); + // --version, --help, the control, the sealed rpc, one per loader flag, the session-shaped run, + // -p, two missing -e. + assert.equal(runs.length, 2 + 2 + loaders.length + 1 + 1 + 2); assert.equal(new Set(runs.map((x) => x.scratch)).size, runs.length, "each run has its own scratch directory"); + // Re-check 4 item 1: the session-shaped run comes after the one-out runs, built by the same + // function the adapter test below holds to the adapter's argv, with rpc at the end. + const at = 2 + 2 + loaders.length; + assert.deepEqual(runs[at].args, [...fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: ["/explicit/fit.ts"], prompt: "/prompt.md", sessionDir: "/sessions" }), "--mode", "rpc"]); const flagLists = []; - for (const { args } of runs.slice(2)) { + for (const { args } of runs.slice(2).filter((_, i) => i + 2 !== at)) { 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 = []; @@ -173,16 +179,44 @@ test("pi-unpin S3 re-check 3: --no-tools or --no-themes can't stand in for the l 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, 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}`; - assert.equal(fit(standIn(t, { standIn: ["--no-approve", "--no-extensions"] }).env).message, but("trusted project file", "--no-approve")); - for (const [flag, kind] of [["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]]) { - assert.equal(fit(standIn(t, { standIn: [flag, "--no-approve"] }).env).message, but(kind, flag), flag); - } - assert.equal(fit(standIn(t, { extraFlags: ["--no-mcp"], standIn: ["--no-mcp", "--no-approve"] }).env).message, but("MCP server", "--no-mcp")); + 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(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(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(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. - assert.equal(fit(standIn(t, { hideWhen: "--no-approve" }).env).message, but("extension", "--no-extensions")); + // It now leaks every other kind in the --no-approve run, where --no-approve is left out. + assert.match(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(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, t => { + for (const [leakWithout, flag] of [[["--no-skills"], "--no-skills"], [LOADERS, "--no-approve"]]) { + const m = 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 --tools and most with --session-dir, so one +// sealed run has a session's shape: no --no-tools, no --no-session. +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, t => { + for (const extraFlags of [[], ["--no-mcp"]]) for (const needs of ["--no-tools", "--no-session"]) { + assert.match(fit(standIn(t, { needs, extraFlags }).env).message ?? "passed", /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted user extension, .* with the seal flags in a session's shape$/, `${needs} ${extraFlags}`); + } +}); + test("pi-unpin S3 re-check 3: a pi that remembers the control in its scratch directory and hides later is refused", SKIP, t => { assert.match(fit(standIn(t, { remember: true }).env).message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted user extension, .* with the seal flags on$/); }); @@ -332,3 +366,30 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it" assert.equal(run(standIn(t, { extraFlags: ["--no-mcp"] })).filter((a) => a === "--no-mcp").length, 1); assert.equal(run(standIn(t, {})).includes("--no-mcp"), false); }); + +// Re-check 4 item 1: the session-shaped fit run's arguments are the adapter's for a bus session +// with tools and a fresh persistent session, so the two can't drift. The adapter runs as a session +// runs it, from pi.env(), with the fit run's tools, provider, model and files put in. +test("pi-unpin S3 re-check 4: the session-shaped fit run passes exactly what the adapter passes", SKIP, t => { + assert.equal(typeof fitModule.sessionShape, "function", "pi-fit.mjs exports sessionShape"); + const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }; + for (const extraFlags of [[], ["--no-mcp"]]) { + const s = standIn(t, { extraFlags }); + const out = join(s.root, "argv"); + const prompt = join(s.root, "prompt.md"); + const ext = join(s.root, "fit.ts"); + const sessions = join(s.root, "sessions"); + writeFileSync(prompt, "p"); + writeFileSync(ext, ""); + const given = pi.env({ policy: { tools: ["read"] }, tools: [], session, env: s.env }); + assert.ok(given.MOSAIC_TOOLS, "a bus session always has tools"); + const shape = fitModule.SESSION_SHAPE; + const r = spawnSync("/bin/sh", [join(REPO, "adapters", "pi", "adapter.sh")], { + encoding: "utf8", + env: { ...given, MOSAIC_TOOLS: shape.tools, PI_PROVIDER: shape.provider, PI_MODEL: shape.model, MOSAIC_THINKING: "", MOSAIC_EXTENSIONS: ext, PATH: "/usr/bin:/bin", ARGV_OUT: out, MOSAIC_SYSTEM_PROMPT_FILE: prompt, MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(s.root, "ws"), MOSAIC_SESSION_DIR: sessions }, + }); + assert.equal(r.status, 0, r.stderr); + const argv = readFileSync(out, "utf8").split("\n").slice(0, -1); + assert.deepEqual(argv, [...fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: [ext], prompt, sessionDir: sessions }), "-p", "x"]); + } +}); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index 2270e209e..e23202b0f 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -33,7 +33,9 @@ // `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. `commands` "fail" answers get_commands with success false; "list" answers +// 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. +// `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. // @@ -92,8 +94,10 @@ if (rpc || print) { if (fs.existsSync(marker)) ignoreAll = true; else { fs.writeFileSync(marker, ""); leak = new Set(ALL); } } - // Whether a seal flag is on: with standIn, its flag reads as the other flag. - const sealed = (flag) => has(K.standIn && K.standIn[0] === flag ? K.standIn[1] : flag); + if ((K.leakWithout || []).some((f) => !has(f))) 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(); -- 2.54.0 From b41bc00b264e43d6c2b8b2fe161482c71211c97a Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 18:11:05 -0600 Subject: [PATCH 11/16] fix(pi): pi-unpin S3 re-check 5: five -p runs in a bus session's shape pass every pair of the adapter's session branches (tool list or --no-tools, -c over a planted session, --thinking, --mode json), a -c run must resume the planted session, each loads two -e, the -e extension's command list shows skills and prompt templates in -p, the drift test drives the adapter through all sixteen branch sets and fails on a pair no run passes, and a test holds the --no-mcp run on its own (#1873) --- packages/harness/README.md | 46 ++++--- packages/harness/src/pi-fit.mjs | 172 ++++++++++++++++--------- packages/harness/tests/pi-fit.test.mjs | 124 +++++++++++++----- packages/harness/tests/pi-stand-in.mjs | 45 +++++-- 4 files changed, 264 insertions(+), 123 deletions(-) diff --git a/packages/harness/README.md b/packages/harness/README.md index 0a0da9769..8c99b75b0 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -34,21 +34,23 @@ empty environment and a scratch home, and calls no model. It needs (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`, so in rpc only; the rest in rpc and in `-p`): extensions, MCP +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 it sees at `session_start`; the -sealed runs must show the `--system-prompt` file and none of the planted -files. A control run passes the seal flags less every loader flag +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`, or the check can't tell and +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. @@ -56,22 +58,24 @@ 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. One -more sealed rpc run has a bus session's shape: the arguments the adapter -passes for a session with tools and a fresh persistent session, in its -order, from `sessionShape` in `src/pi-fit.mjs`, which a test holds to -the adapter's real argv. That is `--tools read` in place of -`--no-tools`, `--session-dir` in place of `--no-session`, a fixed -`--provider` and `--model`, and `-e` right after `--no-extensions`. It -must show no planted source. Each run gets a fresh scratch directory, so -no run sees what another left. What a session can pass that no fit run -does: `--mode json` (the model event monitor) or `-p` with a request in -the session's shape (that run is rpc), `--thinking`, `-c` (a session -directory that already holds a session), `--fork`, `--skill` with -`--no-skills` dropped (worker runs only), more than one `-e`, a tool -list other than `read`, its own provider and model, and a provider key. -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 +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 through all +sixteen branch sets and fails on an argv `sessionShape` doesn't build, +or a pair no run passes. 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, and a session's own values (tool +list, thinking level, 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 diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index ed8bcfa9a..524c1103c 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -10,8 +10,10 @@ // 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 the system prompt it sees at session_start. The -// binary answers get_state and get_commands, and shows each kind of planted source: +// 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 @@ -25,16 +27,18 @@ // --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 one more sealed rpc run in a bus session's shape, with the arguments -// adapters/pi/adapter.sh passes for a session with tools and a fresh persistent -// session, in its order (sessionShape below; a test holds it to the adapter's argv): -// --tools read in place of --no-tools, --session-dir in place of --no-session, a fixed -// --provider and --model, and -e right after --no-extensions. It answers both, loads the -// -e extension, uses the --system-prompt file, and shows no planted source. -// sealed -p the same in -p, the mode the adapter runs. Its -e extension must reach -// session_start, and no planted extension, MCP server, context file or trusted -// project file may show. -p answers no get_commands, so skills and prompt templates -// are checked in rpc only. +// 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 all sixteen branch sets 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 // @@ -53,13 +57,12 @@ // 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. What a session can pass -// that no run does: --mode json (the model event monitor) or -p with a request in the session's -// shape (that run is rpc), --thinking, -c (a session directory that already holds a session), -// --fork, --skill with --no-skills dropped (worker runs only), more than one -e, a tool list -// other than read, its own provider and model, and a provider key. 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. +// 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 five runs; and +// a session's own values: its tool list, thinking level, 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 { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; @@ -73,22 +76,38 @@ export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", " 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 run's fixed tools, provider and model. Offline with no key, pi stops before -// any model call. -export const SESSION_SHAPE = Object.freeze({ tools: "read", provider: "anthropic", model: "claude-sonnet-4-5" }); -// The arguments adapters/pi/adapter.sh builds for a bus session with tools (SESSION_SHAPE.tools), -// a fresh persistent session directory, the given -e files and --system-prompt file, no --thinking -// and no --skill, less the -p and request it appends. Keep its order: pi-fit.test.mjs runs the -// adapter and fails when the two differ. -export function sessionShape({ noMcp, extensions, prompt, sessionDir }) { +// 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. +export const SESSION_SHAPE = Object.freeze({ tools: "read,bash", thinking: "low", provider: "anthropic", model: "claude-sonnet-4-5" }); +// 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", SESSION_SHAPE.tools, "--session-dir", sessionDir, - "--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model, - "--system-prompt", prompt, + ...(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; @@ -118,17 +137,20 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa } function check(found, { levels, bwrap, node, who, digest }) { - // args: the arguments, or a function of the run's scratch directory that returns them. The - // answer is read before the scratch directory goes: the rpc answers, whether the -e extension - // loaded, the system prompt it saw, and the planted sources that showed (commands: false leaves - // get_commands out, for -p). - const run = (args, input = "", { timeout = RUN_TIMEOUT, commands = true } = {}) => { + // 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). + const run = (args, input = "", { timeout = RUN_TIMEOUT } = {}) => { const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-")); try { plant(scratch); const r = runIn(found, { bwrap, node, scratch, args: typeof args === "function" ? args(scratch) : args, input, timeout }); const answer = answered(r.stdout); - return { ...r, answer, explicit: marked(scratch, EXPLICIT), prompt: prompted(scratch), seen: seen(scratch, commands ? answer : { commands: [] }) }; + 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 { rmSync(scratch, { recursive: true, force: true }); } @@ -168,6 +190,11 @@ function check(found, { levels, bwrap, node, who, digest }) { 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 = rpcRun(seal); if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`); @@ -190,22 +217,28 @@ function check(found, { levels, bwrap, node, who, digest }) { if (others.length) return refuse(`loaded a planted ${others.map((s) => s.source).join(", ")} ${but}`); } - // A bus session's shape: every session passes --tools, and a persistent one --session-dir, so a - // seal that holds only with --no-tools or --no-session, or only in the order above, shows here. - const shaped = run((s) => [...sessionShape({ noMcp, extensions: [join(s, "explicit", "fit.ts")], prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions") }), "--mode", "rpc"], ask); - const shape = "in a session's shape"; - if (shaped.stopped) return refuse(`can't be checked: the start ${shape} ${shaped.stopped}`); - if (!shaped.answer.state || !shaped.answer.commands) return refuse(`can't be checked: it didn't answer get_state and get_commands ${shape}`); - if (!shaped.explicit) return refuse(`didn't load an -e extension ${shape}`); - if (shaped.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${shape}`); - if (!shaped.prompt.includes(GIVEN)) return refuse(`didn't use the --system-prompt file ${shape}`); - if (shaped.seen.length) return refuse(`loaded a planted ${shaped.seen.map((s) => s.source).join(", ")} with the seal flags ${shape}`); + // 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. + for (const shape of SESSION_RUNS) { + const r = 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 }); + }); + 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}`); + } // -p takes a message; offline with no key, pi stops before any model call. - const p = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { commands: false }); + const p = run((s) => ["-p", ...seal, ...given(s), "fit check"]); 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((s) => s.source).join(", ")} with the seal flags on in -p`); + if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} with the seal flags on in -p`); // 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. @@ -304,12 +337,24 @@ const TEXTS = [ ]; // The --system-prompt file's word, which the sealed runs must show. const GIVEN = "FITGIVENPROMPT"; -// The -e extension the control and sealed runs pass, which must load. +// 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; } }; -// The system prompt the -e extension saw at session_start, or null when it wrote none. -const prompted = (scratch) => { try { return readFileSync(join(scratch, "marks", "system-prompt"), "utf8"); } catch { return null; } }; +// 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"); @@ -323,7 +368,7 @@ function plant(scratch) { mkdirSync(marks, { recursive: true }); mkdirSync(join(scratch, "tmp"), { recursive: true }); mkdirSync(project, { recursive: true }); - // The session-shaped run's --session-dir, made as the adapter makes it. + // 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. @@ -360,18 +405,29 @@ function plant(scratch) { put(join(fallback, "skills", "fit-default-skill", "SKILL.md"), skill("fit-default-skill")); put(join(fallback, "prompts", "fit-default-prompt.md"), prompt); 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, "system-prompt"))}, ctx.getSystemPrompt()); });\n}\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`); } -// The planted sources a run loaded, as { source, kind }: markers present, planted names -// get_commands listed, and planted words in the system prompt the -e extension saw. -function seen(scratch, answer) { +// 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 })); - for (const [name, [source, kind]] of Object.entries(NAMED)) if ((answer.commands ?? []).some((c) => c === name || c.endsWith(`:${name}`))) out.push({ source, kind }); - const text = prompted(scratch) ?? ""; + 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; } diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 50e1f083f..265cb6aeb 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -7,13 +7,15 @@ import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { spawnSync } from "node:child_process"; import { tmpdir } from "node:os"; -import { join, resolve } from "node:path"; +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, sandboxProblem } from "../../seat/src/sandbox.mjs"; +import { ENV_ALLOW, 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"; @@ -145,16 +147,18 @@ test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the r }); const seal = [...SEAL, ...extraFlags]; const loaders = [...LOADERS, ...extraFlags]; - // --version, --help, the control, the sealed rpc, one per loader flag, the session-shaped run, + // --version, --help, the control, the sealed rpc, one per loader flag, the session-shaped runs, // -p, two missing -e. - assert.equal(runs.length, 2 + 2 + loaders.length + 1 + 1 + 2); + 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"); - // Re-check 4 item 1: the session-shaped run comes after the one-out runs, built by the same - // function the adapter test below holds to the adapter's argv, with rpc at the end. + // Re-check 5 item 1: the session-shaped runs come after the one-out 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. const at = 2 + 2 + loaders.length; - assert.deepEqual(runs[at].args, [...fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: ["/explicit/fit.ts"], prompt: "/prompt.md", sessionDir: "/sessions" }), "--mode", "rpc"]); + shapes.forEach((shape, i) => assert.deepEqual(runs[at + i].args, fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: ["/explicit/fit.ts", "/explicit/second.ts"], prompt: "/prompt.md", sessionDir: "/sessions", message: "fit check", ...shape }), JSON.stringify(shape))); const flagLists = []; - for (const { args } of runs.slice(2).filter((_, i) => i + 2 !== at)) { + for (const { args } of runs.slice(2).filter((_, i) => i + 2 < at || i + 2 >= at + shapes.length)) { 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 = []; @@ -209,14 +213,47 @@ test("pi-unpin S3 re-check 4: a pi that loads other kinds when one loader flag i } }); -// Re-check 4 item 1: every bus session runs pi with --tools and most with --session-dir, so one -// sealed run has a session's shape: no --no-tools, no --no-session. +// 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, t => { - for (const extraFlags of [[], ["--no-mcp"]]) for (const needs of ["--no-tools", "--no-session"]) { - assert.match(fit(standIn(t, { needs, extraFlags }).env).message ?? "passed", /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted user extension, .* with the seal flags in a session's shape$/, `${needs} ${extraFlags}`); + for (const extraFlags of [[], ["--no-mcp"]]) for (const [needs, shape] of [["--no-tools", "--tools, --thinking, --mode json"], ["--no-session", "--no-tools"]]) { + assert.match(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, 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(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, t => { + assert.equal(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(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(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 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, t => { + assert.equal(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(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, t => { assert.match(fit(standIn(t, { remember: true }).env).message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted user extension, .* with the seal flags on$/); }); @@ -241,7 +278,8 @@ test("pi-unpin S3 re-check 2: a --version that hangs costs a few seconds, not th test("pi-unpin S3 review: a stand-in that drops the --system-prompt file, or runs no session_start in -p, is refused", SKIP, t => { assert.equal(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"); - assert.equal(fit(standIn(t, { printNoStart: true }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: a -p start with the seal flags didn't run its -e extension"); + // The session-shaped runs are -p, so the first of them is where that shows. + assert.equal(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. @@ -367,29 +405,51 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it" assert.equal(run(standIn(t, {})).includes("--no-mcp"), false); }); -// Re-check 4 item 1: the session-shaped fit run's arguments are the adapter's for a bus session -// with tools and a fresh persistent session, so the two can't drift. The adapter runs as a session -// runs it, from pi.env(), with the fit run's tools, provider, model and files put in. -test("pi-unpin S3 re-check 4: the session-shaped fit run passes exactly what the adapter passes", SKIP, t => { - assert.equal(typeof fitModule.sessionShape, "function", "pi-fit.mjs exports sessionShape"); - const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }; +// Re-check 5 item 1: the adapter runs through every branch a bus session can reach, with the +// environment the runner hands it (sessionEnv's allowlist, adapterEnv, and pi.env() as harnessEnv). +// Each argv must be one sessionShape() builds, and each pair of its branches must be passed +// together by one session-shaped fit run. Every variable the adapter reads must come from that +// environment or be one no bus session gets, so a new adapter branch can't go unseen. +test("pi-unpin S3 re-check 5: every argv the adapter builds for a bus session is one sessionShape builds, and each pair of its branches is one a fit run passes", SKIP, 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 reads = new Set([...readFileSync(adapter, "utf8").matchAll(/\$\{?((?:MOSAIC|PI)_[A-Z_]+)/g)].map((m) => m[1])); + const set = new Set(["MOSAIC_REQUEST"]); // the runner adds it for each turn + 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])); + let runs = 0; for (const extraFlags of [[], ["--no-mcp"]]) { const s = standIn(t, { extraFlags }); - const out = join(s.root, "argv"); const prompt = join(s.root, "prompt.md"); - const ext = join(s.root, "fit.ts"); - const sessions = join(s.root, "sessions"); writeFileSync(prompt, "p"); - writeFileSync(ext, ""); - const given = pi.env({ policy: { tools: ["read"] }, tools: [], session, env: s.env }); - assert.ok(given.MOSAIC_TOOLS, "a bus session always has tools"); - const shape = fitModule.SESSION_SHAPE; - const r = spawnSync("/bin/sh", [join(REPO, "adapters", "pi", "adapter.sh")], { - encoding: "utf8", - env: { ...given, MOSAIC_TOOLS: shape.tools, PI_PROVIDER: shape.provider, PI_MODEL: shape.model, MOSAIC_THINKING: "", MOSAIC_EXTENSIONS: ext, PATH: "/usr/bin:/bin", ARGV_OUT: out, MOSAIC_SYSTEM_PROMPT_FILE: prompt, MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(s.root, "ws"), MOSAIC_SESSION_DIR: sessions }, - }); - assert.equal(r.status, 0, r.stderr); - const argv = readFileSync(out, "utf8").split("\n").slice(0, -1); - assert.deepEqual(argv, [...fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: [ext], prompt, sessionDir: sessions }), "-p", "x"]); + for (const tools of [[], ["read", "bash"]]) for (const cont of [false, true]) for (const thinking of ["", "low"]) for (const json of [false, true]) { + const dir = mkdtempSync(join(s.root, "run-")); + const sessionDir = join(dir, "session"); + mkdirSync(sessionDir); + if (cont) writeFileSync(join(sessionDir, "earlier.jsonl"), "{}\n"); + const shape = fitModule.SESSION_SHAPE; + const session = { provider: shape.provider, model: shape.model, thinking, toolSocket: join(dir, "tools.sock"), proxyTransport: { piProvider: shape.provider } }; + const harnessEnv = pi.env({ policy: { tools }, tools: [], session, proxy: json ? { key: "fixture-key" } : null, env: s.env }); + const env = adapterEnv({ ...session, bundle: { prompt, policy: join(dir, "policy.json"), tools: join(dir, "tools.json") }, workspace: join(dir, "ws"), sessionDir, turnMarker: join(dir, "turn"), harnessEnv, runDir: dir, proxyFirstTurn: json }, + sessionEnv({ ...s.env, PATH: `${dirname(process.execPath)}:/usr/bin:/bin` }, "fit1", json ? {} : null)); + for (const name of NOT_BUS) assert.equal(env[name], undefined, name); + for (const name of Object.keys(env)) set.add(name); + const out = join(dir, "argv"); + const r = spawnSync("/bin/sh", [adapter], { encoding: "utf8", env: { ...env, ARGV_OUT: out, MOSAIC_REQUEST: "x" } }); + const label = JSON.stringify({ extraFlags, tools, cont, thinking, json }); + // 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"); + assert.deepEqual(argv, fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions, prompt, sessionDir, tools: tools.join(","), cont, thinking, json, message: "x" }), 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]}`); + runs++; + } } + assert.equal(runs, 32); + for (const name of reads) assert.ok(set.has(name) || NOT_BUS.includes(name), `the adapter reads ${name}, which no bus session environment here sets`); }); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index e23202b0f..f9d1bfe46 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -13,8 +13,9 @@ // -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 ctx.getSystemPrompt() -// gets the prompt this stand-in builds), then answers get_state and get_commands +// 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 @@ -35,6 +36,15 @@ // -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. +// 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. @@ -94,7 +104,7 @@ if (rpc || print) { if (fs.existsSync(marker)) ignoreAll = true; else { fs.writeFileSync(marker, ""); leak = new Set(ALL); } } - if ((K.leakWithout || []).some((f) => !has(f))) 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); // 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)); @@ -109,18 +119,19 @@ if (rpc || print) { 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 the system - // prompt at session_start. Do what each would do. + // 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\(("(?:[^"\\]|\\.)*")(, ctx\.getSystemPrompt\(\))?/g)) { + 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]), ""); } }; - if (!(K.dropExplicit && has("--no-extensions"))) for (let i = 0; i < argv.length; i++) if (argv[i] === "-e") load(argv[i + 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); @@ -130,7 +141,8 @@ if (rpc || print) { for (const p of packages) files.push(...list(path.join(p, "extensions"), (n) => n.endsWith(".ts"))); for (const f of files) load(f); } - if (on("mcp", (K.extraFlags || []).includes("--no-mcp") ? "--no-mcp" : "--no-extensions")) { + 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" }); } @@ -148,10 +160,10 @@ if (rpc || print) { } // 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 given = K.ignoreSystemPrompt ? null : val("--system-prompt"); + 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 && given; - const parts = [given ? (read(given) || "") : own("SYSTEM.md"), K.ignoreAppend || hide ? "" : own("APPEND_SYSTEM.md")]; + 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")) { @@ -160,7 +172,16 @@ if (rpc || print) { 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")) || ""); } - if (!(print && K.printNoStart)) for (const f of starts) fs.writeFileSync(f, parts.join("\n")); + // 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) for (const f of list(dir, (n) => n.endsWith(".jsonl"))) { + let h = null; + try { h = JSON.parse((read(f) || "").split("\n")[0]); } catch { h = null; } + if (h && h.type === "session" && h.cwd === cwd) session = h.id; + } + 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); -- 2.54.0 From eac42663ba363b7e1cc745165ee8e3e3f58ae064 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 19:00:35 -0600 Subject: [PATCH 12/16] fix(pi): pi-unpin S3 re-check 6: the fit runs get a bus session's environment, built by the launcher's sessionEnv over ENV_ALLOW, the sandbox's sessionSetenv and the runner's adapterEnv with the pi module's harnessEnv (now in session-env.mjs), so no run sets PI_CODING_AGENT_DIR and the agent sources sit at $HOME/.pi/agent; a test fails when the fit env and a bus session's env differ by a name outside one listed exception; the drift test runs the adapter in that env over 98 argv sets, each tool count, -c case, thinking level and provider, and checks every variable the adapter reads; -c over a session dir with nothing for the project is named under Not run, with a gap test (#1873) --- packages/harness/README.md | 28 ++- packages/harness/src/pi-fit.mjs | 118 ++++++++---- packages/harness/tests/pi-fit.test.mjs | 187 +++++++++++++++----- packages/harness/tests/pi-stand-in.mjs | 31 +++- packages/plugins/README.md | 4 +- packages/plugins/harness/pi/index.mjs | 18 +- packages/plugins/harness/pi/session-env.mjs | 26 +++ packages/seat/src/sandbox.mjs | 17 +- 8 files changed, 315 insertions(+), 114 deletions(-) create mode 100644 packages/plugins/harness/pi/session-env.mjs diff --git a/packages/harness/README.md b/packages/harness/README.md index 8c99b75b0..ec5602e0c 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -28,8 +28,16 @@ bundle's `manifest.json` names the lines it relies on (`reliesOn`): Pi is not pinned (#1873 pi-unpin). Before a session starts, `preflight` and `env` run the fit check in `src/pi-fit.mjs` on the binary the module -found. It runs that binary in a sandbox with the network unshared, an -empty environment and a scratch home, and calls no model. It needs +found. It runs that binary in a sandbox with the network unshared and +calls no model. Each run gets the variable names 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`), 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, 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 and a bus +session's differ, apart from the provider variables. 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 @@ -66,14 +74,20 @@ 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 through all -sixteen branch sets and fails on an argv `sessionShape` doesn't build, -or a pair no run passes. A last sealed `-p` run uses `--no-session`, as +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 fails on an argv +`sessionShape` doesn't build, a pair no run passes, or a variable the +adapter reads that no session sets. 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, and a session's own values (tool -list, thinking level, provider, model, key, `-e` files, request). The +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 diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 524c1103c..94192be1e 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -1,7 +1,8 @@ // #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, an empty environment, a scratch HOME and agent directory, and the found package -// mounted as a session mounts it. No model is called and no provider key is passed. It asks: +// 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 @@ -34,9 +35,10 @@ // 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 all sixteen branch sets and fails on an argv -// sessionShape doesn't build or a pair no run passes. +// 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, @@ -44,7 +46,12 @@ // // Every run gets a fresh scratch directory with the sources planted again, so no run sees what // another one left. 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. +// 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 names a bus session's pi gets, +// built by the code that builds those (the launcher's ENV_ALLOW and sessionEnv, the sandbox's +// sessionSetenv, the runner's adapterEnv and the pi module's harnessEnv), less the provider key. +// So no run sets a variable a session lacks, such as PI_CODING_AGENT_DIR, and a test holds the two +// lists 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 @@ -59,16 +66,22 @@ // 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 five runs; and -// a session's own values: its tool list, thinking level, 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. +// 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 { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join, relative } from "node:path"; -import { BWRAP, checkArgv, sandboxProblem } from "../../seat/src/sandbox.mjs"; +import { BWRAP, checkArgv, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs"; +import { ENV_ALLOW, isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; +import { adapterEnv } from "./runner.mjs"; +import { 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"]); @@ -142,11 +155,14 @@ function check(found, { levels, bwrap, node, who, digest }) { // 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). - const run = (args, input = "", { timeout = RUN_TIMEOUT } = {}) => { + // shape: a session-shaped run's branches, which set its tools, thinking level and monitor + // variables as a session's would. + const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null } = {}) => { const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-")); try { plant(scratch); - const r = runIn(found, { bwrap, node, scratch, args: typeof args === "function" ? args(scratch) : args, input, timeout }); + const env = fitEnv(scratch, { view: found.view, node, noMcp, shape }); + const r = runIn(found, { bwrap, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout }); const answer = answered(r.stdout); const start = started(scratch); const listed = start?.commands ?? []; @@ -175,7 +191,7 @@ function check(found, { levels, bwrap, node, who, digest }) { 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); + const rpcRun = (flags) => run((s) => ["--mode", "rpc", ...flags, ...given(s)], ask, { noMcp }); // 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. @@ -224,7 +240,7 @@ function check(found, { levels, bwrap, node, who, digest }) { const r = 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 }); 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}`); @@ -235,7 +251,7 @@ function check(found, { levels, bwrap, node, who, digest }) { } // -p takes a message; offline with no key, pi stops before any model call. - const p = run((s) => ["-p", ...seal, ...given(s), "fit check"]); + const p = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { noMcp }); 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`); @@ -243,29 +259,58 @@ function check(found, { levels, bwrap, node, who, digest }) { // 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. for (const [mode, args, input] of [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]]) { - const r = run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input); + const r = run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input, { noMcp }); 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 }; } -// The binary, inside the sandbox: the system part, the found package where a session sees it, and -// the scratch directory read-write. The environment holds HOME, the agent directory, TMPDIR, a -// PATH that finds node, and PI_OFFLINE. +// 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. // stopped: why the run was killed ("timed out", "printed more than 4 MB"), or null. -function runIn(found, { bwrap, node, scratch, args, input, timeout }) { +function runIn(found, { bwrap, node, scratch, env, args, input, timeout }) { const list = [ { kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) }, { kind: "rw", src: scratch }, + { kind: "rw", src: join(scratch, "tmp"), dest: "/tmp" }, ]; - const env = { HOME: join(scratch, "home"), PI_CODING_AGENT_DIR: join(scratch, "agent"), TMPDIR: join(scratch, "tmp"), PATH: `${dirname(node)}:/usr/bin:/bin`, PI_OFFLINE: "1" }; - const argv = checkArgv(list, [found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap }); + const argv = checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap }); const r = spawnSync(argv[0], argv.slice(1), { input, encoding: "utf8", timeout, killSignal: "SIGKILL", maxBuffer: OUTPUT_MAX, env: {} }); const stopped = r.error?.code === "ENOBUFS" ? `printed more than ${OUTPUT_MAX / 1024 / 1024} MB` : r.error?.code === "ETIMEDOUT" || r.signal === "SIGKILL" ? "timed out" : null; return { status: r.status, stdout: typeof r.stdout === "string" ? r.stdout : "", stopped }; } +// The values a fit run gives the launcher's ENV_ALLOW names. A name not here gets "fit", so a name +// added there reaches the fit runs too. HOME and MOSAIC_PI_BIN are set again below. +const ALLOW_VALUES = { USER: "fit", LOGNAME: "fit", LANG: "C.UTF-8", LC_ALL: "C.UTF-8", TZ: "UTC", TERM: "dumb" }; +// A fit run's environment: a bus session's pi gets these names, built the same way. The launcher +// passes the ENV_ALLOW names through sessionEnv (every one here but the provider variables, since +// no key is passed: no model is called), 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. 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 and a bus session's differ. +export function fitEnv(scratch, { view, node = process.execPath, noMcp = false, shape = null }) { + const host = { PATH: `${dirname(node)}:/usr/bin:/bin` }; + for (const name of ENV_ALLOW) if (!isProviderVariable(name)) host[name] = ALLOW_VALUES[name] ?? "fit"; + const runner = { ...sessionEnv(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 @@ -304,21 +349,22 @@ function answered(stdout) { // 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. "default agent ..." sits in $HOME/.pi/agent, where pi looks when PI_CODING_AGENT_DIR is -// unset. "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. +// 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", "default agent extension": "extension", + "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", "default agent mcp.json": "MCP server", "project mcp.json": "MCP server", "project .mcp.json": "MCP server", + "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-default-skill": ["default agent skill", "skill"], "fit-project-skill": ["project skill", "skill"], "fit-project-set-skill": ["project settings 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-default-prompt": ["default agent 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"], }; @@ -326,9 +372,8 @@ const NAMED = { // 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 = [ - ["agent/SYSTEM.md", "FITUSERSYSTEM", "user SYSTEM.md", "context file"], - ["agent/AGENTS.md", "FITUSERAGENTS", "user AGENTS.md", "context file"], - ["home/.pi/agent/AGENTS.md", "FITDEFAULTAGENTS", "default agent AGENTS.md", "context file"], + ["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"], @@ -363,7 +408,7 @@ function plant(scratch) { 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, "agent"); + const agent = join(scratch, "home", ".pi", "agent"); const project = join(scratch, "project"); mkdirSync(marks, { recursive: true }); mkdirSync(join(scratch, "tmp"), { recursive: true }); @@ -399,11 +444,6 @@ function plant(scratch) { 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")); - const fallback = join(scratch, "home", ".pi", "agent"); - put(join(fallback, "extensions", "fit.ts"), ext("default agent extension")); - put(join(fallback, "mcp.json"), mcp("default agent mcp.json")); - put(join(fallback, "skills", "fit-default-skill", "SKILL.md"), skill("fit-default-skill")); - put(join(fallback, "prompts", "fit-default-prompt.md"), prompt); 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)); diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 265cb6aeb..80ca7fdd2 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -13,8 +13,8 @@ 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, sandboxProblem } from "../../seat/src/sandbox.mjs"; -import { ENV_ALLOW, sessionEnv } from "../../seat/src/session.mjs"; +import { BWRAP, 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))); @@ -143,7 +143,11 @@ test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the r assert.equal(r.ok, true, r.message); const runs = readFileSync(log, "utf8").split("\n").filter(Boolean).map((l) => l.split("\x1f").slice(0, -1)).filter((a) => a.includes(found.view)).map((a) => { const scratch = a[a.indexOf("--chdir") + 1].replace(/\/project$/, ""); - return { scratch, args: a.slice(a.indexOf(found.view) + 1).map((x) => x.split(scratch).join("")) }; + // 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]; @@ -246,6 +250,67 @@ test("pi-unpin S3 re-check 5: a -c run must resume the planted session, a sessio assert.equal(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, t => { + const r = 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, t => { + assert.equal(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 a host that sets every ENV_ALLOW name and PI_CODING_AGENT_DIR (which it +// drops), 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. +function busEnv(s, dir, { tools = [], thinking = "", provider = fitModule.SESSION_SHAPE.provider, json = false, prompt = join(dir, "prompt.md") } = {}) { + const host = Object.fromEntries(ENV_ALLOW.map((n) => [n, `host-${n.toLowerCase()}`])); + Object.assign(host, { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, MOSAIC_PI_BIN: s.env.MOSAIC_PI_BIN, PI_CODING_AGENT_DIR: join(dir, "host-agent") }); + 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, "fit1", json ? {} : null), ...sessionSetenv(dir, { business: "b", role: "r" }) }; + const env = adapterEnv({ ...session, bundle: { prompt, policy: join(dir, "policy.json"), tools: join(dir, "tools.json") }, workspace: join(dir, "ws"), sessionDir: join(dir, "session"), 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"], +]; +test("pi-unpin S3 re-check 6: a fit run gets the variable names a bus session's pi gets, apart from a named list, and the runs themselves hold them", SKIP, t => { + const s = standIn(t, {}); + const dir = mkdtempSync(join(s.root, "env-")); + const kept = (names) => names.filter((n) => !FIT_ENV_EXCEPT.some(([, drop]) => drop(n))).sort(); + const names = {}; + for (const json of [false, true]) { + const bus = kept(Object.keys(busEnv(s, dir, { tools: ["read", "bash"], thinking: "low", json }))); + const fitNames = Object.keys(fitModule.fitEnv(dir, { view: "/fit/pi", shape: { tools: "read,bash", thinking: "low", json } })).sort(); + assert.deepEqual(fitNames.filter((n) => !bus.includes(n)), [], `fit names a bus session lacks (json ${json})`); + assert.deepEqual(bus.filter((n) => !fitNames.includes(n)), [], `bus names a fit run lacks (json ${json})`); + names[json ? "json" : "other"] = bus; + } + // A run that isn't session-shaped gets the same names as one without --mode json. + assert.deepEqual(Object.keys(fitModule.fitEnv(dir, { view: "/fit/pi" })).sort(), names.other); + assert.ok(!names.other.includes("PI_CODING_AGENT_DIR")); + // 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; expecting one name more is refused. + assert.equal(fit(standIn(t, { leakEnv: names }).env).ok, true); + for (const extra of [{ json: names.json, other: [...names.other, "PI_CODING_AGENT_DIR"] }, { json: [...names.json, "PI_CODING_AGENT_DIR"], other: names.other }]) { + assert.match(fit(standIn(t, { leakEnv: extra }).env).message ?? "passed", /loaded a planted /); + } + for (const [, drop, reason] of FIT_ENV_EXCEPT) assert.ok(typeof drop === "function" && reason.length > 0); +}); + // 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. @@ -405,51 +470,89 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it" assert.equal(run(standIn(t, {})).includes("--no-mcp"), false); }); -// Re-check 5 item 1: the adapter runs through every branch a bus session can reach, with the -// environment the runner hands it (sessionEnv's allowlist, adapterEnv, and pi.env() as harnessEnv). -// Each argv must be one sessionShape() builds, and each pair of its branches must be passed -// together by one session-shaped fit run. Every variable the adapter reads must come from that -// environment or be one no bus session gets, so a new adapter branch can't go unseen. -test("pi-unpin S3 re-check 5: every argv the adapter builds for a bus session is one sessionShape builds, and each pair of its branches is one a fit run passes", SKIP, t => { +// 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: every ENV_ALLOW name set, sessionSetenv, adapterEnv, pi.env()), so +// HOME, TERM and the rest are set as a session sets them, not by the shell. 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. 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, 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 reads = new Set([...readFileSync(adapter, "utf8").matchAll(/\$\{?((?:MOSAIC|PI)_[A-Z_]+)/g)].map((m) => m[1])); - const set = new Set(["MOSAIC_REQUEST"]); // the runner adds it for each turn + 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])); - let runs = 0; - for (const extraFlags of [[], ["--no-mcp"]]) { - const s = standIn(t, { extraFlags }); - const prompt = join(s.root, "prompt.md"); + 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 stands = new Map([false, true].map((noMcp) => [noMcp, standIn(t, { extraFlags: noMcp ? ["--no-mcp"] : [] })])); + const seenLevels = new Set(); + const seenProviders = new Set(); + for (const run of runs) { + const { noMcp, tools, stored, json, thinking, provider } = run; + const s = stands.get(noMcp); + const dir = mkdtempSync(join(s.root, "run-")); + const prompt = join(dir, "prompt.md"); writeFileSync(prompt, "p"); - for (const tools of [[], ["read", "bash"]]) for (const cont of [false, true]) for (const thinking of ["", "low"]) for (const json of [false, true]) { - const dir = mkdtempSync(join(s.root, "run-")); - const sessionDir = join(dir, "session"); - mkdirSync(sessionDir); - if (cont) writeFileSync(join(sessionDir, "earlier.jsonl"), "{}\n"); - const shape = fitModule.SESSION_SHAPE; - const session = { provider: shape.provider, model: shape.model, thinking, toolSocket: join(dir, "tools.sock"), proxyTransport: { piProvider: shape.provider } }; - const harnessEnv = pi.env({ policy: { tools }, tools: [], session, proxy: json ? { key: "fixture-key" } : null, env: s.env }); - const env = adapterEnv({ ...session, bundle: { prompt, policy: join(dir, "policy.json"), tools: join(dir, "tools.json") }, workspace: join(dir, "ws"), sessionDir, turnMarker: join(dir, "turn"), harnessEnv, runDir: dir, proxyFirstTurn: json }, - sessionEnv({ ...s.env, PATH: `${dirname(process.execPath)}:/usr/bin:/bin` }, "fit1", json ? {} : null)); - for (const name of NOT_BUS) assert.equal(env[name], undefined, name); - for (const name of Object.keys(env)) set.add(name); - const out = join(dir, "argv"); - const r = spawnSync("/bin/sh", [adapter], { encoding: "utf8", env: { ...env, ARGV_OUT: out, MOSAIC_REQUEST: "x" } }); - const label = JSON.stringify({ extraFlags, tools, cont, thinking, json }); - // 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"); - assert.deepEqual(argv, fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions, prompt, sessionDir, tools: tools.join(","), cont, thinking, json, message: "x" }), 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]}`); - runs++; - } + const env = busEnv(s, dir, { 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(runs.length, 2 * 3 * 3 * 2 * 2 + 2 * (LEVELS.length - 1) + 2 * (PROVIDERS.length - 1)); + 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`); } - assert.equal(runs, 32); - for (const name of reads) assert.ok(set.has(name) || NOT_BUS.includes(name), `the adapter reads ${name}, which no bus session environment here sets`); }); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index f9d1bfe46..6160b4a37 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -38,7 +38,12 @@ // 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. +// as well as their own flag does. `leakNoAgentDir`: every kind loads when +// PI_CODING_AGENT_DIR is unset, as in a bus session. `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 @@ -92,6 +97,17 @@ if (rpc || print) { } } 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) { @@ -105,6 +121,13 @@ if (rpc || print) { 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.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)); @@ -175,11 +198,7 @@ if (rpc || print) { // 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) for (const f of list(dir, (n) => n.endsWith(".jsonl"))) { - let h = null; - try { h = JSON.parse((read(f) || "").split("\n")[0]); } catch { h = null; } - if (h && h.type === "session" && h.cwd === cwd) session = h.id; - } + 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) { diff --git a/packages/plugins/README.md b/packages/plugins/README.md index c8da9ad44..8000175ac 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -267,7 +267,9 @@ export returns: 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`. + 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. - `version({env})` is what the manifest records as `harnessVersion`. Pi's is the first line its binary prints for `--version`, or null. - `countsAccount()` says whether its launches spend `launch.account` and get diff --git a/packages/plugins/harness/pi/index.mjs b/packages/plugins/harness/pi/index.mjs index 43517da50..647c45410 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -9,6 +9,7 @@ import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; import { locate, printedVersion } from "./binary.mjs"; import { piFit } from "../../../harness/src/pi-fit.mjs"; +import { envNames, harnessEnv } from "./session-env.mjs"; export const interfaceVersion = 1; @@ -128,19 +129,7 @@ export function env({ policy, tools, session, proxy = null, env: hostEnv = proce // #1873 S3: a session runs only a binary that passed the fit check (cached from preflight). const fit = piFit(at, { levels: LEVELS }); if (!fit.ok) throw new Error(fit.message); - return { - MOSAIC_PI_BIN: at.view, - // "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: fit.noMcp ? "1" : "0", - 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 } : {}), - }; + 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 @@ -150,7 +139,8 @@ export function env({ policy, tools, session, proxy = null, env: hostEnv = proce // 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". -export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]); +// 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. diff --git a/packages/plugins/harness/pi/session-env.mjs b/packages/plugins/harness/pi/session-env.mjs new file mode 100644 index 000000000..c50afc8c9 --- /dev/null +++ b/packages/plugins/harness/pi/session-env.mjs @@ -0,0 +1,26 @@ +// 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"]); diff --git a/packages/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index 00daadffb..c0b564c6d 100644 --- a/packages/seat/src/sandbox.mjs +++ b/packages/seat/src/sandbox.mjs @@ -185,12 +185,19 @@ 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. +// 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 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]; + 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 -- 2.54.0 From 098b3771c0e32cd8d992b47ad163532d98624c44 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 21:07:20 -0600 Subject: [PATCH 13/16] fix(pi): pi-unpin S3 re-check 7: the fit check runs its pi processes with async spawn, four at a time, and every caller awaits it, so the bus's event loop stays live (longest gap 22 ms against 6031 ms before) and two launches during one check share it; the launcher's call() awaits a module's promise and refuses its rejection as a throw, and preflight and version are awaited; the Triage host and route await the module's ready(); a run whose pipes outlive it settles a second after it exits or is killed; the fit's host env is the process's own through sessionEnv minus provider keys, its names are in the cache key, parity runs over a login shell's host and a unit's, and stand-ins that leak with TERM, TZ or LC_ALL unset are refused; bundle tests await harnessReady() (#1873) --- packages/cli/src/launcher.mjs | 19 +- packages/cli/tests/fixtures/request-agent.mjs | 3 +- packages/harness/README.md | 43 +- packages/harness/src/pi-fit.mjs | 305 ++++++++---- packages/harness/tests/bundle.test.mjs | 3 +- packages/harness/tests/creative.test.mjs | 3 +- packages/harness/tests/helpers.mjs | 6 + packages/harness/tests/library-tools.test.mjs | 3 +- packages/harness/tests/pi-fit.test.mjs | 453 ++++++++++++------ packages/harness/tests/pi-session.test.mjs | 3 +- packages/harness/tests/pi-stand-in.mjs | 5 +- packages/harness/tests/proxy-bundle.test.mjs | 3 +- packages/harness/tests/rails.test.mjs | 3 +- packages/harness/tests/request-tools.test.mjs | 3 +- packages/harness/tests/skills.test.mjs | 3 +- .../harness/tests/workflow-prompt.test.mjs | 3 +- packages/plugins/README.md | 22 +- packages/plugins/harness/pi/binary.mjs | 42 +- packages/plugins/harness/pi/index.mjs | 35 +- packages/plugins/src/kinds.mjs | 3 + packages/plugins/tests/harness.test.mjs | 9 +- packages/plugins/tests/pi-binary.test.mjs | 37 +- packages/plugins/tests/proxy-harness.test.mjs | 8 +- packages/webui/src/triage-host.mjs | 10 + packages/webui/src/triage-route.mjs | 3 + packages/webui/tests/triage-host.test.mjs | 2 + 26 files changed, 725 insertions(+), 307 deletions(-) diff --git a/packages/cli/src/launcher.mjs b/packages/cli/src/launcher.mjs index dd0b02b21..087d9ddf7 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 @@ -556,7 +563,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 +573,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 +747,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 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/harness/README.md b/packages/harness/README.md index ec5602e0c..276454420 100644 --- a/packages/harness/README.md +++ b/packages/harness/README.md @@ -27,17 +27,24 @@ bundle's `manifest.json` names the lines it relies on (`reliesOn`): | 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` -and `env` run the fit check in `src/pi-fit.mjs` on the binary the module -found. It runs that binary in a sandbox with the network unshared and -calls no model. Each run gets the variable names 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`), 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, 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 and a bus -session's differ, apart from the provider variables. It needs +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 @@ -77,9 +84,14 @@ 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 fails on an argv -`sessionShape` doesn't build, a pair no run passes, or a variable the -adapter reads that no session sets. It can't see a branch keyed on a +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 @@ -99,7 +111,8 @@ flags, so the check doesn't plant one. Any failure refuses the launch with 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). Lines 1, 2, 4 and 5 are not +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). diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 94192be1e..2239f515d 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -45,21 +45,28 @@ // 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. Each run passes the same -e and --system-prompt; the other runs differ only by +// another one left, and so 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 names a bus session's pi gets, -// built by the code that builds those (the launcher's ENV_ALLOW and sessionEnv, the sandbox's -// sessionSetenv, the runner's adapterEnv and the pi module's harnessEnv), less the provider key. -// So no run sets a variable a session lacks, such as PI_CODING_AGENT_DIR, and a test holds the two -// lists together. +// 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) and the mount layout. +// 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. // @@ -73,13 +80,13 @@ // 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 { spawnSync } from "node:child_process"; +import { spawn } from "node:child_process"; import { createHash } from "node:crypto"; -import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join, relative } from "node:path"; -import { BWRAP, checkArgv, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs"; -import { ENV_ALLOW, isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; +import { BWRAP, checkArgv, sessionSetenv } from "../../seat/src/sandbox.mjs"; +import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; import { adapterEnv } from "./runner.mjs"; import { harnessEnv } from "../../plugins/harness/pi/session-env.mjs"; @@ -125,56 +132,120 @@ 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(); + +// 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, node, host }) => JSON.stringify([filesDigest(found), found.root, found.dest ?? null, found.view, bwrap, node, levels, fitNames(host)]); // found: binary.mjs's answer ({ bin, label, root, dest, view, pkg }). levels: the thinking levels -// the module applies. Returns { ok: true, version, noMcp, digest } or { ok: false, message }. -export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPath } = {}) { +// 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. Returns a promise of +// { ok: true, version, noMcp, digest } or { ok: false, message }. A call while a check with the +// same key runs gets that check's promise. A throw rejects it. +export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPath, host = process.env } = {}) { const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`; - let digest; + let key; try { - digest = filesDigest(found); + key = fitKey(found, { levels, bwrap, node, host }); } catch { - return { ok: false, message: `${who(null)} can't be checked: its files can't be read` }; + return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read` }); } - const key = JSON.stringify([digest, found.root, found.dest ?? null, found.view, bwrap, node, levels]); - if (passed.has(key)) return passed.get(key); - // The sandbox's own error text can name a host path, so the line keeps only what failed. - const problem = sandboxProblem({ bwrap }); - if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem.startsWith("the sandbox (bwrap) is missing") ? "the sandbox (bwrap) is missing" : "the sandbox (bwrap) failed"}` }; - const result = check(found, { levels, bwrap, node, who, digest }); - if (result.ok) passed.set(key, result); - return result; + if (passed.has(key)) return Promise.resolve(passed.get(key)); + if (running.has(key)) return running.get(key); + const job = (async () => { + // The sandbox's own error text can name a host path, so the line keeps only what failed. + const problem = await sandboxFails({ bwrap, node }); + if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem}` }; + const result = await check(found, { levels, bwrap, node, who, digest: JSON.parse(key)[0], host }); + if (result.ok) passed.set(key, result); + return result; + })().finally(() => { if (running.get(key) === job) running.delete(key); }); + running.set(key, job); + return job; } -function check(found, { levels, bwrap, node, who, digest }) { +// 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, node = process.execPath, host = process.env } = {}) { + try { + return passed.get(fitKey(found, { levels, bwrap, node, host })) ?? null; + } catch { + return null; + } +} + +async function check(found, { levels, bwrap, 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. - const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null } = {}) => { - const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-")); + // 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 { - plant(scratch); - const env = fitEnv(scratch, { view: found.view, node, noMcp, shape }); - const r = runIn(found, { bwrap, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout }); - 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) }; + if (stop.signal.aborted) return null; + const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-")); + try { + plant(scratch); + const env = fitEnv(scratch, { view: found.view, noMcp, shape, host }); + const r = await runIn(found, { bwrap, 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 { + rmSync(scratch, { recursive: true, force: true }); + } } finally { - rmSync(scratch, { recursive: true, force: true }); + free(); } }; - const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` }); - const v = run(["--version"], "", { timeout: VERSION_TIMEOUT }); + 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 help = run(["--help"]); + 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)); @@ -193,9 +264,27 @@ function check(found, { levels, bwrap, node, who, digest }) { 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 control = rpcRun(without(...loaders.map(([f]) => f))); + 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"); @@ -212,7 +301,7 @@ function check(found, { levels, bwrap, node, who, digest }) { 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 = rpcRun(seal); + 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"); @@ -222,8 +311,8 @@ function check(found, { levels, bwrap, node, who, digest }) { 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] of loaders) { - const r = rpcRun(without(flag)); + 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}`); @@ -233,14 +322,8 @@ function check(found, { levels, bwrap, node, who, digest }) { if (others.length) return refuse(`loaded a planted ${others.map((s) => s.source).join(", ")} ${but}`); } - // 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. - for (const shape of SESSION_RUNS) { - const r = 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 }); + 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}`); @@ -250,54 +333,112 @@ function check(found, { levels, bwrap, node, who, digest }) { if (r.seen.length) return refuse(`loaded a planted ${r.seen.map((x) => x.source).join(", ")} with the seal flags ${name}`); } - // -p takes a message; offline with no key, pi stops before any model call. - const p = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { noMcp }); + 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`); - // 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. - for (const [mode, args, input] of [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]]) { - const r = run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input, { noMcp }); + 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 a child process the caller +// awaits: the system part, the network unshared, an empty environment, then /usr/bin/true. Returns +// null, or which way it failed, without the sandbox's own text. +async function sandboxFails({ bwrap, node }) { + if (!existsSync(bwrap)) return "the sandbox (bwrap) is missing"; + const r = await spawned(checkArgv([], ["/usr/bin/true"], { chdir: "/", node, bwrap }), { input: "", timeout: 10000, signal: new AbortController().signal }); + return r.status === 0 ? null : "the sandbox (bwrap) failed"; +} + // 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. -// stopped: why the run was killed ("timed out", "printed more than 4 MB"), or null. -function runIn(found, { bwrap, node, scratch, env, args, input, timeout }) { +// 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, 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" }, ]; - const argv = checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap }); - const r = spawnSync(argv[0], argv.slice(1), { input, encoding: "utf8", timeout, killSignal: "SIGKILL", maxBuffer: OUTPUT_MAX, env: {} }); - const stopped = r.error?.code === "ENOBUFS" ? `printed more than ${OUTPUT_MAX / 1024 / 1024} MB` : r.error?.code === "ETIMEDOUT" || r.signal === "SIGKILL" ? "timed out" : null; - return { status: r.status, stdout: typeof r.stdout === "string" ? r.stdout : "", stopped }; + return spawned(checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap }), { input, timeout, signal }); } -// The values a fit run gives the launcher's ENV_ALLOW names. A name not here gets "fit", so a name -// added there reaches the fit runs too. HOME and MOSAIC_PI_BIN are set again below. -const ALLOW_VALUES = { USER: "fit", LOGNAME: "fit", LANG: "C.UTF-8", LC_ALL: "C.UTF-8", TZ: "UTC", TERM: "dumb" }; -// A fit run's environment: a bus session's pi gets these names, built the same way. The launcher -// passes the ENV_ALLOW names through sessionEnv (every one here but the provider variables, since -// no key is passed: no model is called), 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. 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 and a bus session's differ. -export function fitEnv(scratch, { view, node = process.execPath, noMcp = false, shape = null }) { - const host = { PATH: `${dirname(node)}:/usr/bin:/bin` }; - for (const name of ENV_ALLOW) if (!isProviderVariable(name)) host[name] = ALLOW_VALUES[name] ?? "fit"; - const runner = { ...sessionEnv(host, "fit"), ...sessionSetenv(scratch, { business: "fit", role: "fit" }) }; +// 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 that lands while bwrap sets up, before setpriv +// sets the parent-death signal), 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")] : [])]; 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 5517c3158..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"]; 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 index 80ca7fdd2..2e5278c61 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -4,7 +4,7 @@ // binary by its label, never a host path. import test from "node:test"; import assert from "node:assert/strict"; -import { mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, 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"; @@ -41,7 +41,32 @@ function standIn(t, knobs, version = "2.0.0") { } const { piFit } = fitModule; -const fit = (env) => piFit(locate(env), { levels: LEVELS }); +// 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) => { @@ -49,40 +74,40 @@ const noPath = (message, paths) => { 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, t => { +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 = fit(s.env); + 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(pi.preflight({ env: { ...CREDENTIAL, ...s.env } }), null); - assert.equal(pi.version({ env: s.env }), "2.0.0", "the launcher records what the binary prints"); + 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, t => { +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 = pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + 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(fit(standIn(t, { modes: ["text", "json"] }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --mode rpc"); - assert.equal(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(fit(standIn(t, { dropFlags: ["--offline", "-e"] }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks -e, --offline"); + 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, t => { +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 = pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + 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(fit(standIn(t, { missingExtOk: mode }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) starts with a missing -e extension", mode); + 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, t => { +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 = pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + 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. @@ -95,34 +120,34 @@ test("pi-unpin S3: a stand-in that touches the planted MCP marker under the seal // 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(fit(standIn(t, { sealLeak: [kind] }).env).message, `pi 2.0.0 (MOSAIC_PI_BIN) ${line}`, kind); + 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, t => { +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 = pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + 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, t => { +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(fit(s.env).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't answer get_commands", commands); + 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(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"); + 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, t => { - assert.equal(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: 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, t => { - assert.equal(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"); +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. @@ -131,17 +156,17 @@ const LOADERS = ["--no-approve", "--no-extensions", "--no-skills", "--no-prompt- // 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 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, t => { +// 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\nfor a in "$@"; do printf '%s\\037' "$a"; done >> '${log}'\nprintf '\\n' >> '${log}'\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 }); + 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 = piFit(found, { levels: LEVELS, bwrap }); + const r = await piFit(found, { levels: LEVELS, bwrap }); assert.equal(r.ok, true, r.message); - const runs = readFileSync(log, "utf8").split("\n").filter(Boolean).map((l) => l.split("\x1f").slice(0, -1)).filter((a) => a.includes(found.view)).map((a) => { + 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.) @@ -156,13 +181,14 @@ test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the r 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"); - // Re-check 5 item 1: the session-shaped runs come after the one-out 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. - const at = 2 + 2 + loaders.length; - shapes.forEach((shape, i) => assert.deepEqual(runs[at + i].args, fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: ["/explicit/fit.ts", "/explicit/second.ts"], prompt: "/prompt.md", sessionDir: "/sessions", message: "fit check", ...shape }), JSON.stringify(shape))); + 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.slice(2).filter((_, i) => i + 2 < at || i + 2 >= at + shapes.length)) { + 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 = []; @@ -175,17 +201,17 @@ test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the r 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, [seal.filter((f) => !loaders.includes(f)), seal, ...loaders.map((l) => seal.filter((f) => f !== l))].map((f) => f.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, t => { +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(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); + 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, t => { +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 @@ -197,21 +223,21 @@ test("pi-unpin S3 re-check 3: each loader flag is shown on its own, so no other ["--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(fit(standIn(t, { standIn: [flag, other], extraFlags }).env).message, but(kind, flag), flag); + ]) 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(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(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")); + 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(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(fit(standIn(t, { hideWhen: "--no-extensions" }).env).message, but("trusted project file", "--no-approve")); + 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, t => { +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 = fit(standIn(t, { leakWithout }).env).message; + 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); } @@ -219,9 +245,9 @@ test("pi-unpin S3 re-check 4: a pi that loads other kinds when one loader flag i // 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, t => { +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(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}`); + 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}`); } }); @@ -229,7 +255,7 @@ test("pi-unpin S3 re-check 4: a pi whose loader flags work only with --no-tools // 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, t => { +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"], @@ -241,19 +267,19 @@ test("pi-unpin S3 re-check 5: a pi that leaks only with a session's -c, --thinki [{ 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(fit(standIn(t, { ...knobs, extraFlags }).env).message ?? "passed", line(shape), `${JSON.stringify(knobs)} ${extraFlags}`); + ]) 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, t => { - assert.equal(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(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(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"); +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, t => { - const r = fit(standIn(t, { leakNoAgentDir: true }).env); +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 /); }); @@ -262,22 +288,22 @@ test("pi-unpin S3 re-check 6: a pi that loads every kind when PI_CODING_AGENT_DI // 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, t => { - assert.equal(fit(standIn(t, { leakNoMatch: true }).env).ok, true); +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 a host that sets every ENV_ALLOW name and PI_CODING_AGENT_DIR (which it -// drops), 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. -function busEnv(s, dir, { tools = [], thinking = "", provider = fitModule.SESSION_SHAPE.provider, json = false, prompt = join(dir, "prompt.md") } = {}) { - const host = Object.fromEntries(ENV_ALLOW.map((n) => [n, `host-${n.toLowerCase()}`])); - Object.assign(host, { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, MOSAIC_PI_BIN: s.env.MOSAIC_PI_BIN, PI_CODING_AGENT_DIR: join(dir, "host-agent") }); +// 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, "fit1", json ? {} : null), ...sessionSetenv(dir, { business: "b", role: "r" }) }; - const env = adapterEnv({ ...session, bundle: { prompt, policy: join(dir, "policy.json"), tools: join(dir, "tools.json") }, workspace: join(dir, "ws"), sessionDir: join(dir, "session"), turnMarker: join(dir, "turn"), harnessEnv, runDir: dir, proxyFirstTurn: json }, runner); + 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" }; } @@ -287,175 +313,307 @@ function busEnv(s, dir, { tools = [], thinking = "", provider = fitModule.SESSIO const FIT_ENV_EXCEPT = [ ["provider variables (isProviderVariable)", isProviderVariable, "the fit check passes no key: it calls no model"], ]; -test("pi-unpin S3 re-check 6: a fit run gets the variable names a bus session's pi gets, apart from a named list, and the runs themselves hold them", SKIP, t => { +// 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, {}); - const dir = mkdtempSync(join(s.root, "env-")); + 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 json of [false, true]) { - const bus = kept(Object.keys(busEnv(s, dir, { tools: ["read", "bash"], thinking: "low", json }))); - const fitNames = Object.keys(fitModule.fitEnv(dir, { view: "/fit/pi", shape: { tools: "read,bash", thinking: "low", json } })).sort(); - assert.deepEqual(fitNames.filter((n) => !bus.includes(n)), [], `fit names a bus session lacks (json ${json})`); - assert.deepEqual(bus.filter((n) => !fitNames.includes(n)), [], `bus names a fit run lacks (json ${json})`); - names[json ? "json" : "other"] = bus; + 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); } - // A run that isn't session-shaped gets the same names as one without --mode json. - assert.deepEqual(Object.keys(fitModule.fitEnv(dir, { view: "/fit/pi" })).sort(), names.other); - assert.ok(!names.other.includes("PI_CODING_AGENT_DIR")); // 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; expecting one name more is refused. - assert.equal(fit(standIn(t, { leakEnv: names }).env).ok, true); - for (const extra of [{ json: names.json, other: [...names.other, "PI_CODING_AGENT_DIR"] }, { json: [...names.json, "PI_CODING_AGENT_DIR"], other: names.other }]) { - assert.match(fit(standIn(t, { leakEnv: extra }).env).message ?? "passed", /loaded a planted /); + // 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, t => { - assert.equal(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(fit(standIn(t, { extraFlags: ["--no-mcp"] }).env).ok, true); +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, t => { - assert.match(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 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, t => { - assert.equal(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(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 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, t => { - assert.equal(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 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, t => { +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 = fit(standIn(t, { versionHang: true }).env); + 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, t => { - assert.equal(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"); +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(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"); + 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, t => { +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(fit(s.env).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files, --no-approve"); + 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"] }); - assert.equal(fit(aliased.env).ok, true, fit(aliased.env).message); + 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, t => { +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 = pi.preflight({ env: { ...CREDENTIAL, ...s.env } }); + 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, t => { +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(fit(s.env).message, "pi (MOSAIC_PI_BIN) lacks --no-skills"); + assert.equal((await fit(s.env)).message, "pi (MOSAIC_PI_BIN) lacks --no-skills"); const quiet = standIn(t, { versionExit: 1 }); - const r = fit(quiet.env); + 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, t => { +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 = fit(s.env); + const first = await fit(s.env); assert.equal(first.ok, true, first.message); - assert.equal(fit(s.env), first, "the second call reuses the pass"); + 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(fit(s.env).message, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-themes"); + 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(fit(s.env).ok, true, "the refusal was not kept"); + 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, t => { +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 = piFit(found, { levels: LEVELS }); + const first = await piFit(found, { levels: LEVELS }); assert.equal(first.ok, true, first.message); - assert.equal(piFit(found, { levels: LEVELS }), first); + assert.equal(await piFit(found, { levels: LEVELS }), first); writeFileSync(lock, '{"packages":{}}'); - const again = piFit(found, { levels: LEVELS }); + 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(piFit(found, { levels: LEVELS }), again, "a changed package-lock.json beside node_modules is checked again"); + 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, t => { +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(fit(s.env).noMcp, true); + 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 plain = pi.env({ policy: { tools: ["read"] }, tools: [], session: { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }, env: standIn(t, {}).env }); + 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"); }); -test("pi-unpin S3: env() refuses a binary that fails the fit check, naming it by its label", SKIP, t => { +// 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"] }); - assert.throws(() => pi.env({ policy: { tools: ["read"] }, tools: [], session: { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }, env: s.env }), { message: "pi 2.0.0 (MOSAIC_PI_BIN) lacks --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"); }); -test("pi-unpin S3: a check that cannot run refuses", SKIP, t => { +// 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 r = piFit(locate(s.env), { levels: LEVELS, bwrap: join(s.root, "no-bwrap") }); + 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(piFit(locate(s.env), { levels: LEVELS, bwrap: broken }), { ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed" }); + 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(piFit(found, { levels: LEVELS }).message, "pi (MOSAIC_PI_BIN) can't be checked: its files can't be read"); + assert.equal((await piFit(found, { levels: LEVELS })).message, "pi (MOSAIC_PI_BIN) can't be checked: its files can't be read"); }); -test("pi-unpin S3: the fit check leaves no scratch behind", SKIP, t => { +// 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-")); - fit(standIn(t, { ignoreAll: true }).env); + 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); }); -test("pi-unpin S3: real pi 0.85.1 from node_modules is accepted", SKIP, t => { +// 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 r = piFit(found, { levels: LEVELS }); + 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(piFit(found, { levels: LEVELS }), r); + 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`); + 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: the adapter passes --no-mcp only when the module asks for it", SKIP, t => { +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); +}); + +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 = (s) => { + 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 }); @@ -466,19 +624,24 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it" assert.equal(r.status, 0, r.stderr); return readFileSync(out, "utf8").split("\n"); }; - assert.equal(run(standIn(t, { extraFlags: ["--no-mcp"] })).filter((a) => a === "--no-mcp").length, 1); - assert.equal(run(standIn(t, {})).includes("--no-mcp"), false); + 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: every ENV_ALLOW name set, sessionSetenv, adapterEnv, pi.env()), so -// HOME, TERM and the rest are set as a session sets them, not by the shell. Each argv must be one +// 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. Every $NAME or ${NAME +// 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 @@ -488,7 +651,7 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it" 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, t => { +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"]; @@ -513,16 +676,19 @@ test("pi-unpin S3 re-check 6: every argv the adapter builds in a bus session's e 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 } = run; + 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, { tools, thinking, provider, json, prompt }); + 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"); @@ -548,7 +714,8 @@ test("pi-unpin S3 re-check 6: every argv the adapter builds in a bus session's e seenLevels.add(thinking); seenProviders.add(provider); } - assert.equal(runs.length, 2 * 3 * 3 * 2 * 2 + 2 * (LEVELS.length - 1) + 2 * (PROVIDERS.length - 1)); + 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) { 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 index 6160b4a37..2209d81f6 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -39,7 +39,9 @@ // `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. `leakNoMatch`: with -c, every +// 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 @@ -122,6 +124,7 @@ if (rpc || print) { } 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"]); 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 c10ccf246..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; 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 8000175ac..5c85e306d 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -246,7 +246,8 @@ 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 that +- `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`. Pi (#1873 pi-unpin, `harness/pi/binary.mjs`) takes `MOSAIC_PI_BIN`, @@ -261,17 +262,26 @@ export returns: 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` and `env` then run + repository's pi", never a host path. Pi's `preflight` then awaits the fit check (#1873 S3, `packages/harness/src/pi-fit.mjs`), which - refuses `harness-feature-missing` when the binary lacks a flag the + 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. -- `version({env})` is what the manifest records as `harnessVersion`. Pi's - is the first line its binary prints for `--version`, or null. + 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 index fb67592f0..1b392f81c 100644 --- a/packages/plugins/harness/pi/binary.mjs +++ b/packages/plugins/harness/pi/binary.mjs @@ -5,7 +5,7 @@ import { accessSync, constants, readFileSync, realpathSync, statSync } from "nod import { dirname, isAbsolute, join, relative } from "node:path"; import { homedir } from "node:os"; import { fileURLToPath } from "node:url"; -import { spawnSync } from "node:child_process"; +import { spawn } from "node:child_process"; const REPO = fileURLToPath(new URL("../../../../", import.meta.url)); export const PACKAGE = "@earendil-works/pi-coding-agent"; @@ -111,15 +111,43 @@ export function packageVersion(bin) { } } +// 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. +// 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 null; - const r = spawnSync(bin, ["--version"], { encoding: "utf8", timeout: 5000, killSignal: "SIGKILL", env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" } }); - if (r.status !== 0 || typeof r.stdout !== "string") return null; - const line = r.stdout.split("\n")[0].trim(); - return cleanLabel(line) ? line : null; + 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/index.mjs b/packages/plugins/harness/pi/index.mjs index 647c45410..8ffc361be 100644 --- a/packages/plugins/harness/pi/index.mjs +++ b/packages/plugins/harness/pi/index.mjs @@ -8,7 +8,7 @@ import { dirname, join } from "node:path"; import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; import { locate, printedVersion } from "./binary.mjs"; -import { piFit } from "../../../harness/src/pi-fit.mjs"; +import { fitVerdict, piFit } from "../../../harness/src/pi-fit.mjs"; import { envNames, harnessEnv } from "./session-env.mjs"; export const interfaceVersion = 1; @@ -32,8 +32,9 @@ 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 the pi binary.mjs finds, from the package directory 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" }; @@ -41,18 +42,26 @@ export function preflight({ env = {}, proxy = null } = {}) { 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. - const fit = piFit(found, { levels: LEVELS }); + // 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; } // #1873 S1: what the found binary prints for --version (5 s timeout), or null. A label only: -// nothing compares it to a fixed version. -export function version({ env = {} } = {}) { +// 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; // #1873 S2: the found pi's package directory (the repository's copy: all of node_modules, as before). @@ -122,13 +131,15 @@ export function bundle({ dir, models, proxy = null, transports = {} } = {}) { // (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. -// Should they ever differ, env() still runs the fit check on what it found and throws, so the -// launch fails closed (as harness-failed, not harness-feature-missing). +// 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 (cached from preflight). - const fit = piFit(at, { levels: LEVELS }); - if (!fit.ok) throw new Error(fit.message); + // #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 }); } 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 b301980b4..311b58a15 100644 --- a/packages/plugins/tests/harness.test.mjs +++ b/packages/plugins/tests/harness.test.mjs @@ -101,9 +101,9 @@ test("row 65 claude-code version asks MOSAIC_CLAUDE_BIN when set, else claude on 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"); + 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(pi.version({ env: {} }), installed); + 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))]); }); @@ -111,6 +111,8 @@ test("row 65 pi preflight refuses missing credentials, and its version is what t 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"]); @@ -147,7 +149,7 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort, 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 env() runs. + // #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")); @@ -170,6 +172,7 @@ 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, env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } })), "--thinking"), thinking); diff --git a/packages/plugins/tests/pi-binary.test.mjs b/packages/plugins/tests/pi-binary.test.mjs index 853db3354..9c61a5830 100644 --- a/packages/plugins/tests/pi-binary.test.mjs +++ b/packages/plugins/tests/pi-binary.test.mjs @@ -53,29 +53,29 @@ const emptyStore = (t) => { 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(pi.version({ env: {} }), "9.9.9"); + 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(pi.version({ env: {} }), null); + 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(pi.version({ env: {} }), null); + assert.equal(await pi.version({ env: {} }), null); const long = await copyRepo(t, `#!/bin/sh\necho ${"9".repeat(129)}\n`, "1.0.0"); - assert.equal(long.pi.version({ env: {} }), null); + 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(pi.version({ env: {} }), installed); + 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 @@ -96,14 +96,15 @@ function prefix(t, script = ARGV_SCRIPT, version = "7.7.0") { 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(real.preflight({ env }), null); - assert.equal(real.version({ env }), "7.7.7"); + 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"); @@ -145,18 +146,18 @@ test("pi-unpin S2: a relative or non-executable MOSAIC_PI_BIN is refused with ha 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 = real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: bin, PATH: `${join(p.root, "bin")}:/usr/bin:/bin` } }); + 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(real.version({ env: { MOSAIC_PI_BIN: bin } }), null, "no fallback to another pi"); + 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(real.preflight({ env }), null); - assert.equal(real.version({ env }), "7.7.7"); + 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)); }); @@ -164,7 +165,7 @@ test("pi-unpin S2: with MOSAIC_PI_BIN unset the first pi on PATH is used, and th 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(real.preflight({ env }), null, 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)); @@ -179,15 +180,15 @@ test("pi-unpin S2: a pi that is not in a pi package is refused, and so is a pi t mkdirSync(dir); writeFileSync(join(dir, "pi"), "#!/bin/sh\necho 1.0.0\n"); chmodSync(join(dir, "pi"), 0o755); - let problem = real.preflight({ env: { ...CREDENTIAL, PATH: `${dir}:/usr/bin:/bin` } }); + 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 = real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(dir, "pi") } }); + 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 = pi.preflight({ env: { ...CREDENTIAL, PATH: "/usr/bin:/bin" } }); + 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/); }); @@ -200,16 +201,16 @@ test("pi-unpin S2 review: a binary that isn't the bin its package names is refus 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 = real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } }); + 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 = real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } }); + 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(real.version({ env: { MOSAIC_PI_BIN: cli } }), "1.2.3"); + 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/webui/src/triage-host.mjs b/packages/webui/src/triage-host.mjs index 8fe9c0c4c..bb28323ad 100644 --- a/packages/webui/src/triage-host.mjs +++ b/packages/webui/src/triage-host.mjs @@ -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 2dc844efb..a64bb5da5 100644 --- a/packages/webui/tests/triage-host.test.mjs +++ b/packages/webui/tests/triage-host.test.mjs @@ -413,6 +413,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" }, })); -- 2.54.0 From 2d065b936e908a54d0fd62e2918840e64f0b91c6 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 22:23:57 -0600 Subject: [PATCH 14/16] fix(pi): pi-unpin S3 re-check 8: the launcher starts pi's fit check in the background when it starts, through one harness load helper a launch shares, so the first launch joins the check in flight; nothing waits on it, a failed warm-up is dropped with no cached verdict and no unhandled rejection; a fit check that throws refuses as harness-feature-missing with no path in its line; tests for the launcher's rejected preflight and the Triage host's wait on ready(); the fit's provider and model move to the pi module's session-env.mjs (#1873) --- packages/cli/src/launcher.mjs | 30 ++++++- packages/cli/tests/launcher.test.mjs | 90 ++++++++++++++++++- .../cli/tests/path-redaction-labels.test.mjs | 11 +++ packages/harness/src/pi-fit.mjs | 28 +++--- packages/harness/tests/pi-fit.test.mjs | 19 ++++ packages/plugins/harness/pi/session-env.mjs | 5 ++ packages/webui/tests/triage-host.test.mjs | 20 +++++ 7 files changed, 188 insertions(+), 15 deletions(-) diff --git a/packages/cli/src/launcher.mjs b/packages/cli/src/launcher.mjs index 087d9ddf7..8445d2f72 100644 --- a/packages/cli/src/launcher.mjs +++ b/packages/cli/src/launcher.mjs @@ -461,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; @@ -1677,6 +1674,31 @@ 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. + function warmHarnesses() { + const on = Array.isArray(b.plugins?.harness) ? b.plugins.harness : []; + for (const name of on) { + (async () => { + const { module } = await freshHarness({ harness: name }, () => {}); + 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/launcher.test.mjs b/packages/cli/tests/launcher.test.mjs index 245bff039..2f1248314 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"; @@ -25,6 +26,7 @@ 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)); @@ -293,6 +295,92 @@ test("pi-unpin: a pi named by MOSAIC_PI_BIN that exits 1 on --version records a 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); +}); + 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/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 2239f515d..1259aae64 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -88,7 +88,7 @@ import { dirname, join, relative } from "node:path"; import { BWRAP, checkArgv, sessionSetenv } from "../../seat/src/sandbox.mjs"; import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; import { adapterEnv } from "./runner.mjs"; -import { harnessEnv } from "../../plugins/harness/pi/session-env.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"]); @@ -97,8 +97,9 @@ const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "-- // 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. -export const SESSION_SHAPE = Object.freeze({ tools: "read,bash", thinking: "low", provider: "anthropic", model: "claude-sonnet-4-5" }); +// 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 @@ -153,7 +154,8 @@ const fitKey = (found, { levels, bwrap, node, host }) => JSON.stringify([filesDi // 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. Returns a promise of // { ok: true, version, noMcp, digest } or { ok: false, message }. A call while a check with the -// same key runs gets that check's promise. A throw rejects it. +// 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, node = process.execPath, host = process.env } = {}) { const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`; let key; @@ -165,12 +167,18 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa if (passed.has(key)) return Promise.resolve(passed.get(key)); if (running.has(key)) return running.get(key); const job = (async () => { - // The sandbox's own error text can name a host path, so the line keeps only what failed. - const problem = await sandboxFails({ bwrap, node }); - if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem}` }; - const result = await check(found, { levels, bwrap, node, who, digest: JSON.parse(key)[0], host }); - if (result.ok) passed.set(key, result); - return result; + try { + // The sandbox's own error text can name a host path, so the line keeps only what failed. + const problem = await sandboxFails({ bwrap, node }); + if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem}` }; + const result = await check(found, { levels, bwrap, node, who, digest: JSON.parse(key)[0], host }); + if (result.ok) passed.set(key, result); + return result; + } 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; diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 2e5278c61..dd0fbd67f 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -610,6 +610,25 @@ test("pi-unpin S3 re-check 7: two launches that start while a fit check runs sha 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. +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) => { diff --git a/packages/plugins/harness/pi/session-env.mjs b/packages/plugins/harness/pi/session-env.mjs index c50afc8c9..0450b5c01 100644 --- a/packages/plugins/harness/pi/session-env.mjs +++ b/packages/plugins/harness/pi/session-env.mjs @@ -24,3 +24,8 @@ export function harnessEnv({ bin, noMcp, tools, extensions, session, proxy = nul // #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. +export const FIT_ROUTE = Object.freeze({ provider: "anthropic", model: "claude-sonnet-4-5" }); diff --git a/packages/webui/tests/triage-host.test.mjs b/packages/webui/tests/triage-host.test.mjs index a64bb5da5..336759ac5 100644 --- a/packages/webui/tests/triage-host.test.mjs +++ b/packages/webui/tests/triage-host.test.mjs @@ -279,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) => () => { -- 2.54.0 From e52a6b714f8de53e37a64395637ac9f9fc8789a4 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 23:15:51 -0600 Subject: [PATCH 15/16] fix(pi): pi-unpin S3 re-check 9: a fit check dies with the process that runs it: its command line runs under setpriv's parent-death signal and a shell that checks its parent is still that process, then unshare puts bwrap as pid 1 of a fresh pid namespace with --kill-child, so a process bwrap leaves in its setup dies with the namespace, and bwrap gets --die-with-parent; each run's scratch directory carries its process's pid and start time and a check first removes those a process no longer running left; the fit route is zai glm-5.3-flash, with a test that goes red on a Claude model or Anthropic provider; the launcher's warm-up records a harness plugin it can't load once, by code only; test 655 waits for pi's ready() before its timed part, with its own timeout raised (#1873) --- .../bus/tests/model-hire-ownership.test.mjs | 9 +- packages/cli/src/launcher.mjs | 11 +- packages/cli/tests/launcher.test.mjs | 24 ++++ packages/harness/src/pi-fit.mjs | 54 ++++++++- packages/harness/tests/pi-fit.test.mjs | 110 +++++++++++++++++- packages/plugins/harness/pi/session-env.mjs | 7 +- packages/seat/src/sandbox.mjs | 22 +++- 7 files changed, 224 insertions(+), 13 deletions(-) diff --git a/packages/bus/tests/model-hire-ownership.test.mjs b/packages/bus/tests/model-hire-ownership.test.mjs index 9abdb20c4..184b7e554 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,8 +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=>{ - const f=await ownershipFixture(t),pGet=f.hold('GET'),p=f.launchAide();await pGet.entered(); +test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:120000},async t=>{ + const f=await ownershipFixture(t); + // #1873 pi-unpin re-check 9: the launch's preflight shares pi's fit check, which is cold in a + // fresh process. Wait for it here (the launcher's own warm-up), so the gate's 5 s times the launch. + await pi.ready({env:{PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR,MOSAIC_CONFIG:f.f.config,MOSAIC_ROLES_DIR:f.f.roles}}); + const 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. const hGet=f.hold('GET',pGet.ordinal+2),h=f.hire();await waitFor(()=>h.invoked,'real H Broker request returned its asynchronous lifecycle operation',5000);pGet.release(); diff --git a/packages/cli/src/launcher.mjs b/packages/cli/src/launcher.mjs index 8445d2f72..bd2dbba03 100644 --- a/packages/cli/src/launcher.mjs +++ b/packages/cli/src/launcher.mjs @@ -1687,12 +1687,19 @@ export function createLauncher({ // 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. + // 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 () => { - const { module } = await freshHarness({ harness: name }, () => {}); + 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(() => {}); } diff --git a/packages/cli/tests/launcher.test.mjs b/packages/cli/tests/launcher.test.mjs index 2f1248314..f4f060021 100644 --- a/packages/cli/tests/launcher.test.mjs +++ b/packages/cli/tests/launcher.test.mjs @@ -381,6 +381,30 @@ test("pi-unpin re-check 8: a fit check the launcher starts that fails is dropped 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/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 1259aae64..7764d9e99 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -45,7 +45,10 @@ // 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, and so the runs share no state: up to FIT_CONCURRENCY of them run at once, +// another one left. The directory's name carries the pid and start time of the process that made +// it, and each check first removes the ones 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). +// 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 @@ -85,7 +88,7 @@ import { createHash } from "node:crypto"; import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join, relative } from "node:path"; -import { BWRAP, checkArgv, sessionSetenv } from "../../seat/src/sandbox.mjs"; +import { BWRAP, UNSHARE, checkArgv, 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"; @@ -144,6 +147,44 @@ 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, is left alone. Known limit: a process in another pid namespace that +// shares this TMPDIR can't be seen, and its directories would be taken for stale. +const SCRATCH = /^mosaic-pi-fit-(\d+)-(\d+)-/; +const startTime = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19]; +let ownStart = null; +const scratchPrefix = () => `mosaic-pi-fit-${process.pid}-${ownStart ??= startTime(process.pid)}-`; +function sweepScratch(dir = tmpdir()) { + let names; + try { + names = readdirSync(dir); + } catch { + return; + } + const uid = process.getuid(); + for (const name of names) { + const m = SCRATCH.exec(name); + if (!m) 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) rmSync(at, { recursive: true, force: true }); + } 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. @@ -168,6 +209,7 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa 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 problem = await sandboxFails({ bwrap, node }); if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem}` }; @@ -215,7 +257,7 @@ async function check(found, { levels, bwrap, node, who, digest, host }) { await slot(); try { if (stop.signal.aborted) return null; - const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-")); + const scratch = mkdtempSync(join(tmpdir(), scratchPrefix())); try { plant(scratch); const env = fitEnv(scratch, { view: found.view, noMcp, shape, host }); @@ -359,6 +401,7 @@ async function judge(found, { levels, who, digest, run }) { // null, or which way it failed, without the sandbox's own text. async function sandboxFails({ bwrap, node }) { if (!existsSync(bwrap)) return "the sandbox (bwrap) is missing"; + if (!existsSync(UNSHARE)) return "the sandbox's pid namespace tool (unshare) is missing"; const r = await spawned(checkArgv([], ["/usr/bin/true"], { chdir: "/", node, bwrap }), { input: "", timeout: 10000, signal: new AbortController().signal }); return r.status === 0 ? null : "the sandbox (bwrap) failed"; } @@ -379,8 +422,9 @@ function runIn(found, { bwrap, node, scratch, env, args, 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 that lands while bwrap sets up, before setpriv -// sets the parent-death signal), and no wait here is unbounded. +// 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), and no wait +// here is unbounded. function spawned(argv, { input, timeout, signal }) { return new Promise((resolve) => { const out = []; diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index dd0fbd67f..978b21038 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -13,7 +13,7 @@ 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, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs"; +import { BWRAP, checkArgv, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs"; import { ENV_ALLOW, isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; import { adapterEnv } from "../src/runner.mjs"; @@ -548,6 +548,100 @@ test("pi-unpin S3: the fit check leaves no scratch behind", SKIP, async 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 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; @@ -613,6 +707,20 @@ test("pi-unpin S3 re-check 7: two launches that start while a fit check runs sha // 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; diff --git a/packages/plugins/harness/pi/session-env.mjs b/packages/plugins/harness/pi/session-env.mjs index 0450b5c01..518a5235f 100644 --- a/packages/plugins/harness/pi/session-env.mjs +++ b/packages/plugins/harness/pi/session-env.mjs @@ -27,5 +27,8 @@ export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_ // #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. -export const FIT_ROUTE = Object.freeze({ provider: "anthropic", model: "claude-sonnet-4-5" }); +// 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/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index c0b564c6d..d53c97dd6 100644 --- a/packages/seat/src/sandbox.mjs +++ b/packages/seat/src/sandbox.mjs @@ -203,9 +203,29 @@ export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWR // #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. Three 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. +// - unshare 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. +// - bwrap's --die-with-parent ties bwrap to unshare as well. +// The one step left open 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. +export const UNSHARE = "/usr/bin/unshare"; export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP }) { const setenv = Object.entries(env).flatMap(([k, v]) => ["--setenv", k, v]); - return [bwrap, ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command]; + const owned = [...KILL_WITH_PARENT, "/bin/sh", "-c", '[ "$PPID" = "$0" ] && exec "$@"', String(process.pid)]; + const bound = [UNSHARE, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"]; + return [...owned, ...bound, bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command]; } // #1873 triage-fences: what a model route may hand a hosted session, checked before the host -- 2.54.0 From 6ad11e6993e2d11b6daf4304d2186df9e7e5d9cc Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 00:32:39 -0600 Subject: [PATCH 16/16] fix(pi): pi-unpin S3 re-check 10: unshare's outer layer is best effort: each fit check probes it once, alone and then with bwrap inside, and runs without it when unshare is missing, older than util-linux 2.36 or refused (Ubuntu 24.04 with apparmor_restrict_unprivileged_userns=1), with bwrap's argv the same either way and no refusal; only a failed bwrap or a missing setpriv refuses, each named; the sweep stops processes of this uid whose command line names a stale scratch dir before it removes it, scratch names carry the pid namespace and a sweep skips other namespaces' dirs, and a run or probe the check kills is swept at once; test 655 waits for pi.ready() before its fixture starts the launcher (#1873) --- .../bus/tests/model-hire-ownership.test.mjs | 11 +- packages/harness/src/pi-fit.mjs | 156 +++++++++---- packages/harness/tests/pi-fit.test.mjs | 206 +++++++++++++++++- packages/seat/src/sandbox.mjs | 45 ++-- 4 files changed, 356 insertions(+), 62 deletions(-) diff --git a/packages/bus/tests/model-hire-ownership.test.mjs b/packages/bus/tests/model-hire-ownership.test.mjs index 184b7e554..525693612 100644 --- a/packages/bus/tests/model-hire-ownership.test.mjs +++ b/packages/bus/tests/model-hire-ownership.test.mjs @@ -19,11 +19,12 @@ test('655 listed duplicate with no record refuses before any model or management }); test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:120000},async t=>{ - const f=await ownershipFixture(t); - // #1873 pi-unpin re-check 9: the launch's preflight shares pi's fit check, which is cold in a - // fresh process. Wait for it here (the launcher's own warm-up), so the gate's 5 s times the launch. - await pi.ready({env:{PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR,MOSAIC_CONFIG:f.f.config,MOSAIC_ROLES_DIR:f.f.roles}}); - const pGet=f.hold('GET'),p=f.launchAide();await pGet.entered(); + // #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. const hGet=f.hold('GET',pGet.ordinal+2),h=f.hire();await waitFor(()=>h.invoked,'real H Broker request returned its asynchronous lifecycle operation',5000);pGet.release(); diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 7764d9e99..5207e7acd 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -45,9 +45,11 @@ // 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 and start time of the process that made -// it, and each check first removes the ones 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). +// 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 @@ -85,10 +87,10 @@ // check can catch. import { spawn } from "node:child_process"; import { createHash } from "node:crypto"; -import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; +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, UNSHARE, checkArgv, sessionSetenv } from "../../seat/src/sandbox.mjs"; +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"; @@ -152,12 +154,23 @@ const running = new Map(); // 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, is left alone. Known limit: a process in another pid namespace that -// shares this TMPDIR can't be seen, and its directories would be taken for stale. -const SCRATCH = /^mosaic-pi-fit-(\d+)-(\d+)-/; +// 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]; -let ownStart = null; -const scratchPrefix = () => `mosaic-pi-fit-${process.pid}-${ownStart ??= startTime(process.pid)}-`; +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 { @@ -166,9 +179,11 @@ function sweepScratch(dir = tmpdir()) { return; } const uid = process.getuid(); + const ns = pidNamespace("self"); + const stale = []; for (const name of names) { const m = SCRATCH.exec(name); - if (!m) continue; + if (!m || m[3] !== ns) continue; try { let alive; try { @@ -180,7 +195,35 @@ function sweepScratch(dir = tmpdir()) { if (alive) continue; const at = join(dir, name); const st = lstatSync(at); - if (st.isDirectory() && st.uid === uid) rmSync(at, { recursive: true, force: true }); + 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 {} } } @@ -189,19 +232,21 @@ function sweepScratch(dir = tmpdir()) { 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, node, host }) => JSON.stringify([filesDigest(found), found.root, found.dest ?? null, found.view, bwrap, node, levels, fitNames(host)]); +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. Returns a promise of -// { ok: true, version, noMcp, digest } or { ok: false, message }. 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 +// 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, node = process.execPath, host = process.env } = {}) { +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, node, host }); + 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` }); } @@ -211,11 +256,13 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa try { sweepScratch(); // The sandbox's own error text can name a host path, so the line keeps only what failed. - const problem = await sandboxFails({ bwrap, node }); - if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem}` }; - const result = await check(found, { levels, bwrap, node, who, digest: JSON.parse(key)[0], host }); - if (result.ok) passed.set(key, result); - return result; + 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. @@ -228,15 +275,15 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa // 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, node = process.execPath, host = process.env } = {}) { +export function fitVerdict(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) { try { - return passed.get(fitKey(found, { levels, bwrap, node, host })) ?? null; + return passed.get(fitKey(found, { levels, bwrap, unshare, node, host })) ?? null; } catch { return null; } } -async function check(found, { levels, bwrap, node, who, digest, host }) { +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. @@ -258,15 +305,20 @@ async function check(found, { levels, bwrap, node, who, digest, host }) { 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 = await runIn(found, { bwrap, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout, signal: stop.signal }); + 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 { @@ -396,14 +448,40 @@ async function judge(found, { levels, who, digest, run }) { return { ok: true, version, noMcp, digest }; } -// Whether bwrap runs here, as sandbox.mjs's sandboxProblem asks, but as a child process the caller -// awaits: the system part, the network unshared, an empty environment, then /usr/bin/true. Returns -// null, or which way it failed, without the sandbox's own text. -async function sandboxFails({ bwrap, node }) { - if (!existsSync(bwrap)) return "the sandbox (bwrap) is missing"; - if (!existsSync(UNSHARE)) return "the sandbox's pid namespace tool (unshare) is missing"; - const r = await spawned(checkArgv([], ["/usr/bin/true"], { chdir: "/", node, bwrap }), { input: "", timeout: 10000, signal: new AbortController().signal }); - return r.status === 0 ? null : "the sandbox (bwrap) failed"; +// 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 @@ -411,20 +489,20 @@ async function sandboxFails({ bwrap, node }) { // 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, node, scratch, env, args, input, timeout, signal }) { +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 }), { input, timeout, signal }); + 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), and no wait -// here is unbounded. +// 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 = []; diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 978b21038..bc01e6c62 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -4,7 +4,7 @@ // 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, realpathSync, rmSync, symlinkSync, watch, writeFileSync } from "node:fs"; +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"; @@ -13,7 +13,7 @@ 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, checkArgv, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.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"; @@ -642,6 +642,208 @@ test("pi-unpin S3 re-check 9: a fit run starts only under the process that built 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; diff --git a/packages/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index d53c97dd6..d71d730fe 100644 --- a/packages/seat/src/sandbox.mjs +++ b/packages/seat/src/sandbox.mjs @@ -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. @@ -205,28 +206,40 @@ export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWR // 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. Three steps make sure of that: +// 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. -// - unshare 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. -// - bwrap's --die-with-parent ties bwrap to unshare as well. -// The one step left open 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. +// - 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"; -export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP }) { +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]); - const owned = [...KILL_WITH_PARENT, "/bin/sh", "-c", '[ "$PPID" = "$0" ] && exec "$@"', String(process.pid)]; - const bound = [UNSHARE, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"]; - return [...owned, ...bound, bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command]; + 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 -- 2.54.0