Files
stack/packages/organization/tests/system-only.test.mjs
T
jason.woltje 4a9c7a931c feat(organization): triage is system-only, no organization gains it (#1873, lane triage-fence)
One list, SYSTEM_ONLY in vocabulary.mjs, names triage; every path by which an
organization could gain one refuses with the one code template-system-only:
hire prepare at org, project and root level and a change onto it (hire.raise,
hire.start.console, decision.raise naming role.hire or role.change); the hire
and definitions template lists omit it; definition import; a definition copied
from it; organization roles, root agents and project agents; a pack record
pinning it; a starter pack defining it, so pack preview and apply refuse an
installed pack that names it; instances.set add and change for the human and
the tester, and the instances add list omits it. The broker passes the code
through and the webui tables map it. The template itself still loads and
validates. Red-first tests in organization and bus system-only.test.mjs; the
channel-proof hash of webui bus.mjs and the broker import pin are updated.
2026-11-16 11:38:17 -06:00

210 lines
10 KiB
JavaScript

// #1873 lane triage-fence: the Triage template is system-only. Jason approved the template on
// 2026-11-16; no organization may hold it until he rules on its use outside one. Every place an
// organization could gain it refuses with one code, template-system-only, and the template itself
// still loads and validates as a template. Fixtures live in a temp directory.
import test from "node:test";
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
// A namespace import, so a run against the commit before the fence fails test by test, not at load.
import * as vocabulary from "../src/vocabulary.mjs";
import { checkDefinition, entryFromTemplate, readTemplate, templateHash, templateHashes } from "../src/definitions.mjs";
import { loadRole } from "../src/role.mjs";
import { validateBusinessDocument } from "../src/organization-config.mjs";
import { checkProjectAgents, checkRootAgents } from "../src/agent-levels.mjs";
import { hireDecisions } from "../src/hire-decisions.mjs";
import { instanceSettings } from "../src/instance-settings.mjs";
import { definitionVerbs } from "../src/definition-verbs.mjs";
import { organizationWriter } from "../src/organization.mjs";
import { ROLE_RIGHTS } from "../src/org-rights.mjs";
import { checkPack, installedStarterPacks } from "../../plugins/src/starter-pack.mjs";
import { businessDoc, rolesCopy, systemFor, tmp, writeJson } from "./helpers.mjs";
const CODE = "template-system-only";
const coded = (e) => e?.code === CODE;
const prefixed = (e) => e?.name === "BusinessError" && e.message.startsWith(`${CODE}: `);
const OWNER = { actor: "jason", agent: false, rights: ["org.hire"], owner: true, instanceOwner: true, subject: { provider: "test", iss: "https://test", sub: "owner" } };
const DEF_OWNER = { actor: "jason", rights: [...ROLE_RIGHTS.owner], agent: false };
const ROUTE = { harness: "pi", model: "claude-opus-5-5" };
function rootDir(t) {
const root = tmp();
t.after(() => rmSync(root, { recursive: true, force: true }));
return root;
}
function hireSetup(t) {
const root = mkdtempSync(join(tmpdir(), "so-"));
t.after(() => rmSync(root, { recursive: true, force: true }));
const rolesDir = rolesCopy(root);
writeJson(join(root, "businesses/demo.json"), { ...businessDoc(root, "demo"), plugins: { harness: ["pi"] } });
const writer = organizationWriter({ dir: root, rolesDir });
const dataRoot = join(root, "data");
mkdirSync(dataRoot, { mode: 0o700 });
const service = hireDecisions({ writer, rolesDir, configDir: root, dataRoot, assertClean: () => {} });
const triage = { template: "triage", sha256: templateHash(rolesDir, "triage") };
let n = 0;
const prepare = (extra) => service.prepare("demo", { op: "hire", level: "org", name: "sorter", role: triage, route: ROUTE, reason: "Sort the inbox", ...extra },
{ id: `1111111${n++}-1111-4111-8111-111111111111`, expiresAt: "2099-01-01T00:00:00.000Z" });
return { root, rolesDir, writer, service, prepare, triage };
}
function settingsSetup(t) {
const root = rootDir(t);
const rolesDir = rolesCopy(root);
const dir = join(root, "config");
const doc = businessDoc(root, "acme");
doc.plugins = { harness: ["pi", "claude-code"] };
writeJson(join(dir, "businesses", "acme.json"), doc);
return { rolesDir, settings: instanceSettings({ dir, rolesDir, system: systemFor(root) }) };
}
function verbsSetup(t) {
const root = rootDir(t);
const dir = join(root, "config");
const rolesDir = rolesCopy(root);
writeJson(join(dir, "businesses/acme.json"), businessDoc(root));
const writer = organizationWriter({ dir, rolesDir });
return { root, dir, rolesDir, writer, verbs: definitionVerbs({ writer, rolesDir, assertClean: () => {} }) };
}
test("the list is one name, triage, and its refusal code is template-system-only", () => {
assert.deepEqual([...(vocabulary.SYSTEM_ONLY ?? [])], ["triage"]);
assert.equal(vocabulary.SYSTEM_ONLY_REFUSAL, CODE);
assert.equal(vocabulary.systemOnly?.("triage"), true);
assert.equal(vocabulary.systemOnly?.("coder"), false);
assert.equal(vocabulary.systemOnly?.(undefined), false);
});
test("triage still loads and validates as a template", (t) => {
const rolesDir = rolesCopy(rootDir(t));
const role = loadRole(rolesDir, "triage");
assert.equal(role.roleVersion, 2);
const source = readTemplate(rolesDir, "triage");
assert.match(source.sha256, /^[a-f0-9]{64}$/);
assert.ok(templateHashes(rolesDir).some(({ template }) => template === "triage"));
});
test("hire refuses a triage template at org level", async (t) => {
const f = hireSetup(t);
await assert.rejects(f.prepare(), coded);
});
test("hire refuses a triage template at project level", async (t) => {
const f = hireSetup(t);
await assert.rejects(f.prepare({ level: "project", project: "stack" }), coded);
});
test("hire refuses a triage template at root level", async (t) => {
const f = hireSetup(t);
await assert.rejects(f.prepare({ level: "root" }), coded);
});
test("a change refuses to move an org instance onto triage", async (t) => {
const f = hireSetup(t);
await assert.rejects(f.prepare({ op: "change", name: "coder" }), coded);
});
test("the hire template list a page offers omits triage and keeps every other template", (t) => {
const f = hireSetup(t);
const offered = f.service.templates().templates.map(({ name }) => name);
assert.ok(!offered.includes("triage"));
assert.deepEqual(offered, templateHashes(f.rolesDir).map(({ template }) => template).filter((n) => n !== "triage"));
});
test("instances add refuses triage for the human and the tester", (t) => {
const f = settingsSetup(t);
for (const actor of ["jason", "tester"]) {
const revision = f.settings.read("acme").revision;
assert.throws(() => f.settings.set("acme", { revision, instance: "sorter", op: "add", definition: "triage" }, actor), coded, actor);
}
});
test("instances change of definition refuses triage", (t) => {
const f = settingsSetup(t);
const revision = f.settings.read("acme").revision;
assert.throws(() => f.settings.set("acme", { revision, instance: "coder", op: "change", definition: "triage" }, "jason"), coded);
});
test("the instances add list omits triage", (t) => {
const f = settingsSetup(t);
assert.equal(Object.hasOwn(f.settings.read("acme").definitions, "triage"), false);
});
test("an organization file whose roles name triage is refused", (t) => {
const root = rootDir(t);
const rolesDir = rolesCopy(root);
const doc = businessDoc(root);
doc.roles.sorter = { definition: "triage" };
assert.throws(() => validateBusinessDocument(doc, join(root, "acme.json"), { rolesDir }), prefixed);
});
test("an organization definition copied from triage is refused", (t) => {
const rolesDir = rolesCopy(rootDir(t));
const entry = entryFromTemplate(readTemplate(rolesDir, "triage"));
assert.throws(() => checkDefinition("sorter", entry), prefixed);
});
test("a root agent naming triage is refused", (t) => {
const rolesDir = rolesCopy(rootDir(t));
assert.throws(() => checkRootAgents({ agentsVersion: 1, agents: { sorter: { definition: "triage" } } }, { rolesDir }), prefixed);
});
test("a project agent naming triage is refused", (t) => {
const rolesDir = rolesCopy(rootDir(t));
assert.throws(() => checkProjectAgents({ sorter: { definition: "triage" } }, { rolesDir, where: "projects.stack.agents" }), prefixed);
});
test("a pack record proposing a triage hire is refused", (t) => {
const root = rootDir(t);
const rolesDir = rolesCopy(root);
const doc = businessDoc(root);
doc.packs = [{ id: "coding", version: "1.0.0", sha256: "a".repeat(64), origin: "shipped", scope: "org", project: null,
at: "2026-11-12T20:00:00.000Z", by: "jason", added: [], proposedHires: [{ name: "sorter-1", pin: { template: "triage", sha256: "a".repeat(64) } }] }];
assert.throws(() => validateBusinessDocument(doc, join(root, "acme.json"), { rolesDir }), prefixed);
});
test("import refuses triage and the definitions template list omits it", (t) => {
const f = verbsSetup(t);
const revision = f.writer.read("acme").revision;
assert.throws(() => f.verbs.import("acme", { revision, template: "triage" }, DEF_OWNER), coded);
const listed = f.verbs.templates().map(({ template }) => template);
assert.deepEqual(listed, templateHashes(f.rolesDir).map(({ template }) => template).filter((n) => n !== "triage"));
});
test("a starter pack naming triage is refused when checked", async (t) => {
const rolesDir = rolesCopy(rootDir(t));
const { starterPack } = await import("../../plugins/starter-pack/coding/index.mjs");
const doc = starterPack();
doc.definitions = { ...doc.definitions, triage: { template: "triage", sha256: templateHash(rolesDir, "triage") } };
assert.throws(() => checkPack(doc, { origin: "installed", rolesDir, name: doc.id }), coded);
});
// The guard: the fence changes nothing for any other template or any shipped pack.
test("every other shipped template imports as before and every shipped pack loads unchanged", async (t) => {
const f = verbsSetup(t);
const all = templateHashes(f.rolesDir).map(({ template }) => template);
const others = all.filter((n) => n !== "triage");
assert.equal(others.length, all.length - 1);
// Each template's import outcome, measured on 5c7194e05 before the fence: chief-of-staff carries
// role.hire, which a hired agent may not hold, so its import refused before and still does.
const refusedBefore = ["chief-of-staff"];
const outcome = {};
for (const template of others) {
try {
f.verbs.import("acme", { revision: f.writer.read("acme").revision, template }, DEF_OWNER);
outcome[template] = "imported";
} catch (e) {
assert.ok(!String(e.message).includes(CODE) && e.code !== CODE, template);
outcome[template] = "refused";
}
}
assert.deepEqual(Object.keys(outcome).filter((n) => outcome[n] === "refused"), refusedBefore.filter((n) => others.includes(n)));
const defs = f.writer.read("acme").document.definitions;
assert.deepEqual(Object.keys(defs).sort(), others.filter((n) => outcome[n] === "imported").sort());
const { packs, refused } = await installedStarterPacks({ dataRoot: null });
assert.deepEqual(refused, []);
assert.deepEqual(packs.map((p) => p.id).sort(), ["business-management", "coding", "creative-writing", "marketing", "montage"]);
});