compaction.e2e.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  9. * The compaction smoke test: a real model runs a multi-step bash task with a
  10. * deliberately tiny context window, so the auto-compaction listener fires
  11. * MID-SESSION and summarizes the older history into a checkpoint. This is the
  12. * first end-to-end exercise of the compaction seam (it is wired nowhere else),
  13. * and the runaway-survival regression net — it proves a session that grows past
  14. * the window keeps running rather than overflowing. Key-gated.
  15. *
  16. * Verifies the WORLD, not the agent's self-report: a compact/start…end pair
  17. * landed in the real session log, the surface actually shrank (a replace node
  18. * exists and shadowed older nodes), and the agent still produced a final answer
  19. * after compaction (so the summarized history did not break the conversation).
  20. */
  21. let workdir: string | undefined
  22. let ctx: Context | undefined
  23. afterEach(async () => {
  24. await ctx?.fiber.dispose()
  25. ctx = undefined
  26. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  27. workdir = undefined
  28. })
  29. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('compaction: a long session compacts mid-flight and keeps running', () => {
  30. it('summarizes older history into a checkpoint without breaking the task', async () => {
  31. workdir = await mkdtemp(join(tmpdir(), 'dsh-compaction-'))
  32. // A handful of files for the model to read, so multiple bash steps
  33. // accumulate surface nodes (tool calls + results) and grow the history past
  34. // the (deliberately tiny) window.
  35. for (let i = 1; i <= 6; i++) {
  36. await writeFile(join(workdir, `file${i}.txt`), `This is file number ${i}. `.repeat(40))
  37. }
  38. // Tiny window so a couple of steps crosses the threshold. The generation
  39. // cap is deliberately larger than the final checkpoint because
  40. // reasoning-capable APIs count reasoning tokens against the provider output
  41. // budget even though those blocks are stripped before the checkpoint is
  42. // stored.
  43. ctx = await codingHarness(workdir, {
  44. compact: {
  45. contextWindow: 2400,
  46. thresholdRatio: 0.5,
  47. retainTokens: 500,
  48. maxTokens: 2048,
  49. },
  50. persistenceRoot: './.sessions',
  51. })
  52. const agent = ctx.agentLoop.create(AgentId('e2e-compaction'), {
  53. model: 'deepseek-v4-flash',
  54. systemPrompt: SYSTEM_PROMPT,
  55. })
  56. agent.send([{
  57. type: 'text',
  58. text: 'Read file1.txt, file2.txt, file3.txt, file4.txt, file5.txt, and file6.txt one at a '
  59. + 'time using cat (a separate bash command for each). After reading all six, tell me how '
  60. + 'many files you read and the number mentioned in file1.txt.',
  61. }])
  62. await waitForIdle(ctx, agent)
  63. const events = [...agent.session.events]
  64. // A compaction ran: the start…end bracket landed in the real log.
  65. const starts = events.filter(e => e.type === 'compact/start')
  66. const ends = events.filter(e => e.type === 'compact/end')
  67. expect(starts.length).toBeGreaterThan(0)
  68. expect(ends.length).toBe(starts.length) // every start was released
  69. // It succeeded at least once: a compact/summary provenance event and a
  70. // replace-op user/message (the surface mutation) both landed.
  71. const summaries = events.filter(e => e.type === 'compact/summary')
  72. expect(summaries.length).toBeGreaterThan(0)
  73. const replaceNode = events.find((e) => {
  74. const se = e as unknown as { type: string; surfaceOp?: unknown }
  75. return se.type === 'user/message' && typeof se.surfaceOp === 'object' && se.surfaceOp !== null
  76. })
  77. expect(replaceNode).toBeDefined()
  78. // The summary shadowed real older nodes (the surface shrank vs. the raw
  79. // message-producing event count).
  80. const summaryData = summaries[0]!.data as { shadowedSeqs: number[] }
  81. expect(summaryData.shadowedSeqs.length).toBeGreaterThan(0)
  82. // The conversation survived compaction: the agent produced a final answer
  83. // that reflects the work (it read six files).
  84. const answer = finalText(events).toLowerCase()
  85. expect(answer.length).toBeGreaterThan(0)
  86. expect(answer).toMatch(/\b(6|six)\b/)
  87. }, 240_000)
  88. })