fix(pi): pi-unpin S3 re-check 3: the control passes --no-tools and --no-themes, one sealed run per loader flag leaves only that flag out and its kind must show, every run gets a fresh scratch directory, and the README says what argv the check runs (#1873)

This commit is contained in:
jason.woltje committed 2026-11-20 01:17:33 -06:00
1 parent d009ccaec4
commit fa5ce3ae65
4 files changed
+182 -87

No files matched your search

+18 -8
View File
@@ -42,14 +42,24 @@ project's `.pi/SYSTEM.md` and `.pi/APPEND_SYSTEM.md`. The project has a
saved trust, so only `--no-approve` keeps those two out. An `-e`
extension writes the system prompt it sees at `session_start`; the
sealed runs must show the `--system-prompt` file and none of the planted
files. A control run differs from the sealed runs only by the seal
flags (it keeps `--no-session` and `--offline`, and passes the same `-e`
and `--system-prompt`). It must show each kind of source (an MCP server
only when `--help` lists `--no-mcp`) and answer `get_state` and
`get_commands`, or the check can't tell and refuses. For every kind but
the trusted project files, only a source outside the project counts
there, since a project source can wait on trust and `--no-approve`
would then hide a seal flag the binary ignores. A run killed for taking
files. A control run passes the seal flags less every loader flag
(`--no-approve`, `--no-extensions`, `--no-skills`,
`--no-prompt-templates`, `--no-context-files`, and `--no-mcp` when
listed), so it keeps `--offline`, `--no-themes`, `--no-tools` and
`--no-session`, and the same `-e` and `--system-prompt`. It must show
each kind of source (an MCP server only when `--help` lists `--no-mcp`)
and answer `get_state` and `get_commands`, or the check can't tell and
refuses. For every kind but the trusted project files, only a source
outside the project counts there, since a project source can wait on
trust and `--no-approve` would then hide a seal flag the binary ignores.
Then one sealed rpc run per loader flag leaves out only that flag, and
its kind must show. So each loader flag is shown to keep its own kind
out, and no other seal flag, nor their order, can stand in for it. Each
run gets a fresh scratch directory, so no run sees what another left.
The fit runs do not pass the flags production adds (`--provider`,
`--model`, `--mode json`, `--session-dir`); a pi built to act one way
without them and another way with them is outside what a start-time
check can catch. A run killed for taking
over 20 s or printing over 4 MB can't be checked and says which. A
`--version` that fails or hangs past 3 s only leaves no label. Pi
0.85.1 appends the agent directory's `APPEND_SYSTEM.md` whatever the
+72 -51
View File
@@ -6,8 +6,10 @@
// --version a label for the refusal line and the record; not a gate
// --help lists, in its option column, every flag Mosaic passes, with rpc mode and each
// thinking level
// control rpc, planted ambient sources, a saved trust for the project, no seal flags but
// --no-session and --offline, the --system-prompt file the sealed runs pass, and
// control rpc, planted ambient sources, a saved trust for the project, the seal flags less
// every loader flag (the ones that each keep one kind of source out: --no-approve,
// --no-extensions, --no-skills, --no-prompt-templates, --no-context-files, and
// --no-mcp when --help lists it), the --system-prompt file the sealed runs pass, and
// one -e extension that writes the system prompt it sees at session_start. The
// binary answers get_state and get_commands, and shows each kind of planted source:
// an extension, a skill, a prompt template, a context file, a trusted project file
@@ -18,6 +20,9 @@
// sealed rpc the adapter's seal flags (and --no-mcp when --help lists it), the -e extension and
// --system-prompt. The binary answers both, loads that extension (a session's gate
// comes in that way), uses the --system-prompt file, and shows no planted source.
// one out one more sealed rpc run per loader flag, with every seal flag but that one. The
// kind it keeps out must show. So each loader flag is shown to keep its own kind out,
// and no other seal flag, nor the order they come in, can stand in for it.
// sealed -p the same in -p, the mode the adapter runs. Its -e extension must reach
// session_start, and no planted extension, MCP server, context file or trusted
// project file may show. -p answers no get_commands, so skills and prompt templates
@@ -25,9 +30,13 @@
// missing -e a missing -e file, in rpc and in -p, stops the start: exit non-zero, no answer,
// and no session_start for the -e extension given after it
//
// Every run gets a fresh scratch directory with the sources planted again, so no run sees what
// another one left. Each run passes the same -e and --system-prompt; runs differ only by their seal
// flags, their mode, the -p message and the missing -e file.
//
// A check that cannot tell refuses: a sandbox that fails, a run that times out or prints more
// than 4 MB, an answer that is missing or doesn't parse, a kind of source the control doesn't
// show. A --version that fails or hangs past 3 s only leaves no label. A pass is cached in memory
// show, or the kind a one-out run should show. A --version that fails or hangs past 3 s only leaves no label. A pass is cached in memory
// by a digest of the files that run (the package's package.json, every file under dist/bundle/,
// every file in the directory tree that holds the binary's real file, and, for a copy mounted
// with the node_modules it sits in, npm's lock files there) and the mount layout.
@@ -35,7 +44,9 @@
// repository's pi), never a host path.
//
// Known limit: pi 0.85.1 appends the agent directory's APPEND_SYSTEM.md whatever the flags. The
// check doesn't plant one; a session's agent directory is the run's own.
// check doesn't plant one; a session's agent directory is the run's own. The runs don't pass the
// flags a session adds (--provider, --model, --mode json, --session-dir), so a pi built to act
// differently with them is outside what this check can catch.
import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
@@ -47,6 +58,8 @@ import { BWRAP, checkArgv, sandboxProblem } from "../../seat/src/sandbox.mjs";
export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", "--session-dir", "--fork", "-c", "--no-extensions", "-e", "--no-skills", "--skill", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-approve", "--offline", "--tools", "--no-tools", "--provider", "--model", "--thinking", "--system-prompt", "-p"]);
// The adapter's seal flags, as it passes them in an ephemeral session with no tools.
const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-tools", "--no-session"];
// The loader flags: each keeps one kind of planted source out. --no-mcp counts when --help lists it.
const LOADERS = [["--no-approve", "trusted project file"], ["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]];
const RUN_TIMEOUT = 20000;
// --version gives only a label, so a hang costs this much and leaves no label.
const VERSION_TIMEOUT = 3000;
@@ -70,21 +83,29 @@ export function piFit(found, { levels = [], bwrap = BWRAP, node = process.execPa
// The sandbox's own error text can name a host path, so the line keeps only what failed.
const problem = sandboxProblem({ bwrap });
if (problem) return { ok: false, message: `${who(null)} can't be checked: ${problem.startsWith("the sandbox (bwrap) is missing") ? "the sandbox (bwrap) is missing" : "the sandbox (bwrap) failed"}` };
const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-"));
try {
const result = check(found, { levels, bwrap, node, scratch, who, digest });
if (result.ok) passed.set(key, result);
return result;
} finally {
rmSync(scratch, { recursive: true, force: true });
}
const result = check(found, { levels, bwrap, node, who, digest });
if (result.ok) passed.set(key, result);
return result;
}
function check(found, { levels, bwrap, node, scratch, who, digest }) {
const run = (args, input = "", timeout = RUN_TIMEOUT) => runIn(found, { bwrap, node, scratch, args, input, timeout });
function check(found, { levels, bwrap, node, who, digest }) {
// args: the arguments, or a function of the run's scratch directory that returns them. The
// answer is read before the scratch directory goes: the rpc answers, whether the -e extension
// loaded, the system prompt it saw, and the planted sources that showed (commands: false leaves
// get_commands out, for -p).
const run = (args, input = "", { timeout = RUN_TIMEOUT, commands = true } = {}) => {
const scratch = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-"));
try {
plant(scratch);
const r = runIn(found, { bwrap, node, scratch, args: typeof args === "function" ? args(scratch) : args, input, timeout });
const answer = answered(r.stdout);
return { ...r, answer, explicit: marked(scratch, EXPLICIT), prompt: prompted(scratch), seen: seen(scratch, commands ? answer : { commands: [] }) };
} finally {
rmSync(scratch, { recursive: true, force: true });
}
};
const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` });
plant(scratch);
const v = run(["--version"], "", VERSION_TIMEOUT);
const v = run(["--version"], "", { timeout: VERSION_TIMEOUT });
const version = v.status === 0 ? versionLabel(v.stdout) : null;
const help = run(["--help"]);
if (help.status !== 0) return refuse("can't be checked: --help failed");
@@ -98,57 +119,58 @@ function check(found, { levels, bwrap, node, scratch, who, digest }) {
const noMcp = has("--no-mcp");
const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n';
// Every run passes the same -e and --system-prompt, so the run without the seal flags and the
// sealed runs differ only by the seal flags. --no-session and --offline are seal flags the
// control keeps: it must not save a session or reach a provider either.
const explicit = ["-e", join(scratch, "explicit", "fit.ts")];
const given = ["--system-prompt", join(scratch, "prompt.md")];
// Every run passes the same -e and --system-prompt, so the runs differ only by the seal flags.
const given = (scratch) => ["-e", join(scratch, "explicit", "fit.ts"), "--system-prompt", join(scratch, "prompt.md")];
const seal = [...SEAL, ...(noMcp ? ["--no-mcp"] : [])];
const sealed = [...seal, ...explicit, ...given];
// -p takes a message; offline with no key, pi stops before any model call.
const print = ["-p", ...sealed, "fit check"];
const loaders = [...LOADERS, ...(noMcp ? [["--no-mcp", "MCP server"]] : [])];
const without = (...flags) => seal.filter((f) => !flags.includes(f));
const rpcRun = (flags) => run((s) => ["--mode", "rpc", ...flags, ...given(s)], ask);
plant(scratch);
const control = run(["--mode", "rpc", "--no-session", "--offline", ...explicit, ...given], ask);
// The control keeps the seal flags no planted source tells apart (--offline, --no-themes,
// --no-tools, --no-session), so one of those can't stand in for a loader flag either.
const control = rpcRun(without(...loaders.map(([f]) => f)));
if (control.stopped) return refuse(`can't be checked: the start without the seal flags ${control.stopped}`);
const controlAnswer = answered(control.stdout);
if (!controlAnswer.state) return refuse("can't be checked: it didn't answer get_state");
if (!controlAnswer.commands) return refuse("can't be checked: it didn't answer get_commands");
if (!marked(scratch, EXPLICIT) || prompted(scratch) === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags");
if (!control.answer.state) return refuse("can't be checked: it didn't answer get_state");
if (!control.answer.commands) return refuse("can't be checked: it didn't answer get_commands");
if (!control.explicit || control.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags");
// A kind counts only through a source outside the project, except the trusted project files
// themselves: a project source can wait on project trust, and then --no-approve alone would keep
// it out of the sealed runs and hide a seal flag of its kind that the binary ignores.
const shown = new Set(seen(scratch, controlAnswer).filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind));
const shown = new Set(control.seen.filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind));
const unshown = KINDS.filter((k) => (k !== "MCP server" || noMcp) && !shown.has(k));
if (unshown.length) return refuse(`can't be checked: it didn't load a planted ${unshown.join(", ")} without the seal flags`);
plant(scratch);
const rpc = run(["--mode", "rpc", ...sealed], ask);
const rpc = rpcRun(seal);
if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`);
const rpcAnswer = answered(rpc.stdout);
if (!rpcAnswer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on");
if (!rpcAnswer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on");
if (!marked(scratch, EXPLICIT)) return refuse("didn't load an -e extension with the seal flags on");
const rpcPrompt = prompted(scratch);
if (rpcPrompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on");
if (!rpcPrompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on");
let leaked = seen(scratch, rpcAnswer);
if (leaked.length) return refuse(`loaded a planted ${leaked.map((s) => s.source).join(", ")} with the seal flags on`);
if (!rpc.answer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on");
if (!rpc.answer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on");
if (!rpc.explicit) return refuse("didn't load an -e extension with the seal flags on");
if (rpc.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on");
if (!rpc.prompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on");
if (rpc.seen.length) return refuse(`loaded a planted ${rpc.seen.map((s) => s.source).join(", ")} with the seal flags on`);
plant(scratch);
const p = run(print);
// Each loader flag on its own: with every other seal flag on, its kind must show.
for (const [flag, kind] of loaders) {
const r = rpcRun(without(flag));
const but = `with every seal flag but ${flag}`;
if (r.stopped) return refuse(`can't be checked: the start ${but} ${r.stopped}`);
if (!r.answer.state || !r.answer.commands) return refuse(`can't be checked: it didn't answer get_state and get_commands ${but}`);
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${but}`);
if (!r.seen.some((s) => s.kind === kind)) return refuse(`can't be checked: it didn't load a planted ${kind} ${but}`);
}
// -p takes a message; offline with no key, pi stops before any model call.
const p = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { commands: false });
if (p.stopped) return refuse(`can't be checked: a -p start with the seal flags ${p.stopped}`);
if (prompted(scratch) === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension");
leaked = seen(scratch, { commands: [] });
if (leaked.length) return refuse(`loaded a planted ${leaked.map((s) => s.source).join(", ")} with the seal flags on in -p`);
if (p.prompt === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension");
if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((s) => s.source).join(", ")} with the seal flags on in -p`);
// A missing -e file stops the start. pi 0.85.1 loads the -e files in order and still loads the
// one after the missing one, then exits 1 before session_start, so only session_start tells.
for (const [mode, args, input] of [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]]) {
plant(scratch);
const r = run([...args, ...seal, "-e", join(scratch, "missing", "extension.ts"), ...explicit, ...given, ...(mode === "-p" ? ["fit check"] : [])], input);
const r = run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input);
if (r.stopped) return refuse(`can't be checked: a ${mode} start with a missing -e extension ${r.stopped}`);
if (r.status === 0 || answered(r.stdout).state || prompted(scratch) !== null) return refuse("starts with a missing -e extension");
if (r.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension");
}
return { ok: true, version, noMcp, digest };
}
@@ -248,7 +270,6 @@ const marked = (scratch, source) => { try { return readdirSync(join(scratch, "ma
const prompted = (scratch) => { try { return readFileSync(join(scratch, "marks", "system-prompt"), "utf8"); } catch { return null; } };
function plant(scratch) {
for (const d of ["home", "agent", "project", "marks", "tmp", "missing", "explicit", "listed"]) rmSync(join(scratch, d), { recursive: true, force: true });
const marks = join(scratch, "marks");
const put = (path, text) => { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, text); };
const ext = (source) => `import { writeFileSync } from "node:fs";\nexport default function (): void { writeFileSync(${JSON.stringify(join(marks, markerName(source)))}, ""); }\n`;
+64 -6
View File
@@ -13,7 +13,7 @@ import { piFit } from "../src/pi-fit.mjs";
import { piStandIn } from "./pi-stand-in.mjs";
import { locate } from "../../plugins/harness/pi/binary.mjs";
import * as pi from "../../plugins/harness/pi/index.mjs";
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
import { BWRAP, sandboxProblem } from "../../seat/src/sandbox.mjs";
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
const NAME = "@earendil-works/pi-coding-agent";
@@ -122,11 +122,69 @@ test("pi-unpin S3 re-check 2: the run without the seal flags passes --system-pro
assert.equal(fit(standIn(t, { sysReplacesAll: true, sealLeak: ["approve", "context"] }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted context file, trusted project file without the seal flags");
});
test("pi-unpin S3 re-check 2: the run without the seal flags and the sealed runs differ only by the seal flags", SKIP, t => {
const plain = fit(standIn(t, { sameRuns: true }).env);
assert.equal(plain.ok, true, plain.message);
const withMcp = fit(standIn(t, { sameRuns: true, extraFlags: ["--no-mcp"] }).env);
assert.equal(withMcp.ok, true, withMcp.message);
// The adapter's seal flags and the loader flags among them, as pi-fit.mjs has them.
const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-tools", "--no-session"];
const LOADERS = ["--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-context-files"];
// Re-check 2's rule, re-check 3 item 3: the runs differ only by their seal flags, their mode, the
// -p message and the missing -e file, and each runs in its own scratch directory. A bwrap that
// logs its argv shows every run the check makes.
test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the runs differ only by their seal flags", SKIP, t => {
for (const extraFlags of [[], ["--no-mcp"]]) {
const s = standIn(t, { extraFlags });
const log = join(s.root, "bwrap.log");
const bwrap = join(s.root, "bwrap");
writeFileSync(bwrap, `#!/bin/sh\nfor a in "$@"; do printf '%s\\037' "$a"; done >> '${log}'\nprintf '\\n' >> '${log}'\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 });
const found = locate(s.env);
const r = piFit(found, { levels: LEVELS, bwrap });
assert.equal(r.ok, true, r.message);
const runs = readFileSync(log, "utf8").split("\n").filter(Boolean).map((l) => l.split("\x1f").slice(0, -1)).filter((a) => a.includes(found.view)).map((a) => {
const scratch = a[a.indexOf("--chdir") + 1].replace(/\/project$/, "");
return { scratch, args: a.slice(a.indexOf(found.view) + 1).map((x) => x.split(scratch).join("<s>")) };
});
const seal = [...SEAL, ...extraFlags];
const loaders = [...LOADERS, ...extraFlags];
// --version, --help, the control, the sealed rpc, one per loader flag, -p, two missing -e.
assert.equal(runs.length, 2 + 2 + loaders.length + 1 + 2);
assert.equal(new Set(runs.map((x) => x.scratch)).size, runs.length, "each run has its own scratch directory");
const flagLists = [];
for (const { args } of runs.slice(2)) {
const flags = args.filter((a) => seal.includes(a));
assert.deepEqual(flags, seal.filter((f) => flags.includes(f)), "the seal flags keep their order");
const rest = [];
for (let i = 0; i < args.length; i++) {
if (args[i] === "--mode") { i++; continue; }
if (args[i] === "-e" && args[i + 1] === "<s>/missing/extension.ts") { i++; continue; }
if (args[i] === "-p" || seal.includes(args[i]) || (args.includes("-p") && i === args.length - 1)) continue;
rest.push(args[i]);
}
assert.deepEqual(rest, ["-e", "<s>/explicit/fit.ts", "--system-prompt", "<s>/prompt.md"], args.join(" "));
if (args[0] === "--mode" && !args.includes("<s>/missing/extension.ts")) flagLists.push(flags.join(" "));
}
assert.deepEqual(flagLists, [seal.filter((f) => !loaders.includes(f)), seal, ...loaders.map((l) => seal.filter((f) => f !== l))].map((f) => f.join(" ")));
}
});
test("pi-unpin S3 re-check 3: --no-tools or --no-themes can't stand in for the loader flags, since the control passes them too", SKIP, t => {
for (const cue of ["--no-tools", "--no-themes", "order"]) {
assert.equal(fit(standIn(t, { hideWhen: cue }).env).message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted extension, skill, prompt template, context file, trusted project file without the seal flags", cue);
}
});
test("pi-unpin S3 re-check 3: each loader flag is shown on its own, so no other seal flag can stand in for it", SKIP, t => {
const but = (kind, flag) => `pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: it didn't load a planted ${kind} with every seal flag but ${flag}`;
assert.equal(fit(standIn(t, { standIn: ["--no-approve", "--no-extensions"] }).env).message, but("trusted project file", "--no-approve"));
for (const [flag, kind] of [["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]]) {
assert.equal(fit(standIn(t, { standIn: [flag, "--no-approve"] }).env).message, but(kind, flag), flag);
}
assert.equal(fit(standIn(t, { extraFlags: ["--no-mcp"], standIn: ["--no-mcp", "--no-approve"] }).env).message, but("MCP server", "--no-mcp"));
// Re-check 3's dispatchers: a pi that hides everything whenever one always-passed loader flag is on.
assert.equal(fit(standIn(t, { hideWhen: "--no-approve" }).env).message, but("extension", "--no-extensions"));
assert.equal(fit(standIn(t, { hideWhen: "--no-extensions" }).env).message, but("trusted project file", "--no-approve"));
});
test("pi-unpin S3 re-check 3: a pi that remembers the control in its scratch directory and hides later is refused", SKIP, t => {
assert.match(fit(standIn(t, { remember: true }).env).message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted user extension, .* with the seal flags on$/);
});
test("pi-unpin S3 re-check 2: a kind shown only through a source that waits on project trust is can't tell, so --no-approve can't stand in for another seal flag", SKIP, t => {
+28 -22
View File
@@ -18,7 +18,8 @@
// (rpc), or prints `sessionOut` and exits 0 (-p).
// An -e file that exists loads even with --no-extensions, unless `dropExplicit`
// (then it loads only without --no-extensions). `printNoStart`: -p runs no
// session_start.
// session_start. MCP servers stay out with --no-mcp when `extraFlags` lists it, else
// with --no-extensions.
// `ignoreAll` loads no planted source. `sealLeak` lists kinds (extensions, mcp,
// skills, prompts, context, approve) loaded even when their seal flag is on.
// `ignoreSystemPrompt` drops the --system-prompt file. `ignoreAppend` drops every
@@ -27,9 +28,13 @@
// project's .pi/extensions, loaded in a trusted project whatever --no-extensions
// says. `trustCtxOnly`: the only context file is the project's AGENTS.md, loaded the
// same way whatever --no-context-files says. `flood`: prints 8 MB first.
// `sameRuns`: each run strips its seal flags (--offline and every --no-*), its mode
// and its -p message, and exits 3 when that differs from the first run's. `commands` "fail" answers
// get_commands with success false; "list" answers with a bare list.
// `hideWhen`: a cue, a flag or "order" (--offline before --no-session). With the cue
// it loads no planted source; without it, every kind even with its seal flag on.
// `standIn`: [flag, other]: it ignores flag, and other keeps flag's kind out instead.
// `remember`: the first run in a scratch directory (it leaves a marker beside the
// -e file's directory) loads every kind whatever the seal flags; a later run loads
// none. `commands` "fail" answers get_commands with success false; "list" answers
// with a bare list.
// otherwise a session with no $ARGV_OUT: prints `sessionOut` and exits 0.
//
// Trust, as pi 0.85.1 has it: the project is trusted with --approve, or with a saved trust and
@@ -74,21 +79,22 @@ if (rpc || print) {
try { off += fs.writeSync(1, chunk, off); } catch (e) { if (e.code !== "EAGAIN") throw e; }
}
}
if (K.sameRuns) {
const rest = [];
for (let i = 0; i < argv.length; i++) {
if (argv[i] === "--mode") { i++; continue; }
if (argv[i] === "-p" || argv[i] === "--offline" || argv[i].startsWith("--no-")) continue;
if (print && i === argv.length - 1) continue;
rest.push(argv[i]);
}
const log = path.join(path.dirname(path.dirname(val("-e"))), "runs.log");
const first = fs.existsSync(log) ? fs.readFileSync(log, "utf8") : null;
if (first === null) fs.writeFileSync(log, JSON.stringify(rest));
else if (first !== JSON.stringify(rest)) process.exit(3);
const ALL = ["extensions", "mcp", "skills", "prompts", "context", "approve"];
let ignoreAll = K.ignoreAll;
let leak = new Set(K.sealLeak || []);
if (K.hideWhen) {
const cued = K.hideWhen === "order" ? argv.indexOf("--offline") !== -1 && argv.indexOf("--offline") < argv.indexOf("--no-session") : has(K.hideWhen);
if (cued) ignoreAll = true;
else leak = new Set(ALL);
}
const leak = new Set(K.sealLeak || []);
const on = (kind, flag) => !K.ignoreAll && (!has(flag) || leak.has(kind));
if (K.remember) {
const marker = path.join(path.dirname(path.dirname(val("-e"))), "seen-before");
if (fs.existsSync(marker)) ignoreAll = true;
else { fs.writeFileSync(marker, ""); leak = new Set(ALL); }
}
// Whether a seal flag is on: with standIn, its flag reads as the other flag.
const sealed = (flag) => has(K.standIn && K.standIn[0] === flag ? K.standIn[1] : flag);
const on = (kind, flag) => !ignoreAll && (!sealed(flag) || leak.has(kind));
const agent = process.env.PI_CODING_AGENT_DIR || path.join(process.env.HOME || "/", ".pi", "agent");
const cwd = process.cwd();
const list = (dir, test) => { try { return fs.readdirSync(dir).filter(test).map((n) => path.join(dir, n)); } catch { return []; } };
@@ -97,7 +103,7 @@ if (rpc || print) {
const settings = [json(path.join(agent, "settings.json")), json(path.join(cwd, ".pi", "settings.json"))].filter(Boolean);
const trust = json(path.join(agent, "trust.json")) || {};
const saved = settings.some((s) => s.defaultProjectTrust === "always") || trust[cwd] === true;
const trusted = !K.ignoreAll && (has("--approve") || (saved && (!has("--no-approve") || leak.has("approve"))));
const trusted = !ignoreAll && (has("--approve") || (saved && (!sealed("--no-approve") || leak.has("approve"))));
const commands = [];
// A planted extension writes one marker on load; an -e extension may also write the system
// prompt at session_start. Do what each would do.
@@ -120,7 +126,7 @@ if (rpc || print) {
for (const p of packages) files.push(...list(path.join(p, "extensions"), (n) => n.endsWith(".ts")));
for (const f of files) load(f);
}
if (on("mcp", "--no-extensions") && (!has("--no-mcp") || leak.has("mcp"))) {
if (on("mcp", (K.extraFlags || []).includes("--no-mcp") ? "--no-mcp" : "--no-extensions")) {
for (const f of [path.join(agent, "mcp.json"), path.join(cwd, ".pi", "mcp.json"), path.join(cwd, ".mcp.json")]) {
for (const s of Object.values(json(f)?.mcpServers || {})) cp.spawnSync(s.command, s.args || [], { stdio: "ignore" });
}
@@ -139,12 +145,12 @@ if (rpc || print) {
// The system prompt: the --system-prompt file, else SYSTEM.md; then APPEND_SYSTEM.md; then the
// context files, from the agent directory and from / down to the working directory.
const given = K.ignoreSystemPrompt ? null : val("--system-prompt");
const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!K.ignoreAll && read(path.join(agent, name))) || "";
const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!ignoreAll && read(path.join(agent, name))) || "";
const hide = K.sysReplacesAll && given;
const parts = [given ? (read(given) || "") : own("SYSTEM.md"), K.ignoreAppend || hide ? "" : own("APPEND_SYSTEM.md")];
if (K.trustCtxOnly) {
if (trusted) parts.push(read(path.join(cwd, "AGENTS.md")) || "");
} else if (!hide && !K.ignoreAll && (!has("--no-context-files") || leak.has("context"))) {
} else if (!hide && on("context", "--no-context-files")) {
const dirs = [];
for (let d = cwd; ; d = path.dirname(d)) { dirs.unshift(d); if (path.dirname(d) === d) break; }
parts.push(read(path.join(agent, "AGENTS.md")) || "");