resume.e2e.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 { SessionId } from '@deepseek-ai/dsh-session'
  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 = SessionId('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(), { persona: SYSTEM_PROMPT, persistenceRoot: root })
  34. const first = (await ctx.agents.create({
  35. sessionId: SESSION_ID,
  36. agentOptions: { provider: 'deepseek', model: 'deepseek-v4-flash' },
  37. })).agent
  38. first.send([{ type: 'text', text: `Remember this code for later: ${SECRET}. Just acknowledge it.` }])
  39. await waitForIdle(ctx, first)
  40. await ctx.fiber.dispose()
  41. ctx = undefined
  42. // Run 2: a brand-new context over the SAME root resumes the persisted
  43. // session. The loaded event log seeds the live session, so the model sees
  44. // run 1's exchange as conversation history.
  45. ctx = await codingHarness(process.cwd(), { persona: SYSTEM_PROMPT, persistenceRoot: root })
  46. const resumed = (await ctx.agents.resume({
  47. resumeSessionId: SESSION_ID,
  48. agentOptions: { provider: 'deepseek', model: 'deepseek-v4-flash' },
  49. })).agent
  50. expect(resumed.session.id).toBe(SESSION_ID)
  51. // The prior user turn is in the rehydrated log before the model is asked.
  52. expect(JSON.stringify(resumed.session.deriveMessages())).toContain(SECRET)
  53. resumed.send([{ type: 'text', text: 'What was the code I asked you to remember? Reply with just the code.' }])
  54. await waitForIdle(ctx, resumed)
  55. // The model recalls it — only possible from the resumed history.
  56. expect(finalText([...resumed.session.events])).toContain(SECRET)
  57. }, 180_000)
  58. })