semantic-checkpoint.snapshot.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { readFile, writeFile } from 'node:fs/promises'
  2. import { dirname, join } from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. import { Context } from 'cordis'
  5. import { normalizeSessionLog, scrubRequestHeaders, type NormalizeContext } from '@deepseek-ai/dsh-acp-snapshot'
  6. import { LOADER_SMOKE_TEST_TIMEOUT_MS, runLoaderSmoke } from '@deepseek-ai/dsh-loader-smoke'
  7. import { createUserMessage, CallId , createMessage } from '@deepseek-ai/dsh-llm'
  8. import SessionStore, { SESSION_FORMAT_VERSION, SessionId, type SessionEvent, type SessionHeader } from '@deepseek-ai/dsh-session'
  9. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  10. import { describe, expect, it } from 'vitest'
  11. const fixtureDir = join(dirname(fileURLToPath(import.meta.url)), 'semantic-checkpoint-snapshots/tool-outcome-unknown')
  12. const replayFixture = join(fixtureDir, 'replay.jsonl')
  13. const replayOverride = join(fixtureDir, 'replay.override.json')
  14. const sessionExpected = join(fixtureDir, 'session.expected.jsonl')
  15. const configPath = fileURLToPath(new URL('../semantic-checkpoint.cordis.snapshot.yml', import.meta.url))
  16. const binScript = fileURLToPath(new URL('../../../packages/examples/cli-demo/src/bin.ts', import.meta.url))
  17. const tsconfigPath = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  18. const sessionId = SessionId('semantic-checkpoint-unknown-outcome')
  19. const refreshing = process.env.DSH_SNAPSHOT === 'refresh'
  20. const task = 'Continue safely from the interrupted operation.'
  21. async function seedInterruptedSession(root: string, cwd: string): Promise<string> {
  22. const ctx = new Context()
  23. await ctx.plugin(SessionStore)
  24. await ctx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  25. const meta: SessionHeader = {
  26. version: SESSION_FORMAT_VERSION,
  27. id: sessionId,
  28. createdAt: 1,
  29. cwd,
  30. delegationDepth: 0,
  31. }
  32. const events: SessionEvent[] = [
  33. { type: 'turn/start', seq: 0, time: 10, data: { turn: 1 } },
  34. { type: 'user/message', seq: 1, time: 11, data: createUserMessage({
  35. content: [{ type: 'text', text: 'Perform one side-effecting remote mutation.' }], source: { kind: 'user' },
  36. }), surfaceOp: 'append' },
  37. { type: 'step/start', seq: 2, time: 12, data: { turn: 1, step: 1 } },
  38. {
  39. type: 'assistant/message',
  40. seq: 3,
  41. time: 13,
  42. data: {
  43. turn: 1,
  44. step: 1,
  45. message: createMessage({
  46. role: 'assistant',
  47. content: [{ type: 'tool-call', id: CallId('unknown-outcome-call'), name: 'write_remote', arguments: '{"value":1}' }],
  48. source: {
  49. kind: 'model',
  50. ...{ provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  51. },
  52. }),
  53. },
  54. surfaceOp: 'append',
  55. },
  56. {
  57. type: 'tool/call',
  58. seq: 4,
  59. time: 14,
  60. data: {
  61. turn: 1,
  62. step: 1,
  63. callId: CallId('unknown-outcome-call'),
  64. name: 'write_remote',
  65. arguments: '{"value":1}',
  66. },
  67. },
  68. ]
  69. try {
  70. await ctx.sessionPersistence.create(meta)
  71. await ctx.sessionPersistence.append(sessionId, events)
  72. const location = ctx.sessionPersistence.locate(meta)
  73. if (location === undefined) throw new Error('JSONL backend did not locate the seeded session')
  74. return location.path
  75. } finally {
  76. await ctx.fiber.dispose()
  77. }
  78. }
  79. describe('semantic checkpoint recovery snapshot', () => {
  80. it('resumes an unknown tool outcome through the headless stream-json app', async () => {
  81. let cwd = ''
  82. let sessionPath = ''
  83. const result = await runLoaderSmoke({
  84. label: 'semantic checkpoint headless stream-json snapshot',
  85. tempDirPrefix: 'dsh-semantic-snapshot-',
  86. binScript,
  87. configPath,
  88. binArgs: ['--config', configPath, '--output-format', 'stream-json', task],
  89. tsconfigPath,
  90. env: {
  91. DSH_SNAPSHOT_FILE: replayFixture,
  92. DSH_SNAPSHOT_OVERRIDE: replayOverride,
  93. },
  94. prepare: async (runCwd) => {
  95. cwd = runCwd
  96. sessionPath = await seedInterruptedSession(join(runCwd, '.sessions'), runCwd)
  97. },
  98. inspect: async () => {
  99. const normalization: NormalizeContext = { sessionIds: [sessionId], cwd }
  100. const session = scrubRequestHeaders(normalizeSessionLog(await readFile(sessionPath, 'utf8'), normalization))
  101. if (refreshing) await writeFile(sessionExpected, session)
  102. expect(session).toBe(await readFile(sessionExpected, 'utf8'))
  103. expect(session).toContain('TOOL_OUTCOME_UNKNOWN')
  104. expect(session).toContain('Do not retry blindly.')
  105. },
  106. })
  107. expect(result.stderr).toBe('')
  108. const records = result.stdout.trimEnd().split('\n').map(line => JSON.parse(line) as Record<string, unknown>)
  109. expect(records.at(-1)).toMatchObject({
  110. type: 'result',
  111. sessionId,
  112. output: 'I will verify the external state before deciding whether to retry the side-effecting operation.',
  113. })
  114. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  115. })