workspace-context.e2e.ts 5.9 KB

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