1
0

tool-subagent.spec.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { Context } from 'cordis'
  3. import Loader from '@cordisjs/plugin-loader'
  4. import { CallId } from '@deepseek-ai/dsh-llm'
  5. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  6. import ToolRegistry from '@deepseek-ai/dsh-tools'
  7. import { type Agent } from '@deepseek-ai/dsh-agent'
  8. import AgentRegistry from '@deepseek-ai/dsh-agent'
  9. import SubagentService from '@deepseek-ai/dsh-subagent'
  10. import TaskService from '@deepseek-ai/dsh-tasks'
  11. import * as ToolTasks from '@deepseek-ai/dsh-tool-tasks'
  12. import * as mock from './scripted-provider.ts'
  13. import * as tool from '../src/index.ts'
  14. import { runOutcome, settleRun } from '../src/index.ts'
  15. import { SessionId } from '@deepseek-ai/dsh-session'
  16. /**
  17. * Drives the REAL plugin body: mounts `dsh-tool-subagent` on a real
  18. * `ToolRegistry` + `SubagentService`, with a package-local scripted child
  19. * boundary, and invokes the registered `subagent` tool through
  20. * `ctx.tools.execute`. Everything downstream of the child boundary is the
  21. * shipping code path.
  22. */
  23. /** A minimal parent Agent — the tool reads `agent.id` for `parent`. */
  24. function fakeAgent(id = 'parent-1'): Agent {
  25. return { id: SessionId(id) } as unknown as Agent
  26. }
  27. async function setup(toolConfig: tool.Config, mockConfig: Partial<mock.Config> = {}) {
  28. const ctx = new Context()
  29. await ctx.plugin(SystemPrompt)
  30. await ctx.plugin(ToolRegistry)
  31. await ctx.plugin(SubagentService)
  32. await mock.mountScriptedProvider(ctx, { name: 'mock', ...mockConfig })
  33. await ctx.plugin(tool, toolConfig)
  34. return ctx
  35. }
  36. let callCounter = 0
  37. function callSubagent(ctx: Context, args: unknown, over: { agent?: Agent | undefined; signal?: AbortSignal } = {}) {
  38. // Distinguish "no override" (use a default agent) from an explicit
  39. // `{ agent: undefined }` (test the no-agent path). Under
  40. // exactOptionalPropertyTypes the key is omitted rather than set to undefined.
  41. const agent = 'agent' in over ? over.agent : fakeAgent()
  42. return ctx.tools.execute({
  43. callId: CallId(`call-${++callCounter}`),
  44. name: 'subagent',
  45. arguments: args,
  46. ...agent ? { agent } : {},
  47. ...over.signal ? { signal: over.signal } : {},
  48. })
  49. }
  50. function text(result: { content: { type: string; text?: string }[] }): string {
  51. return result.content.filter(b => b.type === 'text').map(b => b.text).join('')
  52. }
  53. describe('dsh-tool-subagent', () => {
  54. it('registers a `subagent` tool that delegates to the configured provider and returns its output', async () => {
  55. const ctx = await setup({ provider: 'mock' }, { reply: 'child says hi' })
  56. const result = await callSubagent(ctx, { description: 'do a thing', prompt: 'go research X' })
  57. expect(result.isError).toBe(false)
  58. expect(text(result)).toBe('child says hi')
  59. })
  60. it('exposes description + prompt + run_in_background to the model (no provider/type parameter)', async () => {
  61. const ctx = await setup({ provider: 'mock' })
  62. const schema = ctx.tools.schemas().find(s => s.name === 'subagent')
  63. expect(schema).toBeDefined()
  64. const props = (schema!.parameters as { properties?: Record<string, unknown> }).properties ?? {}
  65. expect(Object.keys(props).sort()).toEqual(['description', 'prompt', 'run_in_background'])
  66. expect(schema!.description).toContain('task_output')
  67. })
  68. it('omits run_in_background entirely when the instance disables it (schema and capability never disagree)', async () => {
  69. const ctx = await setup({ provider: 'mock', enableRunInBackground: false })
  70. const schema = ctx.tools.schemas().find(s => s.name === 'subagent')
  71. const props = (schema!.parameters as { properties?: Record<string, unknown> }).properties ?? {}
  72. expect(Object.keys(props).sort()).toEqual(['description', 'prompt'])
  73. expect(schema!.description).not.toContain('task_output')
  74. })
  75. it('refuses a forced run_in_background at execution time when the instance disables it', async () => {
  76. // Schema omission is advertising, not enforcement: the arg validator
  77. // allows undeclared keys, so the opt-out must also hold in execute().
  78. const ctx = await setup({ provider: 'mock', enableRunInBackground: false })
  79. const parent = { id: SessionId('sess-off'), inject: () => {}, session: { header: { version: 0, id: 'sess-off', createdAt: 0 } } } as unknown as Agent
  80. const forced = await callSubagent(ctx, { description: 'd', prompt: 'p', run_in_background: true }, { agent: parent })
  81. expect(forced.isError).toBe(true)
  82. expect(text(forced)).toContain('run_in_background is disabled for this tool instance')
  83. // The provider was never asked to start a child.
  84. expect(ctx.subagents.getProvider('mock')).toBeDefined()
  85. const foreground = await callSubagent(ctx, { description: 'd', prompt: 'p' }, { agent: parent })
  86. expect(foreground.isError).toBe(false)
  87. })
  88. it('keeps foreground and background calls exclusive', async () => {
  89. const ctx = await setup({ provider: 'mock' })
  90. expect(ctx.tools.executionMode({
  91. callId: CallId('subagent-foreground'),
  92. name: 'subagent',
  93. arguments: { description: 'do work', prompt: 'Reply OK' },
  94. })).toEqual({ kind: 'exclusive' })
  95. expect(ctx.tools.executionMode({
  96. callId: CallId('subagent-background'),
  97. name: 'subagent',
  98. arguments: { description: 'do work', prompt: 'Reply OK', run_in_background: true },
  99. })).toEqual({ kind: 'exclusive' })
  100. })
  101. it.each([
  102. { stopReason: 'aborted' as const, fragment: 'cancelled' },
  103. { stopReason: 'error' as const, fragment: 'failed' },
  104. { stopReason: 'max-tokens' as const, fragment: 'token limit' },
  105. { stopReason: 'refusal' as const, fragment: 'declined' },
  106. ])('maps stop reason $stopReason to an isError result (not partial success)', async ({ stopReason, fragment }) => {
  107. const ctx = await setup({ provider: 'mock' }, { stopReason })
  108. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  109. expect(result.isError).toBe(true)
  110. expect(text(result)).toContain(fragment)
  111. })
  112. it('registers under a configurable toolName so multiple providers can coexist', async () => {
  113. // The defining multi-provider use case: two loads, two distinct tool names,
  114. // each bound to a different provider — the tool registry rejects duplicate
  115. // names, so a configurable name is what makes this work.
  116. const ctx = new Context()
  117. await ctx.plugin(SystemPrompt)
  118. await ctx.plugin(ToolRegistry)
  119. await ctx.plugin(SubagentService)
  120. await mock.mountScriptedProvider(ctx, { name: 'spawn', reply: 'from spawn' })
  121. await mock.mountScriptedProvider(ctx, { name: 'acp', reply: 'from acp' })
  122. await ctx.plugin(tool, { provider: 'spawn', toolName: 'subagent' })
  123. await ctx.plugin(tool, { provider: 'acp', toolName: 'subagent_acp' })
  124. const names = ctx.tools.schemas().map(s => s.name).filter(n => n.startsWith('subagent')).sort()
  125. expect(names).toEqual(['subagent', 'subagent_acp'])
  126. const viaSpawn = await ctx.tools.execute({ callId: CallId('c-spawn'), name: 'subagent', arguments: { description: 'd', prompt: 'p' }, agent: fakeAgent() })
  127. const viaAcp = await ctx.tools.execute({ callId: CallId('c-acp'), name: 'subagent_acp', arguments: { description: 'd', prompt: 'p' }, agent: fakeAgent() })
  128. expect(text(viaSpawn)).toBe('from spawn')
  129. expect(text(viaAcp)).toBe('from acp')
  130. })
  131. it('treats an unknown (plugin-added) stop reason as an isError result', async () => {
  132. // SubagentStopReason is merge-extensible; the tool's stopReasonError default
  133. // arm must treat an unrecognized terminal reason as a failure, not success.
  134. const ctx = new Context()
  135. await ctx.plugin(SystemPrompt)
  136. await ctx.plugin(ToolRegistry)
  137. await ctx.plugin(SubagentService)
  138. ctx.subagents.registerProvider({
  139. name: 'weird',
  140. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  141. inheritsParentContext: false,
  142. start: async () => ({
  143. id: SessionId('weird-child'),
  144. localAgent: undefined,
  145. result: Promise.resolve({ output: [{ type: 'text', text: 'partial' }], stopReason: 'frobnicated' as never }),
  146. dispose: async () => {},
  147. }),
  148. })
  149. await ctx.plugin(tool, { provider: 'weird' })
  150. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  151. expect(result.isError).toBe(true)
  152. expect(text(result)).toContain('abnormally')
  153. })
  154. it('forwards configured agentOptions into the start request', async () => {
  155. // Cover the `config.agentOptions ? … : {}` spread: a provider that captures
  156. // the request lets us assert the agentOptions reached it.
  157. let seen: { agentOptions?: { model?: string } } | undefined
  158. const ctx = new Context()
  159. await ctx.plugin(SystemPrompt)
  160. await ctx.plugin(ToolRegistry)
  161. await ctx.plugin(SubagentService)
  162. ctx.subagents.registerProvider({
  163. name: 'capture',
  164. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  165. inheritsParentContext: false,
  166. start: async (request) => {
  167. seen = request
  168. return {
  169. id: SessionId('capture-child'),
  170. localAgent: undefined,
  171. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  172. dispose: async () => {},
  173. }
  174. },
  175. })
  176. await ctx.plugin(tool, { provider: 'capture', agentOptions: { model: 'child-model' } })
  177. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  178. expect(seen?.agentOptions).toEqual({ model: 'child-model' })
  179. })
  180. it('defaults toolName and omits agentOptions when apply() is called directly (schema bypass)', async () => {
  181. // `ctx.plugin` validates+defaults config first (toolName→'subagent', the
  182. // agentOptions object→{}), so the runtime `?? 'subagent'` fallback and the
  183. // no-agentOptions branch are only reachable via a direct apply() that
  184. // bypasses schemastery — the same pattern acp-agent uses for its defaults.
  185. let seen: { agentOptions?: unknown } | undefined
  186. const ctx = new Context()
  187. await ctx.plugin(SystemPrompt)
  188. await ctx.plugin(ToolRegistry)
  189. await ctx.plugin(SubagentService)
  190. ctx.subagents.registerProvider({
  191. name: 'bare',
  192. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  193. inheritsParentContext: false,
  194. start: async (request) => {
  195. seen = request
  196. return {
  197. id: SessionId('bare-child'),
  198. localAgent: undefined,
  199. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  200. dispose: async () => {},
  201. }
  202. },
  203. })
  204. // Direct apply with only `provider` — no toolName, no agentOptions.
  205. tool.apply(ctx, { provider: 'bare' })
  206. await new Promise(r => setTimeout(r, 10))
  207. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true)
  208. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  209. expect(seen?.agentOptions).toBeUndefined()
  210. })
  211. it('fails loud when invoked without a calling agent', async () => {
  212. const ctx = await setup({ provider: 'mock' })
  213. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' }, { agent: undefined })
  214. expect(result.isError).toBe(true)
  215. expect(text(result)).toContain('requires a calling agent')
  216. })
  217. it('registers when the provider appears LATER — no load-order requirement (Loader starts siblings concurrently)', async () => {
  218. const ctx = new Context()
  219. await ctx.plugin(SystemPrompt)
  220. await ctx.plugin(ToolRegistry)
  221. await ctx.plugin(SubagentService)
  222. // Tool first: no provider yet — the tool must be absent, not broken.
  223. // Direct apply (schema bypass): also covers the waiting-note's default
  224. // toolName fallback, which validated config pre-fills.
  225. tool.apply(ctx, { provider: 'mock' })
  226. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(false)
  227. // Backend arrives (as a delayed sibling fiber would): the tool appears.
  228. await mock.mountScriptedProvider(ctx, { name: 'mock', reply: 'late but fine' })
  229. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true)
  230. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  231. expect(text(result)).toBe('late but fine')
  232. })
  233. it('mirrors the provider lifecycle: gone on backend dispose, re-derived wording on re-registration', async () => {
  234. const ctx = new Context()
  235. await ctx.plugin(SystemPrompt)
  236. await ctx.plugin(ToolRegistry)
  237. await ctx.plugin(SubagentService)
  238. const backend = await mock.mountScriptedProvider(ctx, { name: 'mock' }) // fresh conversation (descriptor: false)
  239. await ctx.plugin(tool, { provider: 'mock' })
  240. expect(ctx.tools.schemas().find(s => s.name === 'subagent')!.description).toContain('does not see this conversation')
  241. // Backend unloads (HMR shape): the tool must not outlive its provider.
  242. await backend.dispose()
  243. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(false)
  244. // Backend reloads with a DIFFERENT conversation-history descriptor: the wording is re-derived
  245. // from the fresh provider, not served stale from the first mount.
  246. await mock.mountScriptedProvider(ctx, { name: 'mock', inheritsParentContext: true })
  247. expect(ctx.tools.schemas().find(s => s.name === 'subagent')!.description).toContain('inherits this conversation')
  248. })
  249. it('the tool PLUGIN fiber owns its lifecycle listeners: disposal unmounts, and a disposed fiber never zombie-mounts', async () => {
  250. const ctx = new Context()
  251. await ctx.plugin(SystemPrompt)
  252. await ctx.plugin(ToolRegistry)
  253. await ctx.plugin(SubagentService)
  254. // Arm 1: a mounted tool dies with its plugin fiber; the provider survives.
  255. await mock.mountScriptedProvider(ctx, { name: 'mock' })
  256. const mounted = await ctx.plugin(tool, { provider: 'mock' })
  257. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true)
  258. await mounted.dispose()
  259. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(false)
  260. expect(ctx.subagents.getProvider('mock')).toBeDefined()
  261. // Arm 2: a fiber disposed while WAITING must not react to the provider
  262. // arriving later — a surviving listener would re-register a tool that no
  263. // live plugin owns (the zombie mount).
  264. const waiting = await ctx.plugin(tool, { provider: 'later', toolName: 'subagent_later' })
  265. await waiting.dispose()
  266. await mock.mountScriptedProvider(ctx, { name: 'later' })
  267. expect(ctx.tools.schemas().some(s => s.name === 'subagent_later')).toBe(false)
  268. })
  269. it('ignores lifecycle events for OTHER providers', async () => {
  270. const ctx = new Context()
  271. await ctx.plugin(SystemPrompt)
  272. await ctx.plugin(ToolRegistry)
  273. await ctx.plugin(SubagentService)
  274. await mock.mountScriptedProvider(ctx, { name: 'mock' })
  275. await ctx.plugin(tool, { provider: 'mock' })
  276. // An unrelated provider registering (added-event with another name) and
  277. // unregistering (removed-event with another name) must not touch the tool.
  278. const other = await mock.mountScriptedProvider(ctx, { name: 'other', inheritsParentContext: true })
  279. expect(ctx.tools.schemas().filter(s => s.name === 'subagent')).toHaveLength(1)
  280. expect(ctx.tools.schemas().find(s => s.name === 'subagent')!.description).toContain('does not see this conversation')
  281. await other.dispose()
  282. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true)
  283. })
  284. it('derives spawn-shaped wording from a fresh-conversation provider (default mock)', async () => {
  285. const ctx = await setup({ provider: 'mock' })
  286. const schema = ctx.tools.schemas().find(s => s.name === 'subagent')!
  287. expect(schema.description).toContain('does not see this conversation')
  288. const props = (schema.parameters as { properties: Record<string, { description: string }> }).properties
  289. expect(props['prompt']!.description).toContain('include everything it needs')
  290. })
  291. it('derives inherited-context wording from a seeded-conversation provider', async () => {
  292. const ctx = await setup({ provider: 'mock', toolName: 'subagent' }, { inheritsParentContext: true })
  293. const schema = ctx.tools.schemas().find(s => s.name === 'subagent')!
  294. expect(schema.description).toContain('inherits this conversation')
  295. expect(schema.description).not.toContain('does not see this conversation')
  296. const props = (schema.parameters as { properties: Record<string, { description: string }> }).properties
  297. expect(props['prompt']!.description).toContain('completed turns')
  298. })
  299. it('disposes the run on the success path (no leaked child)', async () => {
  300. // Spy on the provider's run.dispose via a wrapping provider registered
  301. // directly on the service, then point the tool at it.
  302. const disposed = vi.fn()
  303. const ctx = new Context()
  304. await ctx.plugin(SystemPrompt)
  305. await ctx.plugin(ToolRegistry)
  306. await ctx.plugin(SubagentService)
  307. ctx.subagents.registerProvider({
  308. name: 'spy',
  309. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  310. inheritsParentContext: false,
  311. start: async () => ({
  312. id: SessionId('spy-child'),
  313. localAgent: undefined,
  314. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  315. dispose: async () => void disposed(),
  316. }),
  317. })
  318. await ctx.plugin(tool, { provider: 'spy' })
  319. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  320. expect(disposed).toHaveBeenCalledTimes(1)
  321. })
  322. it('disposes the run on the error path too', async () => {
  323. const disposed = vi.fn()
  324. const ctx = new Context()
  325. await ctx.plugin(SystemPrompt)
  326. await ctx.plugin(ToolRegistry)
  327. await ctx.plugin(SubagentService)
  328. ctx.subagents.registerProvider({
  329. name: 'spy',
  330. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  331. inheritsParentContext: false,
  332. start: async () => ({
  333. id: SessionId('spy-child'),
  334. localAgent: undefined,
  335. result: Promise.resolve({ output: [], stopReason: 'error' as const }),
  336. dispose: async () => void disposed(),
  337. }),
  338. })
  339. await ctx.plugin(tool, { provider: 'spy' })
  340. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  341. expect(result.isError).toBe(true)
  342. expect(disposed).toHaveBeenCalledTimes(1)
  343. })
  344. it('passes the tool abort signal as the provider cancellation channel', async () => {
  345. const cancelled = vi.fn()
  346. const ctx = new Context()
  347. await ctx.plugin(SystemPrompt)
  348. await ctx.plugin(ToolRegistry)
  349. await ctx.plugin(SubagentService)
  350. ctx.subagents.registerProvider({
  351. name: 'spy',
  352. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  353. inheritsParentContext: false,
  354. start: async (request) => {
  355. if (request.signal.aborted) throw new Error('start aborted')
  356. let resolveResult: (r: { output: never[]; stopReason: 'aborted' }) => void
  357. const result = new Promise<{ output: never[]; stopReason: 'aborted' }>((res) => { resolveResult = res })
  358. request.signal.addEventListener('abort', () => {
  359. cancelled()
  360. resolveResult({ output: [], stopReason: 'aborted' })
  361. }, { once: true })
  362. return {
  363. id: SessionId('spy-child'),
  364. localAgent: undefined,
  365. result,
  366. dispose: async () => {},
  367. }
  368. },
  369. })
  370. await ctx.plugin(tool, { provider: 'spy' })
  371. const controller = new AbortController()
  372. const pending = callSubagent(ctx, { description: 'd', prompt: 'p' }, { signal: controller.signal })
  373. // Let provider.start install its listener before aborting.
  374. await Promise.resolve()
  375. await Promise.resolve()
  376. controller.abort()
  377. const result = await pending
  378. expect(cancelled).toHaveBeenCalledTimes(1)
  379. expect(result.isError).toBe(true)
  380. })
  381. it('passes an already-aborted signal so provider startup rejects', async () => {
  382. const sawAborted = vi.fn()
  383. const ctx = new Context()
  384. await ctx.plugin(SystemPrompt)
  385. await ctx.plugin(ToolRegistry)
  386. await ctx.plugin(SubagentService)
  387. ctx.subagents.registerProvider({
  388. name: 'spy',
  389. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  390. inheritsParentContext: false,
  391. start: async (request) => {
  392. if (request.signal.aborted) sawAborted()
  393. throw new Error('start aborted')
  394. },
  395. })
  396. await ctx.plugin(tool, { provider: 'spy' })
  397. const controller = new AbortController()
  398. controller.abort() // already aborted BEFORE the tool runs
  399. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' }, { signal: controller.signal })
  400. expect(sawAborted).toHaveBeenCalledTimes(1)
  401. expect(result.isError).toBe(true)
  402. })
  403. it('tools depend on the service: no `subagent` tool without ctx.subagents', async () => {
  404. const ctx = new Context()
  405. await ctx.plugin(SystemPrompt)
  406. await ctx.plugin(ToolRegistry)
  407. // No SubagentService mounted. The tool injects ['tools','subagents'] so its
  408. // apply never runs; the tool is absent rather than half-registered.
  409. let booted = true
  410. try {
  411. await ctx.plugin(tool, { provider: 'mock' })
  412. await new Promise(r => setTimeout(r, 20))
  413. } catch {
  414. booted = false
  415. }
  416. // Either it never booted, or it booted but registered no tool.
  417. const present = ctx.get('tools')?.schemas().some(s => s.name === 'subagent') ?? false
  418. expect(booted && present).toBe(false)
  419. })
  420. it('has the namespace-plugin export shape (no stray default) so the Loader keeps name/inject/Config/apply', () => {
  421. // Postmortem 0001 guard: this plugin HAS `inject = ['tools','subagents']`, so
  422. // a stray `export default apply` would collapse the module via
  423. // `unwrapExports` (`exports.default ?? exports`), DROP `inject`, and crash at
  424. // load with "cannot get property … without inject". Guard the shape directly.
  425. expect('default' in tool).toBe(false)
  426. expect(tool.name).toBe('tool-subagent')
  427. expect(tool.inject).toEqual(['tools', 'subagents'])
  428. const loader = Object.create(Loader.prototype) as Loader
  429. const unwrapped = loader.unwrapExports(tool) as Record<string, unknown>
  430. expect(unwrapped).toBe(tool)
  431. expect(unwrapped.name).toBe('tool-subagent')
  432. expect(unwrapped.inject).toEqual(['tools', 'subagents'])
  433. expect(typeof unwrapped.apply).toBe('function')
  434. expect(unwrapped.Config).toBeDefined()
  435. })
  436. it('passes persona/toolFilter/maxDepth config through to the start request', async () => {
  437. let seen: { persona?: string; toolFilter?: unknown; maxDepth?: number } | undefined
  438. const ctx = new Context()
  439. await ctx.plugin(SystemPrompt)
  440. await ctx.plugin(ToolRegistry)
  441. await ctx.plugin(SubagentService)
  442. ctx.subagents.registerProvider({
  443. name: 'capture2',
  444. capabilities: { outputSchema: false, depthLimit: true, toolFilter: true, persona: true },
  445. inheritsParentContext: false,
  446. start: async (request) => {
  447. seen = request
  448. return {
  449. id: SessionId('capture2-child'),
  450. localAgent: undefined,
  451. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  452. dispose: async () => {},
  453. }
  454. },
  455. })
  456. await ctx.plugin(tool, {
  457. provider: 'capture2',
  458. persona: 'You are the child.',
  459. toolFilter: { deny: ['subagent'] },
  460. maxDepth: 2,
  461. })
  462. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  463. expect(seen?.persona).toBe('You are the child.')
  464. expect(seen?.toolFilter).toMatchObject({ deny: ['subagent'] })
  465. expect(seen?.maxDepth).toBe(2)
  466. })
  467. it.each([
  468. { label: 'null', value: null as unknown as number },
  469. { label: 'a string', value: '1' as unknown as number },
  470. { label: 'NaN', value: Number.NaN },
  471. { label: 'positive infinity', value: Number.POSITIVE_INFINITY },
  472. { label: 'negative infinity', value: Number.NEGATIVE_INFINITY },
  473. { label: 'a negative integer', value: -1 },
  474. { label: 'a fractional number', value: 1.5 },
  475. { label: 'negative zero', value: -0 },
  476. { label: 'an unsafe integer', value: Number.MAX_SAFE_INTEGER + 1 },
  477. ])('rejects maxDepth=$label when the plugin loads', async ({ value }) => {
  478. await expect(setup({ provider: 'mock', maxDepth: value }))
  479. .rejects.toThrow()
  480. })
  481. it('validates maxDepth when apply() is invoked directly without Schemastery', () => {
  482. const ctx = new Context()
  483. expect(() => {
  484. tool.apply(ctx, {
  485. provider: 'unused',
  486. maxDepth: Number.NaN,
  487. })
  488. }).toThrow('subagent maxDepth must be a non-negative safe integer')
  489. })
  490. it('a partial toolFilter (deny only) does not materialize an empty allow-list (deny-all trap)', async () => {
  491. let seen: { toolFilter?: { readonly allow?: readonly string[]; readonly deny?: readonly string[] } } | undefined
  492. const ctx = new Context()
  493. await ctx.plugin(SystemPrompt)
  494. await ctx.plugin(ToolRegistry)
  495. await ctx.plugin(SubagentService)
  496. ctx.subagents.registerProvider({
  497. name: 'capture3',
  498. capabilities: { outputSchema: false, depthLimit: false, toolFilter: true, persona: false },
  499. inheritsParentContext: false,
  500. start: async (request) => {
  501. seen = request
  502. return {
  503. id: SessionId('capture3-child'),
  504. localAgent: undefined,
  505. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  506. dispose: async () => {},
  507. }
  508. },
  509. })
  510. await ctx.plugin(tool, { provider: 'capture3', toolFilter: { deny: ['subagent'] } })
  511. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  512. expect(seen?.toolFilter).toEqual({ deny: ['subagent'] })
  513. expect(seen?.toolFilter).not.toHaveProperty('allow')
  514. })
  515. it('an omitted agentOptions does not materialize an empty object onto the request', async () => {
  516. // Same schemastery trap as toolFilter, adjacent field: an omitted
  517. // `agentOptions` config key materializes `{}` without the forced default,
  518. // which reads as present and puts a dishonest `agentOptions: {}` on every
  519. // start request.
  520. let seen: { agentOptions?: unknown } | undefined
  521. const ctx = new Context()
  522. await ctx.plugin(SystemPrompt)
  523. await ctx.plugin(ToolRegistry)
  524. await ctx.plugin(SubagentService)
  525. ctx.subagents.registerProvider({
  526. name: 'capture4',
  527. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  528. inheritsParentContext: false,
  529. start: async (request) => {
  530. seen = request
  531. return {
  532. id: SessionId('capture4-child'),
  533. localAgent: undefined,
  534. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  535. dispose: async () => {},
  536. }
  537. },
  538. })
  539. await ctx.plugin(tool, { provider: 'capture4' })
  540. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  541. expect(seen).toBeDefined()
  542. expect(seen).not.toHaveProperty('agentOptions')
  543. })
  544. it('an explicit empty toolFilter fails at plugin load, not at first delegation', async () => {
  545. const ctx = new Context()
  546. await ctx.plugin(SystemPrompt)
  547. await ctx.plugin(ToolRegistry)
  548. await ctx.plugin(SubagentService)
  549. ctx.subagents.registerProvider({
  550. name: 'p',
  551. capabilities: { outputSchema: false, depthLimit: false, toolFilter: true, persona: false },
  552. inheritsParentContext: false,
  553. start: () => { throw new Error('unreachable') },
  554. })
  555. const fiber = ctx.plugin(tool, { provider: 'p', toolFilter: {} })
  556. await expect(fiber).rejects.toThrow(/names neither `allow` nor `deny`/)
  557. })
  558. })
  559. describe('dsh-tool-subagent background mode', () => {
  560. /** A live parent with a dedicated scope fiber for structural task cleanup. */
  561. function ownerAgent(ctx: Context, sessionId: string, inject: (...args: unknown[]) => void = () => {}): Agent {
  562. const scopeFiber = ctx.plugin(() => {})
  563. const id = SessionId(sessionId)
  564. const agent = {
  565. id,
  566. ctx: scopeFiber.ctx,
  567. inject,
  568. session: { id, header: { version: 0, id, createdAt: 0 } },
  569. } as unknown as Agent
  570. ctx.agents.register(agent)
  571. return agent
  572. }
  573. async function backgroundSetup(toolConfig: tool.Config, mockConfig: Partial<mock.Config> = {}) {
  574. const ctx = await setup(toolConfig, mockConfig)
  575. await ctx.plugin(AgentRegistry)
  576. await ctx.plugin(TaskService)
  577. await ctx.plugin(ToolTasks, {})
  578. return ctx
  579. }
  580. it('returns a task id immediately and the answer is collected through task_output', async () => {
  581. const ctx = await backgroundSetup({ provider: 'mock', agentOptions: { model: 'child-model' } }, { reply: 'background answer' })
  582. const parent = ownerAgent(ctx, 'sess-parent')
  583. const start = await callSubagent(ctx, { description: 'deep research', prompt: 'dig in', run_in_background: true }, { agent: parent })
  584. expect(start.isError).toBe(false)
  585. expect(text(start)).toBe('started background subagent task subagent-1')
  586. const collected = await ctx.tools.execute({
  587. callId: CallId('collect-1'),
  588. name: 'task_output',
  589. arguments: { task_id: 'subagent-1', wait: true },
  590. agent: parent,
  591. })
  592. expect(text(collected)).toBe('background answer\n[status: completed]')
  593. // Final-output reads are idempotent (not consumed).
  594. const again = await ctx.tools.execute({
  595. callId: CallId('collect-2'),
  596. name: 'task_output',
  597. arguments: { task_id: 'subagent-1' },
  598. agent: parent,
  599. })
  600. expect(text(again)).toBe('background answer\n[status: completed]')
  601. })
  602. it('fails loud when the tasks runtime is not loaded', async () => {
  603. const ctx = await setup({ provider: 'mock' })
  604. const result = await callSubagent(ctx, { description: 'd', prompt: 'p', run_in_background: true })
  605. expect(result.isError).toBe(true)
  606. expect(text(result)).toContain('background tasks unavailable: load @deepseek-ai/dsh-tasks')
  607. })
  608. it('refuses to start when the tool signal is already aborted', async () => {
  609. const ctx = await backgroundSetup({ provider: 'mock' })
  610. const parent = ownerAgent(ctx, 'sess-parent')
  611. const controller = new AbortController()
  612. controller.abort()
  613. const result = await callSubagent(ctx, { description: 'd', prompt: 'p', run_in_background: true }, { agent: parent, signal: controller.signal })
  614. expect(result.isError).toBe(true)
  615. expect(text(result)).toContain('subagent delegation aborted')
  616. })
  617. it('settles an asynchronous provider-start failure as a failed task', async () => {
  618. const ctx = await backgroundSetup({ provider: 'mock' })
  619. const parent = ownerAgent(ctx, 'sess-parent')
  620. ctx.subagents.registerProvider({
  621. name: 'broken-start',
  622. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  623. inheritsParentContext: false,
  624. start: async () => { throw new Error('setup failed') },
  625. })
  626. tool.apply(ctx, { provider: 'broken-start', toolName: 'subagent_broken' })
  627. const started = await ctx.tools.execute({
  628. callId: CallId('broken-start'),
  629. name: 'subagent_broken',
  630. arguments: { description: 'broken', prompt: 'p', run_in_background: true },
  631. agent: parent,
  632. })
  633. expect(text(started)).toBe('started background subagent task subagent-1')
  634. const output = await ctx.tools.execute({
  635. callId: CallId('broken-output'),
  636. name: 'task_output',
  637. arguments: { task_id: 'subagent-1', wait: true },
  638. agent: parent,
  639. })
  640. expect(text(output)).toContain('[status: failed, Error: setup failed]')
  641. })
  642. it('kills a subagent task while provider readiness is still pending', async () => {
  643. const ctx = await backgroundSetup({ provider: 'mock' })
  644. const parent = ownerAgent(ctx, 'sess-parent')
  645. ctx.subagents.registerProvider({
  646. name: 'pending-start',
  647. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  648. inheritsParentContext: false,
  649. start: request => new Promise((_resolve, reject) => {
  650. request.signal.addEventListener('abort', () => { reject(new Error('startup aborted')) }, { once: true })
  651. }),
  652. })
  653. tool.apply(ctx, { provider: 'pending-start', toolName: 'subagent_pending' })
  654. await ctx.tools.execute({
  655. callId: CallId('pending-start'),
  656. name: 'subagent_pending',
  657. arguments: { description: 'pending', prompt: 'p', run_in_background: true },
  658. agent: parent,
  659. })
  660. await ctx.tools.execute({
  661. callId: CallId('pending-kill'),
  662. name: 'task_kill',
  663. arguments: { task_id: 'subagent-1', reason: 'no longer needed' },
  664. agent: parent,
  665. })
  666. const output = await ctx.tools.execute({
  667. callId: CallId('pending-output'),
  668. name: 'task_output',
  669. arguments: { task_id: 'subagent-1', wait: true },
  670. agent: parent,
  671. })
  672. expect(text(output)).toBe('(no new output)\n[status: killed]')
  673. })
  674. it('forwards task_kill reasons through the run signal (and defaults one when absent)', async () => {
  675. // Use a provider that remains live until its signal is aborted.
  676. const ctx = await backgroundSetup({ provider: 'mock', agentOptions: { model: 'child-model' } })
  677. const parent = ownerAgent(ctx, 'sess-parent')
  678. const cancels: (string | undefined)[] = []
  679. let starts = 0
  680. ctx.subagents.registerProvider({
  681. name: 'hanging',
  682. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  683. inheritsParentContext: false,
  684. start: async (request) => {
  685. let settle!: (value: { output: { type: 'text'; text: string }[]; stopReason: 'aborted' }) => void
  686. const id = SessionId(`hang-${++starts}`)
  687. const result = new Promise<{ output: { type: 'text'; text: string }[]; stopReason: 'aborted' }>((res) => { settle = res })
  688. request.signal.addEventListener('abort', () => {
  689. cancels.push(typeof request.signal.reason === 'string' ? request.signal.reason : undefined)
  690. settle({ output: [], stopReason: 'aborted' })
  691. }, { once: true })
  692. return {
  693. id,
  694. localAgent: undefined,
  695. result,
  696. dispose: () => Promise.resolve(),
  697. }
  698. },
  699. })
  700. // Direct apply preserves omitted agentOptions instead of applying schema defaults.
  701. tool.apply(ctx, { provider: 'hanging', toolName: 'subagent_hang' })
  702. const startOne = await ctx.tools.execute({ callId: CallId('h1'), name: 'subagent_hang', arguments: { description: 'one', prompt: 'p', run_in_background: true }, agent: parent })
  703. const startTwo = await ctx.tools.execute({ callId: CallId('h2'), name: 'subagent_hang', arguments: { description: 'two', prompt: 'p', run_in_background: true }, agent: parent })
  704. expect(text(startOne)).toBe('started background subagent task subagent-1')
  705. expect(text(startTwo)).toBe('started background subagent task subagent-2')
  706. const withReason = await ctx.tools.execute({ callId: CallId('k1'), name: 'task_kill', arguments: { task_id: 'subagent-1', reason: 'superseded' }, agent: parent })
  707. const withoutReason = await ctx.tools.execute({ callId: CallId('k2'), name: 'task_kill', arguments: { task_id: 'subagent-2' }, agent: parent })
  708. expect(text(withReason)).toBe('requested cancellation of task subagent-1')
  709. expect(text(withoutReason)).toBe('requested cancellation of task subagent-2')
  710. expect(cancels).toEqual(['superseded', 'background subagent task killed'])
  711. // The aborted children settle as killed tasks.
  712. const killed = await ctx.tools.execute({ callId: CallId('w1'), name: 'task_output', arguments: { task_id: 'subagent-1', wait: true }, agent: parent })
  713. expect(text(killed)).toBe('(no new output)\n[status: killed]')
  714. })
  715. it('runOutcome maps the stop-reason vocabulary onto task outcomes', () => {
  716. const output = [{ type: 'text' as const, text: 'partial' }]
  717. expect(runOutcome({ output, stopReason: 'completed' })).toEqual({ status: 'completed', output: 'partial' })
  718. expect(runOutcome({ output, stopReason: 'aborted' })).toEqual({ status: 'killed' })
  719. expect(runOutcome({ output, stopReason: 'error' })).toEqual({ status: 'failed', detail: 'error' })
  720. expect(runOutcome({ output, stopReason: 'max-tokens' })).toEqual({ status: 'failed', detail: 'max-tokens' })
  721. expect(runOutcome({ output, stopReason: 'refusal' })).toEqual({ status: 'failed', detail: 'refusal' })
  722. // Merge-extensible: an unknown reason is failed-with-detail, never success.
  723. expect(runOutcome({ output, stopReason: 'paused' as never })).toEqual({ status: 'failed', detail: 'paused' })
  724. })
  725. it('settleRun disposes the run before reporting, on both result paths', async () => {
  726. const order: string[] = []
  727. const completed = await settleRun({
  728. id: SessionId('child-1'),
  729. localAgent: undefined,
  730. result: Promise.resolve({ output: [{ type: 'text' as const, text: 'ok' }], stopReason: 'completed' as const }),
  731. dispose() { order.push('dispose'); return Promise.resolve() },
  732. })
  733. order.push('reported')
  734. expect(completed).toEqual({ status: 'completed', output: 'ok' })
  735. expect(order).toEqual(['dispose', 'reported'])
  736. // An infrastructure rejection still disposes and reports failed.
  737. let disposed = false
  738. const failed = await settleRun({
  739. id: SessionId('child-2'),
  740. localAgent: undefined,
  741. result: Promise.reject(new Error('transport gone')),
  742. dispose() { disposed = true; return Promise.resolve() },
  743. })
  744. expect(failed).toEqual({ status: 'failed', detail: 'Error: transport gone' })
  745. expect(disposed).toBe(true)
  746. const disposeFailed = await settleRun({
  747. id: SessionId('child-3'),
  748. localAgent: undefined,
  749. result: Promise.resolve({ output: [], stopReason: 'completed' }),
  750. dispose: () => Promise.reject(new Error('reap failed')),
  751. })
  752. expect(disposeFailed).toEqual({ status: 'failed', detail: 'dispose failed: Error: reap failed' })
  753. const bothFailed = await settleRun({
  754. id: SessionId('child-4'),
  755. localAgent: undefined,
  756. result: Promise.reject(new Error('result failed')),
  757. dispose: () => Promise.reject(new Error('reap failed')),
  758. })
  759. expect(bothFailed).toEqual({
  760. status: 'failed',
  761. detail: 'Error: result failed; dispose failed: Error: reap failed',
  762. })
  763. })
  764. })
  765. describe('background preflight failure (no orphaned child, by construction)', () => {
  766. it('never starts the child when tasks.start preflight throws', async () => {
  767. // With no control surface, task preflight fails before the provider can spawn.
  768. const ctx = await setup({ provider: 'mock' })
  769. await ctx.plugin(AgentRegistry)
  770. await ctx.plugin(TaskService)
  771. const scopeFiber = ctx.plugin(() => {})
  772. const id = SessionId('sess-p')
  773. const parent = {
  774. id,
  775. ctx: scopeFiber.ctx,
  776. inject: () => {},
  777. session: { id, header: { version: 0, id, createdAt: 0 } },
  778. } as unknown as Agent
  779. ctx.agents.register(parent)
  780. let starts = 0
  781. ctx.subagents.registerProvider({
  782. name: 'probe',
  783. capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  784. inheritsParentContext: false,
  785. start: async () => {
  786. starts += 1
  787. return {
  788. id: SessionId('probe-child'),
  789. localAgent: undefined,
  790. result: Promise.resolve({ output: [], stopReason: 'completed' as const }),
  791. dispose: () => Promise.resolve(),
  792. }
  793. },
  794. })
  795. tool.apply(ctx, { provider: 'probe', toolName: 'subagent_probe' })
  796. const result = await ctx.tools.execute({
  797. callId: CallId('probe-1'),
  798. name: 'subagent_probe',
  799. arguments: { description: 'd', prompt: 'p', run_in_background: true },
  800. agent: parent,
  801. })
  802. expect(result.isError).toBe(true)
  803. expect(text(result)).toContain('no control surface is attached')
  804. // Declare-then-execute: the failed preflight means no child ever existed.
  805. expect(starts).toBe(0)
  806. })
  807. })