tool-calls.spec.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /**
  2. * Exercises scheduler ordering and cancellation with deterministic gated tools.
  3. * ACP expected outputs own transcript-facing coverage.
  4. */
  5. import { describe, expect, it } from 'vitest'
  6. import { Context } from 'cordis'
  7. import { createUserMessage, CallId, StreamChunk } from '@deepseek-ai/dsh-llm'
  8. import SessionStore, { SessionEvent, SessionId } from '@deepseek-ai/dsh-session'
  9. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  10. import LlmService from '@deepseek-ai/dsh-llm'
  11. import ToolRegistry, { defineContentToolFixture, TOOL_ABORTED_BEFORE_DISPATCH, TOOL_REGISTRY_SCHEDULER, type PostToolDecision, type PreToolDecision } from '@deepseek-ai/dsh-tools'
  12. import AgentRegistry, { type Agent } from '@deepseek-ai/dsh-agent'
  13. import AgentLoop, { DEFAULT_MAX_PARALLEL_TOOL_CALLS } from '@deepseek-ai/dsh-agent-loop'
  14. import { MockAdapter, textResponse } from './mock-adapter.ts'
  15. async function harness(adapter: MockAdapter, maxParallelToolCalls?: number) {
  16. const ctx = new Context()
  17. await ctx.plugin(LlmService)
  18. await ctx.plugin(SessionStore)
  19. await ctx.plugin(SystemPrompt, { persona: '' })
  20. await ctx.plugin(ToolRegistry)
  21. await ctx.plugin(AgentRegistry)
  22. await ctx.plugin(AgentLoop, {
  23. agents: [],
  24. ...maxParallelToolCalls === undefined ? {} : { maxParallelToolCalls },
  25. })
  26. ctx.llm.registerAdapter(['mock'], adapter)
  27. return ctx
  28. }
  29. function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  30. return new Promise((resolve) => {
  31. const dispose = ctx.on('agent/status', (subject, status) => {
  32. if (subject === agent && status === 'idle') { dispose(); resolve() }
  33. })
  34. })
  35. }
  36. function events(agent: Agent): SessionEvent[] {
  37. return [...agent.session.events]
  38. }
  39. /** Build one assistant response containing the supplied tool calls. */
  40. function multiCall(calls: { id: string; name: string; args: object }[]): StreamChunk[] {
  41. const chunks: StreamChunk[] = []
  42. calls.forEach((call, index) => {
  43. chunks.push(
  44. { type: 'block-start', index, blockType: 'tool-call' },
  45. { type: 'block-end', index, block: { type: 'tool-call', id: CallId(call.id), name: call.name, arguments: JSON.stringify(call.args) } },
  46. )
  47. })
  48. chunks.push(
  49. { type: 'usage', usage: { inputTokens: 5, outputTokens: 5 } },
  50. { type: 'finish', reason: { kind: 'tool-calls' } },
  51. )
  52. return chunks
  53. }
  54. /** A tool whose calls block until the test releases them by callId. */
  55. function gatedTool(name: string, parallel: boolean) {
  56. const gates = new Map<string, () => void>()
  57. const started: string[] = []
  58. const tool = defineContentToolFixture({
  59. name,
  60. description: `gated ${name}`,
  61. parameters: { id: { type: 'string', required: true } },
  62. ...parallel ? { isConcurrencySafe: () => true } : {},
  63. async execute(args) {
  64. started.push(args.id)
  65. await new Promise<void>((resolve) => { gates.set(args.id, resolve) })
  66. return [{ type: 'text', text: `done-${args.id}` }]
  67. },
  68. })
  69. return {
  70. tool,
  71. started,
  72. release(id: string) { gates.get(id)?.(); gates.delete(id) },
  73. pending() { return [...gates.keys()] },
  74. }
  75. }
  76. function gatedParallelTool(name: string) {
  77. return gatedTool(name, true)
  78. }
  79. function gatedExclusiveTool(name: string) {
  80. return gatedTool(name, false)
  81. }
  82. /** Poll until `predicate` holds, letting microtasks/timers drain between checks. */
  83. async function until(predicate: () => boolean): Promise<void> {
  84. for (let i = 0; i < 1000 && !predicate(); i++) await new Promise(r => setTimeout(r, 0))
  85. if (!predicate()) throw new Error('until: condition never held')
  86. }
  87. describe('tool-call scheduler: grouping and barriers', () => {
  88. it('runs parallel-safe siblings concurrently (all start before any completes)', async () => {
  89. const adapter = new MockAdapter([
  90. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }, { id: 'c3', name: 'p', args: { id: '3' } }]),
  91. textResponse('done'),
  92. ])
  93. const ctx = await harness(adapter)
  94. const gated = gatedParallelTool('p')
  95. ctx.tools.register(gated.tool)
  96. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  97. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  98. await until(() => gated.started.length === 3)
  99. expect(gated.started).toEqual(['1', '2', '3'])
  100. gated.release('1'); gated.release('2'); gated.release('3')
  101. await waitForIdle(ctx, agent)
  102. })
  103. it('an exclusive call between two parallel-safe calls forms a barrier (3 groups)', async () => {
  104. const order: string[] = []
  105. const adapter = new MockAdapter([
  106. multiCall([
  107. { id: 'c1', name: 'r', args: { id: 'A1' } },
  108. { id: 'c2', name: 'w', args: { id: 'A2' } },
  109. { id: 'c3', name: 'r', args: { id: 'A3' } },
  110. ]),
  111. textResponse('done'),
  112. ])
  113. const ctx = await harness(adapter)
  114. ctx.tools.register(defineContentToolFixture({
  115. name: 'r', description: 'read', parameters: { id: { type: 'string', required: true } },
  116. isConcurrencySafe: () => true,
  117. async execute(args) { order.push(`r-start-${args.id}`); order.push(`r-end-${args.id}`); return [{ type: 'text', text: 'r' }] },
  118. }))
  119. ctx.tools.register(defineContentToolFixture({
  120. name: 'w', description: 'write', parameters: { id: { type: 'string', required: true } },
  121. async execute(args) { order.push(`w-${args.id}`); return [{ type: 'text', text: 'w' }] },
  122. }))
  123. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  124. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  125. await waitForIdle(ctx, agent)
  126. expect(order).toEqual(['r-start-A1', 'r-end-A1', 'w-A2', 'r-start-A3', 'r-end-A3'])
  127. })
  128. it('reclassifies pending calls after an exclusive barrier replaces their tool', async () => {
  129. const adapter = new MockAdapter([
  130. multiCall([
  131. { id: 'c1', name: 'replace', args: { id: '0' } },
  132. { id: 'c2', name: 'x', args: { id: '1' } },
  133. { id: 'c3', name: 'x', args: { id: '2' } },
  134. ]),
  135. textResponse('done'),
  136. ])
  137. const ctx = await harness(adapter)
  138. const replacement = gatedExclusiveTool('x')
  139. const disposeSafe = ctx.tools.register(defineContentToolFixture({
  140. name: 'x',
  141. description: 'initially safe',
  142. parameters: { id: { type: 'string', required: true } },
  143. isConcurrencySafe: () => true,
  144. async execute(args) { return [{ type: 'text', text: `old-${args.id}` }] },
  145. }))
  146. ctx.tools.register(defineContentToolFixture({
  147. name: 'replace',
  148. description: 'replace x',
  149. parameters: { id: { type: 'string', required: true } },
  150. async execute() {
  151. disposeSafe()
  152. ctx.tools.register(replacement.tool)
  153. return [{ type: 'text', text: 'replaced' }]
  154. },
  155. }))
  156. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  157. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  158. await until(() => replacement.started.length === 1)
  159. await new Promise(r => setTimeout(r, 5))
  160. expect(replacement.started).toEqual(['1'])
  161. replacement.release('1')
  162. await until(() => replacement.started.length === 2)
  163. expect(replacement.started).toEqual(['1', '2'])
  164. replacement.release('2')
  165. await waitForIdle(ctx, agent)
  166. })
  167. it('stops replenishing when a result observer makes the next call exclusive', async () => {
  168. const adapter = new MockAdapter([
  169. multiCall([
  170. { id: 'c1', name: 'x', args: { id: '1' } },
  171. { id: 'c2', name: 'x', args: { id: '2' } },
  172. { id: 'c3', name: 'x', args: { id: '3' } },
  173. ]),
  174. textResponse('done'),
  175. ])
  176. const ctx = await harness(adapter, 2)
  177. const initial = gatedParallelTool('x')
  178. const replacement = gatedExclusiveTool('x')
  179. const disposeInitial = ctx.tools.register(initial.tool)
  180. ctx.on('tools/result', (exec) => {
  181. if (exec.callId !== CallId('c1')) return
  182. disposeInitial()
  183. ctx.tools.register(replacement.tool)
  184. })
  185. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  186. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  187. await until(() => initial.started.length === 2)
  188. initial.release('1')
  189. await until(() => events(agent).some(event =>
  190. event.type === 'tool/result' && event.data.message.source.callId === CallId('c1')))
  191. await new Promise(r => setTimeout(r, 5))
  192. expect(replacement.started).toEqual([])
  193. initial.release('2')
  194. await until(() => replacement.started.length === 1)
  195. expect(replacement.started).toEqual(['3'])
  196. replacement.release('3')
  197. await waitForIdle(ctx, agent)
  198. })
  199. })
  200. describe('tool-call scheduler: model-order results despite out-of-order settlement', () => {
  201. it('commits tool/result in model order even when a later call settles first', async () => {
  202. const adapter = new MockAdapter([
  203. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }]),
  204. textResponse('done'),
  205. ])
  206. const ctx = await harness(adapter)
  207. const gated = gatedParallelTool('p')
  208. ctx.tools.register(gated.tool)
  209. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  210. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  211. await until(() => gated.started.length === 2)
  212. gated.release('2')
  213. await new Promise(r => setTimeout(r, 5))
  214. const beforeFirst = events(agent).filter(e => e.type === 'tool/result')
  215. expect(beforeFirst).toEqual([])
  216. gated.release('1')
  217. await waitForIdle(ctx, agent)
  218. const results = events(agent).filter(e => e.type === 'tool/result')
  219. expect(results.map(e => e.data.message.source.callId)).toEqual([CallId('c1'), CallId('c2')])
  220. })
  221. it('derived history pairs calls in model order regardless of tool/call log interleaving', async () => {
  222. const adapter = new MockAdapter([
  223. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }]),
  224. textResponse('done'),
  225. ])
  226. const ctx = await harness(adapter)
  227. const gated = gatedParallelTool('p')
  228. ctx.tools.register(gated.tool)
  229. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  230. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  231. await until(() => gated.started.length === 2)
  232. gated.release('2'); gated.release('1')
  233. await waitForIdle(ctx, agent)
  234. const messages = agent.session.deriveMessages()
  235. const toolResults = messages.flatMap(m => m.content.filter(b => b.type === 'tool-result'))
  236. expect(toolResults.map(b => b.toolCallId)).toEqual([CallId('c1'), CallId('c2')])
  237. })
  238. })
  239. describe('tool-call scheduler: rolling pool honors maxParallelToolCalls', () => {
  240. it('rejects invalid global maxParallelToolCalls config at plugin load', async () => {
  241. await expect(harness(new MockAdapter([]), 0)).rejects.toThrow()
  242. await expect(harness(new MockAdapter([]), 1.5)).rejects.toThrow()
  243. })
  244. it('defensively rejects invalid caps when direct construction bypasses the config schema', () => {
  245. expect(() => new AgentLoop(new Context(), { agents: [], maxParallelToolCalls: 0 }))
  246. .toThrow('maxParallelToolCalls must be a positive integer')
  247. expect(() => new AgentLoop(new Context(), { agents: [], maxParallelToolCalls: 1.5 }))
  248. .toThrow('maxParallelToolCalls must be a positive integer')
  249. })
  250. it('defaults the cap when direct construction bypasses the config schema', async () => {
  251. const ctx = new Context()
  252. await ctx.plugin(LlmService)
  253. await ctx.plugin(SessionStore)
  254. await ctx.plugin(SystemPrompt, { persona: '' })
  255. await ctx.plugin(ToolRegistry)
  256. await ctx.plugin(AgentRegistry)
  257. const loop = new AgentLoop(ctx, { agents: [] })
  258. expect(loop.config.maxParallelToolCalls).toBe(DEFAULT_MAX_PARALLEL_TOOL_CALLS)
  259. await ctx.fiber.dispose()
  260. })
  261. it('starts at most the cap, replenishing as calls settle', async () => {
  262. const adapter = new MockAdapter([
  263. multiCall([1, 2, 3, 4].map(n => ({ id: `c${n}`, name: 'p', args: { id: String(n) } }))),
  264. textResponse('done'),
  265. ])
  266. const ctx = await harness(adapter, 2)
  267. const gated = gatedParallelTool('p')
  268. ctx.tools.register(gated.tool)
  269. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  270. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  271. await until(() => gated.started.length === 2)
  272. await new Promise(r => setTimeout(r, 5))
  273. expect(gated.started).toEqual(['1', '2'])
  274. gated.release('1')
  275. await until(() => gated.started.length === 3)
  276. expect(gated.started).toEqual(['1', '2', '3'])
  277. expect(events(agent)
  278. .filter(e => e.type === 'tool/call' || e.type === 'tool/result')
  279. .map(e => e.type === 'tool/call'
  280. ? `${e.type}:${String(e.data.callId)}`
  281. : `${e.type}:${String(e.data.message.source.callId)}`)
  282. .slice(0, 4))
  283. .toEqual(['tool/call:c1', 'tool/call:c2', 'tool/result:c1', 'tool/call:c3'])
  284. gated.release('2'); gated.release('3')
  285. await until(() => gated.started.length === 4)
  286. gated.release('4')
  287. await waitForIdle(ctx, agent)
  288. expect(events(agent).filter(e => e.type === 'tool/result').map(e => e.data.message.source.callId))
  289. .toEqual([CallId('c1'), CallId('c2'), CallId('c3'), CallId('c4')])
  290. })
  291. it('maxParallelToolCalls: 1 is fully serial (no second start before the first settles)', async () => {
  292. const adapter = new MockAdapter([
  293. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }]),
  294. textResponse('done'),
  295. ])
  296. const ctx = await harness(adapter, 1)
  297. const gated = gatedParallelTool('p')
  298. ctx.tools.register(gated.tool)
  299. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  300. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  301. await until(() => gated.started.length === 1)
  302. await new Promise(r => setTimeout(r, 5))
  303. expect(gated.started).toEqual(['1'])
  304. gated.release('1')
  305. await until(() => gated.started.length === 2)
  306. gated.release('2')
  307. await waitForIdle(ctx, agent)
  308. })
  309. it('applies the configured cap to every factory-created agent', async () => {
  310. const adapter = new MockAdapter([
  311. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }]),
  312. textResponse('done'),
  313. ])
  314. const ctx = new Context()
  315. await ctx.plugin(LlmService)
  316. await ctx.plugin(SessionStore)
  317. await ctx.plugin(SystemPrompt, { persona: '' })
  318. await ctx.plugin(ToolRegistry)
  319. await ctx.plugin(AgentRegistry)
  320. await ctx.plugin(AgentLoop, { agents: [], maxParallelToolCalls: 1 })
  321. ctx.llm.registerAdapter(['mock'], adapter)
  322. const gated = gatedParallelTool('p')
  323. ctx.tools.register(gated.tool)
  324. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  325. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  326. await until(() => gated.started.length === 1)
  327. await new Promise(r => setTimeout(r, 5))
  328. expect(gated.started).toEqual(['1'])
  329. gated.release('1')
  330. await until(() => gated.started.length === 2)
  331. gated.release('2')
  332. await waitForIdle(ctx, agent)
  333. })
  334. })
  335. describe('tool-call scheduler: ordered middleware and additional contexts', () => {
  336. it('tools/pre-execute and tools/post-execute observe model call order', async () => {
  337. const adapter = new MockAdapter([
  338. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }, { id: 'c3', name: 'p', args: { id: '3' } }]),
  339. textResponse('done'),
  340. ])
  341. const ctx = await harness(adapter)
  342. const gated = gatedParallelTool('p')
  343. ctx.tools.register(gated.tool)
  344. const pre: string[] = []
  345. const post: string[] = []
  346. ctx.on('tools/pre-execute', async (exec, next): Promise<PreToolDecision> => { pre.push(String(exec.callId)); return next() })
  347. ctx.on('tools/post-execute', async (exec, _result, next): Promise<PostToolDecision> => { post.push(String(exec.callId)); return next() })
  348. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  349. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  350. await until(() => gated.started.length === 3)
  351. gated.release('3'); gated.release('2'); gated.release('1')
  352. await waitForIdle(ctx, agent)
  353. expect(pre).toEqual([CallId('c1'), CallId('c2'), CallId('c3')].map(String))
  354. expect(post).toEqual([CallId('c1'), CallId('c2'), CallId('c3')].map(String))
  355. })
  356. it('injects additional contexts in model call order, not settlement order', async () => {
  357. const adapter = new MockAdapter([
  358. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }]),
  359. textResponse('done'),
  360. ])
  361. const ctx = await harness(adapter, 2)
  362. const gated = gatedParallelTool('p')
  363. ctx.tools.register(gated.tool)
  364. ctx.on('tools/post-execute', async (exec, _result): Promise<PostToolDecision> =>
  365. ({ kind: 'accept', additionalContexts: [createUserMessage({
  366. content: [{ type: 'text', text: `ctx-${exec.callId}` }], source: { kind: 'plugin', plugin: 'p' },
  367. })] }))
  368. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  369. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  370. await until(() => gated.started.length === 2)
  371. gated.release('2'); gated.release('1')
  372. await waitForIdle(ctx, agent)
  373. const log = events(agent)
  374. const contextTexts = log.filter(e => e.type === 'user/message' && e.data.source.kind === 'plugin')
  375. .map(e => ((e.data as { content: { text: string }[] }).content[0]!).text)
  376. expect(contextTexts).toEqual(['ctx-c1', 'ctx-c2'])
  377. const lastResult = log.findLastIndex(e => e.type === 'tool/result')
  378. const firstContext = log.findIndex(e => e.type === 'user/message' && e.data.source.kind === 'plugin')
  379. expect(lastResult).toBeLessThan(firstContext)
  380. })
  381. it('orders pre-execute denials and errors without dispatching them', async () => {
  382. const adapter = new MockAdapter([
  383. multiCall([
  384. { id: 'c1', name: 'p', args: { id: '1' } },
  385. { id: 'c2', name: 'p', args: { id: '2' } },
  386. { id: 'c3', name: 'p', args: { id: '3' } },
  387. ]),
  388. textResponse('done'),
  389. ])
  390. const ctx = await harness(adapter)
  391. const gated = gatedParallelTool('p')
  392. ctx.tools.register(gated.tool)
  393. const post: string[] = []
  394. ctx.on('tools/pre-execute', async (exec, next): Promise<PreToolDecision> => {
  395. if (exec.callId === CallId('c2')) return { kind: 'deny', reason: 'blocked by policy' }
  396. if (exec.callId === CallId('c3')) throw new Error('pre exploded')
  397. return next()
  398. })
  399. ctx.on('tools/post-execute', async (exec, _result, next): Promise<PostToolDecision> => {
  400. post.push(String(exec.callId))
  401. return next()
  402. })
  403. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  404. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  405. await until(() => gated.started.length === 1)
  406. gated.release('1')
  407. await waitForIdle(ctx, agent)
  408. expect(gated.started).toEqual(['1'])
  409. expect(post).toEqual(['c1', 'c2'])
  410. const results = events(agent).filter(e => e.type === 'tool/result')
  411. expect(results.map(e => e.data.message.source.callId)).toEqual([CallId('c1'), CallId('c2'), CallId('c3')])
  412. expect((results[1]!.data.message.content[0].content[0] as { text: string }).text).toContain('blocked by policy')
  413. expect((results[2]!.data.message.content[0].content[0] as { text: string }).text).toContain('pre exploded')
  414. })
  415. })
  416. describe('tool-call scheduler: abort handling', () => {
  417. it('starts no calls when the signal is already aborted before a parallel group', async () => {
  418. const adapter = new MockAdapter([
  419. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }]),
  420. textResponse('should never be requested'),
  421. ])
  422. const ctx = await harness(adapter)
  423. const gated = gatedParallelTool('p')
  424. ctx.tools.register(gated.tool)
  425. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  426. ctx.on('session/event', (session, event) => {
  427. if (session === agent.session && event.type === 'assistant/message') {
  428. agent.cancel({ kind: 'user' })
  429. }
  430. })
  431. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  432. await waitForIdle(ctx, agent)
  433. expect(gated.started).toEqual([])
  434. expect(events(agent).filter(e => e.type === 'tool/call').map(e => e.data.callId))
  435. .toEqual([CallId('c1'), CallId('c2')])
  436. expect(events(agent).filter(e => e.type === 'tool/result').map(e => ({
  437. callId: e.data.message.source.callId,
  438. isError: e.data.message.content[0].isError,
  439. error: e.data.error,
  440. }))).toEqual([
  441. { callId: CallId('c1'), isError: true, error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  442. { callId: CallId('c2'), isError: true, error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  443. ])
  444. })
  445. it('skips dispatch and stops starting siblings when abort fires during ordered pre-execute', async () => {
  446. const adapter = new MockAdapter([
  447. multiCall([{ id: 'c1', name: 'p', args: { id: '1' } }, { id: 'c2', name: 'p', args: { id: '2' } }]),
  448. textResponse('should never be requested'),
  449. ])
  450. const ctx = await harness(adapter)
  451. const gated = gatedParallelTool('p')
  452. ctx.tools.register(gated.tool)
  453. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  454. ctx.on('tools/pre-execute', async (exec, next): Promise<PreToolDecision> => {
  455. if (exec.callId === CallId('c1')) {
  456. agent.cancel({ kind: 'user' })
  457. }
  458. return next()
  459. })
  460. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  461. await waitForIdle(ctx, agent)
  462. expect(gated.started).toEqual([])
  463. expect(events(agent).filter(e => e.type === 'tool/call').map(e => e.data.callId))
  464. .toEqual([CallId('c1'), CallId('c2')])
  465. expect(events(agent).filter(e => e.type === 'tool/result').map(e => ({
  466. callId: e.data.message.source.callId,
  467. isError: e.data.message.content[0].isError,
  468. error: e.data.error,
  469. }))).toEqual([
  470. { callId: CallId('c1'), isError: true, error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  471. { callId: CallId('c2'), isError: true, error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  472. ])
  473. })
  474. it('stops replenishing after abort, commits started results, and parks accepted additional contexts', async () => {
  475. const adapter = new MockAdapter([
  476. multiCall([1, 2, 3, 4].map(n => ({ id: `c${n}`, name: 'p', args: { id: String(n) } }))),
  477. textResponse('after wake'),
  478. ])
  479. const ctx = await harness(adapter, 2)
  480. const gated = gatedParallelTool('p')
  481. ctx.tools.register(gated.tool)
  482. ctx.on('tools/post-execute', async (exec, _result, next): Promise<PostToolDecision> => ({
  483. ...await next(),
  484. additionalContexts: [createUserMessage({
  485. content: [{ type: 'text', text: `ctx-${exec.callId}` }], source: { kind: 'plugin', plugin: 'p' },
  486. })],
  487. }))
  488. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  489. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  490. await until(() => gated.started.length === 2)
  491. agent.cancel({ kind: 'user' })
  492. gated.release('1')
  493. gated.release('2')
  494. await waitForIdle(ctx, agent)
  495. expect(gated.started).toEqual(['1', '2'])
  496. expect(events(agent).filter(e => e.type === 'tool/call').map(e => e.data.callId))
  497. .toEqual([CallId('c1'), CallId('c2'), CallId('c3'), CallId('c4')])
  498. expect(events(agent).filter(e => e.type === 'tool/result').map(e => e.data.message.source.callId))
  499. .toEqual([CallId('c1'), CallId('c2'), CallId('c3'), CallId('c4')])
  500. expect(events(agent).filter(e => e.type === 'tool/result').slice(-2).map(e => ({
  501. callId: e.data.message.source.callId,
  502. isError: e.data.message.content[0].isError,
  503. error: e.data.error,
  504. })))
  505. .toEqual([
  506. {
  507. callId: CallId('c3'),
  508. isError: true,
  509. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  510. },
  511. {
  512. callId: CallId('c4'),
  513. isError: true,
  514. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  515. },
  516. ])
  517. const settled = events(agent).filter(e => e.type === 'tool/result'
  518. || (e.type === 'user/message' && e.data.source.kind === 'plugin'))
  519. expect(settled.map(e => e.type))
  520. .toEqual(['tool/result', 'tool/result', 'tool/result', 'tool/result'])
  521. expect(agent.inbox.nextStep.map(message => message.content[0]))
  522. .toEqual([
  523. { type: 'text', text: 'ctx-c1' },
  524. { type: 'text', text: 'ctx-c2' },
  525. ])
  526. const idle = waitForIdle(ctx, agent)
  527. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'wake' }], source: { kind: 'user' } }))
  528. await idle
  529. expect(events(agent).flatMap(e =>
  530. e.type === 'user/message'
  531. && e.data.source.kind === 'plugin'
  532. && e.data.content[0]?.type === 'text'
  533. ? [e.data.content[0].text]
  534. : []))
  535. .toEqual(['ctx-c1', 'ctx-c2'])
  536. })
  537. it('does not run an exclusive barrier after a parallel group aborts', async () => {
  538. const adapter = new MockAdapter([
  539. multiCall([
  540. { id: 'c1', name: 'p', args: { id: '1' } },
  541. { id: 'c2', name: 'p', args: { id: '2' } },
  542. { id: 'c3', name: 'x', args: { id: '3' } },
  543. ]),
  544. textResponse('should never be requested'),
  545. ])
  546. const ctx = await harness(adapter, 2)
  547. const gated = gatedParallelTool('p')
  548. const exclusive: string[] = []
  549. ctx.tools.register(gated.tool)
  550. ctx.tools.register(defineContentToolFixture({
  551. name: 'x',
  552. description: 'exclusive',
  553. parameters: { id: { type: 'string', required: true } },
  554. async execute(args) { exclusive.push(args.id); return [{ type: 'text', text: 'x' }] },
  555. }))
  556. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  557. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  558. await until(() => gated.started.length === 2)
  559. agent.cancel({ kind: 'user' })
  560. gated.release('1')
  561. gated.release('2')
  562. await waitForIdle(ctx, agent)
  563. expect(exclusive).toEqual([])
  564. expect(events(agent).filter(e => e.type === 'tool/call').map(e => e.data.callId))
  565. .toEqual([CallId('c1'), CallId('c2'), CallId('c3')])
  566. expect(events(agent).filter(e => e.type === 'tool/result').at(-1)?.data)
  567. .toMatchObject({
  568. message: {
  569. source: { kind: 'tool', callId: CallId('c3') },
  570. content: [{ isError: true }],
  571. },
  572. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  573. })
  574. })
  575. })
  576. describe('tool-call scheduler: failure quiescence', () => {
  577. it('stops new dispatches and drains started bodies before surfacing the first failure', async () => {
  578. const adapter = new MockAdapter([
  579. multiCall([
  580. { id: 'c1', name: 'p', args: { id: '1' } },
  581. { id: 'c2', name: 'p', args: { id: '2' } },
  582. { id: 'c3', name: 'p', args: { id: '3' } },
  583. ]),
  584. ])
  585. const ctx = await harness(adapter, 3)
  586. const gated = gatedParallelTool('p')
  587. ctx.tools.register(gated.tool)
  588. // The registry contains expected failures as results; replace its internal
  589. // view only to inject the invariant violation this boundary must contain.
  590. const scheduler = ctx.tools[TOOL_REGISTRY_SCHEDULER]
  591. const prepare = scheduler.prepare.bind(scheduler)
  592. const dispatch = scheduler.dispatch.bind(scheduler)
  593. const prepareGate = Promise.withResolvers<undefined>()
  594. let thirdPrepareEntered = false
  595. scheduler.prepare = async (exec) => {
  596. const prepared = await prepare(exec)
  597. if (exec.callId === CallId('c3')) {
  598. thirdPrepareEntered = true
  599. await prepareGate.promise
  600. }
  601. return prepared
  602. }
  603. const schedulerError = new Error('scheduler exploded')
  604. const drainedError = new Error('sibling failed while draining')
  605. let rejectFirst: ((error: Error) => void) | undefined
  606. scheduler.dispatch = exec => exec.callId === CallId('c1')
  607. ? new Promise((_resolve, reject) => { rejectFirst = reject })
  608. : dispatch(exec).then(() => { throw drainedError })
  609. const agent = ctx.agentLoop.create(SessionId('scheduler-failure'), { provider: 'mock', model: 'mock' })
  610. let idle = false
  611. const idlePromise = waitForIdle(ctx, agent).then(() => { idle = true })
  612. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }))
  613. await until(() => gated.started.includes('2') && thirdPrepareEntered && rejectFirst !== undefined)
  614. rejectFirst?.(schedulerError)
  615. await new Promise<void>(resolve => setImmediate(resolve))
  616. prepareGate.resolve(undefined)
  617. await new Promise<void>(resolve => setImmediate(resolve))
  618. const startedBeforeDrain = [...gated.started]
  619. const idleBeforeDrain = idle
  620. const turnEndBeforeDrain = events(agent).find(event => event.type === 'turn/end')
  621. for (const id of gated.pending()) gated.release(id)
  622. await idlePromise
  623. expect(startedBeforeDrain).toEqual(['2'])
  624. expect(idleBeforeDrain).toBe(false)
  625. expect(turnEndBeforeDrain).toBeUndefined()
  626. expect(gated.pending()).toEqual([])
  627. expect(events(agent).findLast(event => event.type === 'turn/end')).toMatchObject({
  628. data: { reason: { kind: 'error', error: { message: schedulerError.message, code: 'UNKNOWN' } } },
  629. })
  630. })
  631. })