moflo 4.5.0 → 4.6.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.
Files changed (47) hide show
  1. package/package.json +1 -1
  2. package/src/@claude-flow/cli/dist/src/commands/appliance.js +12 -12
  3. package/src/@claude-flow/cli/dist/src/commands/benchmark.js +2 -2
  4. package/src/@claude-flow/cli/dist/src/commands/claims.js +1 -1
  5. package/src/@claude-flow/cli/dist/src/commands/config.js +1 -1
  6. package/src/@claude-flow/cli/dist/src/commands/daemon.js +25 -3
  7. package/src/@claude-flow/cli/dist/src/commands/deployment.js +1 -1
  8. package/src/@claude-flow/cli/dist/src/commands/doctor.js +23 -6
  9. package/src/@claude-flow/cli/dist/src/commands/embeddings.js +1 -1
  10. package/src/@claude-flow/cli/dist/src/commands/hooks.js +1 -1
  11. package/src/@claude-flow/cli/dist/src/commands/init.js +14 -12
  12. package/src/@claude-flow/cli/dist/src/commands/neural.js +1 -1
  13. package/src/@claude-flow/cli/dist/src/commands/orc.d.ts +2 -2
  14. package/src/@claude-flow/cli/dist/src/commands/orc.js +12 -12
  15. package/src/@claude-flow/cli/dist/src/commands/performance.js +1 -1
  16. package/src/@claude-flow/cli/dist/src/commands/plugins.js +1 -1
  17. package/src/@claude-flow/cli/dist/src/commands/providers.js +1 -1
  18. package/src/@claude-flow/cli/dist/src/commands/security.js +1 -1
  19. package/src/@claude-flow/cli/dist/src/commands/start.js +11 -11
  20. package/src/@claude-flow/cli/dist/src/commands/status.js +3 -3
  21. package/src/@claude-flow/cli/dist/src/commands/transfer-store.js +1 -1
  22. package/src/@claude-flow/cli/dist/src/config/moflo-config.d.ts +30 -0
  23. package/src/@claude-flow/cli/dist/src/config/moflo-config.js +103 -7
  24. package/src/@claude-flow/cli/dist/src/index.d.ts +1 -1
  25. package/src/@claude-flow/cli/dist/src/index.js +3 -3
  26. package/src/@claude-flow/cli/dist/src/init/claudemd-generator.js +1 -1
  27. package/src/@claude-flow/cli/dist/src/init/executor.js +9 -12
  28. package/src/@claude-flow/cli/dist/src/init/helpers-generator.js +640 -640
  29. package/src/@claude-flow/cli/dist/src/init/moflo-init.js +646 -192
  30. package/src/@claude-flow/cli/dist/src/init/settings-generator.js +7 -12
  31. package/src/@claude-flow/cli/dist/src/init/statusline-generator.d.ts +1 -1
  32. package/src/@claude-flow/cli/dist/src/init/statusline-generator.js +784 -784
  33. package/src/@claude-flow/cli/dist/src/mcp-tools/agentdb-tools.js +12 -12
  34. package/src/@claude-flow/cli/dist/src/mcp-tools/hooks-tools.js +122 -66
  35. package/src/@claude-flow/cli/dist/src/memory/intelligence.js +5 -1
  36. package/src/@claude-flow/cli/dist/src/memory/memory-initializer.d.ts +1 -1
  37. package/src/@claude-flow/cli/dist/src/memory/memory-initializer.js +371 -371
  38. package/src/@claude-flow/cli/dist/src/parser.d.ts +10 -0
  39. package/src/@claude-flow/cli/dist/src/parser.js +49 -3
  40. package/src/@claude-flow/cli/dist/src/plugins/store/discovery.js +1 -1
  41. package/src/@claude-flow/cli/dist/src/runtime/headless.js +30 -30
  42. package/src/@claude-flow/cli/dist/src/services/claim-service.js +1 -1
  43. package/src/@claude-flow/cli/dist/src/services/ruvector-training.js +11 -5
  44. package/src/@claude-flow/cli/dist/src/services/workflow-gate.d.ts +13 -3
  45. package/src/@claude-flow/cli/dist/src/services/workflow-gate.js +73 -5
  46. package/src/@claude-flow/cli/dist/src/types.d.ts +1 -1
  47. package/src/@claude-flow/cli/dist/src/types.js +1 -1
