integration.spec.ts 4.2 KB

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