compaction.e2e.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. summarizationModel: '',
  36. maxTokens: 1024,
  37. compactionRetries: 1,
  38. },
  39. persistenceRoot: join(workdir, '.sessions'),
  40. })
  41. const agent = ctx.agentLoop.create(AgentId('e2e-compaction'), { model: 'deepseek-v4-flash' })
  42. agent.send([{
  43. type: 'text',
  44. text: 'Read file1.txt, file2.txt, file3.txt, and file4.txt one at a '
  45. + 'time using cat (a separate bash command for each). After reading all four, tell me how '
  46. + 'many files you read and the number mentioned in file1.txt.',
  47. }])
  48. await waitForIdle(ctx, agent)
  49. const events = [...agent.session.events]
  50. // A compaction ran: the start…end bracket landed in the real log.
  51. const starts = events.filter(e => e.type === 'compact/start')
  52. const ends = events.filter(e => e.type === 'compact/end')
  53. expect(starts.length).toBeGreaterThan(0)
  54. expect(ends.length).toBe(starts.length) // every start was released
  55. // It succeeded at least once: a compact/summary provenance event and a
  56. // replace-op user/message (the surface mutation) both landed.
  57. const summaries = events.filter(e => e.type === 'compact/summary')
  58. expect(summaries.length).toBeGreaterThan(0)
  59. const replaceNode = events.find((e) => {
  60. const se = e as unknown as { type: string; surfaceOp?: unknown }
  61. return se.type === 'user/message' && typeof se.surfaceOp === 'object' && se.surfaceOp !== null
  62. })
  63. expect(replaceNode).toBeDefined()
  64. // The summary shadowed real older nodes (the surface shrank vs. the raw
  65. // message-producing event count).
  66. const summaryData = summaries[0]!.data as { shadowedSeqs: number[] }
  67. expect(summaryData.shadowedSeqs.length).toBeGreaterThan(0)
  68. // The conversation survived compaction: the agent produced a final answer
  69. // that reflects the work (it read four files).
  70. const answer = finalText(events).toLowerCase()
  71. expect(answer.length).toBeGreaterThan(0)
  72. expect(answer).toMatch(/\b(4|four)\b/)
  73. }, 240_000)
  74. })