advisers 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/sqlite.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { DatabaseSync } from 'node:sqlite'
2
- import { chmodSync, copyFileSync } from 'node:fs'
2
+ import { chmodSync, closeSync, copyFileSync, fsyncSync, mkdirSync, openSync, readFileSync, rmSync, statSync, unlinkSync, utimesSync, writeSync } from 'node:fs'
3
3
  import { createHash, createHmac, randomUUID } from 'node:crypto'
4
+ import { dirname, join } from 'node:path'
4
5
 
5
6
  /**
6
7
  * Open + write hardening (2026-11-17 robustness audit P0-1/P0-3/P1-2):
@@ -17,7 +18,7 @@ import { createHash, createHmac, randomUUID } from 'node:crypto'
17
18
  * - Migrations are refused for a schema newer than this CLI and are preceded
18
19
  * by a same-directory backup of the old database.
19
20
  */
20
- export const SCHEMA_VERSION = 18
21
+ export const SCHEMA_VERSION = 19
21
22
  const DEFAULT_BUSY_TIMEOUT_MS = 5000
22
23
  const OPEN_RETRIES = 6
23
24
  const BUSY_ERRCODES = new Set([5, 6]) // SQLITE_BUSY, SQLITE_LOCKED
@@ -42,6 +43,310 @@ function backoffMs(attempt) {
42
43
  return Math.min(2000, 50 * 2 ** attempt)
43
44
  }
44
45
 
