compaction.e2e.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { mkdtemp, 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 type { Context } from 'cordis'
  6. import { AgentId } from '@deepseek-ai/dsh-agent'
  7. import { codingHarness, finalText, SYSTEM_PROMPT, waitForIdle } from './harness.ts'
  8. /** Key-gated smoke for mid-session compaction and continued agent progress. */
  9. // FIXME(compaction-snapshot): replay cannot serve the unlogged summarization model call.
  10. let workdir: string | undefined
  11. let ctx: Context | undefined
  12. afterEach(async () => {
  13. await ctx?.fiber.dispose()
  14. ctx = undefined
  15. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  16. workdir = undefined
  17. })
  18. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('compaction: a long session compacts mid-flight and keeps running', () => {
  19. it('summarizes older history into a checkpoint without breaking the task', async () => {
  20. workdir = await mkdtemp(join(tmpdir(), 'dsh-compaction-'))
  21. for (let i = 1; i <= 4; i++) {
  22. await writeFile(join(workdir, `file${i}.txt`), `This is file number ${i}. `.repeat(50))
  23. }
  24. // Reasoning tokens require a larger generation cap than the retained checkpoint.
  25. ctx = await codingHarness(workdir, {
  26. persona: SYSTEM_PROMPT,
  27. compact: {
  28. contextWindow: 2000,
  29. thresholdRatio: 0.5,
  30. retainTokens: 400,
  31. summarizationModel: '',
  32. maxTokens: 1024,
  33. compactionRetries: 1,
  34. },
  35. persistenceRoot: join(workdir, '.sessions'),
  36. })
  37. const agent = ctx.agentLoop.create(AgentId('e2e-compaction'), { model: 'deepseek-v4-flash' })
  38. agent.send([{
  39. type: 'text',
  40. text: 'Read file1.txt, file2.txt, file3.txt, and file4.txt one at a '
  41. + 'time using cat (a separate bash command for each). After reading all four, tell me how '
  42. + 'many files you read and the number mentioned in file1.txt.',
  43. }])
  44. await waitForIdle(ctx, agent)
  45. const events = [...agent.session.events]
  46. // A compaction ran: the start…end bracket landed in the real log.
  47. const starts = events.filter(e => e.type === 'compact/start')
  48. const ends = events.filter(e => e.type === 'compact/end')
  49. expect(starts.length).toBeGreaterThan(0)
  50. expect(ends.length).toBe(starts.length) // every start was released
  51. // It succeeded at least once: a compact/summary provenance event and a
  52. // replace-op user/message (the surface mutation) both landed.
  53. const summaries = events.filter(e => e.type === 'compact/summary')
  54. expect(summaries.length).toBeGreaterThan(0)
  55. const replaceNode = events.find((e) => {
  56. const se = e as unknown as { type: string; surfaceOp?: unknown }
  57. return se.type === 'user/message' && typeof se.surfaceOp === 'object' && se.surfaceOp !== null
  58. })
  59. expect(replaceNode).toBeDefined()
  60. // The summary shadowed real older nodes (the surface shrank vs. the raw
  61. // message-producing event count).
  62. const summaryData = summaries[0]!.data as { shadowedSeqs: number[] }
  63. expect(summaryData.shadowedSeqs.length).toBeGreaterThan(0)
  64. // The conversation survived compaction: the agent produced a final answer
  65. // that reflects the work (it read four files).
  66. const answer = finalText(events).toLowerCase()
  67. expect(answer.length).toBeGreaterThan(0)
  68. expect(answer).toMatch(/\b(4|four)\b/)
  69. }, 240_000)
  70. })