diff --git a/packages/cli/tests/launcher.test.mjs b/packages/cli/tests/launcher.test.mjs index f4f060021..1b6ef2542 100644 --- a/packages/cli/tests/launcher.test.mjs +++ b/packages/cli/tests/launcher.test.mjs @@ -25,7 +25,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 { fixtureDir, 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"; @@ -82,7 +82,9 @@ function commit(dir, text) { // Every credential variable of the shell running the tests is dropped, pi's own provider keys // included (row 332 R2); a pi chief gets one made-up pi key instead. function prepare(t, more = (doc) => doc) { - const root = tmp(t, "mosaic-launch-"); + // pi-fit-tmp re-check: its pi stand-ins sit under root, which must not be under /tmp. + const root = fixtureDir("mosaic-launch-"); + t.after(() => rmSync(root, { recursive: true, force: true })); const f = fixture(root, "acme", (doc) => more(edit(doc))); withoutHostCredentials(f.env, { piKey: f.doc.roles.chief.vars.harness === "pi" || f.doc.roles.chief.vars["fallback.harness"] === "pi" }); f.env.CLAUDE_CODE_OAUTH_TOKEN = "stand-in"; @@ -332,7 +334,9 @@ function busPiBin(t, prepared, bin) { 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-"), {}); + const aloneRoot = fixtureDir("mosaic-launch-pi-"); + t.after(() => rmSync(aloneRoot, { recursive: true, force: true })); + const alone = piPrefix(aloneRoot, {}); await quiet(); made.clear(); assert.equal(await pi.preflight({ env: { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: alone } }), null); diff --git a/packages/cli/tests/pi-unpin-s5.test.mjs b/packages/cli/tests/pi-unpin-s5.test.mjs index e2eccd1b3..5d2cc3dcf 100644 --- a/packages/cli/tests/pi-unpin-s5.test.mjs +++ b/packages/cli/tests/pi-unpin-s5.test.mjs @@ -12,7 +12,7 @@ import { tmpdir } from "node:os"; import { spawnSync } from "node:child_process"; import { fileURLToPath } from "node:url"; import { sandboxProblem } from "../../seat/src/sandbox.mjs"; -import { piStandIn } from "../../harness/tests/pi-stand-in.mjs"; +import { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs"; const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url))); const missing = (e) => { if (e.code !== "ERR_MODULE_NOT_FOUND") throw e; return {}; }; @@ -31,7 +31,7 @@ const lockOf = (version) => ({ name: "fixture", lockfileVersion: 3, requires: tr // A checkout with package.json's range, a lock that resolves `version`, and RELEASE. function checkout(t, { version = "9.9.9", release = "0.0.13" } = {}) { - const root = mkdtempSync(join(tmpdir(), "s5-")); + const root = fixtureDir("s5-"); t.after(() => rmSync(root, { recursive: true, force: true })); writeFileSync(join(root, "package.json"), JSON.stringify({ name: "fixture", dependencies: { [NAME]: RANGE } })); writeFileSync(join(root, "package-lock.json"), JSON.stringify(lockOf(version))); @@ -267,7 +267,7 @@ test("pi-unpin S5: the image fit step can't tell, and refuses, when node_modules // scripts/agent-host-dev.sh --check in a checkout with the range in package.json and a stand-in pi. function hostCheckout(t, piScript) { - const root = mkdtempSync(join(tmpdir(), "s5h-")); + const root = fixtureDir("s5h-"); t.after(() => rmSync(root, { recursive: true, force: true })); for (const d of ["scripts", "agents/fixture", "contracts", "user", "node_modules/.bin", "extensions/goal"]) mkdirSync(join(root, d), { recursive: true }); cpSync(join(REPO, "scripts/agent-host-dev.sh"), join(root, "scripts/agent-host-dev.sh")); diff --git a/packages/conversation/README.md b/packages/conversation/README.md index ce3c914df..433debc46 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -328,8 +328,9 @@ can disagree with it. repository's `.bin`, then this process's PATH, as `sessionEnv` builds it), and it looks inside the fit check's sandbox, which shows only `/usr`, `/etc`, the host's `/bin`, `/lib`, `/lib64` and `/sbin`, and this Node's - own file at its path unless that path is under `/tmp` (the sandbox's fresh - `/tmp` covers it). Up to the first `node`, every entry must read the + own file at its path unless that path is under `/tmp` (a session's sandbox + mounts its private `/tmp` over it, and the fit check stays undecided for a + node there). Up to the first `node`, every entry must read the same in the sandbox as on the host: absolute (env reads an empty or relative one against the fit check's directory), already in plain form (a `..` after a link goes where the link points), and not in `/proc` or diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index e1cc36068..a5f0fd060 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -174,10 +174,12 @@ const shownDir = (p) => SHOWN_DIRS.some((d) => p === d || p.startsWith(d + "/")) // The sandbox makes its own /proc and /dev, so a path there names something // else inside it than on the host. Its /tmp is another directory too, but one // that starts empty (pi-fit.mjs makes it fresh for each run), so an entry -// there holds no node in the sandbox, and a node the host finds there is one -// the sandbox can't show. That holds for this Node's own file too: pi-fit.mjs -// puts the fresh /tmp in place after it mounts that file, so an engine whose -// node sits under /tmp is hidden in the sandbox. +// there holds no node in the sandbox, and a node the host finds there counts +// as one the sandbox can't show. That holds for this Node's own file too. The +// fit sandbox now mounts that file over its fresh /tmp (pi-fit-tmp's +// mountOrder), but a session's sandbox mounts its private /tmp last and +// covers it, and the fit check stays undecided for a node under /tmp, so an +// engine whose node sits under /tmp still can't run. const OWN_COPY_DIRS = ["/proc", "/dev"]; const underTmp = (p) => p === "/tmp" || p.startsWith("/tmp/"); function shownInFit(p, hops = 0) { diff --git a/packages/conversation/tests/harness.mjs b/packages/conversation/tests/harness.mjs index d67a2d915..998466095 100644 --- a/packages/conversation/tests/harness.mjs +++ b/packages/conversation/tests/harness.mjs @@ -11,7 +11,7 @@ import { ConversationClient } from "../src/client.mjs"; import { AUTHORITY } from "../src/cohort.mjs"; import { FixtureVerifier } from "../src/records.mjs"; import { FakeLauncher } from "./fake-pi.mjs"; -import { piStandIn } from "../../harness/tests/pi-stand-in.mjs"; +import { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs"; import { locate } from "../../plugins/harness/pi/binary.mjs"; export const REPO = resolve(import.meta.dirname, "..", "..", ".."); @@ -19,7 +19,7 @@ export const SESSION_ID = "0f5e1c2a-1111-4222-8333-944455556666"; export const FAST = Object.freeze({ ack: 1500, state: 1500, start: 1500, clear: 1500, abort: 3000, settle: 3000, grace: 50, write: 1500, maxRounds: 3 }); export const time = (i) => new Date(Date.UTC(2026, 9, 4, 12, 0, 0) + i * 1000).toISOString(); -const tmp = mkdtempSync(join(tmpdir(), "chat03-")); +const tmp = fixtureDir("chat03-"); let serial = 0; // #1873 S4: a stand-in pi package (packages/harness/tests/pi-stand-in.mjs, diff --git a/packages/conversation/tests/smoke.test.mjs b/packages/conversation/tests/smoke.test.mjs index 3684e256e..bb0e22275 100644 --- a/packages/conversation/tests/smoke.test.mjs +++ b/packages/conversation/tests/smoke.test.mjs @@ -21,13 +21,15 @@ import { piFit } from "../../harness/src/pi-fit.mjs"; import { locate } from "../../plugins/harness/pi/binary.mjs"; import { levels } from "../../plugins/harness/pi/index.mjs"; import { FakePi } from "./fake-pi.mjs"; -import { assistantEntry, header, thinkingEntry, userEntry } from "./harness.mjs"; +import { assistantEntry, cleanupAll, header, thinkingEntry, userEntry } from "./harness.mjs"; // The repository's pi: with no MOSAIC_PI_BIN and no PATH the lookup ends there. const REPO_PI = locate({}); const scratch = mkdtempSync(join(tmpdir(), "chat03-smoke-")); after(() => rmSync(scratch, { recursive: true, force: true })); +// harness.mjs makes its fixture directory when it loads; remove it too. +after(cleanupAll); const COMMANDS = ["get_state", "get_commands", "clear_queue", "abort", "get_tree"]; const exchanges = {}; diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index 4990d1023..6ce697092 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -125,7 +125,7 @@ import { spawn } from "node:child_process"; import { createHash } from "node:crypto"; import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { dirname, join, relative } from "node:path"; +import { dirname, join, posix, relative } from "node:path"; import { fileURLToPath } from "node:url"; import { BWRAP, RUNNER_FILES, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs"; import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; @@ -258,7 +258,11 @@ const startTime = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ") 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()) { +// pi-fit-tmp: where the scratch directories go: the temp directory's real path, so the paths a +// run is given and the paths bwrap mounts are the same spelling when TMPDIR is a link, and the +// sweep matches a left process's command line. tmpdir() itself when that can't be resolved. +const scratchBase = () => { try { return realpathSync(tmpdir()); } catch { return tmpdir(); } }; +function sweepScratch(dir = scratchBase()) { let names; try { names = readdirSync(dir); @@ -347,6 +351,12 @@ export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, no } catch { return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read`, digest: null, undecided: true }); } + // pi-fit-tmp review P3-1: the fit sandbox orders its binds so nothing under /tmp is hidden, but + // a session's sandbox (binds()) mounts its private /tmp last and covers whatever lies under it. + // A pi mounted there, or a node there, would pass the check and then fail every session, so the + // check stays undecided for it, as it was before the fit sandbox's binds were ordered. + const covered = sessionCovers(found.dest ?? found.root) ? "the pi's package" : sessionCovers(node) ? "node" : null; + if (covered) return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: ${covered} sits under the system temp directory, which a session's sandbox covers`, digest: JSON.parse(key)[0], undecided: true }); if (passed.has(key)) return Promise.resolve(passed.get(key)); if (running.has(key)) return share(running.get(key), signal, who, key); const cancel = new AbortController(); @@ -437,7 +447,7 @@ async function check(found, { levels, bwrap, unshare, node, who, digest, host, c await slot(); try { if (stop.signal.aborted) return null; - const scratch = mkdtempSync(join(tmpdir(), `${scratchPrefix()}${model ? "model-" : ""}`)); + const scratch = mkdtempSync(join(scratchBase(), `${scratchPrefix()}${model ? "model-" : ""}`)); let ran = null; try { if (model) { @@ -490,7 +500,7 @@ async function judge(found, { levels, who, digest, run }) { // refuse: a definite failure. unsure: the check could not tell. const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}`, digest, undecided: false }); const unsure = (why) => ({ ok: false, message: `${who(version)} can't be checked: ${why}`, digest, undecided: true }); - if (help.status !== 0) return unsure("--help failed"); + if (help.status !== 0) return unsure(`--help failed (${ended(help)})`); const lines = help.stdout.split("\n"); const has = (flag) => lines.some((l) => optionsIn(l).includes(flag)); const line = (flag) => lines.find((l) => optionsIn(l).includes(flag)) ?? ""; @@ -647,7 +657,7 @@ function judgeModel(m) { 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())); + const scratch = mkdtempSync(join(scratchBase(), scratchPrefix())); let stopped = false; try { const probe = async (argv) => { @@ -688,8 +698,8 @@ function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout return spawned(checkArgv(list, [...prefix, "/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 +// One child process with an empty environment, awaited: its exit status, the signal that ended it +// (pi-fit-tmp), its stdout, and why it was killed, or null. It settles PIPE_GRACE after the child exits or is killed even if its pipes stay // open: a process left behind can hold them (a kill of bwrap's outer process while its child still // sets up leaves that child waiting until the pid namespace checkArgv makes is killed, or, without // that layer, until the next check's sweep), and no wait here is unbounded. @@ -712,7 +722,7 @@ function spawned(argv, { input, timeout, signal }) { child?.stderr?.destroy(); signal.removeEventListener("abort", onAbort); if (!stopped && sig === "SIGKILL") stopped = "timed out"; - resolve({ status, stdout: Buffer.concat(out).toString("utf8"), stopped }); + resolve({ status, signal: sig ?? null, 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) => { @@ -745,6 +755,20 @@ function spawned(argv, { input, timeout, signal }) { }); } +// A path a session's private /tmp covers: /tmp itself or under it, by its spelling (what the +// session's bind names as its target). +const sessionCovers = (p) => typeof p === "string" && /^\/+tmp(\/|$)/.test(posix.normalize(p)); + +// pi-fit-tmp: how a run ended, for a line that must say enough to diagnose: why the check killed +// it, else the signal that ended it, else its exit status. Never its output, which can name a host +// path. +function ended(r) { + if (r.stopped) return r.stopped; + if (typeof r.signal === "string") return `ended by ${r.signal}`; + if (Number.isInteger(r.status)) return `exit status ${r.status}`; + return "it didn't start"; +} + // 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 diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 7ce8f9cc5..5c2235b86 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -10,10 +10,10 @@ import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import * as fitModule from "../src/pi-fit.mjs"; -import { piStandIn } from "./pi-stand-in.mjs"; +import { fixtureDir, piStandIn } from "./pi-stand-in.mjs"; import { locate } from "../../plugins/harness/pi/binary.mjs"; import * as pi from "../../plugins/harness/pi/index.mjs"; -import { BWRAP, UNSHARE, checkArgv, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs"; +import { BWRAP, UNSHARE, checkArgv, mountOrder, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs"; import { ENV_ALLOW, isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; import { adapterEnv } from "../src/runner.mjs"; import { FIT_MODEL_ROUTE } from "../../plugins/harness/pi/session-env.mjs"; @@ -29,7 +29,7 @@ const SKIP = sandboxProblem() ? { skip: "no working bwrap here" } : {}; // 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-")); + const root = fixtureDir("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 }); @@ -604,7 +604,7 @@ const procsNaming = (text) => readdirSync("/proc").filter((p) => /^\d+$/.test(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-")); + const dir = fixtureDir("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 }); @@ -711,7 +711,7 @@ test("pi-unpin S3 re-check 10: bwrap's command line is the same with and without // 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-")); + const dir = fixtureDir("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 }); @@ -766,7 +766,7 @@ test("pi-unpin S3 re-check 10: a failed probe names the tool that failed, and on // 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-")); + const dir = fixtureDir("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 }); @@ -847,7 +847,7 @@ exit $st // 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 dir = fixtureDir("mosaic-pi-fit-t-"); const kids = []; t.after(() => { for (const k of kids) try { k.kill("SIGKILL"); } catch {} @@ -913,7 +913,7 @@ test("pi-unpin S3: real pi 0.85.1 from node_modules is accepted, and the event l // directories: it lists both given skills and no planted one. A logging bwrap shows the run. test("pi-skills: real pi 0.85.1 passes the fit check's worker-shaped run with --no-skills and two --skill directories", SKIP, async t => { const found = locate({ PATH: "/usr/bin:/bin" }); - const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-fit-")); + const root = fixtureDir("mosaic-pi-skills-fit-"); t.after(() => rmSync(root, { recursive: true, force: true })); const log = join(root, "bwrap.log"); const bwrap = join(root, "bwrap"); @@ -1005,6 +1005,125 @@ test("pi-unpin S3 re-check 8: a fit check that throws refuses as harness-feature assert.equal(await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }), null); }); +// pi-fit-tmp: a fit run binds its scratch at its own path and the scratch's tmp/ over /tmp. bwrap +// mounts in the order given, so with the scratch under /tmp the /tmp bind hid the scratch and every +// run failed before pi started: "can't be checked: --help failed". This is the one test that writes +// under /tmp (Jason keeps agents out of it): one mkdtemp directory, removed in a finally. The +// stand-in lacks --no-themes, so the check stops after --version and --help: three scratch +// directories of a few kilobytes, not a whole check's runs. The verdict must be the one a TMPDIR +// outside /tmp gives, also when TMPDIR is a link to it, and the check must leave nothing there. +test("pi-fit-tmp: with its temp directory under /tmp the fit check gives the verdict it gives elsewhere, and leaves nothing there", SKIP, async t => { + const s = standIn(t, { dropFlags: ["--no-themes"] }); + 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 = `const { piFit } = await import(${JSON.stringify(fitUrl)}); const { locate } = await import(${JSON.stringify(binUrl)}); + const r = await piFit(locate(${JSON.stringify(s.env)}), { levels: ${JSON.stringify(LEVELS)} }); console.log(JSON.stringify(r));`; + // Each check in its own process, so the in-process pass cache can't answer for the other. + const checkWith = (tmp) => { + const r = spawnSync(process.execPath, ["--input-type=module", "-e", script], { env: { PATH: NODE_PATH, HOME: s.root, LANG: "C.UTF-8", TMPDIR: tmp }, encoding: "utf8", timeout: 60000 }); + assert.equal(r.status, 0, r.stderr); + return JSON.parse(r.stdout); + }; + const elsewhere = checkWith(s.root); + assert.deepEqual([elsewhere.ok, elsewhere.undecided, elsewhere.message], [false, false, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-themes"]); + const dir = mkdtempSync("/tmp/mosaic-fit-tmp-"); + try { + const under = checkWith(dir); + assert.deepEqual([under.ok, under.undecided, under.message], [elsewhere.ok, elsewhere.undecided, elsewhere.message]); + assert.deepEqual(readdirSync(dir), [], "the check left something in its temp directory under /tmp"); + // TMPDIR a link to the directory under /tmp, in a directory the sandbox mounts (the pi's own + // package), so bwrap follows the link: the scratch goes under the real path, so the binds' + // order sees it under /tmp. + const link = join(dirname(dirname(dirname(s.cli))), "tmp-link"); + symlinkSync(dir, link); + const linked = checkWith(link); + assert.deepEqual([linked.ok, linked.undecided, linked.message], [elsewhere.ok, elsewhere.undecided, elsewhere.message]); + assert.deepEqual(readdirSync(dir), [], "the check left something in its temp directory under /tmp"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + +// pi-fit-tmp: a bind moves ahead of the first bind whose target lies under its own; nothing else +// moves, and binds with the same target keep their order. +test("pi-fit-tmp: mountOrder puts a bind before every bind whose target lies under its own, and moves nothing else", () => { + const ro = (src, dest) => ({ kind: "ro", src, ...(dest ? { dest } : {}) }); + const rw = (src, dest) => ({ kind: "rw", src, ...(dest ? { dest } : {}) }); + const scratch = "/tmp/mosaic-pi-fit-1-2-3-x"; + assert.deepEqual(mountOrder([ro("/repo"), rw(scratch), rw(`${scratch}/tmp`, "/tmp"), ro("/opt/pi")]), + [ro("/repo"), rw(`${scratch}/tmp`, "/tmp"), rw(scratch), ro("/opt/pi")]); + // A scratch outside /tmp: nothing lies under another, so the order is the caller's. + const outside = [ro("/repo"), rw("/srv/s"), rw("/srv/s/tmp", "/tmp"), ro("/opt/pi")]; + assert.deepEqual(mountOrder(outside), outside); + // A name that only starts with the other's is not under it. + assert.deepEqual(mountOrder([ro("/tmpx/a"), rw("/x", "/tmp")]), [ro("/tmpx/a"), rw("/x", "/tmp")]); + // The same target twice: the later still comes later and covers the earlier. + assert.deepEqual(mountOrder([ro("/a", "/t"), ro("/b", "/t")]), [ro("/a", "/t"), ro("/b", "/t")]); + // Nested three deep, given innermost first. + assert.deepEqual(mountOrder([ro("/a/b/c"), ro("/a/b"), ro("/a")]), [ro("/a"), ro("/a/b"), ro("/a/b/c")]); + // A bind at / goes before everything under it. + assert.deepEqual(mountOrder([ro("/usr"), ro("/r", "/")]), [ro("/r", "/"), ro("/usr")]); + // Targets compare in plain form: a trailing slash or a "." still names the same directory. + assert.deepEqual(mountOrder([ro("/a/x"), ro("/b", "/a/")]), [ro("/b", "/a/"), ro("/a/x")]); + assert.deepEqual(mountOrder([ro("/a/x"), ro("/b", "/a/./")]), [ro("/b", "/a/./"), ro("/a/x")]); + assert.deepEqual(mountOrder([ro("/t/a"), rw("/x", "//t")]), [rw("/x", "//t"), ro("/t/a")]); + // An entry with no target, or one that isn't absolute, stays where it is and moves nothing. + const odd = [ro("/a/x"), { kind: "none" }, ro("rel"), ro("/c")]; + assert.deepEqual(mountOrder(odd), odd); + // The limit, by design: no bind stays hidden under a later one. /c at /a no longer covers /a/x. + assert.deepEqual(mountOrder([ro("/b", "/a"), ro("/a/x"), ro("/c", "/a")]), [ro("/b", "/a"), ro("/c", "/a"), ro("/a/x")]); + // In the fit check's own argv the /tmp bind comes before the scratch's. + const argv = checkArgv([rw(scratch), rw(`${scratch}/tmp`, "/tmp")], ["/usr/bin/true"], { chdir: scratch }); + const at = (src, dest) => argv.findIndex((a, i) => a === "--bind" && argv[i + 1] === src && argv[i + 2] === dest); + assert.ok(at(`${scratch}/tmp`, "/tmp") >= 0 && at(scratch, scratch) > at(`${scratch}/tmp`, "/tmp"), "the scratch bind is not after the /tmp bind"); +}); + +// pi-fit-tmp: when --help fails the line says how it ended, an exit status or a signal, and names no +// path. A bwrap stand-in fails only the --help run and passes the sandbox probe through. +test("pi-fit-tmp: a --help run that fails is undecided and its line gives the exit status or the signal, with no path", SKIP, async t => { + for (const [end, ending] of [["exit 3", "exit status 3"], ["kill -TERM $$", "ended by SIGTERM"]]) { + const s = standIn(t, {}); + const bwrap = join(s.root, "help-bwrap"); + writeFileSync(bwrap, `#!/bin/sh +case " $* " in *" --help "*) ${end};; esac +exec '${BWRAP}' "$@" +`, { mode: 0o755 }); + const r = await piFit(locate(s.env), { levels: LEVELS, bwrap, unshare: join(s.root, "no-unshare") }); + assert.deepEqual([r.ok, r.undecided], [false, true], r.message); + assert.equal(r.message, `pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: --help failed (${ending})`); + noPath(r.message, [...s.paths, bwrap]); + } +}); + +// pi-fit-tmp review P3-1: the fit sandbox now shows what lies under /tmp, but a session's sandbox +// mounts its private /tmp last and covers it. A pi mounted under /tmp, or a node there, stays +// undecided, with a line that names no path, and no run starts (the bwrap stand-in would record one). +test("pi-fit-tmp: a pi mounted under /tmp, or a node there, can't be checked, since a session's sandbox covers it", SKIP, async t => { + const s = standIn(t, {}); + const bwrap = join(s.root, "record-bwrap"); + const ran = join(s.root, "ran"); + writeFileSync(bwrap, `#!/bin/sh\necho x >> '${ran}'\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 }); + const found = locate(s.env); + const cases = [ + [{ ...found, dest: null, root: "/tmp/mosaic-fit-root" }, {}, "the pi's package"], + [{ ...found, dest: "/tmp/mosaic-fit-dest" }, {}, "the pi's package"], + [{ ...found, dest: "//tmp" }, {}, "the pi's package"], + [found, { node: "/tmp/mosaic-fit-node" }, "node"], + ]; + for (const [f, extra, what] of cases) { + const r = await piFit(f, { levels: LEVELS, bwrap, ...extra }); + assert.deepEqual([r.ok, r.undecided], [false, true], r.message); + assert.equal(r.message, `pi (MOSAIC_PI_BIN) can't be checked: ${what} sits under the system temp directory, which a session's sandbox covers`); + assert.match(r.digest, /^[0-9a-f]{64}$/); + noPath(r.message, [...s.paths, bwrap, "/tmp"]); + } + assert.equal(existsSync(ran), false, "a run started"); + // A name that only starts with tmp is not under it, and the check runs as usual. + const r = await piFit({ ...found, dest: "/tmpx/mosaic-fit-dest" }, { levels: LEVELS, bwrap }); + assert.doesNotMatch(r.message ?? "", /system temp directory/); + assert.equal(existsSync(ran), true, "no run started for /tmpx"); +}); + 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) => { @@ -1026,7 +1145,7 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it" // pi-skills: a worker's MOSAIC_SKILLS. pi 0.85.1 adds a --skill directory to discovery, so the // adapter keeps --no-skills and puts each --skill after it. A stub records the argv. const workerAdapter = (t, extra = {}) => { - const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-t-")); + const root = fixtureDir("mosaic-pi-skills-t-"); t.after(() => rmSync(root, { recursive: true, force: true })); const stub = join(root, "pi"); writeFileSync(stub, '#!/bin/sh\nprintf "%s\\0" "$@" > "$ARGV_OUT"\n', { mode: 0o755 }); @@ -1086,7 +1205,7 @@ const UNSHARE_NET = spawnSync("unshare", ["-rn", "true"]).status === 0 ? {} : { test("pi-skills: real pi 0.85.1 through the adapter with MOSAIC_SKILLS loads the given skills and no planted one", UNSHARE_NET, async t => { const found = locate({ PATH: "/usr/bin:/bin" }); assert.equal(found.label, "the repository's pi"); - const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-real-")); + const root = fixtureDir("mosaic-pi-skills-real-"); t.after(() => rmSync(root, { recursive: true, force: true })); const put = (p, text) => { mkdirSync(dirname(p), { recursive: true }); writeFileSync(p, text); }; const skill = (name) => put(join(root, ...name.split("/"), "SKILL.md"), `---\nname: ${name.split("/").pop()}\ndescription: a planted skill\n---\nA planted skill.\n`); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index ca086c500..0ad93d420 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -84,6 +84,28 @@ // 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"; import { FIT_MODEL_ROUTE } from "../../plugins/harness/pi/session-env.mjs"; +import { mkdtempSync, realpathSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +// pi-fit-tmp re-check: a new directory for a test's stand-in pi, made like +// mkdtempSync(join(tmpdir(), prefix)). The fit check is undecided for a pi under /tmp, since a +// session's sandbox mounts its private /tmp over it, so a stand-in must not sit there. When the +// temp directory's real path is under /tmp (TMPDIR unset, say), the directory goes under /var/tmp +// instead; otherwise it goes under tmpdir() as spelled, so a run with TMPDIR elsewhere is unchanged. +// With /var/tmp missing or under /tmp too, it throws, so a test fails on its fixture and not with +// the guard's line, which would point at the code under test. +const underTmp = (p) => /^\/tmp(\/|$)/.test(p); +const realOr = (p) => { try { return realpathSync(p); } catch { return null; } }; +export function fixtureDir(prefix) { + let base = tmpdir(); + if (underTmp(realOr(base) ?? base)) { + const alt = realOr("/var/tmp"); + if (alt === null || underTmp(alt)) throw new Error("fixtureDir: the temp directory sits under /tmp, where the fit check won't run a pi, and /var/tmp is missing or under /tmp too; set TMPDIR to a directory outside /tmp"); + base = "/var/tmp"; + } + return mkdtempSync(join(base, prefix)); +} const BODY = String.raw` const fs = require("node:fs"); diff --git a/packages/plugins/tests/harness-watch-w2.test.mjs b/packages/plugins/tests/harness-watch-w2.test.mjs index 8f678f8fd..ce8aaac8c 100644 --- a/packages/plugins/tests/harness-watch-w2.test.mjs +++ b/packages/plugins/tests/harness-watch-w2.test.mjs @@ -11,7 +11,7 @@ import { join, resolve } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import { tree } from "./helpers.mjs"; import { KINDS } from "../src/kinds.mjs"; -import { piStandIn } from "../../harness/tests/pi-stand-in.mjs"; +import { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs"; import { piFit } from "../../harness/src/pi-fit.mjs"; import { sandboxProblem, UNSHARE } from "../../seat/src/sandbox.mjs"; import * as pi from "../harness/pi/index.mjs"; @@ -27,7 +27,7 @@ const CREDENTIAL = { ANTHROPIC_API_KEY: "fixture-api-key" }; const DIGEST = /^[a-f0-9]{64}$/; function scratch(t) { - const root = mkdtempSync(join(tmpdir(), "mosaic-watch-")); + const root = fixtureDir("mosaic-watch-"); t.after(() => rmSync(root, { recursive: true, force: true })); return root; } diff --git a/packages/plugins/tests/harness-watch.test.mjs b/packages/plugins/tests/harness-watch.test.mjs index 7d01c8863..1b11aa5ef 100644 --- a/packages/plugins/tests/harness-watch.test.mjs +++ b/packages/plugins/tests/harness-watch.test.mjs @@ -11,7 +11,7 @@ import { join, resolve } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import { tree } from "./helpers.mjs"; import { KINDS } from "../src/kinds.mjs"; -import { piStandIn } from "../../harness/tests/pi-stand-in.mjs"; +import { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs"; import { sandboxProblem } from "../../seat/src/sandbox.mjs"; import * as pi from "../harness/pi/index.mjs"; import * as claude from "../harness/claude-code/index.mjs"; @@ -289,7 +289,9 @@ test("W1 real pi from node_modules gives a version, a digest and fit pass", NO_B // A stand-in pi package named by MOSAIC_PI_BIN, as pi-fit.test.mjs makes one. function piPackage(t, knobs) { - const root = scratch(t); + // Outside /tmp, so the fit check runs it whatever the temp directory is (pi-stand-in.mjs). + const root = fixtureDir("mosaic-watch-pi-"); + t.after(() => rmSync(root, { recursive: true, force: true })); const pkg = join(root, "prefix", "lib", "node_modules", PI_NAME); mkdirSync(join(pkg, "dist", "bundle"), { recursive: true }); writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: PI_NAME, version: "2.0.0", bin: { pi: "dist/bundle/cli.js" } })); diff --git a/packages/plugins/tests/harness.test.mjs b/packages/plugins/tests/harness.test.mjs index 311b58a15..e4975bea5 100644 --- a/packages/plugins/tests/harness.test.mjs +++ b/packages/plugins/tests/harness.test.mjs @@ -10,7 +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"; +import { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs"; const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url))); @@ -25,7 +25,7 @@ async function load(name) { } function scratch(t) { - const root = mkdtempSync(join(tmpdir(), "mosaic-harness-plugin-")); + const root = fixtureDir("mosaic-harness-plugin-"); t.after(() => rmSync(root, { recursive: true, force: true })); return root; } diff --git a/packages/plugins/tests/pi-binary.test.mjs b/packages/plugins/tests/pi-binary.test.mjs index bf3dfe87a..7f5358817 100644 --- a/packages/plugins/tests/pi-binary.test.mjs +++ b/packages/plugins/tests/pi-binary.test.mjs @@ -9,14 +9,14 @@ 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"; +import { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs"; const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url))); const MODULE = join(REPO, "packages", "plugins", "harness", "pi"); const NAME = "@earendil-works/pi-coding-agent"; function scratch(t) { - const root = mkdtempSync(join(tmpdir(), "mosaic-pi-bin-")); + const root = fixtureDir("mosaic-pi-bin-"); t.after(() => rmSync(root, { recursive: true, force: true })); return root; } diff --git a/packages/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index d71d730fe..4b278b37f 100644 --- a/packages/seat/src/sandbox.mjs +++ b/packages/seat/src/sandbox.mjs @@ -35,7 +35,7 @@ import { spawnSync } from "node:child_process"; import { existsSync, lstatSync, mkdirSync, readlinkSync, realpathSync, statSync } from "node:fs"; -import { dirname, isAbsolute, join, relative, resolve } from "node:path"; +import { dirname, isAbsolute, join, posix, relative, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { ID_PATTERN } from "../../organization/src/vocabulary.mjs"; @@ -234,9 +234,39 @@ export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWR export const UNSHARE = "/usr/bin/unshare"; const owned = () => [...KILL_WITH_PARENT, "/bin/sh", "-c", '[ "$PPID" = "$0" ] && exec "$@"', String(process.pid)]; const bound = (unshare) => unshare ? [unshare, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"] : []; +// +// pi-fit-tmp: bwrap mounts in the order it is given, and a mount hides every mount made earlier +// under its target. A fit run binds its scratch directory at its own path and the scratch's tmp/ +// over /tmp, so with the scratch under /tmp (TMPDIR unset) the second bind hid the first and the +// run couldn't even chdir. The check's binds now go through mountOrder: a bind whose target lies +// under another's comes after it, whatever the caller's temp directory is. export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP, unshare = UNSHARE }) { const setenv = Object.entries(env).flatMap(([k, v]) => ["--setenv", k, v]); - return [...owned(), ...bound(unshare), bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command]; + return [...owned(), ...bound(unshare), bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf(mountOrder([...systemBinds(node), ...list])), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command]; +} + +// pi-fit-tmp: list in an order bwrap can mount without one bind hiding another: each bind moves +// ahead of the first bind whose target lies strictly under its own. Binds with no such relation +// keep their order, and so do binds with the same target, so a later one still covers an earlier +// one at that same target. Only the order changes: the same binds, each with its own source and +// target. Targets compare in plain form ("/a/" and "/a/./" are "/a"); an entry with no target +// stays where it is. Limits, by design: no bind is left hidden under a later one, so a caller that +// counts on a later mount covering what an earlier one put under it (a tmpfs over a directory, +// or [/b at /a, /a/x, /c at /a], where /c no longer covers /a/x) must not use it. Only checkArgv +// does; a session's binds() keep their own order. +export function mountOrder(list) { + const target = (b) => { + const t = b.dest ?? b.src; + return typeof t === "string" && t.startsWith("/") ? posix.normalize(t).replace(/(.)\/+$/, "$1") : null; + }; + const under = (path, dir) => path !== null && dir !== null && (dir === "/" ? path !== "/" : path.startsWith(`${dir}/`)); + const out = []; + for (const b of list) { + const at = out.findIndex((o) => under(target(o), target(b))); + if (at < 0) out.push(b); + else out.splice(at, 0, b); + } + return out; } // 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];