import test from 'node:test'; import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import { spawn } from 'node:child_process'; import { mkdirSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import { createInterface } from 'node:readline'; import { buildBundle, SOURCES } from '../src/bundle.mjs'; import { callTool, typedTools } from '../src/tools.mjs'; import * as runner from '../src/runner.mjs'; import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs'; import { Broker } from '../../bus/src/broker.mjs'; import { Store } from '../../bus/src/store.mjs'; import { createRequestStore } from '../../bus/src/request-store.mjs'; import { createRequestLifecycle } from '../../bus/src/request-lifecycle.mjs'; const reads = ['intake_status', 'intake_projects']; const writes = ['intake_open', 'intake_propose']; const intake = t => t.name.startsWith('intake_'); const shipped = name => JSON.parse(readFileSync(join(REPO, 'roles', `${name}.json`), 'utf8')); const roleDefinition = role => role === 'chief' ? 'chief-of-staff' : role; function bundle(t, role, harness, mutate) { const resolved = resolvedFor(t, role, doc => { if (role === 'assistant') doc.roles.assistant = { definition: 'assistant' }; doc.roles[role].vars = { ...doc.roles[role].vars, harness, model: 'claude-opus-5-5' }; mutate?.(doc); }); const dir = scratch(t), workspace = join(dir, 'ws'); mkdirSync(workspace); return buildBundle({ dir: join(dir, 'bundle'), resolved, contract: '# Fixture contract\n', tracker: false, credentials: [], skillsDir: REPO_SKILLS, harness: HARNESS[harness], harnessVersion: 'fixture', session: { run: 'intake-run', workspace, toolSocket: join(dir, 'tools.sock'), turnMarker: join(dir, 'turn.done') } }); } for (const harness of ['pi', 'claude-code']) { for (const role of ['pm', 'cto', 'coder', 'reviewer', 'assistant']) { test(`${harness}: ${role} actual pre-task manifest exposes only its intake tools`, t => { const b = bundle(t, role, harness); const expected = role === 'pm' ? [...reads, ...writes] : role === 'cto' ? reads : []; const prefix = harness === 'pi' ? '' : 'mcp__mosaic__'; assert.deepEqual(b.manifest.typed.filter(n => n.startsWith(prefix + 'intake_')).sort(), expected.map(n => prefix + n).sort()); const tools = JSON.parse(readFileSync(b.files.tools, 'utf8')); assert.deepEqual(tools.filter(intake).map(t => t.name).sort(), expected.toSorted()); assert.deepEqual(b.policy.typed.filter(n => n.startsWith('intake_')).sort(), expected.toSorted()); assert.ok(!tools.some(t => t.name === 'intake_question'), 'no backing request.question verb'); for (const tool of tools.filter(intake)) { assert.equal(tool.parameters.additionalProperties, false); assert.equal(tool.parameters.properties.request.type, 'string'); } assert.ok(!tools.some(t => t.name === 'task_create'), 'discovery needs no tracker'); }); } } test('PM intake writes and staff request follow the existing message.send grant', t => { const muted = bundle(t, 'pm', 'pi', doc => { doc.roles.pm.vars['limits.authority'] = ['task.assign']; }); assert.deepEqual(muted.tools.filter(intake).map(t => t.name).sort(), reads.toSorted()); assert.ok(!muted.tools.some(t => t.name === 'request_staff')); }); async function setup(t, harness = 'pi', { mute = false } = {}) { const root = scratch(t, 'request-tools-'), store = new Store(root); t.after(() => store.close()); const roles = Object.fromEntries(['pm', 'cto', 'coder', 'reviewer', 'assistant', 'chief'].map(role => { const definition = roleDefinition(role); return [role, { definition, authority: mute && role === 'pm' ? { withinRole: [], crossRole: [] } : shipped(definition).authority }]; })); const business = { id: 'demo', human: 'jason', roles, definitions: Object.fromEntries(Object.keys(roles).map(r => [roleDefinition(r), shipped(roleDefinition(r))])), arbiters: { technical: 'cto', delivery: 'pm' }, launch: { by: 'chief', instances: ['pm', 'cto', 'coder', 'reviewer'], models: {}, limits: {} }, projects: { factory: { root: join(root, 'factory') } } }; const journal = createRequestStore(store); const broker = new Broker({ store, businesses: { demo: business }, requestLifecycle: services => createRequestLifecycle({ journal, services: { ...services, listProjects: () => [{ business: 'demo', project: 'factory', name: 'Factory', tracker: { root: 1, project: 2 } }], preparePlans: (state, args, source) => ['autonomous', 'guided'].map(mode => ({ version: 1, business: 'demo', request: state.request, project: args.project, name: args.name, chat_project: null, mode, organization_revision: 'fixture-revision', source: { project: args.source_project, commit: 'a'.repeat(40), remote: null, forks: [] }, code_root: join(root, 'products/demo', args.project, 'code'), records_root: join(root, 'projects/demo', args.project), tracker: { root: 1, project: args.tracker_project }, staff: { pm: 'pm', technical_reviewer: 'cto', coder: 'coder', reviewer: 'reviewer', chief: 'chief' }, intent: { text: source.text, sha256: createHash('sha256').update(source.text).digest('hex'), source_message: source.source_message, generated: false }, project_vars: {}, existing: false, })), admitPlan: () => ({ ready: true }), } }) }); const caps = { human: broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true }) }; const call = (role, verb, args = {}) => broker.request(caps[role], { verb, args }); const project = call('human', 'project.create', { name: 'Intake' }).id; const thread = call('human', 'thread.create', { project, name: 'New product' }).id; for (const role of Object.keys(roles)) { caps[role] = broker.bindLaunch({ business: 'demo', role, run: role + '-run', harness, ...(role === 'assistant' ? { thread } : {}) }); call(role, 'role.claim'); } const source = call('human', 'thread.send', { thread, body: 'Build a single-file timer.\nKeep the original intent.\n' }); const relay = call('assistant', 'message.send', { to: 'chief', body: `New product request ${source.request}`, in_reply_to: source.id }); call('chief', 'message.send', { to: 'pm', body: `Open request ${source.request}`, in_reply_to: relay.id }); const pm = bundle(t, 'pm', harness); assert.equal(typeof runner.toolServer, 'function', 'exercise the production runner dispatch'); const socket = join(root, 'tools.sock'); const server = await runner.toolServer({ path: socket, tools: pm.tools, local: {}, client: { call: (verb, args) => call('pm', verb, args) } }); t.after(() => new Promise(resolve => server.close(resolve))); return { root, store, broker, caps, call, source, pm, socket, tool: (name, args) => callTool(socket, name, args) }; } function mcp(t, toolsFile, socket) { const child = spawn(process.execPath, [SOURCES.mcpServer, toolsFile, socket], { stdio: ['pipe', 'pipe', 'pipe'] }); const waiting = new Map(); let next = 0; createInterface({ input: child.stdout }).on('line', line => { const m = JSON.parse(line); waiting.get(m.id)?.(m); waiting.delete(m.id); }); t.after(async () => { const exited = new Promise(resolve => child.once('exit', resolve)); child.stdin.end(); await exited; }); return async (name, args) => { const id = ++next; const response = new Promise(resolve => waiting.set(id, resolve)); child.stdin.write(JSON.stringify({ jsonrpc: '2.0', id, method: 'tools/call', params: { name, arguments: args } }) + '\n'); const reply = await response; assert.ok(!reply.error, reply.error?.message); const result = reply.result; assert.ok(!result.isError, result.content[0].text); return JSON.parse(result.content[0].text); }; } for (const harness of ['pi', 'claude-code']) { test(`${harness}: assistant through chief reaches PM intake and a pre-task new-project decision`, async t => { const x = await setup(t, harness); const tool = harness === 'claude-code' ? mcp(t, x.pm.files.tools, x.socket) : x.tool; const args = { request: x.source.request }; const opened = await tool('intake_open', args); assert.equal(opened.source_message, x.source.id); assert.equal(opened.phase, 'selection'); assert.deepEqual(opened.tasks, []); assert.deepEqual(await tool('intake_open', args), opened); assert.deepEqual(await tool('intake_status', args), opened); assert.deepEqual(await tool('intake_projects', args), [{ business: 'demo', project: 'factory', name: 'Factory', tracker: { root: 1, project: 2 } }]); const proposed = await tool('intake_propose', { ...args, revision: opened.revision, op: 'timer-plan', source_project: 'factory', project: 'timer', name: 'Timer', tracker_project: 2 }); const d = x.store.get('SELECT * FROM decisions WHERE id=?', proposed.selection.decision); assert.equal(d.action, 'project.bootstrap'); assert.equal(d.route_to, 'human'); assert.equal(d.blocking, 0); assert.equal(d.task_ref, null); assert.equal(d.requirement_ref, null); assert.deepEqual(JSON.parse(d.options).map(o => o.key), ['autonomous', 'guided', 'existing']); assert.equal(x.store.get("SELECT count(*) n FROM events WHERE kind='task.created'").n, 0); assert.equal(x.store.get('SELECT body FROM messages WHERE id=?', x.source.id).body, 'Build a single-file timer.\nKeep the original intent.\n'); assert.throws(() => x.call('pm', 'decision.resolve', { id: d.id, choice: 'guided' }), /human-required/); }); } test('pre-task request_staff reaches the chief with roles and original request evidence', async t => { const x = await setup(t); const sent = await x.tool('request_staff', { request: x.source.request, roles: [{ role: 'cto', count: 1 }], note: 'Discovery before tasks' }); assert.equal(sent.to, 'chief'); const message = x.call('chief', 'message.receive').find(m => m.id === sent.message); assert.equal(message.class, 'STAFF-REQUEST'); assert.match(message.body, new RegExp(x.source.request)); assert.match(message.body, /cto x1/); assert.match(message.body, /Discovery before tasks/); const body = JSON.parse(x.store.get("SELECT body FROM events WHERE kind='action.allowed' AND json_extract(body,'$.operation')='staff.request'").body); assert.equal(body.staff_for, x.source.request); assert.equal(body.action, 'message.send'); assert.equal(x.store.get('SELECT count(*) n FROM request_events').n, 0, 'staffing does not require an opened intake'); const task = await x.tool('request_staff', { task_ref: 'vikunja:1/2', roles: [{ role: 'coder', count: 1 }] }); assert.match(x.store.get('SELECT body FROM messages WHERE id=?', task.message).body, /Staff request for vikunja:1\/2:/); }); test('request_staff rejects both/neither references, invalid or unreachable requests and wrong roles', async t => { const x = await setup(t), roles = [{ role: 'coder', count: 1 }]; for (const args of [{ roles }, { roles, task_ref: 'vikunja:1/2', request: x.source.request }, { roles, request: '' }, { roles, request: null }, { roles, request: 'absent' }, { roles, request: x.source.id }]) await assert.rejects(x.tool('request_staff', args), /invalid-request|request-origin-refused/); const foreign = x.call('human', 'message.send', { to: 'cto', body: 'Unrelated request' }); await assert.rejects(x.tool('request_staff', { roles, request: foreign.request }), /request-origin-refused/); for (const role of ['cto', 'coder', 'reviewer', 'assistant', 'chief']) assert.throws(() => x.call(role, 'staff.request', { roles, request: x.source.request }), /staff-request-refused/); assert.equal(x.store.get("SELECT count(*) n FROM messages WHERE class='STAFF-REQUEST'").n, 0); }); test('broker keeps wrong-role intake refusals, including the existing PM-only read boundary', async t => { const x = await setup(t), args = { request: x.source.request }; const opened = await x.tool('intake_open', args); for (const role of ['cto', 'coder', 'reviewer', 'assistant', 'chief']) { for (const verb of ['request.open', 'request.get', 'request.projects']) await assert.rejects(async () => x.call(role, verb, args), /request-access-refused/); await assert.rejects(async () => x.call(role, 'request.propose', { ...args, revision: opened.revision, op: 'bad-role', source_project: 'factory', project: 'timer', name: 'Timer', tracker_project: 2 }), /request-access-refused/); } await assert.rejects(x.tool('intake_open', { request: x.source.id }), /request-origin-refused/); await assert.rejects(x.tool('intake_status', { ...args, extra: true }), /invalid-request/); await assert.rejects(x.tool('intake_propose', { ...args, revision: 0, op: 'bad-revision', source_project: 'factory', project: 'timer', name: 'Timer', tracker_project: 2 }), /invalid-request/); await assert.rejects(x.tool('intake_question', args), /unknown-tool/); }); test('pre-task staffing still requires PM message.send authority', async t => { const x = await setup(t, 'pi', { mute: true }); await assert.rejects(x.tool('request_staff', { request: x.source.request, roles: [{ role: 'cto', count: 1 }] }), /decision-required/); assert.equal(x.store.get("SELECT count(*) n FROM messages WHERE class='STAFF-REQUEST'").n, 0); }); test('assistant cannot coordinate directly with PM or reviewer even with a request citation', async t => { const x = await setup(t); for (const to of ['pm', 'reviewer']) assert.throws(() => x.call('assistant', 'message.send', { to, body: `Request ${x.source.request}`, in_reply_to: x.source.id }), /message-recipient-refused/); });