integration.spec.ts 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 AgentExecutionProvider from '@deepseek-ai/dsh-agent-execution'
  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 * as spawn from '@deepseek-ai/dsh-subagent-spawn'
  13. import { STRUCTURED_OUTPUT_TOOL } from '@deepseek-ai/dsh-subagent-inprocess'
  14. import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  15. import WorkerWorkflowEngine from '../src/index.ts'
  16. type Script = ConstructorParameters<typeof MockAdapter>[0]
  17. /**
  18. * The whole in-process stack, keyless, with the script in a REAL worker
  19. * thread: the engine drives the REAL spawn backend (with its
  20. * structured runtime) on a real agent loop; the scripted mock MODEL is the
  21. * only mocked boundary. This is the guard the unit suites structurally
  22. * cannot give — the MessageChannel suite fakes the host, and the host suite
  23. * stubs the subagent seam.
  24. */
  25. async function setup(script: Script) {
  26. const ctx = new Context()
  27. const adapter = new MockAdapter(script)
  28. await ctx.plugin(LlmService)
  29. await ctx.plugin(SessionStore)
  30. await ctx.plugin(SystemPrompt)
  31. await ctx.plugin(ToolRegistry)
  32. await ctx.plugin(AgentRegistry)
  33. await ctx.plugin(Invariants)
  34. await ctx.plugin(AgentExecutionProvider)
  35. await ctx.plugin(AgentLoop, { agents: [] })
  36. await ctx.plugin(SubagentService)
  37. await ctx.plugin(spawn, { providerName: 'spawn' })
  38. await ctx.plugin(WorkerWorkflowEngine, {})
  39. ctx.llm.registerAdapter(['mock'], adapter)
  40. const parent = ctx.agentLoop.create(AgentId('parent'), { model: 'mock' })
  41. return { ctx, parent, adapter }
  42. }
  43. describe('dsh-workflow-workerthread 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.workflows.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(AgentId(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.workflows.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. })