spawn.e2e.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 type { Context } from 'cordis'
  6. import { spawnHarness, waitForIdle } from './harness.ts'
  7. import { SessionId } from '@deepseek-ai/dsh-session'
  8. /**
  9. * With-key smoke for the in-process spawn backend: a REAL parent agent delegates
  10. * to a REAL child (via the `subagent` tool → spawn backend) that uses the REAL
  11. * bash tool to write a file, and we verify the WORLD (the file on disk) — not
  12. * the agent's self-report. This is the "green units, broken product" guard:
  13. * mocks prove the plumbing, only a real model proves a parent can actually drive
  14. * a child to do real work. Key-gated (self-skips without DEEPSEEK_API_KEY).
  15. */
  16. let ctx: Context | undefined
  17. let workdir: string | undefined
  18. afterEach(async () => {
  19. await ctx?.fiber.dispose()
  20. ctx = undefined
  21. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  22. workdir = undefined
  23. })
  24. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('spawn backend with-key smoke', () => {
  25. it('a parent delegates to a child that writes a file on disk', async () => {
  26. workdir = await mkdtemp(join(tmpdir(), 'dsh-subagent-spawn-e2e-'))
  27. ctx = await spawnHarness(workdir)
  28. const parent = ctx.agentLoop.create(SessionId('e2e-parent'), { provider: 'deepseek', model: 'deepseek-v4-flash' })
  29. parent.send([{ type: 'text', text:
  30. 'Use the subagent tool to delegate this exact task: "Use the bash tool to write the text '
  31. + 'SUBAGENT_WAS_HERE into a file named proof.txt in the current directory." '
  32. + 'After the subagent finishes, tell me it is done.' }])
  33. await waitForIdle(ctx, parent)
  34. // Verify the WORLD: the child actually wrote the file.
  35. const proof = await readFile(join(workdir, 'proof.txt'), 'utf8')
  36. expect(proof).toContain('SUBAGENT_WAS_HERE')
  37. // The parent's log records the subagent tool/call + its result (not the
  38. // child's internal steps).
  39. const events = [...parent.session.events]
  40. const subagentCalls = events.filter(e => e.type === 'tool/call' && e.data.name === 'subagent')
  41. expect(subagentCalls.length).toBeGreaterThan(0)
  42. }, 180_000)
  43. })