sqlite.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { existsSync } from 'node:fs'
  4. import { mkdtemp, rm } 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. // A stale OLDER version (e.g. a pre-summary-drop v1 DB) is also rejected —
  235. // we do not migrate (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 = 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. // Two unmerged branches each shipped a DISTINCT layout under user_version 3 (one added only
  245. // `seed_length`, the other only the surface columns). The merged v4 cannot interpret that
  246. // ambiguous, incomplete layout and must reject it.
  247. const path = await freshDbPath()
  248. openDatabase(path, 'wal').close() // creates + stamps user_version = SCHEMA_VERSION (4)
  249. const db = openDatabase(path, 'wal')
  250. db.exec('PRAGMA user_version = 3')
  251. db.close()
  252. expect(() => openDatabase(path, 'wal')).toThrow(/schema version 3, incompatible with this build/)
  253. })
  254. it('a corrupt-JSON row in the uncommitted tail is discarded on load, not unloadable', async () => {
  255. const path = await freshDbPath()
  256. const m = meta('corrupt-tail')
  257. const b1 = await backend(path)
  258. await b1.ctx.sessionPersistence.create(m)
  259. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog()) // committed: seqs 0..5
  260. await b1.dispose()
  261. // A torn row after the last committed turn has invalid JSON. `scanRows` locates the boundary
  262. // from seq/type columns without parsing the tail, preserves the committed prefix, and load
  263. // deletes the row; invalid JSON inside the committed region would remain fatal.
  264. const db = openDatabase(path, 'wal')
  265. db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, 6, ?, 7, ?)')
  266. .run(m.id, 'turn/start', '{not valid json')
  267. db.close()
  268. const b2 = await backend(path)
  269. const loaded = await b2.ctx.sessionPersistence.load(m.id)
  270. expect(loaded.events).toEqual(oneTurnLog()) // torn tail discarded, committed intact (turn 1 already balanced → no closers)
  271. // load physically deleted the corrupt tail row, so a fresh append continues.
  272. await b2.ctx.sessionPersistence.append(m.id, [
  273. { type: 'turn/start', seq: 6, time: 8, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  274. { type: 'turn/end', seq: 7, time: 9, data: { turn: 2, reason: { kind: 'completed' } } },
  275. ])
  276. const reloaded = await b2.ctx.sessionPersistence.load(m.id)
  277. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  278. await b2.dispose()
  279. })
  280. it('append rolls back the whole batch on a mid-batch seq collision (transaction)', async () => {
  281. const ctx = new Context()
  282. await ctx.plugin(SessionStore)
  283. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  284. const m = meta('rollback')
  285. await ctx.sessionPersistence.create(m)
  286. await ctx.sessionPersistence.append(m.id, oneTurnLog()) // seqs 0..5
  287. // A batch that re-states an already-stored seq must be rejected and leave
  288. // the stored log unchanged (the UNIQUE (session_id, seq) constraint fires
  289. // inside the transaction → ROLLBACK).
  290. await expect(ctx.sessionPersistence.append(m.id, oneTurnLog())).rejects.toThrow()
  291. const loaded = await ctx.sessionPersistence.load(m.id)
  292. expect(loaded.events).toEqual(oneTurnLog()) // unchanged
  293. await fiber.dispose()
  294. })
  295. it('persists across separate backend instances over the same file', async () => {
  296. const path = await freshDbPath()
  297. const m = meta('persist', '/proj')
  298. const ctx1 = new Context()
  299. await ctx1.plugin(SessionStore)
  300. const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
  301. await ctx1.sessionPersistence.create(m)
  302. await ctx1.sessionPersistence.append(m.id, oneTurnLog())
  303. await fiber1.dispose()
  304. const ctx2 = new Context()
  305. await ctx2.plugin(SessionStore)
  306. const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
  307. expect((await ctx2.sessionPersistence.list()).map(x => x.id)).toContain(m.id)
  308. const loaded = await ctx2.sessionPersistence.load(m.id)
  309. expect(loaded.meta).toMatchObject({ id: m.id, cwd: '/proj' })
  310. expect(loaded.events).toEqual(oneTurnLog())
  311. await fiber2.dispose()
  312. })
  313. it('exposes the schema version constant', () => {
  314. expect(SCHEMA_VERSION).toBe(4)
  315. })
  316. })
  317. describe('SessionPersistenceSqlite: edge cases', () => {
  318. it('append rolls back and rethrows when an event INSERT fails inside the transaction', async () => {
  319. const path = await freshDbPath()
  320. const m = meta('rollback-insert')
  321. const b1 = await backend(path)
  322. await b1.ctx.sessionPersistence.create(m)
  323. await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
  324. // A SECOND backend over the same file loads the session first, so it adopts
  325. // cursor 6 (the committed length) into its OWN in-memory state.
  326. const b2 = await backend(path)
  327. await b2.ctx.sessionPersistence.load(m.id) // cursor 6 in b2
  328. const turn2: SessionEvent[] = [
  329. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
  330. { type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
  331. ]
  332. // b1 commits seq 6..7 first.
  333. await b1.ctx.sessionPersistence.append(m.id, turn2)
  334. // b2 still thinks its cursor is 6, so this batch passes the contiguity check
  335. // but its INSERT of seq 6 hits the UNIQUE (session_id, seq) constraint
  336. // mid-transaction → ROLLBACK + rethrow.
  337. await expect(b2.ctx.sessionPersistence.append(m.id, turn2)).rejects.toThrow(/UNIQUE/)
  338. // b1's turn is intact; b2's rolled-back attempt left nothing extra.
  339. const loaded = await b1.ctx.sessionPersistence.load(m.id)
  340. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  341. await b1.dispose()
  342. await b2.dispose()
  343. })
  344. it('journalMode config reaches the database (default wal, rollback modes selectable)', async () => {
  345. // :memory: databases always report journal_mode=memory, so probe file DBs.
  346. const walPath = await freshDbPath()
  347. const bWal = await backend(walPath)
  348. await bWal.ctx.sessionPersistence.create(meta('jm-wal'))
  349. expect((openDatabase(walPath, 'wal').prepare('PRAGMA journal_mode').get() as { journal_mode: string }).journal_mode).toBe('wal')
  350. await bWal.dispose()
  351. const deletePath = await freshDbPath()
  352. const ctx = new Context()
  353. await ctx.plugin(SessionStore)
  354. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: deletePath, journalMode: 'delete' })
  355. await ctx.sessionPersistence.create(meta('jm-delete'))
  356. // Probe through a second connection: journal_mode=delete is a per-database
  357. // property only insofar as no WAL files exist — assert the world, not the
  358. // backend's self-report (no -wal sidecar after writes in delete mode).
  359. const db = openDatabase(deletePath, 'delete')
  360. expect((db.prepare('PRAGMA journal_mode').get() as { journal_mode: string }).journal_mode).toBe('delete')
  361. db.close()
  362. expect(existsSync(`${deletePath}-wal`)).toBe(false)
  363. await fiber.dispose()
  364. })
  365. it('HMR: a DIFFERENT session colliding with a materialized on-disk id is rejected', async () => {
  366. const path = await freshDbPath()
  367. // Instance 1 materializes a session and disposes.
  368. const b1 = await backend(path)
  369. const s1 = b1.ctx.sessions.create(SessionId('hmr-collide'))
  370. appendLog(s1, oneTurnLog())
  371. await b1.ctx.parallel('session/flush', s1)
  372. await b1.dispose()
  373. // A fresh context with an UNRELATED live session reusing the id meets a
  374. // materialized row that is NOT a prefix of its events → reject.
  375. const ctx = new Context()
  376. await ctx.plugin(SessionStore)
  377. let session!: Session
  378. await ctx.plugin(Object.assign((inner: Context) => {
  379. session = inner.sessions.create(SessionId('hmr-collide'))
  380. }, { inject: ['sessions'] }))
  381. session.append('turn/start', { turn: 9, trigger: { kind: 'message', source: { kind: 'user' } } })
  382. await ctx.plugin(SessionPersistenceSqlite, { path })
  383. await expectParallelFlushError(ctx.parallel('session/flush', session), /id collision/)
  384. await ctx.fiber.dispose()
  385. })
  386. })
  387. describe('surface field round-trip', () => {
  388. it('rowToEvent parses surface fields from EventRow columns', () => {
  389. const row: EventRow = {
  390. seq: 0, type: 'assistant/message', time: 1,
  391. data: JSON.stringify({ turn: 1, step: 1, content: [] }),
  392. source_event_seqs: JSON.stringify([3, 5]),
  393. surface_op: JSON.stringify('append'),
  394. }
  395. const event = rowToEvent(row)
  396. expect((event as SurfaceEvent).sourceEventSeqs).toEqual([3, 5])
  397. expect((event as SurfaceEvent).surfaceOp).toBe('append')
  398. })
  399. it('rowToEvent handles replace surfaceOp object', () => {
  400. const row: EventRow = {
  401. seq: 0, type: 'assistant/message', time: 1,
  402. data: JSON.stringify({ turn: 1, step: 1, content: [] }),
  403. source_event_seqs: JSON.stringify([0, 1]),
  404. surface_op: JSON.stringify({ op: 'replace', start: 0, end: 1 }),
  405. }
  406. const event = rowToEvent(row)
  407. expect((event as SurfaceEvent).sourceEventSeqs).toEqual([0, 1])
  408. expect((event as SurfaceEvent).surfaceOp).toEqual({ op: 'replace', start: 0, end: 1 })
  409. })
  410. it('scanRows with surface columns reconstructs events with surface fields', () => {
  411. const rows: EventRow[] = [
  412. { seq: 0, type: 'user/message', time: 1,
  413. data: JSON.stringify({ content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }),
  414. source_event_seqs: null, surface_op: '{"op":"replace","start":0,"end":0}' },
  415. { seq: 1, type: 'turn/end', time: 2,
  416. data: JSON.stringify({ turn: 1, reason: { kind: 'completed' } }),
  417. source_event_seqs: null, surface_op: null },
  418. ]
  419. const { preserved } = scanRows(rows)
  420. expect(preserved).toHaveLength(2)
  421. expect((preserved[0]! as SurfaceEvent).surfaceOp).toEqual({ op: 'replace', start: 0, end: 0 })
  422. expect((preserved[0]! as SurfaceEvent).sourceEventSeqs).toBeUndefined()
  423. expect((preserved[1] as SessionEvent<SurfaceEventType>).surfaceOp).toBeUndefined()
  424. })
  425. it('append and load round-trips surface fields through SQLite', async () => {
  426. const ctx = new Context()
  427. await ctx.plugin(SessionStore)
  428. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  429. const session = ctx.sessions.create(SessionId('roundtrip-surface'))
  430. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  431. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  432. session.append('assistant/message', { turn: 1, step: 1, content: [] }, { surfaceOp: 'append', sourceEventSeqs: [0] })
  433. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  434. await ctx.parallel('session/flush', session)
  435. const loaded = await ctx.sessionPersistence.load(SessionId('roundtrip-surface'))
  436. expect(loaded.events).toHaveLength(4)
  437. const um = loaded.events[1]!
  438. expect((um as SurfaceEvent).surfaceOp).toBe('append')
  439. expect((um as SurfaceEvent).sourceEventSeqs).toBeUndefined()
  440. const am = loaded.events[2]!
  441. expect((am as SurfaceEvent).surfaceOp).toBe('append')
  442. expect((am as SurfaceEvent).sourceEventSeqs).toEqual([0])
  443. await fiber.dispose()
  444. })
  445. it('persists events with surfaceOp but no sourceEventSeqs (covers null branch in surfaceBindings)', async () => {
  446. const ctx = new Context()
  447. await ctx.plugin(SessionStore)
  448. const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
  449. const session = ctx.sessions.create(SessionId('surface-noseq'))
  450. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  451. session.append('steering/message', { turn: 1, content: [], source: { kind: 'user' } }, { surfaceOp: 'append' })
  452. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  453. await ctx.parallel('session/flush', session)
  454. const loaded = await ctx.sessionPersistence.load(SessionId('surface-noseq'))
  455. expect((loaded.events[1]! as SurfaceEvent).surfaceOp).toBe('append')
  456. expect((loaded.events[1]! as SurfaceEvent).sourceEventSeqs).toBeUndefined()
  457. await fiber.dispose()
  458. })
  459. })