principles-disciple 1.154.0 → 1.156.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/dist/core/control-ui-db.d.ts +11 -0
- package/dist/core/control-ui-db.js +40 -16
- package/dist/core/trajectory.d.ts +16 -0
- package/dist/core/trajectory.js +343 -254
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/service/subagent-workflow/workflow-store.d.ts +14 -0
- package/dist/service/subagent-workflow/workflow-store.js +91 -2
- package/openclaw.plugin.json +1 -1
- package/package.json +7 -3
- package/dist/core/schema/db-types.d.ts +0 -11
- package/dist/core/schema/db-types.js +0 -5
- package/dist/core/schema/index.d.ts +0 -17
- package/dist/core/schema/index.js +0 -15
- package/dist/core/schema/migration-runner.d.ts +0 -53
- package/dist/core/schema/migration-runner.js +0 -170
- package/dist/core/schema/migrations/001-init-trajectory.d.ts +0 -6
- package/dist/core/schema/migrations/001-init-trajectory.js +0 -190
- package/dist/core/schema/migrations/002-init-central.d.ts +0 -5
- package/dist/core/schema/migrations/002-init-central.js +0 -108
- package/dist/core/schema/migrations/003-init-workflow.d.ts +0 -5
- package/dist/core/schema/migrations/003-init-workflow.js +0 -50
- package/dist/core/schema/migrations/004-add-thinking-and-gfi.d.ts +0 -8
- package/dist/core/schema/migrations/004-add-thinking-and-gfi.js +0 -66
- package/dist/core/schema/migrations/index.d.ts +0 -16
- package/dist/core/schema/migrations/index.js +0 -27
- package/dist/core/schema/schema-definitions.d.ts +0 -46
- package/dist/core/schema/schema-definitions.js +0 -599
- package/scripts/db-migrate.mjs +0 -170
package/dist/core/trajectory.js
CHANGED
|
@@ -19,6 +19,343 @@ const DEFAULT_INLINE_THRESHOLD = 16 * 1024;
|
|
|
19
19
|
const DEFAULT_BUSY_TIMEOUT_MS = 5000;
|
|
20
20
|
const DEFAULT_ORPHAN_BLOB_GRACE_DAYS = 7;
|
|
21
21
|
const SCHEMA_VERSION = 1;
|
|
22
|
+
/**
|
|
23
|
+
* Type guard: narrows `object` to `{ name: unknown }` without `as` casts.
|
|
24
|
+
* Used for PRAGMA table_info rows (rc-2-no-as-bypass).
|
|
25
|
+
*/
|
|
26
|
+
function hasNameField(row) {
|
|
27
|
+
return Object.hasOwn(row, 'name');
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Apply the full trajectory.db schema (tables + indexes + views + migrations) to an open
|
|
31
|
+
* Database handle. Used by TrajectoryDatabase.initSchema() and exported via
|
|
32
|
+
* initTrajectorySchema() for external callers (e.g. `pd runtime init`).
|
|
33
|
+
*
|
|
34
|
+
* DDL is the single source of truth for trajectory.db schema. pain-signal-observability.ts
|
|
35
|
+
* in principles-core also duplicates a subset of this DDL (acknowledged duplication); when
|
|
36
|
+
* updating this function, audit ensureTrajectorySchema() in core for parallel updates.
|
|
37
|
+
*
|
|
38
|
+
* Idempotent: all CREATE statements use IF NOT EXISTS; ALTER columns use try/catch on
|
|
39
|
+
* "duplicate column name" to skip existing columns.
|
|
40
|
+
*/
|
|
41
|
+
function applyTrajectorySchema(db) {
|
|
42
|
+
const warnings = [];
|
|
43
|
+
const tables = [
|
|
44
|
+
'schema_version', 'ingest_checkpoint', 'sessions', 'assistant_turns',
|
|
45
|
+
'user_turns', 'tool_calls', 'pain_events', 'gate_blocks', 'trust_changes',
|
|
46
|
+
'principle_events', 'task_outcomes', 'correction_samples', 'sample_reviews',
|
|
47
|
+
'exports_audit', 'evolution_tasks', 'evolution_events',
|
|
48
|
+
];
|
|
49
|
+
db.exec(`
|
|
50
|
+
CREATE TABLE IF NOT EXISTS schema_version (version INTEGER NOT NULL);
|
|
51
|
+
CREATE TABLE IF NOT EXISTS ingest_checkpoint (
|
|
52
|
+
source_key TEXT PRIMARY KEY,
|
|
53
|
+
imported_at TEXT NOT NULL
|
|
54
|
+
);
|
|
55
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
56
|
+
session_id TEXT PRIMARY KEY,
|
|
57
|
+
started_at TEXT NOT NULL,
|
|
58
|
+
updated_at TEXT NOT NULL
|
|
59
|
+
);
|
|
60
|
+
CREATE TABLE IF NOT EXISTS assistant_turns (
|
|
61
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
62
|
+
session_id TEXT NOT NULL,
|
|
63
|
+
run_id TEXT NOT NULL,
|
|
64
|
+
provider TEXT NOT NULL,
|
|
65
|
+
model TEXT NOT NULL,
|
|
66
|
+
raw_text TEXT,
|
|
67
|
+
sanitized_text TEXT NOT NULL,
|
|
68
|
+
usage_json TEXT NOT NULL,
|
|
69
|
+
empathy_signal_json TEXT NOT NULL,
|
|
70
|
+
blob_ref TEXT,
|
|
71
|
+
raw_excerpt TEXT,
|
|
72
|
+
stop_reason TEXT,
|
|
73
|
+
thinking_blocks_count INTEGER,
|
|
74
|
+
created_at TEXT NOT NULL
|
|
75
|
+
);
|
|
76
|
+
CREATE TABLE IF NOT EXISTS user_turns (
|
|
77
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
78
|
+
session_id TEXT NOT NULL,
|
|
79
|
+
turn_index INTEGER NOT NULL,
|
|
80
|
+
raw_text TEXT,
|
|
81
|
+
blob_ref TEXT,
|
|
82
|
+
raw_excerpt TEXT,
|
|
83
|
+
correction_detected INTEGER NOT NULL DEFAULT 0,
|
|
84
|
+
correction_cue TEXT,
|
|
85
|
+
references_assistant_turn_id INTEGER,
|
|
86
|
+
created_at TEXT NOT NULL
|
|
87
|
+
);
|
|
88
|
+
CREATE TABLE IF NOT EXISTS tool_calls (
|
|
89
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
90
|
+
session_id TEXT NOT NULL,
|
|
91
|
+
tool_name TEXT NOT NULL,
|
|
92
|
+
outcome TEXT NOT NULL,
|
|
93
|
+
duration_ms INTEGER,
|
|
94
|
+
exit_code INTEGER,
|
|
95
|
+
error_type TEXT,
|
|
96
|
+
error_message TEXT,
|
|
97
|
+
gfi_before REAL,
|
|
98
|
+
gfi_after REAL,
|
|
99
|
+
params_json TEXT NOT NULL,
|
|
100
|
+
result_preview TEXT,
|
|
101
|
+
created_at TEXT NOT NULL
|
|
102
|
+
);
|
|
103
|
+
CREATE TABLE IF NOT EXISTS pain_events (
|
|
104
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
105
|
+
session_id TEXT NOT NULL,
|
|
106
|
+
source TEXT NOT NULL,
|
|
107
|
+
score REAL NOT NULL,
|
|
108
|
+
reason TEXT,
|
|
109
|
+
severity TEXT,
|
|
110
|
+
origin TEXT,
|
|
111
|
+
confidence REAL,
|
|
112
|
+
text TEXT,
|
|
113
|
+
canonical_pain_id TEXT,
|
|
114
|
+
runtime_task_id TEXT,
|
|
115
|
+
created_at TEXT NOT NULL
|
|
116
|
+
);
|
|
117
|
+
CREATE TABLE IF NOT EXISTS gate_blocks (
|
|
118
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
119
|
+
session_id TEXT,
|
|
120
|
+
tool_name TEXT NOT NULL,
|
|
121
|
+
file_path TEXT,
|
|
122
|
+
reason TEXT NOT NULL,
|
|
123
|
+
plan_status TEXT,
|
|
124
|
+
created_at TEXT NOT NULL
|
|
125
|
+
);
|
|
126
|
+
CREATE TABLE IF NOT EXISTS trust_changes (
|
|
127
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
128
|
+
session_id TEXT,
|
|
129
|
+
previous_score REAL NOT NULL,
|
|
130
|
+
new_score REAL NOT NULL,
|
|
131
|
+
delta REAL NOT NULL,
|
|
132
|
+
reason TEXT NOT NULL,
|
|
133
|
+
created_at TEXT NOT NULL
|
|
134
|
+
);
|
|
135
|
+
CREATE TABLE IF NOT EXISTS principle_events (
|
|
136
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
137
|
+
principle_id TEXT,
|
|
138
|
+
event_type TEXT NOT NULL,
|
|
139
|
+
payload_json TEXT NOT NULL,
|
|
140
|
+
created_at TEXT NOT NULL
|
|
141
|
+
);
|
|
142
|
+
CREATE TABLE IF NOT EXISTS task_outcomes (
|
|
143
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
144
|
+
session_id TEXT NOT NULL,
|
|
145
|
+
task_id TEXT,
|
|
146
|
+
outcome TEXT NOT NULL,
|
|
147
|
+
summary TEXT,
|
|
148
|
+
principle_ids_json TEXT NOT NULL,
|
|
149
|
+
created_at TEXT NOT NULL
|
|
150
|
+
);
|
|
151
|
+
CREATE TABLE IF NOT EXISTS correction_samples (
|
|
152
|
+
sample_id TEXT PRIMARY KEY,
|
|
153
|
+
session_id TEXT NOT NULL,
|
|
154
|
+
bad_assistant_turn_id INTEGER NOT NULL,
|
|
155
|
+
user_correction_turn_id INTEGER NOT NULL,
|
|
156
|
+
recovery_tool_span_json TEXT NOT NULL,
|
|
157
|
+
diff_excerpt TEXT NOT NULL,
|
|
158
|
+
principle_ids_json TEXT NOT NULL,
|
|
159
|
+
quality_score REAL NOT NULL,
|
|
160
|
+
review_status TEXT NOT NULL,
|
|
161
|
+
export_mode TEXT NOT NULL,
|
|
162
|
+
created_at TEXT NOT NULL,
|
|
163
|
+
updated_at TEXT NOT NULL
|
|
164
|
+
);
|
|
165
|
+
CREATE TABLE IF NOT EXISTS sample_reviews (
|
|
166
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
167
|
+
sample_id TEXT NOT NULL,
|
|
168
|
+
review_status TEXT NOT NULL,
|
|
169
|
+
note TEXT,
|
|
170
|
+
created_at TEXT NOT NULL
|
|
171
|
+
);
|
|
172
|
+
CREATE TABLE IF NOT EXISTS exports_audit (
|
|
173
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
174
|
+
export_kind TEXT NOT NULL,
|
|
175
|
+
mode TEXT NOT NULL,
|
|
176
|
+
approved_only INTEGER NOT NULL,
|
|
177
|
+
file_path TEXT NOT NULL,
|
|
178
|
+
row_count INTEGER NOT NULL,
|
|
179
|
+
created_at TEXT NOT NULL
|
|
180
|
+
);
|
|
181
|
+
CREATE TABLE IF NOT EXISTS evolution_tasks (
|
|
182
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
183
|
+
task_id TEXT UNIQUE NOT NULL,
|
|
184
|
+
trace_id TEXT NOT NULL,
|
|
185
|
+
source TEXT NOT NULL,
|
|
186
|
+
reason TEXT,
|
|
187
|
+
score INTEGER DEFAULT 0,
|
|
188
|
+
status TEXT DEFAULT 'pending',
|
|
189
|
+
enqueued_at TEXT,
|
|
190
|
+
started_at TEXT,
|
|
191
|
+
completed_at TEXT,
|
|
192
|
+
resolution TEXT,
|
|
193
|
+
task_kind TEXT,
|
|
194
|
+
priority TEXT,
|
|
195
|
+
retry_count INTEGER,
|
|
196
|
+
max_retries INTEGER,
|
|
197
|
+
last_error TEXT,
|
|
198
|
+
result_ref TEXT,
|
|
199
|
+
created_at TEXT NOT NULL,
|
|
200
|
+
updated_at TEXT NOT NULL
|
|
201
|
+
);
|
|
202
|
+
CREATE TABLE IF NOT EXISTS evolution_events (
|
|
203
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
204
|
+
trace_id TEXT NOT NULL,
|
|
205
|
+
task_id TEXT,
|
|
206
|
+
stage TEXT NOT NULL,
|
|
207
|
+
level TEXT DEFAULT 'info',
|
|
208
|
+
message TEXT NOT NULL,
|
|
209
|
+
summary TEXT,
|
|
210
|
+
metadata_json TEXT,
|
|
211
|
+
created_at TEXT NOT NULL
|
|
212
|
+
);
|
|
213
|
+
`);
|
|
214
|
+
// Migration: Add text column to pain_events if it doesn't exist (MEM-01)
|
|
215
|
+
// SQLite doesn't support IF NOT EXISTS for ADD COLUMN, so we use try/catch
|
|
216
|
+
try {
|
|
217
|
+
db.exec(`ALTER TABLE pain_events ADD COLUMN text TEXT`);
|
|
218
|
+
}
|
|
219
|
+
catch (err) {
|
|
220
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
221
|
+
if (!message.includes('duplicate column name') && !message.includes('no column named')) {
|
|
222
|
+
// Re-throw unexpected errors — silently swallowing migration failures is dangerous
|
|
223
|
+
throw err;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// PRI-406: Add canonical_pain_id and runtime_task_id columns to pain_events
|
|
227
|
+
for (const col of [
|
|
228
|
+
{ name: 'canonical_pain_id', type: 'TEXT' },
|
|
229
|
+
{ name: 'runtime_task_id', type: 'TEXT' },
|
|
230
|
+
]) {
|
|
231
|
+
try {
|
|
232
|
+
db.exec(`ALTER TABLE pain_events ADD COLUMN ${col.name} ${col.type}`);
|
|
233
|
+
}
|
|
234
|
+
catch (err) {
|
|
235
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
236
|
+
if (!message.includes('duplicate column name') && !message.includes('no column named')) {
|
|
237
|
+
throw err;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Trajectory enhancement: add stop_reason, thinking_blocks_count, result_preview
|
|
242
|
+
const trajectoryEnhancementColumns = [
|
|
243
|
+
{ table: 'assistant_turns', name: 'stop_reason', type: 'TEXT' },
|
|
244
|
+
{ table: 'assistant_turns', name: 'thinking_blocks_count', type: 'INTEGER' },
|
|
245
|
+
{ table: 'tool_calls', name: 'result_preview', type: 'TEXT' },
|
|
246
|
+
];
|
|
247
|
+
for (const col of trajectoryEnhancementColumns) {
|
|
248
|
+
try {
|
|
249
|
+
db.exec(`ALTER TABLE ${col.table} ADD COLUMN ${col.name} ${col.type}`);
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
253
|
+
if (!message.includes('duplicate column name') && !message.includes('no column named')) {
|
|
254
|
+
throw err;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// PRI-406: Partial unique index on canonical_pain_id (non-null only) for dedup
|
|
259
|
+
db.exec(`
|
|
260
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_pain_events_canonical_pain_id
|
|
261
|
+
ON pain_events(canonical_pain_id)
|
|
262
|
+
WHERE canonical_pain_id IS NOT NULL
|
|
263
|
+
`);
|
|
264
|
+
// V2 migration: Add V2 columns to evolution_tasks if they don't exist
|
|
265
|
+
const v2Columns = [
|
|
266
|
+
{ name: 'task_kind', type: 'TEXT' },
|
|
267
|
+
{ name: 'priority', type: 'TEXT' },
|
|
268
|
+
{ name: 'retry_count', type: 'INTEGER' },
|
|
269
|
+
{ name: 'max_retries', type: 'INTEGER' },
|
|
270
|
+
{ name: 'last_error', type: 'TEXT' },
|
|
271
|
+
{ name: 'result_ref', type: 'TEXT' },
|
|
272
|
+
];
|
|
273
|
+
for (const col of v2Columns) {
|
|
274
|
+
const rows = db.prepare(`PRAGMA table_info(evolution_tasks)`).all();
|
|
275
|
+
const exists = rows.some((row) => {
|
|
276
|
+
if (typeof row !== 'object' || row === null)
|
|
277
|
+
return false;
|
|
278
|
+
// Use type guard predicate to narrow without `as` (rc-2-no-as-bypass)
|
|
279
|
+
return hasNameField(row) && row.name === col.name;
|
|
280
|
+
});
|
|
281
|
+
if (!exists) {
|
|
282
|
+
db.exec(`ALTER TABLE evolution_tasks ADD COLUMN ${col.name} ${col.type}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
db.exec(`
|
|
286
|
+
CREATE VIEW IF NOT EXISTS v_error_clusters AS
|
|
287
|
+
SELECT tool_name, COALESCE(error_type, 'unknown') AS error_type, COUNT(*) AS occurrences
|
|
288
|
+
FROM tool_calls
|
|
289
|
+
WHERE outcome = 'failure'
|
|
290
|
+
GROUP BY tool_name, COALESCE(error_type, 'unknown')
|
|
291
|
+
ORDER BY occurrences DESC;
|
|
292
|
+
CREATE VIEW IF NOT EXISTS v_principle_effectiveness AS
|
|
293
|
+
SELECT event_type, COUNT(*) AS total
|
|
294
|
+
FROM principle_events
|
|
295
|
+
GROUP BY event_type
|
|
296
|
+
ORDER BY total DESC;
|
|
297
|
+
CREATE VIEW IF NOT EXISTS v_sample_queue AS
|
|
298
|
+
SELECT review_status, COUNT(*) AS total
|
|
299
|
+
FROM correction_samples
|
|
300
|
+
GROUP BY review_status;
|
|
301
|
+
CREATE INDEX IF NOT EXISTS idx_assistant_turns_session_id ON assistant_turns(session_id);
|
|
302
|
+
CREATE INDEX IF NOT EXISTS idx_assistant_turns_created_at ON assistant_turns(created_at);
|
|
303
|
+
CREATE INDEX IF NOT EXISTS idx_assistant_turns_provider_model ON assistant_turns(provider, model);
|
|
304
|
+
CREATE INDEX IF NOT EXISTS idx_user_turns_session_id ON user_turns(session_id);
|
|
305
|
+
CREATE INDEX IF NOT EXISTS idx_tool_calls_session_id ON tool_calls(session_id);
|
|
306
|
+
CREATE INDEX IF NOT EXISTS idx_tool_calls_created_at ON tool_calls(created_at);
|
|
307
|
+
CREATE INDEX IF NOT EXISTS idx_pain_events_session_id ON pain_events(session_id);
|
|
308
|
+
CREATE INDEX IF NOT EXISTS idx_correction_samples_review_status ON correction_samples(review_status);
|
|
309
|
+
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_trace_id ON evolution_tasks(trace_id);
|
|
310
|
+
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_status ON evolution_tasks(status);
|
|
311
|
+
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_created_at ON evolution_tasks(created_at);
|
|
312
|
+
CREATE INDEX IF NOT EXISTS idx_evolution_events_trace_id ON evolution_events(trace_id);
|
|
313
|
+
CREATE INDEX IF NOT EXISTS idx_evolution_events_created_at ON evolution_events(created_at);
|
|
314
|
+
`);
|
|
315
|
+
return { tables, warnings };
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Initialize trajectory.db schema at the given workspace directory.
|
|
319
|
+
*
|
|
320
|
+
* Opens trajectory.db in write mode, applies the full schema (tables + indexes + views +
|
|
321
|
+
* migrations), then closes the DB. Does NOT run importLegacyArtifacts() or
|
|
322
|
+
* pruneUnreferencedBlobs() — those are runtime side-effects of TrajectoryDatabase
|
|
323
|
+
* construction and are not needed for pure initialization.
|
|
324
|
+
*
|
|
325
|
+
* Idempotent: safe to call on an existing DB; all CREATE statements use IF NOT EXISTS.
|
|
326
|
+
*
|
|
327
|
+
* @returns list of created/verified table names and any warnings
|
|
328
|
+
*/
|
|
329
|
+
export function initTrajectorySchema(workspaceDir) {
|
|
330
|
+
const resolvedDir = path.resolve(workspaceDir);
|
|
331
|
+
const stateDir = resolvePdPath(resolvedDir, 'STATE_DIR');
|
|
332
|
+
const blobDir = resolvePdPath(resolvedDir, 'TRAJECTORY_BLOBS_DIR');
|
|
333
|
+
const exportDir = resolvePdPath(resolvedDir, 'EXPORTS_DIR');
|
|
334
|
+
const dbPath = resolvePdPath(resolvedDir, 'TRAJECTORY_DB');
|
|
335
|
+
fs.mkdirSync(stateDir, { recursive: true });
|
|
336
|
+
fs.mkdirSync(blobDir, { recursive: true });
|
|
337
|
+
fs.mkdirSync(exportDir, { recursive: true });
|
|
338
|
+
const db = new Database(dbPath);
|
|
339
|
+
try {
|
|
340
|
+
db.pragma('journal_mode = WAL');
|
|
341
|
+
db.pragma('foreign_keys = ON');
|
|
342
|
+
db.pragma('synchronous = NORMAL');
|
|
343
|
+
db.pragma(`busy_timeout = ${DEFAULT_BUSY_TIMEOUT_MS}`);
|
|
344
|
+
const result = applyTrajectorySchema(db);
|
|
345
|
+
// Record schema version (same logic as TrajectoryDatabase.initSchema)
|
|
346
|
+
const row = db.prepare('SELECT version FROM schema_version LIMIT 1').get();
|
|
347
|
+
if (!row) {
|
|
348
|
+
db.prepare('INSERT INTO schema_version (version) VALUES (?)').run(SCHEMA_VERSION);
|
|
349
|
+
}
|
|
350
|
+
else if (row.version !== SCHEMA_VERSION) {
|
|
351
|
+
db.prepare('UPDATE schema_version SET version = ?').run(SCHEMA_VERSION);
|
|
352
|
+
}
|
|
353
|
+
return result;
|
|
354
|
+
}
|
|
355
|
+
finally {
|
|
356
|
+
db.close();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
22
359
|
function nowIso() {
|
|
23
360
|
return new Date().toISOString();
|
|
24
361
|
}
|
|
@@ -862,261 +1199,13 @@ export class TrajectoryDatabase {
|
|
|
862
1199
|
return this.pruneUnreferencedBlobs();
|
|
863
1200
|
}
|
|
864
1201
|
initSchema() {
|
|
865
|
-
this.db
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
CREATE TABLE IF NOT EXISTS sessions (
|
|
872
|
-
session_id TEXT PRIMARY KEY,
|
|
873
|
-
started_at TEXT NOT NULL,
|
|
874
|
-
updated_at TEXT NOT NULL
|
|
875
|
-
);
|
|
876
|
-
CREATE TABLE IF NOT EXISTS assistant_turns (
|
|
877
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
878
|
-
session_id TEXT NOT NULL,
|
|
879
|
-
run_id TEXT NOT NULL,
|
|
880
|
-
provider TEXT NOT NULL,
|
|
881
|
-
model TEXT NOT NULL,
|
|
882
|
-
raw_text TEXT,
|
|
883
|
-
sanitized_text TEXT NOT NULL,
|
|
884
|
-
usage_json TEXT NOT NULL,
|
|
885
|
-
empathy_signal_json TEXT NOT NULL,
|
|
886
|
-
blob_ref TEXT,
|
|
887
|
-
raw_excerpt TEXT,
|
|
888
|
-
created_at TEXT NOT NULL
|
|
889
|
-
);
|
|
890
|
-
CREATE TABLE IF NOT EXISTS user_turns (
|
|
891
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
892
|
-
session_id TEXT NOT NULL,
|
|
893
|
-
turn_index INTEGER NOT NULL,
|
|
894
|
-
raw_text TEXT,
|
|
895
|
-
blob_ref TEXT,
|
|
896
|
-
raw_excerpt TEXT,
|
|
897
|
-
correction_detected INTEGER NOT NULL DEFAULT 0,
|
|
898
|
-
correction_cue TEXT,
|
|
899
|
-
references_assistant_turn_id INTEGER,
|
|
900
|
-
created_at TEXT NOT NULL
|
|
901
|
-
);
|
|
902
|
-
CREATE TABLE IF NOT EXISTS tool_calls (
|
|
903
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
904
|
-
session_id TEXT NOT NULL,
|
|
905
|
-
tool_name TEXT NOT NULL,
|
|
906
|
-
outcome TEXT NOT NULL,
|
|
907
|
-
duration_ms INTEGER,
|
|
908
|
-
exit_code INTEGER,
|
|
909
|
-
error_type TEXT,
|
|
910
|
-
error_message TEXT,
|
|
911
|
-
gfi_before REAL,
|
|
912
|
-
gfi_after REAL,
|
|
913
|
-
params_json TEXT NOT NULL,
|
|
914
|
-
created_at TEXT NOT NULL
|
|
915
|
-
);
|
|
916
|
-
CREATE TABLE IF NOT EXISTS pain_events (
|
|
917
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
918
|
-
session_id TEXT NOT NULL,
|
|
919
|
-
source TEXT NOT NULL,
|
|
920
|
-
score REAL NOT NULL,
|
|
921
|
-
reason TEXT,
|
|
922
|
-
severity TEXT,
|
|
923
|
-
origin TEXT,
|
|
924
|
-
confidence REAL,
|
|
925
|
-
text TEXT,
|
|
926
|
-
created_at TEXT NOT NULL
|
|
927
|
-
);
|
|
928
|
-
CREATE TABLE IF NOT EXISTS gate_blocks (
|
|
929
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
930
|
-
session_id TEXT,
|
|
931
|
-
tool_name TEXT NOT NULL,
|
|
932
|
-
file_path TEXT,
|
|
933
|
-
reason TEXT NOT NULL,
|
|
934
|
-
plan_status TEXT,
|
|
935
|
-
created_at TEXT NOT NULL
|
|
936
|
-
);
|
|
937
|
-
CREATE TABLE IF NOT EXISTS trust_changes (
|
|
938
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
939
|
-
session_id TEXT,
|
|
940
|
-
previous_score REAL NOT NULL,
|
|
941
|
-
new_score REAL NOT NULL,
|
|
942
|
-
delta REAL NOT NULL,
|
|
943
|
-
reason TEXT NOT NULL,
|
|
944
|
-
created_at TEXT NOT NULL
|
|
945
|
-
);
|
|
946
|
-
CREATE TABLE IF NOT EXISTS principle_events (
|
|
947
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
948
|
-
principle_id TEXT,
|
|
949
|
-
event_type TEXT NOT NULL,
|
|
950
|
-
payload_json TEXT NOT NULL,
|
|
951
|
-
created_at TEXT NOT NULL
|
|
952
|
-
);
|
|
953
|
-
CREATE TABLE IF NOT EXISTS task_outcomes (
|
|
954
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
955
|
-
session_id TEXT NOT NULL,
|
|
956
|
-
task_id TEXT,
|
|
957
|
-
outcome TEXT NOT NULL,
|
|
958
|
-
summary TEXT,
|
|
959
|
-
principle_ids_json TEXT NOT NULL,
|
|
960
|
-
created_at TEXT NOT NULL
|
|
961
|
-
);
|
|
962
|
-
CREATE TABLE IF NOT EXISTS correction_samples (
|
|
963
|
-
sample_id TEXT PRIMARY KEY,
|
|
964
|
-
session_id TEXT NOT NULL,
|
|
965
|
-
bad_assistant_turn_id INTEGER NOT NULL,
|
|
966
|
-
user_correction_turn_id INTEGER NOT NULL,
|
|
967
|
-
recovery_tool_span_json TEXT NOT NULL,
|
|
968
|
-
diff_excerpt TEXT NOT NULL,
|
|
969
|
-
principle_ids_json TEXT NOT NULL,
|
|
970
|
-
quality_score REAL NOT NULL,
|
|
971
|
-
review_status TEXT NOT NULL,
|
|
972
|
-
export_mode TEXT NOT NULL,
|
|
973
|
-
created_at TEXT NOT NULL,
|
|
974
|
-
updated_at TEXT NOT NULL
|
|
975
|
-
);
|
|
976
|
-
CREATE TABLE IF NOT EXISTS sample_reviews (
|
|
977
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
978
|
-
sample_id TEXT NOT NULL,
|
|
979
|
-
review_status TEXT NOT NULL,
|
|
980
|
-
note TEXT,
|
|
981
|
-
created_at TEXT NOT NULL
|
|
982
|
-
);
|
|
983
|
-
CREATE TABLE IF NOT EXISTS exports_audit (
|
|
984
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
985
|
-
export_kind TEXT NOT NULL,
|
|
986
|
-
mode TEXT NOT NULL,
|
|
987
|
-
approved_only INTEGER NOT NULL,
|
|
988
|
-
file_path TEXT NOT NULL,
|
|
989
|
-
row_count INTEGER NOT NULL,
|
|
990
|
-
created_at TEXT NOT NULL
|
|
991
|
-
);
|
|
992
|
-
CREATE TABLE IF NOT EXISTS evolution_tasks (
|
|
993
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
994
|
-
task_id TEXT UNIQUE NOT NULL,
|
|
995
|
-
trace_id TEXT NOT NULL,
|
|
996
|
-
source TEXT NOT NULL,
|
|
997
|
-
reason TEXT,
|
|
998
|
-
score INTEGER DEFAULT 0,
|
|
999
|
-
status TEXT DEFAULT 'pending',
|
|
1000
|
-
enqueued_at TEXT,
|
|
1001
|
-
started_at TEXT,
|
|
1002
|
-
completed_at TEXT,
|
|
1003
|
-
resolution TEXT,
|
|
1004
|
-
created_at TEXT NOT NULL,
|
|
1005
|
-
updated_at TEXT NOT NULL
|
|
1006
|
-
);
|
|
1007
|
-
CREATE TABLE IF NOT EXISTS evolution_events (
|
|
1008
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1009
|
-
trace_id TEXT NOT NULL,
|
|
1010
|
-
task_id TEXT,
|
|
1011
|
-
stage TEXT NOT NULL,
|
|
1012
|
-
level TEXT DEFAULT 'info',
|
|
1013
|
-
message TEXT NOT NULL,
|
|
1014
|
-
summary TEXT,
|
|
1015
|
-
metadata_json TEXT,
|
|
1016
|
-
created_at TEXT NOT NULL
|
|
1017
|
-
);
|
|
1018
|
-
`);
|
|
1019
|
-
// Migration: Add text column to pain_events if it doesn't exist (MEM-01)
|
|
1020
|
-
// SQLite doesn't support IF NOT EXISTS for ADD COLUMN, so we use try/catch
|
|
1021
|
-
try {
|
|
1022
|
-
this.db.exec(`ALTER TABLE pain_events ADD COLUMN text TEXT`);
|
|
1023
|
-
}
|
|
1024
|
-
catch (err) {
|
|
1025
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
1026
|
-
if (!message.includes('duplicate column name') && !message.includes('no column named')) {
|
|
1027
|
-
// Re-throw unexpected errors — silently swallowing migration failures is dangerous
|
|
1028
|
-
throw err;
|
|
1029
|
-
}
|
|
1202
|
+
const result = applyTrajectorySchema(this.db);
|
|
1203
|
+
// Best-effort: surface any warnings from schema initialization (rc-9: no silent fallback).
|
|
1204
|
+
// Warnings include failed migrations or view creation issues; surfaced via console.info
|
|
1205
|
+
// because TrajectoryDatabase does not have a warnings buffer at this layer.
|
|
1206
|
+
for (const w of result.warnings) {
|
|
1207
|
+
console.info(`[PD:TrajectoryDatabase] schema init warning: ${w}`);
|
|
1030
1208
|
}
|
|
1031
|
-
// PRI-406: Add canonical_pain_id and runtime_task_id columns to pain_events
|
|
1032
|
-
for (const col of [
|
|
1033
|
-
{ name: 'canonical_pain_id', type: 'TEXT' },
|
|
1034
|
-
{ name: 'runtime_task_id', type: 'TEXT' },
|
|
1035
|
-
]) {
|
|
1036
|
-
try {
|
|
1037
|
-
this.db.exec(`ALTER TABLE pain_events ADD COLUMN ${col.name} ${col.type}`);
|
|
1038
|
-
}
|
|
1039
|
-
catch (err) {
|
|
1040
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
1041
|
-
if (!message.includes('duplicate column name') && !message.includes('no column named')) {
|
|
1042
|
-
throw err;
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
// Trajectory enhancement: add stop_reason, thinking_blocks_count, result_preview
|
|
1047
|
-
const trajectoryEnhancementColumns = [
|
|
1048
|
-
{ table: 'assistant_turns', name: 'stop_reason', type: 'TEXT' },
|
|
1049
|
-
{ table: 'assistant_turns', name: 'thinking_blocks_count', type: 'INTEGER' },
|
|
1050
|
-
{ table: 'tool_calls', name: 'result_preview', type: 'TEXT' },
|
|
1051
|
-
];
|
|
1052
|
-
for (const col of trajectoryEnhancementColumns) {
|
|
1053
|
-
try {
|
|
1054
|
-
this.db.exec(`ALTER TABLE ${col.table} ADD COLUMN ${col.name} ${col.type}`);
|
|
1055
|
-
}
|
|
1056
|
-
catch (err) {
|
|
1057
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
1058
|
-
if (!message.includes('duplicate column name') && !message.includes('no column named')) {
|
|
1059
|
-
throw err;
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
// PRI-406: Partial unique index on canonical_pain_id (non-null only) for dedup
|
|
1064
|
-
this.db.exec(`
|
|
1065
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_pain_events_canonical_pain_id
|
|
1066
|
-
ON pain_events(canonical_pain_id)
|
|
1067
|
-
WHERE canonical_pain_id IS NOT NULL
|
|
1068
|
-
`);
|
|
1069
|
-
// pain_events_fts FTS5 virtual table creation removed (PRI-451 Wave 1):
|
|
1070
|
-
// the only reader (searchPainEvents) was dead code. Existing DBs keep the
|
|
1071
|
-
// orphan table harmlessly (CREATE was IF NOT EXISTS); new DBs skip it.
|
|
1072
|
-
// V2 migration: Add V2 columns to evolution_tasks if they don't exist
|
|
1073
|
-
// SQLite does not support IF NOT EXISTS for ADD COLUMN, so we must check manually
|
|
1074
|
-
// before each ALTER to avoid "duplicate column name" errors on existing DBs
|
|
1075
|
-
const v2Columns = [
|
|
1076
|
-
{ name: 'task_kind', type: 'TEXT' },
|
|
1077
|
-
{ name: 'priority', type: 'TEXT' },
|
|
1078
|
-
{ name: 'retry_count', type: 'INTEGER' },
|
|
1079
|
-
{ name: 'max_retries', type: 'INTEGER' },
|
|
1080
|
-
{ name: 'last_error', type: 'TEXT' },
|
|
1081
|
-
{ name: 'result_ref', type: 'TEXT' },
|
|
1082
|
-
];
|
|
1083
|
-
for (const col of v2Columns) {
|
|
1084
|
-
const exists = this.db.prepare(`PRAGMA table_info(evolution_tasks)`).all()
|
|
1085
|
-
.some((row) => row.name === col.name);
|
|
1086
|
-
if (!exists) {
|
|
1087
|
-
this.db.exec(`ALTER TABLE evolution_tasks ADD COLUMN ${col.name} ${col.type}`);
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
this.db.exec(`
|
|
1091
|
-
CREATE VIEW IF NOT EXISTS v_error_clusters AS
|
|
1092
|
-
SELECT tool_name, COALESCE(error_type, 'unknown') AS error_type, COUNT(*) AS occurrences
|
|
1093
|
-
FROM tool_calls
|
|
1094
|
-
WHERE outcome = 'failure'
|
|
1095
|
-
GROUP BY tool_name, COALESCE(error_type, 'unknown')
|
|
1096
|
-
ORDER BY occurrences DESC;
|
|
1097
|
-
CREATE VIEW IF NOT EXISTS v_principle_effectiveness AS
|
|
1098
|
-
SELECT event_type, COUNT(*) AS total
|
|
1099
|
-
FROM principle_events
|
|
1100
|
-
GROUP BY event_type
|
|
1101
|
-
ORDER BY total DESC;
|
|
1102
|
-
CREATE VIEW IF NOT EXISTS v_sample_queue AS
|
|
1103
|
-
SELECT review_status, COUNT(*) AS total
|
|
1104
|
-
FROM correction_samples
|
|
1105
|
-
GROUP BY review_status;
|
|
1106
|
-
CREATE INDEX IF NOT EXISTS idx_assistant_turns_session_id ON assistant_turns(session_id);
|
|
1107
|
-
CREATE INDEX IF NOT EXISTS idx_assistant_turns_created_at ON assistant_turns(created_at);
|
|
1108
|
-
CREATE INDEX IF NOT EXISTS idx_assistant_turns_provider_model ON assistant_turns(provider, model);
|
|
1109
|
-
CREATE INDEX IF NOT EXISTS idx_user_turns_session_id ON user_turns(session_id);
|
|
1110
|
-
CREATE INDEX IF NOT EXISTS idx_tool_calls_session_id ON tool_calls(session_id);
|
|
1111
|
-
CREATE INDEX IF NOT EXISTS idx_tool_calls_created_at ON tool_calls(created_at);
|
|
1112
|
-
CREATE INDEX IF NOT EXISTS idx_pain_events_session_id ON pain_events(session_id);
|
|
1113
|
-
CREATE INDEX IF NOT EXISTS idx_correction_samples_review_status ON correction_samples(review_status);
|
|
1114
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_trace_id ON evolution_tasks(trace_id);
|
|
1115
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_status ON evolution_tasks(status);
|
|
1116
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_created_at ON evolution_tasks(created_at);
|
|
1117
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_events_trace_id ON evolution_events(trace_id);
|
|
1118
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_events_created_at ON evolution_events(created_at);
|
|
1119
|
-
`);
|
|
1120
1209
|
const row = this.db.prepare('SELECT version FROM schema_version LIMIT 1').get();
|
|
1121
1210
|
this.migrateSchema(row?.version);
|
|
1122
1211
|
if (!row) {
|
package/dist/index.d.ts
CHANGED
|
@@ -47,4 +47,6 @@ declare const plugin: {
|
|
|
47
47
|
};
|
|
48
48
|
export { PrincipleTreeLedgerAdapter } from '@principles/core/runtime-v2';
|
|
49
49
|
export { loadFeatureFlagFromWorkspace, isRecord };
|
|
50
|
+
export { initTrajectorySchema } from './core/trajectory.js';
|
|
51
|
+
export { initWorkflowSchema } from './service/subagent-workflow/workflow-store.js';
|
|
50
52
|
export default plugin;
|
package/dist/index.js
CHANGED
|
@@ -747,4 +747,9 @@ const plugin = {
|
|
|
747
747
|
export { PrincipleTreeLedgerAdapter } from '@principles/core/runtime-v2';
|
|
748
748
|
/* istanbul ignore next — test exports for evolution worker gate */
|
|
749
749
|
export { loadFeatureFlagFromWorkspace, isRecord };
|
|
750
|
+
// Schema initialization exports for `pd runtime init` (unified DB init).
|
|
751
|
+
// These functions open the DB in write mode, apply the full schema, and close the DB.
|
|
752
|
+
// They do NOT run runtime side-effects (importLegacyArtifacts, pruneUnreferencedBlobs).
|
|
753
|
+
export { initTrajectorySchema } from './core/trajectory.js';
|
|
754
|
+
export { initWorkflowSchema } from './service/subagent-workflow/workflow-store.js';
|
|
750
755
|
export default plugin;
|
|
@@ -28,3 +28,17 @@ export declare class WorkflowStore {
|
|
|
28
28
|
recordDuration(workflowId: string, durationMs: number): void;
|
|
29
29
|
getCompletionDurations(workflowType: string, limit?: number): number[];
|
|
30
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Initialize subagent_workflows.db schema at the given workspace directory.
|
|
33
|
+
*
|
|
34
|
+
* Opens the DB in write mode, applies the full schema (tables + indexes + migrations),
|
|
35
|
+
* then closes the DB. Used by `pd runtime init` for unified DB initialization.
|
|
36
|
+
*
|
|
37
|
+
* Idempotent: safe to call on an existing DB; all CREATE statements use IF NOT EXISTS.
|
|
38
|
+
*
|
|
39
|
+
* @returns list of created/verified table names and any warnings
|
|
40
|
+
*/
|
|
41
|
+
export declare function initWorkflowSchema(workspaceDir: string): {
|
|
42
|
+
tables: string[];
|
|
43
|
+
warnings: string[];
|
|
44
|
+
};
|