返回 CodeWhale
agent-teams-gating.test.mjs
根目录 / crates / engine / extension-host / test / agent-teams-gating.test.mjs
1 import assert from 'node:assert/strict'
2 import { mkdtempSync, writeFileSync } from 'node:fs'
3 import { tmpdir } from 'node:os'
4 import { join } from 'node:path'
5 import test from 'node:test'
6 import { activate, startHost } from './harness.mjs'
7
8 const TEAMS_ENTRY = join(import.meta.dirname, '../../../plugins/assets/agent-teams/lib/index.js')
9
10 test('third-party agent-teams keeps DSH_COMPATIBILITY.md:117 subagents refusal', async () => {
11 const host = await startHost({ cwd: tmpdir() })
12 try {
13 const { result } = await activate(host, 'user/000000000000/agent-teams', TEAMS_ENTRY, {
14 data_dir: mkdtempSync(join(tmpdir(), 'tp-teams-')),
15 config: { stateDir: '.agent-teams', memberProvider: 'spawn' },
16 })
17 assert.equal(result.status, 'failed')
18 assert.match(result.diagnostic, /cannot install complete retired-member guard/)
19 } finally {
20 await host.stop()
21 }
22 })
23
24 test('third-party plugins keep agent/pre-step and root systemPrompt.section refusals', async () => {
25 const dir = mkdtempSync(join(tmpdir(), 'tp-refusal-'))
26 const preStepPath = join(dir, 'prestep.mjs')
27 const promptPath = join(dir, 'prompt.mjs')
28 writeFileSync(preStepPath, `
29 export function apply(ctx) {
30 ctx.on('agent/pre-step', async (_p, next) => next())
31 }
32 `)
33 writeFileSync(promptPath, `
34 export const inject = ['systemPrompt']
35 export function apply(ctx) {
36 ctx.systemPrompt.section({ name: 'test', order: 1, text: 'hi' })
37 }
38 `)
39 const host = await startHost({ cwd: dir })
40 try {
41 const r1 = await activate(host, 'user/000000000000/prestep', preStepPath, {
42 data_dir: mkdtempSync(join(tmpdir(), 'tp-ps-')),
43 })
44 assert.equal(r1.result.status, 'failed')
45 assert.match(r1.result.diagnostic, /agent\/pre-step is not projected/)
46
47 const r2 = await activate(host, 'user/000000000000/prompt', promptPath, {
48 data_dir: mkdtempSync(join(tmpdir(), 'tp-sp-')),
49 })
50 assert.equal(r2.result.status, 'failed')
51 assert.match(r2.result.diagnostic, /child agent's agent\/created context/)
52 } finally {
53 await host.stop()
54 }
55 })
56
57 test('bundled @nanmicoder/dsh-agent-teams@0.1.29 cold-activates cleanly with all tools, command, prompt sections, and pre-step hooks', async () => {
58 const host = await startHost({ cwd: tmpdir() })
59 try {
60 const { result } = await activate(host, 'builtin/000000000000/agent-teams', TEAMS_ENTRY, {
61 data_dir: mkdtempSync(join(tmpdir(), 'bp-teams-')),
62 config: { stateDir: '.agent-teams', memberProvider: 'spawn' },
63 bundled_first_party: true,
64 })
65 assert.equal(result.status, 'ok', result.diagnostic)
66 assert.equal(result.tools.length, 14)
67 assert.ok(result.tools.includes('agent_teams_create'))
68 assert.deepEqual(result.commands, ['agent-teams'])
69 const promptSections = host.registry.filter(r => r.op === 'register' && r.kind === 'prompt_section')
70 assert.ok(promptSections.length >= 2)
71 const preStepHooks = host.registry.filter(r => r.op === 'register' && r.kind === 'hook' && r.spec?.name === 'agent/pre-step')
72 assert.equal(preStepHooks.length, 2)
73 } finally {
74 await host.stop()
75 }
76 })
77
78 test('bundled first-party subagents view is owner-scoped and routes delivery to subagent/prompt', async () => {
79 const dir = mkdtempSync(join(tmpdir(), 'bp-subagents-'))
80 const bundledPath = join(dir, 'bundled.mjs')
81 const thirdPartyPath = join(dir, 'third-party.mjs')
82 writeFileSync(bundledPath, `
83 export const inject = ['tools', 'subagents']
84 export function apply(ctx) {
85 const queueSym = Symbol.for('dsh.subagent.queuePrompt')
86 const deliverSym = Symbol.for('dsh.subagent.deliverPrompt')
87 if (typeof ctx.subagents.followup !== 'function') throw new Error('missing followup')
88 if (typeof ctx.subagents.sendMessage !== 'function') throw new Error('missing sendMessage')
89 if (typeof ctx.subagents[queueSym] !== 'function') throw new Error('missing queuePrompt')
90 if (typeof ctx.subagents[deliverSym] !== 'function') throw new Error('missing deliverPrompt')
91 const orig = ctx.subagents.followup
92 ctx.subagents.followup = async (...args) => orig.apply(ctx.subagents, args)
93 ctx.tools.register({
94 name: 'deliver_probe',
95 description: 'probe subagent delivery',
96 inputSchema: { type: 'object', properties: {} },
97 async execute(_input, exec) {
98 const m1 = await ctx.subagents.followup(exec.agent, 'child-1', [{ type: 'text', text: 'hi' }])
99 const m2 = await ctx.subagents.sendMessage(exec.agent, 'child-1', [{ type: 'text', text: 'msg' }])
100 const m3 = await ctx.subagents[queueSym](exec.agent, 'child-1', [{ type: 'text', text: 'q' }], { kind: 'agent-teams' })
101 const m4 = await ctx.subagents[deliverSym](exec.agent, 'child-1', [{ type: 'text', text: 's' }], { kind: 'agent-teams' }, undefined, 'steer')
102 return { content: [{ type: 'text', text: JSON.stringify([m1, m2, m3, m4]) }] }
103 },
104 })
105 }
106 `)
107 writeFileSync(thirdPartyPath, `
108 export const inject = ['subagents']
109 export function apply(ctx) {
110 if (ctx.subagents.followup !== undefined) throw new Error('third-party saw followup')
111 if (ctx.subagents.sendMessage !== undefined) throw new Error('third-party saw sendMessage')
112 if (ctx.subagents[Symbol.for('dsh.subagent.queuePrompt')] !== undefined) throw new Error('third-party saw queuePrompt')
113 if (ctx.subagents[Symbol.for('dsh.subagent.deliverPrompt')] !== undefined) throw new Error('third-party saw deliverPrompt')
114 }
115 `)
116 const prompts = []
117 const host = await startHost({
118 cwd: dir,
119 subagent(params, _id, method) {
120 if (method === 'subagent/prompt') {
121 prompts.push({ child_id: params.child_id, prompt: params.prompt, mode: params.mode })
122 return { child_id: params.child_id, message_id: `mid-${prompts.length}` }
123 }
124 return { error: { code: -32002, message: 'unexpected method ' + method } }
125 },
126 })
127 try {
128 const b = await activate(host, 'builtin/000000000000/agent-teams', bundledPath, {
129 data_dir: mkdtempSync(join(tmpdir(), 'bp-d-')),
130 bundled_first_party: true,
131 })
132 assert.equal(b.result.status, 'ok', b.result.diagnostic)
133 const tp = await activate(host, 'user/000000000000/other', thirdPartyPath, {
134 data_dir: mkdtempSync(join(tmpdir(), 'tp-d-')),
135 })
136 assert.equal(tp.result.status, 'ok', tp.result.diagnostic)
137 const reg = host.registry.find(r => r.op === 'register' && r.spec?.name === 'deliver_probe')
138 assert.ok(reg)
139 const res = await host.call('tool/call', {
140 handle: reg.handle,
141 call_id: 'c1',
142 input: {},
143 deadline_ms: 5000,
144 subagent_ticket: 'sub-tkt',
145 session_id: 's1',
146 workspace: dir,
147 })
148 assert.equal(res.is_error, false)
149 assert.deepEqual(prompts, [
150 { child_id: 'child-1', prompt: 'hi', mode: 'followup' },
151 { child_id: 'child-1', prompt: 'msg', mode: 'message' },
152 { child_id: 'child-1', prompt: 'q', mode: 'queue' },
153 { child_id: 'child-1', prompt: 's', mode: 'steer' },
154 ])
155 } finally {
156 await host.stop()
157 }
158 })
159
159 lines Plain Text