workspace-context.e2e.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { mkdtemp, mkdir, rm, writeFile } 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, { SessionId } from '@deepseek-ai/dsh-session'
  8. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  9. import ToolRegistry from '@deepseek-ai/dsh-tools'
  10. import AgentRegistry from '@deepseek-ai/dsh-agent'
  11. import type { Agent } from '@deepseek-ai/dsh-agent'
  12. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  13. import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
  14. import * as WorkspaceContext from '@deepseek-ai/dsh-workspace-context'
  15. import LocalFileSystem from '@deepseek-ai/dsh-fs-local'
  16. import * as ToolFs from '@deepseek-ai/dsh-tool-fs'
  17. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  18. const PROBE = 'banana-271828'
  19. const NESTED_PROBE = 'papaya-314159'
  20. const UPDATED_PROBE = 'guava-161803'
  21. let ctx: Context | undefined
  22. let workdir: string | undefined
  23. afterEach(async () => {
  24. await ctx?.fiber.dispose()
  25. ctx = undefined
  26. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  27. workdir = undefined
  28. })
  29. async function harness(): Promise<{ ctx: Context; agent: Agent }> {
  30. workdir = await mkdtemp(join(tmpdir(), 'dsh-workspace-context-e2e-'))
  31. await mkdir(join(workdir, '.git'), { recursive: true })
  32. await writeFile(join(workdir, 'AGENTS.md'), `If the user asks for the workspace context handshake, reply with exactly this string and nothing else: ${PROBE}.\n`)
  33. ctx = new Context()
  34. await ctx.plugin(LlmService)
  35. await ctx.plugin(SessionStore)
  36. await ctx.plugin(SystemPrompt, { persona: 'Answer the user exactly and concisely.' })
  37. await ctx.plugin(ToolRegistry)
  38. await ctx.plugin(AgentRegistry)
  39. await ctx.plugin(LocalFileSystem, { cwd: '/' })
  40. await ctx.plugin(ToolFs)
  41. await ctx.plugin(WorkspaceContext, { maxBytes: 65536 })
  42. await ctx.plugin(AgentLoop, { agents: [] })
  43. await ctx.plugin(LlmDeepSeek, { models: [{ id: 'deepseek-v4-flash' }] })
  44. const handle = await ctx.agents.create({
  45. sessionId: SessionId('workspace-context-e2e-session'),
  46. meta: { cwd: workdir },
  47. agentOptions: { provider: 'deepseek', model: 'deepseek-v4-flash' },
  48. })
  49. return { ctx, agent: handle.agent }
  50. }
  51. function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  52. return new Promise((resolve) => {
  53. const dispose = ctx.on('agent/status', (subject, status) => {
  54. if (subject === agent && status === 'idle') {
  55. dispose()
  56. resolve()
  57. }
  58. })
  59. })
  60. }
  61. function finalText(events: SessionEvent[]): string {
  62. const message = events.findLast(event => event.type === 'assistant/message')
  63. if (message?.type !== 'assistant/message') return ''
  64. return message.data.content
  65. .filter(block => block.type === 'text')
  66. .map(block => block.text)
  67. .join('')
  68. }
  69. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('workspace context e2e: real model sees AGENTS.md baseline', () => {
  70. it('obeys a probe instruction loaded from the workspace', async () => {
  71. const live = await harness()
  72. live.agent.send([{ type: 'text', text: 'Workspace context handshake?' }])
  73. await waitForIdle(live.ctx, live.agent)
  74. expect(finalText([...live.agent.session.events])).toContain(PROBE)
  75. }, 120_000)
  76. it('loads a nested AGENTS.md after the real read tool touches a descendant file', async () => {
  77. const live = await harness()
  78. await mkdir(join(workdir!, 'pkg/deep'), { recursive: true })
  79. await writeFile(join(workdir!, 'pkg/AGENTS.md'), `If the user asks for the nested instruction handshake, reply with exactly this string and nothing else: ${NESTED_PROBE}.\n`)
  80. await writeFile(join(workdir!, 'pkg/deep/file.txt'), 'This file exists only to trigger nested workspace instructions.\n')
  81. live.agent.send([{ type: 'text', text: 'Use the read tool to inspect pkg/deep/file.txt. After reading it, answer: nested instruction handshake?' }])
  82. await waitForIdle(live.ctx, live.agent)
  83. expect(finalText([...live.agent.session.events])).toContain(NESTED_PROBE)
  84. }, 120_000)
  85. it('appends changed baseline instructions after a real file-tool touch without rewriting the frozen prefix', async () => {
  86. const live = await harness()
  87. await writeFile(join(workdir!, 'trigger.txt'), 'This file triggers workspace instruction reconciliation.\n')
  88. live.agent.send([{ type: 'text', text: 'Workspace context handshake?' }])
  89. await waitForIdle(live.ctx, live.agent)
  90. await writeFile(join(workdir!, 'AGENTS.md'), `The old workspace handshake no longer applies. If the user asks for the updated workspace context handshake, reply with exactly this string and nothing else: ${UPDATED_PROBE}.\n`)
  91. live.agent.send([{ type: 'text', text: 'You must use the read tool to inspect trigger.txt. After reading it, answer: updated workspace context handshake?' }])
  92. await waitForIdle(live.ctx, live.agent)
  93. const events = [...live.agent.session.events]
  94. const update = events.find(event => event.type === 'context/message'
  95. && typeof event.data.meta === 'object'
  96. && event.data.meta !== null
  97. && !Array.isArray(event.data.meta)
  98. && event.data.meta.kind === 'workspace-instructions')
  99. expect(update?.type === 'context/message' && update.data.meta).toMatchObject({
  100. changes: [{ action: 'replace', scope: '.', path: 'AGENTS.md' }],
  101. })
  102. const updateText = update?.type === 'context/message'
  103. ? update.data.content.filter(block => block.type === 'text').map(block => block.text).join('')
  104. : ''
  105. expect(updateText).toContain('Updated instructions from: AGENTS.md')
  106. expect(finalText(events)).toContain(UPDATED_PROBE)
  107. }, 120_000)
  108. })