fix(conversation,discord): pi-unpin S4 review: the controller refuses engine-feature-missing for a pi whose first line doesn't run node (#!/usr/bin/env node, or an absolute path to a node), since the fit check runs the bin as a program and the launch runs its real file with node (F1); the lookup's answer must still hold together at each check, its bin and view resolving to the same file inside the package found, or it refuses unsealed-engine, so a package link moved since the lookup or a hand-built answer with another bin can't launch (F2); the Discord test helpers plant a pi package and name it in MOSAIC_PI_BIN, so no CLI test reads the host's pi (F3); the READMEs say what --repo and the old-claim line mean now (F4, F5); N24b proves the default lookup reads the controller's environment (F6); the stand-in pi gains a node form for the conversation fixtures; tests S4f, S4g and the S4e repeat (#1873, lane pi-unpin-s4)

This commit is contained in:
jason.woltje committed 2026-11-20 05:12:49 -06:00
1 parent 824b1213c1
commit 28a5d38349
10 files changed
+195 -26

No files matched your search

+13 -2
View File
@@ -314,7 +314,18 @@ can disagree with it.
whose pins differ refuses `engine-pin-mismatch`, so changed bytes under a
stopped claim can't resume it. A claim made before #1873 holds npm's
integrity in `enginePin`; it refuses `engine-pin-mismatch` with "the claim
was made under the old pin record; start a new claim".
was made under the old pin record; start a new claim". Like any pin
mismatch, that refusal holds at every start of the session, since a resume
is checked against the session's last proven stop. A new session starts a
new claim.
- **Same file, same interpreter.** The fit check runs the bin as a program,
so its first line picks what runs it, while the launch runs the bin's real
file with this Node. A pi whose first line isn't `#!/usr/bin/env node` or
`#!` and an absolute path to a `node` refuses `engine-feature-missing`.
The lookup's answer must also still hold together at each check: its bin
and the view the fit check runs resolve to the same file, inside the package
the lookup found. A package link moved since the lookup, or an answer built
by hand with another bin, refuses `unsealed-engine`.
- **Engine environment.** The engine gets `ENGINE_ENV` (`pi-seal.mjs`):
`PATH`, `HOME`, `USER`, `LOGNAME`, `SHELL`, `LANG`, `LC_ALL`,
`LC_CTYPE`, `TZ`, `TERM`, `TMPDIR`, `PI_CODING_AGENT_DIR`, `PI_OFFLINE`,
@@ -611,7 +622,7 @@ no prompt:
| Suite | Covers |
|---|---|
| `claim.test.mjs` | W1–W17, W20, G1–G3, S4a–S4e: the writer claim, crash barriers, the guard, the engine fit, pins and seal |
| `claim.test.mjs` | W1–W17, W20, G1–G3, S4a–S4g: the writer claim, crash barriers, the guard, the engine fit, pins and seal, the node-script check and the lookup answer check |
| `races.test.mjs` | H1–H4, H9–H23: takeover, Interrupt and force stop, retries, incarnations |
| `turns.test.mjs` | N1–N25, N24b, N24c: the turn tracker against the fake engine's Pi behaviors; the engine seal, `--no-mcp` and environment |
| `cohort.test.mjs` | K1–K19, R1–R3, R5–R13, E1–E6: scopes, force stop, proofs, recovery, eligibility, the scope's environment, scope release and its retry after a crash, the engine-exit stop (needs a systemd user manager) |
+46 -5
View File
@@ -16,7 +16,7 @@
// as in CHAT-01. With no verifier nothing verifies, and every stop ends
// `uncertain`.
import { lstatSync, mkdirSync, readFileSync, realpathSync, unlinkSync } from "node:fs";
import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, realpathSync, unlinkSync } from "node:fs";
import { createServer } from "node:net";
import { basename, dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
@@ -155,6 +155,10 @@ export function sessionTools(parsed) {
return tools;
}
// A first line that runs node: #!/usr/bin/env node, or #! and an absolute path
// whose last part is node, with nothing after it.
const NODE_SHEBANG = /^#![ \t]*(?:\/usr\/bin\/env[ \t]+node|\/[^\s]*\/node)[ \t]*\r?$/;
export class Controller {
constructor(opts = {}) {
const {
@@ -265,17 +269,54 @@ export class Controller {
// The real file of the pi's bin, read now. No answer from the lookup, or a
// bin that no longer resolves, refuses: the lookup's own line names the
// step, never a path.
// step, never a path. The answer must still hold together: the bin and the
// view the fit check runs resolve to the same file, inside the package the
// lookup found. A stale answer (a link moved since the lookup) or one built
// by hand with another bin refuses unsealed-engine.
#piFile() {
const pi = this.pi;
if (!pi || typeof pi !== "object" || typeof pi.bin !== "string" || !pi.bin) {
const why = typeof pi?.problem === "string" && pi.problem ? pi.problem : "the pi lookup gave no binary";
throw new ControlRefusal(ENGINE_FEATURE_MISSING, why);
}
const label = typeof pi.label === "string" && pi.label ? pi.label : "the pi";
let real;
try {
return realpathSync(pi.bin);
real = realpathSync(pi.bin);
} catch {
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${pi.label ?? "the pi"} no longer resolves to a file`);
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label} no longer resolves to a file`);
}
let view = null, pkg = null;
try {
view = typeof pi.view === "string" ? realpathSync(pi.view) : null;
pkg = typeof pi.pkg === "string" ? realpathSync(pi.pkg) : null;
} catch {
view = null;
}
if (view !== real || pkg === null || !real.startsWith(pkg + "/")) {
throw new ControlRefusal(UNSEALED_ENGINE, `${label} no longer matches the lookup's answer; look the pi up again`);
}
return real;
}
// #1873 S4: the launch is this Node running the bin's real file, while the
// fit check runs the bin as a program, so its first line picks what runs it.
// Only a file whose first line runs node (#!/usr/bin/env node, or an
// absolute path to a node) runs the same way in both.
#nodeScript(file) {
let head = "";
let fd = null;
try {
fd = openSync(file, "r");
const buf = Buffer.alloc(256);
head = buf.subarray(0, readSync(fd, buf, 0, buf.length, 0)).toString("latin1");
} catch {
head = "";
} finally {
if (fd !== null) try { closeSync(fd); } catch {}
}
if (!NODE_SHEBANG.test(head.split("\n")[0])) {
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${this.pi.label ?? "the pi"} isn't a node script, and the engine runs pi with node`);
}
}
@@ -297,7 +338,7 @@ export class Controller {
// engineVersion is the --version text (null when pi printed none) and
// enginePin the digest. --no-mcp joins the seal when the help lists it.
async #pins() {
this.#piFile();
this.#nodeScript(this.#piFile());
const fit = await piFit(this.pi, { levels: levels() });
if (!fit?.ok) throw new ControlRefusal(ENGINE_FEATURE_MISSING, typeof fit?.message === "string" ? fit.message : "the pi could not be checked");
if (typeof fit.digest !== "string" || !/^[0-9a-f]{64}$/.test(fit.digest)) throw new ControlRefusal(ENGINE_FEATURE_MISSING, "the fit check gave no digest");
+83 -7
View File
@@ -648,7 +648,7 @@ test("S4c: changed bytes in the pi between launch and resume refuse engine-pin-m
const fx = fixture();
const pi = standInPi(join(fx.base, "pi"));
const first = await provenStop(fx, { pi: pi.found });
appendFileSync(pi.cli, "# one more line\n");
appendFileSync(pi.cli, "// one more line\n");
const k = keysFor(fx, { pi: pi.found });
const before = [revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)];
const { ctrl, launcher } = controllerFor(fx, { pi: piAt(pi.bin) });
@@ -700,7 +700,7 @@ test("S4d: a pre-argument that is not the resolved bin's real file refuses unsea
}
});
test("S4e: a claim made under the old pin record (npm's integrity in enginePin) refuses engine-pin-mismatch once with the old-record line", async () => {
test("S4e: a claim made under the old pin record (npm's integrity in enginePin) refuses engine-pin-mismatch with the old-record line at every start of that session", async () => {
const fx = fixture();
const first = await provenStop(fx);
// Rewrite the stopped heads as a claim made before #1873 would hold them.
@@ -710,18 +710,94 @@ test("S4e: a claim made under the old pin record (npm's integrity in enginePin)
unlinkSync(path);
writeFileSync(path, JSON.stringify(old), { mode: 0o400 });
}
const { ctrl, launcher } = controllerFor(fx);
// Like any pin mismatch, the refusal holds at each start: the session's last
// proven stop is what a resume is checked against.
for (let i = 0; i < 2; i++) {
const { ctrl, launcher } = controllerFor(fx);
try {
await assert.rejects(ctrl.start(), (e) => {
assert.equal(e.code, ENGINE_PIN_MISMATCH);
assert.equal(e.message, OLD_PIN_LINE);
assert.match(e.message, /old pin record; start a new claim/);
return true;
});
assert.equal(launcher.launches.length, 0);
} finally {
await ctrl.close().catch(() => {});
}
}
});
test("S4f: a pi whose first line doesn't run node is refused engine-feature-missing, though the fit check passes it; #!/usr/bin/env node launches", async () => {
const fx = fixture();
// The sh form of the stand-in passes the fit check, which runs the bin as a
// program, but the engine runs the bin's real file with node.
const sh = standInPi(join(fx.base, "pi-sh"), { form: "sh" });
const fit = await piFit(sh.found, { levels: levels() });
assert.equal(fit.ok, true, fit.message);
const k = keysFor(fx, { pi: sh.found });
{
const { ctrl, launcher } = controllerFor(fx, { pi: sh.found });
try {
await assert.rejects(ctrl.start(), (e) => {
assert.equal(e.code, ENGINE_FEATURE_MISSING);
assert.equal(e.message, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
return true;
});
assert.equal(launcher.launches.length, 0);
assert.deepEqual([revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)], [[], []]);
} finally {
await ctrl.close().catch(() => {});
}
}
// The node form with the shebang npm writes for pi.
const env = standInPi(join(fx.base, "pi-env"));
const text = readFileSync(env.cli, "utf8");
writeFileSync(env.cli, "#!/usr/bin/env node" + text.slice(text.indexOf("\n")));
const h = await started({ fx, pi: env.found });
try {
assert.equal(h.launcher.launches.length, 1);
assert.equal(h.launcher.launches[0].args[0], realpathSync(env.cli));
} finally {
await h.close();
}
});
test("S4g: a lookup answer that no longer holds together refuses unsealed-engine: a package link moved since the lookup, or a hand-built answer with another bin", async () => {
const fx = fixture();
const good = standInPi(join(fx.base, "pi-good"));
const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] });
// A global prefix whose package directory is a link, as a version manager
// keeps it.
const prefix = join(fx.base, "prefix");
mkdirSync(join(prefix, "lib", "node_modules", "@earendil-works"), { recursive: true });
mkdirSync(join(prefix, "bin"));
const link = join(prefix, "lib", "node_modules", PI_PACKAGE);
symlinkSync(good.pkg, link);
const bin = join(prefix, "bin", "pi");
symlinkSync(`../lib/node_modules/${PI_PACKAGE}/dist/bundle/cli.js`, bin);
const found = piAt(bin);
assert.equal(found.bin, bin);
const make = (pi) => new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: new FakeLauncher(), units: noUnits, timeouts: FAST, pi });
const ctrl = make(found);
try {
unlinkSync(link);
symlinkSync(lacking.pkg, link);
// Built before the move: the start refuses.
await assert.rejects(ctrl.start(), (e) => {
assert.equal(e.code, ENGINE_PIN_MISMATCH);
assert.equal(e.message, OLD_PIN_LINE);
assert.match(e.message, /old pin record; start a new claim/);
assert.equal(e.code, UNSEALED_ENGINE);
assert.equal(e.message, "MOSAIC_PI_BIN no longer matches the lookup's answer; look the pi up again");
return true;
});
assert.equal(launcher.launches.length, 0);
assert.equal(ctrl.launcher.launches.length, 0);
} finally {
await ctrl.close().catch(() => {});
}
// Built after the move, with the stale answer.
assert.throws(() => make(found), (e) => e.code === UNSEALED_ENGINE);
// An answer with a bin swapped in by hand.
assert.throws(() => make({ ...good.found, bin: lacking.bin }), (e) => e.code === UNSEALED_ENGINE);
assert.throws(() => make({ bin: good.bin, label: "MOSAIC_PI_BIN" }), (e) => e.code === UNSEALED_ENGINE);
});
// ---- W11, W16, W17 ----------------------------------------------------------
+1 -1
View File
@@ -568,7 +568,7 @@ test("K6: recover without proof, without confirmation, or with changed pins is r
assert.equal((await h.client.request("recover", { stop, confirmation: id })).refusal, "confirmation", "a confirmation for another operation");
// Other bytes in the pi the lookup finds, then the same bytes again.
const original = readFileSync(pi.cli, "utf8");
writeFileSync(pi.cli, `${original}# other bytes\n`);
writeFileSync(pi.cli, `${original}// other bytes\n`);
const changed = await h.client.confirmed("recover", { stop });
assert.equal(changed.refusal, ENGINE_PIN_MISMATCH);
assert.equal(changed.detail, "the engine pin or launch argv changed since the proven stop");
+1 -1
View File
@@ -33,7 +33,7 @@ export function standInPi(dir, knobs = {}) {
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: PI_PACKAGE, version: knobs.version ?? "9.9.9", bin: { pi: "dist/bundle/cli.js" } }));
const cli = join(pkg, "dist", "bundle", "cli.js");
writeFileSync(cli, piStandIn(knobs), { mode: 0o755 });
writeFileSync(cli, piStandIn(knobs, { form: knobs.form ?? "node" }), { mode: 0o755 });
mkdirSync(join(dir, "prefix", "bin"));
const bin = join(dir, "prefix", "bin", "pi");
symlinkSync(`../lib/node_modules/${PI_PACKAGE}/dist/bundle/cli.js`, bin);
+15 -5
View File
@@ -1056,11 +1056,21 @@ test("N24b: the seal covers the engine command and environment: config can't nam
// A command changed after construction is refused at bind; nothing launches.
const fx2 = fixture();
const launcher = new FakeLauncher();
const ctrl = new Controller({ fixtureRoot: fx2.base, claimRoot: fx2.claimRoot, socketDir: fx2.socketDir, sessionFile: fx2.sessionFile, seat: fx2.seat, launcher, units: noUnits, timeouts: FAST });
// With no pi option the controller takes the one the lookup finds (#1873 S4).
const found = locate(process.env);
assert.deepEqual(ctrl.pi, found);
assert.deepEqual(ctrl.engine.preArgs, [realpathSync(found.bin)]);
// With no pi option the controller takes the one the lookup finds in its own
// environment, read when it is built (#1873 S4): here MOSAIC_PI_BIN names a
// stand-in, which no other step of the lookup would find.
const own = standInPi(join(fx2.base, "env-pi"));
const savedBin = process.env.MOSAIC_PI_BIN;
process.env.MOSAIC_PI_BIN = own.bin;
let ctrl;
try {
ctrl = new Controller({ fixtureRoot: fx2.base, claimRoot: fx2.claimRoot, socketDir: fx2.socketDir, sessionFile: fx2.sessionFile, seat: fx2.seat, launcher, units: noUnits, timeouts: FAST });
assert.deepEqual(ctrl.pi, locate(process.env));
} finally {
if (savedBin === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = savedBin;
}
assert.equal(ctrl.pi.label, "MOSAIC_PI_BIN");
assert.deepEqual(ctrl.engine.preArgs, [realpathSync(own.cli)]);
// Closed in finally: a start that wrongly succeeds holds the socket open.
try {
ctrl.engine.command = "/bin/sh";
+4 -1
View File
@@ -32,7 +32,10 @@ scripts/discord-service.sh render | install | uninstall | status <binding>
- `check` validates the binding, the token file (0600, regular, not a
symlink), the context files and the pi the harness lookup finds
(`MOSAIC_PI_BIN`, else the first `pi` on `PATH`, else the repository's;
#1873 S4); reads the bot, the guild and
#1873 S4). `--repo` no longer picks the pi, and the CLI runs no fit check
on it yet, so a pi on `PATH` is used as it is. The service wrapper still
checks that the copy has its own `node_modules/.bin/pi`, but the CLI may run
another pi that the lookup finds first. It then reads the bot, the guild and
every listed channel over REST; opens one gateway connection, waits for
READY, closes it. Nothing is sent to a channel. Close code 4014 is reported
as the message-content intent not being granted in the developer portal.
+10 -3
View File
@@ -1,5 +1,5 @@
// Shared fakes for the offline suite. No network, no token, no model.
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, chmodSync } from "node:fs";
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, chmodSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { after } from "node:test";
@@ -53,12 +53,19 @@ export function binding(overrides = {}) {
return validateBinding(rawBinding(overrides));
}
// A fake repository with the files prepare() looks for.
// A fake repository with the files prepare() looks for. Its pi is a package the
// harness lookup accepts, and MOSAIC_PI_BIN names it for every CLI this test
// process starts (#1873 S4), so no test reads the host's own pi.
export function makeRepo(root) {
const repo = join(root, "repo");
const pkg = join(repo, "node_modules", "@earendil-works", "pi-coding-agent");
mkdirSync(join(repo, "node_modules", ".bin"), { recursive: true });
mkdirSync(join(pkg, "dist"), { recursive: true });
mkdirSync(join(repo, "contracts"), { recursive: true });
writeFileSync(join(repo, "node_modules", ".bin", "pi"), "#!/bin/sh\nexit 0\n", { mode: 0o755 });
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.0.0-test", bin: { pi: "dist/cli.js" } }));
writeFileSync(join(pkg, "dist", "cli.js"), "#!/bin/sh\nexit 0\n", { mode: 0o755 });
symlinkSync("../@earendil-works/pi-coding-agent/dist/cli.js", join(repo, "node_modules", ".bin", "pi"));
process.env.MOSAIC_PI_BIN = join(repo, "node_modules", ".bin", "pi");
writeFileSync(join(repo, "contracts", "CONSTITUTION.md"), "# constitution\nbe good\n");
return repo;
}
@@ -14,6 +14,8 @@ function setup(){
}
test('check --offline validates local fixture without REST or gateway calls',()=>{
const s=setup(),r=s.run('check',['--offline']);assert.equal(r.status,0,r.stderr);assert.match(r.stdout,/token file mode 0600 ok; STOP absent/);assert.match(r.stdout,/offline check passed; no REST or gateway calls were made\n$/);assert.ok(!existsSync(s.calls));
// #1873 S4: the fixture's own pi, which the helpers name in MOSAIC_PI_BIN, never the host's.
assert.ok(r.stdout.includes(`pi ${join(s.repo,'node_modules','.bin','pi')};`),r.stdout);
// Online check still reaches REST: the trap fails it immediately.
const online=s.run();assert.equal(online.status,1);assert.ok(existsSync(s.calls));assert.match(online.stderr,/fixture refuses network/);
});
+20 -1
View File
@@ -233,7 +233,26 @@ 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 } = {}) {
// form "node" (#1873 S4): the same stand-in as one node script with node in its shebang, the kind
// of file the conversation controller launches with node; its --version and $ARGV_OUT parts are
// JavaScript too, so it needs node wherever it runs.
export function piStandIn(knobs = {}, { node = process.execPath, form = "sh" } = {}) {
if (form === "node") {
const head = knobs.versionHang ? "setTimeout(() => process.exit(0), 60000); return;" : knobs.versionExit != null ? `process.exit(${Number(knobs.versionExit)});` : `process.stdout.write(${JSON.stringify(String(knobs.version ?? "9.9.9"))} + "\\n"); process.exit(0);`;
return [
`#!${node}`,
`"use strict";`,
`process.argv.splice(1, 1);`,
`(() => {`,
`if (process.argv[1] === "--version") { ${head} }`,
`if (process.env.ARGV_OUT) { require("node:fs").writeFileSync(process.env.ARGV_OUT, process.argv.slice(1).map((a) => a + "\\n").join("")); process.stdout.write(${JSON.stringify(String(knobs.sessionOut ?? ""))}); process.exit(0); }`,
`const K = ${JSON.stringify(knobs)};`,
`const FLAGS = ${JSON.stringify(FIT_FLAGS)};`,
BODY,
`})();`,
"",
].join("\n");
}
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 [