fs-tools.e2e.ts 3.8 KB

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