Files
stack/packages/business/tests/instance-settings.test.mjs
T
jason.woltje 9466f3e5ed business: the human adds and changes role instances from the CLI and the Console (row 165, Q171, #1662)
`mosaic business instances BUSINESS` reads the role instances, and
`mosaic business instance add|set` writes one add or change through the
broker's new human-only `instances` and `instances.set` verbs. The writer is
packages/business/src/instance-settings.mjs. It takes the caller's revision,
validates the next document, refuses a definition that declares credentials
(organization-credentials-out-of-scope), keeps protected instances as they
are, logs the human as the actor, and says when the running bus needs a
restart. Neither command restarts the bus.

The writer refuses the instance ids human and tester with invalid-request,
because the broker refuses a business whose roles use either name when the
bus starts.

The Console's Settings view shows the instances and, under --send only,
posts to POST /api/instances with the exact Origin and same-origin Fetch
Metadata. A refusal keeps the draft. An answer without the view's shape is
outcome-unknown (503). The page's public/ diff carries the decision 123 go
in mos-0832.

Neither verb is in ORGANIZATION_KEYS, so no agent tool reaches them.

Owner Darkwing, brief in row 165 (#1662). Round 3 rebases round 2 onto
4f45cadc0 (row 156). The public/ +/- digest is 939ecd28...9444, unchanged
at landing. Reviewers on candidate manifest e30b54da...f580: Dewey
approve 29052, Filbert approve 29053. Seat run on 4f45cadc0: 1436/0
singles, 37/37 mutants killed. Sage gate on e8b09735a: GATE_PASSED 25/25,
packages 2282/0, queue node 156/0, script selftests 422/0, 23:59:13Z to
00:05:35Z. Rebased onto 50a57e454 over two SESSIONS-only commits.

Nothing live.
2026-10-18 19:10:20 -05:00

189 lines
14 KiB
JavaScript

// Row 165 (#1662, Q171, REQ-ROLE-3): the human adds a role instance and changes one's definition,
// vars and launch membership through the organization writer. holder, credentials and tracker
// stay hand edits; a refused write leaves the file's bytes alone.
import test, { after } from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { readFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import { instanceSettings } from "../src/instance-settings.mjs";
import { projectFilePath } from "../src/project.mjs";
import { businessDoc, REPO, rolesCopy, systemConfig, systemFor, tmp, writeJson } from "./helpers.mjs";
const CLI = join(REPO, "packages", "business", "src", "cli.mjs");
const roots = [];
after(() => roots.forEach(root => rmSync(root, { recursive: true, force: true })));
// The business file sits where mosaic business validate looks for it, so the test can run it.
function fixture(mutate) {
const root = tmp();
roots.push(root);
const rolesDir = rolesCopy(root);
const doc = businessDoc(root, "acme");
mutate?.(doc, root);
const dir = join(root, "config");
const file = writeJson(join(dir, "businesses", "acme.json"), doc);
const config = systemConfig(root);
const settings = instanceSettings({ dir, rolesDir, system: systemFor(root) });
const validate = () => spawnSync(process.execPath, [CLI, "validate", "acme"], {
env: { PATH: process.env.PATH, HOME: join(root, "home"), MOSAIC_CONFIG: config, MOSAIC_ROLES_DIR: rolesDir }, encoding: "utf8",
});
const log = () => readFileSync(settings.writer.logFile("acme"), "utf8").trim().split("\n").map(line => JSON.parse(line));
return { root, rolesDir, doc, file, settings, validate, log };
}
// Each refusal: its code (or the validator's BusinessError), and the file's bytes unchanged.
function refuses(f, args, code) {
const bytes = readFileSync(f.file, "utf8");
const revision = f.settings.read("acme").revision;
assert.throws(() => f.settings.set("acme", { revision, ...args }, "jason"),
e => (code === "BusinessError" ? e.name === code : e.code === code), JSON.stringify(args));
assert.equal(readFileSync(f.file, "utf8"), bytes, JSON.stringify(args));
}
test("row 165 read lists each instance and the definitions add accepts", () => {
const f = fixture(doc => { doc.roles.assistant = { definition: "assistant", holder: "mos" }; });
const view = f.settings.read("acme");
assert.match(view.revision, /^[a-f0-9]{64}$/);
assert.equal(view.launch, true);
assert.deepEqual(view.instances.coder, { definition: "coder", vars: { harness: "pi", "limits.network": "none" }, launched: true, launcher: false, protected: false });
assert.equal(view.instances.chief.launcher, true);
assert.equal(view.instances.assistant.protected, true);
assert.deepEqual(view.definitions["security-reviewer"], { services: [], accepted: true });
assert.deepEqual(view.definitions.coder, { services: ["gitea", "vikunja"], accepted: false });
assert.equal(view.definitions.assistant.accepted, false);
assert.equal(Object.hasOwn(view.definitions, "researcher"), false);
assert.equal(Object.hasOwn(view.definitions, "conductor-policy"), false);
});
test("row 165 add: two instances join, validate exits 0, the log names the human, and the bus restart is reported", () => {
const f = fixture();
const view = f.settings.read("acme");
const added = f.settings.set("acme", { revision: view.revision, instance: "sec-1", op: "add", definition: "security-reviewer", vars: { model: "claude-sonnet-5-5" }, launch: true }, "jason");
assert.deepEqual(added.instances["sec-1"], { definition: "security-reviewer", vars: { model: "claude-sonnet-5-5" }, launched: true, launcher: false, protected: false });
assert.notEqual(added.revision, view.revision);
assert.match(added.transaction, /\S/);
assert.equal(added.restart, true);
const gate = f.settings.set("acme", { revision: added.revision, instance: "gate-1", op: "add", definition: "merge-gate" }, "jason");
assert.deepEqual(gate.instances["gate-1"], { definition: "merge-gate", vars: {}, launched: false, launcher: false, protected: false });
const file = JSON.parse(readFileSync(f.file, "utf8"));
assert.deepEqual(file.roles["gate-1"], { definition: "merge-gate" });
assert.deepEqual(file.launch.instances, ["coder", "reviewer", "sec-1"]);
const run = f.validate();
assert.equal(run.status, 0, run.stderr);
assert.ok(JSON.parse(run.stdout).instances["sec-1"]);
const [first, second] = f.log().filter(e => e.state === "committed");
assert.equal(first.actor, "jason");
assert.equal(first.transaction, added.transaction);
assert.ok(first.changes.some(c => c.path === "roles.sec-1" && c.oldPresent === false && c.new.definition === "security-reviewer"));
assert.ok(first.changes.some(c => c.path === "launch.instances" && c.new.includes("sec-1") && !c.old.includes("sec-1")));
assert.equal(second.actor, "jason");
});
test("row 165 change: definition, vars and launch membership log before and after; only a definition change asks for a restart", () => {
const f = fixture(doc => { doc.roles["sec-1"] = { definition: "security-reviewer", vars: { harness: "pi" } }; });
const redefined = f.settings.set("acme", { revision: f.settings.read("acme").revision, instance: "sec-1", op: "change", definition: "merge-gate" }, "jason");
assert.equal(redefined.instances["sec-1"].definition, "merge-gate");
assert.equal(redefined.restart, true);
let entry = f.log().at(-1);
assert.ok(entry.changes.some(c => c.path === "roles.sec-1.definition" && c.old === "security-reviewer" && c.new === "merge-gate"));
const revarred = f.settings.set("acme", { revision: redefined.revision, instance: "sec-1", op: "change", vars: { harness: "claude-code", model: "claude-opus-5-5" } }, "jason");
assert.deepEqual(revarred.instances["sec-1"].vars, { harness: "claude-code", model: "claude-opus-5-5" });
assert.equal(revarred.restart, false);
entry = f.log().at(-1);
assert.ok(entry.changes.some(c => c.path === "roles.sec-1.vars.harness" && c.old === "pi" && c.new === "claude-code"));
assert.ok(entry.changes.some(c => c.path === "roles.sec-1.vars.model" && c.oldPresent === false && c.new === "claude-opus-5-5"));
const launched = f.settings.set("acme", { revision: revarred.revision, instance: "sec-1", op: "change", launch: true }, "jason");
assert.equal(launched.instances["sec-1"].launched, true);
assert.equal(launched.restart, false);
const dropped = f.settings.set("acme", { revision: launched.revision, instance: "reviewer", op: "change", launch: false }, "jason");
assert.deepEqual(JSON.parse(readFileSync(f.file, "utf8")).launch.instances, ["coder", "sec-1"]);
assert.deepEqual(f.log().at(-1).changes.find(c => c.path === "launch.instances"),
{ path: "launch.instances", old: ["coder", "reviewer", "sec-1"], new: ["coder", "sec-1"], oldPresent: true, newPresent: true });
assert.equal(f.settings.read("acme").revision, dropped.revision);
assert.equal(f.validate().status, 0);
});
test("row 165 refuses each validation failure and leaves the file's bytes alone", () => {
const f = fixture(doc => { doc.roles["sec-1"] = { definition: "security-reviewer" }; });
refuses(f, { instance: "pm", op: "add", definition: "merge-gate" }, "organization-instance-exists");
refuses(f, { instance: "ghost", op: "change", vars: {} }, "organization-unknown-instance");
refuses(f, { instance: "sec-2", op: "add", definition: "no-such-role" }, "BusinessError");
refuses(f, { instance: "sec-2", op: "add", definition: "researcher" }, "invalid-request");
refuses(f, { instance: "sec-2", op: "add", definition: "merge-gate", vars: { "no.such.var": 1 } }, "BusinessError");
refuses(f, { instance: "sec-2", op: "add", definition: "merge-gate", vars: { model: "not-in-the-table" } }, "BusinessError");
refuses(f, { instance: "chief", op: "change", launch: true }, "BusinessError");
refuses(f, { instance: "chief", op: "change", definition: "merge-gate" }, "BusinessError");
refuses(f, { instance: "coder", op: "change", launch: true }, "organization-no-change");
refuses(f, { instance: "sec-1", op: "change", definition: "security-reviewer" }, "organization-no-change");
for (const bad of [
{ instance: "Bad Name", op: "add", definition: "merge-gate" }, { instance: "sec-2", op: "remove" }, { instance: "sec-2", op: "add" },
{ instance: "sec-1", op: "change" }, { instance: "sec-2", op: "add", definition: "merge-gate", holder: "mos" },
{ instance: "sec-2", op: "add", definition: "merge-gate", credentials: {} }, { instance: "sec-2", op: "add", definition: "merge-gate", tracker: {} },
{ instance: "sec-2", op: "add", definition: "merge-gate", vars: [] }, { instance: "sec-2", op: "add", definition: "merge-gate", launch: "yes" },
]) refuses(f, bad, "invalid-request");
// Dewey F1: the broker refuses a business whose roles name human or tester, so the writer does too.
refuses(f, { instance: "human", op: "add", definition: "merge-gate" }, "invalid-request");
refuses(f, { instance: "tester", op: "add", definition: "merge-gate" }, "invalid-request");
const one = fixture(doc => { doc.launch.instances = ["coder"]; });
refuses(one, { instance: "coder", op: "change", launch: false }, "BusinessError");
const none = fixture(doc => { delete doc.launch; });
refuses(none, { instance: "sec-2", op: "add", definition: "merge-gate", launch: true }, "organization-launch-not-configured");
});
// mos-0823: no instance is written without its credential binding, and nothing binds one here.
test("row 165 refuses an added instance whose definition declares credentials, and says why", () => {
const f = fixture();
refuses(f, { instance: "reviewer-2", op: "add", definition: "reviewer" }, "organization-credentials-out-of-scope");
assert.throws(() => f.settings.set("acme", { revision: f.settings.read("acme").revision, instance: "reviewer-2", op: "add", definition: "reviewer" }, "jason"),
/reviewer declares gitea and vikunja credentials/);
});
test("row 165 refuses a definition change to or from one that declares credentials", () => {
const f = fixture(doc => { doc.roles["sec-1"] = { definition: "security-reviewer" }; });
refuses(f, { instance: "sec-1", op: "change", definition: "coder" }, "organization-credentials-out-of-scope");
refuses(f, { instance: "reviewer", op: "change", definition: "security-reviewer" }, "organization-credentials-out-of-scope");
refuses(f, { instance: "reviewer", op: "change", definition: "cto" }, "organization-credentials-out-of-scope");
});
test("row 165 a vars or launch change on an instance with its binding succeeds and keeps the binding", () => {
const f = fixture();
const before = JSON.parse(readFileSync(f.file, "utf8")).roles.coder;
const done = f.settings.set("acme", { revision: f.settings.read("acme").revision, instance: "coder", op: "change", vars: { harness: "claude-code" }, launch: false }, "jason");
assert.equal(done.restart, false);
const after = JSON.parse(readFileSync(f.file, "utf8")).roles.coder;
assert.deepEqual(after, { ...before, vars: { harness: "claude-code" } });
assert.deepEqual(after.credentials, before.credentials);
assert.deepEqual(after.tracker, before.tracker);
assert.equal(f.log().at(-1).changes.some(c => c.path.startsWith("roles.coder.credentials") || c.path.startsWith("roles.coder.tracker")), false);
assert.equal(f.validate().status, 0);
});
test("row 165 refuses an END_PROTECTED definition and every change to a protected instance", () => {
const f = fixture(doc => { doc.roles.assistant = { definition: "assistant", holder: "mos" }; doc.roles["sec-1"] = { definition: "security-reviewer" }; });
refuses(f, { instance: "assistant-2", op: "add", definition: "assistant" }, "organization-definition-refused");
refuses(f, { instance: "sec-1", op: "change", definition: "assistant" }, "organization-definition-refused");
refuses(f, { instance: "assistant", op: "change", definition: "merge-gate" }, "organization-definition-refused");
refuses(f, { instance: "assistant", op: "change", vars: { model: "claude-opus-5-5" } }, "organization-definition-refused");
refuses(f, { instance: "assistant", op: "change", launch: true }, "organization-definition-refused");
});
test("row 165 refuses while a project file doesn't resolve, as validate would", () => {
const ghost = fixture((doc, root) => writeJson(projectFilePath(join(root, "project")), { projectVersion: 1, id: "stack", roles: { ghost: { vars: {} } } }));
refuses(ghost, { instance: "sec-2", op: "add", definition: "merge-gate" }, "organization-unresolvable-instance");
const misnamed = fixture((doc, root) => writeJson(projectFilePath(join(root, "project")), { projectVersion: 1, id: "other" }));
refuses(misnamed, { instance: "sec-2", op: "add", definition: "merge-gate" }, "organization-project-mismatch");
const good = fixture((doc, root) => writeJson(projectFilePath(join(root, "project")), { projectVersion: 1, id: "stack", roles: { coder: { vars: {} } } }));
const done = good.settings.set("acme", { revision: good.settings.read("acme").revision, instance: "sec-2", op: "add", definition: "merge-gate" }, "jason");
assert.equal(done.instances["sec-2"].definition, "merge-gate");
});
test("row 165 refuses a stale revision and leaves the file's bytes alone", () => {
const f = fixture();
const stale = f.settings.read("acme").revision;
f.settings.set("acme", { revision: stale, instance: "sec-1", op: "add", definition: "security-reviewer" }, "jason");
const bytes = readFileSync(f.file, "utf8");
assert.throws(() => f.settings.set("acme", { revision: stale, instance: "gate-1", op: "add", definition: "merge-gate" }, "jason"), e => e.code === "revision-conflict");
assert.equal(readFileSync(f.file, "utf8"), bytes);
});