neuralbrain 0.0.1 → 0.1.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/.env.example +2 -2
- package/README.md +19 -4
- package/dist/ai/chat.d.ts +1 -2
- package/dist/ai/chat.d.ts.map +1 -1
- package/dist/ai/chat.js +1 -2
- package/dist/ai/chat.js.map +1 -1
- package/dist/ai/config.d.ts +1 -1
- package/dist/ai/config.js +2 -2
- package/dist/ai/config.js.map +1 -1
- package/dist/app.d.ts +1 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +417 -50
- package/dist/app.js.map +1 -1
- package/dist/cli.d.ts +53 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +437 -37
- package/dist/cli.js.map +1 -1
- package/dist/consolidation/candidates.d.ts.map +1 -1
- package/dist/consolidation/candidates.js +19 -4
- package/dist/consolidation/candidates.js.map +1 -1
- package/dist/consolidation/fixtures.js +1 -1
- package/dist/consolidation/fixtures.js.map +1 -1
- package/dist/consolidation/gate.d.ts.map +1 -1
- package/dist/consolidation/gate.js +145 -109
- package/dist/consolidation/gate.js.map +1 -1
- package/dist/consolidation/runner.d.ts +6 -0
- package/dist/consolidation/runner.d.ts.map +1 -1
- package/dist/consolidation/runner.js +22 -2
- package/dist/consolidation/runner.js.map +1 -1
- package/dist/dashboard/clientScript.d.ts +1 -1
- package/dist/dashboard/clientScript.d.ts.map +1 -1
- package/dist/dashboard/clientScript.js +3774 -905
- package/dist/dashboard/clientScript.js.map +1 -1
- package/dist/dashboard/html.d.ts.map +1 -1
- package/dist/dashboard/html.js +605 -101
- package/dist/dashboard/html.js.map +1 -1
- package/dist/dashboard/styles.d.ts +1 -1
- package/dist/dashboard/styles.d.ts.map +1 -1
- package/dist/dashboard/styles.js +1591 -359
- package/dist/dashboard/styles.js.map +1 -1
- package/dist/learning/embedWorker.d.ts +13 -0
- package/dist/learning/embedWorker.d.ts.map +1 -1
- package/dist/learning/embedWorker.js +329 -59
- package/dist/learning/embedWorker.js.map +1 -1
- package/dist/learning/embedding.d.ts +1 -0
- package/dist/learning/embedding.d.ts.map +1 -1
- package/dist/learning/embedding.js +101 -26
- package/dist/learning/embedding.js.map +1 -1
- package/dist/learning/engine.js +1 -1
- package/dist/learning/engine.js.map +1 -1
- package/dist/learning/search.d.ts +17 -1
- package/dist/learning/search.d.ts.map +1 -1
- package/dist/learning/search.js +2094 -354
- package/dist/learning/search.js.map +1 -1
- package/dist/learning/storeMeta.d.ts +9 -0
- package/dist/learning/storeMeta.d.ts.map +1 -1
- package/dist/learning/storeMeta.js +30 -24
- package/dist/learning/storeMeta.js.map +1 -1
- package/dist/learning/types.d.ts +57 -8
- package/dist/learning/types.d.ts.map +1 -1
- package/dist/lib/condenser.d.ts +112 -0
- package/dist/lib/condenser.d.ts.map +1 -0
- package/dist/lib/condenser.js +1337 -0
- package/dist/lib/condenser.js.map +1 -0
- package/dist/lib/db.d.ts +10 -0
- package/dist/lib/db.d.ts.map +1 -1
- package/dist/lib/db.js +654 -167
- package/dist/lib/db.js.map +1 -1
- package/dist/lib/logger.d.ts.map +1 -1
- package/dist/lib/logger.js +3 -2
- package/dist/lib/logger.js.map +1 -1
- package/dist/lib/projectSlug.d.ts +12 -5
- package/dist/lib/projectSlug.d.ts.map +1 -1
- package/dist/lib/projectSlug.js +62 -23
- package/dist/lib/projectSlug.js.map +1 -1
- package/dist/lib/recurrence.d.ts +16 -0
- package/dist/lib/recurrence.d.ts.map +1 -0
- package/dist/lib/recurrence.js +293 -0
- package/dist/lib/recurrence.js.map +1 -0
- package/dist/lib/scope.d.ts +36 -1
- package/dist/lib/scope.d.ts.map +1 -1
- package/dist/lib/scope.js +183 -19
- package/dist/lib/scope.js.map +1 -1
- package/dist/lib/signalTags.d.ts +39 -0
- package/dist/lib/signalTags.d.ts.map +1 -0
- package/dist/lib/signalTags.js +203 -0
- package/dist/lib/signalTags.js.map +1 -0
- package/dist/lib/spool.d.ts +2 -0
- package/dist/lib/spool.d.ts.map +1 -1
- package/dist/lib/spool.js +176 -65
- package/dist/lib/spool.js.map +1 -1
- package/dist/lib/storeDiagnostics.d.ts +41 -0
- package/dist/lib/storeDiagnostics.d.ts.map +1 -0
- package/dist/lib/storeDiagnostics.js +148 -0
- package/dist/lib/storeDiagnostics.js.map +1 -0
- package/dist/lib/temporal.d.ts +44 -0
- package/dist/lib/temporal.d.ts.map +1 -0
- package/dist/lib/temporal.js +1063 -0
- package/dist/lib/temporal.js.map +1 -0
- package/dist/mcp/server.d.ts +93 -3
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +499 -68
- package/dist/mcp/server.js.map +1 -1
- package/dist/memory/actionStore.d.ts +94 -3
- package/dist/memory/actionStore.d.ts.map +1 -1
- package/dist/memory/actionStore.js +412 -124
- package/dist/memory/actionStore.js.map +1 -1
- package/dist/memory/deadLetterStore.d.ts.map +1 -1
- package/dist/memory/deadLetterStore.js +4 -2
- package/dist/memory/deadLetterStore.js.map +1 -1
- package/dist/memory/embedQueueStore.d.ts +19 -8
- package/dist/memory/embedQueueStore.d.ts.map +1 -1
- package/dist/memory/embedQueueStore.js +55 -25
- package/dist/memory/embedQueueStore.js.map +1 -1
- package/dist/memory/failurePatternStore.d.ts +17 -1
- package/dist/memory/failurePatternStore.d.ts.map +1 -1
- package/dist/memory/failurePatternStore.js +54 -31
- package/dist/memory/failurePatternStore.js.map +1 -1
- package/dist/memory/graphStore.d.ts +133 -3
- package/dist/memory/graphStore.d.ts.map +1 -1
- package/dist/memory/graphStore.js +980 -155
- package/dist/memory/graphStore.js.map +1 -1
- package/dist/memory/noteStore.d.ts +27 -2
- package/dist/memory/noteStore.d.ts.map +1 -1
- package/dist/memory/noteStore.js +89 -26
- package/dist/memory/noteStore.js.map +1 -1
- package/dist/memory/observationStore.d.ts +11 -1
- package/dist/memory/observationStore.d.ts.map +1 -1
- package/dist/memory/observationStore.js +90 -15
- package/dist/memory/observationStore.js.map +1 -1
- package/dist/memory/projectRename.d.ts.map +1 -1
- package/dist/memory/projectRename.js +5 -1
- package/dist/memory/projectRename.js.map +1 -1
- package/dist/memory/rollupSweeper.d.ts.map +1 -1
- package/dist/memory/rollupSweeper.js +43 -23
- package/dist/memory/rollupSweeper.js.map +1 -1
- package/dist/memory/sessionStore.d.ts +25 -7
- package/dist/memory/sessionStore.d.ts.map +1 -1
- package/dist/memory/sessionStore.js +145 -33
- package/dist/memory/sessionStore.js.map +1 -1
- package/dist/memory/trajectoryStore.d.ts +8 -0
- package/dist/memory/trajectoryStore.d.ts.map +1 -1
- package/dist/memory/trajectoryStore.js +196 -130
- package/dist/memory/trajectoryStore.js.map +1 -1
- package/dist/middleware/rateLimiter.d.ts.map +1 -1
- package/dist/middleware/rateLimiter.js +24 -1
- package/dist/middleware/rateLimiter.js.map +1 -1
- package/dist/routes/dashboard.d.ts.map +1 -1
- package/dist/routes/dashboard.js +181 -8
- package/dist/routes/dashboard.js.map +1 -1
- package/dist/routes/mcp.d.ts.map +1 -1
- package/dist/routes/mcp.js +173 -21
- package/dist/routes/mcp.js.map +1 -1
- package/dist/routes/memories.js +9 -1
- package/dist/routes/memories.js.map +1 -1
- package/dist/scripts/doctor.d.ts +26 -2
- package/dist/scripts/doctor.d.ts.map +1 -1
- package/dist/scripts/doctor.js +371 -259
- package/dist/scripts/doctor.js.map +1 -1
- package/dist/scripts/installAgent.d.ts.map +1 -1
- package/dist/scripts/installAgent.js +101 -19
- package/dist/scripts/installAgent.js.map +1 -1
- package/dist/scripts/migrateAgentMemory.js +2 -2
- package/dist/scripts/smokeTest.js +1 -1
- package/dist/scripts/smokeTest.js.map +1 -1
- package/docker-compose.yml +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +3 -2
- package/plugin/hooks/postToolUse.js +155 -50
- package/plugin/hooks/sessionStart.js +179 -27
- package/plugin/hooks/sessionStop.js +152 -37
- package/plugin/plugin.json +1 -1
- package/plugin/rules/AGENTS.md +10 -8
- package/plugin/skills/brain-memory/REFERENCE.md +15 -14
- package/plugin/skills/brain-memory/SKILL.md +2 -2
- package/plugin/skills/digest/SKILL.md +1 -1
- package/plugin/skills/handoff/SKILL.md +1 -1
- package/plugin/skills/save/SKILL.md +2 -2
- package/plugin/skills/search/SKILL.md +2 -2
- package/src/ai/chat.ts +1 -2
- package/src/ai/config.ts +2 -2
- package/src/app.ts +475 -54
- package/src/cli.ts +500 -38
- package/src/consolidation/candidates.ts +22 -4
- package/src/consolidation/fixtures.ts +1 -1
- package/src/consolidation/gate.ts +177 -140
- package/src/consolidation/runner.ts +26 -2
- package/src/dashboard/clientScript.ts +3774 -905
- package/src/dashboard/html.ts +605 -101
- package/src/dashboard/styles.ts +1591 -359
- package/src/learning/embedWorker.ts +350 -70
- package/src/learning/embedding.ts +141 -28
- package/src/learning/engine.ts +1 -1
- package/src/learning/search.ts +2533 -397
- package/src/learning/storeMeta.ts +36 -29
- package/src/learning/types.ts +66 -8
- package/src/lib/condenser.ts +1515 -0
- package/src/lib/db.ts +690 -171
- package/src/lib/logger.ts +4 -2
- package/src/lib/projectSlug.ts +77 -23
- package/src/lib/recurrence.ts +382 -0
- package/src/lib/scope.ts +220 -22
- package/src/lib/signalTags.ts +257 -0
- package/src/lib/spool.ts +181 -71
- package/src/lib/storeDiagnostics.ts +181 -0
- package/src/lib/temporal.ts +1250 -0
- package/src/mcp/server.ts +559 -88
- package/src/memory/actionStore.ts +642 -171
- package/src/memory/deadLetterStore.ts +4 -2
- package/src/memory/embedQueueStore.ts +63 -27
- package/src/memory/failurePatternStore.ts +84 -31
- package/src/memory/graphStore.ts +1396 -215
- package/src/memory/noteStore.ts +151 -30
- package/src/memory/observationStore.ts +117 -17
- package/src/memory/projectRename.ts +6 -2
- package/src/memory/rollupSweeper.ts +46 -23
- package/src/memory/sessionStore.ts +197 -38
- package/src/memory/trajectoryStore.ts +260 -140
- package/src/middleware/rateLimiter.ts +24 -1
- package/src/routes/dashboard.ts +194 -9
- package/src/routes/mcp.ts +185 -26
- package/src/routes/memories.ts +8 -1
- package/src/scripts/doctor.ts +461 -262
- package/src/scripts/installAgent.ts +106 -19
- package/src/scripts/migrateAgentMemory.ts +2 -2
- package/src/scripts/smokeTest.ts +1 -1
package/dist/lib/db.js
CHANGED
|
@@ -8,6 +8,7 @@ exports.isDatabaseUnavailableError = isDatabaseUnavailableError;
|
|
|
8
8
|
exports.getDbPath = getDbPath;
|
|
9
9
|
exports.getDb = getDb;
|
|
10
10
|
exports.resetDb = resetDb;
|
|
11
|
+
exports.withIsolatedDb = withIsolatedDb;
|
|
11
12
|
exports.initDb = initDb;
|
|
12
13
|
const fs_1 = __importDefault(require("fs"));
|
|
13
14
|
const os_1 = __importDefault(require("os"));
|
|
@@ -101,10 +102,188 @@ function resetDb() {
|
|
|
101
102
|
_db = null;
|
|
102
103
|
}
|
|
103
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Runs `fn` with `getDb()` pointed at a fresh, migrated database in a new temp
|
|
107
|
+
* directory, then restores the previous connection (open or not) and
|
|
108
|
+
* `DATABASE_URL`, and deletes the temp directory.
|
|
109
|
+
*
|
|
110
|
+
* This is how code outside the service exercises store logic that writes
|
|
111
|
+
* (the doctor's AI conformance fixtures) without becoming a second writer on
|
|
112
|
+
* the real database file.
|
|
113
|
+
*/
|
|
114
|
+
function withIsolatedDb(fn) {
|
|
115
|
+
const previousDb = _db;
|
|
116
|
+
const hadUrl = Object.prototype.hasOwnProperty.call(process.env, 'DATABASE_URL');
|
|
117
|
+
const previousUrl = process.env.DATABASE_URL;
|
|
118
|
+
const dir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'brain-isolated-'));
|
|
119
|
+
_db = null;
|
|
120
|
+
process.env.DATABASE_URL = path_1.default.join(dir, 'isolated.db');
|
|
121
|
+
try {
|
|
122
|
+
return fn();
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
// fn() may have opened the throwaway connection; TS cannot see that.
|
|
126
|
+
const opened = _db;
|
|
127
|
+
if (opened) {
|
|
128
|
+
try {
|
|
129
|
+
opened.close();
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
// ignore close errors on the throwaway connection
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
_db = previousDb;
|
|
136
|
+
if (hadUrl)
|
|
137
|
+
process.env.DATABASE_URL = previousUrl;
|
|
138
|
+
else
|
|
139
|
+
delete process.env.DATABASE_URL;
|
|
140
|
+
fs_1.default.rmSync(dir, { recursive: true, force: true });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
104
143
|
function initDb() {
|
|
105
144
|
getDb();
|
|
106
145
|
logger_1.logger.info('database initialized');
|
|
107
146
|
}
|
|
147
|
+
function tableColumns(db, table) {
|
|
148
|
+
return db.prepare(`PRAGMA table_info(${table})`).all();
|
|
149
|
+
}
|
|
150
|
+
function ensureColumns(db, table, columns) {
|
|
151
|
+
const existing = new Set(tableColumns(db, table).map((c) => c.name));
|
|
152
|
+
for (const col of columns) {
|
|
153
|
+
if (existing.has(col.name))
|
|
154
|
+
continue;
|
|
155
|
+
db.exec(`ALTER TABLE ${table} ADD COLUMN ${col.name} ${col.ddl}`);
|
|
156
|
+
if (col.backfill)
|
|
157
|
+
db.exec(col.backfill);
|
|
158
|
+
logger_1.logger.info({ table, column: col.name }, `${table} schema: column added`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function rebuildTable(db, table, legacyName, createDdl, indexDdl) {
|
|
162
|
+
db.exec(`DROP TABLE IF EXISTS ${legacyName}`);
|
|
163
|
+
(0, sqlite_1.pragma)(db, 'foreign_keys = OFF');
|
|
164
|
+
try {
|
|
165
|
+
(0, sqlite_1.transaction)(db, () => {
|
|
166
|
+
db.exec(`ALTER TABLE ${table} RENAME TO ${legacyName}`);
|
|
167
|
+
db.exec(createDdl);
|
|
168
|
+
const oldCols = new Set(tableColumns(db, legacyName).map((c) => c.name));
|
|
169
|
+
const shared = tableColumns(db, table)
|
|
170
|
+
.map((c) => c.name)
|
|
171
|
+
.filter((name) => oldCols.has(name));
|
|
172
|
+
const list = shared.join(', ');
|
|
173
|
+
db.exec(`INSERT INTO ${table} (${list}) SELECT ${list} FROM ${legacyName}`);
|
|
174
|
+
db.exec(`DROP TABLE ${legacyName}`);
|
|
175
|
+
if (indexDdl)
|
|
176
|
+
db.exec(indexDdl);
|
|
177
|
+
})();
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
(0, sqlite_1.pragma)(db, 'foreign_keys = ON');
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const OBSERVATIONS_DDL = `
|
|
184
|
+
CREATE TABLE IF NOT EXISTS observations (
|
|
185
|
+
id TEXT PRIMARY KEY,
|
|
186
|
+
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
187
|
+
project_id TEXT,
|
|
188
|
+
user_id TEXT,
|
|
189
|
+
agent_id TEXT,
|
|
190
|
+
note_ref TEXT, -- -> notes.id, optional
|
|
191
|
+
task_ref TEXT, -- provenance only, not a scope filter
|
|
192
|
+
session_id TEXT,
|
|
193
|
+
agent TEXT,
|
|
194
|
+
kind TEXT NOT NULL CHECK (kind IN ('tool_call','prompt','response')),
|
|
195
|
+
tool_name TEXT,
|
|
196
|
+
file_path TEXT, -- the tool's TARGET path, nothing else
|
|
197
|
+
input TEXT,
|
|
198
|
+
output TEXT,
|
|
199
|
+
condensed_text TEXT,
|
|
200
|
+
resolved_dates_json TEXT,
|
|
201
|
+
signal_tags TEXT,
|
|
202
|
+
duration_ms INTEGER NOT NULL DEFAULT 0,
|
|
203
|
+
failed INTEGER NOT NULL DEFAULT 0, -- set by the failure hook, never inferred
|
|
204
|
+
event_at TEXT, -- optional UTC ISO-8601 when event occurred
|
|
205
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
206
|
+
archived_at INTEGER,
|
|
207
|
+
CHECK ((scope = 'project') = (project_id IS NOT NULL))
|
|
208
|
+
);
|
|
209
|
+
`;
|
|
210
|
+
const OBSERVATIONS_INDEX_DDL = `
|
|
211
|
+
CREATE INDEX IF NOT EXISTS idx_obs_project ON observations(project_id);
|
|
212
|
+
CREATE INDEX IF NOT EXISTS idx_obs_scope ON observations(scope);
|
|
213
|
+
CREATE INDEX IF NOT EXISTS idx_obs_scope_proj ON observations(scope, project_id, created_at);
|
|
214
|
+
CREATE INDEX IF NOT EXISTS idx_obs_event_at ON observations(scope, project_id, event_at);
|
|
215
|
+
CREATE INDEX IF NOT EXISTS idx_obs_session ON observations(session_id);
|
|
216
|
+
CREATE INDEX IF NOT EXISTS idx_obs_created ON observations(created_at);
|
|
217
|
+
CREATE INDEX IF NOT EXISTS idx_obs_note ON observations(note_ref);
|
|
218
|
+
CREATE INDEX IF NOT EXISTS idx_obs_task ON observations(task_ref);
|
|
219
|
+
CREATE INDEX IF NOT EXISTS idx_obs_file ON observations(file_path);
|
|
220
|
+
CREATE INDEX IF NOT EXISTS idx_obs_failed ON observations(failed, created_at);
|
|
221
|
+
CREATE INDEX IF NOT EXISTS idx_observations_archived ON observations(archived_at);
|
|
222
|
+
`;
|
|
223
|
+
const OBSERVATIONS_ARCHIVE_DDL = `
|
|
224
|
+
CREATE TABLE IF NOT EXISTS observations_archive (
|
|
225
|
+
id TEXT PRIMARY KEY,
|
|
226
|
+
original_observation_id TEXT NOT NULL UNIQUE,
|
|
227
|
+
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
228
|
+
project_id TEXT,
|
|
229
|
+
user_id TEXT,
|
|
230
|
+
agent_id TEXT,
|
|
231
|
+
note_ref TEXT,
|
|
232
|
+
task_ref TEXT,
|
|
233
|
+
session_id TEXT,
|
|
234
|
+
agent TEXT,
|
|
235
|
+
kind TEXT NOT NULL CHECK (kind IN ('tool_call','prompt','response')),
|
|
236
|
+
tool_name TEXT,
|
|
237
|
+
file_path TEXT,
|
|
238
|
+
input TEXT,
|
|
239
|
+
output TEXT,
|
|
240
|
+
condensed_text TEXT,
|
|
241
|
+
resolved_dates_json TEXT,
|
|
242
|
+
signal_tags TEXT,
|
|
243
|
+
duration_ms INTEGER NOT NULL DEFAULT 0,
|
|
244
|
+
failed INTEGER NOT NULL DEFAULT 0,
|
|
245
|
+
event_at TEXT,
|
|
246
|
+
created_at INTEGER NOT NULL,
|
|
247
|
+
archived_at INTEGER NOT NULL,
|
|
248
|
+
CHECK ((scope = 'project') = (project_id IS NOT NULL))
|
|
249
|
+
);
|
|
250
|
+
`;
|
|
251
|
+
const OBSERVATIONS_ARCHIVE_INDEX_DDL = `
|
|
252
|
+
CREATE INDEX IF NOT EXISTS idx_obs_arc_project ON observations_archive(project_id);
|
|
253
|
+
CREATE INDEX IF NOT EXISTS idx_obs_arc_scope ON observations_archive(scope);
|
|
254
|
+
CREATE INDEX IF NOT EXISTS idx_obs_arc_scope_proj ON observations_archive(scope, project_id);
|
|
255
|
+
CREATE INDEX IF NOT EXISTS idx_obs_arc_archived ON observations_archive(archived_at);
|
|
256
|
+
`;
|
|
257
|
+
function alignObservationsSchema(db) {
|
|
258
|
+
const targets = [
|
|
259
|
+
{ table: 'observations', ddl: OBSERVATIONS_DDL, indexDdl: OBSERVATIONS_INDEX_DDL },
|
|
260
|
+
{
|
|
261
|
+
table: 'observations_archive',
|
|
262
|
+
ddl: OBSERVATIONS_ARCHIVE_DDL,
|
|
263
|
+
indexDdl: OBSERVATIONS_ARCHIVE_INDEX_DDL,
|
|
264
|
+
},
|
|
265
|
+
];
|
|
266
|
+
for (const { table, ddl, indexDdl } of targets) {
|
|
267
|
+
ensureColumns(db, table, [
|
|
268
|
+
{ name: 'user_id', ddl: 'TEXT' },
|
|
269
|
+
{ name: 'agent_id', ddl: 'TEXT' },
|
|
270
|
+
{ name: 'event_at', ddl: 'TEXT' },
|
|
271
|
+
{ name: 'condensed_text', ddl: 'TEXT' },
|
|
272
|
+
{ name: 'resolved_dates_json', ddl: 'TEXT' },
|
|
273
|
+
{ name: 'signal_tags', ddl: 'TEXT' },
|
|
274
|
+
]);
|
|
275
|
+
const row = db
|
|
276
|
+
.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?")
|
|
277
|
+
.get(table);
|
|
278
|
+
if (row?.sql && !row.sql.includes("'response'")) {
|
|
279
|
+
rebuildTable(db, table, `${table}_legacy_kind`, ddl, indexDdl);
|
|
280
|
+
logger_1.logger.info({ table }, `${table} schema: kind CHECK updated to include 'response'`);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
db.exec(indexDdl);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
108
287
|
/**
|
|
109
288
|
* Trajectory DDL lives in constants because two things need it: the normal
|
|
110
289
|
* `CREATE TABLE IF NOT EXISTS` pass, and `alignTrajectorySchema` below, which
|
|
@@ -127,6 +306,8 @@ const TRAJECTORIES_DDL = `
|
|
|
127
306
|
id TEXT PRIMARY KEY,
|
|
128
307
|
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
129
308
|
project_id TEXT,
|
|
309
|
+
user_id TEXT,
|
|
310
|
+
agent_id TEXT,
|
|
130
311
|
note_ref TEXT,
|
|
131
312
|
task_ref TEXT,
|
|
132
313
|
session_id TEXT,
|
|
@@ -146,14 +327,15 @@ const TRAJECTORIES_DDL = `
|
|
|
146
327
|
);
|
|
147
328
|
`;
|
|
148
329
|
const TRAJECTORIES_INDEX_DDL = `
|
|
149
|
-
CREATE INDEX IF NOT EXISTS idx_traj_project
|
|
150
|
-
CREATE INDEX IF NOT EXISTS idx_traj_scope
|
|
151
|
-
CREATE INDEX IF NOT EXISTS
|
|
152
|
-
CREATE INDEX IF NOT EXISTS
|
|
153
|
-
CREATE INDEX IF NOT EXISTS
|
|
154
|
-
CREATE INDEX IF NOT EXISTS
|
|
155
|
-
CREATE INDEX IF NOT EXISTS
|
|
156
|
-
CREATE INDEX IF NOT EXISTS
|
|
330
|
+
CREATE INDEX IF NOT EXISTS idx_traj_project ON trajectories(project_id);
|
|
331
|
+
CREATE INDEX IF NOT EXISTS idx_traj_scope ON trajectories(scope);
|
|
332
|
+
CREATE INDEX IF NOT EXISTS idx_traj_scope_proj ON trajectories(scope, project_id, created_at);
|
|
333
|
+
CREATE INDEX IF NOT EXISTS idx_traj_type ON trajectories(task_type);
|
|
334
|
+
CREATE INDEX IF NOT EXISTS idx_traj_created ON trajectories(created_at);
|
|
335
|
+
CREATE INDEX IF NOT EXISTS idx_traj_success ON trajectories(success, created_at);
|
|
336
|
+
CREATE INDEX IF NOT EXISTS idx_traj_note ON trajectories(note_ref);
|
|
337
|
+
CREATE INDEX IF NOT EXISTS idx_traj_session ON trajectories(session_id);
|
|
338
|
+
CREATE INDEX IF NOT EXISTS idx_traj_rollup ON trajectories(rollup_source);
|
|
157
339
|
`;
|
|
158
340
|
/**
|
|
159
341
|
* The archive mirrors the live table, including the nullability. A
|
|
@@ -168,6 +350,8 @@ const TRAJECTORY_ARCHIVE_DDL = `
|
|
|
168
350
|
original_trajectory_id TEXT NOT NULL UNIQUE,
|
|
169
351
|
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
170
352
|
project_id TEXT,
|
|
353
|
+
user_id TEXT,
|
|
354
|
+
agent_id TEXT,
|
|
171
355
|
note_ref TEXT,
|
|
172
356
|
task_ref TEXT,
|
|
173
357
|
session_id TEXT,
|
|
@@ -187,14 +371,12 @@ const TRAJECTORY_ARCHIVE_DDL = `
|
|
|
187
371
|
);
|
|
188
372
|
`;
|
|
189
373
|
const TRAJECTORY_ARCHIVE_INDEX_DDL = `
|
|
190
|
-
CREATE INDEX IF NOT EXISTS idx_traj_arc_project
|
|
191
|
-
CREATE INDEX IF NOT EXISTS idx_traj_arc_scope
|
|
192
|
-
CREATE INDEX IF NOT EXISTS
|
|
193
|
-
CREATE INDEX IF NOT EXISTS
|
|
374
|
+
CREATE INDEX IF NOT EXISTS idx_traj_arc_project ON trajectory_archive(project_id);
|
|
375
|
+
CREATE INDEX IF NOT EXISTS idx_traj_arc_scope ON trajectory_archive(scope);
|
|
376
|
+
CREATE INDEX IF NOT EXISTS idx_traj_arc_scope_proj ON trajectory_archive(scope, project_id);
|
|
377
|
+
CREATE INDEX IF NOT EXISTS idx_traj_arc_archived ON trajectory_archive(archived_at);
|
|
378
|
+
CREATE INDEX IF NOT EXISTS idx_traj_arc_session ON trajectory_archive(session_id);
|
|
194
379
|
`;
|
|
195
|
-
function tableColumns(db, table) {
|
|
196
|
-
return db.prepare(`PRAGMA table_info(${table})`).all();
|
|
197
|
-
}
|
|
198
380
|
/**
|
|
199
381
|
* Bring an already-created trajectory table up to the DDL above.
|
|
200
382
|
*
|
|
@@ -207,7 +389,7 @@ function tableColumns(db, table) {
|
|
|
207
389
|
function alignTrajectorySchema(db, table, createDdl, indexDdl) {
|
|
208
390
|
// 1. Additive columns. Backfill only when the column has just appeared:
|
|
209
391
|
// every pre-existing row is, by definition, an explicit rollup.
|
|
210
|
-
|
|
392
|
+
ensureColumns(db, table, [
|
|
211
393
|
{ name: 'session_id', ddl: 'TEXT' },
|
|
212
394
|
{
|
|
213
395
|
name: 'rollup_source',
|
|
@@ -215,16 +397,9 @@ function alignTrajectorySchema(db, table, createDdl, indexDdl) {
|
|
|
215
397
|
backfill: `UPDATE ${table} SET rollup_source = 'signalled' WHERE rollup_source IS NULL`,
|
|
216
398
|
},
|
|
217
399
|
{ name: 'tool_calls', ddl: 'INTEGER NOT NULL DEFAULT 0' },
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (existing.has(col.name))
|
|
222
|
-
continue;
|
|
223
|
-
db.exec(`ALTER TABLE ${table} ADD COLUMN ${col.name} ${col.ddl}`);
|
|
224
|
-
if (col.backfill)
|
|
225
|
-
db.exec(col.backfill);
|
|
226
|
-
logger_1.logger.info({ table, column: col.name }, 'trajectory schema: column added');
|
|
227
|
-
}
|
|
400
|
+
{ name: 'user_id', ddl: 'TEXT' },
|
|
401
|
+
{ name: 'agent_id', ddl: 'TEXT' },
|
|
402
|
+
]);
|
|
228
403
|
// 2. Relax success to nullable. SQLite has no "ALTER COLUMN", so this is the
|
|
229
404
|
// documented rebuild: rename, recreate, copy, drop, re-index.
|
|
230
405
|
const success = tableColumns(db, table).find((c) => c.name === 'success');
|
|
@@ -232,33 +407,16 @@ function alignTrajectorySchema(db, table, createDdl, indexDdl) {
|
|
|
232
407
|
db.exec(indexDdl);
|
|
233
408
|
return;
|
|
234
409
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
(0, sqlite_1.pragma)(db, 'foreign_keys = OFF');
|
|
238
|
-
try {
|
|
239
|
-
(0, sqlite_1.transaction)(db, () => {
|
|
240
|
-
db.exec(`ALTER TABLE ${table} RENAME TO ${legacy}`);
|
|
241
|
-
db.exec(createDdl);
|
|
242
|
-
const oldCols = new Set(tableColumns(db, legacy).map((c) => c.name));
|
|
243
|
-
const shared = tableColumns(db, table)
|
|
244
|
-
.map((c) => c.name)
|
|
245
|
-
.filter((name) => oldCols.has(name));
|
|
246
|
-
const list = shared.join(', ');
|
|
247
|
-
db.exec(`INSERT INTO ${table} (${list}) SELECT ${list} FROM ${legacy}`);
|
|
248
|
-
db.exec(`DROP TABLE ${legacy}`);
|
|
249
|
-
db.exec(indexDdl);
|
|
250
|
-
})();
|
|
251
|
-
logger_1.logger.info({ table }, 'trajectory schema: success relaxed to nullable (NULL = unknown)');
|
|
252
|
-
}
|
|
253
|
-
finally {
|
|
254
|
-
(0, sqlite_1.pragma)(db, 'foreign_keys = ON');
|
|
255
|
-
}
|
|
410
|
+
rebuildTable(db, table, `${table}_legacy_notnull`, createDdl, indexDdl);
|
|
411
|
+
logger_1.logger.info({ table }, 'trajectory schema: success relaxed to nullable (NULL = unknown)');
|
|
256
412
|
}
|
|
257
413
|
const NOTES_DDL = `
|
|
258
414
|
CREATE TABLE IF NOT EXISTS notes (
|
|
259
415
|
id TEXT PRIMARY KEY,
|
|
260
416
|
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
261
417
|
project_id TEXT,
|
|
418
|
+
user_id TEXT,
|
|
419
|
+
agent_id TEXT,
|
|
262
420
|
session_id TEXT,
|
|
263
421
|
type TEXT NOT NULL CHECK (type IN
|
|
264
422
|
('fact','architecture','pattern','workflow','bug','preference','lesson')),
|
|
@@ -270,58 +428,395 @@ const NOTES_DDL = `
|
|
|
270
428
|
supersedes TEXT REFERENCES notes(id),
|
|
271
429
|
is_latest INTEGER NOT NULL DEFAULT 1,
|
|
272
430
|
embedding BLOB,
|
|
431
|
+
event_at TEXT,
|
|
432
|
+
valid_from TEXT,
|
|
433
|
+
valid_until TEXT,
|
|
273
434
|
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
274
435
|
updated_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
275
436
|
CHECK ((scope = 'project') = (project_id IS NOT NULL))
|
|
276
437
|
);
|
|
277
438
|
`;
|
|
278
439
|
const NOTES_INDEX_DDL = `
|
|
279
|
-
CREATE INDEX IF NOT EXISTS idx_notes_project
|
|
280
|
-
CREATE INDEX IF NOT EXISTS idx_notes_scope
|
|
281
|
-
CREATE INDEX IF NOT EXISTS
|
|
282
|
-
CREATE INDEX IF NOT EXISTS
|
|
283
|
-
CREATE INDEX IF NOT EXISTS
|
|
440
|
+
CREATE INDEX IF NOT EXISTS idx_notes_project ON notes(project_id);
|
|
441
|
+
CREATE INDEX IF NOT EXISTS idx_notes_scope ON notes(scope);
|
|
442
|
+
CREATE INDEX IF NOT EXISTS idx_notes_scope_proj ON notes(scope, project_id, is_latest, updated_at);
|
|
443
|
+
CREATE INDEX IF NOT EXISTS idx_notes_type ON notes(type);
|
|
444
|
+
CREATE INDEX IF NOT EXISTS idx_notes_latest ON notes(is_latest, updated_at);
|
|
445
|
+
CREATE INDEX IF NOT EXISTS idx_notes_session ON notes(session_id);
|
|
446
|
+
CREATE INDEX IF NOT EXISTS idx_notes_temporal ON notes(event_at, valid_from, valid_until);
|
|
447
|
+
CREATE INDEX IF NOT EXISTS idx_notes_user ON notes(user_id) WHERE user_id IS NOT NULL;
|
|
284
448
|
`;
|
|
285
449
|
function alignNotesSchema(db) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
450
|
+
ensureColumns(db, 'notes', [
|
|
451
|
+
{ name: 'session_id', ddl: 'TEXT' },
|
|
452
|
+
{ name: 'user_id', ddl: 'TEXT' },
|
|
453
|
+
{ name: 'agent_id', ddl: 'TEXT' },
|
|
454
|
+
{ name: 'event_at', ddl: 'TEXT' },
|
|
455
|
+
{ name: 'valid_from', ddl: 'TEXT' },
|
|
456
|
+
{ name: 'valid_until', ddl: 'TEXT' },
|
|
457
|
+
]);
|
|
291
458
|
const row = db
|
|
292
459
|
.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'notes'")
|
|
293
460
|
.get();
|
|
294
461
|
if (row?.sql && !row.sql.includes("'lesson'")) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
(0, sqlite_1.pragma)(db, 'foreign_keys = OFF');
|
|
298
|
-
try {
|
|
299
|
-
(0, sqlite_1.transaction)(db, () => {
|
|
300
|
-
db.exec(`ALTER TABLE notes RENAME TO ${legacy}`);
|
|
301
|
-
db.exec(NOTES_DDL);
|
|
302
|
-
const oldCols = new Set(tableColumns(db, legacy).map((c) => c.name));
|
|
303
|
-
const shared = tableColumns(db, 'notes')
|
|
304
|
-
.map((c) => c.name)
|
|
305
|
-
.filter((name) => oldCols.has(name));
|
|
306
|
-
const list = shared.join(', ');
|
|
307
|
-
db.exec(`INSERT INTO notes (${list}) SELECT ${list} FROM ${legacy}`);
|
|
308
|
-
db.exec(`DROP TABLE ${legacy}`);
|
|
309
|
-
})();
|
|
310
|
-
logger_1.logger.info({ table: 'notes' }, "notes schema: type CHECK updated to include 'lesson'");
|
|
311
|
-
}
|
|
312
|
-
finally {
|
|
313
|
-
(0, sqlite_1.pragma)(db, 'foreign_keys = ON');
|
|
314
|
-
}
|
|
462
|
+
rebuildTable(db, 'notes', 'notes_legacy_type', NOTES_DDL);
|
|
463
|
+
logger_1.logger.info({ table: 'notes' }, "notes schema: type CHECK updated to include 'lesson'");
|
|
315
464
|
}
|
|
316
465
|
db.exec(NOTES_INDEX_DDL);
|
|
317
466
|
}
|
|
467
|
+
function alignSessionsSchema(db) {
|
|
468
|
+
ensureColumns(db, 'sessions', [
|
|
469
|
+
{ name: 'user_id', ddl: 'TEXT' },
|
|
470
|
+
{ name: 'agent_id', ddl: 'TEXT' },
|
|
471
|
+
]);
|
|
472
|
+
db.exec(`
|
|
473
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_proj_status ON sessions(project_id, status, started_at);
|
|
474
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_scope_proj ON sessions(scope, project_id, status, started_at);
|
|
475
|
+
`);
|
|
476
|
+
}
|
|
477
|
+
function alignSummariesSchema(db) {
|
|
478
|
+
ensureColumns(db, 'summaries', [
|
|
479
|
+
{ name: 'user_id', ddl: 'TEXT' },
|
|
480
|
+
{ name: 'agent_id', ddl: 'TEXT' },
|
|
481
|
+
]);
|
|
482
|
+
db.exec(`
|
|
483
|
+
CREATE INDEX IF NOT EXISTS idx_sum_project ON summaries(project_id);
|
|
484
|
+
CREATE INDEX IF NOT EXISTS idx_sum_scope_proj ON summaries(scope, project_id, is_latest, created_at);
|
|
485
|
+
CREATE INDEX IF NOT EXISTS idx_sum_kind ON summaries(kind, created_at);
|
|
486
|
+
CREATE INDEX IF NOT EXISTS idx_sum_latest ON summaries(is_latest);
|
|
487
|
+
CREATE INDEX IF NOT EXISTS idx_sum_producer ON summaries(producer_tier, producer_model);
|
|
488
|
+
`);
|
|
489
|
+
}
|
|
318
490
|
function alignFailurePatternsSchema(db) {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
491
|
+
ensureColumns(db, 'failure_patterns', [
|
|
492
|
+
{ name: 'session_id', ddl: 'TEXT' },
|
|
493
|
+
{ name: 'user_id', ddl: 'TEXT' },
|
|
494
|
+
{ name: 'agent_id', ddl: 'TEXT' },
|
|
495
|
+
]);
|
|
496
|
+
db.exec(`
|
|
497
|
+
CREATE INDEX IF NOT EXISTS idx_fp_lookup ON failure_patterns(project_id, task_type, count DESC);
|
|
498
|
+
CREATE INDEX IF NOT EXISTS idx_fp_scope_proj ON failure_patterns(scope, project_id, task_type, count DESC);
|
|
499
|
+
CREATE INDEX IF NOT EXISTS idx_fp_recent ON failure_patterns(last_seen_at);
|
|
500
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_fp_unique_coalesce ON failure_patterns(COALESCE(project_id, '__GLOBAL_OR_UNKNOWN__:' || scope), task_type, error_text);
|
|
501
|
+
`);
|
|
502
|
+
}
|
|
503
|
+
function alignActionsSchema(db) {
|
|
504
|
+
ensureColumns(db, 'actions', [
|
|
505
|
+
{ name: 'user_id', ddl: 'TEXT' },
|
|
506
|
+
{ name: 'agent_id', ddl: 'TEXT' },
|
|
507
|
+
{ name: 'due_at', ddl: 'TEXT' },
|
|
508
|
+
{ name: 'remind_at', ddl: 'TEXT' },
|
|
509
|
+
{ name: 'recurrence', ddl: 'TEXT' },
|
|
510
|
+
{ name: 'last_reminded_at', ddl: 'TEXT' },
|
|
511
|
+
{ name: 'snooze_until', ddl: 'TEXT' },
|
|
512
|
+
{ name: 'parent_action_id', ddl: 'TEXT' },
|
|
513
|
+
{ name: 'embedding', ddl: 'BLOB' },
|
|
514
|
+
]);
|
|
515
|
+
db.exec(`
|
|
516
|
+
CREATE INDEX IF NOT EXISTS idx_actions_proj_status ON actions(project_id, status, priority);
|
|
517
|
+
CREATE INDEX IF NOT EXISTS idx_actions_scope_proj ON actions(scope, project_id, status, priority);
|
|
518
|
+
CREATE INDEX IF NOT EXISTS idx_actions_due ON actions(status, due_at) WHERE status IN ('ready','in_progress') AND due_at IS NOT NULL;
|
|
519
|
+
CREATE INDEX IF NOT EXISTS idx_actions_remind ON actions(status, remind_at) WHERE status IN ('ready','in_progress') AND remind_at IS NOT NULL;
|
|
520
|
+
`);
|
|
521
|
+
}
|
|
522
|
+
const EMBED_QUEUE_DDL = `
|
|
523
|
+
CREATE TABLE IF NOT EXISTS embed_queue (
|
|
524
|
+
record_id TEXT NOT NULL,
|
|
525
|
+
record_type TEXT NOT NULL CHECK (record_type IN ('observation','trajectory','note','summary','entity','action')),
|
|
526
|
+
status TEXT NOT NULL DEFAULT 'pending'
|
|
527
|
+
CHECK (status IN ('pending','running','done','failed')),
|
|
528
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
529
|
+
last_error TEXT,
|
|
530
|
+
target TEXT NOT NULL DEFAULT 'live', -- 'live' or a re-embed collection name
|
|
531
|
+
queued_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
532
|
+
PRIMARY KEY (record_id, record_type, target)
|
|
533
|
+
);
|
|
534
|
+
`;
|
|
535
|
+
const EMBED_QUEUE_INDEX_DDL = `
|
|
536
|
+
CREATE INDEX IF NOT EXISTS idx_eq_claim ON embed_queue(target, status, queued_at);
|
|
537
|
+
`;
|
|
538
|
+
function alignEmbedQueueSchema(db) {
|
|
539
|
+
const row = db
|
|
540
|
+
.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'embed_queue'")
|
|
541
|
+
.get();
|
|
542
|
+
if (row?.sql && !row.sql.includes("'entity'")) {
|
|
543
|
+
rebuildTable(db, 'embed_queue', 'embed_queue_legacy_types', EMBED_QUEUE_DDL, EMBED_QUEUE_INDEX_DDL);
|
|
544
|
+
logger_1.logger.info({ table: 'embed_queue' }, "embed_queue schema: record_type CHECK updated to include 'entity' and 'action'");
|
|
545
|
+
}
|
|
546
|
+
else {
|
|
547
|
+
db.exec(EMBED_QUEUE_INDEX_DDL);
|
|
323
548
|
}
|
|
324
549
|
}
|
|
550
|
+
const NOTES_EXTRA_EXPR = `
|
|
551
|
+
TRIM(
|
|
552
|
+
COALESCE((SELECT GROUP_CONCAT(concept, ' ') FROM note_concepts WHERE note_id = n.id), '') ||
|
|
553
|
+
' ' ||
|
|
554
|
+
COALESCE((SELECT GROUP_CONCAT(file_path, ' ') FROM note_files WHERE note_id = n.id), '')
|
|
555
|
+
)
|
|
556
|
+
`;
|
|
557
|
+
const SUMMARIES_TEXT_EXPR = `
|
|
558
|
+
COALESCE((SELECT GROUP_CONCAT(text, char(10)) FROM (SELECT text FROM summary_claims WHERE summary_id = s.id ORDER BY ordinal ASC)), '')
|
|
559
|
+
`;
|
|
560
|
+
/**
|
|
561
|
+
* Create FTS5 virtual tables, sync triggers, and run one-time backfill on
|
|
562
|
+
* existing databases. Must run AFTER all table rebuilds so triggers are never
|
|
563
|
+
* dropped by ALTER/DROP TABLE operations.
|
|
564
|
+
*/
|
|
565
|
+
function alignFtsSchema(db) {
|
|
566
|
+
const obsFtsRow = db
|
|
567
|
+
.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'fts_observations'")
|
|
568
|
+
.get();
|
|
569
|
+
if (obsFtsRow?.sql && !obsFtsRow.sql.includes('condensed_text')) {
|
|
570
|
+
db.exec(`
|
|
571
|
+
DROP TRIGGER IF EXISTS trg_fts_observations_ai;
|
|
572
|
+
DROP TRIGGER IF EXISTS trg_fts_observations_au;
|
|
573
|
+
DROP TRIGGER IF EXISTS trg_fts_observations_ad;
|
|
574
|
+
DROP TABLE IF EXISTS fts_observations;
|
|
575
|
+
`);
|
|
576
|
+
logger_1.logger.info({ table: 'fts_observations' }, 'fts_observations schema: upgraded to include condensed_text');
|
|
577
|
+
}
|
|
578
|
+
db.exec(`
|
|
579
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS fts_notes USING fts5(
|
|
580
|
+
record_id UNINDEXED, title, content, extra, tokenize='porter unicode61'
|
|
581
|
+
);
|
|
582
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS fts_trajectories USING fts5(
|
|
583
|
+
record_id UNINDEXED, description, result, task_type, tokenize='porter unicode61'
|
|
584
|
+
);
|
|
585
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS fts_observations USING fts5(
|
|
586
|
+
record_id UNINDEXED, tool_name, file_path, input, output, condensed_text, tokenize='porter unicode61'
|
|
587
|
+
);
|
|
588
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS fts_summaries USING fts5(
|
|
589
|
+
record_id UNINDEXED, text, tokenize='porter unicode61'
|
|
590
|
+
);
|
|
591
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS fts_entities USING fts5(
|
|
592
|
+
record_id UNINDEXED, name, entity_type, description, external_ref, tokenize='porter unicode61'
|
|
593
|
+
);
|
|
594
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS fts_actions USING fts5(
|
|
595
|
+
record_id UNINDEXED, title, description, blocked_reason, tokenize='porter unicode61'
|
|
596
|
+
);
|
|
597
|
+
|
|
598
|
+
-- Drop legacy unscoped AFTER UPDATE triggers (and observations AI trigger) so existing
|
|
599
|
+
-- databases upgrade cleanly to column-scoped 6-column triggers.
|
|
600
|
+
DROP TRIGGER IF EXISTS trg_fts_notes_au;
|
|
601
|
+
DROP TRIGGER IF EXISTS trg_fts_trajectories_au;
|
|
602
|
+
DROP TRIGGER IF EXISTS trg_fts_observations_ai;
|
|
603
|
+
DROP TRIGGER IF EXISTS trg_fts_observations_au;
|
|
604
|
+
DROP TRIGGER IF EXISTS trg_fts_summaries_au;
|
|
605
|
+
DROP TRIGGER IF EXISTS trg_fts_summary_claims_au;
|
|
606
|
+
DROP TRIGGER IF EXISTS trg_fts_entities_au;
|
|
607
|
+
DROP TRIGGER IF EXISTS trg_fts_actions_au;
|
|
608
|
+
|
|
609
|
+
-- notes triggers
|
|
610
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_notes_ai AFTER INSERT ON notes BEGIN
|
|
611
|
+
DELETE FROM fts_notes WHERE record_id = new.id;
|
|
612
|
+
INSERT INTO fts_notes(record_id, title, content, extra)
|
|
613
|
+
SELECT n.id, COALESCE(n.title, ''), COALESCE(n.content, ''), ${NOTES_EXTRA_EXPR}
|
|
614
|
+
FROM notes n WHERE n.id = new.id;
|
|
615
|
+
END;
|
|
616
|
+
CREATE TRIGGER trg_fts_notes_au AFTER UPDATE OF id, title, content, type, author ON notes BEGIN
|
|
617
|
+
DELETE FROM fts_notes WHERE record_id = old.id;
|
|
618
|
+
INSERT INTO fts_notes(record_id, title, content, extra)
|
|
619
|
+
SELECT n.id, COALESCE(n.title, ''), COALESCE(n.content, ''), ${NOTES_EXTRA_EXPR}
|
|
620
|
+
FROM notes n WHERE n.id = new.id;
|
|
621
|
+
END;
|
|
622
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_notes_ad AFTER DELETE ON notes BEGIN
|
|
623
|
+
DELETE FROM fts_notes WHERE record_id = old.id;
|
|
624
|
+
END;
|
|
625
|
+
|
|
626
|
+
-- note_concepts triggers
|
|
627
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_note_concepts_ai AFTER INSERT ON note_concepts BEGIN
|
|
628
|
+
DELETE FROM fts_notes WHERE record_id = new.note_id;
|
|
629
|
+
INSERT INTO fts_notes(record_id, title, content, extra)
|
|
630
|
+
SELECT n.id, COALESCE(n.title, ''), COALESCE(n.content, ''), ${NOTES_EXTRA_EXPR}
|
|
631
|
+
FROM notes n WHERE n.id = new.note_id;
|
|
632
|
+
END;
|
|
633
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_note_concepts_ad AFTER DELETE ON note_concepts BEGIN
|
|
634
|
+
DELETE FROM fts_notes WHERE record_id = old.note_id;
|
|
635
|
+
INSERT INTO fts_notes(record_id, title, content, extra)
|
|
636
|
+
SELECT n.id, COALESCE(n.title, ''), COALESCE(n.content, ''), ${NOTES_EXTRA_EXPR}
|
|
637
|
+
FROM notes n WHERE n.id = old.note_id;
|
|
638
|
+
END;
|
|
639
|
+
|
|
640
|
+
-- note_files triggers
|
|
641
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_note_files_ai AFTER INSERT ON note_files BEGIN
|
|
642
|
+
DELETE FROM fts_notes WHERE record_id = new.note_id;
|
|
643
|
+
INSERT INTO fts_notes(record_id, title, content, extra)
|
|
644
|
+
SELECT n.id, COALESCE(n.title, ''), COALESCE(n.content, ''), ${NOTES_EXTRA_EXPR}
|
|
645
|
+
FROM notes n WHERE n.id = new.note_id;
|
|
646
|
+
END;
|
|
647
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_note_files_ad AFTER DELETE ON note_files BEGIN
|
|
648
|
+
DELETE FROM fts_notes WHERE record_id = old.note_id;
|
|
649
|
+
INSERT INTO fts_notes(record_id, title, content, extra)
|
|
650
|
+
SELECT n.id, COALESCE(n.title, ''), COALESCE(n.content, ''), ${NOTES_EXTRA_EXPR}
|
|
651
|
+
FROM notes n WHERE n.id = old.note_id;
|
|
652
|
+
END;
|
|
653
|
+
|
|
654
|
+
-- trajectories triggers
|
|
655
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_trajectories_ai AFTER INSERT ON trajectories BEGIN
|
|
656
|
+
DELETE FROM fts_trajectories WHERE record_id = new.id;
|
|
657
|
+
INSERT INTO fts_trajectories(record_id, description, result, task_type)
|
|
658
|
+
VALUES (new.id, COALESCE(new.description, ''), COALESCE(new.result, ''), COALESCE(new.task_type, ''));
|
|
659
|
+
END;
|
|
660
|
+
CREATE TRIGGER trg_fts_trajectories_au AFTER UPDATE OF id, description, result, task_type ON trajectories BEGIN
|
|
661
|
+
DELETE FROM fts_trajectories WHERE record_id = old.id;
|
|
662
|
+
INSERT INTO fts_trajectories(record_id, description, result, task_type)
|
|
663
|
+
VALUES (new.id, COALESCE(new.description, ''), COALESCE(new.result, ''), COALESCE(new.task_type, ''));
|
|
664
|
+
END;
|
|
665
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_trajectories_ad AFTER DELETE ON trajectories BEGIN
|
|
666
|
+
DELETE FROM fts_trajectories WHERE record_id = old.id;
|
|
667
|
+
END;
|
|
668
|
+
|
|
669
|
+
-- observations triggers
|
|
670
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_observations_ai AFTER INSERT ON observations BEGIN
|
|
671
|
+
DELETE FROM fts_observations WHERE record_id = new.id;
|
|
672
|
+
INSERT INTO fts_observations(record_id, tool_name, file_path, input, output, condensed_text)
|
|
673
|
+
VALUES (new.id, COALESCE(new.tool_name, ''), COALESCE(new.file_path, ''), COALESCE(new.input, ''), COALESCE(new.output, ''), COALESCE(new.condensed_text, ''));
|
|
674
|
+
END;
|
|
675
|
+
CREATE TRIGGER trg_fts_observations_au AFTER UPDATE OF id, tool_name, file_path, input, output, condensed_text ON observations BEGIN
|
|
676
|
+
DELETE FROM fts_observations WHERE record_id = old.id;
|
|
677
|
+
INSERT INTO fts_observations(record_id, tool_name, file_path, input, output, condensed_text)
|
|
678
|
+
VALUES (new.id, COALESCE(new.tool_name, ''), COALESCE(new.file_path, ''), COALESCE(new.input, ''), COALESCE(new.output, ''), COALESCE(new.condensed_text, ''));
|
|
679
|
+
END;
|
|
680
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_observations_ad AFTER DELETE ON observations BEGIN
|
|
681
|
+
DELETE FROM fts_observations WHERE record_id = old.id;
|
|
682
|
+
END;
|
|
683
|
+
|
|
684
|
+
-- summaries triggers
|
|
685
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_summaries_ai AFTER INSERT ON summaries BEGIN
|
|
686
|
+
DELETE FROM fts_summaries WHERE record_id = new.id;
|
|
687
|
+
INSERT INTO fts_summaries(record_id, text)
|
|
688
|
+
SELECT s.id, ${SUMMARIES_TEXT_EXPR}
|
|
689
|
+
FROM summaries s WHERE s.id = new.id;
|
|
690
|
+
END;
|
|
691
|
+
CREATE TRIGGER trg_fts_summaries_au AFTER UPDATE OF id, kind, outcome, files_touched, error_verbatim ON summaries BEGIN
|
|
692
|
+
DELETE FROM fts_summaries WHERE record_id = old.id;
|
|
693
|
+
INSERT INTO fts_summaries(record_id, text)
|
|
694
|
+
SELECT s.id, ${SUMMARIES_TEXT_EXPR}
|
|
695
|
+
FROM summaries s WHERE s.id = new.id;
|
|
696
|
+
END;
|
|
697
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_summaries_ad AFTER DELETE ON summaries BEGIN
|
|
698
|
+
DELETE FROM fts_summaries WHERE record_id = old.id;
|
|
699
|
+
END;
|
|
700
|
+
|
|
701
|
+
-- summary_claims triggers
|
|
702
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_summary_claims_ai AFTER INSERT ON summary_claims BEGIN
|
|
703
|
+
DELETE FROM fts_summaries WHERE record_id = new.summary_id;
|
|
704
|
+
INSERT INTO fts_summaries(record_id, text)
|
|
705
|
+
SELECT s.id, ${SUMMARIES_TEXT_EXPR}
|
|
706
|
+
FROM summaries s WHERE s.id = new.summary_id;
|
|
707
|
+
END;
|
|
708
|
+
CREATE TRIGGER trg_fts_summary_claims_au AFTER UPDATE OF summary_id, text ON summary_claims BEGIN
|
|
709
|
+
DELETE FROM fts_summaries WHERE record_id IN (old.summary_id, new.summary_id);
|
|
710
|
+
INSERT INTO fts_summaries(record_id, text)
|
|
711
|
+
SELECT s.id, ${SUMMARIES_TEXT_EXPR}
|
|
712
|
+
FROM summaries s WHERE s.id IN (old.summary_id, new.summary_id);
|
|
713
|
+
END;
|
|
714
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_summary_claims_ad AFTER DELETE ON summary_claims BEGIN
|
|
715
|
+
DELETE FROM fts_summaries WHERE record_id = old.summary_id;
|
|
716
|
+
INSERT INTO fts_summaries(record_id, text)
|
|
717
|
+
SELECT s.id, ${SUMMARIES_TEXT_EXPR}
|
|
718
|
+
FROM summaries s WHERE s.id = old.summary_id;
|
|
719
|
+
END;
|
|
720
|
+
|
|
721
|
+
-- entities triggers
|
|
722
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_entities_ai AFTER INSERT ON entities BEGIN
|
|
723
|
+
DELETE FROM fts_entities WHERE record_id = new.id;
|
|
724
|
+
INSERT INTO fts_entities(record_id, name, entity_type, description, external_ref)
|
|
725
|
+
VALUES (new.id, COALESCE(new.name, ''), COALESCE(new.entity_type, ''), COALESCE(new.description, ''), COALESCE(new.external_ref, ''));
|
|
726
|
+
END;
|
|
727
|
+
CREATE TRIGGER trg_fts_entities_au AFTER UPDATE OF id, name, entity_type, description, external_ref ON entities BEGIN
|
|
728
|
+
DELETE FROM fts_entities WHERE record_id = old.id;
|
|
729
|
+
INSERT INTO fts_entities(record_id, name, entity_type, description, external_ref)
|
|
730
|
+
VALUES (new.id, COALESCE(new.name, ''), COALESCE(new.entity_type, ''), COALESCE(new.description, ''), COALESCE(new.external_ref, ''));
|
|
731
|
+
END;
|
|
732
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_entities_ad AFTER DELETE ON entities BEGIN
|
|
733
|
+
DELETE FROM fts_entities WHERE record_id = old.id;
|
|
734
|
+
END;
|
|
735
|
+
|
|
736
|
+
-- actions triggers
|
|
737
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_actions_ai AFTER INSERT ON actions BEGIN
|
|
738
|
+
DELETE FROM fts_actions WHERE record_id = new.id;
|
|
739
|
+
INSERT INTO fts_actions(record_id, title, description, blocked_reason)
|
|
740
|
+
VALUES (new.id, COALESCE(new.title, ''), COALESCE(new.description, ''), COALESCE(new.blocked_reason, ''));
|
|
741
|
+
END;
|
|
742
|
+
CREATE TRIGGER trg_fts_actions_au AFTER UPDATE OF id, title, description, blocked_reason ON actions BEGIN
|
|
743
|
+
DELETE FROM fts_actions WHERE record_id = old.id;
|
|
744
|
+
INSERT INTO fts_actions(record_id, title, description, blocked_reason)
|
|
745
|
+
VALUES (new.id, COALESCE(new.title, ''), COALESCE(new.description, ''), COALESCE(new.blocked_reason, ''));
|
|
746
|
+
END;
|
|
747
|
+
CREATE TRIGGER IF NOT EXISTS trg_fts_actions_ad AFTER DELETE ON actions BEGIN
|
|
748
|
+
DELETE FROM fts_actions WHERE record_id = old.id;
|
|
749
|
+
END;
|
|
750
|
+
`);
|
|
751
|
+
const backfills = [
|
|
752
|
+
{
|
|
753
|
+
source: 'notes',
|
|
754
|
+
fts: 'fts_notes',
|
|
755
|
+
sql: `
|
|
756
|
+
INSERT INTO fts_notes(record_id, title, content, extra)
|
|
757
|
+
SELECT n.id, COALESCE(n.title, ''), COALESCE(n.content, ''), ${NOTES_EXTRA_EXPR}
|
|
758
|
+
FROM notes n
|
|
759
|
+
`,
|
|
760
|
+
},
|
|
761
|
+
{
|
|
762
|
+
source: 'trajectories',
|
|
763
|
+
fts: 'fts_trajectories',
|
|
764
|
+
sql: `
|
|
765
|
+
INSERT INTO fts_trajectories(record_id, description, result, task_type)
|
|
766
|
+
SELECT id, COALESCE(description, ''), COALESCE(result, ''), COALESCE(task_type, '')
|
|
767
|
+
FROM trajectories
|
|
768
|
+
`,
|
|
769
|
+
},
|
|
770
|
+
{
|
|
771
|
+
source: 'observations',
|
|
772
|
+
fts: 'fts_observations',
|
|
773
|
+
sql: `
|
|
774
|
+
INSERT INTO fts_observations(record_id, tool_name, file_path, input, output, condensed_text)
|
|
775
|
+
SELECT id, COALESCE(tool_name, ''), COALESCE(file_path, ''), COALESCE(input, ''), COALESCE(output, ''), COALESCE(condensed_text, '')
|
|
776
|
+
FROM observations
|
|
777
|
+
`,
|
|
778
|
+
},
|
|
779
|
+
{
|
|
780
|
+
source: 'summaries',
|
|
781
|
+
fts: 'fts_summaries',
|
|
782
|
+
sql: `
|
|
783
|
+
INSERT INTO fts_summaries(record_id, text)
|
|
784
|
+
SELECT s.id, ${SUMMARIES_TEXT_EXPR}
|
|
785
|
+
FROM summaries s
|
|
786
|
+
`,
|
|
787
|
+
},
|
|
788
|
+
{
|
|
789
|
+
source: 'entities',
|
|
790
|
+
fts: 'fts_entities',
|
|
791
|
+
sql: `
|
|
792
|
+
INSERT INTO fts_entities(record_id, name, entity_type, description, external_ref)
|
|
793
|
+
SELECT id, COALESCE(name, ''), COALESCE(entity_type, ''), COALESCE(description, ''), COALESCE(external_ref, '')
|
|
794
|
+
FROM entities
|
|
795
|
+
`,
|
|
796
|
+
},
|
|
797
|
+
{
|
|
798
|
+
source: 'actions',
|
|
799
|
+
fts: 'fts_actions',
|
|
800
|
+
sql: `
|
|
801
|
+
INSERT INTO fts_actions(record_id, title, description, blocked_reason)
|
|
802
|
+
SELECT id, COALESCE(title, ''), COALESCE(description, ''), COALESCE(blocked_reason, '')
|
|
803
|
+
FROM actions
|
|
804
|
+
`,
|
|
805
|
+
},
|
|
806
|
+
];
|
|
807
|
+
(0, sqlite_1.transaction)(db, () => {
|
|
808
|
+
for (const item of backfills) {
|
|
809
|
+
const hasSrc = db.prepare(`SELECT 1 FROM ${item.source} LIMIT 1`).get();
|
|
810
|
+
if (!hasSrc)
|
|
811
|
+
continue;
|
|
812
|
+
const hasFts = db.prepare(`SELECT 1 FROM ${item.fts} LIMIT 1`).get();
|
|
813
|
+
if (hasFts)
|
|
814
|
+
continue;
|
|
815
|
+
db.exec(item.sql);
|
|
816
|
+
logger_1.logger.info({ source: item.source, fts: item.fts }, 'FTS5 backfill complete');
|
|
817
|
+
}
|
|
818
|
+
})();
|
|
819
|
+
}
|
|
325
820
|
function runMigrations(db) {
|
|
326
821
|
db.exec(`
|
|
327
822
|
-- store_meta (single row id = 1)
|
|
@@ -340,62 +835,9 @@ function runMigrations(db) {
|
|
|
340
835
|
set_at INTEGER NOT NULL DEFAULT (unixepoch())
|
|
341
836
|
);
|
|
342
837
|
|
|
343
|
-
-- observations
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
347
|
-
project_id TEXT,
|
|
348
|
-
note_ref TEXT, -- -> notes.id, optional
|
|
349
|
-
task_ref TEXT, -- provenance only, not a scope filter
|
|
350
|
-
session_id TEXT,
|
|
351
|
-
agent TEXT,
|
|
352
|
-
kind TEXT NOT NULL CHECK (kind IN ('tool_call','prompt')),
|
|
353
|
-
tool_name TEXT,
|
|
354
|
-
file_path TEXT, -- the tool's TARGET path, nothing else
|
|
355
|
-
input TEXT,
|
|
356
|
-
output TEXT,
|
|
357
|
-
duration_ms INTEGER NOT NULL DEFAULT 0,
|
|
358
|
-
failed INTEGER NOT NULL DEFAULT 0, -- set by the failure hook, never inferred
|
|
359
|
-
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
360
|
-
archived_at INTEGER,
|
|
361
|
-
CHECK ((scope = 'project') = (project_id IS NOT NULL))
|
|
362
|
-
);
|
|
363
|
-
|
|
364
|
-
CREATE INDEX IF NOT EXISTS idx_obs_project ON observations(project_id);
|
|
365
|
-
CREATE INDEX IF NOT EXISTS idx_obs_scope ON observations(scope);
|
|
366
|
-
CREATE INDEX IF NOT EXISTS idx_obs_session ON observations(session_id);
|
|
367
|
-
CREATE INDEX IF NOT EXISTS idx_obs_created ON observations(created_at);
|
|
368
|
-
CREATE INDEX IF NOT EXISTS idx_obs_note ON observations(note_ref);
|
|
369
|
-
CREATE INDEX IF NOT EXISTS idx_obs_task ON observations(task_ref);
|
|
370
|
-
CREATE INDEX IF NOT EXISTS idx_obs_file ON observations(file_path);
|
|
371
|
-
CREATE INDEX IF NOT EXISTS idx_obs_failed ON observations(failed, created_at);
|
|
372
|
-
CREATE INDEX IF NOT EXISTS idx_observations_archived ON observations(archived_at);
|
|
373
|
-
|
|
374
|
-
-- observations_archive
|
|
375
|
-
CREATE TABLE IF NOT EXISTS observations_archive (
|
|
376
|
-
id TEXT PRIMARY KEY,
|
|
377
|
-
original_observation_id TEXT NOT NULL UNIQUE,
|
|
378
|
-
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
379
|
-
project_id TEXT,
|
|
380
|
-
note_ref TEXT,
|
|
381
|
-
task_ref TEXT,
|
|
382
|
-
session_id TEXT,
|
|
383
|
-
agent TEXT,
|
|
384
|
-
kind TEXT NOT NULL CHECK (kind IN ('tool_call','prompt')),
|
|
385
|
-
tool_name TEXT,
|
|
386
|
-
file_path TEXT,
|
|
387
|
-
input TEXT,
|
|
388
|
-
output TEXT,
|
|
389
|
-
duration_ms INTEGER NOT NULL DEFAULT 0,
|
|
390
|
-
failed INTEGER NOT NULL DEFAULT 0,
|
|
391
|
-
created_at INTEGER NOT NULL,
|
|
392
|
-
archived_at INTEGER NOT NULL,
|
|
393
|
-
CHECK ((scope = 'project') = (project_id IS NOT NULL))
|
|
394
|
-
);
|
|
395
|
-
|
|
396
|
-
CREATE INDEX IF NOT EXISTS idx_obs_arc_project ON observations_archive(project_id);
|
|
397
|
-
CREATE INDEX IF NOT EXISTS idx_obs_arc_scope ON observations_archive(scope);
|
|
398
|
-
CREATE INDEX IF NOT EXISTS idx_obs_arc_archived ON observations_archive(archived_at);
|
|
838
|
+
-- observations (DDL in OBSERVATIONS_DDL; see alignObservationsSchema)
|
|
839
|
+
${OBSERVATIONS_DDL}
|
|
840
|
+
${OBSERVATIONS_ARCHIVE_DDL}
|
|
399
841
|
|
|
400
842
|
-- trajectories (DDL in TRAJECTORIES_DDL; see alignTrajectorySchema)
|
|
401
843
|
${TRAJECTORIES_DDL}
|
|
@@ -424,6 +866,8 @@ function runMigrations(db) {
|
|
|
424
866
|
id TEXT PRIMARY KEY,
|
|
425
867
|
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
426
868
|
project_id TEXT,
|
|
869
|
+
user_id TEXT,
|
|
870
|
+
agent_id TEXT,
|
|
427
871
|
session_id TEXT,
|
|
428
872
|
task_type TEXT NOT NULL DEFAULT 'unknown',
|
|
429
873
|
error_text TEXT NOT NULL,
|
|
@@ -439,6 +883,8 @@ function runMigrations(db) {
|
|
|
439
883
|
id TEXT PRIMARY KEY,
|
|
440
884
|
scope TEXT NOT NULL DEFAULT 'unknown' CHECK(scope IN ('project','global','unknown')),
|
|
441
885
|
project_id TEXT,
|
|
886
|
+
user_id TEXT,
|
|
887
|
+
agent_id TEXT,
|
|
442
888
|
agent_name TEXT,
|
|
443
889
|
cwd TEXT,
|
|
444
890
|
status TEXT NOT NULL DEFAULT 'active' CHECK(status IN ('active','paused','completed','abandoned')),
|
|
@@ -450,20 +896,27 @@ function runMigrations(db) {
|
|
|
450
896
|
CHECK ((scope = 'project') = (project_id IS NOT NULL))
|
|
451
897
|
);
|
|
452
898
|
|
|
453
|
-
CREATE INDEX IF NOT EXISTS idx_sessions_proj_status ON sessions(project_id, status, started_at);
|
|
454
|
-
|
|
455
899
|
-- actions
|
|
456
900
|
CREATE TABLE IF NOT EXISTS actions (
|
|
457
901
|
id TEXT PRIMARY KEY,
|
|
458
902
|
session_id TEXT,
|
|
459
903
|
scope TEXT NOT NULL DEFAULT 'unknown' CHECK(scope IN ('project','global','unknown')),
|
|
460
904
|
project_id TEXT,
|
|
905
|
+
user_id TEXT,
|
|
906
|
+
agent_id TEXT,
|
|
461
907
|
title TEXT NOT NULL,
|
|
462
908
|
description TEXT,
|
|
463
909
|
status TEXT NOT NULL DEFAULT 'ready' CHECK(status IN ('ready','blocked','in_progress','done','dismissed')),
|
|
464
910
|
priority TEXT NOT NULL DEFAULT 'normal' CHECK(priority IN ('low','normal','high','urgent')),
|
|
465
911
|
blocked_reason TEXT,
|
|
466
912
|
files_json TEXT,
|
|
913
|
+
due_at TEXT,
|
|
914
|
+
remind_at TEXT,
|
|
915
|
+
recurrence TEXT,
|
|
916
|
+
last_reminded_at TEXT,
|
|
917
|
+
snooze_until TEXT,
|
|
918
|
+
parent_action_id TEXT,
|
|
919
|
+
embedding BLOB,
|
|
467
920
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
468
921
|
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
469
922
|
resolved_at TEXT,
|
|
@@ -471,17 +924,13 @@ function runMigrations(db) {
|
|
|
471
924
|
CHECK ((scope = 'project') = (project_id IS NOT NULL))
|
|
472
925
|
);
|
|
473
926
|
|
|
474
|
-
CREATE INDEX IF NOT EXISTS idx_actions_proj_status ON actions(project_id, status, priority);
|
|
475
|
-
|
|
476
|
-
CREATE INDEX IF NOT EXISTS idx_fp_lookup ON failure_patterns(project_id, task_type, count DESC);
|
|
477
|
-
CREATE INDEX IF NOT EXISTS idx_fp_recent ON failure_patterns(last_seen_at);
|
|
478
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_fp_unique_coalesce ON failure_patterns(COALESCE(project_id, '__GLOBAL_OR_UNKNOWN__:' || scope), task_type, error_text);
|
|
479
|
-
|
|
480
927
|
-- summaries
|
|
481
928
|
CREATE TABLE IF NOT EXISTS summaries (
|
|
482
929
|
id TEXT PRIMARY KEY,
|
|
483
930
|
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
484
931
|
project_id TEXT,
|
|
932
|
+
user_id TEXT,
|
|
933
|
+
agent_id TEXT,
|
|
485
934
|
kind TEXT NOT NULL CHECK (kind IN ('session','rollup','failure_digest')),
|
|
486
935
|
window_from INTEGER,
|
|
487
936
|
window_to INTEGER,
|
|
@@ -510,16 +959,52 @@ function runMigrations(db) {
|
|
|
510
959
|
summary_id TEXT NOT NULL REFERENCES summaries(id) ON DELETE CASCADE,
|
|
511
960
|
claim_id TEXT REFERENCES summary_claims(id) ON DELETE CASCADE,
|
|
512
961
|
record_id TEXT NOT NULL,
|
|
513
|
-
|
|
962
|
+
record_type TEXT NOT NULL CHECK (record_type IN ('observation','trajectory','note')),
|
|
514
963
|
PRIMARY KEY (summary_id, claim_id, record_id)
|
|
515
964
|
);
|
|
516
965
|
|
|
517
|
-
CREATE INDEX IF NOT EXISTS idx_sum_project ON summaries(project_id);
|
|
518
|
-
CREATE INDEX IF NOT EXISTS idx_sum_kind ON summaries(kind, created_at);
|
|
519
|
-
CREATE INDEX IF NOT EXISTS idx_sum_latest ON summaries(is_latest);
|
|
520
|
-
CREATE INDEX IF NOT EXISTS idx_sum_producer ON summaries(producer_tier, producer_model);
|
|
521
966
|
CREATE INDEX IF NOT EXISTS idx_sum_src_rec ON summary_sources(record_id);
|
|
522
967
|
|
|
968
|
+
-- entities
|
|
969
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
970
|
+
id TEXT PRIMARY KEY,
|
|
971
|
+
scope TEXT NOT NULL CHECK (scope IN ('project','global','unknown')),
|
|
972
|
+
project_id TEXT,
|
|
973
|
+
user_id TEXT,
|
|
974
|
+
agent_id TEXT,
|
|
975
|
+
entity_type TEXT NOT NULL,
|
|
976
|
+
name TEXT NOT NULL,
|
|
977
|
+
description TEXT,
|
|
978
|
+
external_ref TEXT,
|
|
979
|
+
metadata_json TEXT,
|
|
980
|
+
embedding BLOB,
|
|
981
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
982
|
+
updated_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
983
|
+
CHECK ((scope = 'project') = (project_id IS NOT NULL))
|
|
984
|
+
);
|
|
985
|
+
|
|
986
|
+
CREATE INDEX IF NOT EXISTS idx_entities_scope_proj ON entities(scope, project_id, entity_type);
|
|
987
|
+
CREATE INDEX IF NOT EXISTS idx_entities_ext_ref ON entities(external_ref) WHERE external_ref IS NOT NULL;
|
|
988
|
+
CREATE INDEX IF NOT EXISTS idx_entities_user ON entities(user_id) WHERE user_id IS NOT NULL;
|
|
989
|
+
CREATE INDEX IF NOT EXISTS idx_entities_dedup ON entities(COALESCE(project_id, '__SCOPE__:' || scope), entity_type, lower(name));
|
|
990
|
+
|
|
991
|
+
-- entity_edges
|
|
992
|
+
CREATE TABLE IF NOT EXISTS entity_edges (
|
|
993
|
+
id TEXT PRIMARY KEY,
|
|
994
|
+
source_type TEXT NOT NULL,
|
|
995
|
+
source_id TEXT NOT NULL,
|
|
996
|
+
target_type TEXT NOT NULL,
|
|
997
|
+
target_id TEXT NOT NULL,
|
|
998
|
+
relation TEXT NOT NULL,
|
|
999
|
+
metadata_json TEXT,
|
|
1000
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
1001
|
+
UNIQUE (source_type, source_id, target_type, target_id, relation)
|
|
1002
|
+
);
|
|
1003
|
+
|
|
1004
|
+
CREATE INDEX IF NOT EXISTS idx_entity_edges_src ON entity_edges(source_id, relation);
|
|
1005
|
+
CREATE INDEX IF NOT EXISTS idx_entity_edges_tgt ON entity_edges(target_id, relation);
|
|
1006
|
+
CREATE INDEX IF NOT EXISTS idx_entity_edges_rel ON entity_edges(relation);
|
|
1007
|
+
|
|
523
1008
|
-- consolidation_leases
|
|
524
1009
|
CREATE TABLE IF NOT EXISTS consolidation_leases (
|
|
525
1010
|
lease_id TEXT PRIMARY KEY,
|
|
@@ -536,20 +1021,8 @@ function runMigrations(db) {
|
|
|
536
1021
|
CREATE INDEX IF NOT EXISTS idx_lease_expires ON consolidation_leases(expires_at);
|
|
537
1022
|
CREATE INDEX IF NOT EXISTS idx_lease_session ON consolidation_leases(session_id);
|
|
538
1023
|
|
|
539
|
-
-- embed_queue
|
|
540
|
-
|
|
541
|
-
record_id TEXT NOT NULL,
|
|
542
|
-
grain TEXT NOT NULL CHECK (grain IN ('observation','trajectory','note','summary')),
|
|
543
|
-
status TEXT NOT NULL DEFAULT 'pending'
|
|
544
|
-
CHECK (status IN ('pending','running','done','failed')),
|
|
545
|
-
attempts INTEGER NOT NULL DEFAULT 0,
|
|
546
|
-
last_error TEXT,
|
|
547
|
-
target TEXT NOT NULL DEFAULT 'live', -- 'live' or a re-embed collection name
|
|
548
|
-
queued_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
549
|
-
PRIMARY KEY (record_id, grain, target)
|
|
550
|
-
);
|
|
551
|
-
|
|
552
|
-
CREATE INDEX IF NOT EXISTS idx_eq_claim ON embed_queue(target, status, queued_at);
|
|
1024
|
+
-- embed_queue (DDL in EMBED_QUEUE_DDL; see alignEmbedQueueSchema)
|
|
1025
|
+
${EMBED_QUEUE_DDL}
|
|
553
1026
|
|
|
554
1027
|
-- dead_letters
|
|
555
1028
|
CREATE TABLE IF NOT EXISTS dead_letters (
|
|
@@ -578,12 +1051,26 @@ function runMigrations(db) {
|
|
|
578
1051
|
|
|
579
1052
|
CREATE INDEX IF NOT EXISTS idx_api_keys_key ON api_keys(key);
|
|
580
1053
|
`);
|
|
581
|
-
//
|
|
582
|
-
|
|
583
|
-
|
|
1054
|
+
// Legacy grain -> record_type rename before schema alignments/rebuilds
|
|
1055
|
+
for (const table of ['summary_sources', 'embed_queue']) {
|
|
1056
|
+
const cols = db.prepare(`PRAGMA table_info(${table})`).all();
|
|
1057
|
+
const hasGrain = cols.some((c) => c.name === 'grain');
|
|
1058
|
+
const hasRecordType = cols.some((c) => c.name === 'record_type');
|
|
1059
|
+
if (hasGrain && !hasRecordType) {
|
|
1060
|
+
db.exec(`ALTER TABLE ${table} RENAME COLUMN grain TO record_type`);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
// Schema alignments & rebuilds for pre-existing databases
|
|
1064
|
+
alignObservationsSchema(db);
|
|
584
1065
|
alignTrajectorySchema(db, 'trajectories', TRAJECTORIES_DDL, TRAJECTORIES_INDEX_DDL);
|
|
585
1066
|
alignTrajectorySchema(db, 'trajectory_archive', TRAJECTORY_ARCHIVE_DDL, TRAJECTORY_ARCHIVE_INDEX_DDL);
|
|
586
1067
|
alignNotesSchema(db);
|
|
1068
|
+
alignSessionsSchema(db);
|
|
1069
|
+
alignSummariesSchema(db);
|
|
587
1070
|
alignFailurePatternsSchema(db);
|
|
1071
|
+
alignActionsSchema(db);
|
|
1072
|
+
alignEmbedQueueSchema(db);
|
|
1073
|
+
// Must run AFTER all table rebuilds so FTS triggers are never lost
|
|
1074
|
+
alignFtsSchema(db);
|
|
588
1075
|
}
|
|
589
1076
|
//# sourceMappingURL=db.js.map
|