feat(pi): pi is found, labelled and fit-checked at start instead of pinned; a pi that lacks a needed feature is refused harness-feature-missing (#1873) #2010

Merged
jason.woltje merged 16 commits from pi-unpin-s123 into refactor 2026-11-20 08:24:09 +00:00
36 changed files with 3065 additions and 106 deletions

No files matched your search

+12 -4
View File
@@ -1,6 +1,6 @@
#!/bin/sh
# Pi adapter: implements the Mosaic adapter contract for the pinned
# @earendil-works/pi-coding-agent CLI.
# Pi adapter: implements the Mosaic adapter contract for the
# @earendil-works/pi-coding-agent CLI. Its behaviour notes are read from 0.85.1.
#
# Contract: see /opt/mosaic/adapters/README.md.
# Headless (default): stdout = response only; stderr = diagnostics; exit 0.
@@ -92,6 +92,11 @@ fi
THINKING_FLAG=""
[ -n "${MOSAIC_THINKING:-}" ] && THINKING_FLAG="--thinking $MOSAIC_THINKING"
# MCP (#1873 S3): the harness module sets MOSAIC_PI_NO_MCP=1 when the binary's --help lists
# --no-mcp, so MCP stays off by its own flag and not only as a side effect of --no-extensions.
NO_MCP_FLAG=""
[ "${MOSAIC_PI_NO_MCP:-}" = "1" ] && NO_MCP_FLAG="--no-mcp"
# Mode (M13): interactive TUI or one-shot print.
PRINT_MODE="-p"
REQUEST_ARG=""
@@ -107,6 +112,7 @@ fi
# --system-prompt replace the default prompt with the generated one, given
# as the file's absolute path (checked above), never its text
# --no-* no ambient context/skills/extensions/templates/themes
# NO_MCP_FLAG --no-mcp when the binary lists it, else nothing
# EXT_FLAGS the explicitly provided extensions only (-e)
# SESSION_FLAGS ephemeral | persistent | forked (per env)
# TOOLS_FLAG per capabilities
@@ -118,6 +124,7 @@ set -- \
--offline \
--no-approve \
--no-extensions \
$NO_MCP_FLAG \
$EXT_FLAGS \
$SKILLS_FLAG \
--no-prompt-templates \
@@ -134,6 +141,7 @@ set -- \
[ "$INTERACTIVE" = "1" ] || set -- "$@" -p "$MOSAIC_REQUEST"
if [ -n "${MOSAIC_MODEL_EVENT_MONITOR:-}" ] && [ "$INTERACTIVE" != "1" ]; then
STREAM="$(dirname "$0")/../../packages/plugins/harness/pi/model-stream.mjs"
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" pi "$@" --mode json
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" "${MOSAIC_PI_BIN:-pi}" "$@" --mode json
fi
exec pi "$@"
# #1873 S2: the binary the harness module found (MOSAIC_PI_BIN, its path inside the sandbox).
exec "${MOSAIC_PI_BIN:-pi}" "$@"
@@ -6,6 +6,7 @@ import { join } from 'node:path';
import { sandboxProblem } from '../../seat/src/sandbox.mjs';
import { startTimeOf } from '../../seat/src/proc.mjs';
import { ownershipFixture, waitFor } from './model-hire-ownership-fixture.mjs';
import * as pi from '../../plugins/harness/pi/index.mjs';
const unavailable=sandboxProblem()??false; // Any skip means UNVERIFIED, not green acceptance.
const done=async box=>{await box.promise;return box;};
const refused=async(box,code)=>{await done(box);assert.equal(box.ok,false,'real operation refuses');assert.equal(box.code,code,'real operation preserves exact refusal');};
@@ -17,7 +18,12 @@ test('655 listed duplicate with no record refuses before any model or management
assert.deepEqual(f.snapshot(),before,'duplicate hire makes no credential or management effects');f.assertHistoryGuard();f.valueFree();
});
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:30000},async t=>{
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:120000},async t=>{
// #1873 pi-unpin re-check 9 and 10: the launch's preflight shares pi's fit check, which is cold in
// a fresh process. Wait for it before the fixture starts its launcher, so the launcher's warm-up
// and the launch's preflight hit the cache, the gate's 5 s times the launch, and the launch starts
// at the same point in the launcher's 2 s tick as it did before the fit check existed.
await pi.ready({env:{PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR}});
const f=await ownershipFixture(t),pGet=f.hold('GET'),p=f.launchAide();await pGet.entered();
// P's own acquisition GET is held. Its PUT confirmation GET is ordinal+1;
// only an OLD H reuse GET can be ordinal+2. The correct early refusal removes it.
+46 -10
View File
@@ -215,12 +215,19 @@ const relayed = (failed) => failed && (typeof failed.message === "string" ? { ..
// and names the module rather than escaping the host. What it threw is named by its system code
// or its class, never its message, which the host didn't write (#1873).
function call(name, what, fn) {
try {
return fn();
} catch (e) {
const failed = (e) => {
const thrown = quietMessage(e, "a host file") ?? `threw ${/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(String(e?.name)) ? e.name : "a value"} without a system code`;
throw new Refusal("harness-failed", `harness ${name} ${what}: ${thrown}`);
};
let out;
try {
out = fn();
} catch (e) {
failed(e);
}
// #1873 pi-unpin re-check 7: a module may answer with a promise (pi's preflight runs its fit
// check as child processes). The caller awaits it; a rejection is refused as a throw is.
return typeof out?.then === "function" ? Promise.resolve(out).then(undefined, failed) : out;
}
// host: startHost()'s handle. tracker: true when the broker has task verbs for
@@ -454,14 +461,11 @@ export function createLauncher({
for (const m of [primary, fallback].filter(Boolean)) {
if (!on.includes(m.harness)) throw new Refusal("harness-off", `harness ${m.harness} isn't switched on in plugins.harness`);
}
// One harness through a fresh loader, so a refusal never leaves an earlier load's module in use.
const harnessOf = async (m) => {
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log: (entry) => record({ ...entry, instance }) });
const { refused } = await loader.load({ harness: [m.harness] });
const { refused, module } = await freshHarness(m, (entry) => record({ ...entry, instance }));
// #1873: a loader message can quote the module's path or its load error; only a
// missing-export one, which names the export, is relayed. Any other refusal names its code.
if (refused.length) throw new Refusal("harness-refused", `harness ${m.harness}: ${refused.map((r) => r.code === "missing-export" ? `${r.code}: ${relay(r.message)}` : String(r.code)).join("; ")}`);
const module = loader.modules("harness")[0];
const levels = call(m.harness, "levels", () => module.levels());
if (!levels.includes(m.thinking)) throw new Refusal("thinking-unsupported", `harness ${m.harness} takes thinking ${levels.join(", ")}, not ${m.thinking}`);
return module;
@@ -556,7 +560,7 @@ export function createLauncher({
const pickRoute = async (proxyOf) => {
let sessionProxy = await proxyOf(primary);
// #1873: a preflight's message is the module's own text, relayed with host paths replaced.
const failed = relayed(call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
const failed = relayed(await call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
if (failed) {
if (!fallback) throw new Refusal(failed.code, failed.message);
// The fallback only on a failed primary preflight, and only at launch (row 65). Any refusal of
@@ -566,7 +570,7 @@ export function createLauncher({
throw e;
});
sessionProxy = await proxyOf(fallback);
const also = relayed(call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
const also = relayed(await call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
if (also) throw new Refusal(also.code, `fallback ${fallback.harness}: ${also.message} (primary ${primary.harness}: ${failed.message})`);
[model, harness, route, routeReason] = [fallback, second, "fallback", failed.code];
}
@@ -740,7 +744,7 @@ export function createLauncher({
if (store) compactions(store, model.harness, harness, { instance, thread, session, run });
const toolSocket = join(runDir, "tools.sock");
const turnMarker = join(runDir, "turn.done");
const version = harnessVersion !== undefined ? harnessVersion : call(model.harness, "version", () => harness.version({ env }));
const version = harnessVersion !== undefined ? harnessVersion : await call(model.harness, "version", () => harness.version({ env }));
// Row 137 (#1631): a role and thread holding an approved, unspent merge decision get land_work.
// The broker checks the decision again on every land; this only decides what the bundle shows.
// Row 156 (#1651): a non-thread instance's land and landing come from one landHolders read, so a
@@ -1670,6 +1674,38 @@ export function createLauncher({
clearTimeout(timer);
}
// One harness ({ harness: name }) through a fresh loader, so a refusal never leaves an earlier
// load's module in use: { refused, module }, module null when refused. A launch (harnessOf) and
// the start's fit warm-up below (#1873 re-check 8) both load through here.
async function freshHarness(m, log) {
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log });
const { refused } = await loader.load({ harness: [m.harness] });
return { refused, module: refused.length ? null : loader.modules("harness")[0] };
}
// #1873 pi-unpin re-check 8: each switched-on harness module with ready() (pi's fit check) starts
// it here, in the background, so the first launch joins the check in flight instead of starting
// it. Nothing waits on it: a launch still awaits its own preflight, which shares this check or
// reads its pass. A warm-up that fails or throws is dropped; it caches nothing, and the launch's
// preflight runs the check again. Re-check 9: a harness plugin the warm-up can't load leaves one
// launch log entry, by its code only (a launch that loads it records the message).
function warmHarnesses() {
const on = Array.isArray(b.plugins?.harness) ? b.plugins.harness : [];
for (const name of on) {
(async () => {
let logged = false;
const refusal = (entry) => {
if (logged || entry?.event !== "plugin-refused") return;
logged = true;
record({ event: "plugin-refused", kind: entry.kind, name: entry.name, code: entry.code, during: "warm-up" });
};
const { module } = await freshHarness({ harness: name }, refusal);
if (typeof module?.ready === "function") await module.ready({ env });
})().catch(() => {});
}
}
if (launches) warmHarnesses();
host.beforeClose(close);
return { business: b, registry, listen, launchPm, fromSession, tick, close };
}
+2 -1
View File
@@ -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') } });
+140 -1
View File
@@ -12,7 +12,8 @@ import { createHash } from "node:crypto";
import { once } from "node:events";
import { createInterface } from "node:readline";
import { connect } from "node:net";
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, watch, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "../../bus/src/client.mjs";
@@ -24,6 +25,8 @@ import { bootConfig, loadSystem } from "../src/config.mjs";
import { cmdlineOf, startHost, startTimeOf } from "../src/host.mjs";
import { createLauncher } from "../src/launcher.mjs";
import { mockAnthropic, REPO } from "../../harness/tests/helpers.mjs";
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
import * as pi from "../../plugins/harness/pi/index.mjs";
import { fakeHarnesses, fixture, HOST_CREDENTIALS, tmp, withoutHostCredentials } from "./helpers.mjs";
const FAKE = fileURLToPath(new URL("../../harness/tests/fixtures/fake-adapter.mjs", import.meta.url));
@@ -266,6 +269,142 @@ test("bus start --pm: the launcher instance (the chief of staff) runs under its
assert.equal(end.exitCode, 0);
});
// #1873 pi-unpin S1/S2: the version label comes from the pi the module finds. A pi whose --version
// fails gives a null label, and the launch still goes ahead.
test("pi-unpin: a pi named by MOSAIC_PI_BIN that exits 1 on --version records a null harnessVersion and still launches", { skip, timeout: 30000 }, async (t) => {
const prepared = prepare(t);
const pkg = join(prepared.root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent");
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } }));
// #1873 S3: the stand-in passes the fit check; only its --version fails.
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ versionExit: 1 }), { mode: 0o755 });
mkdirSync(join(prepared.root, "pi-prefix", "bin"));
const bin = join(prepared.root, "pi-prefix", "bin", "pi");
symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin);
prepared.f.env.MOSAIC_PI_BIN = bin;
// env() reads the bus's own environment, as the launcher's is process.env outside tests.
const before = process.env.MOSAIC_PI_BIN;
process.env.MOSAIC_PI_BIN = bin;
t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; });
const { f, launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
const chief = await launcher.launchPm();
assert.equal(chief.harness, "pi");
const [s] = readSessions(f.dataRoot);
const manifest = JSON.parse(readFileSync(join(s.runDir, "bundle", "manifest.json"), "utf8"));
assert.equal(manifest.harnessVersion, null);
assert.equal(await close(), 0);
});
// #1873 re-check 8: a pi stand-in under root/pi-prefix, as an npm prefix holds it. Returns its bin.
function piPrefix(root, knobs) {
const pkg = join(root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent");
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } }));
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn(knobs), { mode: 0o755 });
mkdirSync(join(root, "pi-prefix", "bin"));
const bin = join(root, "pi-prefix", "bin", "pi");
symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin);
return bin;
}
// Each fit run makes one scratch directory under tmpdir(), so a watch there counts the runs.
// quiet() waits until no run has started for a while, so a check an earlier test left going
// (the real pi's, warmed by its launcher) is not counted.
function fitRuns(t) {
const made = new Set();
const watcher = watch(tmpdir(), (event, name) => { if (name && name.startsWith("mosaic-pi-fit-")) made.add(name); });
t.after(() => watcher.close());
const quiet = async () => {
let n;
do { n = made.size; await new Promise((r) => setTimeout(r, 700)); } while (made.size !== n);
};
return { made, quiet };
}
function busPiBin(t, prepared, bin) {
prepared.f.env.MOSAIC_PI_BIN = bin;
const before = process.env.MOSAIC_PI_BIN;
process.env.MOSAIC_PI_BIN = bin;
t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; });
}
// #1873 pi-unpin re-check 8: the launcher starts pi's fit check when it starts, before any launch,
// and does not wait for it. The first launch joins that check: the two together make one check's
// runs, not two.
test("pi-unpin re-check 8: the launcher starts pi's fit check when it starts, and the first launch joins it", { skip, timeout: 60000 }, async (t) => {
const { made, quiet } = fitRuns(t);
// One check on its own, for another stand-in: how many runs it makes.
const alone = piPrefix(tmp(t, "mosaic-launch-pi-"), {});
await quiet();
made.clear();
assert.equal(await pi.preflight({ env: { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: alone } }), null);
await quiet();
const runs = made.size;
assert.ok(runs > 10, `one check made ${runs} runs`);
const prepared = prepare(t);
busPiBin(t, prepared, piPrefix(prepared.root, {}));
made.clear();
const { launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
// No launch yet, and the check is under way.
await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch"));
const chief = await launcher.launchPm();
assert.equal(chief.harness, "pi");
await quiet();
assert.equal(made.size, runs, "the launch joined the check the launcher started");
assert.equal(await close(), 0);
});
// A warm-up that fails leaves no unhandled rejection and keeps no verdict: the next preflight runs
// the check again and refuses on its own.
test("pi-unpin re-check 8: a fit check the launcher starts that fails is dropped: no unhandled rejection, nothing kept", { skip, timeout: 60000 }, async (t) => {
const { made, quiet } = fitRuns(t);
const unhandled = [];
const seen = (e) => unhandled.push(e);
process.on("unhandledRejection", seen);
t.after(() => process.off("unhandledRejection", seen));
const prepared = prepare(t);
const bin = piPrefix(prepared.root, { dropFlags: ["--no-themes"] });
busPiBin(t, prepared, bin);
await quiet();
made.clear();
const { close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch"));
// This preflight joins the failing check; then it is gone.
const env = { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: bin };
const refusal = { code: "harness-feature-missing", message: "pi 9.9.9 (MOSAIC_PI_BIN) lacks --no-themes" };
assert.deepEqual(await pi.preflight({ env }), refusal);
await quiet();
await new Promise((r) => setImmediate(r));
assert.deepEqual(unhandled, []);
made.clear();
assert.deepEqual(await pi.preflight({ env }), refusal);
await quiet();
assert.ok(made.size > 0, "the failed check was kept");
assert.equal(await close(), 0);
});
// Re-check 9: a harness plugin the warm-up can't load leaves one launch log entry, by its code
// only, so the message (which may name a host path) stays out of it.
test("pi-unpin re-check 9: a harness plugin the launcher's warm-up can't load leaves one launch log entry, by code only", { skip, timeout: 30000 }, async (t) => {
const given = prepare(t, (doc) => {
doc.plugins = { ...doc.plugins, harness: [...doc.plugins.harness, "broken"] };
return doc;
});
const makeLoader = await fakeHarnesses(t, { pi: given.adapter, "claude-code": given.adapter }, (write) => {
write("harness", "broken", `throw new Error("broken at /srv/fit-secret-path");\n`);
});
const { f, close } = await setup(t, undefined, given, { makeLoader });
const refused = () => existsSync(launchLogFile(f.dataRoot, "acme")) ? log(f).filter((e) => e.event === "plugin-refused") : [];
await until(() => refused().length > 0, 10000).catch(() => assert.fail("the warm-up left no record of the refused plugin"));
await new Promise((r) => setTimeout(r, 500));
const entries = refused();
assert.equal(entries.length, 1, JSON.stringify(entries));
const [entry] = entries;
assert.deepEqual(Object.keys(entry).sort(), ["at", "code", "during", "event", "kind", "name"]);
assert.deepEqual([entry.kind, entry.name, entry.during], ["harness", "broken", "warm-up"]);
assert.match(entry.code, /^[a-z][a-z-]+$/);
assert.doesNotMatch(readFileSync(launchLogFile(f.dataRoot, "acme"), "utf8"), /fit-secret-path/);
assert.equal(await close(), 0);
});
test("the chief of staff launches a coder through its launch tool; the coder answers; refusals name their code", { skip, timeout: 30000 }, async (t) => {
const { f, launcher, client, cto } = await setup(t);
const sock = launchSocketPath(f.dataRoot);
@@ -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' });
+97 -7
View File
@@ -18,13 +18,103 @@ Every harness layer below is a row S0 line
(`docs/plans/2026-10-04_slice-1.md`, "What S6 can rely on"), and each
bundle's `manifest.json` names the lines it relies on (`reliesOn`):
| | Pi | Claude Code |
|---|---|---|
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed |
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| 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 <gate> \|\| 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 `<gate> \|\| 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 <gate> \|\| exit 2`, hook timeout 20 | suites, on the repository's pi |
| 5. bash | limited only by the tool limit | limited only by the tool limit | suites, on the repository's pi |
Pi is not pinned (#1873 pi-unpin). Before a session starts, `preflight`
awaits the fit check in `src/pi-fit.mjs` on the binary the module found
(the Triage host and route await the module's `ready` instead), and
`env` reads its pass. It runs that binary in a sandbox with the network
unshared and calls no model. Each run is a child process the check
awaits, up to four at a time, so the bus's event loop keeps running
while it does; callers that ask while a check runs share it. Each run
gets the environment a bus session's pi gets, built by the same code
(the launcher's `ENV_ALLOW` and `sessionEnv`, the sandbox's
`sessionSetenv`, the runner's `adapterEnv` and the pi module's
`harnessEnv`) from the bus process's own environment, less the provider
keys, and starts the binary through `/bin/sh` as the adapter does. So
`HOME` is a scratch home, `TMPDIR` is `/tmp` bound to scratch, a run has
`TERM`, `TZ` or `LC_ALL` only when the bus has it (a bus started by its
unit has none), and no run sets `PI_CODING_AGENT_DIR`, which no session
has: pi's agent directory is `$HOME/.pi/agent`, where the user sources
are planted. A test fails when the fit runs' names, or the values the
code sets, differ from a bus session's, apart from the provider
variables, for a login shell's host and a unit's. It needs
`--help` to list, in its option column, every flag the adapter passes
(with rpc mode and each thinking level), and a missing `-e` file to stop
the start in rpc and in `-p`. The planted ambient sources must stay
unloaded with the seal flags on (skills and prompt templates are read
from `get_commands` in rpc and from the `-e` extension's command list in
`-p`): extensions, MCP
servers, skills and prompt templates (found in their directories, listed
in a `settings.json`, or in a local package a `settings.json` lists),
context files (`AGENTS.md`, `CLAUDE.md`, `SYSTEM.md`) and a trusted
project's `.pi/SYSTEM.md` and `.pi/APPEND_SYSTEM.md`. The project has a
saved trust, so only `--no-approve` keeps those two out. An `-e`
extension writes the system prompt, command names and session id it sees
at `session_start`; the sealed runs must show the `--system-prompt` file
and none of the planted sources. A control run passes the seal flags less every loader flag
(`--no-approve`, `--no-extensions`, `--no-skills`,
`--no-prompt-templates`, `--no-context-files`, and `--no-mcp` when
listed), so it keeps `--offline`, `--no-themes`, `--no-tools` and
`--no-session`, and the same `-e` and `--system-prompt`. It must show
each kind of source (an MCP server only when `--help` lists `--no-mcp`)
and answer `get_state` and `get_commands`, and its `-e` extension must
list the planted skill and prompt template, or the check can't tell and
refuses. For every kind but the trusted project files, only a source
outside the project counts there, since a project source can wait on
trust and `--no-approve` would then hide a seal flag the binary ignores.
Then one sealed rpc run per loader flag leaves out only that flag: its
kind must show, and no other kind (an MCP server may show beside an
extension when `--help` lists no `--no-mcp`, since pi then ties MCP to
`--no-extensions`). So each loader flag is shown to keep its own kind
out, and no other seal flag, nor their order, can stand in for it. Five
sealed `-p` runs have a bus session's shape: the arguments the adapter
passes for a session, in its order, from `sessionShape` in
`src/pi-fit.mjs`, with two `-e` and `--session-dir`. The adapter's four
session branches (a tool list or `--no-tools`, `-c` over a stored
session or none, `--thinking` or none, `--mode json` or none) are spread
over the five runs so every pair of them is passed together. A `-c` run
gets a stored session and must resume it. Each must load both `-e`
files and show no planted source. A test drives the adapter, in a bus
session's environment, through every branch set with no tools, one or
two, `-c` over a pi-named session file or an unrelated file, each
thinking level and each provider a session can name. It runs over a
login shell's host and a unit's, and fails on an argv `sessionShape`
doesn't build, a pair no run passes, or a variable the adapter reads
that no session sets. So a branch on `TZ` or `LC_ALL` being unset, or on
`TERM` missing from the environment, turns it red. (Bash run as
`/bin/sh` sets an unset `TERM` to `dumb` for the shell without exporting
it, so `${TERM:-}` in the adapter is never empty, in a session or here.)
It can't see a branch keyed on a
value it doesn't try (a model, a path, another tool or provider). A last sealed `-p` run uses `--no-session`, as
worker runs do. Each run gets a fresh scratch directory, so no run sees
what another left. Not run: `--fork`, `--skill` and the interactive TUI
(worker containers and `scripts/agent.sh` only), the branches three or
four at a time beyond the five runs, `-c` over a session directory that
holds nothing for the project (one more run costs about 340 ms cold),
and a session's own values (tools beyond `read` and `bash`, thinking
levels beyond `low`, provider, model, key, `-e` files, request). The
runs are offline with no key, so nothing past the start is tried. A pi
built to act one way in the fit runs and another way with those is
outside what a start-time check can catch. A run killed for taking
over 20 s or printing over 4 MB can't be checked and says which. A
`--version` that fails or hangs past 3 s only leaves no label. Pi
0.85.1 appends the agent directory's `APPEND_SYSTEM.md` whatever the
flags, so the check doesn't plant one. Any failure refuses the launch with
`harness-feature-missing` and one line that names the binary by its label,
for example `pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files`. When
`--help` lists `--no-mcp`, the adapter passes it too. A pass is cached in
memory by a digest of the package's files (and npm's lock files, for
the repository's copy) and the names in the runs' environment, so a bus
restarted under other names checks again. Lines 1, 2, 4 and 5 are not
checked at start; a pi other than the repository's copy relies on them
unproved until a scripted-model fit run exists (design section 5, S6).
Claude Code also runs with `--restricted`: no user, project or local
settings, file tools confined to the working directory, and no `CLAUDE.md`
+778
View File
@@ -0,0 +1,778 @@
// #1873 pi-unpin S3: the start-time fit check for a pi binary the harness module found. Nothing
// here compares a version. The check runs the binary only inside a sandbox with the network
// unshared, the found package mounted as a session mounts it, and a bus session's environment
// (fitEnv below), so its agent directory is $HOME/.pi/agent under a scratch HOME, as a session's
// is. No model is called and no provider key is passed. It asks:
//
// --version a label for the refusal line and the record; not a gate
// --help lists, in its option column, every flag Mosaic passes, with rpc mode and each
// thinking level
// control rpc, planted ambient sources, a saved trust for the project, the seal flags less
// every loader flag (the ones that each keep one kind of source out: --no-approve,
// --no-extensions, --no-skills, --no-prompt-templates, --no-context-files, and
// --no-mcp when --help lists it), the --system-prompt file the sealed runs pass, and
// one -e extension that writes what it sees at session_start: the system prompt, the
// command names and the session id. The binary answers get_state and get_commands,
// its -e extension lists the planted skill and prompt template (the -p runs see
// those kinds only that way), and it shows each kind of planted source:
// an extension, a skill, a prompt template, a context file, a trusted project file
// (the project's .pi/APPEND_SYSTEM.md, which only the trust --no-approve must undo
// lets in), and an MCP server when --help lists --no-mcp. Only a source outside the
// project counts for the other kinds, since a project source can wait on trust. A
// kind it doesn't show could not be told apart from a sealed one.
// sealed rpc the adapter's seal flags (and --no-mcp when --help lists it), the -e extension and
// --system-prompt. The binary answers both, loads that extension (a session's gate
// comes in that way), uses the --system-prompt file, and shows no planted source.
// one out one more sealed rpc run per loader flag, with every seal flag but that one. The
// kind it keeps out must show, and no other kind (pi without --no-mcp ties MCP to
// --no-extensions, so an MCP server may show beside an extension). So each loader flag
// is shown to keep its own kind out, and no other seal flag, nor the order they come
// in, can stand in for it.
// session five -p runs in a bus session's shape, with the arguments adapters/pi/adapter.sh
// passes for a session, in its order (sessionShape below): two -e, --session-dir in
// place of --no-session, a fixed --provider and --model, -p and a message. The
// adapter's four session branches (a tool list or --no-tools, -c over a stored
// session or none, --thinking or none, --mode json or none) are spread over the five
// runs so each pair of them is passed together (SESSION_RUNS). A -c run gets a
// stored session for the project and must resume it. Each run loads both -e
// extensions, uses the --system-prompt file, and shows no
// planted source. A test drives the adapter through every branch set, tool list
// length and thinking level, and fails on an argv sessionShape doesn't build or a
// pair no run passes.
// sealed -p the seal flags in -p with --no-session, as a worker run passes them. Its -e
// extension must reach session_start, and no planted source may show.
// missing -e a missing -e file, in rpc and in -p, stops the start: exit non-zero, no answer,
// and no session_start for the -e extension given after it
//
// Every run gets a fresh scratch directory with the sources planted again, so no run sees what
// another one left. The directory's name carries the pid, start time and pid namespace of the
// process that made it, and each check first stops the processes and removes the directories a
// process no longer running left (sweepScratch below). Every run dies with the process that runs
// the check (checkArgv in packages/seat/src/sandbox.mjs), at once when unshare runs here and at
// the next check's sweep when it doesn't.
// The runs share no state: up to FIT_CONCURRENCY of them run at once,
// each a child process the check awaits, so the process that runs the check (the bus) keeps
// answering while it runs. --version and --help go first; the rest start in the order above and
// are judged in that order, so the first failure is the one a refusal names, and the runs still
// going are killed. Each run passes the same -e and --system-prompt; the other runs differ only by
// their seal flags, their mode, the -p message and the missing -e file. Each run starts the binary
// through /bin/sh, as the adapter does, with fitEnv's variables: the environment a bus session's pi
// gets, built by the code that builds it (the launcher's ENV_ALLOW and sessionEnv, the sandbox's
// sessionSetenv, the runner's adapterEnv and the pi module's harnessEnv) from the calling process's
// own environment, less the provider variables. So a fit run has a name only when a session in that
// process has it (TERM, TZ and LC_ALL only when the bus has them), no run sets a variable a session
// lacks, such as PI_CODING_AGENT_DIR, and a test holds the two together.
//
// A check that cannot tell refuses: a sandbox that fails, a run that times out or prints more
// than 4 MB, an answer that is missing or doesn't parse, a kind of source the control doesn't
// show, or the kind a one-out run should show. A one-out run that shows another kind, or a
// session-shaped run that shows any, is a leak. A --version that fails or hangs past 3 s only
// leaves no label. A pass is cached in memory by a digest of the files that run (the package's
// package.json, every file under dist/bundle/, every file in the directory tree that holds the
// binary's real file, and, for a copy mounted with the node_modules it sits in, npm's lock files
// there), the mount layout and the names in the fit runs' environment, so a process with other
// names checks again. Callers that ask while a check for the same key is running share it.
// A refusal line names the found binary by its label (MOSAIC_PI_BIN, the pi on PATH, the
// repository's pi), never a host path.
//
// Known limit: pi 0.85.1 appends the agent directory's APPEND_SYSTEM.md whatever the flags. The
// check doesn't plant one; a session's agent directory is the run's own. Not run: --fork,
// --skill with --no-skills dropped, and the interactive TUI, which only worker containers and
// scripts/agent.sh pass; the session branches three or four at a time beyond the session-shaped
// runs; -c over a session directory that holds nothing for the project (the adapter passes -c
// whenever the directory isn't empty), since one more run costs about 340 ms cold; and a
// session's own values: its tool names beyond read and bash, thinking levels beyond low,
// provider, model, key, -e files and request. The runs are offline with no key, so nothing
// past the start is tried. A pi built to act differently with those is outside what a start-time
// check can catch.
import { spawn } from "node:child_process";
import { createHash } from "node:crypto";
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, relative } from "node:path";
import { BWRAP, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs";
import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs";
import { adapterEnv } from "./runner.mjs";
import { FIT_ROUTE, harnessEnv } from "../../plugins/harness/pi/session-env.mjs";
// The flags adapters/pi/adapter.sh and the conversation engine pass.
export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", "--session-dir", "--fork", "-c", "--no-extensions", "-e", "--no-skills", "--skill", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-approve", "--offline", "--tools", "--no-tools", "--provider", "--model", "--thinking", "--system-prompt", "-p"]);
// The adapter's seal flags, as it passes them in an ephemeral session with no tools.
const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-tools", "--no-session"];
// The loader flags: each keeps one kind of planted source out. --no-mcp counts when --help lists it.
const LOADERS = [["--no-approve", "trusted project file"], ["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]];
// The session-shaped runs' fixed values: a two-tool list, a thinking level, a provider and model.
// Offline with no key, pi stops before any model call. The provider and model are the pi module's
// (session-env.mjs FIT_ROUTE), so no core source names them.
export const SESSION_SHAPE = Object.freeze({ tools: "read,bash", thinking: "low", provider: FIT_ROUTE.provider, model: FIT_ROUTE.model });
// The branches adapters/pi/adapter.sh takes for a bus session, each run once or not: a tool list or
// --no-tools, -c over a stored session or a fresh one, --thinking or none, --mode json (the model
// event monitor) or none. The five runs pass every pair of those together; pi-fit.test.mjs drives
// the adapter through all sixteen and fails on a pair no run passes.
const T = SESSION_SHAPE.tools;
const L = SESSION_SHAPE.thinking;
export const SESSION_RUNS = Object.freeze([
{ tools: "", cont: false, thinking: "", json: false },
{ tools: "", cont: true, thinking: L, json: true },
{ tools: T, cont: false, thinking: L, json: true },
{ tools: T, cont: true, thinking: "", json: true },
{ tools: T, cont: true, thinking: L, json: false },
].map((r) => Object.freeze(r)));
// The arguments adapters/pi/adapter.sh builds for a bus session: the given -e files, a tool list
// or none, a persistent session directory with -c when it holds a session, a thinking level or
// none, the --system-prompt file, -p and the request, and --mode json when the model event monitor
// runs pi. No --skill and no --fork: a bus session never passes them. Keep its order:
// pi-fit.test.mjs runs the adapter through every branch and fails when the two differ.
export function sessionShape({ noMcp, extensions, prompt, sessionDir, tools, cont, thinking, json, message }) {
return [
"--offline", "--no-approve", "--no-extensions", ...(noMcp ? ["--no-mcp"] : []), ...extensions.flatMap((e) => ["-e", e]),
"--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files",
...(tools ? ["--tools", tools] : ["--no-tools"]), "--session-dir", sessionDir, ...(cont ? ["-c"] : []),
"--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model, ...(thinking ? ["--thinking", thinking] : []),
"--system-prompt", prompt, "-p", message, ...(json ? ["--mode", "json"] : []),
];
}
// What a session-shaped run is named by in a refusal line.
const shapeName = ({ tools, cont, thinking, json }) => `in a session's shape with ${[tools ? "--tools" : "--no-tools", ...(cont ? ["-c"] : []), ...(thinking ? ["--thinking"] : []), ...(json ? ["--mode json"] : [])].join(", ")}`;
const RUN_TIMEOUT = 20000;
// --version gives only a label, so a hang costs this much and leaves no label.
const VERSION_TIMEOUT = 3000;
const OUTPUT_MAX = 4 * 1024 * 1024;
// How long a run's pipes may stay open after it exits or is killed (spawned).
const PIPE_GRACE = 1000;
const DIGEST_FILES_MAX = 20000;
// At most this many fit runs at once. Each run is one sandboxed pi start in its own scratch
// directory.
export const FIT_CONCURRENCY = 4;
const passed = new Map();
const running = new Map();
// #1873 re-check 9: each run's scratch directory is named for the process that makes it, by its
// pid and its start time (field 22 of /proc/<pid>/stat), so a pid used again later is not taken
// for it. A process killed mid-check can't remove its own directories. So a check removes, before
// it starts, every directory here that a process no longer running made: its pid is gone, or now
// belongs to a process that started at another time. A directory whose process can't be read, or
// one this uid doesn't own, or a symlink, is left alone.
//
// #1873 re-check 10: the name also carries the maker's pid namespace (the inode of
// /proc/<pid>/ns/pid), and a sweep leaves alone every directory made in another one, since it
// can't read that process. And before it removes a directory, the sweep stops the processes that
// name it on their command line: a run's every layer does (the setpriv, shell, unshare and bwrap
// argv, bwrap's child before its exec, pi's own -e and --system-prompt), and so do the sandbox
// probes. Without unshare's outer layer a bwrap child killed in its setup waits for good (see
// checkArgv in sandbox.mjs), and this is what stops it. A process is stopped only when an argument
// is that directory or a path in it, its real and effective uid are this process's, and its start
// time is the same before and after its command line is read. Never by name, never another uid's,
// never one whose directory's maker still runs.
const SCRATCH = /^mosaic-pi-fit-(\d+)-(\d+)-(\d+)-/;
const startTime = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19];
const pidNamespace = (pid) => /^pid:\[(\d+)\]$/.exec(readlinkSync(`/proc/${pid}/ns/pid`))[1];
let ownName = null;
const scratchPrefix = () => `mosaic-pi-fit-${ownName ??= `${process.pid}-${startTime(process.pid)}-${pidNamespace("self")}`}-`;
function sweepScratch(dir = tmpdir()) {
let names;
try {
names = readdirSync(dir);
} catch {
return;
}
const uid = process.getuid();
const ns = pidNamespace("self");
const stale = [];
for (const name of names) {
const m = SCRATCH.exec(name);
if (!m || m[3] !== ns) continue;
try {
let alive;
try {
alive = startTime(m[1]) === m[2];
} catch (e) {
if (e.code !== "ENOENT") continue;
alive = false;
}
if (alive) continue;
const at = join(dir, name);
const st = lstatSync(at);
if (st.isDirectory() && st.uid === uid) stale.push(at);
} catch {}
}
if (!stale.length) return;
stopNaming(stale, uid);
for (const at of stale) {
try {
rmSync(at, { recursive: true, force: true });
} catch {}
}
}
// SIGKILL to every process of uid (real and effective) with an argument that is one of dirs or a
// path in one, other than this one.
function stopNaming(dirs, uid) {
let pids;
try {
pids = readdirSync("/proc").filter((p) => /^\d+$/.test(p) && Number(p) !== process.pid);
} catch {
return;
}
const names = (arg) => dirs.some((d) => arg === d || arg.startsWith(`${d}/`));
for (const pid of pids) {
try {
const start = startTime(pid);
const ids = /^Uid:\s+(\d+)\s+(\d+)\s/m.exec(readFileSync(`/proc/${pid}/status`, "utf8"));
if (!ids || Number(ids[1]) !== uid || Number(ids[2]) !== uid) continue;
if (!readFileSync(`/proc/${pid}/cmdline`, "utf8").split("\0").some(names)) continue;
if (startTime(pid) !== start) continue;
process.kill(Number(pid), "SIGKILL");
} catch {}
}
}
// The provider variables never reach a fit run: no key is passed, since no model is called.
const withoutProviders = (host) => Object.fromEntries(Object.entries(host ?? {}).filter(([k, v]) => typeof v === "string" && !isProviderVariable(k)));
// The names a fit run's host part holds: sessionEnv's answer for the process's environment.
export const fitNames = (host = process.env) => Object.keys(sessionEnv(withoutProviders(host), "fit")).sort();
const fitKey = (found, { levels, bwrap, unshare, node, host }) => JSON.stringify([filesDigest(found), found.root, found.dest ?? null, found.view, bwrap, unshare, node, levels, fitNames(host)]);
// found: binary.mjs's answer ({ bin, label, root, dest, view, pkg }). levels: the thinking levels
// the module applies. host: the environment of the process sessions launch from (the bus's own),
// which a fit run's environment is built from as a session's is. bwrap and unshare: where those
// tools are (tests pass stand-ins). Returns a promise of { ok: true, version, noMcp, digest, outer }
// or { ok: false, message }. outer: { used: true } when the runs had unshare's outer layer, else
// { used: false, why } with why naming unshare and how it failed (sandboxLayers below). A call
// while a check with the same key runs gets that check's promise. A check that throws (a scratch directory it can't
// make, say) can't tell, so it gives { ok: false, message } too and keeps nothing.
export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`;
let key;
try {
key = fitKey(found, { levels, bwrap, unshare, node, host });
} catch {
return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read` });
}
if (passed.has(key)) return Promise.resolve(passed.get(key));
if (running.has(key)) return running.get(key);
const job = (async () => {
try {
sweepScratch();
// The sandbox's own error text can name a host path, so the line keeps only what failed.
const layers = await sandboxLayers({ bwrap, unshare, node });
if (layers.problem) return { ok: false, message: `${who(null)} can't be checked: ${layers.problem}` };
const result = await check(found, { levels, bwrap, unshare: layers.outer, node, who, digest: JSON.parse(key)[0], host });
if (!result.ok) return result;
const passing = { ...result, outer: layers.outer ? { used: true } : { used: false, why: layers.why } };
passed.set(key, passing);
return passing;
} catch {
// #1873 re-check 8: refused as a fit that failed. The error's text can name a host path, so
// the line keeps none of it.
return { ok: false, message: `${who(null)} can't be checked: the check could not run` };
}
})().finally(() => { if (running.get(key) === job) running.delete(key); });
running.set(key, job);
return job;
}
// The passed check for found as piFit would key it now, or null: none passed, or its files can't
// be read. Nothing is run.
export function fitVerdict(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
try {
return passed.get(fitKey(found, { levels, bwrap, unshare, node, host })) ?? null;
} catch {
return null;
}
}
async function check(found, { levels, bwrap, unshare, node, who, digest, host }) {
// At most FIT_CONCURRENCY runs at once, started in the order they are asked for. stop kills the
// runs still going and keeps queued ones from starting; the check waits for every run it began,
// so no scratch directory or child outlives it.
const stop = new AbortController();
const begun = [];
let active = 0;
const waiting = [];
const slot = () => active < FIT_CONCURRENCY ? (active++, Promise.resolve()) : new Promise((resolve) => waiting.push(resolve));
const free = () => { const next = waiting.shift(); if (next) next(); else active--; };
// args: the arguments, or a function of the run's scratch directory that returns them (it may
// plant more first). The answer is read before the scratch directory goes: the rpc answers,
// whether each -e extension loaded, what the first one saw at session_start (the system prompt,
// the command names, the session id), and the planted sources that showed, through the rpc
// get_commands answer or through the -e extension's command list (listed: through that alone).
// shape: a session-shaped run's branches, which set its tools, thinking level and monitor
// variables as a session's would. A run that stop kept from starting gives null.
const once = async (args, input, { timeout, noMcp, shape }) => {
await slot();
try {
if (stop.signal.aborted) return null;
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
let ran = null;
try {
plant(scratch);
const env = fitEnv(scratch, { view: found.view, noMcp, shape, host });
const r = ran = await runIn(found, { bwrap, unshare, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout, signal: stop.signal });
const answer = answered(r.stdout);
const start = started(scratch);
const listed = start?.commands ?? [];
return { ...r, answer, explicit: marked(scratch, EXPLICIT), second: marked(scratch, SECOND), prompt: start?.prompt ?? null, session: start?.session ?? null, listed: named(listed), seen: seen(scratch, [...(answer.commands ?? []), ...listed], start) };
} finally {
// #1873 re-check 10: a run this check killed (a timeout, or stop) may have had bwrap in its
// setup, and without unshare's outer layer bwrap's child then waits for good. The run is
// over, so whatever still names its directory is left from it.
if (ran?.stopped) stopNaming([scratch], process.getuid());
rmSync(scratch, { recursive: true, force: true });
}
} finally {
free();
}
};
const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null } = {}) => {
const p = once(args, input, { timeout, noMcp, shape });
// Judged in order, a later run can fail before an earlier one is awaited: this keeps that
// failure from counting as unhandled. Awaiting p still throws it.
p.catch(() => {});
begun.push(p);
return p;
};
try {
return await judge(found, { levels, who, digest, run });
} finally {
stop.abort();
await Promise.allSettled(begun);
}
}
async function judge(found, { levels, who, digest, run }) {
const helped = run(["--help"]);
const versioned = run(["--version"], "", { timeout: VERSION_TIMEOUT });
const help = await helped;
const v = await versioned;
const version = v.status === 0 ? versionLabel(v.stdout) : null;
const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` });
if (help.status !== 0) return refuse("can't be checked: --help failed");
const lines = help.stdout.split("\n");
const has = (flag) => lines.some((l) => optionsIn(l).includes(flag));
const line = (flag) => lines.find((l) => optionsIn(l).includes(flag)) ?? "";
const missing = FIT_FLAGS.filter((f) => !has(f));
if (has("--mode") && !word(line("--mode"), "rpc")) missing.push("--mode rpc");
if (has("--thinking")) for (const l of levels) if (!word(line("--thinking"), l)) missing.push(`--thinking ${l}`);
if (missing.length) return refuse(`lacks ${missing.join(", ")}`);
const noMcp = has("--no-mcp");
const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n';
// Every run passes the same -e and --system-prompt, so the runs differ only by the seal flags.
const given = (scratch) => ["-e", join(scratch, "explicit", "fit.ts"), "--system-prompt", join(scratch, "prompt.md")];
const seal = [...SEAL, ...(noMcp ? ["--no-mcp"] : [])];
const loaders = [...LOADERS, ...(noMcp ? [["--no-mcp", "MCP server"]] : [])];
const without = (...flags) => seal.filter((f) => !flags.includes(f));
const rpcRun = (flags) => run((s) => ["--mode", "rpc", ...flags, ...given(s)], ask, { noMcp });
// Every run is asked for now, in the order they are judged below; none depends on another.
// The control keeps the seal flags no planted source tells apart (--offline, --no-themes,
// --no-tools, --no-session), so one of those can't stand in for a loader flag either.
const controlRun = rpcRun(without(...loaders.map(([f]) => f)));
const rpcSealed = rpcRun(seal);
const oneOut = loaders.map(([flag, kind]) => [flag, kind, rpcRun(without(flag))]);
// A bus session's shapes, in -p as its turns run: --session-dir in place of --no-session, two -e
// as a session passes, and each pair of the adapter's session branches in one run. A -c run gets
// a stored session for the project first and must resume it, since -c with none starts afresh.
const shaped = SESSION_RUNS.map((shape) => [shape, run((s) => {
if (shape.cont) plantSession(s);
return sessionShape({ noMcp, extensions: [join(s, "explicit", "fit.ts"), join(s, "explicit", "second.ts")], prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions"), message: "fit check", ...shape });
}, "", { noMcp, shape })]);
// -p takes a message; offline with no key, pi stops before any model call.
const pRun = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { noMcp });
// A missing -e file stops the start. pi 0.85.1 loads the -e files in order and still loads the
// one after the missing one, then exits 1 before session_start, so only session_start tells.
const missingRuns = [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]].map(([mode, args, input]) =>
[mode, run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input, { noMcp })]);
const control = await controlRun;
if (control.stopped) return refuse(`can't be checked: the start without the seal flags ${control.stopped}`);
if (!control.answer.state) return refuse("can't be checked: it didn't answer get_state");
if (!control.answer.commands) return refuse("can't be checked: it didn't answer get_commands");
if (!control.explicit || control.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags");
// A kind counts only through a source outside the project, except the trusted project files
// themselves: a project source can wait on project trust, and then --no-approve alone would keep
// it out of the sealed runs and hide a seal flag of its kind that the binary ignores.
const shown = new Set(control.seen.filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind));
const unshown = KINDS.filter((k) => (k !== "MCP server" || noMcp) && !shown.has(k));
if (unshown.length) return refuse(`can't be checked: it didn't load a planted ${unshown.join(", ")} without the seal flags`);
// -p answers no get_commands, so the -p runs see skills and prompt templates only through the -e
// extension's command list, which must show them here.
const listed = new Set(control.listed.filter((s) => !s.source.startsWith("project ")).map((s) => s.kind));
const unlisted = ["skill", "prompt template"].filter((k) => !listed.has(k));
if (unlisted.length) return refuse(`can't be checked: its -e extension didn't list a planted ${unlisted.join(", ")} without the seal flags`);
const rpc = await rpcSealed;
if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`);
if (!rpc.answer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on");
if (!rpc.answer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on");
if (!rpc.explicit) return refuse("didn't load an -e extension with the seal flags on");
if (rpc.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on");
if (!rpc.prompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on");
if (rpc.seen.length) return refuse(`loaded a planted ${rpc.seen.map((s) => s.source).join(", ")} with the seal flags on`);
// Each loader flag on its own: with every other seal flag on, its kind must show.
for (const [flag, kind, pending] of oneOut) {
const r = await pending;
const but = `with every seal flag but ${flag}`;
if (r.stopped) return refuse(`can't be checked: the start ${but} ${r.stopped}`);
if (!r.answer.state || !r.answer.commands) return refuse(`can't be checked: it didn't answer get_state and get_commands ${but}`);
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${but}`);
if (!r.seen.some((s) => s.kind === kind)) return refuse(`can't be checked: it didn't load a planted ${kind} ${but}`);
const others = r.seen.filter((s) => s.kind !== kind && !(flag === "--no-extensions" && !noMcp && s.kind === "MCP server"));
if (others.length) return refuse(`loaded a planted ${others.map((s) => s.source).join(", ")} ${but}`);
}
for (const [shape, pending] of shaped) {
const r = await pending;
const name = shapeName(shape);
if (r.stopped) return refuse(`can't be checked: the start ${name} ${r.stopped}`);
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${name}`);
if (!r.explicit || !r.second) return refuse(`didn't load every -e extension ${name}`);
if (!r.prompt.includes(GIVEN)) return refuse(`didn't use the --system-prompt file ${name}`);
if (shape.cont && r.session !== PLANTED_SESSION) return refuse(`can't be checked: it didn't resume the planted session with -c ${name}`);
if (r.seen.length) return refuse(`loaded a planted ${r.seen.map((x) => x.source).join(", ")} with the seal flags ${name}`);
}
const p = await pRun;
if (p.stopped) return refuse(`can't be checked: a -p start with the seal flags ${p.stopped}`);
if (p.prompt === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension");
if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} with the seal flags on in -p`);
for (const [mode, pending] of missingRuns) {
const r = await pending;
if (r.stopped) return refuse(`can't be checked: a ${mode} start with a missing -e extension ${r.stopped}`);
if (r.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension");
}
return { ok: true, version, noMcp, digest };
}
// Whether bwrap runs here, as sandbox.mjs's sandboxProblem asks, but as child processes the caller
// awaits: the system part, the network unshared, an empty environment, then /usr/bin/true. And,
// once per check, whether unshare's outer layer runs (checkArgv in sandbox.mjs): first on its own,
// then with bwrap inside it. Returns { problem } when the check can't run, naming the tool that
// failed without its own text, else { outer, why }: outer is the unshare to pass checkArgv, or null
// when the layer can't run here, and why then names unshare and how it failed. A missing,
// too old or refused unshare drops only that layer and is no refusal: bwrap's sandbox is the same
// either way. The probes run in a scratch directory of their own and name it on their command
// line, so a probe process a killed owner leaves is one the next sweep finds.
async function sandboxLayers({ bwrap, unshare, node }) {
if (!existsSync(bwrap)) return { problem: "the sandbox (bwrap) is missing" };
if (!existsSync(SETPRIV)) return { problem: "the sandbox's parent-death tool (setpriv) is missing" };
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
let stopped = false;
try {
const probe = async (argv) => {
const r = await spawned(argv, { input: "", timeout: 10000, signal: new AbortController().signal });
stopped ||= Boolean(r.stopped);
return r.status === 0;
};
const sandboxed = (outer) => checkArgv([{ kind: "ro", src: scratch }], ["/usr/bin/true"], { chdir: scratch, node, bwrap, unshare: outer });
let why = "unshare is missing";
if (existsSync(unshare)) {
if (!await probe(outerArgv(["/usr/bin/true", scratch], { unshare }))) why = "unshare failed";
else if (await probe(sandboxed(unshare))) return { outer: unshare, why: null };
else why = "unshare failed with bwrap inside it";
}
if (!await probe(sandboxed(null))) return { problem: "the sandbox (bwrap) failed" };
return { outer: null, why };
} finally {
// As for a run (once, in piFit): a probe killed at its timeout may leave what names its directory.
if (stopped) stopNaming([scratch], process.getuid());
rmSync(scratch, { recursive: true, force: true });
}
}
// The binary, inside the sandbox: the system part, the found package where a session sees it, the
// scratch directory read-write, and its tmp/ as /tmp, as a session has it. The environment is
// fitEnv's. The binary starts through /bin/sh, as the adapter starts it, so it also gets the
// variables the shell sets. The run is a child process the caller awaits; nothing here blocks.
// stopped: why the run was killed ("timed out", "printed more than 4 MB", "was stopped"), or null.
function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout, signal }) {
const list = [
{ kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) },
{ kind: "rw", src: scratch },
{ kind: "rw", src: join(scratch, "tmp"), dest: "/tmp" },
];
return spawned(checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap, unshare }), { input, timeout, signal });
}
// One child process with an empty environment, awaited: its exit status, its stdout, and why it was
// killed, or null. It settles PIPE_GRACE after the child exits or is killed even if its pipes stay
// open: a process left behind can hold them (a kill of bwrap's outer process while its child still
// sets up leaves that child waiting until the pid namespace checkArgv makes is killed, or, without
// that layer, until the next check's sweep), and no wait here is unbounded.
function spawned(argv, { input, timeout, signal }) {
return new Promise((resolve) => {
const out = [];
const size = { stdout: 0, stderr: 0 };
let stopped = null;
let done = false;
let child;
let timer = null;
let grace = null;
let exited = null;
const finish = (status, sig) => {
if (done) return;
done = true;
clearTimeout(timer);
clearTimeout(grace);
child?.stdout?.destroy();
child?.stderr?.destroy();
signal.removeEventListener("abort", onAbort);
if (!stopped && sig === "SIGKILL") stopped = "timed out";
resolve({ status, stdout: Buffer.concat(out).toString("utf8"), stopped });
};
const settleSoon = () => { if (!grace) grace = setTimeout(() => finish(exited?.status ?? null, exited?.sig ?? "SIGKILL"), PIPE_GRACE); };
const kill = (why) => {
if (!stopped) stopped = why;
try { child?.kill("SIGKILL"); } catch {}
settleSoon();
};
const onAbort = () => kill("was stopped");
try {
child = spawn(argv[0], argv.slice(1), { env: {}, stdio: ["pipe", "pipe", "pipe"] });
} catch {
finish(null, null);
return;
}
timer = setTimeout(() => kill("timed out"), timeout);
if (signal.aborted) onAbort();
else signal.addEventListener("abort", onAbort, { once: true });
const count = (stream) => (chunk) => {
size[stream] += chunk.length;
if (size[stream] > OUTPUT_MAX) kill(`printed more than ${OUTPUT_MAX / 1024 / 1024} MB`);
else if (stream === "stdout") out.push(chunk);
};
child.stdout.on("data", count("stdout"));
child.stderr.on("data", count("stderr"));
child.stdin.on("error", () => {});
child.on("error", () => finish(null, null));
child.on("exit", (status, sig) => { exited = { status, sig }; settleSoon(); });
child.on("close", (status, sig) => finish(status, sig));
child.stdin.end(input);
});
}
// A fit run's environment: what a bus session's pi gets, built the same way from host, the
// environment of the process sessions launch from (the bus's own). The launcher passes host's
// ENV_ALLOW names that are set through sessionEnv (less the provider variables here, since no key
// is passed: no model is called), with host's PATH behind the repository's .bin; the sandbox sets
// sessionSetenv's (HOME is the scratch home, so pi's agent directory is $HOME/.pi/agent); the
// runner adds adapterEnv's with the pi module's harnessEnv, and MOSAIC_REQUEST for each turn. No
// name is added that a session in that process would lack: a bus started by its unit has no TERM,
// TZ or LC_ALL, and then no fit run has them either. shape: a session-shaped run's branches (its
// tool list, thinking level and --mode json, which a proxy session's first turn takes with the
// model event monitor's variables), else none: no tools, no thinking, one -e. noMcp: --help lists
// --no-mcp. pi-fit.test.mjs fails when these names, or the values the code sets, differ from a bus
// session's, for a login shell's host and a unit's.
export function fitEnv(scratch, { view, noMcp = false, shape = null, host = process.env }) {
const runner = { ...sessionEnv(withoutProviders(host), "fit"), ...sessionSetenv(scratch, { business: "fit", role: "fit" }) };
const { tools = "", thinking = "", json = false } = shape ?? {};
const session = { provider: SESSION_SHAPE.provider, model: SESSION_SHAPE.model, thinking };
const extensions = [join(scratch, "explicit", "fit.ts"), ...(shape ? [join(scratch, "explicit", "second.ts")] : [])];
const env = adapterEnv({
...session,
bundle: { prompt: join(scratch, "prompt.md"), policy: join(scratch, "policy.json"), tools: join(scratch, "tools.json") },
workspace: join(scratch, "project"), sessionDir: join(scratch, "sessions"), toolSocket: join(scratch, "tools.sock"), turnMarker: join(scratch, "turn"),
harnessEnv: harnessEnv({ bin: view, noMcp, tools: tools ? tools.split(",") : [], extensions, session }),
runDir: scratch, proxyFirstTurn: json,
}, runner);
return { ...env, MOSAIC_REQUEST: "fit check" };
}
// The options one help line lists in its option column: a line that starts with "-" (after its
// indent), up to the first run of two spaces or a tab, split at commas; the first word of each
// part. " --extension, -e <path> Load an extension" lists --extension and -e. A flag named in a
// sentence is not listed.
function optionsIn(line) {
const t = line.trim();
if (!t.startsWith("-")) return [];
return t.split(/ {2,}|\t/)[0].split(",").map((part) => part.trim().split(/[ =<[]/)[0]).filter((o) => /^--?[A-Za-z0-9]/.test(o));
}
const word = (line, w) => line.split(/[^A-Za-z0-9-]+/).includes(w);
// The version a refusal line may carry: a version-shaped first line, else none. Anything else the
// binary prints stays out of the line.
function versionLabel(stdout) {
const first = String(stdout).split("\n")[0].trim();
return /^v?[0-9]+(\.[0-9]+){0,3}([-+][0-9A-Za-z.-]{1,32})?$/.test(first) ? first : null;
}
// The rpc answers in the output: whether get_state succeeded, and the command names get_commands
// listed, or null when it didn't answer with a { commands: [...] } list.
function answered(stdout) {
let state = false;
let commands = null;
for (const l of String(stdout).split("\n")) {
let m;
try { m = JSON.parse(l); } catch { continue; }
if (!m || m.type !== "response" || m.success !== true) continue;
if (m.id === "fit-state" && m.command === "get_state" && m.data && typeof m.data === "object") state = true;
if (m.id === "fit-commands" && m.command === "get_commands" && Array.isArray(m.data?.commands)) {
commands = m.data.commands.filter((c) => typeof c?.name === "string").map((c) => c.name);
}
}
return { state, commands };
}
// The planted sources, each with the kind the control must show. Each extension and each MCP
// server touches its own marker; each skill and prompt template has a name get_commands lists;
// each context and system prompt file holds a word the -e extension's view of the system prompt
// shows. "user" sources sit in the agent directory, $HOME/.pi/agent: no fit run sets
// PI_CODING_AGENT_DIR, as no bus session does. "settings" sources are listed by a settings.json;
// "package" sources come from a local package a settings.json lists. "parent" is a directory above
// the project.
const KINDS = ["extension", "MCP server", "skill", "prompt template", "context file", "trusted project file"];
const MARKED = {
"user extension": "extension", "user settings extension": "extension", "user package extension": "extension",
"project extension": "extension", "project settings extension": "extension", "project package extension": "extension",
"user mcp.json": "MCP server", "project mcp.json": "MCP server", "project .mcp.json": "MCP server",
};
const NAMED = {
"fit-user-skill": ["user skill", "skill"], "fit-user-set-skill": ["user settings skill", "skill"], "fit-user-pkg-skill": ["user package skill", "skill"],
"fit-project-skill": ["project skill", "skill"], "fit-project-set-skill": ["project settings skill", "skill"],
"fit-project-pkg-skill": ["project package skill", "skill"], "fit-agents-skill": ["project .agents skill", "skill"], "fit-home-skill": ["home .agents skill", "skill"],
"fit-user-prompt": ["user prompt template", "prompt template"], "fit-user-set-prompt": ["user settings prompt template", "prompt template"],
"fit-user-pkg-prompt": ["user package prompt template", "prompt template"],
"fit-project-prompt": ["project prompt template", "prompt template"], "fit-project-set-prompt": ["project settings prompt template", "prompt template"],
"fit-project-pkg-prompt": ["project package prompt template", "prompt template"],
};
// Planted file (under the scratch directory), the word it holds, the source, the kind. A project
// SYSTEM.md or APPEND_SYSTEM.md loads only in a trusted project: the saved trust makes it trusted,
// and only --no-approve undoes that with the seal flags on.
const TEXTS = [
["home/.pi/agent/SYSTEM.md", "FITUSERSYSTEM", "user SYSTEM.md", "context file"],
["home/.pi/agent/AGENTS.md", "FITUSERAGENTS", "user AGENTS.md", "context file"],
["project/.pi/SYSTEM.md", "FITPROJECTSYSTEM", "project SYSTEM.md", "trusted project file"],
["project/.pi/APPEND_SYSTEM.md", "FITPROJECTAPPEND", "project APPEND_SYSTEM.md", "trusted project file"],
["project/AGENTS.md", "FITPROJECTAGENTS", "project AGENTS.md", "context file"],
["project/CLAUDE.md", "FITPROJECTCLAUDE", "project CLAUDE.md", "context file"],
["CLAUDE.md", "FITPARENTCLAUDE", "parent CLAUDE.md", "context file"],
];
// The --system-prompt file's word, which the sealed runs must show.
const GIVEN = "FITGIVENPROMPT";
// The -e extension every run passes, which must load, and the second one the session-shaped runs
// pass after it.
const EXPLICIT = "explicit extension";
const SECOND = "second explicit extension";
// The id of the session a -c run must resume.
const PLANTED_SESSION = "fit-planted-session";
const markerName = (source) => source.replace(/[^a-z]/g, "-");
const marked = (scratch, source) => { try { return readdirSync(join(scratch, "marks")).includes(markerName(source)); } catch { return false; } };
// What the -e extension saw at session_start ({ prompt, commands, session }), or null when it wrote
// nothing that parses into that shape.
function started(scratch) {
let m;
try { m = JSON.parse(readFileSync(join(scratch, "marks", "start.json"), "utf8")); } catch { return null; }
if (!m || typeof m.prompt !== "string" || !Array.isArray(m.commands)) return null;
return { prompt: m.prompt, commands: m.commands.filter((c) => typeof c === "string"), session: typeof m.session === "string" ? m.session : null };
}
// The planted skills and prompt templates among command names, as { source, kind }.
const named = (names) => Object.entries(NAMED).filter(([name]) => names.some((c) => c === name || c.endsWith(`:${name}`))).map(([, [source, kind]]) => ({ source, kind }));
function plant(scratch) {
const marks = join(scratch, "marks");
const put = (path, text) => { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, text); };
const ext = (source) => `import { writeFileSync } from "node:fs";\nexport default function (): void { writeFileSync(${JSON.stringify(join(marks, markerName(source)))}, ""); }\n`;
const mcp = (source) => JSON.stringify({ mcpServers: { fit: { command: "/bin/sh", args: ["-c", `: > ${join(marks, markerName(source))}`] } } });
const skill = (name) => `---\nname: ${name}\ndescription: a planted skill\n---\nA planted skill.\n`;
const prompt = "A planted prompt template.\n";
const agent = join(scratch, "home", ".pi", "agent");
const project = join(scratch, "project");
mkdirSync(marks, { recursive: true });
mkdirSync(join(scratch, "tmp"), { recursive: true });
mkdirSync(project, { recursive: true });
// The session-shaped runs' --session-dir, made as the adapter makes it.
mkdirSync(join(scratch, "sessions"), { recursive: true });
// Each settings.json lists an extension, a skill directory, a prompt template and a local
// package holding one of each.
const listed = (who) => {
const dir = join(scratch, "listed", who);
put(join(dir, "fit.ts"), ext(`${who} settings extension`));
put(join(dir, `fit-${who}-set-skill`, "SKILL.md"), skill(`fit-${who}-set-skill`));
put(join(dir, `fit-${who}-set-prompt.md`), prompt);
const pkg = join(dir, "package");
put(join(pkg, "package.json"), JSON.stringify({ name: `fit-${who}-package`, version: "1.0.0" }));
put(join(pkg, "extensions", "fit.ts"), ext(`${who} package extension`));
put(join(pkg, "skills", `fit-${who}-pkg-skill`, "SKILL.md"), skill(`fit-${who}-pkg-skill`));
put(join(pkg, "prompts", `fit-${who}-pkg-prompt.md`), prompt);
return { extensions: [join(dir, "fit.ts")], skills: [join(dir, `fit-${who}-set-skill`)], prompts: [join(dir, `fit-${who}-set-prompt.md`)], packages: [pkg] };
};
// A saved trust for the project, so the control loads its trust-gated files without --approve.
put(join(agent, "settings.json"), JSON.stringify({ ...listed("user"), defaultProjectTrust: "always" }));
put(join(agent, "trust.json"), JSON.stringify({ [realpathSync(project)]: true }));
put(join(agent, "extensions", "fit.ts"), ext("user extension"));
put(join(agent, "mcp.json"), mcp("user mcp.json"));
put(join(agent, "skills", "fit-user-skill", "SKILL.md"), skill("fit-user-skill"));
put(join(agent, "prompts", "fit-user-prompt.md"), prompt);
put(join(project, ".pi", "settings.json"), JSON.stringify(listed("project")));
put(join(project, ".pi", "extensions", "fit.ts"), ext("project extension"));
put(join(project, ".pi", "mcp.json"), mcp("project mcp.json"));
put(join(project, ".mcp.json"), mcp("project .mcp.json"));
put(join(project, ".pi", "skills", "fit-project-skill", "SKILL.md"), skill("fit-project-skill"));
put(join(project, ".agents", "skills", "fit-agents-skill", "SKILL.md"), skill("fit-agents-skill"));
put(join(project, ".pi", "prompts", "fit-project-prompt.md"), prompt);
put(join(scratch, "home", ".agents", "skills", "fit-home-skill", "SKILL.md"), skill("fit-home-skill"));
for (const [file, text] of TEXTS) put(join(scratch, file), `${text}\n`);
put(join(scratch, "explicit", "fit.ts"), `import { writeFileSync } from "node:fs";\nexport default function (pi: any): void {\n writeFileSync(${JSON.stringify(join(marks, markerName(EXPLICIT)))}, "");\n pi.on("session_start", (_event: any, ctx: any) => { writeFileSync(${JSON.stringify(join(marks, "start.json"))}, JSON.stringify({ prompt: ctx.getSystemPrompt(), commands: pi.getCommands().map((c: any) => c.name), session: ctx.sessionManager.getSessionId() })); });\n}\n`);
put(join(scratch, "explicit", "second.ts"), ext(SECOND));
put(join(scratch, "prompt.md"), `${GIVEN}\n`);
}
// A stored session for the project, as pi 0.85.1 writes one: a header whose cwd is the project's
// real path (pi's -c takes only a session for its own cwd) and one user message.
function plantSession(scratch) {
const at = new Date().toISOString();
writeFileSync(join(scratch, "sessions", `${PLANTED_SESSION}.jsonl`), [
{ type: "session", version: 3, id: PLANTED_SESSION, timestamp: at, cwd: realpathSync(join(scratch, "project")) },
{ type: "message", id: "f17e0001", parentId: null, timestamp: at, message: { role: "user", content: [{ type: "text", text: "an earlier turn" }], timestamp: Date.now() } },
].map((e) => `${JSON.stringify(e)}\n`).join(""));
}
// The planted sources a run loaded, as { source, kind }: markers present, planted names among the
// command names, and planted words in the system prompt the -e extension saw.
function seen(scratch, commands, start) {
let marks = [];
try { marks = readdirSync(join(scratch, "marks")); } catch { marks = []; }
const out = Object.entries(MARKED).filter(([s]) => marks.includes(markerName(s))).map(([source, kind]) => ({ source, kind }));
out.push(...named(commands));
const text = start?.prompt ?? "";
for (const [, word, source, kind] of TEXTS) if (text.includes(word)) out.push({ source, kind });
return out;
}
// sha256 over the sorted (path, sha256) list of the package's package.json, every regular file
// under dist/bundle/ (design section 4), every regular file in the directory tree that holds the
// binary's real file, so a bin outside dist/bundle/ can't import a file the digest skips, and npm's
// lock files for a copy mounted with its node_modules. A symlink there is read through.
function filesDigest(found) {
const pkg = found.pkg;
const bin = realpathSync(found.bin);
const files = new Set([join(pkg, "package.json"), bin]);
const walk = (dir) => {
let entries;
try { entries = readdirSync(dir, { withFileTypes: true }); } catch (e) { if (e.code === "ENOENT") return; throw e; }
for (const e of entries) {
const p = join(dir, e.name);
if (e.isDirectory()) walk(p);
else if (e.isFile() || (e.isSymbolicLink() && lstatSync(realpathSync(p)).isFile())) files.add(p);
if (files.size > DIGEST_FILES_MAX) throw new Error("too many files");
}
};
walk(join(pkg, "dist", "bundle"));
const binDir = dirname(bin);
if (binDir !== pkg && binDir.startsWith(`${pkg}/`)) walk(binDir);
// A copy mounted with the node_modules it sits in imports packages beside it: npm's lock files
// there stand for them.
if (found.root !== pkg) for (const f of [join(found.root, ".package-lock.json"), join(dirname(found.root), "package-lock.json")]) {
try { lstatSync(f); } catch (e) { if (e.code === "ENOENT") continue; throw e; }
files.add(f);
}
const h = createHash("sha256");
for (const f of [...files].sort()) {
h.update(relative(pkg, f)).update("\0").update(createHash("sha256").update(readFileSync(f)).digest("hex")).update("\n");
}
return h.digest("hex");
}
+2 -1
View File
@@ -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";
+4 -2
View File
@@ -12,7 +12,8 @@ import { join, relative } from "node:path";
import { buildBundle, PERSONA_CHARS } from "../src/bundle.mjs";
import { decide } from "../src/gate.mjs";
import { adapterEnv } from "../src/runner.mjs";
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
const sha = (data) => createHash("sha256").update(data).digest("hex");
const CREATIVE = ["companion", "ideas", "research", "proofreader", "fact-checker", "continuity"];
@@ -177,7 +178,8 @@ function runAdapter(t, harness, b, { prompt = b.files.prompt, cwd } = {}) {
writeFileSync(cli, '#!/bin/sh\nprintf "%s\\0" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n');
chmodSync(cli, 0o755);
const s = { harness, provider: harness === "pi" ? "probe" : null, model: "claude-sonnet-5-5", workspace: b.workspace, sessionDir: join(b.dir, "session"), toolSocket: join(b.dir, "tools.sock"), turnMarker: join(b.dir, "turn.done"), bundle: { ...b.files, prompt }, harnessEnv: b.env };
const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x" };
// #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found.
const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) };
const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
const argv = existsSync(env.ARGV_OUT) ? readFileSync(env.ARGV_OUT, "utf8").split("\0") : null;
return { ...r, argv };
+6
View File
@@ -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.
@@ -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'];
File diff suppressed because it is too large. Load diff
+2 -1
View File
@@ -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");
+241
View File
@@ -0,0 +1,241 @@
// #1873 pi-unpin S3: a stand-in pi that the fit check (src/pi-fit.mjs) can run. It is one sh
// script with its knobs written into it, because the fit check runs it with an empty
// environment. Put it at dist/bundle/cli.js in a package named @earendil-works/pi-coding-agent.
//
// What it does, in order:
// --version prints `version` (default 9.9.9), or exits `versionExit` when that is set, or
// hangs for a minute with `versionHang`
// $ARGV_OUT set: a session. Writes its argv there, prints `sessionOut`, exits 0. This part is
// sh, so it runs in a sandbox that holds no node.
// otherwise runs node (the absolute path, since the fit check gives it no PATH) on the rest:
// --help lists every flag the fit check wants, less `dropFlags`, plus `extraFlags`, then
// the `helpProse` lines as they are
// -e <file> a file that doesn't exist stops it with exit 1, unless `missingExtOk` is true or
// names the mode ("rpc" or "print") it starts in anyway
// --mode rpc loads the planted sources the way pi 0.85.1 finds them, honouring the seal flags,
// or -p runs each -e extension's session_start (one that writes JSON.stringify(...) gets
// the prompt this stand-in builds, the command names and the session id), then
// answers get_state and get_commands
// (rpc), or prints `sessionOut` and exits 0 (-p).
// An -e file that exists loads even with --no-extensions, unless `dropExplicit`
// (then it loads only without --no-extensions). `printNoStart`: -p runs no
// session_start. MCP servers stay out with --no-mcp when `extraFlags` lists it, else
// with --no-extensions.
// `ignoreAll` loads no planted source. `sealLeak` lists kinds (extensions, mcp,
// skills, prompts, context, approve) loaded even when their seal flag is on.
// `ignoreSystemPrompt` drops the --system-prompt file. `ignoreAppend` drops every
// APPEND_SYSTEM.md. `sysReplacesAll`: a --system-prompt file also drops every
// APPEND_SYSTEM.md and context file. `trustExtOnly`: the only extensions are the
// project's .pi/extensions, loaded in a trusted project whatever --no-extensions
// says. `trustCtxOnly`: the only context file is the project's AGENTS.md, loaded the
// same way whatever --no-context-files says. `flood`: prints 8 MB first.
// `hideWhen`: a cue, a flag or "order" (--offline before --no-session). With the cue
// it loads no planted source; without it, every kind even with its seal flag on.
// `standIn`: [flag, other]: it ignores flag, and other keeps flag's kind out instead.
// `remember`: the first run in a scratch directory (it leaves a marker beside the
// -e file's directory) loads every kind whatever the seal flags; a later run loads
// none. `needs`: a flag the loader flags work only with; without it they do
// nothing. `leakWithout`: flags; when any of them is absent, every kind loads.
// `leakWith`: flags; when all of them are given, every kind loads (with
// `leakWithout` too, both must hold). `mcpAlso`: a flag that keeps MCP servers out
// as well as their own flag does. `leakNoAgentDir`: every kind loads when
// PI_CODING_AGENT_DIR is unset, as in a bus session. `leakNoVar`: names; every
// kind loads when any of them is unset (TERM, TZ, LC_ALL in a unit-started bus).
// `leakNoMatch`: with -c, every
// kind loads when no .jsonl in --session-dir names the working directory.
// `leakEnv`: { json, other }, the variable names a run with --mode json and any
// other run should get; every kind loads when the environment holds a name not in
// the list or lacks one, apart from the names /bin/sh sets (PWD, OLDPWD, SHLVL, _).
// An -e extension that writes JSON.stringify({...}) at session_start gets the
// prompt, the command names, the session id (with -c, the id in the header of a
// .jsonl in --session-dir whose cwd is the working directory, else a new one) and
// the --thinking level (else "medium"). `noResume`: -c always starts a new
// session. `oneExt`: only the first -e loads. `extNoCommands`: the extension sees
// no commands.
// `commands` "fail" answers get_commands with success false; "list" answers
// with a bare list.
// otherwise a session with no $ARGV_OUT: prints `sessionOut` and exits 0.
//
// Trust, as pi 0.85.1 has it: the project is trusted with --approve, or with a saved trust and
// no --no-approve. Only the project SYSTEM.md and APPEND_SYSTEM.md wait on it here. The user
// APPEND_SYSTEM.md is appended whatever the flags, as 0.85.1 does when the project has none.
import { FIT_FLAGS } from "../src/pi-fit.mjs";
const BODY = String.raw`
const fs = require("node:fs");
const path = require("node:path");
const cp = require("node:child_process");
const argv = process.argv.slice(1);
const has = (f) => argv.includes(f);
const val = (f) => { const i = argv.indexOf(f); return i === -1 ? undefined : argv[i + 1]; };
if (argv[0] === "--help") {
const drop = new Set(K.dropFlags || []);
const lines = ["Usage: pi [options] [messages...]", "", "Options:"];
for (const f of FLAGS) {
if (drop.has(f)) continue;
if (f === "--mode") lines.push(" --mode <mode> Output mode: " + (K.modes || ["text", "json", "rpc"]).join(", "));
else if (f === "--thinking") lines.push(" --thinking <level> Thinking level: " + (K.levels || ["off", "minimal", "low", "medium", "high", "xhigh", "max"]).join(", "));
else lines.push(" " + f);
}
for (const f of K.extraFlags || []) lines.push(" " + f);
for (const l of K.helpProse || []) lines.push(l);
process.stdout.write(lines.join("\n") + "\n");
process.exit(0);
}
const rpc = val("--mode") === "rpc";
const print = !rpc && has("-p");
const startsAnyway = K.missingExtOk === true || (rpc && K.missingExtOk === "rpc") || (print && K.missingExtOk === "print");
for (let i = 0; i < argv.length; i++) {
if (argv[i] === "-e" && !fs.existsSync(argv[i + 1]) && !startsAnyway) {
process.stderr.write("Failed to load extension: it does not exist\n");
process.exit(1);
}
}
if (rpc || print) {
if (K.flood) {
const chunk = Buffer.alloc(1024 * 1024, "x");
for (let n = 0; n < 8; n++) for (let off = 0; off < chunk.length;) {
try { off += fs.writeSync(1, chunk, off); } catch (e) { if (e.code !== "EAGAIN") throw e; }
}
}
const ALL = ["extensions", "mcp", "skills", "prompts", "context", "approve"];
// The id in the header of the last .jsonl in dir whose cwd is the working directory, or null.
const storedFor = (dir) => {
let id = null;
if (dir) for (const n of (() => { try { return fs.readdirSync(dir); } catch { return []; } })()) {
if (!n.endsWith(".jsonl")) continue;
let h = null;
try { h = JSON.parse(fs.readFileSync(path.join(dir, n), "utf8").split("\n")[0]); } catch { h = null; }
if (h && h.type === "session" && h.cwd === process.cwd()) id = h.id;
}
return id;
};
let ignoreAll = K.ignoreAll;
let leak = new Set(K.sealLeak || []);
if (K.hideWhen) {
const cued = K.hideWhen === "order" ? argv.indexOf("--offline") !== -1 && argv.indexOf("--offline") < argv.indexOf("--no-session") : has(K.hideWhen);
if (cued) ignoreAll = true;
else leak = new Set(ALL);
}
if (K.remember) {
const marker = path.join(path.dirname(path.dirname(val("-e"))), "seen-before");
if (fs.existsSync(marker)) ignoreAll = true;
else { fs.writeFileSync(marker, ""); leak = new Set(ALL); }
}
if ((K.leakWith || K.leakWithout) && (K.leakWith || []).every(has) && (!K.leakWithout || K.leakWithout.some((f) => !has(f)))) leak = new Set(ALL);
if (K.leakNoAgentDir && !process.env.PI_CODING_AGENT_DIR) leak = new Set(ALL);
if (K.leakNoVar && K.leakNoVar.some((n) => process.env[n] === undefined)) leak = new Set(ALL);
if (K.leakNoMatch && has("-c") && !storedFor(val("--session-dir"))) leak = new Set(ALL);
if (K.leakEnv) {
const want = new Set(K.leakEnv[val("--mode") === "json" ? "json" : "other"]);
const got = Object.keys(process.env).filter((n) => !["PWD", "OLDPWD", "SHLVL", "_"].includes(n));
if (got.length !== want.size || got.some((n) => !want.has(n))) leak = new Set(ALL);
}
// Whether a seal flag is on: with standIn, its flag reads as the other flag; with needs, only
// when that flag is given too.
const sealed = (flag) => has(K.standIn && K.standIn[0] === flag ? K.standIn[1] : flag) && (!K.needs || has(K.needs));
const on = (kind, flag) => !ignoreAll && (!sealed(flag) || leak.has(kind));
const agent = process.env.PI_CODING_AGENT_DIR || path.join(process.env.HOME || "/", ".pi", "agent");
const cwd = process.cwd();
const list = (dir, test) => { try { return fs.readdirSync(dir).filter(test).map((n) => path.join(dir, n)); } catch { return []; } };
const json = (file) => { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } };
const read = (file) => { try { return fs.readFileSync(file, "utf8"); } catch { return null; } };
const settings = [json(path.join(agent, "settings.json")), json(path.join(cwd, ".pi", "settings.json"))].filter(Boolean);
const trust = json(path.join(agent, "trust.json")) || {};
const saved = settings.some((s) => s.defaultProjectTrust === "always") || trust[cwd] === true;
const trusted = !ignoreAll && (has("--approve") || (saved && (!sealed("--no-approve") || leak.has("approve"))));
const commands = [];
// A planted extension writes one marker on load; an -e extension may also write what it saw at
// session_start. Do what each would do.
const starts = [];
const load = (f) => {
const text = read(f);
if (text === null) return;
for (const m of text.matchAll(/writeFileSync\(("(?:[^"\\]|\\.)*")(, JSON\.stringify\()?/g)) {
if (m[2]) starts.push(JSON.parse(m[1]));
else fs.writeFileSync(JSON.parse(m[1]), "");
}
};
const given = argv.flatMap((a, i) => a === "-e" ? [argv[i + 1]] : []);
if (!(K.dropExplicit && has("--no-extensions"))) for (const f of K.oneExt ? given.slice(0, 1) : given) load(f);
const packages = settings.flatMap((s) => (s.packages || []).map((p) => typeof p === "string" ? p : p.source));
if (K.trustExtOnly) {
if (trusted) for (const f of list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))) load(f);
} else if (on("extensions", "--no-extensions")) {
const files = [...list(path.join(agent, "extensions"), (n) => n.endsWith(".ts")), ...list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))];
for (const s of settings) files.push(...(s.extensions || []));
for (const p of packages) files.push(...list(path.join(p, "extensions"), (n) => n.endsWith(".ts")));
for (const f of files) load(f);
}
const mcpFlag = (K.extraFlags || []).includes("--no-mcp") ? "--no-mcp" : "--no-extensions";
if (!ignoreAll && (!(sealed(mcpFlag) || (K.mcpAlso && has(K.mcpAlso))) || leak.has("mcp"))) {
for (const f of [path.join(agent, "mcp.json"), path.join(cwd, ".pi", "mcp.json"), path.join(cwd, ".mcp.json")]) {
for (const s of Object.values(json(f)?.mcpServers || {})) cp.spawnSync(s.command, s.args || [], { stdio: "ignore" });
}
}
if (on("skills", "--no-skills")) {
const home = process.env.HOME || "/";
const dirs = [path.join(agent, "skills"), path.join(cwd, ".pi", "skills"), path.join(cwd, ".agents", "skills"), path.join(home, ".agents", "skills"), ...packages.map((p) => path.join(p, "skills"))];
for (const d of dirs) for (const p of list(d, () => true)) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
for (const s of settings) for (const p of s.skills || []) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
}
if (on("prompts", "--no-prompt-templates")) {
const dirs = [path.join(agent, "prompts"), path.join(cwd, ".pi", "prompts"), ...packages.map((p) => path.join(p, "prompts"))];
for (const d of dirs) for (const p of list(d, (n) => n.endsWith(".md"))) commands.push({ name: path.basename(p, ".md"), source: "prompt" });
for (const s of settings) for (const p of s.prompts || []) commands.push({ name: path.basename(p, ".md"), source: "prompt" });
}
// The system prompt: the --system-prompt file, else SYSTEM.md; then APPEND_SYSTEM.md; then the
// context files, from the agent directory and from / down to the working directory.
const sys = K.ignoreSystemPrompt ? null : val("--system-prompt");
const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!ignoreAll && read(path.join(agent, name))) || "";
const hide = K.sysReplacesAll && sys;
const parts = [sys ? (read(sys) || "") : own("SYSTEM.md"), K.ignoreAppend || hide ? "" : own("APPEND_SYSTEM.md")];
if (K.trustCtxOnly) {
if (trusted) parts.push(read(path.join(cwd, "AGENTS.md")) || "");
} else if (!hide && on("context", "--no-context-files")) {
const dirs = [];
for (let d = cwd; ; d = path.dirname(d)) { dirs.unshift(d); if (path.dirname(d) === d) break; }
parts.push(read(path.join(agent, "AGENTS.md")) || "");
for (const d of dirs) parts.push(read(path.join(d, "AGENTS.md")) || read(path.join(d, "CLAUDE.md")) || "");
}
// The session -c resumes: the newest .jsonl in --session-dir whose header names this cwd.
let session = "stand-in-" + process.pid;
const dir = val("--session-dir");
if (has("-c") && dir && !K.noResume) session = storedFor(dir) || session;
const start = { prompt: parts.join("\n"), commands: K.extNoCommands ? [] : commands.map((c) => c.name), session };
if (!(print && K.printNoStart)) for (const f of starts) fs.writeFileSync(f, JSON.stringify(start));
if (print) {
if (K.sessionOut) process.stdout.write(K.sessionOut);
process.exit(0);
}
let input = "";
try { input = fs.readFileSync(0, "utf8"); } catch { input = ""; }
for (const line of input.split("\n")) {
let m;
try { m = JSON.parse(line); } catch { continue; }
if (m.type === "get_state") process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_state", success: true, data: { isStreaming: false } }) + "\n");
if (m.type === "get_commands") {
const answer = K.commands === "fail" ? { success: false, error: "no" } : { success: true, data: K.commands === "list" ? commands : { commands } };
process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_commands", ...answer }) + "\n");
}
}
process.exit(0);
}
if (K.sessionOut) process.stdout.write(K.sessionOut);
process.exit(0);
`;
const quote = (text) => `'${String(text).replace(/'/g, "'\\''")}'`;
// The script text. node: the interpreter the fit part runs on (absolute, so it runs with no PATH).
export function piStandIn(knobs = {}, { node = process.execPath } = {}) {
const version = knobs.versionHang ? "sleep 60; exit 0" : knobs.versionExit != null ? `exit ${Number(knobs.versionExit)}` : `printf '%s\\n' ${quote(knobs.version ?? "9.9.9")}; exit 0`;
const program = `"use strict";\nconst K = ${JSON.stringify(knobs)};\nconst FLAGS = ${JSON.stringify(FIT_FLAGS)};\n${BODY}`;
return [
"#!/bin/sh",
`[ "$1" = --version ] && { ${version}; }`,
`if [ -n "\${ARGV_OUT:-}" ]; then printf '%s\\n' "$@" > "$ARGV_OUT"; printf '%s' ${quote(knobs.sessionOut ?? "")}; exit 0; fi`,
`exec ${quote(node)} -e ${quote(program)} -- "$@"`,
"",
].join("\n");
}
+2 -1
View File
@@ -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' },
+2 -1
View File
@@ -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';
@@ -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';
+4 -2
View File
@@ -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 = /<skill-file skill="([^"]+)" path="([^"]+)">\n([\s\S]*?)\n<\/skill-file>/g;
@@ -183,7 +184,8 @@ test("each adapter passes the skills only in the prompt file, with skill discove
const b = buildBundle({ dir: join(dir, "bundle"), resolved, contract: "# Contract\n", harness: HARNESS[harness], models, skillsDir: REPO_SKILLS, session: { ...s, run: "run-1", launchedBy: "pm" } });
s.bundle = b.files;
s.harnessEnv = b.env;
const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x" };
// #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found.
const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) };
const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
assert.equal(r.status, 0, `${harness}: ${r.stderr}`);
const argv = readFileSync(join(dir, "argv"), "utf8").split("\0");
@@ -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;
+35 -3
View File
@@ -246,10 +246,42 @@ export returns:
`minimal`, `low`, `medium`, `high`, `xhigh` and `max` as `--thinking`,
Claude Code takes `low`, `medium`, `high`, `xhigh` and `max` as
`--effort`.
- `preflight({env})` returns `null` or `{code, message}`: pi checks its
binary in `node_modules` (`harness-binary-missing`), Claude Code its
- `preflight({env})` returns `null` or `{code, message}`, or a promise of
one, which the launcher awaits before it launches: pi checks that
it finds a pi binary (`harness-binary-missing`), Claude Code its
session credential (`no-session-credential`) and `MOSAIC_CLAUDE_BIN`.
- `version({env})` is what the manifest records as `harnessVersion`.
Pi (#1873 pi-unpin, `harness/pi/binary.mjs`) takes `MOSAIC_PI_BIN`,
which must be an absolute path to an executable file; else the first
`pi` on PATH, skipping the repository's own `node_modules/.bin`; else
the repository's copy. The binary must belong to the
`@earendil-works/pi-coding-agent` package: it is the bin that package's
`package.json` names. With none, the message is
"no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci". No
version is pinned. Pi's `mounts` and `installRoots` hold the found
package's directory (the repository's whole `node_modules` for the
repository's copy, whose packages are hoisted beside pi), and its `env`
sets `MOSAIC_PI_BIN` to the binary's path inside the session, which the
adapter runs. A refusal names the variable or "the pi on PATH" or "the
repository's pi", never a host path. Pi's `preflight` then awaits
the fit check (#1873 S3, `packages/harness/src/pi-fit.mjs`), which
runs its sandboxed pi starts as child processes, four at a time, so the
bus keeps answering, and which every caller that asks while it runs
shares. It refuses `harness-feature-missing` when the binary lacks a flag the
adapter passes, starts with a missing `-e` file, loads a planted ambient
source or context file with the seal flags on, or can't be checked. When its `--help`
lists `--no-mcp`, `env` sets `MOSAIC_PI_NO_MCP=1` and the adapter
passes `--no-mcp`; otherwise it sets `MOSAIC_PI_NO_MCP=0`. `env`'s
variables are built in `harness/pi/session-env.mjs`, which the fit
check also uses so its runs get a session's names, from the bus's own
environment as a session's are. `env` runs nothing: it reads the passed
check for the binary's files and the bus's names, and throws when there
is none. A host that calls `env` without `preflight` awaits the
optional `ready({env})` first (the Triage host and route do), which runs
the check, or shares the one running, and throws its refusal line.
- `version({env})` is what the manifest records as `harnessVersion`, or a
promise of it, which the launcher awaits. Pi's is the first line its
binary prints for `--version`, or null; the binary runs as a child
process.
- `countsAccount()` says whether its launches spend `launch.account` and get
the row 126 account checks. The same answer decides whether an account
quota covers authority hires (#1873); the bus reads it once at start, so
+153
View File
@@ -0,0 +1,153 @@
// #1873 pi-unpin: the pi binary a session runs, and the package it comes from. Nothing here is
// pinned. The version label is read from the binary (or its package.json), never compared to a
// fixed one. The launcher, the bundle and the usage reader call these on the host.
import { accessSync, constants, readFileSync, realpathSync, statSync } from "node:fs";
import { dirname, isAbsolute, join, relative } from "node:path";
import { homedir } from "node:os";
import { fileURLToPath } from "node:url";
import { spawn } from "node:child_process";
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
export const PACKAGE = "@earendil-works/pi-coding-agent";
export const REPO_BIN = join(REPO, "node_modules", ".bin", "pi");
const VERSION_MAX = 128;
export const executable = (path) => {
try {
accessSync(path, constants.X_OK);
return statSync(path).isFile();
} catch {
return false;
}
};
const REPO_NODE_MODULES = join(REPO, "node_modules");
const realOrNull = (path) => { try { return realpathSync(path); } catch { return null; } };
const inside = (path, root) => root === "/" || path === root || path.startsWith(root + "/");
// #1873 S2: the binary a session runs, with the name a refusal line uses for it (never a path):
// MOSAIC_PI_BIN, which must be an absolute path to an executable file; else the first executable
// pi on PATH, skipping the repository's own node_modules/.bin (a session PATH starts with it, so
// the bus's own PATH and a session's give the same answer); else the repository's copy. The binary
// must belong to the pi package: its package directory (or, for the repository's copy, the
// repository's node_modules) is what the sandbox mounts. The answer: { bin, label, root, view,
// dest, pkg } or { bin: null, problem }. root is the real directory mounted, dest where it is
// mounted (null: at root itself), view the binary's path inside the session, pkg the package's
// real directory.
export function locate(env = {}) {
if (env.MOSAIC_PI_BIN !== undefined && env.MOSAIC_PI_BIN !== "") {
const bin = env.MOSAIC_PI_BIN;
if (typeof bin !== "string" || !isAbsolute(bin) || !executable(bin)) return { bin: null, problem: "MOSAIC_PI_BIN isn't an executable file at an absolute path" };
return placed(bin, "MOSAIC_PI_BIN", env);
}
const repoBin = realOrNull(join(REPO_NODE_MODULES, ".bin"));
for (const dir of String(env.PATH ?? "").split(":")) {
if (!isAbsolute(dir)) continue;
const real = realOrNull(dir);
if (real === null || (repoBin !== null && real === repoBin)) continue;
if (executable(join(dir, "pi"))) return placed(join(dir, "pi"), "the pi on PATH", env);
}
if (executable(REPO_BIN)) return placed(REPO_BIN, "the repository's pi", env);
return { bin: null, problem: "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci" };
}
function placed(bin, label, env) {
const pkg = packageDir(bin);
const realBin = realOrNull(bin);
if (!pkg || !realBin) return { bin: null, problem: `${label} isn't part of the ${PACKAGE} package` };
// The package directory is mounted whole, so the binary must be the pi bin its package.json
// names (a string bin, or bin.pi), inside that directory. A stray executable under some wider
// directory whose package.json carries the name doesn't count.
if (namedBin(pkg) !== realBin) return { bin: null, problem: `${label} isn't the pi bin the ${PACKAGE} package names` };
// The repository's copy keeps the mount every session has had: all of node_modules, at its own
// path, since its packages are hoisted beside pi.
const repoReal = realOrNull(REPO_NODE_MODULES);
if (repoReal !== null && inside(pkg, repoReal)) {
return { bin, label, root: repoReal, dest: REPO_NODE_MODULES, view: join(REPO_NODE_MODULES, relative(repoReal, realBin)), pkg };
}
// Any other copy: its package directory alone, at its real path. npm nests a global install's
// dependencies inside it; a copy that needs packages beside it fails the fit check (S3).
// A package directory that holds the home directory or the repository would mount them.
const held = [homedir(), env.HOME, REPO].filter((p) => typeof p === "string" && isAbsolute(p)).map((p) => realOrNull(p) ?? p);
if (held.some((p) => inside(p, pkg))) return { bin: null, problem: `${label}'s package directory holds the home directory or the repository` };
return { bin, label, root: pkg, dest: null, view: realBin, pkg };
}
// The real path of the pi bin the package.json in `pkg` names, inside `pkg`, or null.
function namedBin(pkg) {
let bin;
try { bin = JSON.parse(readFileSync(join(pkg, "package.json"), "utf8")).bin; } catch { return null; }
const rel = typeof bin === "string" ? bin : bin && typeof bin === "object" ? bin.pi : null;
if (typeof rel !== "string" || !rel) return null;
const real = realOrNull(join(pkg, rel));
return real !== null && real !== pkg && inside(real, pkg) ? real : null;
}
// The package directory that holds the binary's real file: the nearest directory above it whose
// package.json has a name. That name must be PACKAGE, or there is none (null).
export function packageDir(bin) {
let dir;
try { dir = dirname(realpathSync(bin)); } catch { return null; }
for (let depth = 0; depth < 32; depth++) {
let pkg = null;
try { pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); } catch { pkg = null; }
if (pkg && typeof pkg === "object" && pkg.name !== undefined) return pkg.name === PACKAGE ? dir : null;
const up = dirname(dir);
if (up === dir) return null;
dir = up;
}
return null;
}
// The found package's own version, a file read with no spawn, or null.
export function packageVersion(bin) {
const dir = bin ? packageDir(bin) : null;
if (!dir) return null;
try {
const v = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).version;
return typeof v === "string" && cleanLabel(v) ? v : null;
} catch {
return null;
}
}
// The most a --version may print (spawnSync's default buffer, which the check used before).
const VERSION_OUTPUT_MAX = 1024 * 1024;
// One printable line, short enough to record.
const cleanLabel = (s) => s.length > 0 && s.length <= VERSION_MAX && !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(s);
// The first line `<bin> --version` prints, or null when it fails, times out or prints something
// that is not one short printable line. The environment holds only a PATH that finds node. A
// promise: the binary runs as a child process, so the caller's event loop keeps running.
export function printedVersion(bin) {
if (!bin) return Promise.resolve(null);
return new Promise((resolve) => {
const out = [];
let size = 0;
let done = false;
let child;
let timer = null;
const finish = (line) => {
if (done) return;
done = true;
clearTimeout(timer);
resolve(line);
};
try {
child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"] });
} catch {
finish(null);
return;
}
timer = setTimeout(() => { try { child.kill("SIGKILL"); } catch {} finish(null); }, 5000);
child.stdout.on("data", (chunk) => {
size += chunk.length;
if (size > VERSION_OUTPUT_MAX) { try { child.kill("SIGKILL"); } catch {} finish(null); } else out.push(chunk);
});
child.on("error", () => finish(null));
child.on("close", (status) => {
const line = Buffer.concat(out).toString("utf8").split("\n")[0].trim();
finish(status === 0 && cleanLabel(line) ? line : null);
});
});
}
+6 -4
View File
@@ -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() };
}
+63 -35
View File
@@ -1,18 +1,19 @@
// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the pinned
// @earendil-works/pi-coding-agent CLI, the repository's node_modules/.bin/pi. The tool gate is the
// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the
// @earendil-works/pi-coding-agent CLI that binary.mjs finds (#1873: no version pin). The tool gate is the
// Pi extension, which stays in packages/harness (a module can't change that boundary). The
// launcher, the bundle and the usage reader call these on the host; the runner gets the answers
// as data in session.json. packages/plugins/README.md lists the interface.
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
import { lstatSync, mkdirSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { createHash } from "node:crypto";
import { fileURLToPath } from "node:url";
import { locate, printedVersion } from "./binary.mjs";
import { fitVerdict, piFit } from "../../../harness/src/pi-fit.mjs";
import { envNames, harnessEnv } from "./session-env.mjs";
export const interfaceVersion = 1;
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
const PACKAGE = "@earendil-works/pi-coding-agent";
const BIN = join(REPO, "node_modules", ".bin", "pi");
const EXTENSION = fileURLToPath(new URL("../../../harness/src/pi-extension.mjs", import.meta.url));
// Row 66 (#1559): the unslop check, loaded as a second extension. It logs to MOSAIC_UNSLOP_LOG,
// which the runner sets, and the runner tells the operator.
@@ -31,33 +32,53 @@ export const adapter = () => join(REPO, "adapters", "pi", "adapter.sh");
export const levels = () => LEVELS;
export const outputPolicy = ({ proxy }) => proxy ? "non-newline-stdout" : "stdout";
// The session runs pi from the repository's node_modules, which the sandbox mounts.
export function preflight({ env = {}, proxy = null } = {}) {
// The session runs the pi binary.mjs finds, from the package directory the sandbox mounts. Async:
// the fit check runs child processes, and the launcher awaits the answer before it launches.
export async function preflight({ env = {}, proxy = null } = {}) {
if (!(typeof proxy?.key === "string" && proxy.key)
&& !["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY"].some(name => Boolean(env[name]))) {
return { code: "no-session-credential", message: "pi needs its own provider key or a session proxy key" };
}
if (!existsSync(BIN)) return { code: "harness-binary-missing", message: "pi isn't installed in the repository's node_modules; run npm ci in the repository" };
const found = locate(env);
if (!found.bin) return { code: "harness-binary-missing", message: found.problem };
// #1873 S3: the found binary takes every flag the adapter passes and keeps the known ambient
// sources off with them, or the launch is refused. A check that cannot tell refuses too. The
// check's environment comes from this process's own (the bus's), as a session's does.
const fit = await piFit(found, { levels: LEVELS });
if (!fit.ok) return { code: "harness-feature-missing", message: fit.message };
return null;
}
// The exact pin from the repository's package.json.
export function version() {
try {
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
return pkg.dependencies?.[PACKAGE] ?? pkg.devDependencies?.[PACKAGE] ?? null;
} catch {
return null;
}
// #1873 S1: what the found binary prints for --version (5 s timeout), or null. A label only:
// nothing compares it to a fixed version. Async, so the bus keeps answering while it runs.
export async function version({ env = {} } = {}) {
return printedVersion(locate(env).bin);
}
// #1873 S3: runs the fit check, or shares the one running, and throws its refusal line when the
// binary fails it. A host that calls env() without preflight (Triage) awaits this first.
export async function ready({ env = process.env } = {}) {
const fit = await piFit(found(env), { levels: LEVELS });
if (!fit.ok) throw new Error(fit.message);
}
// Pi sessions run on a provider key, never the Claude account limit.
export const countsAccount = () => false;
export const mounts = () => [
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
{ src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" },
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
];
// #1873 S2: the found pi's package directory (the repository's copy: all of node_modules, as before).
// One under /usr is already in every sandbox.
const found = (env) => {
const at = locate(env);
if (!at.bin) throw new Error(at.problem);
return at;
};
export function mounts({ env = {} } = {}) {
const at = found(env);
return [
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
...(at.root.startsWith("/usr/") ? [] : [{ src: at.root, ...(at.dest ? { dest: at.dest } : {}), why: `pi and its packages (${at.label})` }]),
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
];
}
// Pi has no web tool of its own, so a granted role under harness-builtin is refused.
export const builtinWeb = () => false;
export const builtins = ({ tools }) => [...tools];
@@ -105,26 +126,33 @@ export function bundle({ dir, models, proxy = null, transports = {} } = {}) {
return { files: { proxyModels: file }, digests: [["../home/.pi/agent/models.json", createHash("sha256").update(text).digest("hex")]], sources };
}
// The tool limit is the built-ins plus the typed tools, which the extension serves.
export function env({ policy, tools, session, proxy = null }) {
return {
PI_OFFLINE: "1",
MOSAIC_TOOLS: [...policy.tools, ...tools.map((t) => t.name)].join(","),
MOSAIC_EXTENSIONS: [EXTENSION, UNSLOP.unslopExtension].join(","),
PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "",
PI_MODEL: session.model,
MOSAIC_THINKING: session.thinking,
...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}),
};
// The tool limit is the built-ins plus the typed tools, which the extension serves. #1873 S2:
// MOSAIC_PI_BIN is the found binary's path inside the session; the bus's own environment names it
// (env defaults to it, since buildBundle and the Triage host call env() without one). preflight,
// version and mounts get the launcher's env instead. Both find the same binary: the session env
// copies PATH and MOSAIC_PI_BIN from the bus's, and locate() skips the repository's .bin on PATH.
// env() runs nothing: it reads the fit check that preflight or ready() awaited for what it found.
// With none passed for those files and this environment (they differ, the files changed since, or
// nobody awaited one), it throws, so the launch fails closed (as harness-failed, not
// harness-feature-missing).
export function env({ policy, tools, session, proxy = null, env: hostEnv = process.env }) {
const at = found(hostEnv);
// #1873 S3: a session runs only a binary that passed the fit check.
const fit = fitVerdict(at, { levels: LEVELS });
if (!fit) throw new Error(`pi (${at.label}) has no passed fit check for its files; preflight or ready() runs one first`);
return harnessEnv({ bin: at.view, noMcp: fit.noMcp, tools: [...policy.tools, ...tools.map((t) => t.name)], extensions: [EXTENSION, UNSLOP.unslopExtension], session, proxy });
}
// #1873 triage-fences: the names env() returns, less the provider key, and the directories its
// adapter and mounts live in. A hosted session's host (Triage) takes harnessEnv names and mount
// sources only from these; launches don't read them. The host refuses every MOSAIC_* name from a
// route, so MOSAIC_TOOLS, MOSAIC_EXTENSIONS and MOSAIC_THINKING are listed only to say what env()
// hands a session: a hosted pi needs the host to set them itself (a later slice).
export const envNames = Object.freeze(["PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]);
export const installRoots = () => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), realpathSync(join(REPO, "node_modules")), join(REPO, "tools", "unslop-hook")];
// hands a session: a hosted pi needs the host to set them itself (a later slice). MOSAIC_PI_BIN
// (#1873 S2) is the binary's path inside the session, which the host keeps from its own env() call;
// MOSAIC_PI_NO_MCP (#1873 S3) is "1" when the binary's --help lists --no-mcp, else "0".
// session-env.mjs holds the list beside the builder.
export { envNames };
export const installRoots = ({ env = {} } = {}) => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), found(env).root, join(REPO, "tools", "unslop-hook")];
// The extension writes the turn marker at agent_end; an exit without it is a failed turn.
export const turnEnd = () => "marker";
@@ -0,0 +1,34 @@
// The variables the pi module hands a session (index.mjs env() returns them as harnessEnv). Built
// here, with no fit check, so packages/harness/src/pi-fit.mjs (#1873 S3) gives its runs the same
// names as a session gets without importing index.mjs, which imports it.
//
// bin: the binary's path inside the session. noMcp: the binary's --help lists --no-mcp. tools: the
// tool names. extensions: the -e files. session: { provider, model, thinking, proxyTransport }.
// proxy: { key } when the session runs on the proxy, else null.
export function harnessEnv({ bin, noMcp, tools, extensions, session, proxy = null }) {
return {
MOSAIC_PI_BIN: bin,
// "1" when --help lists --no-mcp: the adapter then passes it, so MCP stays off by its own
// flag. "0" otherwise, so every name in envNames is always set.
MOSAIC_PI_NO_MCP: noMcp ? "1" : "0",
PI_OFFLINE: "1",
MOSAIC_TOOLS: tools.join(","),
MOSAIC_EXTENSIONS: extensions.join(","),
PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "",
PI_MODEL: session.model,
MOSAIC_THINKING: session.thinking,
...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}),
};
}
// #1873 triage-fences: the names harnessEnv returns, less the provider key. index.mjs exports
// them; its comment says what a hosted session's host does with them.
export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]);
// #1873 pi-unpin re-check 8: the provider and model the fit check's session-shaped runs pass
// (packages/harness/src/pi-fit.mjs SESSION_SHAPE). The runs are offline with no key, so pi stops
// before any model call; the names only need to be ones pi takes. Re-check 9: a Claude model is
// offered, routed and tested only on the claude-code harness, so this names no Claude model and
// no Anthropic provider (pi-fit.test.mjs holds that); glm-5.3-flash is the model index.mjs has
// documented limits for, and pi's own catalogue lists it under zai.
export const FIT_ROUTE = Object.freeze({ provider: "zai", model: "glm-5.3-flash" });
+3
View File
@@ -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
+20 -10
View File
@@ -3,13 +3,14 @@
// harness outside the exceptions the brief lists.
import test from "node:test";
import assert from "node:assert/strict";
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { spawnSync } from "node:child_process";
import { createLoader } from "../src/loader.mjs";
import { KINDS } from "../src/kinds.mjs";
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
@@ -98,11 +99,11 @@ test("row 65 claude-code version asks MOSAIC_CLAUDE_BIN when set, else claude on
assert.equal(claude.version({ env: { PATH: join(dir, "none") } }), null);
});
test("row 65 pi preflight refuses missing credentials, and its version is the package pin", async () => {
test("row 65 pi preflight refuses missing credentials, and its version is what the installed pi prints (#1873 S1)", async () => {
const pi = await load("pi");
assert.equal(pi.preflight({ env: {} }).code, "no-session-credential");
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
assert.equal(pi.version({ env: {} }), pkg.dependencies?.["@earendil-works/pi-coding-agent"] ?? pkg.devDependencies?.["@earendil-works/pi-coding-agent"]);
assert.equal((await pi.preflight({ env: {} })).code, "no-session-credential");
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", "@earendil-works", "pi-coding-agent", "package.json"), "utf8")).version;
assert.equal(await pi.version({ env: {} }), installed);
// Row 66: the unslop extension's files, bound read-only at their own paths.
assert.deepEqual(pi.mounts({ env: {} }).map(m => m.dest ?? m.src), [join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), join(REPO, "node_modules"), ...["extension.ts", "unslop-check.js", "lists.json"].map(f => join(REPO, "tools", "unslop-hook", f))]);
});
@@ -110,6 +111,8 @@ test("row 65 pi preflight refuses missing credentials, and its version is the pa
test("row 65 env passes the thinking level to both adapters, and claude-code's bundle writes its hook and MCP files", async t => {
const pi = await load("pi"), claude = await load("claude-code");
const session = { provider: "fixture", model: "m", thinking: "high", toolSocket: "/run/tools.sock" };
// #1873 S3: env() reads the fit check ready() awaits for the found pi.
await pi.ready();
const piEnv = pi.env({ policy: { tools: ["read"] }, tools: [{ name: "list_tasks" }], session });
assert.equal(piEnv.PI_OFFLINE, "1", "pi avoids startup network operations");
assert.deepEqual([piEnv.MOSAIC_THINKING, piEnv.PI_MODEL, piEnv.MOSAIC_TOOLS], ["high", "m", "read,list_tasks"]);
@@ -140,10 +143,16 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort,
const pi = await load("pi"), claude = await load("claude-code");
const dir = scratch(t);
mkdirSync(join(dir, "bin"));
for (const name of ["pi", "claude"]) {
writeFileSync(join(dir, "bin", name), '#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n');
chmodSync(join(dir, "bin", name), 0o755);
}
// #1873 S2: pi's stand-in sits in a pi package, as the module requires, and is found on PATH.
const piPackage = join(dir, "lib", "node_modules", "@earendil-works", "pi-coding-agent");
mkdirSync(join(piPackage, "dist", "bundle"), { recursive: true });
writeFileSync(join(piPackage, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.0.1", bin: { pi: "dist/bundle/cli.js" } }));
const result = '{"type":"result","subtype":"success","result":"ok"}\n';
writeFileSync(join(dir, "bin", "claude"), `#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho '${result.trim()}'\n`);
// #1873 S3: pi's stand-in also passes the fit check that ready() runs and env() reads.
writeFileSync(join(piPackage, "dist", "bundle", "cli.js"), piStandIn({ sessionOut: result }));
for (const file of [join(dir, "bin", "claude"), join(piPackage, "dist", "bundle", "cli.js")]) chmodSync(file, 0o755);
symlinkSync(join(piPackage, "dist", "bundle", "cli.js"), join(dir, "bin", "pi"));
for (const f of ["prompt.md", "settings.json", "mcp.json"]) writeFileSync(join(dir, f), "{}");
const run = (adapter, moduleEnv) => {
const out = join(dir, "argv");
@@ -163,9 +172,10 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort,
assert.equal(argv.filter(a => a === name).length, 1, argv.join(" "));
return argv[argv.indexOf(name) + 1];
};
await pi.ready({ env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } });
for (const thinking of ["xhigh", "low"]) {
const session = { provider: "fixture", model: "m", thinking, toolSocket: "/run/tools.sock" };
assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session })), "--thinking"), thinking);
assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session, env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } })), "--thinking"), thinking);
const files = { settings: join(dir, "settings.json"), mcp: join(dir, "mcp.json") };
assert.equal(flag(run("claude", claude.env({ files, policy: { tools: ["read"], typed: [] }, session })), "--effort"), thinking);
}
+216
View File
@@ -0,0 +1,216 @@
// #1873 pi-unpin (design section 5, S1): pi's labels come from the binary the module finds, not
// from a pin. The version is what `<bin> --version` prints, or null when that fails; the
// compaction count records the found package's own version.
import test from "node:test";
import assert from "node:assert/strict";
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { spawnSync } from "node:child_process";
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
const MODULE = join(REPO, "packages", "plugins", "harness", "pi");
const NAME = "@earendil-works/pi-coding-agent";
function scratch(t) {
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-bin-"));
t.after(() => rmSync(root, { recursive: true, force: true }));
return root;
}
// A pi package under `dir` (a node_modules directory), with `script` as its bin.
function standIn(dir, script, version) {
const pkg = join(dir, NAME);
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: NAME, version, bin: { pi: "dist/bundle/cli.js" } }));
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), script, { mode: 0o755 });
return join(pkg, "dist", "bundle", "cli.js");
}
// A copy of the pi module in a scratch repository whose node_modules holds a stand-in, so the
// module's repository copy is that stand-in and not the real pi. The rest of packages/ links to
// the real tree, for the modules the copy imports.
async function copyRepo(t, script, version) {
const root = scratch(t);
const mod = join(root, "packages", "plugins", "harness", "pi");
mkdirSync(mod, { recursive: true });
for (const f of readdirSync(MODULE)) if (f.endsWith(".mjs")) copyFileSync(join(MODULE, f), join(mod, f));
for (const p of readdirSync(join(REPO, "packages"))) if (p !== "plugins") symlinkSync(join(REPO, "packages", p), join(root, "packages", p));
standIn(join(root, "node_modules"), script, version);
mkdirSync(join(root, "node_modules", ".bin"));
symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi"));
return { root, pi: await import(pathToFileURL(join(mod, "index.mjs")).href) };
}
const emptyStore = (t) => {
const root = scratch(t);
mkdirSync(join(root, "session"));
return { session: join(root, "session"), projects: join(root, "claude-projects") };
};
test("pi-unpin S1: the version is what the found binary prints, and the compaction count records its package's version", async t => {
const { pi } = await copyRepo(t, "#!/bin/sh\n[ \"$1\" = --version ] && echo 9.9.9\n", "9.9.4");
assert.equal(await pi.version({ env: {} }), "9.9.9");
const counted = pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false });
assert.equal(counted.version, "9.9.4");
});
test("pi-unpin S1: a binary that exits 1 on --version gives a null version, not a guess", async t => {
const { pi } = await copyRepo(t, "#!/bin/sh\nexit 1\n", "8.8.8");
assert.equal(await pi.version({ env: {} }), null);
// The package's own file still names its version for the compaction count.
assert.equal(pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false }).version, "8.8.8");
});
test("pi-unpin S1: a version line with a control character or over 128 characters is not recorded", async t => {
const { pi } = await copyRepo(t, "#!/bin/sh\nprintf '1.0\\033[31m\\n'\n", "1.0.0");
assert.equal(await pi.version({ env: {} }), null);
const long = await copyRepo(t, `#!/bin/sh\necho ${"9".repeat(129)}\n`, "1.0.0");
assert.equal(await long.pi.version({ env: {} }), null);
});
test("pi-unpin S1: the repository's real pi reports its installed package's version", async () => {
const pi = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", NAME, "package.json"), "utf8")).version;
assert.equal(await pi.version({ env: {} }), installed);
});
// #1873 pi-unpin (design section 5, S2): the binary comes from MOSAIC_PI_BIN, else the first pi on
// PATH, else the repository's copy. Mounts, install roots and the session's MOSAIC_PI_BIN follow
// the package that binary belongs to. A refusal names the variable, never a path.
const real = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
const CREDENTIAL = { ZAI_API_KEY: "fixture-zai" };
// #1873 S3: the stand-in passes the fit check, prints 7.7.7 for --version, and as a session
// records its argv in $ARGV_OUT.
const ARGV_SCRIPT = piStandIn({ version: "7.7.7" });
// npm's global layout: prefix/lib/node_modules/<package>, with prefix/bin/pi a link to its bin.
function prefix(t, script = ARGV_SCRIPT, version = "7.7.0") {
const root = join(scratch(t), "prefix");
const cli = standIn(join(root, "lib", "node_modules"), script, version);
mkdirSync(join(root, "bin"), { recursive: true });
symlinkSync(`../lib/node_modules/${NAME}/dist/bundle/cli.js`, join(root, "bin", "pi"));
return { root, bin: join(root, "bin", "pi"), cli: realpathSync(cli), pkg: realpathSync(join(root, "lib", "node_modules", NAME)) };
}
const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" };
// pi.env() reads the fit check that preflight awaited for the same files.
const piEnv = (env) => real.env({ policy: { tools: ["read"] }, tools: [], session, env });
const noPath = (problem, ...paths) => { for (const p of paths) assert.ok(!problem.message.includes(p), problem.message); };
test("pi-unpin S2: a stand-in pi in an npm prefix, named by MOSAIC_PI_BIN, is found, mounted alone and run from the mount", async t => {
const p = prefix(t);
const env = { ...CREDENTIAL, MOSAIC_PI_BIN: p.bin, PATH: "/usr/bin:/bin" };
assert.equal(await real.preflight({ env }), null);
assert.equal(await real.version({ env }), "7.7.7");
const mounts = real.mounts({ env });
assert.deepEqual(mounts.filter(m => (m.dest ?? m.src) === p.pkg || m.src === p.pkg).map(m => m.src), [p.pkg]);
assert.ok(!mounts.some(m => (m.dest ?? m.src) === join(REPO, "node_modules")), "the repository's node_modules is not mounted");
const roots = real.installRoots({ env });
assert.ok(roots.includes(p.pkg));
assert.ok(!roots.includes(realpathSync(join(REPO, "node_modules"))));
const given = piEnv(env);
assert.equal(given.MOSAIC_PI_BIN, p.cli, "the session runs the real file inside the mounted package");
assert.ok(real.envNames.includes("MOSAIC_PI_BIN"));
// Run the real adapter in a sandbox that holds the system, the adapter and the module's mounts,
// and not the repository's node_modules: the stand-in records the argv it was given.
if (sandboxProblem()) return t.skip("no usable bwrap");
const out = join(scratch(t), "out");
mkdirSync(out);
writeFileSync(join(out, "prompt.md"), "p");
const argv = ["/usr/bin/bwrap", "--unshare-user", "--unshare-pid", "--unshare-net", "--die-with-parent", "--clearenv", "--ro-bind", "/usr", "/usr"];
for (const link of ["/bin", "/sbin", "/lib", "/lib64"]) {
if (!existsSync(link)) continue;
if (lstatSync(link).isSymbolicLink()) argv.push("--symlink", readlinkSync(link), link);
else argv.push("--ro-bind", link, link);
}
argv.push("--proc", "/proc", "--dev", "/dev", "--ro-bind", join(REPO, "adapters", "pi"), join(REPO, "adapters", "pi"));
for (const m of mounts) argv.push("--ro-bind", m.src, m.dest ?? m.src);
// The extensions the adapter checks for, which a launch's bundle binds.
for (const e of given.MOSAIC_EXTENSIONS.split(",")) argv.push("--ro-bind", e, e);
argv.push("--bind", out, out);
for (const [k, v] of Object.entries({ ...given, PATH: "/usr/bin:/bin", ARGV_OUT: join(out, "argv"), MOSAIC_SYSTEM_PROMPT_FILE: join(out, "prompt.md"),
MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(out, "ws"), MOSAIC_SESSION_DIR: join(out, "session"), MOSAIC_MODEL: "m" })) argv.push("--setenv", k, v);
argv.push("/bin/sh", join(REPO, "adapters", "pi", "adapter.sh"));
const r = spawnSync(argv[0], argv.slice(1), { encoding: "utf8", timeout: 20000 });
assert.equal(r.status, 0, r.stderr);
const ran = readFileSync(join(out, "argv"), "utf8").split("\n");
assert.ok(ran.includes("--no-context-files") && ran.includes("--thinking"), ran.join(" "));
});
test("pi-unpin S2: a relative or non-executable MOSAIC_PI_BIN is refused with harness-binary-missing, naming the variable only", async t => {
const p = prefix(t);
const plain = join(scratch(t), "pi");
writeFileSync(plain, "#!/bin/sh\n", { mode: 0o644 });
for (const bin of ["bin/pi", "pi", plain, join(p.root, "missing")]) {
const problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: bin, PATH: `${join(p.root, "bin")}:/usr/bin:/bin` } });
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "MOSAIC_PI_BIN isn't an executable file at an absolute path"], bin);
noPath(problem, plain, p.root);
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: bin } }), null, "no fallback to another pi");
}
});
test("pi-unpin S2: with MOSAIC_PI_BIN unset the first pi on PATH is used, and the repository's own .bin on PATH does not count", async t => {
const p = prefix(t);
const env = { ...CREDENTIAL, PATH: `${join(REPO, "node_modules", ".bin")}:/nonexistent:relative:${join(p.root, "bin")}:/usr/bin:/bin` };
assert.equal(await real.preflight({ env }), null);
assert.equal(await real.version({ env }), "7.7.7");
assert.equal(piEnv(env).MOSAIC_PI_BIN, p.cli);
assert.ok(real.mounts({ env }).some(m => m.src === p.pkg));
});
test("pi-unpin S2: with neither, the repository's copy is used and mounted as before", async () => {
for (const PATH of ["/usr/bin:/bin", `${join(REPO, "node_modules", ".bin")}:/usr/bin:/bin`]) {
const env = { ...CREDENTIAL, PATH };
assert.equal(await real.preflight({ env }), null, PATH);
const nm = realpathSync(join(REPO, "node_modules"));
assert.deepEqual(real.mounts({ env }).filter(m => m.dest === join(REPO, "node_modules")).map(m => m.src), [nm]);
assert.ok(real.installRoots({ env }).includes(nm));
const bin = piEnv(env).MOSAIC_PI_BIN;
assert.ok(bin.startsWith(join(REPO, "node_modules") + "/"), bin);
assert.equal(realpathSync(bin), realpathSync(join(REPO, "node_modules", ".bin", "pi")));
}
});
test("pi-unpin S2: a pi that is not in a pi package is refused, and so is a pi that cannot be found at all", async t => {
const dir = join(scratch(t), "bin");
mkdirSync(dir);
writeFileSync(join(dir, "pi"), "#!/bin/sh\necho 1.0.0\n");
chmodSync(join(dir, "pi"), 0o755);
let problem = await real.preflight({ env: { ...CREDENTIAL, PATH: `${dir}:/usr/bin:/bin` } });
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `the pi on PATH isn't part of the ${NAME} package`]);
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(dir, "pi") } });
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't part of the ${NAME} package`]);
noPath(problem, dir);
// The scratch copy has no node_modules of its own: no pi anywhere.
const { root, pi } = await copyRepo(t, "#!/bin/sh\n", "1.0.0");
rmSync(join(root, "node_modules"), { recursive: true });
problem = await pi.preflight({ env: { ...CREDENTIAL, PATH: "/usr/bin:/bin" } });
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci"]);
assert.throws(() => pi.env({ policy: { tools: [] }, tools: [], session, env: { PATH: "/usr/bin:/bin" } }), /no pi found/);
});
// Review finding 8: the package directory is mounted whole, so the binary must be the pi bin its
// package.json names. A stray executable under a directory whose package.json carries the name
// (a wide tree, say) is refused, naming the variable only.
test("pi-unpin S2 review: a binary that isn't the bin its package names is refused", async t => {
const wide = scratch(t);
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0", bin: { pi: "dist/bundle/cli.js" } }));
mkdirSync(join(wide, "tools"));
writeFileSync(join(wide, "tools", "pi"), "#!/bin/sh\necho 1.0.0\n", { mode: 0o755 });
let problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
noPath(problem, wide);
// A package.json with no bin names none.
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0" }));
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
// A bin given as a plain string names it too.
const dir = join(scratch(t), "node_modules");
const cli = standIn(dir, piStandIn({ version: "1.2.3" }), "1.2.3");
writeFileSync(join(dir, NAME, "package.json"), JSON.stringify({ name: NAME, version: "1.2.3", bin: "dist/bundle/cli.js" }));
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: cli } }), "1.2.3");
});
@@ -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,
+56 -8
View File
@@ -6,7 +6,7 @@
//
// read-only /usr, /etc and the host's /bin, /lib, /lib64, /sbin links;
// the repository files the runner loads; the adapter; for pi
// the repository's node_modules, for Claude Code its pinned
// the found package's directory (#1873), for Claude Code its
// binary; node when it lives outside /usr; the broker socket,
// the launch socket for the role that may launch, and the
// host's git socket for a role holding git.push.working or
@@ -41,7 +41,8 @@ import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
export const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
export const BWRAP = "/usr/bin/bwrap";
const KILL_WITH_PARENT = ["/usr/bin/setpriv", "--pdeathsig", "KILL", "--"];
export const SETPRIV = "/usr/bin/setpriv";
const KILL_WITH_PARENT = [SETPRIV, "--pdeathsig", "KILL", "--"];
// What the runner imports, and the three files the harness starts from the
// bundle's settings (claude-gate, mcp-server, pi-extension) with theirs.
@@ -185,14 +186,61 @@ export function gitIdentity({ business, role }) {
return { GIT_AUTHOR_NAME: role, GIT_AUTHOR_EMAIL: email, GIT_COMMITTER_NAME: role, GIT_COMMITTER_EMAIL: email };
}
// The full command line for `command` inside the sandbox. HOME is the run's
// home/, empty at the start; the harness makes what it needs there. The git
// identity is the role's, for the business it runs in.
export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) {
const identity = Object.entries(gitIdentity({ business, role })).flatMap(([k, v]) => ["--setenv", k, v]);
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", "--setenv", "HOME", join(runDir, "home"), "--setenv", "TMPDIR", "/tmp", ...identity, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
// The variables the sandbox sets for a session's runner, over the session env
// the launcher hands it. HOME is the run's home/, empty at the start; the
// harness makes what it needs there. TMPDIR is the private /tmp. The git
// identity is the role's, for the business it runs in. #1873 S3: the pi fit
// check gives its runs these too, so they get the names a session gets.
export function sessionSetenv(runDir, { business, role }) {
return { HOME: join(runDir, "home"), TMPDIR: "/tmp", ...gitIdentity({ business, role }) };
}
// The full command line for `command` inside the sandbox, with sessionSetenv's variables.
export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) {
const setenv = Object.entries(sessionSetenv(runDir, { business, role })).flatMap(([k, v]) => ["--setenv", k, v]);
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", ...setenv, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
}
// #1873 pi-unpin S3: the command line for a start-time check of a harness binary. It has the
// system part every session gets, then `list`, with the network unshared, an empty environment
// but `env`, and `chdir` as the working directory.
//
// #1873 re-check 9: a check dies with the process that runs it, unlike a session. It must not
// outlive a bus that was killed. These steps make sure of that:
// - setpriv gives the first process SIGKILL as its parent-death signal. A shell then checks
// that its parent is still this process ($PPID is read after the signal is set). So either
// the owner dies later and the signal kills the chain, or the owner is already gone and the
// shell exits before anything starts.
// - bwrap's --die-with-parent ties bwrap to the process before it.
// - unshare, when this host lets it (the outer layer below), runs bwrap as pid 1 of a fresh pid
// namespace, and --kill-child kills it when unshare dies. When pid 1 of a namespace dies, the
// kernel kills every process in it. That includes bwrap's own child, wherever it is in its
// setup. bwrap's --die-with-parent can't reach that child: bwrap sets the child's parent-death
// signal only just before its exec, after the child waits for bwrap to write its uid map. If
// bwrap dies before that write, the child waits for good.
// The one step left open with the outer layer is unshare's own: if unshare dies after it forks
// and before its child sets the signal, bwrap still runs. bwrap is alive then, so the run sets up
// and goes on as normal, and it ends when the binary exits. It does not wait for good.
//
// #1873 re-check 10: the outer layer is best effort. unshare may be missing, older than
// util-linux 2.36 (the first with --map-current-user), or refused: on Ubuntu 24.04 with
// kernel.apparmor_restrict_unprivileged_userns=1 only bwrap has a profile that lets it make a
// user namespace, so an unconfined unshare can't write its uid map. The fit check probes it once
// per check (pi-fit.mjs) and passes unshare: null when it can't run, which drops the outer layer
// and nothing else. That is not a weaker sandbox: the outer layer only bounds how long a check's
// processes live. The sandbox a fit run gets is bwrap's, and bwrap's command line is the same
// either way (pi-fit.test.mjs compares the two). Without the outer layer a stuck bwrap child can
// outlive a killed owner; the next check stops it (sweepScratch in pi-fit.mjs).
export const UNSHARE = "/usr/bin/unshare";
const owned = () => [...KILL_WITH_PARENT, "/bin/sh", "-c", '[ "$PPID" = "$0" ] && exec "$@"', String(process.pid)];
const bound = (unshare) => unshare ? [unshare, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"] : [];
export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP, unshare = UNSHARE }) {
const setenv = Object.entries(env).flatMap(([k, v]) => ["--setenv", k, v]);
return [...owned(), ...bound(unshare), bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command];
}
// The outer layer alone, for its probe: the steps before bwrap in checkArgv, then command.
export const outerArgv = (command, { unshare = UNSHARE } = {}) => [...owned(), ...bound(unshare), ...command];
// #1873 triage-fences: what a model route may hand a hosted session, checked before the host
// writes anything and again in hostedBinds. The adapter and each mount's source must be a clean
// absolute path whose real path lies under one of the harness module's install roots, named under
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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" });
+9
View File
@@ -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");
+12 -2
View File
@@ -127,8 +127,8 @@ const CAP = /^[0-9a-f]{64}$/;
// The spawn's environment: these names from the Console's own environment, PATH fixed, and the
// route's proxy variables. bwrap sets HOME and TMPDIR inside.
export const SPAWN_ENV_ALLOW = Object.freeze(["PATH", "LANG", "LC_ALL", "TZ", "TERM"]);
// Passed only as an absolute path: the pinned harness binary (packages/seat/src/session.mjs ENV_ALLOW).
export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN"]);
// Passed only as an absolute path: a harness binary the Console names (packages/seat/src/session.mjs ENV_ALLOW).
export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]);
// The names the runner sets for every harness (runner.mjs adapterEnv); the host's own names never replace them.
const RUNNER_NAMES = new Set(["MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", "MOSAIC_SESSION_DIR", "MOSAIC_POLICY_FILE", "MOSAIC_TOOLS_FILE",
"MOSAIC_TOOL_SOCKET", "MOSAIC_TURN_MARKER", "MOSAIC_PROVIDER", "MOSAIC_MODEL", "MOSAIC_MODEL_ACTIVITY_DIR", "MOSAIC_UNSLOP_LOG",
@@ -826,6 +826,16 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
const start = async ({ admitted, installRoots, transcriptRoot, mod }) => {
ensureDirs();
// #1873 pi-unpin re-check 7: a module whose env() reads a check it runs first (pi's fit check)
// has it run and awaited before the bundle, which calls env(). A refusal or a throw refuses the
// route, as a throw from env() does.
if (typeof mod?.ready === "function") {
try { await mod.ready(); }
catch {
log("triage host: route refused: harness-bundle");
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
}
}
const { run, harnessFiles } = writeBundle(admitted, mod);
// The key this session holds, and anything shaped like a key, never reaches the transcript.
const key = routeKey(admitted.env)?.value ?? null;
+3
View File
@@ -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");
+33 -3
View File
@@ -58,9 +58,13 @@ test("triageEnv keeps the fixed names and the route's proxy variables, and nothi
assert.equal(triageEnv(CONSOLE_ENV, { ANTHROPIC_API_KEY: "from-the-route" }).ANTHROPIC_API_KEY, "from-the-route", "the route's key under the name its harness takes");
assert.equal(triageEnv(CONSOLE_ENV).ANTHROPIC_API_KEY, undefined, "a key in the Console's environment is not the route's");
// #1873 triage-wiring: the pinned harness binary passes as an absolute path, as a bus session's does.
assert.deepEqual(SPAWN_PATH_ALLOW, ["MOSAIC_CLAUDE_BIN"]);
// #1873 pi-unpin S2: and so does the pi binary the Console names, under the same checks.
assert.deepEqual(SPAWN_PATH_ALLOW, ["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]);
assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: "/opt/claude/bin/claude" }).MOSAIC_CLAUDE_BIN, "/opt/claude/bin/claude");
for (const bad of ["claude", "bin/claude", "/opt/a\nb", "/" + "x".repeat(300)]) assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: bad }).MOSAIC_CLAUDE_BIN, undefined, bad);
assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_PI_BIN: "/opt/pi/bin/pi" }).MOSAIC_PI_BIN, "/opt/pi/bin/pi");
for (const name of SPAWN_PATH_ALLOW) {
for (const bad of ["claude", "bin/claude", "/opt/a\nb", "/" + "x".repeat(300)]) assert.equal(triageEnv({ ...CONSOLE_ENV, [name]: bad })[name], undefined, `${name} ${bad}`);
}
});
test("the relay refuses every verb but a message to the person, and any call without the capability", async (t) => {
@@ -275,6 +279,26 @@ test("fence 1: a name the harness module does not declare is refused; a declared
assert.deepEqual(s.harnessEnv, { FAKE_OK: "yes" });
});
// #1873 pi-unpin re-check 8 P3-2: the host awaits a module's ready() (pi's fit check) before the
// bundle. This module's env() works only once its ready() has finished, as pi's reads the check's
// pass; a ready() that rejects refuses the route though its env() would answer.
test("a module's ready() is awaited before the bundle: env() after it works, and a ready() that rejects refuses the route", async (t) => {
const gated = (roots, fail) => async (name) => {
if (name !== "fake") return null;
let done = false;
return {
envNames: DECLARED, installRoots: () => roots, transcript: ({ runDir }) => join(runDir, "session"),
ready: async () => { await new Promise((r) => setTimeout(r, 20)); if (fail) throw new Error("fit refused"); done = true; },
bundle: () => ({ files: {} }),
// The host wants the route's key under its own name, as the key itself, and the proxy URL.
env: ({ proxy }) => { if (!done && !fail) throw new Error("no passed check"); return { ANTHROPIC_AUTH_TOKEN: proxy.key, ANTHROPIC_BASE_URL: proxy.baseUrl }; },
};
};
await admittedRoute(t, "ready first", () => ({}), (root) => ({ harnessModule: gated([join(root, "install")], false) }));
const logs = await refusedRoute(t, "ready rejects", () => ({}), (root) => ({ harnessModule: gated([join(root, "install")], true) }));
assert.ok(logs.includes("triage host: route refused: harness-bundle"), logs.join(" | "));
});
test("fence 1: the bundle holds the copy the host checked; no MOSAIC_ name reaches session.json, even from a getter", async (t) => {
let reads = 0;
const s = await admittedRoute(t, "getter", (root) => () => {
@@ -409,6 +433,8 @@ test("the shipped harness modules declare what their own env() and mounts() hand
for (const name of ["pi", "claude-code"]) {
const mod = await import(new URL(`../../plugins/harness/${name}/index.mjs`, import.meta.url).href);
assert.ok(Array.isArray(mod.envNames) && Object.isFrozen(mod.envNames), name);
// #1873 S3: pi's env() reads the fit check its ready() awaits.
if (typeof mod.ready === "function") await mod.ready();
const keys = Object.keys(mod.env({
files: { settings: "/s", mcp: "/m" }, policy: { tools: ["read"] }, tools: [], session: { model: "m", thinking: "off", provider: "p" },
}));
@@ -438,7 +464,11 @@ test("a host whose HOME holds the repository starts the shipped pi harness's own
// triage-wiring); the module's PI_PROVIDER is not the host's to set.
const own = Object.fromEntries(Object.entries(s.harnessEnv).filter(([k]) => !k.startsWith("MOSAIC_")));
assert.deepEqual(own, { PI_MODEL: "m", PI_OFFLINE: "1" });
assert.deepEqual(Object.keys(s.harnessEnv).filter((k) => k.startsWith("MOSAIC_")).sort(), ["MOSAIC_EXTENSIONS", "MOSAIC_THINKING", "MOSAIC_TOOLS"]);
assert.deepEqual(Object.keys(s.harnessEnv).filter((k) => k.startsWith("MOSAIC_")).sort(), ["MOSAIC_EXTENSIONS", "MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "MOSAIC_THINKING", "MOSAIC_TOOLS"]);
// #1873 pi-unpin S3: the repository's pi has no --no-mcp, so the module sets it to "0".
assert.equal(s.harnessEnv.MOSAIC_PI_NO_MCP, "0");
// #1873 pi-unpin S2: the session runs the pi the module found, here the repository's copy.
assert.ok(s.harnessEnv.MOSAIC_PI_BIN.startsWith(join(REPO, "node_modules") + "/"), s.harnessEnv.MOSAIC_PI_BIN);
// The same host and module, with HOME's own directory (which holds the repository) as an extra
// install root and mount: it is under HOME and not in the code, so it is refused.
const wide = { ...pi, installRoots: (o) => [...pi.installRoots(o), home] };