index.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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 'cordis'
  9. import z from '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 '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. entry.header.id,
  521. entry.header.version,
  522. entry.header.createdAt,
  523. entry.header.cwd ?? null,
  524. entry.header.parentSession ?? null,
  525. entry.header.seedLength ?? null,
  526. entry.header.delegationDepth ?? null,
  527. entry.header.agentPreset ?? null,
  528. revision,
  529. generation,
  530. )
  531. const insert = db.prepare(`
  532. INSERT INTO persisted_docs (text, session_id, seq, type, time, surface, codepoint_length)
  533. VALUES (?, ?, ?, ?, ?, ?, ?)
  534. `)
  535. for (const document of entry.documents) {
  536. const text = sanitizeFtsText(document.text)
  537. insert.run(
  538. text,
  539. document.sessionId,
  540. document.seq,
  541. document.type,
  542. document.time,
  543. document.surface,
  544. Array.from(text).length,
  545. )
  546. }
  547. }
  548. private _replaceLiveSession(entry: ObservedSession, generation: number, persisted: boolean): void {
  549. this._deleteSession('live', entry.header.id)
  550. const db = this._requireDb()
  551. db.prepare(`
  552. INSERT INTO temp.live_sessions
  553. (id, version, created_at, cwd, parent_session, seed_length, delegation_depth, agent_preset, fingerprint, persisted, generation)
  554. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  555. `).run(
  556. entry.header.id,
  557. entry.header.version,
  558. entry.header.createdAt,
  559. entry.header.cwd ?? null,
  560. entry.header.parentSession ?? null,
  561. entry.header.seedLength ?? null,
  562. entry.header.delegationDepth ?? null,
  563. entry.header.agentPreset ?? null,
  564. entry.fingerprint,
  565. persisted ? 1 : 0,
  566. generation,
  567. )
  568. const insert = db.prepare(`
  569. INSERT INTO temp.live_docs (text, session_id, seq, type, time, surface, codepoint_length)
  570. VALUES (?, ?, ?, ?, ?, ?, ?)
  571. `)
  572. for (const document of entry.documents) {
  573. const text = sanitizeFtsText(document.text)
  574. insert.run(
  575. text,
  576. document.sessionId,
  577. document.seq,
  578. document.type,
  579. document.time,
  580. document.surface,
  581. Array.from(text).length,
  582. )
  583. }
  584. }
  585. private _querySessions(
  586. request: NormalizedSessionRequest,
  587. offset: number,
  588. persistenceBinding: PersistenceBinding,
  589. ): SearchRow[] {
  590. const selected = selectedDocumentsSql()
  591. const sessionWhere = buildSessionWhere(request.sessionFilters)
  592. const eventWhere = buildEventWhere(request.eventFilters)
  593. assertFts5OuterPredicateCount(sessionWhere.predicateCount + eventWhere.predicateCount)
  594. const where = [sessionWhere.sql, eventWhere.sql].filter(Boolean).join(' AND ')
  595. const bindings = [
  596. ...selectedDocumentsParams(request.query, persistenceBinding.service !== undefined),
  597. ...sessionWhere.params,
  598. ...eventWhere.params,
  599. request.limit + 1,
  600. offset,
  601. ]
  602. assertPortableBindingCount(bindings.length)
  603. // The browser fixture mirrors these rank keys in
  604. // `packages/client/connection/src/client/fixture.ts`; update both together.
  605. return this._requireDb().prepare(`
  606. ${selected.sql},
  607. filtered AS (
  608. SELECT * FROM matched ${where.length === 0 ? '' : `WHERE ${where}`}
  609. ),
  610. ranked AS (
  611. SELECT *, ROW_NUMBER() OVER (
  612. PARTITION BY session_id
  613. ORDER BY match_count DESC, document_length ASC, time DESC, seq DESC
  614. ) AS event_rank
  615. FROM filtered
  616. )
  617. SELECT * FROM ranked
  618. WHERE event_rank = 1
  619. ORDER BY match_count DESC, document_length ASC, time DESC, session_id ASC, seq DESC
  620. LIMIT ? OFFSET ?
  621. `).all(...bindings) as unknown as SearchRow[]
  622. }
  623. private _queryEvents(
  624. request: NormalizedEventRequest,
  625. offset: number,
  626. persistenceBinding: PersistenceBinding,
  627. ): SearchRow[] {
  628. const selected = selectedDocumentsSql()
  629. const eventWhere = buildEventWhere(request.filters)
  630. assertFts5OuterPredicateCount(1 + eventWhere.predicateCount)
  631. const where = ['session_id = ?', eventWhere.sql].filter(Boolean).join(' AND ')
  632. const bindings = [
  633. ...selectedDocumentsParams(request.query, persistenceBinding.service !== undefined),
  634. request.sessionId,
  635. ...eventWhere.params,
  636. request.limit + 1,
  637. offset,
  638. ]
  639. assertPortableBindingCount(bindings.length)
  640. return this._requireDb().prepare(`
  641. ${selected.sql}
  642. SELECT * FROM matched
  643. WHERE ${where}
  644. ORDER BY match_count DESC, document_length ASC, time DESC, seq DESC
  645. LIMIT ? OFFSET ?
  646. `).all(...bindings) as unknown as SearchRow[]
  647. }
  648. private _targetObservation(
  649. sessionId: SessionId,
  650. persistenceBinding: PersistenceBinding,
  651. ): { header: SessionHeader; generation: string } {
  652. const db = this._requireDb()
  653. const live = db.prepare(
  654. `SELECT
  655. id AS session_id, version, created_at, cwd, parent_session, seed_length, delegation_depth, agent_preset, generation
  656. FROM temp.live_sessions
  657. WHERE id = ?`,
  658. ).get(sessionId) as (SessionHeaderRow & { generation: number }) | undefined
  659. if (live !== undefined) {
  660. return { header: rowHeader(live), generation: `live:${live.generation}` }
  661. }
  662. if (persistenceBinding.service !== undefined) {
  663. const persisted = db.prepare(
  664. `SELECT
  665. id AS session_id, version, created_at, cwd, parent_session, seed_length, delegation_depth, agent_preset, generation
  666. FROM persisted_sessions
  667. WHERE id = ?`,
  668. ).get(sessionId) as (SessionHeaderRow & { generation: number }) | undefined
  669. if (persisted !== undefined) {
  670. return {
  671. header: rowHeader(persisted),
  672. generation: `persisted:${this._persistenceEpoch}:${persisted.generation}`,
  673. }
  674. }
  675. }
  676. throw new SessionQueryError(
  677. `session "${sessionId}" not found`,
  678. 'SESSION_QUERY_SESSION_NOT_FOUND',
  679. )
  680. }
  681. private _sessionHit(row: SearchRow): SessionSearchHit {
  682. return {
  683. header: rowHeader(row),
  684. live: row.live === 1,
  685. persisted: row.persisted === 1,
  686. bestMatch: this._eventHit(row),
  687. }
  688. }
  689. private _eventHit(row: SearchRow): SessionEventSearchHit {
  690. return {
  691. sessionId: row.session_id as SessionId,
  692. seq: row.seq,
  693. type: row.type as SessionEventSearchHit['type'],
  694. time: row.time,
  695. surface: row.surface as SessionEventSearchHit['surface'],
  696. snippet: makeSnippet(row.marked_text, this.config.snippetChars),
  697. }
  698. }
  699. private _requireDb(): DatabaseSync {
  700. /* v8 ignore next -- callers await `_ready`; this guards lifecycle misuse */
  701. if (this._db === undefined) throw indexClosed()
  702. return this._db
  703. }
  704. private _isClosed(): boolean {
  705. return this._closed
  706. }
  707. }
  708. function selectedDocumentsSql(): { sql: string } {
  709. return {
  710. sql: `WITH candidates AS (
  711. SELECT
  712. pd.session_id AS session_id,
  713. ps.version AS version,
  714. ps.created_at AS created_at,
  715. ps.cwd AS cwd,
  716. ps.parent_session AS parent_session,
  717. ps.seed_length AS seed_length,
  718. ps.delegation_depth AS delegation_depth,
  719. ps.agent_preset AS agent_preset,
  720. 0 AS live,
  721. 1 AS persisted,
  722. CAST(pd.seq AS INTEGER) AS seq,
  723. pd.type AS type,
  724. CAST(pd.time AS INTEGER) AS time,
  725. pd.surface AS surface,
  726. highlight(persisted_docs, 0, ?, ?) AS marked_text,
  727. CAST(pd.codepoint_length AS INTEGER) AS document_length
  728. FROM persisted_docs AS pd
  729. JOIN persisted_sessions AS ps ON ps.id = pd.session_id
  730. WHERE persisted_docs MATCH ?
  731. AND ? = 1
  732. AND NOT EXISTS (SELECT 1 FROM temp.live_sessions AS ls WHERE ls.id = pd.session_id)
  733. UNION ALL
  734. SELECT
  735. ld.session_id AS session_id,
  736. ls.version AS version,
  737. ls.created_at AS created_at,
  738. ls.cwd AS cwd,
  739. ls.parent_session AS parent_session,
  740. ls.seed_length AS seed_length,
  741. ls.delegation_depth AS delegation_depth,
  742. ls.agent_preset AS agent_preset,
  743. 1 AS live,
  744. CASE WHEN ? = 1 THEN ls.persisted ELSE 0 END AS persisted,
  745. CAST(ld.seq AS INTEGER) AS seq,
  746. ld.type AS type,
  747. CAST(ld.time AS INTEGER) AS time,
  748. ld.surface AS surface,
  749. highlight(live_docs, 0, ?, ?) AS marked_text,
  750. CAST(ld.codepoint_length AS INTEGER) AS document_length
  751. FROM temp.live_docs AS ld
  752. JOIN temp.live_sessions AS ls ON ls.id = ld.session_id
  753. WHERE live_docs MATCH ?
  754. ), matched AS (
  755. SELECT *,
  756. (
  757. length(CAST(marked_text AS BLOB))
  758. - length(CAST(replace(marked_text, ?, '') AS BLOB))
  759. ) / ? AS match_count
  760. FROM candidates
  761. )`,
  762. }
  763. }
  764. function selectedDocumentsParams(query: string, persistenceVisible: boolean): Array<string | number> {
  765. const expression = quoteFtsData(query)
  766. const visible = persistenceVisible ? 1 : 0
  767. return [
  768. FTS_HIGHLIGHT_START,
  769. FTS_HIGHLIGHT_END,
  770. expression,
  771. visible,
  772. visible,
  773. FTS_HIGHLIGHT_START,
  774. FTS_HIGHLIGHT_END,
  775. expression,
  776. FTS_HIGHLIGHT_START,
  777. Buffer.byteLength(FTS_HIGHLIGHT_START, 'utf8'),
  778. ]
  779. }
  780. function observeLive(session: Session): ObservedSession {
  781. return observeSession(session.header, session.events)
  782. }
  783. function observeSession(header: SessionHeader, events: readonly SessionEvent[]): ObservedSession {
  784. const detachedHeader = structuredClone(header)
  785. const detachedEvents = events.map(event => structuredClone(event))
  786. return {
  787. header: detachedHeader,
  788. documents: buildSessionEventSearchDocuments(detachedHeader.id, detachedEvents),
  789. fingerprint: createHash('sha256')
  790. .update(JSON.stringify({ header: detachedHeader, events: detachedEvents }))
  791. .digest('base64url'),
  792. }
  793. }
  794. function materializePersistenceSnapshots(
  795. snapshots: readonly SessionPersistenceSnapshot[],
  796. ): Map<SessionId, ObservedPersistedSession> {
  797. if (!isRuntimeArray(snapshots)) throw new Error('persistence snapshots must be an array')
  798. const result = new Map<SessionId, ObservedPersistedSession>()
  799. for (const snapshot of snapshots) {
  800. if (typeof snapshot.revision !== 'string') {
  801. throw new Error('persistence snapshot revision must be a string')
  802. }
  803. const header = structuredClone(snapshot.header)
  804. if (result.has(header.id)) {
  805. throw new Error(`persistence listed duplicate session "${header.id}"`)
  806. }
  807. result.set(header.id, { header, revision: snapshot.revision })
  808. }
  809. return result
  810. }
  811. function samePersistenceSnapshots(
  812. before: ReadonlyMap<SessionId, ObservedPersistedSession>,
  813. after: ReadonlyMap<SessionId, ObservedPersistedSession>,
  814. ): boolean {
  815. if (before.size !== after.size) return false
  816. for (const [id, first] of before) {
  817. const second = after.get(id)
  818. if (
  819. second === undefined
  820. || first.revision !== second.revision
  821. || !sameHeader(first.header, second.header)
  822. ) return false
  823. }
  824. return true
  825. }
  826. function sameSessionIds(
  827. before: ReadonlySet<SessionId>,
  828. after: ReadonlyMap<SessionId, ObservedSession>,
  829. ): boolean {
  830. if (before.size !== after.size) return false
  831. for (const id of before) {
  832. if (!after.has(id)) return false
  833. }
  834. return true
  835. }
  836. function sameHeader(a: SessionHeader, b: SessionHeader): boolean {
  837. return a.version === b.version
  838. && a.id === b.id
  839. && a.createdAt === b.createdAt
  840. && a.cwd === b.cwd
  841. && a.parentSession === b.parentSession
  842. && a.seedLength === b.seedLength
  843. && (a.delegationDepth ?? 0) === (b.delegationDepth ?? 0)
  844. && a.agentPreset === b.agentPreset
  845. }
  846. function rowHeader(row: SessionHeaderRow): SessionHeader {
  847. return {
  848. version: row.version,
  849. id: row.session_id as SessionId,
  850. createdAt: row.created_at,
  851. ...row.cwd === null ? {} : { cwd: row.cwd },
  852. ...row.parent_session === null ? {} : { parentSession: row.parent_session as SessionId },
  853. ...row.seed_length === null ? {} : { seedLength: row.seed_length },
  854. ...row.delegation_depth === null ? {} : { delegationDepth: row.delegation_depth },
  855. ...row.agent_preset === null ? {} : { agentPreset: row.agent_preset },
  856. }
  857. }
  858. function page<Row, Item>(
  859. rows: readonly Row[],
  860. limit: number,
  861. convert: (row: Row) => Item,
  862. nextCursor: (offset: number) => SessionSearchCursorValue,
  863. offset: number,
  864. ): SessionSearchPage<Item> {
  865. const hasMore = rows.length > limit
  866. return {
  867. items: rows.slice(0, limit).map(convert),
  868. ...hasMore ? { nextCursor: nextCursor(offset + limit) } : {},
  869. }
  870. }
  871. function encodeCursor(payload: CursorPayload): SessionSearchCursorValue {
  872. return SessionSearchCursor(Buffer.from(JSON.stringify(payload), 'utf8').toString('base64url'))
  873. }
  874. function decodeCursor(
  875. cursor: SessionSearchCursorValue,
  876. instance: string,
  877. scope: CursorPayload['scope'],
  878. fingerprint: string,
  879. generation: string,
  880. ): number {
  881. let decoded: Partial<CursorPayload>
  882. try {
  883. decoded = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8')) as Partial<CursorPayload>
  884. } catch (error: unknown) {
  885. throw invalidCursor(error)
  886. }
  887. if (
  888. decoded.version !== 1
  889. || decoded.instance !== instance
  890. || decoded.scope !== scope
  891. || decoded.fingerprint !== fingerprint
  892. || !Number.isSafeInteger(decoded.offset)
  893. || decoded.offset === undefined
  894. || decoded.offset < 0
  895. ) {
  896. throw invalidCursor(new Error('cursor does not belong to this normalized request'))
  897. }
  898. if (decoded.generation !== generation) {
  899. throw new SessionQueryError(
  900. 'session-search cursor is stale because its relevant corpus changed',
  901. 'SESSION_QUERY_STALE_CURSOR',
  902. )
  903. }
  904. return decoded.offset
  905. }
  906. function invalidCursor(cause: unknown): SessionQueryError {
  907. return new SessionQueryError(
  908. 'session-search cursor is invalid',
  909. 'SESSION_QUERY_INVALID_CURSOR',
  910. { cause },
  911. )
  912. }
  913. function resolveConfig(config: Config): ResolvedConfig {
  914. const resolved: ResolvedConfig = {
  915. path: config.path,
  916. openAt: config.openAt ?? 'startup',
  917. journalMode: config.journalMode ?? 'wal',
  918. defaultLimit: config.defaultLimit ?? SESSION_QUERY_SQLITE_DEFAULT_LIMIT,
  919. maxLimit: config.maxLimit ?? SESSION_QUERY_SQLITE_MAX_LIMIT,
  920. snippetChars: config.snippetChars ?? SESSION_QUERY_SQLITE_SNIPPET_CHARS,
  921. readWindowMax: config.readWindowMax ?? SESSION_QUERY_READ_WINDOW_MAX,
  922. persistedInspectConcurrency: config.persistedInspectConcurrency
  923. ?? SESSION_QUERY_DEFAULT_PERSISTED_INSPECT_CONCURRENCY,
  924. }
  925. if (typeof resolved.path !== 'string' || resolved.path.trim().length === 0) {
  926. throw invalidConfig('path must not be blank')
  927. }
  928. const openPhases: readonly string[] = ['startup', 'first-search']
  929. if (!openPhases.includes(resolved.openAt)) throw invalidConfig('openAt is not supported')
  930. assertPageLimit('defaultLimit', resolved.defaultLimit)
  931. assertPageLimit('maxLimit', resolved.maxLimit)
  932. assertPositiveInteger('snippetChars', resolved.snippetChars)
  933. if (!Number.isInteger(resolved.readWindowMax) || resolved.readWindowMax < 0) {
  934. throw invalidConfig('readWindowMax must be a non-negative integer')
  935. }
  936. if (
  937. !Number.isSafeInteger(resolved.persistedInspectConcurrency)
  938. || resolved.persistedInspectConcurrency < 1
  939. ) {
  940. throw invalidConfig('persistedInspectConcurrency must be a positive safe integer')
  941. }
  942. if (resolved.defaultLimit > resolved.maxLimit) {
  943. throw invalidConfig('defaultLimit must be less than or equal to maxLimit')
  944. }
  945. const journalModes: readonly string[] = ['wal', 'delete', 'truncate', 'persist']
  946. if (!journalModes.includes(resolved.journalMode)) throw invalidConfig('journalMode is not supported')
  947. return resolved
  948. }
  949. function assertPositiveInteger(name: string, value: number): void {
  950. if (!Number.isInteger(value) || value < 1) throw invalidConfig(`${name} must be a positive integer`)
  951. }
  952. function assertPageLimit(name: string, value: number): void {
  953. if (!Number.isSafeInteger(value) || value < 1 || value > SQLITE_MAX_PAGE_LIMIT) {
  954. throw invalidConfig(`${name} must be an integer between 1 and ${SQLITE_MAX_PAGE_LIMIT}`)
  955. }
  956. }
  957. function invalidConfig(detail: string): SessionQueryError {
  958. return new SessionQueryError(
  959. `session-search SQLite config: ${detail}`,
  960. 'SESSION_QUERY_INVALID_CONFIG',
  961. )
  962. }
  963. function indexClosed(): SessionQueryError {
  964. return new SessionQueryError('session-search SQLite index is closed', 'SESSION_QUERY_INDEX_FAILED')
  965. }
  966. function assertNotAborted(signal: AbortSignal | undefined): void {
  967. if (signal?.aborted) {
  968. throw new SessionQueryError('session-search aborted', 'SESSION_QUERY_ABORTED')
  969. }
  970. }
  971. function waitWithAbort<T>(promise: Promise<T>, signal: AbortSignal | undefined): Promise<T> {
  972. if (signal === undefined) return promise
  973. if (signal.aborted) return Promise.reject(new SessionQueryError('session-search aborted', 'SESSION_QUERY_ABORTED'))
  974. return new Promise<T>((resolve, reject) => {
  975. const onAbort = () => {
  976. reject(new SessionQueryError('session-search aborted', 'SESSION_QUERY_ABORTED'))
  977. }
  978. signal.addEventListener('abort', onAbort, { once: true })
  979. promise.then(
  980. (value) => {
  981. signal.removeEventListener('abort', onAbort)
  982. resolve(value)
  983. },
  984. (error: unknown) => {
  985. signal.removeEventListener('abort', onAbort)
  986. reject(asError(error))
  987. },
  988. )
  989. })
  990. }
  991. function isAbort(error: unknown): boolean {
  992. return error instanceof SessionQueryError && error.code === 'SESSION_QUERY_ABORTED'
  993. }
  994. function asError(error: unknown): Error {
  995. return error instanceof Error
  996. ? error
  997. : new Error('session-search dependency rejected with a non-Error value', { cause: error })
  998. }
  999. function errorMessage(error: unknown): string {
  1000. return error instanceof Error ? error.message : 'unknown error'
  1001. }
  1002. function isRuntimeArray(value: unknown): boolean {
  1003. return Array.isArray(value)
  1004. }
  1005. export default SessionQuerySqlite