| 1 | import { test } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 4 | import { tmpdir } from 'node:os' |
| 5 | import { join } from 'node:path' |
| 6 | import { activate, startHost } from './harness.mjs' |
| 7 | import { ErrorCode } from '../dist/protocol.mjs' |
| 8 | |
| 9 | const PLUGIN = ` |
| 10 | export const inject = ['tools', 'subagents', 'agents'] |
| 11 | export function apply(ctx) { |
| 12 | ctx.on('agent/created', ({ agent }) => { |
| 13 | agent.ctx.systemPrompt.section({ name: 'deployment:persona-prefix', order: agent.ctx.systemPrompt.getSectionOrder('DEPLOYMENT_PERSONA_PREFIX'), text: 'Reviewer persona' }) |
| 14 | agent.ctx.tools.restrict({ deny: ['exec_shell'] }) |
| 15 | }) |
| 16 | ctx.tools.register({ name: 'team_start', description: 'Start one child agent.', parameters: { type: 'object' }, |
| 17 | async execute(input, exec) { |
| 18 | try { |
| 19 | const parent = input.forged ? { id: exec.agent?.id } : exec.agent |
| 20 | const handle = await ctx.subagents.startActivation({ |
| 21 | provider: 'spawn', childId: input.childId ?? 'reviewer-1', delivery: 'caller', |
| 22 | request: { parent, prompt: [{ type: 'text', text: 'Review the diff' }] }, |
| 23 | }) |
| 24 | const result = await handle.result |
| 25 | await handle.dispose() |
| 26 | return { messageId: handle.messageId, stopReason: result.stopReason, text: result.output.map(block => block.text).join('') } |
| 27 | } catch (error) { |
| 28 | return { refused: error.code ?? String(error) } |
| 29 | } |
| 30 | }, |
| 31 | }) |
| 32 | } |
| 33 | ` |
| 34 | |
| 35 | test('the agents adapters serve subagents and agents through the reviewed adapter table', async (t) => { |
| 36 | const host = await startHost({ |
| 37 | subagent: (params, _id, method) => method === 'subagent/start' |
| 38 | ? { child_id: params.child_id, message_id: 'message-1' } |
| 39 | : method === 'subagent/result' |
| 40 | ? { stop_reason: 'end_turn', output: [{ type: 'text', text: 'looks good' }], omitted_blocks: 0 } |
| 41 | : {}, |
| 42 | }) |
| 43 | t.after(() => host.stop()) |
| 44 | const dir = mkdtempSync(join(tmpdir(), 'cw-agents-')) |
| 45 | t.after(() => rmSync(dir, { recursive: true, force: true })) |
| 46 | const entry = join(dir, 'index.mjs') |
| 47 | writeFileSync(entry, PLUGIN) |
| 48 | const { ref, result } = await activate(host, 'agent-team', entry) |
| 49 | assert.deepEqual(result, { status: 'ok', tools: ['team_start'], commands: [] }) |
| 50 | const handle = host.registry.find(row => row.op === 'register' && row.spec.name === 'team_start').handle |
| 51 | const call = (input, identity) => host.call('tool/call', { handle, call_id: 'team', input, deadline_ms: 5000, ...identity }) |
| 52 | const root = { session_id: 'session-root', workspace: '/workspace/team', subagent_ticket: 'subagent-ticket-1' } |
| 53 | |
| 54 | assert.deepEqual((await call({}, root)).structured, { messageId: 'message-1', stopReason: 'end_turn', text: 'looks good' }) |
| 55 | assert.deepEqual(host.subagents.map(row => row.method), ['subagent/start', 'subagent/result', 'subagent/dispose']) |
| 56 | const [start] = host.subagents |
| 57 | assert.deepEqual(start.owner, ref) |
| 58 | assert.equal(start.ticket, 'subagent-ticket-1') |
| 59 | assert.equal(start.child_id, 'reviewer-1') |
| 60 | assert.equal(start.prompt, 'Review the diff') |
| 61 | assert.equal(start.persona, 'Reviewer persona') |
| 62 | assert.deepEqual(start.denied_tools, ['exec_shell']) |
| 63 | assert.equal(start.delivery, 'caller') |
| 64 | assert.ok(host.subagents.every(row => row.ticket === 'subagent-ticket-1' && row.child_id === 'reviewer-1')) |
| 65 | |
| 66 | const before = host.subagents.length |
| 67 | assert.deepEqual((await call({}, { ...root, agent_id: 'agent-child' })).structured, { refused: ErrorCode.NotAvailable }, 'a delegated caller cannot nest child agents') |
| 68 | assert.deepEqual((await call({}, { session_id: 'session-root', workspace: '/workspace/team' })).structured, { refused: ErrorCode.NotAvailable }, 'no subagent ticket, no child') |
| 69 | assert.deepEqual((await call({ forged: true }, root)).structured, { refused: ErrorCode.NotAvailable }, 'a forged parent is not the live caller') |
| 70 | assert.deepEqual((await call({ childId: 'bad id' }, root)).structured, { refused: ErrorCode.InvalidParams }) |
| 71 | assert.equal(host.subagents.length, before, 'refused activations never reach the core') |
| 72 | }) |
| 73 |