jsonl.spec.ts 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { appendFile, mkdtemp, mkdir, rm, readFile, writeFile, readdir, stat, symlink } from 'node:fs/promises'
  4. import { tmpdir } from 'node:os'
  5. import { isAbsolute, join, relative, resolve } from 'node:path'
  6. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  7. import type { Session, SessionEvent, SessionHeader } from '@deepseek-ai/dsh-session'
  8. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  9. import {
  10. encodeSegment, eventLines, logPath, projectDir, projectKey, scanLog, sessionDir, toHeaderLine,
  11. } from '../src/format.ts'
  12. import { runPersistenceContract, meta, oneTurnLog, appendLog } from '../../session-persistence/tests/contract.ts'
  13. import { runCoordinatorContract, type CoordinatorFixture } from '../../session-persistence/tests/coordinator-contract.ts'
  14. let root: string
  15. const dirs: string[] = []
  16. type MutableSessionHeader = { -readonly [K in keyof SessionHeader]: SessionHeader[K] }
  17. /** Test-only mutable view used to verify that backends detach returned/caller metadata. */
  18. function mutableHeader(header: SessionHeader): MutableSessionHeader {
  19. return header
  20. }
  21. /** Rewrite only a stored header while preserving every event byte below it. */
  22. async function rewriteHeader(path: string, update: (header: Record<string, unknown>) => void): Promise<void> {
  23. const lines = (await readFile(path, 'utf8')).split('\n')
  24. const header = JSON.parse(lines[0] as string) as Record<string, unknown>
  25. update(header)
  26. lines[0] = JSON.stringify(header)
  27. await writeFile(path, lines.join('\n'))
  28. }
  29. async function expectFlushError(promise: Promise<unknown>, message: RegExp): Promise<void> {
  30. try {
  31. await promise
  32. } catch (error) {
  33. expect(error).toBeInstanceOf(Error)
  34. expect((error as Error).message).toMatch(message)
  35. return
  36. }
  37. throw new Error('expected flush to reject')
  38. }
  39. async function freshRoot(): Promise<string> {
  40. const dir = await mkdtemp(join(tmpdir(), 'dsh-jsonl-'))
  41. dirs.push(dir)
  42. return dir
  43. }
  44. function rawLogPath(root: string, cwd: string | undefined, id: SessionId): string {
  45. return logPath(root, cwd, id, 'none')
  46. }
  47. afterEach(async () => {
  48. vi.restoreAllMocks()
  49. for (const d of dirs.splice(0)) await rm(d, { recursive: true, force: true })
  50. })
  51. function appendClosedTurn(session: Session): void {
  52. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  53. session.append('user/message', {
  54. content: [{ type: 'text', text: 'hello' }],
  55. source: { kind: 'user' },
  56. }, { surfaceOp: 'append' })
  57. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  58. }
  59. // Run the shared backend contract against the real JSONL backend.
  60. runPersistenceContract('jsonl-none', async () => {
  61. const dir = await mkdtemp(join(tmpdir(), 'dsh-jsonl-'))
  62. const ctx = new Context()
  63. await ctx.plugin(SessionStore)
  64. const fiber = await ctx.plugin(SessionPersistenceJsonl, { root: dir, compression: 'none' })
  65. return {
  66. persistence: ctx.sessionPersistence,
  67. dispose: async () => {
  68. await fiber.dispose()
  69. await rm(dir, { recursive: true, force: true })
  70. },
  71. }
  72. })
  73. // Two mounts share this temp root to exercise reload. `corruptTail` appends a partial,
  74. // newline-less fragment past the committed region so coordinator repair runs on real file bytes.
  75. runCoordinatorContract('jsonl-none', async (): Promise<CoordinatorFixture> => {
  76. const dir = await mkdtemp(join(tmpdir(), 'dsh-jsonl-coord-'))
  77. return {
  78. mount: async (ctx) => {
  79. const fiber = await ctx.plugin(SessionPersistenceJsonl, { root: dir, compression: 'none' })
  80. return fiber
  81. },
  82. corruptTail: async (id, cwd) => {
  83. // A half-written record with no trailing newline: scanLog treats it as an
  84. // uncommitted crash fragment and reports committedBytes < byteLength, so
  85. // the coordinator sees a tornMarker to truncate.
  86. await appendFile(rawLogPath(dir, cwd, id), '{"type":"assistant/chunk","seq":8,"ti')
  87. },
  88. cleanup: async () => { await rm(dir, { recursive: true, force: true }) },
  89. }
  90. })
  91. describe('SessionPersistenceJsonl: format helpers', () => {
  92. it('encodeSegment neutralizes traversal, separators, and absolute paths', () => {
  93. expect(encodeSegment('..')).toBe('~002E~002E')
  94. expect(encodeSegment('.')).toBe('~002E')
  95. expect(encodeSegment('a/b')).toBe('a~002Fb')
  96. expect(encodeSegment('/etc/passwd')).toBe('~002Fetc~002Fpasswd')
  97. expect(encodeSegment('a\u0000b')).toBe('a~0000b')
  98. expect(encodeSegment('plain-ID_1.2')).toBe('plain-ID_1.2') // safe chars pass through
  99. expect(encodeSegment('a~b')).toBe('a~007Eb') // ~ itself is escaped
  100. })
  101. it('encodeSegment is injective over UTF-16, incl. lone surrogates', () => {
  102. // Distinct lone surrogates must NOT collide (Buffer.from would normalize
  103. // both to U+FFFD; code-unit escaping keeps them distinct).
  104. const hi = encodeSegment(String.fromCharCode(0xD800))
  105. const lo = encodeSegment(String.fromCharCode(0xDC00))
  106. expect(hi).toBe('~D800')
  107. expect(lo).toBe('~DC00')
  108. expect(hi).not.toBe(lo)
  109. // A literal "~002F" input cannot collide with the encoding of "/".
  110. expect(encodeSegment('~002F')).not.toBe(encodeSegment('/'))
  111. })
  112. it('encodeSegment rejects an empty id', () => {
  113. expect(() => encodeSegment('')).toThrow(/empty/)
  114. })
  115. it('projectKey normalizes project paths into bounded readable names', () => {
  116. expect(projectKey('/Users/qyj/work/deepseek-harness')).toBe('--Users-qyj-work-deepseek-harness--')
  117. expect(projectKey('/a/b-c')).toBe(projectKey('/a-b/c'))
  118. expect(projectKey('C:\\work\\agent')).toBe('--C-work-agent--')
  119. expect(projectKey('/开发/~agent')).toBe('--~5F00~53D1-~007Eagent--')
  120. expect(projectKey('/')).toBe('--root--')
  121. expect(projectKey('/' + 'x'.repeat(1_000))).toHaveLength(255)
  122. expect(() => projectKey('')).toThrow(/empty project path/)
  123. })
  124. it('resolves a relative custom root before locating a session', async () => {
  125. const absoluteRoot = await freshRoot()
  126. const ctx = new Context()
  127. await ctx.plugin(SessionStore)
  128. const fiber = await ctx.plugin(SessionPersistenceJsonl, {
  129. root: relative(process.cwd(), absoluteRoot),
  130. compression: 'none',
  131. })
  132. const m = meta('relative-location', '/work')
  133. expect(ctx.sessionPersistence.locate(m)).toEqual({
  134. kind: 'jsonl',
  135. path: rawLogPath(resolve(absoluteRoot), '/work', m.id),
  136. })
  137. await fiber.dispose()
  138. })
  139. })
  140. describe('SessionPersistenceJsonl: durability and crash semantics', () => {
  141. let ctx: Context
  142. beforeEach(async () => {
  143. root = await freshRoot()
  144. ctx = new Context()
  145. await ctx.plugin(SessionStore)
  146. await ctx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  147. })
  148. afterEach(async () => { await ctx.fiber.dispose() })
  149. it('lazy materialization: create() writes no file until the first append', async () => {
  150. const m = meta('lazy', '/work')
  151. const location = ctx.sessionPersistence.locate(m)
  152. expect(location).toEqual({ kind: 'jsonl', path: rawLogPath(root, '/work', m.id) })
  153. expect(isAbsolute(location!.path)).toBe(true)
  154. await ctx.sessionPersistence.create(m)
  155. // locate() is a pure target-path calculation: neither it nor create()
  156. // materializes a file before the first append.
  157. const dir = sessionDir(root, '/work', m.id)
  158. await expect(stat(rawLogPath(root, '/work', m.id))).rejects.toThrow()
  159. expect((await ctx.sessionPersistence.list()).map(h => h.id)).not.toContain(m.id)
  160. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  161. // now materialized
  162. expect((await stat(dir)).isDirectory()).toBe(true)
  163. expect((await stat(rawLogPath(root, '/work', m.id))).isFile()).toBe(true)
  164. expect((await ctx.sessionPersistence.list()).map(h => h.id)).toContain(m.id)
  165. })
  166. it('keeps the same location on resume and gives a fork its own location', async () => {
  167. const parent = meta('location-parent', '/work')
  168. const parentLocation = ctx.sessionPersistence.locate(parent)
  169. await ctx.sessionPersistence.create(parent)
  170. await ctx.sessionPersistence.append(parent.id, oneTurnLog())
  171. const loaded = await ctx.sessionPersistence.load(parent.id)
  172. expect(ctx.sessionPersistence.locate(loaded.meta)).toEqual(parentLocation)
  173. const child = {
  174. ...loaded.meta,
  175. id: SessionId('location-child'),
  176. parentSession: parent.id,
  177. seedLength: loaded.events.length,
  178. }
  179. const childLocation = ctx.sessionPersistence.locate(child)
  180. expect(childLocation?.path).not.toBe(parentLocation?.path)
  181. expect(childLocation).toEqual({ kind: 'jsonl', path: rawLogPath(root, '/work', child.id) })
  182. })
  183. it('round-trip is byte-identical (incl. assistant/chunk verbatim)', async () => {
  184. const m = meta('chunks')
  185. const log: SessionEvent[] = [
  186. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  187. { type: 'step/start', seq: 1, time: 2, data: { turn: 1, step: 1 } },
  188. { type: 'assistant/chunk', seq: 2, time: 3, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'he' } } },
  189. { type: 'assistant/chunk', seq: 3, time: 4, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'llo' } } },
  190. { type: 'assistant/message', seq: 4, time: 5, data: { turn: 1, step: 1, content: [{ type: 'text', text: 'hello' }], provenance: { provider: 'mock', model: 'mock' } }, surfaceOp: 'append', sourceEventSeqs: [2, 3] },
  191. { type: 'step/end', seq: 5, time: 6, data: { turn: 1, step: 1 } },
  192. { type: 'turn/end', seq: 6, time: 7, data: { turn: 1, reason: { kind: 'completed' } } },
  193. ]
  194. await ctx.sessionPersistence.create(m)
  195. await ctx.sessionPersistence.append(m.id, log)
  196. const loaded = await ctx.sessionPersistence.load(m.id)
  197. expect(loaded.events).toEqual(log) // chunks preserved, contiguous seqs
  198. })
  199. it('source-qualifies revisions across roots while preserving same-log reopen identity', async () => {
  200. const m = meta('revision-source')
  201. await ctx.sessionPersistence.create(m)
  202. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  203. const revision = (await ctx.sessionPersistence.listSnapshots())[0]?.revision
  204. const reopenedCtx = new Context()
  205. await reopenedCtx.plugin(SessionStore)
  206. await reopenedCtx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  207. expect((await reopenedCtx.sessionPersistence.listSnapshots())[0]?.revision).toBe(revision)
  208. const otherRoot = await freshRoot()
  209. const otherCtx = new Context()
  210. await otherCtx.plugin(SessionStore)
  211. await otherCtx.plugin(SessionPersistenceJsonl, { root: otherRoot, compression: 'none' })
  212. await otherCtx.sessionPersistence.create(m)
  213. await otherCtx.sessionPersistence.append(m.id, oneTurnLog())
  214. expect((await otherCtx.sessionPersistence.listSnapshots())[0]?.revision).not.toBe(revision)
  215. await reopenedCtx.fiber.dispose()
  216. await otherCtx.fiber.dispose()
  217. })
  218. it('omits a snapshot artifact removed after discovery', async () => {
  219. const m = meta('vanishing-snapshot')
  220. await ctx.sessionPersistence.create(m)
  221. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  222. const persistence = ctx.sessionPersistence as unknown as {
  223. listArtifacts(): Promise<Array<{ header: SessionHeader; path: string }>>
  224. }
  225. const listArtifacts = persistence.listArtifacts.bind(persistence)
  226. const discovery = vi.spyOn(persistence, 'listArtifacts').mockImplementation(async () => {
  227. const artifacts = await listArtifacts()
  228. await rm(artifacts[0]!.path)
  229. return artifacts
  230. })
  231. await expect(ctx.sessionPersistence.listSnapshots()).resolves.toEqual([])
  232. discovery.mockRestore()
  233. })
  234. it('surfaces non-ENOENT snapshot stat failures after discovery', async () => {
  235. const persistence = ctx.sessionPersistence as unknown as {
  236. listArtifacts(): Promise<Array<{ header: SessionHeader; path: string }>>
  237. }
  238. const discovery = vi.spyOn(persistence, 'listArtifacts').mockResolvedValue([{
  239. header: meta('snapshot-stat-failure'),
  240. path: `${root}\0snapshot-stat-failure`,
  241. }])
  242. await expect(ctx.sessionPersistence.listSnapshots()).rejects.toThrow(/null bytes/)
  243. discovery.mockRestore()
  244. })
  245. it('forwards snapshot-list cancellation and awaits in-flight discovery cleanup', async () => {
  246. const persistence = ctx.sessionPersistence as unknown as {
  247. listArtifacts(signal?: AbortSignal): Promise<Array<{ header: SessionHeader; path: string }>>
  248. }
  249. const started = Promise.withResolvers<AbortSignal>()
  250. const cleanup = Promise.withResolvers<undefined>()
  251. vi.spyOn(persistence, 'listArtifacts').mockImplementation(async (signal) => {
  252. if (signal === undefined) throw new Error('expected snapshot-list signal')
  253. started.resolve(signal)
  254. await cleanup.promise
  255. return []
  256. })
  257. const reason = new Error('JSONL snapshot discovery cancelled')
  258. const controller = new AbortController()
  259. const pending = ctx.sessionPersistence.listSnapshots(controller.signal)
  260. expect(await started.promise).toBe(controller.signal)
  261. let settled = false
  262. void pending.then(
  263. () => { settled = true },
  264. () => { settled = true },
  265. )
  266. controller.abort(reason)
  267. await Promise.resolve()
  268. expect(settled).toBe(false)
  269. cleanup.resolve(undefined)
  270. await expect(pending).rejects.toBe(reason)
  271. })
  272. it('checks cancellation after an uncancellable snapshot stat settles', async () => {
  273. const m = meta('snapshot-stat-cancellation')
  274. await ctx.sessionPersistence.create(m)
  275. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  276. const persistence = ctx.sessionPersistence as unknown as {
  277. listArtifacts(signal?: AbortSignal): Promise<Array<{ header: SessionHeader; path: string }>>
  278. }
  279. const discovery = vi.spyOn(persistence, 'listArtifacts').mockResolvedValue([{
  280. header: m,
  281. path: rawLogPath(root, m.cwd, m.id),
  282. }])
  283. const reason = new Error('JSONL snapshot stat cancelled')
  284. const controller = new AbortController()
  285. const pending = ctx.sessionPersistence.listSnapshots(controller.signal)
  286. queueMicrotask(() => { controller.abort(reason) })
  287. await expect(pending).rejects.toBe(reason)
  288. expect(discovery).toHaveBeenCalledWith(controller.signal)
  289. })
  290. it('rejects a stored v0 log containing a legacy request/header-delta event', async () => {
  291. const m = meta('legacy-header-delta', '/legacy')
  292. const path = rawLogPath(root, m.cwd, m.id)
  293. await mkdir(sessionDir(root, m.cwd, m.id), { recursive: true })
  294. await writeFile(path, [
  295. JSON.stringify(toHeaderLine(m)),
  296. JSON.stringify({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  297. JSON.stringify({ type: 'request/header-delta', seq: 1, time: 2, data: { config: { model: 'legacy' } } }),
  298. JSON.stringify({ type: 'turn/end', seq: 2, time: 3, data: { turn: 1, reason: { kind: 'completed' } } }),
  299. '',
  300. ].join('\n'))
  301. await expect(ctx.sessionPersistence.load(m.id)).rejects.toThrow(/unsupported legacy request\/header-delta event at seq 1/)
  302. })
  303. it('rejects a stored v0 full header carrying the legacy fallback reason', async () => {
  304. const m = meta('legacy-header-fallback', '/legacy')
  305. const path = rawLogPath(root, m.cwd, m.id)
  306. await mkdir(sessionDir(root, m.cwd, m.id), { recursive: true })
  307. await writeFile(path, [
  308. JSON.stringify(toHeaderLine(m)),
  309. JSON.stringify({
  310. type: 'request/header',
  311. seq: 0,
  312. time: 1,
  313. data: { header: { config: { model: 'legacy' } }, reason: 'fallback' },
  314. }),
  315. '',
  316. ].join('\n'))
  317. await expect(ctx.sessionPersistence.load(m.id))
  318. .rejects.toThrow(/unsupported legacy request\/header reason "fallback" at seq 0/)
  319. })
  320. it('persists a forked child seed through the existing session write path', async () => {
  321. const source = ctx.sessions.create(SessionId('persist-parent'), { meta: { cwd: '/workspace' } })
  322. appendClosedTurn(source)
  323. const child = ctx.sessions.fork(source, undefined, SessionId('persist-child'))
  324. await ctx.sessions.flush(child)
  325. const loaded = await ctx.sessionPersistence.load(child.id)
  326. expect(loaded.events).toEqual(source.events)
  327. expect(loaded.meta).toMatchObject({
  328. id: SessionId('persist-child'),
  329. cwd: '/workspace',
  330. parentSession: SessionId('persist-parent'),
  331. seedLength: source.events.length,
  332. })
  333. })
  334. it('crash recovery: load preserves the interrupted turn and closes it with a synthetic turn/end {interrupted}', async () => {
  335. const m = meta('crash', '/proj')
  336. await ctx.sessionPersistence.create(m)
  337. await ctx.sessionPersistence.append(m.id, oneTurnLog()) // seqs 0..5, turn/end at 5
  338. // Simulate a crash mid-second-turn: append raw lines that are NOT closed by
  339. // a turn/end (turn/start + step/start are fully written), plus a final
  340. // partial line with no newline (a torn fragment never fully flushed).
  341. const path = rawLogPath(root, '/proj', m.id)
  342. await writeFile(path, [
  343. JSON.stringify({ type: 'turn/start', seq: 6, time: 8, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  344. JSON.stringify({ type: 'step/start', seq: 7, time: 9, data: { turn: 2, step: 1 } }),
  345. '{"type":"assistant/chunk","seq":8,"ti', // truncated partial line (no newline)
  346. ].join('\n'), { flag: 'a' })
  347. // load PRESERVES the interrupted turn's real events (turn/start 6, step/start
  348. // 7) — a turn can be huge, so they must not be truncated — and durably closes
  349. // the orphaned turn with synthetic step/end (8) + turn/end {interrupted} (9).
  350. const loaded = await ctx.sessionPersistence.load(m.id)
  351. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  352. const last = loaded.events.at(-1)!
  353. expect(last.type === 'turn/end' && last.data.reason).toEqual({ kind: 'interrupted' })
  354. const stepEnd = loaded.events[8]!
  355. expect(stepEnd.type).toBe('step/end')
  356. // the torn seq-8 chunk fragment did not survive
  357. expect(loaded.events.some(e => e.type === 'assistant/chunk' && e.seq === 8)).toBe(false)
  358. // The next append continues at seq 10 (the balanced length).
  359. const turn3 = [
  360. { type: 'turn/start', seq: 10, time: 11, data: { turn: 3, trigger: { kind: 'message', source: { kind: 'user' } } } },
  361. { type: 'turn/end', seq: 11, time: 12, data: { turn: 3, reason: { kind: 'completed' } } },
  362. ] as SessionEvent[]
  363. await ctx.sessionPersistence.append(m.id, turn3)
  364. const reloaded = await ctx.sessionPersistence.load(m.id)
  365. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
  366. })
  367. it('committed events are never rewritten: only the crash tail is repaired', async () => {
  368. const m = meta('append-only')
  369. await ctx.sessionPersistence.create(m)
  370. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  371. const before = await readFile(rawLogPath(root, undefined, m.id), 'utf8')
  372. const committedPrefix = before // the whole committed log
  373. // A crash tail then a repair-append.
  374. await writeFile(rawLogPath(root, undefined, m.id), '\n{"partial', { flag: 'a' })
  375. await ctx.sessionPersistence.load(m.id)
  376. await ctx.sessionPersistence.append(m.id, [
  377. { type: 'turn/start', seq: 6, time: 9, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  378. { type: 'turn/end', seq: 7, time: 10, data: { turn: 2, reason: { kind: 'completed' } } },
  379. ] as SessionEvent[])
  380. const after = await readFile(rawLogPath(root, undefined, m.id), 'utf8')
  381. // the committed prefix is byte-for-byte intact at the head of the file
  382. expect(after.startsWith(committedPrefix)).toBe(true)
  383. })
  384. it('a failed appendLines truncates partial bytes so a retry has no seq gap', async () => {
  385. const m = meta('truncate-retry')
  386. await ctx.sessionPersistence.create(m)
  387. await ctx.sessionPersistence.append(m.id, oneTurnLog()) // materialized, seqs 0..5
  388. const sizeBefore = (await stat(rawLogPath(root, undefined, m.id))).size
  389. // Force the NEXT fsync (inside appendLines) to fail once, AFTER writeFile
  390. // has already put bytes on disk — simulating an ENOSPC/fsync error
  391. // mid-append. The recovery truncate() also fsyncs, so allow that one.
  392. const handle = await (await import('node:fs/promises')).open(rawLogPath(root, undefined, m.id), 'r')
  393. const proto = Object.getPrototypeOf(handle) as { sync: () => Promise<void> }
  394. await handle.close()
  395. const realSync = proto.sync
  396. let failed = false
  397. const spy = vi.spyOn(proto, 'sync').mockImplementation(async function (this: unknown) {
  398. if (!failed) { failed = true; throw new Error('simulated fsync ENOSPC') }
  399. return realSync.call(this)
  400. })
  401. const turn2 = [
  402. { type: 'turn/start', seq: 6, time: 9, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  403. { type: 'turn/end', seq: 7, time: 10, data: { turn: 2, reason: { kind: 'completed' } } },
  404. ] as SessionEvent[]
  405. // The append rejects, but the partial bytes are truncated back: the file is
  406. // its pre-append size and the cursor is unchanged.
  407. await expect(ctx.sessionPersistence.append(m.id, turn2)).rejects.toThrow(/ENOSPC/)
  408. expect((await stat(rawLogPath(root, undefined, m.id))).size).toBe(sizeBefore)
  409. spy.mockRestore()
  410. // The retry now succeeds with NO seq gap — the log is contiguous 0..7.
  411. await ctx.sessionPersistence.append(m.id, turn2)
  412. const loaded = await ctx.sessionPersistence.load(m.id)
  413. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  414. })
  415. it('reports both the append failure and a failed rollback', async () => {
  416. const m = meta('rollback-failure')
  417. await ctx.sessionPersistence.create(m)
  418. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  419. const path = rawLogPath(root, undefined, m.id)
  420. const handle = await (await import('node:fs/promises')).open(path, 'r')
  421. const proto = Object.getPrototypeOf(handle) as { sync: () => Promise<void> }
  422. await handle.close()
  423. const realSync = proto.sync
  424. let failed = false
  425. const syncSpy = vi.spyOn(proto, 'sync').mockImplementation(async function (this: unknown) {
  426. if (!failed) { failed = true; throw new Error('simulated append fsync failure') }
  427. return realSync.call(this)
  428. })
  429. const backend = ctx.sessionPersistence as unknown as {
  430. rollbackAppend: (path: string, size: number) => Promise<void>
  431. }
  432. const realRollback = backend.rollbackAppend.bind(backend)
  433. backend.rollbackAppend = () => Promise.reject(new Error('simulated rollback failure'))
  434. try {
  435. await ctx.sessionPersistence.append(m.id, [
  436. { type: 'turn/start', seq: 6, time: 9, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  437. ] as SessionEvent[])
  438. throw new Error('expected append to reject')
  439. } catch (error) {
  440. expect(error).toBeInstanceOf(AggregateError)
  441. const aggregate = error as AggregateError
  442. expect(aggregate.message).toContain(`failed to roll back append to "${path}"`)
  443. expect(aggregate.errors).toHaveLength(2)
  444. expect(aggregate.errors[0]).toMatchObject({ message: 'simulated append fsync failure' })
  445. expect(aggregate.errors[1]).toMatchObject({ message: 'simulated rollback failure' })
  446. } finally {
  447. backend.rollbackAppend = realRollback
  448. syncSpy.mockRestore()
  449. }
  450. })
  451. it('load returns a meta copy: mutating it does not corrupt backend pathing', async () => {
  452. const m = meta('meta-copy', '/proj')
  453. await ctx.sessionPersistence.create(m)
  454. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  455. const loaded = await ctx.sessionPersistence.load(m.id)
  456. // A consumer mutates the returned meta's cwd. The backend's stored pathing
  457. // metadata must be unaffected, so a later append still finds the right log.
  458. mutableHeader(loaded.meta).cwd = '/evil'
  459. await ctx.sessionPersistence.append(m.id, [
  460. { type: 'turn/start', seq: 6, time: 9, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  461. { type: 'turn/end', seq: 7, time: 10, data: { turn: 2, reason: { kind: 'completed' } } },
  462. ] as SessionEvent[])
  463. // The append landed in the ORIGINAL /proj log, not beside an /evil path.
  464. const reloaded = await ctx.sessionPersistence.load(m.id)
  465. expect(reloaded.meta.cwd).toBe('/proj')
  466. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  467. })
  468. it('rejects a mismatched header before repairing either session log', async () => {
  469. const a = meta('identity-a', '/same')
  470. const b = meta('identity-b', '/same')
  471. await ctx.sessionPersistence.create(a)
  472. await ctx.sessionPersistence.append(a.id, [{
  473. type: 'turn/start',
  474. seq: 0,
  475. time: 1,
  476. data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } },
  477. }])
  478. await ctx.sessionPersistence.create(b)
  479. await ctx.sessionPersistence.append(b.id, oneTurnLog())
  480. const aPath = rawLogPath(root, a.cwd, a.id)
  481. const bPath = rawLogPath(root, b.cwd, b.id)
  482. await rewriteHeader(aPath, (header) => { header.id = b.id })
  483. const beforeA = await readFile(aPath)
  484. const beforeB = await readFile(bPath)
  485. await expect(ctx.sessionPersistence.load(a.id))
  486. .rejects.toThrow(/requested id "identity-a" does not match header id "identity-b"/)
  487. expect(await readFile(aPath)).toEqual(beforeA)
  488. expect(await readFile(bPath)).toEqual(beforeB)
  489. })
  490. it('rejects a re-append of an already-stored seq', async () => {
  491. const m = meta('reappend')
  492. await ctx.sessionPersistence.create(m)
  493. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  494. await expect(ctx.sessionPersistence.append(m.id, oneTurnLog())).rejects.toThrow(/seq mismatch/)
  495. })
  496. it('path-traversal session ids are neutralized (no escape from root)', async () => {
  497. const evil = SessionId('../../etc/pwn')
  498. const m = { version: 0, id: evil, createdAt: 1 }
  499. await ctx.sessionPersistence.create(m)
  500. await ctx.sessionPersistence.append(evil, oneTurnLog())
  501. // The file lives UNDER root, not at ../../etc.
  502. const all: string[] = []
  503. async function walk(dir: string): Promise<void> {
  504. for (const e of await readdir(dir, { withFileTypes: true })) {
  505. const p = join(dir, e.name)
  506. if (e.isDirectory()) await walk(p)
  507. else all.push(p)
  508. }
  509. }
  510. await walk(root)
  511. expect(all.length).toBeGreaterThan(0)
  512. expect(all.every(p => p.startsWith(root))).toBe(true)
  513. })
  514. })
  515. describe('SessionPersistenceJsonl: write path (session/event → flush)', () => {
  516. it('concurrent sessions do not cross buffers', async () => {
  517. root = await freshRoot()
  518. const ctx = new Context()
  519. await ctx.plugin(SessionStore)
  520. await ctx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  521. const a = ctx.sessions.create(SessionId('sa'))
  522. const b = ctx.sessions.create(SessionId('sb'))
  523. a.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  524. b.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  525. a.append('user/message', { content: [{ type: 'text', text: 'A' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  526. b.append('user/message', { content: [{ type: 'text', text: 'B' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  527. a.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  528. b.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  529. await ctx.sessions.flush(a)
  530. await ctx.sessions.flush(b)
  531. const la = await ctx.sessionPersistence.load(SessionId('sa'))
  532. const lb = await ctx.sessionPersistence.load(SessionId('sb'))
  533. expect(JSON.stringify(la.events)).toContain('"A"')
  534. expect(JSON.stringify(la.events)).not.toContain('"B"')
  535. expect(JSON.stringify(lb.events)).toContain('"B"')
  536. expect(JSON.stringify(lb.events)).not.toContain('"A"')
  537. await ctx.fiber.dispose()
  538. })
  539. })
  540. describe('SessionPersistenceJsonl: scanLog unit', () => {
  541. it('rejects a header-less / empty log', () => {
  542. expect(() => scanLog(Buffer.from(''))).toThrow()
  543. })
  544. it('rejects a corrupt header line', () => {
  545. expect(() => scanLog(Buffer.from('not json\n'))).toThrow(/header/)
  546. })
  547. it('rejects a non-session first line', () => {
  548. expect(() => scanLog(Buffer.from('{"type":"event"}\n'))).toThrow(/session header/)
  549. })
  550. it.each([
  551. ['fractional', 1.5],
  552. ['negative', -1],
  553. ['unsafe', Number.MAX_SAFE_INTEGER + 1],
  554. ])('rejects a session header with a %s createdAt', (_label, createdAt) => {
  555. const log = JSON.stringify({
  556. type: 'session',
  557. version: 0,
  558. id: 'invalid-created-at',
  559. createdAt,
  560. delegationDepth: 0,
  561. }) + '\n'
  562. expect(() => scanLog(Buffer.from(log))).toThrow(/session header/)
  563. })
  564. it('rejects a session header with negative-zero createdAt', () => {
  565. const log = '{"type":"session","version":0,"id":"invalid-created-at","createdAt":-0,"delegationDepth":0}\n'
  566. expect(() => scanLog(Buffer.from(log))).toThrow(/session header/)
  567. })
  568. it.each([
  569. ['missing', undefined],
  570. ['a string', '1'],
  571. ['fractional', 1.5],
  572. ['negative', -1],
  573. ])('rejects a session header with %s delegationDepth', (_label, delegationDepth) => {
  574. const log = JSON.stringify({
  575. type: 'session',
  576. version: 0,
  577. id: 'invalid-depth',
  578. createdAt: 1,
  579. ...delegationDepth === undefined ? {} : { delegationDepth },
  580. }) + '\n'
  581. expect(() => scanLog(Buffer.from(log))).toThrow(/session header/)
  582. })
  583. it('rejects a session header with negative-zero delegationDepth', () => {
  584. const log = '{"type":"session","version":0,"id":"invalid-depth","createdAt":1,"delegationDepth":-0}\n'
  585. expect(() => scanLog(Buffer.from(log))).toThrow(/session header/)
  586. })
  587. it('a seq gap after the last turn/end bounds the preserved tail (torn fragment tolerated)', () => {
  588. const log = [
  589. JSON.stringify({ type: 'session', version: 0, id: 'g', createdAt: 1, delegationDepth: 0 }),
  590. JSON.stringify({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  591. JSON.stringify({ type: 'step/start', seq: 2, time: 2, data: { turn: 1, step: 1 } }), // gap: missing seq 1
  592. ].join('\n') + '\n'
  593. // No committed turn/end, so the gap is a tolerated crash boundary: scanLog PRESERVES the
  594. // contiguous prefix (turn/start seq 0) — real interrupted-turn work, not discarded — and
  595. // stops at the gap. `loadCore`, not this scanner, later closes the orphaned turn.
  596. expect(scanLog(Buffer.from(log)).events.map(e => e.seq)).toEqual([0])
  597. })
  598. it('rejects a seq gap BEFORE a later committed turn/end (committed data damaged)', () => {
  599. const log = [
  600. JSON.stringify({ type: 'session', version: 0, id: 'g2', createdAt: 1, delegationDepth: 0 }),
  601. JSON.stringify({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  602. JSON.stringify({ type: 'step/start', seq: 2, time: 2, data: { turn: 1, step: 1 } }), // gap: missing seq 1
  603. JSON.stringify({ type: 'turn/end', seq: 3, time: 3, data: { turn: 1, reason: { kind: 'completed' } } }),
  604. ].join('\n') + '\n'
  605. // A turn/end exists, so the prefix up to it is committed — but it has a hole.
  606. // Truncating it would silently drop committed data → unloadable.
  607. expect(() => scanLog(Buffer.from(log))).toThrow(/seq gap in committed region/)
  608. })
  609. it('rejects a corrupt line BEFORE a later committed turn/end (committed data damaged)', () => {
  610. const log = [
  611. JSON.stringify({ type: 'session', version: 0, id: 'c', createdAt: 1, delegationDepth: 0 }),
  612. '{not json', // corrupt, sits in the committed region (a turn/end follows)
  613. JSON.stringify({ type: 'turn/end', seq: 1, time: 2, data: { turn: 1, reason: { kind: 'completed' } } }),
  614. ].join('\n') + '\n'
  615. expect(() => scanLog(Buffer.from(log))).toThrow(/unparsable committed event/)
  616. })
  617. it('a header-only log (no event lines at all) preserves nothing — committedBytes is the header', () => {
  618. const log = JSON.stringify({ type: 'session', version: 0, id: 'h0', createdAt: 1, delegationDepth: 0 }) + '\n'
  619. const scanned = scanLog(Buffer.from(log))
  620. expect(scanned.events).toEqual([])
  621. // committedBytes falls back to the header line's end (no preserved events).
  622. expect(scanned.committedBytes).toBe(Buffer.byteLength(log, 'utf8'))
  623. })
  624. it('a corrupt line after the last turn/end bounds the preserved tail', () => {
  625. const log = [
  626. JSON.stringify({ type: 'session', version: 0, id: 'c2', createdAt: 1, delegationDepth: 0 }),
  627. JSON.stringify({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  628. '{not json', // corrupt crash fragment, no turn/end committed
  629. ].join('\n') + '\n'
  630. // The contiguous prefix (turn/start seq 0) is preserved; the corrupt
  631. // fragment after it is the tolerated crash boundary.
  632. expect(scanLog(Buffer.from(log)).events.map(e => e.seq)).toEqual([0])
  633. })
  634. it('tolerates a seq gap AFTER a turn/end (uncommitted tail)', () => {
  635. const log = [
  636. JSON.stringify({ type: 'session', version: 0, id: 't', createdAt: 1, delegationDepth: 0 }),
  637. JSON.stringify({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  638. JSON.stringify({ type: 'turn/end', seq: 1, time: 2, data: { turn: 1, reason: { kind: 'completed' } } }),
  639. JSON.stringify({ type: 'step/start', seq: 9, time: 3, data: { turn: 2, step: 1 } }), // gap in uncommitted tail
  640. ].join('\n') + '\n'
  641. const { events } = scanLog(Buffer.from(log))
  642. expect(events.map(e => e.seq)).toEqual([0, 1]) // tail dropped
  643. })
  644. })
  645. describe('SessionPersistenceJsonl: default packed chunk rows', () => {
  646. let ctx: Context
  647. beforeEach(async () => {
  648. root = await freshRoot()
  649. ctx = new Context()
  650. await ctx.plugin(SessionStore)
  651. // compression: 'none' — these tests assert the textual storage-record layout
  652. // (row tags per line); packing is orthogonal to the physical encoding.
  653. await ctx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  654. })
  655. afterEach(async () => { await ctx.fiber.dispose() })
  656. /** A one-turn log whose step streams a five-member text-delta run. */
  657. function chunkRunLog(): SessionEvent[] {
  658. const deltas: SessionEvent[] = Array.from({ length: 5 }, (_, k) => ({
  659. type: 'assistant/chunk',
  660. seq: 2 + k,
  661. time: 3 + k,
  662. data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: `t${k}` } },
  663. }))
  664. return [
  665. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  666. { type: 'step/start', seq: 1, time: 2, data: { turn: 1, step: 1 } },
  667. ...deltas,
  668. { type: 'assistant/message', seq: 7, time: 8, data: { turn: 1, step: 1, content: [{ type: 'text', text: 't0t1t2t3t4' }], provenance: { provider: 'mock', model: 'mock' } }, surfaceOp: 'append', sourceEventSeqs: [2, 3, 4, 5, 6] },
  669. { type: 'step/end', seq: 8, time: 9, data: { turn: 1, step: 1 } },
  670. { type: 'turn/end', seq: 9, time: 10, data: { turn: 1, reason: { kind: 'completed' } } },
  671. ]
  672. }
  673. it('writes a delta run as one text-chunks row by default and loads back identical events', async () => {
  674. const m = meta('packed', '/work')
  675. const log = chunkRunLog()
  676. await ctx.sessionPersistence.create(m)
  677. await ctx.sessionPersistence.append(m.id, log)
  678. const raw = (await readFile(rawLogPath(root, '/work', m.id), 'utf8')).split('\n').filter(Boolean)
  679. const tags = raw.slice(1).map(line => (JSON.parse(line) as { type: string }).type)
  680. expect(tags).toEqual(['turn/start', 'step/start', 'text-chunks', 'assistant/message', 'step/end', 'turn/end'])
  681. const loaded = await ctx.sessionPersistence.load(m.id)
  682. expect(loaded.events).toEqual(log)
  683. })
  684. it('packChunks: false writes one event per line and still loads identical events', async () => {
  685. const unpackedRoot = await freshRoot()
  686. const unpacked = new Context()
  687. await unpacked.plugin(SessionStore)
  688. await unpacked.plugin(SessionPersistenceJsonl, {
  689. root: unpackedRoot,
  690. packChunks: false,
  691. compression: 'none',
  692. })
  693. try {
  694. const m = meta('unpacked', '/work')
  695. const log = chunkRunLog()
  696. await unpacked.sessionPersistence.create(m)
  697. await unpacked.sessionPersistence.append(m.id, log)
  698. const records = (await readFile(rawLogPath(unpackedRoot, '/work', m.id), 'utf8'))
  699. .split('\n').filter(Boolean).slice(1)
  700. .map(line => JSON.parse(line) as { type: string })
  701. expect(records.filter(record => record.type === 'assistant/chunk')).toHaveLength(5)
  702. expect(records.some(record => record.type === 'text-chunks')).toBe(false)
  703. expect((await unpacked.sessionPersistence.load(m.id)).events).toEqual(log)
  704. } finally {
  705. await unpacked.fiber.dispose()
  706. }
  707. })
  708. it('loads a mixed file: verbatim lines from an unpacked writer, then packed appends', async () => {
  709. const m = meta('mixed', '/work')
  710. const log = chunkRunLog()
  711. // First turn written line-per-event by an unpacked-config writer (an old
  712. // file, hand-planted so this packed-config backend adopts it on load).
  713. await mkdir(sessionDir(root, '/work', m.id), { recursive: true })
  714. await writeFile(rawLogPath(root, '/work', m.id), [
  715. JSON.stringify({ type: 'session', version: 0, id: 'mixed', createdAt: 1000, cwd: '/work', delegationDepth: 0 }),
  716. ...log.map(e => JSON.stringify(e)),
  717. ].join('\n') + '\n')
  718. // Adopt the stored log (cursor = stored length), then append a second turn
  719. // through THIS packed-config backend.
  720. expect((await ctx.sessionPersistence.load(m.id)).events).toEqual(log)
  721. const secondTurn: SessionEvent[] = JSON.parse(JSON.stringify(log)) as SessionEvent[]
  722. for (const [k, e] of secondTurn.entries()) {
  723. ;(e as { seq: number }).seq = 10 + k
  724. ;(e.data as { turn: number }).turn = 2
  725. }
  726. await ctx.sessionPersistence.append(m.id, secondTurn)
  727. const loaded = await ctx.sessionPersistence.load(m.id)
  728. expect(loaded.events).toEqual([...log, ...secondTurn])
  729. // The packed append really packed: the file's tail carries a text-chunks row.
  730. const tags = (await readFile(rawLogPath(root, '/work', m.id), 'utf8')).split('\n').filter(Boolean)
  731. .map(line => (JSON.parse(line) as { type: string }).type)
  732. expect(tags.filter(t => t === 'text-chunks')).toHaveLength(1)
  733. expect(tags.filter(t => t === 'assistant/chunk')).toHaveLength(5)
  734. })
  735. it('scanLog: a packed row advances the seq cursor by its whole run', () => {
  736. const logText = [
  737. JSON.stringify({ type: 'session', version: 0, id: 'rows', createdAt: 1, delegationDepth: 0 }),
  738. JSON.stringify({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  739. JSON.stringify({ type: 'text-chunks', seq0: 1, time0: 2, data: { turn: 1, step: 1, index: 0, dt: [1, 1], texts: ['a', 'b', 'c'] } }),
  740. JSON.stringify({ type: 'turn/end', seq: 4, time: 5, data: { turn: 1, reason: { kind: 'completed' } } }),
  741. ].join('\n') + '\n'
  742. const { events } = scanLog(Buffer.from(logText))
  743. expect(events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4])
  744. expect(events[2]).toEqual({ type: 'assistant/chunk', seq: 2, time: 3, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'b' } } })
  745. })
  746. it('scanLog: a malformed packed row in the committed region rejects like corrupt JSON', () => {
  747. const logText = [
  748. JSON.stringify({ type: 'session', version: 0, id: 'bad-row', createdAt: 1, delegationDepth: 0 }),
  749. // dt arity mismatch — row validation throws, so the line is a committed hole.
  750. JSON.stringify({ type: 'text-chunks', seq0: 0, time0: 1, data: { turn: 1, step: 1, index: 0, dt: [], texts: ['a', 'b'] } }),
  751. JSON.stringify({ type: 'turn/end', seq: 2, time: 3, data: { turn: 1, reason: { kind: 'completed' } } }),
  752. ].join('\n') + '\n'
  753. expect(() => scanLog(Buffer.from(logText))).toThrow(/unparsable committed event/)
  754. })
  755. it('scanLog: a packed row with a mid-run seq gap after the last turn/end drops the whole row', () => {
  756. const logText = [
  757. JSON.stringify({ type: 'session', version: 0, id: 'row-gap', createdAt: 1, delegationDepth: 0 }),
  758. JSON.stringify({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  759. // seq0 skips 1 — the run's first member is already a gap; no turn/end follows.
  760. JSON.stringify({ type: 'text-chunks', seq0: 2, time0: 2, data: { turn: 1, step: 1, index: 0, dt: [1, 1], texts: ['a', 'b', 'c'] } }),
  761. ].join('\n') + '\n'
  762. const scanned = scanLog(Buffer.from(logText))
  763. expect(scanned.events.map(e => e.seq)).toEqual([0])
  764. // committedBytes stays on the line boundary BEFORE the dropped row.
  765. const headerAndTurn = logText.split('\n').slice(0, 2).join('\n') + '\n'
  766. expect(scanned.committedBytes).toBe(Buffer.byteLength(headerAndTurn, 'utf8'))
  767. })
  768. it('eventLines(packChunks: false) is byte-identical to the pre-packing layout', () => {
  769. const log = chunkRunLog()
  770. expect(eventLines(log, false)).toBe(log.map(e => JSON.stringify(e)).join('\n'))
  771. })
  772. })
  773. describe('SessionPersistenceJsonl: edge cases', () => {
  774. let ctx: Context
  775. beforeEach(async () => {
  776. root = await freshRoot()
  777. ctx = new Context()
  778. await ctx.plugin(SessionStore)
  779. await ctx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  780. })
  781. afterEach(async () => { await ctx.fiber.dispose() })
  782. it('append rejects non-JSON-serializable undefined-producing data', async () => {
  783. const m = meta('undef')
  784. await ctx.sessionPersistence.create(m)
  785. // A value whose JSON.stringify yields undefined (a bare function as data).
  786. const bad = [{ type: 'user/message', seq: 0, time: 1, data: (() => 0) as unknown }] as unknown as SessionEvent[]
  787. await expect(ctx.sessionPersistence.append(m.id, bad)).rejects.toThrow(/non-JSON-serializable/)
  788. })
  789. it('create snapshots its meta: mutating the caller object after the call is ignored', async () => {
  790. const m = meta('create-snap', '/orig')
  791. const p = ctx.sessionPersistence.create(m)
  792. // Mutate the caller's meta object immediately after calling create.
  793. mutableHeader(m).cwd = '/mutated'
  794. await p
  795. await ctx.sessionPersistence.append(SessionId('create-snap'), oneTurnLog())
  796. // The log materialized under the ORIGINAL cwd, not the mutated one.
  797. expect((await stat(rawLogPath(root, '/orig', SessionId('create-snap')))).isFile()).toBe(true)
  798. await expect(stat(rawLogPath(root, '/mutated', SessionId('create-snap')))).rejects.toThrow()
  799. })
  800. it('list discovers sessions across multiple project directories', async () => {
  801. await ctx.sessionPersistence.create(meta('p1', '/projA'))
  802. await ctx.sessionPersistence.append(SessionId('p1'), oneTurnLog())
  803. await ctx.sessionPersistence.create(meta('p2', '/projB'))
  804. await ctx.sessionPersistence.append(SessionId('p2'), oneTurnLog())
  805. await ctx.sessionPersistence.create(meta('p3')) // no cwd → _no-cwd project directory
  806. await ctx.sessionPersistence.append(SessionId('p3'), oneTurnLog())
  807. const ids = (await ctx.sessionPersistence.list()).map(x => x.id).sort()
  808. expect(ids).toEqual(['p1', 'p2', 'p3'])
  809. })
  810. it('groups sessions whose cwd paths normalize to the same project directory', async () => {
  811. const first = meta('normalized-first', '/a/b-c')
  812. const second = meta('normalized-second', '/a-b/c')
  813. await ctx.sessionPersistence.create(first)
  814. await ctx.sessionPersistence.append(first.id, oneTurnLog())
  815. await ctx.sessionPersistence.create(second)
  816. await ctx.sessionPersistence.append(second.id, oneTurnLog())
  817. expect(projectDir(root, first.cwd)).toBe(projectDir(root, second.cwd))
  818. expect(await readdir(projectDir(root, first.cwd))).toEqual(expect.arrayContaining([
  819. encodeSegment(first.id),
  820. encodeSegment(second.id),
  821. ]))
  822. expect((await ctx.sessionPersistence.list()).map(header => header.id).sort())
  823. .toEqual([first.id, second.id].sort())
  824. })
  825. it('list on an empty root returns nothing', async () => {
  826. expect(await ctx.sessionPersistence.list()).toEqual([])
  827. })
  828. it('keeps the transcript in an extensible session-owned directory', async () => {
  829. const m = meta('owned-directory', '/project')
  830. await ctx.sessionPersistence.create(m)
  831. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  832. const dir = sessionDir(root, m.cwd, m.id)
  833. await writeFile(join(dir, 'metadata.json'), '{}\n')
  834. await writeFile(join(projectDir(root, m.cwd), 'README'), 'project metadata\n')
  835. await mkdir(join(projectDir(root, m.cwd), 'reserved-session'), { recursive: true })
  836. expect(await readdir(dir)).toEqual(expect.arrayContaining(['metadata.json', 'session.jsonl']))
  837. expect((await ctx.sessionPersistence.list()).map(header => header.id)).toContain(m.id)
  838. expect((await ctx.sessionPersistence.load(m.id)).events).toEqual(oneTurnLog())
  839. })
  840. it('rejects the obsolete flat-file layout instead of ignoring stored sessions', async () => {
  841. const m = meta('legacy-flat', '/legacy')
  842. const project = projectDir(root, m.cwd)
  843. const path = join(project, `${encodeSegment(m.id)}.jsonl`)
  844. await mkdir(project, { recursive: true })
  845. await writeFile(path, [
  846. JSON.stringify(toHeaderLine(m)),
  847. ...oneTurnLog().map(event => JSON.stringify(event)),
  848. '',
  849. ].join('\n'))
  850. await expect(ctx.sessionPersistence.load(m.id)).rejects.toThrow(/unsupported flat-file layout/)
  851. await expect(ctx.sessionPersistence.list()).rejects.toThrow(/unsupported flat-file layout/)
  852. })
  853. it('rejects a compressed obsolete flat-file artifact during targeted lookup', async () => {
  854. const m = meta('legacy-compressed-flat', '/legacy')
  855. const project = projectDir(root, m.cwd)
  856. expect(await ctx.sessionPersistence.list()).toEqual([])
  857. await mkdir(project, { recursive: true })
  858. await writeFile(join(project, `${encodeSegment(m.id)}.jsonl.zstd`), 'legacy')
  859. await expect(ctx.sessionPersistence.load(m.id)).rejects.toThrow(/unsupported flat-file layout/)
  860. })
  861. it('list skips empty and non-header session logs (metadata-only read)', async () => {
  862. // A real session…
  863. await ctx.sessionPersistence.create(meta('real', '/p'))
  864. await ctx.sessionPersistence.append(SessionId('real'), oneTurnLog())
  865. // …alongside junk session directories whose fixed transcript is empty or
  866. // lacks a header. Both remain unmaterialized and are skipped.
  867. for (const [id, content] of [
  868. ['empty', ''],
  869. ['notheader', '{"type":"turn/start"}\n'],
  870. ['badjson', 'not json at all\n'],
  871. ] as const) {
  872. const path = rawLogPath(root, undefined, SessionId(id))
  873. await mkdir(sessionDir(root, undefined, SessionId(id)), { recursive: true })
  874. await writeFile(path, content)
  875. }
  876. const ids = (await ctx.sessionPersistence.list()).map(x => x.id).sort()
  877. expect(ids).toEqual(['real'])
  878. })
  879. it('list reads a header line longer than the 8KB read chunk', async () => {
  880. // A tolerated extra field makes this valid header exceed the 8192-byte read buffer, proving
  881. // `readFirstLine` accumulates chunks before `list()` parses it.
  882. const id = SessionId('big')
  883. await mkdir(sessionDir(root, undefined, id), { recursive: true })
  884. const bigHeader = JSON.stringify({ type: 'session', version: 0, id: 'big', createdAt: 1, delegationDepth: 0, pad: 'x'.repeat(9000) })
  885. await writeFile(rawLogPath(root, undefined, id), bigHeader + '\n')
  886. const ids = (await ctx.sessionPersistence.list()).map(x => x.id)
  887. expect(ids).toContain('big')
  888. })
  889. it('list rejects a header whose cwd does not identify its physical log', async () => {
  890. const m = meta('misplaced', '/stored')
  891. await ctx.sessionPersistence.create(m)
  892. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  893. await rewriteHeader(rawLogPath(root, m.cwd, m.id), (header) => { header.cwd = '/elsewhere' })
  894. await expect(ctx.sessionPersistence.list()).rejects.toThrow(/and cwd identify/)
  895. })
  896. it('accepts an alternate project path only when it identifies the same physical log', async () => {
  897. const m = meta('physical-alias', '/stored')
  898. await ctx.sessionPersistence.create(m)
  899. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  900. const path = rawLogPath(root, m.cwd, m.id)
  901. const aliasCwd = '/alias'
  902. await symlink(
  903. projectDir(root, m.cwd),
  904. projectDir(root, aliasCwd),
  905. process.platform === 'win32' ? 'junction' : 'dir',
  906. )
  907. await rewriteHeader(path, (header) => { header.cwd = aliasCwd })
  908. expect((await ctx.sessionPersistence.load(m.id)).meta.cwd).toBe(aliasCwd)
  909. expect((await ctx.sessionPersistence.list()).map(header => header.id)).toContain(m.id)
  910. })
  911. it('list rejects a session header whose id cannot name a storage path', async () => {
  912. const dir = join(projectDir(root, undefined), 'invalid-id')
  913. await mkdir(dir, { recursive: true })
  914. await writeFile(join(dir, 'session.jsonl'), JSON.stringify({
  915. type: 'session', version: 0, id: '', createdAt: 1, delegationDepth: 0,
  916. }) + '\n')
  917. await expect(ctx.sessionPersistence.list()).rejects.toThrow(/header id cannot name a storage path/)
  918. })
  919. it('load and list reject one id materialized in multiple project directories', async () => {
  920. const id = SessionId('duplicate')
  921. for (const cwd of ['/a', '/b']) {
  922. const m = meta(id, cwd)
  923. await mkdir(sessionDir(root, cwd, id), { recursive: true })
  924. const content = [JSON.stringify(toHeaderLine(m)), ...oneTurnLog().map(event => JSON.stringify(event))].join('\n') + '\n'
  925. await writeFile(rawLogPath(root, cwd, id), content)
  926. }
  927. await expect(ctx.sessionPersistence.load(id)).rejects.toThrow(/appears in multiple project directories/)
  928. await expect(ctx.sessionPersistence.list()).rejects.toThrow(/appears in multiple project directories/)
  929. })
  930. it('a DIFFERENT live session object reusing a disposed id gets its own init (no stale cache)', async () => {
  931. // Session A materializes a log under id "reuse".
  932. const sessFiberA = await ctx.plugin(Object.assign((inner: Context) => {
  933. const a = inner.sessions.create(SessionId('reuse'), { meta: { cwd: '/a' } })
  934. appendLog(a, oneTurnLog())
  935. }, { inject: ['sessions'] }))
  936. // Drain A, then dispose ITS fiber (the live session A is gone) while the
  937. // backend stays loaded.
  938. for (const s of ctx.sessions.list()) await ctx.sessions.flush(s)
  939. await sessFiberA.dispose()
  940. // A new Session object reuses the id. Object-keyed initialization must run independently,
  941. // detect the disk collision, and reject instead of appending through session A's stale cursor.
  942. let b!: Session
  943. await ctx.plugin(Object.assign((inner: Context) => {
  944. b = inner.sessions.create(SessionId('reuse'), { meta: { cwd: '/a' } })
  945. }, { inject: ['sessions'] }))
  946. await expect(ctx.sessions.flush(b)).rejects.toThrow(/already bound to a different live session|already has a persisted log on disk/)
  947. })
  948. it('a no-cwd live session cannot adopt a same-id log from another cwd', async () => {
  949. // Backend 1: materialize a log under id "x" in the cwd "/w" bucket, then
  950. // dispose the WHOLE backend (so backend 2 mounts with an EMPTY states map —
  951. // the HMR/reload path with no tracked collision state).
  952. await ctx.sessionPersistence.create(meta('x', '/w'))
  953. await ctx.sessionPersistence.append(SessionId('x'), oneTurnLog())
  954. await ctx.fiber.dispose()
  955. // Backend 2 creates a no-cwd session whose id exists only in `/w`. The
  956. // stored cwd check rejects instead of grafting no-cwd events onto that log.
  957. const ctx2 = new Context()
  958. await ctx2.plugin(SessionStore)
  959. await ctx2.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  960. let b!: Session
  961. await ctx2.plugin(Object.assign((inner: Context) => {
  962. b = inner.sessions.create(SessionId('x')) // no cwd
  963. }, { inject: ['sessions'] }))
  964. await expect(ctx2.sessions.flush(b)).rejects.toThrow(/different cwd|id collision/)
  965. // The "/w" log is untouched — no no-cwd events were grafted onto it, and no
  966. // `_no-cwd` log for "x" was created.
  967. const inW = scanLog(await readFile(rawLogPath(root, '/w', SessionId('x'))))
  968. expect(inW.meta.cwd).toBe('/w')
  969. expect(inW.events).toHaveLength(6)
  970. await expect(stat(rawLogPath(root, undefined, SessionId('x')))).rejects.toThrow()
  971. await ctx2.fiber.dispose()
  972. })
  973. it('a seed with matching seq/type/time but DIFFERENT data is rejected (deep prefix compare)', async () => {
  974. // Materialize and load (ownerless, cursor = 6).
  975. await ctx.sessionPersistence.create(meta('divergent', '/a'))
  976. await ctx.sessionPersistence.append(SessionId('divergent'), oneTurnLog())
  977. await ctx.sessionPersistence.load(SessionId('divergent'))
  978. // A seed that keeps every seq/type/time but mutates a payload must NOT be
  979. // accepted as "the same session" — otherwise drain filters those seqs as
  980. // already persisted and the divergent payload is silently lost.
  981. const tampered = oneTurnLog()
  982. const userMsg = tampered[1]
  983. if (userMsg?.type === 'user/message') userMsg.data.content = [{ type: 'text', text: 'DIFFERENT' }]
  984. let bad!: Session
  985. await ctx.plugin(Object.assign((inner: Context) => {
  986. bad = inner.sessions.create(SessionId('divergent'), { seed: tampered, meta: { cwd: '/a' } })
  987. }, { inject: ['sessions'] }))
  988. await expect(ctx.sessions.flush(bad)).rejects.toThrow(/do not match this live session|already has a persisted log/)
  989. })
  990. it('a second live session reusing a bound id is rejected', async () => {
  991. // A live session materializes and owns the id.
  992. const firstFiber = await ctx.plugin(Object.assign((inner: Context) => {
  993. const a = inner.sessions.create(SessionId('bound'), { meta: { cwd: '/a' } })
  994. a.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  995. a.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  996. }, { inject: ['sessions'] }))
  997. for (const s of ctx.sessions.list()) await ctx.sessions.flush(s)
  998. await firstFiber.dispose()
  999. let second!: Session
  1000. await ctx.plugin(Object.assign((inner: Context) => {
  1001. second = inner.sessions.create(SessionId('bound'), { meta: { cwd: '/a' } })
  1002. }, { inject: ['sessions'] }))
  1003. await expect(ctx.sessions.flush(second))
  1004. .rejects.toThrow(/already bound to a different live session|already has a persisted log|do not match/)
  1005. })
  1006. it('list returns nothing when the root directory does not exist', async () => {
  1007. const ctx2 = new Context()
  1008. await ctx2.plugin(SessionStore)
  1009. await ctx2.plugin(SessionPersistenceJsonl, {
  1010. root: join(root, 'does-not-exist-yet'),
  1011. compression: 'none',
  1012. })
  1013. expect(await ctx2.sessionPersistence.list()).toEqual([])
  1014. await ctx2.fiber.dispose()
  1015. })
  1016. it('plugin load rejects an existing root that is not a directory', async () => {
  1017. const filePath = join(root, 'not-a-dir')
  1018. await writeFile(filePath, 'x')
  1019. const ctx2 = new Context()
  1020. await ctx2.plugin(SessionStore)
  1021. await expect(ctx2.plugin(SessionPersistenceJsonl, { root: filePath, compression: 'none' })).rejects.toThrow(/ENOTDIR/)
  1022. await ctx2.fiber.dispose()
  1023. })
  1024. it('list surfaces a root that becomes unusable after plugin load', async () => {
  1025. await rm(root, { recursive: true })
  1026. await writeFile(root, 'not a directory')
  1027. await expect(ctx.sessionPersistence.list()).rejects.toThrow(/ENOTDIR/)
  1028. })
  1029. it('per-id lookup surfaces non-ENOENT storage errors', async () => {
  1030. const blocker = join(root, 'not-a-directory')
  1031. await writeFile(blocker, 'x')
  1032. const backend = ctx.sessionPersistence as unknown as { exists(path: string): Promise<boolean> }
  1033. await expect(backend.exists(join(blocker, 'child.jsonl'))).rejects.toThrow(/ENOTDIR/)
  1034. })
  1035. it('materialization surfaces a project-directory storage fault', async () => {
  1036. const cwd = '/x'
  1037. const ctx2 = new Context()
  1038. await ctx2.plugin(SessionStore)
  1039. await ctx2.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  1040. await writeFile(projectDir(root, cwd), 'x') // project path is now a file
  1041. let s!: Session
  1042. await ctx2.plugin(Object.assign((inner: Context) => {
  1043. s = inner.sessions.create(SessionId('exists-fault'), { meta: { cwd } })
  1044. appendClosedTurn(s)
  1045. }, { inject: ['sessions'] }))
  1046. await expect(ctx2.sessions.flush(s)).rejects.toThrow(/EEXIST|ENOTDIR/)
  1047. await ctx2.fiber.dispose()
  1048. })
  1049. it('append() to a disk-only session adopts it and repairs a crash tail', async () => {
  1050. // Persist a session, then corrupt its tail, all through ONE backend.
  1051. const m = meta('disk-append', '/d')
  1052. await ctx.sessionPersistence.create(m)
  1053. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  1054. await writeFile(rawLogPath(root, '/d', m.id), '\n{"partial crash', { flag: 'a' })
  1055. // A FRESH backend with no in-memory state: append directly (no prior load)
  1056. // → append must adopt from disk, and the adopt's load schedules a repair
  1057. // that the same append then performs before writing.
  1058. const ctx2 = new Context()
  1059. await ctx2.plugin(SessionStore)
  1060. await ctx2.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  1061. await ctx2.sessionPersistence.append(m.id, [
  1062. { type: 'turn/start', seq: 6, time: 9, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  1063. { type: 'turn/end', seq: 7, time: 10, data: { turn: 2, reason: { kind: 'completed' } } },
  1064. ] as SessionEvent[])
  1065. const loaded = await ctx2.sessionPersistence.load(m.id)
  1066. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  1067. await ctx2.fiber.dispose()
  1068. })
  1069. it('a header-only log (open turn, no turn/end) preserves the open turn on load and closes it', async () => {
  1070. // A session whose only durable content is an unclosed first turn. scanLog
  1071. // preserves the turn/start; loadCore closes it with a synthetic
  1072. // turn/end {interrupted} so the returned log is balanced.
  1073. const m = meta('open-turn', '/h')
  1074. await ctx.sessionPersistence.create(m)
  1075. await ctx.sessionPersistence.append(m.id, [
  1076. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  1077. ] as SessionEvent[])
  1078. const { events } = await ctx.sessionPersistence.load(m.id)
  1079. expect(events.map(e => e.type)).toEqual(['turn/start', 'turn/end'])
  1080. const end = events[1]!
  1081. expect(end.type === 'turn/end' && end.data.reason).toEqual({ kind: 'interrupted' })
  1082. })
  1083. it('createCore rejects an id already on disk under a different project directory', async () => {
  1084. // Persist the id under cwd A.
  1085. const a = meta('dup-id', '/projA')
  1086. await ctx.sessionPersistence.create(a)
  1087. await ctx.sessionPersistence.append(a.id, oneTurnLog())
  1088. // A fresh backend creating the SAME id under cwd B must still refuse: load
  1089. // identifies by id across all projects, so a second log would make resume
  1090. // nondeterministic. create scans every project, not just meta.cwd's.
  1091. const ctx2 = new Context()
  1092. await ctx2.plugin(SessionStore)
  1093. await ctx2.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  1094. await expect(ctx2.sessionPersistence.create(meta('dup-id', '/projB')))
  1095. .rejects.toThrow(/already has a persisted log on disk/)
  1096. await ctx2.fiber.dispose()
  1097. })
  1098. it('flush keeps buffered events when the append fails (no silent loss)', async () => {
  1099. root = await freshRoot()
  1100. const ctx2 = new Context()
  1101. await ctx2.plugin(SessionStore)
  1102. await ctx2.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  1103. const session = ctx2.sessions.create(SessionId('flush-fail'))
  1104. // A full turn lands in the write-behind buffer.
  1105. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  1106. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1107. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  1108. // Make the durable materialize fail on the next flush.
  1109. const backend = ctx2.sessionPersistence as unknown as { materialize: (...args: unknown[]) => Promise<void> }
  1110. const origMat = backend.materialize.bind(backend)
  1111. backend.materialize = () => Promise.reject(new Error('disk full'))
  1112. await expectFlushError(ctx2.sessions.flush(session), /disk full/)
  1113. // The events are STILL buffered (not silently dropped): a retry persists them.
  1114. backend.materialize = origMat
  1115. await ctx2.sessions.flush(session)
  1116. const loaded = await ctx2.sessionPersistence.load(SessionId('flush-fail'))
  1117. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2])
  1118. await ctx2.fiber.dispose()
  1119. })
  1120. it('rejects non-JSON event data: BigInt, function, circular, Map, undefined property', async () => {
  1121. const m = meta('serial')
  1122. await ctx.sessionPersistence.create(m)
  1123. const bad = (extra: unknown) => [{ type: 'user/message', seq: 0, time: 1, data: { content: [{ type: 'text', text: 'x' }], source: { kind: 'user' }, extra } }] as unknown as SessionEvent[]
  1124. await expect(ctx.sessionPersistence.append(m.id, bad(1n))).rejects.toThrow(/non-JSON-serializable/)
  1125. await expect(ctx.sessionPersistence.append(m.id, bad(() => 0))).rejects.toThrow(/non-JSON-serializable/)
  1126. await expect(ctx.sessionPersistence.append(m.id, bad(Symbol('s')))).rejects.toThrow(/non-JSON-serializable/)
  1127. await expect(ctx.sessionPersistence.append(m.id, bad(new Map()))).rejects.toThrow(/non-JSON-serializable/)
  1128. await expect(ctx.sessionPersistence.append(m.id, bad(undefined))).rejects.toThrow(/non-JSON-serializable/)
  1129. await expect(ctx.sessionPersistence.append(m.id, bad(Infinity))).rejects.toThrow(/non-JSON-serializable/)
  1130. // a circular structure
  1131. const circ: Record<string, unknown> = {}
  1132. circ.self = circ
  1133. await expect(ctx.sessionPersistence.append(m.id, bad(circ))).rejects.toThrow(/non-JSON-serializable/)
  1134. // The session was never materialized by any of the rejected appends.
  1135. expect((await ctx.sessionPersistence.list()).map(h => h.id)).not.toContain(m.id)
  1136. })
  1137. it('accepts well-formed JSON values (null, booleans, nested arrays/objects)', async () => {
  1138. const m = meta('json-ok')
  1139. await ctx.sessionPersistence.create(m)
  1140. const ev = [{ type: 'user/message', seq: 0, time: 1, data: { content: [{ type: 'text', text: 'x' }], source: { kind: 'user' }, extra: { a: null, b: true, c: [1, 2, { d: 'nested' }] } } }] as unknown as SessionEvent[]
  1141. await ctx.sessionPersistence.append(m.id, ev)
  1142. expect((await ctx.sessionPersistence.list()).map(h => h.id)).toContain(m.id)
  1143. })
  1144. it('Session.append rejects a non-serializable event at the source (never enters the log)', () => {
  1145. const session = ctx.sessions.create(SessionId('reject-bad'))
  1146. // Serializability is enforced at the source: Session.append throws on a BigInt-bearing
  1147. // event before it enters session.events, so the durable log can never diverge from the live
  1148. // log. The error therefore surfaces synchronously at append, not later during backend flush.
  1149. expect(() => {
  1150. session.append('user/message', { content: [{ type: 'text', text: 'bad' }], source: { kind: 'user' }, bad: 1n } as never, { surfaceOp: 'append' })
  1151. }).toThrow(/non-JSON-serializable/)
  1152. // The bad event was rejected, so the log stayed empty.
  1153. expect(session.events.length).toBe(0)
  1154. })
  1155. })