subagent-acp.e2e.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { mkdtemp, readFile, rm } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import { Context } from 'cordis'
  7. import type { Agent } from '@deepseek-ai/dsh-agent'
  8. import SubagentService from '@deepseek-ai/dsh-subagent'
  9. import { resolveExampleLaunch } from '@deepseek-ai/dsh-loader-smoke'
  10. import * as acp from '../src/index.ts'
  11. /**
  12. * With-key cross-process seam proof: the backend spawns the real acp-agent example, speaks ACP over
  13. * stdio, and returns its real model answer. This is the out-of-process counterpart to in-process
  14. * spawn coverage and self-skips without `DEEPSEEK_API_KEY`.
  15. */
  16. // The real acp-agent example: its bin + cordis.yml (the live DeepSeek config).
  17. const binScript = fileURLToPath(new URL('../../../examples/acp-demo/src/bin.ts', import.meta.url))
  18. const exampleConfig = fileURLToPath(new URL('../../../../examples/acp-agent/cordis.yml', import.meta.url))
  19. const repoTsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  20. // How to launch the child acp-agent (src via tsx / lib via plain node, per DSH_EXAMPLE_MODE).
  21. // buildChildEnv scrubs ambient creds but keeps these extras, so the model key is
  22. // forwarded explicitly; TSX_TSCONFIG_PATH is added by the resolver in src mode only.
  23. const childLaunch = resolveExampleLaunch({
  24. srcBin: binScript,
  25. configArgs: ['--config', exampleConfig],
  26. tsconfigPath: repoTsconfig,
  27. env: {
  28. ...process.env.DEEPSEEK_API_KEY !== undefined ? { DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY } : {},
  29. ...process.env.DEEPSEEK_BASE_URL !== undefined ? { DEEPSEEK_BASE_URL: process.env.DEEPSEEK_BASE_URL } : {},
  30. DSH_PERMISSION_MODE: 'danger-full-access',
  31. },
  32. })
  33. /** The ACP backend ignores the parent, but the seam requires one. */
  34. const fakeParent = { id: 'parent', session: { header: {} } } as unknown as Agent
  35. let ctx: Context | undefined
  36. let workdir: string | undefined
  37. afterEach(async () => {
  38. await ctx?.fiber.dispose()
  39. ctx = undefined
  40. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  41. workdir = undefined
  42. })
  43. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('ACP backend with-key e2e (drive our own acp-agent)', () => {
  44. it('drives the real acp-agent example process to answer a prompt', async () => {
  45. workdir = await mkdtemp(join(tmpdir(), 'dsh-subagent-acp-e2e-'))
  46. ctx = new Context()
  47. await ctx.plugin(SubagentService)
  48. await ctx.plugin(acp, {
  49. providerName: 'acp',
  50. command: childLaunch.command,
  51. args: childLaunch.args,
  52. cwd: workdir,
  53. permission: 'reject',
  54. env: childLaunch.env as Record<string, string>,
  55. })
  56. const run = await ctx.subagents.start('acp', {
  57. prompt: [{ type: 'text', text: 'Reply with exactly the word PONG and nothing else. Do not use any tools.' }],
  58. parent: fakeParent,
  59. signal: new AbortController().signal,
  60. })
  61. const result = await run.result
  62. await run.dispose()
  63. // The real child process completed its turn and streamed a real answer back
  64. // across the ACP boundary.
  65. expect(result.stopReason).toBe('completed')
  66. const text = result.output.filter(b => b.type === 'text').map(b => (b as { text: string }).text).join('')
  67. expect(text.length).toBeGreaterThan(0)
  68. expect(text.toUpperCase()).toContain('PONG')
  69. }, 180_000)
  70. it('drives the child to do real file work via its own bash tool', async () => {
  71. workdir = await mkdtemp(join(tmpdir(), 'dsh-subagent-acp-e2e-'))
  72. ctx = new Context()
  73. await ctx.plugin(SubagentService)
  74. await ctx.plugin(acp, {
  75. providerName: 'acp',
  76. command: childLaunch.command,
  77. args: childLaunch.args,
  78. cwd: workdir,
  79. // The child needs to act (run bash), so approve its permission prompts.
  80. permission: 'allow',
  81. env: childLaunch.env as Record<string, string>,
  82. })
  83. const run = await ctx.subagents.start('acp', {
  84. prompt: [{ type: 'text', text:
  85. 'Use the bash tool to write the text ACP_CHILD_WAS_HERE into a file named proof.txt '
  86. + 'in the current directory. Then reply DONE.' }],
  87. parent: fakeParent,
  88. signal: new AbortController().signal,
  89. })
  90. const result = await run.result
  91. await run.dispose()
  92. expect(result.stopReason).toBe('completed')
  93. // Assert the filesystem effect independently of the model response.
  94. const proof = await readFile(join(workdir, 'proof.txt'), 'utf8')
  95. expect(proof).toContain('ACP_CHILD_WAS_HERE')
  96. }, 180_000)
  97. })