integration.spec.ts 4.0 KB

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