integration.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 SubagentRuntime from '@deepseek-ai/dsh-subagent'
  11. import * as spawn from '@deepseek-ai/dsh-subagent-spawn-in-process'
  12. import { STRUCTURED_OUTPUT_TOOL } from '@deepseek-ai/dsh-subagent-in-process-driver'
  13. import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  14. import PtcWorkflowEngine from '../src/index.ts'
  15. import { mountPtcRuntime } from './setup.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. async function setup(script: Script) {
  24. const ctx = new Context()
  25. const adapter = new MockAdapter(script)
  26. await mountAgentLoopTestDependencies(ctx)
  27. await mountPtcRuntime(ctx)
  28. await mountInvariants(ctx)
  29. await ctx.plugin(AgentLoop, { agents: [] })
  30. await ctx.plugin(SubagentRuntime)
  31. await ctx.plugin(spawn, { providerName: 'spawn' })
  32. await ctx.plugin(PtcWorkflowEngine, {})
  33. ctx.llm.registerAdapter(['mock'], adapter)
  34. const parent = await ctx.agentLoop.create(SessionId('parent'), { provider: 'mock', model: 'mock' })
  35. return { ctx, parent, adapter }
  36. }
  37. describe('dsh-workflow-ptc over the real in-process stack', () => {
  38. it('runs a two-stage workflow: a plain child, then a schema child through the structured runtime', async () => {
  39. const { ctx, parent } = await setup([
  40. textResponse('the file list is a.ts'),
  41. toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { verdict: 'real', confidence: 0.9 }),
  42. ])
  43. const childIds: string[] = []
  44. ctx.on('workflow/agent-start', (_info, agent) => {
  45. // The workflow bridge must await asynchronous provider start: an observer
  46. // sees the real spawn child already published, never a reserved id.
  47. expect(ctx.agents.get(agent.childId)).toBeDefined()
  48. childIds.push(agent.childId)
  49. })
  50. const run = ctx.workflowEngine.start({
  51. meta: { name: 'integration', description: 'plain + structured children' },
  52. script: `phase('Read')
  53. const prose = await agent('read the repo')
  54. phase('Judge')
  55. const judged = await agent('judge: ' + prose, {
  56. schema: { type: 'object', properties: { verdict: { type: 'string', enum: ['real', 'bogus'] }, confidence: { type: 'number' } }, required: ['verdict'] },
  57. })
  58. return { prose, verdict: judged.verdict, confidence: judged.confidence }`,
  59. parent,
  60. })
  61. const result = await run.result
  62. expect(result.stopReason, result.error?.split('\n')[0]).toBe('completed')
  63. expect(result.value).toEqual({ prose: 'the file list is a.ts', verdict: 'real', confidence: 0.9 })
  64. expect(result.agentsStarted).toBe(2)
  65. await run.dispose()
  66. // Both children were disposed to quiescence — no live child agents remain.
  67. expect(childIds.length).toBe(2)
  68. for (const childId of childIds) {
  69. expect(ctx.agents.get(SessionId(childId))).toBeUndefined()
  70. }
  71. })
  72. it('a child that fails against its schema (nudges exhausted) reaches the script as null', async () => {
  73. const { ctx, parent } = await setup([
  74. textResponse('prose only'),
  75. textResponse('still prose after the nudge'),
  76. ])
  77. const run = ctx.workflowEngine.start({
  78. meta: { name: 'null-path', description: 'schema failure maps to null' },
  79. script: `const judged = await agent('judge it', { schema: { type: 'object', properties: { v: { type: 'string' } } } })
  80. return { got: judged === null ? 'null' : 'value' }`,
  81. parent,
  82. })
  83. const result = await run.result
  84. expect(result.stopReason, result.error?.split('\n')[0]).toBe('completed')
  85. expect(result.value).toEqual({ got: 'null' })
  86. await run.dispose()
  87. })
  88. it('keeps real children visible to start observers after a progress burst', async () => {
  89. const { ctx, parent } = await setup([textResponse('child complete')])
  90. const logs: string[] = []
  91. const visible: boolean[] = []
  92. ctx.on('workflow/log', (_info, message) => { logs.push(message) })
  93. ctx.on('workflow/agent-start', (_info, child) => { visible.push(ctx.agents.get(child.childId) !== undefined) })
  94. const run = ctx.workflowEngine.start({
  95. meta: { name: 'progress-burst', description: 'ordered progress and child visibility' },
  96. script: 'for (let index = 0; index < 200; index++) log(String(index)); return await agent("finish")',
  97. parent,
  98. })
  99. try {
  100. await expect(run.result).resolves.toMatchObject({ value: 'child complete', stopReason: 'completed', agentsStarted: 1 })
  101. expect(logs).toEqual(Array.from({ length: 200 }, (_, index) => String(index)))
  102. expect(visible).toEqual([true])
  103. } finally {
  104. await run.dispose()
  105. }
  106. })
  107. })