tool-subagent.spec.ts 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import { mkdtempSync, rmSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import path from 'node:path'
  5. import { Context } from '@deepseek-ai/cordis'
  6. import Loader from '@deepseek-ai/cordis-plugin-loader'
  7. import LlmRuntime, { CallId } from '@deepseek-ai/dsh-llm'
  8. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  9. import ToolRuntime, { TOOL_ABORTED_BEFORE_DISPATCH } from '@deepseek-ai/dsh-tools'
  10. import { assembleContextFor, type Agent } from '@deepseek-ai/dsh-agent'
  11. import AgentRegistry from '@deepseek-ai/dsh-agent'
  12. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  13. import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
  14. import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl'
  15. import SubagentRuntime from '@deepseek-ai/dsh-subagent'
  16. import type { SubagentStartRequest } from '@deepseek-ai/dsh-subagent'
  17. import LocalJobRegistry from '@deepseek-ai/dsh-jobs-local'
  18. import * as SubagentSpawn from '@deepseek-ai/dsh-subagent-spawn-in-process'
  19. import * as ToolTasks from '@deepseek-ai/dsh-tool-jobs'
  20. import { MockAdapter, textResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  21. import * as mock from './scripted-provider.ts'
  22. import * as tool from '../src/index.ts'
  23. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  24. import { callSubagent, fakeAgent, setup, testToolSignal, text } from './harness.ts'
  25. /**
  26. * Drives the REAL plugin body: mounts `dsh-tool-subagent` on a real
  27. * `ToolRuntime` + `SubagentRuntime`, with a package-local scripted child
  28. * boundary, and invokes the registered `subagent` tool through
  29. * `ctx.tools.execute`. Everything downstream of the child boundary is the
  30. * shipping code path.
  31. */
  32. describe('dsh-tool-subagent', () => {
  33. it('rejects continuable background policy when the provider cannot prepare continuable children', async () => {
  34. let failure: unknown
  35. try {
  36. await setup({
  37. provider: 'mock',
  38. backgroundMode: 'continuable',
  39. })
  40. } catch (error: unknown) {
  41. failure = error
  42. }
  43. expect(String(failure)).toContain(
  44. 'provider "mock" does not support `backgroundMode: continuable`',
  45. )
  46. })
  47. it('rejects configured child agent options at mount when the provider cannot apply them', async () => {
  48. await expect(setup(
  49. { provider: 'mock', maxDepth: 'provider-managed', agentOptions: { model: 'configured-model' } },
  50. { capabilities: { agentOptions: false } },
  51. )).rejects.toThrow('does not support child agentOptions')
  52. })
  53. it('registers a `subagent` tool that delegates to the configured provider and returns its output', async () => {
  54. const ctx = await setup({ provider: 'mock' }, { reply: 'child says hi' })
  55. const result = await callSubagent(ctx, {
  56. description: 'do a thing',
  57. prompt: 'go research X',
  58. run_in_background: false,
  59. })
  60. expect(result.isError).toBe(false)
  61. if (result.isError) throw new Error('expected subagent success')
  62. expect(result.value).toEqual({
  63. kind: 'foreground',
  64. runId: 'scripted-subagent:mock:parent-1',
  65. output: [{ type: 'text', text: 'child says hi' }],
  66. })
  67. expect(text(result)).toBe('child says hi')
  68. })
  69. it('omits run_in_background entirely when the instance disables it (schema and capability never disagree)', async () => {
  70. const ctx = await setup({ provider: 'mock', enableRunInBackground: false })
  71. const schema = ctx.tools.schemas().find(s => s.name === 'subagent')
  72. const props = (schema!.parameters as { properties?: Record<string, unknown> }).properties ?? {}
  73. expect(Object.keys(props).sort()).toEqual([
  74. 'description',
  75. 'prompt',
  76. ])
  77. expect(schema!.description).not.toContain('job_output')
  78. })
  79. it('refuses a forced run_in_background at execution time when the instance disables it', async () => {
  80. // Schema omission is advertising, not enforcement: the arg validator
  81. // allows undeclared keys, so the opt-out must also hold in execute().
  82. const ctx = await setup({ provider: 'mock', enableRunInBackground: false })
  83. const parentId = SessionId('sess-off')
  84. const parent = {
  85. id: parentId,
  86. inject: () => {},
  87. options: {},
  88. session: Session.create(parentId),
  89. } as unknown as Agent
  90. const forced = await callSubagent(ctx, { description: 'd', prompt: 'p', run_in_background: true }, { agent: parent })
  91. expect(forced.isError).toBe(true)
  92. expect(text(forced)).toContain('run_in_background is disabled for this tool instance')
  93. // The provider was never asked to start a child.
  94. expect(ctx.subagents.getProvider('mock')).toBeDefined()
  95. const foreground = await callSubagent(ctx, { description: 'd', prompt: 'p' }, { agent: parent })
  96. expect(foreground.isError).toBe(false)
  97. })
  98. it('classifies foreground and background calls concurrency-safe (sibling delegations overlap)', async () => {
  99. const ctx = await setup({ provider: 'mock' })
  100. expect(ctx.tools.executionMode({
  101. signal: testToolSignal,
  102. callId: CallId('subagent-foreground'),
  103. name: 'subagent',
  104. arguments: { description: 'do work', prompt: 'Reply OK' },
  105. })).toEqual({ kind: 'parallel' })
  106. expect(ctx.tools.executionMode({
  107. signal: testToolSignal,
  108. callId: CallId('subagent-background'),
  109. name: 'subagent',
  110. arguments: { description: 'do work', prompt: 'Reply OK', run_in_background: true },
  111. })).toEqual({ kind: 'parallel' })
  112. })
  113. it('overlaps sibling foreground delegations dispatched concurrently', async () => {
  114. // Two children each block until both have started: hidden serialization
  115. // in the tool body, registry pipeline, or provider start path would
  116. // deadlock here instead of passing silently.
  117. const started: string[] = []
  118. let releaseBoth!: () => void
  119. const bothStarted = new Promise<void>((resolve) => { releaseBoth = resolve })
  120. const ctx = await setup({ provider: 'mock', enableRunInBackground: false }, {
  121. onStart: (request: SubagentStartRequest) => {
  122. started.push(request.label ?? '(unlabeled)')
  123. if (started.length === 2) releaseBoth()
  124. return bothStarted
  125. },
  126. })
  127. const results = await Promise.all([
  128. callSubagent(ctx, { description: 'first', prompt: 'p1' }),
  129. callSubagent(ctx, { description: 'second', prompt: 'p2' }),
  130. ])
  131. expect(started.sort()).toEqual(['first', 'second'])
  132. for (const result of results) expect(result.isError).toBe(false)
  133. })
  134. it.each([
  135. { stopReason: 'aborted' as const, fragment: 'cancelled' },
  136. { stopReason: 'error' as const, fragment: 'failed' },
  137. { stopReason: 'max-tokens' as const, fragment: 'token limit' },
  138. { stopReason: 'refusal' as const, fragment: 'declined' },
  139. ])('maps stop reason $stopReason to an isError result (not partial success)', async ({ stopReason, fragment }) => {
  140. const ctx = await setup({ provider: 'mock' }, { stopReason })
  141. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  142. expect(result.isError).toBe(true)
  143. expect(text(result)).toContain(fragment)
  144. // The failure is not partial success, but the child's preserved partial
  145. // answer still reaches the parent model inside the error result.
  146. expect(text(result)).toContain('scripted subagent reply')
  147. })
  148. it('renders provider diagnostics before preserved partial assistant output', async () => {
  149. const ctx = await setup({ provider: 'mock' }, {
  150. reply: 'partial assistant text',
  151. diagnostic: 'Claude Code denied a tool request',
  152. stopReason: 'error',
  153. })
  154. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  155. expect(result.isError).toBe(true)
  156. expect(text(result)).toBe(
  157. 'Error: subagent run failed\n'
  158. + 'Diagnostic: Claude Code denied a tool request\n'
  159. + 'Partial output before the run ended:\npartial assistant text',
  160. )
  161. })
  162. it('registers under a configurable toolName so multiple providers can coexist', async () => {
  163. // The defining multi-provider use case: two loads, two distinct tool names,
  164. // each bound to a different provider — the tool registry rejects duplicate
  165. // names, so a configurable name is what makes this work.
  166. const ctx = new Context()
  167. await ctx.plugin(SystemPrompt)
  168. await ctx.plugin(ToolRuntime)
  169. await ctx.plugin(SubagentRuntime)
  170. await mock.mountScriptedProvider(ctx, { name: 'spawn', reply: 'from spawn' })
  171. await mock.mountScriptedProvider(ctx, { name: 'acp', reply: 'from acp' })
  172. await ctx.plugin(tool, { provider: 'spawn', toolName: 'subagent' })
  173. await ctx.plugin(tool, { provider: 'acp', toolName: 'subagent_acp' })
  174. const names = ctx.tools.schemas().map(s => s.name).filter(n => n.startsWith('subagent')).sort()
  175. expect(names).toEqual(['subagent', 'subagent_acp'])
  176. const viaSpawn = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c-spawn'), name: 'subagent', arguments: { description: 'd', prompt: 'p' }, agent: fakeAgent() })
  177. const viaAcp = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c-acp'), name: 'subagent_acp', arguments: { description: 'd', prompt: 'p' }, agent: fakeAgent() })
  178. expect(text(viaSpawn)).toBe('from spawn')
  179. expect(text(viaAcp)).toBe('from acp')
  180. })
  181. it('treats an unknown (plugin-added) stop reason as an isError result', async () => {
  182. // SubagentStopReason is merge-extensible; the tool's stopReasonError default
  183. // arm must treat an unrecognized terminal reason as a failure, not success.
  184. const ctx = new Context()
  185. await ctx.plugin(SystemPrompt)
  186. await ctx.plugin(ToolRuntime)
  187. await ctx.plugin(SubagentRuntime)
  188. ctx.subagents.registerProvider({
  189. name: 'weird',
  190. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  191. inheritsParentContext: false,
  192. start: async () => ({
  193. id: SessionId('weird-child'),
  194. localAgent: undefined,
  195. result: Promise.resolve({ output: [{ type: 'text', text: 'partial' }], stopReason: 'frobnicated' as never }),
  196. dispose: async () => {},
  197. }),
  198. })
  199. await ctx.plugin(tool, { provider: 'weird', maxDepth: 'provider-managed' })
  200. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  201. expect(result.isError).toBe(true)
  202. expect(text(result)).toContain('abnormally')
  203. })
  204. it('forwards configured agentOptions into the start request', async () => {
  205. // Cover the `config.agentOptions ? … : {}` spread: a provider that captures
  206. // the request lets us assert the agentOptions reached it.
  207. let seen: { agentOptions?: { model?: string } } | undefined
  208. const ctx = new Context()
  209. await ctx.plugin(LlmRuntime)
  210. await ctx.plugin(SystemPrompt)
  211. await ctx.plugin(ToolRuntime)
  212. await ctx.plugin(SubagentRuntime)
  213. ctx.subagents.registerProvider({
  214. name: 'capture',
  215. capabilities: { agentOptions: true, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  216. inheritsParentContext: false,
  217. start: async (request) => {
  218. seen = request
  219. return {
  220. id: SessionId('capture-child'),
  221. localAgent: undefined,
  222. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  223. dispose: async () => {},
  224. }
  225. },
  226. })
  227. ctx.llm.registerAdapter(['alpha'], new MockAdapter([]))
  228. await ctx.plugin(tool, {
  229. provider: 'capture',
  230. agentOptions: { provider: 'alpha', model: 'child-model' },
  231. maxDepth: 'provider-managed',
  232. })
  233. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  234. expect(seen?.agentOptions).toEqual({ provider: 'alpha', model: 'child-model' })
  235. })
  236. it('defaults toolName and omits agentOptions when apply() is called directly (schema bypass)', async () => {
  237. // `ctx.plugin` validates+defaults config first (toolName→'subagent', the
  238. // agentOptions object→{}), so the runtime `?? 'subagent'` fallback and the
  239. // no-agentOptions branch are only reachable via a direct apply() that
  240. // bypasses schemastery — the same pattern acp-agent uses for its defaults.
  241. let seen: { agentOptions?: unknown } | undefined
  242. const ctx = new Context()
  243. await ctx.plugin(SystemPrompt)
  244. await ctx.plugin(ToolRuntime)
  245. await ctx.plugin(SubagentRuntime)
  246. ctx.subagents.registerProvider({
  247. name: 'bare',
  248. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  249. inheritsParentContext: false,
  250. start: async (request) => {
  251. seen = request
  252. return {
  253. id: SessionId('bare-child'),
  254. localAgent: undefined,
  255. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  256. dispose: async () => {},
  257. }
  258. },
  259. })
  260. // Direct apply with only `provider` — no toolName, no agentOptions.
  261. tool.apply(ctx, { provider: 'bare' })
  262. await new Promise(r => setTimeout(r, 10))
  263. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true)
  264. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  265. expect(seen?.agentOptions).toBeUndefined()
  266. })
  267. it('fails loud when invoked without a calling agent', async () => {
  268. const ctx = await setup({ provider: 'mock' })
  269. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' }, { agent: undefined })
  270. expect(result.isError).toBe(true)
  271. expect(text(result)).toContain('requires a calling agent')
  272. })
  273. it('registers when the provider appears LATER — no load-order requirement (Loader starts siblings concurrently)', async () => {
  274. const ctx = new Context()
  275. await ctx.plugin(SystemPrompt)
  276. await ctx.plugin(ToolRuntime)
  277. await ctx.plugin(SubagentRuntime)
  278. // Tool first: no provider yet — the tool must be absent, not broken.
  279. // Direct apply (schema bypass): also covers the waiting-note's default
  280. // toolName fallback, which validated config pre-fills.
  281. tool.apply(ctx, { provider: 'mock' })
  282. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(false)
  283. // Backend arrives (as a delayed sibling fiber would): the tool appears.
  284. await mock.mountScriptedProvider(ctx, { name: 'mock', reply: 'late but fine' })
  285. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true)
  286. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  287. expect(text(result)).toBe('late but fine')
  288. })
  289. it('keeps continuable guidance empty while its provider is absent', async () => {
  290. const ctx = new Context()
  291. await ctx.plugin(SystemPrompt)
  292. await ctx.plugin(ToolRuntime)
  293. await ctx.plugin(SubagentRuntime)
  294. tool.apply(ctx, {
  295. provider: 'later-continuable',
  296. backgroundMode: 'continuable',
  297. maxDepth: 'provider-managed',
  298. })
  299. const assembly = await ctx.systemPrompt.assemble()
  300. expect(assembly.sections.find(section => section.name === 'tool:subagent')?.text).toBe('')
  301. expect(ctx.tools.schemas().some(schema => schema.name === 'subagent')).toBe(false)
  302. })
  303. it('mirrors the provider lifecycle: gone on backend dispose, re-derived wording on re-registration', async () => {
  304. const ctx = new Context()
  305. await ctx.plugin(SystemPrompt)
  306. await ctx.plugin(ToolRuntime)
  307. await ctx.plugin(SubagentRuntime)
  308. const backend = await mock.mountScriptedProvider(ctx, { name: 'mock' }) // fresh conversation (descriptor: false)
  309. await ctx.plugin(tool, { provider: 'mock' })
  310. expect(ctx.tools.schemas().find(s => s.name === 'subagent')!.description).toContain('does not see this conversation')
  311. // Backend unloads (HMR shape): the tool must not outlive its provider.
  312. await backend.dispose()
  313. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(false)
  314. // Backend reloads with a DIFFERENT conversation-history descriptor: the wording is re-derived
  315. // from the fresh provider, not served stale from the first mount.
  316. await mock.mountScriptedProvider(ctx, { name: 'mock', inheritsParentContext: true })
  317. expect(ctx.tools.schemas().find(s => s.name === 'subagent')!.description).toContain('inherits this conversation')
  318. })
  319. it('the tool PLUGIN fiber owns its lifecycle listeners: disposal unmounts, and a disposed fiber never zombie-mounts', async () => {
  320. const ctx = new Context()
  321. await ctx.plugin(SystemPrompt)
  322. await ctx.plugin(ToolRuntime)
  323. await ctx.plugin(SubagentRuntime)
  324. // Arm 1: a mounted tool and its prompt section die with the plugin fiber;
  325. // the provider survives.
  326. ctx.subagents.registerProvider({
  327. name: 'continuable',
  328. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  329. inheritsParentContext: false,
  330. start: async () => { throw new Error('lifecycle test does not start a child') },
  331. prepareContinuable: async () => ({}),
  332. })
  333. const mounted = await ctx.plugin(tool, {
  334. provider: 'continuable',
  335. backgroundMode: 'continuable',
  336. maxDepth: 'provider-managed',
  337. })
  338. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true)
  339. expect((await ctx.systemPrompt.assemble()).sections.some(s => s.name === 'tool:subagent')).toBe(true)
  340. await mounted.dispose()
  341. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(false)
  342. expect((await ctx.systemPrompt.assemble()).sections.some(s => s.name === 'tool:subagent')).toBe(false)
  343. expect(ctx.subagents.getProvider('continuable')).toBeDefined()
  344. // Arm 2: a fiber disposed while WAITING must not react to the provider
  345. // arriving later — a surviving listener would re-register a tool that no
  346. // live plugin owns (the zombie mount).
  347. const waiting = await ctx.plugin(tool, { provider: 'later', toolName: 'subagent_later' })
  348. await waiting.dispose()
  349. await mock.mountScriptedProvider(ctx, { name: 'later' })
  350. expect(ctx.tools.schemas().some(s => s.name === 'subagent_later')).toBe(false)
  351. })
  352. it('ignores lifecycle events for OTHER providers', async () => {
  353. const ctx = new Context()
  354. await ctx.plugin(SystemPrompt)
  355. await ctx.plugin(ToolRuntime)
  356. await ctx.plugin(SubagentRuntime)
  357. await mock.mountScriptedProvider(ctx, { name: 'mock' })
  358. await ctx.plugin(tool, { provider: 'mock' })
  359. // An unrelated provider registering (added-event with another name) and
  360. // unregistering (removed-event with another name) must not touch the tool.
  361. const other = await mock.mountScriptedProvider(ctx, { name: 'other', inheritsParentContext: true })
  362. expect(ctx.tools.schemas().filter(s => s.name === 'subagent')).toHaveLength(1)
  363. expect(ctx.tools.schemas().find(s => s.name === 'subagent')!.description).toContain('does not see this conversation')
  364. await other.dispose()
  365. expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true)
  366. })
  367. it('derives spawn-shaped wording from a fresh-conversation provider (default mock)', async () => {
  368. const ctx = await setup({ provider: 'mock' })
  369. const schema = ctx.tools.schemas().find(s => s.name === 'subagent')!
  370. expect(schema.description).toContain('does not see this conversation')
  371. const props = (schema.parameters as { properties: Record<string, { description: string }> }).properties
  372. expect(props['prompt']!.description).toContain('include everything it needs')
  373. })
  374. it('derives inherited-context wording from a seeded-conversation provider', async () => {
  375. const ctx = await setup({
  376. provider: 'mock',
  377. toolName: 'subagent',
  378. }, { inheritsParentContext: true })
  379. const schema = ctx.tools.schemas().find(s => s.name === 'subagent')!
  380. expect(schema.description).toContain('inherits this conversation')
  381. expect(schema.description).not.toContain('does not see this conversation')
  382. expect(schema.description).not.toContain('can prevent provider-side reuse of the inherited conversation prefix')
  383. const props = (schema.parameters as { properties: Record<string, { description: string }> }).properties
  384. expect(props['prompt']!.description).toContain('completed turns')
  385. })
  386. it('disposes the run on the success path (no leaked child)', async () => {
  387. // Spy on the provider's run.dispose via a wrapping provider registered
  388. // directly on the service, then point the tool at it.
  389. const disposed = vi.fn()
  390. const ctx = new Context()
  391. await ctx.plugin(SystemPrompt)
  392. await ctx.plugin(ToolRuntime)
  393. await ctx.plugin(SubagentRuntime)
  394. ctx.subagents.registerProvider({
  395. name: 'spy',
  396. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  397. inheritsParentContext: false,
  398. start: async () => ({
  399. id: SessionId('spy-child'),
  400. localAgent: undefined,
  401. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  402. dispose: async () => void disposed(),
  403. }),
  404. })
  405. await ctx.plugin(tool, { provider: 'spy', maxDepth: 'provider-managed' })
  406. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  407. expect(disposed).toHaveBeenCalledTimes(1)
  408. })
  409. it('disposes the run on the error path too', async () => {
  410. const disposed = vi.fn()
  411. const ctx = new Context()
  412. await ctx.plugin(SystemPrompt)
  413. await ctx.plugin(ToolRuntime)
  414. await ctx.plugin(SubagentRuntime)
  415. ctx.subagents.registerProvider({
  416. name: 'spy',
  417. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  418. inheritsParentContext: false,
  419. start: async () => ({
  420. id: SessionId('spy-child'),
  421. localAgent: undefined,
  422. result: Promise.resolve({ output: [], stopReason: 'error' as const }),
  423. dispose: async () => void disposed(),
  424. }),
  425. })
  426. await ctx.plugin(tool, { provider: 'spy', maxDepth: 'provider-managed' })
  427. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  428. expect(result.isError).toBe(true)
  429. expect(disposed).toHaveBeenCalledTimes(1)
  430. })
  431. it('preserves independent foreground result and disposal failures', async () => {
  432. const disposed = vi.fn()
  433. const ctx = new Context()
  434. await ctx.plugin(SystemPrompt)
  435. await ctx.plugin(ToolRuntime)
  436. await ctx.plugin(SubagentRuntime)
  437. ctx.subagents.registerProvider({
  438. name: 'spy',
  439. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  440. inheritsParentContext: false,
  441. start: async () => ({
  442. id: SessionId('spy-child'),
  443. localAgent: undefined,
  444. result: Promise.reject(new Error('published run failed')),
  445. dispose: async () => {
  446. disposed()
  447. throw new Error('published handle disposal failed')
  448. },
  449. }),
  450. })
  451. await ctx.plugin(tool, { provider: 'spy', maxDepth: 'provider-managed' })
  452. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  453. expect(result.isError).toBe(true)
  454. expect(text(result)).toContain('published run failed')
  455. expect(text(result)).toContain('published handle disposal failed')
  456. expect(disposed).toHaveBeenCalledTimes(1)
  457. })
  458. it('reports a foreground disposal failure after a completed result', async () => {
  459. const ctx = new Context()
  460. await ctx.plugin(SystemPrompt)
  461. await ctx.plugin(ToolRuntime)
  462. await ctx.plugin(SubagentRuntime)
  463. ctx.subagents.registerProvider({
  464. name: 'spy',
  465. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  466. inheritsParentContext: false,
  467. start: async () => ({
  468. id: SessionId('spy-child'),
  469. localAgent: undefined,
  470. result: Promise.resolve({
  471. output: [{ type: 'text', text: 'completed before disposal' }],
  472. stopReason: 'completed',
  473. }),
  474. dispose: () => Promise.reject(new Error('published handle disposal failed')),
  475. }),
  476. })
  477. await ctx.plugin(tool, { provider: 'spy', maxDepth: 'provider-managed' })
  478. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' })
  479. expect(result.isError).toBe(true)
  480. expect(text(result)).toContain('published handle disposal failed')
  481. })
  482. it('passes the tool abort signal as the provider cancellation channel', async () => {
  483. const cancelled = vi.fn()
  484. const ctx = new Context()
  485. await ctx.plugin(SystemPrompt)
  486. await ctx.plugin(ToolRuntime)
  487. await ctx.plugin(SubagentRuntime)
  488. ctx.subagents.registerProvider({
  489. name: 'spy',
  490. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  491. inheritsParentContext: false,
  492. start: async (request) => {
  493. if (request.signal.aborted) throw new Error('start aborted')
  494. let resolveResult: (r: { output: never[]; stopReason: 'aborted' }) => void
  495. const result = new Promise<{ output: never[]; stopReason: 'aborted' }>((res) => { resolveResult = res })
  496. request.signal.addEventListener('abort', () => {
  497. cancelled()
  498. resolveResult({ output: [], stopReason: 'aborted' })
  499. }, { once: true })
  500. return {
  501. id: SessionId('spy-child'),
  502. localAgent: undefined,
  503. result,
  504. dispose: async () => {},
  505. }
  506. },
  507. })
  508. await ctx.plugin(tool, { provider: 'spy', maxDepth: 'provider-managed' })
  509. const controller = new AbortController()
  510. const pending = callSubagent(ctx, { description: 'd', prompt: 'p' }, { signal: controller.signal })
  511. // Let provider.start install its listener before aborting.
  512. await Promise.resolve()
  513. await Promise.resolve()
  514. controller.abort()
  515. const result = await pending
  516. expect(cancelled).toHaveBeenCalledTimes(1)
  517. expect(result.isError).toBe(true)
  518. })
  519. it('skips provider startup for an already-aborted signal', async () => {
  520. const sawAborted = vi.fn()
  521. const ctx = new Context()
  522. await ctx.plugin(SystemPrompt)
  523. await ctx.plugin(ToolRuntime)
  524. await ctx.plugin(SubagentRuntime)
  525. ctx.subagents.registerProvider({
  526. name: 'spy',
  527. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  528. inheritsParentContext: false,
  529. start: async (request) => {
  530. if (request.signal.aborted) sawAborted()
  531. throw new Error('start aborted')
  532. },
  533. })
  534. await ctx.plugin(tool, { provider: 'spy', maxDepth: 'provider-managed' })
  535. const controller = new AbortController()
  536. controller.abort() // already aborted BEFORE the tool runs
  537. const result = await callSubagent(ctx, { description: 'd', prompt: 'p' }, { signal: controller.signal })
  538. expect(sawAborted).not.toHaveBeenCalled()
  539. expect(result.isError).toBe(true)
  540. expect(result.error).toEqual({
  541. message: 'tool call aborted before dispatch',
  542. info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  543. })
  544. })
  545. it('tools depend on the service: no `subagent` tool without ctx.subagents', async () => {
  546. const ctx = new Context()
  547. await ctx.plugin(SystemPrompt)
  548. await ctx.plugin(ToolRuntime)
  549. // No SubagentRuntime mounted. The tool injects its three required services so its
  550. // apply never runs; the tool is absent rather than half-registered.
  551. let booted = true
  552. try {
  553. await ctx.plugin(tool, { provider: 'mock' })
  554. await new Promise(r => setTimeout(r, 20))
  555. } catch {
  556. booted = false
  557. }
  558. // Either it never booted, or it booted but registered no tool.
  559. const present = ctx.get('tools')?.schemas().some(s => s.name === 'subagent') ?? false
  560. expect(booted && present).toBe(false)
  561. })
  562. it('has the namespace-plugin export shape (no stray default) so the Loader keeps name/inject/Config/apply', () => {
  563. // Postmortem 0001 guard: this plugin HAS an explicit `inject`, so
  564. // a stray `export default apply` would collapse the module via
  565. // `unwrapExports` (`exports.default ?? exports`), DROP `inject`, and crash at
  566. // load with "cannot get property … without inject". Guard the shape directly.
  567. expect('default' in tool).toBe(false)
  568. expect(tool.name).toBe('tool-subagent')
  569. expect(tool.inject).toEqual(['tools', 'subagents', 'systemPrompt'])
  570. const loader = Object.create(Loader.prototype) as Loader
  571. const unwrapped = loader.unwrapExports(tool) as Record<string, unknown>
  572. expect(unwrapped).toBe(tool)
  573. expect(unwrapped.name).toBe('tool-subagent')
  574. expect(unwrapped.inject).toEqual(['tools', 'subagents', 'systemPrompt'])
  575. expect(typeof unwrapped.apply).toBe('function')
  576. expect(unwrapped.Config).toBeDefined()
  577. })
  578. it('passes persona/toolFilter/maxDepth config through to the start request', async () => {
  579. let seen: { persona?: string; toolFilter?: unknown; maxDepth?: number } | undefined
  580. const ctx = new Context()
  581. await ctx.plugin(SystemPrompt)
  582. await ctx.plugin(ToolRuntime)
  583. await ctx.plugin(SubagentRuntime)
  584. ctx.subagents.registerProvider({
  585. name: 'capture2',
  586. capabilities: { agentOptions: false, outputSchema: false, depthLimit: true, toolFilter: true, persona: true },
  587. inheritsParentContext: false,
  588. start: async (request) => {
  589. seen = request
  590. return {
  591. id: SessionId('capture2-child'),
  592. localAgent: undefined,
  593. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  594. dispose: async () => {},
  595. }
  596. },
  597. })
  598. await ctx.plugin(tool, {
  599. provider: 'capture2',
  600. persona: 'You are the child.',
  601. toolFilter: { deny: ['subagent'] },
  602. maxDepth: 2,
  603. })
  604. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  605. expect(seen?.persona).toBe('You are the child.')
  606. expect(seen?.toolFilter).toMatchObject({ deny: ['subagent'] })
  607. expect(seen?.maxDepth).toBe(2)
  608. })
  609. it.each([
  610. { label: 'a string', value: '1' as unknown as number },
  611. { label: 'NaN', value: Number.NaN },
  612. { label: 'positive infinity', value: Number.POSITIVE_INFINITY },
  613. { label: 'negative infinity', value: Number.NEGATIVE_INFINITY },
  614. { label: 'a negative integer', value: -1 },
  615. { label: 'a fractional number', value: 1.5 },
  616. { label: 'negative zero', value: -0 },
  617. { label: 'an unsafe integer', value: Number.MAX_SAFE_INTEGER + 1 },
  618. ])('rejects maxDepth=$label when the plugin loads', async ({ value }) => {
  619. await expect(setup({ provider: 'mock', maxDepth: value }))
  620. .rejects.toThrow()
  621. })
  622. it('validates maxDepth when apply() is invoked directly without Schemastery', () => {
  623. const ctx = new Context()
  624. expect(() => {
  625. tool.apply(ctx, {
  626. provider: 'unused',
  627. maxDepth: Number.NaN,
  628. })
  629. }).toThrow('subagent maxDepth must be a non-negative safe integer')
  630. })
  631. it('a partial toolFilter (deny only) does not materialize an empty allow-list (deny-all trap)', async () => {
  632. let seen: { toolFilter?: { readonly allow?: readonly string[]; readonly deny?: readonly string[] } } | undefined
  633. const ctx = new Context()
  634. await ctx.plugin(SystemPrompt)
  635. await ctx.plugin(ToolRuntime)
  636. await ctx.plugin(SubagentRuntime)
  637. ctx.subagents.registerProvider({
  638. name: 'capture3',
  639. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: true, persona: false },
  640. inheritsParentContext: false,
  641. start: async (request) => {
  642. seen = request
  643. return {
  644. id: SessionId('capture3-child'),
  645. localAgent: undefined,
  646. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  647. dispose: async () => {},
  648. }
  649. },
  650. })
  651. await ctx.plugin(tool, { provider: 'capture3', toolFilter: { deny: ['subagent'] }, maxDepth: 'provider-managed' })
  652. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  653. expect(seen?.toolFilter).toEqual({ deny: ['subagent'] })
  654. expect(seen?.toolFilter).not.toHaveProperty('allow')
  655. })
  656. it('an omitted agentOptions does not materialize an empty object onto the request', async () => {
  657. // Same schemastery trap as toolFilter, adjacent field: an omitted
  658. // `agentOptions` config key materializes `{}` without the forced default,
  659. // which reads as present and puts a dishonest `agentOptions: {}` on every
  660. // start request.
  661. let seen: { agentOptions?: unknown } | undefined
  662. const ctx = new Context()
  663. await ctx.plugin(SystemPrompt)
  664. await ctx.plugin(ToolRuntime)
  665. await ctx.plugin(SubagentRuntime)
  666. ctx.subagents.registerProvider({
  667. name: 'capture4',
  668. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  669. inheritsParentContext: false,
  670. start: async (request) => {
  671. seen = request
  672. return {
  673. id: SessionId('capture4-child'),
  674. localAgent: undefined,
  675. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  676. dispose: async () => {},
  677. }
  678. },
  679. })
  680. await ctx.plugin(tool, { provider: 'capture4', maxDepth: 'provider-managed' })
  681. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  682. expect(seen).toBeDefined()
  683. expect(seen).not.toHaveProperty('agentOptions')
  684. })
  685. it('an explicit empty toolFilter fails at plugin load, not at first delegation', async () => {
  686. const ctx = new Context()
  687. await ctx.plugin(SystemPrompt)
  688. await ctx.plugin(ToolRuntime)
  689. await ctx.plugin(SubagentRuntime)
  690. ctx.subagents.registerProvider({
  691. name: 'p',
  692. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: true, persona: false },
  693. inheritsParentContext: false,
  694. start: () => { throw new Error('unreachable') },
  695. })
  696. const fiber = ctx.plugin(tool, { provider: 'p', toolFilter: {} })
  697. await expect(fiber).rejects.toThrow(/names neither `allow` nor `deny`/)
  698. })
  699. })
  700. describe('dsh-tool-subagent background mode', () => {
  701. /** A live parent with a dedicated scope fiber for structural task cleanup. */
  702. function ownerAgent(ctx: Context, sessionId: string, inject: (...args: unknown[]) => void = () => {}): Agent {
  703. const scopeFiber = ctx.plugin(() => {})
  704. const id = SessionId(sessionId)
  705. const agent = {
  706. id,
  707. ctx: scopeFiber.ctx,
  708. inject,
  709. options: {},
  710. session: Session.create(id),
  711. } as unknown as Agent
  712. ctx.agents.register(agent)
  713. return agent
  714. }
  715. async function backgroundSetup(toolConfig: tool.Config, mockConfig: Partial<mock.Config> = {}) {
  716. const ctx = await setup(toolConfig, mockConfig)
  717. await ctx.plugin(AgentRegistry)
  718. await ctx.plugin(LocalJobRegistry)
  719. await ctx.plugin(ToolTasks, {})
  720. return ctx
  721. }
  722. it('keeps a continuable-capable provider one-shot when backgroundMode selects one-shot', async () => {
  723. const ctx = await backgroundSetup({ provider: 'mock' })
  724. const parent = ownerAgent(ctx, 'sess-parent')
  725. let prepareCalls = 0
  726. ctx.subagents.registerProvider({
  727. name: 'resumable',
  728. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  729. inheritsParentContext: false,
  730. start: async request => ({
  731. id: SessionId('one-shot-child'),
  732. localAgent: undefined,
  733. result: Promise.resolve({
  734. output: [{ type: 'text', text: 'one-shot answer' }],
  735. stopReason: request.signal.aborted ? 'aborted' : 'completed',
  736. }),
  737. dispose: () => Promise.resolve(),
  738. }),
  739. prepareContinuable: async () => {
  740. prepareCalls += 1
  741. throw new Error('one-shot policy must not prepare a continuable child')
  742. },
  743. })
  744. tool.apply(ctx, {
  745. provider: 'resumable',
  746. toolName: 'subagent_resumable',
  747. backgroundMode: 'one-shot',
  748. maxDepth: 'provider-managed',
  749. })
  750. const started = await ctx.tools.execute({
  751. signal: testToolSignal,
  752. callId: CallId('resumable-one-shot'),
  753. name: 'subagent_resumable',
  754. arguments: { description: 'work', prompt: 'go', run_in_background: true },
  755. agent: parent,
  756. })
  757. expect(text(started)).toBe('started background subagent job subagent-1')
  758. expect(prepareCalls).toBe(0)
  759. })
  760. it('returns a job id immediately and the answer is collected through job_output', async () => {
  761. const ctx = await backgroundSetup({ provider: 'mock' }, { reply: 'background answer' })
  762. const parent = ownerAgent(ctx, 'sess-parent')
  763. const start = await callSubagent(ctx, { description: 'deep research', prompt: 'dig in', run_in_background: true }, { agent: parent })
  764. expect(start.isError).toBe(false)
  765. if (start.isError) throw new Error('expected background subagent success')
  766. expect(start.value).toEqual({ kind: 'background', jobId: 'subagent-1' })
  767. expect(text(start)).toBe('started background subagent job subagent-1')
  768. const collected = await ctx.tools.execute({
  769. signal: testToolSignal,
  770. callId: CallId('collect-1'),
  771. name: 'job_output',
  772. arguments: { job_id: 'subagent-1', wait: true },
  773. agent: parent,
  774. })
  775. expect(text(collected)).toBe('background answer\n[status: completed]')
  776. // Final-output reads are idempotent (not consumed).
  777. const again = await ctx.tools.execute({
  778. signal: testToolSignal,
  779. callId: CallId('collect-2'),
  780. name: 'job_output',
  781. arguments: { job_id: 'subagent-1' },
  782. agent: parent,
  783. })
  784. expect(text(again)).toBe('background answer\n[status: completed]')
  785. })
  786. it('preserves provider diagnostics in one-shot background failure detail', async () => {
  787. const ctx = await backgroundSetup({ provider: 'mock' }, {
  788. reply: 'not background output',
  789. diagnostic: 'Claude Code cancelled an unattended dialog',
  790. stopReason: 'error',
  791. })
  792. const parent = ownerAgent(ctx, 'sess-parent')
  793. const started = await ctx.tools.execute({
  794. signal: testToolSignal,
  795. callId: CallId('diagnostic-background-start'),
  796. name: 'subagent',
  797. arguments: { description: 'd', prompt: 'p', run_in_background: true },
  798. agent: parent,
  799. })
  800. expect(text(started)).toBe('started background subagent job subagent-1')
  801. const output = await ctx.tools.execute({
  802. signal: testToolSignal,
  803. callId: CallId('diagnostic-background-output'),
  804. name: 'job_output',
  805. arguments: { job_id: 'subagent-1', wait: true },
  806. agent: parent,
  807. })
  808. expect(text(output)).toBe(
  809. '(no new output)\n'
  810. + '[status: failed, error; diagnostic: Claude Code cancelled an unattended dialog]',
  811. )
  812. })
  813. it('fails loud when the tasks runtime is not loaded', async () => {
  814. const ctx = await setup({ provider: 'mock' })
  815. const result = await callSubagent(ctx, { description: 'd', prompt: 'p', run_in_background: true })
  816. expect(result.isError).toBe(true)
  817. expect(text(result)).toContain('background jobs unavailable: load @deepseek-ai/dsh-jobs')
  818. })
  819. it('skips background startup when the tool signal is already aborted', async () => {
  820. const ctx = await backgroundSetup({ provider: 'mock' })
  821. const parent = ownerAgent(ctx, 'sess-parent')
  822. const controller = new AbortController()
  823. controller.abort()
  824. const result = await callSubagent(ctx, { description: 'd', prompt: 'p', run_in_background: true }, { agent: parent, signal: controller.signal })
  825. expect(result.isError).toBe(true)
  826. expect(result.error).toEqual({
  827. message: 'tool call aborted before dispatch',
  828. info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  829. })
  830. expect(text(result)).toBe('Error: tool call aborted before dispatch')
  831. })
  832. it('skips background startup when cancellation wins asynchronous route preflight', async () => {
  833. const ctx = await backgroundSetup({ provider: 'mock', enableModelSelection: true })
  834. const parent = ownerAgent(ctx, 'sess-parent')
  835. const adapter = new MockAdapter([])
  836. let releasePreflight!: () => void
  837. const preflightGate = new Promise<void>((resolve) => { releasePreflight = resolve })
  838. const resolveModel = vi.spyOn(adapter, 'resolveModel').mockImplementation(async (provider, model) => {
  839. await preflightGate
  840. return { provider, id: model, name: model }
  841. })
  842. ctx.llm.registerAdapter(['alpha'], adapter)
  843. const controller = new AbortController()
  844. const resultPromise = callSubagent(ctx, {
  845. description: 'cancelled selection',
  846. prompt: 'do it',
  847. provider: 'alpha',
  848. model: 'selected-model',
  849. run_in_background: true,
  850. }, { agent: parent, signal: controller.signal })
  851. await vi.waitFor(() => { expect(resolveModel).toHaveBeenCalledOnce() })
  852. controller.abort()
  853. releasePreflight()
  854. const result = await resultPromise
  855. expect(result.isError).toBe(true)
  856. expect(ctx.jobs.list(parent)).toEqual([])
  857. })
  858. it('settles an asynchronous provider-start failure as a failed task', async () => {
  859. const ctx = await backgroundSetup({ provider: 'mock' })
  860. const parent = ownerAgent(ctx, 'sess-parent')
  861. ctx.subagents.registerProvider({
  862. name: 'broken-start',
  863. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  864. inheritsParentContext: false,
  865. start: async () => { throw new Error('setup failed') },
  866. })
  867. tool.apply(ctx, { provider: 'broken-start', toolName: 'subagent_broken' })
  868. const started = await ctx.tools.execute({
  869. signal: testToolSignal,
  870. callId: CallId('broken-start'),
  871. name: 'subagent_broken',
  872. arguments: { description: 'broken', prompt: 'p', run_in_background: true },
  873. agent: parent,
  874. })
  875. expect(text(started)).toBe('started background subagent job subagent-1')
  876. const output = await ctx.tools.execute({
  877. signal: testToolSignal,
  878. callId: CallId('broken-output'),
  879. name: 'job_output',
  880. arguments: { job_id: 'subagent-1', wait: true },
  881. agent: parent,
  882. })
  883. expect(text(output)).toContain('[status: failed, Error: setup failed]')
  884. })
  885. it('kills a subagent task while provider readiness is still pending', async () => {
  886. const ctx = await backgroundSetup({ provider: 'mock' })
  887. const parent = ownerAgent(ctx, 'sess-parent')
  888. ctx.subagents.registerProvider({
  889. name: 'pending-start',
  890. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  891. inheritsParentContext: false,
  892. start: request => new Promise((_resolve, reject) => {
  893. request.signal.addEventListener('abort', () => { reject(new Error('startup aborted')) }, { once: true })
  894. }),
  895. })
  896. tool.apply(ctx, { provider: 'pending-start', toolName: 'subagent_pending' })
  897. await ctx.tools.execute({
  898. signal: testToolSignal,
  899. callId: CallId('pending-start'),
  900. name: 'subagent_pending',
  901. arguments: { description: 'pending', prompt: 'p', run_in_background: true },
  902. agent: parent,
  903. })
  904. await ctx.tools.execute({
  905. signal: testToolSignal,
  906. callId: CallId('pending-kill'),
  907. name: 'job_kill',
  908. arguments: { job_id: 'subagent-1', reason: 'no longer needed' },
  909. agent: parent,
  910. })
  911. const output = await ctx.tools.execute({
  912. signal: testToolSignal,
  913. callId: CallId('pending-output'),
  914. name: 'job_output',
  915. arguments: { job_id: 'subagent-1', wait: true },
  916. agent: parent,
  917. })
  918. expect(text(output)).toBe('(no new output)\n[status: killed]')
  919. })
  920. it('reports startup rollback failure after cancellation as a failed job', async () => {
  921. const ctx = await backgroundSetup({ provider: 'mock' })
  922. const parent = ownerAgent(ctx, 'sess-parent')
  923. ctx.subagents.registerProvider({
  924. name: 'broken-start-rollback',
  925. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  926. inheritsParentContext: false,
  927. start: request => new Promise((_resolve, reject) => {
  928. request.signal.addEventListener('abort', () => {
  929. reject(new AggregateError(
  930. [new Error('startup aborted'), new Error('cleanup failed')],
  931. 'startup failed and cleanup also failed',
  932. ))
  933. }, { once: true })
  934. }),
  935. })
  936. tool.apply(ctx, { provider: 'broken-start-rollback', toolName: 'subagent_broken_rollback' })
  937. await ctx.tools.execute({
  938. signal: testToolSignal,
  939. callId: CallId('broken-rollback-start'),
  940. name: 'subagent_broken_rollback',
  941. arguments: { description: 'broken rollback', prompt: 'p', run_in_background: true },
  942. agent: parent,
  943. })
  944. await ctx.tools.execute({
  945. signal: testToolSignal,
  946. callId: CallId('broken-rollback-kill'),
  947. name: 'job_kill',
  948. arguments: { job_id: 'subagent-1' },
  949. agent: parent,
  950. })
  951. const output = await ctx.tools.execute({
  952. signal: testToolSignal,
  953. callId: CallId('broken-rollback-output'),
  954. name: 'job_output',
  955. arguments: { job_id: 'subagent-1', wait: true },
  956. agent: parent,
  957. })
  958. expect(text(output)).toContain('[status: failed, AggregateError: startup failed and cleanup also failed]')
  959. })
  960. it('forwards job_kill reasons through the run signal (and defaults one when absent)', async () => {
  961. // Use a provider that remains live until its signal is aborted.
  962. const ctx = await backgroundSetup({ provider: 'mock', agentOptions: { model: 'child-model' } })
  963. const parent = ownerAgent(ctx, 'sess-parent')
  964. const cancels: (string | undefined)[] = []
  965. let starts = 0
  966. ctx.subagents.registerProvider({
  967. name: 'hanging',
  968. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  969. inheritsParentContext: false,
  970. start: async (request) => {
  971. let settle!: (value: { output: { type: 'text'; text: string }[]; stopReason: 'aborted' }) => void
  972. const id = SessionId(`hang-${++starts}`)
  973. const result = new Promise<{ output: { type: 'text'; text: string }[]; stopReason: 'aborted' }>((res) => { settle = res })
  974. request.signal.addEventListener('abort', () => {
  975. cancels.push(typeof request.signal.reason === 'string' ? request.signal.reason : undefined)
  976. settle({ output: [], stopReason: 'aborted' })
  977. }, { once: true })
  978. return {
  979. id,
  980. localAgent: undefined,
  981. result,
  982. dispose: () => Promise.resolve(),
  983. }
  984. },
  985. })
  986. // Direct apply preserves omitted agentOptions instead of applying schema defaults.
  987. tool.apply(ctx, { provider: 'hanging', toolName: 'subagent_hang' })
  988. const startOne = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('h1'), name: 'subagent_hang', arguments: { description: 'one', prompt: 'p', run_in_background: true }, agent: parent })
  989. const startTwo = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('h2'), name: 'subagent_hang', arguments: { description: 'two', prompt: 'p', run_in_background: true }, agent: parent })
  990. expect(text(startOne)).toBe('started background subagent job subagent-1')
  991. expect(text(startTwo)).toBe('started background subagent job subagent-2')
  992. const withReason = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('k1'), name: 'job_kill', arguments: { job_id: 'subagent-1', reason: 'superseded' }, agent: parent })
  993. const withoutReason = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('k2'), name: 'job_kill', arguments: { job_id: 'subagent-2' }, agent: parent })
  994. expect(text(withReason)).toBe('requested cancellation of job subagent-1')
  995. expect(text(withoutReason)).toBe('requested cancellation of job subagent-2')
  996. expect(cancels).toEqual(['superseded', 'background subagent task killed'])
  997. // The aborted children settle as killed tasks.
  998. const killed = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('w1'), name: 'job_output', arguments: { job_id: 'subagent-1', wait: true }, agent: parent })
  999. expect(text(killed)).toBe('(no new output)\n[status: killed]')
  1000. })
  1001. })
  1002. describe('dsh-tool-subagent continuable background mode', () => {
  1003. const roots: string[] = []
  1004. afterEach(() => {
  1005. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  1006. })
  1007. /** Boot the real continuable stack without any model-facing follow-up adapter. */
  1008. async function continuableSetup() {
  1009. const ctx = new Context()
  1010. await mountAgentLoopTestDependencies(ctx)
  1011. const root = mkdtempSync(path.join(tmpdir(), 'dsh-tool-subagent-continuable-'))
  1012. roots.push(root)
  1013. await ctx.plugin(JsonlSessionPersistence, { root })
  1014. await ctx.plugin(AgentLoop, { agents: [] })
  1015. await ctx.plugin(SubagentRuntime)
  1016. await ctx.plugin(SubagentSpawn, { providerName: 'spawn' })
  1017. await ctx.plugin(LocalJobRegistry)
  1018. await ctx.plugin(ToolTasks, {})
  1019. await ctx.plugin(tool, { provider: 'spawn', backgroundMode: 'continuable' })
  1020. ctx.llm.registerAdapter(['mock'], new MockAdapter([
  1021. textResponse('continuable answer'),
  1022. ]))
  1023. const parent = ctx.agentLoop.create(SessionId('parent'), { provider: 'mock', model: 'mock' })
  1024. return { ctx, parent }
  1025. }
  1026. it('classifies continuable background calls concurrency-safe', async () => {
  1027. const { ctx } = await continuableSetup()
  1028. expect(ctx.tools.executionMode({
  1029. signal: testToolSignal,
  1030. callId: CallId('subagent-continuable'),
  1031. name: 'subagent',
  1032. arguments: { description: 'do work', prompt: 'Reply OK' },
  1033. })).toEqual({ kind: 'parallel' })
  1034. })
  1035. it('defaults continuable delegation to background and returns only its durable id', async () => {
  1036. const { ctx, parent } = await continuableSetup()
  1037. const schema = ctx.tools.schemas().find(s => s.name === 'subagent')!
  1038. // Continuable delegation has no Task, so the schema promises no collection.
  1039. expect(schema.description).not.toContain('job_output')
  1040. expect(schema.description).not.toContain('job_kill')
  1041. expect(schema.description).toContain('send_message')
  1042. expect(schema.description).toContain('runs in the background by default')
  1043. expect(schema.description).not.toContain('never poll or wait on it')
  1044. const properties = (schema.parameters as {
  1045. properties: Record<string, { description?: string }>
  1046. }).properties
  1047. expect(properties.run_in_background?.description).toContain('Defaults to true')
  1048. const assembly = await ctx.systemPrompt.assemble(assembleContextFor(parent))
  1049. const guidance = assembly.sections.find(section => section.name === 'tool:subagent')
  1050. expect(guidance?.text).toContain('Use subagent in the background by default')
  1051. expect(guidance?.text).toContain('runtime sends you a notice containing its outcome')
  1052. const started = await callSubagent(
  1053. ctx,
  1054. { description: 'continuable work', prompt: 'dig in' },
  1055. { agent: parent },
  1056. )
  1057. expect(started.isError).toBe(false)
  1058. const match = /^started subagent (\S+)$/.exec(text(started))
  1059. expect(match).not.toBeNull()
  1060. const [, childId] = match!
  1061. // No Task was created for the continuable child.
  1062. expect(ctx.jobs.list(parent)).toEqual([])
  1063. await vi.waitFor(() => {
  1064. expect(ctx.agents.get(SessionId(childId!))).toBeUndefined()
  1065. }, { timeout: 5_000 })
  1066. // The child id names a durable session carrying its continuation descriptor.
  1067. const loaded = await ctx.sessionPersistence.load(SessionId(childId!))
  1068. expect(loaded.events.some(event => event.type === 'subagent/descriptor')).toBe(true)
  1069. expect(loaded.events.some(event => event.type === 'assistant/message')).toBe(true)
  1070. })
  1071. it('hides continuable guidance when the current agent cannot see the tool', async () => {
  1072. const { ctx, parent } = await continuableSetup()
  1073. parent.ctx.tools.restrict({ deny: ['subagent'] })
  1074. expect(ctx.tools.get('subagent', parent)).toBeUndefined()
  1075. const assembly = await ctx.systemPrompt.assemble(assembleContextFor(parent))
  1076. expect(assembly.sections.find(section => section.name === 'tool:subagent')?.text).toBe('')
  1077. })
  1078. it('waits for a continuable provider only when run_in_background is explicitly false', async () => {
  1079. const { ctx, parent } = await continuableSetup()
  1080. const result = await callSubagent(
  1081. ctx,
  1082. { description: 'blocking work', prompt: 'dig in', run_in_background: false },
  1083. { agent: parent },
  1084. )
  1085. expect(result.isError).toBe(false)
  1086. if (result.isError) throw new Error('expected foreground subagent success')
  1087. expect(result.value).toMatchObject({ kind: 'foreground' })
  1088. expect(text(result)).toBe('continuable answer')
  1089. expect(ctx.jobs.list(parent)).toEqual([])
  1090. })
  1091. it('isolates a cancelled continuable preparation from a concurrent sibling', async () => {
  1092. const { ctx, parent } = await continuableSetup()
  1093. const bothPreparing = Promise.withResolvers<undefined>()
  1094. const releasePreparations = Promise.withResolvers<undefined>()
  1095. const cancelled = new AbortController()
  1096. let preparationCount = 0
  1097. let cancelledChildId: ReturnType<typeof SessionId> | undefined
  1098. let survivingChildId: ReturnType<typeof SessionId> | undefined
  1099. ctx.subagents.registerProvider({
  1100. name: 'gated',
  1101. capabilities: { agentOptions: false, outputSchema: true, depthLimit: true, toolFilter: true, persona: true },
  1102. inheritsParentContext: false,
  1103. start: async () => { throw new Error('continuable policy must not start a one-shot child') },
  1104. prepareContinuable: async (request) => {
  1105. preparationCount += 1
  1106. if (request.signal === cancelled.signal) cancelledChildId = request.sessionId
  1107. else survivingChildId = request.sessionId
  1108. if (preparationCount === 2) bothPreparing.resolve(undefined)
  1109. await releasePreparations.promise
  1110. return {}
  1111. },
  1112. })
  1113. tool.apply(ctx, {
  1114. provider: 'gated',
  1115. toolName: 'subagent_gated',
  1116. backgroundMode: 'continuable',
  1117. maxDepth: 3,
  1118. })
  1119. const execute = (callId: string, description: string, signal: AbortSignal) => ctx.tools.execute({
  1120. signal,
  1121. callId: CallId(callId),
  1122. name: 'subagent_gated',
  1123. arguments: { description, prompt: 'work', run_in_background: true },
  1124. agent: parent,
  1125. })
  1126. const cancelledResult = execute('continuable-cancelled', 'cancelled sibling', cancelled.signal)
  1127. const survivingResult = execute('continuable-surviving', 'surviving sibling', testToolSignal)
  1128. await bothPreparing.promise
  1129. cancelled.abort()
  1130. releasePreparations.resolve(undefined)
  1131. const [failed, succeeded] = await Promise.all([cancelledResult, survivingResult])
  1132. expect(preparationCount).toBe(2)
  1133. expect(failed.isError).toBe(true)
  1134. expect(succeeded.isError).toBe(false)
  1135. expect(cancelledChildId).toBeDefined()
  1136. expect(survivingChildId).toBeDefined()
  1137. expect(ctx.agents.get(cancelledChildId!)).toBeUndefined()
  1138. await expect(ctx.sessionPersistence.load(cancelledChildId!)).rejects.toThrow(/not found/)
  1139. expect(succeeded.isError ? undefined : succeeded.value).toEqual({
  1140. kind: 'continuable',
  1141. subagentId: survivingChildId,
  1142. })
  1143. await vi.waitFor(() => {
  1144. expect(ctx.agents.get(survivingChildId!)).toBeUndefined()
  1145. }, { timeout: 5_000 })
  1146. const loaded = await ctx.sessionPersistence.load(survivingChildId!)
  1147. expect(loaded.events.some(event => event.type === 'subagent/descriptor')).toBe(true)
  1148. expect(loaded.events.some(event => event.type === 'assistant/message')).toBe(true)
  1149. })
  1150. })
  1151. describe('background preflight failure (no orphaned child, by construction)', () => {
  1152. it('never starts the child when tasks.start preflight throws', async () => {
  1153. // With no job controller, preflight fails before the provider can spawn.
  1154. const ctx = await setup({ provider: 'mock' })
  1155. await ctx.plugin(AgentRegistry)
  1156. await ctx.plugin(LocalJobRegistry)
  1157. const scopeFiber = ctx.plugin(() => {})
  1158. const id = SessionId('sess-p')
  1159. const parent = {
  1160. id,
  1161. ctx: scopeFiber.ctx,
  1162. inject: () => {},
  1163. options: {},
  1164. session: Session.create(id),
  1165. } as unknown as Agent
  1166. ctx.agents.register(parent)
  1167. let starts = 0
  1168. ctx.subagents.registerProvider({
  1169. name: 'probe',
  1170. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  1171. inheritsParentContext: false,
  1172. start: async () => {
  1173. starts += 1
  1174. return {
  1175. id: SessionId('probe-child'),
  1176. localAgent: undefined,
  1177. result: Promise.resolve({ output: [], stopReason: 'completed' as const }),
  1178. dispose: () => Promise.resolve(),
  1179. }
  1180. },
  1181. })
  1182. tool.apply(ctx, { provider: 'probe', toolName: 'subagent_probe' })
  1183. const result = await ctx.tools.execute({
  1184. signal: testToolSignal,
  1185. callId: CallId('probe-1'),
  1186. name: 'subagent_probe',
  1187. arguments: { description: 'd', prompt: 'p', run_in_background: true },
  1188. agent: parent,
  1189. })
  1190. expect(result.isError).toBe(true)
  1191. expect(text(result)).toContain('no job controller serves this agent')
  1192. // Declare-then-execute: the failed preflight means no child ever existed.
  1193. expect(starts).toBe(0)
  1194. })
  1195. })
  1196. describe('depth budget configuration', () => {
  1197. /** Mount the tool over a request-capturing provider with full capabilities. */
  1198. async function captureSetup(config: Omit<tool.Config, 'provider'> = {}) {
  1199. const requests: SubagentStartRequest[] = []
  1200. const ctx = new Context()
  1201. await ctx.plugin(SystemPrompt)
  1202. await ctx.plugin(ToolRuntime)
  1203. await ctx.plugin(SubagentRuntime)
  1204. ctx.subagents.registerProvider({
  1205. name: 'capture',
  1206. capabilities: { agentOptions: false, outputSchema: true, depthLimit: true, toolFilter: true, persona: true },
  1207. inheritsParentContext: false,
  1208. start: async (request) => {
  1209. requests.push(request)
  1210. return {
  1211. id: SessionId(`capture-child-${requests.length}`),
  1212. localAgent: undefined,
  1213. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  1214. dispose: async () => {},
  1215. }
  1216. },
  1217. })
  1218. await ctx.plugin(tool, { provider: 'capture', ...config })
  1219. return { ctx, requests }
  1220. }
  1221. it('defaults maxDepth to 3 and forwards it in the start request', async () => {
  1222. const { ctx, requests } = await captureSetup()
  1223. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  1224. expect(requests[0]?.label).toBe('d')
  1225. expect(requests[0]?.maxDepth).toBe(3)
  1226. expect(requests[0]?.toolFilter).toBeUndefined()
  1227. })
  1228. it('forwards an explicit tool filter unchanged instead of encoding the depth policy into it', async () => {
  1229. const { ctx, requests } = await captureSetup({ toolFilter: { deny: ['dangerous'] }, maxDepth: 0 })
  1230. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  1231. expect(requests[0]?.maxDepth).toBe(0)
  1232. expect(requests[0]?.toolFilter).toEqual({ deny: ['dangerous'] })
  1233. })
  1234. it('rejects a numeric maxDepth on a provider without the depthLimit capability at mount', async () => {
  1235. const ctx = new Context()
  1236. await ctx.plugin(SystemPrompt)
  1237. await ctx.plugin(ToolRuntime)
  1238. await ctx.plugin(SubagentRuntime)
  1239. ctx.subagents.registerProvider({
  1240. name: 'no-depth',
  1241. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  1242. inheritsParentContext: false,
  1243. start: async () => { throw new Error('unreachable') },
  1244. })
  1245. await expect(ctx.plugin(tool, { provider: 'no-depth' }))
  1246. .rejects.toThrow(/provider-managed/)
  1247. })
  1248. it("'provider-managed' omits the cap so a capability-less provider mounts and starts", async () => {
  1249. const requests: SubagentStartRequest[] = []
  1250. const ctx = new Context()
  1251. await ctx.plugin(SystemPrompt)
  1252. await ctx.plugin(ToolRuntime)
  1253. await ctx.plugin(SubagentRuntime)
  1254. ctx.subagents.registerProvider({
  1255. name: 'external',
  1256. capabilities: { agentOptions: false, outputSchema: false, depthLimit: false, toolFilter: false, persona: false },
  1257. inheritsParentContext: false,
  1258. start: async (request) => {
  1259. requests.push(request)
  1260. return {
  1261. id: SessionId('external-child'),
  1262. localAgent: undefined,
  1263. result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }),
  1264. dispose: async () => {},
  1265. }
  1266. },
  1267. })
  1268. await ctx.plugin(tool, { provider: 'external', maxDepth: 'provider-managed' })
  1269. await callSubagent(ctx, { description: 'd', prompt: 'p' })
  1270. expect(requests[0]?.maxDepth).toBeUndefined()
  1271. expect(requests[0]?.toolFilter).toBeUndefined()
  1272. })
  1273. })