multi-subagent.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { SessionId } from '@deepseek-ai/dsh-session'
  4. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  5. import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
  6. import * as Invariants from '@deepseek-ai/dsh-invariants'
  7. import SubagentService, { type SubagentStartRequest } from '@deepseek-ai/dsh-subagent'
  8. import * as Spawn from '@deepseek-ai/dsh-subagent-spawn'
  9. import { MockAdapter, textResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  10. import * as fork from '../src/index.ts'
  11. type Script = ConstructorParameters<typeof MockAdapter>[0]
  12. function start(ctx: Context, provider: string, request: Omit<SubagentStartRequest, 'signal'> & { signal?: AbortSignal }) {
  13. return ctx.subagents.start(provider, { signal: request.signal ?? new AbortController().signal, ...request })
  14. }
  15. /**
  16. * The two in-process backends coexist on one context: the SAME parent agent
  17. * delegates to a `spawn` child (fresh) and a `fork` child (seeded with its log),
  18. * and keeps working itself. This is the multi-provider coexistence the seam
  19. * exists for — the named registry lets one runtime hold both transports.
  20. */
  21. async function setup(script: Script) {
  22. const ctx = new Context()
  23. await mountAgentLoopTestDependencies(ctx)
  24. await ctx.plugin(Invariants)
  25. await ctx.plugin(AgentLoop, { agents: [] })
  26. await ctx.plugin(SubagentService)
  27. await ctx.plugin(Spawn, { providerName: 'spawn' })
  28. await ctx.plugin(fork, { providerName: 'fork' })
  29. ctx.llm.registerAdapter(['mock'], new MockAdapter(script))
  30. const parent = ctx.agentLoop.create(SessionId('parent'), { provider: 'mock', model: 'mock' })
  31. return { ctx, parent }
  32. }
  33. function text(blocks: { type: string; text?: string }[]): string {
  34. return blocks.filter(b => b.type === 'text').map(b => b.text).join('')
  35. }
  36. describe('multi-subagent coexistence (spawn + fork on one context)', () => {
  37. it('both providers register and coexist', async () => {
  38. const { ctx } = await setup([])
  39. expect(ctx.subagents.list().sort()).toEqual(['fork', 'spawn'])
  40. })
  41. it('the same parent drives a spawn child AND a fork child, then keeps working', async () => {
  42. // Script order: parent turn 1, spawn child, fork child, parent turn 2.
  43. const { ctx, parent } = await setup([
  44. textResponse('parent turn one'),
  45. textResponse('spawn child reply'),
  46. textResponse('fork child reply'),
  47. textResponse('parent turn two'),
  48. ])
  49. // Parent does one real turn first, so the fork has a completed turn to seed.
  50. parent.send([{ type: 'text', text: 'parent q1' }])
  51. await parent.whenIdle()
  52. const parentPrefixLen = parent.session.events.length
  53. // Delegate to a fresh spawn child.
  54. const spawnRun = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'spawn task' }], parent })
  55. const spawnResult = await spawnRun.result
  56. expect(spawnResult.stopReason).toBe('completed')
  57. expect(text(spawnResult.output)).toBe('spawn child reply')
  58. // Delegate to a fork child (seeded with the parent's turn-1 prefix).
  59. const forkRun = await start(ctx, 'fork', { prompt: [{ type: 'text', text: 'fork task' }], parent })
  60. const forkResult = await forkRun.result
  61. expect(forkResult.stopReason).toBe('completed')
  62. expect(text(forkResult.output)).toBe('fork child reply')
  63. // The two children are distinct sessions, both lineage-stamped to the parent.
  64. const spawnChild = ctx.agents.get(spawnRun.id)!
  65. const forkChild = ctx.agents.get(forkRun.id)!
  66. expect(spawnChild.session.header.id).not.toBe(forkChild.session.header.id)
  67. expect(spawnChild.session.header.parentSession).toBe(parent.session.header.id)
  68. expect(forkChild.session.header.parentSession).toBe(parent.session.header.id)
  69. // The fork child inherited the parent's prefix; the spawn child did not.
  70. expect(forkChild.session.events.slice(0, parentPrefixLen).some(e => e.type === 'user/message')).toBe(true)
  71. await spawnRun.dispose()
  72. await forkRun.dispose()
  73. // The parent is unaffected and keeps working after both delegations.
  74. parent.send([{ type: 'text', text: 'parent q2' }])
  75. await parent.whenIdle()
  76. const lastParentMessage = parent.session.events.findLast(e => e.type === 'assistant/message')
  77. expect(lastParentMessage?.type === 'assistant/message' && text(lastParentMessage.data.content)).toBe('parent turn two')
  78. // The parent's OWN log never recorded the children's internal steps — its
  79. // only subagent-related entries would be tool/call+tool/result IF it had
  80. // used the tool, but here we called the service directly, so the parent log
  81. // is purely its own two turns.
  82. expect(parent.session.events.filter(e => e.type === 'turn/end')).toHaveLength(2)
  83. })
  84. })