sqlite.spec.ts 85 KB

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