fs-tools.e2e.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 { AgentId } from '@deepseek-ai/dsh-agent'
  7. import { SessionId } from '@deepseek-ai/dsh-session'
  8. import { fsHarness, waitForIdle } from './harness.ts'
  9. /**
  10. * With-key smoke for the filesystem tools: a REAL model drives the REAL
  11. * read/write/edit tools (over the real local backend + policy gate), and we
  12. * verify the WORLD — the file on disk — not the agent's self-report. This is the
  13. * "green units, broken product" guard: mocks prove the plumbing, only a real
  14. * model proves the tools actually work end-to-end. Key-gated (self-skips without
  15. * DEEPSEEK_API_KEY).
  16. */
  17. let ctx: Context | undefined
  18. let workdir: string | undefined
  19. afterEach(async () => {
  20. await ctx?.fiber.dispose()
  21. ctx = undefined
  22. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  23. workdir = undefined
  24. })
  25. const SYSTEM = 'You are a coding assistant. Use the write tool to create files, the read tool to inspect '
  26. + 'them, and the edit tool for literal replacements. Read a file before editing it. Keep replies terse.'
  27. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('fs tools with-key smoke', () => {
  28. it('creates, reads, then edits a file — verified on disk', async () => {
  29. workdir = await mkdtemp(join(tmpdir(), 'dsh-fs-e2e-'))
  30. ctx = await fsHarness(workdir)
  31. // agentLoop.create prepares a session with no cwd, so the provider default
  32. // (config.cwd = workdir) is the workspace.
  33. const agent = ctx.agentLoop.create(AgentId('fs-e2e'), { model: 'deepseek-v4-flash', systemPrompt: SYSTEM })
  34. agent.send([{ type: 'text', text:
  35. 'Create a file named note.txt containing exactly the line: status: draft. '
  36. + 'Then read it back, then edit it to replace the literal word draft with final. '
  37. + 'Tell me when done.' }])
  38. await waitForIdle(ctx, agent)
  39. // Verify the WORLD: the edit landed on disk.
  40. const content = await readFile(join(workdir, 'note.txt'), 'utf8')
  41. expect(content).toContain('status: final')
  42. expect(content).not.toContain('draft')
  43. // The log records real read/write/edit tool calls (not bash).
  44. const calls = [...agent.session.events].filter(e => e.type === 'tool/call').map(e => e.data.name)
  45. expect(calls).toContain('write')
  46. expect(calls).toContain('read')
  47. expect(calls).toContain('edit')
  48. }, 180_000)
  49. it('resolves a relative path against the per-session cwd (factory meta.cwd)', async () => {
  50. // config.cwd is the harness workdir, but the agent's SESSION cwd is a
  51. // different dir; the write must land in the SESSION dir, proving the tool
  52. // passes the per-session cwd (not the backend default).
  53. const configDir = await mkdtemp(join(tmpdir(), 'dsh-fs-e2e-cfg-'))
  54. workdir = configDir
  55. const sessionDir = await mkdtemp(join(tmpdir(), 'dsh-fs-e2e-session-'))
  56. try {
  57. ctx = await fsHarness(configDir)
  58. const handle = ctx.agents.create({
  59. agentId: AgentId('fs-e2e-cwd'),
  60. sessionId: SessionId(`fs-e2e-cwd-${Date.now()}`),
  61. meta: { cwd: sessionDir },
  62. agentOptions: { model: 'deepseek-v4-flash', systemPrompt: SYSTEM },
  63. })
  64. handle.agent.send([{ type: 'text', text:
  65. 'Use the write tool to create a file named where.txt containing exactly the line: here. Tell me when done.' }])
  66. await waitForIdle(ctx, handle.agent)
  67. // The file is in the SESSION dir, not the config dir.
  68. expect(await readFile(join(sessionDir, 'where.txt'), 'utf8')).toContain('here')
  69. await expect(readFile(join(configDir, 'where.txt'), 'utf8')).rejects.toMatchObject({ code: 'ENOENT' })
  70. } finally {
  71. await rm(sessionDir, { recursive: true, force: true })
  72. }
  73. }, 180_000)
  74. })