resume.e2e.ts 3.2 KB

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