multi-subagent.spec.ts 4.7 KB

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