1
0

resume.e2e.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { mkdtemp, rm } 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 type { LoopAgent } from '@deepseek-ai/dsh-agent-loop'
  7. import { codingHarness, finalText, SYSTEM_PROMPT, waitForIdle } from './harness.ts'
  8. /**
  9. * Proves durable conversation continuity end-to-end: run 1 tells the REAL model
  10. * a fact and persists the turn to JSONL; run 2 is a fresh harness (new Context,
  11. * same `.sessions` root) that RESUMES the persisted session id and asks the
  12. * model to recall the fact. The recall can only come from the rehydrated event
  13. * log — a fresh session would have no idea. Key-gated like the other e2es.
  14. */
  15. const SECRET = 'plum-galaxy-1791'
  16. const SESSION_ID = 'resume-e2e-session'
  17. let ctx: Context | undefined
  18. let root: string | undefined
  19. afterEach(async () => {
  20. // Dispose even on failure/retry: agent-loop teardown stops the loop and the
  21. // JSONL backend flushes; then drop the on-disk session log.
  22. await ctx?.fiber.dispose()
  23. ctx = undefined
  24. if (root !== undefined) await rm(root, { recursive: true, force: true })
  25. root = undefined
  26. })
  27. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('resume: continue a persisted session across processes', () => {
  28. it('recalls a fact stored in a prior, separately-disposed session', async () => {
  29. root = await mkdtemp(join(tmpdir(), 'dsh-resume-e2e-'))
  30. // Run 1: a fresh agent on a KNOWN session id learns a secret, then we
  31. // dispose the whole context (simulating process exit) so only the JSONL
  32. // log on disk survives.
  33. ctx = await codingHarness(process.cwd(), root)
  34. const first = ctx.agents.create({
  35. agentId: 'resume-1',
  36. sessionId: SESSION_ID,
  37. agentOptions: { model: 'deepseek-v4-flash', systemPrompt: SYSTEM_PROMPT },
  38. }) as LoopAgent
  39. first.send([{ type: 'text', text: `Remember this code for later: ${SECRET}. Just acknowledge it.` }])
  40. await waitForIdle(ctx, first)
  41. await ctx.fiber.dispose()
  42. ctx = undefined
  43. // Run 2: a brand-new context over the SAME root resumes the persisted
  44. // session. The loaded event log seeds the live session, so the model sees
  45. // run 1's exchange as conversation history.
  46. ctx = await codingHarness(process.cwd(), root)
  47. const resumed = await ctx.agents.resume({
  48. agentId: 'resume-2',
  49. resumeSessionId: SESSION_ID,
  50. agentOptions: { model: 'deepseek-v4-flash', systemPrompt: SYSTEM_PROMPT },
  51. }) as LoopAgent
  52. expect(resumed.session.id).toBe(SESSION_ID)
  53. // The prior user turn is in the rehydrated log before the model is asked.
  54. expect(JSON.stringify(resumed.session.deriveMessages())).toContain(SECRET)
  55. resumed.send([{ type: 'text', text: 'What was the code I asked you to remember? Reply with just the code.' }])
  56. await waitForIdle(ctx, resumed)
  57. // The model recalls it — only possible from the resumed history.
  58. expect(finalText([...resumed.session.events])).toContain(SECRET)
  59. }, 180_000)
  60. })