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
@@ -16,744 +16,31 @@ import * as os from "os";
16
16
  import * as fs4 from "fs";
17
17
  import * as crypto2 from "crypto";
18
18
 
19
- // src/core/event-store.ts
20
- import { randomUUID } from "crypto";
21
-
22
- // src/core/canonical-key.ts
23
- import { createHash } from "crypto";
24
- var MAX_KEY_LENGTH = 200;
25
- function makeCanonicalKey(title, context) {
26
- let normalized = title.normalize("NFKC");
27
- normalized = normalized.toLowerCase();
28
- normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
29
- normalized = normalized.replace(/\s+/g, " ").trim();
30
- let key = normalized;
31
- if (context?.project) {
32
- key = `${context.project}::${key}`;
33
- }
34
- if (key.length > MAX_KEY_LENGTH) {
35
- const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
36
- key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
37
- }
38
- return key;
39
- }
40
- function makeDedupeKey(content, sessionId) {
41
- const contentHash = createHash("sha256").update(content).digest("hex");
42
- return `${sessionId}:${contentHash}`;
43
- }
44
-
45
- // src/core/db-wrapper.ts
46
- import duckdb from "duckdb";
47
- function convertBigInts(obj) {
48
- if (obj === null || obj === void 0)
49
- return obj;
50
- if (typeof obj === "bigint")
51
- return Number(obj);
52
- if (obj instanceof Date)
53
- return obj;
54
- if (Array.isArray(obj))
55
- return obj.map(convertBigInts);
56
- if (typeof obj === "object") {
57
- const result = {};
58
- for (const [key, value] of Object.entries(obj)) {
59
- result[key] = convertBigInts(value);
60
- }
61
- return result;
62
- }
63
- return obj;
64
- }
65
- function toDate(value) {
66
- if (value instanceof Date)
67
- return value;
68
- if (typeof value === "string")
69
- return new Date(value);
70
- if (typeof value === "number")
71
- return new Date(value);
72
- return new Date(String(value));
73
- }
74
- function createDatabase(path5, options) {
75
- if (options?.readOnly) {
76
- return new duckdb.Database(path5, { access_mode: "READ_ONLY" });
77
- }
78
- return new duckdb.Database(path5);
79
- }
80
- function dbRun(db, sql, params = []) {
81
- return new Promise((resolve2, reject) => {
82
- if (params.length === 0) {
83
- db.run(sql, (err) => {
84
- if (err)
85
- reject(err);
86
- else
87
- resolve2();
88
- });
89
- } else {
90
- db.run(sql, ...params, (err) => {
91
- if (err)
92
- reject(err);
93
- else
94
- resolve2();
95
- });
96
- }
97
- });
98
- }
99
- function dbAll(db, sql, params = []) {
100
- return new Promise((resolve2, reject) => {
101
- if (params.length === 0) {
102
- db.all(sql, (err, rows) => {
103
- if (err)
104
- reject(err);
105
- else
106
- resolve2(convertBigInts(rows || []));
107
- });
108
- } else {
109
- db.all(sql, ...params, (err, rows) => {
110
- if (err)
111
- reject(err);
112
- else
113
- resolve2(convertBigInts(rows || []));
114
- });
115
- }
116
- });
117
- }
118
- function dbClose(db) {
119
- return new Promise((resolve2, reject) => {
120
- db.close((err) => {
121
- if (err)
122
- reject(err);
123
- else
124
- resolve2();
125
- });
126
- });
127
- }
128
-
129
- // src/core/event-store.ts
130
- var EventStore = class {
131
- constructor(dbPath, options) {
132
- this.dbPath = dbPath;
133
- this.readOnly = options?.readOnly ?? false;
134
- this.db = createDatabase(dbPath, { readOnly: this.readOnly });
135
- }
136
- db;
137
- initialized = false;
138
- readOnly;
139
- /**
140
- * Initialize database schema
141
- */
142
- async initialize() {
143
- if (this.initialized)
144
- return;
145
- if (this.readOnly) {
146
- this.initialized = true;
147
- return;
148
- }
149
- await dbRun(this.db, `
150
- CREATE TABLE IF NOT EXISTS events (
151
- id VARCHAR PRIMARY KEY,
152
- event_type VARCHAR NOT NULL,
153
- session_id VARCHAR NOT NULL,
154
- timestamp TIMESTAMP NOT NULL,
155
- content TEXT NOT NULL,
156
- canonical_key VARCHAR NOT NULL,
157
- dedupe_key VARCHAR UNIQUE,
158
- metadata JSON
159
- )
160
- `);
161
- await dbRun(this.db, `
162
- CREATE TABLE IF NOT EXISTS event_dedup (
163
- dedupe_key VARCHAR PRIMARY KEY,
164
- event_id VARCHAR NOT NULL,
165
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
166
- )
167
- `);
168
- await dbRun(this.db, `
169
- CREATE TABLE IF NOT EXISTS sessions (
170
- id VARCHAR PRIMARY KEY,
171
- started_at TIMESTAMP NOT NULL,
172
- ended_at TIMESTAMP,
173
- project_path VARCHAR,
174
- summary TEXT,
175
- tags JSON
176
- )
177
- `);
178
- await dbRun(this.db, `
179
- CREATE TABLE IF NOT EXISTS insights (
180
- id VARCHAR PRIMARY KEY,
181
- insight_type VARCHAR NOT NULL,
182
- content TEXT NOT NULL,
183
- canonical_key VARCHAR NOT NULL,
184
- confidence FLOAT,
185
- source_events JSON,
186
- created_at TIMESTAMP,
187
- last_updated TIMESTAMP
188
- )
189
- `);
190
- await dbRun(this.db, `
191
- CREATE TABLE IF NOT EXISTS embedding_outbox (
192
- id VARCHAR PRIMARY KEY,
193
- event_id VARCHAR NOT NULL,
194
- content TEXT NOT NULL,
195
- status VARCHAR DEFAULT 'pending',
196
- retry_count INT DEFAULT 0,
197
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
198
- processed_at TIMESTAMP,
199
- error_message TEXT
200
- )
201
- `);
202
- await dbRun(this.db, `
203
- CREATE TABLE IF NOT EXISTS projection_offsets (
204
- projection_name VARCHAR PRIMARY KEY,
205
- last_event_id VARCHAR,
206
- last_timestamp TIMESTAMP,
207
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
208
- )
209
- `);
210
- await dbRun(this.db, `
211
- CREATE TABLE IF NOT EXISTS memory_levels (
212
- event_id VARCHAR PRIMARY KEY,
213
- level VARCHAR NOT NULL DEFAULT 'L0',
214
- promoted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
215
- )
216
- `);
217
- await dbRun(this.db, `
218
- CREATE TABLE IF NOT EXISTS entries (
219
- entry_id VARCHAR PRIMARY KEY,
220
- created_ts TIMESTAMP NOT NULL,
221
- entry_type VARCHAR NOT NULL,
222
- title VARCHAR NOT NULL,
223
- content_json JSON NOT NULL,
224
- stage VARCHAR NOT NULL DEFAULT 'raw',
225
- status VARCHAR DEFAULT 'active',
226
- superseded_by VARCHAR,
227
- build_id VARCHAR,
228
- evidence_json JSON,
229
- canonical_key VARCHAR,
230
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
231
- )
232
- `);
233
- await dbRun(this.db, `
234
- CREATE TABLE IF NOT EXISTS entities (
235
- entity_id VARCHAR PRIMARY KEY,
236
- entity_type VARCHAR NOT NULL,
237
- canonical_key VARCHAR NOT NULL,
238
- title VARCHAR NOT NULL,
239
- stage VARCHAR NOT NULL DEFAULT 'raw',
240
- status VARCHAR NOT NULL DEFAULT 'active',
241
- current_json JSON NOT NULL,
242
- title_norm VARCHAR,
243
- search_text VARCHAR,
244
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
245
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
246
- )
247
- `);
248
- await dbRun(this.db, `
249
- CREATE TABLE IF NOT EXISTS entity_aliases (
250
- entity_type VARCHAR NOT NULL,
251
- canonical_key VARCHAR NOT NULL,
252
- entity_id VARCHAR NOT NULL,
253
- is_primary BOOLEAN DEFAULT FALSE,
254
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
255
- PRIMARY KEY(entity_type, canonical_key)
256
- )
257
- `);
258
- await dbRun(this.db, `
259
- CREATE TABLE IF NOT EXISTS edges (
260
- edge_id VARCHAR PRIMARY KEY,
261
- src_type VARCHAR NOT NULL,
262
- src_id VARCHAR NOT NULL,
263
- rel_type VARCHAR NOT NULL,
264
- dst_type VARCHAR NOT NULL,
265
- dst_id VARCHAR NOT NULL,
266
- meta_json JSON,
267
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
268
- )
269
- `);
270
- await dbRun(this.db, `
271
- CREATE TABLE IF NOT EXISTS vector_outbox (
272
- job_id VARCHAR PRIMARY KEY,
273
- item_kind VARCHAR NOT NULL,
274
- item_id VARCHAR NOT NULL,
275
- embedding_version VARCHAR NOT NULL,
276
- status VARCHAR NOT NULL DEFAULT 'pending',
277
- retry_count INT DEFAULT 0,
278
- error VARCHAR,
279
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
280
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
281
- UNIQUE(item_kind, item_id, embedding_version)
282
- )
283
- `);
284
- await dbRun(this.db, `
285
- CREATE TABLE IF NOT EXISTS build_runs (
286
- build_id VARCHAR PRIMARY KEY,
287
- started_at TIMESTAMP NOT NULL,
288
- finished_at TIMESTAMP,
289
- extractor_model VARCHAR NOT NULL,
290
- extractor_prompt_hash VARCHAR NOT NULL,
291
- embedder_model VARCHAR NOT NULL,
292
- embedding_version VARCHAR NOT NULL,
293
- idris_version VARCHAR NOT NULL,
294
- schema_version VARCHAR NOT NULL,
295
- status VARCHAR NOT NULL DEFAULT 'running',
296
- error VARCHAR
297
- )
298
- `);
299
- await dbRun(this.db, `
300
- CREATE TABLE IF NOT EXISTS pipeline_metrics (
301
- id VARCHAR PRIMARY KEY,
302
- ts TIMESTAMP NOT NULL,
303
- stage VARCHAR NOT NULL,
304
- latency_ms DOUBLE NOT NULL,
305
- success BOOLEAN NOT NULL,
306
- error VARCHAR,
307
- session_id VARCHAR
308
- )
309
- `);
310
- await dbRun(this.db, `
311
- CREATE TABLE IF NOT EXISTS working_set (
312
- id VARCHAR PRIMARY KEY,
313
- event_id VARCHAR NOT NULL,
314
- added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
315
- relevance_score FLOAT DEFAULT 1.0,
316
- topics JSON,
317
- expires_at TIMESTAMP
318
- )
319
- `);
320
- await dbRun(this.db, `
321
- CREATE TABLE IF NOT EXISTS consolidated_memories (
322
- memory_id VARCHAR PRIMARY KEY,
323
- summary TEXT NOT NULL,
324
- topics JSON,
325
- source_events JSON,
326
- confidence FLOAT DEFAULT 0.5,
327
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
328
- accessed_at TIMESTAMP,
329
- access_count INTEGER DEFAULT 0
330
- )
331
- `);
332
- await dbRun(this.db, `
333
- CREATE TABLE IF NOT EXISTS continuity_log (
334
- log_id VARCHAR PRIMARY KEY,
335
- from_context_id VARCHAR,
336
- to_context_id VARCHAR,
337
- continuity_score FLOAT,
338
- transition_type VARCHAR,
339
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
340
- )
341
- `);
342
- await dbRun(this.db, `
343
- CREATE TABLE IF NOT EXISTS consolidated_rules (
344
- rule_id VARCHAR PRIMARY KEY,
345
- rule TEXT NOT NULL,
346
- topics JSON,
347
- source_memory_ids JSON,
348
- source_events JSON,
349
- confidence FLOAT DEFAULT 0.5,
350
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
351
- )
352
- `);
353
- await dbRun(this.db, `
354
- CREATE TABLE IF NOT EXISTS endless_config (
355
- key VARCHAR PRIMARY KEY,
356
- value JSON,
357
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
358
- )
359
- `);
360
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type)`);
361
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_stage ON entries(stage)`);
362
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_canonical ON entries(canonical_key)`);
363
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_type_key ON entities(entity_type, canonical_key)`);
364
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_status ON entities(status)`);
365
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_src ON edges(src_id, rel_type)`);
366
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_dst ON edges(dst_id, rel_type)`);
367
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_rel ON edges(rel_type)`);
368
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_outbox_status ON vector_outbox(status)`);
369
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
370
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
371
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
372
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
373
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
374
- this.initialized = true;
375
- }
376
- /**
377
- * Append event to store (AXIOMMIND Principle 2: Append-only)
378
- * Returns existing event ID if duplicate (Principle 3: Idempotency)
379
- */
380
- async append(input) {
381
- await this.initialize();
382
- const canonicalKey = makeCanonicalKey(input.content);
383
- const dedupeKey = makeDedupeKey(input.content, input.sessionId);
384
- const existing = await dbAll(
385
- this.db,
386
- `SELECT event_id FROM event_dedup WHERE dedupe_key = ?`,
387
- [dedupeKey]
388
- );
389
- if (existing.length > 0) {
390
- return {
391
- success: true,
392
- eventId: existing[0].event_id,
393
- isDuplicate: true
394
- };
395
- }
396
- const id = randomUUID();
397
- const timestamp = input.timestamp.toISOString();
398
- try {
399
- await dbRun(
400
- this.db,
401
- `INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
402
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
403
- [
404
- id,
405
- input.eventType,
406
- input.sessionId,
407
- timestamp,
408
- input.content,
409
- canonicalKey,
410
- dedupeKey,
411
- JSON.stringify(input.metadata || {})
412
- ]
413
- );
414
- await dbRun(
415
- this.db,
416
- `INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)`,
417
- [dedupeKey, id]
418
- );
419
- await dbRun(
420
- this.db,
421
- `INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')`,
422
- [id]
423
- );
424
- return { success: true, eventId: id, isDuplicate: false };
425
- } catch (error) {
426
- return {
427
- success: false,
428
- error: error instanceof Error ? error.message : String(error)
429
- };
430
- }
431
- }
432
- /**
433
- * Get events by session ID
434
- */
435
- async getSessionEvents(sessionId) {
436
- await this.initialize();
437
- const rows = await dbAll(
438
- this.db,
439
- `SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
440
- [sessionId]
441
- );
442
- return rows.map(this.rowToEvent);
443
- }
444
- /**
445
- * Get recent events
446
- */
447
- async getRecentEvents(limit = 100) {
448
- await this.initialize();
449
- const rows = await dbAll(
450
- this.db,
451
- `SELECT * FROM events ORDER BY timestamp DESC LIMIT ?`,
452
- [limit]
453
- );
454
- return rows.map(this.rowToEvent);
455
- }
456
- /**
457
- * Get event by ID
458
- */
459
- async getEvent(id) {
460
- await this.initialize();
461
- const rows = await dbAll(
462
- this.db,
463
- `SELECT * FROM events WHERE id = ?`,
464
- [id]
465
- );
466
- if (rows.length === 0)
467
- return null;
468
- return this.rowToEvent(rows[0]);
469
- }
470
- /**
471
- * Create or update session
472
- */
473
- async upsertSession(session) {
474
- await this.initialize();
475
- const existing = await dbAll(
476
- this.db,
477
- `SELECT id FROM sessions WHERE id = ?`,
478
- [session.id]
479
- );
480
- if (existing.length === 0) {
481
- await dbRun(
482
- this.db,
483
- `INSERT INTO sessions (id, started_at, project_path, tags)
484
- VALUES (?, ?, ?, ?)`,
485
- [
486
- session.id,
487
- (session.startedAt || /* @__PURE__ */ new Date()).toISOString(),
488
- session.projectPath || null,
489
- JSON.stringify(session.tags || [])
490
- ]
491
- );
492
- } else {
493
- const updates = [];
494
- const values = [];
495
- if (session.endedAt) {
496
- updates.push("ended_at = ?");
497
- values.push(session.endedAt.toISOString());
498
- }
499
- if (session.summary) {
500
- updates.push("summary = ?");
501
- values.push(session.summary);
502
- }
503
- if (session.tags) {
504
- updates.push("tags = ?");
505
- values.push(JSON.stringify(session.tags));
506
- }
507
- if (updates.length > 0) {
508
- values.push(session.id);
509
- await dbRun(
510
- this.db,
511
- `UPDATE sessions SET ${updates.join(", ")} WHERE id = ?`,
512
- values
513
- );
514
- }
515
- }
516
- }
517
- /**
518
- * Get session by ID
519
- */
520
- async getSession(id) {
521
- await this.initialize();
522
- const rows = await dbAll(
523
- this.db,
524
- `SELECT * FROM sessions WHERE id = ?`,
525
- [id]
526
- );
527
- if (rows.length === 0)
528
- return null;
529
- const row = rows[0];
530
- return {
531
- id: row.id,
532
- startedAt: toDate(row.started_at),
533
- endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
534
- projectPath: row.project_path,
535
- summary: row.summary,
536
- tags: row.tags ? JSON.parse(row.tags) : void 0
537
- };
538
- }
539
- /**
540
- * Add to embedding outbox (Single-Writer Pattern)
541
- */
542
- async enqueueForEmbedding(eventId, content) {
543
- await this.initialize();
544
- const id = randomUUID();
545
- await dbRun(
546
- this.db,
547
- `INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
548
- VALUES (?, ?, ?, 'pending', 0)`,
549
- [id, eventId, content]
550
- );
551
- return id;
552
- }
553
- /**
554
- * Get pending outbox items
555
- */
556
- async getPendingOutboxItems(limit = 32) {
557
- await this.initialize();
558
- const pending = await dbAll(
559
- this.db,
560
- `SELECT * FROM embedding_outbox
561
- WHERE status = 'pending'
562
- ORDER BY created_at
563
- LIMIT ?`,
564
- [limit]
565
- );
566
- if (pending.length === 0)
567
- return [];
568
- const ids = pending.map((r) => r.id);
569
- const placeholders = ids.map(() => "?").join(",");
570
- await dbRun(
571
- this.db,
572
- `UPDATE embedding_outbox SET status = 'processing' WHERE id IN (${placeholders})`,
573
- ids
574
- );
575
- return pending.map((row) => ({
576
- id: row.id,
577
- eventId: row.event_id,
578
- content: row.content,
579
- status: "processing",
580
- retryCount: row.retry_count,
581
- createdAt: toDate(row.created_at),
582
- errorMessage: row.error_message
583
- }));
584
- }
585
- /**
586
- * Mark outbox items as done
587
- */
588
- async completeOutboxItems(ids) {
589
- if (ids.length === 0)
590
- return;
591
- const placeholders = ids.map(() => "?").join(",");
592
- await dbRun(
593
- this.db,
594
- `DELETE FROM embedding_outbox WHERE id IN (${placeholders})`,
595
- ids
596
- );
597
- }
598
- /**
599
- * Mark outbox items as failed
600
- */
601
- async failOutboxItems(ids, error) {
602
- if (ids.length === 0)
603
- return;
604
- const placeholders = ids.map(() => "?").join(",");
605
- await dbRun(
606
- this.db,
607
- `UPDATE embedding_outbox
608
- SET status = CASE WHEN retry_count >= 3 THEN 'failed' ELSE 'pending' END,
609
- retry_count = retry_count + 1,
610
- error_message = ?
611
- WHERE id IN (${placeholders})`,
612
- [error, ...ids]
613
- );
614
- }
615
- /**
616
- * Update memory level
617
- */
618
- async updateMemoryLevel(eventId, level) {
619
- await this.initialize();
620
- await dbRun(
621
- this.db,
622
- `UPDATE memory_levels SET level = ?, promoted_at = CURRENT_TIMESTAMP WHERE event_id = ?`,
623
- [level, eventId]
624
- );
625
- }
626
- /**
627
- * Get memory level statistics
628
- */
629
- async getLevelStats() {
630
- await this.initialize();
631
- const rows = await dbAll(
632
- this.db,
633
- `SELECT level, COUNT(*) as count FROM memory_levels GROUP BY level`
634
- );
635
- return rows;
636
- }
637
- /**
638
- * Get events by memory level
639
- */
640
- async getEventsByLevel(level, options) {
641
- await this.initialize();
642
- const limit = options?.limit || 50;
643
- const offset = options?.offset || 0;
644
- const rows = await dbAll(
645
- this.db,
646
- `SELECT e.* FROM events e
647
- INNER JOIN memory_levels ml ON e.id = ml.event_id
648
- WHERE ml.level = ?
649
- ORDER BY e.timestamp DESC
650
- LIMIT ? OFFSET ?`,
651
- [level, limit, offset]
652
- );
653
- return rows.map((row) => this.rowToEvent(row));
654
- }
655
- /**
656
- * Get memory level for a specific event
657
- */
658
- async getEventLevel(eventId) {
659
- await this.initialize();
660
- const rows = await dbAll(
661
- this.db,
662
- `SELECT level FROM memory_levels WHERE event_id = ?`,
663
- [eventId]
664
- );
665
- return rows.length > 0 ? rows[0].level : null;
666
- }
667
- // ============================================================
668
- // Endless Mode Helper Methods
669
- // ============================================================
670
- /**
671
- * Get database instance for Endless Mode stores
672
- */
673
- getDatabase() {
674
- return this.db;
675
- }
676
- /**
677
- * Get config value for endless mode
678
- */
679
- async getEndlessConfig(key) {
680
- await this.initialize();
681
- const rows = await dbAll(
682
- this.db,
683
- `SELECT value FROM endless_config WHERE key = ?`,
684
- [key]
685
- );
686
- if (rows.length === 0)
687
- return null;
688
- return JSON.parse(rows[0].value);
689
- }
690
- /**
691
- * Set config value for endless mode
692
- */
693
- async setEndlessConfig(key, value) {
694
- await this.initialize();
695
- await dbRun(
696
- this.db,
697
- `INSERT OR REPLACE INTO endless_config (key, value, updated_at)
698
- VALUES (?, ?, CURRENT_TIMESTAMP)`,
699
- [key, JSON.stringify(value)]
700
- );
701
- }
702
- /**
703
- * Get all sessions
704
- */
705
- async getAllSessions() {
706
- await this.initialize();
707
- const rows = await dbAll(
708
- this.db,
709
- `SELECT * FROM sessions ORDER BY started_at DESC`
710
- );
711
- return rows.map((row) => ({
712
- id: row.id,
713
- startedAt: toDate(row.started_at),
714
- endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
715
- projectPath: row.project_path,
716
- summary: row.summary,
717
- tags: row.tags ? JSON.parse(row.tags) : void 0
718
- }));
719
- }
720
- /**
721
- * Increment access count for events (stub for compatibility)
722
- */
723
- async incrementAccessCount(eventIds) {
724
- return Promise.resolve();
725
- }
726
- /**
727
- * Get most accessed memories (stub for compatibility)
728
- */
729
- async getMostAccessed(limit = 10) {
730
- return [];
731
- }
732
- /**
733
- * Close database connection
734
- */
735
- async close() {
736
- await dbClose(this.db);
19
+ // src/core/sqlite-event-store.ts
20
+ import { randomUUID } from "crypto";
21
+
22
+ // src/core/canonical-key.ts
23
+ import { createHash } from "crypto";
24
+ var MAX_KEY_LENGTH = 200;
25
+ function makeCanonicalKey(title, context) {
26
+ let normalized = title.normalize("NFKC");
27
+ normalized = normalized.toLowerCase();
28
+ normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
29
+ normalized = normalized.replace(/\s+/g, " ").trim();
30
+ let key = normalized;
31
+ if (context?.project) {
32
+ key = `${context.project}::${key}`;
737
33
  }
738
- /**
739
- * Convert database row to MemoryEvent
740
- */
741
- rowToEvent(row) {
742
- return {
743
- id: row.id,
744
- eventType: row.event_type,
745
- sessionId: row.session_id,
746
- timestamp: toDate(row.timestamp),
747
- content: row.content,
748
- canonicalKey: row.canonical_key,
749
- dedupeKey: row.dedupe_key,
750
- metadata: row.metadata ? JSON.parse(row.metadata) : void 0
751
- };
34
+ if (key.length > MAX_KEY_LENGTH) {
35
+ const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
36
+ key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
752
37
  }
753
- };
754
-
755
- // src/core/sqlite-event-store.ts
756
- import { randomUUID as randomUUID2 } from "crypto";
38
+ return key;
39
+ }
40
+ function makeDedupeKey(content, sessionId) {
41
+ const contentHash = createHash("sha256").update(content).digest("hex");
42
+ return `${sessionId}:${contentHash}`;
43
+ }
757
44
 
758
45
  // src/core/sqlite-wrapper.ts
759
46
  import Database from "better-sqlite3";
@@ -1268,7 +555,7 @@ var SQLiteEventStore = class {
1268
555
  isDuplicate: true
1269
556
  };
1270
557
  }
1271
- const id = randomUUID2();
558
+ const id = randomUUID();
1272
559
  const timestamp = toSQLiteTimestamp(input.timestamp);
1273
560
  try {
1274
561
  const metadata = input.metadata || {};
@@ -1322,6 +609,29 @@ var SQLiteEventStore = class {
1322
609
  };
1323
610
  }
1324
611
  }
612
+ /**
613
+ * Get session IDs that have events but no session_summary event.
614
+ * Used to backfill summaries for sessions that ended without Stop hook.
615
+ */
616
+ async getSessionsWithoutSummary(currentSessionId, limit = 5) {
617
+ await this.initialize();
618
+ const rows = sqliteAll(
619
+ this.db,
620
+ `SELECT DISTINCT e.session_id
621
+ FROM events e
622
+ WHERE e.session_id != ?
623
+ AND e.event_type != 'session_summary'
624
+ AND e.session_id NOT IN (
625
+ SELECT DISTINCT session_id FROM events WHERE event_type = 'session_summary'
626
+ )
627
+ GROUP BY e.session_id
628
+ HAVING COUNT(*) >= 3
629
+ ORDER BY MAX(e.timestamp) DESC
630
+ LIMIT ?`,
631
+ [currentSessionId, limit]
632
+ );
633
+ return rows.map((r) => r.session_id);
634
+ }
1325
635
  /**
1326
636
  * Get events by session ID
1327
637
  */
@@ -1549,7 +859,7 @@ var SQLiteEventStore = class {
1549
859
  */
1550
860
  async enqueueForEmbedding(eventId, content) {
1551
861
  await this.initialize();
1552
- const id = randomUUID2();
862
+ const id = randomUUID();
1553
863
  sqliteRun(
1554
864
  this.db,
1555
865
  `INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
@@ -1830,7 +1140,7 @@ var SQLiteEventStore = class {
1830
1140
  if (this.readOnly)
1831
1141
  return;
1832
1142
  await this.initialize();
1833
- const id = randomUUID2();
1143
+ const id = randomUUID();
1834
1144
  sqliteRun(
1835
1145
  this.db,
1836
1146
  `INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
@@ -1838,6 +1148,21 @@ var SQLiteEventStore = class {
1838
1148
  [id, eventId, sessionId, score, query.slice(0, 100)]
1839
1149
  );
1840
1150
  }
1151
+ /**
1152
+ * Get session IDs that have unevaluated retrievals (measured_at IS NULL).
1153
+ * Excludes the current session. Used to backfill sessions that ended without Stop hook.
1154
+ */
1155
+ async getUnevaluatedSessions(currentSessionId, limit = 5) {
1156
+ await this.initialize();
1157
+ const rows = sqliteAll(
1158
+ this.db,
1159
+ `SELECT DISTINCT session_id FROM memory_helpfulness
1160
+ WHERE measured_at IS NULL AND session_id != ?
1161
+ ORDER BY created_at DESC LIMIT ?`,
1162
+ [currentSessionId, limit]
1163
+ );
1164
+ return rows.map((r) => r.session_id);
1165
+ }
1841
1166
  /**
1842
1167
  * Evaluate helpfulness for all retrievals in a session
1843
1168
  * Called at session end - uses behavioral signals to compute score
@@ -2036,7 +1361,7 @@ var SQLiteEventStore = class {
2036
1361
  }
2037
1362
  async recordRetrievalTrace(input) {
2038
1363
  await this.initialize();
2039
- const traceId = randomUUID2();
1364
+ const traceId = randomUUID();
2040
1365
  sqliteRun(
2041
1366
  this.db,
2042
1367
  `INSERT INTO retrieval_traces (
@@ -2290,169 +1615,6 @@ var SQLiteEventStore = class {
2290
1615
  }
2291
1616
  };
2292
1617
 
2293
- // src/core/sync-worker.ts
2294
- var DEFAULT_CONFIG = {
2295
- intervalMs: 3e4,
2296
- batchSize: 500,
2297
- maxRetries: 3,
2298
- retryDelayMs: 5e3
2299
- };
2300
- var SyncWorker = class {
2301
- constructor(sqliteStore, duckdbStore, config) {
2302
- this.sqliteStore = sqliteStore;
2303
- this.duckdbStore = duckdbStore;
2304
- this.config = { ...DEFAULT_CONFIG, ...config };
2305
- }
2306
- config;
2307
- intervalHandle = null;
2308
- running = false;
2309
- stats = {
2310
- lastSyncAt: null,
2311
- eventsSynced: 0,
2312
- sessionsSynced: 0,
2313
- errors: 0,
2314
- status: "idle"
2315
- };
2316
- /**
2317
- * Start the sync worker
2318
- */
2319
- start() {
2320
- if (this.running)
2321
- return;
2322
- this.running = true;
2323
- this.stats.status = "idle";
2324
- this.syncNow().catch((err) => {
2325
- console.error("[SyncWorker] Initial sync failed:", err);
2326
- });
2327
- this.intervalHandle = setInterval(() => {
2328
- this.syncNow().catch((err) => {
2329
- console.error("[SyncWorker] Periodic sync failed:", err);
2330
- });
2331
- }, this.config.intervalMs);
2332
- }
2333
- /**
2334
- * Stop the sync worker
2335
- */
2336
- stop() {
2337
- this.running = false;
2338
- this.stats.status = "stopped";
2339
- if (this.intervalHandle) {
2340
- clearInterval(this.intervalHandle);
2341
- this.intervalHandle = null;
2342
- }
2343
- }
2344
- /**
2345
- * Trigger immediate sync
2346
- */
2347
- async syncNow() {
2348
- if (this.stats.status === "syncing") {
2349
- return;
2350
- }
2351
- this.stats.status = "syncing";
2352
- try {
2353
- await this.syncEvents();
2354
- await this.syncSessions();
2355
- this.stats.lastSyncAt = /* @__PURE__ */ new Date();
2356
- this.stats.status = "idle";
2357
- } catch (error) {
2358
- this.stats.errors++;
2359
- this.stats.status = "error";
2360
- throw error;
2361
- }
2362
- }
2363
- /**
2364
- * Sync events from SQLite to DuckDB
2365
- */
2366
- async syncEvents() {
2367
- const targetName = "duckdb_analytics";
2368
- const position = await this.sqliteStore.getSyncPosition(targetName);
2369
- const lastTimestamp = position.lastTimestamp || "1970-01-01T00:00:00.000Z";
2370
- let hasMore = true;
2371
- let totalSynced = 0;
2372
- while (hasMore) {
2373
- const events = await this.sqliteStore.getEventsSince(lastTimestamp, this.config.batchSize);
2374
- if (events.length === 0) {
2375
- hasMore = false;
2376
- break;
2377
- }
2378
- await this.retryWithBackoff(async () => {
2379
- for (const event of events) {
2380
- await this.insertEventToDuckDB(event);
2381
- }
2382
- });
2383
- totalSynced += events.length;
2384
- const lastEvent = events[events.length - 1];
2385
- await this.sqliteStore.updateSyncPosition(
2386
- targetName,
2387
- lastEvent.id,
2388
- lastEvent.timestamp.toISOString()
2389
- );
2390
- hasMore = events.length === this.config.batchSize;
2391
- }
2392
- this.stats.eventsSynced += totalSynced;
2393
- }
2394
- /**
2395
- * Sync sessions from SQLite to DuckDB
2396
- */
2397
- async syncSessions() {
2398
- const sessions = await this.sqliteStore.getAllSessions();
2399
- for (const session of sessions) {
2400
- await this.retryWithBackoff(async () => {
2401
- await this.duckdbStore.upsertSession(session);
2402
- });
2403
- }
2404
- this.stats.sessionsSynced = sessions.length;
2405
- }
2406
- /**
2407
- * Insert a single event into DuckDB
2408
- */
2409
- async insertEventToDuckDB(event) {
2410
- await this.duckdbStore.append({
2411
- eventType: event.eventType,
2412
- sessionId: event.sessionId,
2413
- timestamp: event.timestamp,
2414
- content: event.content,
2415
- metadata: event.metadata
2416
- });
2417
- }
2418
- /**
2419
- * Retry operation with exponential backoff
2420
- */
2421
- async retryWithBackoff(fn) {
2422
- let lastError = null;
2423
- for (let attempt = 0; attempt < this.config.maxRetries; attempt++) {
2424
- try {
2425
- return await fn();
2426
- } catch (error) {
2427
- lastError = error instanceof Error ? error : new Error(String(error));
2428
- if (attempt < this.config.maxRetries - 1) {
2429
- const delay = this.config.retryDelayMs * Math.pow(2, attempt);
2430
- await this.sleep(delay);
2431
- }
2432
- }
2433
- }
2434
- throw lastError;
2435
- }
2436
- /**
2437
- * Sleep utility
2438
- */
2439
- sleep(ms) {
2440
- return new Promise((resolve2) => setTimeout(resolve2, ms));
2441
- }
2442
- /**
2443
- * Get sync statistics
2444
- */
2445
- getStats() {
2446
- return { ...this.stats };
2447
- }
2448
- /**
2449
- * Check if worker is running
2450
- */
2451
- isRunning() {
2452
- return this.running;
2453
- }
2454
- };
2455
-
2456
1618
  // src/core/vector-store.ts
2457
1619
  import * as lancedb from "@lancedb/lancedb";
2458
1620
  var VectorStore = class {
@@ -2625,7 +1787,7 @@ var VectorStore = class {
2625
1787
 
2626
1788
  // src/core/embedder.ts
2627
1789
  import { pipeline } from "@huggingface/transformers";
2628
- var Embedder = class {
1790
+ var Embedder = class _Embedder {
2629
1791
  pipeline = null;
2630
1792
  modelName;
2631
1793
  activeModelName;
@@ -2656,6 +1818,11 @@ var Embedder = class {
2656
1818
  this.initialized = true;
2657
1819
  }
2658
1820
  }
1821
+ // ~4 chars per token; 512 tokens * 4 = 2048, use 2000 to be safe
1822
+ static MAX_CHARS = 2e3;
1823
+ truncate(text) {
1824
+ return text.length > _Embedder.MAX_CHARS ? text.slice(0, _Embedder.MAX_CHARS) : text;
1825
+ }
2659
1826
  /**
2660
1827
  * Generate embedding for a single text
2661
1828
  */
@@ -2664,10 +1831,11 @@ var Embedder = class {
2664
1831
  if (!this.pipeline) {
2665
1832
  throw new Error("Embedding pipeline not initialized");
2666
1833
  }
2667
- const output = await this.pipeline(text, {
1834
+ const output = await this.pipeline(this.truncate(text), {
2668
1835
  pooling: "mean",
2669
1836
  normalize: true,
2670
- truncation: true
1837
+ truncation: true,
1838
+ max_length: 512
2671
1839
  });
2672
1840
  const vector = Array.from(output.data);
2673
1841
  return {
@@ -2689,10 +1857,11 @@ var Embedder = class {
2689
1857
  for (let i = 0; i < texts.length; i += batchSize) {
2690
1858
  const batch = texts.slice(i, i + batchSize);
2691
1859
  for (const text of batch) {
2692
- const output = await this.pipeline(text, {
1860
+ const output = await this.pipeline(this.truncate(text), {
2693
1861
  pooling: "mean",
2694
1862
  normalize: true,
2695
- truncation: true
1863
+ truncation: true,
1864
+ max_length: 512
2696
1865
  });
2697
1866
  const vector = Array.from(output.data);
2698
1867
  results.push({
@@ -2733,8 +1902,34 @@ function getDefaultEmbedder() {
2733
1902
  return defaultEmbedder;
2734
1903
  }
2735
1904
 
1905
+ // src/core/db-wrapper.ts
1906
+ import BetterSqlite3 from "better-sqlite3";
1907
+ function toDate(value) {
1908
+ if (value instanceof Date)
1909
+ return value;
1910
+ if (typeof value === "string")
1911
+ return new Date(value);
1912
+ if (typeof value === "number")
1913
+ return new Date(value);
1914
+ return new Date(String(value));
1915
+ }
1916
+ function createDatabase(dbPath, options) {
1917
+ return new BetterSqlite3(dbPath, { readonly: options?.readOnly });
1918
+ }
1919
+ function dbRun(db, sql, params = []) {
1920
+ db.prepare(sql).run(...params);
1921
+ return Promise.resolve();
1922
+ }
1923
+ function dbAll(db, sql, params = []) {
1924
+ return Promise.resolve(db.prepare(sql).all(...params));
1925
+ }
1926
+ function dbClose(db) {
1927
+ db.close();
1928
+ return Promise.resolve();
1929
+ }
1930
+
2736
1931
  // src/core/vector-outbox.ts
2737
- var DEFAULT_CONFIG2 = {
1932
+ var DEFAULT_CONFIG = {
2738
1933
  embeddingVersion: "v1",
2739
1934
  maxRetries: 3,
2740
1935
  stuckThresholdMs: 5 * 60 * 1e3,
@@ -2743,7 +1938,7 @@ var DEFAULT_CONFIG2 = {
2743
1938
  };
2744
1939
 
2745
1940
  // src/core/vector-worker.ts
2746
- var DEFAULT_CONFIG3 = {
1941
+ var DEFAULT_CONFIG2 = {
2747
1942
  batchSize: 32,
2748
1943
  pollIntervalMs: 1e3,
2749
1944
  maxRetries: 3
@@ -2760,7 +1955,7 @@ var VectorWorker = class {
2760
1955
  this.eventStore = eventStore;
2761
1956
  this.vectorStore = vectorStore;
2762
1957
  this.embedder = embedder;
2763
- this.config = { ...DEFAULT_CONFIG3, ...config };
1958
+ this.config = { ...DEFAULT_CONFIG2, ...config };
2764
1959
  }
2765
1960
  /**
2766
1961
  * Start the worker polling loop
@@ -2881,7 +2076,7 @@ function createVectorWorker(eventStore, vectorStore, embedder, config) {
2881
2076
  }
2882
2077
 
2883
2078
  // src/core/matcher.ts
2884
- var DEFAULT_CONFIG4 = {
2079
+ var DEFAULT_CONFIG3 = {
2885
2080
  weights: {
2886
2081
  semanticSimilarity: 0.4,
2887
2082
  ftsScore: 0.25,
@@ -2896,9 +2091,9 @@ var Matcher = class {
2896
2091
  config;
2897
2092
  constructor(config = {}) {
2898
2093
  this.config = {
2899
- ...DEFAULT_CONFIG4,
2094
+ ...DEFAULT_CONFIG3,
2900
2095
  ...config,
2901
- weights: { ...DEFAULT_CONFIG4.weights, ...config.weights }
2096
+ weights: { ...DEFAULT_CONFIG3.weights, ...config.weights }
2902
2097
  };
2903
2098
  }
2904
2099
  /**
@@ -3888,7 +3083,7 @@ function createSharedEventStore(dbPath) {
3888
3083
  }
3889
3084
 
3890
3085
  // src/core/shared-store.ts
3891
- import { randomUUID as randomUUID3 } from "crypto";
3086
+ import { randomUUID as randomUUID2 } from "crypto";
3892
3087
  var SharedStore = class {
3893
3088
  constructor(sharedEventStore) {
3894
3089
  this.sharedEventStore = sharedEventStore;
@@ -3900,7 +3095,7 @@ var SharedStore = class {
3900
3095
  * Promote a verified troubleshooting entry to shared storage
3901
3096
  */
3902
3097
  async promoteEntry(input) {
3903
- const entryId = randomUUID3();
3098
+ const entryId = randomUUID2();
3904
3099
  await dbRun(
3905
3100
  this.db,
3906
3101
  `INSERT INTO shared_troubleshooting (
@@ -4265,7 +3460,7 @@ function createSharedVectorStore(dbPath) {
4265
3460
  }
4266
3461
 
4267
3462
  // src/core/shared-promoter.ts
4268
- import { randomUUID as randomUUID4 } from "crypto";
3463
+ import { randomUUID as randomUUID3 } from "crypto";
4269
3464
  var SharedPromoter = class {
4270
3465
  constructor(sharedStore, sharedVectorStore, embedder, config) {
4271
3466
  this.sharedStore = sharedStore;
@@ -4333,7 +3528,7 @@ var SharedPromoter = class {
4333
3528
  const embeddingContent = this.createEmbeddingContent(input);
4334
3529
  const embedding = await this.embedder.embed(embeddingContent);
4335
3530
  await this.sharedVectorStore.upsert({
4336
- id: randomUUID4(),
3531
+ id: randomUUID3(),
4337
3532
  entryId,
4338
3533
  entryType: "troubleshooting",
4339
3534
  content: embeddingContent,
@@ -4473,7 +3668,7 @@ function createToolObservationEmbedding(toolName, metadata, success) {
4473
3668
  }
4474
3669
 
4475
3670
  // src/core/working-set-store.ts
4476
- import { randomUUID as randomUUID5 } from "crypto";
3671
+ import { randomUUID as randomUUID4 } from "crypto";
4477
3672
  var WorkingSetStore = class {
4478
3673
  constructor(eventStore, config) {
4479
3674
  this.eventStore = eventStore;
@@ -4494,7 +3689,7 @@ var WorkingSetStore = class {
4494
3689
  `INSERT OR REPLACE INTO working_set (id, event_id, added_at, relevance_score, topics, expires_at)
4495
3690
  VALUES (?, ?, CURRENT_TIMESTAMP, ?, ?, ?)`,
4496
3691
  [
4497
- randomUUID5(),
3692
+ randomUUID4(),
4498
3693
  eventId,
4499
3694
  relevanceScore,
4500
3695
  JSON.stringify(topics || []),
@@ -4678,7 +3873,7 @@ function createWorkingSetStore(eventStore, config) {
4678
3873
  }
4679
3874
 
4680
3875
  // src/core/consolidated-store.ts
4681
- import { randomUUID as randomUUID6 } from "crypto";
3876
+ import { randomUUID as randomUUID5 } from "crypto";
4682
3877
  var ConsolidatedStore = class {
4683
3878
  constructor(eventStore) {
4684
3879
  this.eventStore = eventStore;
@@ -4690,7 +3885,7 @@ var ConsolidatedStore = class {
4690
3885
  * Create a new consolidated memory
4691
3886
  */
4692
3887
  async create(input) {
4693
- const memoryId = randomUUID6();
3888
+ const memoryId = randomUUID5();
4694
3889
  await dbRun(
4695
3890
  this.db,
4696
3891
  `INSERT INTO consolidated_memories
@@ -4820,7 +4015,7 @@ var ConsolidatedStore = class {
4820
4015
  * Create a long-term rule promoted from stable summaries
4821
4016
  */
4822
4017
  async createRule(input) {
4823
- const ruleId = randomUUID6();
4018
+ const ruleId = randomUUID5();
4824
4019
  await dbRun(
4825
4020
  this.db,
4826
4021
  `INSERT INTO consolidated_rules
@@ -5342,7 +4537,7 @@ function createConsolidationWorker(workingSetStore, consolidatedStore, config) {
5342
4537
  }
5343
4538
 
5344
4539
  // src/core/continuity-manager.ts
5345
- import { randomUUID as randomUUID7 } from "crypto";
4540
+ import { randomUUID as randomUUID6 } from "crypto";
5346
4541
  var ContinuityManager = class {
5347
4542
  constructor(eventStore, config) {
5348
4543
  this.eventStore = eventStore;
@@ -5496,7 +4691,7 @@ var ContinuityManager = class {
5496
4691
  `INSERT INTO continuity_log
5497
4692
  (log_id, from_context_id, to_context_id, continuity_score, transition_type, created_at)
5498
4693
  VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
5499
- [randomUUID7(), previous.id, current.id, score, type]
4694
+ [randomUUID6(), previous.id, current.id, score, type]
5500
4695
  );
5501
4696
  }
5502
4697
  /**
@@ -5605,7 +4800,7 @@ function createContinuityManager(eventStore, config) {
5605
4800
  }
5606
4801
 
5607
4802
  // src/core/graduation-worker.ts
5608
- var DEFAULT_CONFIG5 = {
4803
+ var DEFAULT_CONFIG4 = {
5609
4804
  evaluationIntervalMs: 3e5,
5610
4805
  // 5 minutes
5611
4806
  batchSize: 50,
@@ -5613,7 +4808,7 @@ var DEFAULT_CONFIG5 = {
5613
4808
  // 1 hour cooldown between evaluations
5614
4809
  };
5615
4810
  var GraduationWorker = class {
5616
- constructor(eventStore, graduation, config = DEFAULT_CONFIG5) {
4811
+ constructor(eventStore, graduation, config = DEFAULT_CONFIG4) {
5617
4812
  this.eventStore = eventStore;
5618
4813
  this.graduation = graduation;
5619
4814
  this.config = config;
@@ -5721,7 +4916,7 @@ function createGraduationWorker(eventStore, graduation, config) {
5721
4916
  return new GraduationWorker(
5722
4917
  eventStore,
5723
4918
  graduation,
5724
- { ...DEFAULT_CONFIG5, ...config }
4919
+ { ...DEFAULT_CONFIG4, ...config }
5725
4920
  );
5726
4921
  }
5727
4922
 
@@ -5934,9 +5129,6 @@ function getSessionProject(sessionId) {
5934
5129
  var MemoryService = class {
5935
5130
  // Primary store: SQLite (WAL mode) - for hooks, always available
5936
5131
  sqliteStore;
5937
- // Analytics store: DuckDB - for server reads (optional, synced from SQLite)
5938
- analyticsStore;
5939
- syncWorker = null;
5940
5132
  vectorStore;
5941
5133
  embedder;
5942
5134
  matcher;
@@ -5962,6 +5154,7 @@ var MemoryService = class {
5962
5154
  projectPath = null;
5963
5155
  readOnly;
5964
5156
  lightweightMode;
5157
+ embeddingOnly;
5965
5158
  mdMirror;
5966
5159
  storagePath;
5967
5160
  constructor(config) {
@@ -5969,6 +5162,7 @@ var MemoryService = class {
5969
5162
  this.storagePath = storagePath;
5970
5163
  this.readOnly = config.readOnly ?? false;
5971
5164
  this.lightweightMode = config.lightweightMode ?? false;
5165
+ this.embeddingOnly = config.embeddingOnly ?? false;
5972
5166
  this.mdMirror = new MarkdownMirror2(process.cwd());
5973
5167
  if (!this.readOnly && !fs4.existsSync(storagePath)) {
5974
5168
  fs4.mkdirSync(storagePath, { recursive: true });
@@ -5983,24 +5177,6 @@ var MemoryService = class {
5983
5177
  markdownMirrorRoot: storagePath
5984
5178
  }
5985
5179
  );
5986
- const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
5987
- if (!analyticsEnabled) {
5988
- this.analyticsStore = null;
5989
- } else if (this.readOnly) {
5990
- try {
5991
- this.analyticsStore = new EventStore(
5992
- path3.join(storagePath, "analytics.duckdb"),
5993
- { readOnly: true }
5994
- );
5995
- } catch {
5996
- this.analyticsStore = null;
5997
- }
5998
- } else {
5999
- this.analyticsStore = new EventStore(
6000
- path3.join(storagePath, "analytics.duckdb"),
6001
- { readOnly: false }
6002
- );
6003
- }
6004
5180
  this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
6005
5181
  const embeddingModel = config.embeddingModel || process.env.CLAUDE_MEMORY_EMBEDDING_MODEL;
6006
5182
  this.embedder = embeddingModel ? new Embedder(embeddingModel) : getDefaultEmbedder();
@@ -6026,13 +5202,6 @@ var MemoryService = class {
6026
5202
  this.initialized = true;
6027
5203
  return;
6028
5204
  }
6029
- if (this.analyticsStore) {
6030
- try {
6031
- await this.analyticsStore.initialize();
6032
- } catch (error) {
6033
- console.warn("[MemoryService] Analytics store (DuckDB) initialization failed, using SQLite for reads:", error);
6034
- }
6035
- }
6036
5205
  await this.vectorStore.initialize();
6037
5206
  await this.embedder.initialize();
6038
5207
  if (!this.readOnly) {
@@ -6042,19 +5211,13 @@ var MemoryService = class {
6042
5211
  this.embedder
6043
5212
  );
6044
5213
  this.vectorWorker.start();
6045
- this.retriever.setGraduationPipeline(this.graduation);
6046
- this.graduationWorker = createGraduationWorker(
6047
- this.sqliteStore,
6048
- this.graduation
6049
- );
6050
- this.graduationWorker.start();
6051
- if (this.analyticsStore) {
6052
- this.syncWorker = new SyncWorker(
5214
+ if (!this.embeddingOnly) {
5215
+ this.retriever.setGraduationPipeline(this.graduation);
5216
+ this.graduationWorker = createGraduationWorker(
6053
5217
  this.sqliteStore,
6054
- this.analyticsStore,
6055
- { intervalMs: 3e4, batchSize: 500 }
5218
+ this.graduation
6056
5219
  );
6057
- this.syncWorker.start();
5220
+ this.graduationWorker.start();
6058
5221
  }
6059
5222
  const savedMode = await this.sqliteStore.getEndlessConfig("mode");
6060
5223
  if (savedMode === "endless") {
@@ -6251,6 +5414,57 @@ var MemoryService = class {
6251
5414
  }
6252
5415
  );
6253
5416
  }
5417
+ /**
5418
+ * Backfill session summaries for recent sessions that are missing them.
5419
+ * Called from session-start hook to catch sessions that ended without Stop hook.
5420
+ */
5421
+ async backfillMissingSummaries(currentSessionId, limit = 5) {
5422
+ await this.initialize();
5423
+ const recentSessionIds = await this.sqliteStore.getSessionsWithoutSummary(currentSessionId, limit);
5424
+ for (const sid of recentSessionIds) {
5425
+ try {
5426
+ await this.generateSessionSummary(sid);
5427
+ } catch {
5428
+ }
5429
+ }
5430
+ }
5431
+ /**
5432
+ * Generate a rule-based session summary from stored events.
5433
+ * Called at session end (Stop hook) when no LLM-generated summary exists.
5434
+ * Skips if a summary already exists for this session.
5435
+ */
5436
+ async generateSessionSummary(sessionId) {
5437
+ await this.initialize();
5438
+ const events = await this.sqliteStore.getSessionEvents(sessionId);
5439
+ if (events.length < 3)
5440
+ return;
5441
+ const hasSummary = events.some((e) => e.eventType === "session_summary");
5442
+ if (hasSummary)
5443
+ return;
5444
+ const prompts = events.filter((e) => e.eventType === "user_prompt");
5445
+ const toolObs = events.filter((e) => e.eventType === "tool_observation");
5446
+ const toolNames = [...new Set(
5447
+ toolObs.map((e) => e.metadata?.toolName).filter(Boolean)
5448
+ )];
5449
+ const errorObs = toolObs.filter((e) => {
5450
+ const meta = e.metadata;
5451
+ return meta?.exitCode !== void 0 && meta.exitCode !== 0;
5452
+ });
5453
+ const datePart = events[0].timestamp.toISOString().split("T")[0];
5454
+ const parts = [`[${datePart}] ${prompts.length}\uD134 \uC138\uC158.`];
5455
+ if (prompts.length > 0) {
5456
+ const firstPrompt = prompts[0].content.slice(0, 120).replace(/\n/g, " ");
5457
+ parts.push(`\uC8FC\uC694 \uC791\uC5C5: ${firstPrompt}`);
5458
+ }
5459
+ if (toolNames.length > 0) {
5460
+ parts.push(`\uC0AC\uC6A9 \uD234: ${toolNames.slice(0, 6).join(", ")}`);
5461
+ }
5462
+ if (errorObs.length > 0) {
5463
+ parts.push(`\uC624\uB958 ${errorObs.length}\uAC74 \uBC1C\uC0DD`);
5464
+ }
5465
+ const summary = parts.join(". ");
5466
+ await this.storeSessionSummary(sessionId, summary, { generated: "rule-based", eventCount: events.length });
5467
+ }
6254
5468
  /**
6255
5469
  * Store a tool observation
6256
5470
  */
@@ -6778,6 +5992,20 @@ var MemoryService = class {
6778
5992
  await this.initialize();
6779
5993
  await this.sqliteStore.recordRetrieval(eventId, sessionId, score, query);
6780
5994
  }
5995
+ /**
5996
+ * Record a query-level retrieval trace (used by user-prompt-submit hook).
5997
+ * Feeds the retrieval_traces table that powers dashboard stats.
5998
+ */
5999
+ async recordQueryTrace(input) {
6000
+ await this.initialize();
6001
+ await this.sqliteStore.recordRetrievalTrace({
6002
+ ...input,
6003
+ projectHash: this.projectHash || void 0,
6004
+ candidateDetails: [],
6005
+ selectedDetails: [],
6006
+ fallbackTrace: []
6007
+ });
6008
+ }
6781
6009
  /**
6782
6010
  * Evaluate helpfulness of retrievals in a session (called at session end)
6783
6011
  */
@@ -6785,6 +6013,20 @@ var MemoryService = class {
6785
6013
  await this.initialize();
6786
6014
  await this.sqliteStore.evaluateSessionHelpfulness(sessionId);
6787
6015
  }
6016
+ /**
6017
+ * Backfill helpfulness evaluation for sessions that ended without Stop hook.
6018
+ * Call on first turn of a new session to catch missed evaluations.
6019
+ */
6020
+ async evaluatePendingSessions(currentSessionId) {
6021
+ await this.initialize();
6022
+ const sessions = await this.sqliteStore.getUnevaluatedSessions(currentSessionId, 5);
6023
+ for (const sid of sessions) {
6024
+ try {
6025
+ await this.sqliteStore.evaluateSessionHelpfulness(sid);
6026
+ } catch {
6027
+ }
6028
+ }
6029
+ }
6788
6030
  /**
6789
6031
  * Get most helpful memories ranked by helpfulness score
6790
6032
  */
@@ -7049,16 +6291,10 @@ var MemoryService = class {
7049
6291
  if (this.vectorWorker) {
7050
6292
  this.vectorWorker.stop();
7051
6293
  }
7052
- if (this.syncWorker) {
7053
- this.syncWorker.stop();
7054
- }
7055
6294
  if (this.sharedEventStore) {
7056
6295
  await this.sharedEventStore.close();
7057
6296
  }
7058
6297
  await this.sqliteStore.close();
7059
- if (this.analyticsStore) {
7060
- await this.analyticsStore.close();
7061
- }
7062
6298
  }
7063
6299
  /**
7064
6300
  * Expand ~ to home directory
@@ -7279,6 +6515,24 @@ function clearTurnState(sessionId) {
7279
6515
  }
7280
6516
  }
7281
6517
  }
6518
+ var LAST_RESPONSE_SNIPPET_CHARS = 500;
6519
+ function getLastResponsePath(sessionId) {
6520
+ return path4.join(TURN_STATE_DIR, `.last-response-${sessionId}.json`);
6521
+ }
6522
+ function writeLastAssistantSnippet(sessionId, text) {
6523
+ try {
6524
+ if (!fs5.existsSync(TURN_STATE_DIR)) {
6525
+ fs5.mkdirSync(TURN_STATE_DIR, { recursive: true });
6526
+ }
6527
+ const snippet = text.slice(0, LAST_RESPONSE_SNIPPET_CHARS);
6528
+ const state = { sessionId, snippet, createdAt: (/* @__PURE__ */ new Date()).toISOString() };
6529
+ const filePath = getLastResponsePath(sessionId);
6530
+ const tempPath = filePath + ".tmp";
6531
+ fs5.writeFileSync(tempPath, JSON.stringify(state));
6532
+ fs5.renameSync(tempPath, filePath);
6533
+ } catch {
6534
+ }
6535
+ }
7282
6536
 
7283
6537
  // src/hooks/stop.ts
7284
6538
  var DEFAULT_PRIVACY_CONFIG = {
@@ -7326,13 +6580,19 @@ async function main() {
7326
6580
  try {
7327
6581
  const turnId = readTurnState(input.session_id);
7328
6582
  const assistantMessages = await extractAssistantMessages(input.transcript_path);
7329
- for (const text of assistantMessages) {
6583
+ const MIN_AGENT_RESPONSE_LEN = parseInt(
6584
+ process.env.CLAUDE_MEMORY_AGENT_RESPONSE_MIN_LEN || "150"
6585
+ );
6586
+ const lastIdx = assistantMessages.length - 1;
6587
+ for (let i = 0; i < assistantMessages.length; i++) {
6588
+ const text = assistantMessages[i];
6589
+ const isLast = i === lastIdx;
7330
6590
  const filterResult = applyPrivacyFilter(text, DEFAULT_PRIVACY_CONFIG);
7331
6591
  let content = filterResult.content;
7332
6592
  if (content.length > 5e3) {
7333
6593
  content = content.slice(0, 5e3) + "...[truncated]";
7334
6594
  }
7335
- if (content.trim().length < 10)
6595
+ if (!isLast && content.trim().length < MIN_AGENT_RESPONSE_LEN)
7336
6596
  continue;
7337
6597
  await memoryService.storeAgentResponse(
7338
6598
  input.session_id,
@@ -7343,7 +6603,19 @@ async function main() {
7343
6603
  }
7344
6604
  );
7345
6605
  }
6606
+ if (assistantMessages.length > 0) {
6607
+ const lastMessage = assistantMessages[assistantMessages.length - 1];
6608
+ writeLastAssistantSnippet(input.session_id, lastMessage);
6609
+ }
7346
6610
  clearTurnState(input.session_id);
6611
+ try {
6612
+ await memoryService.evaluateSessionHelpfulness(input.session_id);
6613
+ } catch {
6614
+ }
6615
+ try {
6616
+ await memoryService.generateSessionSummary(input.session_id);
6617
+ } catch {
6618
+ }
7347
6619
  await memoryService.processPendingEmbeddings();
7348
6620
  console.log(JSON.stringify({}));
7349
6621
  } catch (error) {