compaction.e2e.ts 3.6 KB

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