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.
Files changed (58) hide show
  1. package/.claude/settings.local.json +25 -0
  2. package/README.md +2 -0
  3. package/dist/cli/index.js +229 -978
  4. package/dist/cli/index.js.map +4 -4
  5. package/dist/core/index.js +59 -71
  6. package/dist/core/index.js.map +3 -3
  7. package/dist/hooks/post-tool-use.js +287 -976
  8. package/dist/hooks/post-tool-use.js.map +4 -4
  9. package/dist/hooks/semantic-daemon.js +6520 -0
  10. package/dist/hooks/semantic-daemon.js.map +7 -0
  11. package/dist/hooks/session-end.js +209 -973
  12. package/dist/hooks/session-end.js.map +4 -4
  13. package/dist/hooks/session-start.js +293 -978
  14. package/dist/hooks/session-start.js.map +4 -4
  15. package/dist/hooks/stop.js +247 -975
  16. package/dist/hooks/stop.js.map +4 -4
  17. package/dist/hooks/user-prompt-submit.js +406 -1036
  18. package/dist/hooks/user-prompt-submit.js.map +4 -4
  19. package/dist/server/api/index.js +209 -973
  20. package/dist/server/api/index.js.map +4 -4
  21. package/dist/server/index.js +209 -973
  22. package/dist/server/index.js.map +4 -4
  23. package/dist/services/memory-service.js +209 -973
  24. package/dist/services/memory-service.js.map +4 -4
  25. package/dist/ui/app.js +48 -1
  26. package/dist/ui/index.html +11 -3
  27. package/memory/_index.md +1 -0
  28. package/memory/agent_response/uncategorized/2026-03-04.md +1314 -1
  29. package/memory/session_summary/uncategorized/2026-03-04.md +50 -0
  30. package/memory/tool_observation/uncategorized/2026-03-04.md +969 -1
  31. package/memory/user_prompt/uncategorized/2026-03-04.md +555 -1
  32. package/package.json +1 -2
  33. package/scripts/build.ts +2 -1
  34. package/specs/memory-utilization-improvements/context.md +145 -0
  35. package/specs/memory-utilization-improvements/plan.md +361 -0
  36. package/specs/memory-utilization-improvements/spec.md +308 -0
  37. package/specs/optional-duckdb/context.md +77 -0
  38. package/specs/optional-duckdb/plan.md +142 -0
  39. package/specs/optional-duckdb/spec.md +35 -0
  40. package/specs/selective-tool-observation/context.md +100 -0
  41. package/specs/selective-tool-observation/plan.md +158 -0
  42. package/specs/selective-tool-observation/spec.md +127 -0
  43. package/src/cli/index.ts +1 -0
  44. package/src/core/db-wrapper.ts +18 -73
  45. package/src/core/embedder.ts +13 -4
  46. package/src/core/sqlite-event-store.ts +40 -0
  47. package/src/core/turn-state.ts +48 -0
  48. package/src/core/types.ts +1 -0
  49. package/src/hooks/post-tool-use.ts +72 -2
  50. package/src/hooks/semantic-daemon-client.ts +208 -0
  51. package/src/hooks/semantic-daemon.ts +276 -0
  52. package/src/hooks/session-start.ts +11 -0
  53. package/src/hooks/stop.ts +33 -4
  54. package/src/hooks/user-prompt-submit.ts +48 -40
  55. package/src/services/memory-service.ts +112 -65
  56. package/src/services/session-history-importer.ts +18 -0
  57. package/src/ui/app.js +48 -1
  58. package/src/ui/index.html +11 -3
@@ -37,744 +37,31 @@ import * as os from "os";
37
37
  import * as fs4 from "fs";
38
38
  import * as crypto2 from "crypto";
39
39
 
