sqlite.spec.ts 79 KB

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