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
@@ -7,10 +7,10 @@ const __filename = fileURLToPath(import.meta.url);
7
7
  const __dirname = dirname(__filename);
8
8
 
9
9
  // src/hooks/user-prompt-submit.ts
10
- import { randomUUID as randomUUID9 } from "crypto";
11
- import * as fs6 from "fs";
12
- import * as path5 from "path";
13
- import * as os3 from "os";
10
+ import { randomUUID as randomUUID8 } from "crypto";
11
+ import * as fs7 from "fs";
12
+ import * as path6 from "path";
13
+ import * as os4 from "os";
14
14
 
15
15
  // src/services/memory-service.ts
16
16
  import * as path3 from "path";
@@ -18,7 +18,7 @@ import * as os from "os";
18
18
  import * as fs4 from "fs";
19
19
  import * as crypto2 from "crypto";
20
20
 
21
- // src/core/event-store.ts
21
+ // src/core/sqlite-event-store.ts
22
22
  import { randomUUID } from "crypto";
23
23
 
24
24
  // src/core/canonical-key.ts
@@ -44,729 +44,16 @@ function makeDedupeKey(content, sessionId) {
44
44
  return `${sessionId}:${contentHash}`;
45
45
  }
46
46
 
47
- // src/core/db-wrapper.ts
48
- import duckdb from "duckdb";
49
- function convertBigInts(obj) {
50
- if (obj === null || obj === void 0)
51
- return obj;
52
- if (typeof obj === "bigint")
53
- return Number(obj);
54
- if (obj instanceof Date)
55
- return obj;
56
- if (Array.isArray(obj))
57
- return obj.map(convertBigInts);
58
- if (typeof obj === "object") {
59
- const result = {};
60
- for (const [key, value] of Object.entries(obj)) {
61
- result[key] = convertBigInts(value);
62
- }
63
- return result;
64
- }
65
- return obj;
66
- }
67
- function toDate(value) {
68
- if (value instanceof Date)
69
- return value;
70
- if (typeof value === "string")
71
- return new Date(value);
72
- if (typeof value === "number")
73
- return new Date(value);
74
- return new Date(String(value));
75
- }
76
- function createDatabase(path6, options) {
77
- if (options?.readOnly) {
78
- return new duckdb.Database(path6, { access_mode: "READ_ONLY" });
79
- }
80
- return new duckdb.Database(path6);
81
- }
82
- function dbRun(db, sql, params = []) {
83
- return new Promise((resolve2, reject) => {
84
- if (params.length === 0) {
85
- db.run(sql, (err) => {
86
- if (err)
87
- reject(err);
88
- else
89
- resolve2();
90
- });
91
- } else {
92
- db.run(sql, ...params, (err) => {
93
- if (err)
94
- reject(err);
95
- else
96
- resolve2();
97
- });
98
- }
99
- });
100
- }
101
- function dbAll(db, sql, params = []) {
102
- return new Promise((resolve2, reject) => {
103
- if (params.length === 0) {
104
- db.all(sql, (err, rows) => {
105
- if (err)
106
- reject(err);
107
- else
108
- resolve2(convertBigInts(rows || []));
109
- });
110
- } else {
111
- db.all(sql, ...params, (err, rows) => {
112
- if (err)
113
- reject(err);
114
- else
115
- resolve2(convertBigInts(rows || []));
116
- });
117
- }
118
- });
119
- }
120
- function dbClose(db) {
121
- return new Promise((resolve2, reject) => {
122
- db.close((err) => {
123
- if (err)
124
- reject(err);
125
- else
126
- resolve2();
127
- });
128
- });
129
- }
130
-
131
- // src/core/event-store.ts
132
- var EventStore = class {
133
- constructor(dbPath, options) {
134
- this.dbPath = dbPath;
135
- this.readOnly = options?.readOnly ?? false;
136
- this.db = createDatabase(dbPath, { readOnly: this.readOnly });
137
- }
138
- db;
139
- initialized = false;
140
- readOnly;
141
- /**
142
- * Initialize database schema
143
- */
144
- async initialize() {
145
- if (this.initialized)
146
- return;
147
- if (this.readOnly) {
148
- this.initialized = true;
149
- return;
150
- }
151
- await dbRun(this.db, `
152
- CREATE TABLE IF NOT EXISTS events (
153
- id VARCHAR PRIMARY KEY,
154
- event_type VARCHAR NOT NULL,
155
- session_id VARCHAR NOT NULL,
156
- timestamp TIMESTAMP NOT NULL,
157
- content TEXT NOT NULL,
158
- canonical_key VARCHAR NOT NULL,
159
- dedupe_key VARCHAR UNIQUE,
160
- metadata JSON
161
- )
162
- `);
163
- await dbRun(this.db, `
164
- CREATE TABLE IF NOT EXISTS event_dedup (
165
- dedupe_key VARCHAR PRIMARY KEY,
166
- event_id VARCHAR NOT NULL,
167
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
168
- )
169
- `);
170
- await dbRun(this.db, `
171
- CREATE TABLE IF NOT EXISTS sessions (
172
- id VARCHAR PRIMARY KEY,
173
- started_at TIMESTAMP NOT NULL,
174
- ended_at TIMESTAMP,
175
- project_path VARCHAR,
176
- summary TEXT,
177
- tags JSON
178
- )
179
- `);
180
- await dbRun(this.db, `
181
- CREATE TABLE IF NOT EXISTS insights (
182
- id VARCHAR PRIMARY KEY,
183
- insight_type VARCHAR NOT NULL,
184
- content TEXT NOT NULL,
185
- canonical_key VARCHAR NOT NULL,
186
- confidence FLOAT,
187
- source_events JSON,
188
- created_at TIMESTAMP,
189
- last_updated TIMESTAMP
190
- )
191
- `);
192
- await dbRun(this.db, `
193
- CREATE TABLE IF NOT EXISTS embedding_outbox (
194
- id VARCHAR PRIMARY KEY,
195
- event_id VARCHAR NOT NULL,
196
- content TEXT NOT NULL,
197
- status VARCHAR DEFAULT 'pending',
198
- retry_count INT DEFAULT 0,
199
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
200
- processed_at TIMESTAMP,
201
- error_message TEXT
202
- )
203
- `);
204
- await dbRun(this.db, `
205
- CREATE TABLE IF NOT EXISTS projection_offsets (
206
- projection_name VARCHAR PRIMARY KEY,
207
- last_event_id VARCHAR,
208
- last_timestamp TIMESTAMP,
209
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
210
- )
211
- `);
212
- await dbRun(this.db, `
213
- CREATE TABLE IF NOT EXISTS memory_levels (
214
- event_id VARCHAR PRIMARY KEY,
215
- level VARCHAR NOT NULL DEFAULT 'L0',
216
- promoted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
217
- )
218
- `);
219
- await dbRun(this.db, `
220
- CREATE TABLE IF NOT EXISTS entries (
221
- entry_id VARCHAR PRIMARY KEY,
222
- created_ts TIMESTAMP NOT NULL,
223
- entry_type VARCHAR NOT NULL,
224
- title VARCHAR NOT NULL,
225
- content_json JSON NOT NULL,
226
- stage VARCHAR NOT NULL DEFAULT 'raw',
227
- status VARCHAR DEFAULT 'active',
228
- superseded_by VARCHAR,
229
- build_id VARCHAR,
230
- evidence_json JSON,
231
- canonical_key VARCHAR,
232
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
233
- )
234
- `);
235
- await dbRun(this.db, `
236
- CREATE TABLE IF NOT EXISTS entities (
237
- entity_id VARCHAR PRIMARY KEY,
238
- entity_type VARCHAR NOT NULL,
239
- canonical_key VARCHAR NOT NULL,
240
- title VARCHAR NOT NULL,
241
- stage VARCHAR NOT NULL DEFAULT 'raw',
242
- status VARCHAR NOT NULL DEFAULT 'active',
243
- current_json JSON NOT NULL,
244
- title_norm VARCHAR,
245
- search_text VARCHAR,
246
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
247
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
248
- )
249
- `);
250
- await dbRun(this.db, `
251
- CREATE TABLE IF NOT EXISTS entity_aliases (
252
- entity_type VARCHAR NOT NULL,
253
- canonical_key VARCHAR NOT NULL,
254
- entity_id VARCHAR NOT NULL,
255
- is_primary BOOLEAN DEFAULT FALSE,
256
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
257
- PRIMARY KEY(entity_type, canonical_key)
258
- )
259
- `);
260
- await dbRun(this.db, `
261
- CREATE TABLE IF NOT EXISTS edges (
262
- edge_id VARCHAR PRIMARY KEY,
263
- src_type VARCHAR NOT NULL,
264
- src_id VARCHAR NOT NULL,
265
- rel_type VARCHAR NOT NULL,
266
- dst_type VARCHAR NOT NULL,
267
- dst_id VARCHAR NOT NULL,
268
- meta_json JSON,
269
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
270
- )
271
- `);
272
- await dbRun(this.db, `
273
- CREATE TABLE IF NOT EXISTS vector_outbox (
274
- job_id VARCHAR PRIMARY KEY,
275
- item_kind VARCHAR NOT NULL,
276
- item_id VARCHAR NOT NULL,
277
- embedding_version VARCHAR NOT NULL,
278
- status VARCHAR NOT NULL DEFAULT 'pending',
279
- retry_count INT DEFAULT 0,
280
- error VARCHAR,
281
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
282
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
283
- UNIQUE(item_kind, item_id, embedding_version)
284
- )
285
- `);
286
- await dbRun(this.db, `
287
- CREATE TABLE IF NOT EXISTS build_runs (
288
- build_id VARCHAR PRIMARY KEY,
289
- started_at TIMESTAMP NOT NULL,
290
- finished_at TIMESTAMP,
291
- extractor_model VARCHAR NOT NULL,
292
- extractor_prompt_hash VARCHAR NOT NULL,
293
- embedder_model VARCHAR NOT NULL,
294
- embedding_version VARCHAR NOT NULL,
295
- idris_version VARCHAR NOT NULL,
296
- schema_version VARCHAR NOT NULL,
297
- status VARCHAR NOT NULL DEFAULT 'running',
298
- error VARCHAR
299
- )
300
- `);
301
- await dbRun(this.db, `
302
- CREATE TABLE IF NOT EXISTS pipeline_metrics (
303
- id VARCHAR PRIMARY KEY,
304
- ts TIMESTAMP NOT NULL,
305
- stage VARCHAR NOT NULL,
306
- latency_ms DOUBLE NOT NULL,
307
- success BOOLEAN NOT NULL,
308
- error VARCHAR,
309
- session_id VARCHAR
310
- )
311
- `);
312
- await dbRun(this.db, `
313
- CREATE TABLE IF NOT EXISTS working_set (
314
- id VARCHAR PRIMARY KEY,
315
- event_id VARCHAR NOT NULL,
316
- added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
317
- relevance_score FLOAT DEFAULT 1.0,
318
- topics JSON,
319
- expires_at TIMESTAMP
320
- )
321
- `);
322
- await dbRun(this.db, `
323
- CREATE TABLE IF NOT EXISTS consolidated_memories (
324
- memory_id VARCHAR PRIMARY KEY,
325
- summary TEXT NOT NULL,
326
- topics JSON,
327
- source_events JSON,
328
- confidence FLOAT DEFAULT 0.5,
329
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
330
- accessed_at TIMESTAMP,
331
- access_count INTEGER DEFAULT 0
332
- )
333
- `);
334
- await dbRun(this.db, `
335
- CREATE TABLE IF NOT EXISTS continuity_log (
336
- log_id VARCHAR PRIMARY KEY,
337
- from_context_id VARCHAR,
338
- to_context_id VARCHAR,
339
- continuity_score FLOAT,
340
- transition_type VARCHAR,
341
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
342
- )
343
- `);
344
- await dbRun(this.db, `
345
- CREATE TABLE IF NOT EXISTS consolidated_rules (
346
- rule_id VARCHAR PRIMARY KEY,
347
- rule TEXT NOT NULL,
348
- topics JSON,
349
- source_memory_ids JSON,
350
- source_events JSON,
351
- confidence FLOAT DEFAULT 0.5,
352
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
353
- )
354
- `);
355
- await dbRun(this.db, `
356
- CREATE TABLE IF NOT EXISTS endless_config (
357
- key VARCHAR PRIMARY KEY,
358
- value JSON,
359
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
360
- )
361
- `);
362
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type)`);
363
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_stage ON entries(stage)`);
364
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_canonical ON entries(canonical_key)`);
365
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_type_key ON entities(entity_type, canonical_key)`);
366
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_status ON entities(status)`);
367
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_src ON edges(src_id, rel_type)`);
368
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_dst ON edges(dst_id, rel_type)`);
369
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_rel ON edges(rel_type)`);
370
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_outbox_status ON vector_outbox(status)`);
371
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
372
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
373
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
374
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
375
- await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
376
- this.initialized = true;
377
- }
378
- /**
379
- * Append event to store (AXIOMMIND Principle 2: Append-only)
380
- * Returns existing event ID if duplicate (Principle 3: Idempotency)
381
- */
382
- async append(input) {
383
- await this.initialize();
384
- const canonicalKey = makeCanonicalKey(input.content);
385
- const dedupeKey = makeDedupeKey(input.content, input.sessionId);
386
- const existing = await dbAll(
387
- this.db,
388
- `SELECT event_id FROM event_dedup WHERE dedupe_key = ?`,
389
- [dedupeKey]
390
- );
391
- if (existing.length > 0) {
392
- return {
393
- success: true,
394
- eventId: existing[0].event_id,
395
- isDuplicate: true
396
- };
397
- }
398
- const id = randomUUID();
399
- const timestamp = input.timestamp.toISOString();
400
- try {
401
- await dbRun(
402
- this.db,
403
- `INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
404
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
405
- [
406
- id,
407
- input.eventType,
408
- input.sessionId,
409
- timestamp,
410
- input.content,
411
- canonicalKey,
412
- dedupeKey,
413
- JSON.stringify(input.metadata || {})
414
- ]
415
- );
416
- await dbRun(
417
- this.db,
418
- `INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)`,
419
- [dedupeKey, id]
420
- );
421
- await dbRun(
422
- this.db,
423
- `INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')`,
424
- [id]
425
- );
426
- return { success: true, eventId: id, isDuplicate: false };
427
- } catch (error) {
428
- return {
429
- success: false,
430
- error: error instanceof Error ? error.message : String(error)
431
- };
432
- }
433
- }
434
- /**
435
- * Get events by session ID
436
- */
437
- async getSessionEvents(sessionId) {
438
- await this.initialize();
439
- const rows = await dbAll(
440
- this.db,
441
- `SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
442
- [sessionId]
443
- );
444
- return rows.map(this.rowToEvent);
445
- }
446
- /**
447
- * Get recent events
448
- */
449
- async getRecentEvents(limit = 100) {
450
- await this.initialize();
451
- const rows = await dbAll(
452
- this.db,
453
- `SELECT * FROM events ORDER BY timestamp DESC LIMIT ?`,
454
- [limit]
455
- );
456
- return rows.map(this.rowToEvent);
457
- }
458
- /**
459
- * Get event by ID
460
- */
461
- async getEvent(id) {
462
- await this.initialize();
463
- const rows = await dbAll(
464
- this.db,
465
- `SELECT * FROM events WHERE id = ?`,
466
- [id]
467
- );
468
- if (rows.length === 0)
469
- return null;
470
- return this.rowToEvent(rows[0]);
471
- }
472
- /**
473
- * Create or update session
474
- */
475
- async upsertSession(session) {
476
- await this.initialize();
477
- const existing = await dbAll(
478
- this.db,
479
- `SELECT id FROM sessions WHERE id = ?`,
480
- [session.id]
481
- );
482
- if (existing.length === 0) {
483
- await dbRun(
484
- this.db,
485
- `INSERT INTO sessions (id, started_at, project_path, tags)
486
- VALUES (?, ?, ?, ?)`,
487
- [
488
- session.id,
489
- (session.startedAt || /* @__PURE__ */ new Date()).toISOString(),
490
- session.projectPath || null,
491
- JSON.stringify(session.tags || [])
492
- ]
493
- );
494
- } else {
495
- const updates = [];
496
- const values = [];
497
- if (session.endedAt) {
498
- updates.push("ended_at = ?");
499
- values.push(session.endedAt.toISOString());
500
- }
501
- if (session.summary) {
502
- updates.push("summary = ?");
503
- values.push(session.summary);
504
- }
505
- if (session.tags) {
506
- updates.push("tags = ?");
507
- values.push(JSON.stringify(session.tags));
508
- }
509
- if (updates.length > 0) {
510
- values.push(session.id);
511
- await dbRun(
512
- this.db,
513
- `UPDATE sessions SET ${updates.join(", ")} WHERE id = ?`,
514
- values
515
- );
516
- }
517
- }
518
- }
519
- /**
520
- * Get session by ID
521
- */
522
- async getSession(id) {
523
- await this.initialize();
524
- const rows = await dbAll(
525
- this.db,
526
- `SELECT * FROM sessions WHERE id = ?`,
527
- [id]
528
- );
529
- if (rows.length === 0)
530
- return null;
531
- const row = rows[0];
532
- return {
533
- id: row.id,
534
- startedAt: toDate(row.started_at),
535
- endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
536
- projectPath: row.project_path,
537
- summary: row.summary,
538
- tags: row.tags ? JSON.parse(row.tags) : void 0
539
- };
540
- }
541
- /**
542
- * Add to embedding outbox (Single-Writer Pattern)
543
- */
544
- async enqueueForEmbedding(eventId, content) {
545
- await this.initialize();
546
- const id = randomUUID();
547
- await dbRun(
548
- this.db,
549
- `INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
550
- VALUES (?, ?, ?, 'pending', 0)`,
551
- [id, eventId, content]
552
- );
553
- return id;
554
- }
555
- /**
556
- * Get pending outbox items
557
- */
558
- async getPendingOutboxItems(limit = 32) {
559
- await this.initialize();
560
- const pending = await dbAll(
561
- this.db,
562
- `SELECT * FROM embedding_outbox
563
- WHERE status = 'pending'
564
- ORDER BY created_at
565
- LIMIT ?`,
566
- [limit]
567
- );
568
- if (pending.length === 0)
569
- return [];
570
- const ids = pending.map((r) => r.id);
571
- const placeholders = ids.map(() => "?").join(",");
572
- await dbRun(
573
- this.db,
574
- `UPDATE embedding_outbox SET status = 'processing' WHERE id IN (${placeholders})`,
575
- ids
576
- );
577
- return pending.map((row) => ({
578
- id: row.id,
579
- eventId: row.event_id,
580
- content: row.content,
581
- status: "processing",
582
- retryCount: row.retry_count,
583
- createdAt: toDate(row.created_at),
584
- errorMessage: row.error_message
585
- }));
586
- }
587
- /**
588
- * Mark outbox items as done
589
- */
590
- async completeOutboxItems(ids) {
591
- if (ids.length === 0)
592
- return;
593
- const placeholders = ids.map(() => "?").join(",");
594
- await dbRun(
595
- this.db,
596
- `DELETE FROM embedding_outbox WHERE id IN (${placeholders})`,
597
- ids
598
- );
599
- }
600
- /**
601
- * Mark outbox items as failed
602
- */
603
- async failOutboxItems(ids, error) {
604
- if (ids.length === 0)
605
- return;
606
- const placeholders = ids.map(() => "?").join(",");
607
- await dbRun(
608
- this.db,
609
- `UPDATE embedding_outbox
610
- SET status = CASE WHEN retry_count >= 3 THEN 'failed' ELSE 'pending' END,
611
- retry_count = retry_count + 1,
612
- error_message = ?
613
- WHERE id IN (${placeholders})`,
614
- [error, ...ids]
615
- );
616
- }
617
- /**
618
- * Update memory level
619
- */
620
- async updateMemoryLevel(eventId, level) {
621
- await this.initialize();
622
- await dbRun(
623
- this.db,
624
- `UPDATE memory_levels SET level = ?, promoted_at = CURRENT_TIMESTAMP WHERE event_id = ?`,
625
- [level, eventId]
626
- );
627
- }
628
- /**
629
- * Get memory level statistics
630
- */
631
- async getLevelStats() {
632
- await this.initialize();
633
- const rows = await dbAll(
634
- this.db,
635
- `SELECT level, COUNT(*) as count FROM memory_levels GROUP BY level`
636
- );
637
- return rows;
638
- }
639
- /**
640
- * Get events by memory level
641
- */
642
- async getEventsByLevel(level, options) {
643
- await this.initialize();
644
- const limit = options?.limit || 50;
645
- const offset = options?.offset || 0;
646
- const rows = await dbAll(
647
- this.db,
648
- `SELECT e.* FROM events e
649
- INNER JOIN memory_levels ml ON e.id = ml.event_id
650
- WHERE ml.level = ?
651
- ORDER BY e.timestamp DESC
652
- LIMIT ? OFFSET ?`,
653
- [level, limit, offset]
654
- );
655
- return rows.map((row) => this.rowToEvent(row));
656
- }
657
- /**
658
- * Get memory level for a specific event
659
- */
660
- async getEventLevel(eventId) {
661
- await this.initialize();
662
- const rows = await dbAll(
663
- this.db,
664
- `SELECT level FROM memory_levels WHERE event_id = ?`,
665
- [eventId]
666
- );
667
- return rows.length > 0 ? rows[0].level : null;
668
- }
669
- // ============================================================
670
- // Endless Mode Helper Methods
671
- // ============================================================
672
- /**
673
- * Get database instance for Endless Mode stores
674
- */
675
- getDatabase() {
676
- return this.db;
677
- }
678
- /**
679
- * Get config value for endless mode
680
- */
681
- async getEndlessConfig(key) {
682
- await this.initialize();
683
- const rows = await dbAll(
684
- this.db,
685
- `SELECT value FROM endless_config WHERE key = ?`,
686
- [key]
687
- );
688
- if (rows.length === 0)
689
- return null;
690
- return JSON.parse(rows[0].value);
691
- }
692
- /**
693
- * Set config value for endless mode
694
- */
695
- async setEndlessConfig(key, value) {
696
- await this.initialize();
697
- await dbRun(
698
- this.db,
699
- `INSERT OR REPLACE INTO endless_config (key, value, updated_at)
700
- VALUES (?, ?, CURRENT_TIMESTAMP)`,
701
- [key, JSON.stringify(value)]
702
- );
703
- }
704
- /**
705
- * Get all sessions
706
- */
707
- async getAllSessions() {
708
- await this.initialize();
709
- const rows = await dbAll(
710
- this.db,
711
- `SELECT * FROM sessions ORDER BY started_at DESC`
712
- );
713
- return rows.map((row) => ({
714
- id: row.id,
715
- startedAt: toDate(row.started_at),
716
- endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
717
- projectPath: row.project_path,
718
- summary: row.summary,
719
- tags: row.tags ? JSON.parse(row.tags) : void 0
720
- }));
721
- }
722
- /**
723
- * Increment access count for events (stub for compatibility)
724
- */
725
- async incrementAccessCount(eventIds) {
726
- return Promise.resolve();
727
- }
728
- /**
729
- * Get most accessed memories (stub for compatibility)
730
- */
731
- async getMostAccessed(limit = 10) {
732
- return [];
733
- }
734
- /**
735
- * Close database connection
736
- */
737
- async close() {
738
- await dbClose(this.db);
739
- }
740
- /**
741
- * Convert database row to MemoryEvent
742
- */
743
- rowToEvent(row) {
744
- return {
745
- id: row.id,
746
- eventType: row.event_type,
747
- sessionId: row.session_id,
748
- timestamp: toDate(row.timestamp),
749
- content: row.content,
750
- canonicalKey: row.canonical_key,
751
- dedupeKey: row.dedupe_key,
752
- metadata: row.metadata ? JSON.parse(row.metadata) : void 0
753
- };
754
- }
755
- };
756
-
757
- // src/core/sqlite-event-store.ts
758
- import { randomUUID as randomUUID2 } from "crypto";
759
-
760
47
  // src/core/sqlite-wrapper.ts
761
48
  import Database from "better-sqlite3";
762
49
  import * as fs from "fs";
763
50
  import * as nodePath from "path";
764
- function createSQLiteDatabase(path6, options) {
765
- const dir = nodePath.dirname(path6);
51
+ function createSQLiteDatabase(path7, options) {
52
+ const dir = nodePath.dirname(path7);
766
53
  if (!fs.existsSync(dir)) {
767
54
  fs.mkdirSync(dir, { recursive: true });
768
55
  }
769
- const db = new Database(path6, {
56
+ const db = new Database(path7, {
770
57
  readonly: options?.readonly ?? false
771
58
  });
772
59
  if (!options?.readonly && (options?.walMode ?? true)) {
@@ -1270,7 +557,7 @@ var SQLiteEventStore = class {
1270
557
  isDuplicate: true
1271
558
  };
1272
559
  }
1273
- const id = randomUUID2();
560
+ const id = randomUUID();
1274
561
  const timestamp = toSQLiteTimestamp(input.timestamp);
1275
562
  try {
1276
563
  const metadata = input.metadata || {};
@@ -1324,6 +611,29 @@ var SQLiteEventStore = class {
1324
611
  };
1325
612
  }
1326
613
  }
614
+ /**
615
+ * Get session IDs that have events but no session_summary event.
616
+ * Used to backfill summaries for sessions that ended without Stop hook.
617
+ */
618
+ async getSessionsWithoutSummary(currentSessionId, limit = 5) {
619
+ await this.initialize();
620
+ const rows = sqliteAll(
621
+ this.db,
622
+ `SELECT DISTINCT e.session_id
623
+ FROM events e
624
+ WHERE e.session_id != ?
625
+ AND e.event_type != 'session_summary'
626
+ AND e.session_id NOT IN (
627
+ SELECT DISTINCT session_id FROM events WHERE event_type = 'session_summary'
628
+ )
629
+ GROUP BY e.session_id
630
+ HAVING COUNT(*) >= 3
631
+ ORDER BY MAX(e.timestamp) DESC
632
+ LIMIT ?`,
633
+ [currentSessionId, limit]
634
+ );
635
+ return rows.map((r) => r.session_id);
636
+ }
1327
637
  /**
1328
638
  * Get events by session ID
1329
639
  */
@@ -1551,7 +861,7 @@ var SQLiteEventStore = class {
1551
861
  */
1552
862
  async enqueueForEmbedding(eventId, content) {
1553
863
  await this.initialize();
1554
- const id = randomUUID2();
864
+ const id = randomUUID();
1555
865
  sqliteRun(
1556
866
  this.db,
1557
867
  `INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
@@ -1832,7 +1142,7 @@ var SQLiteEventStore = class {
1832
1142
  if (this.readOnly)
1833
1143
  return;
1834
1144
  await this.initialize();
1835
- const id = randomUUID2();
1145
+ const id = randomUUID();
1836
1146
  sqliteRun(
1837
1147
  this.db,
1838
1148
  `INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
@@ -1840,6 +1150,21 @@ var SQLiteEventStore = class {
1840
1150
  [id, eventId, sessionId, score, query.slice(0, 100)]
1841
1151
  );
1842
1152
  }
1153
+ /**
1154
+ * Get session IDs that have unevaluated retrievals (measured_at IS NULL).
1155
+ * Excludes the current session. Used to backfill sessions that ended without Stop hook.
1156
+ */
1157
+ async getUnevaluatedSessions(currentSessionId, limit = 5) {
1158
+ await this.initialize();
1159
+ const rows = sqliteAll(
1160
+ this.db,
1161
+ `SELECT DISTINCT session_id FROM memory_helpfulness
1162
+ WHERE measured_at IS NULL AND session_id != ?
1163
+ ORDER BY created_at DESC LIMIT ?`,
1164
+ [currentSessionId, limit]
1165
+ );
1166
+ return rows.map((r) => r.session_id);
1167
+ }
1843
1168
  /**
1844
1169
  * Evaluate helpfulness for all retrievals in a session
1845
1170
  * Called at session end - uses behavioral signals to compute score
@@ -2038,7 +1363,7 @@ var SQLiteEventStore = class {
2038
1363
  }
2039
1364
  async recordRetrievalTrace(input) {
2040
1365
  await this.initialize();
2041
- const traceId = randomUUID2();
1366
+ const traceId = randomUUID();
2042
1367
  sqliteRun(
2043
1368
  this.db,
2044
1369
  `INSERT INTO retrieval_traces (
@@ -2292,169 +1617,6 @@ var SQLiteEventStore = class {
2292
1617
  }
2293
1618
  };
2294
1619
 
2295
- // src/core/sync-worker.ts
2296
- var DEFAULT_CONFIG = {
2297
- intervalMs: 3e4,
2298
- batchSize: 500,
2299
- maxRetries: 3,
2300
- retryDelayMs: 5e3
2301
- };
2302
- var SyncWorker = class {
2303
- constructor(sqliteStore, duckdbStore, config) {
2304
- this.sqliteStore = sqliteStore;
2305
- this.duckdbStore = duckdbStore;
2306
- this.config = { ...DEFAULT_CONFIG, ...config };
2307
- }
2308
- config;
2309
- intervalHandle = null;
2310
- running = false;
2311
- stats = {
2312
- lastSyncAt: null,
2313
- eventsSynced: 0,
2314
- sessionsSynced: 0,
2315
- errors: 0,
2316
- status: "idle"
2317
- };
2318
- /**
2319
- * Start the sync worker
2320
- */
2321
- start() {
2322
- if (this.running)
2323
- return;
2324
- this.running = true;
2325
- this.stats.status = "idle";
2326
- this.syncNow().catch((err) => {
2327
- console.error("[SyncWorker] Initial sync failed:", err);
2328
- });
2329
- this.intervalHandle = setInterval(() => {
2330
- this.syncNow().catch((err) => {
2331
- console.error("[SyncWorker] Periodic sync failed:", err);
2332
- });
2333
- }, this.config.intervalMs);
2334
- }
2335
- /**
2336
- * Stop the sync worker
2337
- */
2338
- stop() {
2339
- this.running = false;
2340
- this.stats.status = "stopped";
2341
- if (this.intervalHandle) {
2342
- clearInterval(this.intervalHandle);
2343
- this.intervalHandle = null;
2344
- }
2345
- }
2346
- /**
2347
- * Trigger immediate sync
2348
- */
2349
- async syncNow() {
2350
- if (this.stats.status === "syncing") {
2351
- return;
2352
- }
2353
- this.stats.status = "syncing";
2354
- try {
2355
- await this.syncEvents();
2356
- await this.syncSessions();
2357
- this.stats.lastSyncAt = /* @__PURE__ */ new Date();
2358
- this.stats.status = "idle";
2359
- } catch (error) {
2360
- this.stats.errors++;
2361
- this.stats.status = "error";
2362
- throw error;
2363
- }
2364
- }
2365
- /**
2366
- * Sync events from SQLite to DuckDB
2367
- */
2368
- async syncEvents() {
2369
- const targetName = "duckdb_analytics";
2370
- const position = await this.sqliteStore.getSyncPosition(targetName);
2371
- const lastTimestamp = position.lastTimestamp || "1970-01-01T00:00:00.000Z";
2372
- let hasMore = true;
2373
- let totalSynced = 0;
2374
- while (hasMore) {
2375
- const events = await this.sqliteStore.getEventsSince(lastTimestamp, this.config.batchSize);
2376
- if (events.length === 0) {
2377
- hasMore = false;
2378
- break;
2379
- }
2380
- await this.retryWithBackoff(async () => {
2381
- for (const event of events) {
2382
- await this.insertEventToDuckDB(event);
2383
- }
2384
- });
2385
- totalSynced += events.length;
2386
- const lastEvent = events[events.length - 1];
2387
- await this.sqliteStore.updateSyncPosition(
2388
- targetName,
2389
- lastEvent.id,
2390
- lastEvent.timestamp.toISOString()
2391
- );
2392
- hasMore = events.length === this.config.batchSize;
2393
- }
2394
- this.stats.eventsSynced += totalSynced;
2395
- }
2396
- /**
2397
- * Sync sessions from SQLite to DuckDB
2398
- */
2399
- async syncSessions() {
2400
- const sessions = await this.sqliteStore.getAllSessions();
2401
- for (const session of sessions) {
2402
- await this.retryWithBackoff(async () => {
2403
- await this.duckdbStore.upsertSession(session);
2404
- });
2405
- }
2406
- this.stats.sessionsSynced = sessions.length;
2407
- }
2408
- /**
2409
- * Insert a single event into DuckDB
2410
- */
2411
- async insertEventToDuckDB(event) {
2412
- await this.duckdbStore.append({
2413
- eventType: event.eventType,
2414
- sessionId: event.sessionId,
2415
- timestamp: event.timestamp,
2416
- content: event.content,
2417
- metadata: event.metadata
2418
- });
2419
- }
2420
- /**
2421
- * Retry operation with exponential backoff
2422
- */
2423
- async retryWithBackoff(fn) {
2424
- let lastError = null;
2425
- for (let attempt = 0; attempt < this.config.maxRetries; attempt++) {
2426
- try {
2427
- return await fn();
2428
- } catch (error) {
2429
- lastError = error instanceof Error ? error : new Error(String(error));
2430
- if (attempt < this.config.maxRetries - 1) {
2431
- const delay = this.config.retryDelayMs * Math.pow(2, attempt);
2432
- await this.sleep(delay);
2433
- }
2434
- }
2435
- }
2436
- throw lastError;
2437
- }
2438
- /**
2439
- * Sleep utility
2440
- */
2441
- sleep(ms) {
2442
- return new Promise((resolve2) => setTimeout(resolve2, ms));
2443
- }
2444
- /**
2445
- * Get sync statistics
2446
- */
2447
- getStats() {
2448
- return { ...this.stats };
2449
- }
2450
- /**
2451
- * Check if worker is running
2452
- */
2453
- isRunning() {
2454
- return this.running;
2455
- }
2456
- };
2457
-
2458
1620
  // src/core/vector-store.ts
2459
1621
  import * as lancedb from "@lancedb/lancedb";
2460
1622
  var VectorStore = class {
@@ -2627,7 +1789,7 @@ var VectorStore = class {
2627
1789
 
2628
1790
  // src/core/embedder.ts
2629
1791
  import { pipeline } from "@huggingface/transformers";
2630
- var Embedder = class {
1792
+ var Embedder = class _Embedder {
2631
1793
  pipeline = null;
2632
1794
  modelName;
2633
1795
  activeModelName;
@@ -2658,6 +1820,11 @@ var Embedder = class {
2658
1820
  this.initialized = true;
2659
1821
  }
2660
1822
  }
1823
+ // ~4 chars per token; 512 tokens * 4 = 2048, use 2000 to be safe
1824
+ static MAX_CHARS = 2e3;
1825
+ truncate(text) {
1826
+ return text.length > _Embedder.MAX_CHARS ? text.slice(0, _Embedder.MAX_CHARS) : text;
1827
+ }
2661
1828
  /**
2662
1829
  * Generate embedding for a single text
2663
1830
  */
@@ -2666,10 +1833,11 @@ var Embedder = class {
2666
1833
  if (!this.pipeline) {
2667
1834
  throw new Error("Embedding pipeline not initialized");
2668
1835
  }
2669
- const output = await this.pipeline(text, {
1836
+ const output = await this.pipeline(this.truncate(text), {
2670
1837
  pooling: "mean",
2671
1838
  normalize: true,
2672
- truncation: true
1839
+ truncation: true,
1840
+ max_length: 512
2673
1841
  });
2674
1842
  const vector = Array.from(output.data);
2675
1843
  return {
@@ -2691,10 +1859,11 @@ var Embedder = class {
2691
1859
  for (let i = 0; i < texts.length; i += batchSize) {
2692
1860
  const batch = texts.slice(i, i + batchSize);
2693
1861
  for (const text of batch) {
2694
- const output = await this.pipeline(text, {
1862
+ const output = await this.pipeline(this.truncate(text), {
2695
1863
  pooling: "mean",
2696
1864
  normalize: true,
2697
- truncation: true
1865
+ truncation: true,
1866
+ max_length: 512
2698
1867
  });
2699
1868
  const vector = Array.from(output.data);
2700
1869
  results.push({
@@ -2735,8 +1904,34 @@ function getDefaultEmbedder() {
2735
1904
  return defaultEmbedder;
2736
1905
  }
2737
1906
 
1907
+ // src/core/db-wrapper.ts
1908
+ import BetterSqlite3 from "better-sqlite3";
1909
+ function toDate(value) {
1910
+ if (value instanceof Date)
1911
+ return value;
1912
+ if (typeof value === "string")
1913
+ return new Date(value);
1914
+ if (typeof value === "number")
1915
+ return new Date(value);
1916
+ return new Date(String(value));
1917
+ }
1918
+ function createDatabase(dbPath, options) {
1919
+ return new BetterSqlite3(dbPath, { readonly: options?.readOnly });
1920
+ }
1921
+ function dbRun(db, sql, params = []) {
1922
+ db.prepare(sql).run(...params);
1923
+ return Promise.resolve();
1924
+ }
1925
+ function dbAll(db, sql, params = []) {
1926
+ return Promise.resolve(db.prepare(sql).all(...params));
1927
+ }
1928
+ function dbClose(db) {
1929
+ db.close();
1930
+ return Promise.resolve();
1931
+ }
1932
+
2738
1933
  // src/core/vector-outbox.ts
2739
- var DEFAULT_CONFIG2 = {
1934
+ var DEFAULT_CONFIG = {
2740
1935
  embeddingVersion: "v1",
2741
1936
  maxRetries: 3,
2742
1937
  stuckThresholdMs: 5 * 60 * 1e3,
@@ -2745,7 +1940,7 @@ var DEFAULT_CONFIG2 = {
2745
1940
  };
2746
1941
 
2747
1942
  // src/core/vector-worker.ts
2748
- var DEFAULT_CONFIG3 = {
1943
+ var DEFAULT_CONFIG2 = {
2749
1944
  batchSize: 32,
2750
1945
  pollIntervalMs: 1e3,
2751
1946
  maxRetries: 3
@@ -2762,7 +1957,7 @@ var VectorWorker = class {
2762
1957
  this.eventStore = eventStore;
2763
1958
  this.vectorStore = vectorStore;
2764
1959
  this.embedder = embedder;
2765
- this.config = { ...DEFAULT_CONFIG3, ...config };
1960
+ this.config = { ...DEFAULT_CONFIG2, ...config };
2766
1961
  }
2767
1962
  /**
2768
1963
  * Start the worker polling loop
@@ -2883,7 +2078,7 @@ function createVectorWorker(eventStore, vectorStore, embedder, config) {
2883
2078
  }
2884
2079
 
2885
2080
  // src/core/matcher.ts
2886
- var DEFAULT_CONFIG4 = {
2081
+ var DEFAULT_CONFIG3 = {
2887
2082
  weights: {
2888
2083
  semanticSimilarity: 0.4,
2889
2084
  ftsScore: 0.25,
@@ -2898,9 +2093,9 @@ var Matcher = class {
2898
2093
  config;
2899
2094
  constructor(config = {}) {
2900
2095
  this.config = {
2901
- ...DEFAULT_CONFIG4,
2096
+ ...DEFAULT_CONFIG3,
2902
2097
  ...config,
2903
- weights: { ...DEFAULT_CONFIG4.weights, ...config.weights }
2098
+ weights: { ...DEFAULT_CONFIG3.weights, ...config.weights }
2904
2099
  };
2905
2100
  }
2906
2101
  /**
@@ -3504,8 +2699,8 @@ _Context:_ ${sessionContext}`;
3504
2699
  matchesMetadataScope(metadata, expected) {
3505
2700
  if (!metadata)
3506
2701
  return false;
3507
- return Object.entries(expected).every(([path6, value]) => {
3508
- const actual = path6.split(".").reduce((acc, key) => {
2702
+ return Object.entries(expected).every(([path7, value]) => {
2703
+ const actual = path7.split(".").reduce((acc, key) => {
3509
2704
  if (typeof acc !== "object" || acc === null)
3510
2705
  return void 0;
3511
2706
  return acc[key];
@@ -3890,7 +3085,7 @@ function createSharedEventStore(dbPath) {
3890
3085
  }
3891
3086
 
3892
3087
  // src/core/shared-store.ts
3893
- import { randomUUID as randomUUID3 } from "crypto";
3088
+ import { randomUUID as randomUUID2 } from "crypto";
3894
3089
  var SharedStore = class {
3895
3090
  constructor(sharedEventStore) {
3896
3091
  this.sharedEventStore = sharedEventStore;
@@ -3902,7 +3097,7 @@ var SharedStore = class {
3902
3097
  * Promote a verified troubleshooting entry to shared storage
3903
3098
  */
3904
3099
  async promoteEntry(input) {
3905
- const entryId = randomUUID3();
3100
+ const entryId = randomUUID2();
3906
3101
  await dbRun(
3907
3102
  this.db,
3908
3103
  `INSERT INTO shared_troubleshooting (
@@ -4267,7 +3462,7 @@ function createSharedVectorStore(dbPath) {
4267
3462
  }
4268
3463
 
4269
3464
  // src/core/shared-promoter.ts
4270
- import { randomUUID as randomUUID4 } from "crypto";
3465
+ import { randomUUID as randomUUID3 } from "crypto";
4271
3466
  var SharedPromoter = class {
4272
3467
  constructor(sharedStore, sharedVectorStore, embedder, config) {
4273
3468
  this.sharedStore = sharedStore;
@@ -4335,7 +3530,7 @@ var SharedPromoter = class {
4335
3530
  const embeddingContent = this.createEmbeddingContent(input);
4336
3531
  const embedding = await this.embedder.embed(embeddingContent);
4337
3532
  await this.sharedVectorStore.upsert({
4338
- id: randomUUID4(),
3533
+ id: randomUUID3(),
4339
3534
  entryId,
4340
3535
  entryType: "troubleshooting",
4341
3536
  content: embeddingContent,
@@ -4475,7 +3670,7 @@ function createToolObservationEmbedding(toolName, metadata, success) {
4475
3670
  }
4476
3671
 
4477
3672
  // src/core/working-set-store.ts
4478
- import { randomUUID as randomUUID5 } from "crypto";
3673
+ import { randomUUID as randomUUID4 } from "crypto";
4479
3674
  var WorkingSetStore = class {
4480
3675
  constructor(eventStore, config) {
4481
3676
  this.eventStore = eventStore;
@@ -4496,7 +3691,7 @@ var WorkingSetStore = class {
4496
3691
  `INSERT OR REPLACE INTO working_set (id, event_id, added_at, relevance_score, topics, expires_at)
4497
3692
  VALUES (?, ?, CURRENT_TIMESTAMP, ?, ?, ?)`,
4498
3693
  [
4499
- randomUUID5(),
3694
+ randomUUID4(),
4500
3695
  eventId,
4501
3696
  relevanceScore,
4502
3697
  JSON.stringify(topics || []),
@@ -4680,7 +3875,7 @@ function createWorkingSetStore(eventStore, config) {
4680
3875
  }
4681
3876
 
4682
3877
  // src/core/consolidated-store.ts
4683
- import { randomUUID as randomUUID6 } from "crypto";
3878
+ import { randomUUID as randomUUID5 } from "crypto";
4684
3879
  var ConsolidatedStore = class {
4685
3880
  constructor(eventStore) {
4686
3881
  this.eventStore = eventStore;
@@ -4692,7 +3887,7 @@ var ConsolidatedStore = class {
4692
3887
  * Create a new consolidated memory
4693
3888
  */
4694
3889
  async create(input) {
4695
- const memoryId = randomUUID6();
3890
+ const memoryId = randomUUID5();
4696
3891
  await dbRun(
4697
3892
  this.db,
4698
3893
  `INSERT INTO consolidated_memories
@@ -4822,7 +4017,7 @@ var ConsolidatedStore = class {
4822
4017
  * Create a long-term rule promoted from stable summaries
4823
4018
  */
4824
4019
  async createRule(input) {
4825
- const ruleId = randomUUID6();
4020
+ const ruleId = randomUUID5();
4826
4021
  await dbRun(
4827
4022
  this.db,
4828
4023
  `INSERT INTO consolidated_rules
@@ -5344,7 +4539,7 @@ function createConsolidationWorker(workingSetStore, consolidatedStore, config) {
5344
4539
  }
5345
4540
 
5346
4541
  // src/core/continuity-manager.ts
5347
- import { randomUUID as randomUUID7 } from "crypto";
4542
+ import { randomUUID as randomUUID6 } from "crypto";
5348
4543
  var ContinuityManager = class {
5349
4544
  constructor(eventStore, config) {
5350
4545
  this.eventStore = eventStore;
@@ -5498,7 +4693,7 @@ var ContinuityManager = class {
5498
4693
  `INSERT INTO continuity_log
5499
4694
  (log_id, from_context_id, to_context_id, continuity_score, transition_type, created_at)
5500
4695
  VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
5501
- [randomUUID7(), previous.id, current.id, score, type]
4696
+ [randomUUID6(), previous.id, current.id, score, type]
5502
4697
  );
5503
4698
  }
5504
4699
  /**
@@ -5607,7 +4802,7 @@ function createContinuityManager(eventStore, config) {
5607
4802
  }
5608
4803
 
5609
4804
  // src/core/graduation-worker.ts
5610
- var DEFAULT_CONFIG5 = {
4805
+ var DEFAULT_CONFIG4 = {
5611
4806
  evaluationIntervalMs: 3e5,
5612
4807
  // 5 minutes
5613
4808
  batchSize: 50,
@@ -5615,7 +4810,7 @@ var DEFAULT_CONFIG5 = {
5615
4810
  // 1 hour cooldown between evaluations
5616
4811
  };
5617
4812
  var GraduationWorker = class {
5618
- constructor(eventStore, graduation, config = DEFAULT_CONFIG5) {
4813
+ constructor(eventStore, graduation, config = DEFAULT_CONFIG4) {
5619
4814
  this.eventStore = eventStore;
5620
4815
  this.graduation = graduation;
5621
4816
  this.config = config;
@@ -5723,7 +4918,7 @@ function createGraduationWorker(eventStore, graduation, config) {
5723
4918
  return new GraduationWorker(
5724
4919
  eventStore,
5725
4920
  graduation,
5726
- { ...DEFAULT_CONFIG5, ...config }
4921
+ { ...DEFAULT_CONFIG4, ...config }
5727
4922
  );
5728
4923
  }
5729
4924
 
@@ -5936,9 +5131,6 @@ function getSessionProject(sessionId) {
5936
5131
  var MemoryService = class {
5937
5132
  // Primary store: SQLite (WAL mode) - for hooks, always available
5938
5133
  sqliteStore;
5939
- // Analytics store: DuckDB - for server reads (optional, synced from SQLite)
5940
- analyticsStore;
5941
- syncWorker = null;
5942
5134
  vectorStore;
5943
5135
  embedder;
5944
5136
  matcher;
@@ -5964,6 +5156,7 @@ var MemoryService = class {
5964
5156
  projectPath = null;
5965
5157
  readOnly;
5966
5158
  lightweightMode;
5159
+ embeddingOnly;
5967
5160
  mdMirror;
5968
5161
  storagePath;
5969
5162
  constructor(config) {
@@ -5971,6 +5164,7 @@ var MemoryService = class {
5971
5164
  this.storagePath = storagePath;
5972
5165
  this.readOnly = config.readOnly ?? false;
5973
5166
  this.lightweightMode = config.lightweightMode ?? false;
5167
+ this.embeddingOnly = config.embeddingOnly ?? false;
5974
5168
  this.mdMirror = new MarkdownMirror2(process.cwd());
5975
5169
  if (!this.readOnly && !fs4.existsSync(storagePath)) {
5976
5170
  fs4.mkdirSync(storagePath, { recursive: true });
@@ -5985,24 +5179,6 @@ var MemoryService = class {
5985
5179
  markdownMirrorRoot: storagePath
5986
5180
  }
5987
5181
  );
5988
- const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
5989
- if (!analyticsEnabled) {
5990
- this.analyticsStore = null;
5991
- } else if (this.readOnly) {
5992
- try {
5993
- this.analyticsStore = new EventStore(
5994
- path3.join(storagePath, "analytics.duckdb"),
5995
- { readOnly: true }
5996
- );
5997
- } catch {
5998
- this.analyticsStore = null;
5999
- }
6000
- } else {
6001
- this.analyticsStore = new EventStore(
6002
- path3.join(storagePath, "analytics.duckdb"),
6003
- { readOnly: false }
6004
- );
6005
- }
6006
5182
  this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
6007
5183
  const embeddingModel = config.embeddingModel || process.env.CLAUDE_MEMORY_EMBEDDING_MODEL;
6008
5184
  this.embedder = embeddingModel ? new Embedder(embeddingModel) : getDefaultEmbedder();
@@ -6028,13 +5204,6 @@ var MemoryService = class {
6028
5204
  this.initialized = true;
6029
5205
  return;
6030
5206
  }
6031
- if (this.analyticsStore) {
6032
- try {
6033
- await this.analyticsStore.initialize();
6034
- } catch (error) {
6035
- console.warn("[MemoryService] Analytics store (DuckDB) initialization failed, using SQLite for reads:", error);
6036
- }
6037
- }
6038
5207
  await this.vectorStore.initialize();
6039
5208
  await this.embedder.initialize();
6040
5209
  if (!this.readOnly) {
@@ -6044,19 +5213,13 @@ var MemoryService = class {
6044
5213
  this.embedder
6045
5214
  );
6046
5215
  this.vectorWorker.start();
6047
- this.retriever.setGraduationPipeline(this.graduation);
6048
- this.graduationWorker = createGraduationWorker(
6049
- this.sqliteStore,
6050
- this.graduation
6051
- );
6052
- this.graduationWorker.start();
6053
- if (this.analyticsStore) {
6054
- this.syncWorker = new SyncWorker(
5216
+ if (!this.embeddingOnly) {
5217
+ this.retriever.setGraduationPipeline(this.graduation);
5218
+ this.graduationWorker = createGraduationWorker(
6055
5219
  this.sqliteStore,
6056
- this.analyticsStore,
6057
- { intervalMs: 3e4, batchSize: 500 }
5220
+ this.graduation
6058
5221
  );
6059
- this.syncWorker.start();
5222
+ this.graduationWorker.start();
6060
5223
  }
6061
5224
  const savedMode = await this.sqliteStore.getEndlessConfig("mode");
6062
5225
  if (savedMode === "endless") {
@@ -6253,6 +5416,57 @@ var MemoryService = class {
6253
5416
  }
6254
5417
  );
6255
5418
  }
5419
+ /**
5420
+ * Backfill session summaries for recent sessions that are missing them.
5421
+ * Called from session-start hook to catch sessions that ended without Stop hook.
5422
+ */
5423
+ async backfillMissingSummaries(currentSessionId, limit = 5) {
5424
+ await this.initialize();
5425
+ const recentSessionIds = await this.sqliteStore.getSessionsWithoutSummary(currentSessionId, limit);
5426
+ for (const sid of recentSessionIds) {
5427
+ try {
5428
+ await this.generateSessionSummary(sid);
5429
+ } catch {
5430
+ }
5431
+ }
5432
+ }
5433
+ /**
5434
+ * Generate a rule-based session summary from stored events.
5435
+ * Called at session end (Stop hook) when no LLM-generated summary exists.
5436
+ * Skips if a summary already exists for this session.
5437
+ */
5438
+ async generateSessionSummary(sessionId) {
5439
+ await this.initialize();
5440
+ const events = await this.sqliteStore.getSessionEvents(sessionId);
5441
+ if (events.length < 3)
5442
+ return;
5443
+ const hasSummary = events.some((e) => e.eventType === "session_summary");
5444
+ if (hasSummary)
5445
+ return;
5446
+ const prompts = events.filter((e) => e.eventType === "user_prompt");
5447
+ const toolObs = events.filter((e) => e.eventType === "tool_observation");
5448
+ const toolNames = [...new Set(
5449
+ toolObs.map((e) => e.metadata?.toolName).filter(Boolean)
5450
+ )];
5451
+ const errorObs = toolObs.filter((e) => {
5452
+ const meta = e.metadata;
5453
+ return meta?.exitCode !== void 0 && meta.exitCode !== 0;
5454
+ });
5455
+ const datePart = events[0].timestamp.toISOString().split("T")[0];
5456
+ const parts = [`[${datePart}] ${prompts.length}\uD134 \uC138\uC158.`];
5457
+ if (prompts.length > 0) {
5458
+ const firstPrompt = prompts[0].content.slice(0, 120).replace(/\n/g, " ");
5459
+ parts.push(`\uC8FC\uC694 \uC791\uC5C5: ${firstPrompt}`);
5460
+ }
5461
+ if (toolNames.length > 0) {
5462
+ parts.push(`\uC0AC\uC6A9 \uD234: ${toolNames.slice(0, 6).join(", ")}`);
5463
+ }
5464
+ if (errorObs.length > 0) {
5465
+ parts.push(`\uC624\uB958 ${errorObs.length}\uAC74 \uBC1C\uC0DD`);
5466
+ }
5467
+ const summary = parts.join(". ");
5468
+ await this.storeSessionSummary(sessionId, summary, { generated: "rule-based", eventCount: events.length });
5469
+ }
6256
5470
  /**
6257
5471
  * Store a tool observation
6258
5472
  */
@@ -6780,6 +5994,20 @@ var MemoryService = class {
6780
5994
  await this.initialize();
6781
5995
  await this.sqliteStore.recordRetrieval(eventId, sessionId, score, query);
6782
5996
  }
5997
+ /**
5998
+ * Record a query-level retrieval trace (used by user-prompt-submit hook).
5999
+ * Feeds the retrieval_traces table that powers dashboard stats.
6000
+ */
6001
+ async recordQueryTrace(input) {
6002
+ await this.initialize();
6003
+ await this.sqliteStore.recordRetrievalTrace({
6004
+ ...input,
6005
+ projectHash: this.projectHash || void 0,
6006
+ candidateDetails: [],
6007
+ selectedDetails: [],
6008
+ fallbackTrace: []
6009
+ });
6010
+ }
6783
6011
  /**
6784
6012
  * Evaluate helpfulness of retrievals in a session (called at session end)
6785
6013
  */
@@ -6787,6 +6015,20 @@ var MemoryService = class {
6787
6015
  await this.initialize();
6788
6016
  await this.sqliteStore.evaluateSessionHelpfulness(sessionId);
6789
6017
  }
6018
+ /**
6019
+ * Backfill helpfulness evaluation for sessions that ended without Stop hook.
6020
+ * Call on first turn of a new session to catch missed evaluations.
6021
+ */
6022
+ async evaluatePendingSessions(currentSessionId) {
6023
+ await this.initialize();
6024
+ const sessions = await this.sqliteStore.getUnevaluatedSessions(currentSessionId, 5);
6025
+ for (const sid of sessions) {
6026
+ try {
6027
+ await this.sqliteStore.evaluateSessionHelpfulness(sid);
6028
+ } catch {
6029
+ }
6030
+ }
6031
+ }
6790
6032
  /**
6791
6033
  * Get most helpful memories ranked by helpfulness score
6792
6034
  */
@@ -7051,16 +6293,10 @@ var MemoryService = class {
7051
6293
  if (this.vectorWorker) {
7052
6294
  this.vectorWorker.stop();
7053
6295
  }
7054
- if (this.syncWorker) {
7055
- this.syncWorker.stop();
7056
- }
7057
6296
  if (this.sharedEventStore) {
7058
6297
  await this.sharedEventStore.close();
7059
6298
  }
7060
6299
  await this.sqliteStore.close();
7061
- if (this.analyticsStore) {
7062
- await this.analyticsStore.close();
7063
- }
7064
6300
  }
7065
6301
  /**
7066
6302
  * Expand ~ to home directory
@@ -7073,42 +6309,6 @@ var MemoryService = class {
7073
6309
  }
7074
6310
  };
7075
6311
  var serviceCache = /* @__PURE__ */ new Map();
7076
- var GLOBAL_KEY = "__global__";
7077
- function getDefaultMemoryService() {
7078
- if (!serviceCache.has(GLOBAL_KEY)) {
7079
- serviceCache.set(GLOBAL_KEY, new MemoryService({
7080
- storagePath: "~/.claude-code/memory",
7081
- analyticsEnabled: false,
7082
- // Hooks don't need DuckDB
7083
- sharedStoreConfig: { enabled: false }
7084
- // Shared store uses DuckDB too
7085
- }));
7086
- }
7087
- return serviceCache.get(GLOBAL_KEY);
7088
- }
7089
- function getMemoryServiceForProject(projectPath, sharedStoreConfig) {
7090
- const hash = hashProjectPath(projectPath);
7091
- if (!serviceCache.has(hash)) {
7092
- const storagePath = getProjectStoragePath(projectPath);
7093
- serviceCache.set(hash, new MemoryService({
7094
- storagePath,
7095
- projectHash: hash,
7096
- projectPath,
7097
- // Override shared store config - hooks don't need DuckDB
7098
- sharedStoreConfig: sharedStoreConfig ?? { enabled: false },
7099
- analyticsEnabled: false
7100
- // Hooks don't need DuckDB
7101
- }));
7102
- }
7103
- return serviceCache.get(hash);
7104
- }
7105
- function getMemoryServiceForSession(sessionId) {
7106
- const projectInfo = getSessionProject(sessionId);
7107
- if (projectInfo) {
7108
- return getMemoryServiceForProject(projectInfo.projectPath);
7109
- }
7110
- return getDefaultMemoryService();
7111
- }
7112
6312
  function getLightweightMemoryService(sessionId) {
7113
6313
  const projectInfo = getSessionProject(sessionId);
7114
6314
  const key = projectInfo ? `lightweight_${projectInfo.projectHash}` : "lightweight_global";
@@ -7155,6 +6355,176 @@ function writeTurnState(sessionId, turnId) {
7155
6355
  }
7156
6356
  }
7157
6357
  }
6358
+ function getLastResponsePath(sessionId) {
6359
+ return path4.join(TURN_STATE_DIR, `.last-response-${sessionId}.json`);
6360
+ }
6361
+ function readLastAssistantSnippet(sessionId) {
6362
+ try {
6363
+ const filePath = getLastResponsePath(sessionId);
6364
+ if (!fs5.existsSync(filePath))
6365
+ return null;
6366
+ const state = JSON.parse(fs5.readFileSync(filePath, "utf-8"));
6367
+ if (state.sessionId !== sessionId)
6368
+ return null;
6369
+ if (Date.now() - new Date(state.createdAt).getTime() > 2 * 60 * 60 * 1e3)
6370
+ return null;
6371
+ return state.snippet || null;
6372
+ } catch {
6373
+ return null;
6374
+ }
6375
+ }
6376
+
6377
+ // src/hooks/semantic-daemon-client.ts
6378
+ import { spawn } from "child_process";
6379
+ import * as fs6 from "fs";
6380
+ import * as net from "net";
6381
+ import * as os3 from "os";
6382
+ import * as path5 from "path";
6383
+ var DEFAULT_SOCKET_PATH = path5.join(
6384
+ os3.homedir(),
6385
+ ".claude-code",
6386
+ "memory",
6387
+ "semantic-daemon.sock"
6388
+ );
6389
+ var DAEMON_SOCKET_PATH = process.env.CLAUDE_MEMORY_SEMANTIC_SOCKET || DEFAULT_SOCKET_PATH;
6390
+ var DAEMON_START_TIMEOUT_MS = parseInt(process.env.CLAUDE_MEMORY_SEMANTIC_DAEMON_START_MS || "1500");
6391
+ var daemonStartPromise = null;
6392
+ async function retrieveSemanticMemories(request, timeoutMs) {
6393
+ const payload = {
6394
+ type: "retrieve",
6395
+ sessionId: request.sessionId,
6396
+ prompt: request.prompt,
6397
+ topK: request.topK,
6398
+ minScore: request.minScore
6399
+ };
6400
+ try {
6401
+ return await requestFromDaemon(payload, timeoutMs);
6402
+ } catch (error) {
6403
+ if (!isConnectionError(error)) {
6404
+ throw error;
6405
+ }
6406
+ await ensureDaemonRunning();
6407
+ return requestFromDaemon(payload, timeoutMs).catch((retryError) => {
6408
+ if (process.env.CLAUDE_MEMORY_DEBUG) {
6409
+ console.error("[semantic-client] retry failed after daemon start:", retryError);
6410
+ }
6411
+ throw retryError;
6412
+ });
6413
+ }
6414
+ }
6415
+ function requestFromDaemon(payload, timeoutMs) {
6416
+ return new Promise((resolve2, reject) => {
6417
+ const client = net.createConnection(DAEMON_SOCKET_PATH);
6418
+ client.setEncoding("utf8");
6419
+ let settled = false;
6420
+ let responseRaw = "";
6421
+ const timer = setTimeout(() => {
6422
+ const timeoutError = new Error(`semantic daemon timeout (${timeoutMs}ms)`);
6423
+ timeoutError.code = "ETIMEDOUT";
6424
+ settle(timeoutError);
6425
+ client.destroy();
6426
+ }, timeoutMs);
6427
+ const settle = (error, memories) => {
6428
+ if (settled)
6429
+ return;
6430
+ settled = true;
6431
+ clearTimeout(timer);
6432
+ if (error) {
6433
+ reject(error);
6434
+ } else {
6435
+ resolve2(memories || []);
6436
+ }
6437
+ };
6438
+ client.on("connect", () => {
6439
+ client.end(JSON.stringify(payload));
6440
+ });
6441
+ client.on("data", (chunk) => {
6442
+ responseRaw += chunk;
6443
+ if (responseRaw.length > 4 * 1024 * 1024) {
6444
+ settle(new Error("semantic daemon response too large"));
6445
+ client.destroy();
6446
+ }
6447
+ });
6448
+ client.on("end", () => {
6449
+ try {
6450
+ const parsed = JSON.parse(responseRaw || "{}");
6451
+ if (!parsed.ok) {
6452
+ settle(new Error(parsed.error || "semantic daemon error"));
6453
+ return;
6454
+ }
6455
+ settle(void 0, parsed.memories || []);
6456
+ } catch (error) {
6457
+ settle(error);
6458
+ }
6459
+ });
6460
+ client.on("error", (error) => {
6461
+ settle(error);
6462
+ });
6463
+ });
6464
+ }
6465
+ async function ensureDaemonRunning() {
6466
+ if (daemonStartPromise) {
6467
+ return daemonStartPromise;
6468
+ }
6469
+ daemonStartPromise = (async () => {
6470
+ if (await canConnect()) {
6471
+ return;
6472
+ }
6473
+ const daemonScriptPath = getDaemonScriptPath();
6474
+ if (!fs6.existsSync(daemonScriptPath)) {
6475
+ throw new Error(`semantic daemon script not found: ${daemonScriptPath}`);
6476
+ }
6477
+ const daemonDir = path5.dirname(DAEMON_SOCKET_PATH);
6478
+ if (!fs6.existsSync(daemonDir)) {
6479
+ fs6.mkdirSync(daemonDir, { recursive: true });
6480
+ }
6481
+ const child = spawn(process.execPath, [daemonScriptPath], {
6482
+ detached: true,
6483
+ stdio: "ignore",
6484
+ env: process.env
6485
+ });
6486
+ child.unref();
6487
+ const startDeadline = Date.now() + DAEMON_START_TIMEOUT_MS;
6488
+ while (Date.now() < startDeadline) {
6489
+ if (await canConnect()) {
6490
+ return;
6491
+ }
6492
+ await sleep(60);
6493
+ }
6494
+ throw new Error(`semantic daemon start timeout (${DAEMON_START_TIMEOUT_MS}ms)`);
6495
+ })();
6496
+ try {
6497
+ await daemonStartPromise;
6498
+ } finally {
6499
+ daemonStartPromise = null;
6500
+ }
6501
+ }
6502
+ function getDaemonScriptPath() {
6503
+ return path5.join(path5.dirname(new URL(import.meta.url).pathname), "semantic-daemon.js");
6504
+ }
6505
+ function canConnect() {
6506
+ return new Promise((resolve2) => {
6507
+ let settled = false;
6508
+ const client = net.createConnection(DAEMON_SOCKET_PATH);
6509
+ const finalize = (ok) => {
6510
+ if (settled)
6511
+ return;
6512
+ settled = true;
6513
+ client.destroy();
6514
+ resolve2(ok);
6515
+ };
6516
+ client.on("connect", () => finalize(true));
6517
+ client.on("error", () => finalize(false));
6518
+ setTimeout(() => finalize(false), 120).unref();
6519
+ });
6520
+ }
6521
+ function isConnectionError(error) {
6522
+ const code = error?.code;
6523
+ return code === "ENOENT" || code === "ECONNREFUSED" || code === "EPIPE" || code === "ECONNRESET";
6524
+ }
6525
+ function sleep(ms) {
6526
+ return new Promise((resolve2) => setTimeout(resolve2, ms));
6527
+ }
7158
6528
 
7159
6529
  // src/hooks/user-prompt-submit.ts
7160
6530
  var MAX_MEMORIES = parseInt(process.env.CLAUDE_MEMORY_MAX_COUNT || "5");
@@ -7162,9 +6532,9 @@ var BASE_MIN_SCORE = parseFloat(process.env.CLAUDE_MEMORY_MIN_SCORE || "0.4");
7162
6532
  var FALLBACK_MIN_SCORE = parseFloat(process.env.CLAUDE_MEMORY_FALLBACK_MIN_SCORE || "0.3");
7163
6533
  var ENABLE_SEARCH = process.env.CLAUDE_MEMORY_SEARCH !== "false";
7164
6534
  var RETRIEVAL_MODE = process.env.CLAUDE_MEMORY_RETRIEVAL_MODE || "hybrid";
7165
- var SEMANTIC_TIMEOUT_MS = parseInt(process.env.CLAUDE_MEMORY_SEMANTIC_TIMEOUT_MS || "1200");
6535
+ var SEMANTIC_TIMEOUT_MS = parseInt(process.env.CLAUDE_MEMORY_SEMANTIC_TIMEOUT_MS || "2000");
7166
6536
  var ADHERENCE_INTERVAL_TURNS = parseInt(process.env.CLAUDE_MEMORY_ADHERENCE_INTERVAL_TURNS || "3");
7167
- var ADHERENCE_STATE_DIR = path5.join(os3.homedir(), ".claude-code", "memory");
6537
+ var ADHERENCE_STATE_DIR = path6.join(os4.homedir(), ".claude-code", "memory");
7168
6538
  function shouldStorePrompt(prompt) {
7169
6539
  const trimmed = prompt.trim();
7170
6540
  if (trimmed.startsWith("/"))
@@ -7183,18 +6553,6 @@ function getDynamicMinScore(prompt) {
7183
6553
  return Math.max(0.3, BASE_MIN_SCORE - 0.05);
7184
6554
  return BASE_MIN_SCORE;
7185
6555
  }
7186
- function withTimeout(promise, timeoutMs) {
7187
- return new Promise((resolve2, reject) => {
7188
- const timer = setTimeout(() => reject(new Error(`semantic retrieval timeout (${timeoutMs}ms)`)), timeoutMs);
7189
- promise.then((result) => {
7190
- clearTimeout(timer);
7191
- resolve2(result);
7192
- }).catch((error) => {
7193
- clearTimeout(timer);
7194
- reject(error);
7195
- });
7196
- });
7197
- }
7198
6556
  function formatMemoryContext(items) {
7199
6557
  if (items.length === 0)
7200
6558
  return "";
@@ -7207,12 +6565,12 @@ function formatMemoryContext(items) {
7207
6565
  ${lines.join("\n\n")}`;
7208
6566
  }
7209
6567
  function getAdherenceStatePath(sessionId) {
7210
- return path5.join(ADHERENCE_STATE_DIR, `.adherence-state-${sessionId}.json`);
6568
+ return path6.join(ADHERENCE_STATE_DIR, `.adherence-state-${sessionId}.json`);
7211
6569
  }
7212
6570
  function readAdherenceState(sessionId) {
7213
6571
  try {
7214
6572
  const filePath = getAdherenceStatePath(sessionId);
7215
- if (!fs6.existsSync(filePath)) {
6573
+ if (!fs7.existsSync(filePath)) {
7216
6574
  return {
7217
6575
  sessionId,
7218
6576
  turnCount: 0,
@@ -7222,7 +6580,7 @@ function readAdherenceState(sessionId) {
7222
6580
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
7223
6581
  };
7224
6582
  }
7225
- const data = fs6.readFileSync(filePath, "utf8");
6583
+ const data = fs7.readFileSync(filePath, "utf8");
7226
6584
  const parsed = JSON.parse(data);
7227
6585
  if (parsed.sessionId !== sessionId)
7228
6586
  throw new Error("session mismatch");
@@ -7240,13 +6598,13 @@ function readAdherenceState(sessionId) {
7240
6598
  }
7241
6599
  function writeAdherenceState(state) {
7242
6600
  try {
7243
- if (!fs6.existsSync(ADHERENCE_STATE_DIR)) {
7244
- fs6.mkdirSync(ADHERENCE_STATE_DIR, { recursive: true });
6601
+ if (!fs7.existsSync(ADHERENCE_STATE_DIR)) {
6602
+ fs7.mkdirSync(ADHERENCE_STATE_DIR, { recursive: true });
7245
6603
  }
7246
6604
  const filePath = getAdherenceStatePath(state.sessionId);
7247
6605
  const tempPath = filePath + ".tmp";
7248
- fs6.writeFileSync(tempPath, JSON.stringify(state));
7249
- fs6.renameSync(tempPath, filePath);
6606
+ fs7.writeFileSync(tempPath, JSON.stringify(state));
6607
+ fs7.renameSync(tempPath, filePath);
7250
6608
  } catch {
7251
6609
  }
7252
6610
  }
@@ -7293,7 +6651,7 @@ function logAdherenceDecision(sessionId, turn, run, reason) {
7293
6651
  async function main() {
7294
6652
  const inputData = await readStdin();
7295
6653
  const input = JSON.parse(inputData);
7296
- const turnId = randomUUID9();
6654
+ const turnId = randomUUID8();
7297
6655
  writeTurnState(input.session_id, turnId);
7298
6656
  const memoryService = getLightweightMemoryService(input.session_id);
7299
6657
  try {
@@ -7302,6 +6660,10 @@ async function main() {
7302
6660
  const currentTurn = adherenceState.turnCount + 1;
7303
6661
  const adherenceDecision = shouldRunAdherenceCheck(currentTurn, input.prompt, adherenceState);
7304
6662
  logAdherenceDecision(input.session_id, currentTurn, adherenceDecision.run, adherenceDecision.reason);
6663
+ if (currentTurn === 1) {
6664
+ memoryService.evaluatePendingSessions(input.session_id).catch(() => {
6665
+ });
6666
+ }
7305
6667
  if (shouldStorePrompt(input.prompt)) {
7306
6668
  await memoryService.storeUserPrompt(
7307
6669
  input.session_id,
@@ -7316,41 +6678,37 @@ async function main() {
7316
6678
  }
7317
6679
  );
7318
6680
  }
7319
- if (ENABLE_SEARCH && input.prompt.length > 10 && adherenceDecision.run) {
6681
+ const isSlashCommand = input.prompt.trimStart().startsWith("/");
6682
+ if (ENABLE_SEARCH && !isSlashCommand && input.prompt.length > 10 && adherenceDecision.run) {
7320
6683
  const minScore = getDynamicMinScore(input.prompt);
7321
6684
  let mergedMemories = [];
6685
+ const lastSnippet = currentTurn > 1 ? readLastAssistantSnippet(input.session_id) : null;
6686
+ const retrievalQuery = lastSnippet ? `${lastSnippet}
6687
+
6688
+ ${input.prompt}` : input.prompt;
7322
6689
  const canUseSemantic = RETRIEVAL_MODE === "semantic" || RETRIEVAL_MODE === "hybrid";
7323
6690
  if (canUseSemantic) {
7324
6691
  try {
7325
- const semanticService = getMemoryServiceForSession(input.session_id);
7326
- const semantic = await withTimeout(
7327
- semanticService.retrieveMemories(input.prompt, {
7328
- topK: MAX_MEMORIES,
7329
- minScore,
6692
+ mergedMemories = await retrieveSemanticMemories(
6693
+ {
7330
6694
  sessionId: input.session_id,
7331
- intentRewrite: true,
7332
- adaptiveRerank: true,
7333
- projectScopeMode: "strict"
7334
- }),
6695
+ prompt: retrievalQuery,
6696
+ topK: MAX_MEMORIES,
6697
+ minScore
6698
+ },
7335
6699
  SEMANTIC_TIMEOUT_MS
7336
6700
  );
7337
- mergedMemories = semantic.memories.map((m) => ({
7338
- type: m.event.eventType,
7339
- content: m.event.content,
7340
- id: m.event.id,
7341
- score: m.score
7342
- }));
7343
6701
  } catch {
7344
6702
  }
7345
6703
  }
7346
6704
  const shouldUseKeywordFallback = RETRIEVAL_MODE === "keyword" || RETRIEVAL_MODE === "hybrid" || mergedMemories.length === 0;
7347
6705
  if (shouldUseKeywordFallback && mergedMemories.length < MAX_MEMORIES) {
7348
- let results = await memoryService.keywordSearch(input.prompt, {
6706
+ let results = await memoryService.keywordSearch(retrievalQuery, {
7349
6707
  topK: MAX_MEMORIES,
7350
6708
  minScore
7351
6709
  });
7352
6710
  if (results.length === 0 && FALLBACK_MIN_SCORE < minScore) {
7353
- results = await memoryService.keywordSearch(input.prompt, {
6711
+ results = await memoryService.keywordSearch(retrievalQuery, {
7354
6712
  topK: MAX_MEMORIES,
7355
6713
  minScore: FALLBACK_MIN_SCORE
7356
6714
  });
@@ -7389,6 +6747,18 @@ async function main() {
7389
6747
  }
7390
6748
  context = formatMemoryContext(mergedMemories);
7391
6749
  }
6750
+ const allCandidateIds = mergedMemories.map((m) => m.id).filter((v) => Boolean(v));
6751
+ try {
6752
+ await memoryService.recordQueryTrace({
6753
+ sessionId: input.session_id,
6754
+ queryText: retrievalQuery,
6755
+ strategy: RETRIEVAL_MODE,
6756
+ candidateEventIds: allCandidateIds,
6757
+ selectedEventIds: allCandidateIds,
6758
+ confidence: mergedMemories.length > 0 ? "medium" : "none"
6759
+ });
6760
+ } catch {
6761
+ }
7392
6762
  }
7393
6763
  writeAdherenceState({
7394
6764
  sessionId: input.session_id,