multi-subagent.spec.ts 5.4 KB

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