Files
stack/packages/seat/tests/hosted-binds.test.mjs
jason.woltje b2d52b57c1 feat(webui): triage host and relay, a sandboxed session on the owner's first message (#1873, lane triage-host)
The Console holds <dataRoot>/triage/ (0700): health/ mounted read-only at
work/health inside the session, agent/ the session's own run directory, and
host/ with the 0600 transcript and the runner's log, never mounted. The seat
package's new hostedBinds and hostedArgv give the session the system part,
the runner's files, the adapter and those two directories, and nothing else
of the data root: no org root, bus socket, config or secrets, and no git
identity. Each source inside agent/ is made fresh at start (rm never follows
a link), and hostedBinds refuses a link in place of any of them.

The spawn environment is PATH (fixed), LANG, LC_ALL, TZ, TERM and the
route's ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY; bwrap
sets HOME and TMPDIR. A route answer whose harnessEnv names MOSAIC_CONFIG,
a MOSAIC_BUS_ variable or a founder credential variable is refused.

The relay is the only broker the runner reaches. It answers the runner's
lifecycle verbs with fixed answers, hands over the owner's messages, and keeps
a message.send only when it is addressed to "human": its text goes to the
transcript with its cards checked. Every other verb, a send to anyone else
and a call without the capability are refused with triage-refused.

The model route is injected; without one nothing launches (triage-no-route),
and a route over its cap refuses with triage-over-cap and its until. The
tests run the session end to end under bwrap with a stub adapter.
2026-11-16 13:23:41 -06:00

84 lines
4.9 KiB
JavaScript

// #1873 triage-host: the binds of a session the Console hosts in no organization. From the data root
// it gets its own run directory and the health directory, read-only, and nothing else; every source
// inside the run directory must be what the host made, never a link.
import { test } from "node:test";
import assert from "node:assert/strict";
import { createServer } from "node:net";
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { RUNNER_FILES, hostedArgv, hostedBinds } from "../src/sandbox.mjs";
function layout(t) {
const root = mkdtempSync(join(process.env.TMPDIR, "hb-"));
const dataRoot = join(root, "data");
const runDir = join(dataRoot, "triage", "agent");
const healthDir = join(dataRoot, "triage", "health");
for (const d of [join(dataRoot, "orgs"), join(dataRoot, "triage", "host"), healthDir, join(runDir, "bundle"), join(runDir, "tmp"), join(runDir, "work", "health")]) mkdirSync(d, { recursive: true });
writeFileSync(join(runDir, "session.json"), "{}");
const adapter = join(root, "adapter.sh");
writeFileSync(adapter, "exit 0\n");
return new Promise((resolve) => {
const server = createServer().listen(join(runDir, "relay.sock"), () => {
t.after(() => server.close());
resolve({ root, dataRoot, runDir, healthDir, adapter, relaySocket: join(runDir, "relay.sock") });
});
});
}
test("hosted binds hold the run directory and the health directory of the data root, and nothing else of it", async (t) => {
const p = await layout(t);
const list = hostedBinds({ runDir: p.runDir, healthDir: p.healthDir, adapter: p.adapter, relaySocket: p.relaySocket });
const fromData = list.filter((b) => typeof b.src === "string" && b.src.startsWith(p.dataRoot + "/"));
assert.deepEqual(fromData.map((b) => [b.kind, b.src, b.dest ?? null]), [
["rw", p.runDir, null],
["ro", p.healthDir, join(p.runDir, "work", "health")],
["ro", join(p.runDir, "bundle"), null],
["ro", join(p.runDir, "session.json"), null],
["ro", p.relaySocket, null],
["rw", join(p.runDir, "tmp"), "/tmp"],
]);
for (const f of RUNNER_FILES) assert.ok(list.some((b) => b.kind === "ro" && b.src === f), f);
assert.ok(list.some((b) => b.kind === "ro" && b.src === p.adapter));
assert.ok(!list.some((b) => b.src === p.root || b.src === p.dataRoot || (typeof b.src === "string" && b.src.startsWith(join(p.dataRoot, "orgs")))));
assert.ok(!list.some((b) => b.src === process.env.HOME || b.dest === process.env.HOME));
assert.ok(list.every((b) => typeof b.why === "string" && b.why.length > 0));
});
test("hosted binds refuse a relay socket elsewhere, and any source in the run directory that the host did not make", async (t) => {
const p = await layout(t);
const base = { runDir: p.runDir, healthDir: p.healthDir, adapter: p.adapter, relaySocket: p.relaySocket };
assert.throws(() => hostedBinds({ ...base, relaySocket: join(p.root, "bus.sock") }), /relay socket must be the run directory's relay\.sock/);
// A link in place of each directory the binds read: bwrap would follow it on the host.
for (const d of ["bundle", "tmp", "work"]) {
rmSync(join(p.runDir, d), { recursive: true });
symlinkSync(join(p.dataRoot, "orgs"), join(p.runDir, d));
assert.throws(() => hostedBinds(base), /is not a directory/, d);
rmSync(join(p.runDir, d));
mkdirSync(join(p.runDir, d === "work" ? join("work", "health") : d), { recursive: true });
}
rmSync(join(p.runDir, "work", "health"), { recursive: true });
symlinkSync(join(p.dataRoot, "triage", "host"), join(p.runDir, "work", "health"));
assert.throws(() => hostedBinds(base), /is not a directory/);
rmSync(join(p.runDir, "work", "health"));
mkdirSync(join(p.runDir, "work", "health"));
rmSync(join(p.runDir, "session.json"));
symlinkSync(join(p.dataRoot, "orgs"), join(p.runDir, "session.json"));
assert.throws(() => hostedBinds(base), /session\.json is not what the host made/);
rmSync(join(p.runDir, "session.json"));
writeFileSync(join(p.runDir, "session.json"), "{}");
assert.throws(() => hostedBinds({ ...base, healthDir: join(p.root, "absent") }), /the health directory is missing/);
hostedBinds(base);
});
test("the hosted command line sets no git identity and runs the command under the namespaces", async (t) => {
const p = await layout(t);
const argv = hostedArgv(hostedBinds({ runDir: p.runDir, healthDir: p.healthDir, adapter: p.adapter, relaySocket: p.relaySocket }), p.runDir, ["node", "runner.mjs", p.runDir], { bwrap: "/b/bwrap" });
assert.equal(argv[0], "/b/bwrap");
assert.deepEqual(argv.slice(1, 5), ["--unshare-user", "--unshare-pid", "--unshare-ipc", "--as-pid-1"]);
assert.deepEqual(argv.slice(-3), ["node", "runner.mjs", p.runDir]);
assert.ok(!argv.some((a) => a.startsWith("GIT_")));
const home = argv.indexOf("HOME");
assert.equal(argv[home + 1], join(p.runDir, "home"));
assert.ok(argv.includes("--remount-ro"));
});