compaction.e2e.ts 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 few files for the model to read, so multiple bash steps accumulate
  33. // surface nodes (tool calls + results) and grow the history.
  34. for (let i = 1; i <= 4; i++) {
  35. await writeFile(join(workdir, `file${i}.txt`), `This is file number ${i}. `.repeat(40))
  36. }
  37. // Tiny window so a handful of steps crosses the threshold. The convergence
  38. // invariant requires summarizationMaxTokens + retainTokens <= window *
  39. // ratio = floor(8000 * 0.5) = 4000; 1500 + 2000 = 3500 <= 4000.
  40. ctx = await codingHarness(workdir, {
  41. compact: {
  42. contextWindow: 8000,
  43. thresholdRatio: 0.5,
  44. retainTokens: 2000,
  45. summarizationMaxTokens: 1500,
  46. },
  47. })
  48. const agent = ctx.agentLoop.create(AgentId('e2e-compaction'), {
  49. model: 'deepseek-v4-flash',
  50. systemPrompt: SYSTEM_PROMPT,
  51. })
  52. agent.send([{
  53. type: 'text',
  54. text: 'Read file1.txt, file2.txt, file3.txt, and file4.txt one at a time using cat '
  55. + '(a separate bash command for each). After reading all four, tell me how many '
  56. + 'files you read and the number mentioned in file1.txt.',
  57. }])
  58. await waitForIdle(ctx, agent)
  59. const events = [...agent.session.events]
  60. // A compaction ran: the start…end bracket landed in the real log.
  61. const starts = events.filter(e => e.type === 'compact/start')
  62. const ends = events.filter(e => e.type === 'compact/end')
  63. expect(starts.length).toBeGreaterThan(0)
  64. expect(ends.length).toBe(starts.length) // every start was released
  65. // It succeeded at least once: a compact/summary provenance event and a
  66. // replace-op user/message (the surface mutation) both landed.
  67. const summaries = events.filter(e => e.type === 'compact/summary')
  68. expect(summaries.length).toBeGreaterThan(0)
  69. const replaceNode = events.find((e) => {
  70. const se = e as unknown as { type: string; surfaceOp?: unknown }
  71. return se.type === 'user/message' && typeof se.surfaceOp === 'object' && se.surfaceOp !== null
  72. })
  73. expect(replaceNode).toBeDefined()
  74. // The summary shadowed real older nodes (the surface shrank vs. the raw
  75. // message-producing event count).
  76. const summaryData = summaries[0]!.data as { shadowedSeqs: number[] }
  77. expect(summaryData.shadowedSeqs.length).toBeGreaterThan(0)
  78. // The conversation survived compaction: the agent produced a final answer
  79. // that reflects the work (it read four files).
  80. const answer = finalText(events).toLowerCase()
  81. expect(answer.length).toBeGreaterThan(0)
  82. expect(answer).toMatch(/\b(4|four)\b/)
  83. }, 240_000)
  84. })