1
0

integration.spec.ts 4.6 KB

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