46
+ // ---------------------------------------------------------------------------
47
+ // Cross-process single-writer instance lock (0.2.1 robustness audit P0-1).
48
+ //
49
+ // The ledger snapshot save() path (see _saveRows below) writes whole rows, so
50
+ // two ordinary CLI commands that both load() and then save() can overwrite each
51
+ // other's committed updates (and resurrect soft-deleted rows) even while the
52
+ // SQLite page-level writer serialization keeps the file structurally intact.
53
+ // The audit's recommended first fix is one writer per HOME: every process that
54
+ // opens the SQLite store takes ~/.advisers/instance.lock (O_EXCL) first, waits a
55
+ // bounded time for a live holder, reclaims a provably stale lock, and releases
56
+ // on exit/SIGINT/SIGTERM.
57
+ //
58
+ // Rules that keep the takeover safe:
59
+ // - a lock whose pid is alive is never stolen while its heartbeat is fresh;
60
+ // long-lived holders (ui/mcp/bridge) refresh the lock mtime periodically.
61
+ // - a stale lock is removed only under an O_EXCL reclaim guard directory, so
62
+ // two waiters can never both delete and recreate the lock under each other.
63
+ // - release() only unlinks the lock when its pid + startedAt still match ours.
64
+ // ---------------------------------------------------------------------------
65
+ export const INSTANCE_LOCK_FILE = 'instance.lock'
66
+ const DEFAULT_LOCK_TIMEOUT_MS = 10_000
67
+ const DEFAULT_LOCK_STALE_MS = 60 * 60 * 1000
68
+ const MAX_LOCK_TIMEOUT_MS = 24 * 60 * 60 * 1000
69
+ const MAX_LOCK_STALE_MS = 30 * 24 * 60 * 60 * 1000
70
+ const LOCK_POLL_INTERVAL_MS = 50
71
+ const LOCK_RECLAIM_GUARD_MS = 10_000
72
+
73
+ /** The database exists but cannot be trusted (0 bytes / not a SQLite file). */
74
+ export class DatabaseCorruptError extends Error {
75
+ constructor(path, detail) {
76
+ super(
77
+ `E_DB_CORRUPT: ${path}: ${detail}. Refusing to rebuild or overwrite it. ` +
78
+ 'Back up the file first (do not delete it), then run `advisers doctor`; ' +
79
+ 'restore a known-good copy (state.db.before-*) if you have one.',
80
+ )
81
+ this.name = 'DatabaseCorruptError'
82
+ this.code = 'E_DB_CORRUPT'
83
+ this.path = path
84
+ }
85
+ }
86
+
87
+ /** Another live advisers process holds the HOME's single-writer instance lock. */
88
+ export class DatabaseLockedError extends Error {
89
+ constructor({ lockPath, holder, waitedMs, timeoutMs }) {
90
+ const who = holder
91
+ ? ` (pid ${holder.pid}, started ${holder.startedAt || 'unknown'}${holder.command ? `, "${holder.command}"` : ''})`
92
+ : ''
93
+ super(
94
+ `E_DB_LOCKED: another advisers process is using this home via ${lockPath}${who}; ` +
95
+ `waited ${waitedMs}ms (ADVISERS_LOCK_TIMEOUT_MS=${timeoutMs}). No data was written. ` +
96
+ 'Stop the other process (ui/mcp/bridge servers hold the lock while running) and retry, ' +
97
+ 'or raise ADVISERS_LOCK_TIMEOUT_MS to wait longer. If the holder is gone, remove the stale lock file.',
98
+ )
99
+ this.name = 'DatabaseLockedError'
100
+ this.code = 'E_DB_LOCKED'
101
+ this.lockPath = lockPath
102
+ this.holder = holder
103
+ this.waitedMs = waitedMs
104
+ }
105
+ }
106
+
107
+ function readEnvMs(name, fallback, max) {
108
+ const raw = process.env[name]
109
+ if (raw === undefined || raw === null || String(raw).trim() === '') return fallback
110
+ const value = Number(raw)
111
+ if (!Number.isFinite(value) || value < 0) return fallback
112
+ return Math.min(Math.floor(value), max)
113
+ }
114
+
115
+ /** Wait budget for a contended instance lock (`ADVISERS_LOCK_TIMEOUT_MS`, default 10s). */
116
+ export function instanceLockTimeoutMs() {
117
+ return readEnvMs('ADVISERS_LOCK_TIMEOUT_MS', DEFAULT_LOCK_TIMEOUT_MS, MAX_LOCK_TIMEOUT_MS)
118
+ }
119
+
120
+ /** Age after which an unrefreshed lock may be reclaimed (`ADVISERS_LOCK_STALE_MS`, default 1h). */
121
+ export function instanceLockStaleMs() {
122
+ return readEnvMs('ADVISERS_LOCK_STALE_MS', DEFAULT_LOCK_STALE_MS, MAX_LOCK_STALE_MS)
123
+ }
124
+
125
+ /** Absolute path of the per-HOME instance lock. */
126
+ export function instanceLockPath(home) {
127
+ return join(home, INSTANCE_LOCK_FILE)
128
+ }
129
+
130
+ function isPidAlive(pid) {
131
+ if (!Number.isInteger(pid) || pid <= 0) return false
132
+ try {
133
+ process.kill(pid, 0)
134
+ return true
135
+ } catch (error) {
136
+ // EPERM: the pid exists but belongs to another user; never steal from it.
137
+ return error?.code === 'EPERM'
138
+ }
139
+ }
140
+
141
+ /** Read + stat the instance lock without creating anything; null when absent. */
142
+ export function readInstanceLock(home) {
143
+ const file = instanceLockPath(home)
144
+ let stat
145
+ try {
146
+ stat = statSync(file)
147
+ } catch (error) {
148
+ if (error?.code === 'ENOENT') return null
149
+ throw error
150
+ }
151
+ let record = null
152
+ try {
153
+ const parsed = JSON.parse(readFileSync(file, 'utf8'))
154
+ if (parsed && typeof parsed === 'object' && Number.isFinite(Number(parsed.pid)) && Number(parsed.pid) > 0) {
155
+ record = {
156
+ pid: Math.trunc(Number(parsed.pid)),
157
+ startedAt: typeof parsed.startedAt === 'string' ? parsed.startedAt : '',
158
+ command: typeof parsed.command === 'string' ? parsed.command : '',
159
+ }
160
+ }
161
+ } catch {}
162
+ return { file, record, mtimeMs: stat.mtimeMs, size: stat.size }
163
+ }
164
+
165
+ /**
166
+ * A lock is stale when its owner pid is gone, or when it has not been refreshed
167
+ * (heartbeat / normal short command) for longer than `staleMs`. A live pid with
168
+ * a fresh mtime is never stolen — ui/mcp/bridge holders legitimately keep the
169
+ * lock for hours.
170
+ */
171
+ export function instanceLockIsStale(info, { now = Date.now(), staleMs = instanceLockStaleMs() } = {}) {
172
+ if (!info) return false
173
+ if (info.record && !isPidAlive(info.record.pid)) return true
174
+ return now - info.mtimeMs > staleMs
175
+ }
176
+
177
+ function heartbeatIntervalMs(staleMs) {
178
+ return Math.max(1000, Math.min(5 * 60_000, Math.floor(staleMs / 4)))
179
+ }
180
+
181
+ /** Remove a stale lock while serialising concurrent reclaimers with a tty-less guard dir. */
182
+ function reclaimStaleLock({ path, expected, staleMs, timeoutMs, pollMs }) {
183
+ const guard = path + '.reclaim'
184
+ const guardDeadline = Date.now() + timeoutMs
185
+ for (;;) {
186
+ try {
187
+ mkdirSync(guard, { mode: 0o700 })
188
+ break
189
+ } catch (error) {
190
+ if (error?.code !== 'EEXIST') throw error
191
+ let age = 0
192
+ try { age = Date.now() - statSync(guard).mtimeMs } catch {}
193
+ if (age > LOCK_RECLAIM_GUARD_MS) {
194
+ try { rmSync(guard, { recursive: true, force: true }) } catch {}
195
+ continue
196
+ }
197
+ if (Date.now() >= guardDeadline) return false
198
+ sleepSync(pollMs)
199
+ }
200
+ }
201
+ try {
202
+ const current = readInstanceLock(dirname(path))
203
+ if (!current) return true
204
+ if (!instanceLockIsStale(current, { staleMs })) return false
205
+ // Only delete the exact record we inspected; a new holder may have replaced it.
206
+ const same = expected
207
+ ? current.record && expected.record &&
208
+ current.record.pid === expected.record.pid &&
209
+ current.record.startedAt === expected.record.startedAt
210
+ : !current.record
211
+ if (!same) return false
212
+ try { unlinkSync(path) } catch (error) { if (error?.code !== 'ENOENT') throw error }
213
+ return true
214
+ } finally {
215
+ try { rmSync(guard, { recursive: true, force: true }) } catch {}
216
+ }
217
+ }
218
+
219
+ export class InstanceLock {
220
+ constructor({ home, command = '', timeoutMs = instanceLockTimeoutMs(), staleMs = instanceLockStaleMs(), pollMs = LOCK_POLL_INTERVAL_MS } = {}) {
221
+ this.home = home
222
+ this.path = instanceLockPath(home)
223
+ this.timeoutMs = Math.max(0, Math.floor(Number(timeoutMs) || 0))
224
+ this.staleMs = Math.max(1, Math.floor(Number(staleMs) || DEFAULT_LOCK_STALE_MS))
225
+ this.pollMs = Math.max(5, Math.floor(Number(pollMs) || LOCK_POLL_INTERVAL_MS))
226
+ this.record = { pid: process.pid, startedAt: new Date().toISOString(), command: String(command ?? '').slice(0, 160) }
227
+ this._held = false
228
+ this._heartbeat = null
229
+ }
230
+
231
+ get held() {
232
+ return this._held
233
+ }
234
+
235
+ /** Try once to create the lock file (O_EXCL); returns true on success. */
236
+ _create() {
237
+ mkdirSync(this.home, { recursive: true, mode: 0o700 })
238
+ let fd
239
+ try {
240
+ fd = openSync(this.path, 'wx', 0o600)
241
+ } catch (error) {
242
+ if (error?.code === 'EEXIST') return false
243
+ throw error
244
+ }
245
+ try {
246
+ writeSync(fd, JSON.stringify(this.record) + '\n')
247
+ fsyncSync(fd)
248
+ } catch (error) {
249
+ // Never leave a half-written lock file behind: a reader would see invalid
250
+ // JSON and could only recover by waiting out the (1h) stale threshold.
251
+ try { closeSync(fd) } catch {}
252
+ try { unlinkSync(this.path) } catch {}
253
+ throw error
254
+ }
255
+ closeSync(fd)
256
+ try { chmodSync(this.path, 0o600) } catch {}
257
+ return true
258
+ }
259
+
260
+ acquire() {
261
+ const deadline = Date.now() + this.timeoutMs
262
+ const started = Date.now()
263
+ for (;;) {
264
+ if (this._create()) {
265
+ this._held = true
266
+ this._startHeartbeat()
267
+ return this
268
+ }
269
+ const info = readInstanceLock(this.home)
270
+ if (info && instanceLockIsStale(info, { staleMs: this.staleMs })) {
271
+ if (reclaimStaleLock({ path: this.path, expected: info, staleMs: this.staleMs, timeoutMs: this.timeoutMs, pollMs: this.pollMs })) continue
272
+ }
273
+ const now = Date.now()
274
+ if (now >= deadline) {
275
+ throw new DatabaseLockedError({ lockPath: this.path, holder: info?.record ?? null, waitedMs: now - started, timeoutMs: this.timeoutMs })
276
+ }
277
+ sleepSync(Math.max(1, Math.min(this.pollMs, deadline - now)))
278
+ }
279
+ }
280
+
281
+ /** Refresh the lock mtime so long-lived holders are not age-reclaimed. */
282
+ refresh() {
283
+ if (!this._held) return false
284
+ const info = readInstanceLock(this.home)
285
+ if (!info?.record || info.record.pid !== this.record.pid || info.record.startedAt !== this.record.startedAt) {
286
+ this._stopHeartbeat()
287
+ this._held = false
288
+ return false
289
+ }
290
+ const now = new Date()
291
+ try { utimesSync(this.path, now, now) } catch { this._stopHeartbeat() }
292
+ return true
293
+ }
294
+
295
+ _startHeartbeat() {
296
+ if (this._heartbeat || this.staleMs < 60_000) return
297
+ this._heartbeat = setInterval(() => this.refresh(), heartbeatIntervalMs(this.staleMs))
298
+ if (typeof this._heartbeat.unref === 'function') this._heartbeat.unref()
299
+ }
300
+
301
+ _stopHeartbeat() {
302
+ if (!this._heartbeat) return
303
+ clearInterval(this._heartbeat)
304
+ this._heartbeat = null
305
+ }
306
+
307
+ /** Release the lock; only the exact record this process wrote is removed. */
308
+ release() {
309
+ this._stopHeartbeat()
310
+ if (!this._held) return false
311
+ this._held = false
312
+ let info
313
+ try { info = readInstanceLock(this.home) } catch { return false }
314
+ if (!info?.record) return false
315
+ if (info.record.pid !== this.record.pid || info.record.startedAt !== this.record.startedAt) return false
316
+ try {
317
+ unlinkSync(this.path)
318
+ return true
319
+ } catch (error) {
320
+ return error?.code === 'ENOENT'
321
+ }
322
+ }
323
+ }
324
+
325
+ /** Take the single-writer lock for a HOME; throws E_DB_LOCKED after the wait budget. */
326
+ export function acquireInstanceLock({ home, command = '', timeoutMs, staleMs, pollMs } = {}) {
327
+ if (!home) throw new Error('acquireInstanceLock requires a home directory')
328
+ const lock = new InstanceLock({ home, command, timeoutMs, staleMs, pollMs })
329
+ return lock.acquire()
330
+ }
331
+
332
+ /** Throw E_DB_CORRUPT for an existing 0-byte / non-file database before SQLite touches it. */
333
+ export function assertDatabaseFileUsable(path) {
334
+ if (!path || path === ':memory:') return
335
+ let stat
336
+ try {
337
+ stat = statSync(path)
338
+ } catch (error) {
339
+ if (error?.code === 'ENOENT') return
340
+ throw error
341
+ }
342
+ if (stat.isDirectory()) throw new DatabaseCorruptError(path, 'the path is a directory, not a SQLite database file')
343
+ if (stat.size === 0) throw new DatabaseCorruptError(path, 'the database file is 0 bytes (truncated, or an interrupted first-time initialisation)')
344
+ }
345
+
346
+ function isCorruptSqliteError(error) {
347
+ return /file is not a database|database disk image is malformed|malformed database|not a database/i.test(String(error?.message ?? error))
348
+ }
349
+
45
350
  // Ledger tables are written as a whole snapshot by save(). The id column lets
46
351
  // save() merge with rows other processes committed after our load().
