agent-instructions.e2e.ts 5.8 KB

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