40
- // src/core/event-store.ts
41
- import { randomUUID } from "crypto";
42
-
43
- // src/core/canonical-key.ts
44
- import { createHash } from "crypto";
45
- var MAX_KEY_LENGTH = 200;
46
- function makeCanonicalKey(title, context) {
47
- let normalized = title.normalize("NFKC");
48
- normalized = normalized.toLowerCase();
49
- normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
50
- normalized = normalized.replace(/\s+/g, " ").trim();
51
- let key = normalized;
52
- if (context?.project) {
53
- key = `${context.project}::${key}`;
54
- }
55
- if (key.length > MAX_KEY_LENGTH) {
56
- const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
57
- key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
58
- }
59
- return key;
60
- }
61
- function makeDedupeKey(content, sessionId) {
62
- const contentHash = createHash("sha256").update(content).digest("hex");
63
- return `${sessionId}:${contentHash}`;
64
- }
65
-
66
- // src/core/db-wrapper.ts
67
- import duckdb from "duckdb";
68
- function convertBigInts(obj) {
69
- if (obj === null || obj === void 0)
70
- return obj;
71
- if (typeof obj === "bigint")
72
- return Number(obj);
73
- if (obj instanceof Date)
74
- return obj;
75
- if (Array.isArray(obj))
76
- return obj.map(convertBigInts);
77
- if (typeof obj === "object") {
78
- const result = {};
79
- for (const [key, value] of Object.entries(obj)) {
80
- result[key] = convertBigInts(value);
81
- }
82
- return result;
83
- }
84
- return obj;
85
- }
86
- function toDate(value) {
87
- if (value instanceof Date)
88
- return value;
89
- if (typeof value === "string")
90
- return new Date(value);
91
- if (typeof value === "number")
92
- return new Date(value);
93
- return new Date(String(value));
94
- }
95
- function createDatabase(path8, options) {
96
- if (options?.readOnly) {
97
- return new duckdb.Database(path8, { access_mode: "READ_ONLY" });
98
- }
99
- return new duckdb.Database(path8);
100
- }
101
- function dbRun(db, sql, params = []) {
102
- return new Promise((resolve3, reject) => {
103
- if (params.length === 0) {
104
- db.run(sql, (err) => {
105
- if (err)
106
- reject(err);
107
- else
108
- resolve3();
109
- });
110
- } else {
111
- db.run(sql, ...params, (err) => {
112
- if (err)
113
- reject(err);
114
- else
115
- resolve3();
116
- });
117
- }
118
- });
119
- }
120
- function dbAll(db, sql, params = []) {
121
- return new Promise((resolve3, reject) => {
122
- if (params.length === 0) {
123
- db.all(sql, (err, rows) => {
124
- if (err)
125
- reject(err);
126
- else
127
- resolve3(convertBigInts(rows || []));
128
- });
129
- } else {
130
- db.all(sql, ...params, (err, rows) => {
131
- if (err)
132
- reject(err);
133
- else
134
- resolve3(convertBigInts(rows || []));
135
- });
136
- }
137
- });
138
- }
139
- function dbClose(db) {
140
- return new Promise((resolve3, reject) => {
141
- db.close((err) => {
142
- if (err)
143
- reject(err);
144
- else
145
- resolve3();
146
- });
147
- });
148
- }
149
-
150
- // src/core/event-store.ts
151
- var EventStore = class {
152
- constructor(dbPath, options) {
153
- this.dbPath = dbPath;
154
- this.readOnly = options?.readOnly ?? false;
155
- this.db = createDatabase(dbPath, { readOnly: this.readOnly });
156
- }
157
- db;
158
- initialized = false;
159
- readOnly;
160
- /**
161
- * Initialize database schema
162
- */
163
- async initialize() {
164
- if (this.initialized)
165
- return;
166
- if (this.readOnly) {
167
- this.initialized = true;
168
- return;
169
- }
170
- await dbRun(this.db, `
171
- CREATE TABLE IF NOT EXISTS events (
172
- id VARCHAR PRIMARY KEY,
173
- event_type VARCHAR NOT NULL,
174
- session_id VARCHAR NOT NULL,
175
- timestamp TIMESTAMP NOT NULL,
176
- content TEXT NOT NULL,
177
- canonical_key VARCHAR NOT NULL,
178
- dedupe_key VARCHAR UNIQUE,
179
- metadata JSON
180
- )
181
- `);
182
- await dbRun(this.db, `
183
- CREATE TABLE IF NOT EXISTS event_dedup (
184
- dedupe_key VARCHAR PRIMARY KEY,
185
- event_id VARCHAR NOT NULL,
186
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
187
- )
188
- `);
189
- await dbRun(this.db, `
190
- CREATE TABLE IF NOT EXISTS sessions (
191
- id VARCHAR PRIMARY KEY,
192
- started_at TIMESTAMP NOT NULL,
193
- ended_at TIMESTAMP,
194
- project_path VARCHAR,
195
- summary TEXT,
196
- tags JSON
197
- )
198
- `);
199
- await dbRun(this.db, `
200
- CREATE TABLE IF NOT EXISTS insights (
201
- id VARCHAR PRIMARY KEY,
202
- insight_type VARCHAR NOT NULL,
203
- content TEXT NOT NULL,
204
- canonical_key VARCHAR NOT NULL,
205
- confidence FLOAT,
206
- source_events JSON,
207
- created_at TIMESTAMP,
208
- last_updated TIMESTAMP
209
- )
210
- `);
211
- await dbRun(this.db, `
212
- CREATE TABLE IF NOT EXISTS embedding_outbox (
213
- id VARCHAR PRIMARY KEY,
214
- event_id VARCHAR NOT NULL,
215
- content TEXT NOT NULL,
216
- status VARCHAR DEFAULT 'pending',
217
- retry_count INT DEFAULT 0,
218
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
219
- processed_at TIMESTAMP,
220
- error_message TEXT
221
- )
222
- `);
223
- await dbRun(this.db, `
224
- CREATE TABLE IF NOT EXISTS projection_offsets (
225
- projection_name VARCHAR PRIMARY KEY,
226
- last_event_id VARCHAR,
227
- last_timestamp TIMESTAMP,
228
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
229
- )
230
- `);
231
- await dbRun(this.db, `
232
- CREATE TABLE IF NOT EXISTS memory_levels (
233
- event_id VARCHAR PRIMARY KEY,
234
- level VARCHAR NOT NULL DEFAULT 'L0',
235
- promoted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
236
- )
237
- `);
238
- await dbRun(this.db, `
239
- CREATE TABLE IF NOT EXISTS entries (
240
- entry_id VARCHAR PRIMARY KEY,
241
- created_ts TIMESTAMP NOT NULL,
242
- entry_type VARCHAR NOT NULL,
243
- title VARCHAR NOT NULL,
244
- content_json JSON NOT NULL,
245
- stage VARCHAR NOT NULL DEFAULT 'raw',
246
- status VARCHAR DEFAULT 'active',
247
- superseded_by VARCHAR,
248
- build_id VARCHAR,
249
- evidence_json JSON,
250
- canonical_key VARCHAR,
251
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
252
- )
253
- `);
254
- await dbRun(this.db, `
255
- CREATE TABLE IF NOT EXISTS entities (
256
- entity_id VARCHAR PRIMARY KEY,
257
- entity_type VARCHAR NOT NULL,
258
- canonical_key VARCHAR NOT NULL,
259
- title VARCHAR NOT NULL,
260
- stage VARCHAR NOT NULL DEFAULT 'raw',
261
- status VARCHAR NOT NULL DEFAULT 'active',
262
- current_json JSON NOT NULL,
263
- title_norm VARCHAR,
264
- search_text VARCHAR,
265
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
266
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
267
- )
268
- `);
269
- await dbRun(this.db, `
270
- CREATE TABLE IF NOT EXISTS entity_aliases (
271
- entity_type VARCHAR NOT NULL,
272
- canonical_key VARCHAR NOT NULL,
273
- entity_id VARCHAR NOT NULL,
274
- is_primary BOOLEAN DEFAULT FALSE,
275
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
276
- PRIMARY KEY(entity_type, canonical_key)
277
- )
278
- `);
279
- await dbRun(this.db, `
280
- CREATE TABLE IF NOT EXISTS edges (
281
- edge_id VARCHAR PRIMARY KEY,
282
- src_type VARCHAR NOT NULL,
283
- src_id VARCHAR NOT NULL,
284
- rel_type VARCHAR NOT NULL,
285
- dst_type VARCHAR NOT NULL,
286
- dst_id VARCHAR NOT NULL,
287
- meta_json JSON,
288
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
289
- )
290
- `);
291
- await dbRun(this.db, `
292
- CREATE TABLE IF NOT EXISTS vector_outbox (
293
- job_id VARCHAR PRIMARY KEY,
294
- item_kind VARCHAR NOT NULL,
295
- item_id VARCHAR NOT NULL,
296
- embedding_version VARCHAR NOT NULL,
297
- status VARCHAR NOT NULL DEFAULT 'pending',
298
- retry_count INT DEFAULT 0,
299
- error VARCHAR,
300
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
301
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
302
- UNIQUE(item_kind, item_id, embedding_version)
303
- )
304
- `);
305
- await dbRun(this.db, `
306
- CREATE TABLE IF NOT EXISTS build_runs (
307
- build_id VARCHAR PRIMARY KEY,
308
- started_at TIMESTAMP NOT NULL,
309
- finished_at TIMESTAMP,
310
- extractor_model VARCHAR NOT NULL,
311
- extractor_prompt_hash VARCHAR NOT NULL,
312
- embedder_model VARCHAR NOT NULL,
313
- embedding_version VARCHAR NOT NULL,
314
- idris_version VARCHAR NOT NULL,
315
- schema_version VARCHAR NOT NULL,
316
- status VARCHAR NOT NULL DEFAULT 'running',
317
- error VARCHAR
318
- )
319
- `);
320
- await dbRun(this.db, `
321
- CREATE TABLE IF NOT EXISTS pipeline_metrics (
322
- id VARCHAR PRIMARY KEY,
323
- ts TIMESTAMP NOT NULL,
324
- stage VARCHAR NOT NULL,
325
- latency_ms DOUBLE NOT NULL,
326
- success BOOLEAN NOT NULL,
327
- error VARCHAR,
328
- session_id VARCHAR
329
- )
330
- `);
331
- await dbRun(this.db, `
332
- CREATE TABLE IF NOT EXISTS working_set (
333
- id VARCHAR PRIMARY KEY,
334
- event_id VARCHAR NOT NULL,
335
- added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
336
- relevance_score FLOAT DEFAULT 1.0,
337
- topics JSON,
338
- expires_at TIMESTAMP
339
- )
340
- `);
341
- await dbRun(this.db, `
342
- CREATE TABLE IF NOT EXISTS consolidated_memories (
343
- memory_id VARCHAR PRIMARY KEY,
344
- summary TEXT NOT NULL,
345
- topics JSON,
346
- source_events JSON,
347
- confidence FLOAT DEFAULT 0.5,
348
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
349
- accessed_at TIMESTAMP,
350
- access_count INTEGER DEFAULT 0
351
- )
352
- `);
353
- await dbRun(this.db, `
354
- CREATE TABLE IF NOT EXISTS continuity_log (
355
- log_id VARCHAR PRIMARY KEY,
356
- from_context_id VARCHAR,
357
- to_context_id VARCHAR,
358
- continuity_score FLOAT,
359
- transition_type VARCHAR,
360
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
361
- )
362
- `);
363
- await dbRun(this.db, `
364
- CREATE TABLE IF NOT EXISTS consolidated_rules (
365
- rule_id VARCHAR PRIMARY KEY,
366
- rule TEXT NOT NULL,
367
- topics JSON,
368
- source_memory_ids JSON,
369
- source_events JSON,
370
- confidence FLOAT DEFAULT 0.5,
371
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
372
- )
373
- `);
374
- await dbRun(this.db, `
375
- CREATE TABLE IF NOT EXISTS endless_config (
376
- key VARCHAR PRIMARY KEY,
377
- value JSON,
378
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
379
- )
380
- `);
381
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type)`);
382
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_stage ON entries(stage)`);
383
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_canonical ON entries(canonical_key)`);
384
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_type_key ON entities(entity_type, canonical_key)`);
385
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_status ON entities(status)`);
386
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_src ON edges(src_id, rel_type)`);
387
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_dst ON edges(dst_id, rel_type)`);
388
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_rel ON edges(rel_type)`);
389
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_outbox_status ON vector_outbox(status)`);
390
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
391
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
392
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
393
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
394
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
395
- this.initialized = true;
396
- }
397
- /**
398
- * Append event to store (AXIOMMIND Principle 2: Append-only)
399
- * Returns existing event ID if duplicate (Principle 3: Idempotency)
400
- */
401
- async append(input) {
402
- await this.initialize();
403
- const canonicalKey = makeCanonicalKey(input.content);
404
- const dedupeKey = makeDedupeKey(input.content, input.sessionId);
405
- const existing = await dbAll(
406
- this.db,
407
- `SELECT event_id FROM event_dedup WHERE dedupe_key = ?`,
408
- [dedupeKey]
409
- );
410
- if (existing.length > 0) {
411
- return {
412
- success: true,
413
- eventId: existing[0].event_id,
414
- isDuplicate: true
415
- };
416
- }
417
- const id = randomUUID();
418
- const timestamp = input.timestamp.toISOString();
419
- try {
420
- await dbRun(
421
- this.db,
422
- `INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
423
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
424
- [
425
- id,
426
- input.eventType,
427
- input.sessionId,
428
- timestamp,
429
- input.content,
430
- canonicalKey,
431
- dedupeKey,
432
- JSON.stringify(input.metadata || {})
433
- ]
434
- );
435
- await dbRun(
436
- this.db,
437
- `INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)`,
438
- [dedupeKey, id]
439
- );
440
- await dbRun(
441
- this.db,
442
- `INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')`,
443
- [id]
444
- );
445
- return { success: true, eventId: id, isDuplicate: false };
446
- } catch (error) {
447
- return {
448
- success: false,
449
- error: error instanceof Error ? error.message : String(error)
450
- };
451
- }
452
- }
453
- /**
454
- * Get events by session ID
455
- */
456
- async getSessionEvents(sessionId) {
457
- await this.initialize();
458
- const rows = await dbAll(
459
- this.db,
460
- `SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
461
- [sessionId]
462
- );
463
- return rows.map(this.rowToEvent);
464
- }
465
- /**
466
- * Get recent events
467
- */
468
- async getRecentEvents(limit = 100) {
469
- await this.initialize();
470
- const rows = await dbAll(
471
- this.db,
472
- `SELECT * FROM events ORDER BY timestamp DESC LIMIT ?`,
473
- [limit]
474
- );
475
- return rows.map(this.rowToEvent);
476
- }
477
- /**
478
- * Get event by ID
479
- */
480
- async getEvent(id) {
481
- await this.initialize();
482
- const rows = await dbAll(
483
- this.db,
484
- `SELECT * FROM events WHERE id = ?`,
485
- [id]
486
- );
487
- if (rows.length === 0)
488
- return null;
489
- return this.rowToEvent(rows[0]);
490
- }
491
- /**
492
- * Create or update session
493
- */
494
- async upsertSession(session) {
495
- await this.initialize();
496
- const existing = await dbAll(
497
- this.db,
498
- `SELECT id FROM sessions WHERE id = ?`,
499
- [session.id]
500
- );
501
- if (existing.length === 0) {
502
- await dbRun(
503
- this.db,
504
- `INSERT INTO sessions (id, started_at, project_path, tags)
505
- VALUES (?, ?, ?, ?)`,
506
- [
507
- session.id,
508
- (session.startedAt || /* @__PURE__ */ new Date()).toISOString(),
509
- session.projectPath || null,
510
- JSON.stringify(session.tags || [])
511
- ]
512
- );
513
- } else {
514
- const updates = [];
515
- const values = [];
516
- if (session.endedAt) {
517
- updates.push("ended_at = ?");
518
- values.push(session.endedAt.toISOString());
519
- }
520
- if (session.summary) {
521
- updates.push("summary = ?");
522
- values.push(session.summary);
523
- }
524
- if (session.tags) {
525
- updates.push("tags = ?");
526
- values.push(JSON.stringify(session.tags));
527
- }
528
- if (updates.length > 0) {
529
- values.push(session.id);
530
- await dbRun(
531
- this.db,
532
- `UPDATE sessions SET ${updates.join(", ")} WHERE id = ?`,
533
- values
534
- );
535
- }
536
- }
537
- }
538
- /**
539
- * Get session by ID
540
- */
541
- async getSession(id) {
542
- await this.initialize();
543
- const rows = await dbAll(
544
- this.db,
545
- `SELECT * FROM sessions WHERE id = ?`,
546
- [id]
547
- );
548
- if (rows.length === 0)
549
- return null;
550
- const row = rows[0];
551
- return {
552
- id: row.id,
553
- startedAt: toDate(row.started_at),
554
- endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
555
- projectPath: row.project_path,
556
- summary: row.summary,
557
- tags: row.tags ? JSON.parse(row.tags) : void 0
558
- };
559
- }
560
- /**
561
- * Add to embedding outbox (Single-Writer Pattern)
562
- */
563
- async enqueueForEmbedding(eventId, content) {
564
- await this.initialize();
565
- const id = randomUUID();
566
- await dbRun(
567
- this.db,
568
- `INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
569
- VALUES (?, ?, ?, 'pending', 0)`,
570
- [id, eventId, content]
571
- );
572
- return id;
573
- }
574
- /**
575
- * Get pending outbox items
576
- */
577
- async getPendingOutboxItems(limit = 32) {
578
- await this.initialize();
579
- const pending = await dbAll(
580
- this.db,
581
- `SELECT * FROM embedding_outbox
582
- WHERE status = 'pending'
583
- ORDER BY created_at
584
- LIMIT ?`,
585
- [limit]
586
- );
587
- if (pending.length === 0)
588
- return [];
589
- const ids = pending.map((r) => r.id);
590
- const placeholders = ids.map(() => "?").join(",");
591
- await dbRun(
592
- this.db,
593
- `UPDATE embedding_outbox SET status = 'processing' WHERE id IN (${placeholders})`,
594
- ids
595
- );
596
- return pending.map((row) => ({
597
- id: row.id,
598
- eventId: row.event_id,
599
- content: row.content,
600
- status: "processing",
601
- retryCount: row.retry_count,
602
- createdAt: toDate(row.created_at),
603
- errorMessage: row.error_message
604
- }));
605
- }
606
- /**
607
- * Mark outbox items as done
608
- */
609
- async completeOutboxItems(ids) {
610
- if (ids.length === 0)
611
- return;
612
- const placeholders = ids.map(() => "?").join(",");
613
- await dbRun(
614
- this.db,
615
- `DELETE FROM embedding_outbox WHERE id IN (${placeholders})`,
616
- ids
617
- );
618
- }
619
- /**
620
- * Mark outbox items as failed
621
- */
622
- async failOutboxItems(ids, error) {
623
- if (ids.length === 0)
624
- return;
625
- const placeholders = ids.map(() => "?").join(",");
626
- await dbRun(
627
- this.db,
628
- `UPDATE embedding_outbox
629
- SET status = CASE WHEN retry_count >= 3 THEN 'failed' ELSE 'pending' END,
630
- retry_count = retry_count + 1,
631
- error_message = ?
632
- WHERE id IN (${placeholders})`,
633
- [error, ...ids]
634
- );
635
- }
636
- /**
637
- * Update memory level
638
- */
639
- async updateMemoryLevel(eventId, level) {
640
- await this.initialize();
641
- await dbRun(
642
- this.db,
643
- `UPDATE memory_levels SET level = ?, promoted_at = CURRENT_TIMESTAMP WHERE event_id = ?`,
644
- [level, eventId]
645
- );
646
- }
647
- /**
648
- * Get memory level statistics
649
- */
650
- async getLevelStats() {
651
- await this.initialize();
652
- const rows = await dbAll(
653
- this.db,
654
- `SELECT level, COUNT(*) as count FROM memory_levels GROUP BY level`
655
- );
656
- return rows;
657
- }
658
- /**
659
- * Get events by memory level
660
- */
661
- async getEventsByLevel(level, options) {
662
- await this.initialize();
663
- const limit = options?.limit || 50;
664
- const offset = options?.offset || 0;
665
- const rows = await dbAll(
666
- this.db,
667
- `SELECT e.* FROM events e
668
- INNER JOIN memory_levels ml ON e.id = ml.event_id
669
- WHERE ml.level = ?
670
- ORDER BY e.timestamp DESC
671
- LIMIT ? OFFSET ?`,
672
- [level, limit, offset]
673
- );
674
- return rows.map((row) => this.rowToEvent(row));
675
- }
676
- /**
677
- * Get memory level for a specific event
678
- */
679
- async getEventLevel(eventId) {
680
- await this.initialize();
681
- const rows = await dbAll(
682
- this.db,
683
- `SELECT level FROM memory_levels WHERE event_id = ?`,
684
- [eventId]
685
- );
686
- return rows.length > 0 ? rows[0].level : null;
687
- }
688
- // ============================================================
689
- // Endless Mode Helper Methods
690
- // ============================================================
691
- /**
692
- * Get database instance for Endless Mode stores
693
- */
694
- getDatabase() {
695
- return this.db;
696
- }
697
- /**
698
- * Get config value for endless mode
699
- */
700
- async getEndlessConfig(key) {
701
- await this.initialize();
702
- const rows = await dbAll(
703
- this.db,
704
- `SELECT value FROM endless_config WHERE key = ?`,
705
- [key]
706
- );
707
- if (rows.length === 0)
708
- return null;
709
- return JSON.parse(rows[0].value);
710
- }
711
- /**
712
- * Set config value for endless mode
713
- */
714
- async setEndlessConfig(key, value) {
715
- await this.initialize();
716
- await dbRun(
717
- this.db,
718
- `INSERT OR REPLACE INTO endless_config (key, value, updated_at)
719
- VALUES (?, ?, CURRENT_TIMESTAMP)`,
720
- [key, JSON.stringify(value)]
721
- );
722
- }
723
- /**
724
- * Get all sessions
725
- */
726
- async getAllSessions() {
727
- await this.initialize();
728
- const rows = await dbAll(
729
- this.db,
730
- `SELECT * FROM sessions ORDER BY started_at DESC`
731
- );
732
- return rows.map((row) => ({
733
- id: row.id,
734
- startedAt: toDate(row.started_at),
735
- endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
736
- projectPath: row.project_path,
737
- summary: row.summary,
738
- tags: row.tags ? JSON.parse(row.tags) : void 0
739
- }));
740
- }
741
- /**
742
- * Increment access count for events (stub for compatibility)
743
- */
744
- async incrementAccessCount(eventIds) {
745
- return Promise.resolve();
746
- }
747
- /**
748
- * Get most accessed memories (stub for compatibility)
749
- */
750
- async getMostAccessed(limit = 10) {
751
- return [];
752
- }
753
- /**
754
- * Close database connection
755
- */
756
- async close() {
757
- await dbClose(this.db);
40
+ // src/core/sqlite-event-store.ts
41
+ import { randomUUID } from "crypto";
42
+
43
+ // src/core/canonical-key.ts
44
+ import { createHash } from "crypto";
45
+ var MAX_KEY_LENGTH = 200;
46
+ function makeCanonicalKey(title, context) {
47
+ let normalized = title.normalize("NFKC");
48
+ normalized = normalized.toLowerCase();
49
+ normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
50
+ normalized = normalized.replace(/\s+/g, " ").trim();
51
+ let key = normalized;
52
+ if (context?.project) {
53
+ key = `${context.project}::${key}`;
758
54
  }
759
- /**
760
- * Convert database row to MemoryEvent
761
- */
762
- rowToEvent(row) {
763
- return {
764
- id: row.id,
765
- eventType: row.event_type,
766
- sessionId: row.session_id,
767
- timestamp: toDate(row.timestamp),
768
- content: row.content,
769
- canonicalKey: row.canonical_key,
770
- dedupeKey: row.dedupe_key,
771
- metadata: row.metadata ? JSON.parse(row.metadata) : void 0
772
- };
55
+ if (key.length > MAX_KEY_LENGTH) {
56
+ const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
57
+ key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
773
58
  }
774
- };
775
-
776
- // src/core/sqlite-event-store.ts
777
- import { randomUUID as randomUUID2 } from "crypto";
59
+ return key;
60
+ }
61
+ function makeDedupeKey(content, sessionId) {
62
+ const contentHash = createHash("sha256").update(content).digest("hex");
63
+ return `${sessionId}:${contentHash}`;
64
+ }
778
65
 
779
66
  // src/core/sqlite-wrapper.ts
780
67
  import Database from "better-sqlite3";
@@ -1289,7 +576,7 @@ var SQLiteEventStore = class {
1289
576
  isDuplicate: true
1290
577
  };
1291
578
  }
1292
- const id = randomUUID2();
579
+ const id = randomUUID();
1293
580
  const timestamp = toSQLiteTimestamp(input.timestamp);
1294
581
  try {
1295
582
  const metadata = input.metadata || {};
@@ -1343,6 +630,29 @@ var SQLiteEventStore = class {
1343
630
  };
1344
631
  }
1345
632
  }
633
+ /**
634
+ * Get session IDs that have events but no session_summary event.
635
+ * Used to backfill summaries for sessions that ended without Stop hook.
636
+ */
637
+ async getSessionsWithoutSummary(currentSessionId, limit = 5) {
638
+ await this.initialize();
639
+ const rows = sqliteAll(
640
+ this.db,
641
+ `SELECT DISTINCT e.session_id
642
+ FROM events e
643
+ WHERE e.session_id != ?
644
+ AND e.event_type != 'session_summary'
645
+ AND e.session_id NOT IN (
646
+ SELECT DISTINCT session_id FROM events WHERE event_type = 'session_summary'
647
+ )
648
+ GROUP BY e.session_id
649
+ HAVING COUNT(*) >= 3
650
+ ORDER BY MAX(e.timestamp) DESC
651
+ LIMIT ?`,
652
+ [currentSessionId, limit]
653
+ );
654
+ return rows.map((r) => r.session_id);
655
+ }
1346
656
  /**
1347
657
  * Get events by session ID
1348
658
  */
@@ -1570,7 +880,7 @@ var SQLiteEventStore = class {
1570
880
  */
1571
881
  async enqueueForEmbedding(eventId, content) {
1572
882
  await this.initialize();
1573
- const id = randomUUID2();
883
+ const id = randomUUID();
1574
884
  sqliteRun(
1575
885
  this.db,
1576
886
  `INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
@@ -1851,7 +1161,7 @@ var SQLiteEventStore = class {
1851
1161
  if (this.readOnly)
1852
1162
  return;
1853
1163
  await this.initialize();
1854
- const id = randomUUID2();
1164
+ const id = randomUUID();
1855
1165
  sqliteRun(
1856
1166
  this.db,
1857
1167
  `INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
@@ -1859,6 +1169,21 @@ var SQLiteEventStore = class {
1859
1169
  [id, eventId, sessionId, score, query.slice(0, 100)]
1860
1170
  );
1861
1171
  }
1172
+ /**
1173
+ * Get session IDs that have unevaluated retrievals (measured_at IS NULL).
1174
+ * Excludes the current session. Used to backfill sessions that ended without Stop hook.
1175
+ */
1176
+ async getUnevaluatedSessions(currentSessionId, limit = 5) {
1177
+ await this.initialize();
1178
+ const rows = sqliteAll(
1179
+ this.db,
1180
+ `SELECT DISTINCT session_id FROM memory_helpfulness
1181
+ WHERE measured_at IS NULL AND session_id != ?
1182
+ ORDER BY created_at DESC LIMIT ?`,
1183
+ [currentSessionId, limit]
1184
+ );
1185
+ return rows.map((r) => r.session_id);
1186
+ }
1862
1187
  /**
1863
1188
  * Evaluate helpfulness for all retrievals in a session
1864
1189
  * Called at session end - uses behavioral signals to compute score
@@ -2057,7 +1382,7 @@ var SQLiteEventStore = class {
2057
1382
  }
2058
1383
  async recordRetrievalTrace(input) {
2059
1384
  await this.initialize();
2060
- const traceId = randomUUID2();
1385
+ const traceId = randomUUID();
2061
1386
  sqliteRun(
2062
1387
  this.db,
2063
1388
  `INSERT INTO retrieval_traces (
@@ -2311,169 +1636,6 @@ var SQLiteEventStore = class {
2311
1636
  }
2312
1637
  };
2313
1638
 
2314
- // src/core/sync-worker.ts
2315
- var DEFAULT_CONFIG = {
2316
- intervalMs: 3e4,
2317
- batchSize: 500,
2318
- maxRetries: 3,
2319
- retryDelayMs: 5e3
2320
- };
2321
- var SyncWorker = class {
2322
- constructor(sqliteStore, duckdbStore, config) {
2323
- this.sqliteStore = sqliteStore;
2324
- this.duckdbStore = duckdbStore;
2325
- this.config = { ...DEFAULT_CONFIG, ...config };
2326
- }
2327
- config;
2328
- intervalHandle = null;
2329
- running = false;
2330
- stats = {
2331
- lastSyncAt: null,
2332
- eventsSynced: 0,
2333
- sessionsSynced: 0,
2334
- errors: 0,
2335
- status: "idle"
2336
- };
2337
- /**
2338
- * Start the sync worker
2339
- */
2340
- start() {
2341
- if (this.running)
2342
- return;
2343
- this.running = true;
2344
- this.stats.status = "idle";
2345
- this.syncNow().catch((err) => {
2346
- console.error("[SyncWorker] Initial sync failed:", err);
2347
- });
2348
- this.intervalHandle = setInterval(() => {
2349
- this.syncNow().catch((err) => {
2350
- console.error("[SyncWorker] Periodic sync failed:", err);
2351
- });
2352
- }, this.config.intervalMs);
2353
- }
2354
- /**
2355
- * Stop the sync worker
2356
- */
2357
- stop() {
2358
- this.running = false;
2359
- this.stats.status = "stopped";
2360
- if (this.intervalHandle) {
2361
- clearInterval(this.intervalHandle);
2362
- this.intervalHandle = null;
2363
- }
2364
- }
2365
- /**
2366
- * Trigger immediate sync
2367
- */
2368
- async syncNow() {
2369
- if (this.stats.status === "syncing") {
2370
- return;
2371
- }
2372
- this.stats.status = "syncing";
2373
- try {
2374
- await this.syncEvents();
2375
- await this.syncSessions();
2376
- this.stats.lastSyncAt = /* @__PURE__ */ new Date();
2377
- this.stats.status = "idle";
2378
- } catch (error) {
2379
- this.stats.errors++;
2380
- this.stats.status = "error";
2381
- throw error;
2382
- }
2383
- }
2384
- /**
2385
- * Sync events from SQLite to DuckDB
2386
- */
2387
- async syncEvents() {
2388
- const targetName = "duckdb_analytics";
2389
- const position = await this.sqliteStore.getSyncPosition(targetName);
2390
- const lastTimestamp = position.lastTimestamp || "1970-01-01T00:00:00.000Z";
2391
- let hasMore = true;
2392
- let totalSynced = 0;
2393
- while (hasMore) {
2394
- const events = await this.sqliteStore.getEventsSince(lastTimestamp, this.config.batchSize);
2395
- if (events.length === 0) {
2396
- hasMore = false;
2397
- break;
2398
- }
2399
- await this.retryWithBackoff(async () => {
2400
- for (const event of events) {
2401
- await this.insertEventToDuckDB(event);
2402
- }
2403
- });
2404
- totalSynced += events.length;
2405
- const lastEvent = events[events.length - 1];
2406
- await this.sqliteStore.updateSyncPosition(
2407
- targetName,
2408
- lastEvent.id,
2409
- lastEvent.timestamp.toISOString()
2410
- );
2411
- hasMore = events.length === this.config.batchSize;
2412
- }
2413
- this.stats.eventsSynced += totalSynced;
2414
- }
2415
- /**
2416
- * Sync sessions from SQLite to DuckDB
2417
- */
2418
- async syncSessions() {
2419
- const sessions = await this.sqliteStore.getAllSessions();
2420
- for (const session of sessions) {
2421
- await this.retryWithBackoff(async () => {
2422
- await this.duckdbStore.upsertSession(session);
2423
- });
2424
- }
2425
- this.stats.sessionsSynced = sessions.length;
2426
- }
2427
- /**
2428
- * Insert a single event into DuckDB
2429
- */
2430
- async insertEventToDuckDB(event) {
2431
- await this.duckdbStore.append({
2432
- eventType: event.eventType,
2433
- sessionId: event.sessionId,
2434
- timestamp: event.timestamp,
2435
- content: event.content,
2436
- metadata: event.metadata
2437
- });
2438
- }
2439
- /**
2440
- * Retry operation with exponential backoff
2441
- */
2442
- async retryWithBackoff(fn) {
2443
- let lastError = null;
2444
- for (let attempt = 0; attempt < this.config.maxRetries; attempt++) {
2445
- try {
2446
- return await fn();
2447
- } catch (error) {
2448
- lastError = error instanceof Error ? error : new Error(String(error));
2449
- if (attempt < this.config.maxRetries - 1) {
2450
- const delay = this.config.retryDelayMs * Math.pow(2, attempt);
2451
- await this.sleep(delay);
2452
- }
2453
- }
2454
- }
2455
- throw lastError;
2456
- }
2457
- /**
2458
- * Sleep utility
2459
- */
2460
- sleep(ms) {
2461
- return new Promise((resolve3) => setTimeout(resolve3, ms));
2462
- }
2463
- /**
2464
- * Get sync statistics
2465
- */
2466
- getStats() {
2467
- return { ...this.stats };
2468
- }
2469
- /**
2470
- * Check if worker is running
2471
- */
2472
- isRunning() {
2473
- return this.running;
2474
- }
2475
- };
2476
-
2477
1639
  // src/core/vector-store.ts
2478
1640
  import * as lancedb from "@lancedb/lancedb";
2479
1641
  var VectorStore = class {
@@ -2646,7 +1808,7 @@ var VectorStore = class {
2646
1808
 
2647
1809
  // src/core/embedder.ts
2648
1810
  import { pipeline } from "@huggingface/transformers";
2649
- var Embedder = class {
1811
+ var Embedder = class _Embedder {
2650
1812
  pipeline = null;
2651
1813
  modelName;
2652
1814
  activeModelName;
@@ -2677,6 +1839,11 @@ var Embedder = class {
2677
1839
  this.initialized = true;
2678
1840
  }
2679
1841
  }
1842
+ // ~4 chars per token; 512 tokens * 4 = 2048, use 2000 to be safe
1843
+ static MAX_CHARS = 2e3;
1844
+ truncate(text) {
1845
+ return text.length > _Embedder.MAX_CHARS ? text.slice(0, _Embedder.MAX_CHARS) : text;
1846
+ }
2680
1847
  /**
2681
1848
  * Generate embedding for a single text
2682
1849
  */
@@ -2685,10 +1852,11 @@ var Embedder = class {
2685
1852
  if (!this.pipeline) {
2686
1853
  throw new Error("Embedding pipeline not initialized");
2687
1854
  }
2688
- const output = await this.pipeline(text, {
1855
+ const output = await this.pipeline(this.truncate(text), {
2689
1856
  pooling: "mean",
2690
1857
  normalize: true,
2691
- truncation: true
1858
+ truncation: true,
1859
+ max_length: 512
2692
1860
  });
2693
1861
  const vector = Array.from(output.data);
2694
1862
  return {
@@ -2710,10 +1878,11 @@ var Embedder = class {
2710
1878
  for (let i = 0; i < texts.length; i += batchSize) {
2711
1879
  const batch = texts.slice(i, i + batchSize);
2712
1880
  for (const text of batch) {
2713
- const output = await this.pipeline(text, {
1881
+ const output = await this.pipeline(this.truncate(text), {
2714
1882
  pooling: "mean",
2715
1883
  normalize: true,
2716
- truncation: true
1884
+ truncation: true,
1885
+ max_length: 512
2717
1886
  });
2718
1887
  const vector = Array.from(output.data);
2719
1888
  results.push({
@@ -2754,8 +1923,34 @@ function getDefaultEmbedder() {
2754
1923
  return defaultEmbedder;
2755
1924
  }
2756
1925
 
1926
+ // src/core/db-wrapper.ts
1927
+ import BetterSqlite3 from "better-sqlite3";
1928
+ function toDate(value) {
1929
+ if (value instanceof Date)
1930
+ return value;
1931
+ if (typeof value === "string")
1932
+ return new Date(value);
1933
+ if (typeof value === "number")
1934
+ return new Date(value);
1935
+ return new Date(String(value));
1936
+ }
1937
+ function createDatabase(dbPath, options) {
1938
+ return new BetterSqlite3(dbPath, { readonly: options?.readOnly });
1939
+ }
1940
+ function dbRun(db, sql, params = []) {
1941
+ db.prepare(sql).run(...params);
1942
+ return Promise.resolve();
1943
+ }
1944
+ function dbAll(db, sql, params = []) {
1945
+ return Promise.resolve(db.prepare(sql).all(...params));
1946
+ }
1947
+ function dbClose(db) {
1948
+ db.close();
1949
+ return Promise.resolve();
1950
+ }
1951
+
2757
1952
  // src/core/vector-outbox.ts
2758
- var DEFAULT_CONFIG2 = {
1953
+ var DEFAULT_CONFIG = {
2759
1954
  embeddingVersion: "v1",
2760
1955
  maxRetries: 3,
2761
1956
  stuckThresholdMs: 5 * 60 * 1e3,
@@ -2764,7 +1959,7 @@ var DEFAULT_CONFIG2 = {
2764
1959
  };
2765
1960
 
2766
1961
  // src/core/vector-worker.ts
2767
- var DEFAULT_CONFIG3 = {
1962
+ var DEFAULT_CONFIG2 = {
2768
1963
  batchSize: 32,
2769
1964
  pollIntervalMs: 1e3,
2770
1965
  maxRetries: 3
@@ -2781,7 +1976,7 @@ var VectorWorker = class {
2781
1976
  this.eventStore = eventStore;
2782
1977
  this.vectorStore = vectorStore;
2783
1978
  this.embedder = embedder;
2784
- this.config = { ...DEFAULT_CONFIG3, ...config };
1979
+ this.config = { ...DEFAULT_CONFIG2, ...config };
2785
1980
  }
2786
1981
  /**
2787
1982
  * Start the worker polling loop
@@ -2902,7 +2097,7 @@ function createVectorWorker(eventStore, vectorStore, embedder, config) {
2902
2097
  }
2903
2098
 
2904
2099
  // src/core/matcher.ts
2905
- var DEFAULT_CONFIG4 = {
2100
+ var DEFAULT_CONFIG3 = {
2906
2101
  weights: {
2907
2102
  semanticSimilarity: 0.4,
2908
2103
  ftsScore: 0.25,
@@ -2917,9 +2112,9 @@ var Matcher = class {
2917
2112
  config;
2918
2113
  constructor(config = {}) {
2919
2114
  this.config = {
2920
- ...DEFAULT_CONFIG4,
2115
+ ...DEFAULT_CONFIG3,
2921
2116
  ...config,
2922
- weights: { ...DEFAULT_CONFIG4.weights, ...config.weights }
2117
+ weights: { ...DEFAULT_CONFIG3.weights, ...config.weights }
2923
2118
  };
2924
2119
  }
2925
2120
  /**
@@ -3909,7 +3104,7 @@ function createSharedEventStore(dbPath) {
3909
3104
  }
3910
3105
 
3911
3106
  // src/core/shared-store.ts
3912
- import { randomUUID as randomUUID3 } from "crypto";
3107
+ import { randomUUID as randomUUID2 } from "crypto";
3913
3108
  var SharedStore = class {
3914
3109
  constructor(sharedEventStore) {
3915
3110
  this.sharedEventStore = sharedEventStore;
@@ -3921,7 +3116,7 @@ var SharedStore = class {
3921
3116
  * Promote a verified troubleshooting entry to shared storage
3922
3117
  */
3923
3118
  async promoteEntry(input) {
3924
- const entryId = randomUUID3();
3119
+ const entryId = randomUUID2();
3925
3120
  await dbRun(
3926
3121
  this.db,
3927
3122
  `INSERT INTO shared_troubleshooting (
@@ -4286,7 +3481,7 @@ function createSharedVectorStore(dbPath) {
4286
3481
  }
4287
3482
 
4288
3483
  // src/core/shared-promoter.ts
4289
- import { randomUUID as randomUUID4 } from "crypto";
3484
+ import { randomUUID as randomUUID3 } from "crypto";
4290
3485
  var SharedPromoter = class {
4291
3486
  constructor(sharedStore, sharedVectorStore, embedder, config) {
4292
3487
  this.sharedStore = sharedStore;
@@ -4354,7 +3549,7 @@ var SharedPromoter = class {
4354
3549
  const embeddingContent = this.createEmbeddingContent(input);
4355
3550
  const embedding = await this.embedder.embed(embeddingContent);
4356
3551
  await this.sharedVectorStore.upsert({
4357
- id: randomUUID4(),
3552
+ id: randomUUID3(),
4358
3553
  entryId,
4359
3554
  entryType: "troubleshooting",
4360
3555
  content: embeddingContent,
@@ -4494,7 +3689,7 @@ function createToolObservationEmbedding(toolName, metadata, success) {
4494
3689
  }
4495
3690
 
4496
3691
  // src/core/working-set-store.ts
4497
- import { randomUUID as randomUUID5 } from "crypto";
3692
+ import { randomUUID as randomUUID4 } from "crypto";
4498
3693
  var WorkingSetStore = class {
4499
3694
  constructor(eventStore, config) {
4500
3695
  this.eventStore = eventStore;
@@ -4515,7 +3710,7 @@ var WorkingSetStore = class {
4515
3710
  `INSERT OR REPLACE INTO working_set (id, event_id, added_at, relevance_score, topics, expires_at)
4516
3711
  VALUES (?, ?, CURRENT_TIMESTAMP, ?, ?, ?)`,
4517
3712
  [
4518
- randomUUID5(),
3713
+ randomUUID4(),
4519
3714
  eventId,
4520
3715
  relevanceScore,
4521
3716
  JSON.stringify(topics || []),
@@ -4699,7 +3894,7 @@ function createWorkingSetStore(eventStore, config) {
4699
3894
  }
4700
3895
 
4701
3896
  // src/core/consolidated-store.ts
4702
- import { randomUUID as randomUUID6 } from "crypto";
3897
+ import { randomUUID as randomUUID5 } from "crypto";
4703
3898
  var ConsolidatedStore = class {
4704
3899
  constructor(eventStore) {
4705
3900
  this.eventStore = eventStore;
@@ -4711,7 +3906,7 @@ var ConsolidatedStore = class {
4711
3906
  * Create a new consolidated memory
4712
3907
  */
4713
3908
  async create(input) {
4714
- const memoryId = randomUUID6();
3909
+ const memoryId = randomUUID5();
4715
3910
  await dbRun(
4716
3911
  this.db,
4717
3912
  `INSERT INTO consolidated_memories
@@ -4841,7 +4036,7 @@ var ConsolidatedStore = class {
4841
4036
  * Create a long-term rule promoted from stable summaries
4842
4037
  */
4843
4038
  async createRule(input) {
4844
- const ruleId = randomUUID6();
4039
+ const ruleId = randomUUID5();
4845
4040
  await dbRun(
4846
4041
  this.db,
4847
4042
  `INSERT INTO consolidated_rules
@@ -5363,7 +4558,7 @@ function createConsolidationWorker(workingSetStore, consolidatedStore, config) {
5363
4558
  }
5364
4559
 
5365
4560
  // src/core/continuity-manager.ts
5366
- import { randomUUID as randomUUID7 } from "crypto";
4561
+ import { randomUUID as randomUUID6 } from "crypto";
5367
4562
  var ContinuityManager = class {
5368
4563
  constructor(eventStore, config) {
5369
4564
  this.eventStore = eventStore;
@@ -5517,7 +4712,7 @@ var ContinuityManager = class {
5517
4712
  `INSERT INTO continuity_log
5518
4713
  (log_id, from_context_id, to_context_id, continuity_score, transition_type, created_at)
5519
4714
  VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
5520
- [randomUUID7(), previous.id, current.id, score, type]
4715
+ [randomUUID6(), previous.id, current.id, score, type]
5521
4716
  );
5522
4717
  }
5523
4718
  /**
@@ -5626,7 +4821,7 @@ function createContinuityManager(eventStore, config) {
5626
4821
  }
5627
4822
 
5628
4823
  // src/core/graduation-worker.ts
5629
- var DEFAULT_CONFIG5 = {
4824
+ var DEFAULT_CONFIG4 = {
5630
4825
  evaluationIntervalMs: 3e5,
5631
4826
  // 5 minutes
5632
4827
  batchSize: 50,
@@ -5634,7 +4829,7 @@ var DEFAULT_CONFIG5 = {
5634
4829
  // 1 hour cooldown between evaluations
5635
4830
  };
5636
4831
  var GraduationWorker = class {
5637
- constructor(eventStore, graduation, config = DEFAULT_CONFIG5) {
4832
+ constructor(eventStore, graduation, config = DEFAULT_CONFIG4) {
5638
4833
  this.eventStore = eventStore;
5639
4834
  this.graduation = graduation;
5640
4835
  this.config = config;
@@ -5742,7 +4937,7 @@ function createGraduationWorker(eventStore, graduation, config) {
5742
4937
  return new GraduationWorker(
5743
4938
  eventStore,
5744
4939
  graduation,
5745
- { ...DEFAULT_CONFIG5, ...config }
4940
+ { ...DEFAULT_CONFIG4, ...config }
5746
4941
  );
5747
4942
  }
5748
4943
 
@@ -5951,9 +5146,6 @@ function loadSessionRegistry() {
5951
5146
  var MemoryService = class {
5952
5147
  // Primary store: SQLite (WAL mode) - for hooks, always available
5953
5148
  sqliteStore;
5954
- // Analytics store: DuckDB - for server reads (optional, synced from SQLite)
5955
- analyticsStore;
5956
- syncWorker = null;
5957
5149
  vectorStore;
5958
5150
  embedder;
5959
5151
  matcher;
@@ -5979,6 +5171,7 @@ var MemoryService = class {
5979
5171
  projectPath = null;
5980
5172
  readOnly;
5981
5173
  lightweightMode;
5174
+ embeddingOnly;
5982
5175
  mdMirror;
5983
5176
  storagePath;
5984
5177
  constructor(config) {
@@ -5986,6 +5179,7 @@ var MemoryService = class {
5986
5179
  this.storagePath = storagePath;
5987
5180
  this.readOnly = config.readOnly ?? false;
5988
5181
  this.lightweightMode = config.lightweightMode ?? false;
5182
+ this.embeddingOnly = config.embeddingOnly ?? false;
5989
5183
  this.mdMirror = new MarkdownMirror2(process.cwd());
5990
5184
  if (!this.readOnly && !fs4.existsSync(storagePath)) {
5991
5185
  fs4.mkdirSync(storagePath, { recursive: true });
@@ -6000,24 +5194,6 @@ var MemoryService = class {
6000
5194
  markdownMirrorRoot: storagePath
6001
5195
  }
6002
5196
  );
6003
- const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
6004
- if (!analyticsEnabled) {
6005
- this.analyticsStore = null;
6006
- } else if (this.readOnly) {
6007
- try {
6008
- this.analyticsStore = new EventStore(
6009
- path3.join(storagePath, "analytics.duckdb"),
6010
- { readOnly: true }
6011
- );
6012
- } catch {
6013
- this.analyticsStore = null;
6014
- }
6015
- } else {
6016
- this.analyticsStore = new EventStore(
6017
- path3.join(storagePath, "analytics.duckdb"),
6018
- { readOnly: false }
6019
- );
6020
- }
6021
5197
  this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
6022
5198
  const embeddingModel = config.embeddingModel || process.env.CLAUDE_MEMORY_EMBEDDING_MODEL;
6023
5199
  this.embedder = embeddingModel ? new Embedder(embeddingModel) : getDefaultEmbedder();
@@ -6043,13 +5219,6 @@ var MemoryService = class {
6043
5219
  this.initialized = true;
6044
5220
  return;
6045
5221
  }
6046
- if (this.analyticsStore) {
6047
- try {
6048
- await this.analyticsStore.initialize();
6049
- } catch (error) {
6050
- console.warn("[MemoryService] Analytics store (DuckDB) initialization failed, using SQLite for reads:", error);
6051
- }
6052
- }
6053
5222
  await this.vectorStore.initialize();
6054
5223
  await this.embedder.initialize();
6055
5224
  if (!this.readOnly) {
@@ -6059,19 +5228,13 @@ var MemoryService = class {
6059
5228
  this.embedder
6060
5229
  );
6061
5230
  this.vectorWorker.start();
6062
- this.retriever.setGraduationPipeline(this.graduation);
6063
- this.graduationWorker = createGraduationWorker(
6064
- this.sqliteStore,
6065
- this.graduation
6066
- );
6067
- this.graduationWorker.start();
6068
- if (this.analyticsStore) {
6069
- this.syncWorker = new SyncWorker(
5231
+ if (!this.embeddingOnly) {
5232
+ this.retriever.setGraduationPipeline(this.graduation);
5233
+ this.graduationWorker = createGraduationWorker(
6070
5234
  this.sqliteStore,
6071
- this.analyticsStore,
6072
- { intervalMs: 3e4, batchSize: 500 }
5235
+ this.graduation
6073
5236
  );
6074
- this.syncWorker.start();
5237
+ this.graduationWorker.start();
6075
5238
  }
6076
5239
  const savedMode = await this.sqliteStore.getEndlessConfig("mode");
6077
5240
  if (savedMode === "endless") {
@@ -6268,6 +5431,57 @@ var MemoryService = class {
6268
5431
  }
6269
5432
  );
6270
5433
  }
5434
+ /**
5435
+ * Backfill session summaries for recent sessions that are missing them.
5436
+ * Called from session-start hook to catch sessions that ended without Stop hook.
5437
+ */
5438
+ async backfillMissingSummaries(currentSessionId, limit = 5) {
5439
+ await this.initialize();
5440
+ const recentSessionIds = await this.sqliteStore.getSessionsWithoutSummary(currentSessionId, limit);
5441
+ for (const sid of recentSessionIds) {
5442
+ try {
5443
+ await this.generateSessionSummary(sid);
5444
+ } catch {
5445
+ }
5446
+ }
5447
+ }
5448
+ /**
5449
+ * Generate a rule-based session summary from stored events.
5450
+ * Called at session end (Stop hook) when no LLM-generated summary exists.
5451
+ * Skips if a summary already exists for this session.
5452
+ */
5453
+ async generateSessionSummary(sessionId) {
5454
+ await this.initialize();
5455
+ const events = await this.sqliteStore.getSessionEvents(sessionId);
5456
+ if (events.length < 3)
5457
+ return;
5458
+ const hasSummary = events.some((e) => e.eventType === "session_summary");
5459
+ if (hasSummary)
5460
+ return;
5461
+ const prompts = events.filter((e) => e.eventType === "user_prompt");
5462
+ const toolObs = events.filter((e) => e.eventType === "tool_observation");
5463
+ const toolNames = [...new Set(
5464
+ toolObs.map((e) => e.metadata?.toolName).filter(Boolean)
5465
+ )];
5466
+ const errorObs = toolObs.filter((e) => {
5467
+ const meta = e.metadata;
5468
+ return meta?.exitCode !== void 0 && meta.exitCode !== 0;
5469
+ });
5470
+ const datePart = events[0].timestamp.toISOString().split("T")[0];
5471
+ const parts = [`[${datePart}] ${prompts.length}\uD134 \uC138\uC158.`];
5472
+ if (prompts.length > 0) {
5473
+ const firstPrompt = prompts[0].content.slice(0, 120).replace(/\n/g, " ");
5474
+ parts.push(`\uC8FC\uC694 \uC791\uC5C5: ${firstPrompt}`);
5475
+ }
5476
+ if (toolNames.length > 0) {
5477
+ parts.push(`\uC0AC\uC6A9 \uD234: ${toolNames.slice(0, 6).join(", ")}`);
5478
+ }
5479
+ if (errorObs.length > 0) {
5480
+ parts.push(`\uC624\uB958 ${errorObs.length}\uAC74 \uBC1C\uC0DD`);
5481
+ }
5482
+ const summary = parts.join(". ");
5483
+ await this.storeSessionSummary(sessionId, summary, { generated: "rule-based", eventCount: events.length });
5484
+ }
6271
5485
  /**
6272
5486
  * Store a tool observation
6273
5487
  */
@@ -6795,6 +6009,20 @@ var MemoryService = class {
6795
6009
  await this.initialize();
6796
6010
  await this.sqliteStore.recordRetrieval(eventId, sessionId, score, query);
6797
6011
  }
6012
+ /**
6013
+ * Record a query-level retrieval trace (used by user-prompt-submit hook).
6014
+ * Feeds the retrieval_traces table that powers dashboard stats.
6015
+ */
6016
+ async recordQueryTrace(input) {
6017
+ await this.initialize();
6018
+ await this.sqliteStore.recordRetrievalTrace({
6019
+ ...input,
6020
+ projectHash: this.projectHash || void 0,
6021
+ candidateDetails: [],
6022
+ selectedDetails: [],
6023
+ fallbackTrace: []
6024
+ });
6025
+ }
6798
6026
  /**
6799
6027
  * Evaluate helpfulness of retrievals in a session (called at session end)
6800
6028
  */
@@ -6802,6 +6030,20 @@ var MemoryService = class {
6802
6030
  await this.initialize();
6803
6031
  await this.sqliteStore.evaluateSessionHelpfulness(sessionId);
6804
6032
  }
6033
+ /**
6034
+ * Backfill helpfulness evaluation for sessions that ended without Stop hook.
6035
+ * Call on first turn of a new session to catch missed evaluations.
6036
+ */
6037
+ async evaluatePendingSessions(currentSessionId) {
6038
+ await this.initialize();
6039
+ const sessions = await this.sqliteStore.getUnevaluatedSessions(currentSessionId, 5);
6040
+ for (const sid of sessions) {
6041
+ try {
6042
+ await this.sqliteStore.evaluateSessionHelpfulness(sid);
6043
+ } catch {
6044
+ }
6045
+ }
6046
+ }
6805
6047
  /**
6806
6048
  * Get most helpful memories ranked by helpfulness score
6807
6049
  */
@@ -7066,16 +6308,10 @@ var MemoryService = class {
7066
6308
  if (this.vectorWorker) {
7067
6309
  this.vectorWorker.stop();
7068
6310
  }
7069
- if (this.syncWorker) {
7070
- this.syncWorker.stop();
7071
- }
7072
6311
  if (this.sharedEventStore) {
7073
6312
  await this.sharedEventStore.close();
7074
6313
  }
7075
6314
  await this.sqliteStore.close();
7076
- if (this.analyticsStore) {
7077
- await this.analyticsStore.close();
7078
- }
7079
6315
  }
7080
6316
  /**
7081
6317
  * Expand ~ to home directory