resume.e2e.ts 3.2 KB

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