sqlite.spec.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { mkdtemp, rm } from 'node:fs/promises'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  7. import type { Session, SessionEvent, SessionMeta } from '@deepseek-ai/dsh-session'
  8. import SessionPersistenceSqlite, { SCHEMA_VERSION } from '@deepseek-ai/dsh-session-persistence-sqlite'
  9. import { openDatabase, scanRows, type EventRow } from '../src/schema.ts'
  10. import { runPersistenceContract, meta, oneTurnLog } from '../../session-persistence/tests/contract.ts'
  11. const dirs: string[] = []
  12. afterEach(async () => { for (const d of dirs.splice(0)) await rm(d, { recursive: true, force: true }) })
  13. async function freshDbPath(): Promise<string> {
  14. const dir = await mkdtemp(join(tmpdir(), 'dsh-sqlite-'))
  15. dirs.push(dir)
  16. return join(dir, 'sessions.db')
  17. }
  18. /** A context with the session store + SQLite backend, plus a teardown. */
  19. async function backend(path = ':memory:'): Promise<{ ctx: Context; dispose: () => Promise<void> }> {
  20. const ctx = new Context()
  21. await ctx.plugin(SessionStore)
  22. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path })
  23. return { ctx, dispose: () => fiber.dispose() }
  24. }
  25. // The payoff: the SAME backend-agnostic contract the JSONL backend runs, now
  26. // proving the SQLite backend satisfies identical semantics.
  27. runPersistenceContract('sqlite', async () => {
  28. const ctx = new Context()
  29. await ctx.plugin(SessionStore)
  30. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  31. return {
  32. persistence: ctx.sessionPersistence,
  33. dispose: async () => { await fiber.dispose() },
  34. }
  35. })
  36. describe('scanRows', () => {
  37. // scanRows works off EventRows (data is a JSON string column); build them from
  38. // SessionEvents so the unit tests read in terms of the event vocabulary.
  39. const rows = (events: SessionEvent[]): EventRow[] =>
  40. events.map(e => ({ seq: e.seq, type: e.type, time: e.time, data: JSON.stringify(e.data) }))
  41. it('preserves the full log when it ends exactly on a turn/end (no torn tail)', () => {
  42. const { preserved, tornFrom } = scanRows(rows(oneTurnLog()))
  43. expect(preserved).toEqual(oneTurnLog())
  44. expect(tornFrom).toBeUndefined()
  45. })
  46. it('PRESERVES the real events of an interrupted turn after the last turn/end', () => {
  47. // turn 1 committed (0..5) + a crashed turn 2 (turn/start 6, step/start 7, no
  48. // close): all 8 rows are intact, so the whole prefix is preserved and there
  49. // is no torn fragment to delete. (load() then synthesizes the closers.)
  50. const withOpenTurn: SessionEvent[] = [
  51. ...oneTurnLog(),
  52. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  53. { type: 'step/start', seq: 7, time: 8, data: { turn: 2, step: 1 } },
  54. ]
  55. const { preserved, tornFrom } = scanRows(rows(withOpenTurn))
  56. expect(preserved.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  57. expect(tornFrom).toBeUndefined()
  58. })
  59. it('preserves the contiguous prefix and flags a torn tail at a seq gap', () => {
  60. // A gap after seq 0 (no committed turn/end): seq 0 is the preserved
  61. // interrupted-turn event; the gap bounds it and marks the torn fragment.
  62. const gapped: SessionEvent[] = [
  63. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  64. { type: 'step/start', seq: 2, time: 2, data: { turn: 1, step: 1 } }, // seq 1 missing
  65. ]
  66. const { preserved, tornFrom } = scanRows(rows(gapped))
  67. expect(preserved.map(e => e.seq)).toEqual([0])
  68. expect(tornFrom).toBe(1)
  69. })
  70. it('an empty log preserves nothing and has no torn tail', () => {
  71. expect(scanRows([])).toEqual({ preserved: [] })
  72. })
  73. it('throws on a seq gap inside the committed region (before the last turn/end)', () => {
  74. const gapped: SessionEvent[] = [
  75. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  76. { type: 'step/start', seq: 2, time: 2, data: { turn: 1, step: 1 } }, // seq 1 missing
  77. { type: 'turn/end', seq: 3, time: 3, data: { turn: 1, reason: { kind: 'completed' } } },
  78. ]
  79. expect(() => scanRows(rows(gapped))).toThrow(/seq gap in committed region/)
  80. })
  81. it('throws on an unparsable row inside the committed region', () => {
  82. const withCorruptCommitted: EventRow[] = [
  83. { seq: 0, type: 'turn/start', time: 1, data: '{not json' }, // corrupt, sits before a turn/end
  84. { seq: 1, type: 'turn/end', time: 2, data: JSON.stringify({ turn: 1, reason: { kind: 'completed' } }) },
  85. ]
  86. expect(() => scanRows(withCorruptCommitted)).toThrow(/unparsable committed event/)
  87. })
  88. it('tolerates an unparsable torn-tail row after the last turn/end', () => {
  89. const withCorruptTail: EventRow[] = [
  90. ...rows(oneTurnLog()),
  91. { seq: 6, type: 'turn/start', time: 7, data: '{not json' }, // torn fragment, no committed turn/end after
  92. ]
  93. const { preserved, tornFrom } = scanRows(withCorruptTail)
  94. expect(preserved).toEqual(oneTurnLog())
  95. expect(tornFrom).toBe(6)
  96. })
  97. })
  98. describe('SessionPersistenceSqlite: HMR adoption', () => {
  99. it('does not crash-repair an active open turn as interrupted', async () => {
  100. const path = await freshDbPath()
  101. const ctx = new Context()
  102. await ctx.plugin(SessionStore)
  103. const first = await ctx.plugin(SessionPersistenceSqlite, { path })
  104. const session = ctx.sessions.create('hmr-open', { meta: { cwd: '/hmr' } })
  105. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  106. session.append('step/start', { turn: 1, step: 1 })
  107. await ctx.parallel('session/flush', session)
  108. await first.dispose()
  109. const db = openDatabase(path)
  110. db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, ?, ?, ?, ?)')
  111. .run('hmr-open', 2, 'step/end', 2, '{"torn":')
  112. db.close()
  113. const second = await ctx.plugin(SessionPersistenceSqlite, { path })
  114. session.append('step/end', { turn: 1, step: 1 })
  115. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  116. await ctx.parallel('session/flush', session)
  117. const loaded = await ctx.sessionPersistence.load(SessionId('hmr-open'))
  118. expect(loaded.events.map(e => e.type)).toEqual(['turn/start', 'step/start', 'step/end', 'turn/end'])
  119. expect(loaded.events.at(-1)).toMatchObject({ type: 'turn/end', data: { reason: { kind: 'completed' } } })
  120. await second.dispose()
  121. await ctx.fiber.dispose()
  122. })
  123. })
  124. describe('SessionPersistenceSqlite: durability and crash semantics', () => {
  125. it('an interrupted turn (rows after the last turn/end) is PRESERVED and closed during load', async () => {
  126. const path = await freshDbPath()
  127. const m = meta('crash')
  128. // Run 1: persist a complete turn, then a half-written second turn (no turn/end).
  129. const ctx1 = new Context()
  130. await ctx1.plugin(SessionStore)
  131. const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
  132. await ctx1.sessionPersistence.create(m)
  133. await ctx1.sessionPersistence.append(m.id, oneTurnLog())
  134. await ctx1.sessionPersistence.append(m.id, [
  135. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  136. { type: 'step/start', seq: 7, time: 8, data: { turn: 2, step: 1 } },
  137. ])
  138. await fiber1.dispose()
  139. // Run 2: load PRESERVES the interrupted turn's real events (a turn can be huge
  140. // — never truncated) and closes the orphaned turn with synthetic boundary
  141. // events: step/end (the step was open) then turn/end {interrupted}.
  142. const ctx2 = new Context()
  143. await ctx2.plugin(SessionStore)
  144. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  145. const loaded = await ctx2.sessionPersistence.load(m.id)
  146. expect(loaded.events.map(e => e.type)).toEqual([
  147. 'turn/start', 'user/message', 'step/start', 'assistant/message', 'step/end', 'turn/end', // turn 1
  148. 'turn/start', 'step/start', 'step/end', 'turn/end', // turn 2: real events + synthetic closers
  149. ])
  150. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  151. const last = loaded.events.at(-1)!
  152. expect(last.type === 'turn/end' && last.data.reason).toEqual({ kind: 'interrupted' })
  153. // load durably closed the turn, so the next append continues at the balanced
  154. // length (seq 10) and a reload round-trips identically.
  155. await ctx2.sessionPersistence.append(m.id, [
  156. { type: 'turn/start', seq: 10, time: 9, data: { turn: 3, trigger: { kind: 'message', source: { kind: 'user' } } } },
  157. { type: 'turn/end', seq: 11, time: 10, data: { turn: 3, reason: { kind: 'completed' } } },
  158. ])
  159. const reloaded = await ctx2.sessionPersistence.load(m.id)
  160. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
  161. await fiber2.dispose()
  162. })
  163. it('load() durably closes the interrupted turn: the synthetic closers are on disk after load', async () => {
  164. const path = await freshDbPath()
  165. const m = meta('load-closes')
  166. const b1 = await backend(path)
  167. await b1.ctx.sessionPersistence.create(m)
  168. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog()) // seqs 0..5
  169. await b1.dispose()
  170. // Hand-write an interrupted turn (turn/start seq 6, no turn/end).
  171. const db = openDatabase(path)
  172. db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, 6, ?, 7, ?)')
  173. .run(m.id, 'turn/start', JSON.stringify({ turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } }))
  174. db.close()
  175. const b2 = await backend(path)
  176. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  177. // turn 2's real turn/start (seq 6) is preserved + a synthetic turn/end (seq 7).
  178. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  179. expect(loaded.events.at(-1)!.type).toBe('turn/end')
  180. // load() is mutating: the synthetic turn/end MUST be on disk so the stored log
  181. // is balanced and the cursor is truthful (contract: load closes, not defers).
  182. const probe = openDatabase(path)
  183. const stored = probe.prepare('SELECT seq, type FROM events WHERE session_id = ? ORDER BY seq').all(m.id) as { seq: number; type: string }[]
  184. probe.close()
  185. expect(stored.map(r => r.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  186. expect(stored.at(-1)!.type).toBe('turn/end')
  187. await b2.dispose()
  188. })
  189. it('all-tail load: a session whose only turn never closed is preserved and closed on load', async () => {
  190. const path = await freshDbPath()
  191. const m = meta('all-tail')
  192. const b1 = await backend(path)
  193. await b1.ctx.sessionPersistence.create(m)
  194. // A first turn that NEVER completed: turn/start + user/message, no turn/end.
  195. await b1.ctx.sessionPersistence.append(m.id, [
  196. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  197. { type: 'user/message', seq: 1, time: 2, data: { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } } },
  198. ])
  199. expect(await b1.ctx.sessionPersistence.has(m.id)).toBe(true) // materialized
  200. await b1.dispose()
  201. // A fresh backend loads it: the interrupted (only) turn's real events are
  202. // preserved and closed with a synthetic turn/end {interrupted} — NOT
  203. // truncated. The session was materialized, so has()/list() report it present.
  204. const b2 = await backend(path)
  205. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  206. expect(loaded.events.map(e => e.type)).toEqual(['turn/start', 'user/message', 'turn/end'])
  207. expect(loaded.events.at(-1)!.type === 'turn/end' && loaded.events.at(-1)!.data).toMatchObject({ reason: { kind: 'interrupted' } })
  208. expect(await b2.ctx.sessionPersistence.has(m.id)).toBe(true)
  209. expect((await b2.ctx.sessionPersistence.list()).map(x => x.id)).toContain(m.id)
  210. await b2.dispose()
  211. })
  212. it('rejects opening a database whose schema version is newer than this build', async () => {
  213. const path = await freshDbPath()
  214. openDatabase(path).close() // stamp user_version = SCHEMA_VERSION
  215. // Bump user_version past what this build supports.
  216. const db = openDatabase(path)
  217. db.exec(`PRAGMA user_version = ${SCHEMA_VERSION + 1}`)
  218. db.close()
  219. expect(() => openDatabase(path)).toThrow(/newer than this build/)
  220. })
  221. it('append snapshots the batch: mutating an event after the call does not corrupt the persisted copy', async () => {
  222. const ctx = new Context()
  223. await ctx.plugin(SessionStore)
  224. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  225. const m = meta('snapshot')
  226. await ctx.sessionPersistence.create(m)
  227. const batch: SessionEvent[] = [
  228. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  229. { type: 'user/message', seq: 1, time: 2, data: { content: [{ type: 'text', text: 'original' }], source: { kind: 'user' } } },
  230. { type: 'turn/end', seq: 2, time: 3, data: { turn: 1, reason: { kind: 'completed' } } },
  231. ]
  232. const p = ctx.sessionPersistence.append(m.id, batch)
  233. // Mutate the live array AND an event's data AFTER the call but before it
  234. // drains behind the per-session chain. The snapshot taken at call time must
  235. // shield the persisted copy.
  236. ;(batch[1]!.data as { content: { type: 'text'; text: string }[] }).content[0]!.text = 'HACKED'
  237. batch.push({ type: 'user/message', seq: 3, time: 4, data: { content: [{ type: 'text', text: 'injected' }], source: { kind: 'user' } } })
  238. await p
  239. const loaded = await ctx.sessionPersistence.load(m.id)
  240. expect(loaded.events).toHaveLength(3) // the pushed event was not persisted
  241. const um = loaded.events[1]
  242. expect(um?.type === 'user/message' && (um.data.content[0] as { text: string }).text).toBe('original')
  243. await fiber.dispose()
  244. })
  245. it('a corrupt-JSON row in the uncommitted tail is discarded on load, not unloadable', async () => {
  246. const path = await freshDbPath()
  247. const m = meta('corrupt-tail')
  248. const b1 = await backend(path)
  249. await b1.ctx.sessionPersistence.create(m)
  250. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog()) // committed: seqs 0..5
  251. await b1.dispose()
  252. // Hand-insert a torn tail row (seq 6, no closing turn/end) whose `data` is
  253. // invalid JSON. The contract: only a parse error in the COMMITTED region is
  254. // unloadable; a torn tail must be discarded. scanRows finds the last
  255. // turn/end on the seq+type columns (never parsing tail `data`), so the
  256. // unparsable row after it bounds the preserved prefix and is deleted by load.
  257. const db = openDatabase(path)
  258. db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, 6, ?, 7, ?)')
  259. .run(m.id, 'turn/start', '{not valid json')
  260. db.close()
  261. const b2 = await backend(path)
  262. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  263. expect(loaded.events).toEqual(oneTurnLog()) // torn tail discarded, committed intact (turn 1 already balanced → no closers)
  264. // load physically deleted the corrupt tail row, so a fresh append continues.
  265. await b2.ctx.sessionPersistence.append(m.id, [
  266. { type: 'turn/start', seq: 6, time: 8, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  267. { type: 'turn/end', seq: 7, time: 9, data: { turn: 2, reason: { kind: 'completed' } } },
  268. ])
  269. const reloaded = await b2.ctx.sessionPersistence.load(m.id)
  270. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  271. await b2.dispose()
  272. })
  273. it('append rolls back the whole batch on a mid-batch seq collision (transaction)', async () => {
  274. const ctx = new Context()
  275. await ctx.plugin(SessionStore)
  276. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  277. const m = meta('rollback')
  278. await ctx.sessionPersistence.create(m)
  279. await ctx.sessionPersistence.append(m.id, oneTurnLog()) // seqs 0..5
  280. // A batch that re-states an already-stored seq must be rejected and leave
  281. // the stored log unchanged (the UNIQUE (session_id, seq) constraint fires
  282. // inside the transaction → ROLLBACK).
  283. await expect(ctx.sessionPersistence.append(m.id, oneTurnLog())).rejects.toThrow()
  284. const loaded = await ctx.sessionPersistence.load(m.id)
  285. expect(loaded.events).toEqual(oneTurnLog()) // unchanged
  286. await fiber.dispose()
  287. })
  288. it('persists across separate backend instances over the same file', async () => {
  289. const path = await freshDbPath()
  290. const m = meta('persist', '/proj')
  291. const ctx1 = new Context()
  292. await ctx1.plugin(SessionStore)
  293. const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
  294. await ctx1.sessionPersistence.create(m)
  295. await ctx1.sessionPersistence.append(m.id, oneTurnLog())
  296. await ctx1.sessionPersistence.update(m.id, { title: 'T', firstPrompt: 'hi' })
  297. await fiber1.dispose()
  298. const ctx2 = new Context()
  299. await ctx2.plugin(SessionStore)
  300. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  301. expect((await ctx2.sessionPersistence.list()).map(x => x.id)).toContain(m.id)
  302. const loaded = await ctx2.sessionPersistence.load(m.id)
  303. expect(loaded.meta).toMatchObject({ id: m.id, cwd: '/proj', title: 'T', firstPrompt: 'hi' })
  304. expect(loaded.events).toEqual(oneTurnLog())
  305. await fiber2.dispose()
  306. })
  307. it('rejects an unknown format version on load', async () => {
  308. const path = await freshDbPath()
  309. // Materialize a row with version 2 directly via the real schema.
  310. const db = openDatabase(path)
  311. db.prepare('INSERT INTO sessions (id, version, created_at, updated_at) VALUES (?, ?, ?, ?)')
  312. .run('v2', 2, 1, 1)
  313. db.close()
  314. const ctx = new Context()
  315. await ctx.plugin(SessionStore)
  316. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path })
  317. await expect(ctx.sessionPersistence.load(SessionId('v2'))).rejects.toThrow(/version 2/)
  318. await fiber.dispose()
  319. })
  320. it('create rejects a duplicate id (in memory and on a persisted row)', async () => {
  321. const path = await freshDbPath()
  322. const m = meta('dup')
  323. const ctx = new Context()
  324. await ctx.plugin(SessionStore)
  325. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path })
  326. await ctx.sessionPersistence.create(m)
  327. // Same in-memory state.
  328. await expect(ctx.sessionPersistence.create(m)).rejects.toThrow(/already exists/)
  329. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  330. await fiber.dispose()
  331. // A fresh instance over the same file sees the persisted row.
  332. const ctx2 = new Context()
  333. await ctx2.plugin(SessionStore)
  334. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  335. await expect(ctx2.sessionPersistence.create(m)).rejects.toThrow(/already has a persisted row/)
  336. await fiber2.dispose()
  337. })
  338. it('exposes the schema version constant', () => {
  339. expect(SCHEMA_VERSION).toBe(1)
  340. })
  341. })
  342. describe('SessionPersistenceSqlite: write path (session/event → flush)', () => {
  343. function send(session: Session, events: SessionEvent[]): void {
  344. for (const e of events) session.append(e.type, e.data)
  345. }
  346. it('persists a turn appended through the live session on flush', async () => {
  347. const ctx = new Context()
  348. await ctx.plugin(SessionStore)
  349. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  350. const session = ctx.sessions.create('w1')
  351. send(session, oneTurnLog())
  352. await ctx.parallel('session/flush', session)
  353. const loaded = await ctx.sessionPersistence.load(SessionId('w1'))
  354. expect(loaded.events.map(e => e.type)).toEqual(oneTurnLog().map(e => e.type))
  355. await fiber.dispose()
  356. })
  357. it('a resumed session does not re-append its seed', async () => {
  358. const path = await freshDbPath()
  359. // Run 1: persist a full turn through the live session.
  360. const ctx1 = new Context()
  361. await ctx1.plugin(SessionStore)
  362. const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
  363. const s1 = ctx1.sessions.create('resume')
  364. for (const e of oneTurnLog()) s1.append(e.type, e.data)
  365. await ctx1.parallel('session/flush', s1)
  366. await fiber1.dispose()
  367. // Run 2: reconstruct the live session from the loaded log (seed), then add a
  368. // second turn. The seed must NOT be re-appended (no UNIQUE collision), and
  369. // the second turn continues the seq.
  370. const ctx2 = new Context()
  371. await ctx2.plugin(SessionStore)
  372. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  373. const { events } = await ctx2.sessionPersistence.load(SessionId('resume'))
  374. const s2 = ctx2.sessions.create('resume', { seed: events })
  375. s2.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  376. s2.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
  377. await ctx2.parallel('session/flush', s2)
  378. const reloaded = await ctx2.sessionPersistence.load(SessionId('resume'))
  379. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  380. await fiber2.dispose()
  381. })
  382. it('HMR: applying the plugin seeds existing live sessions', async () => {
  383. const ctx = new Context()
  384. await ctx.plugin(SessionStore)
  385. const session = ctx.sessions.create('hmr')
  386. for (const e of oneTurnLog()) session.append(e.type, e.data)
  387. // Plugin applied AFTER the session already has events.
  388. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  389. await ctx.parallel('session/flush', session)
  390. expect(await ctx.sessionPersistence.has(SessionId('hmr'))).toBe(true)
  391. await fiber.dispose()
  392. })
  393. it('dispose drains a pending buffer before closing the database', async () => {
  394. const path = await freshDbPath()
  395. const ctx = new Context()
  396. await ctx.plugin(SessionStore)
  397. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path })
  398. const session = ctx.sessions.create('drain')
  399. for (const e of oneTurnLog()) session.append(e.type, e.data)
  400. // No explicit flush — dispose must drain the buffer.
  401. await fiber.dispose()
  402. const ctx2 = new Context()
  403. await ctx2.plugin(SessionStore)
  404. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  405. expect(await ctx2.sessionPersistence.has(SessionId('drain'))).toBe(true)
  406. await fiber2.dispose()
  407. })
  408. it('rejects a different live session colliding on a persisted id', async () => {
  409. const path = await freshDbPath()
  410. const ctx1 = new Context()
  411. await ctx1.plugin(SessionStore)
  412. const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
  413. const s1 = ctx1.sessions.create('collide')
  414. for (const e of oneTurnLog()) s1.append(e.type, e.data)
  415. await ctx1.parallel('session/flush', s1)
  416. await fiber1.dispose()
  417. // A fresh, unrelated session reusing the id (no seed) must be rejected.
  418. const ctx2 = new Context()
  419. await ctx2.plugin(SessionStore)
  420. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  421. const s2 = ctx2.sessions.create('collide')
  422. s2.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  423. await expect(ctx2.parallel('session/flush', s2)).rejects.toThrow(/id collision/)
  424. await fiber2.dispose()
  425. })
  426. it('update before the first append keeps the summary in memory and the session lazy', async () => {
  427. const ctx = new Context()
  428. await ctx.plugin(SessionStore)
  429. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  430. const m = meta('lazy-update')
  431. await ctx.sessionPersistence.create(m)
  432. await ctx.sessionPersistence.update(m.id, { title: 'pending' })
  433. // Still lazy: no materialized row yet.
  434. expect(await ctx.sessionPersistence.has(m.id)).toBe(false)
  435. // The first append materializes and carries the pending title.
  436. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  437. const loaded = await ctx.sessionPersistence.load(m.id)
  438. expect(loaded.meta.title).toBe('pending')
  439. await fiber.dispose()
  440. })
  441. })
  442. describe('SessionPersistenceSqlite: edge cases', () => {
  443. it('append of an empty batch is a no-op', async () => {
  444. const { ctx, dispose } = await backend()
  445. const m = meta('empty-batch')
  446. await ctx.sessionPersistence.create(m)
  447. await ctx.sessionPersistence.append(m.id, [])
  448. expect(await ctx.sessionPersistence.has(m.id)).toBe(false) // still lazy
  449. await dispose()
  450. })
  451. it('load rejects a missing session', async () => {
  452. const { ctx, dispose } = await backend()
  453. await expect(ctx.sessionPersistence.load(SessionId('nope'))).rejects.toThrow(/not found/)
  454. await dispose()
  455. })
  456. it('delete of a non-existent session is a no-op', async () => {
  457. const { ctx, dispose } = await backend()
  458. await ctx.sessionPersistence.delete(SessionId('ghost'))
  459. expect(await ctx.sessionPersistence.has(SessionId('ghost'))).toBe(false)
  460. await dispose()
  461. })
  462. it('append adopts a session that exists only in the DB (fresh instance)', async () => {
  463. const path = await freshDbPath()
  464. const m = meta('adopt-append')
  465. const b1 = await backend(path)
  466. await b1.ctx.sessionPersistence.create(m)
  467. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
  468. await b1.dispose()
  469. // A fresh instance appends a second turn WITHOUT a prior create/load: append
  470. // must adopt the on-disk row (cursor = stored length) and continue the seq.
  471. const b2 = await backend(path)
  472. await b2.ctx.sessionPersistence.append(m.id, [
  473. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  474. { type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
  475. ])
  476. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  477. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  478. await b2.dispose()
  479. })
  480. it('update adopts a session that exists only in the DB (fresh instance)', async () => {
  481. const path = await freshDbPath()
  482. const m = meta('adopt-update')
  483. const b1 = await backend(path)
  484. await b1.ctx.sessionPersistence.create(m)
  485. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
  486. await b1.dispose()
  487. const b2 = await backend(path)
  488. await b2.ctx.sessionPersistence.update(m.id, { title: 'after restart' })
  489. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  490. expect(loaded.meta.title).toBe('after restart')
  491. await b2.dispose()
  492. })
  493. it('append rolls back and rethrows when an event INSERT fails inside the transaction', async () => {
  494. const path = await freshDbPath()
  495. const m = meta('rollback-insert')
  496. const b1 = await backend(path)
  497. await b1.ctx.sessionPersistence.create(m)
  498. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
  499. // A SECOND backend over the same file loads the session first, so it adopts
  500. // cursor 6 (the committed length) into its OWN in-memory state.
  501. const b2 = await backend(path)
  502. await b2.ctx.sessionPersistence.load(m.id) // cursor 6 in b2
  503. const turn2: SessionEvent[] = [
  504. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  505. { type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
  506. ]
  507. // b1 commits seq 6..7 first.
  508. await b1.ctx.sessionPersistence.append(m.id, turn2)
  509. // b2 still thinks its cursor is 6, so this batch passes the contiguity check
  510. // but its INSERT of seq 6 hits the UNIQUE (session_id, seq) constraint
  511. // mid-transaction → ROLLBACK + rethrow.
  512. await expect(b2.ctx.sessionPersistence.append(m.id, turn2)).rejects.toThrow(/UNIQUE/)
  513. // b1's turn is intact; b2's rolled-back attempt left nothing extra.
  514. const loaded = await b1.ctx.sessionPersistence.load(m.id)
  515. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  516. await b1.dispose()
  517. await b2.dispose()
  518. })
  519. it('round-trips a header with parentSession (fork lineage)', async () => {
  520. const { ctx, dispose } = await backend()
  521. const m: SessionMeta = { ...meta('child'), parentSession: SessionId('parent') }
  522. await ctx.sessionPersistence.create(m)
  523. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  524. const loaded = await ctx.sessionPersistence.load(m.id)
  525. expect(loaded.meta.parentSession).toBe(SessionId('parent'))
  526. await dispose()
  527. })
  528. it('a fresh live session reusing a previously-loaded id is rejected (ownerless guard)', async () => {
  529. const path = await freshDbPath()
  530. const m = meta('ownerless')
  531. const b1 = await backend(path)
  532. await b1.ctx.sessionPersistence.create(m)
  533. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
  534. await b1.dispose()
  535. const b2 = await backend(path)
  536. // load() leaves ownerless state with cursor 6.
  537. await b2.ctx.sessionPersistence.load(m.id)
  538. // A fresh, unrelated live session reusing the id has a shorter/non-matching
  539. // seed → its onCreated must reject rather than graft onto the loaded prefix.
  540. const s = b2.ctx.sessions.create('ownerless')
  541. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  542. await expect(b2.ctx.parallel('session/flush', s)).rejects.toThrow(/id collision/)
  543. await b2.dispose()
  544. })
  545. it('a live session whose seed matches the loaded prefix claims ownerless state and persists the suffix', async () => {
  546. const path = await freshDbPath()
  547. const m = meta('claim')
  548. const b1 = await backend(path)
  549. await b1.ctx.sessionPersistence.create(m)
  550. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
  551. await b1.dispose()
  552. const b2 = await backend(path)
  553. const { events } = await b2.ctx.sessionPersistence.load(m.id) // ownerless, cursor 6
  554. // A live session seeded with the loaded log PLUS a new turn claims the state
  555. // and persists only the suffix.
  556. const s = b2.ctx.sessions.create('claim', { seed: [
  557. ...events,
  558. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  559. { type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
  560. ] })
  561. await b2.ctx.parallel('session/flush', s)
  562. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  563. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  564. await b2.dispose()
  565. })
  566. it('an abandoned lazy session (never materialized) releases its id for reuse', async () => {
  567. const { ctx, dispose } = await backend()
  568. const inits = (ctx.sessionPersistence as unknown as { inits: Map<Session, Promise<void>> }).inits
  569. let first!: Session
  570. const firstFiber = await ctx.plugin(Object.assign((inner: Context) => {
  571. first = inner.sessions.create('reuse')
  572. }, { inject: ['sessions'] }))
  573. await inits.get(first) // let the lazy create register the state
  574. await firstFiber.dispose() // disposed before any append → never materialized
  575. let reuse!: Session
  576. await ctx.plugin(Object.assign((inner: Context) => {
  577. reuse = inner.sessions.create('reuse')
  578. }, { inject: ['sessions'] }))
  579. await expect(inits.get(reuse)).resolves.toBeUndefined()
  580. reuse.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  581. reuse.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  582. await ctx.parallel('session/flush', reuse)
  583. expect(await ctx.sessionPersistence.has(SessionId('reuse'))).toBe(true)
  584. await dispose()
  585. })
  586. it('does NOT reclaim an id whose abandoned owner still has buffered (unflushed) events', async () => {
  587. const { ctx, dispose } = await backend()
  588. const inits = (ctx.sessionPersistence as unknown as { inits: Map<Session, Promise<void>> }).inits
  589. let first!: Session
  590. const firstFiber = await ctx.plugin(Object.assign((inner: Context) => {
  591. first = inner.sessions.create('buffered')
  592. }, { inject: ['sessions'] }))
  593. await inits.get(first)
  594. // Append a turn but do NOT flush — events sit in the write-behind buffer.
  595. first.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  596. first.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  597. await firstFiber.dispose() // disposed before flush; not materialized, buffer pending
  598. let reuse!: Session
  599. await ctx.plugin(Object.assign((inner: Context) => {
  600. reuse = inner.sessions.create('buffered')
  601. }, { inject: ['sessions'] }))
  602. await expect(inits.get(reuse)).rejects.toThrow(/already bound to a different live session/)
  603. await dispose()
  604. })
  605. it('initFor is idempotent: re-emitting session/created does not re-initialize', async () => {
  606. const { ctx, dispose } = await backend()
  607. const session = ctx.sessions.create('idem')
  608. ctx.emit('session/created', session) // second create event for the same object
  609. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  610. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  611. await ctx.parallel('session/flush', session)
  612. expect(await ctx.sessionPersistence.has(SessionId('idem'))).toBe(true)
  613. await dispose()
  614. })
  615. it('a live session claims cursor-0 ownerless state created via the public API and persists its seed', async () => {
  616. const { ctx, dispose } = await backend()
  617. // create() registers ownerless state with cursor 0 (no events yet).
  618. await ctx.sessionPersistence.create(meta('cursor0'))
  619. // A live session reusing that id, seeded with a turn, claims the ownerless
  620. // state (cursor 0 trivially matches any seed) and persists the whole seed.
  621. const s = ctx.sessions.create('cursor0', { seed: [
  622. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  623. { type: 'turn/end', seq: 1, time: 2, data: { turn: 1, reason: { kind: 'completed' } } },
  624. ] })
  625. await ctx.parallel('session/flush', s)
  626. const loaded = await ctx.sessionPersistence.load(SessionId('cursor0'))
  627. expect(loaded.events.map(e => e.seq)).toEqual([0, 1])
  628. await dispose()
  629. })
  630. it('HMR: reloading the backend adopts a still-live, already-materialized session', async () => {
  631. const path = await freshDbPath()
  632. const ctx = new Context()
  633. await ctx.plugin(SessionStore)
  634. // The session lives in its OWN fiber so it survives the backend reload.
  635. let session!: Session
  636. await ctx.plugin(Object.assign((inner: Context) => {
  637. session = inner.sessions.create('hmr-adopt')
  638. }, { inject: ['sessions'] }))
  639. // Backend instance 1 materializes the session on disk.
  640. const backend1 = await ctx.plugin(SessionPersistenceSqlite, { path })
  641. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  642. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } })
  643. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  644. await ctx.parallel('session/flush', session)
  645. // Hot-reload: dispose instance 1, plug in instance 2 over the SAME file
  646. // while the session stays live. Instance 2 has an empty states map but the
  647. // row is materialized on disk and is a prefix of the live events — it must
  648. // ADOPT (not reject), and a second turn then persists.
  649. await backend1.dispose()
  650. await ctx.plugin(SessionPersistenceSqlite, { path })
  651. session.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  652. session.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
  653. await expect(ctx.parallel('session/flush', session)).resolves.not.toThrow()
  654. const loaded = await ctx.sessionPersistence.load(SessionId('hmr-adopt'))
  655. expect(loaded.events.filter(e => e.type === 'turn/start')).toHaveLength(2)
  656. await ctx.fiber.dispose()
  657. })
  658. it('HMR: adoption persists the live SUFFIX that was ahead of the on-disk prefix', async () => {
  659. const path = await freshDbPath()
  660. const ctx = new Context()
  661. await ctx.plugin(SessionStore)
  662. let session!: Session
  663. await ctx.plugin(Object.assign((inner: Context) => {
  664. session = inner.sessions.create('hmr-suffix')
  665. }, { inject: ['sessions'] }))
  666. const backend1 = await ctx.plugin(SessionPersistenceSqlite, { path })
  667. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  668. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  669. await ctx.parallel('session/flush', session)
  670. // Append turn 2 to the LIVE session, then dispose instance 1 WITHOUT
  671. // flushing turn 2: it is now ONLY in the live session's events.
  672. await backend1.dispose()
  673. session.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  674. session.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
  675. // Instance 2 adopts the on-disk prefix (turn 1) and MUST persist the live
  676. // suffix (turn 2) carried in the session's events.
  677. await ctx.plugin(SessionPersistenceSqlite, { path })
  678. await ctx.parallel('session/flush', session)
  679. const loaded = await ctx.sessionPersistence.load(SessionId('hmr-suffix'))
  680. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3])
  681. expect(loaded.events.filter(e => e.type === 'turn/start')).toHaveLength(2)
  682. await ctx.fiber.dispose()
  683. })
  684. it('HMR: a DIFFERENT session colliding with a materialized on-disk id is rejected', async () => {
  685. const path = await freshDbPath()
  686. // Instance 1 materializes a session and disposes.
  687. const b1 = await backend(path)
  688. const s1 = b1.ctx.sessions.create('hmr-collide')
  689. for (const e of oneTurnLog()) s1.append(e.type, e.data)
  690. await b1.ctx.parallel('session/flush', s1)
  691. await b1.dispose()
  692. // A fresh context with an UNRELATED live session reusing the id meets a
  693. // materialized row that is NOT a prefix of its events → reject.
  694. const ctx = new Context()
  695. await ctx.plugin(SessionStore)
  696. let session!: Session
  697. await ctx.plugin(Object.assign((inner: Context) => {
  698. session = inner.sessions.create('hmr-collide')
  699. }, { inject: ['sessions'] }))
  700. session.append('turn/start', { turn: 9, trigger: { kind: 'message', source: { kind: 'user' } } })
  701. await ctx.plugin(SessionPersistenceSqlite, { path })
  702. await expect(ctx.parallel('session/flush', session)).rejects.toThrow(/id collision/)
  703. await ctx.fiber.dispose()
  704. })
  705. })