integration.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from '@deepseek-ai/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 InvariantRegistry from '@deepseek-ai/dsh-invariants'
  7. import * as SessionInvariant from '@deepseek-ai/dsh-session/invariant'
  8. import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant'
  9. import * as AgentLoopInvariant from '@deepseek-ai/dsh-agent-loop/invariant'
  10. import SubagentRuntime from '@deepseek-ai/dsh-subagent'
  11. import * as spawn from '@deepseek-ai/dsh-subagent-spawn-in-process'
  12. import { STRUCTURED_OUTPUT_TOOL } from '@deepseek-ai/dsh-subagent-in-process-driver'
  13. import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  14. import WorkerThreadWorkflowEngine 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. /**
  23. * The whole in-process stack, keyless, with the script in a REAL worker
  24. * thread: the engine drives the REAL spawn backend (with its
  25. * structured runtime) on a real agent loop; the scripted mock MODEL is the
  26. * only mocked boundary. This is the guard the unit suites structurally
  27. * cannot give — the MessageChannel suite fakes the host, and the host suite
  28. * stubs the subagent seam.
  29. */
  30. async function setup(script: Script) {
  31. const ctx = new Context()
  32. const adapter = new MockAdapter(script)
  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(WorkerThreadWorkflowEngine, {})
  39. ctx.llm.registerAdapter(['mock'], adapter)
  40. const parent = await ctx.agentLoop.create(SessionId('parent'), { provider: 'mock', model: 'mock' })
  41. return { ctx, parent, adapter }
  42. }
  43. describe('dsh-workflow-worker-thread over the real in-process stack', () => {
  44. it('runs a two-stage workflow: a plain child, then a schema child through the structured runtime', async () => {
  45. const { ctx, parent } = await setup([
  46. textResponse('the file list is a.ts'),
  47. toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { verdict: 'real', confidence: 0.9 }),
  48. ])
  49. const childIds: string[] = []
  50. ctx.on('workflow/agent-start', (_info, agent) => {
  51. // The workflow bridge must await asynchronous provider start: an observer
  52. // sees the real spawn child already published, never a reserved id.
  53. expect(ctx.agents.get(agent.childId)).toBeDefined()
  54. childIds.push(agent.childId)
  55. })
  56. const run = ctx.workflowEngine.start({
  57. meta: { name: 'integration', description: 'plain + structured children' },
  58. script: `phase('Read')
  59. const prose = await agent('read the repo')
  60. phase('Judge')
  61. const judged = await agent('judge: ' + prose, {
  62. schema: { type: 'object', properties: { verdict: { type: 'string', enum: ['real', 'bogus'] }, confidence: { type: 'number' } }, required: ['verdict'] },
  63. })
  64. return { prose, verdict: judged.verdict, confidence: judged.confidence }`,
  65. parent,
  66. })
  67. const result = await run.result
  68. expect(result.stopReason).toBe('completed')
  69. expect(result.value).toEqual({ prose: 'the file list is a.ts', verdict: 'real', confidence: 0.9 })
  70. expect(result.agentsStarted).toBe(2)
  71. await run.dispose()
  72. // Both children were disposed to quiescence — no live child agents remain.
  73. expect(childIds.length).toBe(2)
  74. for (const childId of childIds) {
  75. expect(ctx.agents.get(SessionId(childId))).toBeUndefined()
  76. }
  77. })
  78. it('a child that fails against its schema (nudges exhausted) reaches the script as null', async () => {
  79. const { ctx, parent } = await setup([
  80. textResponse('prose only'),
  81. textResponse('still prose after the nudge'),
  82. ])
  83. const run = ctx.workflowEngine.start({
  84. meta: { name: 'null-path', description: 'schema failure maps to null' },
  85. script: `const judged = await agent('judge it', { schema: { type: 'object', properties: { v: { type: 'string' } } } })
  86. return { got: judged === null ? 'null' : 'value' }`,
  87. parent,
  88. })
  89. const result = await run.result
  90. expect(result.stopReason).toBe('completed')
  91. expect(result.value).toEqual({ got: 'null' })
  92. await run.dispose()
  93. })
  94. })