tool-subagent-control.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import { mkdtempSync, rmSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { Context } from '@deepseek-ai/cordis'
  6. import { CallId } from '@deepseek-ai/dsh-llm'
  7. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  8. import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
  9. import { SessionId } from '@deepseek-ai/dsh-session'
  10. import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl'
  11. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  12. import SubagentService from '@deepseek-ai/dsh-subagent'
  13. import * as SubagentSpawn from '@deepseek-ai/dsh-subagent-spawn'
  14. import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
  15. import { LlmAdapter } from '@deepseek-ai/dsh-llm'
  16. import { MockAdapter, textResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  17. import * as tool from '../src/index.ts'
  18. import { parkParent } from './park-parent.ts'
  19. /** One scripted response that may wait on a caller-released gate before streaming. */
  20. interface GatedEntry {
  21. chunks: StreamChunk[]
  22. gate?: Promise<undefined>
  23. }
  24. /** Adapter whose entries can hold a model call open until the test releases it. */
  25. class GatedAdapter extends LlmAdapter {
  26. readonly requests: GenerateOptions[] = []
  27. constructor(private script: GatedEntry[]) {
  28. super()
  29. }
  30. async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  31. this.requests.push(options)
  32. const entry = this.script.shift()
  33. if (!entry) throw new Error('GatedAdapter: script exhausted')
  34. if (entry.gate) await entry.gate
  35. for (const chunk of entry.chunks) {
  36. if (options.signal?.aborted) throw new Error('aborted')
  37. yield chunk
  38. }
  39. }
  40. }
  41. const testToolSignal = new AbortController().signal
  42. const roots: string[] = []
  43. afterEach(() => {
  44. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  45. })
  46. async function setupWith(adapter: MockAdapter | GatedAdapter) {
  47. const ctx = new Context()
  48. await mountAgentLoopTestDependencies(ctx)
  49. const root = mkdtempSync(join(tmpdir(), 'dsh-tool-subagent-control-'))
  50. roots.push(root)
  51. await ctx.plugin(JsonlSessionPersistence, { root })
  52. await ctx.plugin(AgentLoop, { agents: [] })
  53. await ctx.plugin(SessionProjectionRegistry)
  54. await ctx.plugin(SubagentService)
  55. await ctx.plugin(SubagentSpawn, { providerName: 'spawn' })
  56. await ctx.plugin(tool)
  57. ctx.llm.registerAdapter(['mock'], adapter)
  58. const parent = ctx.agentLoop.create(SessionId('parent'), { provider: 'mock', model: 'mock' })
  59. parkParent(ctx, parent)
  60. return { ctx, parent, adapter }
  61. }
  62. async function setup(script: ConstructorParameters<typeof MockAdapter>[0]) {
  63. return setupWith(new MockAdapter(script))
  64. }
  65. function text(result: { content: { type: string; text?: string }[] }): string {
  66. return result.content.filter(block => block.type === 'text').map(block => block.text).join('')
  67. }
  68. let calls = 0
  69. function callTool(
  70. ctx: Context,
  71. name: string,
  72. args: unknown,
  73. agent?: unknown,
  74. signal: AbortSignal = testToolSignal,
  75. ) {
  76. return ctx.tools.execute({
  77. signal,
  78. callId: CallId(`call-${++calls}`),
  79. name,
  80. arguments: args,
  81. ...agent !== undefined ? { agent: agent as never } : {},
  82. })
  83. }
  84. /** Wait until a child's Activation released its handle. */
  85. async function waitNoActivation(ctx: Context, childId: SessionId): Promise<void> {
  86. await vi.waitFor(() => {
  87. expect(ctx.agents.get(childId)).toBeUndefined()
  88. }, { timeout: 5_000 })
  89. }
  90. describe('dsh-tool-subagent-control', () => {
  91. it('registers send_message once, globally, with the two required parameters', async () => {
  92. const { ctx } = await setup([])
  93. const schemas = ctx.tools.schemas().filter(schema => schema.name === 'send_message')
  94. expect(schemas).toHaveLength(1)
  95. const props = (schemas[0]!.parameters as { properties?: Record<string, unknown> }).properties ?? {}
  96. expect(Object.keys(props).sort()).toEqual(['message', 'subagent_id'])
  97. // The continuable path has no Task, so the schema must not promise one.
  98. expect(schemas[0]!.description).not.toContain('task_output')
  99. expect(schemas[0]!.description).not.toContain('task id')
  100. // Follow-up ordering is model-visible: it cannot redirect the open turn.
  101. expect(schemas[0]!.description).toContain('next turn')
  102. })
  103. it('cold-resumes a settled child and reports the queued next turn', async () => {
  104. const { ctx, parent } = await setup([textResponse('first answer'), textResponse('second answer')])
  105. const started = await ctx.subagents.startContinuable({
  106. provider: 'spawn',
  107. label: 'child task',
  108. request: { prompt: [{ type: 'text', text: 'child task' }], parent },
  109. signal: testToolSignal,
  110. })
  111. await waitNoActivation(ctx, started.childId)
  112. const result = await callTool(ctx, 'send_message', {
  113. subagent_id: started.childId,
  114. message: 'and then?',
  115. }, parent)
  116. expect(result.isError).toBe(false)
  117. expect(text(result)).toBe(`message queued as the next turn for subagent ${started.childId}`)
  118. await waitNoActivation(ctx, started.childId)
  119. const loaded = await ctx.sessionPersistence.load(started.childId)
  120. const followUp = loaded.events.findLast(event => event.type === 'user/message')
  121. // The durable message source records the calling agent without granting authority.
  122. expect(followUp?.type === 'user/message' && followUp.data.source).toEqual({
  123. kind: 'coordinator',
  124. form: 'relay',
  125. senderSessionId: parent.id,
  126. })
  127. })
  128. it('queues behind an open turn instead of joining it', async () => {
  129. const { ctx, parent, adapter } = await setup([textResponse('first'), textResponse('second')])
  130. const started = await ctx.subagents.startContinuable({
  131. provider: 'spawn',
  132. label: 'long work',
  133. request: { prompt: [{ type: 'text', text: 'long work' }], parent },
  134. signal: testToolSignal,
  135. })
  136. await vi.waitFor(() => { expect(adapter.requests).toHaveLength(1) })
  137. const result = await callTool(ctx, 'send_message', {
  138. subagent_id: started.childId,
  139. message: 'also consider Y',
  140. }, parent)
  141. expect(result.isError).toBe(false)
  142. await waitNoActivation(ctx, started.childId)
  143. const loaded = await ctx.sessionPersistence.load(started.childId)
  144. const prompts = loaded.events.flatMap(event => event.type === 'user/message' && event.data.source.kind !== 'plugin'
  145. ? event.data.content.flatMap(block => block.type === 'text' ? [block.text] : [])
  146. : [])
  147. // A follow-up is its own later turn, never steering inside the first one.
  148. expect(prompts).toEqual(['long work', 'also consider Y'])
  149. })
  150. it('reports a delivery failure as an errored, not-delivered result', async () => {
  151. const { ctx, parent } = await setup([])
  152. const result = await callTool(ctx, 'send_message', {
  153. subagent_id: 'no-such-child',
  154. message: 'hello?',
  155. }, parent)
  156. expect(result.isError).toBe(true)
  157. expect(text(result)).toContain('unavailable')
  158. })
  159. it('rejects a caller that is not the child\'s durable direct parent', async () => {
  160. const { ctx, parent } = await setup([textResponse('first')])
  161. const started = await ctx.subagents.startContinuable({
  162. provider: 'spawn',
  163. label: 'child task',
  164. request: { prompt: [{ type: 'text', text: 'child task' }], parent },
  165. signal: testToolSignal,
  166. })
  167. await waitNoActivation(ctx, started.childId)
  168. const stranger = ctx.agentLoop.create(SessionId('stranger'), { provider: 'mock', model: 'mock' })
  169. const result = await callTool(ctx, 'send_message', {
  170. subagent_id: started.childId,
  171. message: 'mine now',
  172. }, stranger)
  173. expect(result.isError).toBe(true)
  174. expect(text(result)).toContain('another parent session')
  175. })
  176. it('fails loud when invoked without a calling agent', async () => {
  177. const { ctx } = await setup([])
  178. const result = await callTool(ctx, 'send_message', { subagent_id: 'x', message: 'y' })
  179. expect(result.isError).toBe(true)
  180. expect(text(result)).toContain('requires a calling agent')
  181. })
  182. it('unregisters with its plugin fiber (HMR safety)', async () => {
  183. const ctx = new Context()
  184. await mountAgentLoopTestDependencies(ctx)
  185. await ctx.plugin(AgentLoop, { agents: [] })
  186. await ctx.plugin(SubagentService)
  187. const fiber = await ctx.plugin(tool)
  188. expect(ctx.tools.schemas().some(schema => schema.name === 'send_message')).toBe(true)
  189. expect(ctx.tools.schemas().some(schema => schema.name === 'interrupt_agent')).toBe(true)
  190. await fiber.dispose()
  191. expect(ctx.tools.schemas().some(schema => schema.name === 'send_message')).toBe(false)
  192. expect(ctx.tools.schemas().some(schema => schema.name === 'interrupt_agent')).toBe(false)
  193. })
  194. it('has the namespace-plugin export shape (no stray default)', () => {
  195. expect('default' in tool).toBe(false)
  196. expect(tool.name).toBe('tool-subagent-control')
  197. expect(tool.inject).toEqual(['tools', 'subagents'])
  198. expect(typeof tool.apply).toBe('function')
  199. })
  200. })
  201. describe('dsh-tool-subagent-control interrupt_agent', () => {
  202. it('registers interrupt_agent with the single agent_id parameter and current-turn wording', async () => {
  203. const { ctx } = await setup([])
  204. const schemas = ctx.tools.schemas().filter(schema => schema.name === 'interrupt_agent')
  205. expect(schemas).toHaveLength(1)
  206. const props = (schemas[0]!.parameters as { properties?: Record<string, unknown> }).properties ?? {}
  207. expect(Object.keys(props)).toEqual(['agent_id'])
  208. expect(schemas[0]!.description).toContain('current turn')
  209. expect(schemas[0]!.description).toContain('send_message')
  210. })
  211. it('interrupts a running direct child with the parent cause, parking its queue', async () => {
  212. const releaseFirst = Promise.withResolvers<undefined>()
  213. const adapter = new GatedAdapter([
  214. { chunks: textResponse('held'), gate: releaseFirst.promise },
  215. { chunks: textResponse('parked answer') },
  216. { chunks: textResponse('waking answer') },
  217. ])
  218. const { ctx, parent } = await setupWith(adapter)
  219. const started = await ctx.subagents.startContinuable({
  220. provider: 'spawn',
  221. label: 'long work',
  222. request: { prompt: [{ type: 'text', text: 'long work' }], parent },
  223. signal: testToolSignal,
  224. })
  225. await vi.waitFor(() => { expect(adapter.requests).toHaveLength(1) })
  226. const child = ctx.agents.get(started.childId)!
  227. const queued = await callTool(ctx, 'send_message', {
  228. subagent_id: started.childId,
  229. message: 'parked follow-up',
  230. }, parent)
  231. expect(queued.isError).toBe(false)
  232. const cancelSpy = vi.spyOn(child, 'cancel')
  233. const result = await callTool(ctx, 'interrupt_agent', { agent_id: started.childId }, parent)
  234. expect(result.isError).toBe(false)
  235. expect(text(result)).toBe(`interrupt requested for agent ${started.childId}`)
  236. expect(cancelSpy).toHaveBeenCalledExactlyOnceWith({ kind: 'parent' }, { keepInbox: true })
  237. releaseFirst.resolve(undefined)
  238. await child.whenIdle()
  239. // Parked, not resumed: the queued follow-up waits for a waking send.
  240. expect(adapter.requests).toHaveLength(1)
  241. expect(child.inbox.nextTurn).toHaveLength(1)
  242. const waking = await callTool(ctx, 'send_message', {
  243. subagent_id: started.childId,
  244. message: 'wake up',
  245. }, parent)
  246. expect(waking.isError).toBe(false)
  247. await waitNoActivation(ctx, started.childId)
  248. const loaded = await ctx.sessionPersistence.load(started.childId)
  249. const prompts = loaded.events.flatMap(event => event.type === 'user/message' && event.data.source.kind !== 'plugin'
  250. ? event.data.content.flatMap(block => block.type === 'text' ? [block.text] : [])
  251. : [])
  252. expect(prompts).toEqual(['long work', 'parked follow-up', 'wake up'])
  253. })
  254. it('lets a deep live ancestor interrupt a descendant it did not directly create', async () => {
  255. const releaseChild = Promise.withResolvers<undefined>()
  256. const releaseGrandchild = Promise.withResolvers<undefined>()
  257. const adapter = new GatedAdapter([
  258. { chunks: textResponse('child'), gate: releaseChild.promise },
  259. { chunks: textResponse('grandchild'), gate: releaseGrandchild.promise },
  260. ])
  261. const { ctx, parent } = await setupWith(adapter)
  262. const started = await ctx.subagents.startContinuable({
  263. provider: 'spawn',
  264. label: 'child',
  265. request: { prompt: [{ type: 'text', text: 'child work' }], parent },
  266. signal: testToolSignal,
  267. })
  268. await vi.waitFor(() => { expect(adapter.requests).toHaveLength(1) })
  269. const child = ctx.agents.get(started.childId)!
  270. const grandchild = await ctx.subagents.startContinuable({
  271. provider: 'spawn',
  272. label: 'grandchild',
  273. request: { prompt: [{ type: 'text', text: 'grandchild work' }], parent: child },
  274. signal: testToolSignal,
  275. })
  276. await vi.waitFor(() => { expect(adapter.requests).toHaveLength(2) })
  277. const grandchildAgent = ctx.agents.get(grandchild.childId)!
  278. const cancelSpy = vi.spyOn(grandchildAgent, 'cancel')
  279. const result = await callTool(ctx, 'interrupt_agent', { agent_id: grandchild.childId }, parent)
  280. expect(result.isError).toBe(false)
  281. expect(cancelSpy).toHaveBeenCalledExactlyOnceWith({ kind: 'parent' }, { keepInbox: true })
  282. releaseChild.resolve(undefined)
  283. releaseGrandchild.resolve(undefined)
  284. await waitNoActivation(ctx, grandchild.childId)
  285. await waitNoActivation(ctx, started.childId)
  286. })
  287. it('rejects self, sibling, and unrelated callers without touching the target', async () => {
  288. const releaseA = Promise.withResolvers<undefined>()
  289. const releaseB = Promise.withResolvers<undefined>()
  290. const adapter = new GatedAdapter([
  291. { chunks: textResponse('a'), gate: releaseA.promise },
  292. { chunks: textResponse('b'), gate: releaseB.promise },
  293. ])
  294. const { ctx, parent } = await setupWith(adapter)
  295. const target = await ctx.subagents.startContinuable({
  296. provider: 'spawn',
  297. label: 'target',
  298. request: { prompt: [{ type: 'text', text: 'a' }], parent },
  299. signal: testToolSignal,
  300. })
  301. await vi.waitFor(() => { expect(adapter.requests).toHaveLength(1) })
  302. const sibling = await ctx.subagents.startContinuable({
  303. provider: 'spawn',
  304. label: 'sibling',
  305. request: { prompt: [{ type: 'text', text: 'b' }], parent },
  306. signal: testToolSignal,
  307. })
  308. await vi.waitFor(() => { expect(adapter.requests).toHaveLength(2) })
  309. const targetAgent = ctx.agents.get(target.childId)!
  310. const siblingAgent = ctx.agents.get(sibling.childId)!
  311. const stranger = ctx.agentLoop.create(SessionId('stranger'), { provider: 'mock', model: 'mock' })
  312. const cancelSpy = vi.spyOn(targetAgent, 'cancel')
  313. const self = await callTool(ctx, 'interrupt_agent', { agent_id: target.childId }, targetAgent)
  314. expect(self.isError).toBe(true)
  315. expect(text(self)).toContain('cannot interrupt itself')
  316. const fromSibling = await callTool(ctx, 'interrupt_agent', { agent_id: target.childId }, siblingAgent)
  317. expect(fromSibling.isError).toBe(true)
  318. expect(text(fromSibling)).toContain('not a live descendant')
  319. const fromStranger = await callTool(ctx, 'interrupt_agent', { agent_id: target.childId }, stranger)
  320. expect(fromStranger.isError).toBe(true)
  321. expect(text(fromStranger)).toContain('not a live descendant')
  322. expect(cancelSpy).not.toHaveBeenCalled()
  323. releaseA.resolve(undefined)
  324. releaseB.resolve(undefined)
  325. await waitNoActivation(ctx, target.childId)
  326. await waitNoActivation(ctx, sibling.childId)
  327. })
  328. it('accepts an absent target as a no-op without cold-resuming it', async () => {
  329. const { ctx, parent } = await setup([textResponse('done')])
  330. const started = await ctx.subagents.startContinuable({
  331. provider: 'spawn',
  332. label: 'settled child',
  333. request: { prompt: [{ type: 'text', text: 'child work' }], parent },
  334. signal: testToolSignal,
  335. })
  336. await waitNoActivation(ctx, started.childId)
  337. const settled = await callTool(ctx, 'interrupt_agent', { agent_id: started.childId }, parent)
  338. expect(settled.isError).toBe(false)
  339. expect(text(settled)).toBe(`interrupt requested for agent ${started.childId}`)
  340. const unknown = await callTool(ctx, 'interrupt_agent', { agent_id: 'no-such-agent' }, parent)
  341. expect(unknown.isError).toBe(false)
  342. // No cold resume: the settled target never rematerialized.
  343. expect(ctx.agents.get(started.childId)).toBeUndefined()
  344. })
  345. it('fails loud when invoked without a calling agent', async () => {
  346. const { ctx } = await setup([])
  347. const result = await callTool(ctx, 'interrupt_agent', { agent_id: 'x' })
  348. expect(result.isError).toBe(true)
  349. expect(text(result)).toContain('requires a calling agent')
  350. })
  351. })