sqlite.spec.ts 34 KB

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