workspace-context.e2e.ts 5.8 KB

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