claude-memory-layer 1.0.23 → 1.0.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/settings.local.json +25 -0
- package/README.md +2 -0
- package/dist/cli/index.js +229 -978
- package/dist/cli/index.js.map +4 -4
- package/dist/core/index.js +59 -71
- package/dist/core/index.js.map +3 -3
- package/dist/hooks/post-tool-use.js +287 -976
- package/dist/hooks/post-tool-use.js.map +4 -4
- package/dist/hooks/semantic-daemon.js +6520 -0
- package/dist/hooks/semantic-daemon.js.map +7 -0
- package/dist/hooks/session-end.js +209 -973
- package/dist/hooks/session-end.js.map +4 -4
- package/dist/hooks/session-start.js +293 -978
- package/dist/hooks/session-start.js.map +4 -4
- package/dist/hooks/stop.js +247 -975
- package/dist/hooks/stop.js.map +4 -4
- package/dist/hooks/user-prompt-submit.js +406 -1036
- package/dist/hooks/user-prompt-submit.js.map +4 -4
- package/dist/server/api/index.js +209 -973
- package/dist/server/api/index.js.map +4 -4
- package/dist/server/index.js +209 -973
- package/dist/server/index.js.map +4 -4
- package/dist/services/memory-service.js +209 -973
- package/dist/services/memory-service.js.map +4 -4
- package/dist/ui/app.js +48 -1
- package/dist/ui/index.html +11 -3
- package/memory/_index.md +1 -0
- package/memory/agent_response/uncategorized/2026-03-04.md +1314 -1
- package/memory/session_summary/uncategorized/2026-03-04.md +50 -0
- package/memory/tool_observation/uncategorized/2026-03-04.md +969 -1
- package/memory/user_prompt/uncategorized/2026-03-04.md +555 -1
- package/package.json +1 -2
- package/scripts/build.ts +2 -1
- package/specs/memory-utilization-improvements/context.md +145 -0
- package/specs/memory-utilization-improvements/plan.md +361 -0
- package/specs/memory-utilization-improvements/spec.md +308 -0
- package/specs/optional-duckdb/context.md +77 -0
- package/specs/optional-duckdb/plan.md +142 -0
- package/specs/optional-duckdb/spec.md +35 -0
- package/specs/selective-tool-observation/context.md +100 -0
- package/specs/selective-tool-observation/plan.md +158 -0
- package/specs/selective-tool-observation/spec.md +127 -0
- package/src/cli/index.ts +1 -0
- package/src/core/db-wrapper.ts +18 -73
- package/src/core/embedder.ts +13 -4
- package/src/core/sqlite-event-store.ts +40 -0
- package/src/core/turn-state.ts +48 -0
- package/src/core/types.ts +1 -0
- package/src/hooks/post-tool-use.ts +72 -2
- package/src/hooks/semantic-daemon-client.ts +208 -0
- package/src/hooks/semantic-daemon.ts +276 -0
- package/src/hooks/session-start.ts +11 -0
- package/src/hooks/stop.ts +33 -4
- package/src/hooks/user-prompt-submit.ts +48 -40
- package/src/services/memory-service.ts +112 -65
- package/src/services/session-history-importer.ts +18 -0
- package/src/ui/app.js +48 -1
- package/src/ui/index.html +11 -3
package/dist/cli/index.js
CHANGED
|
@@ -26,744 +26,31 @@ import * as os from "os";
|
|
|
26
26
|
import * as fs4 from "fs";
|
|
27
27
|
import * as crypto2 from "crypto";
|
|
28
28
|
|
|
29
|
-
// src/core/event-store.ts
|
|
30
|
-
import { randomUUID } from "crypto";
|
|
31
|
-
|
|
32
|
-
// src/core/canonical-key.ts
|
|
33
|
-
import { createHash } from "crypto";
|
|
34
|
-
var MAX_KEY_LENGTH = 200;
|
|
35
|
-
function makeCanonicalKey(title, context) {
|
|
36
|
-
let normalized = title.normalize("NFKC");
|
|
37
|
-
normalized = normalized.toLowerCase();
|
|
38
|
-
normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
|
|
39
|
-
normalized = normalized.replace(/\s+/g, " ").trim();
|
|
40
|
-
let key = normalized;
|
|
41
|
-
if (context?.project) {
|
|
42
|
-
key = `${context.project}::${key}`;
|
|
43
|
-
}
|
|
44
|
-
if (key.length > MAX_KEY_LENGTH) {
|
|
45
|
-
const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
|
|
46
|
-
key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
|
|
47
|
-
}
|
|
48
|
-
return key;
|
|
49
|
-
}
|
|
50
|
-
function makeDedupeKey(content, sessionId) {
|
|
51
|
-
const contentHash = createHash("sha256").update(content).digest("hex");
|
|
52
|
-
return `${sessionId}:${contentHash}`;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// src/core/db-wrapper.ts
|
|
56
|
-
import duckdb from "duckdb";
|
|
57
|
-
function convertBigInts(obj) {
|
|
58
|
-
if (obj === null || obj === void 0)
|
|
59
|
-
return obj;
|
|
60
|
-
if (typeof obj === "bigint")
|
|
61
|
-
return Number(obj);
|
|
62
|
-
if (obj instanceof Date)
|
|
63
|
-
return obj;
|
|
64
|
-
if (Array.isArray(obj))
|
|
65
|
-
return obj.map(convertBigInts);
|
|
66
|
-
if (typeof obj === "object") {
|
|
67
|
-
const result = {};
|
|
68
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
69
|
-
result[key] = convertBigInts(value);
|
|
70
|
-
}
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
return obj;
|
|
74
|
-
}
|
|
75
|
-
function toDate(value) {
|
|
76
|
-
if (value instanceof Date)
|
|
77
|
-
return value;
|
|
78
|
-
if (typeof value === "string")
|
|
79
|
-
return new Date(value);
|
|
80
|
-
if (typeof value === "number")
|
|
81
|
-
return new Date(value);
|
|
82
|
-
return new Date(String(value));
|
|
83
|
-
}
|
|
84
|
-
function createDatabase(path11, options) {
|
|
85
|
-
if (options?.readOnly) {
|
|
86
|
-
return new duckdb.Database(path11, { access_mode: "READ_ONLY" });
|
|
87
|
-
}
|
|
88
|
-
return new duckdb.Database(path11);
|
|
89
|
-
}
|
|
90
|
-
function dbRun(db, sql, params = []) {
|
|
91
|
-
return new Promise((resolve5, reject) => {
|
|
92
|
-
if (params.length === 0) {
|
|
93
|
-
db.run(sql, (err) => {
|
|
94
|
-
if (err)
|
|
95
|
-
reject(err);
|
|
96
|
-
else
|
|
97
|
-
resolve5();
|
|
98
|
-
});
|
|
99
|
-
} else {
|
|
100
|
-
db.run(sql, ...params, (err) => {
|
|
101
|
-
if (err)
|
|
102
|
-
reject(err);
|
|
103
|
-
else
|
|
104
|
-
resolve5();
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
function dbAll(db, sql, params = []) {
|
|
110
|
-
return new Promise((resolve5, reject) => {
|
|
111
|
-
if (params.length === 0) {
|
|
112
|
-
db.all(sql, (err, rows) => {
|
|
113
|
-
if (err)
|
|
114
|
-
reject(err);
|
|
115
|
-
else
|
|
116
|
-
resolve5(convertBigInts(rows || []));
|
|
117
|
-
});
|
|
118
|
-
} else {
|
|
119
|
-
db.all(sql, ...params, (err, rows) => {
|
|
120
|
-
if (err)
|
|
121
|
-
reject(err);
|
|
122
|
-
else
|
|
123
|
-
resolve5(convertBigInts(rows || []));
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
function dbClose(db) {
|
|
129
|
-
return new Promise((resolve5, reject) => {
|
|
130
|
-
db.close((err) => {
|
|
131
|
-
if (err)
|
|
132
|
-
reject(err);
|
|
133
|
-
else
|
|
134
|
-
resolve5();
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// src/core/event-store.ts
|
|
140
|
-
var EventStore = class {
|
|
141
|
-
constructor(dbPath, options) {
|
|
142
|
-
this.dbPath = dbPath;
|
|
143
|
-
this.readOnly = options?.readOnly ?? false;
|
|
144
|
-
this.db = createDatabase(dbPath, { readOnly: this.readOnly });
|
|
145
|
-
}
|
|
146
|
-
db;
|
|
147
|
-
initialized = false;
|
|
148
|
-
readOnly;
|
|
149
|
-
/**
|
|
150
|
-
* Initialize database schema
|
|
151
|
-
*/
|
|
152
|
-
async initialize() {
|
|
153
|
-
if (this.initialized)
|
|
154
|
-
return;
|
|
155
|
-
if (this.readOnly) {
|
|
156
|
-
this.initialized = true;
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
await dbRun(this.db, `
|
|
160
|
-
CREATE TABLE IF NOT EXISTS events (
|
|
161
|
-
id VARCHAR PRIMARY KEY,
|
|
162
|
-
event_type VARCHAR NOT NULL,
|
|
163
|
-
session_id VARCHAR NOT NULL,
|
|
164
|
-
timestamp TIMESTAMP NOT NULL,
|
|
165
|
-
content TEXT NOT NULL,
|
|
166
|
-
canonical_key VARCHAR NOT NULL,
|
|
167
|
-
dedupe_key VARCHAR UNIQUE,
|
|
168
|
-
metadata JSON
|
|
169
|
-
)
|
|
170
|
-
`);
|
|
171
|
-
await dbRun(this.db, `
|
|
172
|
-
CREATE TABLE IF NOT EXISTS event_dedup (
|
|
173
|
-
dedupe_key VARCHAR PRIMARY KEY,
|
|
174
|
-
event_id VARCHAR NOT NULL,
|
|
175
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
176
|
-
)
|
|
177
|
-
`);
|
|
178
|
-
await dbRun(this.db, `
|
|
179
|
-
CREATE TABLE IF NOT EXISTS sessions (
|
|
180
|
-
id VARCHAR PRIMARY KEY,
|
|
181
|
-
started_at TIMESTAMP NOT NULL,
|
|
182
|
-
ended_at TIMESTAMP,
|
|
183
|
-
project_path VARCHAR,
|
|
184
|
-
summary TEXT,
|
|
185
|
-
tags JSON
|
|
186
|
-
)
|
|
187
|
-
`);
|
|
188
|
-
await dbRun(this.db, `
|
|
189
|
-
CREATE TABLE IF NOT EXISTS insights (
|
|
190
|
-
id VARCHAR PRIMARY KEY,
|
|
191
|
-
insight_type VARCHAR NOT NULL,
|
|
192
|
-
content TEXT NOT NULL,
|
|
193
|
-
canonical_key VARCHAR NOT NULL,
|
|
194
|
-
confidence FLOAT,
|
|
195
|
-
source_events JSON,
|
|
196
|
-
created_at TIMESTAMP,
|
|
197
|
-
last_updated TIMESTAMP
|
|
198
|
-
)
|
|
199
|
-
`);
|
|
200
|
-
await dbRun(this.db, `
|
|
201
|
-
CREATE TABLE IF NOT EXISTS embedding_outbox (
|
|
202
|
-
id VARCHAR PRIMARY KEY,
|
|
203
|
-
event_id VARCHAR NOT NULL,
|
|
204
|
-
content TEXT NOT NULL,
|
|
205
|
-
status VARCHAR DEFAULT 'pending',
|
|
206
|
-
retry_count INT DEFAULT 0,
|
|
207
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
208
|
-
processed_at TIMESTAMP,
|
|
209
|
-
error_message TEXT
|
|
210
|
-
)
|
|
211
|
-
`);
|
|
212
|
-
await dbRun(this.db, `
|
|
213
|
-
CREATE TABLE IF NOT EXISTS projection_offsets (
|
|
214
|
-
projection_name VARCHAR PRIMARY KEY,
|
|
215
|
-
last_event_id VARCHAR,
|
|
216
|
-
last_timestamp TIMESTAMP,
|
|
217
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
218
|
-
)
|
|
219
|
-
`);
|
|
220
|
-
await dbRun(this.db, `
|
|
221
|
-
CREATE TABLE IF NOT EXISTS memory_levels (
|
|
222
|
-
event_id VARCHAR PRIMARY KEY,
|
|
223
|
-
level VARCHAR NOT NULL DEFAULT 'L0',
|
|
224
|
-
promoted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
225
|
-
)
|
|
226
|
-
`);
|
|
227
|
-
await dbRun(this.db, `
|
|
228
|
-
CREATE TABLE IF NOT EXISTS entries (
|
|
229
|
-
entry_id VARCHAR PRIMARY KEY,
|
|
230
|
-
created_ts TIMESTAMP NOT NULL,
|
|
231
|
-
entry_type VARCHAR NOT NULL,
|
|
232
|
-
title VARCHAR NOT NULL,
|
|
233
|
-
content_json JSON NOT NULL,
|
|
234
|
-
stage VARCHAR NOT NULL DEFAULT 'raw',
|
|
235
|
-
status VARCHAR DEFAULT 'active',
|
|
236
|
-
superseded_by VARCHAR,
|
|
237
|
-
build_id VARCHAR,
|
|
238
|
-
evidence_json JSON,
|
|
239
|
-
canonical_key VARCHAR,
|
|
240
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
241
|
-
)
|
|
242
|
-
`);
|
|
243
|
-
await dbRun(this.db, `
|
|
244
|
-
CREATE TABLE IF NOT EXISTS entities (
|
|
245
|
-
entity_id VARCHAR PRIMARY KEY,
|
|
246
|
-
entity_type VARCHAR NOT NULL,
|
|
247
|
-
canonical_key VARCHAR NOT NULL,
|
|
248
|
-
title VARCHAR NOT NULL,
|
|
249
|
-
stage VARCHAR NOT NULL DEFAULT 'raw',
|
|
250
|
-
status VARCHAR NOT NULL DEFAULT 'active',
|
|
251
|
-
current_json JSON NOT NULL,
|
|
252
|
-
title_norm VARCHAR,
|
|
253
|
-
search_text VARCHAR,
|
|
254
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
255
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
256
|
-
)
|
|
257
|
-
`);
|
|
258
|
-
await dbRun(this.db, `
|
|
259
|
-
CREATE TABLE IF NOT EXISTS entity_aliases (
|
|
260
|
-
entity_type VARCHAR NOT NULL,
|
|
261
|
-
canonical_key VARCHAR NOT NULL,
|
|
262
|
-
entity_id VARCHAR NOT NULL,
|
|
263
|
-
is_primary BOOLEAN DEFAULT FALSE,
|
|
264
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
265
|
-
PRIMARY KEY(entity_type, canonical_key)
|
|
266
|
-
)
|
|
267
|
-
`);
|
|
268
|
-
await dbRun(this.db, `
|
|
269
|
-
CREATE TABLE IF NOT EXISTS edges (
|
|
270
|
-
edge_id VARCHAR PRIMARY KEY,
|
|
271
|
-
src_type VARCHAR NOT NULL,
|
|
272
|
-
src_id VARCHAR NOT NULL,
|
|
273
|
-
rel_type VARCHAR NOT NULL,
|
|
274
|
-
dst_type VARCHAR NOT NULL,
|
|
275
|
-
dst_id VARCHAR NOT NULL,
|
|
276
|
-
meta_json JSON,
|
|
277
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
278
|
-
)
|
|
279
|
-
`);
|
|
280
|
-
await dbRun(this.db, `
|
|
281
|
-
CREATE TABLE IF NOT EXISTS vector_outbox (
|
|
282
|
-
job_id VARCHAR PRIMARY KEY,
|
|
283
|
-
item_kind VARCHAR NOT NULL,
|
|
284
|
-
item_id VARCHAR NOT NULL,
|
|
285
|
-
embedding_version VARCHAR NOT NULL,
|
|
286
|
-
status VARCHAR NOT NULL DEFAULT 'pending',
|
|
287
|
-
retry_count INT DEFAULT 0,
|
|
288
|
-
error VARCHAR,
|
|
289
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
290
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
291
|
-
UNIQUE(item_kind, item_id, embedding_version)
|
|
292
|
-
)
|
|
293
|
-
`);
|
|
294
|
-
await dbRun(this.db, `
|
|
295
|
-
CREATE TABLE IF NOT EXISTS build_runs (
|
|
296
|
-
build_id VARCHAR PRIMARY KEY,
|
|
297
|
-
started_at TIMESTAMP NOT NULL,
|
|
298
|
-
finished_at TIMESTAMP,
|
|
299
|
-
extractor_model VARCHAR NOT NULL,
|
|
300
|
-
extractor_prompt_hash VARCHAR NOT NULL,
|
|
301
|
-
embedder_model VARCHAR NOT NULL,
|
|
302
|
-
embedding_version VARCHAR NOT NULL,
|
|
303
|
-
idris_version VARCHAR NOT NULL,
|
|
304
|
-
schema_version VARCHAR NOT NULL,
|
|
305
|
-
status VARCHAR NOT NULL DEFAULT 'running',
|
|
306
|
-
error VARCHAR
|
|
307
|
-
)
|
|
308
|
-
`);
|
|
309
|
-
await dbRun(this.db, `
|
|
310
|
-
CREATE TABLE IF NOT EXISTS pipeline_metrics (
|
|
311
|
-
id VARCHAR PRIMARY KEY,
|
|
312
|
-
ts TIMESTAMP NOT NULL,
|
|
313
|
-
stage VARCHAR NOT NULL,
|
|
314
|
-
latency_ms DOUBLE NOT NULL,
|
|
315
|
-
success BOOLEAN NOT NULL,
|
|
316
|
-
error VARCHAR,
|
|
317
|
-
session_id VARCHAR
|
|
318
|
-
)
|
|
319
|
-
`);
|
|
320
|
-
await dbRun(this.db, `
|
|
321
|
-
CREATE TABLE IF NOT EXISTS working_set (
|
|
322
|
-
id VARCHAR PRIMARY KEY,
|
|
323
|
-
event_id VARCHAR NOT NULL,
|
|
324
|
-
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
325
|
-
relevance_score FLOAT DEFAULT 1.0,
|
|
326
|
-
topics JSON,
|
|
327
|
-
expires_at TIMESTAMP
|
|
328
|
-
)
|
|
329
|
-
`);
|
|
330
|
-
await dbRun(this.db, `
|
|
331
|
-
CREATE TABLE IF NOT EXISTS consolidated_memories (
|
|
332
|
-
memory_id VARCHAR PRIMARY KEY,
|
|
333
|
-
summary TEXT NOT NULL,
|
|
334
|
-
topics JSON,
|
|
335
|
-
source_events JSON,
|
|
336
|
-
confidence FLOAT DEFAULT 0.5,
|
|
337
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
338
|
-
accessed_at TIMESTAMP,
|
|
339
|
-
access_count INTEGER DEFAULT 0
|
|
340
|
-
)
|
|
341
|
-
`);
|
|
342
|
-
await dbRun(this.db, `
|
|
343
|
-
CREATE TABLE IF NOT EXISTS continuity_log (
|
|
344
|
-
log_id VARCHAR PRIMARY KEY,
|
|
345
|
-
from_context_id VARCHAR,
|
|
346
|
-
to_context_id VARCHAR,
|
|
347
|
-
continuity_score FLOAT,
|
|
348
|
-
transition_type VARCHAR,
|
|
349
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
350
|
-
)
|
|
351
|
-
`);
|
|
352
|
-
await dbRun(this.db, `
|
|
353
|
-
CREATE TABLE IF NOT EXISTS consolidated_rules (
|
|
354
|
-
rule_id VARCHAR PRIMARY KEY,
|
|
355
|
-
rule TEXT NOT NULL,
|
|
356
|
-
topics JSON,
|
|
357
|
-
source_memory_ids JSON,
|
|
358
|
-
source_events JSON,
|
|
359
|
-
confidence FLOAT DEFAULT 0.5,
|
|
360
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
361
|
-
)
|
|
362
|
-
`);
|
|
363
|
-
await dbRun(this.db, `
|
|
364
|
-
CREATE TABLE IF NOT EXISTS endless_config (
|
|
365
|
-
key VARCHAR PRIMARY KEY,
|
|
366
|
-
value JSON,
|
|
367
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
368
|
-
)
|
|
369
|
-
`);
|
|
370
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type)`);
|
|
371
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_stage ON entries(stage)`);
|
|
372
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_canonical ON entries(canonical_key)`);
|
|
373
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_type_key ON entities(entity_type, canonical_key)`);
|
|
374
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_status ON entities(status)`);
|
|
375
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_src ON edges(src_id, rel_type)`);
|
|
376
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_dst ON edges(dst_id, rel_type)`);
|
|
377
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_rel ON edges(rel_type)`);
|
|
378
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_outbox_status ON vector_outbox(status)`);
|
|
379
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
|
|
380
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
|
|
381
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
|
|
382
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
|
|
383
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
|
|
384
|
-
this.initialized = true;
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Append event to store (AXIOMMIND Principle 2: Append-only)
|
|
388
|
-
* Returns existing event ID if duplicate (Principle 3: Idempotency)
|
|
389
|
-
*/
|
|
390
|
-
async append(input) {
|
|
391
|
-
await this.initialize();
|
|
392
|
-
const canonicalKey = makeCanonicalKey(input.content);
|
|
393
|
-
const dedupeKey = makeDedupeKey(input.content, input.sessionId);
|
|
394
|
-
const existing = await dbAll(
|
|
395
|
-
this.db,
|
|
396
|
-
`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`,
|
|
397
|
-
[dedupeKey]
|
|
398
|
-
);
|
|
399
|
-
if (existing.length > 0) {
|
|
400
|
-
return {
|
|
401
|
-
success: true,
|
|
402
|
-
eventId: existing[0].event_id,
|
|
403
|
-
isDuplicate: true
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
const id = randomUUID();
|
|
407
|
-
const timestamp = input.timestamp.toISOString();
|
|
408
|
-
try {
|
|
409
|
-
await dbRun(
|
|
410
|
-
this.db,
|
|
411
|
-
`INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
|
|
412
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
413
|
-
[
|
|
414
|
-
id,
|
|
415
|
-
input.eventType,
|
|
416
|
-
input.sessionId,
|
|
417
|
-
timestamp,
|
|
418
|
-
input.content,
|
|
419
|
-
canonicalKey,
|
|
420
|
-
dedupeKey,
|
|
421
|
-
JSON.stringify(input.metadata || {})
|
|
422
|
-
]
|
|
423
|
-
);
|
|
424
|
-
await dbRun(
|
|
425
|
-
this.db,
|
|
426
|
-
`INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)`,
|
|
427
|
-
[dedupeKey, id]
|
|
428
|
-
);
|
|
429
|
-
await dbRun(
|
|
430
|
-
this.db,
|
|
431
|
-
`INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')`,
|
|
432
|
-
[id]
|
|
433
|
-
);
|
|
434
|
-
return { success: true, eventId: id, isDuplicate: false };
|
|
435
|
-
} catch (error) {
|
|
436
|
-
return {
|
|
437
|
-
success: false,
|
|
438
|
-
error: error instanceof Error ? error.message : String(error)
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* Get events by session ID
|
|
444
|
-
*/
|
|
445
|
-
async getSessionEvents(sessionId) {
|
|
446
|
-
await this.initialize();
|
|
447
|
-
const rows = await dbAll(
|
|
448
|
-
this.db,
|
|
449
|
-
`SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
|
|
450
|
-
[sessionId]
|
|
451
|
-
);
|
|
452
|
-
return rows.map(this.rowToEvent);
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Get recent events
|
|
456
|
-
*/
|
|
457
|
-
async getRecentEvents(limit = 100) {
|
|
458
|
-
await this.initialize();
|
|
459
|
-
const rows = await dbAll(
|
|
460
|
-
this.db,
|
|
461
|
-
`SELECT * FROM events ORDER BY timestamp DESC LIMIT ?`,
|
|
462
|
-
[limit]
|
|
463
|
-
);
|
|
464
|
-
return rows.map(this.rowToEvent);
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* Get event by ID
|
|
468
|
-
*/
|
|
469
|
-
async getEvent(id) {
|
|
470
|
-
await this.initialize();
|
|
471
|
-
const rows = await dbAll(
|
|
472
|
-
this.db,
|
|
473
|
-
`SELECT * FROM events WHERE id = ?`,
|
|
474
|
-
[id]
|
|
475
|
-
);
|
|
476
|
-
if (rows.length === 0)
|
|
477
|
-
return null;
|
|
478
|
-
return this.rowToEvent(rows[0]);
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* Create or update session
|
|
482
|
-
*/
|
|
483
|
-
async upsertSession(session) {
|
|
484
|
-
await this.initialize();
|
|
485
|
-
const existing = await dbAll(
|
|
486
|
-
this.db,
|
|
487
|
-
`SELECT id FROM sessions WHERE id = ?`,
|
|
488
|
-
[session.id]
|
|
489
|
-
);
|
|
490
|
-
if (existing.length === 0) {
|
|
491
|
-
await dbRun(
|
|
492
|
-
this.db,
|
|
493
|
-
`INSERT INTO sessions (id, started_at, project_path, tags)
|
|
494
|
-
VALUES (?, ?, ?, ?)`,
|
|
495
|
-
[
|
|
496
|
-
session.id,
|
|
497
|
-
(session.startedAt || /* @__PURE__ */ new Date()).toISOString(),
|
|
498
|
-
session.projectPath || null,
|
|
499
|
-
JSON.stringify(session.tags || [])
|
|
500
|
-
]
|
|
501
|
-
);
|
|
502
|
-
} else {
|
|
503
|
-
const updates = [];
|
|
504
|
-
const values = [];
|
|
505
|
-
if (session.endedAt) {
|
|
506
|
-
updates.push("ended_at = ?");
|
|
507
|
-
values.push(session.endedAt.toISOString());
|
|
508
|
-
}
|
|
509
|
-
if (session.summary) {
|
|
510
|
-
updates.push("summary = ?");
|
|
511
|
-
values.push(session.summary);
|
|
512
|
-
}
|
|
513
|
-
if (session.tags) {
|
|
514
|
-
updates.push("tags = ?");
|
|
515
|
-
values.push(JSON.stringify(session.tags));
|
|
516
|
-
}
|
|
517
|
-
if (updates.length > 0) {
|
|
518
|
-
values.push(session.id);
|
|
519
|
-
await dbRun(
|
|
520
|
-
this.db,
|
|
521
|
-
`UPDATE sessions SET ${updates.join(", ")} WHERE id = ?`,
|
|
522
|
-
values
|
|
523
|
-
);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
/**
|
|
528
|
-
* Get session by ID
|
|
529
|
-
*/
|
|
530
|
-
async getSession(id) {
|
|
531
|
-
await this.initialize();
|
|
532
|
-
const rows = await dbAll(
|
|
533
|
-
this.db,
|
|
534
|
-
`SELECT * FROM sessions WHERE id = ?`,
|
|
535
|
-
[id]
|
|
536
|
-
);
|
|
537
|
-
if (rows.length === 0)
|
|
538
|
-
return null;
|
|
539
|
-
const row = rows[0];
|
|
540
|
-
return {
|
|
541
|
-
id: row.id,
|
|
542
|
-
startedAt: toDate(row.started_at),
|
|
543
|
-
endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
|
|
544
|
-
projectPath: row.project_path,
|
|
545
|
-
summary: row.summary,
|
|
546
|
-
tags: row.tags ? JSON.parse(row.tags) : void 0
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* Add to embedding outbox (Single-Writer Pattern)
|
|
551
|
-
*/
|
|
552
|
-
async enqueueForEmbedding(eventId, content) {
|
|
553
|
-
await this.initialize();
|
|
554
|
-
const id = randomUUID();
|
|
555
|
-
await dbRun(
|
|
556
|
-
this.db,
|
|
557
|
-
`INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
|
|
558
|
-
VALUES (?, ?, ?, 'pending', 0)`,
|
|
559
|
-
[id, eventId, content]
|
|
560
|
-
);
|
|
561
|
-
return id;
|
|
562
|
-
}
|
|
563
|
-
/**
|
|
564
|
-
* Get pending outbox items
|
|
565
|
-
*/
|
|
566
|
-
async getPendingOutboxItems(limit = 32) {
|
|
567
|
-
await this.initialize();
|
|
568
|
-
const pending = await dbAll(
|
|
569
|
-
this.db,
|
|
570
|
-
`SELECT * FROM embedding_outbox
|
|
571
|
-
WHERE status = 'pending'
|
|
572
|
-
ORDER BY created_at
|
|
573
|
-
LIMIT ?`,
|
|
574
|
-
[limit]
|
|
575
|
-
);
|
|
576
|
-
if (pending.length === 0)
|
|
577
|
-
return [];
|
|
578
|
-
const ids = pending.map((r) => r.id);
|
|
579
|
-
const placeholders = ids.map(() => "?").join(",");
|
|
580
|
-
await dbRun(
|
|
581
|
-
this.db,
|
|
582
|
-
`UPDATE embedding_outbox SET status = 'processing' WHERE id IN (${placeholders})`,
|
|
583
|
-
ids
|
|
584
|
-
);
|
|
585
|
-
return pending.map((row) => ({
|
|
586
|
-
id: row.id,
|
|
587
|
-
eventId: row.event_id,
|
|
588
|
-
content: row.content,
|
|
589
|
-
status: "processing",
|
|
590
|
-
retryCount: row.retry_count,
|
|
591
|
-
createdAt: toDate(row.created_at),
|
|
592
|
-
errorMessage: row.error_message
|
|
593
|
-
}));
|
|
594
|
-
}
|
|
595
|
-
/**
|
|
596
|
-
* Mark outbox items as done
|
|
597
|
-
*/
|
|
598
|
-
async completeOutboxItems(ids) {
|
|
599
|
-
if (ids.length === 0)
|
|
600
|
-
return;
|
|
601
|
-
const placeholders = ids.map(() => "?").join(",");
|
|
602
|
-
await dbRun(
|
|
603
|
-
this.db,
|
|
604
|
-
`DELETE FROM embedding_outbox WHERE id IN (${placeholders})`,
|
|
605
|
-
ids
|
|
606
|
-
);
|
|
607
|
-
}
|
|
608
|
-
/**
|
|
609
|
-
* Mark outbox items as failed
|
|
610
|
-
*/
|
|
611
|
-
async failOutboxItems(ids, error) {
|
|
612
|
-
if (ids.length === 0)
|
|
613
|
-
return;
|
|
614
|
-
const placeholders = ids.map(() => "?").join(",");
|
|
615
|
-
await dbRun(
|
|
616
|
-
this.db,
|
|
617
|
-
`UPDATE embedding_outbox
|
|
618
|
-
SET status = CASE WHEN retry_count >= 3 THEN 'failed' ELSE 'pending' END,
|
|
619
|
-
retry_count = retry_count + 1,
|
|
620
|
-
error_message = ?
|
|
621
|
-
WHERE id IN (${placeholders})`,
|
|
622
|
-
[error, ...ids]
|
|
623
|
-
);
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* Update memory level
|
|
627
|
-
*/
|
|
628
|
-
async updateMemoryLevel(eventId, level) {
|
|
629
|
-
await this.initialize();
|
|
630
|
-
await dbRun(
|
|
631
|
-
this.db,
|
|
632
|
-
`UPDATE memory_levels SET level = ?, promoted_at = CURRENT_TIMESTAMP WHERE event_id = ?`,
|
|
633
|
-
[level, eventId]
|
|
634
|
-
);
|
|
635
|
-
}
|
|
636
|
-
/**
|
|
637
|
-
* Get memory level statistics
|
|
638
|
-
*/
|
|
639
|
-
async getLevelStats() {
|
|
640
|
-
await this.initialize();
|
|
641
|
-
const rows = await dbAll(
|
|
642
|
-
this.db,
|
|
643
|
-
`SELECT level, COUNT(*) as count FROM memory_levels GROUP BY level`
|
|
644
|
-
);
|
|
645
|
-
return rows;
|
|
646
|
-
}
|
|
647
|
-
/**
|
|
648
|
-
* Get events by memory level
|
|
649
|
-
*/
|
|
650
|
-
async getEventsByLevel(level, options) {
|
|
651
|
-
await this.initialize();
|
|
652
|
-
const limit = options?.limit || 50;
|
|
653
|
-
const offset = options?.offset || 0;
|
|
654
|
-
const rows = await dbAll(
|
|
655
|
-
this.db,
|
|
656
|
-
`SELECT e.* FROM events e
|
|
657
|
-
INNER JOIN memory_levels ml ON e.id = ml.event_id
|
|
658
|
-
WHERE ml.level = ?
|
|
659
|
-
ORDER BY e.timestamp DESC
|
|
660
|
-
LIMIT ? OFFSET ?`,
|
|
661
|
-
[level, limit, offset]
|
|
662
|
-
);
|
|
663
|
-
return rows.map((row) => this.rowToEvent(row));
|
|
664
|
-
}
|
|
665
|
-
/**
|
|
666
|
-
* Get memory level for a specific event
|
|
667
|
-
*/
|
|
668
|
-
async getEventLevel(eventId) {
|
|
669
|
-
await this.initialize();
|
|
670
|
-
const rows = await dbAll(
|
|
671
|
-
this.db,
|
|
672
|
-
`SELECT level FROM memory_levels WHERE event_id = ?`,
|
|
673
|
-
[eventId]
|
|
674
|
-
);
|
|
675
|
-
return rows.length > 0 ? rows[0].level : null;
|
|
676
|
-
}
|
|
677
|
-
// ============================================================
|
|
678
|
-
// Endless Mode Helper Methods
|
|
679
|
-
// ============================================================
|
|
680
|
-
/**
|
|
681
|
-
* Get database instance for Endless Mode stores
|
|
682
|
-
*/
|
|
683
|
-
getDatabase() {
|
|
684
|
-
return this.db;
|
|
685
|
-
}
|
|
686
|
-
/**
|
|
687
|
-
* Get config value for endless mode
|
|
688
|
-
*/
|
|
689
|
-
async getEndlessConfig(key) {
|
|
690
|
-
await this.initialize();
|
|
691
|
-
const rows = await dbAll(
|
|
692
|
-
this.db,
|
|
693
|
-
`SELECT value FROM endless_config WHERE key = ?`,
|
|
694
|
-
[key]
|
|
695
|
-
);
|
|
696
|
-
if (rows.length === 0)
|
|
697
|
-
return null;
|
|
698
|
-
return JSON.parse(rows[0].value);
|
|
699
|
-
}
|
|
700
|
-
/**
|
|
701
|
-
* Set config value for endless mode
|
|
702
|
-
*/
|
|
703
|
-
async setEndlessConfig(key, value) {
|
|
704
|
-
await this.initialize();
|
|
705
|
-
await dbRun(
|
|
706
|
-
this.db,
|
|
707
|
-
`INSERT OR REPLACE INTO endless_config (key, value, updated_at)
|
|
708
|
-
VALUES (?, ?, CURRENT_TIMESTAMP)`,
|
|
709
|
-
[key, JSON.stringify(value)]
|
|
710
|
-
);
|
|
711
|
-
}
|
|
712
|
-
/**
|
|
713
|
-
* Get all sessions
|
|
714
|
-
*/
|
|
715
|
-
async getAllSessions() {
|
|
716
|
-
await this.initialize();
|
|
717
|
-
const rows = await dbAll(
|
|
718
|
-
this.db,
|
|
719
|
-
`SELECT * FROM sessions ORDER BY started_at DESC`
|
|
720
|
-
);
|
|
721
|
-
return rows.map((row) => ({
|
|
722
|
-
id: row.id,
|
|
723
|
-
startedAt: toDate(row.started_at),
|
|
724
|
-
endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
|
|
725
|
-
projectPath: row.project_path,
|
|
726
|
-
summary: row.summary,
|
|
727
|
-
tags: row.tags ? JSON.parse(row.tags) : void 0
|
|
728
|
-
}));
|
|
729
|
-
}
|
|
730
|
-
/**
|
|
731
|
-
* Increment access count for events (stub for compatibility)
|
|
732
|
-
*/
|
|
733
|
-
async incrementAccessCount(eventIds) {
|
|
734
|
-
return Promise.resolve();
|
|
735
|
-
}
|
|
736
|
-
/**
|
|
737
|
-
* Get most accessed memories (stub for compatibility)
|
|
738
|
-
*/
|
|
739
|
-
async getMostAccessed(limit = 10) {
|
|
740
|
-
return [];
|
|
741
|
-
}
|
|
742
|
-
/**
|
|
743
|
-
* Close database connection
|
|
744
|
-
*/
|
|
745
|
-
async close() {
|
|
746
|
-
await dbClose(this.db);
|
|
29
|
+
// src/core/sqlite-event-store.ts
|
|
30
|
+
import { randomUUID } from "crypto";
|
|
31
|
+
|
|
32
|
+
// src/core/canonical-key.ts
|
|
33
|
+
import { createHash } from "crypto";
|
|
34
|
+
var MAX_KEY_LENGTH = 200;
|
|
35
|
+
function makeCanonicalKey(title, context) {
|
|
36
|
+
let normalized = title.normalize("NFKC");
|
|
37
|
+
normalized = normalized.toLowerCase();
|
|
38
|
+
normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
|
|
39
|
+
normalized = normalized.replace(/\s+/g, " ").trim();
|
|
40
|
+
let key = normalized;
|
|
41
|
+
if (context?.project) {
|
|
42
|
+
key = `${context.project}::${key}`;
|
|
747
43
|
}
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
rowToEvent(row) {
|
|
752
|
-
return {
|
|
753
|
-
id: row.id,
|
|
754
|
-
eventType: row.event_type,
|
|
755
|
-
sessionId: row.session_id,
|
|
756
|
-
timestamp: toDate(row.timestamp),
|
|
757
|
-
content: row.content,
|
|
758
|
-
canonicalKey: row.canonical_key,
|
|
759
|
-
dedupeKey: row.dedupe_key,
|
|
760
|
-
metadata: row.metadata ? JSON.parse(row.metadata) : void 0
|
|
761
|
-
};
|
|
44
|
+
if (key.length > MAX_KEY_LENGTH) {
|
|
45
|
+
const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
|
|
46
|
+
key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
|
|
762
47
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
48
|
+
return key;
|
|
49
|
+
}
|
|
50
|
+
function makeDedupeKey(content, sessionId) {
|
|
51
|
+
const contentHash = createHash("sha256").update(content).digest("hex");
|
|
52
|
+
return `${sessionId}:${contentHash}`;
|
|
53
|
+
}
|
|
767
54
|
|
|
768
55
|
// src/core/sqlite-wrapper.ts
|
|
769
56
|
import Database from "better-sqlite3";
|
|
@@ -1278,7 +565,7 @@ var SQLiteEventStore = class {
|
|
|
1278
565
|
isDuplicate: true
|
|
1279
566
|
};
|
|
1280
567
|
}
|
|
1281
|
-
const id =
|
|
568
|
+
const id = randomUUID();
|
|
1282
569
|
const timestamp = toSQLiteTimestamp(input.timestamp);
|
|
1283
570
|
try {
|
|
1284
571
|
const metadata = input.metadata || {};
|
|
@@ -1332,6 +619,29 @@ var SQLiteEventStore = class {
|
|
|
1332
619
|
};
|
|
1333
620
|
}
|
|
1334
621
|
}
|
|
622
|
+
/**
|
|
623
|
+
* Get session IDs that have events but no session_summary event.
|
|
624
|
+
* Used to backfill summaries for sessions that ended without Stop hook.
|
|
625
|
+
*/
|
|
626
|
+
async getSessionsWithoutSummary(currentSessionId, limit = 5) {
|
|
627
|
+
await this.initialize();
|
|
628
|
+
const rows = sqliteAll(
|
|
629
|
+
this.db,
|
|
630
|
+
`SELECT DISTINCT e.session_id
|
|
631
|
+
FROM events e
|
|
632
|
+
WHERE e.session_id != ?
|
|
633
|
+
AND e.event_type != 'session_summary'
|
|
634
|
+
AND e.session_id NOT IN (
|
|
635
|
+
SELECT DISTINCT session_id FROM events WHERE event_type = 'session_summary'
|
|
636
|
+
)
|
|
637
|
+
GROUP BY e.session_id
|
|
638
|
+
HAVING COUNT(*) >= 3
|
|
639
|
+
ORDER BY MAX(e.timestamp) DESC
|
|
640
|
+
LIMIT ?`,
|
|
641
|
+
[currentSessionId, limit]
|
|
642
|
+
);
|
|
643
|
+
return rows.map((r) => r.session_id);
|
|
644
|
+
}
|
|
1335
645
|
/**
|
|
1336
646
|
* Get events by session ID
|
|
1337
647
|
*/
|
|
@@ -1559,7 +869,7 @@ var SQLiteEventStore = class {
|
|
|
1559
869
|
*/
|
|
1560
870
|
async enqueueForEmbedding(eventId, content) {
|
|
1561
871
|
await this.initialize();
|
|
1562
|
-
const id =
|
|
872
|
+
const id = randomUUID();
|
|
1563
873
|
sqliteRun(
|
|
1564
874
|
this.db,
|
|
1565
875
|
`INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
|
|
@@ -1840,7 +1150,7 @@ var SQLiteEventStore = class {
|
|
|
1840
1150
|
if (this.readOnly)
|
|
1841
1151
|
return;
|
|
1842
1152
|
await this.initialize();
|
|
1843
|
-
const id =
|
|
1153
|
+
const id = randomUUID();
|
|
1844
1154
|
sqliteRun(
|
|
1845
1155
|
this.db,
|
|
1846
1156
|
`INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
|
|
@@ -1848,6 +1158,21 @@ var SQLiteEventStore = class {
|
|
|
1848
1158
|
[id, eventId, sessionId, score, query.slice(0, 100)]
|
|
1849
1159
|
);
|
|
1850
1160
|
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Get session IDs that have unevaluated retrievals (measured_at IS NULL).
|
|
1163
|
+
* Excludes the current session. Used to backfill sessions that ended without Stop hook.
|
|
1164
|
+
*/
|
|
1165
|
+
async getUnevaluatedSessions(currentSessionId, limit = 5) {
|
|
1166
|
+
await this.initialize();
|
|
1167
|
+
const rows = sqliteAll(
|
|
1168
|
+
this.db,
|
|
1169
|
+
`SELECT DISTINCT session_id FROM memory_helpfulness
|
|
1170
|
+
WHERE measured_at IS NULL AND session_id != ?
|
|
1171
|
+
ORDER BY created_at DESC LIMIT ?`,
|
|
1172
|
+
[currentSessionId, limit]
|
|
1173
|
+
);
|
|
1174
|
+
return rows.map((r) => r.session_id);
|
|
1175
|
+
}
|
|
1851
1176
|
/**
|
|
1852
1177
|
* Evaluate helpfulness for all retrievals in a session
|
|
1853
1178
|
* Called at session end - uses behavioral signals to compute score
|
|
@@ -2046,7 +1371,7 @@ var SQLiteEventStore = class {
|
|
|
2046
1371
|
}
|
|
2047
1372
|
async recordRetrievalTrace(input) {
|
|
2048
1373
|
await this.initialize();
|
|
2049
|
-
const traceId =
|
|
1374
|
+
const traceId = randomUUID();
|
|
2050
1375
|
sqliteRun(
|
|
2051
1376
|
this.db,
|
|
2052
1377
|
`INSERT INTO retrieval_traces (
|
|
@@ -2300,169 +1625,6 @@ var SQLiteEventStore = class {
|
|
|
2300
1625
|
}
|
|
2301
1626
|
};
|
|
2302
1627
|
|
|
2303
|
-
// src/core/sync-worker.ts
|
|
2304
|
-
var DEFAULT_CONFIG = {
|
|
2305
|
-
intervalMs: 3e4,
|
|
2306
|
-
batchSize: 500,
|
|
2307
|
-
maxRetries: 3,
|
|
2308
|
-
retryDelayMs: 5e3
|
|
2309
|
-
};
|
|
2310
|
-
var SyncWorker = class {
|
|
2311
|
-
constructor(sqliteStore, duckdbStore, config) {
|
|
2312
|
-
this.sqliteStore = sqliteStore;
|
|
2313
|
-
this.duckdbStore = duckdbStore;
|
|
2314
|
-
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
2315
|
-
}
|
|
2316
|
-
config;
|
|
2317
|
-
intervalHandle = null;
|
|
2318
|
-
running = false;
|
|
2319
|
-
stats = {
|
|
2320
|
-
lastSyncAt: null,
|
|
2321
|
-
eventsSynced: 0,
|
|
2322
|
-
sessionsSynced: 0,
|
|
2323
|
-
errors: 0,
|
|
2324
|
-
status: "idle"
|
|
2325
|
-
};
|
|
2326
|
-
/**
|
|
2327
|
-
* Start the sync worker
|
|
2328
|
-
*/
|
|
2329
|
-
start() {
|
|
2330
|
-
if (this.running)
|
|
2331
|
-
return;
|
|
2332
|
-
this.running = true;
|
|
2333
|
-
this.stats.status = "idle";
|
|
2334
|
-
this.syncNow().catch((err) => {
|
|
2335
|
-
console.error("[SyncWorker] Initial sync failed:", err);
|
|
2336
|
-
});
|
|
2337
|
-
this.intervalHandle = setInterval(() => {
|
|
2338
|
-
this.syncNow().catch((err) => {
|
|
2339
|
-
console.error("[SyncWorker] Periodic sync failed:", err);
|
|
2340
|
-
});
|
|
2341
|
-
}, this.config.intervalMs);
|
|
2342
|
-
}
|
|
2343
|
-
/**
|
|
2344
|
-
* Stop the sync worker
|
|
2345
|
-
*/
|
|
2346
|
-
stop() {
|
|
2347
|
-
this.running = false;
|
|
2348
|
-
this.stats.status = "stopped";
|
|
2349
|
-
if (this.intervalHandle) {
|
|
2350
|
-
clearInterval(this.intervalHandle);
|
|
2351
|
-
this.intervalHandle = null;
|
|
2352
|
-
}
|
|
2353
|
-
}
|
|
2354
|
-
/**
|
|
2355
|
-
* Trigger immediate sync
|
|
2356
|
-
*/
|
|
2357
|
-
async syncNow() {
|
|
2358
|
-
if (this.stats.status === "syncing") {
|
|
2359
|
-
return;
|
|
2360
|
-
}
|
|
2361
|
-
this.stats.status = "syncing";
|
|
2362
|
-
try {
|
|
2363
|
-
await this.syncEvents();
|
|
2364
|
-
await this.syncSessions();
|
|
2365
|
-
this.stats.lastSyncAt = /* @__PURE__ */ new Date();
|
|
2366
|
-
this.stats.status = "idle";
|
|
2367
|
-
} catch (error) {
|
|
2368
|
-
this.stats.errors++;
|
|
2369
|
-
this.stats.status = "error";
|
|
2370
|
-
throw error;
|
|
2371
|
-
}
|
|
2372
|
-
}
|
|
2373
|
-
/**
|
|
2374
|
-
* Sync events from SQLite to DuckDB
|
|
2375
|
-
*/
|
|
2376
|
-
async syncEvents() {
|
|
2377
|
-
const targetName = "duckdb_analytics";
|
|
2378
|
-
const position = await this.sqliteStore.getSyncPosition(targetName);
|
|
2379
|
-
const lastTimestamp = position.lastTimestamp || "1970-01-01T00:00:00.000Z";
|
|
2380
|
-
let hasMore = true;
|
|
2381
|
-
let totalSynced = 0;
|
|
2382
|
-
while (hasMore) {
|
|
2383
|
-
const events = await this.sqliteStore.getEventsSince(lastTimestamp, this.config.batchSize);
|
|
2384
|
-
if (events.length === 0) {
|
|
2385
|
-
hasMore = false;
|
|
2386
|
-
break;
|
|
2387
|
-
}
|
|
2388
|
-
await this.retryWithBackoff(async () => {
|
|
2389
|
-
for (const event of events) {
|
|
2390
|
-
await this.insertEventToDuckDB(event);
|
|
2391
|
-
}
|
|
2392
|
-
});
|
|
2393
|
-
totalSynced += events.length;
|
|
2394
|
-
const lastEvent = events[events.length - 1];
|
|
2395
|
-
await this.sqliteStore.updateSyncPosition(
|
|
2396
|
-
targetName,
|
|
2397
|
-
lastEvent.id,
|
|
2398
|
-
lastEvent.timestamp.toISOString()
|
|
2399
|
-
);
|
|
2400
|
-
hasMore = events.length === this.config.batchSize;
|
|
2401
|
-
}
|
|
2402
|
-
this.stats.eventsSynced += totalSynced;
|
|
2403
|
-
}
|
|
2404
|
-
/**
|
|
2405
|
-
* Sync sessions from SQLite to DuckDB
|
|
2406
|
-
*/
|
|
2407
|
-
async syncSessions() {
|
|
2408
|
-
const sessions = await this.sqliteStore.getAllSessions();
|
|
2409
|
-
for (const session of sessions) {
|
|
2410
|
-
await this.retryWithBackoff(async () => {
|
|
2411
|
-
await this.duckdbStore.upsertSession(session);
|
|
2412
|
-
});
|
|
2413
|
-
}
|
|
2414
|
-
this.stats.sessionsSynced = sessions.length;
|
|
2415
|
-
}
|
|
2416
|
-
/**
|
|
2417
|
-
* Insert a single event into DuckDB
|
|
2418
|
-
*/
|
|
2419
|
-
async insertEventToDuckDB(event) {
|
|
2420
|
-
await this.duckdbStore.append({
|
|
2421
|
-
eventType: event.eventType,
|
|
2422
|
-
sessionId: event.sessionId,
|
|
2423
|
-
timestamp: event.timestamp,
|
|
2424
|
-
content: event.content,
|
|
2425
|
-
metadata: event.metadata
|
|
2426
|
-
});
|
|
2427
|
-
}
|
|
2428
|
-
/**
|
|
2429
|
-
* Retry operation with exponential backoff
|
|
2430
|
-
*/
|
|
2431
|
-
async retryWithBackoff(fn) {
|
|
2432
|
-
let lastError = null;
|
|
2433
|
-
for (let attempt = 0; attempt < this.config.maxRetries; attempt++) {
|
|
2434
|
-
try {
|
|
2435
|
-
return await fn();
|
|
2436
|
-
} catch (error) {
|
|
2437
|
-
lastError = error instanceof Error ? error : new Error(String(error));
|
|
2438
|
-
if (attempt < this.config.maxRetries - 1) {
|
|
2439
|
-
const delay = this.config.retryDelayMs * Math.pow(2, attempt);
|
|
2440
|
-
await this.sleep(delay);
|
|
2441
|
-
}
|
|
2442
|
-
}
|
|
2443
|
-
}
|
|
2444
|
-
throw lastError;
|
|
2445
|
-
}
|
|
2446
|
-
/**
|
|
2447
|
-
* Sleep utility
|
|
2448
|
-
*/
|
|
2449
|
-
sleep(ms) {
|
|
2450
|
-
return new Promise((resolve5) => setTimeout(resolve5, ms));
|
|
2451
|
-
}
|
|
2452
|
-
/**
|
|
2453
|
-
* Get sync statistics
|
|
2454
|
-
*/
|
|
2455
|
-
getStats() {
|
|
2456
|
-
return { ...this.stats };
|
|
2457
|
-
}
|
|
2458
|
-
/**
|
|
2459
|
-
* Check if worker is running
|
|
2460
|
-
*/
|
|
2461
|
-
isRunning() {
|
|
2462
|
-
return this.running;
|
|
2463
|
-
}
|
|
2464
|
-
};
|
|
2465
|
-
|
|
2466
1628
|
// src/core/vector-store.ts
|
|
2467
1629
|
import * as lancedb from "@lancedb/lancedb";
|
|
2468
1630
|
var VectorStore = class {
|
|
@@ -2635,7 +1797,7 @@ var VectorStore = class {
|
|
|
2635
1797
|
|
|
2636
1798
|
// src/core/embedder.ts
|
|
2637
1799
|
import { pipeline } from "@huggingface/transformers";
|
|
2638
|
-
var Embedder = class {
|
|
1800
|
+
var Embedder = class _Embedder {
|
|
2639
1801
|
pipeline = null;
|
|
2640
1802
|
modelName;
|
|
2641
1803
|
activeModelName;
|
|
@@ -2666,6 +1828,11 @@ var Embedder = class {
|
|
|
2666
1828
|
this.initialized = true;
|
|
2667
1829
|
}
|
|
2668
1830
|
}
|
|
1831
|
+
// ~4 chars per token; 512 tokens * 4 = 2048, use 2000 to be safe
|
|
1832
|
+
static MAX_CHARS = 2e3;
|
|
1833
|
+
truncate(text) {
|
|
1834
|
+
return text.length > _Embedder.MAX_CHARS ? text.slice(0, _Embedder.MAX_CHARS) : text;
|
|
1835
|
+
}
|
|
2669
1836
|
/**
|
|
2670
1837
|
* Generate embedding for a single text
|
|
2671
1838
|
*/
|
|
@@ -2674,10 +1841,11 @@ var Embedder = class {
|
|
|
2674
1841
|
if (!this.pipeline) {
|
|
2675
1842
|
throw new Error("Embedding pipeline not initialized");
|
|
2676
1843
|
}
|
|
2677
|
-
const output = await this.pipeline(text, {
|
|
1844
|
+
const output = await this.pipeline(this.truncate(text), {
|
|
2678
1845
|
pooling: "mean",
|
|
2679
1846
|
normalize: true,
|
|
2680
|
-
truncation: true
|
|
1847
|
+
truncation: true,
|
|
1848
|
+
max_length: 512
|
|
2681
1849
|
});
|
|
2682
1850
|
const vector = Array.from(output.data);
|
|
2683
1851
|
return {
|
|
@@ -2699,10 +1867,11 @@ var Embedder = class {
|
|
|
2699
1867
|
for (let i = 0; i < texts.length; i += batchSize) {
|
|
2700
1868
|
const batch = texts.slice(i, i + batchSize);
|
|
2701
1869
|
for (const text of batch) {
|
|
2702
|
-
const output = await this.pipeline(text, {
|
|
1870
|
+
const output = await this.pipeline(this.truncate(text), {
|
|
2703
1871
|
pooling: "mean",
|
|
2704
1872
|
normalize: true,
|
|
2705
|
-
truncation: true
|
|
1873
|
+
truncation: true,
|
|
1874
|
+
max_length: 512
|
|
2706
1875
|
});
|
|
2707
1876
|
const vector = Array.from(output.data);
|
|
2708
1877
|
results.push({
|
|
@@ -2743,8 +1912,34 @@ function getDefaultEmbedder() {
|
|
|
2743
1912
|
return defaultEmbedder;
|
|
2744
1913
|
}
|
|
2745
1914
|
|
|
1915
|
+
// src/core/db-wrapper.ts
|
|
1916
|
+
import BetterSqlite3 from "better-sqlite3";
|
|
1917
|
+
function toDate(value) {
|
|
1918
|
+
if (value instanceof Date)
|
|
1919
|
+
return value;
|
|
1920
|
+
if (typeof value === "string")
|
|
1921
|
+
return new Date(value);
|
|
1922
|
+
if (typeof value === "number")
|
|
1923
|
+
return new Date(value);
|
|
1924
|
+
return new Date(String(value));
|
|
1925
|
+
}
|
|
1926
|
+
function createDatabase(dbPath, options) {
|
|
1927
|
+
return new BetterSqlite3(dbPath, { readonly: options?.readOnly });
|
|
1928
|
+
}
|
|
1929
|
+
function dbRun(db, sql, params = []) {
|
|
1930
|
+
db.prepare(sql).run(...params);
|
|
1931
|
+
return Promise.resolve();
|
|
1932
|
+
}
|
|
1933
|
+
function dbAll(db, sql, params = []) {
|
|
1934
|
+
return Promise.resolve(db.prepare(sql).all(...params));
|
|
1935
|
+
}
|
|
1936
|
+
function dbClose(db) {
|
|
1937
|
+
db.close();
|
|
1938
|
+
return Promise.resolve();
|
|
1939
|
+
}
|
|
1940
|
+
|
|
2746
1941
|
// src/core/vector-outbox.ts
|
|
2747
|
-
var
|
|
1942
|
+
var DEFAULT_CONFIG = {
|
|
2748
1943
|
embeddingVersion: "v1",
|
|
2749
1944
|
maxRetries: 3,
|
|
2750
1945
|
stuckThresholdMs: 5 * 60 * 1e3,
|
|
@@ -2753,7 +1948,7 @@ var DEFAULT_CONFIG2 = {
|
|
|
2753
1948
|
};
|
|
2754
1949
|
|
|
2755
1950
|
// src/core/vector-worker.ts
|
|
2756
|
-
var
|
|
1951
|
+
var DEFAULT_CONFIG2 = {
|
|
2757
1952
|
batchSize: 32,
|
|
2758
1953
|
pollIntervalMs: 1e3,
|
|
2759
1954
|
maxRetries: 3
|
|
@@ -2770,7 +1965,7 @@ var VectorWorker = class {
|
|
|
2770
1965
|
this.eventStore = eventStore;
|
|
2771
1966
|
this.vectorStore = vectorStore;
|
|
2772
1967
|
this.embedder = embedder;
|
|
2773
|
-
this.config = { ...
|
|
1968
|
+
this.config = { ...DEFAULT_CONFIG2, ...config };
|
|
2774
1969
|
}
|
|
2775
1970
|
/**
|
|
2776
1971
|
* Start the worker polling loop
|
|
@@ -2891,7 +2086,7 @@ function createVectorWorker(eventStore, vectorStore, embedder, config) {
|
|
|
2891
2086
|
}
|
|
2892
2087
|
|
|
2893
2088
|
// src/core/matcher.ts
|
|
2894
|
-
var
|
|
2089
|
+
var DEFAULT_CONFIG3 = {
|
|
2895
2090
|
weights: {
|
|
2896
2091
|
semanticSimilarity: 0.4,
|
|
2897
2092
|
ftsScore: 0.25,
|
|
@@ -2906,9 +2101,9 @@ var Matcher = class {
|
|
|
2906
2101
|
config;
|
|
2907
2102
|
constructor(config = {}) {
|
|
2908
2103
|
this.config = {
|
|
2909
|
-
...
|
|
2104
|
+
...DEFAULT_CONFIG3,
|
|
2910
2105
|
...config,
|
|
2911
|
-
weights: { ...
|
|
2106
|
+
weights: { ...DEFAULT_CONFIG3.weights, ...config.weights }
|
|
2912
2107
|
};
|
|
2913
2108
|
}
|
|
2914
2109
|
/**
|
|
@@ -3898,7 +3093,7 @@ function createSharedEventStore(dbPath) {
|
|
|
3898
3093
|
}
|
|
3899
3094
|
|
|
3900
3095
|
// src/core/shared-store.ts
|
|
3901
|
-
import { randomUUID as
|
|
3096
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
3902
3097
|
var SharedStore = class {
|
|
3903
3098
|
constructor(sharedEventStore) {
|
|
3904
3099
|
this.sharedEventStore = sharedEventStore;
|
|
@@ -3910,7 +3105,7 @@ var SharedStore = class {
|
|
|
3910
3105
|
* Promote a verified troubleshooting entry to shared storage
|
|
3911
3106
|
*/
|
|
3912
3107
|
async promoteEntry(input) {
|
|
3913
|
-
const entryId =
|
|
3108
|
+
const entryId = randomUUID2();
|
|
3914
3109
|
await dbRun(
|
|
3915
3110
|
this.db,
|
|
3916
3111
|
`INSERT INTO shared_troubleshooting (
|
|
@@ -4275,7 +3470,7 @@ function createSharedVectorStore(dbPath) {
|
|
|
4275
3470
|
}
|
|
4276
3471
|
|
|
4277
3472
|
// src/core/shared-promoter.ts
|
|
4278
|
-
import { randomUUID as
|
|
3473
|
+
import { randomUUID as randomUUID3 } from "crypto";
|
|
4279
3474
|
var SharedPromoter = class {
|
|
4280
3475
|
constructor(sharedStore, sharedVectorStore, embedder, config) {
|
|
4281
3476
|
this.sharedStore = sharedStore;
|
|
@@ -4343,7 +3538,7 @@ var SharedPromoter = class {
|
|
|
4343
3538
|
const embeddingContent = this.createEmbeddingContent(input);
|
|
4344
3539
|
const embedding = await this.embedder.embed(embeddingContent);
|
|
4345
3540
|
await this.sharedVectorStore.upsert({
|
|
4346
|
-
id:
|
|
3541
|
+
id: randomUUID3(),
|
|
4347
3542
|
entryId,
|
|
4348
3543
|
entryType: "troubleshooting",
|
|
4349
3544
|
content: embeddingContent,
|
|
@@ -4483,7 +3678,7 @@ function createToolObservationEmbedding(toolName, metadata, success) {
|
|
|
4483
3678
|
}
|
|
4484
3679
|
|
|
4485
3680
|
// src/core/working-set-store.ts
|
|
4486
|
-
import { randomUUID as
|
|
3681
|
+
import { randomUUID as randomUUID4 } from "crypto";
|
|
4487
3682
|
var WorkingSetStore = class {
|
|
4488
3683
|
constructor(eventStore, config) {
|
|
4489
3684
|
this.eventStore = eventStore;
|
|
@@ -4504,7 +3699,7 @@ var WorkingSetStore = class {
|
|
|
4504
3699
|
`INSERT OR REPLACE INTO working_set (id, event_id, added_at, relevance_score, topics, expires_at)
|
|
4505
3700
|
VALUES (?, ?, CURRENT_TIMESTAMP, ?, ?, ?)`,
|
|
4506
3701
|
[
|
|
4507
|
-
|
|
3702
|
+
randomUUID4(),
|
|
4508
3703
|
eventId,
|
|
4509
3704
|
relevanceScore,
|
|
4510
3705
|
JSON.stringify(topics || []),
|
|
@@ -4688,7 +3883,7 @@ function createWorkingSetStore(eventStore, config) {
|
|
|
4688
3883
|
}
|
|
4689
3884
|
|
|
4690
3885
|
// src/core/consolidated-store.ts
|
|
4691
|
-
import { randomUUID as
|
|
3886
|
+
import { randomUUID as randomUUID5 } from "crypto";
|
|
4692
3887
|
var ConsolidatedStore = class {
|
|
4693
3888
|
constructor(eventStore) {
|
|
4694
3889
|
this.eventStore = eventStore;
|
|
@@ -4700,7 +3895,7 @@ var ConsolidatedStore = class {
|
|
|
4700
3895
|
* Create a new consolidated memory
|
|
4701
3896
|
*/
|
|
4702
3897
|
async create(input) {
|
|
4703
|
-
const memoryId =
|
|
3898
|
+
const memoryId = randomUUID5();
|
|
4704
3899
|
await dbRun(
|
|
4705
3900
|
this.db,
|
|
4706
3901
|
`INSERT INTO consolidated_memories
|
|
@@ -4830,7 +4025,7 @@ var ConsolidatedStore = class {
|
|
|
4830
4025
|
* Create a long-term rule promoted from stable summaries
|
|
4831
4026
|
*/
|
|
4832
4027
|
async createRule(input) {
|
|
4833
|
-
const ruleId =
|
|
4028
|
+
const ruleId = randomUUID5();
|
|
4834
4029
|
await dbRun(
|
|
4835
4030
|
this.db,
|
|
4836
4031
|
`INSERT INTO consolidated_rules
|
|
@@ -5352,7 +4547,7 @@ function createConsolidationWorker(workingSetStore, consolidatedStore, config) {
|
|
|
5352
4547
|
}
|
|
5353
4548
|
|
|
5354
4549
|
// src/core/continuity-manager.ts
|
|
5355
|
-
import { randomUUID as
|
|
4550
|
+
import { randomUUID as randomUUID6 } from "crypto";
|
|
5356
4551
|
var ContinuityManager = class {
|
|
5357
4552
|
constructor(eventStore, config) {
|
|
5358
4553
|
this.eventStore = eventStore;
|
|
@@ -5506,7 +4701,7 @@ var ContinuityManager = class {
|
|
|
5506
4701
|
`INSERT INTO continuity_log
|
|
5507
4702
|
(log_id, from_context_id, to_context_id, continuity_score, transition_type, created_at)
|
|
5508
4703
|
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
|
|
5509
|
-
[
|
|
4704
|
+
[randomUUID6(), previous.id, current.id, score, type]
|
|
5510
4705
|
);
|
|
5511
4706
|
}
|
|
5512
4707
|
/**
|
|
@@ -5615,7 +4810,7 @@ function createContinuityManager(eventStore, config) {
|
|
|
5615
4810
|
}
|
|
5616
4811
|
|
|
5617
4812
|
// src/core/graduation-worker.ts
|
|
5618
|
-
var
|
|
4813
|
+
var DEFAULT_CONFIG4 = {
|
|
5619
4814
|
evaluationIntervalMs: 3e5,
|
|
5620
4815
|
// 5 minutes
|
|
5621
4816
|
batchSize: 50,
|
|
@@ -5623,7 +4818,7 @@ var DEFAULT_CONFIG5 = {
|
|
|
5623
4818
|
// 1 hour cooldown between evaluations
|
|
5624
4819
|
};
|
|
5625
4820
|
var GraduationWorker = class {
|
|
5626
|
-
constructor(eventStore, graduation, config =
|
|
4821
|
+
constructor(eventStore, graduation, config = DEFAULT_CONFIG4) {
|
|
5627
4822
|
this.eventStore = eventStore;
|
|
5628
4823
|
this.graduation = graduation;
|
|
5629
4824
|
this.config = config;
|
|
@@ -5731,7 +4926,7 @@ function createGraduationWorker(eventStore, graduation, config) {
|
|
|
5731
4926
|
return new GraduationWorker(
|
|
5732
4927
|
eventStore,
|
|
5733
4928
|
graduation,
|
|
5734
|
-
{ ...
|
|
4929
|
+
{ ...DEFAULT_CONFIG4, ...config }
|
|
5735
4930
|
);
|
|
5736
4931
|
}
|
|
5737
4932
|
|
|
@@ -5965,9 +5160,6 @@ function registerSession(sessionId, projectPath) {
|
|
|
5965
5160
|
var MemoryService = class {
|
|
5966
5161
|
// Primary store: SQLite (WAL mode) - for hooks, always available
|
|
5967
5162
|
sqliteStore;
|
|
5968
|
-
// Analytics store: DuckDB - for server reads (optional, synced from SQLite)
|
|
5969
|
-
analyticsStore;
|
|
5970
|
-
syncWorker = null;
|
|
5971
5163
|
vectorStore;
|
|
5972
5164
|
embedder;
|
|
5973
5165
|
matcher;
|
|
@@ -5993,6 +5185,7 @@ var MemoryService = class {
|
|
|
5993
5185
|
projectPath = null;
|
|
5994
5186
|
readOnly;
|
|
5995
5187
|
lightweightMode;
|
|
5188
|
+
embeddingOnly;
|
|
5996
5189
|
mdMirror;
|
|
5997
5190
|
storagePath;
|
|
5998
5191
|
constructor(config) {
|
|
@@ -6000,6 +5193,7 @@ var MemoryService = class {
|
|
|
6000
5193
|
this.storagePath = storagePath;
|
|
6001
5194
|
this.readOnly = config.readOnly ?? false;
|
|
6002
5195
|
this.lightweightMode = config.lightweightMode ?? false;
|
|
5196
|
+
this.embeddingOnly = config.embeddingOnly ?? false;
|
|
6003
5197
|
this.mdMirror = new MarkdownMirror2(process.cwd());
|
|
6004
5198
|
if (!this.readOnly && !fs4.existsSync(storagePath)) {
|
|
6005
5199
|
fs4.mkdirSync(storagePath, { recursive: true });
|
|
@@ -6014,24 +5208,6 @@ var MemoryService = class {
|
|
|
6014
5208
|
markdownMirrorRoot: storagePath
|
|
6015
5209
|
}
|
|
6016
5210
|
);
|
|
6017
|
-
const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
|
|
6018
|
-
if (!analyticsEnabled) {
|
|
6019
|
-
this.analyticsStore = null;
|
|
6020
|
-
} else if (this.readOnly) {
|
|
6021
|
-
try {
|
|
6022
|
-
this.analyticsStore = new EventStore(
|
|
6023
|
-
path3.join(storagePath, "analytics.duckdb"),
|
|
6024
|
-
{ readOnly: true }
|
|
6025
|
-
);
|
|
6026
|
-
} catch {
|
|
6027
|
-
this.analyticsStore = null;
|
|
6028
|
-
}
|
|
6029
|
-
} else {
|
|
6030
|
-
this.analyticsStore = new EventStore(
|
|
6031
|
-
path3.join(storagePath, "analytics.duckdb"),
|
|
6032
|
-
{ readOnly: false }
|
|
6033
|
-
);
|
|
6034
|
-
}
|
|
6035
5211
|
this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
|
|
6036
5212
|
const embeddingModel = config.embeddingModel || process.env.CLAUDE_MEMORY_EMBEDDING_MODEL;
|
|
6037
5213
|
this.embedder = embeddingModel ? new Embedder(embeddingModel) : getDefaultEmbedder();
|
|
@@ -6057,13 +5233,6 @@ var MemoryService = class {
|
|
|
6057
5233
|
this.initialized = true;
|
|
6058
5234
|
return;
|
|
6059
5235
|
}
|
|
6060
|
-
if (this.analyticsStore) {
|
|
6061
|
-
try {
|
|
6062
|
-
await this.analyticsStore.initialize();
|
|
6063
|
-
} catch (error) {
|
|
6064
|
-
console.warn("[MemoryService] Analytics store (DuckDB) initialization failed, using SQLite for reads:", error);
|
|
6065
|
-
}
|
|
6066
|
-
}
|
|
6067
5236
|
await this.vectorStore.initialize();
|
|
6068
5237
|
await this.embedder.initialize();
|
|
6069
5238
|
if (!this.readOnly) {
|
|
@@ -6073,19 +5242,13 @@ var MemoryService = class {
|
|
|
6073
5242
|
this.embedder
|
|
6074
5243
|
);
|
|
6075
5244
|
this.vectorWorker.start();
|
|
6076
|
-
|
|
6077
|
-
|
|
6078
|
-
this.
|
|
6079
|
-
this.graduation
|
|
6080
|
-
);
|
|
6081
|
-
this.graduationWorker.start();
|
|
6082
|
-
if (this.analyticsStore) {
|
|
6083
|
-
this.syncWorker = new SyncWorker(
|
|
5245
|
+
if (!this.embeddingOnly) {
|
|
5246
|
+
this.retriever.setGraduationPipeline(this.graduation);
|
|
5247
|
+
this.graduationWorker = createGraduationWorker(
|
|
6084
5248
|
this.sqliteStore,
|
|
6085
|
-
this.
|
|
6086
|
-
{ intervalMs: 3e4, batchSize: 500 }
|
|
5249
|
+
this.graduation
|
|
6087
5250
|
);
|
|
6088
|
-
this.
|
|
5251
|
+
this.graduationWorker.start();
|
|
6089
5252
|
}
|
|
6090
5253
|
const savedMode = await this.sqliteStore.getEndlessConfig("mode");
|
|
6091
5254
|
if (savedMode === "endless") {
|
|
@@ -6282,6 +5445,57 @@ var MemoryService = class {
|
|
|
6282
5445
|
}
|
|
6283
5446
|
);
|
|
6284
5447
|
}
|
|
5448
|
+
/**
|
|
5449
|
+
* Backfill session summaries for recent sessions that are missing them.
|
|
5450
|
+
* Called from session-start hook to catch sessions that ended without Stop hook.
|
|
5451
|
+
*/
|
|
5452
|
+
async backfillMissingSummaries(currentSessionId, limit = 5) {
|
|
5453
|
+
await this.initialize();
|
|
5454
|
+
const recentSessionIds = await this.sqliteStore.getSessionsWithoutSummary(currentSessionId, limit);
|
|
5455
|
+
for (const sid of recentSessionIds) {
|
|
5456
|
+
try {
|
|
5457
|
+
await this.generateSessionSummary(sid);
|
|
5458
|
+
} catch {
|
|
5459
|
+
}
|
|
5460
|
+
}
|
|
5461
|
+
}
|
|
5462
|
+
/**
|
|
5463
|
+
* Generate a rule-based session summary from stored events.
|
|
5464
|
+
* Called at session end (Stop hook) when no LLM-generated summary exists.
|
|
5465
|
+
* Skips if a summary already exists for this session.
|
|
5466
|
+
*/
|
|
5467
|
+
async generateSessionSummary(sessionId) {
|
|
5468
|
+
await this.initialize();
|
|
5469
|
+
const events = await this.sqliteStore.getSessionEvents(sessionId);
|
|
5470
|
+
if (events.length < 3)
|
|
5471
|
+
return;
|
|
5472
|
+
const hasSummary = events.some((e) => e.eventType === "session_summary");
|
|
5473
|
+
if (hasSummary)
|
|
5474
|
+
return;
|
|
5475
|
+
const prompts = events.filter((e) => e.eventType === "user_prompt");
|
|
5476
|
+
const toolObs = events.filter((e) => e.eventType === "tool_observation");
|
|
5477
|
+
const toolNames = [...new Set(
|
|
5478
|
+
toolObs.map((e) => e.metadata?.toolName).filter(Boolean)
|
|
5479
|
+
)];
|
|
5480
|
+
const errorObs = toolObs.filter((e) => {
|
|
5481
|
+
const meta = e.metadata;
|
|
5482
|
+
return meta?.exitCode !== void 0 && meta.exitCode !== 0;
|
|
5483
|
+
});
|
|
5484
|
+
const datePart = events[0].timestamp.toISOString().split("T")[0];
|
|
5485
|
+
const parts = [`[${datePart}] ${prompts.length}\uD134 \uC138\uC158.`];
|
|
5486
|
+
if (prompts.length > 0) {
|
|
5487
|
+
const firstPrompt = prompts[0].content.slice(0, 120).replace(/\n/g, " ");
|
|
5488
|
+
parts.push(`\uC8FC\uC694 \uC791\uC5C5: ${firstPrompt}`);
|
|
5489
|
+
}
|
|
5490
|
+
if (toolNames.length > 0) {
|
|
5491
|
+
parts.push(`\uC0AC\uC6A9 \uD234: ${toolNames.slice(0, 6).join(", ")}`);
|
|
5492
|
+
}
|
|
5493
|
+
if (errorObs.length > 0) {
|
|
5494
|
+
parts.push(`\uC624\uB958 ${errorObs.length}\uAC74 \uBC1C\uC0DD`);
|
|
5495
|
+
}
|
|
5496
|
+
const summary = parts.join(". ");
|
|
5497
|
+
await this.storeSessionSummary(sessionId, summary, { generated: "rule-based", eventCount: events.length });
|
|
5498
|
+
}
|
|
6285
5499
|
/**
|
|
6286
5500
|
* Store a tool observation
|
|
6287
5501
|
*/
|
|
@@ -6809,6 +6023,20 @@ var MemoryService = class {
|
|
|
6809
6023
|
await this.initialize();
|
|
6810
6024
|
await this.sqliteStore.recordRetrieval(eventId, sessionId, score, query);
|
|
6811
6025
|
}
|
|
6026
|
+
/**
|
|
6027
|
+
* Record a query-level retrieval trace (used by user-prompt-submit hook).
|
|
6028
|
+
* Feeds the retrieval_traces table that powers dashboard stats.
|
|
6029
|
+
*/
|
|
6030
|
+
async recordQueryTrace(input) {
|
|
6031
|
+
await this.initialize();
|
|
6032
|
+
await this.sqliteStore.recordRetrievalTrace({
|
|
6033
|
+
...input,
|
|
6034
|
+
projectHash: this.projectHash || void 0,
|
|
6035
|
+
candidateDetails: [],
|
|
6036
|
+
selectedDetails: [],
|
|
6037
|
+
fallbackTrace: []
|
|
6038
|
+
});
|
|
6039
|
+
}
|
|
6812
6040
|
/**
|
|
6813
6041
|
* Evaluate helpfulness of retrievals in a session (called at session end)
|
|
6814
6042
|
*/
|
|
@@ -6816,6 +6044,20 @@ var MemoryService = class {
|
|
|
6816
6044
|
await this.initialize();
|
|
6817
6045
|
await this.sqliteStore.evaluateSessionHelpfulness(sessionId);
|
|
6818
6046
|
}
|
|
6047
|
+
/**
|
|
6048
|
+
* Backfill helpfulness evaluation for sessions that ended without Stop hook.
|
|
6049
|
+
* Call on first turn of a new session to catch missed evaluations.
|
|
6050
|
+
*/
|
|
6051
|
+
async evaluatePendingSessions(currentSessionId) {
|
|
6052
|
+
await this.initialize();
|
|
6053
|
+
const sessions = await this.sqliteStore.getUnevaluatedSessions(currentSessionId, 5);
|
|
6054
|
+
for (const sid of sessions) {
|
|
6055
|
+
try {
|
|
6056
|
+
await this.sqliteStore.evaluateSessionHelpfulness(sid);
|
|
6057
|
+
} catch {
|
|
6058
|
+
}
|
|
6059
|
+
}
|
|
6060
|
+
}
|
|
6819
6061
|
/**
|
|
6820
6062
|
* Get most helpful memories ranked by helpfulness score
|
|
6821
6063
|
*/
|
|
@@ -7080,16 +6322,10 @@ var MemoryService = class {
|
|
|
7080
6322
|
if (this.vectorWorker) {
|
|
7081
6323
|
this.vectorWorker.stop();
|
|
7082
6324
|
}
|
|
7083
|
-
if (this.syncWorker) {
|
|
7084
|
-
this.syncWorker.stop();
|
|
7085
|
-
}
|
|
7086
6325
|
if (this.sharedEventStore) {
|
|
7087
6326
|
await this.sharedEventStore.close();
|
|
7088
6327
|
}
|
|
7089
6328
|
await this.sqliteStore.close();
|
|
7090
|
-
if (this.analyticsStore) {
|
|
7091
|
-
await this.analyticsStore.close();
|
|
7092
|
-
}
|
|
7093
6329
|
}
|
|
7094
6330
|
/**
|
|
7095
6331
|
* Expand ~ to home directory
|
|
@@ -7147,7 +6383,17 @@ import * as fs5 from "fs";
|
|
|
7147
6383
|
import * as path4 from "path";
|
|
7148
6384
|
import * as os2 from "os";
|
|
7149
6385
|
import * as readline from "readline";
|
|
7150
|
-
import { randomUUID as
|
|
6386
|
+
import { randomUUID as randomUUID8 } from "crypto";
|
|
6387
|
+
function isWorthStoringPrompt(content) {
|
|
6388
|
+
const trimmed = content.trim();
|
|
6389
|
+
if (trimmed.startsWith("/"))
|
|
6390
|
+
return false;
|
|
6391
|
+
if (trimmed.length < 15)
|
|
6392
|
+
return false;
|
|
6393
|
+
if (!/[a-zA-Z가-힣]{2,}/.test(trimmed))
|
|
6394
|
+
return false;
|
|
6395
|
+
return true;
|
|
6396
|
+
}
|
|
7151
6397
|
function classifyEntry(entry) {
|
|
7152
6398
|
if (entry.type !== "user" && entry.type !== "assistant") {
|
|
7153
6399
|
return "skip";
|
|
@@ -7324,7 +6570,11 @@ var SessionHistoryImporter = class {
|
|
|
7324
6570
|
const content = this.extractContent(entry);
|
|
7325
6571
|
if (!content)
|
|
7326
6572
|
continue;
|
|
7327
|
-
|
|
6573
|
+
if (!isWorthStoringPrompt(content)) {
|
|
6574
|
+
result.skippedDuplicates++;
|
|
6575
|
+
continue;
|
|
6576
|
+
}
|
|
6577
|
+
currentTurnId = randomUUID8();
|
|
7328
6578
|
const appendResult = await this.memoryService.storeUserPrompt(
|
|
7329
6579
|
sessionId,
|
|
7330
6580
|
content,
|
|
@@ -9365,7 +8615,7 @@ if (isMainModule) {
|
|
|
9365
8615
|
}
|
|
9366
8616
|
|
|
9367
8617
|
// src/core/mongo-sync-worker.ts
|
|
9368
|
-
import { randomUUID as
|
|
8618
|
+
import { randomUUID as randomUUID9 } from "crypto";
|
|
9369
8619
|
import * as os5 from "os";
|
|
9370
8620
|
import { MongoClient } from "mongodb";
|
|
9371
8621
|
function redactMongoUri(uri) {
|
|
@@ -9399,7 +8649,7 @@ var MongoSyncWorker = class {
|
|
|
9399
8649
|
direction: config.direction ?? "both",
|
|
9400
8650
|
intervalMs: config.intervalMs ?? 3e4,
|
|
9401
8651
|
batchSize: config.batchSize ?? 500,
|
|
9402
|
-
instanceId: config.instanceId ??
|
|
8652
|
+
instanceId: config.instanceId ?? randomUUID9()
|
|
9403
8653
|
};
|
|
9404
8654
|
}
|
|
9405
8655
|
config;
|
|
@@ -9711,7 +8961,7 @@ function getHooksConfig(pluginPath) {
|
|
|
9711
8961
|
};
|
|
9712
8962
|
}
|
|
9713
8963
|
var program = new Command();
|
|
9714
|
-
program.name("claude-memory-layer").description("Claude Code Memory Plugin CLI").version("1.0.
|
|
8964
|
+
program.name("claude-memory-layer").description("Claude Code Memory Plugin CLI").version("1.0.24");
|
|
9715
8965
|
program.command("install").description("Install hooks into Claude Code settings").option("--path <path>", "Custom plugin path (defaults to auto-detect)").action(async (options) => {
|
|
9716
8966
|
try {
|
|
9717
8967
|
const pluginPath = options.path || getPluginPath();
|
|
@@ -9912,6 +9162,7 @@ program.command("process").description("Process pending embeddings").option("-p,
|
|
|
9912
9162
|
const projectPath = options.project || process.cwd();
|
|
9913
9163
|
const service = getMemoryServiceForProject(projectPath);
|
|
9914
9164
|
try {
|
|
9165
|
+
await service.initialize();
|
|
9915
9166
|
console.log("\u23F3 Processing pending embeddings...");
|
|
9916
9167
|
const count = await service.processPendingEmbeddings();
|
|
9917
9168
|
console.log(`\u2705 Processed ${count} embeddings`);
|