sqlite.spec.ts 76 KB

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