sqlite.spec.ts 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. import { createAssistantMessage, createUserMessage } from '@deepseek-ai/dsh-llm'
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { Context, type Fiber } from '@deepseek-ai/cordis'
  4. import { DatabaseSync } from 'node:sqlite'
  5. import { chmod, mkdtemp, rm, stat, writeFile } from 'node:fs/promises'
  6. import { tmpdir } from 'node:os'
  7. import { dirname, join } from 'node:path'
  8. import SessionStore, { SESSION_FORMAT_VERSION, SessionId } from '@deepseek-ai/dsh-session'
  9. import type { SessionEvent, SessionHeader, SessionId as SessionIdType } from '@deepseek-ai/dsh-session'
  10. import SessionPersistence, { SessionPersistenceRevision } from '@deepseek-ai/dsh-session-persistence'
  11. import type { SessionPersistenceSnapshot } from '@deepseek-ai/dsh-session-persistence'
  12. import SessionPersistenceSqlite from '@deepseek-ai/dsh-session-persistence-sqlite'
  13. import SessionQuerySqlite, {
  14. SESSION_QUERY_SQLITE_SCHEMA_VERSION,
  15. } from '@deepseek-ai/dsh-session-query-sqlite'
  16. import {
  17. SESSION_QUERY_DEFAULT_PERSISTED_INSPECT_CONCURRENCY,
  18. SessionQueryError,
  19. SessionSearchCursor,
  20. type SessionAvailability,
  21. type SessionQueryErrorCode,
  22. type SessionSearchRequest,
  23. } from '@deepseek-ai/dsh-session-query'
  24. const temporaryDirectories: string[] = []
  25. afterEach(async () => {
  26. for (const directory of temporaryDirectories.splice(0)) {
  27. await rm(directory, { recursive: true, force: true })
  28. }
  29. })
  30. async function temporaryPath(name = 'search.db'): Promise<string> {
  31. const directory = await mkdtemp(join(tmpdir(), 'dsh-session-search-'))
  32. temporaryDirectories.push(directory)
  33. return join(directory, name)
  34. }
  35. function header(id: string, createdAt = 1, extra: Partial<SessionHeader> = {}): SessionHeader {
  36. return { version: SESSION_FORMAT_VERSION, id: SessionId(id), createdAt, ...extra }
  37. }
  38. function messageEvents(text: string, time = 1): SessionEvent[] {
  39. return [{
  40. type: 'user/message',
  41. seq: 0,
  42. time,
  43. data: createUserMessage({
  44. content: [{ type: 'text', text }], source: { kind: 'user' },
  45. }),
  46. surfaceOp: 'append',
  47. }]
  48. }
  49. function expectCode(code: SessionQueryErrorCode): Error {
  50. return expect.objectContaining({ code }) as Error
  51. }
  52. function replaceCursorOffset(
  53. cursor: ReturnType<typeof SessionSearchCursor>,
  54. offset: number,
  55. ): ReturnType<typeof SessionSearchCursor> {
  56. const payload = JSON.parse(
  57. Buffer.from(cursor, 'base64url').toString('utf8'),
  58. ) as Record<string, unknown>
  59. return SessionSearchCursor(Buffer.from(JSON.stringify({ ...payload, offset }), 'utf8').toString('base64url'))
  60. }
  61. class TestPersistence extends SessionPersistence {
  62. override readonly supportsRawArtifacts = false
  63. static entries = new Map<SessionIdType, { meta: SessionHeader; events: SessionEvent[] }>()
  64. static revisions = new Map<SessionIdType, number>()
  65. static nextRevision = 0
  66. static loads = new Map<SessionIdType, number>()
  67. static inspections = new Map<SessionIdType, number>()
  68. static inspectSignals: Array<AbortSignal | undefined> = []
  69. static snapshotSignals: Array<AbortSignal | undefined> = []
  70. static loadEffect: ((entry: { meta: SessionHeader; events: SessionEvent[] }) => void) | undefined
  71. static inspectEffect: ((
  72. entry: { meta: SessionHeader; events: SessionEvent[] },
  73. signal?: AbortSignal,
  74. ) => void | Promise<void>) | undefined
  75. static listGate: Promise<void> | undefined
  76. static listStarted: (() => void) | undefined
  77. static snapshotEffect: ((signal?: AbortSignal) => void | Promise<void>) | undefined
  78. static snapshotOverride: (() => SessionPersistenceSnapshot[]) | undefined
  79. static failure: unknown
  80. locate(_meta: SessionHeader): undefined {
  81. return undefined
  82. }
  83. static reset(entries: readonly { meta: SessionHeader; events: SessionEvent[] }[] = []): void {
  84. this.entries = new Map()
  85. this.revisions = new Map()
  86. this.loads = new Map()
  87. this.inspections = new Map()
  88. this.inspectSignals = []
  89. this.snapshotSignals = []
  90. this.loadEffect = undefined
  91. this.inspectEffect = undefined
  92. for (const entry of entries) this.set(entry)
  93. this.listGate = undefined
  94. this.listStarted = undefined
  95. this.snapshotEffect = undefined
  96. this.snapshotOverride = undefined
  97. this.failure = undefined
  98. }
  99. static set(entry: { meta: SessionHeader; events: SessionEvent[] }): void {
  100. this.entries.set(entry.meta.id, structuredClone(entry))
  101. this.revisions.set(entry.meta.id, ++this.nextRevision)
  102. }
  103. create(meta: SessionHeader): Promise<void> {
  104. TestPersistence.set({ meta, events: [] })
  105. return Promise.resolve()
  106. }
  107. append(id: SessionIdType, events: readonly SessionEvent[]): Promise<void> {
  108. const entry = TestPersistence.entries.get(id)
  109. if (entry === undefined) return Promise.reject(new Error('missing test session'))
  110. entry.events.push(...structuredClone(events))
  111. TestPersistence.revisions.set(id, ++TestPersistence.nextRevision)
  112. return Promise.resolve()
  113. }
  114. async load(id: SessionIdType): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
  115. TestPersistence.loads.set(id, (TestPersistence.loads.get(id) ?? 0) + 1)
  116. if (TestPersistence.failure !== undefined) throw TestPersistence.failure
  117. const entry = TestPersistence.entries.get(id)
  118. if (entry === undefined) throw new Error('missing test session')
  119. if (TestPersistence.loadEffect !== undefined) {
  120. const effect = TestPersistence.loadEffect
  121. TestPersistence.loadEffect = undefined
  122. effect(entry)
  123. TestPersistence.revisions.set(id, ++TestPersistence.nextRevision)
  124. }
  125. return structuredClone(entry)
  126. }
  127. async inspect(id: SessionIdType, signal?: AbortSignal): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
  128. TestPersistence.inspections.set(id, (TestPersistence.inspections.get(id) ?? 0) + 1)
  129. TestPersistence.inspectSignals.push(signal)
  130. if (TestPersistence.failure !== undefined) throw TestPersistence.failure
  131. const entry = TestPersistence.entries.get(id)
  132. if (entry === undefined) throw new Error('missing test session')
  133. await TestPersistence.inspectEffect?.(entry, signal)
  134. TestPersistence.inspectEffect = undefined
  135. return structuredClone(entry)
  136. }
  137. async readFrom(id: SessionIdType, fromSeq: number, signal?: AbortSignal): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
  138. const whole = await this.inspect(id, signal)
  139. return { meta: whole.meta, events: whole.events.filter(event => event.seq >= fromSeq) }
  140. }
  141. async list(): Promise<SessionHeader[]> {
  142. TestPersistence.listStarted?.()
  143. await TestPersistence.listGate
  144. if (TestPersistence.failure !== undefined) throw TestPersistence.failure
  145. return [...TestPersistence.entries.values()].map(entry => structuredClone(entry.meta))
  146. }
  147. async listSnapshots(signal?: AbortSignal): Promise<SessionPersistenceSnapshot[]> {
  148. TestPersistence.snapshotSignals.push(signal)
  149. TestPersistence.listStarted?.()
  150. await TestPersistence.listGate
  151. if (TestPersistence.failure !== undefined) throw TestPersistence.failure
  152. const snapshots = TestPersistence.snapshotOverride?.()
  153. ?? [...TestPersistence.entries.values()].map(entry => ({
  154. header: structuredClone(entry.meta),
  155. revision: SessionPersistenceRevision(`test:${TestPersistence.revisions.get(entry.meta.id)}`),
  156. }))
  157. await TestPersistence.snapshotEffect?.(signal)
  158. return snapshots
  159. }
  160. }
  161. async function liveContext(config: ConstructorParameters<typeof SessionQuerySqlite>[1] = { path: ':memory:' }): Promise<Context> {
  162. const ctx = new Context()
  163. await ctx.plugin(SessionStore)
  164. await ctx.plugin(SessionQuerySqlite, config)
  165. return ctx
  166. }
  167. describe('SQLite session search', () => {
  168. it('defaults and validates opening policy and persisted inspection concurrency through its Cordis config', async () => {
  169. const defaultCtx = await liveContext()
  170. expect((defaultCtx.sessionQuery as SessionQuerySqlite).config.openAt).toBe('startup')
  171. expect((defaultCtx.sessionQuery as SessionQuerySqlite).config.persistedInspectConcurrency)
  172. .toBe(SESSION_QUERY_DEFAULT_PERSISTED_INSPECT_CONCURRENCY)
  173. const configuredValue = 2
  174. const configured = new SessionQuerySqlite.Config({
  175. path: ':memory:',
  176. openAt: 'first-search',
  177. persistedInspectConcurrency: configuredValue,
  178. })
  179. expect(configured.openAt).toBe('first-search')
  180. expect(configured.persistedInspectConcurrency).toBe(configuredValue)
  181. const configuredCtx = await liveContext(configured)
  182. expect((configuredCtx.sessionQuery as SessionQuerySqlite).config.persistedInspectConcurrency)
  183. .toBe(configuredValue)
  184. for (const persistedInspectConcurrency of [0, Number.MAX_SAFE_INTEGER + 1]) {
  185. expect(() => new SessionQuerySqlite.Config({
  186. path: ':memory:',
  187. persistedInspectConcurrency,
  188. })).toThrow()
  189. }
  190. expect(() => new SessionQuerySqlite.Config({
  191. path: ':memory:',
  192. openAt: 'later' as never,
  193. })).toThrow()
  194. })
  195. it('mounts and disposes first-search mode without opening its database', async () => {
  196. const path = await temporaryPath('unopened.db')
  197. const ctx = new Context()
  198. await ctx.plugin(SessionStore)
  199. const search = await ctx.plugin(SessionQuerySqlite, {
  200. path,
  201. openAt: 'first-search',
  202. })
  203. await expect(stat(path)).rejects.toMatchObject({ code: 'ENOENT' })
  204. await search.dispose()
  205. await expect(stat(path)).rejects.toMatchObject({ code: 'ENOENT' })
  206. })
  207. it('opens once on the first search and reuses readiness for later searches', async () => {
  208. const ctx = new Context()
  209. await ctx.plugin(SessionStore)
  210. await ctx.plugin(SessionQuerySqlite, {
  211. path: ':memory:',
  212. openAt: 'first-search',
  213. })
  214. const service = ctx.sessionQuery as SessionQuerySqlite
  215. const internals = service as unknown as { _open(): Promise<void> }
  216. const open = vi.spyOn(internals, '_open')
  217. await expect(service.searchSessions({ query: 'first' })).resolves.toEqual({ items: [] })
  218. await expect(service.searchSessions({ query: 'second' })).resolves.toEqual({ items: [] })
  219. expect(open).toHaveBeenCalledOnce()
  220. })
  221. it('shares one readiness promise across concurrent first searches', async () => {
  222. const ctx = new Context()
  223. await ctx.plugin(SessionStore)
  224. await ctx.plugin(SessionQuerySqlite, {
  225. path: ':memory:',
  226. openAt: 'first-search',
  227. })
  228. const service = ctx.sessionQuery as SessionQuerySqlite
  229. const internals = service as unknown as { _open(): Promise<void> }
  230. const originalOpen = internals._open.bind(internals)
  231. const release = Promise.withResolvers<undefined>()
  232. const started = Promise.withResolvers<undefined>()
  233. const open = vi.spyOn(internals, '_open').mockImplementation(async () => {
  234. started.resolve(undefined)
  235. await release.promise
  236. await originalOpen()
  237. })
  238. const first = service.searchSessions({ query: 'first' })
  239. const second = service.searchSessions({ query: 'second' })
  240. await started.promise
  241. expect(open).toHaveBeenCalledOnce()
  242. release.resolve(undefined)
  243. await expect(Promise.all([first, second])).resolves.toEqual([
  244. { items: [] },
  245. { items: [] },
  246. ])
  247. expect(open).toHaveBeenCalledOnce()
  248. })
  249. it('searches two-character Unicode61 tokens in live-only sessions', async () => {
  250. const ctx = await liveContext({ path: ':memory:', snippetChars: 20 })
  251. const session = ctx.sessions.create(SessionId('live'), {
  252. // agentPreset rides along: the index rebuilds the header a caller reads,
  253. // and a session listed under the wrong composition is a lie about what it
  254. // ran. The full-header comparison below is what pins every column.
  255. meta: { cwd: '/work', createdAt: 10, seedLength: 1, delegationDepth: 2, agentPreset: 'minimal' },
  256. })
  257. session.append(
  258. 'user/message',
  259. createUserMessage({
  260. content: [{ type: 'text', text: 'An AI helper' }], source: { kind: 'user' },
  261. }),
  262. { surfaceOp: 'append' },
  263. )
  264. await expect(ctx.sessionQuery.searchEvents({ sessionId: session.id, query: 'AI' }))
  265. .resolves.toMatchObject({
  266. session: { ...session.header, seedLength: 1 },
  267. items: [{ sessionId: session.id, seq: 0, snippet: 'An AI helper' }],
  268. })
  269. await expect(ctx.sessionQuery.searchSessions({ query: 'AI' }))
  270. .resolves.toMatchObject({ items: [{ header: { ...session.header, seedLength: 1 }, live: true, persisted: false }] })
  271. })
  272. it('excludes assistant reasoning while indexing visible answer text', async () => {
  273. const ctx = await liveContext()
  274. const session = ctx.sessions.create(SessionId('reasoning'))
  275. session.append(
  276. 'assistant/message',
  277. {
  278. turn: 1,
  279. step: 1,
  280. message: createAssistantMessage({
  281. content: [
  282. { type: 'reasoning', text: 'private-chain-marker' },
  283. { type: 'text', text: 'visible-answer-marker' },
  284. ],
  285. source: { provider: 'mock', model: 'mock' },
  286. }),
  287. },
  288. { surfaceOp: 'append' },
  289. )
  290. await expect(ctx.sessionQuery.searchSessions({ query: 'private-chain-marker' }))
  291. .resolves.toEqual({ items: [] })
  292. await expect(ctx.sessionQuery.searchSessions({ query: 'visible-answer-marker' }))
  293. .resolves.toMatchObject({
  294. items: [{
  295. header: { id: session.id },
  296. bestMatch: { snippet: 'visible-answer-marker' },
  297. }],
  298. })
  299. })
  300. it('searches all surfaces by default and applies metadata before ranking', async () => {
  301. const ctx = await liveContext({ path: ':memory:', defaultLimit: 10, maxLimit: 20 })
  302. const parent = SessionId('parent')
  303. const events: SessionEvent[] = [
  304. { type: 'user/message', seq: 0, time: 10, data: createUserMessage({
  305. content: [{ type: 'text', text: 'needle original' }], source: { kind: 'user' },
  306. }), surfaceOp: 'append' },
  307. { type: 'assistant/chunk', seq: 1, time: 11, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'needle raw' } } },
  308. { type: 'user/message', seq: 2, time: 12, data: createUserMessage({
  309. content: [{ type: 'text', text: 'needle summary' }], source: { kind: 'plugin', plugin: 'test' },
  310. }), surfaceOp: { op: 'replace', start: 0, end: 0 }, sourceEventSeqs: [0] },
  311. { type: 'turn/end', seq: 3, time: 13, data: { turn: 1, reason: { kind: 'error', error: { message: 'needle failure', code: 'UNKNOWN' } } } },
  312. ]
  313. ctx.sessions.create(SessionId('a'), { seed: events, meta: { cwd: '/a', parentSession: parent, createdAt: 20 } })
  314. ctx.sessions.create(SessionId('b'), { seed: messageEvents('needle peer', 12), meta: { createdAt: 20 } })
  315. const all = await ctx.sessionQuery.searchEvents({ sessionId: SessionId('a'), query: 'needle' })
  316. expect(new Set(all.items.map(item => item.surface))).toEqual(new Set(['current', 'shadowed', 'log-only']))
  317. await expect(ctx.sessionQuery.searchEvents({
  318. sessionId: SessionId('a'),
  319. query: 'needle',
  320. filters: [
  321. { kind: 'seq', from: 2, to: 2 },
  322. { kind: 'time', from: 12, to: 12 },
  323. { kind: 'type', values: ['user/message'] },
  324. { kind: 'surface', values: ['current'] },
  325. ],
  326. })).resolves.toMatchObject({ items: [{ seq: 2, surface: 'current' }] })
  327. const grouped = await ctx.sessionQuery.searchSessions({
  328. query: 'needle',
  329. sessionFilters: [
  330. { kind: 'id', values: [SessionId('a')] },
  331. { kind: 'cwd', values: ['/a'] },
  332. { kind: 'created-at', from: 20, to: 20 },
  333. { kind: 'parent', values: [parent] },
  334. { kind: 'availability', values: ['live'] },
  335. ],
  336. eventFilters: [{ kind: 'surface', values: ['shadowed'] }],
  337. })
  338. expect(grouped.items).toHaveLength(1)
  339. expect(grouped.items[0]).toMatchObject({
  340. header: { id: SessionId('a'), cwd: '/a', parentSession: parent },
  341. live: true,
  342. persisted: false,
  343. bestMatch: { seq: 0, surface: 'shadowed' },
  344. })
  345. })
  346. it('searches at the supported FTS5 outer-predicate boundary in both scopes', async () => {
  347. const ctx = await liveContext()
  348. const session = ctx.sessions.create(SessionId('predicate-boundary'), {
  349. seed: messageEvents('needle'),
  350. meta: { cwd: '/work' },
  351. })
  352. const sessionFilters = Array.from(
  353. { length: 14 },
  354. () => ({ kind: 'cwd' as const, values: ['/work', null] }),
  355. )
  356. const eventFilters = Array.from(
  357. { length: 13 },
  358. () => ({ kind: 'type' as const, values: ['user/message' as const] }),
  359. )
  360. await expect(ctx.sessionQuery.searchSessions({ query: 'needle', sessionFilters }))
  361. .resolves.toMatchObject({ items: [{ header: { id: session.id } }] })
  362. await expect(ctx.sessionQuery.searchEvents({
  363. sessionId: session.id,
  364. query: 'needle',
  365. filters: eventFilters,
  366. })).resolves.toMatchObject({ items: [{ sessionId: session.id, seq: 0 }] })
  367. })
  368. it('rejects unsupported FTS5 outer-predicate counts with typed errors', async () => {
  369. const ctx = await liveContext()
  370. const session = ctx.sessions.create(SessionId('predicate-limit'), { seed: messageEvents('needle') })
  371. const sessionFilters = Array.from(
  372. { length: 1_100 },
  373. () => ({ kind: 'id' as const, values: [session.id] }),
  374. )
  375. const eventFilters = Array.from(
  376. { length: 1_100 },
  377. () => ({ kind: 'type' as const, values: ['user/message' as const] }),
  378. )
  379. await expect(ctx.sessionQuery.searchSessions({ query: 'needle', sessionFilters }))
  380. .rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  381. await expect(ctx.sessionQuery.searchEvents({
  382. sessionId: session.id,
  383. query: 'needle',
  384. filters: eventFilters,
  385. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  386. await expect(ctx.sessionQuery.searchSessions({
  387. query: 'needle',
  388. sessionFilters: sessionFilters.slice(0, 7),
  389. eventFilters: eventFilters.slice(0, 8),
  390. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  391. await expect(ctx.sessionQuery.searchEvents({
  392. sessionId: session.id,
  393. query: 'needle',
  394. filters: eventFilters.slice(0, 14),
  395. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  396. })
  397. it('uses literal phrase tokens, stable ties, and bounded Unicode snippets', async () => {
  398. const ctx = await liveContext({ path: ':memory:', defaultLimit: 10, maxLimit: 10, snippetChars: 5 })
  399. ctx.sessions.create(SessionId('a'), { seed: messageEvents('😀😀 alpha beta BRAID 😀😀', 10), meta: { createdAt: 1 } })
  400. ctx.sessions.create(SessionId('b'), { seed: messageEvents('alpha beta', 10), meta: { createdAt: 1 } })
  401. ctx.sessions.create(SessionId('c'), { seed: messageEvents('alpha middle beta', 10), meta: { createdAt: 1 } })
  402. ctx.sessions.create(SessionId('d'), { seed: messageEvents('alpha beta', 10), meta: { createdAt: 1 } })
  403. ctx.sessions.create(SessionId('operator'), { seed: messageEvents('needle OR absent', 10), meta: { createdAt: 1 } })
  404. ctx.sessions.create(SessionId('only'), { seed: messageEvents('needle only', 10), meta: { createdAt: 1 } })
  405. ctx.sessions.create(SessionId('quote'), { seed: messageEvents('say "needle" exactly', 10), meta: { createdAt: 1 } })
  406. const phrase = await ctx.sessionQuery.searchSessions({ query: 'alpha beta' })
  407. expect(phrase.items.map(item => item.header.id)).toEqual([SessionId('b'), SessionId('d'), SessionId('a')])
  408. expect(phrase.items.every(item => Array.from(item.bestMatch.snippet).length <= 5)).toBe(true)
  409. await expect(ctx.sessionQuery.searchSessions({ query: 'AI' })).resolves.toEqual({ items: [] })
  410. await expect(ctx.sessionQuery.searchSessions({ query: 'needle OR absent' }))
  411. .resolves.toMatchObject({ items: [{ header: { id: SessionId('operator') } }] })
  412. await expect(ctx.sessionQuery.searchSessions({ query: 'say "needle"' }))
  413. .resolves.toMatchObject({ items: [{ header: { id: SessionId('quote') } }] })
  414. await expect(ctx.sessionQuery.searchSessions({ query: '*' })).resolves.toEqual({ items: [] })
  415. })
  416. it('ranks live and persisted matches on one source-comparable contract', async () => {
  417. const persisted = header('z-persisted')
  418. TestPersistence.reset([
  419. { meta: persisted, events: messageEvents('needle needle', 10) },
  420. ...Array.from({ length: 12 }, (_, index) => ({
  421. meta: header(`filler-${index}`),
  422. events: messageEvents('needle', 10),
  423. })),
  424. ])
  425. const ctx = await liveContext()
  426. const persistence = await ctx.plugin(TestPersistence)
  427. ctx.sessions.create(SessionId('a-live'), {
  428. seed: messageEvents('needle needle', 10),
  429. meta: { createdAt: persisted.createdAt },
  430. })
  431. const result = await ctx.sessionQuery.searchSessions({
  432. query: 'needle',
  433. sessionFilters: [{ kind: 'id', values: [SessionId('a-live'), persisted.id] }],
  434. })
  435. expect(result.items.map(item => item.header.id)).toEqual([SessionId('a-live'), persisted.id])
  436. await persistence.dispose()
  437. })
  438. it('positions snippets from FTS5 matches across diacritics and punctuation', async () => {
  439. const ctx = await liveContext({ path: ':memory:', snippetChars: 14 })
  440. const session = ctx.sessions.create(SessionId('snippet'), {
  441. seed: messageEvents('long long long—café,\nnext value', 10),
  442. })
  443. const page = await ctx.sessionQuery.searchEvents({ sessionId: session.id, query: 'CAFE' })
  444. expect(page.items).toHaveLength(1)
  445. expect(page.items[0]!.snippet).toContain('café')
  446. expect(page.items[0]!.snippet).toContain('—')
  447. expect(page.items[0]!.snippet).not.toContain('\n')
  448. expect(Array.from(page.items[0]!.snippet).length).toBeLessThanOrEqual(14)
  449. })
  450. it('binds cursors to requests and only invalidates within-session pages for target changes', async () => {
  451. const ctx = await liveContext({ path: ':memory:', defaultLimit: 1, maxLimit: 5 })
  452. const target = ctx.sessions.create(SessionId('target'), {
  453. seed: [
  454. ...messageEvents('needle one', 10),
  455. { ...messageEvents('needle two', 11)[0]!, seq: 1 },
  456. { ...messageEvents('needle three', 12)[0]!, seq: 2 },
  457. ],
  458. })
  459. ctx.sessions.create(SessionId('other'), { seed: messageEvents('needle other', 10) })
  460. const eventPage = await ctx.sessionQuery.searchEvents({ sessionId: target.id, query: 'needle', limit: 1 })
  461. const sessionPage = await ctx.sessionQuery.searchSessions({ query: 'needle', limit: 1 })
  462. expect(eventPage.nextCursor).toEqual(expect.any(String))
  463. expect(sessionPage.nextCursor).toEqual(expect.any(String))
  464. if (eventPage.nextCursor === undefined || sessionPage.nextCursor === undefined) throw new Error('expected cursors')
  465. const unsafeOffsetCursor = replaceCursorOffset(eventPage.nextCursor, 1e100)
  466. await expect(ctx.sessionQuery.searchEvents({
  467. sessionId: target.id,
  468. query: 'needle',
  469. limit: 1,
  470. cursor: unsafeOffsetCursor,
  471. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_CURSOR'))
  472. const eventKeys = eventPage.items.map(item => `${item.sessionId}:${item.seq}`)
  473. let eventCursor: ReturnType<typeof SessionSearchCursor> | undefined = eventPage.nextCursor
  474. while (eventCursor !== undefined) {
  475. const next = await ctx.sessionQuery.searchEvents({
  476. sessionId: target.id,
  477. query: 'needle',
  478. limit: 1,
  479. cursor: eventCursor,
  480. })
  481. eventKeys.push(...next.items.map(item => `${item.sessionId}:${item.seq}`))
  482. eventCursor = next.nextCursor
  483. }
  484. expect(eventKeys).toHaveLength(3)
  485. expect(new Set(eventKeys).size).toBe(eventKeys.length)
  486. const sessionIds = sessionPage.items.map(item => item.header.id)
  487. let sessionCursor: ReturnType<typeof SessionSearchCursor> | undefined = sessionPage.nextCursor
  488. while (sessionCursor !== undefined) {
  489. const next = await ctx.sessionQuery.searchSessions({ query: 'needle', limit: 1, cursor: sessionCursor })
  490. sessionIds.push(...next.items.map(item => item.header.id))
  491. sessionCursor = next.nextCursor
  492. }
  493. expect(sessionIds).toHaveLength(2)
  494. expect(new Set(sessionIds).size).toBe(sessionIds.length)
  495. ctx.sessions.create(SessionId('unrelated'), { seed: messageEvents('needle unrelated', 20) })
  496. await expect(ctx.sessionQuery.searchEvents({
  497. sessionId: target.id,
  498. query: 'needle',
  499. limit: 1,
  500. cursor: eventPage.nextCursor,
  501. })).resolves.toMatchObject({ items: [{ sessionId: target.id }] })
  502. await expect(ctx.sessionQuery.searchSessions({ query: 'needle', limit: 1, cursor: sessionPage.nextCursor }))
  503. .rejects.toThrow(expectCode('SESSION_QUERY_STALE_CURSOR'))
  504. await expect(ctx.sessionQuery.searchEvents({
  505. sessionId: target.id,
  506. query: 'different',
  507. limit: 1,
  508. cursor: eventPage.nextCursor,
  509. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_CURSOR'))
  510. target.append('user/message', createUserMessage({
  511. content: [{ type: 'text', text: 'needle four' }], source: { kind: 'user' },
  512. }), { surfaceOp: 'append' })
  513. await expect(ctx.sessionQuery.searchEvents({
  514. sessionId: target.id,
  515. query: 'needle',
  516. limit: 1,
  517. cursor: eventPage.nextCursor,
  518. })).rejects.toThrow(expectCode('SESSION_QUERY_STALE_CURSOR'))
  519. })
  520. it('invalidates session cursors after transient persistence topology changes', async () => {
  521. TestPersistence.reset()
  522. const ctx = await liveContext({ path: ':memory:', defaultLimit: 1, maxLimit: 5 })
  523. ctx.sessions.create(SessionId('first'), { seed: messageEvents('needle first') })
  524. ctx.sessions.create(SessionId('second'), { seed: messageEvents('needle second') })
  525. const page = await ctx.sessionQuery.searchSessions({ query: 'needle', limit: 1 })
  526. if (page.nextCursor === undefined) throw new Error('expected cursor')
  527. const persistence = await ctx.plugin(TestPersistence)
  528. await persistence.dispose()
  529. await expect(ctx.sessionQuery.searchSessions({
  530. query: 'needle',
  531. limit: 1,
  532. cursor: page.nextCursor,
  533. })).rejects.toThrow(expectCode('SESSION_QUERY_STALE_CURSOR'))
  534. })
  535. it('rejects invalid requests, filters, cursors, and direct config', async () => {
  536. const ctx = await liveContext({ path: ':memory:', defaultLimit: 2, maxLimit: 3 })
  537. const session = ctx.sessions.create(SessionId('valid'), { seed: messageEvents('needle') })
  538. for (const request of [
  539. { sessionId: session.id, query: '' },
  540. { sessionId: session.id, query: 'needle', limit: 0 },
  541. { sessionId: session.id, query: 'needle', limit: 4 },
  542. { sessionId: session.id, query: 'needle', filters: [{ kind: 'seq', from: 2, to: 1 }] },
  543. { sessionId: session.id, query: 'needle', filters: [{ kind: 'surface', values: ['future'] }] },
  544. { sessionId: session.id, query: 'bad\0query' },
  545. ] as const) {
  546. await expect(ctx.sessionQuery.searchEvents(request as never)).rejects.toBeInstanceOf(Error)
  547. }
  548. await expect(ctx.sessionQuery.searchSessions({
  549. query: 'needle',
  550. sessionFilters: [{ kind: 'availability', values: ['remote' as never] }],
  551. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  552. await expect(ctx.sessionQuery.searchSessions({
  553. query: 'needle',
  554. sessionFilters: [{ kind: 'future' } as never],
  555. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  556. await expect(ctx.sessionQuery.searchSessions({
  557. query: 'needle',
  558. eventFilters: [{ kind: 'future' } as never],
  559. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  560. await expect(ctx.sessionQuery.searchEvents({
  561. sessionId: session.id,
  562. query: 'needle',
  563. filters: [{ kind: 'future' } as never],
  564. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  565. await expect(ctx.sessionQuery.searchEvents({
  566. sessionId: session.id,
  567. query: 'needle',
  568. cursor: SessionSearchCursor('not-json'),
  569. }))
  570. .rejects.toThrow(expectCode('SESSION_QUERY_INVALID_CURSOR'))
  571. await expect(ctx.sessionQuery.searchEvents({ sessionId: SessionId('absent'), query: 'needle' }))
  572. .rejects.toThrow(expectCode('SESSION_QUERY_SESSION_NOT_FOUND'))
  573. for (const config of [
  574. { path: '' },
  575. { path: ':memory:', defaultLimit: 0 },
  576. { path: ':memory:', maxLimit: 0 },
  577. { path: ':memory:', defaultLimit: 1e100 },
  578. { path: ':memory:', maxLimit: 1e100 },
  579. { path: ':memory:', snippetChars: 0 },
  580. { path: ':memory:', readWindowMax: -1 },
  581. { path: ':memory:', persistedInspectConcurrency: 0 },
  582. { path: ':memory:', persistedInspectConcurrency: Number.MAX_SAFE_INTEGER + 1 },
  583. { path: ':memory:', defaultLimit: 3, maxLimit: 2 },
  584. { path: ':memory:', openAt: 'later' },
  585. { path: ':memory:', journalMode: 'memory' },
  586. ]) {
  587. const direct = new Context()
  588. await direct.plugin(SessionStore)
  589. expect(() => new SessionQuerySqlite(direct, config as never))
  590. .toThrow(expectCode('SESSION_QUERY_INVALID_CONFIG'))
  591. expect(direct.sessionQuery).toBeUndefined()
  592. }
  593. })
  594. it('rejects aggregate filter bindings above SQLite\'s portable variable limit', async () => {
  595. const ctx = await liveContext()
  596. const session = ctx.sessions.create(SessionId('binding-limit'), { seed: messageEvents('needle') })
  597. // Each clause is below the ceiling; combined with its sibling and fixed
  598. // query bindings, the complete statement is not portable.
  599. const halfPortableLimit = 16_383
  600. const ids = Array.from(
  601. { length: halfPortableLimit },
  602. (_, index) => SessionId(`binding-${index}`),
  603. )
  604. const types = Array.from({ length: halfPortableLimit }, () => 'user/message' as const)
  605. const surfaces = Array.from({ length: halfPortableLimit }, () => 'current' as const)
  606. await expect(ctx.sessionQuery.searchSessions({
  607. query: 'needle',
  608. sessionFilters: [{ kind: 'id', values: ids }],
  609. eventFilters: [{ kind: 'type', values: types }],
  610. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  611. await expect(ctx.sessionQuery.searchEvents({
  612. sessionId: session.id,
  613. query: 'needle',
  614. filters: [
  615. { kind: 'type', values: types },
  616. { kind: 'surface', values: surfaces },
  617. ],
  618. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  619. })
  620. it('rejects one 125,000-value filter list with a typed error', async () => {
  621. const ctx = await liveContext()
  622. const ids = Array.from(
  623. { length: 125_000 },
  624. (_, index) => SessionId(`oversized-binding-${index}`),
  625. )
  626. await expect(ctx.sessionQuery.searchSessions({
  627. query: 'needle',
  628. sessionFilters: [{ kind: 'id', values: ids }],
  629. })).rejects.toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  630. })
  631. })
  632. describe('SQLite reconciliation and source lifecycle', () => {
  633. it('owns queued request and filter values before waiting for the serializer', async () => {
  634. const durable = header('owned')
  635. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  636. const ctx = await liveContext()
  637. const persistence = await ctx.plugin(TestPersistence)
  638. let release!: () => void
  639. TestPersistence.listGate = new Promise<void>((resolve) => { release = resolve })
  640. let markStarted!: () => void
  641. const started = new Promise<void>((resolve) => { markStarted = resolve })
  642. TestPersistence.listStarted = () => {
  643. TestPersistence.listStarted = undefined
  644. markStarted()
  645. }
  646. const blocking = ctx.sessionQuery.searchSessions({ query: 'needle' })
  647. await started
  648. const availability: SessionAvailability[] = ['persisted']
  649. const request: SessionSearchRequest = {
  650. query: 'needle',
  651. sessionFilters: [{ kind: 'availability', values: availability }],
  652. }
  653. const queued = ctx.sessionQuery.searchSessions(request)
  654. request.query = 'absent'
  655. availability[0] = 'live'
  656. release()
  657. await expect(blocking).resolves.toMatchObject({ items: [{ header: durable }] })
  658. await expect(queued).resolves.toMatchObject({ items: [{ header: durable }] })
  659. await persistence.dispose()
  660. })
  661. it('mounts persistence dynamically, shadows with TEMP live rows, reveals, and hides on unmount', async () => {
  662. const shared = header('shared', 10, { cwd: '/work' })
  663. const durable = header('durable', 5)
  664. TestPersistence.reset([
  665. { meta: shared, events: messageEvents('persisted needle') },
  666. { meta: durable, events: messageEvents('durable needle') },
  667. ])
  668. const ctx = await liveContext()
  669. await expect(ctx.sessionQuery.searchSessions({ query: 'durable' })).resolves.toEqual({ items: [] })
  670. const persistenceFiber = await ctx.plugin(TestPersistence)
  671. await expect(ctx.sessionQuery.searchSessions({ query: 'durable' }))
  672. .resolves.toMatchObject({ items: [{ header: durable, live: false, persisted: true }] })
  673. const live = ctx.sessions.prepare(shared.id, { meta: { createdAt: 10, cwd: '/work' } })
  674. live.append('user/message', createUserMessage({
  675. content: [{ type: 'text', text: 'live needle' }], source: { kind: 'user' },
  676. }), { surfaceOp: 'append' })
  677. const detach = ctx.sessions.enter(live)
  678. ctx.sessions.announce(live)
  679. await expect(ctx.sessionQuery.searchSessions({ query: 'persisted' })).resolves.toEqual({ items: [] })
  680. await expect(ctx.sessionQuery.searchSessions({ query: 'live' }))
  681. .resolves.toMatchObject({ items: [{ header: shared, live: true, persisted: true }] })
  682. detach()
  683. await expect(ctx.sessionQuery.searchSessions({ query: 'persisted' }))
  684. .resolves.toMatchObject({ items: [{ header: shared, live: false, persisted: true }] })
  685. await persistenceFiber.dispose()
  686. await expect(ctx.sessionQuery.searchSessions({ query: 'durable' })).resolves.toEqual({ items: [] })
  687. await expect(ctx.sessionQuery.searchEvents({ sessionId: durable.id, query: 'needle' }))
  688. .rejects.toThrow(expectCode('SESSION_QUERY_SESSION_NOT_FOUND'))
  689. })
  690. it('does not load a persisted log while the same session is live', async () => {
  691. const shared = header('checkpointed-live', 10)
  692. TestPersistence.reset([{ meta: shared, events: messageEvents('persisted needle') }])
  693. const ctx = await liveContext()
  694. const live = ctx.sessions.prepare(shared.id, {
  695. seed: messageEvents('live needle'),
  696. meta: { createdAt: shared.createdAt },
  697. })
  698. const detach = ctx.sessions.enter(live)
  699. ctx.sessions.announce(live)
  700. const persistence = await ctx.plugin(TestPersistence)
  701. await expect(ctx.sessionQuery.searchSessions({
  702. query: 'live',
  703. sessionFilters: [{ kind: 'availability', values: ['persisted'] }],
  704. })).resolves.toMatchObject({
  705. items: [{ header: shared, live: true, persisted: true }],
  706. })
  707. expect(TestPersistence.loads.get(shared.id)).toBeUndefined()
  708. expect(TestPersistence.inspections.get(shared.id)).toBeUndefined()
  709. detach()
  710. await expect(ctx.sessionQuery.searchSessions({ query: 'persisted' }))
  711. .resolves.toMatchObject({ items: [{ header: shared, live: false, persisted: true }] })
  712. expect(TestPersistence.loads.get(shared.id)).toBeUndefined()
  713. expect(TestPersistence.inspections.get(shared.id)).toBe(1)
  714. await persistence.dispose()
  715. })
  716. it('retries when a live owner attaches during persistence observation', async () => {
  717. TestPersistence.reset()
  718. const ctx = await liveContext()
  719. await ctx.plugin(TestPersistence)
  720. TestPersistence.snapshotEffect = () => {
  721. TestPersistence.snapshotEffect = undefined
  722. ctx.sessions.create(SessionId('attached'), { seed: messageEvents('attached needle') })
  723. }
  724. await expect(ctx.sessionQuery.searchSessions({ query: 'attached' }))
  725. .resolves.toMatchObject({ items: [{ header: { id: SessionId('attached') } }] })
  726. })
  727. it('cannot crash-repair a log when live ownership begins during persisted inspection', async () => {
  728. const shared = header('attach-during-inspect', 10)
  729. const persistedEvents = messageEvents('persisted needle')
  730. TestPersistence.reset([{ meta: shared, events: persistedEvents }])
  731. const ctx = await liveContext()
  732. await ctx.plugin(TestPersistence)
  733. TestPersistence.loadEffect = (entry) => {
  734. entry.events = messageEvents('incorrect repair')
  735. }
  736. TestPersistence.inspectEffect = () => {
  737. ctx.sessions.create(shared.id, {
  738. seed: messageEvents('live needle'),
  739. meta: { createdAt: shared.createdAt },
  740. })
  741. }
  742. await expect(ctx.sessionQuery.searchSessions({ query: 'live' }))
  743. .resolves.toMatchObject({ items: [{ header: shared, live: true, persisted: true }] })
  744. expect(TestPersistence.loads.get(shared.id)).toBeUndefined()
  745. expect(TestPersistence.entries.get(shared.id)?.events).toEqual(persistedEvents)
  746. })
  747. it('retries when one live owner replaces another during persistence observation', async () => {
  748. TestPersistence.reset()
  749. const ctx = await liveContext()
  750. const first = ctx.sessions.prepare(SessionId('first'), { seed: messageEvents('first needle') })
  751. const detachFirst = ctx.sessions.enter(first)
  752. ctx.sessions.announce(first)
  753. await ctx.plugin(TestPersistence)
  754. TestPersistence.snapshotEffect = () => {
  755. TestPersistence.snapshotEffect = undefined
  756. detachFirst()
  757. ctx.sessions.create(SessionId('second'), { seed: messageEvents('second needle') })
  758. }
  759. await expect(ctx.sessionQuery.searchSessions({ query: 'second' }))
  760. .resolves.toMatchObject({ items: [{ header: { id: SessionId('second') } }] })
  761. })
  762. it('uses the reconciled persistence binding through the query boundary', async () => {
  763. const durable = header('post-reconcile-unmount')
  764. TestPersistence.reset([{ meta: durable, events: [
  765. ...messageEvents('durable needle', 1),
  766. { ...messageEvents('durable needle again', 2)[0]!, seq: 1 },
  767. ] }])
  768. const ctx = await liveContext({ path: ':memory:', defaultLimit: 1, maxLimit: 2 })
  769. const persistence = await ctx.plugin(TestPersistence)
  770. const internals = ctx.sessionQuery as unknown as {
  771. _reconcile(signal: AbortSignal | undefined): Promise<{
  772. identity: symbol
  773. service?: SessionPersistence
  774. }>
  775. }
  776. const reconcile = internals._reconcile.bind(internals)
  777. const boundary = vi.spyOn(internals, '_reconcile').mockImplementation(async (signal) => {
  778. const binding = await reconcile(signal)
  779. await persistence.dispose()
  780. return binding
  781. })
  782. const page = await ctx.sessionQuery.searchEvents({
  783. sessionId: durable.id,
  784. query: 'needle',
  785. limit: 1,
  786. })
  787. expect(page.items).toMatchObject([{ sessionId: durable.id }])
  788. expect(page.nextCursor).toEqual(expect.any(String))
  789. boundary.mockRestore()
  790. await expect(ctx.sessionQuery.searchEvents({ sessionId: durable.id, query: 'needle' }))
  791. .rejects.toThrow(expectCode('SESSION_QUERY_SESSION_NOT_FOUND'))
  792. })
  793. it('discards a stale list rejection when persistence unmounts during observation', async () => {
  794. const durable = header('racing')
  795. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  796. const ctx = await liveContext()
  797. const persistenceFiber = await ctx.plugin(TestPersistence)
  798. let release!: () => void
  799. TestPersistence.listGate = new Promise<void>((resolve) => { release = resolve })
  800. let markStarted!: () => void
  801. const started = new Promise<void>((resolve) => { markStarted = resolve })
  802. TestPersistence.listStarted = () => {
  803. TestPersistence.listStarted = undefined
  804. markStarted()
  805. }
  806. const search = ctx.sessionQuery.searchSessions({ query: 'needle' })
  807. await started
  808. await persistenceFiber.dispose()
  809. TestPersistence.failure = new Error('stale backend rejection')
  810. release()
  811. await expect(search).resolves.toEqual({ items: [] })
  812. })
  813. it('retries against a replacement after the prior binding rejects', async () => {
  814. const durable = header('replacement')
  815. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  816. const ctx = await liveContext()
  817. const prior = await ctx.plugin(TestPersistence)
  818. let rejectPrior!: (reason: unknown) => void
  819. TestPersistence.listGate = new Promise<void>((_resolve, reject) => { rejectPrior = reject })
  820. let markStarted!: () => void
  821. const started = new Promise<void>((resolve) => { markStarted = resolve })
  822. TestPersistence.listStarted = () => {
  823. TestPersistence.listStarted = undefined
  824. markStarted()
  825. }
  826. const search = ctx.sessionQuery.searchSessions({ query: 'needle' })
  827. await started
  828. await prior.dispose()
  829. TestPersistence.listGate = undefined
  830. const replacement = await ctx.plugin(TestPersistence)
  831. rejectPrior(new Error('stale prior binding'))
  832. await expect(search).resolves.toMatchObject({ items: [{ header: durable }] })
  833. await replacement.dispose()
  834. })
  835. it('reloads a replacement source even when its opaque revisions collide', async () => {
  836. const durable = header('colliding-replacement')
  837. TestPersistence.reset([{ meta: durable, events: messageEvents('old content') }])
  838. const revision = TestPersistence.revisions.get(durable.id)!
  839. const ctx = await liveContext()
  840. const prior = await ctx.plugin(TestPersistence)
  841. await expect(ctx.sessionQuery.searchSessions({ query: 'old' }))
  842. .resolves.toMatchObject({ items: [{ header: durable }] })
  843. await prior.dispose()
  844. TestPersistence.set({ meta: durable, events: messageEvents('new needle') })
  845. TestPersistence.revisions.set(durable.id, revision)
  846. const replacement = await ctx.plugin(TestPersistence)
  847. const page = await ctx.sessionQuery.searchSessions({ query: 'new needle' })
  848. expect(TestPersistence.inspections.get(durable.id)).toBe(2)
  849. expect(page).toMatchObject({ items: [{ header: durable }] })
  850. await expect(ctx.sessionQuery.searchSessions({ query: 'old' })).resolves.toEqual({ items: [] })
  851. expect(TestPersistence.inspections.get(durable.id)).toBe(2)
  852. await replacement.dispose()
  853. })
  854. it('retries when a successful observation belongs to a source unmounted during listing', async () => {
  855. const durable = header('successful-unmount')
  856. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  857. const ctx = await liveContext()
  858. const persistence = await ctx.plugin(TestPersistence)
  859. let lists = 0
  860. TestPersistence.snapshotEffect = async () => {
  861. lists += 1
  862. if (lists === 2) await persistence.dispose()
  863. }
  864. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' })).resolves.toEqual({ items: [] })
  865. expect(lists).toBe(2)
  866. })
  867. it('retries when the snapshot population changes during observation', async () => {
  868. const first = header('first')
  869. const added = header('added-during-list')
  870. TestPersistence.reset([{ meta: first, events: messageEvents('first needle') }])
  871. const ctx = await liveContext()
  872. await ctx.plugin(TestPersistence)
  873. TestPersistence.snapshotEffect = () => {
  874. TestPersistence.snapshotEffect = undefined
  875. TestPersistence.set({ meta: added, events: messageEvents('added needle') })
  876. }
  877. const page = await ctx.sessionQuery.searchSessions({ query: 'needle' })
  878. expect(page.items.map(item => item.header.id).sort()).toEqual([added.id, first.id].sort())
  879. expect(TestPersistence.inspections.get(first.id)).toBe(2)
  880. expect(TestPersistence.inspections.get(added.id)).toBe(1)
  881. })
  882. it('fails after one retry when persistence snapshots keep changing', async () => {
  883. const durable = header('continuous-mutation')
  884. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  885. const ctx = await liveContext()
  886. await ctx.plugin(TestPersistence)
  887. let lists = 0
  888. TestPersistence.snapshotEffect = () => {
  889. lists += 1
  890. TestPersistence.set({ meta: durable, events: messageEvents(`durable needle ${lists}`) })
  891. }
  892. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  893. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  894. expect(lists).toBe(4)
  895. })
  896. it('retries if the persistence binding changes while live sessions are observed', async () => {
  897. const durable = header('live-boundary-retry')
  898. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  899. const ctx = await liveContext()
  900. await ctx.plugin(TestPersistence)
  901. const internals = ctx.sessionQuery as unknown as {
  902. _persistenceBinding: { identity: symbol; service?: SessionPersistence }
  903. }
  904. const originalList = ctx.sessions.list.bind(ctx.sessions)
  905. let bumped = false
  906. const list = vi.spyOn(ctx.sessions, 'list').mockImplementation(() => {
  907. if (!bumped) {
  908. bumped = true
  909. internals._persistenceBinding = {
  910. ...internals._persistenceBinding,
  911. identity: Symbol(),
  912. }
  913. }
  914. return originalList()
  915. })
  916. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  917. .resolves.toMatchObject({ items: [{ header: durable }] })
  918. expect(TestPersistence.inspections.get(durable.id)).toBe(2)
  919. list.mockRestore()
  920. })
  921. it('rejects malformed snapshots and preserves typed persistence failures', async () => {
  922. const durable = header('invalid-snapshot')
  923. TestPersistence.reset([{ meta: durable, events: messageEvents('durable needle') }])
  924. const ctx = await liveContext()
  925. await ctx.plugin(TestPersistence)
  926. TestPersistence.snapshotOverride = () => 'not-an-array' as never
  927. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  928. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  929. TestPersistence.snapshotOverride = () => [{ header: durable, revision: 1 as never }]
  930. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  931. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  932. TestPersistence.snapshotOverride = () => [
  933. { header: durable, revision: SessionPersistenceRevision('duplicate:1') },
  934. { header: durable, revision: SessionPersistenceRevision('duplicate:2') },
  935. ]
  936. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  937. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  938. TestPersistence.snapshotOverride = undefined
  939. const typed = new SessionQueryError('typed persistence failure', 'SESSION_QUERY_PERSISTENCE_FAILED')
  940. TestPersistence.failure = typed
  941. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' })).rejects.toBe(typed)
  942. })
  943. it('rejects immutable header conflicts between live and persisted sources', async () => {
  944. const shared = header('conflict', 10, { delegationDepth: 1 })
  945. TestPersistence.reset([{ meta: shared, events: messageEvents('persisted needle') }])
  946. const ctx = await liveContext()
  947. await ctx.plugin(TestPersistence)
  948. ctx.sessions.create(shared.id, {
  949. seed: messageEvents('live needle'),
  950. meta: { createdAt: 10, delegationDepth: 2 },
  951. })
  952. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  953. .rejects.toThrow(expectCode('SESSION_QUERY_SOURCE_CONFLICT'))
  954. })
  955. it('preserves unchanged persisted generations while reconciling new, changed, and deleted rows', { timeout: 20_000 }, async () => {
  956. const path = await temporaryPath()
  957. const unchanged = header('unchanged')
  958. const changed = header('changed')
  959. const deleted = header('deleted')
  960. TestPersistence.reset([
  961. { meta: unchanged, events: messageEvents('unchanged needle') },
  962. { meta: changed, events: messageEvents('old needle') },
  963. { meta: deleted, events: messageEvents('deleted needle') },
  964. ])
  965. const first = new Context()
  966. await first.plugin(SessionStore)
  967. const firstPersistence = await first.plugin(TestPersistence)
  968. const firstSearch = await first.plugin(SessionQuerySqlite, { path })
  969. await first.sessionQuery.searchSessions({ query: 'needle' })
  970. expect(Object.fromEntries(TestPersistence.inspections)).toEqual({ unchanged: 1, changed: 1, deleted: 1 })
  971. await first.sessionQuery.searchSessions({ query: 'needle' })
  972. expect(Object.fromEntries(TestPersistence.inspections)).toEqual({ unchanged: 1, changed: 1, deleted: 1 })
  973. await firstSearch.dispose()
  974. await firstPersistence.dispose()
  975. const beforeDb = new DatabaseSync(path)
  976. const beforeRows = beforeDb.prepare('SELECT id, generation FROM persisted_sessions ORDER BY id').all() as Array<{ id: string; generation: number }>
  977. beforeDb.close()
  978. const before = new Map(beforeRows.map(row => [row.id, row.generation]))
  979. const added = header('added')
  980. TestPersistence.entries.delete(deleted.id)
  981. TestPersistence.set({ meta: changed, events: messageEvents('changed needle') })
  982. TestPersistence.set({ meta: added, events: messageEvents('added needle') })
  983. const second = new Context()
  984. await second.plugin(SessionStore)
  985. const secondPersistence = await second.plugin(TestPersistence)
  986. const secondSearch = await second.plugin(SessionQuerySqlite, { path })
  987. const result = await second.sessionQuery.searchSessions({ query: 'needle' })
  988. expect(result.items.map(item => item.header.id).sort()).toEqual([added.id, changed.id, unchanged.id].sort())
  989. expect(Object.fromEntries(TestPersistence.inspections)).toEqual({
  990. unchanged: 1,
  991. changed: 2,
  992. deleted: 1,
  993. added: 1,
  994. })
  995. await secondSearch.dispose()
  996. await secondPersistence.dispose()
  997. const afterDb = new DatabaseSync(path)
  998. const afterRows = afterDb.prepare('SELECT id, generation FROM persisted_sessions ORDER BY id').all() as Array<{ id: string; generation: number }>
  999. afterDb.close()
  1000. const after = new Map(afterRows.map(row => [row.id, row.generation]))
  1001. expect(after.get(unchanged.id)).toBe(before.get(unchanged.id))
  1002. expect(after.get(changed.id)).toBeGreaterThan(before.get(changed.id)!)
  1003. expect(after.has(deleted.id)).toBe(false)
  1004. expect(after.has(added.id)).toBe(true)
  1005. })
  1006. it('drops connection-local live overlays on reopen and retains persistent bases', async () => {
  1007. const path = await temporaryPath()
  1008. const shared = header('shared', 10)
  1009. TestPersistence.reset([{ meta: shared, events: messageEvents('persisted needle') }])
  1010. const first = new Context()
  1011. await first.plugin(SessionStore)
  1012. const persistence = await first.plugin(TestPersistence)
  1013. const live = first.sessions.create(shared.id, { seed: messageEvents('live needle'), meta: { createdAt: 10 } })
  1014. const search = await first.plugin(SessionQuerySqlite, { path })
  1015. await expect(first.sessionQuery.searchEvents({ sessionId: live.id, query: 'live' })).resolves.toMatchObject({ items: [{}] })
  1016. await search.dispose()
  1017. await persistence.dispose()
  1018. const second = new Context()
  1019. await second.plugin(SessionStore)
  1020. const persistenceAgain = await second.plugin(TestPersistence)
  1021. const searchAgain = await second.plugin(SessionQuerySqlite, { path })
  1022. await expect(second.sessionQuery.searchSessions({ query: 'live' })).resolves.toEqual({ items: [] })
  1023. await expect(second.sessionQuery.searchSessions({ query: 'persisted' }))
  1024. .resolves.toMatchObject({ items: [{ header: shared, live: false, persisted: true }] })
  1025. expect(TestPersistence.inspections.get(shared.id)).toBe(1)
  1026. await searchAgain.dispose()
  1027. await persistenceAgain.dispose()
  1028. })
  1029. it('refreshes after an external mutating load repair without loading from the query path', async () => {
  1030. const durable = header('repair')
  1031. TestPersistence.reset([{ meta: durable, events: messageEvents('before repair') }])
  1032. const ctx = await liveContext()
  1033. const persistence = await ctx.plugin(TestPersistence)
  1034. await expect(ctx.sessionQuery.searchSessions({ query: 'before' }))
  1035. .resolves.toMatchObject({ items: [{ header: durable }] })
  1036. TestPersistence.loadEffect = (entry) => {
  1037. entry.events = messageEvents('repaired needle')
  1038. }
  1039. await ctx.sessionPersistence.load(durable.id)
  1040. await expect(ctx.sessionQuery.searchSessions({ query: 'repaired' }))
  1041. .resolves.toMatchObject({ items: [{ header: durable }] })
  1042. expect(TestPersistence.inspections.get(durable.id)).toBe(2)
  1043. await ctx.sessionQuery.searchSessions({ query: 'repaired' })
  1044. expect(TestPersistence.inspections.get(durable.id)).toBe(2)
  1045. expect(TestPersistence.loads.get(durable.id)).toBe(1)
  1046. await persistence.dispose()
  1047. })
  1048. it('recovers on the next search after source and SQLite transaction failures', async () => {
  1049. TestPersistence.reset([{ meta: header('durable'), events: messageEvents('durable needle') }])
  1050. const ctx = await liveContext()
  1051. await ctx.plugin(TestPersistence)
  1052. TestPersistence.failure = 'offline'
  1053. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  1054. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  1055. const signal = new AbortController().signal
  1056. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal }))
  1057. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  1058. TestPersistence.failure = new Error('still offline')
  1059. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal }))
  1060. .rejects.toThrow(expectCode('SESSION_QUERY_PERSISTENCE_FAILED'))
  1061. TestPersistence.failure = undefined
  1062. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' })).resolves.toMatchObject({ items: [{}] })
  1063. const live = ctx.sessions.create(SessionId('live'), { seed: messageEvents('base') })
  1064. await ctx.sessionQuery.searchEvents({ sessionId: live.id, query: 'base' })
  1065. const db = (ctx.sessionQuery as unknown as { _db: DatabaseSync })._db
  1066. db.exec('PRAGMA query_only = ON')
  1067. live.append('user/message', createUserMessage({
  1068. content: [{ type: 'text', text: 'retry needle' }], source: { kind: 'user' },
  1069. }), { surfaceOp: 'append' })
  1070. await expect(ctx.sessionQuery.searchEvents({ sessionId: live.id, query: 'needle' }))
  1071. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1072. db.exec('PRAGMA query_only = OFF')
  1073. // seq 2: one-event seed, end-seed, then the live message.
  1074. await expect(ctx.sessionQuery.searchEvents({ sessionId: live.id, query: 'needle' }))
  1075. .resolves.toMatchObject({ items: [{ seq: 2 }] })
  1076. })
  1077. })
  1078. describe('SQLite schema, cancellation, and real persistence integration', () => {
  1079. it('creates a new database and WAL sidecars owner-only without changing its parent mode', async () => {
  1080. if (process.platform === 'win32') return
  1081. const path = await temporaryPath()
  1082. const directory = dirname(path)
  1083. await chmod(directory, 0o755)
  1084. const ctx = await liveContext({ path })
  1085. await ctx.sessionQuery.searchSessions({ query: 'needle' })
  1086. expect((await stat(directory)).mode & 0o777).toBe(0o755)
  1087. expect((await stat(path)).mode & 0o777).toBe(0o600)
  1088. expect((await stat(`${path}-wal`)).mode & 0o777).toBe(0o600)
  1089. expect((await stat(`${path}-shm`)).mode & 0o777).toBe(0o600)
  1090. await (ctx.sessionQuery as SessionQuerySqlite).close()
  1091. })
  1092. it('creates a persistent rollback journal owner-only', async () => {
  1093. if (process.platform === 'win32') return
  1094. const path = await temporaryPath()
  1095. const ctx = await liveContext({ path, journalMode: 'persist' })
  1096. await ctx.sessionQuery.searchSessions({ query: 'needle' })
  1097. expect((await stat(path)).mode & 0o777).toBe(0o600)
  1098. expect((await stat(`${path}-journal`)).mode & 0o777).toBe(0o600)
  1099. await (ctx.sessionQuery as SessionQuerySqlite).close()
  1100. })
  1101. it('preserves the mode of an existing database file', async () => {
  1102. if (process.platform === 'win32') return
  1103. const path = await temporaryPath()
  1104. await writeFile(path, '', { mode: 0o644 })
  1105. await chmod(path, 0o644)
  1106. const ctx = await liveContext({ path, journalMode: 'delete' })
  1107. await ctx.sessionQuery.searchSessions({ query: 'needle' })
  1108. expect((await stat(path)).mode & 0o777).toBe(0o644)
  1109. await (ctx.sessionQuery as SessionQuerySqlite).close()
  1110. })
  1111. it('surfaces filesystem failures while pre-creating the database', async () => {
  1112. const path = `${await temporaryPath()}\0`
  1113. const ctx = new Context()
  1114. await ctx.plugin(SessionStore)
  1115. await expect(ctx.plugin(SessionQuerySqlite, { path })).rejects.toMatchObject({
  1116. code: 'SESSION_QUERY_INDEX_FAILED',
  1117. cause: { code: 'ERR_INVALID_ARG_VALUE' },
  1118. })
  1119. expect(ctx.sessionQuery).toBeUndefined()
  1120. })
  1121. it('resets a recognized incompatible schema but refuses unknown or foreign tables', { timeout: 20_000 }, async () => {
  1122. const stalePath = await temporaryPath('stale.db')
  1123. const staleOwner = await liveContext({ path: stalePath })
  1124. await (staleOwner.sessionQuery as SessionQuerySqlite).close()
  1125. const stale = new DatabaseSync(stalePath)
  1126. stale.exec(`PRAGMA user_version = ${SESSION_QUERY_SQLITE_SCHEMA_VERSION - 1}`)
  1127. stale.close()
  1128. const staleCtx = await liveContext({ path: stalePath })
  1129. staleCtx.sessions.create(SessionId('live'), { seed: messageEvents('needle') })
  1130. await staleCtx.sessionQuery.searchSessions({ query: 'needle' })
  1131. await (staleCtx.sessionQuery as SessionQuerySqlite).close()
  1132. const rebuilt = new DatabaseSync(stalePath)
  1133. expect((rebuilt.prepare('PRAGMA user_version').get() as { user_version: number }).user_version)
  1134. .toBe(SESSION_QUERY_SQLITE_SCHEMA_VERSION)
  1135. rebuilt.close()
  1136. const augmentedPath = await temporaryPath('augmented.db')
  1137. const augmentedOwner = await liveContext({ path: augmentedPath })
  1138. await (augmentedOwner.sessionQuery as SessionQuerySqlite).close()
  1139. const augmented = new DatabaseSync(augmentedPath)
  1140. augmented.exec('CREATE TABLE unrelated(value TEXT)')
  1141. augmented.exec("INSERT INTO unrelated VALUES ('safe')")
  1142. augmented.exec('PRAGMA user_version = 999')
  1143. augmented.close()
  1144. const augmentedCtx = new Context()
  1145. await augmentedCtx.plugin(SessionStore)
  1146. await expect(augmentedCtx.plugin(SessionQuerySqlite, { path: augmentedPath }))
  1147. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1148. expect(augmentedCtx.sessionQuery).toBeUndefined()
  1149. const stillAugmented = new DatabaseSync(augmentedPath)
  1150. expect(stillAugmented.prepare('SELECT value FROM unrelated').get()).toEqual({ value: 'safe' })
  1151. expect(stillAugmented.prepare('PRAGMA user_version').get()).toEqual({ user_version: 999 })
  1152. stillAugmented.close()
  1153. const currentAugmentedPath = await temporaryPath('current-augmented.db')
  1154. const currentAugmentedOwner = await liveContext({ path: currentAugmentedPath })
  1155. await (currentAugmentedOwner.sessionQuery as SessionQuerySqlite).close()
  1156. const currentAugmented = new DatabaseSync(currentAugmentedPath)
  1157. currentAugmented.exec('CREATE TABLE unrelated(value TEXT)')
  1158. currentAugmented.exec("INSERT INTO unrelated VALUES ('safe')")
  1159. currentAugmented.close()
  1160. const currentAugmentedCtx = new Context()
  1161. await currentAugmentedCtx.plugin(SessionStore)
  1162. await expect(currentAugmentedCtx.plugin(SessionQuerySqlite, {
  1163. path: currentAugmentedPath,
  1164. journalMode: 'delete',
  1165. })).rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1166. expect(currentAugmentedCtx.sessionQuery).toBeUndefined()
  1167. const stillCurrentAugmented = new DatabaseSync(currentAugmentedPath)
  1168. expect(stillCurrentAugmented.prepare('SELECT value FROM unrelated').get()).toEqual({ value: 'safe' })
  1169. expect(stillCurrentAugmented.prepare('PRAGMA user_version').get())
  1170. .toEqual({ user_version: SESSION_QUERY_SQLITE_SCHEMA_VERSION })
  1171. expect(stillCurrentAugmented.prepare('PRAGMA journal_mode').get()).toEqual({ journal_mode: 'wal' })
  1172. stillCurrentAugmented.close()
  1173. const foreignPath = await temporaryPath('foreign.db')
  1174. const foreign = new DatabaseSync(foreignPath)
  1175. foreign.exec('PRAGMA journal_mode = WAL')
  1176. foreign.exec('CREATE TABLE canonical(value TEXT)')
  1177. foreign.exec("INSERT INTO canonical VALUES ('safe')")
  1178. foreign.close()
  1179. const foreignCtx = new Context()
  1180. await foreignCtx.plugin(SessionStore)
  1181. await expect(foreignCtx.plugin(SessionQuerySqlite, { path: foreignPath, journalMode: 'delete' }))
  1182. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1183. expect(foreignCtx.sessionQuery).toBeUndefined()
  1184. const stillForeign = new DatabaseSync(foreignPath)
  1185. expect(stillForeign.prepare('SELECT value FROM canonical').get()).toEqual({ value: 'safe' })
  1186. expect(stillForeign.prepare('PRAGMA journal_mode').get()).toEqual({ journal_mode: 'wal' })
  1187. stillForeign.close()
  1188. const wildcardPath = await temporaryPath('sqlite-wildcard.db')
  1189. const wildcard = new DatabaseSync(wildcardPath)
  1190. wildcard.exec('PRAGMA journal_mode = WAL')
  1191. wildcard.exec('CREATE TABLE sqliteX(value TEXT)')
  1192. wildcard.exec("INSERT INTO sqliteX VALUES ('safe')")
  1193. wildcard.close()
  1194. const wildcardCtx = new Context()
  1195. await wildcardCtx.plugin(SessionStore)
  1196. await expect(wildcardCtx.plugin(SessionQuerySqlite, {
  1197. path: wildcardPath,
  1198. journalMode: 'delete',
  1199. })).rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1200. expect(wildcardCtx.sessionQuery).toBeUndefined()
  1201. const stillWildcard = new DatabaseSync(wildcardPath)
  1202. expect(stillWildcard.prepare('SELECT value FROM sqliteX').get()).toEqual({ value: 'safe' })
  1203. expect(stillWildcard.prepare('PRAGMA application_id').get()).toEqual({ application_id: 0 })
  1204. expect(stillWildcard.prepare('PRAGMA user_version').get()).toEqual({ user_version: 0 })
  1205. expect(stillWildcard.prepare('PRAGMA journal_mode').get()).toEqual({ journal_mode: 'wal' })
  1206. stillWildcard.close()
  1207. const otherAppPath = await temporaryPath('other-app.db')
  1208. const otherApp = new DatabaseSync(otherAppPath)
  1209. otherApp.exec('PRAGMA application_id = 123')
  1210. otherApp.close()
  1211. const otherAppCtx = new Context()
  1212. await otherAppCtx.plugin(SessionStore)
  1213. await expect(otherAppCtx.plugin(SessionQuerySqlite, { path: otherAppPath }))
  1214. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1215. expect(otherAppCtx.sessionQuery).toBeUndefined()
  1216. })
  1217. it('fails plugin initialization without an unhandled rejection or partial service', async () => {
  1218. const path = await temporaryPath('never-queried.db')
  1219. const foreign = new DatabaseSync(path)
  1220. foreign.exec('CREATE TABLE canonical(value TEXT)')
  1221. foreign.close()
  1222. const unhandled: unknown[] = []
  1223. const onUnhandled = (reason: unknown) => { unhandled.push(reason) }
  1224. process.on('unhandledRejection', onUnhandled)
  1225. try {
  1226. const ctx = new Context()
  1227. await ctx.plugin(SessionStore)
  1228. await expect(ctx.plugin(SessionQuerySqlite, { path }))
  1229. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1230. await new Promise<void>((resolve) => { setImmediate(resolve) })
  1231. expect(unhandled).toEqual([])
  1232. expect(ctx.sessionQuery).toBeUndefined()
  1233. } finally {
  1234. process.off('unhandledRejection', onUnhandled)
  1235. }
  1236. })
  1237. it('defers an invalid database failure only in first-search mode', async () => {
  1238. const path = await temporaryPath('lazy-invalid.db')
  1239. const foreign = new DatabaseSync(path)
  1240. foreign.exec('CREATE TABLE canonical(value TEXT)')
  1241. foreign.close()
  1242. const lazyCtx = new Context()
  1243. await lazyCtx.plugin(SessionStore)
  1244. const lazy = await lazyCtx.plugin(SessionQuerySqlite, {
  1245. path,
  1246. openAt: 'first-search',
  1247. })
  1248. expect(lazyCtx.sessionQuery).toBeInstanceOf(SessionQuerySqlite)
  1249. await expect(lazyCtx.sessionQuery.searchSessions({ query: 'needle' }))
  1250. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1251. await lazy.dispose()
  1252. const eagerCtx = new Context()
  1253. await eagerCtx.plugin(SessionStore)
  1254. await expect(eagerCtx.plugin(SessionQuerySqlite, { path }))
  1255. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1256. expect(eagerCtx.sessionQuery).toBeUndefined()
  1257. })
  1258. it.each(['sessions', 'events'] as const)(
  1259. 'forwards one exact reconciliation signal through both snapshot lists and persisted inspection for %s search',
  1260. async (scope) => {
  1261. const durable = header(`signal-${scope}`)
  1262. TestPersistence.reset([{ meta: durable, events: messageEvents('signal needle') }])
  1263. const ctx = await liveContext()
  1264. await ctx.plugin(TestPersistence)
  1265. const controller = new AbortController()
  1266. const result = scope === 'sessions'
  1267. ? await ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: controller.signal })
  1268. : await ctx.sessionQuery.searchEvents(
  1269. { sessionId: durable.id, query: 'needle' },
  1270. { signal: controller.signal },
  1271. )
  1272. expect(result.items).toHaveLength(1)
  1273. expect(TestPersistence.snapshotSignals).toEqual([controller.signal, controller.signal])
  1274. expect(TestPersistence.inspectSignals).toEqual([controller.signal])
  1275. },
  1276. )
  1277. it.each(['sessions', 'events'] as const)(
  1278. 'starts no persistence observation for a pre-aborted %s search',
  1279. async (scope) => {
  1280. const durable = header(`pre-aborted-${scope}`)
  1281. TestPersistence.reset([{ meta: durable, events: messageEvents('needle') }])
  1282. const ctx = await liveContext()
  1283. await ctx.plugin(TestPersistence)
  1284. const controller = new AbortController()
  1285. controller.abort(new Error(`pre-aborted ${scope}`))
  1286. const pending = scope === 'sessions'
  1287. ? ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: controller.signal })
  1288. : ctx.sessionQuery.searchEvents(
  1289. { sessionId: durable.id, query: 'needle' },
  1290. { signal: controller.signal },
  1291. )
  1292. await expect(pending).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1293. expect(TestPersistence.snapshotSignals).toEqual([])
  1294. expect(TestPersistence.inspectSignals).toEqual([])
  1295. },
  1296. )
  1297. it('awaits cooperative snapshot-list cancellation cleanup without starting another observation step', async () => {
  1298. const durable = header('cooperative-list-abort')
  1299. TestPersistence.reset([{ meta: durable, events: messageEvents('needle') }])
  1300. const ctx = await liveContext()
  1301. await ctx.plugin(TestPersistence)
  1302. const started = Promise.withResolvers<AbortSignal>()
  1303. const abortObserved = Promise.withResolvers<undefined>()
  1304. const cleanup = Promise.withResolvers<undefined>()
  1305. TestPersistence.snapshotEffect = async (signal) => {
  1306. TestPersistence.snapshotEffect = undefined
  1307. if (signal === undefined) throw new Error('expected reconciliation signal')
  1308. started.resolve(signal)
  1309. await new Promise<void>((resolve) => {
  1310. signal.addEventListener('abort', () => { resolve() }, { once: true })
  1311. })
  1312. abortObserved.resolve(undefined)
  1313. await cleanup.promise
  1314. signal.throwIfAborted()
  1315. }
  1316. const controller = new AbortController()
  1317. const pending = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: controller.signal })
  1318. expect(await started.promise).toBe(controller.signal)
  1319. let settled = false
  1320. void pending.then(
  1321. () => { settled = true },
  1322. () => { settled = true },
  1323. )
  1324. controller.abort(new Error('cooperative list cancellation'))
  1325. await abortObserved.promise
  1326. expect(settled).toBe(false)
  1327. expect(TestPersistence.snapshotSignals).toEqual([controller.signal])
  1328. expect(TestPersistence.inspectSignals).toEqual([])
  1329. cleanup.resolve(undefined)
  1330. await expect(pending).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1331. })
  1332. it('keeps a second search serialized while an abort-ignoring snapshot list finishes', async () => {
  1333. const durable = header('serialized-list-abort')
  1334. TestPersistence.reset([{ meta: durable, events: messageEvents('needle') }])
  1335. const ctx = await liveContext()
  1336. await ctx.plugin(TestPersistence)
  1337. const cleanup = Promise.withResolvers<undefined>()
  1338. const started = Promise.withResolvers<undefined>()
  1339. TestPersistence.listGate = cleanup.promise
  1340. TestPersistence.listStarted = () => {
  1341. TestPersistence.listStarted = undefined
  1342. started.resolve(undefined)
  1343. }
  1344. const controller = new AbortController()
  1345. const first = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: controller.signal })
  1346. await started.promise
  1347. let firstSettled = false
  1348. let secondSettled = false
  1349. void first.then(
  1350. () => { firstSettled = true },
  1351. () => { firstSettled = true },
  1352. )
  1353. controller.abort(new Error('ignored list cancellation'))
  1354. const second = ctx.sessionQuery.searchEvents({ sessionId: durable.id, query: 'needle' })
  1355. void second.then(
  1356. () => { secondSettled = true },
  1357. () => { secondSettled = true },
  1358. )
  1359. await Promise.resolve()
  1360. expect(firstSettled).toBe(false)
  1361. expect(secondSettled).toBe(false)
  1362. expect(TestPersistence.snapshotSignals).toEqual([controller.signal])
  1363. expect(TestPersistence.inspectSignals).toEqual([])
  1364. cleanup.resolve(undefined)
  1365. await expect(first).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1366. await expect(second).resolves.toMatchObject({ items: [{ sessionId: durable.id }] })
  1367. })
  1368. it('awaits an abort-ignoring inspection and starts neither another inspection nor the after-list', async () => {
  1369. const first = header('ignored-inspect-first')
  1370. const second = header('ignored-inspect-second')
  1371. TestPersistence.reset([
  1372. { meta: first, events: messageEvents('first needle') },
  1373. { meta: second, events: messageEvents('second needle') },
  1374. ])
  1375. const ctx = await liveContext()
  1376. await ctx.plugin(TestPersistence)
  1377. const started = Promise.withResolvers<AbortSignal>()
  1378. const cleanup = Promise.withResolvers<undefined>()
  1379. TestPersistence.inspectEffect = async (_entry, signal) => {
  1380. TestPersistence.inspectEffect = undefined
  1381. if (signal === undefined) throw new Error('expected reconciliation signal')
  1382. started.resolve(signal)
  1383. await cleanup.promise
  1384. }
  1385. const controller = new AbortController()
  1386. const pending = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: controller.signal })
  1387. expect(await started.promise).toBe(controller.signal)
  1388. let settled = false
  1389. void pending.then(
  1390. () => { settled = true },
  1391. () => { settled = true },
  1392. )
  1393. controller.abort(new Error('ignored inspect cancellation'))
  1394. await Promise.resolve()
  1395. expect(settled).toBe(false)
  1396. expect(TestPersistence.snapshotSignals).toEqual([controller.signal])
  1397. expect(TestPersistence.inspections.get(first.id)).toBe(1)
  1398. expect(TestPersistence.inspections.get(second.id)).toBeUndefined()
  1399. cleanup.resolve(undefined)
  1400. await expect(pending).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1401. expect(TestPersistence.snapshotSignals).toEqual([controller.signal])
  1402. expect(TestPersistence.inspections.get(second.id)).toBeUndefined()
  1403. })
  1404. it('cancels both queued and in-flight source waits without committing them', async () => {
  1405. TestPersistence.reset()
  1406. const ctx = await liveContext()
  1407. await ctx.plugin(TestPersistence)
  1408. const boundaryController = new AbortController()
  1409. const boundary = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: boundaryController.signal })
  1410. queueMicrotask(() => { boundaryController.abort() })
  1411. await expect(boundary).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1412. const readyController = new AbortController()
  1413. readyController.abort()
  1414. const internals = ctx.sessionQuery as unknown as {
  1415. _ensureReady(signal: AbortSignal): Promise<void>
  1416. }
  1417. await expect(internals._ensureReady(readyController.signal))
  1418. .rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1419. let releaseBlocking!: () => void
  1420. TestPersistence.listGate = new Promise<void>((resolve) => { releaseBlocking = resolve })
  1421. let markBlockingStarted!: () => void
  1422. const blockingStarted = new Promise<void>((resolve) => { markBlockingStarted = resolve })
  1423. TestPersistence.listStarted = () => {
  1424. TestPersistence.listStarted = undefined
  1425. markBlockingStarted()
  1426. }
  1427. const blocking = ctx.sessionQuery.searchSessions({ query: 'needle' })
  1428. await blockingStarted
  1429. const queuedController = new AbortController()
  1430. const queued = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: queuedController.signal })
  1431. queuedController.abort()
  1432. await expect(queued).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1433. releaseBlocking()
  1434. await expect(blocking).resolves.toEqual({ items: [] })
  1435. TestPersistence.set({
  1436. meta: header('uncommitted'),
  1437. events: messageEvents('durable needle'),
  1438. })
  1439. let releaseActive!: () => void
  1440. TestPersistence.listGate = new Promise<void>((resolve) => { releaseActive = resolve })
  1441. let markActiveStarted!: () => void
  1442. const activeStarted = new Promise<void>((resolve) => { markActiveStarted = resolve })
  1443. TestPersistence.listStarted = () => {
  1444. TestPersistence.listStarted = undefined
  1445. markActiveStarted()
  1446. }
  1447. const activeController = new AbortController()
  1448. const active = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: activeController.signal })
  1449. await activeStarted
  1450. activeController.abort()
  1451. let activeSettled = false
  1452. void active.then(
  1453. () => { activeSettled = true },
  1454. () => { activeSettled = true },
  1455. )
  1456. await Promise.resolve()
  1457. expect(activeSettled).toBe(false)
  1458. releaseActive()
  1459. await expect(active).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1460. const db = (ctx.sessionQuery as unknown as { _db: DatabaseSync })._db
  1461. expect(db.prepare('SELECT COUNT(*) AS count FROM persisted_sessions').get()).toEqual({ count: 0 })
  1462. await expect(ctx.sessionQuery.searchSessions({ query: 'needle' }))
  1463. .resolves.toMatchObject({ items: [{ header: { id: SessionId('uncommitted') } }] })
  1464. })
  1465. it.each([
  1466. [new Error('ready error'), 'ready error'],
  1467. ['non-error ready failure', 'session-search dependency rejected with a non-Error value'],
  1468. ])('normalizes a rejected readiness wait before mapping it to an index error', async (failure, detail) => {
  1469. TestPersistence.reset()
  1470. const ctx = await liveContext()
  1471. const internals = ctx.sessionQuery as unknown as {
  1472. _ready: Promise<void>
  1473. _ensureReady(signal: AbortSignal): Promise<void>
  1474. }
  1475. internals._ready = Promise.resolve().then(() => {
  1476. throw failure
  1477. })
  1478. await expect(internals._ensureReady(new AbortController().signal))
  1479. .rejects.toThrow(`session-search SQLite index failed to open: ${detail}`)
  1480. })
  1481. it('checks cancellation after readiness before reconciliation accesses SQLite', async () => {
  1482. TestPersistence.reset()
  1483. const ctx = await liveContext()
  1484. const internals = ctx.sessionQuery as unknown as {
  1485. _db: DatabaseSync
  1486. _ready: Promise<void>
  1487. _ensureReady(signal: AbortSignal | undefined): Promise<void>
  1488. }
  1489. const readiness = Promise.withResolvers<undefined>()
  1490. internals._ready = readiness.promise
  1491. const readyWaitStarted = Promise.withResolvers<undefined>()
  1492. const ensureReady = internals._ensureReady.bind(internals)
  1493. vi.spyOn(internals, '_ensureReady').mockImplementation(async (signal) => {
  1494. const pending = ensureReady(signal)
  1495. readyWaitStarted.resolve(undefined)
  1496. return pending
  1497. })
  1498. const prepare = vi.spyOn(internals._db, 'prepare')
  1499. const reason = new Error('cancelled after readiness')
  1500. const controller = new AbortController()
  1501. const pending = ctx.sessionQuery.searchSessions({ query: 'needle' }, { signal: controller.signal })
  1502. await readyWaitStarted.promise
  1503. const queueBoundaryAbort = readiness.promise.then(() => {
  1504. queueMicrotask(() => { controller.abort(reason) })
  1505. })
  1506. readiness.resolve(undefined)
  1507. await queueBoundaryAbort
  1508. await expect(pending).rejects.toThrow(expectCode('SESSION_QUERY_ABORTED'))
  1509. expect(prepare).not.toHaveBeenCalled()
  1510. })
  1511. it('rejects queued and future work when close waits for an accepted operation', async () => {
  1512. TestPersistence.reset()
  1513. let release!: () => void
  1514. TestPersistence.listGate = new Promise<void>((resolve) => { release = resolve })
  1515. let markStarted!: () => void
  1516. const started = new Promise<void>((resolve) => { markStarted = resolve })
  1517. TestPersistence.listStarted = () => {
  1518. TestPersistence.listStarted = undefined
  1519. markStarted()
  1520. }
  1521. const ctx = await liveContext()
  1522. await ctx.plugin(TestPersistence)
  1523. const search = ctx.sessionQuery as SessionQuerySqlite
  1524. const accepted = search.searchSessions({ query: 'needle' })
  1525. await started
  1526. const queued = search.searchSessions({ query: 'needle' })
  1527. const closing = search.close()
  1528. const repeatedClose = search.close()
  1529. expect(repeatedClose).toBe(closing)
  1530. release()
  1531. await expect(accepted).resolves.toEqual({ items: [] })
  1532. await expect(queued).rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1533. await Promise.all([closing, repeatedClose])
  1534. await expect(search.searchSessions({ query: 'needle' }))
  1535. .rejects.toThrow(expectCode('SESSION_QUERY_INDEX_FAILED'))
  1536. expect(search.close()).toBe(closing)
  1537. })
  1538. it('awaits optional-persistence child-fiber quiescence on disposal', async () => {
  1539. TestPersistence.reset()
  1540. const ctx = new Context()
  1541. await ctx.plugin(SessionStore)
  1542. const search = await ctx.plugin(SessionQuerySqlite, { path: ':memory:' })
  1543. const persistence = await ctx.plugin(TestPersistence)
  1544. const optional = (ctx.sessionQuery as unknown as {
  1545. _optionalPersistenceFiber: Fiber
  1546. })._optionalPersistenceFiber
  1547. let release!: () => void
  1548. const cleanup = new Promise<void>((resolve) => { release = resolve })
  1549. optional.ctx.effect(() => () => cleanup)
  1550. let settled = false
  1551. const disposing = search.dispose().then(() => { settled = true })
  1552. await Promise.resolve()
  1553. expect(settled).toBe(false)
  1554. release()
  1555. await disposing
  1556. await persistence.dispose()
  1557. })
  1558. it('combines the real SQLite persistence backend with the real search service keylessly', async () => {
  1559. const persistencePath = await temporaryPath('canonical.db')
  1560. const searchPath = await temporaryPath('derived.db')
  1561. const ctx = new Context()
  1562. await ctx.plugin(SessionStore)
  1563. const persistence = await ctx.plugin(SessionPersistenceSqlite, { path: persistencePath })
  1564. const search = await ctx.plugin(SessionQuerySqlite, { path: searchPath })
  1565. const meta = header('real', 10, { cwd: '/work' })
  1566. await ctx.sessionPersistence.create(meta)
  1567. await ctx.sessionPersistence.append(meta.id, messageEvents('real SQLite needle'))
  1568. await expect(ctx.sessionQuery.searchSessions({ query: 'SQLite needle' }))
  1569. .resolves.toMatchObject({ items: [{ header: meta, persisted: true, live: false }] })
  1570. await expect(ctx.sessionQuery.searchEvents({ sessionId: meta.id, query: 'SQLite needle' }))
  1571. .resolves.toMatchObject({ session: meta, items: [{ sessionId: meta.id, seq: 0 }] })
  1572. await expect(ctx.sessionQuery.searchEvents({ sessionId: SessionId('absent'), query: 'needle' }))
  1573. .rejects.toThrow(expectCode('SESSION_QUERY_SESSION_NOT_FOUND'))
  1574. await search.dispose()
  1575. await expect(ctx.sessionPersistence.load(meta.id)).resolves.toMatchObject({ meta, events: [{ seq: 0 }] })
  1576. await persistence.dispose()
  1577. })
  1578. it('reconciles colliding local revisions when a derived index reopens against another SQLite store', async () => {
  1579. const persistencePathA = await temporaryPath('canonical-a.db')
  1580. const persistencePathB = await temporaryPath('canonical-b.db')
  1581. const searchPath = await temporaryPath('derived-collision.db')
  1582. const shared = header('same-id', 10)
  1583. const first = new Context()
  1584. await first.plugin(SessionStore)
  1585. const persistenceA = await first.plugin(SessionPersistenceSqlite, { path: persistencePathA })
  1586. await first.sessionPersistence.create(shared)
  1587. await first.sessionPersistence.append(shared.id, messageEvents('alpha source'))
  1588. const inspectA = vi.spyOn(first.sessionPersistence, 'inspect')
  1589. const searchA = await first.plugin(SessionQuerySqlite, { path: searchPath })
  1590. await expect(first.sessionQuery.searchSessions({ query: 'alpha' }))
  1591. .resolves.toMatchObject({ items: [{ header: shared }] })
  1592. expect(inspectA).toHaveBeenCalledTimes(1)
  1593. await searchA.dispose()
  1594. await persistenceA.dispose()
  1595. const reopened = new Context()
  1596. await reopened.plugin(SessionStore)
  1597. const persistenceAAgain = await reopened.plugin(SessionPersistenceSqlite, { path: persistencePathA })
  1598. const reopenedInspect = vi.spyOn(reopened.sessionPersistence, 'inspect')
  1599. const searchAAgain = await reopened.plugin(SessionQuerySqlite, { path: searchPath })
  1600. await expect(reopened.sessionQuery.searchSessions({ query: 'alpha' }))
  1601. .resolves.toMatchObject({ items: [{ header: shared }] })
  1602. expect(reopenedInspect).not.toHaveBeenCalled()
  1603. await searchAAgain.dispose()
  1604. await persistenceAAgain.dispose()
  1605. const second = new Context()
  1606. await second.plugin(SessionStore)
  1607. const persistenceB = await second.plugin(SessionPersistenceSqlite, { path: persistencePathB })
  1608. await second.sessionPersistence.create(shared)
  1609. await second.sessionPersistence.append(shared.id, messageEvents('bravo source'))
  1610. const inspectB = vi.spyOn(second.sessionPersistence, 'inspect')
  1611. const searchB = await second.plugin(SessionQuerySqlite, { path: searchPath })
  1612. await expect(second.sessionQuery.searchSessions({ query: 'bravo' }))
  1613. .resolves.toMatchObject({ items: [{ header: shared }] })
  1614. await expect(second.sessionQuery.searchSessions({ query: 'alpha' })).resolves.toEqual({ items: [] })
  1615. expect(inspectB).toHaveBeenCalledTimes(1)
  1616. await searchB.dispose()
  1617. await persistenceB.dispose()
  1618. })
  1619. })