subagent-fork.spec.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import Loader from '@cordisjs/plugin-loader'
  4. import LlmService from '@deepseek-ai/dsh-llm'
  5. import SessionStore from '@deepseek-ai/dsh-session'
  6. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  7. import ToolRegistry from '@deepseek-ai/dsh-tools'
  8. import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent'
  9. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  10. import * as Invariants from '@deepseek-ai/dsh-invariants'
  11. import SubagentService from '@deepseek-ai/dsh-subagent'
  12. import { MockAdapter, textResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  13. import type { StreamChunk } from '@deepseek-ai/dsh-llm'
  14. import * as fork from '../src/index.ts'
  15. import { completedTurnPrefix } from '../src/index.ts'
  16. type Script = ConstructorParameters<typeof MockAdapter>[0]
  17. /** A bare `stop` finish that streams no content → the turn ends `completed`
  18. * with NO `assistant/message` of its own. */
  19. const emptyStop: StreamChunk[] = [{ type: 'finish', reason: { kind: 'stop' } }]
  20. /**
  21. * Drives the REAL fork backend with a real loop + scripted mock MODEL + the
  22. * real dsh-invariants plugin. The invariants plugin re-replays a seeded child
  23. * log on `session/created` (its freeze-check), so a malformed (unbalanced) fork
  24. * seed makes these tests THROW — that is the regression guard for the
  25. * completed-turn-prefix boundary.
  26. */
  27. async function setup(script: Script) {
  28. const ctx = new Context()
  29. await ctx.plugin(LlmService)
  30. await ctx.plugin(SessionStore)
  31. await ctx.plugin(SystemPrompt)
  32. await ctx.plugin(ToolRegistry)
  33. await ctx.plugin(AgentRegistry)
  34. await ctx.plugin(Invariants)
  35. await ctx.plugin(AgentLoop, { agents: [] })
  36. await ctx.plugin(SubagentService)
  37. await ctx.plugin(fork, { providerName: 'fork' })
  38. ctx.llm.registerAdapter(['mock'], new MockAdapter(script))
  39. const parent = ctx.agentLoop.create(AgentId('parent'), { model: 'mock' })
  40. return { ctx, parent }
  41. }
  42. function text(blocks: { type: string; text?: string }[]): string {
  43. return blocks.filter(b => b.type === 'text').map(b => b.text).join('')
  44. }
  45. describe('completedTurnPrefix', () => {
  46. it('returns an empty prefix for a parent that has never completed a turn', async () => {
  47. const { parent } = await setup([])
  48. expect(completedTurnPrefix(parent)).toEqual([])
  49. })
  50. it('returns the balanced prefix up to and including the last turn/end', async () => {
  51. const { parent } = await setup([textResponse('first'), textResponse('second')])
  52. parent.send([{ type: 'text', text: 'q1' }])
  53. await parent.whenIdle()
  54. parent.send([{ type: 'text', text: 'q2' }])
  55. await parent.whenIdle()
  56. const prefix = completedTurnPrefix(parent)
  57. // Ends exactly at the last turn/end; seq is contiguous from 0.
  58. expect(prefix.at(-1)?.type).toBe('turn/end')
  59. expect(prefix.map(e => e.seq)).toEqual(prefix.map((_, i) => i))
  60. // Both completed turns are present.
  61. expect(prefix.filter(e => e.type === 'turn/end')).toHaveLength(2)
  62. })
  63. })
  64. describe('dsh-subagent-fork', () => {
  65. it('forks an UNSEEDED (fresh) child when the parent has no completed turn', async () => {
  66. // The parent has never completed a turn → empty prefix → the provider omits
  67. // the seed → the child runs fresh. Exercises the `seed.length > 0` false arm.
  68. const { ctx, parent } = await setup([textResponse('fresh child')])
  69. expect(completedTurnPrefix(parent)).toEqual([])
  70. const run = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'child q' }], parent })
  71. const result = await run.result
  72. expect(result.stopReason).toBe('completed')
  73. expect(text(result.output)).toBe('fresh child')
  74. const child = ctx.agents.get(run.id)!
  75. // Only the child's own turn — no seeded parent turns.
  76. expect(child.session.events.filter(e => e.type === 'turn/end')).toHaveLength(1)
  77. await run.dispose()
  78. })
  79. it('seeds the child with the parent\'s completed-turn prefix (child inherits context)', async () => {
  80. // Parent runs one turn, then we fork. The child's seeded log should contain
  81. // the parent's first turn, and the child should run its own new turn on top.
  82. const { ctx, parent } = await setup([textResponse('parent answer'), textResponse('child answer')])
  83. parent.send([{ type: 'text', text: 'parent question' }])
  84. await parent.whenIdle()
  85. const parentPrefixLen = parent.session.events.length
  86. const run = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'child question' }], parent })
  87. const result = await run.result
  88. expect(result.stopReason).toBe('completed')
  89. expect(text(result.output)).toBe('child answer')
  90. const child = ctx.agents.get(run.id)!
  91. // The child's log STARTS with the parent's prefix (seeded), then its own turn.
  92. expect(child.session.events.length).toBeGreaterThan(parentPrefixLen)
  93. // The seeded prefix carried the parent's user message.
  94. const seededUser = child.session.events.slice(0, parentPrefixLen).find(e => e.type === 'user/message')
  95. expect(seededUser).toBeDefined()
  96. // Lineage stamped.
  97. expect(child.session.header.parentSession).toBe(parent.session.header.id)
  98. // The seed boundary is recorded on the header (= the seeded prefix length),
  99. // so a reload / replay harness can tell the inherited prefix from the
  100. // child's own events.
  101. expect(child.session.header.seedLength).toBe(parentPrefixLen)
  102. await run.dispose()
  103. })
  104. it('produces an invariant-CLEAN seed: forking mid-turn excludes the open turn', async () => {
  105. // Drive the parent so it has ONE completed turn, then start a SECOND turn
  106. // that is still open (a hanging model call), and fork while it's in flight.
  107. // The fork must seed only the completed first turn — an unbalanced seed
  108. // would make the invariants replay throw inside ctx.subagents.start.
  109. const { ctx, parent } = await setup([textResponse('done'), 'hang', textResponse('child')])
  110. parent.send([{ type: 'text', text: 'q1' }])
  111. await parent.whenIdle()
  112. // Start a second turn that hangs (open turn/start + open step, never ends).
  113. parent.send([{ type: 'text', text: 'q2' }])
  114. await new Promise(r => setTimeout(r, 20)) // let the hanging turn open
  115. // Forking now must NOT throw (the open second turn is excluded from the seed).
  116. const run = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'child q' }], parent })
  117. const result = await run.result
  118. expect(result.stopReason).toBe('completed')
  119. expect(text(result.output)).toBe('child')
  120. const child = ctx.agents.get(run.id)!
  121. // The child's seed has exactly the ONE completed parent turn (the open one excluded).
  122. const seedTurnEnds = child.session.events.filter(e => e.type === 'turn/end')
  123. // 1 from the seeded parent turn + 1 from the child's own completed turn.
  124. expect(seedTurnEnds.length).toBe(2)
  125. parent.cancel()
  126. await run.dispose()
  127. })
  128. it('does NOT return the seeded parent output when the child produces no message of its own', async () => {
  129. // Regression: readResult must scope to the child's OWN events (after the
  130. // seed). The parent completes a turn with a distinctive assistant message,
  131. // then the fork child's own turn finishes with a bare `stop` and NO
  132. // assistant/message. Scanning the whole (seeded) log would return the
  133. // parent's "parent stale" message with stopReason 'completed'; scoped to the
  134. // child's own events the output is empty.
  135. const { ctx, parent } = await setup([textResponse('parent stale'), emptyStop])
  136. parent.send([{ type: 'text', text: 'parent question' }])
  137. await parent.whenIdle()
  138. const run = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'child question' }], parent })
  139. const result = await run.result
  140. // The child completed its own (empty) turn — completed, but with NO output
  141. // borrowed from the seeded parent prefix.
  142. expect(result.stopReason).toBe('completed')
  143. expect(result.output).toEqual([])
  144. await run.dispose()
  145. })
  146. it('advertises depthLimit but not outputSchema/toolFilter', async () => {
  147. const { ctx } = await setup([])
  148. expect(ctx.subagents.getProvider('fork')!.capabilities).toEqual({ outputSchema: false, depthLimit: true, toolFilter: false })
  149. })
  150. it('unregisters the provider when its fiber is disposed (HMR safety)', async () => {
  151. const ctx = new Context()
  152. await ctx.plugin(SubagentService)
  153. await ctx.plugin(AgentRegistry)
  154. const fiber = await ctx.plugin(fork, { providerName: 'fork' })
  155. expect(ctx.subagents.list()).toEqual(['fork'])
  156. await fiber.dispose()
  157. expect(ctx.subagents.list()).toEqual([])
  158. })
  159. it('has the namespace-plugin export shape (no stray default)', () => {
  160. expect('default' in fork).toBe(false)
  161. expect(fork.name).toBe('subagent-fork')
  162. expect(fork.inject).toEqual(['subagents', 'agents'])
  163. const loader = Object.create(Loader.prototype) as Loader
  164. const unwrapped = loader.unwrapExports(fork) as Record<string, unknown>
  165. expect(unwrapped).toBe(fork)
  166. expect(unwrapped.name).toBe('subagent-fork')
  167. expect(unwrapped.inject).toEqual(['subagents', 'agents'])
  168. expect(typeof unwrapped.apply).toBe('function')
  169. })
  170. })