@facthouse/mcp 0.26.0 → 0.28.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 +21 -21
- package/README.md +622 -611
- package/dist/cli/index.d.ts +4 -9
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +50 -48
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/init-backfill.d.ts +23 -4
- package/dist/cli/init-backfill.d.ts.map +1 -1
- package/dist/cli/init-backfill.js +33 -12
- package/dist/cli/init-backfill.js.map +1 -1
- package/dist/cli/init-knobs.d.ts +20 -11
- package/dist/cli/init-knobs.d.ts.map +1 -1
- package/dist/cli/init-knobs.js +62 -32
- package/dist/cli/init-knobs.js.map +1 -1
- package/dist/cli/init-wizard.d.ts +2 -0
- package/dist/cli/init-wizard.d.ts.map +1 -1
- package/dist/cli/init-wizard.js +7 -5
- package/dist/cli/init-wizard.js.map +1 -1
- package/dist/cli/init.d.ts +4 -1
- package/dist/cli/init.d.ts.map +1 -1
- package/dist/cli/init.js +8 -6
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/inspect-html.js +460 -460
- package/dist/cli/inspect-intelligence.js +70 -70
- package/dist/cli/inspect-payload.js +39 -39
- package/dist/cli/inspect-theme.js +18 -18
- package/dist/cli/query.js +4 -4
- package/dist/cli/settings.d.ts.map +1 -1
- package/dist/cli/settings.js +3 -3
- package/dist/cli/settings.js.map +1 -1
- package/dist/cli/spend-dashboard.js +4 -4
- package/dist/cli/stats-html.js +674 -674
- package/dist/cli/web.d.ts.map +1 -1
- package/dist/cli/web.js +91 -96
- package/dist/cli/web.js.map +1 -1
- package/dist/db/consolidation-lock.js +2 -2
- package/dist/db/consolidations.js +15 -15
- package/dist/db/embeddings.js +10 -10
- package/dist/db/entities.js +26 -26
- package/dist/db/extract-watermarks.js +79 -79
- package/dist/db/facts.js +39 -39
- package/dist/db/inferences.js +8 -8
- package/dist/db/intelligence-runs.js +5 -5
- package/dist/db/postgres-schema.js +323 -323
- package/dist/db/prune.js +17 -17
- package/dist/db/schema.js +530 -530
- package/dist/db/session-facts.js +26 -26
- package/dist/db/sessions.js +10 -10
- package/dist/db/sources.js +1 -1
- package/dist/db/stats.js +10 -10
- package/dist/db/watermarks.js +7 -7
- package/dist/identity.d.ts +7 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +13 -0
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +1 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/intelligence/consolidate.d.ts +7 -1
- package/dist/intelligence/consolidate.d.ts.map +1 -1
- package/dist/intelligence/consolidate.js +54 -40
- package/dist/intelligence/consolidate.js.map +1 -1
- package/dist/intelligence/extract-prompt.js +26 -26
- package/dist/intelligence/token-budget.d.ts +2 -0
- package/dist/intelligence/token-budget.d.ts.map +1 -1
- package/dist/intelligence/token-budget.js +1 -1
- package/dist/intelligence/token-budget.js.map +1 -1
- package/dist/search/episodes.js +14 -14
- package/dist/search/key-facts.js +3 -3
- package/dist/suppress-sqlite-warning.d.ts +2 -0
- package/dist/suppress-sqlite-warning.d.ts.map +1 -0
- package/dist/suppress-sqlite-warning.js +17 -0
- package/dist/suppress-sqlite-warning.js.map +1 -0
- package/dist/tools/fact-manager.d.ts.map +1 -1
- package/dist/tools/fact-manager.js +9 -11
- package/dist/tools/fact-manager.js.map +1 -1
- package/dist/tools/read-tools.js +4 -4
- package/dist/tools/resources.js +3 -3
- package/dist/tools/resources.js.map +1 -1
- package/dist/types/config.d.ts +5 -3
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/config.js +7 -1
- package/dist/types/config.js.map +1 -1
- package/package.json +62 -62
- package/dist/cli/log-event.d.ts +0 -55
- package/dist/cli/log-event.d.ts.map +0 -1
- package/dist/cli/log-event.js +0 -149
- package/dist/cli/log-event.js.map +0 -1
- package/dist/db/scheduler-signal.d.ts +0 -20
- package/dist/db/scheduler-signal.d.ts.map +0 -1
- package/dist/db/scheduler-signal.js +0 -32
- package/dist/db/scheduler-signal.js.map +0 -1
- package/dist/schemas/starter-vocabulary.d.ts +0 -34
- package/dist/schemas/starter-vocabulary.d.ts.map +0 -1
- package/dist/schemas/starter-vocabulary.js +0 -91
- package/dist/schemas/starter-vocabulary.js.map +0 -1
- package/dist/search/profile.d.ts +0 -31
- package/dist/search/profile.d.ts.map +0 -1
- package/dist/search/profile.js +0 -43
- package/dist/search/profile.js.map +0 -1
- package/dist/sources/jsonl-ingest.d.ts +0 -58
- package/dist/sources/jsonl-ingest.d.ts.map +0 -1
- package/dist/sources/jsonl-ingest.js +0 -195
- package/dist/sources/jsonl-ingest.js.map +0 -1
- package/dist/sources/pull.d.ts +0 -85
- package/dist/sources/pull.d.ts.map +0 -1
- package/dist/sources/pull.js +0 -145
- package/dist/sources/pull.js.map +0 -1
- package/dist/types.d.ts +0 -115
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -6
- package/dist/types.js.map +0 -1
- package/dist/warnings.d.ts.map +0 -1
- package/dist/warnings.js.map +0 -1
package/dist/db/schema.js
CHANGED
|
@@ -97,31 +97,31 @@ export async function applySchema(db) {
|
|
|
97
97
|
// Schema version 1 — sessions + session_events (DIKW Data layer)
|
|
98
98
|
// ---------------------------------------------------------------------------
|
|
99
99
|
async function applyV1(db) {
|
|
100
|
-
await db.exec(`
|
|
101
|
-
CREATE TABLE IF NOT EXISTS sessions (
|
|
102
|
-
id TEXT PRIMARY KEY,
|
|
103
|
-
source_tool TEXT,
|
|
104
|
-
project TEXT,
|
|
105
|
-
started_at TEXT NOT NULL,
|
|
106
|
-
last_activity_at TEXT NOT NULL
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
CREATE TABLE IF NOT EXISTS session_events (
|
|
110
|
-
id TEXT PRIMARY KEY,
|
|
111
|
-
session_id TEXT NOT NULL REFERENCES sessions(id),
|
|
112
|
-
sequence INTEGER NOT NULL,
|
|
113
|
-
event_type TEXT NOT NULL CHECK (event_type IN ('message', 'tool_call', 'tool_result', 'artifact')),
|
|
114
|
-
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system', 'tool')),
|
|
115
|
-
content_type TEXT NOT NULL DEFAULT 'text' CHECK (content_type IN ('text', 'json', 'image', 'audio', 'binary')),
|
|
116
|
-
content TEXT,
|
|
117
|
-
content_ref TEXT,
|
|
118
|
-
metadata TEXT,
|
|
119
|
-
created_at TEXT NOT NULL,
|
|
120
|
-
UNIQUE(session_id, sequence)
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
CREATE INDEX IF NOT EXISTS idx_session_events_session
|
|
124
|
-
ON session_events(session_id, sequence);
|
|
100
|
+
await db.exec(`
|
|
101
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
102
|
+
id TEXT PRIMARY KEY,
|
|
103
|
+
source_tool TEXT,
|
|
104
|
+
project TEXT,
|
|
105
|
+
started_at TEXT NOT NULL,
|
|
106
|
+
last_activity_at TEXT NOT NULL
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
CREATE TABLE IF NOT EXISTS session_events (
|
|
110
|
+
id TEXT PRIMARY KEY,
|
|
111
|
+
session_id TEXT NOT NULL REFERENCES sessions(id),
|
|
112
|
+
sequence INTEGER NOT NULL,
|
|
113
|
+
event_type TEXT NOT NULL CHECK (event_type IN ('message', 'tool_call', 'tool_result', 'artifact')),
|
|
114
|
+
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system', 'tool')),
|
|
115
|
+
content_type TEXT NOT NULL DEFAULT 'text' CHECK (content_type IN ('text', 'json', 'image', 'audio', 'binary')),
|
|
116
|
+
content TEXT,
|
|
117
|
+
content_ref TEXT,
|
|
118
|
+
metadata TEXT,
|
|
119
|
+
created_at TEXT NOT NULL,
|
|
120
|
+
UNIQUE(session_id, sequence)
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
CREATE INDEX IF NOT EXISTS idx_session_events_session
|
|
124
|
+
ON session_events(session_id, sequence);
|
|
125
125
|
`);
|
|
126
126
|
await pragmaWrite(db, "user_version = 1");
|
|
127
127
|
}
|
|
@@ -131,33 +131,33 @@ async function applyV1(db) {
|
|
|
131
131
|
async function applyV2(db) {
|
|
132
132
|
// foreign_keys pragma cannot be changed inside a transaction.
|
|
133
133
|
await pragmaWrite(db, "foreign_keys = OFF");
|
|
134
|
-
await db.exec(`
|
|
135
|
-
CREATE TABLE session_events_new (
|
|
136
|
-
id TEXT PRIMARY KEY,
|
|
137
|
-
mcp_session_id TEXT,
|
|
138
|
-
client_session_id TEXT,
|
|
139
|
-
sequence INTEGER NOT NULL,
|
|
140
|
-
event_type TEXT NOT NULL CHECK (event_type IN ('message', 'tool_call', 'tool_result', 'artifact')),
|
|
141
|
-
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system', 'tool')),
|
|
142
|
-
content_type TEXT NOT NULL DEFAULT 'text' CHECK (content_type IN ('text', 'json', 'image', 'audio', 'binary')),
|
|
143
|
-
content TEXT,
|
|
144
|
-
content_ref TEXT,
|
|
145
|
-
metadata TEXT,
|
|
146
|
-
created_at TEXT NOT NULL
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
INSERT INTO session_events_new
|
|
150
|
-
(id, mcp_session_id, client_session_id, sequence, event_type, role,
|
|
151
|
-
content_type, content, content_ref, metadata, created_at)
|
|
152
|
-
SELECT id, session_id, NULL, sequence, event_type, role,
|
|
153
|
-
content_type, content, content_ref, metadata, created_at
|
|
154
|
-
FROM session_events;
|
|
155
|
-
|
|
156
|
-
DROP TABLE session_events;
|
|
157
|
-
ALTER TABLE session_events_new RENAME TO session_events;
|
|
158
|
-
|
|
159
|
-
CREATE INDEX idx_session_events_mcp ON session_events(mcp_session_id, sequence);
|
|
160
|
-
CREATE INDEX idx_session_events_client ON session_events(client_session_id);
|
|
134
|
+
await db.exec(`
|
|
135
|
+
CREATE TABLE session_events_new (
|
|
136
|
+
id TEXT PRIMARY KEY,
|
|
137
|
+
mcp_session_id TEXT,
|
|
138
|
+
client_session_id TEXT,
|
|
139
|
+
sequence INTEGER NOT NULL,
|
|
140
|
+
event_type TEXT NOT NULL CHECK (event_type IN ('message', 'tool_call', 'tool_result', 'artifact')),
|
|
141
|
+
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system', 'tool')),
|
|
142
|
+
content_type TEXT NOT NULL DEFAULT 'text' CHECK (content_type IN ('text', 'json', 'image', 'audio', 'binary')),
|
|
143
|
+
content TEXT,
|
|
144
|
+
content_ref TEXT,
|
|
145
|
+
metadata TEXT,
|
|
146
|
+
created_at TEXT NOT NULL
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
INSERT INTO session_events_new
|
|
150
|
+
(id, mcp_session_id, client_session_id, sequence, event_type, role,
|
|
151
|
+
content_type, content, content_ref, metadata, created_at)
|
|
152
|
+
SELECT id, session_id, NULL, sequence, event_type, role,
|
|
153
|
+
content_type, content, content_ref, metadata, created_at
|
|
154
|
+
FROM session_events;
|
|
155
|
+
|
|
156
|
+
DROP TABLE session_events;
|
|
157
|
+
ALTER TABLE session_events_new RENAME TO session_events;
|
|
158
|
+
|
|
159
|
+
CREATE INDEX idx_session_events_mcp ON session_events(mcp_session_id, sequence);
|
|
160
|
+
CREATE INDEX idx_session_events_client ON session_events(client_session_id);
|
|
161
161
|
`);
|
|
162
162
|
await pragmaWrite(db, "foreign_keys = ON");
|
|
163
163
|
await pragmaWrite(db, "user_version = 2");
|
|
@@ -169,53 +169,53 @@ async function applyV2(db) {
|
|
|
169
169
|
// integrity via findOrCreateEntity, claimForConsolidation, etc.
|
|
170
170
|
// ---------------------------------------------------------------------------
|
|
171
171
|
async function applyV3(db) {
|
|
172
|
-
await db.exec(`
|
|
173
|
-
CREATE TABLE IF NOT EXISTS session_facts (
|
|
174
|
-
id TEXT PRIMARY KEY,
|
|
175
|
-
session_id TEXT NOT NULL,
|
|
176
|
-
content TEXT NOT NULL,
|
|
177
|
-
content_hash TEXT NOT NULL,
|
|
178
|
-
source_origin TEXT NOT NULL DEFAULT 'explicit'
|
|
179
|
-
CHECK (source_origin IN ('explicit', 'inferred')),
|
|
180
|
-
source_event_id TEXT,
|
|
181
|
-
domain_hint TEXT,
|
|
182
|
-
confidence REAL,
|
|
183
|
-
importance REAL,
|
|
184
|
-
source_tool TEXT,
|
|
185
|
-
capture_context TEXT,
|
|
186
|
-
consolidation_id TEXT,
|
|
187
|
-
created_at TEXT NOT NULL
|
|
188
|
-
);
|
|
189
|
-
|
|
190
|
-
CREATE INDEX IF NOT EXISTS idx_session_facts_session
|
|
191
|
-
ON session_facts(session_id);
|
|
192
|
-
|
|
193
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_facts_hash
|
|
194
|
-
ON session_facts(session_id, content_hash);
|
|
195
|
-
|
|
196
|
-
CREATE INDEX IF NOT EXISTS idx_session_facts_unclaimed
|
|
197
|
-
ON session_facts(created_at) WHERE consolidation_id IS NULL;
|
|
198
|
-
|
|
199
|
-
CREATE TABLE IF NOT EXISTS session_fact_sources (
|
|
200
|
-
session_fact_id TEXT NOT NULL,
|
|
201
|
-
event_id TEXT NOT NULL,
|
|
202
|
-
relevance REAL NOT NULL DEFAULT 1.0,
|
|
203
|
-
extraction_type TEXT NOT NULL DEFAULT 'contextual'
|
|
204
|
-
CHECK (extraction_type IN ('primary', 'corroborating', 'contextual')),
|
|
205
|
-
PRIMARY KEY (session_fact_id, event_id)
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
CREATE TABLE IF NOT EXISTS domains (
|
|
209
|
-
name TEXT PRIMARY KEY,
|
|
210
|
-
subdomains TEXT NOT NULL DEFAULT '[]',
|
|
211
|
-
created_at TEXT NOT NULL
|
|
212
|
-
);
|
|
213
|
-
|
|
214
|
-
CREATE TABLE IF NOT EXISTS consolidation_lock (
|
|
215
|
-
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
216
|
-
holder TEXT NOT NULL,
|
|
217
|
-
started_at TEXT NOT NULL
|
|
218
|
-
);
|
|
172
|
+
await db.exec(`
|
|
173
|
+
CREATE TABLE IF NOT EXISTS session_facts (
|
|
174
|
+
id TEXT PRIMARY KEY,
|
|
175
|
+
session_id TEXT NOT NULL,
|
|
176
|
+
content TEXT NOT NULL,
|
|
177
|
+
content_hash TEXT NOT NULL,
|
|
178
|
+
source_origin TEXT NOT NULL DEFAULT 'explicit'
|
|
179
|
+
CHECK (source_origin IN ('explicit', 'inferred')),
|
|
180
|
+
source_event_id TEXT,
|
|
181
|
+
domain_hint TEXT,
|
|
182
|
+
confidence REAL,
|
|
183
|
+
importance REAL,
|
|
184
|
+
source_tool TEXT,
|
|
185
|
+
capture_context TEXT,
|
|
186
|
+
consolidation_id TEXT,
|
|
187
|
+
created_at TEXT NOT NULL
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
CREATE INDEX IF NOT EXISTS idx_session_facts_session
|
|
191
|
+
ON session_facts(session_id);
|
|
192
|
+
|
|
193
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_facts_hash
|
|
194
|
+
ON session_facts(session_id, content_hash);
|
|
195
|
+
|
|
196
|
+
CREATE INDEX IF NOT EXISTS idx_session_facts_unclaimed
|
|
197
|
+
ON session_facts(created_at) WHERE consolidation_id IS NULL;
|
|
198
|
+
|
|
199
|
+
CREATE TABLE IF NOT EXISTS session_fact_sources (
|
|
200
|
+
session_fact_id TEXT NOT NULL,
|
|
201
|
+
event_id TEXT NOT NULL,
|
|
202
|
+
relevance REAL NOT NULL DEFAULT 1.0,
|
|
203
|
+
extraction_type TEXT NOT NULL DEFAULT 'contextual'
|
|
204
|
+
CHECK (extraction_type IN ('primary', 'corroborating', 'contextual')),
|
|
205
|
+
PRIMARY KEY (session_fact_id, event_id)
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
CREATE TABLE IF NOT EXISTS domains (
|
|
209
|
+
name TEXT PRIMARY KEY,
|
|
210
|
+
subdomains TEXT NOT NULL DEFAULT '[]',
|
|
211
|
+
created_at TEXT NOT NULL
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
CREATE TABLE IF NOT EXISTS consolidation_lock (
|
|
215
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
216
|
+
holder TEXT NOT NULL,
|
|
217
|
+
started_at TEXT NOT NULL
|
|
218
|
+
);
|
|
219
219
|
`);
|
|
220
220
|
await pragmaWrite(db, "user_version = 3");
|
|
221
221
|
}
|
|
@@ -223,113 +223,113 @@ async function applyV3(db) {
|
|
|
223
223
|
// Schema version 4 — Knowledge layer: facts + FTS5 + entities + graph + sources + consolidations
|
|
224
224
|
// ---------------------------------------------------------------------------
|
|
225
225
|
async function applyV4(db) {
|
|
226
|
-
await db.exec(`
|
|
227
|
-
CREATE TABLE IF NOT EXISTS facts (
|
|
228
|
-
id TEXT PRIMARY KEY,
|
|
229
|
-
content TEXT NOT NULL,
|
|
230
|
-
domain TEXT NOT NULL,
|
|
231
|
-
subdomain TEXT,
|
|
232
|
-
confidence REAL NOT NULL DEFAULT 0.7,
|
|
233
|
-
importance REAL NOT NULL DEFAULT 0.5,
|
|
234
|
-
source_type TEXT NOT NULL,
|
|
235
|
-
source_tool TEXT,
|
|
236
|
-
source_id TEXT,
|
|
237
|
-
status TEXT NOT NULL DEFAULT 'active'
|
|
238
|
-
CHECK (status IN ('active', 'superseded', 'rejected')),
|
|
239
|
-
superseded_by TEXT,
|
|
240
|
-
is_latest INTEGER NOT NULL DEFAULT 1,
|
|
241
|
-
created_at TEXT NOT NULL,
|
|
242
|
-
valid_from TEXT,
|
|
243
|
-
valid_until TEXT,
|
|
244
|
-
system_retired_at TEXT,
|
|
245
|
-
session_id TEXT,
|
|
246
|
-
capture_context TEXT,
|
|
247
|
-
access_count INTEGER NOT NULL DEFAULT 0
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS facts_fts USING fts5(
|
|
251
|
-
content, domain, subdomain,
|
|
252
|
-
content=facts, content_rowid=rowid
|
|
253
|
-
);
|
|
254
|
-
|
|
255
|
-
-- FTS5 external content sync triggers.
|
|
256
|
-
-- Only INSERT and DELETE are needed: facts are immutable so the
|
|
257
|
-
-- FTS5-indexed columns (content, domain, subdomain) are never UPDATEd.
|
|
258
|
-
-- supersedeFact only updates status/is_latest/valid_until (and, in
|
|
259
|
-
-- bi-temporal mode, system_retired_at), which are not in the FTS5 index.
|
|
260
|
-
-- DELETE trigger is a safety net — facts are never deleted in normal
|
|
261
|
-
-- operation, but if one were, FTS5 must stay in sync.
|
|
262
|
-
CREATE TRIGGER IF NOT EXISTS facts_ai AFTER INSERT ON facts BEGIN
|
|
263
|
-
INSERT INTO facts_fts(rowid, content, domain, subdomain)
|
|
264
|
-
VALUES (new.rowid, new.content, new.domain, new.subdomain);
|
|
265
|
-
END;
|
|
266
|
-
|
|
267
|
-
CREATE TRIGGER IF NOT EXISTS facts_ad AFTER DELETE ON facts BEGIN
|
|
268
|
-
INSERT INTO facts_fts(facts_fts, rowid, content, domain, subdomain)
|
|
269
|
-
VALUES ('delete', old.rowid, old.content, old.domain, old.subdomain);
|
|
270
|
-
END;
|
|
271
|
-
|
|
272
|
-
CREATE TABLE IF NOT EXISTS entities (
|
|
273
|
-
id TEXT PRIMARY KEY,
|
|
274
|
-
type TEXT NOT NULL,
|
|
275
|
-
name TEXT NOT NULL,
|
|
276
|
-
canonical_name TEXT NOT NULL,
|
|
277
|
-
metadata TEXT,
|
|
278
|
-
created_at TEXT NOT NULL,
|
|
279
|
-
access_count INTEGER NOT NULL DEFAULT 0,
|
|
280
|
-
last_accessed_at TEXT
|
|
281
|
-
);
|
|
282
|
-
|
|
283
|
-
CREATE TABLE IF NOT EXISTS fact_entities (
|
|
284
|
-
fact_id TEXT NOT NULL,
|
|
285
|
-
entity_id TEXT NOT NULL,
|
|
286
|
-
relationship TEXT NOT NULL,
|
|
287
|
-
PRIMARY KEY (fact_id, entity_id, relationship)
|
|
288
|
-
);
|
|
289
|
-
|
|
290
|
-
CREATE TABLE IF NOT EXISTS entity_edges (
|
|
291
|
-
from_entity TEXT NOT NULL,
|
|
292
|
-
to_entity TEXT NOT NULL,
|
|
293
|
-
relationship TEXT NOT NULL,
|
|
294
|
-
strength REAL NOT NULL,
|
|
295
|
-
metadata TEXT,
|
|
296
|
-
created_at TEXT NOT NULL,
|
|
297
|
-
last_accessed_at TEXT,
|
|
298
|
-
PRIMARY KEY (from_entity, to_entity, relationship)
|
|
299
|
-
);
|
|
300
|
-
|
|
301
|
-
-- Source provenance records.
|
|
302
|
-
CREATE TABLE IF NOT EXISTS sources (
|
|
303
|
-
id TEXT PRIMARY KEY,
|
|
304
|
-
type TEXT NOT NULL,
|
|
305
|
-
tool_id TEXT,
|
|
306
|
-
timestamp TEXT NOT NULL,
|
|
307
|
-
raw_content TEXT,
|
|
308
|
-
metadata TEXT
|
|
309
|
-
);
|
|
310
|
-
|
|
311
|
-
CREATE TABLE IF NOT EXISTS consolidations (
|
|
312
|
-
id TEXT PRIMARY KEY,
|
|
313
|
-
session_id TEXT,
|
|
314
|
-
facts_in INTEGER NOT NULL,
|
|
315
|
-
facts_graduated INTEGER NOT NULL,
|
|
316
|
-
facts_rejected INTEGER NOT NULL,
|
|
317
|
-
entities_created INTEGER NOT NULL DEFAULT 0,
|
|
318
|
-
entities_linked INTEGER NOT NULL DEFAULT 0,
|
|
319
|
-
supersessions INTEGER NOT NULL DEFAULT 0,
|
|
320
|
-
summary TEXT,
|
|
321
|
-
open_threads TEXT,
|
|
322
|
-
created_at TEXT NOT NULL
|
|
323
|
-
);
|
|
324
|
-
|
|
325
|
-
CREATE INDEX IF NOT EXISTS idx_facts_domain ON facts(domain, subdomain);
|
|
326
|
-
CREATE INDEX IF NOT EXISTS idx_facts_status ON facts(status, is_latest);
|
|
327
|
-
CREATE INDEX IF NOT EXISTS idx_facts_session ON facts(session_id);
|
|
328
|
-
CREATE INDEX IF NOT EXISTS idx_fact_entities_entity ON fact_entities(entity_id);
|
|
329
|
-
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
330
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_entities_canonical_type ON entities(canonical_name, type);
|
|
331
|
-
CREATE INDEX IF NOT EXISTS idx_entity_edges_from ON entity_edges(from_entity);
|
|
332
|
-
CREATE INDEX IF NOT EXISTS idx_entity_edges_to ON entity_edges(to_entity);
|
|
226
|
+
await db.exec(`
|
|
227
|
+
CREATE TABLE IF NOT EXISTS facts (
|
|
228
|
+
id TEXT PRIMARY KEY,
|
|
229
|
+
content TEXT NOT NULL,
|
|
230
|
+
domain TEXT NOT NULL,
|
|
231
|
+
subdomain TEXT,
|
|
232
|
+
confidence REAL NOT NULL DEFAULT 0.7,
|
|
233
|
+
importance REAL NOT NULL DEFAULT 0.5,
|
|
234
|
+
source_type TEXT NOT NULL,
|
|
235
|
+
source_tool TEXT,
|
|
236
|
+
source_id TEXT,
|
|
237
|
+
status TEXT NOT NULL DEFAULT 'active'
|
|
238
|
+
CHECK (status IN ('active', 'superseded', 'rejected')),
|
|
239
|
+
superseded_by TEXT,
|
|
240
|
+
is_latest INTEGER NOT NULL DEFAULT 1,
|
|
241
|
+
created_at TEXT NOT NULL,
|
|
242
|
+
valid_from TEXT,
|
|
243
|
+
valid_until TEXT,
|
|
244
|
+
system_retired_at TEXT,
|
|
245
|
+
session_id TEXT,
|
|
246
|
+
capture_context TEXT,
|
|
247
|
+
access_count INTEGER NOT NULL DEFAULT 0
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS facts_fts USING fts5(
|
|
251
|
+
content, domain, subdomain,
|
|
252
|
+
content=facts, content_rowid=rowid
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
-- FTS5 external content sync triggers.
|
|
256
|
+
-- Only INSERT and DELETE are needed: facts are immutable so the
|
|
257
|
+
-- FTS5-indexed columns (content, domain, subdomain) are never UPDATEd.
|
|
258
|
+
-- supersedeFact only updates status/is_latest/valid_until (and, in
|
|
259
|
+
-- bi-temporal mode, system_retired_at), which are not in the FTS5 index.
|
|
260
|
+
-- DELETE trigger is a safety net — facts are never deleted in normal
|
|
261
|
+
-- operation, but if one were, FTS5 must stay in sync.
|
|
262
|
+
CREATE TRIGGER IF NOT EXISTS facts_ai AFTER INSERT ON facts BEGIN
|
|
263
|
+
INSERT INTO facts_fts(rowid, content, domain, subdomain)
|
|
264
|
+
VALUES (new.rowid, new.content, new.domain, new.subdomain);
|
|
265
|
+
END;
|
|
266
|
+
|
|
267
|
+
CREATE TRIGGER IF NOT EXISTS facts_ad AFTER DELETE ON facts BEGIN
|
|
268
|
+
INSERT INTO facts_fts(facts_fts, rowid, content, domain, subdomain)
|
|
269
|
+
VALUES ('delete', old.rowid, old.content, old.domain, old.subdomain);
|
|
270
|
+
END;
|
|
271
|
+
|
|
272
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
273
|
+
id TEXT PRIMARY KEY,
|
|
274
|
+
type TEXT NOT NULL,
|
|
275
|
+
name TEXT NOT NULL,
|
|
276
|
+
canonical_name TEXT NOT NULL,
|
|
277
|
+
metadata TEXT,
|
|
278
|
+
created_at TEXT NOT NULL,
|
|
279
|
+
access_count INTEGER NOT NULL DEFAULT 0,
|
|
280
|
+
last_accessed_at TEXT
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
CREATE TABLE IF NOT EXISTS fact_entities (
|
|
284
|
+
fact_id TEXT NOT NULL,
|
|
285
|
+
entity_id TEXT NOT NULL,
|
|
286
|
+
relationship TEXT NOT NULL,
|
|
287
|
+
PRIMARY KEY (fact_id, entity_id, relationship)
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
CREATE TABLE IF NOT EXISTS entity_edges (
|
|
291
|
+
from_entity TEXT NOT NULL,
|
|
292
|
+
to_entity TEXT NOT NULL,
|
|
293
|
+
relationship TEXT NOT NULL,
|
|
294
|
+
strength REAL NOT NULL,
|
|
295
|
+
metadata TEXT,
|
|
296
|
+
created_at TEXT NOT NULL,
|
|
297
|
+
last_accessed_at TEXT,
|
|
298
|
+
PRIMARY KEY (from_entity, to_entity, relationship)
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
-- Source provenance records.
|
|
302
|
+
CREATE TABLE IF NOT EXISTS sources (
|
|
303
|
+
id TEXT PRIMARY KEY,
|
|
304
|
+
type TEXT NOT NULL,
|
|
305
|
+
tool_id TEXT,
|
|
306
|
+
timestamp TEXT NOT NULL,
|
|
307
|
+
raw_content TEXT,
|
|
308
|
+
metadata TEXT
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
CREATE TABLE IF NOT EXISTS consolidations (
|
|
312
|
+
id TEXT PRIMARY KEY,
|
|
313
|
+
session_id TEXT,
|
|
314
|
+
facts_in INTEGER NOT NULL,
|
|
315
|
+
facts_graduated INTEGER NOT NULL,
|
|
316
|
+
facts_rejected INTEGER NOT NULL,
|
|
317
|
+
entities_created INTEGER NOT NULL DEFAULT 0,
|
|
318
|
+
entities_linked INTEGER NOT NULL DEFAULT 0,
|
|
319
|
+
supersessions INTEGER NOT NULL DEFAULT 0,
|
|
320
|
+
summary TEXT,
|
|
321
|
+
open_threads TEXT,
|
|
322
|
+
created_at TEXT NOT NULL
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
CREATE INDEX IF NOT EXISTS idx_facts_domain ON facts(domain, subdomain);
|
|
326
|
+
CREATE INDEX IF NOT EXISTS idx_facts_status ON facts(status, is_latest);
|
|
327
|
+
CREATE INDEX IF NOT EXISTS idx_facts_session ON facts(session_id);
|
|
328
|
+
CREATE INDEX IF NOT EXISTS idx_fact_entities_entity ON fact_entities(entity_id);
|
|
329
|
+
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
330
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_entities_canonical_type ON entities(canonical_name, type);
|
|
331
|
+
CREATE INDEX IF NOT EXISTS idx_entity_edges_from ON entity_edges(from_entity);
|
|
332
|
+
CREATE INDEX IF NOT EXISTS idx_entity_edges_to ON entity_edges(to_entity);
|
|
333
333
|
`);
|
|
334
334
|
await pragmaWrite(db, "user_version = 4");
|
|
335
335
|
}
|
|
@@ -341,9 +341,9 @@ async function applyV4(db) {
|
|
|
341
341
|
// when a run emits zero facts).
|
|
342
342
|
// ---------------------------------------------------------------------------
|
|
343
343
|
async function applyV5(db) {
|
|
344
|
-
await db.exec(`
|
|
345
|
-
ALTER TABLE consolidations ADD COLUMN last_event_sequence INTEGER NOT NULL DEFAULT 0;
|
|
346
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_created ON consolidations(created_at);
|
|
344
|
+
await db.exec(`
|
|
345
|
+
ALTER TABLE consolidations ADD COLUMN last_event_sequence INTEGER NOT NULL DEFAULT 0;
|
|
346
|
+
CREATE INDEX IF NOT EXISTS idx_consolidations_created ON consolidations(created_at);
|
|
347
347
|
`);
|
|
348
348
|
await pragmaWrite(db, "user_version = 5");
|
|
349
349
|
}
|
|
@@ -356,18 +356,18 @@ async function applyV5(db) {
|
|
|
356
356
|
// re-invoking the LLM. All nullable so the heuristic provider stays conformant.
|
|
357
357
|
// ---------------------------------------------------------------------------
|
|
358
358
|
async function applyV6(db) {
|
|
359
|
-
await db.exec(`
|
|
360
|
-
ALTER TABLE facts ADD COLUMN source_quality TEXT NOT NULL DEFAULT 'heuristic'
|
|
361
|
-
CHECK (source_quality IN ('heuristic', 'cli', 'sampling', 'explicit'));
|
|
362
|
-
|
|
363
|
-
ALTER TABLE session_facts ADD COLUMN subdomain_hint TEXT;
|
|
364
|
-
ALTER TABLE session_facts ADD COLUMN confidence_signal REAL;
|
|
365
|
-
ALTER TABLE session_facts ADD COLUMN importance_signal REAL;
|
|
366
|
-
ALTER TABLE session_facts ADD COLUMN valid_from_hint TEXT;
|
|
367
|
-
ALTER TABLE session_facts ADD COLUMN valid_until_hint TEXT;
|
|
368
|
-
ALTER TABLE session_facts ADD COLUMN entities_json TEXT;
|
|
369
|
-
ALTER TABLE session_facts ADD COLUMN source_quality TEXT NOT NULL DEFAULT 'heuristic'
|
|
370
|
-
CHECK (source_quality IN ('heuristic', 'cli', 'sampling', 'explicit'));
|
|
359
|
+
await db.exec(`
|
|
360
|
+
ALTER TABLE facts ADD COLUMN source_quality TEXT NOT NULL DEFAULT 'heuristic'
|
|
361
|
+
CHECK (source_quality IN ('heuristic', 'cli', 'sampling', 'explicit'));
|
|
362
|
+
|
|
363
|
+
ALTER TABLE session_facts ADD COLUMN subdomain_hint TEXT;
|
|
364
|
+
ALTER TABLE session_facts ADD COLUMN confidence_signal REAL;
|
|
365
|
+
ALTER TABLE session_facts ADD COLUMN importance_signal REAL;
|
|
366
|
+
ALTER TABLE session_facts ADD COLUMN valid_from_hint TEXT;
|
|
367
|
+
ALTER TABLE session_facts ADD COLUMN valid_until_hint TEXT;
|
|
368
|
+
ALTER TABLE session_facts ADD COLUMN entities_json TEXT;
|
|
369
|
+
ALTER TABLE session_facts ADD COLUMN source_quality TEXT NOT NULL DEFAULT 'heuristic'
|
|
370
|
+
CHECK (source_quality IN ('heuristic', 'cli', 'sampling', 'explicit'));
|
|
371
371
|
`);
|
|
372
372
|
await pragmaWrite(db, "user_version = 6");
|
|
373
373
|
}
|
|
@@ -380,35 +380,35 @@ async function applyV6(db) {
|
|
|
380
380
|
// ---------------------------------------------------------------------------
|
|
381
381
|
async function applyV7(db) {
|
|
382
382
|
await pragmaWrite(db, "foreign_keys = OFF");
|
|
383
|
-
await db.exec(`
|
|
384
|
-
CREATE TABLE consolidations_new (
|
|
385
|
-
id TEXT PRIMARY KEY,
|
|
386
|
-
session_id TEXT,
|
|
387
|
-
facts_in INTEGER NOT NULL,
|
|
388
|
-
facts_graduated INTEGER NOT NULL,
|
|
389
|
-
facts_rejected INTEGER NOT NULL,
|
|
390
|
-
entities_created INTEGER NOT NULL DEFAULT 0,
|
|
391
|
-
entities_linked INTEGER NOT NULL DEFAULT 0,
|
|
392
|
-
supersessions INTEGER NOT NULL DEFAULT 0,
|
|
393
|
-
summary TEXT,
|
|
394
|
-
open_threads TEXT,
|
|
395
|
-
created_at TEXT NOT NULL,
|
|
396
|
-
last_event_sequence INTEGER NOT NULL DEFAULT 0
|
|
397
|
-
);
|
|
398
|
-
|
|
399
|
-
INSERT INTO consolidations_new
|
|
400
|
-
(id, session_id, facts_in, facts_graduated, facts_rejected,
|
|
401
|
-
entities_created, entities_linked, supersessions,
|
|
402
|
-
summary, open_threads, created_at, last_event_sequence)
|
|
403
|
-
SELECT id, session_id, facts_in, facts_graduated, facts_rejected,
|
|
404
|
-
entities_created, entities_linked, supersessions,
|
|
405
|
-
summary, open_threads, created_at, last_event_sequence
|
|
406
|
-
FROM consolidations;
|
|
407
|
-
|
|
408
|
-
DROP TABLE consolidations;
|
|
409
|
-
ALTER TABLE consolidations_new RENAME TO consolidations;
|
|
410
|
-
|
|
411
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_created ON consolidations(created_at);
|
|
383
|
+
await db.exec(`
|
|
384
|
+
CREATE TABLE consolidations_new (
|
|
385
|
+
id TEXT PRIMARY KEY,
|
|
386
|
+
session_id TEXT,
|
|
387
|
+
facts_in INTEGER NOT NULL,
|
|
388
|
+
facts_graduated INTEGER NOT NULL,
|
|
389
|
+
facts_rejected INTEGER NOT NULL,
|
|
390
|
+
entities_created INTEGER NOT NULL DEFAULT 0,
|
|
391
|
+
entities_linked INTEGER NOT NULL DEFAULT 0,
|
|
392
|
+
supersessions INTEGER NOT NULL DEFAULT 0,
|
|
393
|
+
summary TEXT,
|
|
394
|
+
open_threads TEXT,
|
|
395
|
+
created_at TEXT NOT NULL,
|
|
396
|
+
last_event_sequence INTEGER NOT NULL DEFAULT 0
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
INSERT INTO consolidations_new
|
|
400
|
+
(id, session_id, facts_in, facts_graduated, facts_rejected,
|
|
401
|
+
entities_created, entities_linked, supersessions,
|
|
402
|
+
summary, open_threads, created_at, last_event_sequence)
|
|
403
|
+
SELECT id, session_id, facts_in, facts_graduated, facts_rejected,
|
|
404
|
+
entities_created, entities_linked, supersessions,
|
|
405
|
+
summary, open_threads, created_at, last_event_sequence
|
|
406
|
+
FROM consolidations;
|
|
407
|
+
|
|
408
|
+
DROP TABLE consolidations;
|
|
409
|
+
ALTER TABLE consolidations_new RENAME TO consolidations;
|
|
410
|
+
|
|
411
|
+
CREATE INDEX IF NOT EXISTS idx_consolidations_created ON consolidations(created_at);
|
|
412
412
|
`);
|
|
413
413
|
await pragmaWrite(db, "foreign_keys = ON");
|
|
414
414
|
await pragmaWrite(db, "user_version = 7");
|
|
@@ -430,35 +430,35 @@ async function applyV7(db) {
|
|
|
430
430
|
* that means anything before consolidation.
|
|
431
431
|
*/
|
|
432
432
|
async function applyV8(db) {
|
|
433
|
-
await db.exec(`
|
|
434
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS session_facts_fts USING fts5(
|
|
435
|
-
content,
|
|
436
|
-
content=session_facts, content_rowid=rowid
|
|
437
|
-
);
|
|
438
|
-
|
|
439
|
-
-- Sync triggers. INSERT and DELETE only, deliberately:
|
|
440
|
-
-- session_facts.content is never UPDATEd — the sole update in the codebase
|
|
441
|
-
-- sets consolidation_id (claimForConsolidation, and its release on rollback),
|
|
442
|
-
-- which is not indexed here. An UPDATE trigger would fire on every claim and
|
|
443
|
-
-- rewrite the index for a column change FTS5 cannot see.
|
|
444
|
-
-- The DELETE trigger is a safety net: nothing deletes session_facts today,
|
|
445
|
-
-- but retention will, and an external-content index that silently drifts is
|
|
446
|
-
-- worse than one that costs a trigger.
|
|
447
|
-
CREATE TRIGGER IF NOT EXISTS session_facts_ai AFTER INSERT ON session_facts BEGIN
|
|
448
|
-
INSERT INTO session_facts_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
449
|
-
END;
|
|
450
|
-
|
|
451
|
-
CREATE TRIGGER IF NOT EXISTS session_facts_ad AFTER DELETE ON session_facts BEGIN
|
|
452
|
-
INSERT INTO session_facts_fts(session_facts_fts, rowid, content)
|
|
453
|
-
VALUES ('delete', old.rowid, old.content);
|
|
454
|
-
END;
|
|
433
|
+
await db.exec(`
|
|
434
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS session_facts_fts USING fts5(
|
|
435
|
+
content,
|
|
436
|
+
content=session_facts, content_rowid=rowid
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
-- Sync triggers. INSERT and DELETE only, deliberately:
|
|
440
|
+
-- session_facts.content is never UPDATEd — the sole update in the codebase
|
|
441
|
+
-- sets consolidation_id (claimForConsolidation, and its release on rollback),
|
|
442
|
+
-- which is not indexed here. An UPDATE trigger would fire on every claim and
|
|
443
|
+
-- rewrite the index for a column change FTS5 cannot see.
|
|
444
|
+
-- The DELETE trigger is a safety net: nothing deletes session_facts today,
|
|
445
|
+
-- but retention will, and an external-content index that silently drifts is
|
|
446
|
+
-- worse than one that costs a trigger.
|
|
447
|
+
CREATE TRIGGER IF NOT EXISTS session_facts_ai AFTER INSERT ON session_facts BEGIN
|
|
448
|
+
INSERT INTO session_facts_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
449
|
+
END;
|
|
450
|
+
|
|
451
|
+
CREATE TRIGGER IF NOT EXISTS session_facts_ad AFTER DELETE ON session_facts BEGIN
|
|
452
|
+
INSERT INTO session_facts_fts(session_facts_fts, rowid, content)
|
|
453
|
+
VALUES ('delete', old.rowid, old.content);
|
|
454
|
+
END;
|
|
455
455
|
`);
|
|
456
456
|
// Backfill. Triggers only catch rows inserted from now on, so without this
|
|
457
457
|
// every fact captured before this migration stays invisible to search — the
|
|
458
458
|
// exact bug this migration exists to fix, preserved for existing users.
|
|
459
|
-
await db.exec(`
|
|
460
|
-
INSERT INTO session_facts_fts(rowid, content)
|
|
461
|
-
SELECT rowid, content FROM session_facts;
|
|
459
|
+
await db.exec(`
|
|
460
|
+
INSERT INTO session_facts_fts(rowid, content)
|
|
461
|
+
SELECT rowid, content FROM session_facts;
|
|
462
462
|
`);
|
|
463
463
|
await pragmaWrite(db, "user_version = 8");
|
|
464
464
|
}
|
|
@@ -492,15 +492,15 @@ async function applyV9(db) {
|
|
|
492
492
|
// SQLite cannot add a column conditionally, and this migration must be safe
|
|
493
493
|
// to run against a store already carrying entities. DEFAULT 0 backfills every
|
|
494
494
|
// existing row as "not the user", which is correct: none of them was.
|
|
495
|
-
await db.exec(`
|
|
496
|
-
ALTER TABLE entities ADD COLUMN is_self INTEGER NOT NULL DEFAULT 0;
|
|
495
|
+
await db.exec(`
|
|
496
|
+
ALTER TABLE entities ADD COLUMN is_self INTEGER NOT NULL DEFAULT 0;
|
|
497
497
|
`);
|
|
498
|
-
await db.exec(`
|
|
499
|
-
-- Partial, so it constrains only the single row that claims to be the user
|
|
500
|
-
-- and leaves every other entity unaffected. A second self becomes an error
|
|
501
|
-
-- at the database rather than a duplicate nobody notices.
|
|
502
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_entities_self
|
|
503
|
-
ON entities(is_self) WHERE is_self = 1;
|
|
498
|
+
await db.exec(`
|
|
499
|
+
-- Partial, so it constrains only the single row that claims to be the user
|
|
500
|
+
-- and leaves every other entity unaffected. A second self becomes an error
|
|
501
|
+
-- at the database rather than a duplicate nobody notices.
|
|
502
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_entities_self
|
|
503
|
+
ON entities(is_self) WHERE is_self = 1;
|
|
504
504
|
`);
|
|
505
505
|
await pragmaWrite(db, "user_version = 9");
|
|
506
506
|
}
|
|
@@ -527,25 +527,25 @@ async function applyV9(db) {
|
|
|
527
527
|
// doubles the facts that fit in the same budget.
|
|
528
528
|
// ---------------------------------------------------------------------------
|
|
529
529
|
async function applyV10(db) {
|
|
530
|
-
await db.exec(`
|
|
531
|
-
CREATE TABLE IF NOT EXISTS fact_embeddings (
|
|
532
|
-
fact_id TEXT PRIMARY KEY,
|
|
533
|
-
-- The model and dimension that produced this vector. NOT metadata:
|
|
534
|
-
-- vectors from different models occupy different spaces and comparing
|
|
535
|
-
-- them yields confident nonsense with no error anywhere. Every read
|
|
536
|
-
-- filters on both, so a model change is a detectable state with a
|
|
537
|
-
-- re-embed path rather than silent corruption.
|
|
538
|
-
model TEXT NOT NULL,
|
|
539
|
-
dimensions INTEGER NOT NULL,
|
|
540
|
-
-- Float32Array, little-endian. 4 bytes per dimension.
|
|
541
|
-
vector BLOB NOT NULL,
|
|
542
|
-
created_at TEXT NOT NULL
|
|
543
|
-
);
|
|
544
|
-
|
|
545
|
-
-- The scan filters on (model, dimensions) before touching any vector, so
|
|
546
|
-
-- a store mid-migration between models reads only the rows it can use.
|
|
547
|
-
CREATE INDEX IF NOT EXISTS idx_fact_embeddings_model
|
|
548
|
-
ON fact_embeddings(model, dimensions);
|
|
530
|
+
await db.exec(`
|
|
531
|
+
CREATE TABLE IF NOT EXISTS fact_embeddings (
|
|
532
|
+
fact_id TEXT PRIMARY KEY,
|
|
533
|
+
-- The model and dimension that produced this vector. NOT metadata:
|
|
534
|
+
-- vectors from different models occupy different spaces and comparing
|
|
535
|
+
-- them yields confident nonsense with no error anywhere. Every read
|
|
536
|
+
-- filters on both, so a model change is a detectable state with a
|
|
537
|
+
-- re-embed path rather than silent corruption.
|
|
538
|
+
model TEXT NOT NULL,
|
|
539
|
+
dimensions INTEGER NOT NULL,
|
|
540
|
+
-- Float32Array, little-endian. 4 bytes per dimension.
|
|
541
|
+
vector BLOB NOT NULL,
|
|
542
|
+
created_at TEXT NOT NULL
|
|
543
|
+
);
|
|
544
|
+
|
|
545
|
+
-- The scan filters on (model, dimensions) before touching any vector, so
|
|
546
|
+
-- a store mid-migration between models reads only the rows it can use.
|
|
547
|
+
CREATE INDEX IF NOT EXISTS idx_fact_embeddings_model
|
|
548
|
+
ON fact_embeddings(model, dimensions);
|
|
549
549
|
`);
|
|
550
550
|
await pragmaWrite(db, "user_version = 10");
|
|
551
551
|
}
|
|
@@ -565,14 +565,14 @@ async function applyV10(db) {
|
|
|
565
565
|
// detected rather than tailed from a now-invalid offset.
|
|
566
566
|
// ---------------------------------------------------------------------------
|
|
567
567
|
async function applyV11(db) {
|
|
568
|
-
await db.exec(`
|
|
569
|
-
CREATE TABLE IF NOT EXISTS source_watermarks (
|
|
570
|
-
path TEXT PRIMARY KEY,
|
|
571
|
-
byte_offset INTEGER NOT NULL,
|
|
572
|
-
line_number INTEGER NOT NULL,
|
|
573
|
-
fingerprint TEXT NOT NULL,
|
|
574
|
-
updated_at TEXT NOT NULL
|
|
575
|
-
);
|
|
568
|
+
await db.exec(`
|
|
569
|
+
CREATE TABLE IF NOT EXISTS source_watermarks (
|
|
570
|
+
path TEXT PRIMARY KEY,
|
|
571
|
+
byte_offset INTEGER NOT NULL,
|
|
572
|
+
line_number INTEGER NOT NULL,
|
|
573
|
+
fingerprint TEXT NOT NULL,
|
|
574
|
+
updated_at TEXT NOT NULL
|
|
575
|
+
);
|
|
576
576
|
`);
|
|
577
577
|
await pragmaWrite(db, "user_version = 11");
|
|
578
578
|
}
|
|
@@ -586,12 +586,12 @@ async function applyV11(db) {
|
|
|
586
586
|
// column lists keep working.
|
|
587
587
|
// ---------------------------------------------------------------------------
|
|
588
588
|
async function applyV12(db) {
|
|
589
|
-
await db.exec(`
|
|
590
|
-
ALTER TABLE consolidations ADD COLUMN now TEXT;
|
|
591
|
-
ALTER TABLE consolidations ADD COLUMN now_start_sequence INTEGER;
|
|
592
|
-
ALTER TABLE consolidations ADD COLUMN now_referents TEXT;
|
|
593
|
-
ALTER TABLE consolidations ADD COLUMN segments TEXT;
|
|
594
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_session ON consolidations(session_id);
|
|
589
|
+
await db.exec(`
|
|
590
|
+
ALTER TABLE consolidations ADD COLUMN now TEXT;
|
|
591
|
+
ALTER TABLE consolidations ADD COLUMN now_start_sequence INTEGER;
|
|
592
|
+
ALTER TABLE consolidations ADD COLUMN now_referents TEXT;
|
|
593
|
+
ALTER TABLE consolidations ADD COLUMN segments TEXT;
|
|
594
|
+
CREATE INDEX IF NOT EXISTS idx_consolidations_session ON consolidations(session_id);
|
|
595
595
|
`);
|
|
596
596
|
await pragmaWrite(db, "user_version = 12");
|
|
597
597
|
}
|
|
@@ -609,26 +609,26 @@ async function applyV12(db) {
|
|
|
609
609
|
// (prune DELETEs). INSERT and DELETE triggers; no UPDATE trigger.
|
|
610
610
|
// ---------------------------------------------------------------------------
|
|
611
611
|
async function applyV13(db) {
|
|
612
|
-
await db.exec(`
|
|
613
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS session_events_fts USING fts5(
|
|
614
|
-
content,
|
|
615
|
-
content=session_events, content_rowid=rowid
|
|
616
|
-
);
|
|
617
|
-
|
|
618
|
-
CREATE TRIGGER IF NOT EXISTS session_events_ai AFTER INSERT ON session_events
|
|
619
|
-
WHEN new.content IS NOT NULL BEGIN
|
|
620
|
-
INSERT INTO session_events_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
621
|
-
END;
|
|
622
|
-
|
|
623
|
-
CREATE TRIGGER IF NOT EXISTS session_events_ad AFTER DELETE ON session_events
|
|
624
|
-
WHEN old.content IS NOT NULL BEGIN
|
|
625
|
-
INSERT INTO session_events_fts(session_events_fts, rowid, content)
|
|
626
|
-
VALUES ('delete', old.rowid, old.content);
|
|
627
|
-
END;
|
|
612
|
+
await db.exec(`
|
|
613
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS session_events_fts USING fts5(
|
|
614
|
+
content,
|
|
615
|
+
content=session_events, content_rowid=rowid
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
CREATE TRIGGER IF NOT EXISTS session_events_ai AFTER INSERT ON session_events
|
|
619
|
+
WHEN new.content IS NOT NULL BEGIN
|
|
620
|
+
INSERT INTO session_events_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
621
|
+
END;
|
|
622
|
+
|
|
623
|
+
CREATE TRIGGER IF NOT EXISTS session_events_ad AFTER DELETE ON session_events
|
|
624
|
+
WHEN old.content IS NOT NULL BEGIN
|
|
625
|
+
INSERT INTO session_events_fts(session_events_fts, rowid, content)
|
|
626
|
+
VALUES ('delete', old.rowid, old.content);
|
|
627
|
+
END;
|
|
628
628
|
`);
|
|
629
|
-
await db.exec(`
|
|
630
|
-
INSERT INTO session_events_fts(rowid, content)
|
|
631
|
-
SELECT rowid, content FROM session_events WHERE content IS NOT NULL;
|
|
629
|
+
await db.exec(`
|
|
630
|
+
INSERT INTO session_events_fts(rowid, content)
|
|
631
|
+
SELECT rowid, content FROM session_events WHERE content IS NOT NULL;
|
|
632
632
|
`);
|
|
633
633
|
await pragmaWrite(db, "user_version = 13");
|
|
634
634
|
}
|
|
@@ -646,8 +646,8 @@ async function applyV13(db) {
|
|
|
646
646
|
// does not reorder extract.
|
|
647
647
|
// ---------------------------------------------------------------------------
|
|
648
648
|
async function applyV14(db) {
|
|
649
|
-
await db.exec(`
|
|
650
|
-
ALTER TABLE session_events ADD COLUMN occurred_at TEXT;
|
|
649
|
+
await db.exec(`
|
|
650
|
+
ALTER TABLE session_events ADD COLUMN occurred_at TEXT;
|
|
651
651
|
`);
|
|
652
652
|
await pragmaWrite(db, "user_version = 14");
|
|
653
653
|
}
|
|
@@ -661,25 +661,25 @@ async function applyV14(db) {
|
|
|
661
661
|
// table exists so a store can opt in without a second migration.
|
|
662
662
|
// ---------------------------------------------------------------------------
|
|
663
663
|
async function applyV15(db) {
|
|
664
|
-
await db.exec(`
|
|
665
|
-
CREATE TABLE IF NOT EXISTS inferences (
|
|
666
|
-
id TEXT PRIMARY KEY,
|
|
667
|
-
hypothesis TEXT NOT NULL,
|
|
668
|
-
status TEXT NOT NULL CHECK (status IN ('pending', 'confirmed', 'rejected')),
|
|
669
|
-
reason TEXT,
|
|
670
|
-
fact_id TEXT,
|
|
671
|
-
created_at TEXT NOT NULL,
|
|
672
|
-
validated_at TEXT
|
|
673
|
-
);
|
|
674
|
-
|
|
675
|
-
CREATE INDEX IF NOT EXISTS idx_inferences_status
|
|
676
|
-
ON inferences(status, created_at);
|
|
677
|
-
|
|
678
|
-
CREATE TABLE IF NOT EXISTS inference_evidence (
|
|
679
|
-
inference_id TEXT NOT NULL,
|
|
680
|
-
fact_id TEXT NOT NULL,
|
|
681
|
-
PRIMARY KEY (inference_id, fact_id)
|
|
682
|
-
);
|
|
664
|
+
await db.exec(`
|
|
665
|
+
CREATE TABLE IF NOT EXISTS inferences (
|
|
666
|
+
id TEXT PRIMARY KEY,
|
|
667
|
+
hypothesis TEXT NOT NULL,
|
|
668
|
+
status TEXT NOT NULL CHECK (status IN ('pending', 'confirmed', 'rejected')),
|
|
669
|
+
reason TEXT,
|
|
670
|
+
fact_id TEXT,
|
|
671
|
+
created_at TEXT NOT NULL,
|
|
672
|
+
validated_at TEXT
|
|
673
|
+
);
|
|
674
|
+
|
|
675
|
+
CREATE INDEX IF NOT EXISTS idx_inferences_status
|
|
676
|
+
ON inferences(status, created_at);
|
|
677
|
+
|
|
678
|
+
CREATE TABLE IF NOT EXISTS inference_evidence (
|
|
679
|
+
inference_id TEXT NOT NULL,
|
|
680
|
+
fact_id TEXT NOT NULL,
|
|
681
|
+
PRIMARY KEY (inference_id, fact_id)
|
|
682
|
+
);
|
|
683
683
|
`);
|
|
684
684
|
await pragmaWrite(db, "user_version = 15");
|
|
685
685
|
}
|
|
@@ -691,11 +691,11 @@ async function applyV15(db) {
|
|
|
691
691
|
// is the event's role. Null when we cannot tell (no primary event).
|
|
692
692
|
// ---------------------------------------------------------------------------
|
|
693
693
|
async function applyV16(db) {
|
|
694
|
-
await db.exec(`
|
|
695
|
-
ALTER TABLE session_facts ADD COLUMN speaker_role TEXT
|
|
696
|
-
CHECK (speaker_role IS NULL OR speaker_role IN ('user', 'assistant', 'system', 'tool'));
|
|
697
|
-
ALTER TABLE facts ADD COLUMN speaker_role TEXT
|
|
698
|
-
CHECK (speaker_role IS NULL OR speaker_role IN ('user', 'assistant', 'system', 'tool'));
|
|
694
|
+
await db.exec(`
|
|
695
|
+
ALTER TABLE session_facts ADD COLUMN speaker_role TEXT
|
|
696
|
+
CHECK (speaker_role IS NULL OR speaker_role IN ('user', 'assistant', 'system', 'tool'));
|
|
697
|
+
ALTER TABLE facts ADD COLUMN speaker_role TEXT
|
|
698
|
+
CHECK (speaker_role IS NULL OR speaker_role IN ('user', 'assistant', 'system', 'tool'));
|
|
699
699
|
`);
|
|
700
700
|
await pragmaWrite(db, "user_version = 16");
|
|
701
701
|
}
|
|
@@ -707,10 +707,10 @@ async function applyV16(db) {
|
|
|
707
707
|
// no name. Do not spend a role value on "person".
|
|
708
708
|
// ---------------------------------------------------------------------------
|
|
709
709
|
async function applyV17(db) {
|
|
710
|
-
await db.exec(`
|
|
711
|
-
ALTER TABLE session_events ADD COLUMN speaker TEXT;
|
|
712
|
-
ALTER TABLE session_facts ADD COLUMN speaker TEXT;
|
|
713
|
-
ALTER TABLE facts ADD COLUMN speaker TEXT;
|
|
710
|
+
await db.exec(`
|
|
711
|
+
ALTER TABLE session_events ADD COLUMN speaker TEXT;
|
|
712
|
+
ALTER TABLE session_facts ADD COLUMN speaker TEXT;
|
|
713
|
+
ALTER TABLE facts ADD COLUMN speaker TEXT;
|
|
714
714
|
`);
|
|
715
715
|
await pragmaWrite(db, "user_version = 17");
|
|
716
716
|
}
|
|
@@ -723,24 +723,24 @@ async function applyV17(db) {
|
|
|
723
723
|
// SQLite cannot ALTER a CHECK, so the table is rebuilt.
|
|
724
724
|
// ---------------------------------------------------------------------------
|
|
725
725
|
async function applyV18(db) {
|
|
726
|
-
await db.exec(`
|
|
727
|
-
CREATE TABLE session_fact_sources_v18 (
|
|
728
|
-
session_fact_id TEXT NOT NULL,
|
|
729
|
-
event_id TEXT NOT NULL,
|
|
730
|
-
relevance REAL NOT NULL DEFAULT 1.0,
|
|
731
|
-
extraction_type TEXT NOT NULL DEFAULT 'contextual'
|
|
732
|
-
CHECK (extraction_type IN (
|
|
733
|
-
'primary', 'corroborating', 'contextual',
|
|
734
|
-
'assent', 'observation', 'restatement'
|
|
735
|
-
)),
|
|
736
|
-
PRIMARY KEY (session_fact_id, event_id)
|
|
737
|
-
);
|
|
738
|
-
INSERT INTO session_fact_sources_v18
|
|
739
|
-
(session_fact_id, event_id, relevance, extraction_type)
|
|
740
|
-
SELECT session_fact_id, event_id, relevance, extraction_type
|
|
741
|
-
FROM session_fact_sources;
|
|
742
|
-
DROP TABLE session_fact_sources;
|
|
743
|
-
ALTER TABLE session_fact_sources_v18 RENAME TO session_fact_sources;
|
|
726
|
+
await db.exec(`
|
|
727
|
+
CREATE TABLE session_fact_sources_v18 (
|
|
728
|
+
session_fact_id TEXT NOT NULL,
|
|
729
|
+
event_id TEXT NOT NULL,
|
|
730
|
+
relevance REAL NOT NULL DEFAULT 1.0,
|
|
731
|
+
extraction_type TEXT NOT NULL DEFAULT 'contextual'
|
|
732
|
+
CHECK (extraction_type IN (
|
|
733
|
+
'primary', 'corroborating', 'contextual',
|
|
734
|
+
'assent', 'observation', 'restatement'
|
|
735
|
+
)),
|
|
736
|
+
PRIMARY KEY (session_fact_id, event_id)
|
|
737
|
+
);
|
|
738
|
+
INSERT INTO session_fact_sources_v18
|
|
739
|
+
(session_fact_id, event_id, relevance, extraction_type)
|
|
740
|
+
SELECT session_fact_id, event_id, relevance, extraction_type
|
|
741
|
+
FROM session_fact_sources;
|
|
742
|
+
DROP TABLE session_fact_sources;
|
|
743
|
+
ALTER TABLE session_fact_sources_v18 RENAME TO session_fact_sources;
|
|
744
744
|
`);
|
|
745
745
|
await pragmaWrite(db, "user_version = 18");
|
|
746
746
|
}
|
|
@@ -754,14 +754,14 @@ async function applyV18(db) {
|
|
|
754
754
|
// consumed) vs extract (how far D→I has read).
|
|
755
755
|
// ---------------------------------------------------------------------------
|
|
756
756
|
async function applyV19(db) {
|
|
757
|
-
await db.exec(`
|
|
758
|
-
CREATE TABLE IF NOT EXISTS extract_watermarks (
|
|
759
|
-
kind TEXT NOT NULL CHECK (kind IN ('client', 'mcp', 'unkeyed')),
|
|
760
|
-
conversation_id TEXT NOT NULL,
|
|
761
|
-
last_event_sequence INTEGER NOT NULL,
|
|
762
|
-
updated_at TEXT NOT NULL,
|
|
763
|
-
PRIMARY KEY (kind, conversation_id)
|
|
764
|
-
);
|
|
757
|
+
await db.exec(`
|
|
758
|
+
CREATE TABLE IF NOT EXISTS extract_watermarks (
|
|
759
|
+
kind TEXT NOT NULL CHECK (kind IN ('client', 'mcp', 'unkeyed')),
|
|
760
|
+
conversation_id TEXT NOT NULL,
|
|
761
|
+
last_event_sequence INTEGER NOT NULL,
|
|
762
|
+
updated_at TEXT NOT NULL,
|
|
763
|
+
PRIMARY KEY (kind, conversation_id)
|
|
764
|
+
);
|
|
765
765
|
`);
|
|
766
766
|
await seedExtractWatermarksFromConsolidations(db);
|
|
767
767
|
await pragmaWrite(db, "user_version = 19");
|
|
@@ -773,16 +773,16 @@ async function applyV19(db) {
|
|
|
773
773
|
// may later bill without a run row, and stats must not have two definitions.
|
|
774
774
|
// ---------------------------------------------------------------------------
|
|
775
775
|
async function applyV20(db) {
|
|
776
|
-
await db.exec(`
|
|
777
|
-
CREATE TABLE IF NOT EXISTS intelligence_runs (
|
|
778
|
-
id TEXT PRIMARY KEY,
|
|
779
|
-
kind TEXT NOT NULL CHECK (kind IN ('consolidate', 'capture')),
|
|
780
|
-
consolidation_id TEXT,
|
|
781
|
-
created_at TEXT NOT NULL,
|
|
782
|
-
usage TEXT NOT NULL
|
|
783
|
-
);
|
|
784
|
-
CREATE INDEX IF NOT EXISTS idx_intelligence_runs_created
|
|
785
|
-
ON intelligence_runs(created_at);
|
|
776
|
+
await db.exec(`
|
|
777
|
+
CREATE TABLE IF NOT EXISTS intelligence_runs (
|
|
778
|
+
id TEXT PRIMARY KEY,
|
|
779
|
+
kind TEXT NOT NULL CHECK (kind IN ('consolidate', 'capture')),
|
|
780
|
+
consolidation_id TEXT,
|
|
781
|
+
created_at TEXT NOT NULL,
|
|
782
|
+
usage TEXT NOT NULL
|
|
783
|
+
);
|
|
784
|
+
CREATE INDEX IF NOT EXISTS idx_intelligence_runs_created
|
|
785
|
+
ON intelligence_runs(created_at);
|
|
786
786
|
`);
|
|
787
787
|
await pragmaWrite(db, "user_version = 20");
|
|
788
788
|
}
|
|
@@ -830,138 +830,138 @@ async function applyV23(db) {
|
|
|
830
830
|
const names = new Set(tables.map((t) => t.name));
|
|
831
831
|
await pragmaWrite(db, "foreign_keys = OFF");
|
|
832
832
|
if (names.has("facts")) {
|
|
833
|
-
await db.exec(`
|
|
834
|
-
DROP TRIGGER IF EXISTS facts_ai;
|
|
835
|
-
DROP TRIGGER IF EXISTS facts_ad;
|
|
836
|
-
DROP TABLE IF EXISTS facts_fts;
|
|
837
|
-
|
|
838
|
-
CREATE TABLE facts_v23 (
|
|
839
|
-
id TEXT PRIMARY KEY,
|
|
840
|
-
content TEXT NOT NULL,
|
|
841
|
-
domain TEXT NOT NULL,
|
|
842
|
-
subdomain TEXT,
|
|
843
|
-
confidence REAL NOT NULL DEFAULT 0.7,
|
|
844
|
-
importance REAL NOT NULL DEFAULT 0.5,
|
|
845
|
-
source_type TEXT NOT NULL,
|
|
846
|
-
source_tool TEXT,
|
|
847
|
-
source_id TEXT,
|
|
848
|
-
status TEXT NOT NULL DEFAULT 'active'
|
|
849
|
-
CHECK (status IN ('active', 'superseded', 'rejected')),
|
|
850
|
-
superseded_by TEXT,
|
|
851
|
-
is_latest INTEGER NOT NULL DEFAULT 1,
|
|
852
|
-
created_at TEXT NOT NULL,
|
|
853
|
-
valid_from TEXT,
|
|
854
|
-
valid_until TEXT,
|
|
855
|
-
system_retired_at TEXT,
|
|
856
|
-
session_id TEXT,
|
|
857
|
-
capture_context TEXT,
|
|
858
|
-
access_count INTEGER NOT NULL DEFAULT 0,
|
|
859
|
-
source_quality TEXT NOT NULL DEFAULT 'heuristic' ${quality},
|
|
860
|
-
speaker_role TEXT
|
|
861
|
-
CHECK (speaker_role IS NULL OR speaker_role IN ('user', 'assistant', 'system', 'tool')),
|
|
862
|
-
speaker TEXT
|
|
863
|
-
);
|
|
864
|
-
INSERT INTO facts_v23 (
|
|
865
|
-
id, content, domain, subdomain, confidence, importance,
|
|
866
|
-
source_type, source_tool, source_id, status, superseded_by, is_latest,
|
|
867
|
-
created_at, valid_from, valid_until, system_retired_at, session_id,
|
|
868
|
-
capture_context, access_count, source_quality, speaker_role, speaker
|
|
869
|
-
)
|
|
870
|
-
SELECT
|
|
871
|
-
id, content, domain, subdomain, confidence, importance,
|
|
872
|
-
source_type, source_tool, source_id, status, superseded_by, is_latest,
|
|
873
|
-
created_at, valid_from, valid_until, system_retired_at, session_id,
|
|
874
|
-
capture_context, access_count, source_quality, speaker_role, speaker
|
|
875
|
-
FROM facts;
|
|
876
|
-
DROP TABLE facts;
|
|
877
|
-
ALTER TABLE facts_v23 RENAME TO facts;
|
|
878
|
-
CREATE INDEX IF NOT EXISTS idx_facts_domain ON facts(domain, subdomain);
|
|
879
|
-
CREATE INDEX IF NOT EXISTS idx_facts_status ON facts(status, is_latest);
|
|
880
|
-
CREATE INDEX IF NOT EXISTS idx_facts_session ON facts(session_id);
|
|
881
|
-
|
|
882
|
-
CREATE VIRTUAL TABLE facts_fts USING fts5(
|
|
883
|
-
content, domain, subdomain,
|
|
884
|
-
content=facts, content_rowid=rowid
|
|
885
|
-
);
|
|
886
|
-
CREATE TRIGGER facts_ai AFTER INSERT ON facts BEGIN
|
|
887
|
-
INSERT INTO facts_fts(rowid, content, domain, subdomain)
|
|
888
|
-
VALUES (new.rowid, new.content, new.domain, new.subdomain);
|
|
889
|
-
END;
|
|
890
|
-
CREATE TRIGGER facts_ad AFTER DELETE ON facts BEGIN
|
|
891
|
-
INSERT INTO facts_fts(facts_fts, rowid, content, domain, subdomain)
|
|
892
|
-
VALUES ('delete', old.rowid, old.content, old.domain, old.subdomain);
|
|
893
|
-
END;
|
|
894
|
-
INSERT INTO facts_fts(rowid, content, domain, subdomain)
|
|
895
|
-
SELECT rowid, content, domain, subdomain FROM facts;
|
|
833
|
+
await db.exec(`
|
|
834
|
+
DROP TRIGGER IF EXISTS facts_ai;
|
|
835
|
+
DROP TRIGGER IF EXISTS facts_ad;
|
|
836
|
+
DROP TABLE IF EXISTS facts_fts;
|
|
837
|
+
|
|
838
|
+
CREATE TABLE facts_v23 (
|
|
839
|
+
id TEXT PRIMARY KEY,
|
|
840
|
+
content TEXT NOT NULL,
|
|
841
|
+
domain TEXT NOT NULL,
|
|
842
|
+
subdomain TEXT,
|
|
843
|
+
confidence REAL NOT NULL DEFAULT 0.7,
|
|
844
|
+
importance REAL NOT NULL DEFAULT 0.5,
|
|
845
|
+
source_type TEXT NOT NULL,
|
|
846
|
+
source_tool TEXT,
|
|
847
|
+
source_id TEXT,
|
|
848
|
+
status TEXT NOT NULL DEFAULT 'active'
|
|
849
|
+
CHECK (status IN ('active', 'superseded', 'rejected')),
|
|
850
|
+
superseded_by TEXT,
|
|
851
|
+
is_latest INTEGER NOT NULL DEFAULT 1,
|
|
852
|
+
created_at TEXT NOT NULL,
|
|
853
|
+
valid_from TEXT,
|
|
854
|
+
valid_until TEXT,
|
|
855
|
+
system_retired_at TEXT,
|
|
856
|
+
session_id TEXT,
|
|
857
|
+
capture_context TEXT,
|
|
858
|
+
access_count INTEGER NOT NULL DEFAULT 0,
|
|
859
|
+
source_quality TEXT NOT NULL DEFAULT 'heuristic' ${quality},
|
|
860
|
+
speaker_role TEXT
|
|
861
|
+
CHECK (speaker_role IS NULL OR speaker_role IN ('user', 'assistant', 'system', 'tool')),
|
|
862
|
+
speaker TEXT
|
|
863
|
+
);
|
|
864
|
+
INSERT INTO facts_v23 (
|
|
865
|
+
id, content, domain, subdomain, confidence, importance,
|
|
866
|
+
source_type, source_tool, source_id, status, superseded_by, is_latest,
|
|
867
|
+
created_at, valid_from, valid_until, system_retired_at, session_id,
|
|
868
|
+
capture_context, access_count, source_quality, speaker_role, speaker
|
|
869
|
+
)
|
|
870
|
+
SELECT
|
|
871
|
+
id, content, domain, subdomain, confidence, importance,
|
|
872
|
+
source_type, source_tool, source_id, status, superseded_by, is_latest,
|
|
873
|
+
created_at, valid_from, valid_until, system_retired_at, session_id,
|
|
874
|
+
capture_context, access_count, source_quality, speaker_role, speaker
|
|
875
|
+
FROM facts;
|
|
876
|
+
DROP TABLE facts;
|
|
877
|
+
ALTER TABLE facts_v23 RENAME TO facts;
|
|
878
|
+
CREATE INDEX IF NOT EXISTS idx_facts_domain ON facts(domain, subdomain);
|
|
879
|
+
CREATE INDEX IF NOT EXISTS idx_facts_status ON facts(status, is_latest);
|
|
880
|
+
CREATE INDEX IF NOT EXISTS idx_facts_session ON facts(session_id);
|
|
881
|
+
|
|
882
|
+
CREATE VIRTUAL TABLE facts_fts USING fts5(
|
|
883
|
+
content, domain, subdomain,
|
|
884
|
+
content=facts, content_rowid=rowid
|
|
885
|
+
);
|
|
886
|
+
CREATE TRIGGER facts_ai AFTER INSERT ON facts BEGIN
|
|
887
|
+
INSERT INTO facts_fts(rowid, content, domain, subdomain)
|
|
888
|
+
VALUES (new.rowid, new.content, new.domain, new.subdomain);
|
|
889
|
+
END;
|
|
890
|
+
CREATE TRIGGER facts_ad AFTER DELETE ON facts BEGIN
|
|
891
|
+
INSERT INTO facts_fts(facts_fts, rowid, content, domain, subdomain)
|
|
892
|
+
VALUES ('delete', old.rowid, old.content, old.domain, old.subdomain);
|
|
893
|
+
END;
|
|
894
|
+
INSERT INTO facts_fts(rowid, content, domain, subdomain)
|
|
895
|
+
SELECT rowid, content, domain, subdomain FROM facts;
|
|
896
896
|
`);
|
|
897
897
|
}
|
|
898
898
|
if (names.has("session_facts")) {
|
|
899
|
-
await db.exec(`
|
|
900
|
-
DROP TRIGGER IF EXISTS session_facts_ai;
|
|
901
|
-
DROP TRIGGER IF EXISTS session_facts_ad;
|
|
902
|
-
DROP TABLE IF EXISTS session_facts_fts;
|
|
903
|
-
|
|
904
|
-
CREATE TABLE session_facts_v23 (
|
|
905
|
-
id TEXT PRIMARY KEY,
|
|
906
|
-
session_id TEXT NOT NULL,
|
|
907
|
-
content TEXT NOT NULL,
|
|
908
|
-
content_hash TEXT NOT NULL,
|
|
909
|
-
source_origin TEXT NOT NULL DEFAULT 'explicit'
|
|
910
|
-
CHECK (source_origin IN ('explicit', 'inferred')),
|
|
911
|
-
source_event_id TEXT,
|
|
912
|
-
domain_hint TEXT,
|
|
913
|
-
subdomain_hint TEXT,
|
|
914
|
-
confidence REAL,
|
|
915
|
-
importance REAL,
|
|
916
|
-
confidence_signal REAL,
|
|
917
|
-
importance_signal REAL,
|
|
918
|
-
valid_from_hint TEXT,
|
|
919
|
-
valid_until_hint TEXT,
|
|
920
|
-
entities_json TEXT,
|
|
921
|
-
source_quality TEXT NOT NULL DEFAULT 'heuristic' ${quality},
|
|
922
|
-
source_tool TEXT,
|
|
923
|
-
capture_context TEXT,
|
|
924
|
-
consolidation_id TEXT,
|
|
925
|
-
created_at TEXT NOT NULL,
|
|
926
|
-
speaker_role TEXT
|
|
927
|
-
CHECK (speaker_role IS NULL OR speaker_role IN ('user', 'assistant', 'system', 'tool')),
|
|
928
|
-
speaker TEXT
|
|
929
|
-
);
|
|
930
|
-
INSERT INTO session_facts_v23 (
|
|
931
|
-
id, session_id, content, content_hash, source_origin, source_event_id,
|
|
932
|
-
domain_hint, subdomain_hint, confidence, importance, confidence_signal,
|
|
933
|
-
importance_signal, valid_from_hint, valid_until_hint, entities_json,
|
|
934
|
-
source_quality, source_tool, capture_context, consolidation_id, created_at,
|
|
935
|
-
speaker_role, speaker
|
|
936
|
-
)
|
|
937
|
-
SELECT
|
|
938
|
-
id, session_id, content, content_hash, source_origin, source_event_id,
|
|
939
|
-
domain_hint, subdomain_hint, confidence, importance, confidence_signal,
|
|
940
|
-
importance_signal, valid_from_hint, valid_until_hint, entities_json,
|
|
941
|
-
source_quality, source_tool, capture_context, consolidation_id, created_at,
|
|
942
|
-
speaker_role, speaker
|
|
943
|
-
FROM session_facts;
|
|
944
|
-
DROP TABLE session_facts;
|
|
945
|
-
ALTER TABLE session_facts_v23 RENAME TO session_facts;
|
|
946
|
-
CREATE INDEX IF NOT EXISTS idx_session_facts_session ON session_facts(session_id);
|
|
947
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_facts_hash
|
|
948
|
-
ON session_facts(session_id, content_hash);
|
|
949
|
-
CREATE INDEX IF NOT EXISTS idx_session_facts_unclaimed
|
|
950
|
-
ON session_facts(created_at) WHERE consolidation_id IS NULL;
|
|
951
|
-
|
|
952
|
-
CREATE VIRTUAL TABLE session_facts_fts USING fts5(
|
|
953
|
-
content,
|
|
954
|
-
content=session_facts, content_rowid=rowid
|
|
955
|
-
);
|
|
956
|
-
CREATE TRIGGER session_facts_ai AFTER INSERT ON session_facts BEGIN
|
|
957
|
-
INSERT INTO session_facts_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
958
|
-
END;
|
|
959
|
-
CREATE TRIGGER session_facts_ad AFTER DELETE ON session_facts BEGIN
|
|
960
|
-
INSERT INTO session_facts_fts(session_facts_fts, rowid, content)
|
|
961
|
-
VALUES ('delete', old.rowid, old.content);
|
|
962
|
-
END;
|
|
963
|
-
INSERT INTO session_facts_fts(rowid, content)
|
|
964
|
-
SELECT rowid, content FROM session_facts;
|
|
899
|
+
await db.exec(`
|
|
900
|
+
DROP TRIGGER IF EXISTS session_facts_ai;
|
|
901
|
+
DROP TRIGGER IF EXISTS session_facts_ad;
|
|
902
|
+
DROP TABLE IF EXISTS session_facts_fts;
|
|
903
|
+
|
|
904
|
+
CREATE TABLE session_facts_v23 (
|
|
905
|
+
id TEXT PRIMARY KEY,
|
|
906
|
+
session_id TEXT NOT NULL,
|
|
907
|
+
content TEXT NOT NULL,
|
|
908
|
+
content_hash TEXT NOT NULL,
|
|
909
|
+
source_origin TEXT NOT NULL DEFAULT 'explicit'
|
|
910
|
+
CHECK (source_origin IN ('explicit', 'inferred')),
|
|
911
|
+
source_event_id TEXT,
|
|
912
|
+
domain_hint TEXT,
|
|
913
|
+
subdomain_hint TEXT,
|
|
914
|
+
confidence REAL,
|
|
915
|
+
importance REAL,
|
|
916
|
+
confidence_signal REAL,
|
|
917
|
+
importance_signal REAL,
|
|
918
|
+
valid_from_hint TEXT,
|
|
919
|
+
valid_until_hint TEXT,
|
|
920
|
+
entities_json TEXT,
|
|
921
|
+
source_quality TEXT NOT NULL DEFAULT 'heuristic' ${quality},
|
|
922
|
+
source_tool TEXT,
|
|
923
|
+
capture_context TEXT,
|
|
924
|
+
consolidation_id TEXT,
|
|
925
|
+
created_at TEXT NOT NULL,
|
|
926
|
+
speaker_role TEXT
|
|
927
|
+
CHECK (speaker_role IS NULL OR speaker_role IN ('user', 'assistant', 'system', 'tool')),
|
|
928
|
+
speaker TEXT
|
|
929
|
+
);
|
|
930
|
+
INSERT INTO session_facts_v23 (
|
|
931
|
+
id, session_id, content, content_hash, source_origin, source_event_id,
|
|
932
|
+
domain_hint, subdomain_hint, confidence, importance, confidence_signal,
|
|
933
|
+
importance_signal, valid_from_hint, valid_until_hint, entities_json,
|
|
934
|
+
source_quality, source_tool, capture_context, consolidation_id, created_at,
|
|
935
|
+
speaker_role, speaker
|
|
936
|
+
)
|
|
937
|
+
SELECT
|
|
938
|
+
id, session_id, content, content_hash, source_origin, source_event_id,
|
|
939
|
+
domain_hint, subdomain_hint, confidence, importance, confidence_signal,
|
|
940
|
+
importance_signal, valid_from_hint, valid_until_hint, entities_json,
|
|
941
|
+
source_quality, source_tool, capture_context, consolidation_id, created_at,
|
|
942
|
+
speaker_role, speaker
|
|
943
|
+
FROM session_facts;
|
|
944
|
+
DROP TABLE session_facts;
|
|
945
|
+
ALTER TABLE session_facts_v23 RENAME TO session_facts;
|
|
946
|
+
CREATE INDEX IF NOT EXISTS idx_session_facts_session ON session_facts(session_id);
|
|
947
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_facts_hash
|
|
948
|
+
ON session_facts(session_id, content_hash);
|
|
949
|
+
CREATE INDEX IF NOT EXISTS idx_session_facts_unclaimed
|
|
950
|
+
ON session_facts(created_at) WHERE consolidation_id IS NULL;
|
|
951
|
+
|
|
952
|
+
CREATE VIRTUAL TABLE session_facts_fts USING fts5(
|
|
953
|
+
content,
|
|
954
|
+
content=session_facts, content_rowid=rowid
|
|
955
|
+
);
|
|
956
|
+
CREATE TRIGGER session_facts_ai AFTER INSERT ON session_facts BEGIN
|
|
957
|
+
INSERT INTO session_facts_fts(rowid, content) VALUES (new.rowid, new.content);
|
|
958
|
+
END;
|
|
959
|
+
CREATE TRIGGER session_facts_ad AFTER DELETE ON session_facts BEGIN
|
|
960
|
+
INSERT INTO session_facts_fts(session_facts_fts, rowid, content)
|
|
961
|
+
VALUES ('delete', old.rowid, old.content);
|
|
962
|
+
END;
|
|
963
|
+
INSERT INTO session_facts_fts(rowid, content)
|
|
964
|
+
SELECT rowid, content FROM session_facts;
|
|
965
965
|
`);
|
|
966
966
|
}
|
|
967
967
|
await pragmaWrite(db, "foreign_keys = ON");
|