sqlite.spec.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { existsSync } from 'node:fs'
  4. import { mkdtemp, rm, symlink } from 'node:fs/promises'
  5. import { tmpdir } from 'node:os'
  6. import { join } from 'node:path'
  7. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  8. import type { Session, SessionEvent, SurfaceEvent, SurfaceEventType } from '@deepseek-ai/dsh-session'
  9. import SessionPersistenceSqlite, { SCHEMA_VERSION } from '@deepseek-ai/dsh-session-persistence-sqlite'
  10. import { openDatabase, rowToEvent, scanRows, type EventRow } from '../src/schema.ts'
  11. import { runPersistenceContract, meta, oneTurnLog, appendLog } from '../../session-persistence/tests/contract.ts'
  12. import { runCoordinatorContract, type CoordinatorFixture } from '../../session-persistence/tests/coordinator-contract.ts'
  13. const dirs: string[] = []
  14. afterEach(async () => { for (const d of dirs.splice(0)) await rm(d, { recursive: true, force: true }) })
  15. async function expectParallelFlushError(promise: Promise<unknown>, message: RegExp): Promise<void> {
  16. try {
  17. await promise
  18. } catch (error) {
  19. expect(error).toBeInstanceOf(AggregateError)
  20. const [cause] = (error as AggregateError).errors as unknown[]
  21. expect(cause).toBeInstanceOf(Error)
  22. expect((cause as Error).message).toMatch(message)
  23. return
  24. }
  25. throw new Error('expected parallel flush to reject')
  26. }
  27. async function freshDbPath(): Promise<string> {
  28. const dir = await mkdtemp(join(tmpdir(), 'dsh-sqlite-'))
  29. dirs.push(dir)
  30. return join(dir, 'sessions.db')
  31. }
  32. /** A context with the session store + SQLite backend, plus a teardown. */
  33. async function backend(path = ':memory:'): Promise<{ ctx: Context; dispose: () => Promise<void> }> {
  34. const ctx = new Context()
  35. await ctx.plugin(SessionStore)
  36. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path })
  37. return { ctx, dispose: () => fiber.dispose() }
  38. }
  39. // Run the same backend-agnostic contract as JSONL to pin identical semantics.
  40. runPersistenceContract('sqlite', async () => {
  41. const ctx = new Context()
  42. await ctx.plugin(SessionStore)
  43. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  44. return {
  45. persistence: ctx.sessionPersistence,
  46. dispose: async () => { await fiber.dispose() },
  47. }
  48. })
  49. // A file-backed database lets two mounts share rows across reload. `corruptTail` inserts invalid
  50. // JSON past the committed seq, exercising coordinator repair against real database rows.
  51. runCoordinatorContract('sqlite', async (): Promise<CoordinatorFixture> => {
  52. const dir = await mkdtemp(join(tmpdir(), 'dsh-sqlite-coord-'))
  53. const path = join(dir, 'sessions.db')
  54. return {
  55. mount: async ctx => ctx.plugin(SessionPersistenceSqlite, { path }),
  56. corruptTail: async (id) => {
  57. // A row past the committed region whose `data` does not parse: scanRows
  58. // bounds the preserved prefix at it and returns its seq as tornFrom, which
  59. // the backend surfaces to the coordinator as the tornMarker to delete from.
  60. const db = openDatabase(path, 'wal')
  61. const next = (db.prepare('SELECT COALESCE(MAX(seq), -1) + 1 AS n FROM events WHERE session_id = ?')
  62. .get(id) as { n: number }).n
  63. db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, ?, ?, ?, ?)')
  64. .run(id, next, 'assistant/chunk', 99, '{not valid json')
  65. db.close()
  66. },
  67. cleanup: async () => { await rm(dir, { recursive: true, force: true }) },
  68. }
  69. })
  70. describe('scanRows', () => {
  71. // scanRows works off EventRows (data is a JSON string column); build them from SessionEvents
  72. // so the unit tests read in terms of the event vocabulary. Surface metadata is serialized to
  73. // its nullable columns so the conversion remains faithful.
  74. const rows = (events: SessionEvent[]): EventRow[] =>
  75. events.map((e) => {
  76. const se = e as SessionEvent<SurfaceEventType>
  77. return {
  78. seq: e.seq, type: e.type, time: e.time, data: JSON.stringify(e.data),
  79. source_event_seqs: se.sourceEventSeqs !== undefined ? JSON.stringify(se.sourceEventSeqs) : null,
  80. surface_op: se.surfaceOp !== undefined ? JSON.stringify(se.surfaceOp) : null,
  81. }
  82. })
  83. it('preserves the full log when it ends exactly on a turn/end (no torn tail)', () => {
  84. const { preserved, tornFrom } = scanRows(rows(oneTurnLog()))
  85. expect(preserved).toEqual(oneTurnLog())
  86. expect(tornFrom).toBeUndefined()
  87. })
  88. it('PRESERVES the real events of an interrupted turn after the last turn/end', () => {
  89. // turn 1 committed (0..5) + a crashed turn 2 (turn/start 6, step/start 7, no
  90. // close): all 8 rows are intact, so the whole prefix is preserved and there
  91. // is no torn fragment to delete. (load() then synthesizes the closers.)
  92. const withOpenTurn: SessionEvent[] = [
  93. ...oneTurnLog(),
  94. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  95. { type: 'step/start', seq: 7, time: 8, data: { turn: 2, step: 1 } },
  96. ]
  97. const { preserved, tornFrom } = scanRows(rows(withOpenTurn))
  98. expect(preserved.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  99. expect(tornFrom).toBeUndefined()
  100. })
  101. it('preserves the contiguous prefix and flags a torn tail at a seq gap', () => {
  102. // A gap after seq 0 (no committed turn/end): seq 0 is the preserved
  103. // interrupted-turn event; the gap bounds it and marks the torn fragment.
  104. const gapped: SessionEvent[] = [
  105. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  106. { type: 'step/start', seq: 2, time: 2, data: { turn: 1, step: 1 } }, // seq 1 missing
  107. ]
  108. const { preserved, tornFrom } = scanRows(rows(gapped))
  109. expect(preserved.map(e => e.seq)).toEqual([0])
  110. expect(tornFrom).toBe(1)
  111. })
  112. it('an empty log preserves nothing and has no torn tail', () => {
  113. expect(scanRows([])).toEqual({ preserved: [] })
  114. })
  115. it('throws on a seq gap inside the committed region (before the last turn/end)', () => {
  116. const gapped: SessionEvent[] = [
  117. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  118. { type: 'step/start', seq: 2, time: 2, data: { turn: 1, step: 1 } }, // seq 1 missing
  119. { type: 'turn/end', seq: 3, time: 3, data: { turn: 1, reason: { kind: 'completed' } } },
  120. ]
  121. expect(() => scanRows(rows(gapped))).toThrow(/seq gap in committed region/)
  122. })
  123. it('throws on an unparsable row inside the committed region', () => {
  124. const withCorruptCommitted: EventRow[] = [
  125. { seq: 0, type: 'turn/start', time: 1, data: '{not json', source_event_seqs: null, surface_op: null }, // corrupt, sits before a turn/end
  126. { seq: 1, type: 'turn/end', time: 2, data: JSON.stringify({ turn: 1, reason: { kind: 'completed' } }), source_event_seqs: null, surface_op: null },
  127. ]
  128. expect(() => scanRows(withCorruptCommitted)).toThrow(/unparsable committed event/)
  129. })
  130. it('tolerates an unparsable torn-tail row after the last turn/end', () => {
  131. const withCorruptTail: EventRow[] = [
  132. ...rows(oneTurnLog()),
  133. { seq: 6, type: 'turn/start', time: 7, data: '{not json', source_event_seqs: null, surface_op: null }, // torn fragment, no committed turn/end after
  134. ]
  135. const { preserved, tornFrom } = scanRows(withCorruptTail)
  136. expect(preserved).toEqual(oneTurnLog())
  137. expect(tornFrom).toBe(6)
  138. })
  139. })
  140. describe('SessionPersistenceSqlite: durability and crash semantics', () => {
  141. it('an interrupted turn (rows after the last turn/end) is PRESERVED and closed during load', async () => {
  142. const path = await freshDbPath()
  143. const m = meta('crash')
  144. // Run 1: persist a complete turn, then a half-written second turn (no turn/end).
  145. const ctx1 = new Context()
  146. await ctx1.plugin(SessionStore)
  147. const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
  148. await ctx1.sessionPersistence.create(m)
  149. await ctx1.sessionPersistence.append(m.id, oneTurnLog())
  150. await ctx1.sessionPersistence.append(m.id, [
  151. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  152. { type: 'step/start', seq: 7, time: 8, data: { turn: 2, step: 1 } },
  153. ])
  154. await fiber1.dispose()
  155. // Run 2: load PRESERVES the interrupted turn's real events (a turn can be huge
  156. // — never truncated) and closes the orphaned turn with synthetic boundary
  157. // events: step/end (the step was open) then turn/end {interrupted}.
  158. const ctx2 = new Context()
  159. await ctx2.plugin(SessionStore)
  160. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  161. const loaded = await ctx2.sessionPersistence.load(m.id)
  162. expect(loaded.events.map(e => e.type)).toEqual([
  163. 'turn/start', 'user/message', 'step/start', 'assistant/message', 'step/end', 'turn/end', // turn 1
  164. 'turn/start', 'step/start', 'step/end', 'turn/end', // turn 2: real events + synthetic closers
  165. ])
  166. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  167. const last = loaded.events.at(-1)!
  168. expect(last.type === 'turn/end' && last.data.reason).toEqual({ kind: 'interrupted' })
  169. // load durably closed the turn, so the next append continues at the balanced
  170. // length (seq 10) and a reload round-trips identically.
  171. await ctx2.sessionPersistence.append(m.id, [
  172. { type: 'turn/start', seq: 10, time: 9, data: { turn: 3, trigger: { kind: 'message', source: { kind: 'user' } } } },
  173. { type: 'turn/end', seq: 11, time: 10, data: { turn: 3, reason: { kind: 'completed' } } },
  174. ])
  175. const reloaded = await ctx2.sessionPersistence.load(m.id)
  176. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
  177. await fiber2.dispose()
  178. })
  179. it('load() durably closes the interrupted turn: the synthetic closers are on disk after load', async () => {
  180. const path = await freshDbPath()
  181. const m = meta('load-closes')
  182. const b1 = await backend(path)
  183. await b1.ctx.sessionPersistence.create(m)
  184. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog()) // seqs 0..5
  185. await b1.dispose()
  186. // Hand-write an interrupted turn (turn/start seq 6, no turn/end).
  187. const db = openDatabase(path, 'wal')
  188. db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, 6, ?, 7, ?)')
  189. .run(m.id, 'turn/start', JSON.stringify({ turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } }))
  190. db.close()
  191. const b2 = await backend(path)
  192. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  193. // turn 2's real turn/start (seq 6) is preserved + a synthetic turn/end (seq 7).
  194. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  195. expect(loaded.events.at(-1)!.type).toBe('turn/end')
  196. // load() is mutating: the synthetic turn/end MUST be on disk so the stored log
  197. // is balanced and the cursor is truthful (contract: load closes, not defers).
  198. const probe = openDatabase(path, 'wal')
  199. const stored = probe.prepare('SELECT seq, type FROM events WHERE session_id = ? ORDER BY seq').all(m.id) as { seq: number; type: string }[]
  200. probe.close()
  201. expect(stored.map(r => r.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  202. expect(stored.at(-1)!.type).toBe('turn/end')
  203. await b2.dispose()
  204. })
  205. it('all-tail load: a session whose only turn never closed is preserved and closed on load', async () => {
  206. const path = await freshDbPath()
  207. const m = meta('all-tail')
  208. const b1 = await backend(path)
  209. await b1.ctx.sessionPersistence.create(m)
  210. // A first turn that NEVER completed: turn/start + user/message, no turn/end.
  211. await b1.ctx.sessionPersistence.append(m.id, [
  212. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  213. { type: 'user/message', seq: 1, time: 2, data: { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } } },
  214. ])
  215. await b1.dispose()
  216. // A fresh backend loads it: the interrupted (only) turn's real events are
  217. // preserved and closed with a synthetic turn/end {interrupted} — NOT
  218. // truncated. The session was materialized, so list() reports it present.
  219. const b2 = await backend(path)
  220. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  221. expect(loaded.events.map(e => e.type)).toEqual(['turn/start', 'user/message', 'turn/end'])
  222. expect(loaded.events.at(-1)!.type === 'turn/end' && loaded.events.at(-1)!.data).toMatchObject({ reason: { kind: 'interrupted' } })
  223. expect((await b2.ctx.sessionPersistence.list()).map(x => x.id)).toContain(m.id)
  224. await b2.dispose()
  225. })
  226. it('rejects opening a database whose schema version is not the current build (newer OR older)', async () => {
  227. const path = await freshDbPath()
  228. openDatabase(path, 'wal').close() // stamp user_version = SCHEMA_VERSION
  229. // Bump user_version past what this build supports.
  230. const dbNewer = openDatabase(path, 'wal')
  231. dbNewer.exec(`PRAGMA user_version = ${SCHEMA_VERSION + 1}`)
  232. dbNewer.close()
  233. expect(() => openDatabase(path, 'wal')).toThrow(/incompatible with this build/)
  234. // The immediately preceding layout lacks the required store identity and is
  235. // rejected rather than migrated (unreleased software, no backward-compat).
  236. const olderPath = await freshDbPath()
  237. openDatabase(olderPath, 'wal').close()
  238. const dbOlder = openDatabase(olderPath, 'wal')
  239. dbOlder.exec(`PRAGMA user_version = ${SCHEMA_VERSION - 1}`)
  240. dbOlder.close()
  241. expect(() => openDatabase(olderPath, 'wal')).toThrow(/incompatible with this build/)
  242. })
  243. it('rejects a sibling v3 database (the merge-collided version) rather than opening it against missing columns', async () => {
  244. // Version 3 identified two incompatible sibling layouts, so it is always rejected.
  245. const path = await freshDbPath()
  246. openDatabase(path, 'wal').close() // creates + stamps user_version = SCHEMA_VERSION
  247. const db = openDatabase(path, 'wal')
  248. db.exec('PRAGMA user_version = 3')
  249. db.close()
  250. expect(() => openDatabase(path, 'wal')).toThrow(/schema version 3, incompatible with this build/)
  251. })
  252. it('a corrupt-JSON row in the uncommitted tail is discarded on load, not unloadable', async () => {
  253. const path = await freshDbPath()
  254. const m = meta('corrupt-tail')
  255. const b1 = await backend(path)
  256. await b1.ctx.sessionPersistence.create(m)
  257. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog()) // committed: seqs 0..5
  258. await b1.dispose()
  259. // A torn row after the last committed turn has invalid JSON. `scanRows` locates the boundary
  260. // from seq/type columns without parsing the tail, preserves the committed prefix, and load
  261. // deletes the row; invalid JSON inside the committed region would remain fatal.
  262. const db = openDatabase(path, 'wal')
  263. db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, 6, ?, 7, ?)')
  264. .run(m.id, 'turn/start', '{not valid json')
  265. db.close()
  266. const b2 = await backend(path)
  267. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  268. expect(loaded.events).toEqual(oneTurnLog()) // torn tail discarded, committed intact (turn 1 already balanced → no closers)
  269. // load physically deleted the corrupt tail row, so a fresh append continues.
  270. await b2.ctx.sessionPersistence.append(m.id, [
  271. { type: 'turn/start', seq: 6, time: 8, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  272. { type: 'turn/end', seq: 7, time: 9, data: { turn: 2, reason: { kind: 'completed' } } },
  273. ])
  274. const reloaded = await b2.ctx.sessionPersistence.load(m.id)
  275. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  276. await b2.dispose()
  277. })
  278. it('append rolls back the whole batch on a mid-batch seq collision (transaction)', async () => {
  279. const ctx = new Context()
  280. await ctx.plugin(SessionStore)
  281. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  282. const m = meta('rollback')
  283. await ctx.sessionPersistence.create(m)
  284. await ctx.sessionPersistence.append(m.id, oneTurnLog()) // seqs 0..5
  285. // A batch that re-states an already-stored seq must be rejected and leave
  286. // the stored log unchanged (the UNIQUE (session_id, seq) constraint fires
  287. // inside the transaction → ROLLBACK).
  288. await expect(ctx.sessionPersistence.append(m.id, oneTurnLog())).rejects.toThrow()
  289. const loaded = await ctx.sessionPersistence.load(m.id)
  290. expect(loaded.events).toEqual(oneTurnLog()) // unchanged
  291. await fiber.dispose()
  292. })
  293. it('persists across separate backend instances over the same file', async () => {
  294. const path = await freshDbPath()
  295. const m = meta('persist', '/proj')
  296. const ctx1 = new Context()
  297. await ctx1.plugin(SessionStore)
  298. const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
  299. await ctx1.sessionPersistence.create(m)
  300. await ctx1.sessionPersistence.append(m.id, oneTurnLog())
  301. await fiber1.dispose()
  302. const ctx2 = new Context()
  303. await ctx2.plugin(SessionStore)
  304. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  305. expect((await ctx2.sessionPersistence.list()).map(x => x.id)).toContain(m.id)
  306. const loaded = await ctx2.sessionPersistence.load(m.id)
  307. expect(loaded.meta).toMatchObject({ id: m.id, cwd: '/proj' })
  308. expect(loaded.events).toEqual(oneTurnLog())
  309. await fiber2.dispose()
  310. })
  311. it('source-qualifies revisions across stores while preserving same-file reopen identity', async () => {
  312. const pathA = await freshDbPath()
  313. const pathB = await freshDbPath()
  314. const m = meta('revision-source')
  315. const a = await backend(pathA)
  316. await a.ctx.sessionPersistence.create(m)
  317. await a.ctx.sessionPersistence.append(m.id, oneTurnLog())
  318. const revisionA = (await a.ctx.sessionPersistence.listSnapshots())[0]?.revision
  319. await a.dispose()
  320. const probeA = openDatabase(pathA, 'wal')
  321. const storeIdA = (probeA.prepare(
  322. 'SELECT store_id FROM persistence_state WHERE singleton = 1',
  323. ).get() as { store_id: string }).store_id
  324. probeA.close()
  325. const aliasA = `${pathA}.alias`
  326. await symlink(pathA, aliasA)
  327. const reopenedA = await backend(aliasA)
  328. expect((await reopenedA.ctx.sessionPersistence.listSnapshots())[0]?.revision).toBe(revisionA)
  329. await reopenedA.dispose()
  330. const b = await backend(pathB)
  331. await b.ctx.sessionPersistence.create(m)
  332. await b.ctx.sessionPersistence.append(m.id, oneTurnLog())
  333. const revisionB = (await b.ctx.sessionPersistence.listSnapshots())[0]?.revision
  334. const probeB = openDatabase(pathB, 'wal')
  335. const storeIdB = (probeB.prepare(
  336. 'SELECT store_id FROM persistence_state WHERE singleton = 1',
  337. ).get() as { store_id: string }).store_id
  338. probeB.close()
  339. expect(storeIdB).not.toBe(storeIdA)
  340. expect(revisionB).not.toBe(revisionA)
  341. expect(String(revisionA)).toMatch(/:revision:1$/)
  342. expect(String(revisionB)).toMatch(/:revision:1$/)
  343. await b.dispose()
  344. })
  345. it('exposes the schema version constant', () => {
  346. expect(SCHEMA_VERSION).toBe(6)
  347. })
  348. it('keeps the revision stable for an empty repair hook', async () => {
  349. const b = await backend()
  350. const m = meta('empty-repair')
  351. await b.ctx.sessionPersistence.create(m)
  352. await b.ctx.sessionPersistence.append(m.id, oneTurnLog())
  353. const before = await b.ctx.sessionPersistence.listSnapshots()
  354. await (b.ctx.sessionPersistence as SessionPersistenceSqlite).commitRepair(m, undefined, [])
  355. expect(await b.ctx.sessionPersistence.listSnapshots()).toEqual(before)
  356. await b.dispose()
  357. })
  358. })
  359. describe('SessionPersistenceSqlite: edge cases', () => {
  360. it('rejects and closes a current-schema database with an invalid store identity', async () => {
  361. const path = await freshDbPath()
  362. const db = openDatabase(path, 'wal')
  363. db.exec("UPDATE persistence_state SET store_id = '' WHERE singleton = 1")
  364. db.close()
  365. const b = await backend(path)
  366. await expect(b.ctx.sessionPersistence.listSnapshots()).rejects.toThrow(/no valid store identity/)
  367. await expect(b.dispose()).resolves.toBeUndefined()
  368. })
  369. it('append rolls back and rethrows when an event INSERT fails inside the transaction', async () => {
  370. const path = await freshDbPath()
  371. const m = meta('rollback-insert')
  372. const b1 = await backend(path)
  373. await b1.ctx.sessionPersistence.create(m)
  374. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
  375. // A SECOND backend over the same file loads the session first, so it adopts
  376. // cursor 6 (the committed length) into its OWN in-memory state.
  377. const b2 = await backend(path)
  378. await b2.ctx.sessionPersistence.load(m.id) // cursor 6 in b2
  379. const turn2: SessionEvent[] = [
  380. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  381. { type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
  382. ]
  383. // b1 commits seq 6..7 first.
  384. await b1.ctx.sessionPersistence.append(m.id, turn2)
  385. // b2 still thinks its cursor is 6, so this batch passes the contiguity check
  386. // but its INSERT of seq 6 hits the UNIQUE (session_id, seq) constraint
  387. // mid-transaction → ROLLBACK + rethrow.
  388. await expect(b2.ctx.sessionPersistence.append(m.id, turn2)).rejects.toThrow(/UNIQUE/)
  389. // b1's turn is intact; b2's rolled-back attempt left nothing extra.
  390. const loaded = await b1.ctx.sessionPersistence.load(m.id)
  391. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  392. await b1.dispose()
  393. await b2.dispose()
  394. })
  395. it('journalMode config reaches the database (default wal, rollback modes selectable)', async () => {
  396. // :memory: databases always report journal_mode=memory, so probe file DBs.
  397. const walPath = await freshDbPath()
  398. const bWal = await backend(walPath)
  399. await bWal.ctx.sessionPersistence.create(meta('jm-wal'))
  400. expect((openDatabase(walPath, 'wal').prepare('PRAGMA journal_mode').get() as { journal_mode: string }).journal_mode).toBe('wal')
  401. await bWal.dispose()
  402. const deletePath = await freshDbPath()
  403. const ctx = new Context()
  404. await ctx.plugin(SessionStore)
  405. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: deletePath, journalMode: 'delete' })
  406. await ctx.sessionPersistence.create(meta('jm-delete'))
  407. // Probe through a second connection: journal_mode=delete is a per-database
  408. // property only insofar as no WAL files exist — assert the world, not the
  409. // backend's self-report (no -wal sidecar after writes in delete mode).
  410. const db = openDatabase(deletePath, 'delete')
  411. expect((db.prepare('PRAGMA journal_mode').get() as { journal_mode: string }).journal_mode).toBe('delete')
  412. db.close()
  413. expect(existsSync(`${deletePath}-wal`)).toBe(false)
  414. await fiber.dispose()
  415. })
  416. it('HMR: a DIFFERENT session colliding with a materialized on-disk id is rejected', async () => {
  417. const path = await freshDbPath()
  418. // Instance 1 materializes a session and disposes.
  419. const b1 = await backend(path)
  420. const s1 = b1.ctx.sessions.create(SessionId('hmr-collide'))
  421. appendLog(s1, oneTurnLog())
  422. await b1.ctx.parallel('session/flush', s1)
  423. await b1.dispose()
  424. // A fresh context with an UNRELATED live session reusing the id meets a
  425. // materialized row that is NOT a prefix of its events → reject.
  426. const ctx = new Context()
  427. await ctx.plugin(SessionStore)
  428. let session!: Session
  429. await ctx.plugin(Object.assign((inner: Context) => {
  430. session = inner.sessions.create(SessionId('hmr-collide'))
  431. }, { inject: ['sessions'] }))
  432. session.append('turn/start', { turn: 9, trigger: { kind: 'message', source: { kind: 'user' } } })
  433. await ctx.plugin(SessionPersistenceSqlite, { path })
  434. await expectParallelFlushError(ctx.parallel('session/flush', session), /id collision/)
  435. await ctx.fiber.dispose()
  436. })
  437. })
  438. describe('surface field round-trip', () => {
  439. it('rowToEvent parses surface fields from EventRow columns', () => {
  440. const row: EventRow = {
  441. seq: 0, type: 'assistant/message', time: 1,
  442. data: JSON.stringify({ turn: 1, step: 1, content: [] }),
  443. source_event_seqs: JSON.stringify([3, 5]),
  444. surface_op: JSON.stringify('append'),
  445. }
  446. const event = rowToEvent(row)
  447. expect((event as SurfaceEvent).sourceEventSeqs).toEqual([3, 5])
  448. expect((event as SurfaceEvent).surfaceOp).toBe('append')
  449. })
  450. it('rowToEvent handles replace surfaceOp object', () => {
  451. const row: EventRow = {
  452. seq: 0, type: 'assistant/message', time: 1,
  453. data: JSON.stringify({ turn: 1, step: 1, content: [] }),
  454. source_event_seqs: JSON.stringify([0, 1]),
  455. surface_op: JSON.stringify({ op: 'replace', start: 0, end: 1 }),
  456. }
  457. const event = rowToEvent(row)
  458. expect((event as SurfaceEvent).sourceEventSeqs).toEqual([0, 1])
  459. expect((event as SurfaceEvent).surfaceOp).toEqual({ op: 'replace', start: 0, end: 1 })
  460. })
  461. it('scanRows with surface columns reconstructs events with surface fields', () => {
  462. const rows: EventRow[] = [
  463. { seq: 0, type: 'user/message', time: 1,
  464. data: JSON.stringify({ content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }),
  465. source_event_seqs: null, surface_op: '{"op":"replace","start":0,"end":0}' },
  466. { seq: 1, type: 'turn/end', time: 2,
  467. data: JSON.stringify({ turn: 1, reason: { kind: 'completed' } }),
  468. source_event_seqs: null, surface_op: null },
  469. ]
  470. const { preserved } = scanRows(rows)
  471. expect(preserved).toHaveLength(2)
  472. expect((preserved[0]! as SurfaceEvent).surfaceOp).toEqual({ op: 'replace', start: 0, end: 0 })
  473. expect((preserved[0]! as SurfaceEvent).sourceEventSeqs).toBeUndefined()
  474. expect((preserved[1] as SessionEvent<SurfaceEventType>).surfaceOp).toBeUndefined()
  475. })
  476. it('append and load round-trips surface fields through SQLite', async () => {
  477. const ctx = new Context()
  478. await ctx.plugin(SessionStore)
  479. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  480. const session = ctx.sessions.create(SessionId('roundtrip-surface'))
  481. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  482. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  483. session.append('assistant/message', { turn: 1, step: 1, content: [] }, { surfaceOp: 'append', sourceEventSeqs: [0] })
  484. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  485. await ctx.parallel('session/flush', session)
  486. const loaded = await ctx.sessionPersistence.load(SessionId('roundtrip-surface'))
  487. expect(loaded.events).toHaveLength(4)
  488. const um = loaded.events[1]!
  489. expect((um as SurfaceEvent).surfaceOp).toBe('append')
  490. expect((um as SurfaceEvent).sourceEventSeqs).toBeUndefined()
  491. const am = loaded.events[2]!
  492. expect((am as SurfaceEvent).surfaceOp).toBe('append')
  493. expect((am as SurfaceEvent).sourceEventSeqs).toEqual([0])
  494. await fiber.dispose()
  495. })
  496. it('persists events with surfaceOp but no sourceEventSeqs (covers null branch in surfaceBindings)', async () => {
  497. const ctx = new Context()
  498. await ctx.plugin(SessionStore)
  499. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  500. const session = ctx.sessions.create(SessionId('surface-noseq'))
  501. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  502. session.append('steering/message', { turn: 1, content: [], source: { kind: 'user' } }, { surfaceOp: 'append' })
  503. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  504. await ctx.parallel('session/flush', session)
  505. const loaded = await ctx.sessionPersistence.load(SessionId('surface-noseq'))
  506. expect((loaded.events[1]! as SurfaceEvent).surfaceOp).toBe('append')
  507. expect((loaded.events[1]! as SurfaceEvent).sourceEventSeqs).toBeUndefined()
  508. await fiber.dispose()
  509. })
  510. })