From 824b1213c1babcf574583ec2bb5be46947b1c7f7 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 04:04:39 -0600 Subject: [PATCH 1/8] feat(conversation,discord): pi-unpin S4: the controller launches the pi the harness lookup finds (binary.mjs locate, passed in as the pi option and defaulting to locate(process.env)) in place of pinRoot and PI_BIN; the seal compares the pre-argument to the real path of that bin, read again at each check; the version pin gives way to the fit check (pi-fit.mjs): a pi that lacks a feature or can't be checked refuses engine-feature-missing with the check's line, and the claim pins are engineVersion (the --version text) and enginePin (the fit digest), so changed bytes under a stopped claim still refuse engine-pin-mismatch; --no-mcp joins the seal when the pi's help lists it; a claim made under the old pin record refuses engine-pin-mismatch with a line saying to start a new claim; pi-pin.mjs is renamed pi-seal.mjs without the pin constants; the Discord CLI takes its pi from the same lookup; tests red on 29daa4c4b (#1873, lane pi-unpin-s4) --- packages/cli/tests/stack-copy.test.mjs | 3 +- packages/conversation/README.md | 48 +++++-- packages/conversation/src/controller.mjs | 79 +++++++--- .../src/{pi-pin.mjs => pi-seal.mjs} | 80 +++++------ packages/conversation/src/turns.mjs | 2 +- packages/conversation/tests/claim.test.mjs | 136 ++++++++++++++++-- packages/conversation/tests/cohort.test.mjs | 36 ++--- packages/conversation/tests/ctrl-child.mjs | 3 +- packages/conversation/tests/harness.mjs | 29 +++- packages/conversation/tests/smoke.test.mjs | 40 ++++-- packages/conversation/tests/turns.test.mjs | 43 +++++- packages/discord/README.md | 4 +- packages/discord/src/cli.mjs | 10 +- packages/discord/tests/offline-check.test.mjs | 15 +- 14 files changed, 384 insertions(+), 144 deletions(-) rename packages/conversation/src/{pi-pin.mjs => pi-seal.mjs} (66%) diff --git a/packages/cli/tests/stack-copy.test.mjs b/packages/cli/tests/stack-copy.test.mjs index 6457cdbf5..86bc9e9e7 100644 --- a/packages/cli/tests/stack-copy.test.mjs +++ b/packages/cli/tests/stack-copy.test.mjs @@ -146,7 +146,8 @@ test('real pinned install loads bus and Console; the Console starts only with -- const s=setup(t,true); const copied=(await s.run('stack-copy.sh',['copy',s.id,'--copies',s.copies]));assert.equal(copied.status,0,copied.stderr); const root=join(s.copies,s.id),url=f=>pathToFileURL(join(root,f)).href; - const {checkEnginePin}=await import(url('packages/conversation/src/pi-pin.mjs'));assert.equal(checkEnginePin(root).version,'0.85.1'); + await import(url('packages/conversation/src/pi-seal.mjs'));const {locate}=await import(url('packages/plugins/harness/pi/binary.mjs')),{levels}=await import(url('packages/plugins/harness/pi/index.mjs')),{piFit}=await import(url('packages/harness/src/pi-fit.mjs')); + const copyPi=locate({});assert.equal(copyPi.label,"the repository's pi");assert.equal(copyPi.bin,join(root,'node_modules','.bin','pi'));const fit=await piFit(copyPi,{levels:levels()});assert.equal(fit.ok,true,fit.message);assert.equal(fit.version,'0.85.1'); const home=join(s.root,'home'),f=fixture(home),configDir=join(home,'.config','mosaic-pin'); mkdirSync(join(home,'.config'),{recursive:true});renameSync(join(home,'config'),configDir);chmodSync(configDir,0o700); writeFileSync(join(f.dataRoot,'instance.json'),JSON.stringify({instanceVersion:1,name:'pin'})); diff --git a/packages/conversation/README.md b/packages/conversation/README.md index c721a9d9f..3904bc70d 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -234,7 +234,7 @@ live session, and the controller never writes a session file (W11). | `src/cohort.mjs` | `ScopeLauncher` (systemd user scope plus `shim.mjs`), `PgroupLauncher`, force stop, cohort and boot proofs | | `src/shim.mjs` | the scope's first process, outside the `engine` cgroup; ops `hello`, `events`, `members`, `term`, `freeze`, `kill`, `release`; ignores SIGTERM and exits on `release` once `engine` is empty | | `src/guard.mjs` | `LiveSessionGuard`: refuses live sessions and paths under live roots | -| `src/pi-pin.mjs` | the Pi pin (0.85.1 and its integrity) and the seal | +| `src/pi-seal.mjs` | the seal (with `--no-mcp` when the pi's help lists it) and the engine refusal codes | | `src/engine.mjs`, `src/framing.mjs` | the engine link and LF framing | | `src/turns.mjs` | `Tracker`: runs, the dispatch slot, overlap signals O1–O6 | | `src/events.mjs` | native event to CHAT-01 event mapping | @@ -289,16 +289,33 @@ can disagree with it. (`--no-session`, `--fork`, `--export`, `--print`, `--continue`, ...) or a bare word, which Pi reads as a prompt (lead decision 31; N24, including the missing flag through `checkSeal`). -- **Engine command and environment (slice 1 S5, #1522).** The controller - always launches `node /node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js` - and checks that at construction and again at bind. `engine` takes only +- **Engine command and environment (slice 1 S5, #1522; #1873 S4).** The + controller launches `node `. + The lookup is the pi harness module's (`packages/plugins/harness/pi/binary.mjs`): + `MOSAIC_PI_BIN`, else the first `pi` on `PATH`, else the repository's + copy. The `pi` option takes a lookup answer and defaults to + `locate(process.env)`. The controller checks the command at construction + and again at bind, resolving the bin's real path each time, so a bin link + moved after construction refuses `unsealed-engine`. `engine` takes only `extraArgs`, `cwd` and `envKeys`; any other key (`command`, `preArgs`, `env`), or an `engine` that isn't an object, refuses `unsealed-engine`. The fake engine comes in through `opts[TEST_ENGINE]`, a symbol key that JSON config can't carry, so no config file reaches an unsealed command. `preArgs` carrying `--extension` still refuses there too. The binding's `argvDigest` records the full command line. -- **Engine environment.** The engine gets `ENGINE_ENV` (`pi-pin.mjs`): +- **Engine fit and pins (#1873 S4).** No pi is refused for its version. + Before a claim, a resume or a launch, the controller runs the fit check + (`packages/harness/src/pi-fit.mjs`) on the pi the lookup finds. A pi that + lacks a feature the seal needs, or that the check can't read, refuses + `engine-feature-missing` with the fit check's line (for example + `pi 9.9.9 (MOSAIC_PI_BIN) lacks --mode rpc`). The claim's pins are + `engineVersion` (what `--version` printed, or null), `enginePin` (the fit + check's digest of the pi's files) and `argvDigest`. A resume or recovery + 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". +- **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`, `PI_SKIP_VERSION_CHECK` and `PI_TELEMETRY`, plus the names in @@ -472,7 +489,7 @@ different IDs, so overlap at the seam can't be deduplicated (CHAT-01 lines ## Slash text `textPolicy` refuses any prompt whose text, after leading whitespace, starts -with `/`: pinned Pi runs extension commands, skills (`/skill:`) and prompt +with `/`: Pi runs extension commands, skills (`/skill:`) and prompt templates from index 0 (S1, S2). `!`, `!!` and `@` are interpreted only by Pi's interactive mode and CLI, not on the RPC prompt path, so they are admitted and sent as text. A `/` on a later line is not interpreted @@ -547,13 +564,14 @@ runs in CHAT-01 `check.mjs` order, then the CHAT-03 narrowings. | `eligibility` | launch with a used or unknown eligibility record, one from another incarnation, or a changed reservation (K17) | | `already-active`, `unsafe-replacement`, `foreign-host` | the writer claim (W2, W3, W17) | | `live-session-refused` | the live-session guard, at construction, bind and launch (G1–G3) | -| `unsealed-engine`, `engine-pin-mismatch` | the seal or the Pi pin (N24) | +| `unsealed-engine`, `engine-pin-mismatch` | the seal, or the engine's version, digest or argv changed under a claim (N24, W9, S4c) | +| `engine-feature-missing` | the fit check found the pi lacks a feature, or couldn't tell (S4b) | | `configuration` | a required constructor path is missing or misplaced, the session file is unreadable, or the session header ID isn't a CHAT-01 ID | `malformed`, `eligibility` and `preflight` are names this build adds; the brief describes those refusals without naming them. -The controller, claim store, live-session guard and engine pin throw +The controller, claim store, live-session guard and engine checks throw `ControlRefusal` (`safe-fs.mjs`), a subclass of the reader's `Refusal`. These codes reach a socket client, never the reader's HTTP routes, so the control board's status map (`REFUSAL_STATUS`, which its tests check against every @@ -563,15 +581,15 @@ board ever serves the controller, these codes need statuses there first. Receipt reason codes, not refusals: `transport-unknown`, `handled-without-run`, `ack-without-start`, `interrupted`, `run-overlap`. -## Findings against pinned Pi +## Findings against Pi 0.85.1 -From `tests/smoke.test.mjs`, which starts the pinned binary sealed, in a +From `tests/smoke.test.mjs`, which starts the repository's pi (0.85.1) sealed, in a scratch home with an empty agent dir and no inherited environment, and sends no prompt: - It starts with no credentials and answers `get_state`, `get_commands`, `clear_queue`, `abort` and `get_tree`. Every field the fake engine sends - for those commands, pinned Pi sends with the same type. `clear_queue` emits + for those commands, Pi 0.85.1 sends with the same type. `clear_queue` emits `queue_update` with empty queues before its response, as the fake does. Pi writes `auth.json` (`{}`) and `models-store.json` into the agent dir. - **Startup append.** Pi appends to the session at startup in two cases @@ -593,13 +611,13 @@ no prompt: | Suite | Covers | |---|---| -| `claim.test.mjs` | W1–W17, W20, G1–G3: the writer claim, crash barriers, the guard | +| `claim.test.mjs` | W1–W17, W20, G1–G3, S4a–S4e: the writer claim, crash barriers, the guard, the engine fit, pins and seal | | `races.test.mjs` | H1–H4, H9–H23: takeover, Interrupt and force stop, retries, incarnations | -| `turns.test.mjs` | N1–N25, N24b: the turn tracker against the fake engine's Pi behaviors; the engine seal and environment | +| `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) | | `flows.test.mjs` | S1–S7, P3, E1–E7, the terminal, and a CHAT-01 schema check of every record produced | -| `smoke.test.mjs` | the pinned Pi binary, as above | +| `smoke.test.mjs` | the repository's Pi binary, as above, and its fit check | -`fake-pi.mjs` models pinned Pi's RPC mode, including the startup append, and +`fake-pi.mjs` models Pi 0.85.1's RPC mode, including the startup append, and `ctrl-child.mjs` runs a controller in a child process for the crash tests. Fixtures live in temporary directories and are removed after each file. diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index 1b2afede0..b4ad05c57 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -16,7 +16,7 @@ // as in CHAT-01. With no verifier nothing verifies, and every stop ends // `uncertain`. -import { lstatSync, mkdirSync, readFileSync, unlinkSync } from "node:fs"; +import { lstatSync, mkdirSync, readFileSync, realpathSync, unlinkSync } from "node:fs"; import { createServer } from "node:net"; import { basename, dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; @@ -30,7 +30,10 @@ import { LineSplitter, encodeLine, parseLine } from "./framing.mjs"; import { LiveSessionGuard, realPath } from "./guard.mjs"; import { ID, fragments, safeId } from "./parts.mjs"; import { parseSnapshot } from "./pi.mjs"; -import { ENGINE_PIN_MISMATCH, PI_BIN, UNSEALED_ENGINE, argvDigest, buildPiArgs, checkEnginePin, checkSeal, engineEnv } from "./pi-pin.mjs"; +import { ENGINE_FEATURE_MISSING, ENGINE_PIN_MISMATCH, OLD_PIN_LINE, UNSEALED_ENGINE, argvDigest, buildPiArgs, checkSeal, engineEnv, oldPinRecord } from "./pi-seal.mjs"; +import { locate } from "../../plugins/harness/pi/binary.mjs"; +import { levels } from "../../plugins/harness/pi/index.mjs"; +import { piFit } from "../../harness/src/pi-fit.mjs"; import { ACTOR, conversationId, createReader, rootsFromSpecs } from "./reader.mjs"; import { clone, equal, hash, newId, receiptAllows, record, scopeMatch, sealProof, sha256, targetOf } from "./records.mjs"; import { ControlRefusal, Refusal } from "./safe-fs.mjs"; @@ -79,7 +82,7 @@ const STOP_IN_PROGRESS = new Set(["fenced", "cancelling", "stopping"]); const STOP_NEXT = { fenced: ["cancelling", "stopping", "uncertain"], cancelling: ["stopping", "uncertain"], stopping: ["uncertain"], uncertain: [] }; // check.mjs `ending`: the binding follows only stops that end the engine. const ending = (mode) => mode === "force-stop" || mode === "engine-exit"; -const SEAL_BASIS = "seal: --no-extensions and no --extension argument (pi-pin.mjs SEAL_FLAGS)"; +const SEAL_BASIS = "seal: --no-extensions and no --extension argument (pi-seal.mjs SEAL_FLAGS)"; const DIALOG_REASON = "Pi dialogs are not answered in CHAT-03 (lead decision 30); shown disabled"; const refused = (code, extra = {}) => ({ outcome: `refused:${code}`, refusal: code, ...extra }); @@ -158,7 +161,7 @@ export class Controller { fixtureRoot, claimRoot, socketDir, sessionFile, seat, workspace = "default", engine = {}, launcher = new PgroupLauncher(), verifier = null, host, identity, barrier = null, units = systemdUnits, grants = null, guardOptions = {}, timeouts = {}, now = () => new Date(), - policyRevision = "policy-fixture-1", sourceRootRef = "source-fixture-1", approvedMappings = null, pinRoot = REPO_ROOT, auditWritable = true, + policyRevision = "policy-fixture-1", sourceRootRef = "source-fixture-1", approvedMappings = null, pi = locate(process.env), auditWritable = true, hostId = null, } = opts; for (const [k, v] of Object.entries({ claimRoot, socketDir, sessionFile, seat })) { @@ -180,12 +183,15 @@ export class Controller { this.conversation = conversationId(this.root, basename(this.paths.sessionFile)); if (!engine || typeof engine !== "object" || Array.isArray(engine)) throw new ControlRefusal(UNSEALED_ENGINE, "engine is not an object"); const extra = Object.keys(engine).find((k) => !ENGINE_KEYS.has(k)); - if (extra !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${extra.slice(0, 40)} can't be configured; the controller launches the pinned Pi with its own environment`); + if (extra !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${extra.slice(0, 40)} can't be configured; the controller launches the pi the lookup finds, with its own environment`); const test = opts[TEST_ENGINE] ?? null; + // #1873 S4: the pi is binary.mjs's answer ({ bin, label, ... } or + // { bin: null, problem }). A sealed launch runs its bin's real file. + this.pi = pi; this.engine = { sealed: test === null, command: test ? test.command : process.execPath, - preArgs: test ? test.preArgs : [join(pinRoot, PI_BIN)], + preArgs: test ? test.preArgs : [this.#piFile()], extraArgs: engine.extraArgs ?? [], cwd: engine.cwd ?? projectRoot, env: test ? test.env : engineEnv(engine.envKeys), @@ -194,8 +200,8 @@ export class Controller { if (!Array.isArray(this.engine[k])) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${k} is not a list`); } if (typeof this.engine.command !== "string" || !this.engine.command) throw new ControlRefusal(UNSEALED_ENGINE, "the engine command is not a path"); + this.noMcp = false; this.piArgs = buildPiArgs({ sessionFile: this.paths.sessionFile, extraArgs: this.engine.extraArgs }); - this.pinRoot = pinRoot; this.#checkSeal(); this.launcher = launcher; this.verifier = verifier; @@ -257,20 +263,53 @@ export class Controller { if (this.barrier) await this.barrier(name, detail); } + // 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. + #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); + } + try { + return realpathSync(pi.bin); + } catch { + throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${pi.label ?? "the pi"} no longer resolves to a file`); + } + } + // The seal covers the command: unless a test engine was given, the launch - // is this Node running the pinned Pi's bin, and nothing else. + // is this Node running the real file of the bin the lookup found, read + // again at each check, and nothing else. #checkSeal() { - if (this.engine.sealed && (this.engine.command !== process.execPath || this.engine.preArgs.length !== 1 || this.engine.preArgs[0] !== join(this.pinRoot, PI_BIN))) { - throw new ControlRefusal(UNSEALED_ENGINE, "the engine command is not the pinned Pi"); + if (this.engine.sealed && (this.engine.command !== process.execPath || this.engine.preArgs.length !== 1 || this.engine.preArgs[0] !== this.#piFile())) { + throw new ControlRefusal(UNSEALED_ENGINE, `the engine command is not ${this.pi?.label ?? "the pi"} the lookup found`); } const bad = this.engine.preArgs.find((a) => typeof a !== "string" || a === "-e" || a === "--extension" || a.startsWith("--extension=")); if (bad !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine pre-arguments carry ${bad}`); - checkSeal(this.piArgs); + checkSeal(this.piArgs, { noMcp: this.noMcp }); } - #pins() { - const pin = checkEnginePin(this.pinRoot); - return { engineVersion: pin.version, enginePin: pin.pin, argvDigest: argvDigest(this.engine.command, [...this.engine.preArgs, ...this.piArgs]) }; + // #1873 S4: the fit check (pi-fit.mjs, cached by the digest of the files + // that run) stands where the version pin stood. A pi that fails it, or + // can't be checked, refuses engine-feature-missing with the check's line. + // 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(); + 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"); + this.noMcp = fit.noMcp === true; + this.piArgs = buildPiArgs({ sessionFile: this.paths.sessionFile, extraArgs: this.engine.extraArgs, noMcp: this.noMcp }); + this.#checkSeal(); + return { engineVersion: typeof fit.version === "string" ? fit.version : null, enginePin: fit.digest, argvDigest: argvDigest(this.engine.command, [...this.engine.preArgs, ...this.piArgs]) }; + } + + // The line for a pins mismatch against a claim's recorded pins. + static pinLine(recorded, since) { + return oldPinRecord(recorded) ? OLD_PIN_LINE : `the engine pin or launch argv changed since ${since}`; } // Every read after construction refuses a header ID other than the one the @@ -341,7 +380,7 @@ export class Controller { async start() { this.#guardCheck("bind"); this.#checkSeal(); - const pins = this.#pins(); + const pins = await this.#pins(); const session = this.#readSession(); const classified = await this.store.classify(this.seatK, this.sessionK, { units: this.units, @@ -373,7 +412,7 @@ export class Controller { let priorRef = null; if (prior.n > 0 && !prior.damaged && prior.record.state === "stopped") { const p = prior.record; - if (!equal(p.pins, pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, "the engine pin or launch argv changed since the proven stop"); + if (!equal(p.pins, pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, Controller.pinLine(p.pins, "the proven stop")); const leaf = p.leafAtProof !== undefined ? p.leafAtProof : p.leaf; const branch = p.branchAtProof ?? p.branch; if (leaf !== session.leaf || branch !== session.branch) throw new ControlRefusal("target", "the session branch or leaf changed since the proven stop"); @@ -1679,11 +1718,11 @@ export class Controller { if (b.state !== "stopped" || b.admission !== "closed" || !this.#stopped(s)) return refused("stop-proof"); let pins; try { - pins = this.#pins(); + pins = await this.#pins(); } catch (err) { - return refused(err.code ?? ENGINE_PIN_MISMATCH); + return refused(err.code ?? ENGINE_PIN_MISMATCH, typeof err?.message === "string" ? { detail: err.message } : {}); } - if (!equal(pins, this.claim.record.pins)) return refused(ENGINE_PIN_MISMATCH); + if (!equal(pins, this.claim.record.pins)) return refused(ENGINE_PIN_MISMATCH, { detail: Controller.pinLine(this.claim.record.pins, "the proven stop") }); const session = this.#readSession(); if (session.leaf !== this.claim.record.leafAtProof || session.branch !== this.claim.record.branchAtProof) return refused("target"); if (!this.#checkConfirmation(r, c, "recover")) return refused("confirmation"); @@ -1715,7 +1754,7 @@ export class Controller { throw new ControlRefusal(ELIGIBILITY, "the reserved claim changed since eligibility"); } } - const pins = this.#pins(); + const pins = await this.#pins(); if (!equal(pins, e.pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, "the engine pin or launch argv changed since eligibility"); const session = this.#readSession(); if (session.leaf !== e.leaf || session.branch !== e.branch) throw new ControlRefusal("target", "the session branch or leaf changed since eligibility (K18)"); diff --git a/packages/conversation/src/pi-pin.mjs b/packages/conversation/src/pi-seal.mjs similarity index 66% rename from packages/conversation/src/pi-pin.mjs rename to packages/conversation/src/pi-seal.mjs index 7cc612416..41d4262f5 100644 --- a/packages/conversation/src/pi-pin.mjs +++ b/packages/conversation/src/pi-seal.mjs @@ -1,14 +1,21 @@ -// The Pi pin and the engine seal (#1507, CHAT-03 §3, lead decisions 31–32). +// The engine seal and the engine's pins (#1507, CHAT-03 §3, lead decisions +// 31–32; #1873 pi-unpin S4). // -// Pin: package-lock.json and npm's installed record -// (node_modules/.package-lock.json) must both name the pinned version with the -// pinned integrity. That ties the install to the package through npm's record; -// it is not a hash of the files on disk. `pi` runs dist/bundle/cli.js, the -// package's bin, and the built-in llama.cpp extension ships inside it. +// Pins: no pi version is pinned (Jason, 2026-11-19). The controller runs the +// pi binary.mjs's lookup finds (MOSAIC_PI_BIN, else the first pi on PATH, else +// the repository's) once the fit check (packages/harness/src/pi-fit.mjs) +// passes it. The claim's pins are what that check reports: `engineVersion`, +// the `--version` text (null when pi printed none), and `enginePin`, the +// sha256 digest of the files that run. A pi that fails the check, or can't be +// checked, is refused `engine-feature-missing`; a digest or argv that differs +// from a claim's is refused `engine-pin-mismatch`. A claim made before #1873 +// holds npm's sha512 integrity in `enginePin`, which no digest equals, so it +// refuses once with OLD_PIN_LINE. // // Seal: the controller builds the launch argv. It always carries -// --no-extensions, --no-prompt-templates, --no-themes and --no-approve, and -// never an --extension argument (cli/args.js; usage.md 224 and 233–236). +// --no-extensions, --no-prompt-templates, --no-themes and --no-approve, plus +// --no-mcp when the pi's --help lists it, and never an --extension argument +// (cli/args.js; usage.md 224 and 233–236). // --no-approve sets the project trust override to false, so a project's // .pi/settings.json, SYSTEM.md, APPEND_SYSTEM.md and skills don't load even // when trust.json under the agent dir trusts it (main.js 574–581; @@ -23,60 +30,45 @@ // file, so the argv must be exactly the controller's prefix followed by // ENGINE_OPTIONS pairs, each at most once with one plain value. -import { readFileSync } from "node:fs"; -import { isAbsolute, join } from "node:path"; +import { isAbsolute } from "node:path"; import { createHash } from "node:crypto"; import { ControlRefusal } from "./safe-fs.mjs"; -export const PI_PACKAGE = "@earendil-works/pi-coding-agent"; -export const PI_VERSION = "0.85.1"; -export const PI_INTEGRITY = "sha512-FGRN+OHbWaefBPGaTggAdLjrIHW+s2PzLyglz/5dfLzb9of7uuXMXYC0fJIeZTw+shS32o2cuQ9jF7YSDuL/oQ=="; -export const PI_BIN = join("node_modules", PI_PACKAGE, "dist", "bundle", "cli.js"); export const SEAL_FLAGS = Object.freeze(["--no-extensions", "--no-prompt-templates", "--no-themes", "--no-approve"]); export const ENGINE_OPTIONS = Object.freeze(["--model", "--provider", "--thinking"]); export const ENGINE_PIN_MISMATCH = "engine-pin-mismatch"; export const UNSEALED_ENGINE = "unsealed-engine"; +export const ENGINE_FEATURE_MISSING = "engine-feature-missing"; -function lockEntry(path) { - let lock; - try { - lock = JSON.parse(readFileSync(path, "utf8")); - } catch { - return null; - } - const entry = lock?.packages?.[`node_modules/${PI_PACKAGE}`]; - return entry && typeof entry === "object" ? entry : null; +// The seal flags for a pi whose --help does (noMcp) or doesn't list --no-mcp. +export const sealFlags = (noMcp = false) => (noMcp ? [...SEAL_FLAGS, "--no-mcp"] : [...SEAL_FLAGS]); + +// An `enginePin` the fit check gives is a sha256 hex digest. +export const PIN_DIGEST = /^[0-9a-f]{64}$/; +export const OLD_PIN_LINE = "the claim was made under the old pin record; start a new claim"; +// True when a claim's pins came from the old npm pin record: an `enginePin` +// string that isn't a digest (npm's integrity is `sha512-...`). +export const oldPinRecord = (pins) => typeof pins?.enginePin === "string" && !PIN_DIGEST.test(pins.enginePin); + +export function buildPiArgs({ sessionFile, extraArgs = [], noMcp = false }) { + return ["--mode", "rpc", ...sealFlags(noMcp), "--session", sessionFile, ...extraArgs]; } -// `root` holds package-lock.json and node_modules/.package-lock.json. -export function checkEnginePin(root) { - for (const path of [join(root, "package-lock.json"), join(root, "node_modules", ".package-lock.json")]) { - const entry = lockEntry(path); - if (!entry || entry.version !== PI_VERSION || entry.integrity !== PI_INTEGRITY) { - throw new ControlRefusal(ENGINE_PIN_MISMATCH, `${path} does not pin ${PI_PACKAGE} ${PI_VERSION} with the pinned integrity`); - } - } - return { version: PI_VERSION, pin: PI_INTEGRITY }; -} - -export function buildPiArgs({ sessionFile, extraArgs = [] }) { - return ["--mode", "rpc", ...SEAL_FLAGS, "--session", sessionFile, ...extraArgs]; -} - -// Refuses any argv that is not `--mode rpc`, the four --no-* flags and -// `--session `, in that order, followed by ENGINE_OPTIONS -// pairs. That covers --extension in either spelling, a second --mode or +// Refuses any argv that is not `--mode rpc`, the four --no-* flags (and +// --no-mcp when noMcp), and `--session `, in that order, +// followed by ENGINE_OPTIONS pairs. That covers --extension in either spelling, a second --mode or // --session, session and output flags (--no-session, --fork, --export, ...) // and stray prompt words. -export function checkSeal(args) { +export function checkSeal(args, { noMcp = false } = {}) { if (!Array.isArray(args) || args.some((a) => typeof a !== "string")) throw new ControlRefusal(UNSEALED_ENGINE, "launch argv is not a list of strings"); const extension = args.find((a) => a === "-e" || a === "--extension" || a.startsWith("--extension=")); if (extension !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv carries ${extension}`); - for (const flag of SEAL_FLAGS) { + const flags = sealFlags(noMcp); + for (const flag of flags) { if (!args.includes(flag)) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv lacks ${flag}`); } - const prefix = ["--mode", "rpc", ...SEAL_FLAGS, "--session"]; + const prefix = ["--mode", "rpc", ...flags, "--session"]; if (prefix.some((a, i) => args[i] !== a)) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv does not start with ${prefix.join(" ")}`); const file = args[prefix.length]; if (typeof file !== "string" || !isAbsolute(file)) throw new ControlRefusal(UNSEALED_ENGINE, "the --session value is not an absolute path"); diff --git a/packages/conversation/src/turns.mjs b/packages/conversation/src/turns.mjs index 1aaf8a9ac..f726d37a2 100644 --- a/packages/conversation/src/turns.mjs +++ b/packages/conversation/src/turns.mjs @@ -1,6 +1,6 @@ // Run tracking, overlap signals and receipt settlement (#1507, CHAT-03 §3). // -// Pinned Pi ties no event to a prompt. Under the seal (pi-pin.mjs) the Mosaic +// Pi ties no event to a prompt. Under the seal (pi-seal.mjs) the Mosaic // prompt in the slot is the only thing that can start a run, so a run that // follows the slot's ack is attributed to it by order. The tracker watches // for signs that the seal failed (O1–O6) and for lead decision 34's diff --git a/packages/conversation/tests/claim.test.mjs b/packages/conversation/tests/claim.test.mjs index 9ce1e492b..87831dd0b 100644 --- a/packages/conversation/tests/claim.test.mjs +++ b/packages/conversation/tests/claim.test.mjs @@ -5,23 +5,26 @@ import { test, after } from "node:test"; import assert from "node:assert/strict"; -import { appendFileSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs"; +import { appendFileSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs"; import { homedir, tmpdir } from "node:os"; import { join } from "node:path"; import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { ALREADY_ACTIVE, ClaimStore, FOREIGN_HOST, UNSAFE_REPLACEMENT, held, machineId } from "../src/claim.mjs"; -import { Controller } from "../src/controller.mjs"; +import { Controller, REPO_ROOT } from "../src/controller.mjs"; import { ConversationClient } from "../src/client.mjs"; import { AUTHORITY, scopeAvailable } from "../src/cohort.mjs"; import { FixtureVerifier } from "../src/records.mjs"; import { LIVE_SESSION_REFUSED } from "../src/guard.mjs"; -import { ENGINE_PIN_MISMATCH, PI_PACKAGE } from "../src/pi-pin.mjs"; +import { ENGINE_FEATURE_MISSING, ENGINE_PIN_MISMATCH, OLD_PIN_LINE, UNSEALED_ENGINE } from "../src/pi-seal.mjs"; +import { piFit } from "../../harness/src/pi-fit.mjs"; +import { levels } from "../../plugins/harness/pi/index.mjs"; import { bootId } from "../../discord/src/journal.mjs"; import { FakeLauncher } from "./fake-pi.mjs"; import { REPO, FAST, fixture, controllerFor, started, spawnController, killChildren, cleanupAll, reap, claimRecords, userEntry, assistantEntry, thinkingEntry, receiptState, noUnits, tick, killShims, shimsGone, + PI_PACKAGE, standInPi, piAt, } from "./harness.mjs"; const reaped = []; @@ -579,14 +582,8 @@ test("W8: resume after a proven stop with the same pins: new claim ID, generatio test("W9: resume with a changed binary, argv digest, branch or leaf is refused and the claim is unchanged", async () => { const cases = { - binary: (fx) => { - const root = mkdtempSync(join(fx.base, "pin-")); - const lock = { packages: { [`node_modules/${PI_PACKAGE}`]: { version: "0.85.2", integrity: "sha512-other" } } }; - mkdirSync(join(root, "node_modules")); - writeFileSync(join(root, "package-lock.json"), JSON.stringify(lock)); - writeFileSync(join(root, "node_modules", ".package-lock.json"), JSON.stringify(lock)); - return [{ pinRoot: root }, ENGINE_PIN_MISMATCH]; - }, + // Another pi, which the fit check passes, with other files. + binary: (fx) => [{ pi: standInPi(join(fx.base, "other-pi"), { version: "0.85.2" }).found }, ENGINE_PIN_MISMATCH], argv: () => [{ engine: { extraArgs: ["--model", "other"] } }, ENGINE_PIN_MISMATCH], leaf: (fx) => { appendFileSync(fx.sessionFile, JSON.stringify(userEntry("d4e5f6a7", "b2c3d4e5", "outside", 9)) + "\n"); @@ -610,6 +607,123 @@ test("W9: resume with a changed binary, argv digest, branch or leaf is refused a } }); +// ---- #1873 pi-unpin S4: the engine is the pi the lookup finds -------------- + +test("S4a: a stand-in pi with another version and every feature launches, and the claim records its version and digest", async () => { + const fx = fixture(); + const pi = standInPi(join(fx.base, "pi"), { version: "7.4.2" }); + const h = await started({ fx, pi: pi.found }); + try { + assert.equal(h.launcher.launches.length, 1); + const fit = await piFit(pi.found, { levels: levels() }); + assert.equal(fit.ok, true, fit.message); + const recorded = h.ctrl.claim.record.pins; + assert.equal(recorded.engineVersion, "7.4.2"); + assert.equal(recorded.enginePin, fit.digest); + assert.match(recorded.enginePin, /^[0-9a-f]{64}$/); + } finally { + await h.close(); + } +}); + +test("S4b: a stand-in pi whose --help lacks --mode rpc is refused engine-feature-missing with a plain line; nothing launches or is claimed", async () => { + const fx = fixture(); + const pi = standInPi(join(fx.base, "pi"), { modes: ["text", "json"] }); + const k = keysFor(fx, { pi: pi.found }); + const { ctrl, launcher } = controllerFor(fx, { pi: pi.found }); + try { + await assert.rejects(ctrl.start(), (e) => { + assert.equal(e.code, ENGINE_FEATURE_MISSING); + assert.equal(e.message, "pi 9.9.9 (MOSAIC_PI_BIN) lacks --mode rpc"); + 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(() => {}); + } +}); + +test("S4c: changed bytes in the pi between launch and resume refuse engine-pin-mismatch; the claim is unchanged", async () => { + 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"); + 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) }); + try { + await assert.rejects(ctrl.start(), (e) => { + assert.equal(e.code, ENGINE_PIN_MISMATCH); + assert.equal(e.message, "the engine pin or launch argv changed since the proven stop"); + return true; + }); + assert.equal(launcher.launches.length, 0); + assert.deepEqual([revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)], before); + assert.equal(first.pins.engineVersion, "9.9.9"); + } finally { + await ctrl.close().catch(() => {}); + } +}); + +test("S4d: a pre-argument that is not the resolved bin's real file refuses unsealed-engine at bind, as does a bin link moved after construction", async () => { + const fx = fixture(); + const pi = standInPi(join(fx.base, "pi")); + const make = () => 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: pi.found }); + { + const ctrl = make(); + try { + assert.deepEqual(ctrl.engine.preArgs, [realpathSync(pi.cli)]); + // The repository's own pi, which the old pin accepted. + ctrl.engine.preArgs = [join(REPO_ROOT, "node_modules", PI_PACKAGE, "dist", "bundle", "cli.js")]; + await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE); + assert.equal(ctrl.launcher.launches.length, 0); + // The bin link itself, not its real file. + ctrl.engine.preArgs = [pi.bin]; + await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE); + assert.equal(ctrl.launcher.launches.length, 0); + } finally { + await ctrl.close().catch(() => {}); + } + } + { + const ctrl = make(); + const other = standInPi(join(fx.base, "pi-other"), { version: "1.0.0" }); + try { + unlinkSync(pi.bin); + symlinkSync(other.cli, pi.bin); + await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE); + assert.equal(ctrl.launcher.launches.length, 0); + } finally { + await ctrl.close().catch(() => {}); + } + } +}); + +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 () => { + const fx = fixture(); + const first = await provenStop(fx); + // Rewrite the stopped heads as a claim made before #1873 would hold them. + for (const { path, record } of claimRecords(fx)) { + if (record?.claimId !== first.claimId || record.state !== "stopped") continue; + const old = { ...record, pins: { ...record.pins, engineVersion: "0.85.1", enginePin: "sha512-FGRN+OHbWaefBPGaTggAdLjrIHW+s2PzLyglz/5dfLzb9of7uuXMXYC0fJIeZTw+shS32o2cuQ9jF7YSDuL/oQ==" } }; + unlinkSync(path); + writeFileSync(path, JSON.stringify(old), { mode: 0o400 }); + } + 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(() => {}); + } +}); + // ---- W11, W16, W17 ---------------------------------------------------------- test("W11: the controller writes no session file; only the fake engine's own appends appear", async () => { diff --git a/packages/conversation/tests/cohort.test.mjs b/packages/conversation/tests/cohort.test.mjs index ca0461fad..38b987421 100644 --- a/packages/conversation/tests/cohort.test.mjs +++ b/packages/conversation/tests/cohort.test.mjs @@ -9,17 +9,17 @@ import { test, after } from "node:test"; import assert from "node:assert/strict"; -import { appendFileSync, chmodSync, copyFileSync, mkdirSync, readFileSync, renameSync, rmdirSync, writeFileSync } from "node:fs"; +import { appendFileSync, chmodSync, mkdirSync, readFileSync, renameSync, rmdirSync, writeFileSync } from "node:fs"; import { spawn, spawnSync } from "node:child_process"; import { dirname, join } from "node:path"; import { ClaimStore, FOREIGN_HOST, defaultHost, newClaimId, unitNameFor } from "../src/claim.mjs"; import { ConversationClient } from "../src/client.mjs"; import { AUTHORITY, PgroupLauncher, ScopeLauncher, engineExitCohort, forceStopCohort, releaseCohort, scopeAvailable, shimRequest, systemctlShow, systemdUnits } from "../src/cohort.mjs"; import { Controller, ELIGIBILITY, TEST_ENGINE } from "../src/controller.mjs"; -import { ENGINE_ENV, ENGINE_PIN_MISMATCH, engineEnv } from "../src/pi-pin.mjs"; +import { ENGINE_ENV, ENGINE_PIN_MISMATCH, engineEnv } from "../src/pi-seal.mjs"; import { FixtureVerifier, newId } from "../src/records.mjs"; import { ControlClient, FakeLauncher } from "./fake-pi.mjs"; -import { FAST, REPO, assistantEntry, claimRecords, cleanupAll, controllerFor, fixture, killChildren, killShims, liveShims, noUnits, reap, receiptState, shimsGone, spawnController, started, tick } from "./harness.mjs"; +import { FAST, PI, assistantEntry, claimRecords, cleanupAll, controllerFor, fixture, killChildren, killShims, liveShims, noUnits, reap, receiptState, shimsGone, spawnController, standInPi, started, tick } from "./harness.mjs"; const reaped = []; const strays = new Set(); @@ -111,7 +111,7 @@ async function live({ kind = "scope", barrier = null, verifier = new FixtureVeri fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: launcher ?? (kind === "scope" ? new ScopeLauncher() : new PgroupLauncher()), engine: { cwd: fx.proj }, [TEST_ENGINE]: { command: process.execPath, preArgs: [FAKE_PI], env: { ...process.env, FAKE_PI_CONTROL: control, FAKE_PI_LOG: join(fx.base, "fake.log") } }, - verifier, units: kind === "scope" ? systemdUnits : noUnits, timeouts, barrier, + verifier, units: kind === "scope" ? systemdUnits : noUnits, timeouts, barrier, pi: PI.found, }); await ctrl.start(); assert.equal(ctrl.binding.state, "active", JSON.stringify(ctrl.evidence.uncertain)); @@ -532,14 +532,6 @@ async function provenStop(h, c = h.client) { return fs.stop.id; } -function pinRootCopy(fx) { - const root = join(fx.base, "pins"); - mkdirSync(join(root, "node_modules"), { recursive: true }); - copyFileSync(join(REPO, "package-lock.json"), join(root, "package-lock.json")); - copyFileSync(join(REPO, "node_modules", ".package-lock.json"), join(root, "node_modules", ".package-lock.json")); - return root; -} - test("K6: recover without proof, without confirmation, or with changed pins is refused", async () => { { const h = await started(); @@ -562,8 +554,8 @@ test("K6: recover without proof, without confirmation, or with changed pins is r } { const fx = track(fixture()); - const pinRoot = pinRootCopy(fx); - const h = await started({ fx, pinRoot }); + const pi = standInPi(join(fx.base, "pi")); + const h = await started({ fx, pi: pi.found }); try { const stop = await provenStop(h); const missing = await h.client.request("recover", { stop }); @@ -574,13 +566,13 @@ test("K6: recover without proof, without confirmation, or with changed pins is r const id = issued.data.confirmation.id; await h.client.request("answer-confirmation", { confirmation: id, answer: "confirm" }); assert.equal((await h.client.request("recover", { stop, confirmation: id })).refusal, "confirmation", "a confirmation for another operation"); - const lock = join(pinRoot, "package-lock.json"); - const original = readFileSync(lock, "utf8"); - const changed = JSON.parse(original); - changed.packages["node_modules/@earendil-works/pi-coding-agent"].version = "0.0.0"; - writeFileSync(lock, JSON.stringify(changed)); - assert.equal((await h.client.confirmed("recover", { stop })).refusal, ENGINE_PIN_MISMATCH); - writeFileSync(lock, original); + // 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`); + 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"); + writeFileSync(pi.cli, original); const again = await h.client.request("issue-confirmation", { operationToConfirm: "recover" }); const once = again.data.confirmation.id; await h.client.request("answer-confirmation", { confirmation: once, answer: "confirm" }); @@ -1177,7 +1169,7 @@ function scopeController(fx, { host, verifier = new FixtureVerifier({ authoritie fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher, engine: { cwd: fx.proj }, [TEST_ENGINE]: { command: process.execPath, preArgs: [FAKE_PI], env: { ...process.env, FAKE_PI_CONTROL: join(fx.base, "fake-b.sock"), FAKE_PI_LOG: join(fx.base, "fake-b.log") } }, - verifier, units: systemdUnits, timeouts: FAST, barrier, host, + verifier, units: systemdUnits, timeouts: FAST, barrier, host, pi: PI.found, }); } diff --git a/packages/conversation/tests/ctrl-child.mjs b/packages/conversation/tests/ctrl-child.mjs index 608933a38..2c73b199b 100644 --- a/packages/conversation/tests/ctrl-child.mjs +++ b/packages/conversation/tests/ctrl-child.mjs @@ -20,6 +20,7 @@ import { Controller, TEST_ENGINE } from "../src/controller.mjs"; import { AUTHORITY, PgroupLauncher, ScopeLauncher, systemdUnits } from "../src/cohort.mjs"; import { FixtureVerifier } from "../src/records.mjs"; import { defaultHost } from "../src/claim.mjs"; +import { locate } from "../../plugins/harness/pi/binary.mjs"; const cfg = JSON.parse(process.argv[2]); const out = (v) => process.stdout.write(JSON.stringify(v) + "\n"); @@ -79,7 +80,7 @@ try { engine: { cwd: fx.proj }, [TEST_ENGINE]: { command: process.execPath, preArgs: [fakePi], env: { ...process.env, ...(cfg.fakeEnv ?? {}) } }, verifier: cfg.verifier === false ? null : new FixtureVerifier({ authorities: [AUTHORITY] }), units: cfg.units === "absent" ? { lookup: async () => ({ state: "absent" }) } : systemdUnits, - host, barrier, timeouts: cfg.timeouts, + host, barrier, timeouts: cfg.timeouts, pi: locate({ MOSAIC_PI_BIN: cfg.piBin, PATH: "/usr/bin:/bin" }), }); const started = await ctrl.start(); out({ ready: true, started, socketPath: ctrl.socketPath, incarnation: ctrl.incarnation, pid: process.pid, binding: ctrl.binding, claim: ctrl.claim ? { claimId: ctrl.claim.claimId, record: ctrl.claim.record } : null }); diff --git a/packages/conversation/tests/harness.mjs b/packages/conversation/tests/harness.mjs index 3dfd58427..c0a85fb35 100644 --- a/packages/conversation/tests/harness.mjs +++ b/packages/conversation/tests/harness.mjs @@ -2,7 +2,7 @@ // directory: fixture root, project, session file, claim root and socket // directory. Nothing here touches a live session. -import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync } from "node:fs"; +import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync, symlinkSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { spawnSync } from "node:child_process"; @@ -11,6 +11,8 @@ import { ConversationClient } from "../src/client.mjs"; import { AUTHORITY } from "../src/cohort.mjs"; import { FixtureVerifier } from "../src/records.mjs"; import { FakeLauncher } from "./fake-pi.mjs"; +import { piStandIn } from "../../harness/tests/pi-stand-in.mjs"; +import { locate } from "../../plugins/harness/pi/binary.mjs"; export const REPO = resolve(import.meta.dirname, "..", "..", ".."); export const SESSION_ID = "0f5e1c2a-1111-4222-8333-944455556666"; @@ -20,6 +22,27 @@ export const time = (i) => new Date(Date.UTC(2026, 9, 4, 12, 0, 0) + i * 1000).t const tmp = mkdtempSync(join(tmpdir(), "chat03-")); let serial = 0; +// #1873 S4: a stand-in pi package (packages/harness/tests/pi-stand-in.mjs, +// which the fit check passes unless its knobs say otherwise) under `dir`, +// with an npm-style bin link. `found` is the lookup's answer for it, through +// MOSAIC_PI_BIN. The test engine still launches fake-pi.mjs; the stand-in is +// what the controller's fit check and pins read. +export const PI_PACKAGE = "@earendil-works/pi-coding-agent"; +export function standInPi(dir, knobs = {}) { + const pkg = join(dir, "prefix", "lib", "node_modules", PI_PACKAGE); + 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 }); + mkdirSync(join(dir, "prefix", "bin")); + const bin = join(dir, "prefix", "bin", "pi"); + symlinkSync(`../lib/node_modules/${PI_PACKAGE}/dist/bundle/cli.js`, bin); + return { bin, cli, pkg, found: piAt(bin) }; +} +export const piAt = (bin) => locate({ MOSAIC_PI_BIN: bin, PATH: "/usr/bin:/bin" }); +// The pi every fixture controller checks unless a test passes its own. +export const PI = standInPi(join(tmp, "pi")); + export function cleanupAll() { spawnSync("chmod", ["-R", "u+rwx", tmp]); rmSync(tmp, { recursive: true, force: true }); @@ -51,7 +74,7 @@ export const noUnits = Object.freeze({ lookup: async () => ({ state: "absent" }) export function controllerFor(fx, { launcher = new FakeLauncher(), verifier = new FixtureVerifier({ authorities: [AUTHORITY] }), timeouts = FAST, ...opts } = {}) { const ctrl = new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, - launcher, verifier, units: noUnits, timeouts, ...opts, + launcher, verifier, units: noUnits, timeouts, pi: PI.found, ...opts, }); return { ctrl, launcher, verifier }; } @@ -118,7 +141,7 @@ export function killChildren() { // Spawns ctrl-child.mjs. `msgs` collects its JSON lines; `next(pred)` waits // for one. export function spawnController(cfg) { - const proc = spawn(process.execPath, [CHILD, JSON.stringify({ timeouts: FAST, ...cfg })], { stdio: ["pipe", "pipe", "pipe"] }); + const proc = spawn(process.execPath, [CHILD, JSON.stringify({ timeouts: FAST, piBin: PI.bin, ...cfg })], { stdio: ["pipe", "pipe", "pipe"] }); childPids.add(proc.pid); const msgs = []; const waiters = new Set(); diff --git a/packages/conversation/tests/smoke.test.mjs b/packages/conversation/tests/smoke.test.mjs index 7ac31d24c..3684e256e 100644 --- a/packages/conversation/tests/smoke.test.mjs +++ b/packages/conversation/tests/smoke.test.mjs @@ -1,8 +1,8 @@ -// CHAT-03 (#1507): the pinned Pi binary, started sealed in a scratch home with +// CHAT-03 (#1507): the repository's Pi binary, started sealed in a scratch home with // no credentials, answers the RPC commands the controller sends with the // shapes the fake engine models. No prompt is sent, so no model is called. // -// It also records pinned Pi's startup append (sdk.js 240–252): a session +// It also records Pi 0.85.1's startup append (sdk.js 240–252): a session // whose branch has no thinking_level_change entry gains one at every start, // so the leaf moves after launch and the K8 load check fails closed on it. // @@ -16,9 +16,15 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { PassThrough } from "node:stream"; import { LineSplitter, encodeLine } from "../src/framing.mjs"; -import { PI_BIN, PI_VERSION, buildPiArgs, checkEnginePin, checkSeal } from "../src/pi-pin.mjs"; +import { buildPiArgs, checkSeal } from "../src/pi-seal.mjs"; +import { piFit } from "../../harness/src/pi-fit.mjs"; +import { locate } from "../../plugins/harness/pi/binary.mjs"; +import { levels } from "../../plugins/harness/pi/index.mjs"; import { FakePi } from "./fake-pi.mjs"; -import { REPO, assistantEntry, header, thinkingEntry, userEntry } from "./harness.mjs"; +import { assistantEntry, header, thinkingEntry, userEntry } from "./harness.mjs"; + +// The repository's pi: with no MOSAIC_PI_BIN and no PATH the lookup ends there. +const REPO_PI = locate({}); const scratch = mkdtempSync(join(tmpdir(), "chat03-smoke-")); after(() => rmSync(scratch, { recursive: true, force: true })); @@ -90,9 +96,11 @@ async function realPi(tag, sessionFile, { project = "proj", trusted = null, afte assert.equal(existsSync(join(home, ".pi", "agent", "auth.json")), false); assert.deepEqual(readdirSync(agent), [], "the agent dir starts empty"); if (trusted) writeFileSync(join(agent, "trust.json"), JSON.stringify({ [realpathSync(join(scratch, trusted))]: true })); - const args = buildPiArgs({ sessionFile }); - checkSeal(args); - const pi = spawn(process.execPath, [join(REPO, PI_BIN), ...args, ...after], { cwd: join(scratch, project), env, stdio: ["pipe", "pipe", "pipe"] }); + const fit = await piFit(REPO_PI, { levels: levels() }); + assert.equal(fit.ok, true, fit.message); + const args = buildPiArgs({ sessionFile, noMcp: fit.noMcp }); + checkSeal(args, { noMcp: fit.noMcp }); + const pi = spawn(process.execPath, [realpathSync(REPO_PI.bin), ...args, ...after], { cwd: join(scratch, project), env, stdio: ["pipe", "pipe", "pipe"] }); let stderr = ""; pi.stderr.on("data", (c) => (stderr += c)); const exited = new Promise((r) => pi.on("exit", (code, signal) => r({ code, signal }))); @@ -118,11 +126,15 @@ function fakePi(sessionFile) { const lastId = (file) => JSON.parse(readFileSync(file, "utf8").trim().split("\n").at(-1)).id; const typesOf = (data) => Object.fromEntries(Object.entries(data ?? {}).map(([k, v]) => [k, v === null ? "null" : Array.isArray(v) ? "array" : typeof v])); -test("the engine pin holds for the installed package", () => { - assert.deepEqual(checkEnginePin(REPO).version, PI_VERSION); +test("the repository's pi passes the fit check and gives a version and a digest", async () => { + assert.equal(REPO_PI.label, "the repository's pi"); + const fit = await piFit(REPO_PI, { levels: levels() }); + assert.equal(fit.ok, true, fit.message); + assert.equal(fit.version, "0.85.1"); + assert.match(fit.digest, /^[0-9a-f]{64}$/); }); -test("pinned Pi, sealed and without credentials, answers the controller's commands with the shapes the fake models", async () => { +test("the repository's Pi, sealed and without credentials, answers the controller's commands with the shapes the fake models", async () => { const entries = [thinkingEntry(), userEntry("a1b2c3d4", "f0e1d2c3", "hello"), assistantEntry("b2c3d4e5", "a1b2c3d4", "hi")]; const realFile = session("real.jsonl", entries); const fakeFile = session("fake.jsonl", entries); @@ -155,7 +167,7 @@ test("pinned Pi, sealed and without credentials, answers the controller's comman assert.equal(r.command, f.command, type); assert.equal(r.success, f.success, type); const rt = typesOf(r.data), ft = typesOf(f.data); - for (const [k, t] of Object.entries(ft)) assert.equal(rt[k], t, `${type}.${k}: the fake models a field pinned Pi doesn't send that way`); + for (const [k, t] of Object.entries(ft)) assert.equal(rt[k], t, `${type}.${k}: the fake models a field the repository's Pi doesn't send that way`); } // clear_queue: one queue_update with both queues empty precedes the response, in both. for (const side of [real, fake]) { @@ -165,7 +177,7 @@ test("pinned Pi, sealed and without credentials, answers the controller's comman } }); -test("sealed, pinned Pi ignores a trusted project's .pi resources; --approve past the seal would load them, and checkSeal refuses it (Filbert F2 on #1522)", async () => { +test("sealed Pi ignores a trusted project's .pi resources; --approve past the seal would load them, and checkSeal refuses it (Filbert F2 on #1522)", async () => { const skill = join(scratch, "trusted", ".pi", "skills", "probe"); mkdirSync(skill, { recursive: true }); writeFileSync(join(skill, "SKILL.md"), "---\nname: probe\ndescription: A project skill that only a trusted load offers.\n---\nprobe\n"); @@ -183,7 +195,7 @@ test("sealed, pinned Pi ignores a trusted project's .pi resources; --approve pas } }); -test("pinned Pi appends thinking_level_change at start when the branch lacks one, so the leaf moves (K8 then fails closed)", async () => { +test("the repository's Pi appends thinking_level_change at start when the branch lacks one, so the leaf moves (K8 then fails closed)", async () => { const entries = [userEntry("a1b2c3d4", null, "hello"), assistantEntry("b2c3d4e5", "a1b2c3d4", "hi")]; const realFile = session("bare-real.jsonl", entries); const fakeFile = session("bare-fake.jsonl", entries); @@ -197,5 +209,5 @@ test("pinned Pi appends thinking_level_change at start when the branch lacks one assert.notEqual(side.responses.get_tree.data.leafId, "b2c3d4e5", "the leaf moved off the loaded leaf"); assert.equal(side.responses.get_tree.data.leafId, added[0].id); } - if (process.env.CHAT03_SMOKE_OUT) writeFileSync(process.env.CHAT03_SMOKE_OUT, JSON.stringify({ pi: PI_VERSION, commands: COMMANDS, exchanges }, null, 2) + "\n"); + if (process.env.CHAT03_SMOKE_OUT) writeFileSync(process.env.CHAT03_SMOKE_OUT, JSON.stringify({ pi: (await piFit(REPO_PI, { levels: levels() })).version, commands: COMMANDS, exchanges }, null, 2) + "\n"); }); diff --git a/packages/conversation/tests/turns.test.mjs b/packages/conversation/tests/turns.test.mjs index 404b1dbdf..97e87c442 100644 --- a/packages/conversation/tests/turns.test.mjs +++ b/packages/conversation/tests/turns.test.mjs @@ -5,14 +5,16 @@ import { test, after } from "node:test"; import assert from "node:assert/strict"; import { PassThrough } from "node:stream"; +import { realpathSync } from "node:fs"; import { join } from "node:path"; import { Controller, TEST_ENGINE, REPO_ROOT, HANDLED_WITHOUT_RUN, ACK_WITHOUT_START, NO_TURN, RUN_OVERLAP, TRANSPORT_UNKNOWN } from "../src/controller.mjs"; import { AUTHORITY } from "../src/cohort.mjs"; import { FixtureVerifier, sha256 } from "../src/records.mjs"; -import { ENGINE_ENV, PI_BIN, SEAL_FLAGS, UNSEALED_ENGINE, checkSeal } from "../src/pi-pin.mjs"; +import { ENGINE_ENV, SEAL_FLAGS, UNSEALED_ENGINE, checkSeal } from "../src/pi-seal.mjs"; +import { locate } from "../../plugins/harness/pi/binary.mjs"; import { LineSplitter, encodeLine, parseLine } from "../src/framing.mjs"; import { BUSY_ERROR, FakeLauncher, FakePi } from "./fake-pi.mjs"; -import { fixture, started, receiptState, outcomeUnknownPush, sessionText, cleanupAll, tick, noUnits, FAST } from "./harness.mjs"; +import { fixture, started, receiptState, outcomeUnknownPush, sessionText, cleanupAll, tick, noUnits, FAST, PI, PI_PACKAGE, standInPi } from "./harness.mjs"; after(() => cleanupAll()); @@ -982,12 +984,38 @@ test("N24: the seal is an allow-list: --extension, a missing --no-* flag, a seco } }); +test("N24c: when --help lists --no-mcp the launch passes it after the seal flags, and the seal then requires it (#1873 S4)", async () => { + const fx = fixture(); + const pi = standInPi(join(fx.base, "pi"), { extraFlags: ["--no-mcp"] }); + const h = await started({ fx, pi: pi.found }); + try { + const args = h.launcher.launches[0].args.slice(1); + assert.deepEqual(args.slice(0, SEAL_FLAGS.length + 5), ["--mode", "rpc", ...SEAL_FLAGS, "--no-mcp", "--session", fx.sessionFile]); + assert.equal(h.ctrl.noMcp, true); + assert.equal(checkSeal(args, { noMcp: true }), true); + const without = args.filter((a) => a !== "--no-mcp"); + assert.throws(() => checkSeal(without, { noMcp: true }), (e) => e.code === UNSEALED_ENGINE); + assert.throws(() => checkSeal(args), (e) => e.code === UNSEALED_ENGINE, "--no-mcp where help lacks it"); + } finally { + await h.close(); + } + // The shared stand-in's help lacks --no-mcp, so its launch has none. + const plain = await started({ fx: fixture() }); + try { + assert.equal(plain.launcher.launches[0].args.includes("--no-mcp"), false); + assert.equal(plain.ctrl.noMcp, false); + } finally { + await plain.close(); + } +}); + test("N24b: the seal covers the engine command and environment: config can't name either, the env is built from names, and a mutated command is refused at bind", async () => { const fx = fixture(); const make = (extra) => new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: new FakeLauncher(), units: noUnits, timeouts: FAST, ...extra }); // The plain engine option takes extraArgs, cwd and envKeys; the command, // pre-arguments and environment are the controller's (I3, #1507). - for (const engine of [{ command: "/bin/sh" }, { preArgs: [join(REPO_ROOT, PI_BIN)] }, { command: process.execPath, preArgs: [join(REPO_ROOT, PI_BIN), "--extension", "x"] }, { env: {} }, { env: process.env }, null, [], "pi"]) { + const repoCli = join(REPO_ROOT, "node_modules", PI_PACKAGE, "dist", "bundle", "cli.js"); + for (const engine of [{ command: "/bin/sh" }, { preArgs: [repoCli] }, { command: process.execPath, preArgs: [repoCli, "--extension", "x"] }, { env: {} }, { env: process.env }, null, [], "pi"]) { assert.throws(() => make({ engine }), (e) => e.code === UNSEALED_ENGINE, JSON.stringify(engine)); } // A configuration is JSON, which can't carry the symbol-keyed test engine. @@ -1001,7 +1029,7 @@ test("N24b: the seal covers the engine command and environment: config can't nam assert.throws(() => make({ [TEST_ENGINE]: { command: process.execPath, preArgs: ["fake.mjs", "-e", "x"], env: {} } }), (e) => e.code === UNSEALED_ENGINE); assert.throws(() => make({ [TEST_ENGINE]: { command: "", preArgs: [], env: {} } }), (e) => e.code === UNSEALED_ENGINE); - // The launch: this Node, the pinned bin, and an environment of named keys + // The launch: this Node, the real file of the pi the lookup found, and an environment of named keys // only, whatever the controller's own environment holds. const planted = { NODE_OPTIONS: "--require /tmp/x.cjs", LD_PRELOAD: "/tmp/x.so", PI_PACKAGE_DIR: "/tmp/pkg", ZAI_API_KEY: "zai-test-value", OTHER_API_KEY: "other-test-value" }; const saved = Object.fromEntries(Object.keys(planted).map((k) => [k, process.env[k]])); @@ -1015,7 +1043,8 @@ test("N24b: the seal covers the engine command and environment: config can't nam try { const l = h.launcher.launches[0]; assert.equal(l.command, process.execPath); - assert.equal(l.args[0], join(REPO_ROOT, PI_BIN)); + assert.equal(l.args[0], realpathSync(PI.bin)); + assert.equal(l.args[0], h.ctrl.engine.preArgs[0]); assert.equal(l.env.ZAI_API_KEY, "zai-test-value"); for (const k of Object.keys(l.env)) assert.ok(ENGINE_ENV.includes(k) || k === "ZAI_API_KEY", k); for (const k of ["NODE_OPTIONS", "LD_PRELOAD", "PI_PACKAGE_DIR", "OTHER_API_KEY"]) assert.equal(k in l.env, false, k); @@ -1028,6 +1057,10 @@ test("N24b: the seal covers the engine command and environment: config can't nam 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)]); // Closed in finally: a start that wrongly succeeds holds the socket open. try { ctrl.engine.command = "/bin/sh"; diff --git a/packages/discord/README.md b/packages/discord/README.md index d0a0249e8..362c79024 100644 --- a/packages/discord/README.md +++ b/packages/discord/README.md @@ -30,7 +30,9 @@ scripts/discord-service.sh render | install | uninstall | status `--repo` for you; the CLI also takes `--config PATH`. - `check` validates the binding, the token file (0600, regular, not a - symlink), the context files and the pi install; reads the bot, the guild and + 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 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. diff --git a/packages/discord/src/cli.mjs b/packages/discord/src/cli.mjs index 11726a514..37d746ab0 100755 --- a/packages/discord/src/cli.mjs +++ b/packages/discord/src/cli.mjs @@ -46,7 +46,7 @@ // 3 refused by a brake (STOP present or the binding held; a supervisor must // not retry); 4 usage. -import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { createHash } from "node:crypto"; import { DiscordError, printable } from "./errors.mjs"; @@ -57,6 +57,7 @@ import { defaultConfigPath, loadDataRoot, bindingPath, bindingDataDir, loadBindi import { createRest } from "./rest.mjs"; import { createGateway, CONNECTOR_INTENTS } from "./gateway.mjs"; import { createEngine, buildPiArgs, sessionContinues } from "./engine-pi.mjs"; +import { locate } from "../../plugins/harness/pi/binary.mjs"; import { TOOLS_ENV, enabledToolNames } from "./tools.mjs"; import { createSetsparkApi } from "./setspark.mjs"; import { assembleContext } from "./context.mjs"; @@ -134,8 +135,11 @@ function prepare(opts) { checkLibraryEngine(dirname(opts.config), binding); const contextFiles = resolveContextFiles(binding, opts.repo); const toolRoots = resolveToolRoots(binding, { dataRoot }); - const pi = join(opts.repo, "node_modules", ".bin", "pi"); - if (!existsSync(pi)) throw new DiscordError("pi not found at node_modules/.bin/pi in the repository; run npm ci in the repository"); + // #1873 S4: the pi the harness lookup finds (MOSAIC_PI_BIN, the first pi on PATH, the + // repository's), not only the repository's copy. + const found = locate(process.env); + if (!found.bin) throw new DiscordError(found.problem); + const pi = found.bin; const journalDir = bindingDataDir(dataRoot, binding.name); const sessionDir = join(dataRoot, "sessions", `discord-${binding.name}`); return { dataRoot, bindingFile, binding, contextFiles, toolRoots, pi, journalDir, sessionDir }; diff --git a/packages/discord/tests/offline-check.test.mjs b/packages/discord/tests/offline-check.test.mjs index ae069322b..bd6ad4299 100644 --- a/packages/discord/tests/offline-check.test.mjs +++ b/packages/discord/tests/offline-check.test.mjs @@ -1,15 +1,15 @@ import test from 'node:test'; import assert from 'node:assert/strict'; import {spawnSync} from 'node:child_process'; -import {writeFileSync,chmodSync,unlinkSync,existsSync} from 'node:fs'; -import {join} from 'node:path'; +import {writeFileSync,chmodSync,unlinkSync,existsSync,mkdirSync,symlinkSync} from 'node:fs'; +import {dirname,join} from 'node:path'; import {fileURLToPath} from 'node:url'; import {makeRoot,makeRepo,makeDeployment} from './helpers.mjs'; const CLI=fileURLToPath(new URL('../src/cli.mjs',import.meta.url)); function setup(){ const root=makeRoot(),repo=makeRepo(root),d=makeDeployment(root,{}, {tokenContent:'MTAw.abcdefghijklmnopqrstuvwxyz0123456789'}),calls=join(root,'network-called'),preload=join(root,'no-network.mjs'); writeFileSync(preload,`import {writeFileSync} from 'node:fs';const refuse=()=>{writeFileSync(${JSON.stringify(calls)},'called');throw Error('fixture refuses network')};globalThis.fetch=refuse;globalThis.WebSocket=class{constructor(){refuse()}};`); - const run=(command='check',args=[])=>spawnSync(process.execPath,['--import',preload,CLI,command,d.raw.name,'--config',d.config,'--repo',repo,...args],{encoding:'utf8',timeout:5000}); + const run=(command='check',args=[],env=process.env)=>spawnSync(process.execPath,['--import',preload,CLI,command,d.raw.name,'--config',d.config,'--repo',repo,...args],{encoding:'utf8',timeout:5000,env}); return {root,repo,d,calls,run}; } test('check --offline validates local fixture without REST or gateway calls',()=>{ @@ -24,3 +24,12 @@ test('check --offline refuses token mode and missing context under the selected test('run --offline is a usage refusal before reading config or networking',()=>{ const s=setup();unlinkSync(s.d.config);const r=s.run('run',['--offline']);assert.equal(r.status,4,r.stderr);assert.match(r.stderr,/--offline/);assert.ok(!existsSync(s.calls)); }); +test('#1873 S4: check takes the pi the harness lookup finds; MOSAIC_PI_BIN names it, and a bad one refuses with the lookup line, no path',()=>{ + // A pi package the way npm installs one under a prefix: its bin is linked from prefix/bin. + const s=setup(),pkg=join(s.root,'prefix','lib','node_modules','@earendil-works','pi-coding-agent'),bin=join(s.root,'prefix','bin','pi'); + mkdirSync(join(pkg,'dist'),{recursive:true});mkdirSync(dirname(bin),{recursive:true}); + writeFileSync(join(pkg,'package.json'),JSON.stringify({name:'@earendil-works/pi-coding-agent',version:'9.9.9',bin:{pi:'dist/cli.js'}})); + writeFileSync(join(pkg,'dist','cli.js'),'#!/bin/sh\nexit 0\n',{mode:0o755});symlinkSync(join(pkg,'dist','cli.js'),bin); + const named=s.run('check',['--offline'],{...process.env,MOSAIC_PI_BIN:bin});assert.equal(named.status,0,named.stderr);assert.ok(named.stdout.includes(`pi ${bin};`),named.stdout); + const bad=s.run('check',['--offline'],{...process.env,MOSAIC_PI_BIN:'relative/pi'});assert.equal(bad.status,2,bad.stderr);assert.match(bad.stderr,/MOSAIC_PI_BIN isn't an executable file at an absolute path/);assert.ok(!bad.stderr.includes(s.root));assert.ok(!existsSync(s.calls)); +}); -- 2.54.0 From 28a5d38349c33c753a367e4497654763a5d56396 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 04:26:33 -0600 Subject: [PATCH 2/8] 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) --- packages/conversation/README.md | 15 +++- packages/conversation/src/controller.mjs | 51 +++++++++-- packages/conversation/tests/claim.test.mjs | 90 +++++++++++++++++-- packages/conversation/tests/cohort.test.mjs | 2 +- packages/conversation/tests/harness.mjs | 2 +- packages/conversation/tests/turns.test.mjs | 20 +++-- packages/discord/README.md | 5 +- packages/discord/tests/helpers.mjs | 13 ++- packages/discord/tests/offline-check.test.mjs | 2 + packages/harness/tests/pi-stand-in.mjs | 21 ++++- 10 files changed, 195 insertions(+), 26 deletions(-) diff --git a/packages/conversation/README.md b/packages/conversation/README.md index 3904bc70d..c29138324 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -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) | diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index b4ad05c57..97e0b67b9 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -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"); diff --git a/packages/conversation/tests/claim.test.mjs b/packages/conversation/tests/claim.test.mjs index 87831dd0b..d7b111e4f 100644 --- a/packages/conversation/tests/claim.test.mjs +++ b/packages/conversation/tests/claim.test.mjs @@ -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 ---------------------------------------------------------- diff --git a/packages/conversation/tests/cohort.test.mjs b/packages/conversation/tests/cohort.test.mjs index 38b987421..c2fa6efeb 100644 --- a/packages/conversation/tests/cohort.test.mjs +++ b/packages/conversation/tests/cohort.test.mjs @@ -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"); diff --git a/packages/conversation/tests/harness.mjs b/packages/conversation/tests/harness.mjs index c0a85fb35..d67a2d915 100644 --- a/packages/conversation/tests/harness.mjs +++ b/packages/conversation/tests/harness.mjs @@ -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); diff --git a/packages/conversation/tests/turns.test.mjs b/packages/conversation/tests/turns.test.mjs index 97e87c442..27f223b43 100644 --- a/packages/conversation/tests/turns.test.mjs +++ b/packages/conversation/tests/turns.test.mjs @@ -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"; diff --git a/packages/discord/README.md b/packages/discord/README.md index 362c79024..f4922b166 100644 --- a/packages/discord/README.md +++ b/packages/discord/README.md @@ -32,7 +32,10 @@ scripts/discord-service.sh render | install | uninstall | status - `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. diff --git a/packages/discord/tests/helpers.mjs b/packages/discord/tests/helpers.mjs index 291bb1b28..76f0c2f71 100644 --- a/packages/discord/tests/helpers.mjs +++ b/packages/discord/tests/helpers.mjs @@ -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; } diff --git a/packages/discord/tests/offline-check.test.mjs b/packages/discord/tests/offline-check.test.mjs index bd6ad4299..655f8592f 100644 --- a/packages/discord/tests/offline-check.test.mjs +++ b/packages/discord/tests/offline-check.test.mjs @@ -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/); }); diff --git a/packages/harness/tests/pi-stand-in.mjs b/packages/harness/tests/pi-stand-in.mjs index 81f8d566e..5d06668b2 100644 --- a/packages/harness/tests/pi-stand-in.mjs +++ b/packages/harness/tests/pi-stand-in.mjs @@ -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 [ -- 2.54.0 From 3f39ed3f11bfab6f231e2ee2569fac89b9ebbdef Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 04:43:11 -0600 Subject: [PATCH 3/8] fix(conversation): pi-unpin S4 re-check: a first line must end within the 256 bytes the kernel reads and carry no CR, and an absolute node in it must resolve to the engine's own node, so a package's own program named node or a long first line can't make the fit check run another program than the launch (P1); the lookup is run again on the answer's bin and must give the same root, dest, view and pkg, so a hand-built answer can't mount one package for the fit check and launch another (P2); S4g checks each field alone (P3); test S4h (#1873, lane pi-unpin-s4) --- packages/conversation/README.md | 15 +++-- packages/conversation/src/controller.mjs | 42 ++++++++------ packages/conversation/tests/claim.test.mjs | 66 +++++++++++++++++++++- 3 files changed, 99 insertions(+), 24 deletions(-) diff --git a/packages/conversation/README.md b/packages/conversation/README.md index c29138324..c8f93ee73 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -321,11 +321,14 @@ can disagree with it. - **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`. + `#!` and an absolute path that resolves to this Node refuses + `engine-feature-missing`. So does a first line that doesn't end within the + 256 bytes the kernel reads, or one with a CR before its newline. + The lookup's answer must also still be the lookup's at each check: the + lookup run again on its bin gives the same `root`, `dest`, `view` and `pkg`, + so the fit check mounts, runs and digests the package the launch runs. A + package link moved since the lookup, or an answer changed by hand, 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`, @@ -622,7 +625,7 @@ no prompt: | Suite | Covers | |---|---| -| `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 | +| `claim.test.mjs` | W1–W17, W20, G1–G3, S4a–S4h: 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) | diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index 97e0b67b9..9c78cdbe9 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -157,7 +157,8 @@ export function sessionTools(parsed) { // 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?$/; +const NODE_SHEBANG = /^#![ \t]*(?:\/usr\/bin\/env[ \t]+node|(\/[^\s]*\/node))[ \t]*$/; +const ANSWER_KEYS = ["root", "dest", "view", "pkg"]; export class Controller { constructor(opts = {}) { @@ -269,10 +270,10 @@ 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. 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. + // step, never a path. The answer must still be the lookup's: the lookup run + // again on its bin gives the same root, dest, view and pkg. A stale + // answer (a link moved since the lookup) or one changed by hand refuses + // unsealed-engine. #piFile() { const pi = this.pi; if (!pi || typeof pi !== "object" || typeof pi.bin !== "string" || !pi.bin) { @@ -286,14 +287,11 @@ export class Controller { } catch { 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 + "/")) { + // The lookup run again on the same bin must give the same answer: what the + // fit check mounts (root at dest), runs (view) and digests (pkg) is then + // the package the launch runs. + const again = locate({ MOSAIC_PI_BIN: pi.bin, ...(typeof process.env.HOME === "string" ? { HOME: process.env.HOME } : {}) }); + if (again.bin === null || ANSWER_KEYS.some((k) => (again[k] ?? null) !== (pi[k] ?? null))) { throw new ControlRefusal(UNSEALED_ENGINE, `${label} no longer matches the lookup's answer; look the pi up again`); } return real; @@ -302,7 +300,7 @@ export class Controller { // #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. + // absolute path to this same node) runs the same way in both. #nodeScript(file) { let head = ""; let fd = null; @@ -315,8 +313,20 @@ export class Controller { } 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`); + const label = this.pi.label ?? "the pi"; + // The kernel reads at most 256 bytes for the first line; one that doesn't + // end inside them isn't run as a #! line at all. + const nl = head.indexOf("\n"); + const m = nl < 0 ? null : NODE_SHEBANG.exec(head.slice(0, nl)); + if (!m) throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label} isn't a node script, and the engine runs pi with node`); + // An absolute node must be this one: a file named node that is something + // else would let the fit check run another program than the launch does. + if (m[1] !== undefined) { + let interp = null; + try { interp = realpathSync(m[1]); } catch {} + if (interp === null || interp !== realpathSync(process.execPath)) { + throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs another node than the engine's`); + } } } diff --git a/packages/conversation/tests/claim.test.mjs b/packages/conversation/tests/claim.test.mjs index d7b111e4f..07a788228 100644 --- a/packages/conversation/tests/claim.test.mjs +++ b/packages/conversation/tests/claim.test.mjs @@ -7,7 +7,7 @@ import { test, after } from "node:test"; import assert from "node:assert/strict"; import { appendFileSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs"; import { homedir, tmpdir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { ALREADY_ACTIVE, ClaimStore, FOREIGN_HOST, UNSAFE_REPLACEMENT, held, machineId } from "../src/claim.mjs"; @@ -763,7 +763,7 @@ test("S4f: a pi whose first line doesn't run node is refused engine-feature-miss } }); -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 () => { +test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engine: a package link moved since the lookup, a hand-built answer with another bin, or one field changed by hand", async () => { const fx = fixture(); const good = standInPi(join(fx.base, "pi-good")); const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] }); @@ -798,6 +798,68 @@ test("S4g: a lookup answer that no longer holds together refuses 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); + // One field changed by hand at a time: the fit check would mount, run or + // digest another package than the launch runs. + for (const k of ["root", "dest", "view", "pkg"]) { + const value = k === "dest" ? lacking.found.root : lacking.found[k]; + assert.throws(() => make({ ...good.found, [k]: value }), (e) => e.code === UNSEALED_ENGINE, k); + } + // The lacking pi's answer with the good package mounted over its path. + assert.throws(() => make({ ...lacking.found, root: good.found.root, dest: lacking.found.root }), (e) => e.code === UNSEALED_ENGINE); + // The answer as the lookup gives it still builds. + await make(piAt(good.bin)).close(); +}); + +test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline; a link to the engine's node launches", async () => { + const fx = fixture(); + const refused = async (pi, message) => { + const { ctrl, launcher } = controllerFor(fx, { pi }); + try { + await assert.rejects(ctrl.start(), (e) => { + assert.equal(e.code, ENGINE_FEATURE_MISSING); + assert.equal(e.message, message); + return true; + }); + assert.equal(launcher.launches.length, 0); + } finally { + await ctrl.close().catch(() => {}); + } + }; + const withFirstLine = (name, line) => { + const pi = standInPi(join(fx.base, name)); + const text = readFileSync(pi.cli, "utf8"); + writeFileSync(pi.cli, line + text.slice(text.indexOf("\n"))); + return pi; + }; + // A program named node in the package that runs another program: here the + // real node, so the fit check passes it. + const shim = standInPi(join(fx.base, "pi-shim")); + const fakeNode = join(dirname(shim.cli), "node"); + writeFileSync(fakeNode, `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 }); + const shimText = readFileSync(shim.cli, "utf8"); + writeFileSync(shim.cli, `#!${fakeNode}` + shimText.slice(shimText.indexOf("\n"))); + const fit = await piFit(shim.found, { levels: levels() }); + assert.equal(fit.ok, true, fit.message); + await refused(shim.found, "MOSAIC_PI_BIN's first line runs another node than the engine's"); + // The kernel reads 256 bytes for the first line; this one ends past them. + const long = withFirstLine("pi-long", "#!/usr/bin/env node" + " ".repeat(260)); + await refused(long.found, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node"); + // The kernel looks for a program named with the CR. + const cr = withFirstLine("pi-cr", "#!/usr/bin/env node\r"); + await refused(cr.found, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node"); + // A link to the engine's own node is the same file. + const linked = standInPi(join(fx.base, "pi-linked")); + const nodeLink = join(dirname(linked.cli), "node"); + symlinkSync(process.execPath, nodeLink); + const linkedText = readFileSync(linked.cli, "utf8"); + writeFileSync(linked.cli, `#!${nodeLink}` + linkedText.slice(linkedText.indexOf("\n"))); + const h = await started({ fx, pi: linked.found }); + try { + assert.equal(h.launcher.launches.length, 1); + assert.equal(h.launcher.launches[0].args[0], realpathSync(linked.cli)); + } finally { + await h.close(); + } }); // ---- W11, W16, W17 ---------------------------------------------------------- -- 2.54.0 From e0065b6a44e10c7d43a1553491a7657e0599774e Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 05:12:44 -0600 Subject: [PATCH 4/8] fix(conversation): pi-unpin S4 second re-check: under #!/usr/bin/env node the first node on the fit check's PATH (sessionEnv's: the repository's .bin, then this process's PATH) must resolve to the engine's own node and no node on it may lie inside the package the fit check mounts, or the start refuses engine-feature-missing, so the fit check and the launch run the same node (N1); the controller keeps a frozen plain copy of the lookup's answer, its own string data fields only, so a getter or a Proxy that answers differently later can't make the fit check read one package and the launch run another (N2); S4g covers a bin the lookup no longer takes (N3); a refusal line's label is the lookup's string label or "the pi"; tests in S4g and S4h (#1873, lane pi-unpin-s4) --- packages/conversation/README.md | 19 ++++-- packages/conversation/src/controller.mjs | 57 +++++++++++++++-- packages/conversation/tests/claim.test.mjs | 73 ++++++++++++++++++++-- 3 files changed, 134 insertions(+), 15 deletions(-) diff --git a/packages/conversation/README.md b/packages/conversation/README.md index c8f93ee73..f77b6ce2c 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -323,12 +323,19 @@ can disagree with it. file with this Node. A pi whose first line isn't `#!/usr/bin/env node` or `#!` and an absolute path that resolves to this Node refuses `engine-feature-missing`. So does a first line that doesn't end within the - 256 bytes the kernel reads, or one with a CR before its newline. - The lookup's answer must also still be the lookup's at each check: the - lookup run again on its bin gives the same `root`, `dest`, `view` and `pkg`, - so the fit check mounts, runs and digests the package the launch runs. A - package link moved since the lookup, or an answer changed by hand, refuses - `unsealed-engine`. + 256 bytes the kernel reads, or one with a CR before its newline. Under + `#!/usr/bin/env node`, env takes node from the fit check's PATH (the + repository's `.bin`, then this process's PATH, as `sessionEnv` builds it): + the first `node` on it must resolve to this Node, and none on it may lie + inside the package the fit check mounts, or the start refuses + `engine-feature-missing`. + The controller keeps a frozen plain copy of the lookup's answer, its own + string data fields only, so an answer that changes later changes nothing. + That copy must still be the lookup's at each check: the lookup run again on + its bin gives the same `root`, `dest`, `view` and `pkg`, so the fit check + mounts, runs and digests the package the launch runs. A package link moved + since the lookup, an answer changed by hand, or a bin the lookup no longer + takes 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`, diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index 9c78cdbe9..5eea54eab 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -31,7 +31,8 @@ import { LiveSessionGuard, realPath } from "./guard.mjs"; import { ID, fragments, safeId } from "./parts.mjs"; import { parseSnapshot } from "./pi.mjs"; import { ENGINE_FEATURE_MISSING, ENGINE_PIN_MISMATCH, OLD_PIN_LINE, UNSEALED_ENGINE, argvDigest, buildPiArgs, checkSeal, engineEnv, oldPinRecord } from "./pi-seal.mjs"; -import { locate } from "../../plugins/harness/pi/binary.mjs"; +import { executable, locate } from "../../plugins/harness/pi/binary.mjs"; +import { sessionEnv } from "../../seat/src/session.mjs"; import { levels } from "../../plugins/harness/pi/index.mjs"; import { piFit } from "../../harness/src/pi-fit.mjs"; import { ACTOR, conversationId, createReader, rootsFromSpecs } from "./reader.mjs"; @@ -159,6 +160,23 @@ export function sessionTools(parsed) { // whose last part is node, with nothing after it. const NODE_SHEBANG = /^#![ \t]*(?:\/usr\/bin\/env[ \t]+node|(\/[^\s]*\/node))[ \t]*$/; const ANSWER_KEYS = ["root", "dest", "view", "pkg"]; +const ANSWER_FIELDS = ["bin", "label", "problem", ...ANSWER_KEYS]; + +// #1873 S4: the lookup's answer, copied once into a frozen plain object of +// its own data fields, so what the seal checks and what the fit check reads +// are the same values for the controller's life. A getter, a Proxy that +// answers differently later, or an inherited field changes nothing after +// this copy. +function answerCopy(pi) { + if (!pi || typeof pi !== "object") return pi; + const out = {}; + for (const k of ANSWER_FIELDS) { + let d; + try { d = Object.getOwnPropertyDescriptor(pi, k); } catch { d = undefined; } + if (d && "value" in d && (typeof d.value === "string" || d.value === null)) out[k] = d.value; + } + return Object.freeze(out); +} export class Controller { constructor(opts = {}) { @@ -192,7 +210,7 @@ export class Controller { const test = opts[TEST_ENGINE] ?? null; // #1873 S4: the pi is binary.mjs's answer ({ bin, label, ... } or // { bin: null, problem }). A sealed launch runs its bin's real file. - this.pi = pi; + this.pi = answerCopy(pi); this.engine = { sealed: test === null, command: test ? test.command : process.execPath, @@ -280,7 +298,7 @@ export class Controller { 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"; + const label = this.#label(); let real; try { real = realpathSync(pi.bin); @@ -313,7 +331,7 @@ export class Controller { } finally { if (fd !== null) try { closeSync(fd); } catch {} } - const label = this.pi.label ?? "the pi"; + const label = this.#label(); // The kernel reads at most 256 bytes for the first line; one that doesn't // end inside them isn't run as a #! line at all. const nl = head.indexOf("\n"); @@ -327,7 +345,36 @@ export class Controller { if (interp === null || interp !== realpathSync(process.execPath)) { throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs another node than the engine's`); } + return; } + // env takes node from the fit check's PATH, the one a bus session gets + // (sessionEnv: the repository's .bin, then this process's PATH). The + // first node on it must be this one, and no node on it may come from the + // package the fit check mounts. + const engineNode = realpathSync(process.execPath); + const realOrNull = (p) => { try { return realpathSync(p); } catch { return null; } }; + const root = realOrNull(this.pi.root) ?? this.pi.root; + let first = null; + for (const dir of String(sessionEnv(process.env, "fit").PATH ?? "").split(":")) { + if (!dir.startsWith("/")) continue; + const candidate = join(dir, "node"); + if (!executable(candidate)) continue; + const real = realOrNull(candidate); + if (real === null) continue; + if (real === root || real.startsWith(root + "/")) { + throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s package puts a node on the fit check's PATH`); + } + if (first === null) first = real; + } + if (first !== engineNode) { + throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs the first node on the fit check's PATH, which isn't the engine's`); + } + } + + // The name a refusal line uses for the pi: the lookup's label, a string. + #label() { + const label = this.pi?.label; + return typeof label === "string" && label ? label : "the pi"; } // The seal covers the command: unless a test engine was given, the launch @@ -335,7 +382,7 @@ export class Controller { // again at each check, and nothing else. #checkSeal() { if (this.engine.sealed && (this.engine.command !== process.execPath || this.engine.preArgs.length !== 1 || this.engine.preArgs[0] !== this.#piFile())) { - throw new ControlRefusal(UNSEALED_ENGINE, `the engine command is not ${this.pi?.label ?? "the pi"} the lookup found`); + throw new ControlRefusal(UNSEALED_ENGINE, `the engine command is not ${this.#label()} the lookup found`); } const bad = this.engine.preArgs.find((a) => typeof a !== "string" || a === "-e" || a === "--extension" || a.startsWith("--extension=")); if (bad !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine pre-arguments carry ${bad}`); diff --git a/packages/conversation/tests/claim.test.mjs b/packages/conversation/tests/claim.test.mjs index 07a788228..f6db03002 100644 --- a/packages/conversation/tests/claim.test.mjs +++ b/packages/conversation/tests/claim.test.mjs @@ -10,6 +10,7 @@ import { homedir, tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; +import { types as utilTypes } from "node:util"; import { ALREADY_ACTIVE, ClaimStore, FOREIGN_HOST, UNSAFE_REPLACEMENT, held, machineId } from "../src/claim.mjs"; import { Controller, REPO_ROOT } from "../src/controller.mjs"; import { ConversationClient } from "../src/client.mjs"; @@ -763,7 +764,7 @@ test("S4f: a pi whose first line doesn't run node is refused engine-feature-miss } }); -test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engine: a package link moved since the lookup, a hand-built answer with another bin, or one field changed by hand", async () => { +test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engine: a package link moved since the lookup, a hand-built answer with another bin, one field changed by hand, a bin no package holds, or a getter; an answer that changes after construction changes nothing", async () => { const fx = fixture(); const good = standInPi(join(fx.base, "pi-good")); const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] }); @@ -806,14 +807,43 @@ test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engin } // The lacking pi's answer with the good package mounted over its path. assert.throws(() => make({ ...lacking.found, root: good.found.root, dest: lacking.found.root }), (e) => e.code === UNSEALED_ENGINE); + // A bin the lookup no longer takes (no package holds it) refuses when the + // controller is built. + const loose = join(fx.base, "loose-pi"); + writeFileSync(loose, "#!/usr/bin/env node\n", { mode: 0o755 }); + assert.throws(() => make({ bin: loose, label: "MOSAIC_PI_BIN" }), (e) => e.code === UNSEALED_ENGINE); + // A getter is not a data field: the copy the controller keeps lacks it. + assert.throws(() => make({ ...good.found, get view() { return good.found.view; } }), (e) => e.code === UNSEALED_ENGINE); + // An answer that changes after the controller is built changes nothing: it + // keeps a frozen plain copy, and the start checks and launches the good pi. + let flipped = false; + const live = new Proxy({ ...good.found }, { + get: (t, k) => (flipped && k in lacking.found ? lacking.found[k] : t[k]), + getOwnPropertyDescriptor: (t, k) => { + const d = Reflect.getOwnPropertyDescriptor(t, k); + return d && flipped && k in lacking.found ? { ...d, value: lacking.found[k] } : d; + }, + }); + const { ctrl: kept, launcher: keptLauncher } = controllerFor(fx, { pi: live }); + try { + flipped = true; + assert.ok(Object.isFrozen(kept.pi)); + assert.equal(utilTypes.isProxy(kept.pi), false); + assert.deepEqual(kept.pi, good.found); + await kept.start(); + assert.equal(keptLauncher.launches.length, 1); + assert.equal(keptLauncher.launches[0].args[0], realpathSync(good.cli)); + } finally { + await kept.close({ killEngine: true }).catch(() => {}); + } // The answer as the lookup gives it still builds. await make(piAt(good.bin)).close(); }); -test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline; a link to the engine's node launches", async () => { +test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, a node from the package or another node first on the fit check's PATH under env; a link to the engine's node launches", async () => { const fx = fixture(); - const refused = async (pi, message) => { - const { ctrl, launcher } = controllerFor(fx, { pi }); + const refused = async (pi, message, f = fx) => { + const { ctrl, launcher } = controllerFor(f, { pi }); try { await assert.rejects(ctrl.start(), (e) => { assert.equal(e.code, ENGINE_FEATURE_MISSING); @@ -860,6 +890,41 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat } finally { await h.close(); } + // #!/usr/bin/env node takes node from the fit check's PATH, which is this + // process's PATH behind the repository's .bin. + const envPi = withFirstLine("pi-env-path", "#!/usr/bin/env node"); + const fx2 = fixture(); + const onPath = async (dir, fn) => { + const saved = process.env.PATH; + process.env.PATH = `${dir}:${saved}`; + try { + await fn(); + } finally { + process.env.PATH = saved; + } + }; + // A node in the package itself, put on PATH. + const pkgBin = join(envPi.pkg, "bin"); + mkdirSync(pkgBin); + writeFileSync(join(pkgBin, "node"), `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 }); + await onPath(pkgBin, () => refused(envPi.found, "MOSAIC_PI_BIN's package puts a node on the fit check's PATH", fx2)); + // Another node ahead of the engine's on PATH. + const otherBin = join(fx.base, "other-bin"); + mkdirSync(otherBin); + writeFileSync(join(otherBin, "node"), `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 }); + await onPath(otherBin, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs the first node on the fit check's PATH, which isn't the engine's", fx2)); + // A link to the engine's own node ahead of it is the same file. + const linkBin = join(fx.base, "link-bin"); + mkdirSync(linkBin); + symlinkSync(process.execPath, join(linkBin, "node")); + await onPath(linkBin, async () => { + const h2 = await started({ fx: fx2, pi: envPi.found }); + try { + assert.equal(h2.launcher.launches.length, 1); + } finally { + await h2.close(); + } + }); }); // ---- W11, W16, W17 ---------------------------------------------------------- -- 2.54.0 From b432f9cd6fb66a3225c072431e8ef05961b3f12b Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 05:36:07 -0600 Subject: [PATCH 5/8] fix(conversation): pi-unpin S4 third re-check: under #!/usr/bin/env node the PATH walk follows what the fit check's sandbox shows (/usr, /etc, the host's /bin, /lib, /lib64 and /sbin, and the engine's node file at its path): a node the sandbox can't show, or reaches through a link it can't show, may not come before the first one it can, and that first one must be the engine's, so the fit check and the launch run the same node (R3-1); an empty or relative PATH entry refuses, since env reads it against the fit check's directory (R3-2); a label that isn't a plain string without a / reads "the pi" in a refusal line; S4h covers each case (#1873, lane pi-unpin-s4) --- packages/conversation/README.md | 15 ++++-- packages/conversation/src/controller.mjs | 59 ++++++++++++++++++---- packages/conversation/tests/claim.test.mjs | 44 ++++++++++++++-- 3 files changed, 99 insertions(+), 19 deletions(-) diff --git a/packages/conversation/README.md b/packages/conversation/README.md index f77b6ce2c..83fca7a57 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -325,10 +325,17 @@ can disagree with it. `engine-feature-missing`. So does a first line that doesn't end within the 256 bytes the kernel reads, or one with a CR before its newline. Under `#!/usr/bin/env node`, env takes node from the fit check's PATH (the - repository's `.bin`, then this process's PATH, as `sessionEnv` builds it): - the first `node` on it must resolve to this Node, and none on it may lie - inside the package the fit check mounts, or the start refuses - `engine-feature-missing`. + repository's `.bin`, then this process's PATH, as `sessionEnv` builds it), + and it looks inside the fit check's sandbox, which shows only `/usr`, + `/etc`, the host's `/bin`, `/lib`, `/lib64` and `/sbin`, and this Node's + own file at its path. Every entry must be absolute, since env reads an + empty or relative one against the fit check's directory. No `node` on it + may lie inside the package the fit check mounts. No `node` the sandbox + can't show (or reaches through a link it can't show) may come before the + first one it can, and that first one must be this Node. Otherwise the + start refuses `engine-feature-missing`. A refusal line names the pi by the + lookup's label, or "the pi" when the label isn't a plain string without a + `/`. The controller keeps a frozen plain copy of the lookup's answer, its own string data fields only, so an answer that changes later changes nothing. That copy must still be the lookup's at each check: the lookup run again on diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index 5eea54eab..4c14d753c 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -16,7 +16,7 @@ // as in CHAT-01. With no verifier nothing verifies, and every stop ends // `uncertain`. -import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, realpathSync, unlinkSync } from "node:fs"; +import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readlinkSync, realpathSync, unlinkSync } from "node:fs"; import { createServer } from "node:net"; import { basename, dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; @@ -162,6 +162,34 @@ const NODE_SHEBANG = /^#![ \t]*(?:\/usr\/bin\/env[ \t]+node|(\/[^\s]*\/node))[ \ const ANSWER_KEYS = ["root", "dest", "view", "pkg"]; const ANSWER_FIELDS = ["bin", "label", "problem", ...ANSWER_KEYS]; +// #1873 S4: what the fit check's sandbox (systemBinds in seat/src/sandbox.mjs) +// shows as the host has it: /usr, /etc and the host's /bin, /lib, /lib64 and +// /sbin, and this node's own file at process.execPath. shownInFit follows p +// one part at a time as the sandbox would and gives the file it reaches, or +// null when a link or the file lies anywhere else. Null may be too strict (the +// package's own mount is left out on purpose: no node may come from it), never +// too loose. +const SHOWN_DIRS = ["/usr", "/etc", "/bin", "/lib", "/lib64", "/sbin"]; +const shownDir = (p) => SHOWN_DIRS.some((d) => p === d || p.startsWith(d + "/")); +function shownInFit(p, hops = 0) { + if (hops > 40) return null; + const parts = p.split("/").filter(Boolean); + let at = "/"; + for (let i = 0; i < parts.length; i++) { + const next = join(at, parts[i]); + let st; + try { st = lstatSync(next); } catch { return null; } + if (st.isSymbolicLink()) { + if (!shownDir(next)) return null; + let target; + try { target = readlinkSync(next); } catch { return null; } + return shownInFit(resolve(at, target, ...parts.slice(i + 1)), hops + 1); + } + at = next; + } + return at === process.execPath || shownDir(at) ? at : null; +} + // #1873 S4: the lookup's answer, copied once into a frozen plain object of // its own data fields, so what the seal checks and what the fit check reads // are the same values for the controller's life. A getter, a Proxy that @@ -348,33 +376,44 @@ export class Controller { return; } // env takes node from the fit check's PATH, the one a bus session gets - // (sessionEnv: the repository's .bin, then this process's PATH). The - // first node on it must be this one, and no node on it may come from the - // package the fit check mounts. + // (sessionEnv: the repository's .bin, then this process's PATH), and it + // looks inside the sandbox, which shows only some of the host. Every entry + // must be absolute: env would read an empty or relative one against the + // fit check's own directory. No node on it may come from the package the + // fit check mounts. No node the sandbox can't show may come before the + // first one it can, since the host and the sandbox would then pick + // different files. That first one must be this node. const engineNode = realpathSync(process.execPath); const realOrNull = (p) => { try { return realpathSync(p); } catch { return null; } }; const root = realOrNull(this.pi.root) ?? this.pi.root; let first = null; for (const dir of String(sessionEnv(process.env, "fit").PATH ?? "").split(":")) { - if (!dir.startsWith("/")) continue; - const candidate = join(dir, "node"); + if (!dir.startsWith("/")) { + throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs node from the fit check's PATH, which has an empty or relative entry`); + } + const candidate = resolve(dir, "node"); if (!executable(candidate)) continue; const real = realOrNull(candidate); if (real === null) continue; if (real === root || real.startsWith(root + "/")) { throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s package puts a node on the fit check's PATH`); } - if (first === null) first = real; + if (first !== null) continue; + first = shownInFit(candidate); + if (first === null) { + throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs node from the fit check's PATH, where a node the fit check can't see comes first`); + } } - if (first !== engineNode) { + if (first === null || realOrNull(first) !== engineNode) { throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs the first node on the fit check's PATH, which isn't the engine's`); } } - // The name a refusal line uses for the pi: the lookup's label, a string. + // The name a refusal line uses for the pi: the lookup's label, a string + // that names a step, never a path; anything else is "the pi". #label() { const label = this.pi?.label; - return typeof label === "string" && label ? label : "the pi"; + return typeof label === "string" && label && !label.includes("/") ? label : "the pi"; } // The seal covers the command: unless a test engine was given, the launch diff --git a/packages/conversation/tests/claim.test.mjs b/packages/conversation/tests/claim.test.mjs index f6db03002..0315c3d25 100644 --- a/packages/conversation/tests/claim.test.mjs +++ b/packages/conversation/tests/claim.test.mjs @@ -840,7 +840,7 @@ test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engin await make(piAt(good.bin)).close(); }); -test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, a node from the package or another node first on the fit check's PATH under env; a link to the engine's node launches", async () => { +test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, under env a node from the package, a node the fit check's sandbox can't see ahead of the first it can (even a link to the engine's node), an empty or relative PATH entry, or a first shown node that isn't the engine's; the engine's own directory first launches, and a label that is a path reads \"the pi\"", async () => { const fx = fixture(); const refused = async (pi, message, f = fx) => { const { ctrl, launcher } = controllerFor(f, { pi }); @@ -908,23 +908,57 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat mkdirSync(pkgBin); writeFileSync(join(pkgBin, "node"), `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 }); await onPath(pkgBin, () => refused(envPi.found, "MOSAIC_PI_BIN's package puts a node on the fit check's PATH", fx2)); - // Another node ahead of the engine's on PATH. + // The fit check's sandbox shows only /usr, /etc, the host's /bin, /lib, + // /lib64 and /sbin, and the engine's own node file. A node in any other + // directory ahead of the engine's: env in the sandbox would pass it by, + // while the host finds it first. Even a link to the engine's node. const otherBin = join(fx.base, "other-bin"); mkdirSync(otherBin); writeFileSync(join(otherBin, "node"), `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 }); - await onPath(otherBin, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs the first node on the fit check's PATH, which isn't the engine's", fx2)); - // A link to the engine's own node ahead of it is the same file. + const hidden = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where a node the fit check can't see comes first"; + await onPath(otherBin, () => refused(envPi.found, hidden, fx2)); const linkBin = join(fx.base, "link-bin"); mkdirSync(linkBin); symlinkSync(process.execPath, join(linkBin, "node")); - await onPath(linkBin, async () => { + await onPath(linkBin, () => refused(envPi.found, hidden, fx2)); + // env reads an empty or relative entry against the fit check's directory. + const relative = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an empty or relative entry"; + await onPath("", () => refused(envPi.found, relative, fx2)); + await onPath("some/bin", () => refused(envPi.found, relative, fx2)); + // The first node the sandbox shows must be the engine's: no node at all + // isn't, and neither is a system node that is another file. + const notEngine = "MOSAIC_PI_BIN's first line runs the first node on the fit check's PATH, which isn't the engine's"; + const withPath = async (path, fn) => { + const saved = process.env.PATH; + process.env.PATH = path; + try { + await fn(); + } finally { + process.env.PATH = saved; + } + }; + await withPath("/usr/share", () => refused(envPi.found, notEngine, fx2)); + const systemNode = ["/usr/bin/node", "/usr/local/bin/node"].find((n) => { + try { + return realpathSync(n) !== realpathSync(process.execPath); + } catch { + return false; + } + }); + if (systemNode) await withPath(`${dirname(systemNode)}:${dirname(process.execPath)}`, () => refused(envPi.found, notEngine, fx2)); + // The engine's own directory first: the sandbox shows that file at the same + // path, so env runs the engine's node in both. + await withPath(`${dirname(process.execPath)}:/usr/bin:/bin`, async () => { const h2 = await started({ fx: fx2, pi: envPi.found }); try { assert.equal(h2.launcher.launches.length, 1); + assert.equal(h2.launcher.launches[0].args[0], realpathSync(envPi.cli)); } finally { await h2.close(); } }); + // A hand-built label that is a path never reaches a refusal line. + await refused({ ...shim.found, label: join(fx.base, "named") }, "the pi's first line runs another node than the engine's"); }); // ---- W11, W16, W17 ---------------------------------------------------------- -- 2.54.0 From 83250ef36b09566853f1c9c2610515109678d797 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 05:53:43 -0600 Subject: [PATCH 6/8] fix(conversation): pi-unpin S4 fourth re-check: under #!/usr/bin/env node, up to the first node every fit PATH entry must read the same in the sandbox as on the host: in plain form, so a .. after a link can't point elsewhere, and not in /proc, /dev or /tmp, which the sandbox makes its own, by its spelling or by the real path of the nearest part of it that exists; an entry or a node link the host can't check refuses, and entries after the first node are not judged (R4-1); the fit check gets the controller's label, which must be words like the lookup's, so no refusal line carries a hand-built label with a path or a newline (R4-2); S4h covers each case (#1873, lane pi-unpin-s4) --- packages/conversation/README.md | 21 ++++--- packages/conversation/src/controller.mjs | 64 ++++++++++++++++------ packages/conversation/tests/claim.test.mjs | 39 ++++++++++++- 3 files changed, 95 insertions(+), 29 deletions(-) diff --git a/packages/conversation/README.md b/packages/conversation/README.md index 83fca7a57..21f8c8ce1 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -328,14 +328,19 @@ can disagree with it. repository's `.bin`, then this process's PATH, as `sessionEnv` builds it), and it looks inside the fit check's sandbox, which shows only `/usr`, `/etc`, the host's `/bin`, `/lib`, `/lib64` and `/sbin`, and this Node's - own file at its path. Every entry must be absolute, since env reads an - empty or relative one against the fit check's directory. No `node` on it - may lie inside the package the fit check mounts. No `node` the sandbox - can't show (or reaches through a link it can't show) may come before the - first one it can, and that first one must be this Node. Otherwise the - start refuses `engine-feature-missing`. A refusal line names the pi by the - lookup's label, or "the pi" when the label isn't a plain string without a - `/`. + own file at its path. Up to the first `node`, every entry must read the + same in the sandbox as on the host: absolute (env reads an empty or + relative one against the fit check's directory), already in plain form (a + `..` after a link goes where the link points), and not in `/proc`, `/dev` + or `/tmp`, which the sandbox makes its own, by its spelling or by the real + path of the nearest part of it that exists. An entry or a `node` link + there that can't be checked refuses too. No `node` the sandbox can't show + (or reaches through a link it can't show) may come before the first one it + can, and that first one must be this Node. No `node` anywhere on the PATH + may lie inside the package the fit check mounts. Otherwise the start + refuses `engine-feature-missing`. A refusal line, the fit check's + included, names the pi by the lookup's label, which is words such as + `MOSAIC_PI_BIN`, or "the pi" for any other label. The controller keeps a frozen plain copy of the lookup's answer, its own string data fields only, so an answer that changes later changes nothing. That copy must still be the lookup's at each check: the lookup run again on diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index 4c14d753c..c027c832d 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -16,7 +16,7 @@ // as in CHAT-01. With no verifier nothing verifies, and every stop ends // `uncertain`. -import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readlinkSync, realpathSync, unlinkSync } from "node:fs"; +import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readlinkSync, realpathSync, statSync, unlinkSync } from "node:fs"; import { createServer } from "node:net"; import { basename, dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; @@ -171,6 +171,9 @@ const ANSWER_FIELDS = ["bin", "label", "problem", ...ANSWER_KEYS]; // too loose. const SHOWN_DIRS = ["/usr", "/etc", "/bin", "/lib", "/lib64", "/sbin"]; const shownDir = (p) => SHOWN_DIRS.some((d) => p === d || p.startsWith(d + "/")); +// The sandbox makes its own /proc, /dev and /tmp, so a path there names +// something else inside it than on the host. +const OWN_COPY_DIRS = ["/proc", "/dev", "/tmp"]; function shownInFit(p, hops = 0) { if (hops > 40) return null; const parts = p.split("/").filter(Boolean); @@ -377,43 +380,68 @@ export class Controller { } // env takes node from the fit check's PATH, the one a bus session gets // (sessionEnv: the repository's .bin, then this process's PATH), and it - // looks inside the sandbox, which shows only some of the host. Every entry - // must be absolute: env would read an empty or relative one against the - // fit check's own directory. No node on it may come from the package the - // fit check mounts. No node the sandbox can't show may come before the - // first one it can, since the host and the sandbox would then pick - // different files. That first one must be this node. + // looks inside the sandbox, which shows only some of the host. Up to the + // first node, every entry must read the same in both: absolute, already + // in plain form (a ".." after a link goes where the link points, not + // where the text does), and nowhere the sandbox gives its own copy of + // (/proc, /dev, /tmp), in its spelling or its real path. An entry or a + // node there that can't be checked refuses. No node the sandbox can't show + // may come first, and the first one must be this node. No node on the + // PATH may come from the package the fit check mounts. const engineNode = realpathSync(process.execPath); const realOrNull = (p) => { try { return realpathSync(p); } catch { return null; } }; const root = realOrNull(this.pi.root) ?? this.pi.root; + const refuse = (why) => { throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs node from the fit check's PATH, ${why}`); }; + const missing = (e) => e?.code === "ENOENT" || e?.code === "ENOTDIR"; + const ownCopy = (p) => OWN_COPY_DIRS.some((d) => p === d || p.startsWith(d + "/")); let first = null; for (const dir of String(sessionEnv(process.env, "fit").PATH ?? "").split(":")) { - if (!dir.startsWith("/")) { - throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs node from the fit check's PATH, which has an empty or relative entry`); + if (first === null) { + if (!dir.startsWith("/")) refuse("which has an empty or relative entry"); + const plain = dir.length > 1 ? dir.replace(/\/+$/, "") : dir; + if (resolve(plain) !== plain) refuse("which has an entry that isn't in plain form"); + const own = "which has an entry the fit check's sandbox has its own copy of"; + if (ownCopy(plain)) refuse(own); + // An entry that doesn't exist is judged by the real path of the + // nearest part of it that does. + let realDir = null; + for (let at = plain; realDir === null; at = dirname(at)) { + try { realDir = realpathSync(at); } catch (e) { if (!missing(e) || at === "/") refuse("where an entry can't be checked"); } + } + if (ownCopy(realDir)) refuse(own); + const candidate = join(plain, "node"); + try { lstatSync(candidate); } catch (e) { if (missing(e)) continue; refuse("where an entry can't be checked"); } + let held = null; + try { held = statSync(candidate); } catch { refuse("where a node can't be checked"); } + if (!held.isFile() || !executable(candidate)) continue; + } else if (!dir.startsWith("/") || !executable(resolve(dir, "node"))) { + continue; } const candidate = resolve(dir, "node"); - if (!executable(candidate)) continue; const real = realOrNull(candidate); - if (real === null) continue; + if (real === null) { + if (first === null) refuse("where a node can't be checked"); + continue; + } if (real === root || real.startsWith(root + "/")) { throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s package puts a node on the fit check's PATH`); } if (first !== null) continue; first = shownInFit(candidate); - if (first === null) { - throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs node from the fit check's PATH, where a node the fit check can't see comes first`); - } + if (first === null) refuse("where a node the fit check can't see comes first"); } if (first === null || realOrNull(first) !== engineNode) { throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs the first node on the fit check's PATH, which isn't the engine's`); } } - // The name a refusal line uses for the pi: the lookup's label, a string - // that names a step, never a path; anything else is "the pi". + // The name a refusal line uses for the pi: the lookup's label, which names + // a step in words ("MOSAIC_PI_BIN", "the pi on PATH", "the repository's + // pi"), never a path. Anything else, such as a hand-built answer's label + // with a / or a newline in it, is "the pi". #label() { const label = this.pi?.label; - return typeof label === "string" && label && !label.includes("/") ? label : "the pi"; + return typeof label === "string" && /^[A-Za-z_' ]{1,64}$/.test(label) ? label : "the pi"; } // The seal covers the command: unless a test engine was given, the launch @@ -435,7 +463,7 @@ export class Controller { // enginePin the digest. --no-mcp joins the seal when the help lists it. async #pins() { this.#nodeScript(this.#piFile()); - const fit = await piFit(this.pi, { levels: levels() }); + const fit = await piFit({ ...this.pi, label: this.#label() }, { 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"); this.noMcp = fit.noMcp === true; diff --git a/packages/conversation/tests/claim.test.mjs b/packages/conversation/tests/claim.test.mjs index 0315c3d25..ad751454b 100644 --- a/packages/conversation/tests/claim.test.mjs +++ b/packages/conversation/tests/claim.test.mjs @@ -5,7 +5,7 @@ import { test, after } from "node:test"; import assert from "node:assert/strict"; -import { appendFileSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs"; +import { appendFileSync, chmodSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs"; import { homedir, tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { spawnSync } from "node:child_process"; @@ -840,7 +840,7 @@ test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engin await make(piAt(good.bin)).close(); }); -test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, under env a node from the package, a node the fit check's sandbox can't see ahead of the first it can (even a link to the engine's node), an empty or relative PATH entry, or a first shown node that isn't the engine's; the engine's own directory first launches, and a label that is a path reads \"the pi\"", async () => { +test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, under env a node from the package, a node the fit check's sandbox can't see ahead of the first it can (even a link to the engine's node), an empty, relative or unplain entry before the first node, one in /proc, /dev or /tmp, one that can't be checked, or a first shown node that isn't the engine's; the engine's own directory first launches, and a label that isn't the lookup's kind reads \"the pi\", in the fit check's line too", async () => { const fx = fixture(); const refused = async (pi, message, f = fx) => { const { ctrl, launcher } = controllerFor(f, { pi }); @@ -925,6 +925,33 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat const relative = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an empty or relative entry"; await onPath("", () => refused(envPi.found, relative, fx2)); await onPath("some/bin", () => refused(envPi.found, relative, fx2)); + // Up to the first node, an entry must read the same in the sandbox as on + // the host. A ".." after a link goes where the link points, not where the + // text does. + symlinkSync("/usr/lib", join(fx.base, "sys")); + await onPath(`${fx.base}/sys/../bin`, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an entry that isn't in plain form", fx2)); + // The sandbox makes its own /proc, /dev and /tmp: pid 1 there is its own, + // and a link into /proc counts too, even past a pid the host lacks. + const ownCopy = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an entry the fit check's sandbox has its own copy of"; + await onPath("/proc/1/root/usr/bin", () => refused(envPi.found, ownCopy, fx2)); + await onPath("/tmp/mosaic-no-such-dir", () => refused(envPi.found, ownCopy, fx2)); + symlinkSync("/proc", join(fx.base, "p")); + await onPath(`${fx.base}/p/999999999/root/usr/bin`, () => refused(envPi.found, ownCopy, fx2)); + // A node link that leads nowhere on the host may lead somewhere in the + // sandbox (into its /tmp, say), so it can't be checked. + const dangling = join(fx.base, "dangling-bin"); + mkdirSync(dangling); + symlinkSync("/tmp/mosaic-no-such-dir/node", join(dangling, "node")); + await onPath(dangling, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where a node can't be checked", fx2)); + // An entry the host can't read can't be checked. + const locked = join(fx.base, "locked"); + mkdirSync(join(locked, "bin"), { recursive: true }); + chmodSync(locked, 0o000); + try { + await onPath(join(locked, "bin"), () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where an entry can't be checked", fx2)); + } finally { + chmodSync(locked, 0o755); + } // The first node the sandbox shows must be the engine's: no node at all // isn't, and neither is a system node that is another file. const notEngine = "MOSAIC_PI_BIN's first line runs the first node on the fit check's PATH, which isn't the engine's"; @@ -948,7 +975,9 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat if (systemNode) await withPath(`${dirname(systemNode)}:${dirname(process.execPath)}`, () => refused(envPi.found, notEngine, fx2)); // The engine's own directory first: the sandbox shows that file at the same // path, so env runs the engine's node in both. - await withPath(`${dirname(process.execPath)}:/usr/bin:/bin`, async () => { + // Entries after it are never read, so a trailing slash on it and an empty + // entry at the end change nothing. + await withPath(`${dirname(process.execPath)}/:/usr/bin:/bin:`, async () => { const h2 = await started({ fx: fx2, pi: envPi.found }); try { assert.equal(h2.launcher.launches.length, 1); @@ -959,6 +988,10 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat }); // A hand-built label that is a path never reaches a refusal line. await refused({ ...shim.found, label: join(fx.base, "named") }, "the pi's first line runs another node than the engine's"); + await refused({ ...shim.found, label: "MOSAIC_PI_BIN\nrefused: forged" }, "the pi's first line runs another node than the engine's"); + // Nor the fit check's own line. + const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] }); + await refused({ ...lacking.found, label: join(fx.base, "named") }, "pi 9.9.9 (the pi) lacks --mode rpc"); }); // ---- W11, W16, W17 ---------------------------------------------------------- -- 2.54.0 From c065034a6e5cd2a68d391d88df9df22e4cd0be76 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 06:13:38 -0600 Subject: [PATCH 7/8] fix(conversation): pi-unpin S4 fifth re-check: the own-copy rule for fit PATH entries covers /proc and /dev only; the sandbox's /tmp starts empty (pi-fit.mjs makes it fresh for each run), so a /tmp entry is judged like any directory the sandbox doesn't show, and S4h and a checkout under /tmp no longer refuse for it (R5-1); a lookup's problem line with a path or a control character in it reads "the pi lookup gave no binary"; S4h covers a /dev entry, a /tmp entry ahead of the engine's, and lookup lines (#1873, lane pi-unpin-s4) --- packages/conversation/README.md | 14 +++++++---- packages/conversation/src/controller.mjs | 19 ++++++++++----- packages/conversation/tests/claim.test.mjs | 28 +++++++++++++++++----- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/packages/conversation/README.md b/packages/conversation/README.md index 21f8c8ce1..fc086c48e 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -331,16 +331,20 @@ can disagree with it. own file at its path. Up to the first `node`, every entry must read the same in the sandbox as on the host: absolute (env reads an empty or relative one against the fit check's directory), already in plain form (a - `..` after a link goes where the link points), and not in `/proc`, `/dev` - or `/tmp`, which the sandbox makes its own, by its spelling or by the real - path of the nearest part of it that exists. An entry or a `node` link - there that can't be checked refuses too. No `node` the sandbox can't show + `..` after a link goes where the link points), and not in `/proc` or + `/dev`, which the sandbox makes its own, by its spelling or by the real + path of the nearest part of it that resolves. The sandbox's `/tmp` is its + own too, but starts empty, so a `/tmp` entry is judged like any other + directory the sandbox doesn't show. An entry or a `node` link there that + can't be checked refuses too. No `node` the sandbox can't show (or reaches through a link it can't show) may come before the first one it can, and that first one must be this Node. No `node` anywhere on the PATH may lie inside the package the fit check mounts. Otherwise the start refuses `engine-feature-missing`. A refusal line, the fit check's included, names the pi by the lookup's label, which is words such as - `MOSAIC_PI_BIN`, or "the pi" for any other label. + `MOSAIC_PI_BIN`, or "the pi" for any other label. A lookup that found no + pi refuses with its own line, or "the pi lookup gave no binary" when that + line holds a path or a control character. The controller keeps a frozen plain copy of the lookup's answer, its own string data fields only, so an answer that changes later changes nothing. That copy must still be the lookup's at each check: the lookup run again on diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index c027c832d..81fb4a04a 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -31,7 +31,7 @@ import { LiveSessionGuard, realPath } from "./guard.mjs"; import { ID, fragments, safeId } from "./parts.mjs"; import { parseSnapshot } from "./pi.mjs"; import { ENGINE_FEATURE_MISSING, ENGINE_PIN_MISMATCH, OLD_PIN_LINE, UNSEALED_ENGINE, argvDigest, buildPiArgs, checkSeal, engineEnv, oldPinRecord } from "./pi-seal.mjs"; -import { executable, locate } from "../../plugins/harness/pi/binary.mjs"; +import { PACKAGE, executable, locate } from "../../plugins/harness/pi/binary.mjs"; import { sessionEnv } from "../../seat/src/session.mjs"; import { levels } from "../../plugins/harness/pi/index.mjs"; import { piFit } from "../../harness/src/pi-fit.mjs"; @@ -171,9 +171,12 @@ const ANSWER_FIELDS = ["bin", "label", "problem", ...ANSWER_KEYS]; // too loose. const SHOWN_DIRS = ["/usr", "/etc", "/bin", "/lib", "/lib64", "/sbin"]; const shownDir = (p) => SHOWN_DIRS.some((d) => p === d || p.startsWith(d + "/")); -// The sandbox makes its own /proc, /dev and /tmp, so a path there names -// something else inside it than on the host. -const OWN_COPY_DIRS = ["/proc", "/dev", "/tmp"]; +// The sandbox makes its own /proc and /dev, so a path there names something +// else inside it than on the host. Its /tmp is another directory too, but one +// that starts empty (pi-fit.mjs makes it fresh for each run), so an entry +// there holds no node in the sandbox, and a node the host finds there is one +// the sandbox can't show. +const OWN_COPY_DIRS = ["/proc", "/dev"]; function shownInFit(p, hops = 0) { if (hops > 40) return null; const parts = p.split("/").filter(Boolean); @@ -326,7 +329,11 @@ export class Controller { #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"; + // The lookup's problem lines are words and its package's name; a + // hand-built one with a path or a control character in it isn't shown. + const problem = pi?.problem; + const plain = typeof problem === "string" && problem && !/[\x00-\x1f\x7f]/.test(problem) && !problem.replaceAll(PACKAGE, "").includes("/"); + const why = plain ? problem : "the pi lookup gave no binary"; throw new ControlRefusal(ENGINE_FEATURE_MISSING, why); } const label = this.#label(); @@ -384,7 +391,7 @@ export class Controller { // first node, every entry must read the same in both: absolute, already // in plain form (a ".." after a link goes where the link points, not // where the text does), and nowhere the sandbox gives its own copy of - // (/proc, /dev, /tmp), in its spelling or its real path. An entry or a + // (/proc, /dev), in its spelling or its real path. An entry or a // node there that can't be checked refuses. No node the sandbox can't show // may come first, and the first one must be this node. No node on the // PATH may come from the package the fit check mounts. diff --git a/packages/conversation/tests/claim.test.mjs b/packages/conversation/tests/claim.test.mjs index ad751454b..2ffa0b4a7 100644 --- a/packages/conversation/tests/claim.test.mjs +++ b/packages/conversation/tests/claim.test.mjs @@ -840,7 +840,7 @@ test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engin await make(piAt(good.bin)).close(); }); -test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, under env a node from the package, a node the fit check's sandbox can't see ahead of the first it can (even a link to the engine's node), an empty, relative or unplain entry before the first node, one in /proc, /dev or /tmp, one that can't be checked, or a first shown node that isn't the engine's; the engine's own directory first launches, and a label that isn't the lookup's kind reads \"the pi\", in the fit check's line too", async () => { +test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, under env a node from the package, a node the fit check's sandbox can't see ahead of the first it can (even a link to the engine's node), an empty, relative or unplain entry before the first node, one in /proc or /dev, one that can't be checked, or a first shown node that isn't the engine's; the engine's own directory first launches, and a label that isn't the lookup's kind reads \"the pi\", in the fit check's line too; a lookup's problem line shows, a hand-built one with a path or a newline doesn't", async () => { const fx = fixture(); const refused = async (pi, message, f = fx) => { const { ctrl, launcher } = controllerFor(f, { pi }); @@ -930,11 +930,11 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat // text does. symlinkSync("/usr/lib", join(fx.base, "sys")); await onPath(`${fx.base}/sys/../bin`, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an entry that isn't in plain form", fx2)); - // The sandbox makes its own /proc, /dev and /tmp: pid 1 there is its own, - // and a link into /proc counts too, even past a pid the host lacks. + // The sandbox makes its own /proc and /dev: pid 1 there is its own, and a + // link into /proc counts too, even past a pid the host lacks. const ownCopy = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an entry the fit check's sandbox has its own copy of"; await onPath("/proc/1/root/usr/bin", () => refused(envPi.found, ownCopy, fx2)); - await onPath("/tmp/mosaic-no-such-dir", () => refused(envPi.found, ownCopy, fx2)); + await onPath("/dev/mosaic-no-such-dir", () => refused(envPi.found, ownCopy, fx2)); symlinkSync("/proc", join(fx.base, "p")); await onPath(`${fx.base}/p/999999999/root/usr/bin`, () => refused(envPi.found, ownCopy, fx2)); // A node link that leads nowhere on the host may lead somewhere in the @@ -976,8 +976,9 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat // The engine's own directory first: the sandbox shows that file at the same // path, so env runs the engine's node in both. // Entries after it are never read, so a trailing slash on it and an empty - // entry at the end change nothing. - await withPath(`${dirname(process.execPath)}/:/usr/bin:/bin:`, async () => { + // entry at the end change nothing. The sandbox's /tmp starts empty, so a + // /tmp entry with no node on the host holds none there either. + await withPath(`/tmp/mosaic-no-such-dir:${dirname(process.execPath)}/:/usr/bin:/bin:`, async () => { const h2 = await started({ fx: fx2, pi: envPi.found }); try { assert.equal(h2.launcher.launches.length, 1); @@ -992,6 +993,21 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat // Nor the fit check's own line. const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] }); await refused({ ...lacking.found, label: join(fx.base, "named") }, "pi 9.9.9 (the pi) lacks --mode rpc"); + // A lookup that found no pi refuses when the controller is built: its own + // line, which names the step and the package, never a path; a hand-built + // line with a path or a newline isn't shown. + const unbuilt = (pi, message) => + assert.throws(() => controllerFor(fx, { pi }), (e) => { + assert.equal(e.code, ENGINE_FEATURE_MISSING); + assert.equal(e.message, message); + return true; + }); + unbuilt(piAt("relative/pi"), "MOSAIC_PI_BIN isn't an executable file at an absolute path"); + const loose = join(fx.base, "loose-pi"); + writeFileSync(loose, "#!/usr/bin/env node\n", { mode: 0o755 }); + unbuilt(piAt(loose), `MOSAIC_PI_BIN isn't part of the ${PI_PACKAGE} package`); + unbuilt({ bin: null, problem: `${fx.base} has no pi` }, "the pi lookup gave no binary"); + unbuilt({ bin: null, problem: "no pi\nrefused: forged" }, "the pi lookup gave no binary"); }); // ---- W11, W16, W17 ---------------------------------------------------------- -- 2.54.0 From 4a3f501e13eb250b2225f2cb73550fd1b781007f Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 06:30:10 -0600 Subject: [PATCH 8/8] fix(conversation): pi-unpin S4 sixth re-check: an engine node under /tmp no longer counts as one the fit check's sandbox shows, since pi-fit.mjs puts the fresh /tmp over it after it mounts that file, so a PATH with that node first refuses instead of letting the fit check run another node (R6-1); a lookup's problem line with a C1 control or a line or paragraph separator reads "the pi lookup gave no binary"; README and a test comment match (#1873, lane pi-unpin-s4) --- packages/conversation/README.md | 3 ++- packages/conversation/src/controller.mjs | 9 ++++++--- packages/conversation/tests/claim.test.mjs | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/conversation/README.md b/packages/conversation/README.md index fc086c48e..ce3c914df 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -328,7 +328,8 @@ can disagree with it. repository's `.bin`, then this process's PATH, as `sessionEnv` builds it), and it looks inside the fit check's sandbox, which shows only `/usr`, `/etc`, the host's `/bin`, `/lib`, `/lib64` and `/sbin`, and this Node's - own file at its path. Up to the first `node`, every entry must read the + own file at its path unless that path is under `/tmp` (the sandbox's fresh + `/tmp` covers it). Up to the first `node`, every entry must read the same in the sandbox as on the host: absolute (env reads an empty or relative one against the fit check's directory), already in plain form (a `..` after a link goes where the link points), and not in `/proc` or diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index 81fb4a04a..e1cc36068 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -175,8 +175,11 @@ const shownDir = (p) => SHOWN_DIRS.some((d) => p === d || p.startsWith(d + "/")) // else inside it than on the host. Its /tmp is another directory too, but one // that starts empty (pi-fit.mjs makes it fresh for each run), so an entry // there holds no node in the sandbox, and a node the host finds there is one -// the sandbox can't show. +// the sandbox can't show. That holds for this Node's own file too: pi-fit.mjs +// puts the fresh /tmp in place after it mounts that file, so an engine whose +// node sits under /tmp is hidden in the sandbox. const OWN_COPY_DIRS = ["/proc", "/dev"]; +const underTmp = (p) => p === "/tmp" || p.startsWith("/tmp/"); function shownInFit(p, hops = 0) { if (hops > 40) return null; const parts = p.split("/").filter(Boolean); @@ -193,7 +196,7 @@ function shownInFit(p, hops = 0) { } at = next; } - return at === process.execPath || shownDir(at) ? at : null; + return (at === process.execPath && !underTmp(at)) || shownDir(at) ? at : null; } // #1873 S4: the lookup's answer, copied once into a frozen plain object of @@ -332,7 +335,7 @@ export class Controller { // The lookup's problem lines are words and its package's name; a // hand-built one with a path or a control character in it isn't shown. const problem = pi?.problem; - const plain = typeof problem === "string" && problem && !/[\x00-\x1f\x7f]/.test(problem) && !problem.replaceAll(PACKAGE, "").includes("/"); + const plain = typeof problem === "string" && problem && !/[\x00-\x1f\x7f-\x9f\u2028\u2029]/.test(problem) && !problem.replaceAll(PACKAGE, "").includes("/"); const why = plain ? problem : "the pi lookup gave no binary"; throw new ControlRefusal(ENGINE_FEATURE_MISSING, why); } diff --git a/packages/conversation/tests/claim.test.mjs b/packages/conversation/tests/claim.test.mjs index 2ffa0b4a7..70575877f 100644 --- a/packages/conversation/tests/claim.test.mjs +++ b/packages/conversation/tests/claim.test.mjs @@ -937,8 +937,8 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat await onPath("/dev/mosaic-no-such-dir", () => refused(envPi.found, ownCopy, fx2)); symlinkSync("/proc", join(fx.base, "p")); await onPath(`${fx.base}/p/999999999/root/usr/bin`, () => refused(envPi.found, ownCopy, fx2)); - // A node link that leads nowhere on the host may lead somewhere in the - // sandbox (into its /tmp, say), so it can't be checked. + // A node link that leads nowhere on the host can't be checked: what the + // sandbox finds at its target need not be what the host lacks there. const dangling = join(fx.base, "dangling-bin"); mkdirSync(dangling); symlinkSync("/tmp/mosaic-no-such-dir/node", join(dangling, "node")); @@ -1008,6 +1008,8 @@ test("S4h: a first line the kernel and node read differently refuses engine-feat unbuilt(piAt(loose), `MOSAIC_PI_BIN isn't part of the ${PI_PACKAGE} package`); unbuilt({ bin: null, problem: `${fx.base} has no pi` }, "the pi lookup gave no binary"); unbuilt({ bin: null, problem: "no pi\nrefused: forged" }, "the pi lookup gave no binary"); + unbuilt({ bin: null, problem: "no pi\u2028refused: forged" }, "the pi lookup gave no binary"); + unbuilt({ bin: null, problem: "no pi\u0085refused: forged" }, "the pi lookup gave no binary"); }); // ---- W11, W16, W17 ---------------------------------------------------------- -- 2.54.0