advisers 0.2.1 → 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/README-zh.md +5 -3
- package/README.md +6 -5
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/bin/advisers.js +98 -23
- package/index.js +1 -1
- package/lib/sqlite.js +534 -106
- package/package.json +4 -4
package/README-zh.md
CHANGED
|
@@ -5,12 +5,13 @@
|
|
|
5
5
|
## 安装
|
|
6
6
|
|
|
7
7
|
```sh
|
|
8
|
-
# 0.2.
|
|
8
|
+
# 0.2.2 是已 staged 到 npm @latest 的发布候选,待账号所有者 OTP 批准;
|
|
9
|
+
# 0.2.1 是当前已发布的 @latest(稳定渠道 = 默认 `latest`):
|
|
9
10
|
# npm i -g --registry=https://registry.npmjs.org advisers@latest advisers-office@latest
|
|
10
11
|
# 从 checkout 运行:
|
|
11
12
|
# corepack pnpm install && corepack pnpm -r build
|
|
12
13
|
# ADVISERS_OFFICE_LOCAL=1 node packages/advisers/bin/advisers.js status
|
|
13
|
-
# preview/RC 构建使用 `next` 渠道(当前 `next` 也指向 0.2.
|
|
14
|
+
# preview/RC 构建使用 `next` 渠道(当前 `next` 也指向 0.2.1):
|
|
14
15
|
# npm i -g --registry=https://registry.npmjs.org advisers@next advisers-office@next
|
|
15
16
|
advisers soul init
|
|
16
17
|
advisers brief "做一份 Q4 预算 Excel,周五前给老板"
|
|
@@ -36,7 +37,8 @@ CLI 是**本地秘书运行时**:一个 REPL、用户账/承诺账/流程账
|
|
|
36
37
|
|
|
37
38
|
## 状态
|
|
38
39
|
|
|
39
|
-
`0.2.
|
|
40
|
+
`0.2.2` 是发布候选(2026-11-18),已 staged 到 npm `latest`、待账号所有者 OTP 批准;
|
|
41
|
+
批准前 npm `latest` 仍为 `0.2.1`(2026-11-17 已发布):
|
|
40
42
|
|
|
41
43
|
- SQLite 账本 `~/.advisers/state.db`(JSON fallback);
|
|
42
44
|
- soul 位于 `~/.advisers/soul.md`,人类独占;
|
package/README.md
CHANGED
|
@@ -7,13 +7,13 @@ routines.
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
9
9
|
```sh
|
|
10
|
-
# 0.2.
|
|
11
|
-
# (stable channel = default `latest
|
|
10
|
+
# 0.2.2 is the release candidate staged to npm @latest (owner OTP pending);
|
|
11
|
+
# 0.2.1 is the published @latest (stable channel = default `latest`):
|
|
12
12
|
# npm i -g --registry=https://registry.npmjs.org advisers@latest advisers-office@latest
|
|
13
13
|
# From checkout:
|
|
14
14
|
# corepack pnpm install && corepack pnpm -r build
|
|
15
15
|
# ADVISERS_OFFICE_LOCAL=1 node packages/advisers/bin/advisers.js status
|
|
16
|
-
# Pre-release/RC builds land on the `next` channel (currently also 0.2.
|
|
16
|
+
# Pre-release/RC builds land on the `next` channel (currently also 0.2.1):
|
|
17
17
|
# npm i -g --registry=https://registry.npmjs.org advisers@next advisers-office@next
|
|
18
18
|
advisers soul init
|
|
19
19
|
advisers brief "做一份 Q4 预算 Excel,周五前给老板"
|
|
@@ -44,8 +44,9 @@ turn natural language into commitments or artifacts.
|
|
|
44
44
|
|
|
45
45
|
## Status
|
|
46
46
|
|
|
47
|
-
`0.2.
|
|
48
|
-
owner's OTP approval
|
|
47
|
+
`0.2.2` is the release candidate (2026-11-18), staged to npm `latest` and awaiting
|
|
48
|
+
the account owner's OTP approval; npm `latest` still resolves to `0.2.1` (published
|
|
49
|
+
2026-11-17):
|
|
49
50
|
|
|
50
51
|
- SQLite ledger at `~/.advisers/state.db` (JSON fallback with `ADVISERS_STORE=json`);
|
|
51
52
|
- soul at `~/.advisers/soul.md`, human-owned;
|
package/THIRD-PARTY-NOTICES.md
CHANGED
|
@@ -11,7 +11,7 @@ dependency closure of the published packages ("pnpm list --prod --depth 100";
|
|
|
11
11
|
devDependencies and workspace packages excluded). Do not edit by hand; CI runs
|
|
12
12
|
"node scripts/check-licenses.mjs".
|
|
13
13
|
|
|
14
|
-
This file is shipped with the advisers@0.2.
|
|
14
|
+
This file is shipped with the advisers@0.2.2 npm package and covers the third-party production dependencies of that package only. Each dependency keeps its own license.
|
|
15
15
|
|
|
16
16
|
Optional platform-specific binaries (for example the @napi-rs/canvas builds for
|
|
17
17
|
other operating systems and CPU architectures) are installed only on a matching
|
package/bin/advisers.js
CHANGED
|
@@ -283,18 +283,76 @@ function openStore({ checkIntegrity = false } = {}) {
|
|
|
283
283
|
return activeStore
|
|
284
284
|
}
|
|
285
285
|
|
|
286
|
-
|
|
286
|
+
/**
|
|
287
|
+
* P1-2 (0.2.2): per-table partial snapshot loaders.
|
|
288
|
+
*
|
|
289
|
+
* Every ledger command used to pay for the full 9-table store.load() -- 488ms
|
|
290
|
+
* at a 50k-row ledger and 1.2s at 100k in the 2026-11-17 performance audit
|
|
291
|
+
* round 2 -- even when it read one table or no rows at all. A read command now
|
|
292
|
+
* passes only the tables it uses (e.g. today/who load open commitments only)
|
|
293
|
+
* and the store records which rows were read so a later save() (tick waking a
|
|
294
|
+
* snoozed commitment) never deletes rows of tables that were not loaded.
|
|
295
|
+
*/
|
|
296
|
+
const LEDGER_PART_LOADERS = {
|
|
297
|
+
matters: (store, options) => store.loadMatters(options),
|
|
298
|
+
facts: (store, options) => store.loadFacts(options),
|
|
299
|
+
commitments: (store, options) => store.loadCommitments(options),
|
|
300
|
+
preferences: (store, options) => store.loadPreferences(options),
|
|
301
|
+
links: (store, options) => store.loadLinks(options),
|
|
302
|
+
updates: (store, options) => store.loadUpdates(options),
|
|
303
|
+
routines: (store, options) => store.loadRoutines(options),
|
|
304
|
+
routinePatches: (store, options) => store.loadRoutinePatches(options),
|
|
305
|
+
routes: (store) => store.loadRoutes(),
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Open the SQLite store for a ledger read, after honouring the legacy
|
|
310
|
+
* state.json -> SQLite migration and the sealed-vault/corrupt-DB fail-fast
|
|
311
|
+
* paths. Exits the process (with the documented hints) instead of ever
|
|
312
|
+
* silently falling back to the JSON store.
|
|
313
|
+
*/
|
|
314
|
+
function openLedgerStore() {
|
|
287
315
|
ensureHome()
|
|
288
|
-
if (mode === 'json') return loadJson()
|
|
289
316
|
const sealed = vaultSealedPath()
|
|
290
317
|
if (sealed) throw vaultSealedError(sealed)
|
|
291
318
|
try {
|
|
292
319
|
if (!existsSync(DB) && existsSync(STATE)) {
|
|
293
320
|
const ledger = loadJson()
|
|
294
321
|
openStore().save(ledger.toJSON())
|
|
295
|
-
return ledger
|
|
296
322
|
}
|
|
297
|
-
return
|
|
323
|
+
return openStore()
|
|
324
|
+
} catch (error) {
|
|
325
|
+
const hint = storeFailureHint(error)
|
|
326
|
+
if (hint) {
|
|
327
|
+
console.error(hint)
|
|
328
|
+
process.exit(1)
|
|
329
|
+
}
|
|
330
|
+
console.error(`sqlite unavailable: ${error.message}`)
|
|
331
|
+
console.error('Run `advisers doctor`; refusing to silently switch to JSON (would split the ledger).')
|
|
332
|
+
process.exit(1)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Load the ledger. No arguments: the full snapshot (write commands and
|
|
338
|
+
* anything that may save keep using this). With a table map such as
|
|
339
|
+
* `{ commitments: { status: 'open' } }` only those tables are read, mapped by
|
|
340
|
+
* the same row mappers, and the returned Ledger treats every omitted table as
|
|
341
|
+
* empty (Ledger.fromParts).
|
|
342
|
+
*/
|
|
343
|
+
function loadLedger(parts = null) {
|
|
344
|
+
ensureHome()
|
|
345
|
+
if (mode === 'json') return loadJson()
|
|
346
|
+
const store = openLedgerStore()
|
|
347
|
+
try {
|
|
348
|
+
if (!parts) return Ledger.fromJSON(store.load(), LOCAL_PORTS)
|
|
349
|
+
const snapshot = { version: 1 }
|
|
350
|
+
for (const [table, options] of Object.entries(parts)) {
|
|
351
|
+
const loader = LEDGER_PART_LOADERS[table]
|
|
352
|
+
if (!loader) throw new Error(`unknown ledger table for partial load: ${table}`)
|
|
353
|
+
snapshot[table] = loader(store, options ?? {})
|
|
354
|
+
}
|
|
355
|
+
return Ledger.fromParts(snapshot, LOCAL_PORTS)
|
|
298
356
|
} catch (error) {
|
|
299
357
|
const hint = storeFailureHint(error)
|
|
300
358
|
if (hint) {
|
|
@@ -471,7 +529,8 @@ function printReceipt() {
|
|
|
471
529
|
if (!store) { console.log('receipt requires SQLite store'); return }
|
|
472
530
|
const delivery = store.listDeliveries(1)[0]
|
|
473
531
|
if (!delivery) { console.log('还没有交付记录;先运行 `advisers office` / `advisers template`。'); return }
|
|
474
|
-
|
|
532
|
+
// P1-2 (0.2.2): only this delivery's matter and its open commitments are read.
|
|
533
|
+
const ledger = loadLedger({ commitments: { status: 'open', matterId: delivery.matterId } })
|
|
475
534
|
const open = ledger.openCommitments(delivery.matterId)
|
|
476
535
|
console.log(`回执 ${delivery.id}`)
|
|
477
536
|
console.log(`时间:${delivery.createdAt}`)
|
|
@@ -498,8 +557,9 @@ function printDeliveries() {
|
|
|
498
557
|
console.log('no deliveries yet')
|
|
499
558
|
return
|
|
500
559
|
}
|
|
560
|
+
// P1-2 (0.2.2): only matter titles are needed for the fallback label.
|
|
501
561
|
let matters = []
|
|
502
|
-
try { matters = loadLedger().listMatters() } catch {}
|
|
562
|
+
try { matters = loadLedger({ matters: {} }).listMatters() } catch {}
|
|
503
563
|
const title = new Map(matters.map((m) => [m.id, m.title]))
|
|
504
564
|
for (const row of rows) {
|
|
505
565
|
console.log(`${row.createdAt.slice(0, 19)} ${row.title ?? title.get(row.matterId) ?? row.matterId} [${row.tool ?? 'tool'}] ${row.validation ?? ''}`)
|
|
@@ -1434,7 +1494,6 @@ function init() {
|
|
|
1434
1494
|
}
|
|
1435
1495
|
|
|
1436
1496
|
function printStatus(asJson = false) {
|
|
1437
|
-
const ledger = loadLedger()
|
|
1438
1497
|
const data = {
|
|
1439
1498
|
version: VERSION,
|
|
1440
1499
|
node: process.version,
|
|
@@ -1442,19 +1501,32 @@ function printStatus(asJson = false) {
|
|
|
1442
1501
|
home: HOME,
|
|
1443
1502
|
store: mode === 'sqlite' ? { type: 'sqlite', path: DB } : { type: mode, path: STATE },
|
|
1444
1503
|
model: modelLabel(),
|
|
1445
|
-
matters: ledger.listMatters().length,
|
|
1446
|
-
prefs: ledger.listPreferences().length,
|
|
1447
|
-
routines: ledger.listRoutines().length,
|
|
1448
|
-
links: ledger.listLinks().length,
|
|
1449
1504
|
soul: existsSync(SOUL) ? 'present' : 'not initialized',
|
|
1450
1505
|
}
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
data.
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1506
|
+
if (mode === 'json') {
|
|
1507
|
+
// Legacy JSON store: the snapshot is small, keep the historical behaviour.
|
|
1508
|
+
const ledger = loadLedger()
|
|
1509
|
+
data.matters = ledger.listMatters().length
|
|
1510
|
+
data.prefs = ledger.listPreferences().length
|
|
1511
|
+
data.routines = ledger.listRoutines().length
|
|
1512
|
+
data.links = ledger.listLinks().length
|
|
1513
|
+
data.sessions = 0
|
|
1514
|
+
data.deliveries = 0
|
|
1515
|
+
} else {
|
|
1516
|
+
// P1-2 (0.2.2): COUNT-only status. The old path loaded all 9 tables just to
|
|
1517
|
+
// count four of them (664ms / 352MB RSS at 50k rows in the round-2 audit).
|
|
1518
|
+
// listMatters()/listPreferences()/listRoutines()/listLinks() filter status,
|
|
1519
|
+
// so the counts replicate those filters exactly.
|
|
1520
|
+
const store = openLedgerStore()
|
|
1521
|
+
data.matters = store.countMatters({ status: 'active' })
|
|
1522
|
+
data.prefs = store.countPreferences({ status: 'confirmed' })
|
|
1523
|
+
data.routines = store.countRoutines()
|
|
1524
|
+
data.links = store.countLinks({ excludeRejected: true })
|
|
1525
|
+
data.sessions = store.countSessions()
|
|
1526
|
+
data.deliveries = store.countDeliveries()
|
|
1527
|
+
const usage = store.sumModelUsage()
|
|
1528
|
+
if (usage.calls) data.modelUsage = usage
|
|
1529
|
+
}
|
|
1458
1530
|
if (asJson) { console.log(JSON.stringify(data, null, 2)); return }
|
|
1459
1531
|
console.log(`version ${data.version}`)
|
|
1460
1532
|
console.log(`node ${data.node}`)
|
|
@@ -4807,13 +4879,16 @@ if (!command) {
|
|
|
4807
4879
|
else console.log(`${sub}: ${id}`)
|
|
4808
4880
|
} else { console.error('usage: advisers nudge list|done|ignore'); process.exitCode = 1 }
|
|
4809
4881
|
} else if (command === 'risks') {
|
|
4810
|
-
risks(loadLedger())
|
|
4882
|
+
risks(loadLedger({ commitments: { status: 'open' } }))
|
|
4811
4883
|
} else if (command === 'pet-brief') {
|
|
4812
4884
|
await petBrief(args)
|
|
4813
4885
|
} else if (command === 'today') {
|
|
4814
|
-
|
|
4886
|
+
// P1-2 (0.2.2): today only reads open commitments; --sources reports source
|
|
4887
|
+
// rows for every commitment, so that flag keeps the wider load.
|
|
4888
|
+
const sources = args.includes('--sources')
|
|
4889
|
+
await today(loadLedger({ commitments: sources ? {} : { status: 'open' } }), { calendar: args.includes('--calendar'), sources, json: args.includes('--json') })
|
|
4815
4890
|
} else if (command === 'tick') {
|
|
4816
|
-
tick(loadLedger(), args.includes('--json'))
|
|
4891
|
+
tick(loadLedger({ commitments: {} }), args.includes('--json'))
|
|
4817
4892
|
} else if (command === 'ui') {
|
|
4818
4893
|
await ui(args)
|
|
4819
4894
|
} else if (command === 'remind') {
|
|
@@ -4821,7 +4896,7 @@ if (!command) {
|
|
|
4821
4896
|
} else if (command === 'calendar') {
|
|
4822
4897
|
await calendarImport(args.slice(1))
|
|
4823
4898
|
} else if (command === 'who') {
|
|
4824
|
-
const open = loadLedger().openCommitments()
|
|
4899
|
+
const open = loadLedger({ commitments: { status: 'open' } }).openCommitments()
|
|
4825
4900
|
const mine = open.filter((c) => c.debtor === 'user' || c.debtor === 'agent')
|
|
4826
4901
|
const waiting = open.filter((c) => c.debtor === 'third' || c.debtor.startsWith('third:'))
|
|
4827
4902
|
if (args.includes('--json')) { console.log(JSON.stringify({ mine, waiting }, null, 2)); }
|
|
@@ -4891,7 +4966,7 @@ if (!command) {
|
|
|
4891
4966
|
deliveries: store?.listDeliveries(100) ?? [],
|
|
4892
4967
|
}))
|
|
4893
4968
|
} else if (command === 'week') {
|
|
4894
|
-
printWeek(loadLedger())
|
|
4969
|
+
printWeek(loadLedger({ commitments: {} }))
|
|
4895
4970
|
} else if (command === 'prefs') {
|
|
4896
4971
|
printPreferences()
|
|
4897
4972
|
} else if (command === 'privacy') {
|
package/index.js
CHANGED
package/lib/sqlite.js
CHANGED
|
@@ -18,7 +18,7 @@ import { dirname, join } from 'node:path'
|
|
|
18
18
|
* - Migrations are refused for a schema newer than this CLI and are preceded
|
|
19
19
|
* by a same-directory backup of the old database.
|
|
20
20
|
*/
|
|
21
|
-
export const SCHEMA_VERSION =
|
|
21
|
+
export const SCHEMA_VERSION = 19
|
|
22
22
|
const DEFAULT_BUSY_TIMEOUT_MS = 5000
|
|
23
23
|
const OPEN_RETRIES = 6
|
|
24
24
|
const BUSY_ERRCODES = new Set([5, 6]) // SQLITE_BUSY, SQLITE_LOCKED
|
|
@@ -458,17 +458,6 @@ CREATE TABLE IF NOT EXISTS session_summaries (
|
|
|
458
458
|
conversation_id TEXT PRIMARY KEY, summary TEXT NOT NULL,
|
|
459
459
|
through_seq INTEGER NOT NULL, updated_at TEXT NOT NULL
|
|
460
460
|
);
|
|
461
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS turns_fts USING fts5(content, content='turns', content_rowid='rowid');
|
|
462
|
-
CREATE TRIGGER IF NOT EXISTS turns_ai AFTER INSERT ON turns BEGIN
|
|
463
|
-
INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
464
|
-
END;
|
|
465
|
-
CREATE TRIGGER IF NOT EXISTS turns_ad AFTER DELETE ON turns BEGIN
|
|
466
|
-
INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
|
|
467
|
-
END;
|
|
468
|
-
CREATE TRIGGER IF NOT EXISTS turns_au AFTER UPDATE ON turns BEGIN
|
|
469
|
-
INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
|
|
470
|
-
INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
471
|
-
END;
|
|
472
461
|
CREATE TABLE IF NOT EXISTS summary_versions (
|
|
473
462
|
conversation_id TEXT NOT NULL, version INTEGER NOT NULL, summary TEXT NOT NULL,
|
|
474
463
|
through_seq INTEGER NOT NULL, pins TEXT, created_at TEXT NOT NULL,
|
|
@@ -491,6 +480,73 @@ CREATE TABLE IF NOT EXISTS calendar_events (
|
|
|
491
480
|
CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
492
481
|
`
|
|
493
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
|
+
|
|
494
550
|
/**
|
|
495
551
|
* P2-1: secondary indexes for the hot read/write paths (tick, today, history,
|
|
496
552
|
* deliveries). Indexes are layout only, not a schema change, so they are
|
|
@@ -521,12 +577,109 @@ const parse = (v, fallback = []) => {
|
|
|
521
577
|
try { return JSON.parse(v) } catch { return fallback }
|
|
522
578
|
}
|
|
523
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
|
+
|
|
524
670
|
export class SqliteStore {
|
|
525
671
|
constructor(path, { readOnly = false, checkIntegrity = false, busyTimeoutMs = Number(process.env.ADVISERS_SQLITE_BUSY_TIMEOUT_MS) || DEFAULT_BUSY_TIMEOUT_MS } = {}) {
|
|
526
672
|
this.path = path
|
|
527
673
|
this.busyTimeoutMs = busyTimeoutMs
|
|
528
674
|
this._transactionDepth = 0
|
|
529
675
|
this._baselineIds = null
|
|
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()
|
|
530
683
|
// P2-2 (0.2.1): an existing 0-byte file is treated as corrupt instead of a
|
|
531
684
|
// brand-new database. Before this, SQLite turned it into an empty schema and
|
|
532
685
|
// every command reported "no data" while silently rebuilding the ledger.
|
|
@@ -538,6 +691,7 @@ export class SqliteStore {
|
|
|
538
691
|
// because busy_timeout was set afterwards.
|
|
539
692
|
this.db.exec(`PRAGMA busy_timeout = ${Math.max(0, Math.floor(Number(busyTimeoutMs) || 0))}`)
|
|
540
693
|
if (readOnly) {
|
|
694
|
+
this._initFtsState()
|
|
541
695
|
if (checkIntegrity) this.assertIntegrity()
|
|
542
696
|
return
|
|
543
697
|
}
|
|
@@ -588,12 +742,94 @@ export class SqliteStore {
|
|
|
588
742
|
this._transaction('IMMEDIATE', () => {
|
|
589
743
|
this.db.exec(SCHEMA)
|
|
590
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()
|
|
591
748
|
// Indexes come after migrate(): some index the columns migrate() adds
|
|
592
749
|
// (e.g. facts.deleted_at), so an old database must be upgraded first.
|
|
593
750
|
this.db.exec(INDEXES)
|
|
594
751
|
})
|
|
595
752
|
}
|
|
596
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
|
+
|
|
597
833
|
/**
|
|
598
834
|
* Run fn() in a write transaction with bounded SQLITE_BUSY retries.
|
|
599
835
|
* Nested calls join the outer transaction (no nested BEGIN).
|
|
@@ -700,18 +936,6 @@ export class SqliteStore {
|
|
|
700
936
|
conversation_id TEXT PRIMARY KEY, summary TEXT NOT NULL,
|
|
701
937
|
through_seq INTEGER NOT NULL, updated_at TEXT NOT NULL
|
|
702
938
|
)`)
|
|
703
|
-
this.db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS turns_fts USING fts5(content, content='turns', content_rowid='rowid')`)
|
|
704
|
-
this.db.exec(`CREATE TRIGGER IF NOT EXISTS turns_ai AFTER INSERT ON turns BEGIN
|
|
705
|
-
INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
706
|
-
END`)
|
|
707
|
-
this.db.exec(`CREATE TRIGGER IF NOT EXISTS turns_ad AFTER DELETE ON turns BEGIN
|
|
708
|
-
INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
|
|
709
|
-
END`)
|
|
710
|
-
this.db.exec(`CREATE TRIGGER IF NOT EXISTS turns_au AFTER UPDATE ON turns BEGIN
|
|
711
|
-
INSERT INTO turns_fts(turns_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
|
|
712
|
-
INSERT INTO turns_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
713
|
-
END`)
|
|
714
|
-
try { this.db.exec("INSERT INTO turns_fts(turns_fts) VALUES('rebuild')") } catch {}
|
|
715
939
|
const commitmentCols = this.db.prepare('PRAGMA table_info(commitments)').all().map((c) => c.name)
|
|
716
940
|
if (!commitmentCols.includes('done_at')) this.db.exec('ALTER TABLE commitments ADD COLUMN done_at TEXT')
|
|
717
941
|
// 0.2.0: per-commitment custom reminder offsets (JSON array of strings).
|
|
@@ -757,6 +981,11 @@ CREATE TABLE IF NOT EXISTS task_runs (
|
|
|
757
981
|
all_day INTEGER NOT NULL DEFAULT 0, tzid TEXT, location TEXT, rrule TEXT,
|
|
758
982
|
source TEXT, imported_at TEXT NOT NULL
|
|
759
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()
|
|
760
989
|
this.db.prepare('INSERT OR REPLACE INTO meta(key,value) VALUES(?,?)').run('schema_version', String(SCHEMA_VERSION))
|
|
761
990
|
} catch (error) {
|
|
762
991
|
throw error
|
|
@@ -1021,15 +1250,35 @@ CREATE TABLE IF NOT EXISTS task_runs (
|
|
|
1021
1250
|
return id
|
|
1022
1251
|
}
|
|
1023
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
|
+
*/
|
|
1024
1258
|
sumModelUsage({ since } = {}) {
|
|
1025
|
-
const
|
|
1026
|
-
? this.db.prepare(
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
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
|
+
}
|
|
1033
1282
|
}
|
|
1034
1283
|
|
|
1035
1284
|
proposeCandidate({ kind, payload, evidence }) {
|
|
@@ -1056,15 +1305,6 @@ CREATE TABLE IF NOT EXISTS task_runs (
|
|
|
1056
1305
|
return this.getCandidate(id)
|
|
1057
1306
|
}
|
|
1058
1307
|
|
|
1059
|
-
searchTurns(query, { limit = 8 } = {}) {
|
|
1060
|
-
const terms = String(query || '').split(/[^\p{L}\p{N}]+/u).filter((t) => t.length >= 2).slice(0, 8)
|
|
1061
|
-
if (!terms.length) return []
|
|
1062
|
-
const rows = this.db.prepare('SELECT * FROM turns ORDER BY created_at DESC LIMIT 2000').all()
|
|
1063
|
-
const score = (text) => terms.filter((t) => String(text).includes(t)).length
|
|
1064
|
-
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) }))
|
|
1065
|
-
.filter((r) => r.score > 0).sort((a, b) => b.score - a.score || Date.parse(b.createdAt) - Date.parse(a.createdAt)).slice(0, limit)
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
1308
|
searchMemory(query, { limit = 6, matterId } = {}) {
|
|
1069
1309
|
const terms = String(query || '').split(/[^\p{L}\p{N}]+/u).filter((t) => t.length >= 2).slice(0, 8)
|
|
1070
1310
|
if (!terms.length) return []
|
|
@@ -1089,6 +1329,19 @@ CREATE TABLE IF NOT EXISTS task_runs (
|
|
|
1089
1329
|
].sort((a, b) => b.score - a.score || a.id.localeCompare(b.id)).slice(0, limit)
|
|
1090
1330
|
}
|
|
1091
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
|
+
*/
|
|
1092
1345
|
searchTurns(query, { conversationId, matterId, limit = 8 } = {}) {
|
|
1093
1346
|
const q = String(query ?? '').trim()
|
|
1094
1347
|
if (!q) return []
|
|
@@ -1098,14 +1351,40 @@ CREATE TABLE IF NOT EXISTS task_runs (
|
|
|
1098
1351
|
if (matterId) { clauses.push('s.matter_id = ?'); filterArgs.push(matterId) }
|
|
1099
1352
|
const join = matterId ? ' JOIN sessions s ON s.conversation_id = t.conversation_id' : ''
|
|
1100
1353
|
const where = clauses.length ? ` AND ${clauses.join(' AND ')}` : ''
|
|
1101
|
-
|
|
1102
|
-
|
|
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 {
|
|
1103
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)
|
|
1104
|
-
if (rows.length) return rows.map(
|
|
1358
|
+
if (rows.length) return rows.map(map)
|
|
1359
|
+
} catch {
|
|
1360
|
+
// Malformed or special FTS query: use the LIKE scan below.
|
|
1105
1361
|
}
|
|
1106
|
-
}
|
|
1362
|
+
}
|
|
1107
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)
|
|
1108
|
-
return rows.reverse().map(
|
|
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)
|
|
1109
1388
|
}
|
|
1110
1389
|
|
|
1111
1390
|
getSessionSummary(conversationId) {
|
|
@@ -1204,70 +1483,219 @@ CREATE TABLE IF NOT EXISTS task_runs (
|
|
|
1204
1483
|
}
|
|
1205
1484
|
}
|
|
1206
1485
|
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
preferences: all('SELECT * FROM preferences').map((r) => ({
|
|
1241
|
-
id: r.id, kind: r.kind, statement: r.statement, scope: parse(r.scope, {}),
|
|
1242
|
-
source: parse(r.source, {}), evidence: parse(r.evidence), confidence: r.confidence,
|
|
1243
|
-
status: r.status, validFrom: r.valid_from, lastConfirmed: r.last_confirmed ?? undefined,
|
|
1244
|
-
expiresAt: r.expires_at ?? undefined, priority: r.priority,
|
|
1245
|
-
})),
|
|
1246
|
-
links: all('SELECT * FROM links').map((r) => ({
|
|
1247
|
-
id: r.id, fromMatter: r.from_matter, toMatter: r.to_matter, type: r.type,
|
|
1248
|
-
confidence: r.confidence, evidence: parse(r.evidence), status: r.status,
|
|
1249
|
-
createdBy: r.created_by, createdAt: r.created_at,
|
|
1250
|
-
})),
|
|
1251
|
-
updates: all('SELECT * FROM updates').map((r) => ({
|
|
1252
|
-
id: r.id, sourceMatter: r.source_matter, targetMatter: r.target_matter, change: r.change,
|
|
1253
|
-
status: r.status, createdAt: r.created_at, rollback: r.rollback ?? undefined,
|
|
1254
|
-
})),
|
|
1255
|
-
routines: all('SELECT * FROM routines').map((r) => ({
|
|
1256
|
-
id: r.id, name: r.name, description: r.description, trigger: parse(r.trigger),
|
|
1257
|
-
steps: parse(r.steps), preferences: parse(r.preferences), pitfalls: parse(r.pitfalls),
|
|
1258
|
-
verification: parse(r.verification), scope: r.scope ?? undefined, version: r.version,
|
|
1259
|
-
status: r.status, createdBy: r.created_by, createdAt: r.created_at, updatedAt: r.updated_at,
|
|
1260
|
-
})),
|
|
1261
|
-
routinePatches: all('SELECT * FROM routine_patches').map((r) => ({
|
|
1262
|
-
id: r.id, routineId: r.routine_id, baseVersion: r.base_version,
|
|
1263
|
-
changes: parse(r.changes, {}), before: parse(r.before, {}), diff: r.diff, status: r.status, createdAt: r.created_at,
|
|
1264
|
-
})),
|
|
1265
|
-
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
|
|
1266
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()
|
|
1267
1693
|
// Remember which rows this connection has seen (P1-1): save() only deletes
|
|
1268
1694
|
// rows this loader owned, so another process's concurrent additions are
|
|
1269
|
-
// 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.
|
|
1270
1697
|
this._baselineIds = idsFor(snapshot)
|
|
1698
|
+
this._fullLoad = true
|
|
1271
1699
|
return snapshot
|
|
1272
1700
|
}
|
|
1273
1701
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "advisers",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "A staff of AI advisers: a cordis-native personal secretary runtime with scoped memory, commitments, and reviewable routines.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cordis",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@pdf-lib/fontkit": "^1.1.1",
|
|
38
|
-
"advisers-core": "0.2.
|
|
39
|
-
"advisers-policy-strict": "0.2.
|
|
38
|
+
"advisers-core": "0.2.2",
|
|
39
|
+
"advisers-policy-strict": "0.2.2",
|
|
40
40
|
"jszip": "^3.10.2",
|
|
41
41
|
"pdf-lib": "^1.17.1",
|
|
42
42
|
"pdfjs-dist": "^6.5.438"
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"test": "node --experimental-strip-types --test test/*.test.js"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
|
-
"advisers-office": "0.2.
|
|
63
|
+
"advisers-office": "0.2.2"
|
|
64
64
|
},
|
|
65
65
|
"peerDependenciesMeta": {
|
|
66
66
|
"advisers-office": {
|