Files
stack/packages/business/tests/assistant.test.mjs
T
jason.woltjeandClaude Opus 5.5 3190ad762b bus, project: file-backed north-star specs with spec.read and record.write (row 138, #1632)
Row 138 round 2, Rocko's candidate e3f9a400 (43 paths) on 26a0b0fab.
Dewey approved (comment 28754) after round 1 changes (comment 28752):
D1-D5 mutants killed, the 17 touched test files run singly passed
124/124. Rocko's own run: 753 tests and 59/59 mutants killed.

The bus gains the project adapter through runtime.mjs, the way it
already injects the organization, so broker.mjs takes no new imports
and refuses with project-not-configured when no adapter is set. The
project socket timeout is 120s. Roles gain record.write. The schema
moves from 3e to 3f; the live migration follows MIGRATION.md and
needs Mos's window.

Sage's landing gate ran on c842686d5 plus the candidate: 25 steps,
all rc 0 (15 packages, 10 script suites), network sage-r138_default
removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 20:49:44 -05:00

170 lines
7.7 KiB
JavaScript

// The assistant role (REQ-ASSIST-1, row 119). The role file is policy: it
// holds message.send and record.write within role, no credentials, and tools
// as read-only as the chief of staff's, plus the web grant (REQ-HARN-2, vikunja:19/295). A widened file fails here.
import { test } from "node:test";
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { ACTIONS, BusinessError, classify, loadBusiness, loadRole, resolveInstance } from "../src/index.mjs";
import { businessDoc, REPO_ROLES, systemFor, tmp, writeJson } from "./helpers.mjs";
const APPROVED = Object.freeze({
tools: ["read", "grep", "find", "ls", "web"],
network: "api-only",
authority: Object.freeze({ withinRole: Object.freeze(["message.send", "record.write"]), crossRole: Object.freeze([]) }),
credentials: Object.freeze([]),
// REQ-ASSIST-2: the user and the chief of staff, both ways. The broker enforces these lists (row 120).
messaging: Object.freeze({
sendTo: Object.freeze(["human", "chief-of-staff"]),
receiveFrom: Object.freeze(["human", "chief-of-staff"]),
}),
});
const INSTANCE = "assistant";
function refuses(fn, pattern) {
assert.throws(fn, (error) => {
assert.ok(error instanceof BusinessError, `expected BusinessError, got ${error}`);
assert.equal(error.exitCode, 2);
assert.match(error.message, pattern);
return true;
});
}
function loadFixture(mutate = () => {}) {
const root = tmp();
const doc = businessDoc(root, "acme");
doc.roles[INSTANCE] = { definition: "assistant", holder: "mos" };
mutate(doc);
writeJson(join(root, "businesses", "acme.json"), doc);
return { root, business: loadBusiness("acme", { dir: root, rolesDir: REPO_ROLES }) };
}
function resolved({ root, business }) {
return resolveInstance({ system: systemFor(root), business, instance: INSTANCE });
}
test("the role file loads and resolves through loadBusiness", () => {
const role = loadRole(REPO_ROLES, "assistant");
assert.equal(role.roleVersion, 2);
assert.equal(role.name, "assistant");
assert.equal(role.contract, "assistant.md");
assert.equal(role.contractPath, join(REPO_ROLES, "assistant.md"));
const record = resolved(loadFixture());
assert.equal(record.definition, "assistant");
assert.equal(record.holder, "mos");
assert.equal(record.contract, join(REPO_ROLES, "assistant.md"));
assert.equal(record.launch, null);
assert.equal(record.tracker, null);
});
test("the shipped file is exactly the approved list", () => {
const raw = JSON.parse(readFileSync(join(REPO_ROLES, "assistant.json"), "utf8"));
const { title, ...rest } = raw;
assert.equal(typeof title, "string");
assert.deepEqual(rest, { roleVersion: 2, name: "assistant", contract: "assistant.md", ...APPROVED });
});
test("the assistant's authority is message.send and record.write within role", () => {
const role = loadRole(REPO_ROLES, "assistant");
assert.deepEqual(role.authority, { withinRole: ["message.send", "record.write"], crossRole: [] });
assert.deepEqual(role.credentials, []);
const record = resolved(loadFixture());
assert.deepEqual(record.limits.authority, { withinRole: ["message.send", "record.write"], crossRole: [] });
assert.deepEqual(record.credentials, {});
for (const action of ACTIONS) {
assert.equal(classify(record, action), ["message.send", "record.write"].includes(action) ? "within" : "gated", action);
}
for (const action of ["task.create", "task.assign", "role.launch"]) assert.equal(classify(record, action), "gated", action);
});
test("the assistant talks to the user and the chief of staff only, and no other shipped role is limited", () => {
const role = loadRole(REPO_ROLES, "assistant");
assert.deepEqual(role.messaging, { sendTo: ["human", "chief-of-staff"], receiveFrom: ["human", "chief-of-staff"] });
assert.ok(Object.isFrozen(role.messaging.sendTo));
// Limiting another role is a policy change of its own; the shipped chief of staff and PM stay open.
for (const name of ["chief-of-staff", "pm", "cto", "coder", "reviewer", "researcher"]) {
assert.equal(loadRole(REPO_ROLES, name).messaging, null, name);
}
});
test("a business grant through limits does not widen the role", () => {
const record = resolved(loadFixture((doc) => {
doc.roles[INSTANCE].vars = { "limits.authority": ["message.send", "task.create", "role.launch"] };
}));
assert.deepEqual(record.limits.authority, { withinRole: ["message.send"], crossRole: [] });
assert.equal(record.launch, null);
});
test("a launch block whose by is the assistant is refused", () => {
refuses(
() => loadFixture((doc) => {
doc.launch = { ...doc.launch, by: INSTANCE, instances: ["coder"] };
}),
/launch\.by names assistant, whose role assistant doesn't hold role\.launch within-role/,
);
});
test("the instance takes no credential references and no tracker", () => {
refuses(
() => loadFixture((doc) => {
doc.roles[INSTANCE].credentials = { gitea: { file: "/x/token", rotateBy: "2099-01-01" } };
}),
/must reference exactly the services its role definition needs \(none; got gitea\)/,
);
refuses(
() => loadFixture((doc) => {
doc.roles[INSTANCE].tracker = { bot: "bot-acme-assistant", botId: 7 };
}),
/has "tracker" but assistant uses no vikunja credential/,
);
});
// Row 133 (#1627): an arbiter messages the raiser when it closes a decision, so a role limited by
// messaging lists can't be one. The message names the kind, the instance and the definition.
test("an arbiter whose role definition declares messaging lists is refused, for each kind", () => {
for (const kind of ["delivery", "technical"]) {
refuses(
() => loadFixture((doc) => { doc.arbiters[kind] = INSTANCE; }),
new RegExp(`arbiters\\.${kind} names assistant, whose role definition assistant declares messaging lists`),
);
// An instance name that isn't the definition's: both are named.
refuses(
() => loadFixture((doc) => {
doc.roles.aide = { definition: "assistant" };
doc.arbiters[kind] = "aide";
}),
new RegExp(`arbiters\\.${kind} names aide, whose role definition assistant declares messaging lists`),
);
}
});
test("an arbiter without messaging lists is accepted beside the limited assistant", () => {
assert.deepEqual(loadFixture().business.arbiters, { delivery: "pm", technical: "cto" });
const { business } = loadFixture((doc) => { doc.arbiters.delivery = "chief"; });
assert.deepEqual(business.arbiters, { delivery: "chief", technical: "cto" });
});
test("the contract passes work to the chief of staff and directs no one", () => {
const text = readFileSync(join(REPO_ROLES, "assistant.md"), "utf8");
assert.match(text, /chief of staff/);
assert.match(text, /don't direct\s+other agents/);
assert.match(text, /create or assign\s+tasks/);
assert.match(text, /launch\s+sessions/);
// Round 1 F1: rulings go to the chief of staff, never to the role they concern (REQ-ASSIST-1, REQ-ASSIST-2).
assert.match(text, /Relay each ruling the user gives verbatim to the chief\s+of\s+staff,\s+naming\s+whose\s+ruling\s+it\s+is\s+and\s+which\s+role\s+it\s+concerns/);
assert.match(text, /Don't\s+send\s+it\s+to\s+that\s+role\s+yourself/);
assert.doesNotMatch(text, /to\s+the\s+role\s+it\s+concerns/);
});
test("REQ-HARN-2: the assistant holds the web grant, and no other role file does", () => {
const names = readdirSync(REPO_ROLES).filter((f) => f.endsWith(".json") && f !== "conductor-policy.json").map((f) => f.slice(0, -5));
const granted = names.filter((name) => loadRole(REPO_ROLES, name).tools.includes("web"));
assert.deepEqual(granted, ["assistant"]);
for (const name of ["coder", "reviewer", "chief-of-staff", "pm", "cto", "researcher"]) {
assert.ok(!loadRole(REPO_ROLES, name).tools.includes("web"), name);
}
});