@@ -30,295 +30,295 @@ async function getBridge() {
30
30
  * Enhanced schema with pattern confidence, temporal decay, versioning
31
31
  * Vector embeddings enabled for semantic search
32
32
  */
33
- export const MEMORY_SCHEMA_V3 = `
34
- -- RuFlo V3 Memory Database
35
- -- Version: 3.0.0
36
- -- Features: Pattern learning, vector embeddings, temporal decay, migration tracking
37
-
38
- PRAGMA journal_mode = WAL;
39
- PRAGMA synchronous = NORMAL;
40
- PRAGMA foreign_keys = ON;
41
-
42
- -- ============================================
43
- -- CORE MEMORY TABLES
44
- -- ============================================
45
-
46
- -- Memory entries (main storage)
47
- CREATE TABLE IF NOT EXISTS memory_entries (
48
- id TEXT PRIMARY KEY,
49
- key TEXT NOT NULL,
50
- namespace TEXT DEFAULT 'default',
51
- content TEXT NOT NULL,
52
- type TEXT DEFAULT 'semantic' CHECK(type IN ('semantic', 'episodic', 'procedural', 'working', 'pattern')),
53
-
54
- -- Vector embedding for semantic search (stored as JSON array)
55
- embedding TEXT,
56
- embedding_model TEXT DEFAULT 'local',
57
- embedding_dimensions INTEGER,
58
-
59
- -- Metadata
60
- tags TEXT, -- JSON array
61
- metadata TEXT, -- JSON object
62
- owner_id TEXT,
63
-
64
- -- Timestamps
65
- created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
66
- updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
67
- expires_at INTEGER,
68
- last_accessed_at INTEGER,
69
-
70
- -- Access tracking for hot/cold detection
71
- access_count INTEGER DEFAULT 0,
72
-
73
- -- Status
74
- status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deleted')),
75
-
76
- UNIQUE(namespace, key)
77
- );
78
-
79
- -- Indexes for memory entries
80
- CREATE INDEX IF NOT EXISTS idx_memory_namespace ON memory_entries(namespace);
81
- CREATE INDEX IF NOT EXISTS idx_memory_key ON memory_entries(key);
82
- CREATE INDEX IF NOT EXISTS idx_memory_type ON memory_entries(type);
83
- CREATE INDEX IF NOT EXISTS idx_memory_status ON memory_entries(status);
84
- CREATE INDEX IF NOT EXISTS idx_memory_created ON memory_entries(created_at);
85
- CREATE INDEX IF NOT EXISTS idx_memory_accessed ON memory_entries(last_accessed_at);
86
- CREATE INDEX IF NOT EXISTS idx_memory_owner ON memory_entries(owner_id);
87
-
88
- -- ============================================
89
- -- PATTERN LEARNING TABLES
90
- -- ============================================
91
-
92
- -- Learned patterns with confidence scoring and versioning
93
- CREATE TABLE IF NOT EXISTS patterns (
94
- id TEXT PRIMARY KEY,
95
-
96
- -- Pattern identification
97
- name TEXT NOT NULL,
98
- pattern_type TEXT NOT NULL CHECK(pattern_type IN (
99
- 'task-routing', 'error-recovery', 'optimization', 'learning',
100
- 'coordination', 'prediction', 'code-pattern', 'workflow'
101
- )),
102
-
103
- -- Pattern definition
104
- condition TEXT NOT NULL, -- Regex or semantic match
105
- action TEXT NOT NULL, -- What to do when pattern matches
106
- description TEXT,
107
-
108
- -- Confidence scoring (0.0 - 1.0)
109
- confidence REAL DEFAULT 0.5,
110
- success_count INTEGER DEFAULT 0,
111
- failure_count INTEGER DEFAULT 0,
112
-
113
- -- Temporal decay
114
- decay_rate REAL DEFAULT 0.01, -- How fast confidence decays
115
- half_life_days INTEGER DEFAULT 30, -- Days until confidence halves without use
116
-
117
- -- Vector embedding for semantic pattern matching
118
- embedding TEXT,
119
- embedding_dimensions INTEGER,
120
-
121
- -- Versioning
122
- version INTEGER DEFAULT 1,
123
- parent_id TEXT REFERENCES patterns(id),
124
-
125
- -- Metadata
126
- tags TEXT, -- JSON array
127
- metadata TEXT, -- JSON object
128
- source TEXT, -- Where the pattern was learned from
129
-
130
- -- Timestamps
131
- created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
132
- updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
133
- last_matched_at INTEGER,
134
- last_success_at INTEGER,
135
- last_failure_at INTEGER,
136
-
137
- -- Status
138
- status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deprecated', 'experimental'))
139
- );
140
-
141
- -- Indexes for patterns
142
- CREATE INDEX IF NOT EXISTS idx_patterns_type ON patterns(pattern_type);
143
- CREATE INDEX IF NOT EXISTS idx_patterns_confidence ON patterns(confidence DESC);
144
- CREATE INDEX IF NOT EXISTS idx_patterns_status ON patterns(status);
145
- CREATE INDEX IF NOT EXISTS idx_patterns_last_matched ON patterns(last_matched_at);
146
-
147
- -- Pattern evolution history (for versioning)
148
- CREATE TABLE IF NOT EXISTS pattern_history (
149
- id INTEGER PRIMARY KEY AUTOINCREMENT,
150
- pattern_id TEXT NOT NULL REFERENCES patterns(id),
151
- version INTEGER NOT NULL,
152
-
153
- -- Snapshot of pattern state
154
- confidence REAL,
155
- success_count INTEGER,
156
- failure_count INTEGER,
157
- condition TEXT,
158
- action TEXT,
159
-
160
- -- What changed
161
- change_type TEXT CHECK(change_type IN ('created', 'updated', 'success', 'failure', 'decay', 'merged', 'split')),
162
- change_reason TEXT,
163
-
164
- created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
165
- );
166
-
167
- CREATE INDEX IF NOT EXISTS idx_pattern_history_pattern ON pattern_history(pattern_id);
168
-
169
- -- ============================================
170
- -- LEARNING & TRAJECTORY TABLES
171
- -- ============================================
172
-
173
- -- Learning trajectories (SONA integration)
174
- CREATE TABLE IF NOT EXISTS trajectories (
175
- id TEXT PRIMARY KEY,
176
- session_id TEXT,
177
-
178
- -- Trajectory state
179
- status TEXT DEFAULT 'active' CHECK(status IN ('active', 'completed', 'failed', 'abandoned')),
180
- verdict TEXT CHECK(verdict IN ('success', 'failure', 'partial', NULL)),
181
-
182
- -- Context
183
- task TEXT,
184
- context TEXT, -- JSON object
185
-
186
- -- Metrics
187
- total_steps INTEGER DEFAULT 0,
188
- total_reward REAL DEFAULT 0,
189
-
190
- -- Timestamps
191
- started_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
192
- ended_at INTEGER,
193
-
194
- -- Reference to extracted pattern (if any)
195
- extracted_pattern_id TEXT REFERENCES patterns(id)
196
- );
197
-
198
- -- Trajectory steps
199
- CREATE TABLE IF NOT EXISTS trajectory_steps (
200
- id INTEGER PRIMARY KEY AUTOINCREMENT,
201
- trajectory_id TEXT NOT NULL REFERENCES trajectories(id),
202
- step_number INTEGER NOT NULL,
203
-
204
- -- Step data
205
- action TEXT NOT NULL,
206
- observation TEXT,
207
- reward REAL DEFAULT 0,
208
-
209
- -- Metadata
210
- metadata TEXT, -- JSON object
211
-
212
- created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
213
- );
214
-
215
- CREATE INDEX IF NOT EXISTS idx_steps_trajectory ON trajectory_steps(trajectory_id);
216
-
217
- -- ============================================
218
- -- MIGRATION STATE TRACKING
219
- -- ============================================
220
-
221
- -- Migration state (for resume capability)
222
- CREATE TABLE IF NOT EXISTS migration_state (
223
- id TEXT PRIMARY KEY,
224
- migration_type TEXT NOT NULL, -- 'v2-to-v3', 'pattern', 'memory', etc.
225
-
226
- -- Progress tracking
227
- status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'in_progress', 'completed', 'failed', 'rolled_back')),
228
- total_items INTEGER DEFAULT 0,
229
- processed_items INTEGER DEFAULT 0,
230
- failed_items INTEGER DEFAULT 0,
231
- skipped_items INTEGER DEFAULT 0,
232
-
233
- -- Current position (for resume)
234
- current_batch INTEGER DEFAULT 0,
235
- last_processed_id TEXT,
236
-
237
- -- Source/destination info
238
- source_path TEXT,
239
- source_type TEXT,
240
- destination_path TEXT,
241
-
242
- -- Backup info
243
- backup_path TEXT,
244
- backup_created_at INTEGER,
245
-
246
- -- Error tracking
247
- last_error TEXT,
248
- errors TEXT, -- JSON array of errors
249
-
250
- -- Timestamps
251
- started_at INTEGER,
252
- completed_at INTEGER,
253
- created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
254
- updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
255
- );
256
-
257
- -- ============================================
258
- -- SESSION MANAGEMENT
259
- -- ============================================
260
-
261
- -- Sessions for context persistence
262
- CREATE TABLE IF NOT EXISTS sessions (
263
- id TEXT PRIMARY KEY,
264
-
265
- -- Session state
266
- state TEXT NOT NULL, -- JSON object with full session state
267
- status TEXT DEFAULT 'active' CHECK(status IN ('active', 'paused', 'completed', 'expired')),
268
-
269
- -- Context
270
- project_path TEXT,
271
- branch TEXT,
272
-
273
- -- Metrics
274
- tasks_completed INTEGER DEFAULT 0,
275
- patterns_learned INTEGER DEFAULT 0,
276
-
277
- -- Timestamps
278
- created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
279
- updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
280
- expires_at INTEGER
281
- );
282
-
283
- -- ============================================
284
- -- VECTOR INDEX METADATA (for HNSW)
285
- -- ============================================
286
-
287
- -- Track HNSW index state
288
- CREATE TABLE IF NOT EXISTS vector_indexes (
289
- id TEXT PRIMARY KEY,
290
- name TEXT NOT NULL UNIQUE,
291
-
292
- -- Index configuration
293
- dimensions INTEGER NOT NULL,
294
- metric TEXT DEFAULT 'cosine' CHECK(metric IN ('cosine', 'euclidean', 'dot')),
295
-
296
- -- HNSW parameters
297
- hnsw_m INTEGER DEFAULT 16,
298
- hnsw_ef_construction INTEGER DEFAULT 200,
299
- hnsw_ef_search INTEGER DEFAULT 100,
300
-
301
- -- Quantization
302
- quantization_type TEXT CHECK(quantization_type IN ('none', 'scalar', 'product')),
303
- quantization_bits INTEGER DEFAULT 8,
304
-
305
- -- Statistics
306
- total_vectors INTEGER DEFAULT 0,
307
- last_rebuild_at INTEGER,
308
-
309
- created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
310
- updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
311
- );
312
-
313
- -- ============================================
314
- -- SYSTEM METADATA
315
- -- ============================================
316
-
317
- CREATE TABLE IF NOT EXISTS metadata (
318
- key TEXT PRIMARY KEY,
319
- value TEXT NOT NULL,
320
- updated_at INTEGER DEFAULT (strftime('%s', 'now') * 1000)
321
- );
33
+ export const MEMORY_SCHEMA_V3 = `
34
+ -- MoFlo Memory Database
35
+ -- Version: 3.0.0
36
+ -- Features: Pattern learning, vector embeddings, temporal decay, migration tracking
37
+
38
+ PRAGMA journal_mode = WAL;
39
+ PRAGMA synchronous = NORMAL;
40
+ PRAGMA foreign_keys = ON;
41
+
42
+ -- ============================================
43
+ -- CORE MEMORY TABLES
44
+ -- ============================================
45
+
46
+ -- Memory entries (main storage)
47
+ CREATE TABLE IF NOT EXISTS memory_entries (
48
+ id TEXT PRIMARY KEY,
49
+ key TEXT NOT NULL,
50
+ namespace TEXT DEFAULT 'default',
51
+ content TEXT NOT NULL,
52
+ type TEXT DEFAULT 'semantic' CHECK(type IN ('semantic', 'episodic', 'procedural', 'working', 'pattern')),
53
+
54
+ -- Vector embedding for semantic search (stored as JSON array)
55
+ embedding TEXT,
56
+ embedding_model TEXT DEFAULT 'local',
57
+ embedding_dimensions INTEGER,
58
+
59
+ -- Metadata
60
+ tags TEXT, -- JSON array
61
+ metadata TEXT, -- JSON object
62
+ owner_id TEXT,
63
+
64
+ -- Timestamps
65
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
66
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
67
+ expires_at INTEGER,
68
+ last_accessed_at INTEGER,
69
+
70
+ -- Access tracking for hot/cold detection
71
+ access_count INTEGER DEFAULT 0,
72
+
73
+ -- Status
74
+ status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deleted')),
75
+
76
+ UNIQUE(namespace, key)
77
+ );
78
+
79
+ -- Indexes for memory entries
80
+ CREATE INDEX IF NOT EXISTS idx_memory_namespace ON memory_entries(namespace);
81
+ CREATE INDEX IF NOT EXISTS idx_memory_key ON memory_entries(key);
82
+ CREATE INDEX IF NOT EXISTS idx_memory_type ON memory_entries(type);
83
+ CREATE INDEX IF NOT EXISTS idx_memory_status ON memory_entries(status);
84
+ CREATE INDEX IF NOT EXISTS idx_memory_created ON memory_entries(created_at);
85
+ CREATE INDEX IF NOT EXISTS idx_memory_accessed ON memory_entries(last_accessed_at);
86
+ CREATE INDEX IF NOT EXISTS idx_memory_owner ON memory_entries(owner_id);
87
+
88
+ -- ============================================
89
+ -- PATTERN LEARNING TABLES
90
+ -- ============================================
91
+
92
+ -- Learned patterns with confidence scoring and versioning
93
+ CREATE TABLE IF NOT EXISTS patterns (
94
+ id TEXT PRIMARY KEY,
95
+
96
+ -- Pattern identification
97
+ name TEXT NOT NULL,
98
+ pattern_type TEXT NOT NULL CHECK(pattern_type IN (
99
+ 'task-routing', 'error-recovery', 'optimization', 'learning',
100
+ 'coordination', 'prediction', 'code-pattern', 'workflow'
101
+ )),
102
+
103
+ -- Pattern definition
104
+ condition TEXT NOT NULL, -- Regex or semantic match
105
+ action TEXT NOT NULL, -- What to do when pattern matches
106
+ description TEXT,
107
+
108
+ -- Confidence scoring (0.0 - 1.0)
109
+ confidence REAL DEFAULT 0.5,
110
+ success_count INTEGER DEFAULT 0,
111
+ failure_count INTEGER DEFAULT 0,
112
+
113
+ -- Temporal decay
114
+ decay_rate REAL DEFAULT 0.01, -- How fast confidence decays
115
+ half_life_days INTEGER DEFAULT 30, -- Days until confidence halves without use
116
+
117
+ -- Vector embedding for semantic pattern matching
118
+ embedding TEXT,
119
+ embedding_dimensions INTEGER,
120
+
121
+ -- Versioning
122
+ version INTEGER DEFAULT 1,
123
+ parent_id TEXT REFERENCES patterns(id),
124
+
125
+ -- Metadata
126
+ tags TEXT, -- JSON array
127
+ metadata TEXT, -- JSON object
128
+ source TEXT, -- Where the pattern was learned from
129
+
130
+ -- Timestamps
131
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
132
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
133
+ last_matched_at INTEGER,
134
+ last_success_at INTEGER,
135
+ last_failure_at INTEGER,
136
+
137
+ -- Status
138
+ status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deprecated', 'experimental'))
139
+ );
140
+
141
+ -- Indexes for patterns
142
+ CREATE INDEX IF NOT EXISTS idx_patterns_type ON patterns(pattern_type);
143
+ CREATE INDEX IF NOT EXISTS idx_patterns_confidence ON patterns(confidence DESC);
144
+ CREATE INDEX IF NOT EXISTS idx_patterns_status ON patterns(status);
145
+ CREATE INDEX IF NOT EXISTS idx_patterns_last_matched ON patterns(last_matched_at);
146
+
147
+ -- Pattern evolution history (for versioning)
148
+ CREATE TABLE IF NOT EXISTS pattern_history (
149
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
150
+ pattern_id TEXT NOT NULL REFERENCES patterns(id),
151
+ version INTEGER NOT NULL,
152
+
153
+ -- Snapshot of pattern state
154
+ confidence REAL,
155
+ success_count INTEGER,
156
+ failure_count INTEGER,
157
+ condition TEXT,
158
+ action TEXT,
159
+
160
+ -- What changed
161
+ change_type TEXT CHECK(change_type IN ('created', 'updated', 'success', 'failure', 'decay', 'merged', 'split')),
162
+ change_reason TEXT,
163
+
164
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
165
+ );
166
+
167
+ CREATE INDEX IF NOT EXISTS idx_pattern_history_pattern ON pattern_history(pattern_id);
168
+
169
+ -- ============================================
170
+ -- LEARNING & TRAJECTORY TABLES
171
+ -- ============================================
172
+
173
+ -- Learning trajectories (SONA integration)
174
+ CREATE TABLE IF NOT EXISTS trajectories (
175
+ id TEXT PRIMARY KEY,
176
+ session_id TEXT,
177
+
178
+ -- Trajectory state
179
+ status TEXT DEFAULT 'active' CHECK(status IN ('active', 'completed', 'failed', 'abandoned')),
180
+ verdict TEXT CHECK(verdict IN ('success', 'failure', 'partial', NULL)),
181
+
182
+ -- Context
183
+ task TEXT,
184
+ context TEXT, -- JSON object
185
+
186
+ -- Metrics
187
+ total_steps INTEGER DEFAULT 0,
188
+ total_reward REAL DEFAULT 0,
189
+
190
+ -- Timestamps
191
+ started_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
192
+ ended_at INTEGER,
193
+
194
+ -- Reference to extracted pattern (if any)
195
+ extracted_pattern_id TEXT REFERENCES patterns(id)
196
+ );
197
+
198
+ -- Trajectory steps
199
+ CREATE TABLE IF NOT EXISTS trajectory_steps (
200
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
201
+ trajectory_id TEXT NOT NULL REFERENCES trajectories(id),
202
+ step_number INTEGER NOT NULL,
203
+
204
+ -- Step data
205
+ action TEXT NOT NULL,
206
+ observation TEXT,
207
+ reward REAL DEFAULT 0,
208
+
209
+ -- Metadata
210
+ metadata TEXT, -- JSON object
211
+
212
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
213
+ );
214
+
215
+ CREATE INDEX IF NOT EXISTS idx_steps_trajectory ON trajectory_steps(trajectory_id);
216
+
217
+ -- ============================================
218
+ -- MIGRATION STATE TRACKING
219
+ -- ============================================
220
+
221
+ -- Migration state (for resume capability)
222
+ CREATE TABLE IF NOT EXISTS migration_state (
223
+ id TEXT PRIMARY KEY,
224
+ migration_type TEXT NOT NULL, -- 'v2-to-v3', 'pattern', 'memory', etc.
225
+
226
+ -- Progress tracking
227
+ status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'in_progress', 'completed', 'failed', 'rolled_back')),
228
+ total_items INTEGER DEFAULT 0,
229
+ processed_items INTEGER DEFAULT 0,
230
+ failed_items INTEGER DEFAULT 0,
231
+ skipped_items INTEGER DEFAULT 0,
232
+
233
+ -- Current position (for resume)
234
+ current_batch INTEGER DEFAULT 0,
235
+ last_processed_id TEXT,
236
+
237
+ -- Source/destination info
238
+ source_path TEXT,
239
+ source_type TEXT,
240
+ destination_path TEXT,
241
+
242
+ -- Backup info
243
+ backup_path TEXT,
244
+ backup_created_at INTEGER,
245
+
246
+ -- Error tracking
247
+ last_error TEXT,
248
+ errors TEXT, -- JSON array of errors
249
+
250
+ -- Timestamps
251
+ started_at INTEGER,
252
+ completed_at INTEGER,
253
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
254
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
255
+ );
256
+
257
+ -- ============================================
258
+ -- SESSION MANAGEMENT
259
+ -- ============================================
260
+
261
+ -- Sessions for context persistence
262
+ CREATE TABLE IF NOT EXISTS sessions (
263
+ id TEXT PRIMARY KEY,
264
+
265
+ -- Session state
266
+ state TEXT NOT NULL, -- JSON object with full session state
267
+ status TEXT DEFAULT 'active' CHECK(status IN ('active', 'paused', 'completed', 'expired')),
268
+
269
+ -- Context
270
+ project_path TEXT,
271
+ branch TEXT,
272
+
273
+ -- Metrics
274
+ tasks_completed INTEGER DEFAULT 0,
275
+ patterns_learned INTEGER DEFAULT 0,
276
+
277
+ -- Timestamps
278
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
279
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
280
+ expires_at INTEGER
281
+ );
282
+
283
+ -- ============================================
284
+ -- VECTOR INDEX METADATA (for HNSW)
285
+ -- ============================================
286
+
287
+ -- Track HNSW index state
288
+ CREATE TABLE IF NOT EXISTS vector_indexes (
289
+ id TEXT PRIMARY KEY,
290
+ name TEXT NOT NULL UNIQUE,
291
+
292
+ -- Index configuration
293
+ dimensions INTEGER NOT NULL,
294
+ metric TEXT DEFAULT 'cosine' CHECK(metric IN ('cosine', 'euclidean', 'dot')),
295
+
296
+ -- HNSW parameters
297
+ hnsw_m INTEGER DEFAULT 16,
298
+ hnsw_ef_construction INTEGER DEFAULT 200,
299
+ hnsw_ef_search INTEGER DEFAULT 100,
300
+
301
+ -- Quantization
302
+ quantization_type TEXT CHECK(quantization_type IN ('none', 'scalar', 'product')),
303
+ quantization_bits INTEGER DEFAULT 8,
304
+
305
+ -- Statistics
306
+ total_vectors INTEGER DEFAULT 0,
307
+ last_rebuild_at INTEGER,
308
+
309
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
310
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
311
+ );
312
+
313
+ -- ============================================
314
+ -- SYSTEM METADATA
315
+ -- ============================================
316
+
317
+ CREATE TABLE IF NOT EXISTS metadata (
318
+ key TEXT PRIMARY KEY,
319
+ value TEXT NOT NULL,
320
+ updated_at INTEGER DEFAULT (strftime('%s', 'now') * 1000)
321
+ );
322
322
  `;
323
323
  let hnswIndex = null;
324
324
  let hnswInitializing = false;
@@ -406,11 +406,11 @@ export async function getHNSWIndex(options) {
406
406
  const fileBuffer = fs.readFileSync(dbPath);
407
407
  const sqlDb = new SQL.Database(fileBuffer);
408
408
  // Load all entries with embeddings
409
- const result = sqlDb.exec(`
410
- SELECT id, key, namespace, content, embedding
411
- FROM memory_entries
412
- WHERE status = 'active' AND embedding IS NOT NULL
413
- LIMIT 10000
409
+ const result = sqlDb.exec(`
410
+ SELECT id, key, namespace, content, embedding
411
+ FROM memory_entries
412
+ WHERE status = 'active' AND embedding IS NOT NULL
413
+ LIMIT 10000
414
414
  `);
