sqlite.spec.ts 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import { Context, type Fiber } from 'cordis'
  3. import { DatabaseSync } from 'node:sqlite'
  4. import { chmod, mkdtemp, rm, stat, writeFile } from 'node:fs/promises'
  5. import { tmpdir } from 'node:os'
  6. import { dirname, join } from 'node:path'
  7. import SessionStore, { SESSION_FORMAT_VERSION, SessionId } from '@deepseek-ai/dsh-session'
  8. import type { SessionEvent, SessionHeader, SessionId as SessionIdType } from '@deepseek-ai/dsh-session'
  9. import SessionPersistence, { SessionPersistenceRevision } from '@deepseek-ai/dsh-session-persistence'
  10. import type { SessionPersistenceSnapshot } from '@deepseek-ai/dsh-session-persistence'
  11. import SessionPersistenceSqlite from '@deepseek-ai/dsh-session-persistence-sqlite'
  12. import SessionQuerySqlite, {
  13. SESSION_QUERY_SQLITE_SCHEMA_VERSION,
  14. } from '@deepseek-ai/dsh-session-query-sqlite'
  15. import {
  16. SessionQueryError,
  17. SessionSearchCursor,
  18. type SessionAvailability,
  19. type SessionQueryErrorCode,
  20. type SessionSearchRequest,
  21. } from '@deepseek-ai/dsh-session-query'
  22. const temporaryDirectories: string[] = []
  23. afterEach(async () => {
  24. for (const directory of temporaryDirectories.splice(0)) {
  25. await rm(directory, { recursive: true, force: true })
  26. }
  27. })
  28. async function temporaryPath(name = 'search.db'): Promise<string> {
  29. const directory = await mkdtemp(join(tmpdir(), 'dsh-session-search-'))
  30. temporaryDirectories.push(directory)
  31. return join(directory, name)
  32. }
  33. function header(id: string, createdAt = 1, extra: Partial<SessionHeader> = {}): SessionHeader {
  34. return { version: SESSION_FORMAT_VERSION, id: SessionId(id), createdAt, ...extra }
  35. }
  36. function messageEvents(text: string, time = 1): SessionEvent[] {
  37. return [{
  38. type: 'user/message',
  39. seq: 0,
  40. time,
  41. data: { content: [{ type: 'text', text }], source: { kind: 'user' } },
  42. surfaceOp: 'append',
  43. }]
  44. }
  45. function expectCode(code: SessionQueryErrorCode): Error {
  46. return expect.objectContaining({ code }) as Error
  47. }
  48. function replaceCursorOffset(
  49. cursor: ReturnType<typeof SessionSearchCursor>,
  50. offset: number,
  51. ): ReturnType<typeof SessionSearchCursor> {
  52. const payload = JSON.parse(
  53. Buffer.from(cursor, 'base64url').toString('utf8'),
  54. ) as Record<string, unknown>
  55. return SessionSearchCursor(Buffer.from(JSON.stringify({ ...payload, offset }), 'utf8').toString('base64url'))
  56. }
  57. class TestPersistence extends SessionPersistence {
  58. static entries = new Map<SessionIdType, { meta: SessionHeader; events: SessionEvent[] }>()
  59. static revisions = new Map<SessionIdType, number>()
  60. static nextRevision = 0
  61. static loads = new Map<SessionIdType, number>()
  62. static loadEffect: ((entry: { meta: SessionHeader; events: SessionEvent[] }) => void) | undefined
  63. static listGate: Promise<void> | undefined
  64. static listStarted: (() => void) | undefined
  65. static snapshotEffect: (() => void | Promise<void>) | undefined
  66. static snapshotOverride: (() => SessionPersistenceSnapshot[]) | undefined
  67. static failure: unknown
  68. locate(_meta: SessionHeader): undefined {
  69. return undefined
  70. }
  71. static reset(entries: readonly { meta: SessionHeader; events: SessionEvent[] }[] = []): void {
  72. this.entries = new Map()
  73. this.revisions = new Map()
  74. this.loads = new Map()
  75. this.loadEffect = undefined
  76. for (const entry of entries) this.set(entry)
  77. this.listGate = undefined
  78. this.listStarted = undefined
  79. this.snapshotEffect = undefined
  80. this.snapshotOverride = undefined
  81. this.failure = undefined
  82. }
  83. static set(entry: { meta: SessionHeader; events: SessionEvent[] }): void {
  84. this.entries.set(entry.meta.id, structuredClone(entry))
  85. this.revisions.set(entry.meta.id, ++this.nextRevision)
  86. }
  87. create(meta: SessionHeader): Promise<void> {
  88. TestPersistence.set({ meta, events: [] })
  89. return Promise.resolve()
  90. }
  91. append(id: SessionIdType, events: readonly SessionEvent[]): Promise<void> {
  92. const entry = TestPersistence.entries.get(id)
  93. if (entry === undefined) return Promise.reject(new Error('missing test session'))
  94. entry.events.push(...structuredClone(events))
  95. TestPersistence.revisions.set(id, ++TestPersistence.nextRevision)
  96. return Promise.resolve()
  97. }
  98. async load(id: SessionIdType): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
  99. TestPersistence.loads.set(id, (TestPersistence.loads.get(id) ?? 0) + 1)
  100. if (TestPersistence.failure !== undefined) throw TestPersistence.failure
  101. const entry = TestPersistence.entries.get(id)
  102. if (entry === undefined) throw new Error('missing test session')
  103. if (TestPersistence.loadEffect !== undefined) {
  104. const effect = TestPersistence.loadEffect
  105. TestPersistence.loadEffect = undefined
  106. effect(entry)
  107. TestPersistence.revisions.set(id, ++TestPersistence.nextRevision)
  108. }
  109. return structuredClone(entry)
  110. }
  111. async list(): Promise<SessionHeader[]> {
  112. TestPersistence.listStarted?.()
  113. await TestPersistence.listGate
  114. if (TestPersistence.failure !== undefined) throw TestPersistence.failure
  115. return [...TestPersistence.entries.values()].map(entry => structuredClone(entry.meta))
  116. }
  117. async listSnapshots(): Promise<SessionPersistenceSnapshot[]> {
  118. TestPersistence.listStarted?.()
  119. await TestPersistence.listGate
  120. if (TestPersistence.failure !== undefined) throw TestPersistence.failure
  121. const snapshots = TestPersistence.snapshotOverride?.()
  122. ?? [...TestPersistence.entries.values()].map(entry => ({
  123. header: structuredClone(entry.meta),
  124. revision: SessionPersistenceRevision(`test:${TestPersistence.revisions.get(entry.meta.id)}`),
  125. }))
  126. await TestPersistence.snapshotEffect?.()
  127. return snapshots
  128. }
  129. }
  130. async function liveContext(config: ConstructorParameters<typeof SessionQuerySqlite>[1] = { path: ':memory:' }): Promise<Context> {
  131. const ctx = new Context()
  132. await ctx.plugin(SessionStore)
  133. await ctx.plugin(SessionQuerySqlite, config)
  134. return ctx
  135. }
  136. describe('SQLite session search', () => {
  137. it('searches two-character Unicode61 tokens in live-only sessions', async () => {
  138. const ctx = await liveContext({ path: ':memory:', snippetChars: 20 })
  139. const session = ctx.sessions.create(SessionId('live'), {
  140. meta: { cwd: '/work', createdAt: 10, seedLength: 1, delegationDepth: 2 },
  141. })
  142. session.append(
  143. 'user/message',
  144. { content: [{ type: 'text', text: 'An AI helper' }], source: { kind: 'user' } },
  145. { surfaceOp: 'append' },
  146. )
  147. await expect(ctx.sessionQuery.searchEvents({ sessionId: session.id, query: 'AI' }))
  148. .resolves.toMatchObject({ items: [{ sessionId: session.id, seq: 0, snippet: 'An AI helper' }] })
  149. await expect(ctx.sessionQuery.searchSessions({ query: 'AI' }))
  150. .resolves.toMatchObject({ items: [{ header: { ...session.header, seedLength: 1 }, live: true, persisted: false }] })
  151. })
  152. it('searches all surfaces by default and applies metadata before ranking', async () => {
  153. const ctx = await liveContext({ path: ':memory:', defaultLimit: 10, maxLimit: 20 })
  154. const parent = SessionId('parent')
  155. const events: SessionEvent[] = [
  156. { type: 'user/message', seq: 0, time: 10, data: { content: [{ type: 'text', text: 'needle original' }], source: { kind: 'user' } }, surfaceOp: 'append' },
  157. { type: 'assistant/chunk', seq: 1, time: 11, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'needle raw' } } },
  158. { type: 'user/message', seq: 2, time: 12, data: { content: [{ type: 'text', text: 'needle summary' }], source: { kind: 'plugin', plugin: 'test' } }, surfaceOp: { op: 'replace', start: 0, end: 0 }, sourceEventSeqs: [0] },
  159. { type: 'turn/end', seq: 3, time: 13, data: { turn: 1, reason: { kind: 'error', step: 1, message: 'needle failure' } } },
  160. ]
  161. ctx.sessions.create(SessionId('a'), { seed: events, meta: { cwd: '/a', parentSession: parent, createdAt: 20 } })
  162. ctx.sessions.create(SessionId('b'), { seed: messageEvents('needle peer', 12), meta: { createdAt: 20 } })
  163. const all = await ctx.sessionQuery.searchEvents({ sessionId: SessionId('a'), query: 'needle' })
  164. expect(new Set(all.items.map(item => item.surface))).toEqual(new Set(['current', 'shadowed', 'log-only']))
  165. await expect(ctx.sessionQuery.searchEvents({
  166. sessionId: SessionId('a'),
  167. query: 'needle',
  168. filters: [
  169. { kind: 'seq', from: 2, to: 2 },
  170. { kind: 'time', from: 12, to: 12 },
  171. { kind: 'type', values: ['user/message'] },
  172. { kind: 'surface', values: ['current'] },
  173. ],
  174. })).resolves.toMatchObject({ items: [{ seq: 2, surface: 'current' }] })
  175. const grouped = await ctx.sessionQuery.searchSessions({
  176. query: 'needle',
  177. sessionFilters: [
  178. { kind: 'id', values: [SessionId('a')] },
  179. { kind: 'cwd', values: ['/a'] },
  180. { kind: 'created-at', from: 20, to: 20 },
  181. { kind: 'parent', values: [parent] },
  182. { kind: 'availability', values: ['live'] },
  183. ],
  184. eventFilters: [{ kind: 'surface', values: ['shadowed'] }],
  185. })
  186. expect(grouped.items).toHaveLength(1)
  187. expect(grouped.items[0]).toMatchObject({
  188. header: { id: SessionId('a'), cwd: '/a', parentSession: parent },
  189. live: true,
  190. persisted: false,
  191. bestMatch: { seq: 0, surface: 'shadowed' },
  192. })
  193. })
  194. it('searches at the supported FTS5 outer-predicate boundary in both scopes', async () => {
  195. const ctx = await liveContext()
  196. const session = ctx.sessions.create(SessionId('predicate-boundary'), {
  197. seed: messageEvents('needle'),
  198. meta: { cwd: '/work' },
  199. })
  200. const sessionFilters = Array.from(
  201. { length: 14 },
  202. () => ({ kind: 'cwd' as const, values: ['/work', null] }),
  203. )
  204. const eventFilters = Array.from(
  205. { length: 13 },
  206. () => ({ kind: 'type' as const, values: ['user/message' as const] }),
  207. )
  208. await expect(ctx.sessionQuery.searchSessions({ query: 'needle', sessionFilters }))
  209. .resolves.toMatchObject({ items: [{ header: { id: session.id } }] })
  210. await expect(ctx.sessionQuery.searchEvents({
  211. sessionId: session.id,
  212. query: 'needle',
  213. filters: eventFilters,
  214. })).resolves.toMatchObject({ items: [{ sessionId: session.id, seq: 0 }] })
  215. })
  216. it('rejects unsupported FTS5 outer-predicate counts with typed errors', async () => {
  217. const ctx = await liveContext()
  218. const session = ctx.sessions.create(SessionId('predicate-limit'), { seed: messageEvents('needle') })
  219. const sessionFilters = Array.from(
  220. { length: 1_100 },
  221. () => ({ kind: 'id' as const, values: [session.id] }),
  222. )
  223. const eventFilters = Array.from(
  224. { length: 1_100 },
  225. () => ({ kind: 'type' as const, values: ['user/message' as const] }),
  226. )
  227. await expect(ctx.sessionQuery.searchSessions({ query: 'needle', sessionFilters }))
  228. .rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  229. await expect(ctx.sessionQuery.searchEvents({
  230. sessionId: session.id,
  231. query: 'needle',
  232. filters: eventFilters,
  233. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  234. await expect(ctx.sessionQuery.searchSessions({
  235. query: 'needle',
  236. sessionFilters: sessionFilters.slice(0, 7),
  237. eventFilters: eventFilters.slice(0, 8),
  238. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  239. await expect(ctx.sessionQuery.searchEvents({
  240. sessionId: session.id,
  241. query: 'needle',
  242. filters: eventFilters.slice(0, 14),
  243. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  244. })
  245. it('uses literal phrase tokens, stable ties, and bounded Unicode snippets', async () => {
  246. const ctx = await liveContext({ path: ':memory:', defaultLimit: 10, maxLimit: 10, snippetChars: 5 })
  247. ctx.sessions.create(SessionId('a'), { seed: messageEvents('😀😀 alpha beta BRAID 😀😀', 10), meta: { createdAt: 1 } })
  248. ctx.sessions.create(SessionId('b'), { seed: messageEvents('alpha beta', 10), meta: { createdAt: 1 } })
  249. ctx.sessions.create(SessionId('c'), { seed: messageEvents('alpha middle beta', 10), meta: { createdAt: 1 } })
  250. ctx.sessions.create(SessionId('d'), { seed: messageEvents('alpha beta', 10), meta: { createdAt: 1 } })
  251. ctx.sessions.create(SessionId('operator'), { seed: messageEvents('needle OR absent', 10), meta: { createdAt: 1 } })
  252. ctx.sessions.create(SessionId('only'), { seed: messageEvents('needle only', 10), meta: { createdAt: 1 } })
  253. ctx.sessions.create(SessionId('quote'), { seed: messageEvents('say "needle" exactly', 10), meta: { createdAt: 1 } })
  254. const phrase = await ctx.sessionQuery.searchSessions({ query: 'alpha beta' })
  255. expect(phrase.items.map(item => item.header.id)).toEqual([SessionId('b'), SessionId('d'), SessionId('a')])
  256. expect(phrase.items.every(item => Array.from(item.bestMatch.snippet).length <= 5)).toBe(true)
  257. await expect(ctx.sessionQuery.searchSessions({ query: 'AI' })).resolves.toEqual({ items: [] })
  258. await expect(ctx.sessionQuery.searchSessions({ query: 'needle OR absent' }))
  259. .resolves.toMatchObject({ items: [{ header: { id: SessionId('operator') } }] })
  260. await expect(ctx.sessionQuery.searchSessions({ query: 'say "needle"' }))
  261. .resolves.toMatchObject({ items: [{ header: { id: SessionId('quote') } }] })
  262. await expect(ctx.sessionQuery.searchSessions({ query: '*' })).resolves.toEqual({ items: [] })
  263. })
  264. it('ranks live and persisted matches on one source-comparable contract', async () => {
  265. const persisted = header('z-persisted')
  266. TestPersistence.reset([
  267. { meta: persisted, events: messageEvents('needle needle', 10) },
  268. ...Array.from({ length: 12 }, (_, index) => ({
  269. meta: header(`filler-${index}`),
  270. events: messageEvents('needle', 10),
  271. })),
  272. ])
  273. const ctx = await liveContext()
  274. const persistence = await ctx.plugin(TestPersistence)
  275. ctx.sessions.create(SessionId('a-live'), {
  276. seed: messageEvents('needle needle', 10),
  277. meta: { createdAt: persisted.createdAt },
  278. })
  279. const result = await ctx.sessionQuery.searchSessions({
  280. query: 'needle',
  281. sessionFilters: [{ kind: 'id', values: [SessionId('a-live'), persisted.id] }],
  282. })
  283. expect(result.items.map(item => item.header.id)).toEqual([SessionId('a-live'), persisted.id])
  284. await persistence.dispose()
  285. })
  286. it('positions snippets from FTS5 matches across diacritics and punctuation', async () => {
  287. const ctx = await liveContext({ path: ':memory:', snippetChars: 14 })
  288. const session = ctx.sessions.create(SessionId('snippet'), {
  289. seed: messageEvents('long long long—café,\nnext value', 10),
  290. })
  291. const page = await ctx.sessionQuery.searchEvents({ sessionId: session.id, query: 'CAFE' })
  292. expect(page.items).toHaveLength(1)
  293. expect(page.items[0]!.snippet).toContain('café')
  294. expect(page.items[0]!.snippet).toContain('—')
  295. expect(page.items[0]!.snippet).not.toContain('\n')
  296. expect(Array.from(page.items[0]!.snippet).length).toBeLessThanOrEqual(14)
  297. })
  298. it('binds cursors to requests and only invalidates within-session pages for target changes', async () => {
  299. const ctx = await liveContext({ path: ':memory:', defaultLimit: 1, maxLimit: 5 })
  300. const target = ctx.sessions.create(SessionId('target'), {
  301. seed: [
  302. ...messageEvents('needle one', 10),
  303. { ...messageEvents('needle two', 11)[0]!, seq: 1 },
  304. { ...messageEvents('needle three', 12)[0]!, seq: 2 },
  305. ],
  306. })
  307. ctx.sessions.create(SessionId('other'), { seed: messageEvents('needle other', 10) })
  308. const eventPage = await ctx.sessionQuery.searchEvents({ sessionId: target.id, query: 'needle', limit: 1 })
  309. const sessionPage = await ctx.sessionQuery.searchSessions({ query: 'needle', limit: 1 })
  310. expect(eventPage.nextCursor).toEqual(expect.any(String))
  311. expect(sessionPage.nextCursor).toEqual(expect.any(String))
  312. if (eventPage.nextCursor === undefined || sessionPage.nextCursor === undefined) throw new Error('expected cursors')
  313. const unsafeOffsetCursor = replaceCursorOffset(eventPage.nextCursor, 1e100)
  314. await expect(ctx.sessionQuery.searchEvents({
  315. sessionId: target.id,
  316. query: 'needle',
  317. limit: 1,
  318. cursor: unsafeOffsetCursor,
  319. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_CURSOR'))
  320. const eventKeys = eventPage.items.map(item => `${item.sessionId}:${item.seq}`)
  321. let eventCursor: ReturnType<typeof SessionSearchCursor> | undefined = eventPage.nextCursor
  322. while (eventCursor !== undefined) {
  323. const next = await ctx.sessionQuery.searchEvents({
  324. sessionId: target.id,
  325. query: 'needle',
  326. limit: 1,
  327. cursor: eventCursor,
  328. })
  329. eventKeys.push(...next.items.map(item => `${item.sessionId}:${item.seq}`))
  330. eventCursor = next.nextCursor
  331. }
  332. expect(eventKeys).toHaveLength(3)
  333. expect(new Set(eventKeys).size).toBe(eventKeys.length)
  334. const sessionIds = sessionPage.items.map(item => item.header.id)
  335. let sessionCursor: ReturnType<typeof SessionSearchCursor> | undefined = sessionPage.nextCursor
  336. while (sessionCursor !== undefined) {
  337. const next = await ctx.sessionQuery.searchSessions({ query: 'needle', limit: 1, cursor: sessionCursor })
  338. sessionIds.push(...next.items.map(item => item.header.id))
  339. sessionCursor = next.nextCursor
  340. }
  341. expect(sessionIds).toHaveLength(2)
  342. expect(new Set(sessionIds).size).toBe(sessionIds.length)
  343. ctx.sessions.create(SessionId('unrelated'), { seed: messageEvents('needle unrelated', 20) })
  344. await expect(ctx.sessionQuery.searchEvents({
  345. sessionId: target.id,
  346. query: 'needle',
  347. limit: 1,
  348. cursor: eventPage.nextCursor,
  349. })).resolves.toMatchObject({ items: [{ sessionId: target.id }] })
  350. await expect(ctx.sessionQuery.searchSessions({ query: 'needle', limit: 1, cursor: sessionPage.nextCursor }))
  351. .rejects.toThrow(expectCode('SESSION_QUERY_STALE_CURSOR'))
  352. await expect(ctx.sessionQuery.searchEvents({
  353. sessionId: target.id,
  354. query: 'different',
  355. limit: 1,
  356. cursor: eventPage.nextCursor,
  357. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_CURSOR'))
  358. target.append('user/message', { content: [{ type: 'text', text: 'needle four' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  359. await expect(ctx.sessionQuery.searchEvents({
  360. sessionId: target.id,
  361. query: 'needle',
  362. limit: 1,
  363. cursor: eventPage.nextCursor,
  364. })).rejects.toThrow(expectCode('SESSION_QUERY_STALE_CURSOR'))
  365. })
  366. it('invalidates session cursors after transient persistence topology changes', async () => {
  367. TestPersistence.reset()
  368. const ctx = await liveContext({ path: ':memory:', defaultLimit: 1, maxLimit: 5 })
  369. ctx.sessions.create(SessionId('first'), { seed: messageEvents('needle first') })
  370. ctx.sessions.create(SessionId('second'), { seed: messageEvents('needle second') })
  371. const page = await ctx.sessionQuery.searchSessions({ query: 'needle', limit: 1 })
  372. if (page.nextCursor === undefined) throw new Error('expected cursor')
  373. const persistence = await ctx.plugin(TestPersistence)
  374. await persistence.dispose()
  375. await expect(ctx.sessionQuery.searchSessions({
  376. query: 'needle',
  377. limit: 1,
  378. cursor: page.nextCursor,
  379. })).rejects.toThrow(expectCode('SESSION_QUERY_STALE_CURSOR'))
  380. })
  381. it('rejects invalid requests, filters, cursors, and direct config', async () => {
  382. const ctx = await liveContext({ path: ':memory:', defaultLimit: 2, maxLimit: 3 })
  383. const session = ctx.sessions.create(SessionId('valid'), { seed: messageEvents('needle') })
  384. for (const request of [
  385. { sessionId: session.id, query: '' },
  386. { sessionId: session.id, query: 'needle', limit: 0 },
  387. { sessionId: session.id, query: 'needle', limit: 4 },
  388. { sessionId: session.id, query: 'needle', filters: [{ kind: 'seq', from: 2, to: 1 }] },
  389. { sessionId: session.id, query: 'needle', filters: [{ kind: 'surface', values: ['future'] }] },
  390. { sessionId: session.id, query: 'bad\0query' },
  391. ] as const) {
  392. await expect(ctx.sessionQuery.searchEvents(request as never)).rejects.toBeInstanceOf(Error)
  393. }
  394. await expect(ctx.sessionQuery.searchSessions({
  395. query: 'needle',
  396. sessionFilters: [{ kind: 'availability', values: ['remote' as never] }],
  397. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  398. await expect(ctx.sessionQuery.searchSessions({
  399. query: 'needle',
  400. sessionFilters: [{ kind: 'future' } as never],
  401. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  402. await expect(ctx.sessionQuery.searchSessions({
  403. query: 'needle',
  404. eventFilters: [{ kind: 'future' } as never],
  405. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  406. await expect(ctx.sessionQuery.searchEvents({
  407. sessionId: session.id,
  408. query: 'needle',
  409. filters: [{ kind: 'future' } as never],
  410. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  411. await expect(ctx.sessionQuery.searchEvents({
  412. sessionId: session.id,
  413. query: 'needle',
  414. cursor: SessionSearchCursor('not-json'),
  415. }))
  416. .rejects.toThrow(expectCode('SESSION_QUERY_INVALID_CURSOR'))
  417. await expect(ctx.sessionQuery.searchEvents({ sessionId: SessionId('absent'), query: 'needle' }))
  418. .rejects.toThrow(expectCode('SESSION_QUERY_SESSION_NOT_FOUND'))
  419. for (const config of [
  420. { path: '' },
  421. { path: ':memory:', defaultLimit: 0 },
  422. { path: ':memory:', maxLimit: 0 },
  423. { path: ':memory:', defaultLimit: 1e100 },
  424. { path: ':memory:', maxLimit: 1e100 },
  425. { path: ':memory:', snippetChars: 0 },
  426. { path: ':memory:', defaultLimit: 3, maxLimit: 2 },
  427. { path: ':memory:', journalMode: 'memory' },
  428. ]) {
  429. const direct = new Context()
  430. await direct.plugin(SessionStore)
  431. expect(() => new SessionQuerySqlite(direct, config as never))
  432. .toThrow(expectCode('SESSION_QUERY_INVALID_CONFIG'))
  433. }
  434. })
  435. it('rejects aggregate filter bindings above SQLite\'s portable variable limit', async () => {
  436. const ctx = await liveContext()
  437. const session = ctx.sessions.create(SessionId('binding-limit'), { seed: messageEvents('needle') })
  438. // Each clause is below the ceiling; combined with its sibling and fixed
  439. // query bindings, the complete statement is not portable.
  440. const halfPortableLimit = 16_383
  441. const ids = Array.from(
  442. { length: halfPortableLimit },
  443. (_, index) => SessionId(`binding-${index}`),
  444. )
  445. const types = Array.from({ length: halfPortableLimit }, () => 'user/message' as const)
  446. const surfaces = Array.from({ length: halfPortableLimit }, () => 'current' as const)
  447. await expect(ctx.sessionQuery.searchSessions({
  448. query: 'needle',
  449. sessionFilters: [{ kind: 'id', values: ids }],
  450. eventFilters: [{ kind: 'type', values: types }],
  451. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  452. await expect(ctx.sessionQuery.searchEvents({
  453. sessionId: session.id,
  454. query: 'needle',
  455. filters: [
  456. { kind: 'type', values: types },
  457. { kind: 'surface', values: surfaces },
  458. ],
  459. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  460. })
  461. it('rejects one 125,000-value filter list with a typed error', async () => {
  462. const ctx = await liveContext()
  463. const ids = Array.from(
  464. { length: 125_000 },
  465. (_, index) => SessionId(`oversized-binding-${index}`),
  466. )
  467. await expect(ctx.sessionQuery.searchSessions({
  468. query: 'needle',
  469. sessionFilters: [{ kind: 'id', values: ids }],
  470. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  471. })
  472. })
  473. describe('SQLite reconciliation and source lifecycle', () => {
  474. it('owns queued request and filter values before waiting for the serializer', async () => {
  475. const durable = header('owned')
  476. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  477. const ctx = await liveContext()
  478. const persistence = await ctx.plugin(TestPersistence)
  479. let release!: () => void
  480. TestPersistence.listGate = new Promise<void>((resolve) => { release = resolve })
  481. let markStarted!: () => void
  482. const started = new Promise<void>((resolve) => { markStarted = resolve })
  483. TestPersistence.listStarted = () => {
  484. TestPersistence.listStarted = undefined
  485. markStarted()
  486. }
  487. const blocking = ctx.sessionQuery.searchSessions({ query: 'needle' })
  488. await started
  489. const availability: SessionAvailability[] = ['persisted']
  490. const request: SessionSearchRequest = {
  491. query: 'needle',
  492. sessionFilters: [{ kind: 'availability', values: availability }],
  493. }
  494. const queued = ctx.sessionQuery.searchSessions(request)
  495. request.query = 'absent'
  496. availability[0] = 'live'
  497. release()
  498. await expect(blocking).resolves.toMatchObject({ items: [{ header: durable }] })
  499. await expect(queued).resolves.toMatchObject({ items: [{ header: durable }] })
  500. await persistence.dispose()
  501. })
  502. it('mounts persistence dynamically, shadows with TEMP live rows, reveals, and hides on unmount', async () => {
  503. const shared = header('shared', 10, { cwd: '/work' })
  504. const durable = header('durable', 5)
  505. TestPersistence.reset([
  506. { meta: shared, events: messageEvents('persisted needle') },
  507. { meta: durable, events: messageEvents('durable needle') },
  508. ])
  509. const ctx = await liveContext()
  510. await expect(ctx.sessionQuery.searchSessions({ query: 'durable' })).resolves.toEqual({ items: [] })
  511. const persistenceFiber = await ctx.plugin(TestPersistence)
  512. await expect(ctx.sessionQuery.searchSessions({ query: 'durable' }))
  513. .resolves.toMatchObject({ items: [{ header: durable, live: false, persisted: true }] })
  514. const live = ctx.sessions.prepare(shared.id, { meta: { createdAt: 10, cwd: '/work' } })
  515. live.append('user/message', { content: [{ type: 'text', text: 'live needle' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  516. const detach = ctx.sessions.enter(live)
  517. ctx.sessions.announce(live)
  518. await expect(ctx.sessionQuery.searchSessions({ query: 'persisted' })).resolves.toEqual({ items: [] })
  519. await expect(ctx.sessionQuery.searchSessions({ query: 'live' }))
  520. .resolves.toMatchObject({ items: [{ header: shared, live: true, persisted: true }] })
  521. detach()
  522. await expect(ctx.sessionQuery.searchSessions({ query: 'persisted' }))
  523. .resolves.toMatchObject({ items: [{ header: shared, live: false, persisted: true }] })
  524. await persistenceFiber.dispose()
  525. await expect(ctx.sessionQuery.searchSessions({ query: 'durable' })).resolves.toEqual({ items: [] })
  526. await expect(ctx.sessionQuery.searchEvents({ sessionId: durable.id, query: 'needle' }))
  527. .rejects.toThrow(expectCode('SESSION_QUERY_SESSION_NOT_FOUND'))
  528. })
  529. it('does not load a persisted log while the same session is live', async () => {
  530. const shared = header('checkpointed-live', 10)
  531. TestPersistence.reset([{ meta: shared, events: messageEvents('persisted needle') }])
  532. const ctx = await liveContext()
  533. const live = ctx.sessions.prepare(shared.id, {
  534. seed: messageEvents('live needle'),
  535. meta: { createdAt: shared.createdAt },
  536. })
  537. const detach = ctx.sessions.enter(live)
  538. ctx.sessions.announce(live)
  539. const persistence = await ctx.plugin(TestPersistence)
  540. await expect(ctx.sessionQuery.searchSessions({
  541. query: 'live',
  542. sessionFilters: [{ kind: 'availability', values: ['persisted'] }],
  543. })).resolves.toMatchObject({
  544. items: [{ header: shared, live: true, persisted: true }],
  545. })
  546. expect(TestPersistence.loads.get(shared.id)).toBeUndefined()
  547. detach()
  548. await expect(ctx.sessionQuery.searchSessions({ query: 'persisted' }))
  549. .resolves.toMatchObject({ items: [{ header: shared, live: false, persisted: true }] })
  550. expect(TestPersistence.loads.get(shared.id)).toBe(1)
  551. await persistence.dispose()
  552. })
  553. it('retries when a live owner attaches during persistence observation', async () => {
  554. TestPersistence.reset()
  555. const ctx = await liveContext()
  556. await ctx.plugin(TestPersistence)
  557. TestPersistence.snapshotEffect = () => {
  558. TestPersistence.snapshotEffect = undefined
  559. ctx.sessions.create(SessionId('attached'), { seed: messageEvents('attached needle') })
  560. }
  561. await expect(ctx.sessionQuery.searchSessions({ query: 'attached' }))
  562. .resolves.toMatchObject({ items: [{ header: { id: SessionId('attached') } }] })
  563. })
  564. it('retries when one live owner replaces another during persistence observation', async () => {
  565. TestPersistence.reset()
  566. const ctx = await liveContext()
  567. const first = ctx.sessions.prepare(SessionId('first'), { seed: messageEvents('first needle') })
  568. const detachFirst = ctx.sessions.enter(first)
  569. ctx.sessions.announce(first)
  570. await ctx.plugin(TestPersistence)
  571. TestPersistence.snapshotEffect = () => {
  572. TestPersistence.snapshotEffect = undefined
  573. detachFirst()
  574. ctx.sessions.create(SessionId('second'), { seed: messageEvents('second needle') })
  575. }
  576. await expect(ctx.sessionQuery.searchSessions({ query: 'second' }))
  577. .resolves.toMatchObject({ items: [{ header: { id: SessionId('second') } }] })
  578. })
  579. it('uses the reconciled persistence binding through the query boundary', async () => {
  580. const durable = header('post-reconcile-unmount')
  581. TestPersistence.reset([{ meta: durable, events: [
  582. ...messageEvents('durable needle', 1),
  583. { ...messageEvents('durable needle again', 2)[0]!, seq: 1 },
  584. ] }])
  585. const ctx = await liveContext({ path: ':memory:', defaultLimit: 1, maxLimit: 2 })
  586. const persistence = await ctx.plugin(TestPersistence)
  587. const internals = ctx.sessionQuery as unknown as {
  588. _reconcile(signal: AbortSignal | undefined): Promise<{
  589. identity: symbol
  590. service?: SessionPersistence
  591. }>
  592. }
  593. const reconcile = internals._reconcile.bind(internals)
  594. const boundary = vi.spyOn(internals, '_reconcile').mockImplementation(async (signal) => {
  595. const binding = await reconcile(signal)
  596. await persistence.dispose()
  597. return binding
  598. })
  599. const page = await ctx.sessionQuery.searchEvents({
  600. sessionId: durable.id,
  601. query: 'needle',
  602. limit: 1,
  603. })
  604. expect(page.items).toMatchObject([{ sessionId: durable.id }])
  605. expect(page.nextCursor).toEqual(expect.any(String))
  606. boundary.mockRestore()
  607. await expect(ctx.sessionQuery.searchEvents({ sessionId: durable.id, query: 'needle' }))
  608. .rejects.toThrow(expectCode('SESSION_QUERY_SESSION_NOT_FOUND'))
  609. })
  610. it('discards a stale list rejection when persistence unmounts during observation', async () => {
  611. const durable = header('racing')
  612. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  613. const ctx = await liveContext()
  614. const persistenceFiber = await ctx.plugin(TestPersistence)
  615. let release!: () => void
  616. TestPersistence.listGate = new Promise<void>((resolve) => { release = resolve })
  617. let markStarted!: () => void
  618. const started = new Promise<void>((resolve) => { markStarted = resolve })
  619. TestPersistence.listStarted = () => {
  620. TestPersistence.listStarted = undefined
  621. markStarted()
  622. }
  623. const search = ctx.sessionQuery.searchSessions({ query: 'needle' })
  624. await started
  625. await persistenceFiber.dispose()
  626. TestPersistence.failure = new Error('stale backend rejection')
  627. release()
  628. await expect(search).resolves.toEqual({ items: [] })
  629. })
  630. it('retries against a replacement after the prior binding rejects', async () => {
  631. const durable = header('replacement')
  632. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  633. const ctx = await liveContext()
  634. const prior = await ctx.plugin(TestPersistence)
  635. let rejectPrior!: (reason: unknown) => void
  636. TestPersistence.listGate = new Promise<void>((_resolve, reject) => { rejectPrior = reject })
  637. let markStarted!: () => void
  638. const started = new Promise<void>((resolve) => { markStarted = resolve })
  639. TestPersistence.listStarted = () => {
  640. TestPersistence.listStarted = undefined
  641. markStarted()
  642. }
  643. const search = ctx.sessionQuery.searchSessions({ query: 'needle' })
  644. await started
  645. await prior.dispose()
  646. TestPersistence.listGate = undefined
  647. const replacement = await ctx.plugin(TestPersistence)
  648. rejectPrior(new Error('stale prior binding'))
  649. await expect(search).resolves.toMatchObject({ items: [{ header: durable }] })
  650. await replacement.dispose()
  651. })
  652. it('reloads a replacement source even when its opaque revisions collide', async () => {
  653. const durable = header('colliding-replacement')
  654. TestPersistence.reset([{ meta: durable, events: messageEvents('old content') }])
  655. const revision = TestPersistence.revisions.get(durable.id)!
  656. const ctx = await liveContext()
  657. const prior = await ctx.plugin(TestPersistence)
  658. await expect(ctx.sessionQuery.searchSessions({ query: 'old' }))
  659. .resolves.toMatchObject({ items: [{ header: durable }] })
  660. await prior.dispose()
  661. TestPersistence.set({ meta: durable, events: messageEvents('new needle') })
  662. TestPersistence.revisions.set(durable.id, revision)
  663. const replacement = await ctx.plugin(TestPersistence)
  664. const page = await ctx.sessionQuery.searchSessions({ query: 'new needle' })
  665. expect(TestPersistence.loads.get(durable.id)).toBe(2)
  666. expect(page).toMatchObject({ items: [{ header: durable }] })
  667. await expect(ctx.sessionQuery.searchSessions({ query: 'old' })).resolves.toEqual({ items: [] })
  668. expect(TestPersistence.loads.get(durable.id)).toBe(2)
  669. await replacement.dispose()
  670. })
  671. it('retries when a successful observation belongs to a source unmounted during listing', async () => {
  672. const durable = header('successful-unmount')
  673. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  674. const ctx = await liveContext()
  675. const persistence = await ctx.plugin(TestPersistence)
  676. let lists = 0
  677. TestPersistence.snapshotEffect = async () => {
  678. lists += 1
  679. if (lists === 2) await persistence.dispose()
  680. }
  681. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' })).resolves.toEqual({ items: [] })
  682. expect(lists).toBe(2)
  683. })
  684. it('retries when the snapshot population changes during observation', async () => {
  685. const first = header('first')
  686. const added = header('added-during-list')
  687. TestPersistence.reset([{ meta: first, events: messageEvents('first needle') }])
  688. const ctx = await liveContext()
  689. await ctx.plugin(TestPersistence)
  690. TestPersistence.snapshotEffect = () => {
  691. TestPersistence.snapshotEffect = undefined
  692. TestPersistence.set({ meta: added, events: messageEvents('added needle') })
  693. }
  694. const page = await ctx.sessionQuery.searchSessions({ query: 'needle' })
  695. expect(page.items.map(item => item.header.id).sort()).toEqual([added.id, first.id].sort())
  696. expect(TestPersistence.loads.get(first.id)).toBe(2)
  697. expect(TestPersistence.loads.get(added.id)).toBe(1)
  698. })
  699. it('fails after one retry when persistence snapshots keep changing', async () => {
  700. const durable = header('continuous-mutation')
  701. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  702. const ctx = await liveContext()
  703. await ctx.plugin(TestPersistence)
  704. let lists = 0
  705. TestPersistence.snapshotEffect = () => {
  706. lists += 1
  707. TestPersistence.set({ meta: durable, events: messageEvents(`durable needle ${lists}`) })
  708. }
  709. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  710. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  711. expect(lists).toBe(4)
  712. })
  713. it('retries if the persistence binding changes while live sessions are observed', async () => {
  714. const durable = header('live-boundary-retry')
  715. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  716. const ctx = await liveContext()
  717. await ctx.plugin(TestPersistence)
  718. const internals = ctx.sessionQuery as unknown as {
  719. _persistenceBinding: { identity: symbol; service?: SessionPersistence }
  720. }
  721. const originalList = ctx.sessions.list.bind(ctx.sessions)
  722. let bumped = false
  723. const list = vi.spyOn(ctx.sessions, 'list').mockImplementation(() => {
  724. if (!bumped) {
  725. bumped = true
  726. internals._persistenceBinding = {
  727. ...internals._persistenceBinding,
  728. identity: Symbol(),
  729. }
  730. }
  731. return originalList()
  732. })
  733. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  734. .resolves.toMatchObject({ items: [{ header: durable }] })
  735. expect(TestPersistence.loads.get(durable.id)).toBe(2)
  736. list.mockRestore()
  737. })
  738. it('rejects malformed snapshots and preserves typed persistence failures', async () => {
  739. const durable = header('invalid-snapshot')
  740. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  741. const ctx = await liveContext()
  742. await ctx.plugin(TestPersistence)
  743. TestPersistence.snapshotOverride = () => 'not-an-array' as never
  744. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  745. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  746. TestPersistence.snapshotOverride = () => [{ header: durable, revision: 1 as never }]
  747. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  748. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  749. TestPersistence.snapshotOverride = () => [
  750. { header: durable, revision: SessionPersistenceRevision('duplicate:1') },
  751. { header: durable, revision: SessionPersistenceRevision('duplicate:2') },
  752. ]
  753. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  754. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  755. TestPersistence.snapshotOverride = undefined
  756. const typed = new SessionQueryError('typed persistence failure', 'SESSION_QUERY_PERSISTENCE_FAILED')
  757. TestPersistence.failure = typed
  758. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' })).rejects.toBe(typed)
  759. })
  760. it('rejects immutable header conflicts between live and persisted sources', async () => {
  761. const shared = header('conflict', 10, { delegationDepth: 1 })
  762. TestPersistence.reset([{ meta: shared, events: messageEvents('persisted needle') }])
  763. const ctx = await liveContext()
  764. await ctx.plugin(TestPersistence)
  765. ctx.sessions.create(shared.id, {
  766. seed: messageEvents('live needle'),
  767. meta: { createdAt: 10, delegationDepth: 2 },
  768. })
  769. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  770. .rejects.toThrow(expectCode('SESSION_QUERY_SOURCE_CONFLICT'))
  771. })
  772. it('preserves unchanged persisted generations while reconciling new, changed, and deleted rows', async () => {
  773. const path = await temporaryPath()
  774. const unchanged = header('unchanged')
  775. const changed = header('changed')
  776. const deleted = header('deleted')
  777. TestPersistence.reset([
  778. { meta: unchanged, events: messageEvents('unchanged needle') },
  779. { meta: changed, events: messageEvents('old needle') },
  780. { meta: deleted, events: messageEvents('deleted needle') },
  781. ])
  782. const first = new Context()
  783. await first.plugin(SessionStore)
  784. const firstPersistence = await first.plugin(TestPersistence)
  785. const firstSearch = await first.plugin(SessionQuerySqlite, { path })
  786. await first.sessionQuery.searchSessions({ query: 'needle' })
  787. expect(Object.fromEntries(TestPersistence.loads)).toEqual({ unchanged: 1, changed: 1, deleted: 1 })
  788. await first.sessionQuery.searchSessions({ query: 'needle' })
  789. expect(Object.fromEntries(TestPersistence.loads)).toEqual({ unchanged: 1, changed: 1, deleted: 1 })
  790. await firstSearch.dispose()
  791. await firstPersistence.dispose()
  792. const beforeDb = new DatabaseSync(path)
  793. const beforeRows = beforeDb.prepare('SELECT id, generation FROM persisted_sessions ORDER BY id').all() as Array<{ id: string; generation: number }>
  794. beforeDb.close()
  795. const before = new Map(beforeRows.map(row => [row.id, row.generation]))
  796. const added = header('added')
  797. TestPersistence.entries.delete(deleted.id)
  798. TestPersistence.set({ meta: changed, events: messageEvents('changed needle') })
  799. TestPersistence.set({ meta: added, events: messageEvents('added needle') })
  800. const second = new Context()
  801. await second.plugin(SessionStore)
  802. const secondPersistence = await second.plugin(TestPersistence)
  803. const secondSearch = await second.plugin(SessionQuerySqlite, { path })
  804. const result = await second.sessionQuery.searchSessions({ query: 'needle' })
  805. expect(result.items.map(item => item.header.id).sort()).toEqual([added.id, changed.id, unchanged.id].sort())
  806. expect(Object.fromEntries(TestPersistence.loads)).toEqual({
  807. unchanged: 1,
  808. changed: 2,
  809. deleted: 1,
  810. added: 1,
  811. })
  812. await secondSearch.dispose()
  813. await secondPersistence.dispose()
  814. const afterDb = new DatabaseSync(path)
  815. const afterRows = afterDb.prepare('SELECT id, generation FROM persisted_sessions ORDER BY id').all() as Array<{ id: string; generation: number }>
  816. afterDb.close()
  817. const after = new Map(afterRows.map(row => [row.id, row.generation]))
  818. expect(after.get(unchanged.id)).toBe(before.get(unchanged.id))
  819. expect(after.get(changed.id)).toBeGreaterThan(before.get(changed.id)!)
  820. expect(after.has(deleted.id)).toBe(false)
  821. expect(after.has(added.id)).toBe(true)
  822. })
  823. it('drops connection-local live overlays on reopen and retains persistent bases', async () => {
  824. const path = await temporaryPath()
  825. const shared = header('shared', 10)
  826. TestPersistence.reset([{ meta: shared, events: messageEvents('persisted needle') }])
  827. const first = new Context()
  828. await first.plugin(SessionStore)
  829. const persistence = await first.plugin(TestPersistence)
  830. const live = first.sessions.create(shared.id, { seed: messageEvents('live needle'), meta: { createdAt: 10 } })
  831. const search = await first.plugin(SessionQuerySqlite, { path })
  832. await expect(first.sessionQuery.searchEvents({ sessionId: live.id, query: 'live' })).resolves.toMatchObject({ items: [{}] })
  833. await search.dispose()
  834. await persistence.dispose()
  835. const second = new Context()
  836. await second.plugin(SessionStore)
  837. const persistenceAgain = await second.plugin(TestPersistence)
  838. const searchAgain = await second.plugin(SessionQuerySqlite, { path })
  839. await expect(second.sessionQuery.searchSessions({ query: 'live' })).resolves.toEqual({ items: [] })
  840. await expect(second.sessionQuery.searchSessions({ query: 'persisted' }))
  841. .resolves.toMatchObject({ items: [{ header: shared, live: false, persisted: true }] })
  842. expect(TestPersistence.loads.get(shared.id)).toBe(1)
  843. await searchAgain.dispose()
  844. await persistenceAgain.dispose()
  845. })
  846. it('refreshes the stored revision after a mutating load repair', async () => {
  847. const durable = header('repair')
  848. TestPersistence.reset([{ meta: durable, events: messageEvents('before repair') }])
  849. TestPersistence.loadEffect = (entry) => {
  850. entry.events = messageEvents('repaired needle')
  851. }
  852. const ctx = await liveContext()
  853. await ctx.plugin(TestPersistence)
  854. await expect(ctx.sessionQuery.searchSessions({ query: 'repaired' }))
  855. .resolves.toMatchObject({ items: [{ header: durable }] })
  856. expect(TestPersistence.loads.get(durable.id)).toBe(2)
  857. await ctx.sessionQuery.searchSessions({ query: 'repaired' })
  858. expect(TestPersistence.loads.get(durable.id)).toBe(2)
  859. })
  860. it('recovers on the next search after source and SQLite transaction failures', async () => {
  861. TestPersistence.reset([{ meta: header('durable'), events: messageEvents('durable needle') }])
  862. const ctx = await liveContext()
  863. await ctx.plugin(TestPersistence)
  864. TestPersistence.failure = 'offline'
  865. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  866. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  867. const signal = new AbortController().signal
  868. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal }))
  869. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  870. TestPersistence.failure = new Error('still offline')
  871. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal }))
  872. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  873. TestPersistence.failure = undefined
  874. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' })).resolves.toMatchObject({ items: [{}] })
  875. const live = ctx.sessions.create(SessionId('live'), { seed: messageEvents('base') })
  876. await ctx.sessionQuery.searchEvents({ sessionId: live.id, query: 'base' })
  877. const db = (ctx.sessionQuery as unknown as { _db: DatabaseSync })._db
  878. db.exec('PRAGMA query_only = ON')
  879. live.append('user/message', { content: [{ type: 'text', text: 'retry needle' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  880. await expect(ctx.sessionQuery.searchEvents({ sessionId: live.id, query: 'needle' }))
  881. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  882. db.exec('PRAGMA query_only = OFF')
  883. await expect(ctx.sessionQuery.searchEvents({ sessionId: live.id, query: 'needle' }))
  884. .resolves.toMatchObject({ items: [{ seq: 1 }] })
  885. })
  886. })
  887. describe('SQLite schema, cancellation, and real persistence integration', () => {
  888. it('creates a new database and WAL sidecars owner-only without changing its parent mode', async () => {
  889. if (process.platform === 'win32') return
  890. const path = await temporaryPath()
  891. const directory = dirname(path)
  892. await chmod(directory, 0o755)
  893. const ctx = await liveContext({ path })
  894. await ctx.sessionQuery.searchSessions({ query: 'needle' })
  895. expect((await stat(directory)).mode & 0o777).toBe(0o755)
  896. expect((await stat(path)).mode & 0o777).toBe(0o600)
  897. expect((await stat(`${path}-wal`)).mode & 0o777).toBe(0o600)
  898. expect((await stat(`${path}-shm`)).mode & 0o777).toBe(0o600)
  899. await (ctx.sessionQuery as SessionQuerySqlite).close()
  900. })
  901. it('creates a persistent rollback journal owner-only', async () => {
  902. if (process.platform === 'win32') return
  903. const path = await temporaryPath()
  904. const ctx = await liveContext({ path, journalMode: 'persist' })
  905. await ctx.sessionQuery.searchSessions({ query: 'needle' })
  906. expect((await stat(path)).mode & 0o777).toBe(0o600)
  907. expect((await stat(`${path}-journal`)).mode & 0o777).toBe(0o600)
  908. await (ctx.sessionQuery as SessionQuerySqlite).close()
  909. })
  910. it('preserves the mode of an existing database file', async () => {
  911. if (process.platform === 'win32') return
  912. const path = await temporaryPath()
  913. await writeFile(path, '', { mode: 0o644 })
  914. await chmod(path, 0o644)
  915. const ctx = await liveContext({ path, journalMode: 'delete' })
  916. await ctx.sessionQuery.searchSessions({ query: 'needle' })
  917. expect((await stat(path)).mode & 0o777).toBe(0o644)
  918. await (ctx.sessionQuery as SessionQuerySqlite).close()
  919. })
  920. it('surfaces filesystem failures while pre-creating the database', async () => {
  921. const path = `${await temporaryPath()}\0`
  922. const ctx = new Context()
  923. await ctx.plugin(SessionStore)
  924. await expect(ctx.plugin(SessionQuerySqlite, { path })).rejects.toMatchObject({
  925. code: 'SESSION_QUERY_INDEX_FAILED',
  926. cause: { code: 'ERR_INVALID_ARG_VALUE' },
  927. })
  928. expect(ctx.sessionQuery).toBeUndefined()
  929. })
  930. it('resets a recognized incompatible schema but refuses unknown or foreign tables', async () => {
  931. const stalePath = await temporaryPath('stale.db')
  932. const staleOwner = await liveContext({ path: stalePath })
  933. await (staleOwner.sessionQuery as SessionQuerySqlite).close()
  934. const stale = new DatabaseSync(stalePath)
  935. stale.exec('PRAGMA user_version = 999')
  936. stale.close()
  937. const staleCtx = await liveContext({ path: stalePath })
  938. staleCtx.sessions.create(SessionId('live'), { seed: messageEvents('needle') })
  939. await staleCtx.sessionQuery.searchSessions({ query: 'needle' })
  940. await (staleCtx.sessionQuery as SessionQuerySqlite).close()
  941. const rebuilt = new DatabaseSync(stalePath)
  942. expect((rebuilt.prepare('PRAGMA user_version').get() as { user_version: number }).user_version)
  943. .toBe(SESSION_QUERY_SQLITE_SCHEMA_VERSION)
  944. rebuilt.close()
  945. const augmentedPath = await temporaryPath('augmented.db')
  946. const augmentedOwner = await liveContext({ path: augmentedPath })
  947. await (augmentedOwner.sessionQuery as SessionQuerySqlite).close()
  948. const augmented = new DatabaseSync(augmentedPath)
  949. augmented.exec('CREATE TABLE unrelated(value TEXT)')
  950. augmented.exec("INSERT INTO unrelated VALUES ('safe')")
  951. augmented.exec('PRAGMA user_version = 999')
  952. augmented.close()
  953. const augmentedCtx = new Context()
  954. await augmentedCtx.plugin(SessionStore)
  955. await expect(augmentedCtx.plugin(SessionQuerySqlite, { path: augmentedPath }))
  956. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  957. expect(augmentedCtx.sessionQuery).toBeUndefined()
  958. const stillAugmented = new DatabaseSync(augmentedPath)
  959. expect(stillAugmented.prepare('SELECT value FROM unrelated').get()).toEqual({ value: 'safe' })
  960. expect(stillAugmented.prepare('PRAGMA user_version').get()).toEqual({ user_version: 999 })
  961. stillAugmented.close()
  962. const foreignPath = await temporaryPath('foreign.db')
  963. const foreign = new DatabaseSync(foreignPath)
  964. foreign.exec('PRAGMA journal_mode = WAL')
  965. foreign.exec('CREATE TABLE canonical(value TEXT)')
  966. foreign.exec("INSERT INTO canonical VALUES ('safe')")
  967. foreign.close()
  968. const foreignCtx = new Context()
  969. await foreignCtx.plugin(SessionStore)
  970. await expect(foreignCtx.plugin(SessionQuerySqlite, { path: foreignPath, journalMode: 'delete' }))
  971. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  972. expect(foreignCtx.sessionQuery).toBeUndefined()
  973. const stillForeign = new DatabaseSync(foreignPath)
  974. expect(stillForeign.prepare('SELECT value FROM canonical').get()).toEqual({ value: 'safe' })
  975. expect(stillForeign.prepare('PRAGMA journal_mode').get()).toEqual({ journal_mode: 'wal' })
  976. stillForeign.close()
  977. const otherAppPath = await temporaryPath('other-app.db')
  978. const otherApp = new DatabaseSync(otherAppPath)
  979. otherApp.exec('PRAGMA application_id = 123')
  980. otherApp.close()
  981. const otherAppCtx = new Context()
  982. await otherAppCtx.plugin(SessionStore)
  983. await expect(otherAppCtx.plugin(SessionQuerySqlite, { path: otherAppPath }))
  984. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  985. expect(otherAppCtx.sessionQuery).toBeUndefined()
  986. })
  987. it('fails plugin initialization without an unhandled rejection or partial service', async () => {
  988. const path = await temporaryPath('never-queried.db')
  989. const foreign = new DatabaseSync(path)
  990. foreign.exec('CREATE TABLE canonical(value TEXT)')
  991. foreign.close()
  992. const unhandled: unknown[] = []
  993. const onUnhandled = (reason: unknown) => { unhandled.push(reason) }
  994. process.on('unhandledRejection', onUnhandled)
  995. try {
  996. const ctx = new Context()
  997. await ctx.plugin(SessionStore)
  998. await expect(ctx.plugin(SessionQuerySqlite, { path }))
  999. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1000. await new Promise<void>((resolve) => { setImmediate(resolve) })
  1001. expect(unhandled).toEqual([])
  1002. expect(ctx.sessionQuery).toBeUndefined()
  1003. } finally {
  1004. process.off('unhandledRejection', onUnhandled)
  1005. }
  1006. })
  1007. it('cancels both queued and in-flight source waits without committing them', async () => {
  1008. TestPersistence.reset()
  1009. const ctx = await liveContext()
  1010. await ctx.plugin(TestPersistence)
  1011. const boundaryController = new AbortController()
  1012. const boundary = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: boundaryController.signal })
  1013. queueMicrotask(() => { boundaryController.abort() })
  1014. await expect(boundary).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1015. const readyController = new AbortController()
  1016. readyController.abort()
  1017. const internals = ctx.sessionQuery as unknown as {
  1018. _ensureReady(signal: AbortSignal): Promise<void>
  1019. }
  1020. await expect(internals._ensureReady(readyController.signal))
  1021. .rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1022. let releaseBlocking!: () => void
  1023. TestPersistence.listGate = new Promise<void>((resolve) => { releaseBlocking = resolve })
  1024. let markBlockingStarted!: () => void
  1025. const blockingStarted = new Promise<void>((resolve) => { markBlockingStarted = resolve })
  1026. TestPersistence.listStarted = () => {
  1027. TestPersistence.listStarted = undefined
  1028. markBlockingStarted()
  1029. }
  1030. const blocking = ctx.sessionQuery.searchSessions({ query: 'needle' })
  1031. await blockingStarted
  1032. const queuedController = new AbortController()
  1033. const queued = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: queuedController.signal })
  1034. queuedController.abort()
  1035. await expect(queued).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1036. releaseBlocking()
  1037. await expect(blocking).resolves.toEqual({ items: [] })
  1038. TestPersistence.set({
  1039. meta: header('uncommitted'),
  1040. events: messageEvents('durable needle'),
  1041. })
  1042. let releaseActive!: () => void
  1043. TestPersistence.listGate = new Promise<void>((resolve) => { releaseActive = resolve })
  1044. let markActiveStarted!: () => void
  1045. const activeStarted = new Promise<void>((resolve) => { markActiveStarted = resolve })
  1046. TestPersistence.listStarted = () => {
  1047. TestPersistence.listStarted = undefined
  1048. markActiveStarted()
  1049. }
  1050. const activeController = new AbortController()
  1051. const active = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: activeController.signal })
  1052. await activeStarted
  1053. activeController.abort()
  1054. await expect(active).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1055. releaseActive()
  1056. const db = (ctx.sessionQuery as unknown as { _db: DatabaseSync })._db
  1057. expect(db.prepare('SELECT COUNT(*) AS count FROM persisted_sessions').get()).toEqual({ count: 0 })
  1058. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  1059. .resolves.toMatchObject({ items: [{ header: { id: SessionId('uncommitted') } }] })
  1060. })
  1061. it('rejects queued and future work when close waits for an accepted operation', async () => {
  1062. TestPersistence.reset()
  1063. let release!: () => void
  1064. TestPersistence.listGate = new Promise<void>((resolve) => { release = resolve })
  1065. let markStarted!: () => void
  1066. const started = new Promise<void>((resolve) => { markStarted = resolve })
  1067. TestPersistence.listStarted = () => {
  1068. TestPersistence.listStarted = undefined
  1069. markStarted()
  1070. }
  1071. const ctx = await liveContext()
  1072. await ctx.plugin(TestPersistence)
  1073. const search = ctx.sessionQuery as SessionQuerySqlite
  1074. const accepted = search.searchSessions({ query: 'needle' })
  1075. await started
  1076. const queued = search.searchSessions({ query: 'needle' })
  1077. const closing = search.close()
  1078. const repeatedClose = search.close()
  1079. expect(repeatedClose).toBe(closing)
  1080. release()
  1081. await expect(accepted).resolves.toEqual({ items: [] })
  1082. await expect(queued).rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1083. await Promise.all([closing, repeatedClose])
  1084. await expect(search.searchSessions({ query: 'needle' }))
  1085. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1086. expect(search.close()).toBe(closing)
  1087. })
  1088. it('awaits optional-persistence child-fiber quiescence on disposal', async () => {
  1089. TestPersistence.reset()
  1090. const ctx = new Context()
  1091. await ctx.plugin(SessionStore)
  1092. const search = await ctx.plugin(SessionQuerySqlite, { path: ':memory:' })
  1093. const persistence = await ctx.plugin(TestPersistence)
  1094. const optional = (ctx.sessionQuery as unknown as {
  1095. _optionalPersistenceFiber: Fiber
  1096. })._optionalPersistenceFiber
  1097. let release!: () => void
  1098. const cleanup = new Promise<void>((resolve) => { release = resolve })
  1099. optional.ctx.effect(() => () => cleanup)
  1100. let settled = false
  1101. const disposing = search.dispose().then(() => { settled = true })
  1102. await Promise.resolve()
  1103. expect(settled).toBe(false)
  1104. release()
  1105. await disposing
  1106. await persistence.dispose()
  1107. })
  1108. it('combines the real SQLite persistence backend with the real search service keylessly', async () => {
  1109. const persistencePath = await temporaryPath('canonical.db')
  1110. const searchPath = await temporaryPath('derived.db')
  1111. const ctx = new Context()
  1112. await ctx.plugin(SessionStore)
  1113. const persistence = await ctx.plugin(SessionPersistenceSqlite, { path: persistencePath })
  1114. const search = await ctx.plugin(SessionQuerySqlite, { path: searchPath })
  1115. const meta = header('real', 10, { cwd: '/work' })
  1116. await ctx.sessionPersistence.create(meta)
  1117. await ctx.sessionPersistence.append(meta.id, messageEvents('real SQLite needle'))
  1118. await expect(ctx.sessionQuery.searchSessions({ query: 'SQLite needle' }))
  1119. .resolves.toMatchObject({ items: [{ header: meta, persisted: true, live: false }] })
  1120. await expect(ctx.sessionQuery.searchEvents({ sessionId: meta.id, query: 'SQLite needle' }))
  1121. .resolves.toMatchObject({ items: [{ sessionId: meta.id, seq: 0 }] })
  1122. await expect(ctx.sessionQuery.searchEvents({ sessionId: SessionId('absent'), query: 'needle' }))
  1123. .rejects.toThrow(expectCode('SESSION_QUERY_SESSION_NOT_FOUND'))
  1124. await search.dispose()
  1125. await expect(ctx.sessionPersistence.load(meta.id)).resolves.toMatchObject({ meta, events: [{ seq: 0 }] })
  1126. await persistence.dispose()
  1127. })
  1128. it('reconciles colliding local revisions when a derived index reopens against another SQLite store', async () => {
  1129. const persistencePathA = await temporaryPath('canonical-a.db')
  1130. const persistencePathB = await temporaryPath('canonical-b.db')
  1131. const searchPath = await temporaryPath('derived-collision.db')
  1132. const shared = header('same-id', 10)
  1133. const first = new Context()
  1134. await first.plugin(SessionStore)
  1135. const persistenceA = await first.plugin(SessionPersistenceSqlite, { path: persistencePathA })
  1136. await first.sessionPersistence.create(shared)
  1137. await first.sessionPersistence.append(shared.id, messageEvents('alpha source'))
  1138. const loadA = vi.spyOn(first.sessionPersistence, 'load')
  1139. const searchA = await first.plugin(SessionQuerySqlite, { path: searchPath })
  1140. await expect(first.sessionQuery.searchSessions({ query: 'alpha' }))
  1141. .resolves.toMatchObject({ items: [{ header: shared }] })
  1142. expect(loadA).toHaveBeenCalledTimes(1)
  1143. await searchA.dispose()
  1144. await persistenceA.dispose()
  1145. const reopened = new Context()
  1146. await reopened.plugin(SessionStore)
  1147. const persistenceAAgain = await reopened.plugin(SessionPersistenceSqlite, { path: persistencePathA })
  1148. const reopenedLoad = vi.spyOn(reopened.sessionPersistence, 'load')
  1149. const searchAAgain = await reopened.plugin(SessionQuerySqlite, { path: searchPath })
  1150. await expect(reopened.sessionQuery.searchSessions({ query: 'alpha' }))
  1151. .resolves.toMatchObject({ items: [{ header: shared }] })
  1152. expect(reopenedLoad).not.toHaveBeenCalled()
  1153. await searchAAgain.dispose()
  1154. await persistenceAAgain.dispose()
  1155. const second = new Context()
  1156. await second.plugin(SessionStore)
  1157. const persistenceB = await second.plugin(SessionPersistenceSqlite, { path: persistencePathB })
  1158. await second.sessionPersistence.create(shared)
  1159. await second.sessionPersistence.append(shared.id, messageEvents('bravo source'))
  1160. const loadB = vi.spyOn(second.sessionPersistence, 'load')
  1161. const searchB = await second.plugin(SessionQuerySqlite, { path: searchPath })
  1162. await expect(second.sessionQuery.searchSessions({ query: 'bravo' }))
  1163. .resolves.toMatchObject({ items: [{ header: shared }] })
  1164. await expect(second.sessionQuery.searchSessions({ query: 'alpha' })).resolves.toEqual({ items: [] })
  1165. expect(loadB).toHaveBeenCalledTimes(1)
  1166. await searchB.dispose()
  1167. await persistenceB.dispose()
  1168. })
  1169. })