advisers 0.1.0-preview.4 → 0.2.0
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/LICENSE +131 -21
- package/NOTICE +34 -0
- package/README-zh.md +40 -9
- package/README.md +53 -10
- package/THIRD-PARTY-NOTICES.md +277 -0
- package/bin/advisers.js +4230 -122
- package/index.js +1 -1
- package/lib/agent.js +414 -0
- package/lib/attachments.js +283 -0
- package/lib/capture.js +168 -0
- package/lib/context-window.js +123 -0
- package/lib/digest.js +27 -0
- package/lib/doc-review.js +30 -0
- package/lib/egress.js +27 -0
- package/lib/evidence.js +1995 -0
- package/lib/ical.js +542 -0
- package/lib/imap.js +181 -0
- package/lib/importers.js +1001 -0
- package/lib/learning.js +46 -0
- package/lib/mcp-server.js +869 -0
- package/lib/model.js +307 -0
- package/lib/notify.js +189 -0
- package/lib/nudge.js +10 -0
- package/lib/ocr.js +47 -0
- package/lib/pdf.js +105 -0
- package/lib/pet-brief-store.js +51 -0
- package/lib/pet-brief.js +644 -0
- package/lib/receipt-verify.js +624 -0
- package/lib/relative-date.js +393 -0
- package/lib/remind.js +690 -0
- package/lib/research.js +193 -0
- package/lib/review.js +88 -0
- package/lib/routine.js +22 -0
- package/lib/safety/classifier.js +392 -0
- package/lib/safety/explain.js +157 -0
- package/lib/safety/index.js +333 -0
- package/lib/safety/redact.js +212 -0
- package/lib/safety/rules.js +824 -0
- package/lib/scheduler.js +399 -0
- package/lib/source.js +294 -0
- package/lib/sqlite.js +1022 -0
- package/lib/sync.js +129 -0
- package/lib/timestamp.js +45 -0
- package/lib/tos/index.js +332 -0
- package/lib/tos/manifest.json +32 -0
- package/lib/tos/privacy.zh-CN.md +136 -0
- package/lib/tos/safety.zh-CN.md +101 -0
- package/lib/tos/summary.zh-CN.md +12 -0
- package/lib/tos/terms.zh-CN.md +439 -0
- package/lib/ui.js +49 -0
- package/lib/undo.js +83 -0
- package/lib/vault.js +51 -0
- package/package.json +26 -4
package/lib/sqlite.js
ADDED
|
@@ -0,0 +1,1022 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite'
|
|
2
|
+
import { chmodSync, copyFileSync } from 'node:fs'
|
|
3
|
+
import { createHash, createHmac, randomUUID } from 'node:crypto'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Open + write hardening (2026-11-17 robustness audit P0-1/P0-3/P1-2):
|
|
7
|
+
* - busy_timeout is set before any lock-taking pragma; journal_mode=WAL is
|
|
8
|
+
* idempotent and retried with bounded backoff while another process holds
|
|
9
|
+
* the database lock.
|
|
10
|
+
* - Opening no longer runs a full 'PRAGMA quick_check' (that is O(db) and made
|
|
11
|
+
* every command slow). Integrity checks live in assertIntegrity() and are
|
|
12
|
+
* run by 'advisers doctor' / 'advisers audit --check-db'.
|
|
13
|
+
* - appendAudit() and saveWithAudit() run in BEGIN IMMEDIATE transactions that
|
|
14
|
+
* re-read the previous hash before inserting, so the audit chain cannot fork
|
|
15
|
+
* under concurrent writers and a ledger write and its audit row commit
|
|
16
|
+
* together or not at all.
|
|
17
|
+
* - Migrations are refused for a schema newer than this CLI and are preceded
|
|
18
|
+
* by a same-directory backup of the old database.
|
|
19
|
+
*/
|
|
20
|
+
export const SCHEMA_VERSION = 18
|
|
21
|
+
const DEFAULT_BUSY_TIMEOUT_MS = 5000
|
|
22
|
+
const OPEN_RETRIES = 6
|
|
23
|
+
const BUSY_ERRCODES = new Set([5, 6]) // SQLITE_BUSY, SQLITE_LOCKED
|
|
24
|
+
|
|
25
|
+
function isBusyError(error) {
|
|
26
|
+
if (!error) return false
|
|
27
|
+
if (typeof error.errcode === 'number' && BUSY_ERRCODES.has(error.errcode)) return true
|
|
28
|
+
return /SQLITE_BUSY|SQLITE_LOCKED|database (?:table )?is locked/i.test(String(error.message ?? error))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function sleepSync(ms) {
|
|
32
|
+
if (!ms || ms <= 0) return
|
|
33
|
+
try {
|
|
34
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms)
|
|
35
|
+
} catch {
|
|
36
|
+
const end = Date.now() + ms
|
|
37
|
+
while (Date.now() < end) {}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function backoffMs(attempt) {
|
|
42
|
+
return Math.min(2000, 50 * 2 ** attempt)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Ledger tables are written as a whole snapshot by save(). The id column lets
|
|
46
|
+
// save() merge with rows other processes committed after our load().
|
|
47
|
+
const LEDGER_TABLES = [
|
|
48
|
+
['matters', 'id'],
|
|
49
|
+
['facts', 'id'],
|
|
50
|
+
['commitments', 'id'],
|
|
51
|
+
['preferences', 'id'],
|
|
52
|
+
['links', 'id'],
|
|
53
|
+
['updates', 'id'],
|
|
54
|
+
['routines', 'id'],
|
|
55
|
+
['routine_patches', 'id'],
|
|
56
|
+
['routes', 'key'],
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
function idsFor(snapshot) {
|
|
60
|
+
const ids = {}
|
|
61
|
+
for (const [table, key] of LEDGER_TABLES) {
|
|
62
|
+
const value = snapshot?.[table]
|
|
63
|
+
const rows = table === 'routes' ? Object.keys(value ?? {}) : (value ?? []).map((row) => row?.id)
|
|
64
|
+
ids[table] = new Set(rows.filter((id) => id != null))
|
|
65
|
+
}
|
|
66
|
+
return ids
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const SCHEMA = `
|
|
70
|
+
CREATE TABLE IF NOT EXISTS matters (
|
|
71
|
+
id TEXT PRIMARY KEY, title TEXT NOT NULL, status TEXT NOT NULL, domain TEXT NOT NULL,
|
|
72
|
+
secrecy INTEGER NOT NULL, summary TEXT, participants TEXT NOT NULL, deadlines TEXT NOT NULL,
|
|
73
|
+
open_questions TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL
|
|
74
|
+
);
|
|
75
|
+
CREATE TABLE IF NOT EXISTS facts (
|
|
76
|
+
id TEXT PRIMARY KEY, matter_id TEXT, subject TEXT NOT NULL, predicate TEXT NOT NULL,
|
|
77
|
+
object TEXT NOT NULL, recorded_at TEXT NOT NULL, authority TEXT NOT NULL, confidence REAL NOT NULL,
|
|
78
|
+
source TEXT, status TEXT NOT NULL,
|
|
79
|
+
valid_from TEXT, valid_to TEXT, expired_at TEXT, evidence TEXT, supersedes TEXT,
|
|
80
|
+
superseded_by TEXT, shadowed_by TEXT, last_seen_at TEXT, deleted_at TEXT, deleted_reason TEXT
|
|
81
|
+
);
|
|
82
|
+
CREATE TABLE IF NOT EXISTS commitments (
|
|
83
|
+
id TEXT PRIMARY KEY, matter_id TEXT NOT NULL, debtor TEXT NOT NULL, creditor TEXT NOT NULL,
|
|
84
|
+
action TEXT NOT NULL, due_at TEXT, timezone TEXT NOT NULL, status TEXT NOT NULL,
|
|
85
|
+
evidence TEXT NOT NULL, capability TEXT NOT NULL, done_at TEXT, reminder_offsets TEXT
|
|
86
|
+
);
|
|
87
|
+
CREATE TABLE IF NOT EXISTS preferences (
|
|
88
|
+
id TEXT PRIMARY KEY, kind TEXT NOT NULL, statement TEXT NOT NULL, scope TEXT NOT NULL,
|
|
89
|
+
source TEXT NOT NULL, evidence TEXT NOT NULL, confidence REAL NOT NULL, status TEXT NOT NULL,
|
|
90
|
+
valid_from TEXT NOT NULL, last_confirmed TEXT, expires_at TEXT, priority TEXT NOT NULL
|
|
91
|
+
);
|
|
92
|
+
CREATE TABLE IF NOT EXISTS links (
|
|
93
|
+
id TEXT PRIMARY KEY, from_matter TEXT NOT NULL, to_matter TEXT NOT NULL, type TEXT NOT NULL,
|
|
94
|
+
confidence REAL NOT NULL, evidence TEXT NOT NULL, status TEXT NOT NULL, created_by TEXT NOT NULL,
|
|
95
|
+
created_at TEXT NOT NULL
|
|
96
|
+
);
|
|
97
|
+
CREATE TABLE IF NOT EXISTS updates (
|
|
98
|
+
id TEXT PRIMARY KEY, source_matter TEXT NOT NULL, target_matter TEXT NOT NULL, change TEXT NOT NULL,
|
|
99
|
+
status TEXT NOT NULL, created_at TEXT NOT NULL, rollback TEXT
|
|
100
|
+
);
|
|
101
|
+
CREATE TABLE IF NOT EXISTS routines (
|
|
102
|
+
id TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL, trigger TEXT NOT NULL,
|
|
103
|
+
steps TEXT NOT NULL, preferences TEXT NOT NULL, pitfalls TEXT NOT NULL, verification TEXT NOT NULL,
|
|
104
|
+
scope TEXT, version INTEGER NOT NULL, status TEXT NOT NULL, created_by TEXT NOT NULL,
|
|
105
|
+
created_at TEXT NOT NULL, updated_at TEXT NOT NULL
|
|
106
|
+
);
|
|
107
|
+
CREATE TABLE IF NOT EXISTS routine_patches (
|
|
108
|
+
id TEXT PRIMARY KEY, routine_id TEXT NOT NULL, base_version INTEGER NOT NULL,
|
|
109
|
+
changes TEXT NOT NULL, before TEXT, diff TEXT NOT NULL, status TEXT NOT NULL, created_at TEXT NOT NULL
|
|
110
|
+
);
|
|
111
|
+
CREATE TABLE IF NOT EXISTS permit_consumed (
|
|
112
|
+
permit_id TEXT PRIMARY KEY, expires_at INTEGER NOT NULL, consumed_at TEXT NOT NULL
|
|
113
|
+
);
|
|
114
|
+
CREATE TABLE IF NOT EXISTS research_runs (
|
|
115
|
+
id TEXT PRIMARY KEY, matter_id TEXT, query TEXT NOT NULL, answer TEXT NOT NULL,
|
|
116
|
+
sources TEXT NOT NULL, usage TEXT, stop_reason TEXT, created_at TEXT NOT NULL
|
|
117
|
+
);
|
|
118
|
+
CREATE TABLE IF NOT EXISTS model_calls (
|
|
119
|
+
id TEXT PRIMARY KEY, conversation_id TEXT, matter_id TEXT, turn_id TEXT, task TEXT NOT NULL,
|
|
120
|
+
model TEXT NOT NULL, effort TEXT, thinking INTEGER NOT NULL DEFAULT 0, status TEXT NOT NULL DEFAULT 'ok',
|
|
121
|
+
code TEXT, attempts INTEGER NOT NULL DEFAULT 1, prompt_tokens INTEGER NOT NULL DEFAULT 0,
|
|
122
|
+
completion_tokens INTEGER NOT NULL DEFAULT 0, reasoning_tokens INTEGER NOT NULL DEFAULT 0,
|
|
123
|
+
cached_tokens INTEGER NOT NULL DEFAULT 0, total_tokens INTEGER NOT NULL DEFAULT 0,
|
|
124
|
+
duration_ms INTEGER NOT NULL DEFAULT 0, finish_reason TEXT, created_at TEXT NOT NULL
|
|
125
|
+
);
|
|
126
|
+
CREATE TABLE IF NOT EXISTS memory_candidates (
|
|
127
|
+
id TEXT PRIMARY KEY, kind TEXT NOT NULL, payload TEXT NOT NULL, evidence TEXT NOT NULL,
|
|
128
|
+
status TEXT NOT NULL DEFAULT 'pending', created_at TEXT NOT NULL, reviewed_at TEXT, reject_reason TEXT
|
|
129
|
+
);
|
|
130
|
+
CREATE TABLE IF NOT EXISTS routes (key TEXT PRIMARY KEY, matter_id TEXT NOT NULL);
|
|
131
|
+
CREATE TABLE IF NOT EXISTS seen_events (
|
|
132
|
+
link_id TEXT NOT NULL, account_id TEXT NOT NULL, dedupe_key TEXT NOT NULL,
|
|
133
|
+
seen_at TEXT NOT NULL, PRIMARY KEY(link_id, account_id, dedupe_key)
|
|
134
|
+
);
|
|
135
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
136
|
+
conversation_id TEXT PRIMARY KEY, matter_id TEXT NOT NULL, title TEXT NOT NULL,
|
|
137
|
+
created_at TEXT NOT NULL, updated_at TEXT NOT NULL
|
|
138
|
+
);
|
|
139
|
+
CREATE TABLE IF NOT EXISTS deliveries (
|
|
140
|
+
id TEXT PRIMARY KEY, matter_id TEXT NOT NULL, conversation_id TEXT NOT NULL,
|
|
141
|
+
files TEXT NOT NULL, tool TEXT, title TEXT, validation TEXT, created_at TEXT NOT NULL
|
|
142
|
+
);
|
|
143
|
+
CREATE TABLE IF NOT EXISTS audit (
|
|
144
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT, at TEXT NOT NULL, actor TEXT NOT NULL,
|
|
145
|
+
action TEXT NOT NULL, allowed INTEGER NOT NULL, reason TEXT, prev_hash TEXT, hash TEXT NOT NULL
|
|
146
|
+
);
|
|
147
|
+
CREATE TABLE IF NOT EXISTS turns (
|
|
148
|
+
id TEXT PRIMARY KEY, conversation_id TEXT NOT NULL, seq INTEGER NOT NULL,
|
|
149
|
+
role TEXT NOT NULL, content TEXT NOT NULL, created_at TEXT NOT NULL,
|
|
150
|
+
UNIQUE(conversation_id, seq)
|
|
151
|
+
);
|
|
152
|
+
CREATE TABLE IF NOT EXISTS session_summaries (
|
|
153
|
+
conversation_id TEXT PRIMARY KEY, summary TEXT NOT NULL,
|
|
154
|
+
through_seq INTEGER NOT NULL, updated_at TEXT NOT NULL
|
|
155
|
+
);
|
|
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
|
+
CREATE TABLE IF NOT EXISTS summary_versions (
|
|
168
|
+
conversation_id TEXT NOT NULL, version INTEGER NOT NULL, summary TEXT NOT NULL,
|
|
169
|
+
through_seq INTEGER NOT NULL, pins TEXT, created_at TEXT NOT NULL,
|
|
170
|
+
PRIMARY KEY (conversation_id, version)
|
|
171
|
+
);
|
|
172
|
+
CREATE TABLE IF NOT EXISTS task_runs (
|
|
173
|
+
id TEXT PRIMARY KEY, conversation_id TEXT NOT NULL, matter_id TEXT, task TEXT NOT NULL,
|
|
174
|
+
tools TEXT NOT NULL, artifacts TEXT NOT NULL, outcome TEXT NOT NULL, created_at TEXT NOT NULL
|
|
175
|
+
);
|
|
176
|
+
CREATE TABLE IF NOT EXISTS nudges (
|
|
177
|
+
id TEXT PRIMARY KEY, at TEXT NOT NULL, local_day TEXT NOT NULL, level TEXT NOT NULL,
|
|
178
|
+
commitment_id TEXT NOT NULL, dedupe_key TEXT NOT NULL UNIQUE, body TEXT NOT NULL,
|
|
179
|
+
outcome TEXT NOT NULL DEFAULT 'pending', outcome_at TEXT
|
|
180
|
+
);
|
|
181
|
+
CREATE TABLE IF NOT EXISTS calendar_events (
|
|
182
|
+
uid TEXT PRIMARY KEY, summary TEXT NOT NULL, starts_at TEXT, ends_at TEXT,
|
|
183
|
+
all_day INTEGER NOT NULL DEFAULT 0, tzid TEXT, location TEXT, rrule TEXT,
|
|
184
|
+
source TEXT, imported_at TEXT NOT NULL
|
|
185
|
+
);
|
|
186
|
+
CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
187
|
+
`
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* P2-1: secondary indexes for the hot read/write paths (tick, today, history,
|
|
191
|
+
* deliveries). Indexes are layout only, not a schema change, so they are
|
|
192
|
+
* created with IF NOT EXISTS after migrate() on every open: new databases get
|
|
193
|
+
* them at creation time and existing databases get them the first time they
|
|
194
|
+
* are opened by this version. Each one was a full table SCAN + TEMP B-TREE in
|
|
195
|
+
* the 2026-11-17 performance audit (nudges/facts/deliveries/calendar_events/
|
|
196
|
+
* task_runs/sessions/model_calls/preferences/commitments).
|
|
197
|
+
*/
|
|
198
|
+
const INDEXES = `
|
|
199
|
+
CREATE INDEX IF NOT EXISTS idx_nudges_commitment_at ON nudges(commitment_id, at);
|
|
200
|
+
CREATE INDEX IF NOT EXISTS idx_nudges_local_day_level ON nudges(local_day, level);
|
|
201
|
+
CREATE INDEX IF NOT EXISTS idx_nudges_at ON nudges(at);
|
|
202
|
+
CREATE INDEX IF NOT EXISTS idx_commitments_status_due ON commitments(status, due_at);
|
|
203
|
+
CREATE INDEX IF NOT EXISTS idx_facts_matter ON facts(matter_id);
|
|
204
|
+
CREATE INDEX IF NOT EXISTS idx_facts_status_deleted ON facts(status, deleted_at);
|
|
205
|
+
CREATE INDEX IF NOT EXISTS idx_preferences_status_expires ON preferences(status, expires_at);
|
|
206
|
+
CREATE INDEX IF NOT EXISTS idx_calendar_events_starts ON calendar_events(starts_at);
|
|
207
|
+
CREATE INDEX IF NOT EXISTS idx_deliveries_created ON deliveries(created_at);
|
|
208
|
+
CREATE INDEX IF NOT EXISTS idx_task_runs_created ON task_runs(created_at);
|
|
209
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_updated ON sessions(updated_at);
|
|
210
|
+
CREATE INDEX IF NOT EXISTS idx_model_calls_created ON model_calls(created_at);
|
|
211
|
+
`
|
|
212
|
+
|
|
213
|
+
const json = (v) => JSON.stringify(v ?? [])
|
|
214
|
+
const parse = (v, fallback = []) => {
|
|
215
|
+
if (v == null) return fallback
|
|
216
|
+
try { return JSON.parse(v) } catch { return fallback }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export class SqliteStore {
|
|
220
|
+
constructor(path, { readOnly = false, checkIntegrity = false, busyTimeoutMs = Number(process.env.ADVISERS_SQLITE_BUSY_TIMEOUT_MS) || DEFAULT_BUSY_TIMEOUT_MS } = {}) {
|
|
221
|
+
this.path = path
|
|
222
|
+
this.busyTimeoutMs = busyTimeoutMs
|
|
223
|
+
this._transactionDepth = 0
|
|
224
|
+
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
|
|
233
|
+
}
|
|
234
|
+
this.enableWal()
|
|
235
|
+
this.db.exec('PRAGMA secure_delete = ON')
|
|
236
|
+
this.db.exec('PRAGMA synchronous = NORMAL')
|
|
237
|
+
this.initializeSchema()
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Full-database integrity check; explicit because it is O(database size). */
|
|
241
|
+
assertIntegrity() {
|
|
242
|
+
const row = this.db.prepare('PRAGMA quick_check').get()
|
|
243
|
+
const verdict = row?.quick_check ?? 'unknown'
|
|
244
|
+
if (verdict !== 'ok') throw new Error(`database integrity check failed: ${verdict}`)
|
|
245
|
+
return verdict
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** WAL is persistent; skip the switch when already enabled and retry on busy. */
|
|
249
|
+
enableWal() {
|
|
250
|
+
if (!this.path || this.path === ':memory:') return 'memory'
|
|
251
|
+
const current = String(this.db.prepare('PRAGMA journal_mode').get()?.journal_mode ?? '').toLowerCase()
|
|
252
|
+
if (current === 'wal' || current === 'memory') return current
|
|
253
|
+
let lastError
|
|
254
|
+
for (let attempt = 0; attempt <= OPEN_RETRIES; attempt++) {
|
|
255
|
+
try {
|
|
256
|
+
const result = this.db.prepare('PRAGMA journal_mode = WAL').get()
|
|
257
|
+
const mode = String(result?.journal_mode ?? '').toLowerCase()
|
|
258
|
+
if (mode === 'wal' || mode === 'memory') return mode
|
|
259
|
+
lastError = new Error(`could not enable WAL journal mode (got '${mode || 'unknown'}')`)
|
|
260
|
+
} catch (error) {
|
|
261
|
+
lastError = error
|
|
262
|
+
if (!isBusyError(error)) throw error
|
|
263
|
+
}
|
|
264
|
+
sleepSync(backoffMs(attempt))
|
|
265
|
+
}
|
|
266
|
+
throw lastError
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
initializeSchema() {
|
|
270
|
+
this._transaction('IMMEDIATE', () => {
|
|
271
|
+
this.db.exec(SCHEMA)
|
|
272
|
+
this.migrate()
|
|
273
|
+
// Indexes come after migrate(): some index the columns migrate() adds
|
|
274
|
+
// (e.g. facts.deleted_at), so an old database must be upgraded first.
|
|
275
|
+
this.db.exec(INDEXES)
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Run fn() in a write transaction with bounded SQLITE_BUSY retries.
|
|
281
|
+
* Nested calls join the outer transaction (no nested BEGIN).
|
|
282
|
+
*/
|
|
283
|
+
_transaction(mode, fn, { retries = OPEN_RETRIES } = {}) {
|
|
284
|
+
if (this._transactionDepth > 0) return fn()
|
|
285
|
+
let lastError
|
|
286
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
287
|
+
let began = false
|
|
288
|
+
try {
|
|
289
|
+
this.db.exec(`BEGIN ${mode}`)
|
|
290
|
+
began = true
|
|
291
|
+
this._transactionDepth++
|
|
292
|
+
let result
|
|
293
|
+
try {
|
|
294
|
+
result = fn()
|
|
295
|
+
} finally {
|
|
296
|
+
this._transactionDepth--
|
|
297
|
+
}
|
|
298
|
+
this.db.exec('COMMIT')
|
|
299
|
+
return result
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (began) { try { this.db.exec('ROLLBACK') } catch {} }
|
|
302
|
+
lastError = error
|
|
303
|
+
if (!isBusyError(error) || attempt === retries) throw error
|
|
304
|
+
}
|
|
305
|
+
sleepSync(backoffMs(attempt))
|
|
306
|
+
}
|
|
307
|
+
throw lastError
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Public transaction helper for multi-statement invariants (returns fn result). */
|
|
311
|
+
transaction(fn, { mode = 'IMMEDIATE' } = {}) {
|
|
312
|
+
return this._transaction(mode, fn)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Checkpoint the WAL so a copied/sealed database file is self-contained.
|
|
317
|
+
* Returns busy=true when another connection prevented a full checkpoint.
|
|
318
|
+
*/
|
|
319
|
+
checkpoint({ mode = 'TRUNCATE', retries = OPEN_RETRIES } = {}) {
|
|
320
|
+
let last = null
|
|
321
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
322
|
+
last = this.db.prepare(`PRAGMA wal_checkpoint(${mode})`).get()
|
|
323
|
+
if (!last || Number(last.busy) === 0) {
|
|
324
|
+
return { busy: false, log: Number(last?.log ?? 0), checkpointed: Number(last?.checkpointed ?? 0) }
|
|
325
|
+
}
|
|
326
|
+
sleepSync(backoffMs(attempt))
|
|
327
|
+
}
|
|
328
|
+
return { busy: Number(last?.busy ?? 1) !== 0, log: Number(last?.log ?? 0), checkpointed: Number(last?.checkpointed ?? 0) }
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
backupBeforeMigrate(current) {
|
|
332
|
+
if (!this.path || this.path === ':memory:') return
|
|
333
|
+
const target = `${this.path}.before-migrate-v${current}-${Date.now()}`
|
|
334
|
+
try {
|
|
335
|
+
copyFileSync(this.path, target)
|
|
336
|
+
try { chmodSync(target, 0o600) } catch {}
|
|
337
|
+
} catch (error) {
|
|
338
|
+
throw new Error(`refusing to migrate without a backup of ${this.path}: ${error.message}`)
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Bring an older database up to SCHEMA_VERSION. Runs inside the caller's
|
|
344
|
+
* write transaction (initializeSchema). Refuses futures schemas and keeps
|
|
345
|
+
* a same-directory backup before altering an existing database.
|
|
346
|
+
*/
|
|
347
|
+
migrate() {
|
|
348
|
+
const row = this.db.prepare('SELECT value FROM meta WHERE key = ?').get('schema_version')
|
|
349
|
+
const current = row ? Number(row.value) : 0
|
|
350
|
+
if (!Number.isFinite(current) || current < 0) throw new Error(`invalid schema_version in ${this.path}: ${row?.value}`)
|
|
351
|
+
if (current > SCHEMA_VERSION) throw new Error(`database schema v${current} is newer than this CLI (v${SCHEMA_VERSION}); upgrade advisers before opening ${this.path}`)
|
|
352
|
+
if (current >= SCHEMA_VERSION) return
|
|
353
|
+
if (current > 0) this.backupBeforeMigrate(current)
|
|
354
|
+
try {
|
|
355
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS seen_events (
|
|
356
|
+
link_id TEXT NOT NULL, account_id TEXT NOT NULL, dedupe_key TEXT NOT NULL,
|
|
357
|
+
seen_at TEXT NOT NULL, PRIMARY KEY(link_id, account_id, dedupe_key)
|
|
358
|
+
)`)
|
|
359
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS sessions (
|
|
360
|
+
conversation_id TEXT PRIMARY KEY, matter_id TEXT NOT NULL, title TEXT NOT NULL,
|
|
361
|
+
created_at TEXT NOT NULL, updated_at TEXT NOT NULL
|
|
362
|
+
)`)
|
|
363
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS deliveries (
|
|
364
|
+
id TEXT PRIMARY KEY, matter_id TEXT NOT NULL, conversation_id TEXT NOT NULL,
|
|
365
|
+
files TEXT NOT NULL, tool TEXT, title TEXT, validation TEXT, created_at TEXT NOT NULL
|
|
366
|
+
)`)
|
|
367
|
+
const cols = this.db.prepare('PRAGMA table_info(deliveries)').all().map((c) => c.name)
|
|
368
|
+
if (!cols.includes('title')) this.db.exec('ALTER TABLE deliveries ADD COLUMN title TEXT')
|
|
369
|
+
if (!cols.includes('validation')) this.db.exec('ALTER TABLE deliveries ADD COLUMN validation TEXT')
|
|
370
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS audit (
|
|
371
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT, at TEXT NOT NULL, actor TEXT NOT NULL,
|
|
372
|
+
action TEXT NOT NULL, allowed INTEGER NOT NULL, reason TEXT, prev_hash TEXT, hash TEXT NOT NULL
|
|
373
|
+
)`)
|
|
374
|
+
const patchCols = this.db.prepare('PRAGMA table_info(routine_patches)').all().map((c) => c.name)
|
|
375
|
+
if (!patchCols.includes('before')) this.db.exec('ALTER TABLE routine_patches ADD COLUMN before TEXT')
|
|
376
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS turns (
|
|
377
|
+
id TEXT PRIMARY KEY, conversation_id TEXT NOT NULL, seq INTEGER NOT NULL,
|
|
378
|
+
role TEXT NOT NULL, content TEXT NOT NULL, created_at TEXT NOT NULL,
|
|
379
|
+
UNIQUE(conversation_id, seq)
|
|
380
|
+
)`)
|
|
381
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS session_summaries (
|
|
382
|
+
conversation_id TEXT PRIMARY KEY, summary TEXT NOT NULL,
|
|
383
|
+
through_seq INTEGER NOT NULL, updated_at TEXT NOT NULL
|
|
384
|
+
)`)
|
|
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
|
+
const commitmentCols = this.db.prepare('PRAGMA table_info(commitments)').all().map((c) => c.name)
|
|
398
|
+
if (!commitmentCols.includes('done_at')) this.db.exec('ALTER TABLE commitments ADD COLUMN done_at TEXT')
|
|
399
|
+
// 0.2.0: per-commitment custom reminder offsets (JSON array of strings).
|
|
400
|
+
if (!commitmentCols.includes('reminder_offsets')) this.db.exec('ALTER TABLE commitments ADD COLUMN reminder_offsets TEXT')
|
|
401
|
+
const factCols = this.db.prepare('PRAGMA table_info(facts)').all().map((c) => c.name)
|
|
402
|
+
for (const [name, type] of [['valid_from','TEXT'],['valid_to','TEXT'],['expired_at','TEXT'],['evidence','TEXT'],['supersedes','TEXT'],['superseded_by','TEXT'],['shadowed_by','TEXT'],['last_seen_at','TEXT'],['deleted_at','TEXT'],['deleted_reason','TEXT']]) {
|
|
403
|
+
if (!factCols.includes(name)) this.db.exec(`ALTER TABLE facts ADD COLUMN ${name} ${type}`)
|
|
404
|
+
}
|
|
405
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS summary_versions (
|
|
406
|
+
conversation_id TEXT NOT NULL, version INTEGER NOT NULL, summary TEXT NOT NULL,
|
|
407
|
+
through_seq INTEGER NOT NULL, pins TEXT, created_at TEXT NOT NULL,
|
|
408
|
+
PRIMARY KEY (conversation_id, version)
|
|
409
|
+
);
|
|
410
|
+
CREATE TABLE IF NOT EXISTS task_runs (
|
|
411
|
+
id TEXT PRIMARY KEY, conversation_id TEXT NOT NULL, matter_id TEXT, task TEXT NOT NULL,
|
|
412
|
+
tools TEXT NOT NULL, artifacts TEXT NOT NULL, outcome TEXT NOT NULL, created_at TEXT NOT NULL
|
|
413
|
+
);
|
|
414
|
+
CREATE TABLE IF NOT EXISTS nudges (
|
|
415
|
+
id TEXT PRIMARY KEY, at TEXT NOT NULL, local_day TEXT NOT NULL, level TEXT NOT NULL,
|
|
416
|
+
commitment_id TEXT NOT NULL, dedupe_key TEXT NOT NULL UNIQUE, body TEXT NOT NULL
|
|
417
|
+
)`)
|
|
418
|
+
const nudgeCols = this.db.prepare('PRAGMA table_info(nudges)').all().map((c) => c.name)
|
|
419
|
+
if (!nudgeCols.includes('outcome')) this.db.exec("ALTER TABLE nudges ADD COLUMN outcome TEXT NOT NULL DEFAULT 'pending'")
|
|
420
|
+
if (!nudgeCols.includes('outcome_at')) this.db.exec('ALTER TABLE nudges ADD COLUMN outcome_at TEXT')
|
|
421
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS summary_versions (
|
|
422
|
+
conversation_id TEXT NOT NULL, version INTEGER NOT NULL, summary TEXT NOT NULL,
|
|
423
|
+
through_seq INTEGER NOT NULL, pins TEXT, created_at TEXT NOT NULL,
|
|
424
|
+
PRIMARY KEY (conversation_id, version)
|
|
425
|
+
);
|
|
426
|
+
CREATE TABLE IF NOT EXISTS task_runs (
|
|
427
|
+
id TEXT PRIMARY KEY, conversation_id TEXT NOT NULL, task TEXT NOT NULL,
|
|
428
|
+
tools TEXT NOT NULL, artifacts TEXT NOT NULL, outcome TEXT NOT NULL, created_at TEXT NOT NULL
|
|
429
|
+
)`)
|
|
430
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS summary_versions (
|
|
431
|
+
conversation_id TEXT NOT NULL, version INTEGER NOT NULL, summary TEXT NOT NULL,
|
|
432
|
+
through_seq INTEGER NOT NULL, pins TEXT, created_at TEXT NOT NULL,
|
|
433
|
+
PRIMARY KEY (conversation_id, version)
|
|
434
|
+
)`)
|
|
435
|
+
const runCols = this.db.prepare('PRAGMA table_info(task_runs)').all().map((c) => c.name)
|
|
436
|
+
if (!runCols.includes('matter_id')) this.db.exec('ALTER TABLE task_runs ADD COLUMN matter_id TEXT')
|
|
437
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS calendar_events (
|
|
438
|
+
uid TEXT PRIMARY KEY, summary TEXT NOT NULL, starts_at TEXT, ends_at TEXT,
|
|
439
|
+
all_day INTEGER NOT NULL DEFAULT 0, tzid TEXT, location TEXT, rrule TEXT,
|
|
440
|
+
source TEXT, imported_at TEXT NOT NULL
|
|
441
|
+
)`)
|
|
442
|
+
this.db.prepare('INSERT OR REPLACE INTO meta(key,value) VALUES(?,?)').run('schema_version', String(SCHEMA_VERSION))
|
|
443
|
+
} catch (error) {
|
|
444
|
+
throw error
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
upsertSession({ conversationId, matterId, title }) {
|
|
449
|
+
const now = new Date().toISOString()
|
|
450
|
+
this.db.prepare(`INSERT INTO sessions(conversation_id,matter_id,title,created_at,updated_at)
|
|
451
|
+
VALUES(?,?,?,?,?)
|
|
452
|
+
ON CONFLICT(conversation_id) DO UPDATE SET matter_id=excluded.matter_id, title=excluded.title, updated_at=excluded.updated_at`)
|
|
453
|
+
.run(conversationId, matterId, title.slice(0, 120), now, now)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
listSessions(limit = 20) {
|
|
457
|
+
return this.db.prepare('SELECT * FROM sessions ORDER BY updated_at DESC LIMIT ?').all(limit)
|
|
458
|
+
.map((r) => ({ conversationId: r.conversation_id, matterId: r.matter_id, title: r.title, createdAt: r.created_at, updatedAt: r.updated_at }))
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
getSession(conversationId) {
|
|
462
|
+
const r = this.db.prepare('SELECT * FROM sessions WHERE conversation_id = ?').get(conversationId)
|
|
463
|
+
return r ? { conversationId: r.conversation_id, matterId: r.matter_id, title: r.title, createdAt: r.created_at, updatedAt: r.updated_at } : undefined
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
addDelivery({ matterId, conversationId, files, tool, title, validation }) {
|
|
467
|
+
const id = `del_${randomUUID().replace(/-/g, '').slice(0, 16)}`
|
|
468
|
+
this.db.prepare('INSERT INTO deliveries(id,matter_id,conversation_id,files,tool,title,validation,created_at) VALUES(?,?,?,?,?,?,?,?)')
|
|
469
|
+
.run(id, matterId, conversationId, JSON.stringify(files ?? []), tool ?? null, title ?? null, validation ?? null, new Date().toISOString())
|
|
470
|
+
return id
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
listDeliveries(limit = 20) {
|
|
474
|
+
return this.db.prepare('SELECT * FROM deliveries ORDER BY created_at DESC LIMIT ?').all(limit)
|
|
475
|
+
.map((r) => ({ id: r.id, matterId: r.matter_id, conversationId: r.conversation_id, files: parse(r.files), tool: r.tool ?? undefined, title: r.title ?? undefined, validation: r.validation ?? undefined, createdAt: r.created_at }))
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
getMeta(key) {
|
|
479
|
+
return this.db.prepare('SELECT value FROM meta WHERE key = ?').get(key)?.value
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
setMeta(key, value) {
|
|
483
|
+
this.db.prepare('INSERT OR REPLACE INTO meta(key,value) VALUES(?,?)').run(key, String(value))
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
deleteMeta(key) {
|
|
487
|
+
this.db.prepare('DELETE FROM meta WHERE key = ?').run(key)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
addTaskRun({ conversationId, matterId, task, tools, artifacts, outcome }) {
|
|
491
|
+
const id = `run_${randomUUID().replace(/-/g, '').slice(0, 16)}`
|
|
492
|
+
this.db.prepare('INSERT INTO task_runs(id,conversation_id,matter_id,task,tools,artifacts,outcome,created_at) VALUES(?,?,?,?,?,?,?,?)')
|
|
493
|
+
.run(id, conversationId, matterId ?? null, task, json(tools), json(artifacts), outcome, new Date().toISOString())
|
|
494
|
+
return id
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
listTaskRuns(limit = 20) {
|
|
498
|
+
return this.db.prepare('SELECT * FROM task_runs ORDER BY created_at DESC LIMIT ?').all(limit)
|
|
499
|
+
.map((r) => ({ id: r.id, conversationId: r.conversation_id, matterId: r.matter_id ?? undefined, task: r.task, tools: parse(r.tools), artifacts: parse(r.artifacts), outcome: r.outcome, createdAt: r.created_at }))
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
addNudge({ localDay, level, commitmentId, body }) {
|
|
503
|
+
const key = `${localDay}:${level}:${commitmentId}`
|
|
504
|
+
const id = `nudge_${randomUUID().replace(/-/g, '').slice(0, 16)}`
|
|
505
|
+
try {
|
|
506
|
+
this.db.prepare('INSERT INTO nudges(id,at,local_day,level,commitment_id,dedupe_key,body) VALUES(?,?,?,?,?,?,?)')
|
|
507
|
+
.run(id, new Date().toISOString(), localDay, level, commitmentId, key, body)
|
|
508
|
+
return { id, dedupeKey: key }
|
|
509
|
+
} catch {
|
|
510
|
+
return null
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
setNudgeOutcome(id, outcome) {
|
|
515
|
+
return Number(this.db.prepare('UPDATE nudges SET outcome = ?, outcome_at = ? WHERE id = ?').run(outcome, new Date().toISOString(), id).changes) > 0
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
listCommitmentNudges(commitmentId, limit = 3) {
|
|
519
|
+
return this.db.prepare('SELECT * FROM nudges WHERE commitment_id = ? ORDER BY at DESC LIMIT ?').all(commitmentId, limit)
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
countNudges(localDay, level) {
|
|
523
|
+
return this.db.prepare('SELECT COUNT(*) AS n FROM nudges WHERE local_day = ? AND level = ?').get(localDay, level).n
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* P0-2 (tick N+1 removal): insert every nudge for one tick in a single
|
|
528
|
+
* transaction with one prepared statement. INSERT OR IGNORE keeps the
|
|
529
|
+
* per-(local_day, level, commitment_id) dedupe key semantics of addNudge()
|
|
530
|
+
* without aborting the batch on an expected duplicate. Returns the number of
|
|
531
|
+
* rows actually inserted (existing rows are left untouched).
|
|
532
|
+
*/
|
|
533
|
+
addNudges(rows) {
|
|
534
|
+
if (!rows?.length) return 0
|
|
535
|
+
return this._transaction('IMMEDIATE', () => {
|
|
536
|
+
const statement = this.db.prepare('INSERT OR IGNORE INTO nudges(id,at,local_day,level,commitment_id,dedupe_key,body) VALUES(?,?,?,?,?,?,?)')
|
|
537
|
+
const now = new Date().toISOString()
|
|
538
|
+
let inserted = 0
|
|
539
|
+
for (const row of rows) {
|
|
540
|
+
const key = `${row.localDay}:${row.level}:${row.commitmentId}`
|
|
541
|
+
const id = `nudge_${randomUUID().replace(/-/g, '').slice(0, 16)}`
|
|
542
|
+
inserted += Number(statement.run(id, row.at ?? now, row.localDay, row.level, row.commitmentId, key, row.body).changes)
|
|
543
|
+
}
|
|
544
|
+
return inserted
|
|
545
|
+
})
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
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.
|
|
552
|
+
*/
|
|
553
|
+
recentNudgeOutcomes(limit = 3) {
|
|
554
|
+
const rows = this.db.prepare(`SELECT commitment_id, outcome, at FROM (
|
|
555
|
+
SELECT commitment_id, outcome, at, ROW_NUMBER() OVER (PARTITION BY commitment_id ORDER BY at DESC) AS rn
|
|
556
|
+
FROM nudges
|
|
557
|
+
) WHERE rn <= ?`).all(limit)
|
|
558
|
+
const byCommitment = new Map()
|
|
559
|
+
for (const row of rows) {
|
|
560
|
+
const list = byCommitment.get(row.commitment_id)
|
|
561
|
+
if (list) list.push(row.outcome)
|
|
562
|
+
else byCommitment.set(row.commitment_id, [row.outcome])
|
|
563
|
+
}
|
|
564
|
+
return byCommitment
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** P0-2 (tick N+1 removal): today's nudge count per level in one query. */
|
|
568
|
+
countNudgesByDay(localDay) {
|
|
569
|
+
const rows = this.db.prepare('SELECT level, COUNT(*) AS n FROM nudges WHERE local_day = ? GROUP BY level').all(localDay)
|
|
570
|
+
return new Map(rows.map((row) => [row.level, Number(row.n)]))
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/** P0-2 (tick N+1 removal): existing (commitment, level) nudge keys for a day. */
|
|
574
|
+
listNudgeLevelKeysByDay(localDay) {
|
|
575
|
+
const rows = this.db.prepare('SELECT commitment_id, level FROM nudges WHERE local_day = ?').all(localDay)
|
|
576
|
+
return new Set(rows.map((row) => `${row.commitment_id}\u0000${row.level}`))
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** P0-2 (tick N+1 removal): all meta rows with a prefix in one query (snooze:*). */
|
|
580
|
+
listMetaByPrefix(prefix) {
|
|
581
|
+
const rows = this.db.prepare('SELECT key, value FROM meta WHERE key LIKE ?').all(`${prefix}%`)
|
|
582
|
+
return new Map(rows.map((row) => [row.key, row.value]))
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** P0-2: delete a batch of meta keys in one transaction. */
|
|
586
|
+
deleteMetaKeys(keys) {
|
|
587
|
+
if (!keys?.length) return 0
|
|
588
|
+
return this._transaction('IMMEDIATE', () => {
|
|
589
|
+
const statement = this.db.prepare('DELETE FROM meta WHERE key = ?')
|
|
590
|
+
let deleted = 0
|
|
591
|
+
for (const key of keys) deleted += Number(statement.run(key).changes)
|
|
592
|
+
return deleted
|
|
593
|
+
})
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
listNudges(limit = 20) {
|
|
597
|
+
return this.db.prepare('SELECT * FROM nudges ORDER BY at DESC LIMIT ?').all(limit)
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
purgeMatterData(matterId) {
|
|
601
|
+
const conversations = this.db.prepare('SELECT conversation_id FROM sessions WHERE matter_id = ?').all(matterId).map((r) => r.conversation_id)
|
|
602
|
+
let turns = 0
|
|
603
|
+
let summaries = 0
|
|
604
|
+
let runs = 0
|
|
605
|
+
let nudges = 0
|
|
606
|
+
for (const conversationId of conversations) {
|
|
607
|
+
turns += Number(this.db.prepare('DELETE FROM turns WHERE conversation_id = ?').run(conversationId).changes)
|
|
608
|
+
summaries += Number(this.db.prepare('DELETE FROM summary_versions WHERE conversation_id = ?').run(conversationId).changes)
|
|
609
|
+
runs += Number(this.db.prepare('DELETE FROM task_runs WHERE conversation_id = ?').run(conversationId).changes)
|
|
610
|
+
this.db.prepare('DELETE FROM session_summaries WHERE conversation_id = ?').run(conversationId)
|
|
611
|
+
}
|
|
612
|
+
const candidates = Number(this.db.prepare("DELETE FROM memory_candidates WHERE payload LIKE ? OR evidence LIKE ?").run(`%${matterId}%`, `%${matterId}%`).changes)
|
|
613
|
+
const commitmentIds = this.db.prepare('SELECT id FROM commitments WHERE matter_id = ?').all(matterId).map((r) => r.id)
|
|
614
|
+
for (const id of commitmentIds) nudges += Number(this.db.prepare('DELETE FROM nudges WHERE commitment_id = ?').run(id).changes)
|
|
615
|
+
runs += Number(this.db.prepare('DELETE FROM task_runs WHERE matter_id = ?').run(matterId).changes)
|
|
616
|
+
this.db.prepare('DELETE FROM model_calls WHERE matter_id = ?').run(matterId)
|
|
617
|
+
this.db.prepare('DELETE FROM research_runs WHERE matter_id = ?').run(matterId)
|
|
618
|
+
const sessions = this.db.prepare('DELETE FROM sessions WHERE matter_id = ?').run(matterId).changes
|
|
619
|
+
const deliveries = this.db.prepare('DELETE FROM deliveries WHERE matter_id = ?').run(matterId).changes
|
|
620
|
+
try { this.db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); this.db.exec('VACUUM') } catch {}
|
|
621
|
+
return { sessions: Number(sessions), deliveries: Number(deliveries), turns, summaries, runs, nudges, candidates }
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
addTurn({ conversationId, role, content }) {
|
|
625
|
+
const seq = (this.db.prepare('SELECT MAX(seq) AS n FROM turns WHERE conversation_id = ?').get(conversationId)?.n ?? 0) + 1
|
|
626
|
+
const id = `turn_${randomUUID().replace(/-/g, '').slice(0, 16)}`
|
|
627
|
+
this.db.prepare('INSERT INTO turns(id,conversation_id,seq,role,content,created_at) VALUES(?,?,?,?,?,?)')
|
|
628
|
+
.run(id, conversationId, seq, role, content, new Date().toISOString())
|
|
629
|
+
return { id, conversationId, seq, role, content, createdAt: new Date().toISOString() }
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
getTurn(id) {
|
|
633
|
+
const r = this.db.prepare('SELECT * FROM turns WHERE id = ?').get(id)
|
|
634
|
+
return r ? { id: r.id, conversationId: r.conversation_id, seq: r.seq, role: r.role, content: r.content, createdAt: r.created_at } : undefined
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
upsertCalendarEvent({ uid, summary, startsAt, endsAt, allDay = false, tzid, location, rrule, source }) {
|
|
638
|
+
this.db.prepare(`INSERT INTO calendar_events(uid,summary,starts_at,ends_at,all_day,tzid,location,rrule,source,imported_at)
|
|
639
|
+
VALUES(?,?,?,?,?,?,?,?,?,?)
|
|
640
|
+
ON CONFLICT(uid) DO UPDATE SET summary=excluded.summary, starts_at=excluded.starts_at, ends_at=excluded.ends_at,
|
|
641
|
+
all_day=excluded.all_day, tzid=excluded.tzid, location=excluded.location, rrule=excluded.rrule,
|
|
642
|
+
source=excluded.source, imported_at=excluded.imported_at`)
|
|
643
|
+
.run(uid, summary ?? '', startsAt ?? null, endsAt ?? null, allDay ? 1 : 0, tzid ?? null, location ?? null, rrule ?? null, source ?? null, new Date().toISOString())
|
|
644
|
+
return { uid, summary, startsAt, endsAt, allDay, tzid, location, rrule, source }
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
listCalendarEvents(limit = 500) {
|
|
648
|
+
return this.db.prepare('SELECT * FROM calendar_events ORDER BY starts_at ASC LIMIT ?').all(limit)
|
|
649
|
+
.map((r) => ({ uid: r.uid, summary: r.summary, startsAt: r.starts_at ?? undefined, endsAt: r.ends_at ?? undefined, allDay: Boolean(r.all_day), tzid: r.tzid ?? undefined, location: r.location ?? undefined, rrule: r.rrule ?? undefined, source: r.source ?? undefined, importedAt: r.imported_at }))
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
listTurns(conversationId, limit = 50) {
|
|
653
|
+
return this.db.prepare('SELECT * FROM turns WHERE conversation_id = ? ORDER BY seq DESC LIMIT ?').all(conversationId, limit)
|
|
654
|
+
.map((r) => ({ id: r.id, conversationId: r.conversation_id, seq: r.seq, role: r.role, content: r.content, createdAt: r.created_at }))
|
|
655
|
+
.reverse()
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
permitReplayStore() {
|
|
659
|
+
return {
|
|
660
|
+
has: (permitId) => Boolean(this.db.prepare('SELECT 1 FROM permit_consumed WHERE permit_id = ?').get(permitId)),
|
|
661
|
+
claim: (permitId, expiresAt) => Number(this.db.prepare('INSERT OR IGNORE INTO permit_consumed(permit_id,expires_at,consumed_at) VALUES(?,?,?)').run(permitId, expiresAt, new Date().toISOString()).changes) === 1,
|
|
662
|
+
prune: (now) => Number(this.db.prepare('DELETE FROM permit_consumed WHERE expires_at <= ?').run(now).changes),
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
addResearchRun({ matterId, query, answer, sources, usage, stopReason }) {
|
|
667
|
+
const id = `rr_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`
|
|
668
|
+
this.db.prepare('INSERT INTO research_runs(id,matter_id,query,answer,sources,usage,stop_reason,created_at) VALUES(?,?,?,?,?,?,?,?)')
|
|
669
|
+
.run(id, matterId ?? null, query, answer, JSON.stringify(sources ?? []), usage ? JSON.stringify(usage) : null, stopReason ?? null, new Date().toISOString())
|
|
670
|
+
return id
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
listResearch(limit = 10) {
|
|
674
|
+
return this.db.prepare('SELECT * FROM research_runs ORDER BY created_at DESC LIMIT ?').all(limit).map((r) => ({ id: r.id, matterId: r.matter_id ?? undefined, query: r.query, answer: r.answer, sources: JSON.parse(r.sources), usage: r.usage ? JSON.parse(r.usage) : undefined, stopReason: r.stop_reason ?? undefined, createdAt: r.created_at }))
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
addModelCall({ conversationId, matterId, turnId, task, model, effort, thinking, usage, durationMs, finishReason, status = 'ok', code, attempts = 1 }) {
|
|
678
|
+
const id = `mc_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`
|
|
679
|
+
this.db.prepare(`INSERT INTO model_calls(id,conversation_id,matter_id,turn_id,task,model,effort,thinking,status,code,attempts,prompt_tokens,completion_tokens,reasoning_tokens,cached_tokens,total_tokens,duration_ms,finish_reason,created_at)
|
|
680
|
+
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(
|
|
681
|
+
id, conversationId ?? null, matterId ?? null, turnId ?? null, task, model, effort ?? null, thinking ? 1 : 0,
|
|
682
|
+
status, code ?? null, attempts, usage?.promptTokens ?? 0, usage?.completionTokens ?? 0, usage?.reasoningTokens ?? 0,
|
|
683
|
+
usage?.cachedTokens ?? 0, usage?.totalTokens ?? 0, durationMs ?? 0, finishReason ?? null, new Date().toISOString(),
|
|
684
|
+
)
|
|
685
|
+
return id
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
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 })
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
proposeCandidate({ kind, payload, evidence }) {
|
|
700
|
+
const id = `cand_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`
|
|
701
|
+
const now = new Date().toISOString()
|
|
702
|
+
this.db.prepare('INSERT INTO memory_candidates(id,kind,payload,evidence,status,created_at) VALUES(?,?,?,?,?,?)')
|
|
703
|
+
.run(id, kind, JSON.stringify(payload), JSON.stringify(evidence), 'pending', now)
|
|
704
|
+
return this.getCandidate(id)
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
getCandidate(id) {
|
|
708
|
+
const r = this.db.prepare('SELECT * FROM memory_candidates WHERE id = ?').get(id)
|
|
709
|
+
return r ? { id: r.id, kind: r.kind, payload: JSON.parse(r.payload), evidence: JSON.parse(r.evidence), status: r.status, createdAt: r.created_at, reviewedAt: r.reviewed_at, rejectReason: r.reject_reason } : undefined
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
listCandidates(status = 'pending') {
|
|
713
|
+
const rows = status === 'all' ? this.db.prepare('SELECT * FROM memory_candidates ORDER BY created_at DESC').all() : this.db.prepare('SELECT * FROM memory_candidates WHERE status = ? ORDER BY created_at DESC').all(status)
|
|
714
|
+
return rows.map((r) => ({ id: r.id, kind: r.kind, payload: JSON.parse(r.payload), evidence: JSON.parse(r.evidence), status: r.status, createdAt: r.created_at, reviewedAt: r.reviewed_at, rejectReason: r.reject_reason }))
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
reviewCandidate(id, status, reason) {
|
|
718
|
+
this.db.prepare('UPDATE memory_candidates SET status = ?, reviewed_at = ?, reject_reason = ? WHERE id = ?')
|
|
719
|
+
.run(status, new Date().toISOString(), reason ?? null, id)
|
|
720
|
+
return this.getCandidate(id)
|
|
721
|
+
}
|
|
722
|
+
|
|
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
|
+
searchMemory(query, { limit = 6, matterId } = {}) {
|
|
733
|
+
const terms = String(query || '').split(/[^\p{L}\p{N}]+/u).filter((t) => t.length >= 2).slice(0, 8)
|
|
734
|
+
if (!terms.length) return []
|
|
735
|
+
const scope = matterId ? 'AND (matter_id IS NULL OR matter_id = ?)' : ''
|
|
736
|
+
const factRows = this.db.prepare(
|
|
737
|
+
`SELECT id, matter_id, subject, predicate, object, authority, evidence FROM facts
|
|
738
|
+
WHERE status = 'confirmed' AND deleted_at IS NULL
|
|
739
|
+
${scope}
|
|
740
|
+
AND (${terms.map(() => '(subject LIKE ? OR predicate LIKE ? OR object LIKE ?)').join(' OR ')})
|
|
741
|
+
LIMIT 50`
|
|
742
|
+
).all(...(matterId ? [matterId] : []), ...terms.flatMap((t) => [`%${t}%`, `%${t}%`, `%${t}%`]))
|
|
743
|
+
const prefRows = this.db.prepare(
|
|
744
|
+
`SELECT id, statement, kind, evidence, priority FROM preferences
|
|
745
|
+
WHERE status IN ('confirmed','candidate') AND (expires_at IS NULL OR expires_at > ?)
|
|
746
|
+
AND (${terms.map(() => 'statement LIKE ?').join(' OR ')})
|
|
747
|
+
LIMIT 50`
|
|
748
|
+
).all(new Date().toISOString(), ...terms.map((t) => `%${t}%`))
|
|
749
|
+
const score = (text) => terms.filter((t) => text.includes(t)).length
|
|
750
|
+
return [
|
|
751
|
+
...factRows.map((r) => ({ kind: 'fact', id: r.id, text: r.object, detail: `${r.subject} ${r.predicate}`, matterId: r.matter_id ?? undefined, evidence: r.evidence ? JSON.parse(r.evidence)[0]?.quote : undefined, score: score(`${r.subject} ${r.predicate} ${r.object}`) })),
|
|
752
|
+
...prefRows.map((r) => ({ kind: 'preference', id: r.id, text: r.statement, detail: r.kind, evidence: r.evidence ? JSON.parse(r.evidence)[0]?.quote : undefined, score: score(r.statement) })),
|
|
753
|
+
].sort((a, b) => b.score - a.score || a.id.localeCompare(b.id)).slice(0, limit)
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
searchTurns(query, { conversationId, matterId, limit = 8 } = {}) {
|
|
757
|
+
const q = String(query ?? '').trim()
|
|
758
|
+
if (!q) return []
|
|
759
|
+
const clauses = []
|
|
760
|
+
const filterArgs = []
|
|
761
|
+
if (conversationId) { clauses.push('t.conversation_id = ?'); filterArgs.push(conversationId) }
|
|
762
|
+
if (matterId) { clauses.push('s.matter_id = ?'); filterArgs.push(matterId) }
|
|
763
|
+
const join = matterId ? ' JOIN sessions s ON s.conversation_id = t.conversation_id' : ''
|
|
764
|
+
const where = clauses.length ? ` AND ${clauses.join(' AND ')}` : ''
|
|
765
|
+
try {
|
|
766
|
+
if (q.length >= 3) {
|
|
767
|
+
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 }))
|
|
769
|
+
}
|
|
770
|
+
} catch {}
|
|
771
|
+
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 }))
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
getSessionSummary(conversationId) {
|
|
776
|
+
const r = this.db.prepare('SELECT * FROM session_summaries WHERE conversation_id = ?').get(conversationId)
|
|
777
|
+
return r ? { conversationId: r.conversation_id, summary: r.summary, throughSeq: r.through_seq, updatedAt: r.updated_at } : undefined
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
upsertSessionSummary({ conversationId, summary, throughSeq, pins }) {
|
|
781
|
+
const max = this.db.prepare('SELECT MAX(version) AS v FROM summary_versions WHERE conversation_id = ?').get(conversationId)?.v ?? 0
|
|
782
|
+
const version = max + 1
|
|
783
|
+
const at = new Date().toISOString()
|
|
784
|
+
this.db.prepare('INSERT INTO summary_versions(conversation_id,version,summary,through_seq,pins,created_at) VALUES(?,?,?,?,?,?)')
|
|
785
|
+
.run(conversationId, version, summary, throughSeq, pins ?? null, at)
|
|
786
|
+
this.db.prepare(`INSERT INTO session_summaries(conversation_id,summary,through_seq,updated_at) VALUES(?,?,?,?)
|
|
787
|
+
ON CONFLICT(conversation_id) DO UPDATE SET summary=excluded.summary, through_seq=excluded.through_seq, updated_at=excluded.updated_at`)
|
|
788
|
+
.run(conversationId, summary, throughSeq, at)
|
|
789
|
+
return { version, summary, throughSeq, createdAt: at }
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
listSummaryVersions(conversationId) {
|
|
793
|
+
return this.db.prepare('SELECT * FROM summary_versions WHERE conversation_id = ? ORDER BY version DESC').all(conversationId)
|
|
794
|
+
.map((r) => ({ conversationId: r.conversation_id, version: r.version, summary: r.summary, throughSeq: r.through_seq, pins: r.pins ?? undefined, createdAt: r.created_at }))
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
countDeliveries() {
|
|
798
|
+
return this.db.prepare('SELECT COUNT(*) AS n FROM deliveries').get().n
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Append an audit row atomically. BEGIN IMMEDIATE serializes writers and the
|
|
803
|
+
* previous hash is re-read inside the transaction, so two concurrent writers
|
|
804
|
+
* can never both chain onto the same predecessor (audit fork, P0-3).
|
|
805
|
+
*/
|
|
806
|
+
appendAudit({ actor, action, allowed, reason }) {
|
|
807
|
+
return this._transaction('IMMEDIATE', () => this._appendAuditRow({ actor, action, allowed, reason }))
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
_appendAuditRow({ actor, action, allowed, reason }) {
|
|
811
|
+
const last = this.db.prepare('SELECT hash FROM audit ORDER BY seq DESC LIMIT 1').get()
|
|
812
|
+
const prev = last?.hash ?? ''
|
|
813
|
+
const at = new Date().toISOString()
|
|
814
|
+
const material = `${prev}\n${at}\n${actor}\n${action}\n${allowed ? 1 : 0}\n${reason ?? ''}`
|
|
815
|
+
const key = process.env.ADVISERS_AUDIT_KEY
|
|
816
|
+
const hash = key ? createHmac('sha256', key).update(material).digest('hex') : createHash('sha256').update(material).digest('hex')
|
|
817
|
+
const info = this.db.prepare('INSERT INTO audit(at,actor,action,allowed,reason,prev_hash,hash) VALUES(?,?,?,?,?,?,?)')
|
|
818
|
+
.run(at, actor, action, allowed ? 1 : 0, reason ?? null, prev, hash)
|
|
819
|
+
return { seq: Number(info.lastInsertRowid), at, actor, action, allowed: Boolean(allowed), reason, prevHash: prev, hash }
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
listAudit(limit = 50) {
|
|
823
|
+
return this.db.prepare('SELECT * FROM audit ORDER BY seq DESC LIMIT ?').all(limit)
|
|
824
|
+
.map((r) => ({ seq: r.seq, at: r.at, actor: r.actor, action: r.action, allowed: Boolean(r.allowed), reason: r.reason ?? undefined, prevHash: r.prev_hash ?? undefined, hash: r.hash }))
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
verifyAudit() {
|
|
828
|
+
const rows = this.db.prepare('SELECT * FROM audit ORDER BY seq ASC').all()
|
|
829
|
+
let prev = ''
|
|
830
|
+
for (const row of rows) {
|
|
831
|
+
const material = `${prev}\n${row.at}\n${row.actor}\n${row.action}\n${row.allowed}\n${row.reason ?? ''}`
|
|
832
|
+
const key = process.env.ADVISERS_AUDIT_KEY
|
|
833
|
+
const expected = key ? createHmac('sha256', key).update(material).digest('hex') : createHash('sha256').update(material).digest('hex')
|
|
834
|
+
if (row.prev_hash !== prev || row.hash !== expected) return { ok: false, seq: row.seq }
|
|
835
|
+
prev = row.hash
|
|
836
|
+
}
|
|
837
|
+
return { ok: true, count: rows.length }
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
hasSeen(linkId, accountId, dedupeKey, ttlDays = 7) {
|
|
841
|
+
const cutoff = new Date(Date.now() - ttlDays * 86_400_000).toISOString()
|
|
842
|
+
const row = this.db.prepare(
|
|
843
|
+
'SELECT 1 AS hit FROM seen_events WHERE link_id = ? AND account_id = ? AND dedupe_key = ? AND seen_at >= ?',
|
|
844
|
+
).get(linkId, accountId, dedupeKey, cutoff)
|
|
845
|
+
return Boolean(row)
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
markSeen(linkId, accountId, dedupeKey) {
|
|
849
|
+
this.db.prepare('INSERT OR REPLACE INTO seen_events VALUES(?,?,?,?)')
|
|
850
|
+
.run(linkId, accountId, dedupeKey, new Date().toISOString())
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
pruneSeen(ttlDays = 7) {
|
|
854
|
+
const cutoff = new Date(Date.now() - ttlDays * 86_400_000).toISOString()
|
|
855
|
+
this.db.prepare('DELETE FROM seen_events WHERE seen_at < ?').run(cutoff)
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
seenStore() {
|
|
859
|
+
return {
|
|
860
|
+
has: (key) => {
|
|
861
|
+
const [linkId, accountId, ...rest] = key.split(':')
|
|
862
|
+
return this.hasSeen(linkId, accountId, rest.join(':'))
|
|
863
|
+
},
|
|
864
|
+
add: (key) => {
|
|
865
|
+
const [linkId, accountId, ...rest] = key.split(':')
|
|
866
|
+
this.markSeen(linkId, accountId, rest.join(':'))
|
|
867
|
+
},
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
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])),
|
|
930
|
+
}
|
|
931
|
+
// Remember which rows this connection has seen (P1-1): save() only deletes
|
|
932
|
+
// rows this loader owned, so another process's concurrent additions are
|
|
933
|
+
// merged instead of being silently overwritten by a stale snapshot.
|
|
934
|
+
this._baselineIds = idsFor(snapshot)
|
|
935
|
+
return snapshot
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
save(snapshot) {
|
|
939
|
+
return this._transaction('IMMEDIATE', () => this._saveRows(snapshot))
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* Write the ledger snapshot and its audit row in one transaction, so the
|
|
944
|
+
* audit trail can never claim a write that rolled back (or miss one that
|
|
945
|
+
* committed). `inside` runs inside the same transaction before the audit row.
|
|
946
|
+
*/
|
|
947
|
+
saveWithAudit(snapshot, audit, inside) {
|
|
948
|
+
return this._transaction('IMMEDIATE', () => {
|
|
949
|
+
this._saveRows(snapshot)
|
|
950
|
+
if (typeof inside === 'function') inside(this)
|
|
951
|
+
return this._appendAuditRow(audit)
|
|
952
|
+
})
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
_saveRows(snapshot) {
|
|
956
|
+
const db = this.db
|
|
957
|
+
const newIds = idsFor(snapshot)
|
|
958
|
+
const baseline = this._baselineIds
|
|
959
|
+
try {
|
|
960
|
+
if (baseline) {
|
|
961
|
+
// P1-1: delete only rows this connection loaded that the new snapshot
|
|
962
|
+
// dropped; rows another process added after our load() are kept.
|
|
963
|
+
for (const [table, key] of LEDGER_TABLES) {
|
|
964
|
+
const removed = [...(baseline[table] ?? [])].filter((id) => !newIds[table].has(id))
|
|
965
|
+
if (!removed.length) continue
|
|
966
|
+
const statement = db.prepare(`DELETE FROM ${table} WHERE ${key} = ?`)
|
|
967
|
+
for (const id of removed) statement.run(id)
|
|
968
|
+
}
|
|
969
|
+
} else {
|
|
970
|
+
// No prior load(): keep the historical full-replace semantics for
|
|
971
|
+
// direct library use (e.g. importing or seeding a ledger).
|
|
972
|
+
for (const [table] of LEDGER_TABLES) db.exec(`DELETE FROM ${table}`)
|
|
973
|
+
}
|
|
974
|
+
const m = db.prepare('INSERT OR REPLACE INTO matters VALUES (?,?,?,?,?,?,?,?,?,?,?)')
|
|
975
|
+
for (const x of snapshot.matters ?? []) {
|
|
976
|
+
m.run(x.id, x.title, x.status, x.domain, x.secrecy, x.summary ?? null, json(x.participants), json(x.deadlines), json(x.openQuestions), x.createdAt, x.updatedAt)
|
|
977
|
+
}
|
|
978
|
+
const f = db.prepare(`INSERT OR REPLACE INTO facts(id,matter_id,subject,predicate,object,recorded_at,expired_at,authority,confidence,source,evidence,status,valid_from,valid_to,supersedes,superseded_by,shadowed_by,last_seen_at,deleted_at,deleted_reason)
|
|
979
|
+
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
|
|
980
|
+
for (const x of snapshot.facts ?? []) {
|
|
981
|
+
f.run(x.id, x.matterId ?? null, x.subject, x.predicate, x.object, x.recordedAt, x.expiredAt ?? null, x.authority, x.confidence, x.source ? json(x.source) : null, x.evidence ? json(x.evidence) : null, x.status, x.validFrom ?? null, x.validTo ?? null, x.supersedes ?? null, x.supersededBy ?? null, x.shadowedBy ?? null, x.lastSeenAt ?? null, x.deletedAt ?? null, x.deletedReason ?? null)
|
|
982
|
+
}
|
|
983
|
+
const c = db.prepare(`INSERT OR REPLACE INTO commitments(id,matter_id,debtor,creditor,action,due_at,timezone,status,evidence,capability,done_at,reminder_offsets)
|
|
984
|
+
VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`)
|
|
985
|
+
for (const x of snapshot.commitments ?? []) {
|
|
986
|
+
// A missing/undefined list stays NULL so a round-trip keeps the pre-0.2.0
|
|
987
|
+
// "no field" shape; an explicit [] is preserved as its own value.
|
|
988
|
+
const reminderOffsets = Array.isArray(x.reminderOffsets) ? JSON.stringify(x.reminderOffsets) : null
|
|
989
|
+
c.run(x.id, x.matterId, x.debtor, x.creditor, x.action, x.dueAt ?? null, x.timezone, x.status, json(x.evidence), x.capability, x.doneAt ?? null, reminderOffsets)
|
|
990
|
+
}
|
|
991
|
+
const p = db.prepare('INSERT OR REPLACE INTO preferences VALUES (?,?,?,?,?,?,?,?,?,?,?,?)')
|
|
992
|
+
for (const x of snapshot.preferences ?? []) {
|
|
993
|
+
p.run(x.id, x.kind, x.statement, json(x.scope), json(x.source), json(x.evidence), x.confidence, x.status, x.validFrom, x.lastConfirmed ?? null, x.expiresAt ?? null, x.priority)
|
|
994
|
+
}
|
|
995
|
+
const l = db.prepare('INSERT OR REPLACE INTO links VALUES (?,?,?,?,?,?,?,?,?)')
|
|
996
|
+
for (const x of snapshot.links ?? []) {
|
|
997
|
+
l.run(x.id, x.fromMatter, x.toMatter, x.type, x.confidence, json(x.evidence), x.status, x.createdBy, x.createdAt)
|
|
998
|
+
}
|
|
999
|
+
const u = db.prepare('INSERT OR REPLACE INTO updates VALUES (?,?,?,?,?,?,?)')
|
|
1000
|
+
for (const x of snapshot.updates ?? []) {
|
|
1001
|
+
u.run(x.id, x.sourceMatter, x.targetMatter, x.change, x.status, x.createdAt, x.rollback ?? null)
|
|
1002
|
+
}
|
|
1003
|
+
const r = db.prepare('INSERT OR REPLACE INTO routines VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)')
|
|
1004
|
+
for (const x of snapshot.routines ?? []) {
|
|
1005
|
+
r.run(x.id, x.name, x.description, json(x.trigger), json(x.steps), json(x.preferences), json(x.pitfalls), json(x.verification), x.scope ?? null, x.version, x.status, x.createdBy, x.createdAt, x.updatedAt)
|
|
1006
|
+
}
|
|
1007
|
+
const rp = db.prepare('INSERT OR REPLACE INTO routine_patches VALUES (?,?,?,?,?,?,?,?)')
|
|
1008
|
+
for (const x of snapshot.routinePatches ?? []) {
|
|
1009
|
+
rp.run(x.id, x.routineId, x.baseVersion, json(x.changes), json(x.before), x.diff, x.status, x.createdAt)
|
|
1010
|
+
}
|
|
1011
|
+
const rt = db.prepare('INSERT OR REPLACE INTO routes VALUES (?,?)')
|
|
1012
|
+
for (const [key, matterId] of Object.entries(snapshot.routes ?? {})) rt.run(key, matterId)
|
|
1013
|
+
this._baselineIds = newIds
|
|
1014
|
+
} catch (error) {
|
|
1015
|
+
throw error
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
close() {
|
|
1020
|
+
this.db.close()
|
|
1021
|
+
}
|
|
1022
|
+
}
|