subagent-acp.e2e.ts 5.1 KB

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