session-fixture-layout.spec.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { resolve } from 'node:path'
  2. import { describe, expect, it } from 'vitest'
  3. import { createAssistantMessage } from '@deepseek-ai/dsh-llm'
  4. import { SESSION_FORMAT_VERSION, SessionSeq, type SessionEvent } from '@deepseek-ai/dsh-session'
  5. import { parseSessionLog } from '@deepseek-ai/dsh-llm-replay'
  6. import {
  7. canonicalSessionFixture,
  8. inspectSessionFixtureLayouts,
  9. isPhysicalSessionFixture,
  10. } from './session-fixture-layout.ts'
  11. const HEADER = ` {"type":"session","version":${SESSION_FORMAT_VERSION},"id":"fixture","createdAt":1,"isSeeded":false,"delegationDepth":0} `
  12. const root = resolve(import.meta.dirname, '..')
  13. const FIXTURE_MESSAGE = createAssistantMessage({
  14. content: [{ type: 'text', text: 'part-0part-1part-2part-3' }],
  15. source: { provider: 'mock', model: 'mock' },
  16. })
  17. const FIXTURE_STREAM: SessionEvent<'assistant/message'>['data']['stream'] = [
  18. {
  19. type: 'text-chunks',
  20. time0: 10,
  21. index: 0,
  22. dt: [1, 1, 1],
  23. texts: ['part-0', 'part-1', 'part-2', 'part-3'],
  24. },
  25. { type: 'chunk', time: 14, chunk: { type: 'finish', reason: { kind: 'stop' } } },
  26. ]
  27. function assistantMessage(): SessionEvent<'assistant/message'> {
  28. return {
  29. type: 'assistant/message',
  30. seq: SessionSeq(2),
  31. time: 14,
  32. data: {
  33. turn: 1,
  34. step: 1,
  35. message: FIXTURE_MESSAGE,
  36. stream: FIXTURE_STREAM,
  37. },
  38. surfaceOp: 'append',
  39. }
  40. }
  41. function fixtureEvents(): SessionEvent[] {
  42. return [
  43. { type: 'turn/start', seq: SessionSeq(0), time: 1, data: { turn: 1 } },
  44. { type: 'step/start', seq: SessionSeq(1), time: 2, data: { turn: 1, step: 1 } },
  45. assistantMessage(),
  46. ]
  47. }
  48. function unpackedFixture(): string {
  49. return [HEADER, ...fixtureEvents().map(event => JSON.stringify(event)), ''].join('\n')
  50. }
  51. function decodedBody(content: string): SessionEvent[] {
  52. return parseSessionLog(content)
  53. }
  54. describe('canonicalSessionFixture', () => {
  55. it('preserves the header line and nested compact stream losslessly', () => {
  56. const canonical = canonicalSessionFixture(unpackedFixture(), 'fixture.jsonl')
  57. expect(canonical).toBeDefined()
  58. expect(canonical?.split('\n')[0]).toBe(HEADER)
  59. const message = canonical?.split('\n')
  60. .map(line => JSON.parse(line || '{}') as Record<string, unknown>)
  61. .find(record => record.type === 'assistant/message')
  62. expect(message).toMatchObject({
  63. type: 'assistant/message',
  64. data: {
  65. stream: FIXTURE_STREAM,
  66. },
  67. })
  68. expect(message).not.toHaveProperty('seq')
  69. expect(message).not.toHaveProperty('time')
  70. expect(decodedBody(canonical ?? '').map(({ seq: _seq, time: _time, ...event }) => event))
  71. .toStrictEqual(fixtureEvents().map(({ seq: _seq, time: _time, ...event }) => event))
  72. })
  73. it('ignores JSONL whose first record is not a session header', () => {
  74. expect(canonicalSessionFixture('{"type":"session_event"}\n{"value":1}\n')).toBeUndefined()
  75. })
  76. it('is idempotent for an already packed fixture', () => {
  77. const packed = canonicalSessionFixture(unpackedFixture())
  78. expect(packed).toBeDefined()
  79. expect(canonicalSessionFixture(packed ?? '')).toBe(packed)
  80. })
  81. it('is idempotent for an already projected fixture', () => {
  82. const projected = [
  83. HEADER,
  84. '{"type":"turn/start","data":{"turn":1}}',
  85. '',
  86. ].join('\n')
  87. expect(canonicalSessionFixture(projected)).toBe(projected)
  88. })
  89. it('preserves owner-restored request-header tokens in current projected fixtures', () => {
  90. const projected = [
  91. HEADER,
  92. '{"type":"turn/start","data":{"turn":1}}',
  93. '{"type":"request/header","data":{"header":{"config":{"provider":"mock","model":"mock"},"system":"{{system}}","tools":"{{tools}}"},"reason":"initial"}}',
  94. '',
  95. ].join('\n')
  96. expect(canonicalSessionFixture(projected)).toBe(projected)
  97. })
  98. it.each([0, 1, 2])('preserves physically valid v%i bytes without requiring migration to current', (version) => {
  99. const header = { type: 'session', version, id: 'historical', createdAt: 1, delegationDepth: 0, ...(version === 2 ? { isSeeded: false } : {}) }
  100. const content = [
  101. JSON.stringify(header),
  102. JSON.stringify({ type: 'user/message', data: { role: 'user', id: 'historical-user', source: { kind: 'user' }, content: [] }, surfaceOp: 'append' }),
  103. '',
  104. ].join('\n')
  105. expect(canonicalSessionFixture(content)).toBe(content)
  106. })
  107. it.each([0, 1, 2])('rejects v%i sequence gaps and invalid provenance ranges with source line diagnostics', (version) => {
  108. const header = JSON.stringify({ type: 'session', version, id: 'historical', createdAt: 1, delegationDepth: 0, ...(version === 2 ? { isSeeded: false } : {}) })
  109. expect(() => canonicalSessionFixture(`${header}\n{"type":"feedback/record","seq":3,"data":{"text":"gap"}}\n`, 'gap.jsonl'))
  110. .toThrow(/gap\.jsonl: session snapshot line 2:.*seq/)
  111. expect(() => canonicalSessionFixture(`${header}\n{"type":"feedback/record","data":{},"sourceEventSeqs":[[2,0]]}\n`, 'range.jsonl'))
  112. .toThrow(/range\.jsonl: session snapshot line 2:/)
  113. })
  114. it.each([0, 1, 2])('finalizes the v%i source inherited cut', (version) => {
  115. const header = { type: 'session', version, id: 'historical', createdAt: 1, delegationDepth: 0, ...(version === 2 ? { isSeeded: true } : { seedLength: 1 }) }
  116. expect(() => canonicalSessionFixture(`${JSON.stringify(header)}\n`, 'cut.jsonl'))
  117. .toThrow(/cut\.jsonl: session snapshot line 1:.*(?:inherited|seed)/)
  118. })
  119. it('refuses unsupported generation headers', () => {
  120. const header = { type: 'session', version: 99, id: 'future', createdAt: 1, isSeeded: false, delegationDepth: 0 }
  121. expect(() => canonicalSessionFixture(`${JSON.stringify(header)}\n`, 'future.jsonl'))
  122. .toThrow(/future\.jsonl: session snapshot line 1:.*99/)
  123. })
  124. it('fails loud on malformed records after a session header', () => {
  125. expect(() => canonicalSessionFixture(`${HEADER}\n{not-json}\n`, 'broken.jsonl'))
  126. .toThrow(/broken\.jsonl: session snapshot line 2 contains invalid JSON/)
  127. })
  128. it('labels malformed packed rows with the fixture path and line', () => {
  129. const releasedHeader = '{"type":"session","version":0,"id":"fixture","createdAt":1,"delegationDepth":0}'
  130. expect(() => canonicalSessionFixture(`${releasedHeader}\n{"type":"text-chunks"}\n`, 'broken.jsonl'))
  131. .toThrow(/broken\.jsonl: session snapshot line 2: released text-chunks row 0 lacks required member "data"/)
  132. })
  133. })
  134. describe('isPhysicalSessionFixture', () => {
  135. it('recognizes fixtures that preserve physical persistence encoding', () => {
  136. expect(isPhysicalSessionFixture(
  137. 'packages/experimental/webworker-runtime/tests/fixtures/vfs-example/home/sessions/--dsh-workspace--/main/session.jsonl',
  138. )).toBe(true)
  139. expect(isPhysicalSessionFixture(
  140. 'packages/experimental/webworker-runtime/tests/fixtures/vfs-example/home/sessions/--dsh-workspace--/main/session.v1.jsonl',
  141. )).toBe(true)
  142. expect(isPhysicalSessionFixture(
  143. 'scripts/snapshots/python-sdk-single-exe/advanced/session.1.jsonl',
  144. )).toBe(true)
  145. expect(isPhysicalSessionFixture(
  146. 'scripts/snapshots/python-sdk-single-exe/advanced/session.1.v1.jsonl',
  147. )).toBe(true)
  148. expect(isPhysicalSessionFixture(
  149. 'scripts/snapshots/python-sdk-single-exe/advanced/session.jsonl',
  150. )).toBe(true)
  151. expect(isPhysicalSessionFixture(
  152. 'scripts/snapshots/python-sdk-single-exe/restart/session.2.jsonl',
  153. )).toBe(true)
  154. expect(isPhysicalSessionFixture(
  155. 'packages/experimental/webworker-runtime/tests/fixtures/vfs-example/home/sessions/README.jsonl',
  156. )).toBe(false)
  157. expect(isPhysicalSessionFixture(
  158. 'scripts/snapshots/python-sdk-single-exe/advanced/requests.jsonl',
  159. )).toBe(false)
  160. expect(isPhysicalSessionFixture('apps/web/tests/snapshots/example/session.jsonl')).toBe(false)
  161. })
  162. })
  163. it('keeps every session-format JSONL fixture projected into canonical event layout', () => {
  164. const nonCanonical = inspectSessionFixtureLayouts(root)
  165. .filter(fixture => fixture.source !== fixture.canonical)
  166. .map(fixture => fixture.path)
  167. expect(
  168. nonCanonical,
  169. 'Run `pnpm run migrate:packed-session-fixtures` and commit the mechanical fixture rewrite.',
  170. ).toEqual([])
  171. })