claude-memory-layer 1.0.24 → 1.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +15 -1
- package/dist/cli/index.js +156 -972
- package/dist/cli/index.js.map +4 -4
- package/dist/core/index.js +33 -67
- package/dist/core/index.js.map +3 -3
- package/dist/hooks/post-tool-use.js +183 -968
- package/dist/hooks/post-tool-use.js.map +4 -4
- package/dist/hooks/semantic-daemon.js +150 -966
- package/dist/hooks/semantic-daemon.js.map +4 -4
- package/dist/hooks/session-end.js +150 -966
- package/dist/hooks/session-end.js.map +4 -4
- package/dist/hooks/session-start.js +152 -966
- package/dist/hooks/session-start.js.map +4 -4
- package/dist/hooks/stop.js +158 -966
- package/dist/hooks/stop.js.map +4 -4
- package/dist/hooks/user-prompt-submit.js +152 -968
- package/dist/hooks/user-prompt-submit.js.map +4 -4
- package/dist/server/api/index.js +151 -967
- package/dist/server/api/index.js.map +4 -4
- package/dist/server/index.js +151 -967
- package/dist/server/index.js.map +4 -4
- package/dist/services/memory-service.js +150 -966
- package/dist/services/memory-service.js.map +4 -4
- package/memory/_index.md +2 -0
- package/memory/agent_response/uncategorized/2026-03-04.md +276 -1
- package/memory/agent_response/uncategorized/2026-03-05.md +48 -0
- package/memory/session_summary/uncategorized/2026-03-04.md +20 -1
- package/memory/tool_observation/uncategorized/2026-03-04.md +245 -1
- package/memory/tool_observation/uncategorized/2026-03-05.md +29 -0
- package/memory/user_prompt/uncategorized/2026-03-04.md +193 -1
- package/package.json +1 -2
- package/specs/memory-utilization-improvements/context.md +145 -0
- package/specs/memory-utilization-improvements/plan.md +361 -0
- package/specs/memory-utilization-improvements/spec.md +361 -0
- package/specs/optional-duckdb/context.md +77 -0
- package/specs/optional-duckdb/plan.md +142 -0
- package/specs/optional-duckdb/spec.md +35 -0
- package/src/core/db-wrapper.ts +18 -73
- package/src/core/sqlite-event-store.ts +32 -4
- package/src/hooks/post-tool-use.ts +25 -0
- package/src/hooks/session-start.ts +4 -0
- package/src/hooks/stop.ts +14 -0
- package/src/server/api/utils.ts +1 -1
- package/src/services/memory-service.ts +62 -58
|
@@ -12,744 +12,31 @@ import * as os from "os";
|
|
|
12
12
|
import * as fs4 from "fs";
|
|
13
13
|
import * as crypto2 from "crypto";
|
|
14
14
|
|
|
15
|
-
// src/core/event-store.ts
|
|
16
|
-
import { randomUUID } from "crypto";
|
|
17
|
-
|
|
18
|
-
// src/core/canonical-key.ts
|
|
19
|
-
import { createHash } from "crypto";
|
|
20
|
-
var MAX_KEY_LENGTH = 200;
|
|
21
|
-
function makeCanonicalKey(title, context) {
|
|
22
|
-
let normalized = title.normalize("NFKC");
|
|
23
|
-
normalized = normalized.toLowerCase();
|
|
24
|
-
normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
|
|
25
|
-
normalized = normalized.replace(/\s+/g, " ").trim();
|
|
26
|
-
let key = normalized;
|
|
27
|
-
if (context?.project) {
|
|
28
|
-
key = `${context.project}::${key}`;
|
|
29
|
-
}
|
|
30
|
-
if (key.length > MAX_KEY_LENGTH) {
|
|
31
|
-
const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
|
|
32
|
-
key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
|
|
33
|
-
}
|
|
34
|
-
return key;
|
|
35
|
-
}
|
|
36
|
-
function makeDedupeKey(content, sessionId) {
|
|
37
|
-
const contentHash = createHash("sha256").update(content).digest("hex");
|
|
38
|
-
return `${sessionId}:${contentHash}`;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/core/db-wrapper.ts
|
|
42
|
-
import duckdb from "duckdb";
|
|
43
|
-
function convertBigInts(obj) {
|
|
44
|
-
if (obj === null || obj === void 0)
|
|
45
|
-
return obj;
|
|
46
|
-
if (typeof obj === "bigint")
|
|
47
|
-
return Number(obj);
|
|
48
|
-
if (obj instanceof Date)
|
|
49
|
-
return obj;
|
|
50
|
-
if (Array.isArray(obj))
|
|
51
|
-
return obj.map(convertBigInts);
|
|
52
|
-
if (typeof obj === "object") {
|
|
53
|
-
const result = {};
|
|
54
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
55
|
-
result[key] = convertBigInts(value);
|
|
56
|
-
}
|
|
57
|
-
return result;
|
|
58
|
-
}
|
|
59
|
-
return obj;
|
|
60
|
-
}
|
|
61
|
-
function toDate(value) {
|
|
62
|
-
if (value instanceof Date)
|
|
63
|
-
return value;
|
|
64
|
-
if (typeof value === "string")
|
|
65
|
-
return new Date(value);
|
|
66
|
-
if (typeof value === "number")
|
|
67
|
-
return new Date(value);
|
|
68
|
-
return new Date(String(value));
|
|
69
|
-
}
|
|
70
|
-
function createDatabase(path4, options) {
|
|
71
|
-
if (options?.readOnly) {
|
|
72
|
-
return new duckdb.Database(path4, { access_mode: "READ_ONLY" });
|
|
73
|
-
}
|
|
74
|
-
return new duckdb.Database(path4);
|
|
75
|
-
}
|
|
76
|
-
function dbRun(db, sql, params = []) {
|
|
77
|
-
return new Promise((resolve2, reject) => {
|
|
78
|
-
if (params.length === 0) {
|
|
79
|
-
db.run(sql, (err) => {
|
|
80
|
-
if (err)
|
|
81
|
-
reject(err);
|
|
82
|
-
else
|
|
83
|
-
resolve2();
|
|
84
|
-
});
|
|
85
|
-
} else {
|
|
86
|
-
db.run(sql, ...params, (err) => {
|
|
87
|
-
if (err)
|
|
88
|
-
reject(err);
|
|
89
|
-
else
|
|
90
|
-
resolve2();
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
function dbAll(db, sql, params = []) {
|
|
96
|
-
return new Promise((resolve2, reject) => {
|
|
97
|
-
if (params.length === 0) {
|
|
98
|
-
db.all(sql, (err, rows) => {
|
|
99
|
-
if (err)
|
|
100
|
-
reject(err);
|
|
101
|
-
else
|
|
102
|
-
resolve2(convertBigInts(rows || []));
|
|
103
|
-
});
|
|
104
|
-
} else {
|
|
105
|
-
db.all(sql, ...params, (err, rows) => {
|
|
106
|
-
if (err)
|
|
107
|
-
reject(err);
|
|
108
|
-
else
|
|
109
|
-
resolve2(convertBigInts(rows || []));
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
function dbClose(db) {
|
|
115
|
-
return new Promise((resolve2, reject) => {
|
|
116
|
-
db.close((err) => {
|
|
117
|
-
if (err)
|
|
118
|
-
reject(err);
|
|
119
|
-
else
|
|
120
|
-
resolve2();
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// src/core/event-store.ts
|
|
126
|
-
var EventStore = class {
|
|
127
|
-
constructor(dbPath, options) {
|
|
128
|
-
this.dbPath = dbPath;
|
|
129
|
-
this.readOnly = options?.readOnly ?? false;
|
|
130
|
-
this.db = createDatabase(dbPath, { readOnly: this.readOnly });
|
|
131
|
-
}
|
|
132
|
-
db;
|
|
133
|
-
initialized = false;
|
|
134
|
-
readOnly;
|
|
135
|
-
/**
|
|
136
|
-
* Initialize database schema
|
|
137
|
-
*/
|
|
138
|
-
async initialize() {
|
|
139
|
-
if (this.initialized)
|
|
140
|
-
return;
|
|
141
|
-
if (this.readOnly) {
|
|
142
|
-
this.initialized = true;
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
await dbRun(this.db, `
|
|
146
|
-
CREATE TABLE IF NOT EXISTS events (
|
|
147
|
-
id VARCHAR PRIMARY KEY,
|
|
148
|
-
event_type VARCHAR NOT NULL,
|
|
149
|
-
session_id VARCHAR NOT NULL,
|
|
150
|
-
timestamp TIMESTAMP NOT NULL,
|
|
151
|
-
content TEXT NOT NULL,
|
|
152
|
-
canonical_key VARCHAR NOT NULL,
|
|
153
|
-
dedupe_key VARCHAR UNIQUE,
|
|
154
|
-
metadata JSON
|
|
155
|
-
)
|
|
156
|
-
`);
|
|
157
|
-
await dbRun(this.db, `
|
|
158
|
-
CREATE TABLE IF NOT EXISTS event_dedup (
|
|
159
|
-
dedupe_key VARCHAR PRIMARY KEY,
|
|
160
|
-
event_id VARCHAR NOT NULL,
|
|
161
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
162
|
-
)
|
|
163
|
-
`);
|
|
164
|
-
await dbRun(this.db, `
|
|
165
|
-
CREATE TABLE IF NOT EXISTS sessions (
|
|
166
|
-
id VARCHAR PRIMARY KEY,
|
|
167
|
-
started_at TIMESTAMP NOT NULL,
|
|
168
|
-
ended_at TIMESTAMP,
|
|
169
|
-
project_path VARCHAR,
|
|
170
|
-
summary TEXT,
|
|
171
|
-
tags JSON
|
|
172
|
-
)
|
|
173
|
-
`);
|
|
174
|
-
await dbRun(this.db, `
|
|
175
|
-
CREATE TABLE IF NOT EXISTS insights (
|
|
176
|
-
id VARCHAR PRIMARY KEY,
|
|
177
|
-
insight_type VARCHAR NOT NULL,
|
|
178
|
-
content TEXT NOT NULL,
|
|
179
|
-
canonical_key VARCHAR NOT NULL,
|
|
180
|
-
confidence FLOAT,
|
|
181
|
-
source_events JSON,
|
|
182
|
-
created_at TIMESTAMP,
|
|
183
|
-
last_updated TIMESTAMP
|
|
184
|
-
)
|
|
185
|
-
`);
|
|
186
|
-
await dbRun(this.db, `
|
|
187
|
-
CREATE TABLE IF NOT EXISTS embedding_outbox (
|
|
188
|
-
id VARCHAR PRIMARY KEY,
|
|
189
|
-
event_id VARCHAR NOT NULL,
|
|
190
|
-
content TEXT NOT NULL,
|
|
191
|
-
status VARCHAR DEFAULT 'pending',
|
|
192
|
-
retry_count INT DEFAULT 0,
|
|
193
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
194
|
-
processed_at TIMESTAMP,
|
|
195
|
-
error_message TEXT
|
|
196
|
-
)
|
|
197
|
-
`);
|
|
198
|
-
await dbRun(this.db, `
|
|
199
|
-
CREATE TABLE IF NOT EXISTS projection_offsets (
|
|
200
|
-
projection_name VARCHAR PRIMARY KEY,
|
|
201
|
-
last_event_id VARCHAR,
|
|
202
|
-
last_timestamp TIMESTAMP,
|
|
203
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
204
|
-
)
|
|
205
|
-
`);
|
|
206
|
-
await dbRun(this.db, `
|
|
207
|
-
CREATE TABLE IF NOT EXISTS memory_levels (
|
|
208
|
-
event_id VARCHAR PRIMARY KEY,
|
|
209
|
-
level VARCHAR NOT NULL DEFAULT 'L0',
|
|
210
|
-
promoted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
211
|
-
)
|
|
212
|
-
`);
|
|
213
|
-
await dbRun(this.db, `
|
|
214
|
-
CREATE TABLE IF NOT EXISTS entries (
|
|
215
|
-
entry_id VARCHAR PRIMARY KEY,
|
|
216
|
-
created_ts TIMESTAMP NOT NULL,
|
|
217
|
-
entry_type VARCHAR NOT NULL,
|
|
218
|
-
title VARCHAR NOT NULL,
|
|
219
|
-
content_json JSON NOT NULL,
|
|
220
|
-
stage VARCHAR NOT NULL DEFAULT 'raw',
|
|
221
|
-
status VARCHAR DEFAULT 'active',
|
|
222
|
-
superseded_by VARCHAR,
|
|
223
|
-
build_id VARCHAR,
|
|
224
|
-
evidence_json JSON,
|
|
225
|
-
canonical_key VARCHAR,
|
|
226
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
227
|
-
)
|
|
228
|
-
`);
|
|
229
|
-
await dbRun(this.db, `
|
|
230
|
-
CREATE TABLE IF NOT EXISTS entities (
|
|
231
|
-
entity_id VARCHAR PRIMARY KEY,
|
|
232
|
-
entity_type VARCHAR NOT NULL,
|
|
233
|
-
canonical_key VARCHAR NOT NULL,
|
|
234
|
-
title VARCHAR NOT NULL,
|
|
235
|
-
stage VARCHAR NOT NULL DEFAULT 'raw',
|
|
236
|
-
status VARCHAR NOT NULL DEFAULT 'active',
|
|
237
|
-
current_json JSON NOT NULL,
|
|
238
|
-
title_norm VARCHAR,
|
|
239
|
-
search_text VARCHAR,
|
|
240
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
241
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
242
|
-
)
|
|
243
|
-
`);
|
|
244
|
-
await dbRun(this.db, `
|
|
245
|
-
CREATE TABLE IF NOT EXISTS entity_aliases (
|
|
246
|
-
entity_type VARCHAR NOT NULL,
|
|
247
|
-
canonical_key VARCHAR NOT NULL,
|
|
248
|
-
entity_id VARCHAR NOT NULL,
|
|
249
|
-
is_primary BOOLEAN DEFAULT FALSE,
|
|
250
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
251
|
-
PRIMARY KEY(entity_type, canonical_key)
|
|
252
|
-
)
|
|
253
|
-
`);
|
|
254
|
-
await dbRun(this.db, `
|
|
255
|
-
CREATE TABLE IF NOT EXISTS edges (
|
|
256
|
-
edge_id VARCHAR PRIMARY KEY,
|
|
257
|
-
src_type VARCHAR NOT NULL,
|
|
258
|
-
src_id VARCHAR NOT NULL,
|
|
259
|
-
rel_type VARCHAR NOT NULL,
|
|
260
|
-
dst_type VARCHAR NOT NULL,
|
|
261
|
-
dst_id VARCHAR NOT NULL,
|
|
262
|
-
meta_json JSON,
|
|
263
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
264
|
-
)
|
|
265
|
-
`);
|
|
266
|
-
await dbRun(this.db, `
|
|
267
|
-
CREATE TABLE IF NOT EXISTS vector_outbox (
|
|
268
|
-
job_id VARCHAR PRIMARY KEY,
|
|
269
|
-
item_kind VARCHAR NOT NULL,
|
|
270
|
-
item_id VARCHAR NOT NULL,
|
|
271
|
-
embedding_version VARCHAR NOT NULL,
|
|
272
|
-
status VARCHAR NOT NULL DEFAULT 'pending',
|
|
273
|
-
retry_count INT DEFAULT 0,
|
|
274
|
-
error VARCHAR,
|
|
275
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
276
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
277
|
-
UNIQUE(item_kind, item_id, embedding_version)
|
|
278
|
-
)
|
|
279
|
-
`);
|
|
280
|
-
await dbRun(this.db, `
|
|
281
|
-
CREATE TABLE IF NOT EXISTS build_runs (
|
|
282
|
-
build_id VARCHAR PRIMARY KEY,
|
|
283
|
-
started_at TIMESTAMP NOT NULL,
|
|
284
|
-
finished_at TIMESTAMP,
|
|
285
|
-
extractor_model VARCHAR NOT NULL,
|
|
286
|
-
extractor_prompt_hash VARCHAR NOT NULL,
|
|
287
|
-
embedder_model VARCHAR NOT NULL,
|
|
288
|
-
embedding_version VARCHAR NOT NULL,
|
|
289
|
-
idris_version VARCHAR NOT NULL,
|
|
290
|
-
schema_version VARCHAR NOT NULL,
|
|
291
|
-
status VARCHAR NOT NULL DEFAULT 'running',
|
|
292
|
-
error VARCHAR
|
|
293
|
-
)
|
|
294
|
-
`);
|
|
295
|
-
await dbRun(this.db, `
|
|
296
|
-
CREATE TABLE IF NOT EXISTS pipeline_metrics (
|
|
297
|
-
id VARCHAR PRIMARY KEY,
|
|
298
|
-
ts TIMESTAMP NOT NULL,
|
|
299
|
-
stage VARCHAR NOT NULL,
|
|
300
|
-
latency_ms DOUBLE NOT NULL,
|
|
301
|
-
success BOOLEAN NOT NULL,
|
|
302
|
-
error VARCHAR,
|
|
303
|
-
session_id VARCHAR
|
|
304
|
-
)
|
|
305
|
-
`);
|
|
306
|
-
await dbRun(this.db, `
|
|
307
|
-
CREATE TABLE IF NOT EXISTS working_set (
|
|
308
|
-
id VARCHAR PRIMARY KEY,
|
|
309
|
-
event_id VARCHAR NOT NULL,
|
|
310
|
-
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
311
|
-
relevance_score FLOAT DEFAULT 1.0,
|
|
312
|
-
topics JSON,
|
|
313
|
-
expires_at TIMESTAMP
|
|
314
|
-
)
|
|
315
|
-
`);
|
|
316
|
-
await dbRun(this.db, `
|
|
317
|
-
CREATE TABLE IF NOT EXISTS consolidated_memories (
|
|
318
|
-
memory_id VARCHAR PRIMARY KEY,
|
|
319
|
-
summary TEXT NOT NULL,
|
|
320
|
-
topics JSON,
|
|
321
|
-
source_events JSON,
|
|
322
|
-
confidence FLOAT DEFAULT 0.5,
|
|
323
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
324
|
-
accessed_at TIMESTAMP,
|
|
325
|
-
access_count INTEGER DEFAULT 0
|
|
326
|
-
)
|
|
327
|
-
`);
|
|
328
|
-
await dbRun(this.db, `
|
|
329
|
-
CREATE TABLE IF NOT EXISTS continuity_log (
|
|
330
|
-
log_id VARCHAR PRIMARY KEY,
|
|
331
|
-
from_context_id VARCHAR,
|
|
332
|
-
to_context_id VARCHAR,
|
|
333
|
-
continuity_score FLOAT,
|
|
334
|
-
transition_type VARCHAR,
|
|
335
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
336
|
-
)
|
|
337
|
-
`);
|
|
338
|
-
await dbRun(this.db, `
|
|
339
|
-
CREATE TABLE IF NOT EXISTS consolidated_rules (
|
|
340
|
-
rule_id VARCHAR PRIMARY KEY,
|
|
341
|
-
rule TEXT NOT NULL,
|
|
342
|
-
topics JSON,
|
|
343
|
-
source_memory_ids JSON,
|
|
344
|
-
source_events JSON,
|
|
345
|
-
confidence FLOAT DEFAULT 0.5,
|
|
346
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
347
|
-
)
|
|
348
|
-
`);
|
|
349
|
-
await dbRun(this.db, `
|
|
350
|
-
CREATE TABLE IF NOT EXISTS endless_config (
|
|
351
|
-
key VARCHAR PRIMARY KEY,
|
|
352
|
-
value JSON,
|
|
353
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
354
|
-
)
|
|
355
|
-
`);
|
|
356
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type)`);
|
|
357
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_stage ON entries(stage)`);
|
|
358
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entries_canonical ON entries(canonical_key)`);
|
|
359
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_type_key ON entities(entity_type, canonical_key)`);
|
|
360
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_entities_status ON entities(status)`);
|
|
361
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_src ON edges(src_id, rel_type)`);
|
|
362
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_dst ON edges(dst_id, rel_type)`);
|
|
363
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_edges_rel ON edges(rel_type)`);
|
|
364
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_outbox_status ON vector_outbox(status)`);
|
|
365
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
|
|
366
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
|
|
367
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
|
|
368
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
|
|
369
|
-
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
|
|
370
|
-
this.initialized = true;
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Append event to store (AXIOMMIND Principle 2: Append-only)
|
|
374
|
-
* Returns existing event ID if duplicate (Principle 3: Idempotency)
|
|
375
|
-
*/
|
|
376
|
-
async append(input) {
|
|
377
|
-
await this.initialize();
|
|
378
|
-
const canonicalKey = makeCanonicalKey(input.content);
|
|
379
|
-
const dedupeKey = makeDedupeKey(input.content, input.sessionId);
|
|
380
|
-
const existing = await dbAll(
|
|
381
|
-
this.db,
|
|
382
|
-
`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`,
|
|
383
|
-
[dedupeKey]
|
|
384
|
-
);
|
|
385
|
-
if (existing.length > 0) {
|
|
386
|
-
return {
|
|
387
|
-
success: true,
|
|
388
|
-
eventId: existing[0].event_id,
|
|
389
|
-
isDuplicate: true
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
const id = randomUUID();
|
|
393
|
-
const timestamp = input.timestamp.toISOString();
|
|
394
|
-
try {
|
|
395
|
-
await dbRun(
|
|
396
|
-
this.db,
|
|
397
|
-
`INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
|
|
398
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
399
|
-
[
|
|
400
|
-
id,
|
|
401
|
-
input.eventType,
|
|
402
|
-
input.sessionId,
|
|
403
|
-
timestamp,
|
|
404
|
-
input.content,
|
|
405
|
-
canonicalKey,
|
|
406
|
-
dedupeKey,
|
|
407
|
-
JSON.stringify(input.metadata || {})
|
|
408
|
-
]
|
|
409
|
-
);
|
|
410
|
-
await dbRun(
|
|
411
|
-
this.db,
|
|
412
|
-
`INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)`,
|
|
413
|
-
[dedupeKey, id]
|
|
414
|
-
);
|
|
415
|
-
await dbRun(
|
|
416
|
-
this.db,
|
|
417
|
-
`INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')`,
|
|
418
|
-
[id]
|
|
419
|
-
);
|
|
420
|
-
return { success: true, eventId: id, isDuplicate: false };
|
|
421
|
-
} catch (error) {
|
|
422
|
-
return {
|
|
423
|
-
success: false,
|
|
424
|
-
error: error instanceof Error ? error.message : String(error)
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Get events by session ID
|
|
430
|
-
*/
|
|
431
|
-
async getSessionEvents(sessionId) {
|
|
432
|
-
await this.initialize();
|
|
433
|
-
const rows = await dbAll(
|
|
434
|
-
this.db,
|
|
435
|
-
`SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
|
|
436
|
-
[sessionId]
|
|
437
|
-
);
|
|
438
|
-
return rows.map(this.rowToEvent);
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Get recent events
|
|
442
|
-
*/
|
|
443
|
-
async getRecentEvents(limit = 100) {
|
|
444
|
-
await this.initialize();
|
|
445
|
-
const rows = await dbAll(
|
|
446
|
-
this.db,
|
|
447
|
-
`SELECT * FROM events ORDER BY timestamp DESC LIMIT ?`,
|
|
448
|
-
[limit]
|
|
449
|
-
);
|
|
450
|
-
return rows.map(this.rowToEvent);
|
|
451
|
-
}
|
|
452
|
-
/**
|
|
453
|
-
* Get event by ID
|
|
454
|
-
*/
|
|
455
|
-
async getEvent(id) {
|
|
456
|
-
await this.initialize();
|
|
457
|
-
const rows = await dbAll(
|
|
458
|
-
this.db,
|
|
459
|
-
`SELECT * FROM events WHERE id = ?`,
|
|
460
|
-
[id]
|
|
461
|
-
);
|
|
462
|
-
if (rows.length === 0)
|
|
463
|
-
return null;
|
|
464
|
-
return this.rowToEvent(rows[0]);
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* Create or update session
|
|
468
|
-
*/
|
|
469
|
-
async upsertSession(session) {
|
|
470
|
-
await this.initialize();
|
|
471
|
-
const existing = await dbAll(
|
|
472
|
-
this.db,
|
|
473
|
-
`SELECT id FROM sessions WHERE id = ?`,
|
|
474
|
-
[session.id]
|
|
475
|
-
);
|
|
476
|
-
if (existing.length === 0) {
|
|
477
|
-
await dbRun(
|
|
478
|
-
this.db,
|
|
479
|
-
`INSERT INTO sessions (id, started_at, project_path, tags)
|
|
480
|
-
VALUES (?, ?, ?, ?)`,
|
|
481
|
-
[
|
|
482
|
-
session.id,
|
|
483
|
-
(session.startedAt || /* @__PURE__ */ new Date()).toISOString(),
|
|
484
|
-
session.projectPath || null,
|
|
485
|
-
JSON.stringify(session.tags || [])
|
|
486
|
-
]
|
|
487
|
-
);
|
|
488
|
-
} else {
|
|
489
|
-
const updates = [];
|
|
490
|
-
const values = [];
|
|
491
|
-
if (session.endedAt) {
|
|
492
|
-
updates.push("ended_at = ?");
|
|
493
|
-
values.push(session.endedAt.toISOString());
|
|
494
|
-
}
|
|
495
|
-
if (session.summary) {
|
|
496
|
-
updates.push("summary = ?");
|
|
497
|
-
values.push(session.summary);
|
|
498
|
-
}
|
|
499
|
-
if (session.tags) {
|
|
500
|
-
updates.push("tags = ?");
|
|
501
|
-
values.push(JSON.stringify(session.tags));
|
|
502
|
-
}
|
|
503
|
-
if (updates.length > 0) {
|
|
504
|
-
values.push(session.id);
|
|
505
|
-
await dbRun(
|
|
506
|
-
this.db,
|
|
507
|
-
`UPDATE sessions SET ${updates.join(", ")} WHERE id = ?`,
|
|
508
|
-
values
|
|
509
|
-
);
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
/**
|
|
514
|
-
* Get session by ID
|
|
515
|
-
*/
|
|
516
|
-
async getSession(id) {
|
|
517
|
-
await this.initialize();
|
|
518
|
-
const rows = await dbAll(
|
|
519
|
-
this.db,
|
|
520
|
-
`SELECT * FROM sessions WHERE id = ?`,
|
|
521
|
-
[id]
|
|
522
|
-
);
|
|
523
|
-
if (rows.length === 0)
|
|
524
|
-
return null;
|
|
525
|
-
const row = rows[0];
|
|
526
|
-
return {
|
|
527
|
-
id: row.id,
|
|
528
|
-
startedAt: toDate(row.started_at),
|
|
529
|
-
endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
|
|
530
|
-
projectPath: row.project_path,
|
|
531
|
-
summary: row.summary,
|
|
532
|
-
tags: row.tags ? JSON.parse(row.tags) : void 0
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
/**
|
|
536
|
-
* Add to embedding outbox (Single-Writer Pattern)
|
|
537
|
-
*/
|
|
538
|
-
async enqueueForEmbedding(eventId, content) {
|
|
539
|
-
await this.initialize();
|
|
540
|
-
const id = randomUUID();
|
|
541
|
-
await dbRun(
|
|
542
|
-
this.db,
|
|
543
|
-
`INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
|
|
544
|
-
VALUES (?, ?, ?, 'pending', 0)`,
|
|
545
|
-
[id, eventId, content]
|
|
546
|
-
);
|
|
547
|
-
return id;
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* Get pending outbox items
|
|
551
|
-
*/
|
|
552
|
-
async getPendingOutboxItems(limit = 32) {
|
|
553
|
-
await this.initialize();
|
|
554
|
-
const pending = await dbAll(
|
|
555
|
-
this.db,
|
|
556
|
-
`SELECT * FROM embedding_outbox
|
|
557
|
-
WHERE status = 'pending'
|
|
558
|
-
ORDER BY created_at
|
|
559
|
-
LIMIT ?`,
|
|
560
|
-
[limit]
|
|
561
|
-
);
|
|
562
|
-
if (pending.length === 0)
|
|
563
|
-
return [];
|
|
564
|
-
const ids = pending.map((r) => r.id);
|
|
565
|
-
const placeholders = ids.map(() => "?").join(",");
|
|
566
|
-
await dbRun(
|
|
567
|
-
this.db,
|
|
568
|
-
`UPDATE embedding_outbox SET status = 'processing' WHERE id IN (${placeholders})`,
|
|
569
|
-
ids
|
|
570
|
-
);
|
|
571
|
-
return pending.map((row) => ({
|
|
572
|
-
id: row.id,
|
|
573
|
-
eventId: row.event_id,
|
|
574
|
-
content: row.content,
|
|
575
|
-
status: "processing",
|
|
576
|
-
retryCount: row.retry_count,
|
|
577
|
-
createdAt: toDate(row.created_at),
|
|
578
|
-
errorMessage: row.error_message
|
|
579
|
-
}));
|
|
580
|
-
}
|
|
581
|
-
/**
|
|
582
|
-
* Mark outbox items as done
|
|
583
|
-
*/
|
|
584
|
-
async completeOutboxItems(ids) {
|
|
585
|
-
if (ids.length === 0)
|
|
586
|
-
return;
|
|
587
|
-
const placeholders = ids.map(() => "?").join(",");
|
|
588
|
-
await dbRun(
|
|
589
|
-
this.db,
|
|
590
|
-
`DELETE FROM embedding_outbox WHERE id IN (${placeholders})`,
|
|
591
|
-
ids
|
|
592
|
-
);
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* Mark outbox items as failed
|
|
596
|
-
*/
|
|
597
|
-
async failOutboxItems(ids, error) {
|
|
598
|
-
if (ids.length === 0)
|
|
599
|
-
return;
|
|
600
|
-
const placeholders = ids.map(() => "?").join(",");
|
|
601
|
-
await dbRun(
|
|
602
|
-
this.db,
|
|
603
|
-
`UPDATE embedding_outbox
|
|
604
|
-
SET status = CASE WHEN retry_count >= 3 THEN 'failed' ELSE 'pending' END,
|
|
605
|
-
retry_count = retry_count + 1,
|
|
606
|
-
error_message = ?
|
|
607
|
-
WHERE id IN (${placeholders})`,
|
|
608
|
-
[error, ...ids]
|
|
609
|
-
);
|
|
610
|
-
}
|
|
611
|
-
/**
|
|
612
|
-
* Update memory level
|
|
613
|
-
*/
|
|
614
|
-
async updateMemoryLevel(eventId, level) {
|
|
615
|
-
await this.initialize();
|
|
616
|
-
await dbRun(
|
|
617
|
-
this.db,
|
|
618
|
-
`UPDATE memory_levels SET level = ?, promoted_at = CURRENT_TIMESTAMP WHERE event_id = ?`,
|
|
619
|
-
[level, eventId]
|
|
620
|
-
);
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* Get memory level statistics
|
|
624
|
-
*/
|
|
625
|
-
async getLevelStats() {
|
|
626
|
-
await this.initialize();
|
|
627
|
-
const rows = await dbAll(
|
|
628
|
-
this.db,
|
|
629
|
-
`SELECT level, COUNT(*) as count FROM memory_levels GROUP BY level`
|
|
630
|
-
);
|
|
631
|
-
return rows;
|
|
632
|
-
}
|
|
633
|
-
/**
|
|
634
|
-
* Get events by memory level
|
|
635
|
-
*/
|
|
636
|
-
async getEventsByLevel(level, options) {
|
|
637
|
-
await this.initialize();
|
|
638
|
-
const limit = options?.limit || 50;
|
|
639
|
-
const offset = options?.offset || 0;
|
|
640
|
-
const rows = await dbAll(
|
|
641
|
-
this.db,
|
|
642
|
-
`SELECT e.* FROM events e
|
|
643
|
-
INNER JOIN memory_levels ml ON e.id = ml.event_id
|
|
644
|
-
WHERE ml.level = ?
|
|
645
|
-
ORDER BY e.timestamp DESC
|
|
646
|
-
LIMIT ? OFFSET ?`,
|
|
647
|
-
[level, limit, offset]
|
|
648
|
-
);
|
|
649
|
-
return rows.map((row) => this.rowToEvent(row));
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
652
|
-
* Get memory level for a specific event
|
|
653
|
-
*/
|
|
654
|
-
async getEventLevel(eventId) {
|
|
655
|
-
await this.initialize();
|
|
656
|
-
const rows = await dbAll(
|
|
657
|
-
this.db,
|
|
658
|
-
`SELECT level FROM memory_levels WHERE event_id = ?`,
|
|
659
|
-
[eventId]
|
|
660
|
-
);
|
|
661
|
-
return rows.length > 0 ? rows[0].level : null;
|
|
662
|
-
}
|
|
663
|
-
// ============================================================
|
|
664
|
-
// Endless Mode Helper Methods
|
|
665
|
-
// ============================================================
|
|
666
|
-
/**
|
|
667
|
-
* Get database instance for Endless Mode stores
|
|
668
|
-
*/
|
|
669
|
-
getDatabase() {
|
|
670
|
-
return this.db;
|
|
671
|
-
}
|
|
672
|
-
/**
|
|
673
|
-
* Get config value for endless mode
|
|
674
|
-
*/
|
|
675
|
-
async getEndlessConfig(key) {
|
|
676
|
-
await this.initialize();
|
|
677
|
-
const rows = await dbAll(
|
|
678
|
-
this.db,
|
|
679
|
-
`SELECT value FROM endless_config WHERE key = ?`,
|
|
680
|
-
[key]
|
|
681
|
-
);
|
|
682
|
-
if (rows.length === 0)
|
|
683
|
-
return null;
|
|
684
|
-
return JSON.parse(rows[0].value);
|
|
685
|
-
}
|
|
686
|
-
/**
|
|
687
|
-
* Set config value for endless mode
|
|
688
|
-
*/
|
|
689
|
-
async setEndlessConfig(key, value) {
|
|
690
|
-
await this.initialize();
|
|
691
|
-
await dbRun(
|
|
692
|
-
this.db,
|
|
693
|
-
`INSERT OR REPLACE INTO endless_config (key, value, updated_at)
|
|
694
|
-
VALUES (?, ?, CURRENT_TIMESTAMP)`,
|
|
695
|
-
[key, JSON.stringify(value)]
|
|
696
|
-
);
|
|
697
|
-
}
|
|
698
|
-
/**
|
|
699
|
-
* Get all sessions
|
|
700
|
-
*/
|
|
701
|
-
async getAllSessions() {
|
|
702
|
-
await this.initialize();
|
|
703
|
-
const rows = await dbAll(
|
|
704
|
-
this.db,
|
|
705
|
-
`SELECT * FROM sessions ORDER BY started_at DESC`
|
|
706
|
-
);
|
|
707
|
-
return rows.map((row) => ({
|
|
708
|
-
id: row.id,
|
|
709
|
-
startedAt: toDate(row.started_at),
|
|
710
|
-
endedAt: row.ended_at ? toDate(row.ended_at) : void 0,
|
|
711
|
-
projectPath: row.project_path,
|
|
712
|
-
summary: row.summary,
|
|
713
|
-
tags: row.tags ? JSON.parse(row.tags) : void 0
|
|
714
|
-
}));
|
|
715
|
-
}
|
|
716
|
-
/**
|
|
717
|
-
* Increment access count for events (stub for compatibility)
|
|
718
|
-
*/
|
|
719
|
-
async incrementAccessCount(eventIds) {
|
|
720
|
-
return Promise.resolve();
|
|
721
|
-
}
|
|
722
|
-
/**
|
|
723
|
-
* Get most accessed memories (stub for compatibility)
|
|
724
|
-
*/
|
|
725
|
-
async getMostAccessed(limit = 10) {
|
|
726
|
-
return [];
|
|
727
|
-
}
|
|
728
|
-
/**
|
|
729
|
-
* Close database connection
|
|
730
|
-
*/
|
|
731
|
-
async close() {
|
|
732
|
-
await dbClose(this.db);
|
|
15
|
+
// src/core/sqlite-event-store.ts
|
|
16
|
+
import { randomUUID } from "crypto";
|
|
17
|
+
|
|
18
|
+
// src/core/canonical-key.ts
|
|
19
|
+
import { createHash } from "crypto";
|
|
20
|
+
var MAX_KEY_LENGTH = 200;
|
|
21
|
+
function makeCanonicalKey(title, context) {
|
|
22
|
+
let normalized = title.normalize("NFKC");
|
|
23
|
+
normalized = normalized.toLowerCase();
|
|
24
|
+
normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
|
|
25
|
+
normalized = normalized.replace(/\s+/g, " ").trim();
|
|
26
|
+
let key = normalized;
|
|
27
|
+
if (context?.project) {
|
|
28
|
+
key = `${context.project}::${key}`;
|
|
733
29
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
rowToEvent(row) {
|
|
738
|
-
return {
|
|
739
|
-
id: row.id,
|
|
740
|
-
eventType: row.event_type,
|
|
741
|
-
sessionId: row.session_id,
|
|
742
|
-
timestamp: toDate(row.timestamp),
|
|
743
|
-
content: row.content,
|
|
744
|
-
canonicalKey: row.canonical_key,
|
|
745
|
-
dedupeKey: row.dedupe_key,
|
|
746
|
-
metadata: row.metadata ? JSON.parse(row.metadata) : void 0
|
|
747
|
-
};
|
|
30
|
+
if (key.length > MAX_KEY_LENGTH) {
|
|
31
|
+
const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
|
|
32
|
+
key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
|
|
748
33
|
}
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
34
|
+
return key;
|
|
35
|
+
}
|
|
36
|
+
function makeDedupeKey(content, sessionId) {
|
|
37
|
+
const contentHash = createHash("sha256").update(content).digest("hex");
|
|
38
|
+
return `${sessionId}:${contentHash}`;
|
|
39
|
+
}
|
|
753
40
|
|
|
754
41
|
// src/core/sqlite-wrapper.ts
|
|
755
42
|
import Database from "better-sqlite3";
|
|
@@ -1264,7 +551,7 @@ var SQLiteEventStore = class {
|
|
|
1264
551
|
isDuplicate: true
|
|
1265
552
|
};
|
|
1266
553
|
}
|
|
1267
|
-
const id =
|
|
554
|
+
const id = randomUUID();
|
|
1268
555
|
const timestamp = toSQLiteTimestamp(input.timestamp);
|
|
1269
556
|
try {
|
|
1270
557
|
const metadata = input.metadata || {};
|
|
@@ -1318,6 +605,29 @@ var SQLiteEventStore = class {
|
|
|
1318
605
|
};
|
|
1319
606
|
}
|
|
1320
607
|
}
|
|
608
|
+
/**
|
|
609
|
+
* Get session IDs that have events but no session_summary event.
|
|
610
|
+
* Used to backfill summaries for sessions that ended without Stop hook.
|
|
611
|
+
*/
|
|
612
|
+
async getSessionsWithoutSummary(currentSessionId, limit = 5) {
|
|
613
|
+
await this.initialize();
|
|
614
|
+
const rows = sqliteAll(
|
|
615
|
+
this.db,
|
|
616
|
+
`SELECT DISTINCT e.session_id
|
|
617
|
+
FROM events e
|
|
618
|
+
WHERE e.session_id != ?
|
|
619
|
+
AND e.event_type != 'session_summary'
|
|
620
|
+
AND e.session_id NOT IN (
|
|
621
|
+
SELECT DISTINCT session_id FROM events WHERE event_type = 'session_summary'
|
|
622
|
+
)
|
|
623
|
+
GROUP BY e.session_id
|
|
624
|
+
HAVING COUNT(*) >= 3
|
|
625
|
+
ORDER BY MAX(e.timestamp) DESC
|
|
626
|
+
LIMIT ?`,
|
|
627
|
+
[currentSessionId, limit]
|
|
628
|
+
);
|
|
629
|
+
return rows.map((r) => r.session_id);
|
|
630
|
+
}
|
|
1321
631
|
/**
|
|
1322
632
|
* Get events by session ID
|
|
1323
633
|
*/
|
|
@@ -1545,7 +855,7 @@ var SQLiteEventStore = class {
|
|
|
1545
855
|
*/
|
|
1546
856
|
async enqueueForEmbedding(eventId, content) {
|
|
1547
857
|
await this.initialize();
|
|
1548
|
-
const id =
|
|
858
|
+
const id = randomUUID();
|
|
1549
859
|
sqliteRun(
|
|
1550
860
|
this.db,
|
|
1551
861
|
`INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
|
|
@@ -1826,7 +1136,7 @@ var SQLiteEventStore = class {
|
|
|
1826
1136
|
if (this.readOnly)
|
|
1827
1137
|
return;
|
|
1828
1138
|
await this.initialize();
|
|
1829
|
-
const id =
|
|
1139
|
+
const id = randomUUID();
|
|
1830
1140
|
sqliteRun(
|
|
1831
1141
|
this.db,
|
|
1832
1142
|
`INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
|
|
@@ -1904,7 +1214,8 @@ var SQLiteEventStore = class {
|
|
|
1904
1214
|
}
|
|
1905
1215
|
}
|
|
1906
1216
|
const retrievalScore = retrieval.retrieval_score || 0;
|
|
1907
|
-
const
|
|
1217
|
+
const promptNorm = Math.min(promptCountAfter / 2, 1);
|
|
1218
|
+
const helpfulnessScore = 0.4 * Math.min(retrievalScore, 1) + 0.3 * promptNorm + 0.2 * toolSuccessRatio + 0.1 * (sessionContinued ? 1 : 0);
|
|
1908
1219
|
sqliteRun(
|
|
1909
1220
|
this.db,
|
|
1910
1221
|
`UPDATE memory_helpfulness
|
|
@@ -2047,7 +1358,7 @@ var SQLiteEventStore = class {
|
|
|
2047
1358
|
}
|
|
2048
1359
|
async recordRetrievalTrace(input) {
|
|
2049
1360
|
await this.initialize();
|
|
2050
|
-
const traceId =
|
|
1361
|
+
const traceId = randomUUID();
|
|
2051
1362
|
sqliteRun(
|
|
2052
1363
|
this.db,
|
|
2053
1364
|
`INSERT INTO retrieval_traces (
|
|
@@ -2301,169 +1612,6 @@ var SQLiteEventStore = class {
|
|
|
2301
1612
|
}
|
|
2302
1613
|
};
|
|
2303
1614
|
|
|
2304
|
-
// src/core/sync-worker.ts
|
|
2305
|
-
var DEFAULT_CONFIG = {
|
|
2306
|
-
intervalMs: 3e4,
|
|
2307
|
-
batchSize: 500,
|
|
2308
|
-
maxRetries: 3,
|
|
2309
|
-
retryDelayMs: 5e3
|
|
2310
|
-
};
|
|
2311
|
-
var SyncWorker = class {
|
|
2312
|
-
constructor(sqliteStore, duckdbStore, config) {
|
|
2313
|
-
this.sqliteStore = sqliteStore;
|
|
2314
|
-
this.duckdbStore = duckdbStore;
|
|
2315
|
-
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
2316
|
-
}
|
|
2317
|
-
config;
|
|
2318
|
-
intervalHandle = null;
|
|
2319
|
-
running = false;
|
|
2320
|
-
stats = {
|
|
2321
|
-
lastSyncAt: null,
|
|
2322
|
-
eventsSynced: 0,
|
|
2323
|
-
sessionsSynced: 0,
|
|
2324
|
-
errors: 0,
|
|
2325
|
-
status: "idle"
|
|
2326
|
-
};
|
|
2327
|
-
/**
|
|
2328
|
-
* Start the sync worker
|
|
2329
|
-
*/
|
|
2330
|
-
start() {
|
|
2331
|
-
if (this.running)
|
|
2332
|
-
return;
|
|
2333
|
-
this.running = true;
|
|
2334
|
-
this.stats.status = "idle";
|
|
2335
|
-
this.syncNow().catch((err) => {
|
|
2336
|
-
console.error("[SyncWorker] Initial sync failed:", err);
|
|
2337
|
-
});
|
|
2338
|
-
this.intervalHandle = setInterval(() => {
|
|
2339
|
-
this.syncNow().catch((err) => {
|
|
2340
|
-
console.error("[SyncWorker] Periodic sync failed:", err);
|
|
2341
|
-
});
|
|
2342
|
-
}, this.config.intervalMs);
|
|
2343
|
-
}
|
|
2344
|
-
/**
|
|
2345
|
-
* Stop the sync worker
|
|
2346
|
-
*/
|
|
2347
|
-
stop() {
|
|
2348
|
-
this.running = false;
|
|
2349
|
-
this.stats.status = "stopped";
|
|
2350
|
-
if (this.intervalHandle) {
|
|
2351
|
-
clearInterval(this.intervalHandle);
|
|
2352
|
-
this.intervalHandle = null;
|
|
2353
|
-
}
|
|
2354
|
-
}
|
|
2355
|
-
/**
|
|
2356
|
-
* Trigger immediate sync
|
|
2357
|
-
*/
|
|
2358
|
-
async syncNow() {
|
|
2359
|
-
if (this.stats.status === "syncing") {
|
|
2360
|
-
return;
|
|
2361
|
-
}
|
|
2362
|
-
this.stats.status = "syncing";
|
|
2363
|
-
try {
|
|
2364
|
-
await this.syncEvents();
|
|
2365
|
-
await this.syncSessions();
|
|
2366
|
-
this.stats.lastSyncAt = /* @__PURE__ */ new Date();
|
|
2367
|
-
this.stats.status = "idle";
|
|
2368
|
-
} catch (error) {
|
|
2369
|
-
this.stats.errors++;
|
|
2370
|
-
this.stats.status = "error";
|
|
2371
|
-
throw error;
|
|
2372
|
-
}
|
|
2373
|
-
}
|
|
2374
|
-
/**
|
|
2375
|
-
* Sync events from SQLite to DuckDB
|
|
2376
|
-
*/
|
|
2377
|
-
async syncEvents() {
|
|
2378
|
-
const targetName = "duckdb_analytics";
|
|
2379
|
-
const position = await this.sqliteStore.getSyncPosition(targetName);
|
|
2380
|
-
const lastTimestamp = position.lastTimestamp || "1970-01-01T00:00:00.000Z";
|
|
2381
|
-
let hasMore = true;
|
|
2382
|
-
let totalSynced = 0;
|
|
2383
|
-
while (hasMore) {
|
|
2384
|
-
const events = await this.sqliteStore.getEventsSince(lastTimestamp, this.config.batchSize);
|
|
2385
|
-
if (events.length === 0) {
|
|
2386
|
-
hasMore = false;
|
|
2387
|
-
break;
|
|
2388
|
-
}
|
|
2389
|
-
await this.retryWithBackoff(async () => {
|
|
2390
|
-
for (const event of events) {
|
|
2391
|
-
await this.insertEventToDuckDB(event);
|
|
2392
|
-
}
|
|
2393
|
-
});
|
|
2394
|
-
totalSynced += events.length;
|
|
2395
|
-
const lastEvent = events[events.length - 1];
|
|
2396
|
-
await this.sqliteStore.updateSyncPosition(
|
|
2397
|
-
targetName,
|
|
2398
|
-
lastEvent.id,
|
|
2399
|
-
lastEvent.timestamp.toISOString()
|
|
2400
|
-
);
|
|
2401
|
-
hasMore = events.length === this.config.batchSize;
|
|
2402
|
-
}
|
|
2403
|
-
this.stats.eventsSynced += totalSynced;
|
|
2404
|
-
}
|
|
2405
|
-
/**
|
|
2406
|
-
* Sync sessions from SQLite to DuckDB
|
|
2407
|
-
*/
|
|
2408
|
-
async syncSessions() {
|
|
2409
|
-
const sessions = await this.sqliteStore.getAllSessions();
|
|
2410
|
-
for (const session of sessions) {
|
|
2411
|
-
await this.retryWithBackoff(async () => {
|
|
2412
|
-
await this.duckdbStore.upsertSession(session);
|
|
2413
|
-
});
|
|
2414
|
-
}
|
|
2415
|
-
this.stats.sessionsSynced = sessions.length;
|
|
2416
|
-
}
|
|
2417
|
-
/**
|
|
2418
|
-
* Insert a single event into DuckDB
|
|
2419
|
-
*/
|
|
2420
|
-
async insertEventToDuckDB(event) {
|
|
2421
|
-
await this.duckdbStore.append({
|
|
2422
|
-
eventType: event.eventType,
|
|
2423
|
-
sessionId: event.sessionId,
|
|
2424
|
-
timestamp: event.timestamp,
|
|
2425
|
-
content: event.content,
|
|
2426
|
-
metadata: event.metadata
|
|
2427
|
-
});
|
|
2428
|
-
}
|
|
2429
|
-
/**
|
|
2430
|
-
* Retry operation with exponential backoff
|
|
2431
|
-
*/
|
|
2432
|
-
async retryWithBackoff(fn) {
|
|
2433
|
-
let lastError = null;
|
|
2434
|
-
for (let attempt = 0; attempt < this.config.maxRetries; attempt++) {
|
|
2435
|
-
try {
|
|
2436
|
-
return await fn();
|
|
2437
|
-
} catch (error) {
|
|
2438
|
-
lastError = error instanceof Error ? error : new Error(String(error));
|
|
2439
|
-
if (attempt < this.config.maxRetries - 1) {
|
|
2440
|
-
const delay = this.config.retryDelayMs * Math.pow(2, attempt);
|
|
2441
|
-
await this.sleep(delay);
|
|
2442
|
-
}
|
|
2443
|
-
}
|
|
2444
|
-
}
|
|
2445
|
-
throw lastError;
|
|
2446
|
-
}
|
|
2447
|
-
/**
|
|
2448
|
-
* Sleep utility
|
|
2449
|
-
*/
|
|
2450
|
-
sleep(ms) {
|
|
2451
|
-
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
2452
|
-
}
|
|
2453
|
-
/**
|
|
2454
|
-
* Get sync statistics
|
|
2455
|
-
*/
|
|
2456
|
-
getStats() {
|
|
2457
|
-
return { ...this.stats };
|
|
2458
|
-
}
|
|
2459
|
-
/**
|
|
2460
|
-
* Check if worker is running
|
|
2461
|
-
*/
|
|
2462
|
-
isRunning() {
|
|
2463
|
-
return this.running;
|
|
2464
|
-
}
|
|
2465
|
-
};
|
|
2466
|
-
|
|
2467
1615
|
// src/core/vector-store.ts
|
|
2468
1616
|
import * as lancedb from "@lancedb/lancedb";
|
|
2469
1617
|
var VectorStore = class {
|
|
@@ -2751,8 +1899,34 @@ function getDefaultEmbedder() {
|
|
|
2751
1899
|
return defaultEmbedder;
|
|
2752
1900
|
}
|
|
2753
1901
|
|
|
1902
|
+
// src/core/db-wrapper.ts
|
|
1903
|
+
import BetterSqlite3 from "better-sqlite3";
|
|
1904
|
+
function toDate(value) {
|
|
1905
|
+
if (value instanceof Date)
|
|
1906
|
+
return value;
|
|
1907
|
+
if (typeof value === "string")
|
|
1908
|
+
return new Date(value);
|
|
1909
|
+
if (typeof value === "number")
|
|
1910
|
+
return new Date(value);
|
|
1911
|
+
return new Date(String(value));
|
|
1912
|
+
}
|
|
1913
|
+
function createDatabase(dbPath, options) {
|
|
1914
|
+
return new BetterSqlite3(dbPath, { readonly: options?.readOnly });
|
|
1915
|
+
}
|
|
1916
|
+
function dbRun(db, sql, params = []) {
|
|
1917
|
+
db.prepare(sql).run(...params);
|
|
1918
|
+
return Promise.resolve();
|
|
1919
|
+
}
|
|
1920
|
+
function dbAll(db, sql, params = []) {
|
|
1921
|
+
return Promise.resolve(db.prepare(sql).all(...params));
|
|
1922
|
+
}
|
|
1923
|
+
function dbClose(db) {
|
|
1924
|
+
db.close();
|
|
1925
|
+
return Promise.resolve();
|
|
1926
|
+
}
|
|
1927
|
+
|
|
2754
1928
|
// src/core/vector-outbox.ts
|
|
2755
|
-
var
|
|
1929
|
+
var DEFAULT_CONFIG = {
|
|
2756
1930
|
embeddingVersion: "v1",
|
|
2757
1931
|
maxRetries: 3,
|
|
2758
1932
|
stuckThresholdMs: 5 * 60 * 1e3,
|
|
@@ -2761,7 +1935,7 @@ var DEFAULT_CONFIG2 = {
|
|
|
2761
1935
|
};
|
|
2762
1936
|
|
|
2763
1937
|
// src/core/vector-worker.ts
|
|
2764
|
-
var
|
|
1938
|
+
var DEFAULT_CONFIG2 = {
|
|
2765
1939
|
batchSize: 32,
|
|
2766
1940
|
pollIntervalMs: 1e3,
|
|
2767
1941
|
maxRetries: 3
|
|
@@ -2778,7 +1952,7 @@ var VectorWorker = class {
|
|
|
2778
1952
|
this.eventStore = eventStore;
|
|
2779
1953
|
this.vectorStore = vectorStore;
|
|
2780
1954
|
this.embedder = embedder;
|
|
2781
|
-
this.config = { ...
|
|
1955
|
+
this.config = { ...DEFAULT_CONFIG2, ...config };
|
|
2782
1956
|
}
|
|
2783
1957
|
/**
|
|
2784
1958
|
* Start the worker polling loop
|
|
@@ -2899,7 +2073,7 @@ function createVectorWorker(eventStore, vectorStore, embedder, config) {
|
|
|
2899
2073
|
}
|
|
2900
2074
|
|
|
2901
2075
|
// src/core/matcher.ts
|
|
2902
|
-
var
|
|
2076
|
+
var DEFAULT_CONFIG3 = {
|
|
2903
2077
|
weights: {
|
|
2904
2078
|
semanticSimilarity: 0.4,
|
|
2905
2079
|
ftsScore: 0.25,
|
|
@@ -2914,9 +2088,9 @@ var Matcher = class {
|
|
|
2914
2088
|
config;
|
|
2915
2089
|
constructor(config = {}) {
|
|
2916
2090
|
this.config = {
|
|
2917
|
-
...
|
|
2091
|
+
...DEFAULT_CONFIG3,
|
|
2918
2092
|
...config,
|
|
2919
|
-
weights: { ...
|
|
2093
|
+
weights: { ...DEFAULT_CONFIG3.weights, ...config.weights }
|
|
2920
2094
|
};
|
|
2921
2095
|
}
|
|
2922
2096
|
/**
|
|
@@ -3906,7 +3080,7 @@ function createSharedEventStore(dbPath) {
|
|
|
3906
3080
|
}
|
|
3907
3081
|
|
|
3908
3082
|
// src/core/shared-store.ts
|
|
3909
|
-
import { randomUUID as
|
|
3083
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
3910
3084
|
var SharedStore = class {
|
|
3911
3085
|
constructor(sharedEventStore) {
|
|
3912
3086
|
this.sharedEventStore = sharedEventStore;
|
|
@@ -3918,7 +3092,7 @@ var SharedStore = class {
|
|
|
3918
3092
|
* Promote a verified troubleshooting entry to shared storage
|
|
3919
3093
|
*/
|
|
3920
3094
|
async promoteEntry(input) {
|
|
3921
|
-
const entryId =
|
|
3095
|
+
const entryId = randomUUID2();
|
|
3922
3096
|
await dbRun(
|
|
3923
3097
|
this.db,
|
|
3924
3098
|
`INSERT INTO shared_troubleshooting (
|
|
@@ -4283,7 +3457,7 @@ function createSharedVectorStore(dbPath) {
|
|
|
4283
3457
|
}
|
|
4284
3458
|
|
|
4285
3459
|
// src/core/shared-promoter.ts
|
|
4286
|
-
import { randomUUID as
|
|
3460
|
+
import { randomUUID as randomUUID3 } from "crypto";
|
|
4287
3461
|
var SharedPromoter = class {
|
|
4288
3462
|
constructor(sharedStore, sharedVectorStore, embedder, config) {
|
|
4289
3463
|
this.sharedStore = sharedStore;
|
|
@@ -4351,7 +3525,7 @@ var SharedPromoter = class {
|
|
|
4351
3525
|
const embeddingContent = this.createEmbeddingContent(input);
|
|
4352
3526
|
const embedding = await this.embedder.embed(embeddingContent);
|
|
4353
3527
|
await this.sharedVectorStore.upsert({
|
|
4354
|
-
id:
|
|
3528
|
+
id: randomUUID3(),
|
|
4355
3529
|
entryId,
|
|
4356
3530
|
entryType: "troubleshooting",
|
|
4357
3531
|
content: embeddingContent,
|
|
@@ -4491,7 +3665,7 @@ function createToolObservationEmbedding(toolName, metadata, success) {
|
|
|
4491
3665
|
}
|
|
4492
3666
|
|
|
4493
3667
|
// src/core/working-set-store.ts
|
|
4494
|
-
import { randomUUID as
|
|
3668
|
+
import { randomUUID as randomUUID4 } from "crypto";
|
|
4495
3669
|
var WorkingSetStore = class {
|
|
4496
3670
|
constructor(eventStore, config) {
|
|
4497
3671
|
this.eventStore = eventStore;
|
|
@@ -4512,7 +3686,7 @@ var WorkingSetStore = class {
|
|
|
4512
3686
|
`INSERT OR REPLACE INTO working_set (id, event_id, added_at, relevance_score, topics, expires_at)
|
|
4513
3687
|
VALUES (?, ?, CURRENT_TIMESTAMP, ?, ?, ?)`,
|
|
4514
3688
|
[
|
|
4515
|
-
|
|
3689
|
+
randomUUID4(),
|
|
4516
3690
|
eventId,
|
|
4517
3691
|
relevanceScore,
|
|
4518
3692
|
JSON.stringify(topics || []),
|
|
@@ -4696,7 +3870,7 @@ function createWorkingSetStore(eventStore, config) {
|
|
|
4696
3870
|
}
|
|
4697
3871
|
|
|
4698
3872
|
// src/core/consolidated-store.ts
|
|
4699
|
-
import { randomUUID as
|
|
3873
|
+
import { randomUUID as randomUUID5 } from "crypto";
|
|
4700
3874
|
var ConsolidatedStore = class {
|
|
4701
3875
|
constructor(eventStore) {
|
|
4702
3876
|
this.eventStore = eventStore;
|
|
@@ -4708,7 +3882,7 @@ var ConsolidatedStore = class {
|
|
|
4708
3882
|
* Create a new consolidated memory
|
|
4709
3883
|
*/
|
|
4710
3884
|
async create(input) {
|
|
4711
|
-
const memoryId =
|
|
3885
|
+
const memoryId = randomUUID5();
|
|
4712
3886
|
await dbRun(
|
|
4713
3887
|
this.db,
|
|
4714
3888
|
`INSERT INTO consolidated_memories
|
|
@@ -4838,7 +4012,7 @@ var ConsolidatedStore = class {
|
|
|
4838
4012
|
* Create a long-term rule promoted from stable summaries
|
|
4839
4013
|
*/
|
|
4840
4014
|
async createRule(input) {
|
|
4841
|
-
const ruleId =
|
|
4015
|
+
const ruleId = randomUUID5();
|
|
4842
4016
|
await dbRun(
|
|
4843
4017
|
this.db,
|
|
4844
4018
|
`INSERT INTO consolidated_rules
|
|
@@ -5360,7 +4534,7 @@ function createConsolidationWorker(workingSetStore, consolidatedStore, config) {
|
|
|
5360
4534
|
}
|
|
5361
4535
|
|
|
5362
4536
|
// src/core/continuity-manager.ts
|
|
5363
|
-
import { randomUUID as
|
|
4537
|
+
import { randomUUID as randomUUID6 } from "crypto";
|
|
5364
4538
|
var ContinuityManager = class {
|
|
5365
4539
|
constructor(eventStore, config) {
|
|
5366
4540
|
this.eventStore = eventStore;
|
|
@@ -5514,7 +4688,7 @@ var ContinuityManager = class {
|
|
|
5514
4688
|
`INSERT INTO continuity_log
|
|
5515
4689
|
(log_id, from_context_id, to_context_id, continuity_score, transition_type, created_at)
|
|
5516
4690
|
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
|
|
5517
|
-
[
|
|
4691
|
+
[randomUUID6(), previous.id, current.id, score, type]
|
|
5518
4692
|
);
|
|
5519
4693
|
}
|
|
5520
4694
|
/**
|
|
@@ -5623,7 +4797,7 @@ function createContinuityManager(eventStore, config) {
|
|
|
5623
4797
|
}
|
|
5624
4798
|
|
|
5625
4799
|
// src/core/graduation-worker.ts
|
|
5626
|
-
var
|
|
4800
|
+
var DEFAULT_CONFIG4 = {
|
|
5627
4801
|
evaluationIntervalMs: 3e5,
|
|
5628
4802
|
// 5 minutes
|
|
5629
4803
|
batchSize: 50,
|
|
@@ -5631,7 +4805,7 @@ var DEFAULT_CONFIG5 = {
|
|
|
5631
4805
|
// 1 hour cooldown between evaluations
|
|
5632
4806
|
};
|
|
5633
4807
|
var GraduationWorker = class {
|
|
5634
|
-
constructor(eventStore, graduation, config =
|
|
4808
|
+
constructor(eventStore, graduation, config = DEFAULT_CONFIG4) {
|
|
5635
4809
|
this.eventStore = eventStore;
|
|
5636
4810
|
this.graduation = graduation;
|
|
5637
4811
|
this.config = config;
|
|
@@ -5739,7 +4913,7 @@ function createGraduationWorker(eventStore, graduation, config) {
|
|
|
5739
4913
|
return new GraduationWorker(
|
|
5740
4914
|
eventStore,
|
|
5741
4915
|
graduation,
|
|
5742
|
-
{ ...
|
|
4916
|
+
{ ...DEFAULT_CONFIG4, ...config }
|
|
5743
4917
|
);
|
|
5744
4918
|
}
|
|
5745
4919
|
|
|
@@ -5952,9 +5126,6 @@ function getSessionProject(sessionId) {
|
|
|
5952
5126
|
var MemoryService = class {
|
|
5953
5127
|
// Primary store: SQLite (WAL mode) - for hooks, always available
|
|
5954
5128
|
sqliteStore;
|
|
5955
|
-
// Analytics store: DuckDB - for server reads (optional, synced from SQLite)
|
|
5956
|
-
analyticsStore;
|
|
5957
|
-
syncWorker = null;
|
|
5958
5129
|
vectorStore;
|
|
5959
5130
|
embedder;
|
|
5960
5131
|
matcher;
|
|
@@ -6003,24 +5174,6 @@ var MemoryService = class {
|
|
|
6003
5174
|
markdownMirrorRoot: storagePath
|
|
6004
5175
|
}
|
|
6005
5176
|
);
|
|
6006
|
-
const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
|
|
6007
|
-
if (!analyticsEnabled) {
|
|
6008
|
-
this.analyticsStore = null;
|
|
6009
|
-
} else if (this.readOnly) {
|
|
6010
|
-
try {
|
|
6011
|
-
this.analyticsStore = new EventStore(
|
|
6012
|
-
path3.join(storagePath, "analytics.duckdb"),
|
|
6013
|
-
{ readOnly: true }
|
|
6014
|
-
);
|
|
6015
|
-
} catch {
|
|
6016
|
-
this.analyticsStore = null;
|
|
6017
|
-
}
|
|
6018
|
-
} else {
|
|
6019
|
-
this.analyticsStore = new EventStore(
|
|
6020
|
-
path3.join(storagePath, "analytics.duckdb"),
|
|
6021
|
-
{ readOnly: false }
|
|
6022
|
-
);
|
|
6023
|
-
}
|
|
6024
5177
|
this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
|
|
6025
5178
|
const embeddingModel = config.embeddingModel || process.env.CLAUDE_MEMORY_EMBEDDING_MODEL;
|
|
6026
5179
|
this.embedder = embeddingModel ? new Embedder(embeddingModel) : getDefaultEmbedder();
|
|
@@ -6046,13 +5199,6 @@ var MemoryService = class {
|
|
|
6046
5199
|
this.initialized = true;
|
|
6047
5200
|
return;
|
|
6048
5201
|
}
|
|
6049
|
-
if (this.analyticsStore) {
|
|
6050
|
-
try {
|
|
6051
|
-
await this.analyticsStore.initialize();
|
|
6052
|
-
} catch (error) {
|
|
6053
|
-
console.warn("[MemoryService] Analytics store (DuckDB) initialization failed, using SQLite for reads:", error);
|
|
6054
|
-
}
|
|
6055
|
-
}
|
|
6056
5202
|
await this.vectorStore.initialize();
|
|
6057
5203
|
await this.embedder.initialize();
|
|
6058
5204
|
if (!this.readOnly) {
|
|
@@ -6069,14 +5215,6 @@ var MemoryService = class {
|
|
|
6069
5215
|
this.graduation
|
|
6070
5216
|
);
|
|
6071
5217
|
this.graduationWorker.start();
|
|
6072
|
-
if (this.analyticsStore) {
|
|
6073
|
-
this.syncWorker = new SyncWorker(
|
|
6074
|
-
this.sqliteStore,
|
|
6075
|
-
this.analyticsStore,
|
|
6076
|
-
{ intervalMs: 3e4, batchSize: 500 }
|
|
6077
|
-
);
|
|
6078
|
-
this.syncWorker.start();
|
|
6079
|
-
}
|
|
6080
5218
|
}
|
|
6081
5219
|
const savedMode = await this.sqliteStore.getEndlessConfig("mode");
|
|
6082
5220
|
if (savedMode === "endless") {
|
|
@@ -6273,6 +5411,57 @@ var MemoryService = class {
|
|
|
6273
5411
|
}
|
|
6274
5412
|
);
|
|
6275
5413
|
}
|
|
5414
|
+
/**
|
|
5415
|
+
* Backfill session summaries for recent sessions that are missing them.
|
|
5416
|
+
* Called from session-start hook to catch sessions that ended without Stop hook.
|
|
5417
|
+
*/
|
|
5418
|
+
async backfillMissingSummaries(currentSessionId, limit = 5) {
|
|
5419
|
+
await this.initialize();
|
|
5420
|
+
const recentSessionIds = await this.sqliteStore.getSessionsWithoutSummary(currentSessionId, limit);
|
|
5421
|
+
for (const sid of recentSessionIds) {
|
|
5422
|
+
try {
|
|
5423
|
+
await this.generateSessionSummary(sid);
|
|
5424
|
+
} catch {
|
|
5425
|
+
}
|
|
5426
|
+
}
|
|
5427
|
+
}
|
|
5428
|
+
/**
|
|
5429
|
+
* Generate a rule-based session summary from stored events.
|
|
5430
|
+
* Called at session end (Stop hook) when no LLM-generated summary exists.
|
|
5431
|
+
* Skips if a summary already exists for this session.
|
|
5432
|
+
*/
|
|
5433
|
+
async generateSessionSummary(sessionId) {
|
|
5434
|
+
await this.initialize();
|
|
5435
|
+
const events = await this.sqliteStore.getSessionEvents(sessionId);
|
|
5436
|
+
if (events.length < 3)
|
|
5437
|
+
return;
|
|
5438
|
+
const hasSummary = events.some((e) => e.eventType === "session_summary");
|
|
5439
|
+
if (hasSummary)
|
|
5440
|
+
return;
|
|
5441
|
+
const prompts = events.filter((e) => e.eventType === "user_prompt");
|
|
5442
|
+
const toolObs = events.filter((e) => e.eventType === "tool_observation");
|
|
5443
|
+
const toolNames = [...new Set(
|
|
5444
|
+
toolObs.map((e) => e.metadata?.toolName).filter(Boolean)
|
|
5445
|
+
)];
|
|
5446
|
+
const errorObs = toolObs.filter((e) => {
|
|
5447
|
+
const meta = e.metadata;
|
|
5448
|
+
return meta?.exitCode !== void 0 && meta.exitCode !== 0;
|
|
5449
|
+
});
|
|
5450
|
+
const datePart = events[0].timestamp.toISOString().split("T")[0];
|
|
5451
|
+
const parts = [`[${datePart}] ${prompts.length}\uD134 \uC138\uC158.`];
|
|
5452
|
+
if (prompts.length > 0) {
|
|
5453
|
+
const firstPrompt = prompts[0].content.slice(0, 120).replace(/\n/g, " ");
|
|
5454
|
+
parts.push(`\uC8FC\uC694 \uC791\uC5C5: ${firstPrompt}`);
|
|
5455
|
+
}
|
|
5456
|
+
if (toolNames.length > 0) {
|
|
5457
|
+
parts.push(`\uC0AC\uC6A9 \uD234: ${toolNames.slice(0, 6).join(", ")}`);
|
|
5458
|
+
}
|
|
5459
|
+
if (errorObs.length > 0) {
|
|
5460
|
+
parts.push(`\uC624\uB958 ${errorObs.length}\uAC74 \uBC1C\uC0DD`);
|
|
5461
|
+
}
|
|
5462
|
+
const summary = parts.join(". ");
|
|
5463
|
+
await this.storeSessionSummary(sessionId, summary, { generated: "rule-based", eventCount: events.length });
|
|
5464
|
+
}
|
|
6276
5465
|
/**
|
|
6277
5466
|
* Store a tool observation
|
|
6278
5467
|
*/
|
|
@@ -6808,6 +5997,7 @@ var MemoryService = class {
|
|
|
6808
5997
|
await this.initialize();
|
|
6809
5998
|
await this.sqliteStore.recordRetrievalTrace({
|
|
6810
5999
|
...input,
|
|
6000
|
+
projectHash: this.projectHash || void 0,
|
|
6811
6001
|
candidateDetails: [],
|
|
6812
6002
|
selectedDetails: [],
|
|
6813
6003
|
fallbackTrace: []
|
|
@@ -7098,16 +6288,10 @@ var MemoryService = class {
|
|
|
7098
6288
|
if (this.vectorWorker) {
|
|
7099
6289
|
this.vectorWorker.stop();
|
|
7100
6290
|
}
|
|
7101
|
-
if (this.syncWorker) {
|
|
7102
|
-
this.syncWorker.stop();
|
|
7103
|
-
}
|
|
7104
6291
|
if (this.sharedEventStore) {
|
|
7105
6292
|
await this.sharedEventStore.close();
|
|
7106
6293
|
}
|
|
7107
6294
|
await this.sqliteStore.close();
|
|
7108
|
-
if (this.analyticsStore) {
|
|
7109
|
-
await this.analyticsStore.close();
|
|
7110
|
-
}
|
|
7111
6295
|
}
|
|
7112
6296
|
/**
|
|
7113
6297
|
* Expand ~ to home directory
|