index.ts 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /**
  2. * Concrete session-query service with SQLite FTS5 over the live-preferred corpus.
  3. *
  4. * @module @deepseek-ai/dsh-session-query-sqlite
  5. */
  6. import { createHash, randomUUID } from 'node:crypto'
  7. import type { DatabaseSync } from 'node:sqlite'
  8. import { Context, Service, type Fiber } from '@deepseek-ai/cordis'
  9. import z from '@deepseek-ai/schemastery'
  10. import type { Session, SessionEvent, SessionHeader, SessionId } from '@deepseek-ai/dsh-session'
  11. import type SessionPersistence from '@deepseek-ai/dsh-session-persistence'
  12. import type {
  13. SessionPersistenceRevision,
  14. SessionPersistenceSnapshot,
  15. } from '@deepseek-ai/dsh-session-persistence'
  16. import SessionQueryService, {
  17. SESSION_QUERY_DEFAULT_PERSISTED_INSPECT_CONCURRENCY,
  18. SESSION_QUERY_READ_WINDOW_MAX,
  19. SessionQueryError,
  20. SessionSearchCursor,
  21. assertSessionHeadersCompatible,
  22. buildSessionEventSearchDocuments,
  23. } from '@deepseek-ai/dsh-session-query'
  24. import type {
  25. Config as SessionQueryConfig,
  26. SessionEventSearchDocument,
  27. SessionEventSearchHit,
  28. SessionEventSearchPage,
  29. SessionEventSearchRequest,
  30. SessionSearchExecContext,
  31. SessionSearchHit,
  32. SessionSearchCursor as SessionSearchCursorValue,
  33. SessionSearchPage,
  34. SessionSearchRequest,
  35. } from '@deepseek-ai/dsh-session-query'
  36. import {
  37. type JournalMode,
  38. openSearchDatabase,
  39. } from './schema.ts'
  40. import {
  41. type NormalizedEventRequest,
  42. type NormalizedSessionRequest,
  43. FTS_HIGHLIGHT_END,
  44. FTS_HIGHLIGHT_START,
  45. assertFts5OuterPredicateCount,
  46. assertPortableBindingCount,
  47. buildEventWhere,
  48. buildSessionWhere,
  49. makeSnippet,
  50. normalizeEventRequest,
  51. normalizeSessionRequest,
  52. quoteFtsData,
  53. requestFingerprint,
  54. sanitizeFtsText,
  55. SQLITE_MAX_PAGE_LIMIT,
  56. } from './query.ts'
  57. export {
  58. SESSION_QUERY_SQLITE_APPLICATION_ID,
  59. SESSION_QUERY_SQLITE_SCHEMA_VERSION,
  60. type JournalMode,
  61. } from './schema.ts'
  62. /** Boot-context slot for a launcher-owned absolute path to this process's derived query index. */
  63. export const SESSION_QUERY_SQLITE_PATH_KEY = 'launcherSessionQueryPath'
  64. declare module '@deepseek-ai/cordis' {
  65. interface Context {
  66. /** Launcher-owned absolute path to this process's disposable derived query index. */
  67. launcherSessionQueryPath?: string
  68. }
  69. }
  70. /** Default result page size. */
  71. export const SESSION_QUERY_SQLITE_DEFAULT_LIMIT = 20
  72. /** Maximum accepted result page size. */
  73. export const SESSION_QUERY_SQLITE_MAX_LIMIT = 100
  74. /** Default maximum snippet length in Unicode code points. */
  75. export const SESSION_QUERY_SQLITE_SNIPPET_CHARS = 240
  76. // One transient source change gets a retry; repeated churn fails rather than monopolizing the queue.
  77. const STABLE_OBSERVATION_ATTEMPTS = 2
  78. /** SQLite module/handle opening phase. */
  79. export type OpenAt = 'startup' | 'first-search'
  80. /** Combined session-query configuration backed by SQLite full-text search. */
  81. export interface Config extends SessionQueryConfig {
  82. /**
  83. * Dedicated derived-index path; `:memory:` is supported for ephemeral
  84. * indexes. Missing directories and database files are created owner-only on
  85. * POSIX filesystems; existing modes are preserved.
  86. */
  87. path: string
  88. /** Open the SQLite module and handle at service activation or the first search. Defaults to `startup`. */
  89. openAt?: OpenAt
  90. /** SQLite journal mode. Defaults to `wal`. */
  91. journalMode?: JournalMode
  92. /** Page size when a request omits `limit`. At most `Number.MAX_SAFE_INTEGER - 1`; defaults to 20. */
  93. defaultLimit?: number
  94. /** Largest accepted page size. At most `Number.MAX_SAFE_INTEGER - 1`; defaults to 100. */
  95. maxLimit?: number
  96. /** Maximum snippet length in Unicode code points. Defaults to 240. */
  97. snippetChars?: number
  98. /** Maximum concurrent persisted-log inspections in one inherited batch read. Defaults to 4. */
  99. persistedInspectConcurrency?: number
  100. }
  101. interface ResolvedConfig {
  102. path: string
  103. openAt: OpenAt
  104. journalMode: JournalMode
  105. defaultLimit: number
  106. maxLimit: number
  107. snippetChars: number
  108. readWindowMax: number
  109. persistedInspectConcurrency: number
  110. }
  111. interface ObservedSession {
  112. header: SessionHeader
  113. documents: SessionEventSearchDocument[]
  114. fingerprint: string
  115. }
  116. interface ObservedPersistedSession {
  117. header: SessionHeader
  118. revision: SessionPersistenceRevision
  119. loaded?: ObservedSession
  120. }
  121. interface PersistenceBinding {
  122. readonly identity: symbol
  123. readonly service?: SessionPersistence
  124. }
  125. interface Observation {
  126. persistenceBinding: PersistenceBinding
  127. persisted: Map<SessionId, ObservedPersistedSession>
  128. live: Map<SessionId, ObservedSession>
  129. }
  130. interface IndexedPersistedRow {
  131. id: string
  132. revision: string
  133. generation: number
  134. }
  135. interface IndexedLiveRow {
  136. id: string
  137. fingerprint: string
  138. persisted: number
  139. generation: number
  140. }
  141. interface SessionHeaderRow {
  142. session_id: string
  143. version: number
  144. created_at: number
  145. cwd: string | null
  146. parent_session: string | null
  147. seed_length: number | null
  148. delegation_depth: number | null
  149. agent_preset: string | null
  150. }
  151. interface SearchRow extends SessionHeaderRow {
  152. live: number
  153. persisted: number
  154. seq: number
  155. type: string
  156. time: number
  157. surface: string
  158. marked_text: string
  159. match_count: number
  160. document_length: number
  161. }
  162. interface CursorPayload {
  163. version: 1
  164. instance: string
  165. scope: 'sessions' | 'events'
  166. fingerprint: string
  167. generation: string
  168. offset: number
  169. }
  170. /** Concrete SQLite owner of the combined `ctx.sessionQuery` service. */
  171. export class SessionQuerySqlite extends SessionQueryService {
  172. static override inject = ['sessions']
  173. static Config: z<Config> = z.object({
  174. path: z.string().required(),
  175. openAt: z.union(['startup', 'first-search'] as const).default('startup'),
  176. journalMode: z.union(['wal', 'delete', 'truncate', 'persist'] as const).default('wal'),
  177. defaultLimit: z.number().step(1).min(1).max(SQLITE_MAX_PAGE_LIMIT).default(SESSION_QUERY_SQLITE_DEFAULT_LIMIT),
  178. maxLimit: z.number().step(1).min(1).max(SQLITE_MAX_PAGE_LIMIT).default(SESSION_QUERY_SQLITE_MAX_LIMIT),
  179. snippetChars: z.number().step(1).min(1).default(SESSION_QUERY_SQLITE_SNIPPET_CHARS),
  180. readWindowMax: z.number().step(1).min(0).default(SESSION_QUERY_READ_WINDOW_MAX),
  181. persistedInspectConcurrency: z.number()
  182. .step(1)
  183. .min(1)
  184. .max(Number.MAX_SAFE_INTEGER)
  185. .default(SESSION_QUERY_DEFAULT_PERSISTED_INSPECT_CONCURRENCY),
  186. })
  187. /** Validated and defaulted backend configuration. */
  188. readonly config: ResolvedConfig
  189. private readonly _instance = randomUUID()
  190. private _ready: Promise<void> | undefined
  191. private _db: DatabaseSync | undefined
  192. private _persistenceBinding: PersistenceBinding = { identity: Symbol() }
  193. private _lastPersistenceIdentity: symbol | undefined
  194. private _persistenceEpoch = 0
  195. private _globalGeneration = 0
  196. private _localGeneration = 0
  197. private _tail: Promise<void> = Promise.resolve()
  198. private _closed = false
  199. private _closePromise: Promise<void> | undefined
  200. private readonly _optionalPersistenceFiber: Fiber
  201. constructor(ctx: Context, config: Config) {
  202. // The assignment expression resolves before the base constructor can
  203. // register `ctx.sessionQuery`; keep that same validated value afterward.
  204. super(ctx, config = resolveConfig(config))
  205. this.config = config as ResolvedConfig
  206. this._optionalPersistenceFiber = ctx.inject(['sessionPersistence'], (childCtx: Context) => {
  207. const service = childCtx.sessionPersistence
  208. const binding = { identity: Symbol(), service }
  209. this._persistenceBinding = binding
  210. childCtx.effect(() => () => {
  211. /* v8 ignore next -- a stale optional-service disposer cannot clear a replacement */
  212. if (this._persistenceBinding !== binding) return
  213. this._persistenceBinding = { identity: Symbol() }
  214. }, 'sessionQuerySqlite.persistenceBinding')
  215. })
  216. ctx.effect(() => {
  217. return () => this._optionalPersistenceFiber.dispose()
  218. }, 'sessionQuerySqlite.optionalPersistence')
  219. ctx.effect(() => async () => this.close(), 'sessionQuerySqlite.close')
  220. }
  221. /** Open eagerly only when activation owns the configured readiness boundary. */
  222. protected async [Service.init](): Promise<void> {
  223. if (this.config.openAt === 'startup') await this._ensureReady(undefined)
  224. }
  225. override async searchSessions(
  226. request: SessionSearchRequest,
  227. exec?: SessionSearchExecContext,
  228. ): Promise<SessionSearchPage<SessionSearchHit>> {
  229. const normalized = normalizeSessionRequest(request, this.config)
  230. const signal = exec?.signal
  231. return this._serialized(signal, async () => {
  232. await this._ensureReady(signal)
  233. const persistenceBinding = await this._reconcile(signal)
  234. assertNotAborted(signal)
  235. const generation = String(this._globalGeneration)
  236. const fingerprint = requestFingerprint(normalized)
  237. const offset = normalized.cursor === undefined
  238. ? 0
  239. : decodeCursor(normalized.cursor, this._instance, 'sessions', fingerprint, generation)
  240. const rows = this._querySessions(normalized, offset, persistenceBinding)
  241. return page(rows, normalized.limit, row => this._sessionHit(row), cursorOffset => encodeCursor({
  242. version: 1,
  243. instance: this._instance,
  244. scope: 'sessions',
  245. fingerprint,
  246. generation,
  247. offset: cursorOffset,
  248. }), offset)
  249. })
  250. }
  251. override async searchEvents(
  252. request: SessionEventSearchRequest,
  253. exec?: SessionSearchExecContext,
  254. ): Promise<SessionEventSearchPage> {
  255. const normalized = normalizeEventRequest(request, this.config)
  256. const signal = exec?.signal
  257. return this._serialized(signal, async () => {
  258. await this._ensureReady(signal)
  259. const persistenceBinding = await this._reconcile(signal)
  260. assertNotAborted(signal)
  261. const target = this._targetObservation(normalized.sessionId, persistenceBinding)
  262. const fingerprint = requestFingerprint(normalized)
  263. const offset = normalized.cursor === undefined
  264. ? 0
  265. : decodeCursor(normalized.cursor, this._instance, 'events', fingerprint, target.generation)
  266. const rows = this._queryEvents(normalized, offset, persistenceBinding)
  267. return {
  268. session: target.header,
  269. ...page(rows, normalized.limit, row => this._eventHit(row), cursorOffset => encodeCursor({
  270. version: 1,
  271. instance: this._instance,
  272. scope: 'events',
  273. fingerprint,
  274. generation: target.generation,
  275. offset: cursorOffset,
  276. }), offset),
  277. }
  278. })
  279. }
  280. /** Close the database after every accepted operation reaches quiescence. */
  281. close(): Promise<void> {
  282. this._closePromise ??= this._close()
  283. return this._closePromise
  284. }
  285. private async _close(): Promise<void> {
  286. this._closed = true
  287. await this._tail
  288. if (this._ready !== undefined) {
  289. try {
  290. await this._ready
  291. } catch {
  292. // Opening already closed a partially-created handle; disposal only waits.
  293. }
  294. }
  295. this._db?.close()
  296. this._db = undefined
  297. }
  298. private async _open(): Promise<void> {
  299. this._db = await openSearchDatabase(this.config.path, this.config.journalMode)
  300. const state = this._db.prepare(
  301. 'SELECT global_generation FROM search_state WHERE singleton = 1',
  302. ).get() as { global_generation: number }
  303. this._globalGeneration = state.global_generation
  304. this._localGeneration = state.global_generation
  305. }
  306. private async _ensureReady(signal: AbortSignal | undefined): Promise<void> {
  307. this._ready ??= this._open()
  308. try {
  309. await waitWithAbort(this._ready, signal)
  310. } catch (error: unknown) {
  311. if (isAbort(error)) throw error
  312. throw new SessionQueryError(
  313. `session-search SQLite index failed to open: ${errorMessage(error)}`,
  314. 'SESSION_QUERY_INDEX_FAILED',
  315. { cause: error },
  316. )
  317. }
  318. }
  319. private async _serialized<T>(signal: AbortSignal | undefined, operation: () => Promise<T>): Promise<T> {
  320. if (this._isClosed()) throw indexClosed()
  321. let release!: () => void
  322. const gate = new Promise<void>((resolve) => { release = resolve })
  323. const prior = this._tail
  324. this._tail = prior.then(() => gate)
  325. try {
  326. await waitWithAbort(prior, signal)
  327. } catch (error: unknown) {
  328. release()
  329. throw error
  330. }
  331. if (this._isClosed()) {
  332. release()
  333. throw indexClosed()
  334. }
  335. try {
  336. assertNotAborted(signal)
  337. return await operation()
  338. } finally {
  339. release()
  340. }
  341. }
  342. private async _reconcile(signal: AbortSignal | undefined): Promise<PersistenceBinding> {
  343. assertNotAborted(signal)
  344. const db = this._requireDb()
  345. const persistedRows = db.prepare(
  346. 'SELECT id, revision, generation FROM persisted_sessions',
  347. ).all() as unknown as IndexedPersistedRow[]
  348. const liveRows = db.prepare(
  349. 'SELECT id, fingerprint, persisted, generation FROM temp.live_sessions',
  350. ).all() as unknown as IndexedLiveRow[]
  351. const persistedById = new Map(persistedRows.map(row => [row.id as SessionId, row]))
  352. const liveById = new Map(liveRows.map(row => [row.id as SessionId, row]))
  353. const observation = await this._observeStable(persistedById, signal)
  354. assertNotAborted(signal)
  355. const persistentChanges = observation.persistenceBinding.service === undefined
  356. ? []
  357. : [...observation.persisted.values()].filter(entry => entry.loaded !== undefined)
  358. const persistentDeletes = observation.persistenceBinding.service === undefined
  359. ? []
  360. : persistedRows.filter(row => !observation.persisted.has(row.id as SessionId))
  361. const liveChanges = [...observation.live.values()].filter((entry) => {
  362. const indexed = liveById.get(entry.header.id)
  363. const persisted = observation.persisted.has(entry.header.id) ? 1 : 0
  364. return indexed?.fingerprint !== entry.fingerprint || indexed.persisted !== persisted
  365. })
  366. const liveDeletes = liveRows.filter(row => !observation.live.has(row.id as SessionId))
  367. const pointerChanged = this._lastPersistenceIdentity !== undefined
  368. && this._lastPersistenceIdentity !== observation.persistenceBinding.identity
  369. const hasWrites = persistentChanges.length > 0
  370. || persistentDeletes.length > 0
  371. || liveChanges.length > 0
  372. || liveDeletes.length > 0
  373. let nextMainGeneration = this._mainGeneration()
  374. let nextLocalGeneration = this._localGeneration
  375. if (persistentChanges.length > 0 || persistentDeletes.length > 0) nextMainGeneration += 1
  376. const liveReplacements = liveChanges.map((entry) => {
  377. nextLocalGeneration = Math.max(nextLocalGeneration, nextMainGeneration) + 1
  378. return {
  379. entry,
  380. generation: nextLocalGeneration,
  381. persisted: observation.persisted.has(entry.header.id),
  382. }
  383. })
  384. if (hasWrites) {
  385. let began = false
  386. try {
  387. db.exec('BEGIN IMMEDIATE')
  388. began = true
  389. for (const row of persistentDeletes) this._deleteSession('persisted', row.id as SessionId)
  390. for (const entry of persistentChanges) {
  391. /* v8 ignore next -- observation loads every entry whose revision differs */
  392. if (entry.loaded === undefined) throw new Error(`missing loaded revision for session "${entry.header.id}"`)
  393. this._replacePersistedSession(entry.loaded, entry.revision, nextMainGeneration)
  394. }
  395. if (persistentChanges.length > 0 || persistentDeletes.length > 0) {
  396. db.prepare('UPDATE search_state SET global_generation = ? WHERE singleton = 1').run(nextMainGeneration)
  397. }
  398. for (const row of liveDeletes) this._deleteSession('live', row.id as SessionId)
  399. for (const { entry, generation, persisted } of liveReplacements) {
  400. this._replaceLiveSession(entry, generation, persisted)
  401. }
  402. db.exec('COMMIT')
  403. } catch (error: unknown) {
  404. /* v8 ignore next -- a BEGIN failure has no transaction to roll back; the common wrapper still reports it. */
  405. if (began) {
  406. /* v8 ignore next 5 -- ROLLBACK failure requires a SQLite double fault; the original failure remains actionable. */
  407. try {
  408. db.exec('ROLLBACK')
  409. } catch {
  410. // The original SQLite failure remains the actionable cause.
  411. }
  412. }
  413. throw new SessionQueryError(
  414. `session-search reconciliation failed: ${errorMessage(error)}`,
  415. 'SESSION_QUERY_INDEX_FAILED',
  416. { cause: error },
  417. )
  418. }
  419. }
  420. if (hasWrites || pointerChanged) this._globalGeneration += 1
  421. if (pointerChanged) this._persistenceEpoch += 1
  422. this._localGeneration = nextLocalGeneration
  423. this._lastPersistenceIdentity = observation.persistenceBinding.identity
  424. return observation.persistenceBinding
  425. }
  426. private async _observeStable(
  427. indexed: ReadonlyMap<SessionId, IndexedPersistedRow>,
  428. signal: AbortSignal | undefined,
  429. ): Promise<Observation> {
  430. for (let attempt = 0; attempt < STABLE_OBSERVATION_ATTEMPTS; attempt += 1) {
  431. assertNotAborted(signal)
  432. const persistenceBinding = this._persistenceBinding
  433. const persistence = persistenceBinding.service
  434. const initiallyLive = new Set(this.ctx.sessions.list().map(session => session.id))
  435. let persisted = new Map<SessionId, ObservedPersistedSession>()
  436. if (persistence !== undefined) {
  437. try {
  438. const canReuseIndexed = this._lastPersistenceIdentity === undefined
  439. || this._lastPersistenceIdentity === persistenceBinding.identity
  440. const before = await persistence.listSnapshots(signal)
  441. assertNotAborted(signal)
  442. persisted = materializePersistenceSnapshots(before)
  443. for (const entry of persisted.values()) {
  444. if (canReuseIndexed && indexed.get(entry.header.id)?.revision === entry.revision) continue
  445. // Skip work already shadowed by a live owner. `inspect()` is
  446. // non-mutating, so an owner attaching after this check cannot cause
  447. // crash-repair side effects; the live-membership retry below makes
  448. // the returned observation live-preferred.
  449. if (initiallyLive.has(entry.header.id) || this.ctx.sessions.get(entry.header.id) !== undefined) continue
  450. assertNotAborted(signal)
  451. const loaded = await persistence.inspect(entry.header.id, signal)
  452. assertNotAborted(signal)
  453. assertSessionHeadersCompatible(entry.header, loaded.meta)
  454. entry.loaded = observeSession(loaded.meta, loaded.events)
  455. }
  456. assertNotAborted(signal)
  457. const afterSnapshots = await persistence.listSnapshots(signal)
  458. assertNotAborted(signal)
  459. const after = materializePersistenceSnapshots(afterSnapshots)
  460. if (!samePersistenceSnapshots(persisted, after)) continue
  461. if (this._persistenceBinding !== persistenceBinding) continue
  462. } catch (error: unknown) {
  463. if (isAbort(error) || signal?.aborted) {
  464. throw new SessionQueryError('session-search aborted', 'SESSION_QUERY_ABORTED', {
  465. cause: error,
  466. })
  467. }
  468. if (this._persistenceBinding !== persistenceBinding) continue
  469. if (error instanceof SessionQueryError) throw error
  470. throw new SessionQueryError(
  471. `session-search persistence observation failed: ${errorMessage(error)}`,
  472. 'SESSION_QUERY_PERSISTENCE_FAILED',
  473. { cause: error },
  474. )
  475. }
  476. }
  477. const live = new Map<SessionId, ObservedSession>()
  478. for (const session of this.ctx.sessions.list()) {
  479. const observed = observeLive(session)
  480. const durable = persisted.get(session.id)
  481. if (durable !== undefined) assertSessionHeadersCompatible(observed.header, durable.header)
  482. live.set(session.id, observed)
  483. }
  484. if (!sameSessionIds(initiallyLive, live)) continue
  485. return { persistenceBinding, persisted, live }
  486. }
  487. throw new SessionQueryError(
  488. 'session-search persistence observation did not stabilize after one retry',
  489. 'SESSION_QUERY_PERSISTENCE_FAILED',
  490. )
  491. }
  492. private _mainGeneration(): number {
  493. const row = this._requireDb().prepare(
  494. 'SELECT global_generation FROM search_state WHERE singleton = 1',
  495. ).get() as { global_generation: number }
  496. return row.global_generation
  497. }
  498. private _deleteSession(source: 'persisted' | 'live', id: SessionId): void {
  499. const db = this._requireDb()
  500. if (source === 'persisted') {
  501. db.prepare('DELETE FROM persisted_docs WHERE session_id = ?').run(id)
  502. db.prepare('DELETE FROM persisted_sessions WHERE id = ?').run(id)
  503. } else {
  504. db.prepare('DELETE FROM temp.live_docs WHERE session_id = ?').run(id)
  505. db.prepare('DELETE FROM temp.live_sessions WHERE id = ?').run(id)
  506. }
  507. }
  508. private _replacePersistedSession(
  509. entry: ObservedSession,
  510. revision: SessionPersistenceRevision,
  511. generation: number,
  512. ): void {
  513. this._deleteSession('persisted', entry.header.id)
  514. const db = this._requireDb()
  515. db.prepare(`
  516. INSERT INTO persisted_sessions
  517. (id, version, created_at, cwd, parent_session, seed_length, delegation_depth, agent_preset, revision, generation)
  518. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  519. `).run(
  520. ...headerBindings(entry.header),
  521. revision,
  522. generation,
  523. )
  524. const insert = db.prepare(`
  525. INSERT INTO persisted_docs (text, session_id, seq, type, time, surface, codepoint_length)
  526. VALUES (?, ?, ?, ?, ?, ?, ?)
  527. `)
  528. for (const document of entry.documents) {
  529. const text = sanitizeFtsText(document.text)
  530. insert.run(
  531. text,
  532. document.sessionId,
  533. document.seq,
  534. document.type,
  535. document.time,
  536. document.surface,
  537. Array.from(text).length,
  538. )
  539. }
  540. }
  541. private _replaceLiveSession(entry: ObservedSession, generation: number, persisted: boolean): void {
  542. this._deleteSession('live', entry.header.id)
  543. const db = this._requireDb()
  544. db.prepare(`
  545. INSERT INTO temp.live_sessions
  546. (id, version, created_at, cwd, parent_session, seed_length, delegation_depth, agent_preset, fingerprint, persisted, generation)
  547. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  548. `).run(
  549. ...headerBindings(entry.header),
  550. entry.fingerprint,
  551. persisted ? 1 : 0,
  552. generation,
  553. )
  554. const insert = db.prepare(`
  555. INSERT INTO temp.live_docs (text, session_id, seq, type, time, surface, codepoint_length)
  556. VALUES (?, ?, ?, ?, ?, ?, ?)
  557. `)
  558. for (const document of entry.documents) {
  559. const text = sanitizeFtsText(document.text)
  560. insert.run(
  561. text,
  562. document.sessionId,
  563. document.seq,
  564. document.type,
  565. document.time,
  566. document.surface,
  567. Array.from(text).length,
  568. )
  569. }
  570. }
  571. private _querySessions(
  572. request: NormalizedSessionRequest,
  573. offset: number,
  574. persistenceBinding: PersistenceBinding,
  575. ): SearchRow[] {
  576. const selected = selectedDocumentsSql()
  577. const sessionWhere = buildSessionWhere(request.sessionFilters)
  578. const eventWhere = buildEventWhere(request.eventFilters)
  579. assertFts5OuterPredicateCount(sessionWhere.predicateCount + eventWhere.predicateCount)
  580. const where = [sessionWhere.sql, eventWhere.sql].filter(Boolean).join(' AND ')
  581. const bindings = [
  582. ...selectedDocumentsParams(request.query, persistenceBinding.service !== undefined),
  583. ...sessionWhere.params,
  584. ...eventWhere.params,
  585. request.limit + 1,
  586. offset,
  587. ]
  588. assertPortableBindingCount(bindings.length)
  589. // The browser fixture mirrors these rank keys in
  590. // `packages/client/connection/src/client/fixture.ts`; update both together.
  591. return this._requireDb().prepare(`
  592. ${selected.sql},
  593. filtered AS (
  594. SELECT * FROM matched ${where.length === 0 ? '' : `WHERE ${where}`}
  595. ),
  596. ranked AS (
  597. SELECT *, ROW_NUMBER() OVER (
  598. PARTITION BY session_id
  599. ORDER BY match_count DESC, document_length ASC, time DESC, seq DESC
  600. ) AS event_rank
  601. FROM filtered
  602. )
  603. SELECT * FROM ranked
  604. WHERE event_rank = 1
  605. ORDER BY match_count DESC, document_length ASC, time DESC, session_id ASC, seq DESC
  606. LIMIT ? OFFSET ?
  607. `).all(...bindings) as unknown as SearchRow[]
  608. }
  609. private _queryEvents(
  610. request: NormalizedEventRequest,
  611. offset: number,
  612. persistenceBinding: PersistenceBinding,
  613. ): SearchRow[] {
  614. const selected = selectedDocumentsSql()
  615. const eventWhere = buildEventWhere(request.filters)
  616. assertFts5OuterPredicateCount(1 + eventWhere.predicateCount)
  617. const where = ['session_id = ?', eventWhere.sql].filter(Boolean).join(' AND ')
  618. const bindings = [
  619. ...selectedDocumentsParams(request.query, persistenceBinding.service !== undefined),
  620. request.sessionId,
  621. ...eventWhere.params,
  622. request.limit + 1,
  623. offset,
  624. ]
  625. assertPortableBindingCount(bindings.length)
  626. return this._requireDb().prepare(`
  627. ${selected.sql}
  628. SELECT * FROM matched
  629. WHERE ${where}
  630. ORDER BY match_count DESC, document_length ASC, time DESC, seq DESC
  631. LIMIT ? OFFSET ?
  632. `).all(...bindings) as unknown as SearchRow[]
  633. }
  634. private _targetObservation(
  635. sessionId: SessionId,
  636. persistenceBinding: PersistenceBinding,
  637. ): { header: SessionHeader; generation: string } {
  638. const db = this._requireDb()
  639. const live = db.prepare(
  640. `SELECT
  641. id AS session_id, version, created_at, cwd, parent_session, seed_length, delegation_depth, agent_preset, generation
  642. FROM temp.live_sessions
  643. WHERE id = ?`,
  644. ).get(sessionId) as (SessionHeaderRow & { generation: number }) | undefined
  645. if (live !== undefined) {
  646. return { header: rowHeader(live), generation: `live:${live.generation}` }
  647. }
  648. if (persistenceBinding.service !== undefined) {
  649. const persisted = db.prepare(
  650. `SELECT
  651. id AS session_id, version, created_at, cwd, parent_session, seed_length, delegation_depth, agent_preset, generation
  652. FROM persisted_sessions
  653. WHERE id = ?`,
  654. ).get(sessionId) as (SessionHeaderRow & { generation: number }) | undefined
  655. if (persisted !== undefined) {
  656. return {
  657. header: rowHeader(persisted),
  658. generation: `persisted:${this._persistenceEpoch}:${persisted.generation}`,
  659. }
  660. }
  661. }
  662. throw new SessionQueryError(
  663. `session "${sessionId}" not found`,
  664. 'SESSION_QUERY_SESSION_NOT_FOUND',
  665. )
  666. }
  667. private _sessionHit(row: SearchRow): SessionSearchHit {
  668. return {
  669. header: rowHeader(row),
  670. live: row.live === 1,
  671. persisted: row.persisted === 1,
  672. bestMatch: this._eventHit(row),
  673. }
  674. }
  675. private _eventHit(row: SearchRow): SessionEventSearchHit {
  676. return {
  677. sessionId: row.session_id as SessionId,
  678. seq: row.seq,
  679. type: row.type as SessionEventSearchHit['type'],
  680. time: row.time,
  681. surface: row.surface as SessionEventSearchHit['surface'],
  682. snippet: makeSnippet(row.marked_text, this.config.snippetChars),
  683. }
  684. }
  685. private _requireDb(): DatabaseSync {
  686. /* v8 ignore next -- callers await `_ready`; this guards lifecycle misuse */
  687. if (this._db === undefined) throw indexClosed()
  688. return this._db
  689. }
  690. private _isClosed(): boolean {
  691. return this._closed
  692. }
  693. }
  694. /**
  695. * The header columns both session upserts bind, in the order their INSERT
  696. * lists them. The two statements differ only in what they append after these.
  697. * @param header - the session header being written.
  698. * @returns one bound value per header column.
  699. */
  700. function headerBindings(header: SessionHeader): (string | number | null)[] {
  701. return [
  702. header.id,
  703. header.version,
  704. header.createdAt,
  705. header.cwd ?? null,
  706. header.parentSession ?? null,
  707. header.seedLength ?? null,
  708. header.delegationDepth ?? null,
  709. header.agentPreset ?? null,
  710. ]
  711. }
  712. function selectedDocumentsSql(): { sql: string } {
  713. return {
  714. sql: `WITH candidates AS (
  715. SELECT
  716. pd.session_id AS session_id,
  717. ps.version AS version,
  718. ps.created_at AS created_at,
  719. ps.cwd AS cwd,
  720. ps.parent_session AS parent_session,
  721. ps.seed_length AS seed_length,
  722. ps.delegation_depth AS delegation_depth,
  723. ps.agent_preset AS agent_preset,
  724. 0 AS live,
  725. 1 AS persisted,
  726. CAST(pd.seq AS INTEGER) AS seq,
  727. pd.type AS type,
  728. CAST(pd.time AS INTEGER) AS time,
  729. pd.surface AS surface,
  730. highlight(persisted_docs, 0, ?, ?) AS marked_text,
  731. CAST(pd.codepoint_length AS INTEGER) AS document_length
  732. FROM persisted_docs AS pd
  733. JOIN persisted_sessions AS ps ON ps.id = pd.session_id
  734. WHERE persisted_docs MATCH ?
  735. AND ? = 1
  736. AND NOT EXISTS (SELECT 1 FROM temp.live_sessions AS ls WHERE ls.id = pd.session_id)
  737. UNION ALL
  738. SELECT
  739. ld.session_id AS session_id,
  740. ls.version AS version,
  741. ls.created_at AS created_at,
  742. ls.cwd AS cwd,
  743. ls.parent_session AS parent_session,
  744. ls.seed_length AS seed_length,
  745. ls.delegation_depth AS delegation_depth,
  746. ls.agent_preset AS agent_preset,
  747. 1 AS live,
  748. CASE WHEN ? = 1 THEN ls.persisted ELSE 0 END AS persisted,
  749. CAST(ld.seq AS INTEGER) AS seq,
  750. ld.type AS type,
  751. CAST(ld.time AS INTEGER) AS time,
  752. ld.surface AS surface,
  753. highlight(live_docs, 0, ?, ?) AS marked_text,
  754. CAST(ld.codepoint_length AS INTEGER) AS document_length
  755. FROM temp.live_docs AS ld
  756. JOIN temp.live_sessions AS ls ON ls.id = ld.session_id
  757. WHERE live_docs MATCH ?
  758. ), matched AS (
  759. SELECT *,
  760. (
  761. length(CAST(marked_text AS BLOB))
  762. - length(CAST(replace(marked_text, ?, '') AS BLOB))
  763. ) / ? AS match_count
  764. FROM candidates
  765. )`,
  766. }
  767. }
  768. function selectedDocumentsParams(query: string, persistenceVisible: boolean): Array<string | number> {
  769. const expression = quoteFtsData(query)
  770. const visible = persistenceVisible ? 1 : 0
  771. return [
  772. FTS_HIGHLIGHT_START,
  773. FTS_HIGHLIGHT_END,
  774. expression,
  775. visible,
  776. visible,
  777. FTS_HIGHLIGHT_START,
  778. FTS_HIGHLIGHT_END,
  779. expression,
  780. FTS_HIGHLIGHT_START,
  781. Buffer.byteLength(FTS_HIGHLIGHT_START, 'utf8'),
  782. ]
  783. }
  784. function observeLive(session: Session): ObservedSession {
  785. return observeSession(session.header, session.events)
  786. }
  787. function observeSession(header: SessionHeader, events: readonly SessionEvent[]): ObservedSession {
  788. const detachedHeader = structuredClone(header)
  789. const detachedEvents = events.map(event => structuredClone(event))
  790. return {
  791. header: detachedHeader,
  792. documents: buildSessionEventSearchDocuments(detachedHeader.id, detachedEvents),
  793. fingerprint: createHash('sha256')
  794. .update(JSON.stringify({ header: detachedHeader, events: detachedEvents }))
  795. .digest('base64url'),
  796. }
  797. }
  798. function materializePersistenceSnapshots(
  799. snapshots: readonly SessionPersistenceSnapshot[],
  800. ): Map<SessionId, ObservedPersistedSession> {
  801. if (!isRuntimeArray(snapshots)) throw new Error('persistence snapshots must be an array')
  802. const result = new Map<SessionId, ObservedPersistedSession>()
  803. for (const snapshot of snapshots) {
  804. if (typeof snapshot.revision !== 'string') {
  805. throw new Error('persistence snapshot revision must be a string')
  806. }
  807. const header = structuredClone(snapshot.header)
  808. if (result.has(header.id)) {
  809. throw new Error(`persistence listed duplicate session "${header.id}"`)
  810. }
  811. result.set(header.id, { header, revision: snapshot.revision })
  812. }
  813. return result
  814. }
  815. function samePersistenceSnapshots(
  816. before: ReadonlyMap<SessionId, ObservedPersistedSession>,
  817. after: ReadonlyMap<SessionId, ObservedPersistedSession>,
  818. ): boolean {
  819. if (before.size !== after.size) return false
  820. for (const [id, first] of before) {
  821. const second = after.get(id)
  822. if (
  823. second === undefined
  824. || first.revision !== second.revision
  825. || !sameHeader(first.header, second.header)
  826. ) return false
  827. }
  828. return true
  829. }
  830. function sameSessionIds(
  831. before: ReadonlySet<SessionId>,
  832. after: ReadonlyMap<SessionId, ObservedSession>,
  833. ): boolean {
  834. if (before.size !== after.size) return false
  835. for (const id of before) {
  836. if (!after.has(id)) return false
  837. }
  838. return true
  839. }
  840. function sameHeader(a: SessionHeader, b: SessionHeader): boolean {
  841. return a.version === b.version
  842. && a.id === b.id
  843. && a.createdAt === b.createdAt
  844. && a.cwd === b.cwd
  845. && a.parentSession === b.parentSession
  846. && a.seedLength === b.seedLength
  847. && (a.delegationDepth ?? 0) === (b.delegationDepth ?? 0)
  848. && a.agentPreset === b.agentPreset
  849. }
  850. function rowHeader(row: SessionHeaderRow): SessionHeader {
  851. return {
  852. version: row.version,
  853. id: row.session_id as SessionId,
  854. createdAt: row.created_at,
  855. ...row.cwd === null ? {} : { cwd: row.cwd },
  856. ...row.parent_session === null ? {} : { parentSession: row.parent_session as SessionId },
  857. ...row.seed_length === null ? {} : { seedLength: row.seed_length },
  858. ...row.delegation_depth === null ? {} : { delegationDepth: row.delegation_depth },
  859. ...row.agent_preset === null ? {} : { agentPreset: row.agent_preset },
  860. }
  861. }
  862. function page<Row, Item>(
  863. rows: readonly Row[],
  864. limit: number,
  865. convert: (row: Row) => Item,
  866. nextCursor: (offset: number) => SessionSearchCursorValue,
  867. offset: number,
  868. ): SessionSearchPage<Item> {
  869. const hasMore = rows.length > limit
  870. return {
  871. items: rows.slice(0, limit).map(convert),
  872. ...hasMore ? { nextCursor: nextCursor(offset + limit) } : {},
  873. }
  874. }
  875. function encodeCursor(payload: CursorPayload): SessionSearchCursorValue {
  876. return SessionSearchCursor(Buffer.from(JSON.stringify(payload), 'utf8').toString('base64url'))
  877. }
  878. function decodeCursor(
  879. cursor: SessionSearchCursorValue,
  880. instance: string,
  881. scope: CursorPayload['scope'],
  882. fingerprint: string,
  883. generation: string,
  884. ): number {
  885. let decoded: Partial<CursorPayload>
  886. try {
  887. decoded = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8')) as Partial<CursorPayload>
  888. } catch (error: unknown) {
  889. throw invalidCursor(error)
  890. }
  891. if (
  892. decoded.version !== 1
  893. || decoded.instance !== instance
  894. || decoded.scope !== scope
  895. || decoded.fingerprint !== fingerprint
  896. || !Number.isSafeInteger(decoded.offset)
  897. || decoded.offset === undefined
  898. || decoded.offset < 0
  899. ) {
  900. throw invalidCursor(new Error('cursor does not belong to this normalized request'))
  901. }
  902. if (decoded.generation !== generation) {
  903. throw new SessionQueryError(
  904. 'session-search cursor is stale because its relevant corpus changed',
  905. 'SESSION_QUERY_STALE_CURSOR',
  906. )
  907. }
  908. return decoded.offset
  909. }
  910. function invalidCursor(cause: unknown): SessionQueryError {
  911. return new SessionQueryError(
  912. 'session-search cursor is invalid',
  913. 'SESSION_QUERY_INVALID_CURSOR',
  914. { cause },
  915. )
  916. }
  917. function resolveConfig(config: Config): ResolvedConfig {
  918. const resolved: ResolvedConfig = {
  919. path: config.path,
  920. openAt: config.openAt ?? 'startup',
  921. journalMode: config.journalMode ?? 'wal',
  922. defaultLimit: config.defaultLimit ?? SESSION_QUERY_SQLITE_DEFAULT_LIMIT,
  923. maxLimit: config.maxLimit ?? SESSION_QUERY_SQLITE_MAX_LIMIT,
  924. snippetChars: config.snippetChars ?? SESSION_QUERY_SQLITE_SNIPPET_CHARS,
  925. readWindowMax: config.readWindowMax ?? SESSION_QUERY_READ_WINDOW_MAX,
  926. persistedInspectConcurrency: config.persistedInspectConcurrency
  927. ?? SESSION_QUERY_DEFAULT_PERSISTED_INSPECT_CONCURRENCY,
  928. }
  929. if (typeof resolved.path !== 'string' || resolved.path.trim().length === 0) {
  930. throw invalidConfig('path must not be blank')
  931. }
  932. const openPhases: readonly string[] = ['startup', 'first-search']
  933. if (!openPhases.includes(resolved.openAt)) throw invalidConfig('openAt is not supported')
  934. assertPageLimit('defaultLimit', resolved.defaultLimit)
  935. assertPageLimit('maxLimit', resolved.maxLimit)
  936. assertPositiveInteger('snippetChars', resolved.snippetChars)
  937. if (!Number.isInteger(resolved.readWindowMax) || resolved.readWindowMax < 0) {
  938. throw invalidConfig('readWindowMax must be a non-negative integer')
  939. }
  940. if (
  941. !Number.isSafeInteger(resolved.persistedInspectConcurrency)
  942. || resolved.persistedInspectConcurrency < 1
  943. ) {
  944. throw invalidConfig('persistedInspectConcurrency must be a positive safe integer')
  945. }
  946. if (resolved.defaultLimit > resolved.maxLimit) {
  947. throw invalidConfig('defaultLimit must be less than or equal to maxLimit')
  948. }
  949. const journalModes: readonly string[] = ['wal', 'delete', 'truncate', 'persist']
  950. if (!journalModes.includes(resolved.journalMode)) throw invalidConfig('journalMode is not supported')
  951. return resolved
  952. }
  953. function assertPositiveInteger(name: string, value: number): void {
  954. if (!Number.isInteger(value) || value < 1) throw invalidConfig(`${name} must be a positive integer`)
  955. }
  956. function assertPageLimit(name: string, value: number): void {
  957. if (!Number.isSafeInteger(value) || value < 1 || value > SQLITE_MAX_PAGE_LIMIT) {
  958. throw invalidConfig(`${name} must be an integer between 1 and ${SQLITE_MAX_PAGE_LIMIT}`)
  959. }
  960. }
  961. function invalidConfig(detail: string): SessionQueryError {
  962. return new SessionQueryError(
  963. `session-search SQLite config: ${detail}`,
  964. 'SESSION_QUERY_INVALID_CONFIG',
  965. )
  966. }
  967. function indexClosed(): SessionQueryError {
  968. return new SessionQueryError('session-search SQLite index is closed', 'SESSION_QUERY_INDEX_FAILED')
  969. }
  970. function assertNotAborted(signal: AbortSignal | undefined): void {
  971. if (signal?.aborted) {
  972. throw new SessionQueryError('session-search aborted', 'SESSION_QUERY_ABORTED')
  973. }
  974. }
  975. function waitWithAbort<T>(promise: Promise<T>, signal: AbortSignal | undefined): Promise<T> {
  976. if (signal === undefined) return promise
  977. if (signal.aborted) return Promise.reject(new SessionQueryError('session-search aborted', 'SESSION_QUERY_ABORTED'))
  978. return new Promise<T>((resolve, reject) => {
  979. const onAbort = () => {
  980. reject(new SessionQueryError('session-search aborted', 'SESSION_QUERY_ABORTED'))
  981. }
  982. signal.addEventListener('abort', onAbort, { once: true })
  983. promise.then(
  984. (value) => {
  985. signal.removeEventListener('abort', onAbort)
  986. resolve(value)
  987. },
  988. (error: unknown) => {
  989. signal.removeEventListener('abort', onAbort)
  990. reject(asError(error))
  991. },
  992. )
  993. })
  994. }
  995. function isAbort(error: unknown): boolean {
  996. return error instanceof SessionQueryError && error.code === 'SESSION_QUERY_ABORTED'
  997. }
  998. function asError(error: unknown): Error {
  999. return error instanceof Error
  1000. ? error
  1001. : new Error('session-search dependency rejected with a non-Error value', { cause: error })
  1002. }
  1003. function errorMessage(error: unknown): string {
  1004. return error instanceof Error ? error.message : 'unknown error'
  1005. }
  1006. function isRuntimeArray(value: unknown): boolean {
  1007. return Array.isArray(value)
  1008. }
  1009. export default SessionQuerySqlite