217 lines
13 KiB
JavaScript
217 lines
13 KiB
JavaScript
// #1873 pi-unpin (design section 5, S1): pi's labels come from the binary the module finds, not
|
|
// from a pin. The version is what `<bin> --version` prints, or null when that fails; the
|
|
// compaction count records the found package's own version.
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
import { spawnSync } from "node:child_process";
|
|
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
|
|
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
|
|
|
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
|
const MODULE = join(REPO, "packages", "plugins", "harness", "pi");
|
|
const NAME = "@earendil-works/pi-coding-agent";
|
|
|
|
function scratch(t) {
|
|
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-bin-"));
|
|
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
return root;
|
|
}
|
|
|
|
// A pi package under `dir` (a node_modules directory), with `script` as its bin.
|
|
function standIn(dir, script, version) {
|
|
const pkg = join(dir, NAME);
|
|
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
|
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: NAME, version, bin: { pi: "dist/bundle/cli.js" } }));
|
|
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), script, { mode: 0o755 });
|
|
return join(pkg, "dist", "bundle", "cli.js");
|
|
}
|
|
|
|
// A copy of the pi module in a scratch repository whose node_modules holds a stand-in, so the
|
|
// module's repository copy is that stand-in and not the real pi. The rest of packages/ links to
|
|
// the real tree, for the modules the copy imports.
|
|
async function copyRepo(t, script, version) {
|
|
const root = scratch(t);
|
|
const mod = join(root, "packages", "plugins", "harness", "pi");
|
|
mkdirSync(mod, { recursive: true });
|
|
for (const f of readdirSync(MODULE)) if (f.endsWith(".mjs")) copyFileSync(join(MODULE, f), join(mod, f));
|
|
for (const p of readdirSync(join(REPO, "packages"))) if (p !== "plugins") symlinkSync(join(REPO, "packages", p), join(root, "packages", p));
|
|
standIn(join(root, "node_modules"), script, version);
|
|
mkdirSync(join(root, "node_modules", ".bin"));
|
|
symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi"));
|
|
return { root, pi: await import(pathToFileURL(join(mod, "index.mjs")).href) };
|
|
}
|
|
|
|
const emptyStore = (t) => {
|
|
const root = scratch(t);
|
|
mkdirSync(join(root, "session"));
|
|
return { session: join(root, "session"), projects: join(root, "claude-projects") };
|
|
};
|
|
|
|
test("pi-unpin S1: the version is what the found binary prints, and the compaction count records its package's version", async t => {
|
|
const { pi } = await copyRepo(t, "#!/bin/sh\n[ \"$1\" = --version ] && echo 9.9.9\n", "9.9.4");
|
|
assert.equal(await pi.version({ env: {} }), "9.9.9");
|
|
const counted = pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false });
|
|
assert.equal(counted.version, "9.9.4");
|
|
});
|
|
|
|
test("pi-unpin S1: a binary that exits 1 on --version gives a null version, not a guess", async t => {
|
|
const { pi } = await copyRepo(t, "#!/bin/sh\nexit 1\n", "8.8.8");
|
|
assert.equal(await pi.version({ env: {} }), null);
|
|
// The package's own file still names its version for the compaction count.
|
|
assert.equal(pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false }).version, "8.8.8");
|
|
});
|
|
|
|
test("pi-unpin S1: a version line with a control character or over 128 characters is not recorded", async t => {
|
|
const { pi } = await copyRepo(t, "#!/bin/sh\nprintf '1.0\\033[31m\\n'\n", "1.0.0");
|
|
assert.equal(await pi.version({ env: {} }), null);
|
|
const long = await copyRepo(t, `#!/bin/sh\necho ${"9".repeat(129)}\n`, "1.0.0");
|
|
assert.equal(await long.pi.version({ env: {} }), null);
|
|
});
|
|
|
|
test("pi-unpin S1: the repository's real pi reports its installed package's version", async () => {
|
|
const pi = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
|
|
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", NAME, "package.json"), "utf8")).version;
|
|
assert.equal(await pi.version({ env: {} }), installed);
|
|
});
|
|
|
|
// #1873 pi-unpin (design section 5, S2): the binary comes from MOSAIC_PI_BIN, else the first pi on
|
|
// PATH, else the repository's copy. Mounts, install roots and the session's MOSAIC_PI_BIN follow
|
|
// the package that binary belongs to. A refusal names the variable, never a path.
|
|
const real = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
|
|
const CREDENTIAL = { ZAI_API_KEY: "fixture-zai" };
|
|
// #1873 S3: the stand-in passes the fit check, prints 7.7.7 for --version, and as a session
|
|
// records its argv in $ARGV_OUT.
|
|
const ARGV_SCRIPT = piStandIn({ version: "7.7.7" });
|
|
|
|
// npm's global layout: prefix/lib/node_modules/<package>, with prefix/bin/pi a link to its bin.
|
|
function prefix(t, script = ARGV_SCRIPT, version = "7.7.0") {
|
|
const root = join(scratch(t), "prefix");
|
|
const cli = standIn(join(root, "lib", "node_modules"), script, version);
|
|
mkdirSync(join(root, "bin"), { recursive: true });
|
|
symlinkSync(`../lib/node_modules/${NAME}/dist/bundle/cli.js`, join(root, "bin", "pi"));
|
|
return { root, bin: join(root, "bin", "pi"), cli: realpathSync(cli), pkg: realpathSync(join(root, "lib", "node_modules", NAME)) };
|
|
}
|
|
const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" };
|
|
// pi.env() reads the fit check that preflight awaited for the same files.
|
|
const piEnv = (env) => real.env({ policy: { tools: ["read"] }, tools: [], session, env });
|
|
const noPath = (problem, ...paths) => { for (const p of paths) assert.ok(!problem.message.includes(p), problem.message); };
|
|
|
|
test("pi-unpin S2: a stand-in pi in an npm prefix, named by MOSAIC_PI_BIN, is found, mounted alone and run from the mount", async t => {
|
|
const p = prefix(t);
|
|
const env = { ...CREDENTIAL, MOSAIC_PI_BIN: p.bin, PATH: "/usr/bin:/bin" };
|
|
assert.equal(await real.preflight({ env }), null);
|
|
assert.equal(await real.version({ env }), "7.7.7");
|
|
const mounts = real.mounts({ env });
|
|
assert.deepEqual(mounts.filter(m => (m.dest ?? m.src) === p.pkg || m.src === p.pkg).map(m => m.src), [p.pkg]);
|
|
assert.ok(!mounts.some(m => (m.dest ?? m.src) === join(REPO, "node_modules")), "the repository's node_modules is not mounted");
|
|
const roots = real.installRoots({ env });
|
|
assert.ok(roots.includes(p.pkg));
|
|
assert.ok(!roots.includes(realpathSync(join(REPO, "node_modules"))));
|
|
const given = piEnv(env);
|
|
assert.equal(given.MOSAIC_PI_BIN, p.cli, "the session runs the real file inside the mounted package");
|
|
assert.ok(real.envNames.includes("MOSAIC_PI_BIN"));
|
|
|
|
// Run the real adapter in a sandbox that holds the system, the adapter and the module's mounts,
|
|
// and not the repository's node_modules: the stand-in records the argv it was given.
|
|
if (sandboxProblem()) return t.skip("no usable bwrap");
|
|
const out = join(scratch(t), "out");
|
|
mkdirSync(out);
|
|
writeFileSync(join(out, "prompt.md"), "p");
|
|
const argv = ["/usr/bin/bwrap", "--unshare-user", "--unshare-pid", "--unshare-net", "--die-with-parent", "--clearenv", "--ro-bind", "/usr", "/usr"];
|
|
for (const link of ["/bin", "/sbin", "/lib", "/lib64"]) {
|
|
if (!existsSync(link)) continue;
|
|
if (lstatSync(link).isSymbolicLink()) argv.push("--symlink", readlinkSync(link), link);
|
|
else argv.push("--ro-bind", link, link);
|
|
}
|
|
argv.push("--proc", "/proc", "--dev", "/dev", "--ro-bind", join(REPO, "adapters", "pi"), join(REPO, "adapters", "pi"));
|
|
for (const m of mounts) argv.push("--ro-bind", m.src, m.dest ?? m.src);
|
|
// The extensions the adapter checks for, which a launch's bundle binds.
|
|
for (const e of given.MOSAIC_EXTENSIONS.split(",")) argv.push("--ro-bind", e, e);
|
|
argv.push("--bind", out, out);
|
|
for (const [k, v] of Object.entries({ ...given, PATH: "/usr/bin:/bin", ARGV_OUT: join(out, "argv"), MOSAIC_SYSTEM_PROMPT_FILE: join(out, "prompt.md"),
|
|
MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(out, "ws"), MOSAIC_SESSION_DIR: join(out, "session"), MOSAIC_MODEL: "m" })) argv.push("--setenv", k, v);
|
|
argv.push("/bin/sh", join(REPO, "adapters", "pi", "adapter.sh"));
|
|
const r = spawnSync(argv[0], argv.slice(1), { encoding: "utf8", timeout: 20000 });
|
|
assert.equal(r.status, 0, r.stderr);
|
|
const ran = readFileSync(join(out, "argv"), "utf8").split("\n");
|
|
assert.ok(ran.includes("--no-context-files") && ran.includes("--thinking"), ran.join(" "));
|
|
});
|
|
|
|
test("pi-unpin S2: a relative or non-executable MOSAIC_PI_BIN is refused with harness-binary-missing, naming the variable only", async t => {
|
|
const p = prefix(t);
|
|
const plain = join(scratch(t), "pi");
|
|
writeFileSync(plain, "#!/bin/sh\n", { mode: 0o644 });
|
|
for (const bin of ["bin/pi", "pi", plain, join(p.root, "missing")]) {
|
|
const problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: bin, PATH: `${join(p.root, "bin")}:/usr/bin:/bin` } });
|
|
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "MOSAIC_PI_BIN isn't an executable file at an absolute path"], bin);
|
|
noPath(problem, plain, p.root);
|
|
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: bin } }), null, "no fallback to another pi");
|
|
}
|
|
});
|
|
|
|
test("pi-unpin S2: with MOSAIC_PI_BIN unset the first pi on PATH is used, and the repository's own .bin on PATH does not count", async t => {
|
|
const p = prefix(t);
|
|
const env = { ...CREDENTIAL, PATH: `${join(REPO, "node_modules", ".bin")}:/nonexistent:relative:${join(p.root, "bin")}:/usr/bin:/bin` };
|
|
assert.equal(await real.preflight({ env }), null);
|
|
assert.equal(await real.version({ env }), "7.7.7");
|
|
assert.equal(piEnv(env).MOSAIC_PI_BIN, p.cli);
|
|
assert.ok(real.mounts({ env }).some(m => m.src === p.pkg));
|
|
});
|
|
|
|
test("pi-unpin S2: with neither, the repository's copy is used and mounted as before", async () => {
|
|
for (const PATH of ["/usr/bin:/bin", `${join(REPO, "node_modules", ".bin")}:/usr/bin:/bin`]) {
|
|
const env = { ...CREDENTIAL, PATH };
|
|
assert.equal(await real.preflight({ env }), null, PATH);
|
|
const nm = realpathSync(join(REPO, "node_modules"));
|
|
assert.deepEqual(real.mounts({ env }).filter(m => m.dest === join(REPO, "node_modules")).map(m => m.src), [nm]);
|
|
assert.ok(real.installRoots({ env }).includes(nm));
|
|
const bin = piEnv(env).MOSAIC_PI_BIN;
|
|
assert.ok(bin.startsWith(join(REPO, "node_modules") + "/"), bin);
|
|
assert.equal(realpathSync(bin), realpathSync(join(REPO, "node_modules", ".bin", "pi")));
|
|
}
|
|
});
|
|
|
|
test("pi-unpin S2: a pi that is not in a pi package is refused, and so is a pi that cannot be found at all", async t => {
|
|
const dir = join(scratch(t), "bin");
|
|
mkdirSync(dir);
|
|
writeFileSync(join(dir, "pi"), "#!/bin/sh\necho 1.0.0\n");
|
|
chmodSync(join(dir, "pi"), 0o755);
|
|
let problem = await real.preflight({ env: { ...CREDENTIAL, PATH: `${dir}:/usr/bin:/bin` } });
|
|
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `the pi on PATH isn't part of the ${NAME} package`]);
|
|
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(dir, "pi") } });
|
|
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't part of the ${NAME} package`]);
|
|
noPath(problem, dir);
|
|
// The scratch copy has no node_modules of its own: no pi anywhere.
|
|
const { root, pi } = await copyRepo(t, "#!/bin/sh\n", "1.0.0");
|
|
rmSync(join(root, "node_modules"), { recursive: true });
|
|
problem = await pi.preflight({ env: { ...CREDENTIAL, PATH: "/usr/bin:/bin" } });
|
|
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci"]);
|
|
assert.throws(() => pi.env({ policy: { tools: [] }, tools: [], session, env: { PATH: "/usr/bin:/bin" } }), /no pi found/);
|
|
});
|
|
|
|
// Review finding 8: the package directory is mounted whole, so the binary must be the pi bin its
|
|
// package.json names. A stray executable under a directory whose package.json carries the name
|
|
// (a wide tree, say) is refused, naming the variable only.
|
|
test("pi-unpin S2 review: a binary that isn't the bin its package names is refused", async t => {
|
|
const wide = scratch(t);
|
|
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
|
mkdirSync(join(wide, "tools"));
|
|
writeFileSync(join(wide, "tools", "pi"), "#!/bin/sh\necho 1.0.0\n", { mode: 0o755 });
|
|
let problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
|
|
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
|
|
noPath(problem, wide);
|
|
// A package.json with no bin names none.
|
|
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0" }));
|
|
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
|
|
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
|
|
// A bin given as a plain string names it too.
|
|
const dir = join(scratch(t), "node_modules");
|
|
const cli = standIn(dir, piStandIn({ version: "1.2.3" }), "1.2.3");
|
|
writeFileSync(join(dir, NAME, "package.json"), JSON.stringify({ name: NAME, version: "1.2.3", bin: "dist/bundle/cli.js" }));
|
|
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: cli } }), "1.2.3");
|
|
});
|