sqlite.spec.ts 74 KB

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