47
352
  const LEDGER_TABLES = [
@@ -153,17 +458,6 @@ CREATE TABLE IF NOT EXISTS session_summaries (
153
458
  conversation_id TEXT PRIMARY KEY, summary TEXT NOT NULL,
154
459
  through_seq INTEGER NOT NULL, updated_at TEXT NOT NULL
155
460
  );
156
- CREATE VIRTUAL TABLE IF NOT EXISTS turns_fts USING fts5(content, content='turns', content_rowid='rowid');
157
- CREATE TRIGGER IF NOT EXISTS turns_ai AFTER INSERT ON turns BEGIN
158
- INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
159
- END;
160
- CREATE TRIGGER IF NOT EXISTS turns_ad AFTER DELETE ON turns BEGIN
161
- INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
162
- END;
163
- CREATE TRIGGER IF NOT EXISTS turns_au AFTER UPDATE ON turns BEGIN
164
- INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
165
- INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
166
- END;
167
461
  CREATE TABLE IF NOT EXISTS summary_versions (
168
462
  conversation_id TEXT NOT NULL, version INTEGER NOT NULL, summary TEXT NOT NULL,
169
463
  through_seq INTEGER NOT NULL, pins TEXT, created_at TEXT NOT NULL,
@@ -186,6 +480,73 @@ CREATE TABLE IF NOT EXISTS calendar_events (
186
480
  CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
187
481
  `
188
482
 
483
+ // ---------------------------------------------------------------------------
484
+ // Full-text search (schema v19; 2026-11-17 performance audit round 2, P2-6).
485
+ //
486
+ // v18 created turns_fts with the default unicode61 tokenizer, which treats a
487
+ // whole run of CJK characters as one token. It indexed the data but could not
488
+ // answer a Chinese query: 'MATCH "报价单"' returned 0 rows, so every Chinese
489
+ // search fell back to 'content LIKE ?' (a full table scan). v19 rebuilds the
490
+ // turn index (and adds an index for session summaries) with the FTS5 trigram
491
+ // tokenizer. Trigram indexes every 3-character substring, so it supports CJK
492
+ // substring search, phrases, and English substring search with the same query
493
+ // path. Queries shorter than 3 characters cannot produce a trigram, so they
494
+ // keep the LIKE path; malformed/special FTS syntax is caught and also falls
495
+ // back to LIKE.
496
+ //
497
+ // The FTS5 schema is created by migrate() rather than the static SCHEMA so the
498
+ // tokenizer can be probed at runtime. If the SQLite build has no trigram
499
+ // tokenizer the store degrades to unicode61 (Chinese keeps working through
500
+ // LIKE); if FTS5 itself is missing it degrades to LIKE-only. Opening never
501
+ // fails because of a missing tokenizer; ftsWarning explains the degradation.
502
+ //
503
+ // ADVISERS_FTS_TOKENIZER=auto|trigram|unicode61 is an operator override:
504
+ // 'unicode61' forces the legacy tokenizer (and the LIKE path for Chinese),
505
+ // which is also how tests exercise the degraded mode deterministically.
506
+ // ---------------------------------------------------------------------------
507
+ const FTS_TOKENIZER_ENV = 'ADVISERS_FTS_TOKENIZER'
508
+ const FTS_TRIGRAM_MIN_QUERY = 3
509
+ const FTS_TABLE_NAMES = ['turns_fts', 'summaries_fts']
510
+ const FTS_TRIGGER_NAMES = ['turns_ai', 'turns_ad', 'turns_au', 'summaries_ai', 'summaries_ad', 'summaries_au']
511
+
512
+ function ftsTokenizerPreference() {
513
+ const raw = String(process.env[FTS_TOKENIZER_ENV] ?? '').trim().toLowerCase()
514
+ return raw === 'trigram' || raw === 'unicode61' ? raw : 'auto'
515
+ }
516
+
517
+ /** DDL for both FTS indexes and their sync triggers for one tokenizer. */
518
+ function ftsDdl(tokenizer) {
519
+ const trigram = tokenizer === 'trigram' ? ", tokenize='trigram'" : ''
520
+ return {
521
+ tables: [
522
+ `CREATE VIRTUAL TABLE turns_fts USING fts5(content, content='turns', content_rowid='rowid'${trigram})`,
523
+ `CREATE VIRTUAL TABLE summaries_fts USING fts5(summary, content='session_summaries', content_rowid='rowid'${trigram})`,
524
+ ],
525
+ triggers: [
526
+ `CREATE TRIGGER turns_ai AFTER INSERT ON turns BEGIN
527
+ INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
528
+ END`,
529
+ `CREATE TRIGGER turns_ad AFTER DELETE ON turns BEGIN
530
+ INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
531
+ END`,
532
+ `CREATE TRIGGER turns_au AFTER UPDATE ON turns BEGIN
533
+ INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
534
+ INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
535
+ END`,
536
+ `CREATE TRIGGER summaries_ai AFTER INSERT ON session_summaries BEGIN
537
+ INSERT INTO summaries_fts(rowid, summary) VALUES (new.rowid, new.summary);
538
+ END`,
539
+ `CREATE TRIGGER summaries_ad AFTER DELETE ON session_summaries BEGIN
540
+ INSERT INTO summaries_fts(summaries_fts, rowid, summary) VALUES ('delete', old.rowid, old.summary);
541
+ END`,
542
+ `CREATE TRIGGER summaries_au AFTER UPDATE ON session_summaries BEGIN
543
+ INSERT INTO summaries_fts(summaries_fts, rowid, summary) VALUES ('delete', old.rowid, old.summary);
544
+ INSERT INTO summaries_fts(rowid, summary) VALUES (new.rowid, new.summary);
545
+ END`,
546
+ ],
547
+ }
548
+ }
549
+
189
550
  /**
190
551
  * P2-1: secondary indexes for the hot read/write paths (tick, today, history,
191
552
  * deliveries). Indexes are layout only, not a schema change, so they are
@@ -216,25 +577,136 @@ const parse = (v, fallback = []) => {
216
577
  try { return JSON.parse(v) } catch { return fallback }
217
578
  }
218
579
 
580
+ // ---------------------------------------------------------------------------
581
+ // Ledger row mappers + table registry (P1-2, 0.2.2).
582
+ //
583
+ // Before this, every ledger command paid for store.load() -- the whole 9-table
584
+ // snapshot -- even when it needed one table or a count: 488ms at a 50k-row
585
+ // ledger (1.2s at 100k) in the 2026-11-17 performance audit round 2. The
586
+ // mappers are shared by load() and the per-table load*/count* helpers so a
587
+ // partial snapshot returns byte-identical row objects to a full snapshot; a
588
+ // partial snapshot is safe to hand to Ledger.fromJSON/fromParts because the
589
+ // core Ledger treats missing tables as empty.
590
+ // ---------------------------------------------------------------------------
591
+ const mapMatterRow = (r) => ({
592
+ id: r.id, title: r.title, status: r.status, domain: r.domain, secrecy: r.secrecy,
593
+ summary: r.summary ?? undefined, participants: parse(r.participants), deadlines: parse(r.deadlines),
594
+ openQuestions: parse(r.open_questions), createdAt: r.created_at, updatedAt: r.updated_at,
595
+ })
596
+
597
+ const mapFactRow = (r) => ({
598
+ id: r.id, matterId: r.matter_id ?? undefined, subject: r.subject, predicate: r.predicate,
599
+ object: r.object, recordedAt: r.recorded_at, expiredAt: r.expired_at ?? undefined,
600
+ authority: r.authority, confidence: r.confidence,
601
+ source: r.source ? parse(r.source, undefined) : undefined,
602
+ evidence: r.evidence ? parse(r.evidence, undefined) : undefined,
603
+ status: r.status, validFrom: r.valid_from ?? undefined, validTo: r.valid_to ?? undefined,
604
+ supersedes: r.supersedes ?? undefined, supersededBy: r.superseded_by ?? undefined,
605
+ shadowedBy: r.shadowed_by ?? undefined, lastSeenAt: r.last_seen_at ?? undefined,
606
+ deletedAt: r.deleted_at ?? undefined, deletedReason: r.deleted_reason ?? undefined,
607
+ })
608
+
609
+ const mapCommitmentRow = (r) => {
610
+ // NULL means "never customised": keep the field absent so old snapshots
611
+ // and the pre-0.2.0 commitment shape stay identical. Corrupt or
612
+ // non-array JSON degrades to absent rather than breaking the ledger.
613
+ const parsedOffsets = r.reminder_offsets == null ? null : parse(r.reminder_offsets, null)
614
+ const reminderOffsets = Array.isArray(parsedOffsets) ? parsedOffsets : undefined
615
+ return {
616
+ id: r.id, matterId: r.matter_id, debtor: r.debtor, creditor: r.creditor, action: r.action,
617
+ dueAt: r.due_at ?? undefined, timezone: r.timezone, status: r.status,
618
+ evidence: parse(r.evidence), capability: r.capability, doneAt: r.done_at ?? undefined,
619
+ ...(reminderOffsets === undefined ? {} : { reminderOffsets }),
620
+ }
621
+ }
622
+
623
+ const mapPreferenceRow = (r) => ({
624
+ id: r.id, kind: r.kind, statement: r.statement, scope: parse(r.scope, {}),
625
+ source: parse(r.source, {}), evidence: parse(r.evidence), confidence: r.confidence,
626
+ status: r.status, validFrom: r.valid_from, lastConfirmed: r.last_confirmed ?? undefined,
627
+ expiresAt: r.expires_at ?? undefined, priority: r.priority,
628
+ })
629
+
630
+ const mapLinkRow = (r) => ({
631
+ id: r.id, fromMatter: r.from_matter, toMatter: r.to_matter, type: r.type,
632
+ confidence: r.confidence, evidence: parse(r.evidence), status: r.status,
633
+ createdBy: r.created_by, createdAt: r.created_at,
634
+ })
635
+
636
+ const mapUpdateRow = (r) => ({
637
+ id: r.id, sourceMatter: r.source_matter, targetMatter: r.target_matter, change: r.change,
638
+ status: r.status, createdAt: r.created_at, rollback: r.rollback ?? undefined,
639
+ })
640
+
641
+ const mapRoutineRow = (r) => ({
642
+ id: r.id, name: r.name, description: r.description, trigger: parse(r.trigger),
643
+ steps: parse(r.steps), preferences: parse(r.preferences), pitfalls: parse(r.pitfalls),
644
+ verification: parse(r.verification), scope: r.scope ?? undefined, version: r.version,
645
+ status: r.status, createdBy: r.created_by, createdAt: r.created_at, updatedAt: r.updated_at,
646
+ })
647
+
648
+ const mapRoutinePatchRow = (r) => ({
649
+ id: r.id, routineId: r.routine_id, baseVersion: r.base_version,
650
+ changes: parse(r.changes, {}), before: parse(r.before, {}), diff: r.diff, status: r.status, createdAt: r.created_at,
651
+ })
652
+
653
+ const mapTurnRow = (r) => ({
654
+ id: r.id, conversationId: r.conversation_id, seq: r.seq, role: r.role, content: r.content, createdAt: r.created_at,
655
+ })
656
+
657
+ /** Every table Ledger.fromJSON understands: raw SQL table + row mapper. */
658
+ const LEDGER_TABLE_LOADERS = {
659
+ matters: { table: 'matters', map: mapMatterRow },
660
+ facts: { table: 'facts', map: mapFactRow },
661
+ commitments: { table: 'commitments', map: mapCommitmentRow },
662
+ preferences: { table: 'preferences', map: mapPreferenceRow },
663
+ links: { table: 'links', map: mapLinkRow },
664
+ updates: { table: 'updates', map: mapUpdateRow },
665
+ routines: { table: 'routines', map: mapRoutineRow },
666
+ routinePatches: { table: 'routine_patches', map: mapRoutinePatchRow },
667
+ routes: { table: 'routes', routes: true },
668
+ }
669
+
219
670
  export class SqliteStore {
220
671
  constructor(path, { readOnly = false, checkIntegrity = false, busyTimeoutMs = Number(process.env.ADVISERS_SQLITE_BUSY_TIMEOUT_MS) || DEFAULT_BUSY_TIMEOUT_MS } = {}) {
221
672
  this.path = path
222
673
  this.busyTimeoutMs = busyTimeoutMs
223
674
  this._transactionDepth = 0
224
675
  this._baselineIds = null
225
- this.db = readOnly ? new DatabaseSync(path, { readOnly: true }) : new DatabaseSync(path)
226
- // P0-1: sqlite only applies busy_timeout to statements that run after the
227
- // pragma is set; concurrent first-opens used to fail on journal_mode=WAL
228
- // because busy_timeout was set afterwards.
229
- this.db.exec(`PRAGMA busy_timeout = ${Math.max(0, Math.floor(Number(busyTimeoutMs) || 0))}`)
230
- if (readOnly) {
231
- if (checkIntegrity) this.assertIntegrity()
232
- return
676
+ this._fullLoad = false
677
+ // FTS5 state (schema v19): which indexes exist and which tokenizer built
678
+ // them. Fillable stores discover it after migrate(); read-only stores detect
679
+ // it from sqlite_master without running DDL (see _initFtsState).
680
+ this.ftsTokenizer = null
681
+ this.ftsWarning = null
682
+ this._ftsTables = new Set()
683
+ // P2-2 (0.2.1): an existing 0-byte file is treated as corrupt instead of a
684
+ // brand-new database. Before this, SQLite turned it into an empty schema and
685
+ // every command reported "no data" while silently rebuilding the ledger.
686
+ assertDatabaseFileUsable(path)
687
+ try {
688
+ this.db = readOnly ? new DatabaseSync(path, { readOnly: true }) : new DatabaseSync(path)
689
+ // P0-1: sqlite only applies busy_timeout to statements that run after the
690
+ // pragma is set; concurrent first-opens used to fail on journal_mode=WAL
691
+ // because busy_timeout was set afterwards.
692
+ this.db.exec(`PRAGMA busy_timeout = ${Math.max(0, Math.floor(Number(busyTimeoutMs) || 0))}`)
693
+ if (readOnly) {
694
+ this._initFtsState()
695
+ if (checkIntegrity) this.assertIntegrity()
696
+ return
697
+ }
698
+ this.enableWal()
699
+ this.db.exec('PRAGMA secure_delete = ON')
700
+ this.db.exec('PRAGMA synchronous = NORMAL')
701
+ this.initializeSchema()
702
+ } catch (error) {
703
+ try { this.db?.close() } catch {}
704
+ if (error?.code === 'E_DB_CORRUPT') throw error
705
+ if (isCorruptSqliteError(error)) {
706
+ throw new DatabaseCorruptError(path, `SQLite reports that the file is not a valid database (${String(error?.message ?? error).slice(0, 200)})`)
707
+ }
708
+ throw error
233
709
  }
234
- this.enableWal()
235
- this.db.exec('PRAGMA secure_delete = ON')
236
- this.db.exec('PRAGMA synchronous = NORMAL')
237
- this.initializeSchema()
238
710
  }
239
711
 
240
712
  /** Full-database integrity check; explicit because it is O(database size). */
@@ -270,12 +742,94 @@ export class SqliteStore {
270
742
  this._transaction('IMMEDIATE', () => {
271
743
  this.db.exec(SCHEMA)
272
744
  this.migrate()
745
+ // migrate() installs the FTS indexes during an upgrade; a database that
746
+ // was already at SCHEMA_VERSION only needs their tokenizer read back.
747
+ this._initFtsState()
273
748
  // Indexes come after migrate(): some index the columns migrate() adds
274
749
  // (e.g. facts.deleted_at), so an old database must be upgraded first.
275
750
  this.db.exec(INDEXES)
276
751
  })
277
752
  }
278
753
 
754
+ /** Look up which FTS indexes/tokenizer this database actually has on disk. */
755
+ _initFtsState() {
756
+ const rows = this.db.prepare("SELECT name, sql FROM sqlite_master WHERE name IN ('turns_fts', 'summaries_fts')").all()
757
+ this._ftsTables = new Set(rows.map((row) => row.name))
758
+ const turns = rows.find((row) => row.name === 'turns_fts')
759
+ // v19 records the tokenizer in the virtual-table DDL. An older or
760
+ // hand-modified database may have no index at all: search then uses the
761
+ // LIKE path only, which stays correct.
762
+ this.ftsTokenizer = turns ? (/tokenize\s*=\s*'?trigram/i.test(String(turns.sql ?? '')) ? 'trigram' : 'unicode61') : null
763
+ return this.ftsTokenizer
764
+ }
765
+
766
+ _createFts(tokenizer) {
767
+ const ddl = ftsDdl(tokenizer)
768
+ for (const sql of ddl.tables) this.db.exec(sql)
769
+ for (const sql of ddl.triggers) this.db.exec(sql)
770
+ }
771
+
772
+ _dropFts() {
773
+ // Triggers first: they reference the virtual tables and a leftover trigger
774
+ // on the source table would break the next write.
775
+ for (const trigger of FTS_TRIGGER_NAMES) this.db.exec(`DROP TRIGGER IF EXISTS ${trigger}`)
776
+ for (const table of FTS_TABLE_NAMES) this.db.exec(`DROP TABLE IF EXISTS ${table}`)
777
+ }
778
+
779
+ /**
780
+ * Install (or rebuild) the v19 FTS5 indexes with the trigram tokenizer.
781
+ *
782
+ * Runs inside migrate()'s write transaction, so dropping/creating both
783
+ * virtual tables, replacing the triggers, and the 'rebuild' backfill are
784
+ * atomic with the schema_version bump: a failure rolls the migration back
785
+ * and the old v18 index survives. Rows are re-indexed from the source
786
+ * tables, so a database upgraded from v18 keeps all searchable data.
787
+ *
788
+ * Degrades instead of crashing: trigram (SQLite >= 3.34) -> unicode61 ->
789
+ * LIKE-only, recording ftsWarning and printing one warning line.
790
+ */
791
+ _setupFts() {
792
+ this._dropFts()
793
+ const preference = ftsTokenizerPreference()
794
+ let tokenizer = preference === 'unicode61' ? null : 'trigram'
795
+ if (tokenizer) {
796
+ try {
797
+ this._createFts(tokenizer)
798
+ } catch (error) {
799
+ // This build has no trigram tokenizer (or rejected the DDL): drop any
800
+ // partial index and fall through to the legacy tokenizer.
801
+ this._dropFts()
802
+ this.ftsWarning = `FTS5 trigram tokenizer unavailable (${error.message}); Chinese substring search falls back to LIKE`
803
+ try { console.warn(`advisers: warning: ${this.ftsWarning}`) } catch {}
804
+ tokenizer = null
805
+ }
806
+ } else if (preference === 'unicode61') {
807
+ this.ftsWarning = 'ADVISERS_FTS_TOKENIZER=unicode61 forces the legacy FTS5 tokenizer; Chinese substring search falls back to LIKE'
808
+ try { console.warn(`advisers: warning: ${this.ftsWarning}`) } catch {}
809
+ }
810
+ if (!tokenizer) {
811
+ try {
812
+ this._createFts('unicode61')
813
+ tokenizer = 'unicode61'
814
+ } catch (error) {
815
+ // FTS5 itself is absent: keep the database usable with LIKE-only
816
+ // search (no index, no triggers). SCHEMA_VERSION still advances so the
817
+ // migration is not retried on every open.
818
+ this._dropFts()
819
+ tokenizer = null
820
+ this.ftsWarning = `FTS5 is unavailable in this SQLite build (${error.message}); search uses LIKE only`
821
+ try { console.warn(`advisers: warning: ${this.ftsWarning}`) } catch {}
822
+ }
823
+ }
824
+ this.ftsTokenizer = tokenizer
825
+ this._ftsTables = new Set(tokenizer ? FTS_TABLE_NAMES : [])
826
+ if (tokenizer) {
827
+ this.db.exec("INSERT INTO turns_fts(turns_fts) VALUES('rebuild')")
828
+ this.db.exec("INSERT INTO summaries_fts(summaries_fts) VALUES('rebuild')")
829
+ }
830
+ return tokenizer
831
+ }
832
+
279
833
  /**
280
834
  * Run fn() in a write transaction with bounded SQLITE_BUSY retries.
281
835
  * Nested calls join the outer transaction (no nested BEGIN).
@@ -382,18 +936,6 @@ export class SqliteStore {
382
936
  conversation_id TEXT PRIMARY KEY, summary TEXT NOT NULL,
383
937
  through_seq INTEGER NOT NULL, updated_at TEXT NOT NULL
384
938
  )`)
385
- this.db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS turns_fts USING fts5(content, content='turns', content_rowid='rowid')`)
386
- this.db.exec(`CREATE TRIGGER IF NOT EXISTS turns_ai AFTER INSERT ON turns BEGIN
387
- INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
388
- END`)
389
- this.db.exec(`CREATE TRIGGER IF NOT EXISTS turns_ad AFTER DELETE ON turns BEGIN
390
- INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
391
- END`)
392
- this.db.exec(`CREATE TRIGGER IF NOT EXISTS turns_au AFTER UPDATE ON turns BEGIN
393
- INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
394
- INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
395
- END`)
396
- try { this.db.exec("INSERT INTO turns_fts(turns_fts) VALUES('rebuild')") } catch {}
397
939
  const commitmentCols = this.db.prepare('PRAGMA table_info(commitments)').all().map((c) => c.name)
398
940
  if (!commitmentCols.includes('done_at')) this.db.exec('ALTER TABLE commitments ADD COLUMN done_at TEXT')
399
941
  // 0.2.0: per-commitment custom reminder offsets (JSON array of strings).
@@ -439,6 +981,11 @@ CREATE TABLE IF NOT EXISTS task_runs (
439
981
  all_day INTEGER NOT NULL DEFAULT 0, tzid TEXT, location TEXT, rrule TEXT,
440
982
  source TEXT, imported_at TEXT NOT NULL
441
983
  )`)
984
+ // Schema v19 (perf audit round 2, P2-6): rebuild full-text search with the
985
+ // trigram tokenizer so 3+ character Chinese/substring queries hit FTS5
986
+ // instead of falling back to a LIKE table scan. Runs last so the source
987
+ // tables (turns, session_summaries) exist for the 'rebuild' backfill.
988
+ this._setupFts()
442
989
  this.db.prepare('INSERT OR REPLACE INTO meta(key,value) VALUES(?,?)').run('schema_version', String(SCHEMA_VERSION))
443
990
  } catch (error) {
444
991
  throw error
@@ -546,16 +1093,34 @@ CREATE TABLE IF NOT EXISTS task_runs (
546
1093
  }
547
1094
 
548
1095
  /**
549
- * P0-2 (tick N+1 removal): the newest `limit` outcomes per commitment in one
550
- * query (window function over idx_nudges_commitment_at). Replaces one
551
- * listCommitmentNudges() round-trip per commitment.
1096
+ * P0-2 (tick N+1 removal): the newest `limit` outcomes per commitment.
1097
+ *
1098
+ * P2-2 (0.2.1): the original form windowed over the WHOLE nudges table
1099
+ * (scan + temp b-tree sort), so every tick paid for the entire nudge history:
1100
+ * 271ms at 200k rows and growing linearly. Tick only needs outcomes for the
1101
+ * commitments it is about to nudge, so callers that know that set pass
1102
+ * `commitmentIds`; the read then becomes `limit` index seeks per commitment on
1103
+ * idx_nudges_commitment_at, bounded by the candidate set instead of the
1104
+ * history size. Passing no ids keeps the historical whole-table map for
1105
+ * callers/tests that need it.
552
1106
  */
553
- recentNudgeOutcomes(limit = 3) {
1107
+ recentNudgeOutcomes(limit = 3, commitmentIds = null) {
1108
+ const newest = Math.max(1, Math.floor(Number(limit) || 3))
1109
+ const byCommitment = new Map()
1110
+ if (commitmentIds) {
1111
+ const ids = [...new Set(commitmentIds)]
1112
+ if (!ids.length) return byCommitment
1113
+ const statement = this.db.prepare('SELECT outcome FROM nudges WHERE commitment_id = ? ORDER BY at DESC LIMIT ?')
1114
+ for (const id of ids) {
1115
+ const rows = statement.all(id, newest)
1116
+ if (rows.length) byCommitment.set(id, rows.map((row) => row.outcome))
1117
+ }
1118
+ return byCommitment
1119
+ }
554
1120
  const rows = this.db.prepare(`SELECT commitment_id, outcome, at FROM (
555
1121
  SELECT commitment_id, outcome, at, ROW_NUMBER() OVER (PARTITION BY commitment_id ORDER BY at DESC) AS rn
556
1122
  FROM nudges
557
- ) WHERE rn <= ?`).all(limit)
558
- const byCommitment = new Map()
1123
+ ) WHERE rn <= ?`).all(newest)
559
1124
  for (const row of rows) {
560
1125
  const list = byCommitment.get(row.commitment_id)
561
1126
  if (list) list.push(row.outcome)
@@ -685,15 +1250,35 @@ CREATE TABLE IF NOT EXISTS task_runs (
685
1250
  return id
686
1251
  }
687
1252
 
1253
+ /**
1254
+ * Token totals for 'advisers status' / 'advisers receipt'.
1255
+ * P1-2: aggregate in SQLite instead of materialising every model_calls row
1256
+ * (the old reduce loaded the whole table just to sum five columns).
1257
+ */
688
1258
  sumModelUsage({ since } = {}) {
689
- const rows = since
690
- ? this.db.prepare('SELECT * FROM model_calls WHERE created_at >= ?').all(since)
691
- : this.db.prepare('SELECT * FROM model_calls').all()
692
- return rows.reduce((acc, r) => {
693
- acc.calls += 1
694
- for (const key of ['prompt_tokens', 'completion_tokens', 'reasoning_tokens', 'cached_tokens', 'total_tokens']) acc[key] += Number(r[key] || 0)
695
- return acc
696
- }, { calls: 0, prompt_tokens: 0, completion_tokens: 0, reasoning_tokens: 0, cached_tokens: 0, total_tokens: 0 })
1259
+ const row = since
1260
+ ? this.db.prepare(`SELECT COUNT(*) AS calls,
1261
+ COALESCE(SUM(prompt_tokens), 0) AS prompt_tokens,
1262
+ COALESCE(SUM(completion_tokens), 0) AS completion_tokens,
1263
+ COALESCE(SUM(reasoning_tokens), 0) AS reasoning_tokens,
1264
+ COALESCE(SUM(cached_tokens), 0) AS cached_tokens,
1265
+ COALESCE(SUM(total_tokens), 0) AS total_tokens
1266
+ FROM model_calls WHERE created_at >= ?`).get(since)
1267
+ : this.db.prepare(`SELECT COUNT(*) AS calls,
1268
+ COALESCE(SUM(prompt_tokens), 0) AS prompt_tokens,
1269
+ COALESCE(SUM(completion_tokens), 0) AS completion_tokens,
1270
+ COALESCE(SUM(reasoning_tokens), 0) AS reasoning_tokens,
1271
+ COALESCE(SUM(cached_tokens), 0) AS cached_tokens,
1272
+ COALESCE(SUM(total_tokens), 0) AS total_tokens
1273
+ FROM model_calls`).get()
1274
+ return {
1275
+ calls: Number(row?.calls ?? 0),
1276
+ prompt_tokens: Number(row?.prompt_tokens ?? 0),
1277
+ completion_tokens: Number(row?.completion_tokens ?? 0),
1278
+ reasoning_tokens: Number(row?.reasoning_tokens ?? 0),
1279
+ cached_tokens: Number(row?.cached_tokens ?? 0),
1280
+ total_tokens: Number(row?.total_tokens ?? 0),
1281
+ }
697
1282
  }
698
1283
 
699
1284
  proposeCandidate({ kind, payload, evidence }) {
@@ -720,15 +1305,6 @@ CREATE TABLE IF NOT EXISTS task_runs (
720
1305
  return this.getCandidate(id)
721
1306
  }
722
1307
 
723
- searchTurns(query, { limit = 8 } = {}) {
724
- const terms = String(query || '').split(/[^\p{L}\p{N}]+/u).filter((t) => t.length >= 2).slice(0, 8)
725
- if (!terms.length) return []
726
- const rows = this.db.prepare('SELECT * FROM turns ORDER BY created_at DESC LIMIT 2000').all()
727
- const score = (text) => terms.filter((t) => String(text).includes(t)).length
728
- return rows.map((r) => ({ id: r.id, conversationId: r.conversation_id, seq: r.seq, role: r.role, text: r.content, createdAt: r.created_at, score: score(r.content) }))
729
- .filter((r) => r.score > 0).sort((a, b) => b.score - a.score || Date.parse(b.createdAt) - Date.parse(a.createdAt)).slice(0, limit)
730
- }
731
-
732
1308
  searchMemory(query, { limit = 6, matterId } = {}) {
733
1309
  const terms = String(query || '').split(/[^\p{L}\p{N}]+/u).filter((t) => t.length >= 2).slice(0, 8)
734
1310
  if (!terms.length) return []
@@ -753,6 +1329,19 @@ CREATE TABLE IF NOT EXISTS task_runs (
753
1329
  ].sort((a, b) => b.score - a.score || a.id.localeCompare(b.id)).slice(0, limit)
754
1330
  }
755
1331
 
1332
+ /**
1333
+ * Full-text turn search (schema v19; perf audit round 2, P2-6).
1334
+ *
1335
+ * 3+ characters go through FTS5 MATCH. With the trigram tokenizer that is
1336
+ * CJK substring/phrase search and English substring search; with the
1337
+ * unicode61 fallback it is word search (Chinese misses then fall through to
1338
+ * LIKE). Shorter queries and malformed/special FTS syntax keep the previous
1339
+ * LIKE path as the correctness backstop. Public shape and limit are unchanged
1340
+ * from v18: { id, conversationId, seq, role, content, createdAt }. Ordering
1341
+ * keeps the v18 conventions: FTS hits are ranked, LIKE hits are in seq order
1342
+ * (so Chinese results move from the old LIKE order to relevance order once
1343
+ * they hit the trigram index).
1344
+ */
756
1345
  searchTurns(query, { conversationId, matterId, limit = 8 } = {}) {
757
1346
  const q = String(query ?? '').trim()
758
1347
  if (!q) return []
@@ -762,14 +1351,40 @@ CREATE TABLE IF NOT EXISTS task_runs (
762
1351
  if (matterId) { clauses.push('s.matter_id = ?'); filterArgs.push(matterId) }
763
1352
  const join = matterId ? ' JOIN sessions s ON s.conversation_id = t.conversation_id' : ''
764
1353
  const where = clauses.length ? ` AND ${clauses.join(' AND ')}` : ''
765
- try {
766
- if (q.length >= 3) {
1354
+ const map = (r) => ({ id: r.id, conversationId: r.conversation_id, seq: r.seq, role: r.role, content: r.content, createdAt: r.created_at })
1355
+ if (this.ftsTokenizer && this._ftsTables.has('turns_fts') && q.length >= FTS_TRIGRAM_MIN_QUERY) {
1356
+ try {
767
1357
  const rows = this.db.prepare(`SELECT t.* FROM turns_fts f JOIN turns t ON t.rowid = f.rowid${join} WHERE turns_fts MATCH ?${where} ORDER BY rank LIMIT ?`).all(q, ...filterArgs, limit)
768
- if (rows.length) return rows.map((r) => ({ id: r.id, conversationId: r.conversation_id, seq: r.seq, role: r.role, content: r.content, createdAt: r.created_at }))
1358
+ if (rows.length) return rows.map(map)
1359
+ } catch {
1360
+ // Malformed or special FTS query: use the LIKE scan below.
769
1361
  }
770
- } catch {}
1362
+ }
771
1363
  const rows = this.db.prepare(`SELECT t.* FROM turns t${join} WHERE t.content LIKE ?${where} ORDER BY t.seq DESC LIMIT ?`).all(`%${q}%`, ...filterArgs, limit)
772
- return rows.reverse().map((r) => ({ id: r.id, conversationId: r.conversation_id, seq: r.seq, role: r.role, content: r.content, createdAt: r.created_at }))
1364
+ return rows.reverse().map(map)
1365
+ }
1366
+
1367
+ /**
1368
+ * Full-text session-summary search (schema v19). Same trigram/FTS-then-LIKE
1369
+ * strategy as searchTurns; returns the public summary shape used by
1370
+ * getSessionSummary().
1371
+ */
1372
+ searchSummaries(query, { conversationId, limit = 8 } = {}) {
1373
+ const q = String(query ?? '').trim()
1374
+ if (!q) return []
1375
+ const where = conversationId ? ' AND s.conversation_id = ?' : ''
1376
+ const filterArgs = conversationId ? [conversationId] : []
1377
+ const map = (r) => ({ conversationId: r.conversation_id, summary: r.summary, throughSeq: r.through_seq, updatedAt: r.updated_at })
1378
+ if (this.ftsTokenizer && this._ftsTables.has('summaries_fts') && q.length >= FTS_TRIGRAM_MIN_QUERY) {
1379
+ try {
1380
+ const rows = this.db.prepare(`SELECT s.* FROM summaries_fts f JOIN session_summaries s ON s.rowid = f.rowid WHERE summaries_fts MATCH ?${where} ORDER BY rank LIMIT ?`).all(q, ...filterArgs, limit)
1381
+ if (rows.length) return rows.map(map)
1382
+ } catch {
1383
+ // Malformed or special FTS query: use the LIKE scan below.
1384
+ }
1385
+ }
1386
+ const rows = this.db.prepare(`SELECT s.* FROM session_summaries s WHERE s.summary LIKE ?${where} ORDER BY s.updated_at DESC LIMIT ?`).all(`%${q}%`, ...filterArgs, limit)
1387
+ return rows.map(map)
773
1388
  }
774
1389
 
775
1390
  getSessionSummary(conversationId) {
@@ -868,70 +1483,219 @@ CREATE TABLE IF NOT EXISTS task_runs (
868
1483
  }
869
1484
  }
870
1485
 
871
- load() {
872
- const all = (sql) => this.db.prepare(sql).all()
873
- const snapshot = {
874
- version: 1,
875
- matters: all('SELECT * FROM matters').map((r) => ({
876
- id: r.id, title: r.title, status: r.status, domain: r.domain, secrecy: r.secrecy,
877
- summary: r.summary ?? undefined, participants: parse(r.participants), deadlines: parse(r.deadlines),
878
- openQuestions: parse(r.open_questions), createdAt: r.created_at, updatedAt: r.updated_at,
879
- })),
880
- facts: all('SELECT * FROM facts').map((r) => ({
881
- id: r.id, matterId: r.matter_id ?? undefined, subject: r.subject, predicate: r.predicate,
882
- object: r.object, recordedAt: r.recorded_at, expiredAt: r.expired_at ?? undefined,
883
- authority: r.authority, confidence: r.confidence,
884
- source: r.source ? parse(r.source, undefined) : undefined,
885
- evidence: r.evidence ? parse(r.evidence, undefined) : undefined,
886
- status: r.status, validFrom: r.valid_from ?? undefined, validTo: r.valid_to ?? undefined,
887
- supersedes: r.supersedes ?? undefined, supersededBy: r.superseded_by ?? undefined,
888
- shadowedBy: r.shadowed_by ?? undefined, lastSeenAt: r.last_seen_at ?? undefined,
889
- deletedAt: r.deleted_at ?? undefined, deletedReason: r.deleted_reason ?? undefined,
890
- })),
891
- commitments: all('SELECT * FROM commitments').map((r) => {
892
- // NULL means "never customised": keep the field absent so old snapshots
893
- // and the pre-0.2.0 commitment shape stay identical. Corrupt or
894
- // non-array JSON degrades to absent rather than breaking the ledger.
895
- const parsedOffsets = r.reminder_offsets == null ? null : parse(r.reminder_offsets, null)
896
- const reminderOffsets = Array.isArray(parsedOffsets) ? parsedOffsets : undefined
897
- return {
898
- id: r.id, matterId: r.matter_id, debtor: r.debtor, creditor: r.creditor, action: r.action,
899
- dueAt: r.due_at ?? undefined, timezone: r.timezone, status: r.status,
900
- evidence: parse(r.evidence), capability: r.capability, doneAt: r.done_at ?? undefined,
901
- ...(reminderOffsets === undefined ? {} : { reminderOffsets }),
902
- }
903
- }),
904
- preferences: all('SELECT * FROM preferences').map((r) => ({
905
- id: r.id, kind: r.kind, statement: r.statement, scope: parse(r.scope, {}),
906
- source: parse(r.source, {}), evidence: parse(r.evidence), confidence: r.confidence,
907
- status: r.status, validFrom: r.valid_from, lastConfirmed: r.last_confirmed ?? undefined,
908
- expiresAt: r.expires_at ?? undefined, priority: r.priority,
909
- })),
910
- links: all('SELECT * FROM links').map((r) => ({
911
- id: r.id, fromMatter: r.from_matter, toMatter: r.to_matter, type: r.type,
912
- confidence: r.confidence, evidence: parse(r.evidence), status: r.status,
913
- createdBy: r.created_by, createdAt: r.created_at,
914
- })),
915
- updates: all('SELECT * FROM updates').map((r) => ({
916
- id: r.id, sourceMatter: r.source_matter, targetMatter: r.target_matter, change: r.change,
917
- status: r.status, createdAt: r.created_at, rollback: r.rollback ?? undefined,
918
- })),
919
- routines: all('SELECT * FROM routines').map((r) => ({
920
- id: r.id, name: r.name, description: r.description, trigger: parse(r.trigger),
921
- steps: parse(r.steps), preferences: parse(r.preferences), pitfalls: parse(r.pitfalls),
922
- verification: parse(r.verification), scope: r.scope ?? undefined, version: r.version,
923
- status: r.status, createdBy: r.created_by, createdAt: r.created_at, updatedAt: r.updated_at,
924
- })),
925
- routinePatches: all('SELECT * FROM routine_patches').map((r) => ({
926
- id: r.id, routineId: r.routine_id, baseVersion: r.base_version,
927
- changes: parse(r.changes, {}), before: parse(r.before, {}), diff: r.diff, status: r.status, createdAt: r.created_at,
928
- })),
929
- routes: Object.fromEntries(all('SELECT * FROM routes').map((r) => [r.key, r.matter_id])),
1486
+ // -------------------------------------------------------------------------
1487
+ // Partial loading (P1-2, 0.2.2)
1488
+ //
1489
+ // Read commands no longer pay for the whole 9-table snapshot: they call the
1490
+ // per-table loaders below for the tables they actually use (today/who -> open
1491
+ // commitments, week/tick -> commitments, status -> count* only) and build a
1492
+ // partial snapshot for Ledger.fromParts. Every loader also records the row
1493
+ // ids it saw on this connection, so a later save() keeps the P1-1 merge
1494
+ // semantics: rows a loader never read are never deleted, and a partially
1495
+ // loaded ledger can be saved without truncating the tables it did not touch
1496
+ // (this is what lets 'tick' persist a snooze wake-up without touching facts).
1497
+ // -------------------------------------------------------------------------
1498
+
1499
+ _selectRows(table, where = [], params = [], suffix = '') {
1500
+ const sql = 'SELECT * FROM ' + table + (where.length ? ' WHERE ' + where.join(' AND ') : '') + suffix
1501
+ return this.db.prepare(sql).all(...params)
1502
+ }
1503
+
1504
+ _countRows(table, where = [], params = []) {
1505
+ const sql = 'SELECT COUNT(*) AS n FROM ' + table + (where.length ? ' WHERE ' + where.join(' AND ') : '')
1506
+ return Number(this.db.prepare(sql).get(...params)?.n ?? 0)
1507
+ }
1508
+
1509
+ /** Record the row ids this connection loaded so save() only deletes those. */
1510
+ _markLoadedRows(table, rows) {
1511
+ // A partial load after a full load() starts a fresh partial baseline: a
1512
+ // snapshot built from the partial ledger does not contain the tables the
1513
+ // full load read, and keeping the full baseline would let save() delete
1514
+ // rows that the partial snapshot simply never had. Conservative rule: a
1515
+ // save may only delete rows a loader on this connection actually read.
1516
+ if (this._fullLoad) {
1517
+ this._baselineIds = idsFor({})
1518
+ this._fullLoad = false
930
1519
  }
1520
+ if (!this._baselineIds) this._baselineIds = idsFor({})
1521
+ this._baselineIds[table] = table === 'routes'
1522
+ ? new Set(Object.keys(rows ?? {}))
1523
+ : new Set((rows ?? []).map((row) => row?.id).filter((id) => id != null))
1524
+ }
1525
+
1526
+ /** Load one whole ledger table by its Ledger.fromJSON table name. */
1527
+ _loadTable(table) {
1528
+ const definition = LEDGER_TABLE_LOADERS[table]
1529
+ if (!definition) throw new Error('unknown ledger table: ' + table)
1530
+ if (definition.routes) return this.loadRoutes()
1531
+ const rows = this._selectRows(definition.table).map(definition.map)
1532
+ this._markLoadedRows(table, rows)
1533
+ return rows
1534
+ }
1535
+
1536
+ /** Snapshot of a subset of ledger tables (default: all of them). */
1537
+ loadSnapshot(tables = null) {
1538
+ const snapshot = { version: 1 }
1539
+ for (const table of tables ?? Object.keys(LEDGER_TABLE_LOADERS)) snapshot[table] = this._loadTable(table)
1540
+ return snapshot
1541
+ }
1542
+
1543
+ loadMatters({ status } = {}) {
1544
+ const rows = status === undefined
1545
+ ? this._selectRows('matters', [], [], ' ORDER BY rowid')
1546
+ : this._selectRows('matters', ['status = ?'], [status], ' ORDER BY rowid')
1547
+ const matters = rows.map(mapMatterRow)
1548
+ this._markLoadedRows('matters', matters)
1549
+ return matters
1550
+ }
1551
+
1552
+ loadFacts({ status, matterId, includeDeleted } = {}) {
1553
+ const where = []
1554
+ const params = []
1555
+ if (status !== undefined) { where.push('status = ?'); params.push(status) }
1556
+ if (matterId !== undefined) { where.push('matter_id = ?'); params.push(matterId) }
1557
+ if (includeDeleted === false) where.push('deleted_at IS NULL')
1558
+ const facts = this._selectRows('facts', where, params, ' ORDER BY rowid').map(mapFactRow)
1559
+ this._markLoadedRows('facts', facts)
1560
+ return facts
1561
+ }
1562
+
1563
+ loadCommitments({ status, matterId } = {}) {
1564
+ const where = []
1565
+ const params = []
1566
+ if (status !== undefined) { where.push('status = ?'); params.push(status) }
1567
+ if (matterId !== undefined) { where.push('matter_id = ?'); params.push(matterId) }
1568
+ const commitments = this._selectRows('commitments', where, params, ' ORDER BY rowid').map(mapCommitmentRow)
1569
+ this._markLoadedRows('commitments', commitments)
1570
+ return commitments
1571
+ }
1572
+
1573
+ loadPreferences({ status, kind } = {}) {
1574
+ const where = []
1575
+ const params = []
1576
+ if (status !== undefined) { where.push('status = ?'); params.push(status) }
1577
+ if (kind !== undefined) { where.push('kind = ?'); params.push(kind) }
1578
+ const preferences = this._selectRows('preferences', where, params, ' ORDER BY rowid').map(mapPreferenceRow)
1579
+ this._markLoadedRows('preferences', preferences)
1580
+ return preferences
1581
+ }
1582
+
1583
+ loadLinks({ status, excludeRejected } = {}) {
1584
+ const where = []
1585
+ const params = []
1586
+ if (status !== undefined) { where.push('status = ?'); params.push(status) }
1587
+ if (excludeRejected === true) where.push("status != 'rejected'")
1588
+ const links = this._selectRows('links', where, params, ' ORDER BY rowid').map(mapLinkRow)
1589
+ this._markLoadedRows('links', links)
1590
+ return links
1591
+ }
1592
+
1593
+ loadUpdates({ status } = {}) {
1594
+ const rows = status === undefined
1595
+ ? this._selectRows('updates', [], [], ' ORDER BY rowid')
1596
+ : this._selectRows('updates', ['status = ?'], [status], ' ORDER BY rowid')
1597
+ const updates = rows.map(mapUpdateRow)
1598
+ this._markLoadedRows('updates', updates)
1599
+ return updates
1600
+ }
1601
+
1602
+ loadRoutines({ status } = {}) {
1603
+ const rows = status === undefined
1604
+ ? this._selectRows('routines', [], [], ' ORDER BY rowid')
1605
+ : this._selectRows('routines', ['status = ?'], [status], ' ORDER BY rowid')
1606
+ const routines = rows.map(mapRoutineRow)
1607
+ this._markLoadedRows('routines', routines)
1608
+ return routines
1609
+ }
1610
+
1611
+ loadRoutinePatches({ status } = {}) {
1612
+ const rows = status === undefined
1613
+ ? this._selectRows('routine_patches', [], [], ' ORDER BY rowid')
1614
+ : this._selectRows('routine_patches', ['status = ?'], [status], ' ORDER BY rowid')
1615
+ const routinePatches = rows.map(mapRoutinePatchRow)
1616
+ this._markLoadedRows('routinePatches', routinePatches)
1617
+ return routinePatches
1618
+ }
1619
+
1620
+ loadRoutes() {
1621
+ const routes = Object.fromEntries(this._selectRows('routes', [], [], ' ORDER BY rowid').map((r) => [r.key, r.matter_id]))
1622
+ this._markLoadedRows('routes', routes)
1623
+ return routes
1624
+ }
1625
+
1626
+ /**
1627
+ * Latest turns for one conversation (same window and order as listTurns),
1628
+ * or every turn in seq order when no conversation/limit is given.
1629
+ */
1630
+ loadTurns({ conversationId, limit } = {}) {
1631
+ const where = []
1632
+ const params = []
1633
+ if (conversationId !== undefined) { where.push('conversation_id = ?'); params.push(conversationId) }
1634
+ if (limit === undefined) return this._selectRows('turns', where, params, ' ORDER BY seq ASC').map(mapTurnRow)
1635
+ const rows = this._selectRows('turns', where, params.concat([limit]), ' ORDER BY seq DESC LIMIT ?')
1636
+ return rows.reverse().map(mapTurnRow)
1637
+ }
1638
+
1639
+ /** Row counts for 'advisers status' and friends (no row mapping, no snapshot). */
1640
+ countMatters({ status } = {}) {
1641
+ return status === undefined ? this._countRows('matters') : this._countRows('matters', ['status = ?'], [status])
1642
+ }
1643
+
1644
+ countFacts({ status, matterId, includeDeleted } = {}) {
1645
+ const where = []
1646
+ const params = []
1647
+ if (status !== undefined) { where.push('status = ?'); params.push(status) }
1648
+ if (matterId !== undefined) { where.push('matter_id = ?'); params.push(matterId) }
1649
+ if (includeDeleted === false) where.push('deleted_at IS NULL')
1650
+ return this._countRows('facts', where, params)
1651
+ }
1652
+
1653
+ countCommitments({ status, matterId } = {}) {
1654
+ const where = []
1655
+ const params = []
1656
+ if (status !== undefined) { where.push('status = ?'); params.push(status) }
1657
+ if (matterId !== undefined) { where.push('matter_id = ?'); params.push(matterId) }
1658
+ return this._countRows('commitments', where, params)
1659
+ }
1660
+
1661
+ countPreferences({ status, kind } = {}) {
1662
+ const where = []
1663
+ const params = []
1664
+ if (status !== undefined) { where.push('status = ?'); params.push(status) }
1665
+ if (kind !== undefined) { where.push('kind = ?'); params.push(kind) }
1666
+ return this._countRows('preferences', where, params)
1667
+ }
1668
+
1669
+ countRoutines({ status } = {}) {
1670
+ return status === undefined ? this._countRows('routines') : this._countRows('routines', ['status = ?'], [status])
1671
+ }
1672
+
1673
+ countLinks({ status, excludeRejected } = {}) {
1674
+ const where = []
1675
+ const params = []
1676
+ if (status !== undefined) { where.push('status = ?'); params.push(status) }
1677
+ if (excludeRejected === true) where.push("status != 'rejected'")
1678
+ return this._countRows('links', where, params)
1679
+ }
1680
+
1681
+ countSessions() {
1682
+ return this._countRows('sessions')
1683
+ }
1684
+
1685
+ countTurns({ conversationId } = {}) {
1686
+ return conversationId === undefined
1687
+ ? this._countRows('turns')
1688
+ : this._countRows('turns', ['conversation_id = ?'], [conversationId])
1689
+ }
1690
+
1691
+ load() {
1692
+ const snapshot = this.loadSnapshot()
931
1693
  // Remember which rows this connection has seen (P1-1): save() only deletes
932
1694
  // rows this loader owned, so another process's concurrent additions are
933
- // merged instead of being silently overwritten by a stale snapshot.
1695
+ // merged instead of being silently overwritten by a stale snapshot. The
1696
+ // partial loaders above record the same baseline for their own tables.
934
1697
  this._baselineIds = idsFor(snapshot)
1698
+ this._fullLoad = true
935
1699
  return snapshot
936
1700
  }
937
1701