integration.spec.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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) => { childIds.push(agent.childId) })
  49. const run = ctx.workflows.start({
  50. meta: { name: 'integration', description: 'plain + structured children' },
  51. script: `phase('Read')
  52. const prose = await agent('read the repo')
  53. phase('Judge')
  54. const judged = await agent('judge: ' + prose, {
  55. schema: { type: 'object', properties: { verdict: { type: 'string', enum: ['real', 'bogus'] }, confidence: { type: 'number' } }, required: ['verdict'] },
  56. })
  57. return { prose, verdict: judged.verdict, confidence: judged.confidence }`,
  58. parent,
  59. })
  60. const result = await run.result
  61. expect(result.stopReason).toBe('completed')
  62. expect(result.value).toEqual({ prose: 'the file list is a.ts', verdict: 'real', confidence: 0.9 })
  63. expect(result.agentsStarted).toBe(2)
  64. await run.dispose()
  65. // Both children were disposed to quiescence — no live child agents remain.
  66. expect(childIds.length).toBe(2)
  67. for (const childId of childIds) {
  68. expect(ctx.agents.get(AgentId(childId))).toBeUndefined()
  69. }
  70. })
  71. it('a child that fails against its schema (nudges exhausted) reaches the script as null', async () => {
  72. const { ctx, parent } = await setup([
  73. textResponse('prose only'),
  74. textResponse('still prose after the nudge'),
  75. ])
  76. const run = ctx.workflows.start({
  77. meta: { name: 'null-path', description: 'schema failure maps to null' },
  78. script: `const judged = await agent('judge it', { schema: { type: 'object', properties: { v: { type: 'string' } } } })
  79. return { got: judged === null ? 'null' : 'value' }`,
  80. parent,
  81. })
  82. const result = await run.result
  83. expect(result.stopReason).toBe('completed')
  84. expect(result.value).toEqual({ got: 'null' })
  85. await run.dispose()
  86. })
  87. })