code-mode.e2e.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { mkdtemp, readFile, rm } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { afterEach, describe, expect, it } from 'vitest'
  5. import { Context } from 'cordis'
  6. import LlmService from '@deepseek-ai/dsh-llm'
  7. import SessionStore from '@deepseek-ai/dsh-session'
  8. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  9. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  10. import ToolRegistry, { RUN_CODE_NAME } from '@deepseek-ai/dsh-tools'
  11. import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent'
  12. import AgentLoop, { type ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop'
  13. import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
  14. import * as ToolBash from '@deepseek-ai/dsh-tool-bash'
  15. import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
  16. import { WorkerCodeRuntime } from '@deepseek-ai/dsh-code-runtime-worker'
  17. /**
  18. * The Code Mode with-key proof (the RFC's e2e tier): a REAL model under
  19. * `mode: 'code'`, a task that requires composing two tool calls, verified
  20. * against the WORLD — the persisted request header carried exactly
  21. * `[run_code]` as the wire tool list, each sub-call landed as a
  22. * `tool/code-dispatch` event, the file the program wrote exists on disk, and
  23. * the final answer is the program's curated output. Key-gated (see
  24. * vitest.e2e.config.ts); the keyless Loader-path smoke of the overlay lives
  25. * in `code-mode-keyless-smoke.e2e.ts`.
  26. */
  27. const PERSONA = 'You are coding-agent. You work by writing TypeScript programs for run_code: '
  28. + 'batch related tool work into one program and print or return ONLY the findings that matter.'
  29. let ctx: Context | undefined
  30. let workdir: string | undefined
  31. afterEach(async () => {
  32. // Always dispose, even on failure/retry/timeout: agent-loop teardown stops
  33. // the loop, the executor kills stray processes, and the code runtime's
  34. // dispose awaits worker exits.
  35. await ctx?.fiber.dispose()
  36. ctx = undefined
  37. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  38. workdir = undefined
  39. })
  40. async function codeModeHarness(cwd: string): Promise<Context> {
  41. const harness = new Context()
  42. await harness.plugin(LlmService)
  43. await harness.plugin(SessionStore)
  44. await harness.plugin(SystemPrompt, { persona: PERSONA })
  45. await harness.plugin(ToolRegistry, { mode: 'code' })
  46. await harness.plugin(AgentRegistry)
  47. await harness.plugin(AgentLoop, { agents: [] })
  48. await harness.plugin(LlmDeepSeek, { models: ['deepseek-v4-flash'] })
  49. await harness.plugin(LocalBashExecutor, { cwd, timeoutMs: 30_000 })
  50. await harness.plugin(ToolBash)
  51. await harness.plugin(WorkerCodeRuntime, {})
  52. return harness
  53. }
  54. function waitForIdle(harness: Context, agent: ReactLoopAgent): Promise<void> {
  55. return new Promise((resolve) => {
  56. const dispose = harness.on('agent/status', (subject, status) => {
  57. if (subject === agent && status === 'idle') {
  58. dispose()
  59. resolve()
  60. }
  61. })
  62. })
  63. }
  64. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('Code Mode: real model writes a program over real tools', () => {
  65. it('collapses the wire tool list to [run_code], bridges sub-calls, and returns curated output', async () => {
  66. workdir = await mkdtemp(join(tmpdir(), 'dsh-code-mode-e2e-'))
  67. ctx = await codeModeHarness(workdir)
  68. const agent = ctx.agentLoop.create(AgentId('e2e-code-mode'), { model: 'deepseek-v4-flash' })
  69. agent.send([{
  70. type: 'text',
  71. text: 'Using one run_code program: run `echo alpha-7` with the bash tool, run `echo beta-9` with the bash tool, '
  72. + 'then write both outputs joined by a plus sign into combined.txt (bash heredoc or redirect), '
  73. + 'and return only the joined string.',
  74. }])
  75. await waitForIdle(ctx, agent)
  76. const events: SessionEvent[] = [...agent.session.events]
  77. // The wire contract: every request this session made offered EXACTLY ONE
  78. // tool — run_code (the logged header snapshots the assembled list).
  79. const headers = events.filter(event => event.type === 'request/header')
  80. expect(headers.length).toBeGreaterThan(0)
  81. for (const header of headers) {
  82. expect(header.data.header.tools?.map(tool => tool.name)).toEqual([RUN_CODE_NAME])
  83. }
  84. // The model actually went through run_code…
  85. const calls = events.filter(event => event.type === 'tool/call')
  86. expect(calls.length).toBeGreaterThan(0)
  87. expect(calls.every(event => event.data.name === RUN_CODE_NAME)).toBe(true)
  88. // …and the program's tool calls landed as dispatch events under it.
  89. const dispatches = events.filter(event => event.type === 'tool/code-dispatch')
  90. expect(dispatches.length).toBeGreaterThanOrEqual(2)
  91. expect(dispatches.every(event => event.data.name === 'bash')).toBe(true)
  92. const parents = new Set(calls.map(event => event.data.callId))
  93. expect(dispatches.every(event => parents.has(event.data.parentCallId))).toBe(true)
  94. // World verification: the file the program wrote, and the curated answer.
  95. const combined = await readFile(join(workdir, 'combined.txt'), 'utf8')
  96. expect(combined).toContain('alpha-7')
  97. expect(combined).toContain('beta-9')
  98. const finalMessage = events.findLast(event => event.type === 'assistant/message')
  99. const finalText = finalMessage !== undefined
  100. ? finalMessage.data.content.filter(block => block.type === 'text').map(block => block.text).join('')
  101. : ''
  102. expect(finalText).toContain('alpha-7')
  103. expect(finalText).toContain('beta-9')
  104. }, 180_000)
  105. })