// #1873 lane triage-fence: through the broker, no organization gains the system-only Triage // template. Pack preview and apply, the Console hire start, the chief's hire.raise and // decision.raise, and instances.set add as the human and the tester each refuse with // template-system-only; the Console's hire template list omits triage. The coding pack still // previews as before. Fixtures live in a temp directory. import test from 'node:test'; import assert from 'node:assert/strict'; import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { Broker } from '../src/broker.mjs'; import { Store } from '../src/store.mjs'; import { pluginHash } from '../../plugins/src/pinned.mjs'; import { installPlugin } from '../../plugins/src/install.mjs'; import { starterPack as codingPack } from '../../plugins/starter-pack/coding/index.mjs'; import { templateHash } from '../../organization/src/definitions.mjs'; import { hireDecisions } from '../../organization/src/hire-decisions.mjs'; import { organizationWriter } from '../../organization/src/organization.mjs'; import { loadBusiness } from '../../organization/src/organization-config.mjs'; import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs'; const CODE = 'template-system-only'; const SUBJECT = { provider: 'test', iss: 'https://issuer.test', sub: 'owner' }; const ROUTE = { harness: 'pi', model: 'claude-opus-5-5' }; // The broker started as the runtime starts it, with the organization writer and installed packs. async function runtime(t) { const { startBroker } = await import('../src/runtime.mjs'); const root = mkdtempSync(join(tmpdir(), 'sobus-')); const rolesDir = rolesCopy(root); const doc = businessDoc(root, 'demo'); doc.plugins = { harness: ['pi'] }; doc.roles.chief.vars = { ...ROUTE }; writeJson(join(root, 'businesses/demo.json'), doc); const data = join(root, 'data'); mkdirSync(data, { mode: 0o700 }); const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery', 'organization.change'], crossRole: [] } } }; const businesses = { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles, vars: { 'bus.tester': true } } }; const bus = await startBroker({ dataRoot: data, businesses, organization: { dir: root, rolesDir, system: systemFor(root) } }); const thaw = (p) => { const st = lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { chmodSync(p, 0o700); for (const e of readdirSync(p)) thaw(join(p, e)); } }; t.after(async () => { await bus.close(); thaw(root); rmSync(root, { recursive: true, force: true }); }); const b = bus.broker; const owner = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT }); const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true }); const tester = b.bindTester({ business: 'demo' }); const call = async (verb, args = {}, cap = owner) => b.request(cap, { verb, args }); const file = () => readFileSync(join(root, 'businesses/demo.json'), 'utf8'); let n = 0; const install = (pack) => { const src = join(root, `src-${n++}`); mkdirSync(join(src, 'starter-pack'), { recursive: true }); writeFileSync(join(src, 'starter-pack', 'index.mjs'), `export const interfaceVersion = 1;\nexport function starterPack() { return ${JSON.stringify(pack)}; }\n`); const manifest = { name: pack.id, version: '1.0.0', kinds: ['starter-pack'], interfaceVersions: { 'starter-pack': 1 }, sha256: '0'.repeat(64), source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] }; writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest)); manifest.sha256 = pluginHash(src); writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest)); installPlugin({ dataRoot: data, source: src, expectedHash: manifest.sha256 }); }; return { b, root, rolesDir, owner, human, tester, call, file, install }; } // The A4 broker with the real hire service, so the chief's raise and the Console start reach it. function hires(t) { const root = mkdtempSync(join(tmpdir(), 'sohire-')), rolesDir = rolesCopy(root); writeJson(join(root, 'businesses/demo.json'), { ...businessDoc(root, 'demo'), plugins: { harness: ['pi'] } }); const storeRoot = join(root, 'data'); mkdirSync(storeRoot, { mode: 0o700 }); const writer = organizationWriter({ dir: root, rolesDir }), store = new Store(storeRoot); const service = hireDecisions({ writer, rolesDir, dataRoot: storeRoot, configDir: root, assertClean: () => {} }); const loaded = () => { const doc = loadBusiness('demo', { dir: root, rolesDir }); return { ...doc, roles: Object.fromEntries(Object.entries(doc.roles).map(([k, e]) => [k, { ...doc.definitions[e.definition], ...e }])) }; }; const b = new Broker({ store, businesses: { demo: loaded() }, hires: service }); t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); }); const owner = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT }); const chief = b.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi' }); b.request(chief, { verb: 'role.claim' }); const call = (cap, verb, args = {}) => b.request(cap, { verb, args }); const triage = { template: 'triage', sha256: templateHash(rolesDir, 'triage') }; const hire = (extra = {}) => ({ op: 'hire', level: 'org', name: 'sorter', role: triage, route: ROUTE, reason: 'Sort the inbox', ...extra }); const decisions = () => store.get('SELECT count(*) n FROM decisions').n; return { owner, chief, call, hire, decisions }; } const refuses = async (promise) => assert.rejects(Promise.resolve().then(() => promise), (e) => e?.code === CODE); test('pack preview refuses an installed pack that defines triage', async (t) => { const h = await runtime(t); const pack = { ...codingPack(), id: 'sorting', label: 'Sorting' }; pack.definitions = { ...pack.definitions, triage: { template: 'triage', sha256: templateHash(h.rolesDir, 'triage') } }; h.install(pack); const before = h.file(); await refuses(h.call('pack.preview.console', { pack: 'sorting' })); assert.equal(h.file(), before); }); test('pack apply refuses an installed pack that defines triage', async (t) => { const h = await runtime(t); const pack = { ...codingPack(), id: 'sorting', label: 'Sorting' }; pack.definitions = { ...pack.definitions, triage: { template: 'triage', sha256: templateHash(h.rolesDir, 'triage') } }; h.install(pack); const before = h.file(); await refuses(h.call('pack.apply.console', { pack: 'sorting', digest: 'a'.repeat(64) })); assert.equal(h.file(), before); }); test('instances.set add refuses triage for the human', async (t) => { const h = await runtime(t); const { revision } = await h.call('instances', {}, h.human); const before = h.file(); await refuses(h.call('instances.set', { revision, instance: 'sorter', op: 'add', definition: 'triage' }, h.human)); assert.equal(h.file(), before); }); test('instances.set add refuses triage for the tester', async (t) => { const h = await runtime(t); const { revision } = await h.call('instances', {}, h.human); const before = h.file(); await refuses(h.call('instances.set', { revision, instance: 'sorter', op: 'add', definition: 'triage' }, h.tester)); assert.equal(h.file(), before); }); test('the instances read offers no triage add', async (t) => { const h = await runtime(t); const view = await h.call('instances', {}, h.human); assert.equal(Object.hasOwn(view.definitions, 'triage'), false); }); test('the Console hire start refuses a triage template', async (t) => { const f = hires(t); await refuses(f.call(f.owner, 'hire.start.console', f.hire())); assert.equal(f.decisions(), 0); }); test('the chief hire.raise refuses a triage template at org, project and root level', async (t) => { const f = hires(t); await refuses(f.call(f.chief, 'hire.raise', f.hire())); await refuses(f.call(f.chief, 'hire.raise', f.hire({ level: 'project', project: 'stack' }))); await refuses(f.call(f.chief, 'hire.raise', f.hire({ level: 'root' }))); assert.equal(f.decisions(), 0); }); test('the chief decision.raise naming role.hire or role.change refuses a triage template', async (t) => { const f = hires(t); await refuses(f.call(f.chief, 'decision.raise', { action: 'role.hire', hire: f.hire() })); await refuses(f.call(f.chief, 'decision.raise', { action: 'role.change', hire: f.hire({ op: 'change', name: 'coder' }) })); assert.equal(f.decisions(), 0); }); test('the Console hire template list omits triage and keeps the rest', async (t) => { const f = hires(t); const names = (await f.call(f.owner, 'hire.templates.console')).templates.map((x) => x.name); assert.ok(!names.includes('triage')); assert.ok(names.includes('coder') && names.includes('reviewer')); }); // The guard: a shipped pack previews and applies as before, and an installed pack with no triage loads. test('the shipped coding pack and an installed pack without triage preview as before', async (t) => { const h = await runtime(t); const preview = await h.call('pack.preview.console', { pack: 'coding' }); assert.match(preview.digest, /^[a-f0-9]{64}$/); h.install({ ...codingPack(), id: 'plain', label: 'Plain' }); const plain = await h.call('pack.preview.console', { pack: 'plain' }); assert.match(plain.digest, /^[a-f0-9]{64}$/); const out = await h.call('pack.apply.console', { pack: 'coding', digest: preview.digest }); assert.deepEqual(out.hires.map((x) => [x.name, x.state]), [['pm-1', 'hired'], ['coder-1', 'hired'], ['reviewer-1', 'hired']]); });