moflo 4.10.24 → 4.10.25
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/.claude/guidance/shipped/moflo-yaml-reference.md +5 -5
- package/.claude/skills/meditate/SKILL.md +2 -2
- package/bin/cli-hooks/statusline.js +12 -8
- package/bin/cli.js +1 -1
- package/bin/hooks.mjs +1 -1
- package/bin/lib/meditate.mjs +1 -0
- package/bin/lib/pii-scrub.mjs +2 -5
- package/dist/src/cli/commands/daemon.js +4 -3
- package/dist/src/cli/commands/doctor-checks-deep.js +2 -2
- package/dist/src/cli/commands/doctor-checks-swarm.js +122 -13
- package/dist/src/cli/commands/hooks.js +7 -23
- package/dist/src/cli/commands/init.js +1 -1
- package/dist/src/cli/commands/mcp.js +2 -1
- package/dist/src/cli/commands/session.js +1 -1
- package/dist/src/cli/commands/start.js +1 -1
- package/dist/src/cli/commands/status.js +1 -1
- package/dist/src/cli/commands/task.js +1 -1
- package/dist/src/cli/commands/update.js +12 -12
- package/dist/src/cli/guidance/analyzer.js +3 -3
- package/dist/src/cli/guidance/gates.js +1 -1
- package/dist/src/cli/guidance/hooks.js +1 -1
- package/dist/src/cli/guidance/meta-governance.js +1 -1
- package/dist/src/cli/hooks/index.js +1 -1
- package/dist/src/cli/hooks/reasoningbank/guidance-provider.js +1 -1
- package/dist/src/cli/hooks/workers/index.js +1 -1
- package/dist/src/cli/hooks/workers/session-hook.js +0 -40
- package/dist/src/cli/index.js +2 -2
- package/dist/src/cli/init/executor.js +36 -20
- package/dist/src/cli/init/mcp-generator.js +10 -8
- package/dist/src/cli/init/settings-generator.js +10 -7
- package/dist/src/cli/init/types.js +2 -2
- package/dist/src/cli/mcp-server.js +2 -1
- package/dist/src/cli/memory/bridge-loader.js +42 -0
- package/dist/src/cli/memory/embedding-model.js +157 -0
- package/dist/src/cli/memory/entries-read.js +380 -0
- package/dist/src/cli/memory/entries-shared.js +73 -0
- package/dist/src/cli/memory/entries-write.js +384 -0
- package/dist/src/cli/memory/hnsw-singleton.js +242 -0
- package/dist/src/cli/memory/init.js +367 -0
- package/dist/src/cli/memory/learnings-overview.js +156 -0
- package/dist/src/cli/memory/memory-initializer.js +37 -2257
- package/dist/src/cli/memory/quantization.js +221 -0
- package/dist/src/cli/memory/schema.js +382 -0
- package/dist/src/cli/memory/verify.js +178 -0
- package/dist/src/cli/movector/index.js +1 -1
- package/dist/src/cli/plugins/store/discovery.js +9 -9
- package/dist/src/cli/{transfer/ipfs/client.js → plugins/store/ipfs-client.js} +4 -1
- package/dist/src/cli/plugins/tests/demo-plugin-store.js +1 -1
- package/dist/src/cli/plugins/tests/standalone-test.js +1 -1
- package/dist/src/cli/runtime/headless.js +5 -4
- package/dist/src/cli/scripts/publish-registry.js +6 -6
- package/dist/src/cli/services/daemon-dashboard.js +108 -7
- package/dist/src/cli/services/daemon-readiness.js +1 -1
- package/dist/src/cli/services/daemon-service.js +1 -1
- package/dist/src/cli/services/env-compat.js +29 -0
- package/dist/src/cli/services/hook-block-hash.js +5 -6
- package/dist/src/cli/services/registry-api.js +1 -1
- package/dist/src/cli/shared/core/config/loader.js +19 -11
- package/dist/src/cli/shared/events/example-usage.js +2 -2
- package/dist/src/cli/shared/events/index.js +1 -1
- package/dist/src/cli/shared/index.js +1 -1
- package/dist/src/cli/shared/mcp/index.js +1 -1
- package/dist/src/cli/shared/mcp/server.js +3 -3
- package/dist/src/cli/shared/plugin-interface.js +1 -1
- package/dist/src/cli/shared/plugins/index.js +1 -1
- package/dist/src/cli/shared/plugins/official/index.js +1 -1
- package/dist/src/cli/shared/security/index.js +1 -1
- package/dist/src/cli/shared/services/v3-progress.service.js +40 -29
- package/dist/src/cli/shared/types.js +1 -1
- package/dist/src/cli/swarm/coordination/swarm-hub.js +1 -1
- package/dist/src/cli/update/index.js +1 -1
- package/dist/src/cli/update/rate-limiter.js +3 -2
- package/dist/src/cli/version.js +1 -1
- package/package.json +2 -2
- package/dist/src/cli/commands/transfer-store.js +0 -428
- package/dist/src/cli/transfer/anonymization/index.js +0 -281
- package/dist/src/cli/transfer/deploy-seraphine.js +0 -205
- package/dist/src/cli/transfer/export.js +0 -113
- package/dist/src/cli/transfer/index.js +0 -31
- package/dist/src/cli/transfer/ipfs/upload.js +0 -411
- package/dist/src/cli/transfer/models/seraphine.js +0 -373
- package/dist/src/cli/transfer/serialization/cfp.js +0 -184
- package/dist/src/cli/transfer/storage/gcs.js +0 -242
- package/dist/src/cli/transfer/storage/index.js +0 -6
- package/dist/src/cli/transfer/store/discovery.js +0 -382
- package/dist/src/cli/transfer/store/download.js +0 -334
- package/dist/src/cli/transfer/store/index.js +0 -153
- package/dist/src/cli/transfer/store/publish.js +0 -294
- package/dist/src/cli/transfer/store/registry.js +0 -285
- package/dist/src/cli/transfer/store/search.js +0 -232
- package/dist/src/cli/transfer/store/tests/standalone-test.js +0 -190
- package/dist/src/cli/transfer/store/types.js +0 -6
- package/dist/src/cli/transfer/test-seraphine.js +0 -105
- package/dist/src/cli/transfer/tests/test-store.js +0 -214
- package/dist/src/cli/transfer/types.js +0 -6
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector quantization + flash-attention batch ops.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from `memory-initializer.ts` (#1203 decomposition) — pure,
|
|
5
|
+
* dependency-free numeric helpers. INT8 quantization (4x memory reduction)
|
|
6
|
+
* and V8-optimized batch similarity / softmax / top-k used by the HNSW and
|
|
7
|
+
* search paths. No I/O, no module state.
|
|
8
|
+
*
|
|
9
|
+
* @module memory/quantization
|
|
10
|
+
*/
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// INT8 VECTOR QUANTIZATION (4x memory reduction)
|
|
13
|
+
// ============================================================================
|
|
14
|
+
/**
|
|
15
|
+
* Quantize a Float32 embedding to Int8 (4x memory reduction)
|
|
16
|
+
* Uses symmetric quantization with scale factor stored per-vector
|
|
17
|
+
*
|
|
18
|
+
* @param embedding - Float32 embedding array
|
|
19
|
+
* @returns Quantized Int8 array with scale factor
|
|
20
|
+
*/
|
|
21
|
+
export function quantizeInt8(embedding) {
|
|
22
|
+
const arr = embedding instanceof Float32Array ? embedding : new Float32Array(embedding);
|
|
23
|
+
// Find min/max for symmetric quantization
|
|
24
|
+
let min = Infinity, max = -Infinity;
|
|
25
|
+
for (let i = 0; i < arr.length; i++) {
|
|
26
|
+
if (arr[i] < min)
|
|
27
|
+
min = arr[i];
|
|
28
|
+
if (arr[i] > max)
|
|
29
|
+
max = arr[i];
|
|
30
|
+
}
|
|
31
|
+
// Symmetric quantization: scale = max(|min|, |max|) / 127
|
|
32
|
+
const absMax = Math.max(Math.abs(min), Math.abs(max));
|
|
33
|
+
const scale = absMax / 127 || 1e-10; // Avoid division by zero
|
|
34
|
+
const zeroPoint = 0; // Symmetric quantization
|
|
35
|
+
// Quantize
|
|
36
|
+
const quantized = new Int8Array(arr.length);
|
|
37
|
+
for (let i = 0; i < arr.length; i++) {
|
|
38
|
+
// Clamp to [-127, 127] to leave room for potential rounding
|
|
39
|
+
const q = Math.round(arr[i] / scale);
|
|
40
|
+
quantized[i] = Math.max(-127, Math.min(127, q));
|
|
41
|
+
}
|
|
42
|
+
return { quantized, scale, zeroPoint };
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Dequantize Int8 back to Float32
|
|
46
|
+
*
|
|
47
|
+
* @param quantized - Int8 quantized array
|
|
48
|
+
* @param scale - Scale factor from quantization
|
|
49
|
+
* @param zeroPoint - Zero point (usually 0 for symmetric)
|
|
50
|
+
* @returns Float32Array
|
|
51
|
+
*/
|
|
52
|
+
export function dequantizeInt8(quantized, scale, zeroPoint = 0) {
|
|
53
|
+
const result = new Float32Array(quantized.length);
|
|
54
|
+
for (let i = 0; i < quantized.length; i++) {
|
|
55
|
+
result[i] = (quantized[i] - zeroPoint) * scale;
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Compute cosine similarity between quantized vectors
|
|
61
|
+
* Faster than dequantizing first
|
|
62
|
+
*/
|
|
63
|
+
export function quantizedCosineSim(a, aScale, b, bScale) {
|
|
64
|
+
if (a.length !== b.length)
|
|
65
|
+
return 0;
|
|
66
|
+
let dot = 0, normA = 0, normB = 0;
|
|
67
|
+
for (let i = 0; i < a.length; i++) {
|
|
68
|
+
dot += a[i] * b[i];
|
|
69
|
+
normA += a[i] * a[i];
|
|
70
|
+
normB += b[i] * b[i];
|
|
71
|
+
}
|
|
72
|
+
// Scales cancel out in cosine similarity for normalized vectors
|
|
73
|
+
const mag = Math.sqrt(normA * normB);
|
|
74
|
+
return mag === 0 ? 0 : dot / mag;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get quantization statistics for an embedding
|
|
78
|
+
*/
|
|
79
|
+
export function getQuantizationStats(embedding) {
|
|
80
|
+
const len = embedding.length;
|
|
81
|
+
const originalBytes = len * 4; // Float32 = 4 bytes
|
|
82
|
+
const quantizedBytes = len + 8; // Int8 = 1 byte + 8 bytes for scale/zeroPoint
|
|
83
|
+
const compressionRatio = originalBytes / quantizedBytes;
|
|
84
|
+
return { originalBytes, quantizedBytes, compressionRatio };
|
|
85
|
+
}
|
|
86
|
+
// ============================================================================
|
|
87
|
+
// FLASH ATTENTION-STYLE BATCH OPERATIONS (V8-Optimized)
|
|
88
|
+
// ============================================================================
|
|
89
|
+
/**
|
|
90
|
+
* Batch cosine similarity - compute query against multiple vectors
|
|
91
|
+
* Optimized for V8 JIT with typed arrays
|
|
92
|
+
* ~50μs per 1000 vectors (384-dim)
|
|
93
|
+
*/
|
|
94
|
+
export function batchCosineSim(query, vectors) {
|
|
95
|
+
const n = vectors.length;
|
|
96
|
+
const scores = new Float32Array(n);
|
|
97
|
+
if (n === 0 || query.length === 0)
|
|
98
|
+
return scores;
|
|
99
|
+
// Pre-compute query norm
|
|
100
|
+
let queryNorm = 0;
|
|
101
|
+
for (let i = 0; i < query.length; i++) {
|
|
102
|
+
queryNorm += query[i] * query[i];
|
|
103
|
+
}
|
|
104
|
+
queryNorm = Math.sqrt(queryNorm);
|
|
105
|
+
if (queryNorm === 0)
|
|
106
|
+
return scores;
|
|
107
|
+
// Compute similarities
|
|
108
|
+
for (let v = 0; v < n; v++) {
|
|
109
|
+
const vec = vectors[v];
|
|
110
|
+
const len = Math.min(query.length, vec.length);
|
|
111
|
+
let dot = 0, vecNorm = 0;
|
|
112
|
+
for (let i = 0; i < len; i++) {
|
|
113
|
+
dot += query[i] * vec[i];
|
|
114
|
+
vecNorm += vec[i] * vec[i];
|
|
115
|
+
}
|
|
116
|
+
vecNorm = Math.sqrt(vecNorm);
|
|
117
|
+
scores[v] = vecNorm === 0 ? 0 : dot / (queryNorm * vecNorm);
|
|
118
|
+
}
|
|
119
|
+
return scores;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Softmax normalization for attention scores
|
|
123
|
+
* Numerically stable implementation
|
|
124
|
+
*/
|
|
125
|
+
export function softmaxAttention(scores, temperature = 1.0) {
|
|
126
|
+
const n = scores.length;
|
|
127
|
+
const result = new Float32Array(n);
|
|
128
|
+
if (n === 0)
|
|
129
|
+
return result;
|
|
130
|
+
// Find max for numerical stability
|
|
131
|
+
let max = scores[0];
|
|
132
|
+
for (let i = 1; i < n; i++) {
|
|
133
|
+
if (scores[i] > max)
|
|
134
|
+
max = scores[i];
|
|
135
|
+
}
|
|
136
|
+
// Compute exp and sum
|
|
137
|
+
let sum = 0;
|
|
138
|
+
for (let i = 0; i < n; i++) {
|
|
139
|
+
result[i] = Math.exp((scores[i] - max) / temperature);
|
|
140
|
+
sum += result[i];
|
|
141
|
+
}
|
|
142
|
+
// Normalize
|
|
143
|
+
if (sum > 0) {
|
|
144
|
+
for (let i = 0; i < n; i++) {
|
|
145
|
+
result[i] /= sum;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Top-K selection with partial sort (O(n + k log k))
|
|
152
|
+
* More efficient than full sort for small k
|
|
153
|
+
*/
|
|
154
|
+
export function topKIndices(scores, k) {
|
|
155
|
+
const n = scores.length;
|
|
156
|
+
if (k >= n) {
|
|
157
|
+
// Return all indices sorted by score
|
|
158
|
+
return Array.from({ length: n }, (_, i) => i)
|
|
159
|
+
.sort((a, b) => scores[b] - scores[a]);
|
|
160
|
+
}
|
|
161
|
+
// Build min-heap of size k
|
|
162
|
+
const heap = [];
|
|
163
|
+
for (let i = 0; i < n; i++) {
|
|
164
|
+
if (heap.length < k) {
|
|
165
|
+
heap.push({ idx: i, score: scores[i] });
|
|
166
|
+
// Bubble up
|
|
167
|
+
let j = heap.length - 1;
|
|
168
|
+
while (j > 0) {
|
|
169
|
+
const parent = Math.floor((j - 1) / 2);
|
|
170
|
+
if (heap[j].score < heap[parent].score) {
|
|
171
|
+
[heap[j], heap[parent]] = [heap[parent], heap[j]];
|
|
172
|
+
j = parent;
|
|
173
|
+
}
|
|
174
|
+
else
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else if (scores[i] > heap[0].score) {
|
|
179
|
+
// Replace min and heapify down
|
|
180
|
+
heap[0] = { idx: i, score: scores[i] };
|
|
181
|
+
let j = 0;
|
|
182
|
+
while (true) {
|
|
183
|
+
const left = 2 * j + 1, right = 2 * j + 2;
|
|
184
|
+
let smallest = j;
|
|
185
|
+
if (left < k && heap[left].score < heap[smallest].score)
|
|
186
|
+
smallest = left;
|
|
187
|
+
if (right < k && heap[right].score < heap[smallest].score)
|
|
188
|
+
smallest = right;
|
|
189
|
+
if (smallest === j)
|
|
190
|
+
break;
|
|
191
|
+
[heap[j], heap[smallest]] = [heap[smallest], heap[j]];
|
|
192
|
+
j = smallest;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Extract and sort descending
|
|
197
|
+
return heap.sort((a, b) => b.score - a.score).map(h => h.idx);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Flash Attention-style search
|
|
201
|
+
* Combines batch similarity, softmax, and top-k in one pass
|
|
202
|
+
* Returns indices and attention weights
|
|
203
|
+
*/
|
|
204
|
+
export function flashAttentionSearch(query, vectors, options = {}) {
|
|
205
|
+
const { k = 10, temperature = 1.0, threshold = 0 } = options;
|
|
206
|
+
// Compute batch similarity
|
|
207
|
+
const scores = batchCosineSim(query, vectors);
|
|
208
|
+
// Get top-k indices
|
|
209
|
+
const indices = topKIndices(scores, k);
|
|
210
|
+
// Filter by threshold
|
|
211
|
+
const filtered = indices.filter(i => scores[i] >= threshold);
|
|
212
|
+
// Extract scores for filtered results
|
|
213
|
+
const topScores = new Float32Array(filtered.length);
|
|
214
|
+
for (let i = 0; i < filtered.length; i++) {
|
|
215
|
+
topScores[i] = scores[filtered[i]];
|
|
216
|
+
}
|
|
217
|
+
// Compute attention weights (softmax over top-k)
|
|
218
|
+
const weights = softmaxAttention(topScores, temperature);
|
|
219
|
+
return { indices: filtered, scores: topScores, weights };
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=quantization.js.map
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory database schema + schema-migration helpers.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from `memory-initializer.ts` (#1203 decomposition). Holds the
|
|
5
|
+
* canonical V3 schema DDL, the initial-metadata seed, the
|
|
6
|
+
* {@link MemoryInitResult} shape, and {@link ensureSchemaColumns} (the
|
|
7
|
+
* add-missing-columns migration older consumer DBs need before a read/write).
|
|
8
|
+
*
|
|
9
|
+
* @module memory/schema
|
|
10
|
+
*/
|
|
11
|
+
import * as fs from 'fs';
|
|
12
|
+
import { errorDetail } from '../shared/utils/error-detail.js';
|
|
13
|
+
import { openDaemonDatabase } from './daemon-backend.js';
|
|
14
|
+
/**
|
|
15
|
+
* Enhanced schema with pattern confidence, temporal decay, versioning
|
|
16
|
+
* Vector embeddings enabled for semantic search
|
|
17
|
+
*/
|
|
18
|
+
export const MEMORY_SCHEMA_V3 = `
|
|
19
|
+
-- MoFlo Memory Database
|
|
20
|
+
-- Version: 3.0.0
|
|
21
|
+
-- Features: Pattern learning, vector embeddings, temporal decay, migration tracking
|
|
22
|
+
|
|
23
|
+
PRAGMA journal_mode = WAL;
|
|
24
|
+
PRAGMA synchronous = NORMAL;
|
|
25
|
+
PRAGMA foreign_keys = ON;
|
|
26
|
+
|
|
27
|
+
-- ============================================
|
|
28
|
+
-- CORE MEMORY TABLES
|
|
29
|
+
-- ============================================
|
|
30
|
+
|
|
31
|
+
-- Memory entries (main storage)
|
|
32
|
+
CREATE TABLE IF NOT EXISTS memory_entries (
|
|
33
|
+
id TEXT PRIMARY KEY,
|
|
34
|
+
key TEXT NOT NULL,
|
|
35
|
+
namespace TEXT DEFAULT 'default',
|
|
36
|
+
content TEXT NOT NULL,
|
|
37
|
+
type TEXT DEFAULT 'semantic' CHECK(type IN ('semantic', 'episodic', 'procedural', 'working', 'pattern')),
|
|
38
|
+
|
|
39
|
+
-- Vector embedding for semantic search (stored as JSON array)
|
|
40
|
+
embedding TEXT,
|
|
41
|
+
embedding_model TEXT DEFAULT 'local',
|
|
42
|
+
embedding_dimensions INTEGER,
|
|
43
|
+
|
|
44
|
+
-- Metadata
|
|
45
|
+
tags TEXT, -- JSON array
|
|
46
|
+
metadata TEXT, -- JSON object
|
|
47
|
+
owner_id TEXT,
|
|
48
|
+
|
|
49
|
+
-- Timestamps
|
|
50
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
51
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
52
|
+
expires_at INTEGER,
|
|
53
|
+
last_accessed_at INTEGER,
|
|
54
|
+
|
|
55
|
+
-- Access tracking for hot/cold detection
|
|
56
|
+
access_count INTEGER DEFAULT 0,
|
|
57
|
+
|
|
58
|
+
-- Status
|
|
59
|
+
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived')),
|
|
60
|
+
|
|
61
|
+
UNIQUE(namespace, key)
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
-- Indexes for memory entries
|
|
65
|
+
CREATE INDEX IF NOT EXISTS idx_memory_namespace ON memory_entries(namespace);
|
|
66
|
+
CREATE INDEX IF NOT EXISTS idx_memory_key ON memory_entries(key);
|
|
67
|
+
CREATE INDEX IF NOT EXISTS idx_memory_type ON memory_entries(type);
|
|
68
|
+
CREATE INDEX IF NOT EXISTS idx_memory_status ON memory_entries(status);
|
|
69
|
+
CREATE INDEX IF NOT EXISTS idx_memory_created ON memory_entries(created_at);
|
|
70
|
+
CREATE INDEX IF NOT EXISTS idx_memory_accessed ON memory_entries(last_accessed_at);
|
|
71
|
+
CREATE INDEX IF NOT EXISTS idx_memory_owner ON memory_entries(owner_id);
|
|
72
|
+
|
|
73
|
+
-- ============================================
|
|
74
|
+
-- PATTERN LEARNING TABLES
|
|
75
|
+
-- ============================================
|
|
76
|
+
|
|
77
|
+
-- Learned patterns with confidence scoring and versioning
|
|
78
|
+
CREATE TABLE IF NOT EXISTS patterns (
|
|
79
|
+
id TEXT PRIMARY KEY,
|
|
80
|
+
|
|
81
|
+
-- Pattern identification
|
|
82
|
+
name TEXT NOT NULL,
|
|
83
|
+
pattern_type TEXT NOT NULL CHECK(pattern_type IN (
|
|
84
|
+
'task-routing', 'error-recovery', 'optimization', 'learning',
|
|
85
|
+
'coordination', 'prediction', 'code-pattern', 'workflow'
|
|
86
|
+
)),
|
|
87
|
+
|
|
88
|
+
-- Pattern definition
|
|
89
|
+
condition TEXT NOT NULL, -- Regex or semantic match
|
|
90
|
+
action TEXT NOT NULL, -- What to do when pattern matches
|
|
91
|
+
description TEXT,
|
|
92
|
+
|
|
93
|
+
-- Confidence scoring (0.0 - 1.0)
|
|
94
|
+
confidence REAL DEFAULT 0.5,
|
|
95
|
+
success_count INTEGER DEFAULT 0,
|
|
96
|
+
failure_count INTEGER DEFAULT 0,
|
|
97
|
+
|
|
98
|
+
-- Temporal decay
|
|
99
|
+
decay_rate REAL DEFAULT 0.01, -- How fast confidence decays
|
|
100
|
+
half_life_days INTEGER DEFAULT 30, -- Days until confidence halves without use
|
|
101
|
+
|
|
102
|
+
-- Vector embedding for semantic pattern matching
|
|
103
|
+
embedding TEXT,
|
|
104
|
+
embedding_dimensions INTEGER,
|
|
105
|
+
|
|
106
|
+
-- Versioning
|
|
107
|
+
version INTEGER DEFAULT 1,
|
|
108
|
+
parent_id TEXT REFERENCES patterns(id),
|
|
109
|
+
|
|
110
|
+
-- Metadata
|
|
111
|
+
tags TEXT, -- JSON array
|
|
112
|
+
metadata TEXT, -- JSON object
|
|
113
|
+
source TEXT, -- Where the pattern was learned from
|
|
114
|
+
|
|
115
|
+
-- Timestamps
|
|
116
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
117
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
118
|
+
last_matched_at INTEGER,
|
|
119
|
+
last_success_at INTEGER,
|
|
120
|
+
last_failure_at INTEGER,
|
|
121
|
+
|
|
122
|
+
-- Status
|
|
123
|
+
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deprecated', 'experimental'))
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
-- Indexes for patterns
|
|
127
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_type ON patterns(pattern_type);
|
|
128
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_confidence ON patterns(confidence DESC);
|
|
129
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_status ON patterns(status);
|
|
130
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_last_matched ON patterns(last_matched_at);
|
|
131
|
+
|
|
132
|
+
-- Pattern evolution history (for versioning)
|
|
133
|
+
CREATE TABLE IF NOT EXISTS pattern_history (
|
|
134
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
135
|
+
pattern_id TEXT NOT NULL REFERENCES patterns(id),
|
|
136
|
+
version INTEGER NOT NULL,
|
|
137
|
+
|
|
138
|
+
-- Snapshot of pattern state
|
|
139
|
+
confidence REAL,
|
|
140
|
+
success_count INTEGER,
|
|
141
|
+
failure_count INTEGER,
|
|
142
|
+
condition TEXT,
|
|
143
|
+
action TEXT,
|
|
144
|
+
|
|
145
|
+
-- What changed
|
|
146
|
+
change_type TEXT CHECK(change_type IN ('created', 'updated', 'success', 'failure', 'decay', 'merged', 'split')),
|
|
147
|
+
change_reason TEXT,
|
|
148
|
+
|
|
149
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
CREATE INDEX IF NOT EXISTS idx_pattern_history_pattern ON pattern_history(pattern_id);
|
|
153
|
+
|
|
154
|
+
-- ============================================
|
|
155
|
+
-- LEARNING & TRAJECTORY TABLES
|
|
156
|
+
-- ============================================
|
|
157
|
+
|
|
158
|
+
-- Learning trajectories (SONA integration)
|
|
159
|
+
CREATE TABLE IF NOT EXISTS trajectories (
|
|
160
|
+
id TEXT PRIMARY KEY,
|
|
161
|
+
session_id TEXT,
|
|
162
|
+
|
|
163
|
+
-- Trajectory state
|
|
164
|
+
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'completed', 'failed', 'abandoned')),
|
|
165
|
+
verdict TEXT CHECK(verdict IN ('success', 'failure', 'partial', NULL)),
|
|
166
|
+
|
|
167
|
+
-- Context
|
|
168
|
+
task TEXT,
|
|
169
|
+
context TEXT, -- JSON object
|
|
170
|
+
|
|
171
|
+
-- Metrics
|
|
172
|
+
total_steps INTEGER DEFAULT 0,
|
|
173
|
+
total_reward REAL DEFAULT 0,
|
|
174
|
+
|
|
175
|
+
-- Timestamps
|
|
176
|
+
started_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
177
|
+
ended_at INTEGER,
|
|
178
|
+
|
|
179
|
+
-- Reference to extracted pattern (if any)
|
|
180
|
+
extracted_pattern_id TEXT REFERENCES patterns(id)
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
-- Trajectory steps
|
|
184
|
+
CREATE TABLE IF NOT EXISTS trajectory_steps (
|
|
185
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
186
|
+
trajectory_id TEXT NOT NULL REFERENCES trajectories(id),
|
|
187
|
+
step_number INTEGER NOT NULL,
|
|
188
|
+
|
|
189
|
+
-- Step data
|
|
190
|
+
action TEXT NOT NULL,
|
|
191
|
+
observation TEXT,
|
|
192
|
+
reward REAL DEFAULT 0,
|
|
193
|
+
|
|
194
|
+
-- Metadata
|
|
195
|
+
metadata TEXT, -- JSON object
|
|
196
|
+
|
|
197
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
CREATE INDEX IF NOT EXISTS idx_steps_trajectory ON trajectory_steps(trajectory_id);
|
|
201
|
+
|
|
202
|
+
-- ============================================
|
|
203
|
+
-- MIGRATION STATE TRACKING
|
|
204
|
+
-- ============================================
|
|
205
|
+
|
|
206
|
+
-- Migration state (for resume capability)
|
|
207
|
+
CREATE TABLE IF NOT EXISTS migration_state (
|
|
208
|
+
id TEXT PRIMARY KEY,
|
|
209
|
+
migration_type TEXT NOT NULL, -- 'v2-to-v3', 'pattern', 'memory', etc.
|
|
210
|
+
|
|
211
|
+
-- Progress tracking
|
|
212
|
+
status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'in_progress', 'completed', 'failed', 'rolled_back')),
|
|
213
|
+
total_items INTEGER DEFAULT 0,
|
|
214
|
+
processed_items INTEGER DEFAULT 0,
|
|
215
|
+
failed_items INTEGER DEFAULT 0,
|
|
216
|
+
skipped_items INTEGER DEFAULT 0,
|
|
217
|
+
|
|
218
|
+
-- Current position (for resume)
|
|
219
|
+
current_batch INTEGER DEFAULT 0,
|
|
220
|
+
last_processed_id TEXT,
|
|
221
|
+
|
|
222
|
+
-- Source/destination info
|
|
223
|
+
source_path TEXT,
|
|
224
|
+
source_type TEXT,
|
|
225
|
+
destination_path TEXT,
|
|
226
|
+
|
|
227
|
+
-- Backup info
|
|
228
|
+
backup_path TEXT,
|
|
229
|
+
backup_created_at INTEGER,
|
|
230
|
+
|
|
231
|
+
-- Error tracking
|
|
232
|
+
last_error TEXT,
|
|
233
|
+
errors TEXT, -- JSON array of errors
|
|
234
|
+
|
|
235
|
+
-- Timestamps
|
|
236
|
+
started_at INTEGER,
|
|
237
|
+
completed_at INTEGER,
|
|
238
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
239
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
-- ============================================
|
|
243
|
+
-- SESSION MANAGEMENT
|
|
244
|
+
-- ============================================
|
|
245
|
+
|
|
246
|
+
-- Sessions for context persistence
|
|
247
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
248
|
+
id TEXT PRIMARY KEY,
|
|
249
|
+
|
|
250
|
+
-- Session state
|
|
251
|
+
state TEXT NOT NULL, -- JSON object with full session state
|
|
252
|
+
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'paused', 'completed', 'expired')),
|
|
253
|
+
|
|
254
|
+
-- Context
|
|
255
|
+
project_path TEXT,
|
|
256
|
+
branch TEXT,
|
|
257
|
+
|
|
258
|
+
-- Metrics
|
|
259
|
+
tasks_completed INTEGER DEFAULT 0,
|
|
260
|
+
patterns_learned INTEGER DEFAULT 0,
|
|
261
|
+
|
|
262
|
+
-- Timestamps
|
|
263
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
264
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
265
|
+
expires_at INTEGER
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
-- ============================================
|
|
269
|
+
-- VECTOR INDEX METADATA (for HNSW)
|
|
270
|
+
-- ============================================
|
|
271
|
+
|
|
272
|
+
-- Track HNSW index state
|
|
273
|
+
CREATE TABLE IF NOT EXISTS vector_indexes (
|
|
274
|
+
id TEXT PRIMARY KEY,
|
|
275
|
+
name TEXT NOT NULL UNIQUE,
|
|
276
|
+
|
|
277
|
+
-- Index configuration
|
|
278
|
+
dimensions INTEGER NOT NULL,
|
|
279
|
+
metric TEXT DEFAULT 'cosine' CHECK(metric IN ('cosine', 'euclidean', 'dot')),
|
|
280
|
+
|
|
281
|
+
-- HNSW parameters
|
|
282
|
+
hnsw_m INTEGER DEFAULT 16,
|
|
283
|
+
hnsw_ef_construction INTEGER DEFAULT 200,
|
|
284
|
+
hnsw_ef_search INTEGER DEFAULT 100,
|
|
285
|
+
|
|
286
|
+
-- Quantization
|
|
287
|
+
quantization_type TEXT CHECK(quantization_type IN ('none', 'scalar', 'product')),
|
|
288
|
+
quantization_bits INTEGER DEFAULT 8,
|
|
289
|
+
|
|
290
|
+
-- Statistics
|
|
291
|
+
total_vectors INTEGER DEFAULT 0,
|
|
292
|
+
last_rebuild_at INTEGER,
|
|
293
|
+
|
|
294
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
295
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
-- ============================================
|
|
299
|
+
-- SYSTEM METADATA
|
|
300
|
+
-- ============================================
|
|
301
|
+
|
|
302
|
+
CREATE TABLE IF NOT EXISTS metadata (
|
|
303
|
+
key TEXT PRIMARY KEY,
|
|
304
|
+
value TEXT NOT NULL,
|
|
305
|
+
updated_at INTEGER DEFAULT (strftime('%s', 'now') * 1000)
|
|
306
|
+
);
|
|
307
|
+
`;
|
|
308
|
+
/**
|
|
309
|
+
* Initial metadata to insert after schema creation
|
|
310
|
+
*/
|
|
311
|
+
export function getInitialMetadata(backend) {
|
|
312
|
+
return `
|
|
313
|
+
INSERT OR REPLACE INTO metadata (key, value) VALUES
|
|
314
|
+
('schema_version', '3.0.0'),
|
|
315
|
+
('backend', '${backend}'),
|
|
316
|
+
('created_at', '${new Date().toISOString()}'),
|
|
317
|
+
('sql_js', 'true'),
|
|
318
|
+
('vector_embeddings', 'enabled'),
|
|
319
|
+
('pattern_learning', 'enabled'),
|
|
320
|
+
('temporal_decay', 'enabled'),
|
|
321
|
+
('hnsw_indexing', 'enabled'),
|
|
322
|
+
('embeddings_version', '2');
|
|
323
|
+
|
|
324
|
+
-- Create default vector index configuration
|
|
325
|
+
INSERT OR IGNORE INTO vector_indexes (id, name, dimensions) VALUES
|
|
326
|
+
('default', 'default', 768),
|
|
327
|
+
('patterns', 'patterns', 768);
|
|
328
|
+
`;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Ensure memory_entries table has all required columns
|
|
332
|
+
* Adds missing columns for older databases (e.g., 'content' column)
|
|
333
|
+
*/
|
|
334
|
+
export async function ensureSchemaColumns(dbPath) {
|
|
335
|
+
const columnsAdded = [];
|
|
336
|
+
try {
|
|
337
|
+
if (!fs.existsSync(dbPath)) {
|
|
338
|
+
return { success: true, columnsAdded: [] };
|
|
339
|
+
}
|
|
340
|
+
const db = openDaemonDatabase(dbPath);
|
|
341
|
+
// Get current columns in memory_entries
|
|
342
|
+
const tableInfo = db.exec("PRAGMA table_info(memory_entries)");
|
|
343
|
+
const existingColumns = new Set(tableInfo[0]?.values?.map(row => row[1]) || []);
|
|
344
|
+
// Required columns that may be missing in older schemas
|
|
345
|
+
// Issue #977: 'type' column was missing from this list, causing store failures on older DBs
|
|
346
|
+
const requiredColumns = [
|
|
347
|
+
{ name: 'content', definition: "content TEXT DEFAULT ''" },
|
|
348
|
+
{ name: 'type', definition: "type TEXT DEFAULT 'semantic'" },
|
|
349
|
+
{ name: 'embedding', definition: 'embedding TEXT' },
|
|
350
|
+
{ name: 'embedding_model', definition: "embedding_model TEXT DEFAULT 'local'" },
|
|
351
|
+
{ name: 'embedding_dimensions', definition: 'embedding_dimensions INTEGER' },
|
|
352
|
+
{ name: 'tags', definition: 'tags TEXT' },
|
|
353
|
+
{ name: 'metadata', definition: 'metadata TEXT' },
|
|
354
|
+
{ name: 'owner_id', definition: 'owner_id TEXT' },
|
|
355
|
+
{ name: 'expires_at', definition: 'expires_at INTEGER' },
|
|
356
|
+
{ name: 'last_accessed_at', definition: 'last_accessed_at INTEGER' },
|
|
357
|
+
{ name: 'access_count', definition: 'access_count INTEGER DEFAULT 0' },
|
|
358
|
+
{ name: 'status', definition: "status TEXT DEFAULT 'active'" }
|
|
359
|
+
];
|
|
360
|
+
for (const col of requiredColumns) {
|
|
361
|
+
if (!existingColumns.has(col.name)) {
|
|
362
|
+
try {
|
|
363
|
+
db.run(`ALTER TABLE memory_entries ADD COLUMN ${col.definition}`);
|
|
364
|
+
columnsAdded.push(col.name);
|
|
365
|
+
}
|
|
366
|
+
catch (e) {
|
|
367
|
+
// Column might already exist or other error - continue
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
db.close();
|
|
372
|
+
return { success: true, columnsAdded };
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
return {
|
|
376
|
+
success: false,
|
|
377
|
+
columnsAdded,
|
|
378
|
+
error: errorDetail(error)
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
//# sourceMappingURL=schema.js.map
|