415
415
  if (result[0]?.values) {
416
416
  for (const row of result[0].values) {
@@ -790,21 +790,21 @@ export function flashAttentionSearch(query, vectors, options = {}) {
790
790
  * Initial metadata to insert after schema creation
791
791
  */
792
792
  export function getInitialMetadata(backend) {
793
- return `
794
- INSERT OR REPLACE INTO metadata (key, value) VALUES
795
- ('schema_version', '3.0.0'),
796
- ('backend', '${backend}'),
797
- ('created_at', '${new Date().toISOString()}'),
798
- ('sql_js', 'true'),
799
- ('vector_embeddings', 'enabled'),
800
- ('pattern_learning', 'enabled'),
801
- ('temporal_decay', 'enabled'),
802
- ('hnsw_indexing', 'enabled');
803
-
804
- -- Create default vector index configuration
805
- INSERT OR IGNORE INTO vector_indexes (id, name, dimensions) VALUES
806
- ('default', 'default', 768),
807
- ('patterns', 'patterns', 768);
793
+ return `
794
+ INSERT OR REPLACE INTO metadata (key, value) VALUES
795
+ ('schema_version', '3.0.0'),
796
+ ('backend', '${backend}'),
797
+ ('created_at', '${new Date().toISOString()}'),
798
+ ('sql_js', 'true'),
799
+ ('vector_embeddings', 'enabled'),
800
+ ('pattern_learning', 'enabled'),
801
+ ('temporal_decay', 'enabled'),
802
+ ('hnsw_indexing', 'enabled');
803
+
804
+ -- Create default vector index configuration
805
+ INSERT OR IGNORE INTO vector_indexes (id, name, dimensions) VALUES
806
+ ('default', 'default', 768),
807
+ ('patterns', 'patterns', 768);
808
808
  `;
809
809
  }
810
810
  /**
@@ -1204,14 +1204,14 @@ export async function applyTemporalDecay(dbPath) {
1204
1204
  const db = new SQL.Database(fileBuffer);
1205
1205
  // Apply decay: confidence *= exp(-decay_rate * days_since_last_use)
1206
1206
  const now = Date.now();
1207
- const decayQuery = `
1208
- UPDATE patterns
1209
- SET
1210
- confidence = confidence * (1.0 - decay_rate * ((? - COALESCE(last_matched_at, created_at)) / 86400000.0)),
1211
- updated_at = ?
1212
- WHERE status = 'active'
1213
- AND confidence > 0.1
1214
- AND (? - COALESCE(last_matched_at, created_at)) > 86400000
1207
+ const decayQuery = `
1208
+ UPDATE patterns
1209
+ SET
1210
+ confidence = confidence * (1.0 - decay_rate * ((? - COALESCE(last_matched_at, created_at)) / 86400000.0)),
1211
+ updated_at = ?
1212
+ WHERE status = 'active'
1213
+ AND confidence > 0.1
1214
+ AND (? - COALESCE(last_matched_at, created_at)) > 86400000
1215
1215
  `;
1216
1216
  db.run(decayQuery, [now, now, now]);
1217
1217
  const changes = db.getRowsModified();
@@ -1599,9 +1599,9 @@ export async function verifyMemoryInit(dbPath, options) {
1599
1599
  const testKey = 'verification_test';
1600
1600
  const testValue = 'This is a verification test entry for memory initialization';
1601
1601
  try {
1602
- db.run(`
1603
- INSERT INTO memory_entries (id, key, namespace, content, type, created_at, updated_at)
1604
- VALUES (?, ?, 'test', ?, 'semantic', ?, ?)
1602
+ db.run(`
1603
+ INSERT INTO memory_entries (id, key, namespace, content, type, created_at, updated_at)
1604
+ VALUES (?, ?, 'test', ?, 'semantic', ?, ?)
1605
1605
  `, [testId, testKey, testValue, Date.now(), Date.now()]);
1606
1606
  tests.push({
1607
1607
  name: 'Write entry',
@@ -1643,10 +1643,10 @@ export async function verifyMemoryInit(dbPath, options) {
1643
1643
  try {
1644
1644
  const { embedding, dimensions, model } = await generateEmbedding(testValue);
1645
1645
  const embeddingJson = JSON.stringify(embedding);
1646
- db.run(`
1647
- UPDATE memory_entries
1648
- SET embedding = ?, embedding_dimensions = ?, embedding_model = ?
1649
- WHERE id = ?
1646
+ db.run(`
1647
+ UPDATE memory_entries
1648
+ SET embedding = ?, embedding_dimensions = ?, embedding_model = ?
1649
+ WHERE id = ?
1650
1650
  `, [embeddingJson, dimensions, model, testId]);
1651
1651
  tests.push({
1652
1652
  name: 'Generate embedding',
@@ -1667,9 +1667,9 @@ export async function verifyMemoryInit(dbPath, options) {
1667
1667
  const patternStart = Date.now();
1668
1668
  try {
1669
1669
  const patternId = `pattern_${Date.now()}`;
1670
- db.run(`
1671
- INSERT INTO patterns (id, name, pattern_type, condition, action, confidence, created_at, updated_at)
1672
- VALUES (?, 'test-pattern', 'task-routing', 'test condition', 'test action', 0.5, ?, ?)
1670
+ db.run(`
1671
+ INSERT INTO patterns (id, name, pattern_type, condition, action, confidence, created_at, updated_at)
1672
+ VALUES (?, 'test-pattern', 'task-routing', 'test condition', 'test action', 0.5, ?, ?)
1673
1673
  `, [patternId, Date.now(), Date.now()]);
1674
1674
  tests.push({
1675
1675
  name: 'Pattern storage',
@@ -1778,15 +1778,15 @@ export async function storeEntry(options) {
1778
1778
  }
1779
1779
  // Insert or update entry (upsert mode uses REPLACE)
1780
1780
  const insertSql = upsert
1781
- ? `INSERT OR REPLACE INTO memory_entries (
1782
- id, key, namespace, content, type,
1783
- embedding, embedding_dimensions, embedding_model,
1784
- tags, metadata, created_at, updated_at, expires_at, status
1781
+ ? `INSERT OR REPLACE INTO memory_entries (
1782
+ id, key, namespace, content, type,
1783
+ embedding, embedding_dimensions, embedding_model,
1784
+ tags, metadata, created_at, updated_at, expires_at, status
1785
1785
  ) VALUES (?, ?, ?, ?, 'semantic', ?, ?, ?, ?, ?, ?, ?, ?, 'active')`
1786
- : `INSERT INTO memory_entries (
1787
- id, key, namespace, content, type,
1788
- embedding, embedding_dimensions, embedding_model,
1789
- tags, metadata, created_at, updated_at, expires_at, status
1786
+ : `INSERT INTO memory_entries (
1787
+ id, key, namespace, content, type,
1788
+ embedding, embedding_dimensions, embedding_model,
1789
+ tags, metadata, created_at, updated_at, expires_at, status
1790
1790
  ) VALUES (?, ?, ?, ?, 'semantic', ?, ?, ?, ?, ?, ?, ?, ?, 'active')`;
1791
1791
  db.run(insertSql, [
1792
1792
  id,
@@ -1873,12 +1873,12 @@ export async function searchEntries(options) {
1873
1873
  const fileBuffer = fs.readFileSync(dbPath);
1874
1874
  const db = new SQL.Database(fileBuffer);
1875
1875
  // Get entries with embeddings
1876
- const entries = db.exec(`
1877
- SELECT id, key, namespace, content, embedding
1878
- FROM memory_entries
1879
- WHERE status = 'active'
1880
- ${namespace !== 'all' ? `AND namespace = '${namespace.replace(/'/g, "''")}'` : ''}
1881
- LIMIT 1000
1876
+ const entries = db.exec(`
1877
+ SELECT id, key, namespace, content, embedding
1878
+ FROM memory_entries
1879
+ WHERE status = 'active'
1880
+ ${namespace !== 'all' ? `AND namespace = '${namespace.replace(/'/g, "''")}'` : ''}
1881
+ LIMIT 1000
1882
1882
  `);
1883
1883
  const results = [];
1884
1884
  if (entries[0]?.values) {
@@ -1985,13 +1985,13 @@ export async function listEntries(options) {
1985
1985
  const countResult = db.exec(countQuery);
1986
1986
  const total = countResult[0]?.values?.[0]?.[0] || 0;
1987
1987
  // Get entries
1988
- const listQuery = `
1989
- SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at
1990
- FROM memory_entries
1991
- WHERE status = 'active'
1992
- ${namespace ? `AND namespace = '${namespace.replace(/'/g, "''")}'` : ''}
1993
- ORDER BY updated_at DESC
1994
- LIMIT ${limit} OFFSET ${offset}
1988
+ const listQuery = `
1989
+ SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at
1990
+ FROM memory_entries
1991
+ WHERE status = 'active'
1992
+ ${namespace ? `AND namespace = '${namespace.replace(/'/g, "''")}'` : ''}
1993
+ ORDER BY updated_at DESC
1994
+ LIMIT ${limit} OFFSET ${offset}
1995
1995
  `;
1996
1996
  const result = db.exec(listQuery);
1997
1997
  const entries = [];
@@ -2048,13 +2048,13 @@ export async function getEntry(options) {
2048
2048
  const fileBuffer = fs.readFileSync(dbPath);
2049
2049
  const db = new SQL.Database(fileBuffer);
2050
2050
  // Find entry by key
2051
- const result = db.exec(`
2052
- SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, tags
2053
- FROM memory_entries
2054
- WHERE status = 'active'
2055
- AND key = '${key.replace(/'/g, "''")}'
2056
- AND namespace = '${namespace.replace(/'/g, "''")}'
2057
- LIMIT 1
2051
+ const result = db.exec(`
2052
+ SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, tags
2053
+ FROM memory_entries
2054
+ WHERE status = 'active'
2055
+ AND key = '${key.replace(/'/g, "''")}'
2056
+ AND namespace = '${namespace.replace(/'/g, "''")}'
2057
+ LIMIT 1
2058
2058
  `);
2059
2059
  if (!result[0]?.values?.[0]) {
2060
2060
  db.close();
@@ -2062,10 +2062,10 @@ export async function getEntry(options) {
2062
2062
  }
2063
2063
  const [id, entryKey, ns, content, embedding, accessCount, createdAt, updatedAt, tagsJson] = result[0].values[0];
2064
2064
  // Update access count
2065
- db.run(`
2066
- UPDATE memory_entries
2067
- SET access_count = access_count + 1, last_accessed_at = strftime('%s', 'now') * 1000
2068
- WHERE id = '${String(id).replace(/'/g, "''")}'
2065
+ db.run(`
2066
+ UPDATE memory_entries
2067
+ SET access_count = access_count + 1, last_accessed_at = strftime('%s', 'now') * 1000
2068
+ WHERE id = '${String(id).replace(/'/g, "''")}'
2069
2069
  `);
2070
2070
  // Save updated database
2071
2071
  const data = db.export();
@@ -2138,12 +2138,12 @@ export async function deleteEntry(options) {
2138
2138
  const fileBuffer = fs.readFileSync(dbPath);
2139
2139
  const db = new SQL.Database(fileBuffer);
2140
2140
  // Check if entry exists first
2141
- const checkResult = db.exec(`
2142
- SELECT id FROM memory_entries
2143
- WHERE status = 'active'
2144
- AND key = '${key.replace(/'/g, "''")}'
2145
- AND namespace = '${namespace.replace(/'/g, "''")}'
2146
- LIMIT 1
2141
+ const checkResult = db.exec(`
2142
+ SELECT id FROM memory_entries
2143
+ WHERE status = 'active'
2144
+ AND key = '${key.replace(/'/g, "''")}'
2145
+ AND namespace = '${namespace.replace(/'/g, "''")}'
2146
+ LIMIT 1
2147
2147
  `);
2148
2148
  if (!checkResult[0]?.values?.[0]) {
2149
2149
  // Get remaining count before closing
@@ -2160,12 +2160,12 @@ export async function deleteEntry(options) {
2160
2160
  };
2161
2161
  }
2162
2162
  // Delete the entry (soft delete by setting status to 'deleted')
2163
- db.run(`
2164
- UPDATE memory_entries
2165
- SET status = 'deleted', updated_at = strftime('%s', 'now') * 1000
2166
- WHERE key = '${key.replace(/'/g, "''")}'
2167
- AND namespace = '${namespace.replace(/'/g, "''")}'
2168
- AND status = 'active'
2163
+ db.run(`
2164
+ UPDATE memory_entries
2165
+ SET status = 'deleted', updated_at = strftime('%s', 'now') * 1000
2166
+ WHERE key = '${key.replace(/'/g, "''")}'
2167
+ AND namespace = '${namespace.replace(/'/g, "''")}'
2168
+ AND status = 'active'
2169
2169
  `);
2170
2170
  // Get remaining count
2171
2171
  const countResult = db.exec(`SELECT COUNT(*) FROM memory_entries WHERE status = 'active'`);