workspace-context.e2e.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 { candidateScopeKey } from '../src/render.ts'
  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(AgentLoop, { agents: [] })
  44. await ctx.plugin(LlmDeepSeek, { models: [{ id: 'deepseek-v4-flash' }] })
  45. const handle = await ctx.agents.create({
  46. sessionId: SessionId('workspace-context-e2e-session'),
  47. meta: { cwd: workdir },
  48. agentOptions: { provider: 'deepseek', model: 'deepseek-v4-flash' },
  49. })
  50. return { ctx, agent: handle.agent }
  51. }
  52. function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  53. return new Promise((resolve) => {
  54. const dispose = ctx.on('agent/status', (subject, status) => {
  55. if (subject === agent && status === 'idle') {
  56. dispose()
  57. resolve()
  58. }
  59. })
  60. })
  61. }
  62. function finalText(events: SessionEvent[]): string {
  63. const message = events.findLast(event => event.type === 'assistant/message')
  64. if (message?.type !== 'assistant/message') return ''
  65. return message.data.content
  66. .filter(block => block.type === 'text')
  67. .map(block => block.text)
  68. .join('')
  69. }
  70. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('workspace context e2e: real model sees AGENTS.md baseline', () => {
  71. it('obeys a probe instruction loaded from the workspace', async () => {
  72. const live = await harness()
  73. live.agent.followup({ content: [{ type: 'text', text: 'Workspace context handshake?' }], source: { kind: 'user' } })
  74. await waitForIdle(live.ctx, live.agent)
  75. expect(finalText([...live.agent.session.events])).toContain(PROBE)
  76. }, 120_000)
  77. it('loads a nested AGENTS.md after the real read tool touches a descendant file', async () => {
  78. const live = await harness()
  79. await mkdir(join(workdir!, 'pkg/deep'), { recursive: true })
  80. 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`)
  81. await writeFile(join(workdir!, 'pkg/deep/file.txt'), 'This file exists only to trigger nested workspace instructions.\n')
  82. live.agent.followup({ content: [{ type: 'text', text: 'Use the read tool to inspect pkg/deep/file.txt. After reading it, answer: nested instruction handshake?' }], source: { kind: 'user' } })
  83. await waitForIdle(live.ctx, live.agent)
  84. expect(finalText([...live.agent.session.events])).toContain(NESTED_PROBE)
  85. }, 120_000)
  86. it('appends changed baseline instructions after a real file-tool touch without rewriting the frozen prefix', async () => {
  87. const live = await harness()
  88. await writeFile(join(workdir!, 'trigger.txt'), 'This file triggers workspace instruction reconciliation.\n')
  89. live.agent.followup({ content: [{ type: 'text', text: 'Workspace context handshake?' }], source: { kind: 'user' } })
  90. await waitForIdle(live.ctx, live.agent)
  91. 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`)
  92. live.agent.followup({ content: [{ type: 'text', text: 'You must use the read tool to inspect trigger.txt. After reading it, answer: updated workspace context handshake?' }], source: { kind: 'user' } })
  93. await waitForIdle(live.ctx, live.agent)
  94. const events = [...live.agent.session.events]
  95. const update = events.find(event => event.type === 'user/message'
  96. && event.data.source.kind === 'workspace-instructions'
  97. && event.data.source.baseline !== true)
  98. expect(update?.type === 'user/message' && update.data.source).toMatchObject({
  99. changes: [{ action: 'replace', scope: candidateScopeKey('.', 'AGENTS.md'), path: 'AGENTS.md' }],
  100. })
  101. const updateText = update?.type === 'user/message'
  102. ? update.data.content.filter(block => block.type === 'text').map(block => block.text).join('')
  103. : ''
  104. expect(updateText).toContain('Updated instructions from: AGENTS.md')
  105. expect(finalText(events)).toContain(UPDATED_PROBE)
  106. }, 120_000)
  107. })