claude-memory-layer 1.0.11 → 1.0.12
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/AGENTS.md +60 -0
- package/README.md +166 -2
- package/bootstrap-kb/decisions/decisions.md +244 -0
- package/bootstrap-kb/glossary/glossary.md +46 -0
- package/bootstrap-kb/modules/.claude-plugin.md +22 -0
- package/bootstrap-kb/modules/agents.md.md +15 -0
- package/bootstrap-kb/modules/claude.md.md +15 -0
- package/bootstrap-kb/modules/context.md.md +15 -0
- package/bootstrap-kb/modules/docs.md +18 -0
- package/bootstrap-kb/modules/handoff.md.md +15 -0
- package/bootstrap-kb/modules/package-lock.json.md +15 -0
- package/bootstrap-kb/modules/package.json.md +15 -0
- package/bootstrap-kb/modules/plan.md.md +15 -0
- package/bootstrap-kb/modules/readme.md.md +15 -0
- package/bootstrap-kb/modules/scripts.md +26 -0
- package/bootstrap-kb/modules/spec.md.md +15 -0
- package/bootstrap-kb/modules/specs.md +20 -0
- package/bootstrap-kb/modules/src.md +51 -0
- package/bootstrap-kb/modules/tests.md +42 -0
- package/bootstrap-kb/modules/tsconfig.json.md +15 -0
- package/bootstrap-kb/modules/vitest.config.ts.md +15 -0
- package/bootstrap-kb/overview/overview.md +40 -0
- package/bootstrap-kb/sources/manifest.json +950 -0
- package/bootstrap-kb/sources/manifest.md +227 -0
- package/bootstrap-kb/timeline/timeline.md +57 -0
- package/d.sh +3 -0
- package/deploy.sh +3 -0
- package/dist/cli/index.js +2389 -286
- package/dist/cli/index.js.map +4 -4
- package/dist/core/index.js +1017 -132
- package/dist/core/index.js.map +4 -4
- package/dist/hooks/post-tool-use.js +1347 -202
- package/dist/hooks/post-tool-use.js.map +4 -4
- package/dist/hooks/session-end.js +1339 -194
- package/dist/hooks/session-end.js.map +4 -4
- package/dist/hooks/session-start.js +1343 -198
- package/dist/hooks/session-start.js.map +4 -4
- package/dist/hooks/stop.js +1351 -206
- package/dist/hooks/stop.js.map +4 -4
- package/dist/hooks/user-prompt-submit.js +1347 -202
- package/dist/hooks/user-prompt-submit.js.map +4 -4
- package/dist/server/api/index.js +1436 -211
- package/dist/server/api/index.js.map +4 -4
- package/dist/server/index.js +1445 -220
- package/dist/server/index.js.map +4 -4
- package/dist/services/memory-service.js +1345 -199
- package/dist/services/memory-service.js.map +4 -4
- package/dist/ui/app.js +69 -2
- package/dist/ui/index.html +8 -0
- package/docs/MCP_MEMORY_SERVICE_COMPARATIVE_REVIEW.md +271 -0
- package/docs/MEMU_ADOPTION.md +40 -0
- package/memory/.claude-plugin/commands/2026-02-25.md +263 -0
- package/memory/_index.md +405 -0
- package/memory/default/uncategorized/2026-02-25.md +4839 -0
- package/memory/specs/20260207-dashboard-upgrade/2026-02-25.md +142 -0
- package/memory/specs/citations-system/2026-02-25.md +1121 -0
- package/memory/specs/endless-mode/2026-02-25.md +1392 -0
- package/memory/specs/entity-edge-model/2026-02-25.md +1263 -0
- package/memory/specs/evidence-aligner-v2/2026-02-25.md +1028 -0
- package/memory/specs/mcp-desktop-integration/2026-02-25.md +1334 -0
- package/memory/specs/post-tool-use-hook/2026-02-25.md +1164 -0
- package/memory/specs/private-tags/2026-02-25.md +1057 -0
- package/memory/specs/progressive-disclosure/2026-02-25.md +1436 -0
- package/memory/specs/task-entity-system/2026-02-25.md +924 -0
- package/memory/specs/vector-outbox-v2/2026-02-25.md +1510 -0
- package/memory/specs/web-viewer-ui/2026-02-25.md +1709 -0
- package/package.json +2 -1
- package/scripts/build.ts +6 -0
- package/src/cli/index.ts +281 -2
- package/src/core/consolidated-store.ts +63 -1
- package/src/core/consolidation-worker.ts +115 -6
- package/src/core/event-store.ts +14 -0
- package/src/core/index.ts +1 -0
- package/src/core/ingest-interceptor.ts +80 -0
- package/src/core/markdown-mirror.ts +70 -0
- package/src/core/md-mirror.ts +92 -0
- package/src/core/mongo-sync-config.ts +165 -0
- package/src/core/mongo-sync-worker.ts +381 -0
- package/src/core/retriever.ts +540 -150
- package/src/core/sqlite-event-store.ts +350 -1
- package/src/core/tag-taxonomy.ts +51 -0
- package/src/core/types.ts +28 -0
- package/src/server/api/health.ts +53 -0
- package/src/server/api/index.ts +3 -1
- package/src/server/api/stats.ts +46 -1
- package/src/services/bootstrap-organizer.ts +443 -0
- package/src/services/codex-session-history-importer.ts +474 -0
- package/src/services/memory-service.ts +373 -68
- package/src/ui/app.js +69 -2
- package/src/ui/index.html +8 -0
- package/tests/bootstrap-organizer.test.ts +111 -0
- package/tests/consolidation-worker.test.ts +75 -0
- package/tests/ingest-interceptor.test.ts +38 -0
- package/tests/markdown-mirror.test.ts +85 -0
- package/tests/md-mirror.test.ts +50 -0
- package/tests/retriever-fallback-chain.test.ts +223 -0
- package/tests/retriever-strategy-scope.test.ts +97 -0
- package/tests/retriever.memu-adoption.test.ts +122 -0
- package/tests/sqlite-event-store-replication.test.ts +92 -0
package/dist/server/api/index.js
CHANGED
|
@@ -13,19 +13,19 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
// src/server/api/index.ts
|
|
16
|
-
import { Hono as
|
|
16
|
+
import { Hono as Hono10 } from "hono";
|
|
17
17
|
|
|
18
18
|
// src/server/api/sessions.ts
|
|
19
19
|
import { Hono } from "hono";
|
|
20
20
|
|
|
21
21
|
// src/server/api/utils.ts
|
|
22
|
-
import * as
|
|
22
|
+
import * as path4 from "path";
|
|
23
23
|
import * as os2 from "os";
|
|
24
24
|
|
|
25
25
|
// src/services/memory-service.ts
|
|
26
|
-
import * as
|
|
26
|
+
import * as path3 from "path";
|
|
27
27
|
import * as os from "os";
|
|
28
|
-
import * as
|
|
28
|
+
import * as fs4 from "fs";
|
|
29
29
|
import * as crypto2 from "crypto";
|
|
30
30
|
|
|
31
31
|
// src/core/event-store.ts
|
|
@@ -83,11 +83,11 @@ function toDate(value) {
|
|
|
83
83
|
return new Date(value);
|
|
84
84
|
return new Date(String(value));
|
|
85
85
|
}
|
|
86
|
-
function createDatabase(
|
|
86
|
+
function createDatabase(path6, options) {
|
|
87
87
|
if (options?.readOnly) {
|
|
88
|
-
return new duckdb.Database(
|
|
88
|
+
return new duckdb.Database(path6, { access_mode: "READ_ONLY" });
|
|
89
89
|
}
|
|
90
|
-
return new duckdb.Database(
|
|
90
|
+
return new duckdb.Database(path6);
|
|
91
91
|
}
|
|
92
92
|
function dbRun(db, sql, params = []) {
|
|
93
93
|
return new Promise((resolve2, reject) => {
|
|
@@ -351,6 +351,17 @@ var EventStore = class {
|
|
|
351
351
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
352
352
|
)
|
|
353
353
|
`);
|
|
354
|
+
await dbRun(this.db, `
|
|
355
|
+
CREATE TABLE IF NOT EXISTS consolidated_rules (
|
|
356
|
+
rule_id VARCHAR PRIMARY KEY,
|
|
357
|
+
rule TEXT NOT NULL,
|
|
358
|
+
topics JSON,
|
|
359
|
+
source_memory_ids JSON,
|
|
360
|
+
source_events JSON,
|
|
361
|
+
confidence FLOAT DEFAULT 0.5,
|
|
362
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
363
|
+
)
|
|
364
|
+
`);
|
|
354
365
|
await dbRun(this.db, `
|
|
355
366
|
CREATE TABLE IF NOT EXISTS endless_config (
|
|
356
367
|
key VARCHAR PRIMARY KEY,
|
|
@@ -370,6 +381,7 @@ var EventStore = class {
|
|
|
370
381
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
|
|
371
382
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
|
|
372
383
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
|
|
384
|
+
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
|
|
373
385
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
|
|
374
386
|
this.initialized = true;
|
|
375
387
|
}
|
|
@@ -759,12 +771,12 @@ import { randomUUID as randomUUID2 } from "crypto";
|
|
|
759
771
|
import Database from "better-sqlite3";
|
|
760
772
|
import * as fs from "fs";
|
|
761
773
|
import * as nodePath from "path";
|
|
762
|
-
function createSQLiteDatabase(
|
|
763
|
-
const dir = nodePath.dirname(
|
|
774
|
+
function createSQLiteDatabase(path6, options) {
|
|
775
|
+
const dir = nodePath.dirname(path6);
|
|
764
776
|
if (!fs.existsSync(dir)) {
|
|
765
777
|
fs.mkdirSync(dir, { recursive: true });
|
|
766
778
|
}
|
|
767
|
-
const db = new Database(
|
|
779
|
+
const db = new Database(path6, {
|
|
768
780
|
readonly: options?.readonly ?? false
|
|
769
781
|
});
|
|
770
782
|
if (!options?.readonly && (options?.walMode ?? true)) {
|
|
@@ -805,6 +817,64 @@ function toSQLiteTimestamp(date) {
|
|
|
805
817
|
return date.toISOString();
|
|
806
818
|
}
|
|
807
819
|
|
|
820
|
+
// src/core/markdown-mirror.ts
|
|
821
|
+
import * as fs2 from "fs/promises";
|
|
822
|
+
import * as path from "path";
|
|
823
|
+
var DEFAULT_NAMESPACE = "default";
|
|
824
|
+
var DEFAULT_CATEGORY = "uncategorized";
|
|
825
|
+
function sanitizeSegment(input, fallback) {
|
|
826
|
+
const raw = String(input ?? "").trim().toLowerCase();
|
|
827
|
+
const safe = raw.normalize("NFKD").replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
828
|
+
if (!safe || safe === "." || safe === "..")
|
|
829
|
+
return fallback;
|
|
830
|
+
return safe;
|
|
831
|
+
}
|
|
832
|
+
function getCategorySegments(metadata, eventType) {
|
|
833
|
+
const raw = metadata?.categoryPath;
|
|
834
|
+
if (Array.isArray(raw) && raw.length > 0) {
|
|
835
|
+
return raw.map((s) => sanitizeSegment(s, DEFAULT_CATEGORY));
|
|
836
|
+
}
|
|
837
|
+
const single = metadata?.category;
|
|
838
|
+
if (typeof single === "string" && single.trim()) {
|
|
839
|
+
return [sanitizeSegment(single, DEFAULT_CATEGORY)];
|
|
840
|
+
}
|
|
841
|
+
return [sanitizeSegment(eventType, DEFAULT_CATEGORY)];
|
|
842
|
+
}
|
|
843
|
+
function buildMirrorPath(rootDir, event) {
|
|
844
|
+
const metadata = event.metadata;
|
|
845
|
+
const namespace = sanitizeSegment(metadata?.namespace, DEFAULT_NAMESPACE);
|
|
846
|
+
const categories = getCategorySegments(metadata, event.eventType);
|
|
847
|
+
const d = event.timestamp;
|
|
848
|
+
const yyyy = d.getFullYear();
|
|
849
|
+
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
850
|
+
const dd = String(d.getDate()).padStart(2, "0");
|
|
851
|
+
return path.join(rootDir, "memory", namespace, ...categories, `${yyyy}-${mm}-${dd}.md`);
|
|
852
|
+
}
|
|
853
|
+
function formatMirrorEntry(event) {
|
|
854
|
+
const category = Array.isArray(event.metadata?.categoryPath) ? event.metadata.categoryPath.join("/") : String(event.metadata?.category ?? event.eventType);
|
|
855
|
+
return [
|
|
856
|
+
"",
|
|
857
|
+
`- ts: ${event.timestamp.toISOString()}`,
|
|
858
|
+
` id: ${event.id}`,
|
|
859
|
+
` type: ${event.eventType}`,
|
|
860
|
+
` session: ${event.sessionId}`,
|
|
861
|
+
` category: ${category}`,
|
|
862
|
+
" content: |",
|
|
863
|
+
...event.content.split("\n").map((line) => ` ${line}`)
|
|
864
|
+
].join("\n") + "\n";
|
|
865
|
+
}
|
|
866
|
+
var MarkdownMirror = class {
|
|
867
|
+
constructor(rootDir) {
|
|
868
|
+
this.rootDir = rootDir;
|
|
869
|
+
}
|
|
870
|
+
async append(event) {
|
|
871
|
+
const outPath = buildMirrorPath(this.rootDir, event);
|
|
872
|
+
await fs2.mkdir(path.dirname(outPath), { recursive: true });
|
|
873
|
+
await fs2.appendFile(outPath, formatMirrorEntry(event), "utf8");
|
|
874
|
+
return outPath;
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
|
|
808
878
|
// src/core/sqlite-event-store.ts
|
|
809
879
|
var SQLiteEventStore = class {
|
|
810
880
|
constructor(dbPath, options) {
|
|
@@ -814,10 +884,12 @@ var SQLiteEventStore = class {
|
|
|
814
884
|
readonly: this.readOnly,
|
|
815
885
|
walMode: !this.readOnly
|
|
816
886
|
});
|
|
887
|
+
this.markdownMirror = this.readOnly || !options?.markdownMirrorRoot ? null : new MarkdownMirror(options.markdownMirrorRoot);
|
|
817
888
|
}
|
|
818
889
|
db;
|
|
819
890
|
initialized = false;
|
|
820
891
|
readOnly;
|
|
892
|
+
markdownMirror;
|
|
821
893
|
/**
|
|
822
894
|
* Initialize database schema
|
|
823
895
|
*/
|
|
@@ -1024,6 +1096,17 @@ var SQLiteEventStore = class {
|
|
|
1024
1096
|
created_at TEXT DEFAULT (datetime('now'))
|
|
1025
1097
|
);
|
|
1026
1098
|
|
|
1099
|
+
-- Consolidated Rules table (long-term stable memory)
|
|
1100
|
+
CREATE TABLE IF NOT EXISTS consolidated_rules (
|
|
1101
|
+
rule_id TEXT PRIMARY KEY,
|
|
1102
|
+
rule TEXT NOT NULL,
|
|
1103
|
+
topics TEXT,
|
|
1104
|
+
source_memory_ids TEXT,
|
|
1105
|
+
source_events TEXT,
|
|
1106
|
+
confidence REAL DEFAULT 0.5,
|
|
1107
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
1108
|
+
);
|
|
1109
|
+
|
|
1027
1110
|
-- Endless Mode Config table
|
|
1028
1111
|
CREATE TABLE IF NOT EXISTS endless_config (
|
|
1029
1112
|
key TEXT PRIMARY KEY,
|
|
@@ -1048,6 +1131,24 @@ var SQLiteEventStore = class {
|
|
|
1048
1131
|
measured_at TEXT
|
|
1049
1132
|
);
|
|
1050
1133
|
|
|
1134
|
+
-- Retrieval trace log (query -> candidates -> selected for context)
|
|
1135
|
+
CREATE TABLE IF NOT EXISTS retrieval_traces (
|
|
1136
|
+
trace_id TEXT PRIMARY KEY,
|
|
1137
|
+
session_id TEXT,
|
|
1138
|
+
project_hash TEXT,
|
|
1139
|
+
query_text TEXT NOT NULL,
|
|
1140
|
+
strategy TEXT,
|
|
1141
|
+
candidate_event_ids TEXT,
|
|
1142
|
+
selected_event_ids TEXT,
|
|
1143
|
+
candidate_details_json TEXT,
|
|
1144
|
+
selected_details_json TEXT,
|
|
1145
|
+
candidate_count INTEGER DEFAULT 0,
|
|
1146
|
+
selected_count INTEGER DEFAULT 0,
|
|
1147
|
+
confidence TEXT,
|
|
1148
|
+
fallback_trace TEXT,
|
|
1149
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
1150
|
+
);
|
|
1151
|
+
|
|
1051
1152
|
-- Sync position tracking (for SQLite -> DuckDB sync)
|
|
1052
1153
|
CREATE TABLE IF NOT EXISTS sync_positions (
|
|
1053
1154
|
target_name TEXT PRIMARY KEY,
|
|
@@ -1072,10 +1173,14 @@ var SQLiteEventStore = class {
|
|
|
1072
1173
|
CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score);
|
|
1073
1174
|
CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
|
|
1074
1175
|
CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
|
|
1176
|
+
CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence);
|
|
1075
1177
|
CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
|
|
1076
1178
|
CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
|
|
1077
1179
|
CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
|
|
1078
1180
|
CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
|
|
1181
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_created_at ON retrieval_traces(created_at DESC);
|
|
1182
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_project_hash ON retrieval_traces(project_hash);
|
|
1183
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_session_id ON retrieval_traces(session_id);
|
|
1079
1184
|
|
|
1080
1185
|
-- FTS5 Full-Text Search for fast keyword search
|
|
1081
1186
|
CREATE VIRTUAL TABLE IF NOT EXISTS events_fts USING fts5(
|
|
@@ -1099,6 +1204,14 @@ var SQLiteEventStore = class {
|
|
|
1099
1204
|
INSERT INTO events_fts(rowid, content, event_id) VALUES (NEW.rowid, NEW.content, NEW.id);
|
|
1100
1205
|
END;
|
|
1101
1206
|
`);
|
|
1207
|
+
try {
|
|
1208
|
+
sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN selected_details_json TEXT;`);
|
|
1209
|
+
} catch {
|
|
1210
|
+
}
|
|
1211
|
+
try {
|
|
1212
|
+
sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN candidate_details_json TEXT;`);
|
|
1213
|
+
} catch {
|
|
1214
|
+
}
|
|
1102
1215
|
const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
|
|
1103
1216
|
const columnNames = tableInfo.map((col) => col.name);
|
|
1104
1217
|
if (!columnNames.includes("access_count")) {
|
|
@@ -1198,6 +1311,21 @@ var SQLiteEventStore = class {
|
|
|
1198
1311
|
insertLevel.run(id);
|
|
1199
1312
|
});
|
|
1200
1313
|
transaction();
|
|
1314
|
+
if (this.markdownMirror) {
|
|
1315
|
+
const event = {
|
|
1316
|
+
id,
|
|
1317
|
+
eventType: input.eventType,
|
|
1318
|
+
sessionId: input.sessionId,
|
|
1319
|
+
timestamp: input.timestamp,
|
|
1320
|
+
content: input.content,
|
|
1321
|
+
canonicalKey,
|
|
1322
|
+
dedupeKey,
|
|
1323
|
+
metadata
|
|
1324
|
+
};
|
|
1325
|
+
this.markdownMirror.append(event).catch((err) => {
|
|
1326
|
+
console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
|
|
1327
|
+
});
|
|
1328
|
+
}
|
|
1201
1329
|
return { success: true, eventId: id, isDuplicate: false };
|
|
1202
1330
|
} catch (error) {
|
|
1203
1331
|
return {
|
|
@@ -1256,6 +1384,92 @@ var SQLiteEventStore = class {
|
|
|
1256
1384
|
);
|
|
1257
1385
|
return rows.map(this.rowToEvent);
|
|
1258
1386
|
}
|
|
1387
|
+
/**
|
|
1388
|
+
* Get events since a SQLite rowid (for robust incremental replication).
|
|
1389
|
+
* Rowid is monotonic for append-only tables, independent of client timestamps.
|
|
1390
|
+
*/
|
|
1391
|
+
async getEventsSinceRowid(lastRowid, limit = 1e3) {
|
|
1392
|
+
await this.initialize();
|
|
1393
|
+
const rows = sqliteAll(
|
|
1394
|
+
this.db,
|
|
1395
|
+
`SELECT rowid as _rowid, * FROM events WHERE rowid > ? ORDER BY rowid ASC LIMIT ?`,
|
|
1396
|
+
[lastRowid, limit]
|
|
1397
|
+
);
|
|
1398
|
+
return rows.map((row) => ({
|
|
1399
|
+
rowid: row._rowid,
|
|
1400
|
+
event: this.rowToEvent(row)
|
|
1401
|
+
}));
|
|
1402
|
+
}
|
|
1403
|
+
/**
|
|
1404
|
+
* Import events with fixed IDs (used for cross-machine replication).
|
|
1405
|
+
* Idempotent: skips if event id or dedupeKey already exists.
|
|
1406
|
+
*
|
|
1407
|
+
* NOTE: This bypasses the append() id generation to preserve stable IDs.
|
|
1408
|
+
*/
|
|
1409
|
+
async importEvents(events) {
|
|
1410
|
+
if (events.length === 0)
|
|
1411
|
+
return { inserted: 0, skipped: 0 };
|
|
1412
|
+
if (this.readOnly)
|
|
1413
|
+
return { inserted: 0, skipped: events.length };
|
|
1414
|
+
await this.initialize();
|
|
1415
|
+
const getById = this.db.prepare(`SELECT id FROM events WHERE id = ?`);
|
|
1416
|
+
const getByDedupe = this.db.prepare(`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`);
|
|
1417
|
+
const insertEvent = this.db.prepare(`
|
|
1418
|
+
INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
|
|
1419
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1420
|
+
`);
|
|
1421
|
+
const insertDedup = this.db.prepare(`
|
|
1422
|
+
INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
|
|
1423
|
+
`);
|
|
1424
|
+
const insertLevel = this.db.prepare(`
|
|
1425
|
+
INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')
|
|
1426
|
+
`);
|
|
1427
|
+
let inserted = 0;
|
|
1428
|
+
let skipped = 0;
|
|
1429
|
+
const insertedEvents = [];
|
|
1430
|
+
const tx = this.db.transaction((batch) => {
|
|
1431
|
+
for (const ev of batch) {
|
|
1432
|
+
const existingById = getById.get(ev.id);
|
|
1433
|
+
if (existingById) {
|
|
1434
|
+
skipped++;
|
|
1435
|
+
continue;
|
|
1436
|
+
}
|
|
1437
|
+
const canonicalKey = ev.canonicalKey || makeCanonicalKey(ev.content);
|
|
1438
|
+
const dedupeKey = ev.dedupeKey || makeDedupeKey(ev.content, ev.sessionId);
|
|
1439
|
+
const existingByDedupe = getByDedupe.get(dedupeKey);
|
|
1440
|
+
if (existingByDedupe) {
|
|
1441
|
+
skipped++;
|
|
1442
|
+
continue;
|
|
1443
|
+
}
|
|
1444
|
+
const metadata = ev.metadata || {};
|
|
1445
|
+
const turnId = metadata.turnId;
|
|
1446
|
+
insertEvent.run(
|
|
1447
|
+
ev.id,
|
|
1448
|
+
ev.eventType,
|
|
1449
|
+
ev.sessionId,
|
|
1450
|
+
toSQLiteTimestamp(ev.timestamp),
|
|
1451
|
+
ev.content,
|
|
1452
|
+
canonicalKey,
|
|
1453
|
+
dedupeKey,
|
|
1454
|
+
JSON.stringify(metadata),
|
|
1455
|
+
turnId ?? null
|
|
1456
|
+
);
|
|
1457
|
+
insertDedup.run(dedupeKey, ev.id);
|
|
1458
|
+
insertLevel.run(ev.id);
|
|
1459
|
+
inserted++;
|
|
1460
|
+
insertedEvents.push(ev);
|
|
1461
|
+
}
|
|
1462
|
+
});
|
|
1463
|
+
tx(events);
|
|
1464
|
+
if (this.markdownMirror && insertedEvents.length > 0) {
|
|
1465
|
+
for (const ev of insertedEvents) {
|
|
1466
|
+
this.markdownMirror.append(ev).catch((err) => {
|
|
1467
|
+
console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
return { inserted, skipped };
|
|
1472
|
+
}
|
|
1259
1473
|
/**
|
|
1260
1474
|
* Create or update session
|
|
1261
1475
|
*/
|
|
@@ -1418,6 +1632,35 @@ var SQLiteEventStore = class {
|
|
|
1418
1632
|
[error, ...ids]
|
|
1419
1633
|
);
|
|
1420
1634
|
}
|
|
1635
|
+
/**
|
|
1636
|
+
* Get embedding/vector outbox health statistics
|
|
1637
|
+
*/
|
|
1638
|
+
async getOutboxStats() {
|
|
1639
|
+
await this.initialize();
|
|
1640
|
+
const embeddingRows = sqliteAll(
|
|
1641
|
+
this.db,
|
|
1642
|
+
`SELECT status, COUNT(*) as count FROM embedding_outbox GROUP BY status`
|
|
1643
|
+
);
|
|
1644
|
+
const vectorRows = sqliteAll(
|
|
1645
|
+
this.db,
|
|
1646
|
+
`SELECT status, COUNT(*) as count FROM vector_outbox GROUP BY status`
|
|
1647
|
+
);
|
|
1648
|
+
const fromRows = (rows) => {
|
|
1649
|
+
const out = { pending: 0, processing: 0, failed: 0, total: 0 };
|
|
1650
|
+
for (const row of rows) {
|
|
1651
|
+
const key = row.status;
|
|
1652
|
+
if (key === "pending" || key === "processing" || key === "failed") {
|
|
1653
|
+
out[key] += row.count;
|
|
1654
|
+
}
|
|
1655
|
+
out.total += row.count;
|
|
1656
|
+
}
|
|
1657
|
+
return out;
|
|
1658
|
+
};
|
|
1659
|
+
return {
|
|
1660
|
+
embedding: fromRows(embeddingRows),
|
|
1661
|
+
vector: fromRows(vectorRows)
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1421
1664
|
/**
|
|
1422
1665
|
* Update memory level
|
|
1423
1666
|
*/
|
|
@@ -1776,6 +2019,79 @@ var SQLiteEventStore = class {
|
|
|
1776
2019
|
getDatabase() {
|
|
1777
2020
|
return this.db;
|
|
1778
2021
|
}
|
|
2022
|
+
async recordRetrievalTrace(input) {
|
|
2023
|
+
await this.initialize();
|
|
2024
|
+
const traceId = randomUUID2();
|
|
2025
|
+
sqliteRun(
|
|
2026
|
+
this.db,
|
|
2027
|
+
`INSERT INTO retrieval_traces (
|
|
2028
|
+
trace_id, session_id, project_hash, query_text, strategy,
|
|
2029
|
+
candidate_event_ids, selected_event_ids, candidate_details_json, selected_details_json,
|
|
2030
|
+
candidate_count, selected_count, confidence, fallback_trace
|
|
2031
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
2032
|
+
[
|
|
2033
|
+
traceId,
|
|
2034
|
+
input.sessionId || null,
|
|
2035
|
+
input.projectHash || null,
|
|
2036
|
+
input.queryText,
|
|
2037
|
+
input.strategy || null,
|
|
2038
|
+
JSON.stringify(input.candidateEventIds || []),
|
|
2039
|
+
JSON.stringify(input.selectedEventIds || []),
|
|
2040
|
+
JSON.stringify(input.candidateDetails || []),
|
|
2041
|
+
JSON.stringify(input.selectedDetails || []),
|
|
2042
|
+
(input.candidateEventIds || []).length,
|
|
2043
|
+
(input.selectedEventIds || []).length,
|
|
2044
|
+
input.confidence || null,
|
|
2045
|
+
JSON.stringify(input.fallbackTrace || [])
|
|
2046
|
+
]
|
|
2047
|
+
);
|
|
2048
|
+
}
|
|
2049
|
+
async getRecentRetrievalTraces(limit = 50) {
|
|
2050
|
+
await this.initialize();
|
|
2051
|
+
const rows = sqliteAll(
|
|
2052
|
+
this.db,
|
|
2053
|
+
`SELECT * FROM retrieval_traces ORDER BY created_at DESC LIMIT ?`,
|
|
2054
|
+
[limit]
|
|
2055
|
+
);
|
|
2056
|
+
return rows.map((row) => ({
|
|
2057
|
+
traceId: row.trace_id,
|
|
2058
|
+
sessionId: row.session_id || void 0,
|
|
2059
|
+
projectHash: row.project_hash || void 0,
|
|
2060
|
+
queryText: row.query_text,
|
|
2061
|
+
strategy: row.strategy || void 0,
|
|
2062
|
+
candidateEventIds: row.candidate_event_ids ? JSON.parse(row.candidate_event_ids) : [],
|
|
2063
|
+
selectedEventIds: row.selected_event_ids ? JSON.parse(row.selected_event_ids) : [],
|
|
2064
|
+
candidateDetails: row.candidate_details_json ? JSON.parse(row.candidate_details_json) : [],
|
|
2065
|
+
selectedDetails: row.selected_details_json ? JSON.parse(row.selected_details_json) : [],
|
|
2066
|
+
candidateCount: Number(row.candidate_count || 0),
|
|
2067
|
+
selectedCount: Number(row.selected_count || 0),
|
|
2068
|
+
confidence: row.confidence || void 0,
|
|
2069
|
+
fallbackTrace: row.fallback_trace ? JSON.parse(row.fallback_trace) : [],
|
|
2070
|
+
createdAt: toDateFromSQLite(row.created_at)
|
|
2071
|
+
}));
|
|
2072
|
+
}
|
|
2073
|
+
async getRetrievalTraceStats() {
|
|
2074
|
+
await this.initialize();
|
|
2075
|
+
const row = sqliteGet(
|
|
2076
|
+
this.db,
|
|
2077
|
+
`SELECT
|
|
2078
|
+
COUNT(*) as total_queries,
|
|
2079
|
+
AVG(candidate_count) as avg_candidate_count,
|
|
2080
|
+
AVG(selected_count) as avg_selected_count,
|
|
2081
|
+
CASE
|
|
2082
|
+
WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
|
|
2083
|
+
ELSE 0
|
|
2084
|
+
END as selection_rate
|
|
2085
|
+
FROM retrieval_traces`,
|
|
2086
|
+
[]
|
|
2087
|
+
);
|
|
2088
|
+
return {
|
|
2089
|
+
totalQueries: Number(row?.total_queries || 0),
|
|
2090
|
+
avgCandidateCount: Number(row?.avg_candidate_count || 0),
|
|
2091
|
+
avgSelectedCount: Number(row?.avg_selected_count || 0),
|
|
2092
|
+
selectionRate: Number(row?.selection_rate || 0)
|
|
2093
|
+
};
|
|
2094
|
+
}
|
|
1779
2095
|
/**
|
|
1780
2096
|
* Close database connection
|
|
1781
2097
|
*/
|
|
@@ -2637,7 +2953,20 @@ var DEFAULT_OPTIONS = {
|
|
|
2637
2953
|
topK: 5,
|
|
2638
2954
|
minScore: 0.7,
|
|
2639
2955
|
maxTokens: 2e3,
|
|
2640
|
-
includeSessionContext: true
|
|
2956
|
+
includeSessionContext: true,
|
|
2957
|
+
strategy: "auto",
|
|
2958
|
+
rerankWithKeyword: true,
|
|
2959
|
+
decayPolicy: {
|
|
2960
|
+
enabled: true,
|
|
2961
|
+
windowDays: 30,
|
|
2962
|
+
maxPenalty: 0.15
|
|
2963
|
+
},
|
|
2964
|
+
graphHop: {
|
|
2965
|
+
enabled: true,
|
|
2966
|
+
maxHops: 1,
|
|
2967
|
+
hopPenalty: 0.08
|
|
2968
|
+
},
|
|
2969
|
+
projectScopeMode: "global"
|
|
2641
2970
|
};
|
|
2642
2971
|
var Retriever = class {
|
|
2643
2972
|
eventStore;
|
|
@@ -2647,6 +2976,7 @@ var Retriever = class {
|
|
|
2647
2976
|
sharedStore;
|
|
2648
2977
|
sharedVectorStore;
|
|
2649
2978
|
graduation;
|
|
2979
|
+
queryRewriter;
|
|
2650
2980
|
constructor(eventStore, vectorStore, embedder, matcher, sharedOptions) {
|
|
2651
2981
|
this.eventStore = eventStore;
|
|
2652
2982
|
this.vectorStore = vectorStore;
|
|
@@ -2655,47 +2985,105 @@ var Retriever = class {
|
|
|
2655
2985
|
this.sharedStore = sharedOptions?.sharedStore;
|
|
2656
2986
|
this.sharedVectorStore = sharedOptions?.sharedVectorStore;
|
|
2657
2987
|
}
|
|
2658
|
-
/**
|
|
2659
|
-
* Set graduation pipeline for access tracking
|
|
2660
|
-
*/
|
|
2661
2988
|
setGraduationPipeline(graduation) {
|
|
2662
2989
|
this.graduation = graduation;
|
|
2663
2990
|
}
|
|
2664
|
-
/**
|
|
2665
|
-
* Set shared stores after construction
|
|
2666
|
-
*/
|
|
2667
2991
|
setSharedStores(sharedStore, sharedVectorStore) {
|
|
2668
2992
|
this.sharedStore = sharedStore;
|
|
2669
2993
|
this.sharedVectorStore = sharedVectorStore;
|
|
2670
2994
|
}
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2995
|
+
setQueryRewriter(rewriter) {
|
|
2996
|
+
this.queryRewriter = rewriter;
|
|
2997
|
+
}
|
|
2674
2998
|
async retrieve(query, options = {}) {
|
|
2675
2999
|
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
2676
|
-
const
|
|
2677
|
-
const
|
|
2678
|
-
|
|
2679
|
-
|
|
3000
|
+
const sessionFilter = opts.scope?.sessionId ?? opts.sessionId;
|
|
3001
|
+
const fallbackTrace = [];
|
|
3002
|
+
const fallbackEnabled = (opts.strategy ?? "auto") === "auto";
|
|
3003
|
+
const primaryStrategy = opts.strategy === "auto" ? "fast" : opts.strategy || "fast";
|
|
3004
|
+
let current = await this.runStage(query, {
|
|
3005
|
+
strategy: primaryStrategy,
|
|
3006
|
+
topK: opts.topK,
|
|
2680
3007
|
minScore: opts.minScore,
|
|
2681
|
-
sessionId:
|
|
3008
|
+
sessionId: sessionFilter,
|
|
3009
|
+
scope: opts.scope,
|
|
3010
|
+
rerankWithKeyword: opts.rerankWithKeyword !== false,
|
|
3011
|
+
rerankWeights: opts.rerankWeights,
|
|
3012
|
+
decayPolicy: opts.decayPolicy,
|
|
3013
|
+
intentRewrite: opts.intentRewrite === true,
|
|
3014
|
+
graphHop: opts.graphHop,
|
|
3015
|
+
projectScopeMode: opts.projectScopeMode,
|
|
3016
|
+
projectHash: opts.projectHash,
|
|
3017
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
2682
3018
|
});
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
3019
|
+
fallbackTrace.push(`stage:primary:${primaryStrategy}`);
|
|
3020
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results) && primaryStrategy !== "deep") {
|
|
3021
|
+
current = await this.runStage(query, {
|
|
3022
|
+
strategy: "deep",
|
|
3023
|
+
topK: opts.topK,
|
|
3024
|
+
minScore: opts.minScore,
|
|
3025
|
+
sessionId: sessionFilter,
|
|
3026
|
+
scope: opts.scope,
|
|
3027
|
+
rerankWithKeyword: opts.rerankWithKeyword !== false,
|
|
3028
|
+
rerankWeights: opts.rerankWeights,
|
|
3029
|
+
decayPolicy: opts.decayPolicy,
|
|
3030
|
+
graphHop: opts.graphHop,
|
|
3031
|
+
projectScopeMode: opts.projectScopeMode,
|
|
3032
|
+
projectHash: opts.projectHash,
|
|
3033
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
3034
|
+
});
|
|
3035
|
+
fallbackTrace.push("fallback:deep");
|
|
3036
|
+
}
|
|
3037
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
|
|
3038
|
+
current = await this.runStage(query, {
|
|
3039
|
+
strategy: "deep",
|
|
3040
|
+
topK: opts.topK,
|
|
3041
|
+
minScore: Math.max(0.5, opts.minScore - 0.15),
|
|
3042
|
+
sessionId: void 0,
|
|
3043
|
+
scope: void 0,
|
|
3044
|
+
rerankWithKeyword: true,
|
|
3045
|
+
rerankWeights: opts.rerankWeights,
|
|
3046
|
+
decayPolicy: opts.decayPolicy,
|
|
3047
|
+
graphHop: opts.graphHop,
|
|
3048
|
+
projectScopeMode: opts.projectScopeMode,
|
|
3049
|
+
projectHash: opts.projectHash,
|
|
3050
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
3051
|
+
});
|
|
3052
|
+
fallbackTrace.push("fallback:scope-expanded");
|
|
3053
|
+
}
|
|
3054
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
|
|
3055
|
+
const summary = await this.buildSummaryFallback(query, opts.topK);
|
|
3056
|
+
current = {
|
|
3057
|
+
results: summary,
|
|
3058
|
+
candidateResults: summary,
|
|
3059
|
+
matchResult: this.matcher.matchSearchResults(summary, () => 0)
|
|
3060
|
+
};
|
|
3061
|
+
fallbackTrace.push("fallback:summary");
|
|
3062
|
+
}
|
|
3063
|
+
const memories = await this.enrichResults(current.results.slice(0, opts.topK), opts);
|
|
2688
3064
|
const context = this.buildContext(memories, opts.maxTokens);
|
|
2689
3065
|
return {
|
|
2690
3066
|
memories,
|
|
2691
|
-
matchResult,
|
|
3067
|
+
matchResult: current.matchResult,
|
|
2692
3068
|
totalTokens: this.estimateTokens(context),
|
|
2693
|
-
context
|
|
3069
|
+
context,
|
|
3070
|
+
fallbackTrace,
|
|
3071
|
+
selectedDebug: current.results.slice(0, opts.topK).map((r) => ({
|
|
3072
|
+
eventId: r.eventId,
|
|
3073
|
+
score: r.score,
|
|
3074
|
+
semanticScore: r.semanticScore,
|
|
3075
|
+
lexicalScore: r.lexicalScore,
|
|
3076
|
+
recencyScore: r.recencyScore
|
|
3077
|
+
})),
|
|
3078
|
+
candidateDebug: (current.candidateResults || []).slice(0, Math.max(opts.topK * 3, 20)).map((r) => ({
|
|
3079
|
+
eventId: r.eventId,
|
|
3080
|
+
score: r.score,
|
|
3081
|
+
semanticScore: r.semanticScore,
|
|
3082
|
+
lexicalScore: r.lexicalScore,
|
|
3083
|
+
recencyScore: r.recencyScore
|
|
3084
|
+
}))
|
|
2694
3085
|
};
|
|
2695
3086
|
}
|
|
2696
|
-
/**
|
|
2697
|
-
* Retrieve with unified search (project + shared)
|
|
2698
|
-
*/
|
|
2699
3087
|
async retrieveUnified(query, options = {}) {
|
|
2700
3088
|
const projectResult = await this.retrieve(query, options);
|
|
2701
3089
|
if (!options.includeShared || !this.sharedStore || !this.sharedVectorStore) {
|
|
@@ -2703,22 +3091,19 @@ var Retriever = class {
|
|
|
2703
3091
|
}
|
|
2704
3092
|
try {
|
|
2705
3093
|
const queryEmbedding = await this.embedder.embed(query);
|
|
2706
|
-
const sharedVectorResults = await this.sharedVectorStore.search(
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
excludeProjectHash: options.projectHash
|
|
2712
|
-
}
|
|
2713
|
-
);
|
|
3094
|
+
const sharedVectorResults = await this.sharedVectorStore.search(queryEmbedding.vector, {
|
|
3095
|
+
limit: options.topK || 5,
|
|
3096
|
+
minScore: options.minScore || 0.7,
|
|
3097
|
+
excludeProjectHash: options.projectHash
|
|
3098
|
+
});
|
|
2714
3099
|
const sharedMemories = [];
|
|
2715
3100
|
for (const result of sharedVectorResults) {
|
|
2716
3101
|
const entry = await this.sharedStore.get(result.entryId);
|
|
2717
|
-
if (entry)
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
3102
|
+
if (!entry)
|
|
3103
|
+
continue;
|
|
3104
|
+
if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
|
|
3105
|
+
sharedMemories.push(entry);
|
|
3106
|
+
await this.sharedStore.recordUsage(entry.entryId);
|
|
2722
3107
|
}
|
|
2723
3108
|
}
|
|
2724
3109
|
const unifiedContext = this.buildUnifiedContext(projectResult, sharedMemories);
|
|
@@ -2733,50 +3118,243 @@ var Retriever = class {
|
|
|
2733
3118
|
return projectResult;
|
|
2734
3119
|
}
|
|
2735
3120
|
}
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
3121
|
+
async runStage(query, input) {
|
|
3122
|
+
let initialResults = await this.searchByStrategy(query, {
|
|
3123
|
+
strategy: input.strategy,
|
|
3124
|
+
topK: input.topK,
|
|
3125
|
+
minScore: input.minScore,
|
|
3126
|
+
sessionId: input.sessionId
|
|
3127
|
+
});
|
|
3128
|
+
if (input.intentRewrite && input.strategy === "deep" && this.queryRewriter) {
|
|
3129
|
+
const rewritten = (await this.queryRewriter(query))?.trim();
|
|
3130
|
+
if (rewritten && rewritten !== query) {
|
|
3131
|
+
const rewrittenResults = await this.searchByStrategy(rewritten, {
|
|
3132
|
+
strategy: "deep",
|
|
3133
|
+
topK: input.topK,
|
|
3134
|
+
minScore: Math.max(0.5, input.minScore - 0.1),
|
|
3135
|
+
sessionId: input.sessionId
|
|
3136
|
+
});
|
|
3137
|
+
initialResults = this.mergeResults(initialResults, rewrittenResults, input.topK * 3);
|
|
3138
|
+
}
|
|
3139
|
+
}
|
|
3140
|
+
const expandedResults = input.graphHop?.enabled === false ? initialResults : await this.expandGraphHops(initialResults, {
|
|
3141
|
+
maxHops: Math.max(1, input.graphHop?.maxHops ?? 1),
|
|
3142
|
+
hopPenalty: Math.max(0, input.graphHop?.hopPenalty ?? 0.08),
|
|
3143
|
+
limit: input.topK * 4
|
|
3144
|
+
});
|
|
3145
|
+
const rerankedResults = input.rerankWithKeyword ? this.rerankByKeywordOverlap(expandedResults, query, input.rerankWeights, input.decayPolicy) : expandedResults;
|
|
3146
|
+
const filtered = await this.applyScopeFilters(rerankedResults, {
|
|
3147
|
+
scope: input.scope,
|
|
3148
|
+
projectScopeMode: input.projectScopeMode,
|
|
3149
|
+
projectHash: input.projectHash,
|
|
3150
|
+
allowedProjectHashes: input.allowedProjectHashes
|
|
3151
|
+
});
|
|
3152
|
+
const top = filtered.slice(0, input.topK);
|
|
3153
|
+
const matchResult = this.matcher.matchSearchResults(top, () => 0);
|
|
3154
|
+
return { results: top, candidateResults: filtered, matchResult };
|
|
3155
|
+
}
|
|
3156
|
+
mergeResults(primary, secondary, limit) {
|
|
3157
|
+
const byId = /* @__PURE__ */ new Map();
|
|
3158
|
+
for (const row of primary)
|
|
3159
|
+
byId.set(row.eventId, row);
|
|
3160
|
+
for (const row of secondary) {
|
|
3161
|
+
const prev = byId.get(row.eventId);
|
|
3162
|
+
if (!prev || row.score > prev.score) {
|
|
3163
|
+
byId.set(row.eventId, row);
|
|
3164
|
+
}
|
|
3165
|
+
}
|
|
3166
|
+
return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, limit);
|
|
3167
|
+
}
|
|
3168
|
+
async expandGraphHops(seeds, opts) {
|
|
3169
|
+
const byId = /* @__PURE__ */ new Map();
|
|
3170
|
+
for (const s of seeds)
|
|
3171
|
+
byId.set(s.eventId, s);
|
|
3172
|
+
let frontier = seeds.map((s) => ({ row: s, hop: 0 }));
|
|
3173
|
+
for (let hop = 1; hop <= opts.maxHops; hop += 1) {
|
|
3174
|
+
const next = [];
|
|
3175
|
+
for (const f of frontier) {
|
|
3176
|
+
const ev = await this.eventStore.getEvent(f.row.eventId);
|
|
3177
|
+
if (!ev)
|
|
3178
|
+
continue;
|
|
3179
|
+
const rel = ev.metadata?.relatedEventIds ?? [];
|
|
3180
|
+
const relatedIds = Array.isArray(rel) ? rel.filter((x) => typeof x === "string") : [];
|
|
3181
|
+
for (const rid of relatedIds) {
|
|
3182
|
+
if (byId.has(rid))
|
|
3183
|
+
continue;
|
|
3184
|
+
const target = await this.eventStore.getEvent(rid);
|
|
3185
|
+
if (!target)
|
|
3186
|
+
continue;
|
|
3187
|
+
const score = Math.max(0, f.row.score - opts.hopPenalty * hop);
|
|
3188
|
+
const row = {
|
|
3189
|
+
id: `hop-${hop}-${rid}`,
|
|
3190
|
+
eventId: target.id,
|
|
3191
|
+
content: target.content,
|
|
3192
|
+
score,
|
|
3193
|
+
sessionId: target.sessionId,
|
|
3194
|
+
eventType: target.eventType,
|
|
3195
|
+
timestamp: target.timestamp.toISOString()
|
|
3196
|
+
};
|
|
3197
|
+
byId.set(row.eventId, row);
|
|
3198
|
+
next.push({ row, hop });
|
|
3199
|
+
if (byId.size >= opts.limit)
|
|
3200
|
+
break;
|
|
2757
3201
|
}
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
`;
|
|
3202
|
+
if (byId.size >= opts.limit)
|
|
3203
|
+
break;
|
|
2761
3204
|
}
|
|
3205
|
+
frontier = next;
|
|
3206
|
+
if (frontier.length === 0 || byId.size >= opts.limit)
|
|
3207
|
+
break;
|
|
2762
3208
|
}
|
|
2763
|
-
return
|
|
3209
|
+
return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, opts.limit);
|
|
3210
|
+
}
|
|
3211
|
+
shouldFallback(matchResult, results) {
|
|
3212
|
+
if (results.length === 0)
|
|
3213
|
+
return true;
|
|
3214
|
+
if (matchResult.confidence === "none")
|
|
3215
|
+
return true;
|
|
3216
|
+
return false;
|
|
3217
|
+
}
|
|
3218
|
+
async buildSummaryFallback(query, topK) {
|
|
3219
|
+
const recent = await this.eventStore.getRecentEvents(Math.max(topK * 6, 20));
|
|
3220
|
+
const q = this.tokenize(query);
|
|
3221
|
+
const ranked = recent.map((e) => ({ e, overlap: this.keywordOverlap(q, this.tokenize(e.content)) })).filter((r) => r.overlap > 0).sort((a, b) => b.overlap - a.overlap).slice(0, topK).map((row, idx) => ({
|
|
3222
|
+
id: `summary-${row.e.id}`,
|
|
3223
|
+
eventId: row.e.id,
|
|
3224
|
+
content: row.e.content,
|
|
3225
|
+
score: Math.max(0.25, 0.6 - idx * 0.05),
|
|
3226
|
+
sessionId: row.e.sessionId,
|
|
3227
|
+
eventType: row.e.eventType,
|
|
3228
|
+
timestamp: row.e.timestamp.toISOString()
|
|
3229
|
+
}));
|
|
3230
|
+
return ranked;
|
|
3231
|
+
}
|
|
3232
|
+
async searchByStrategy(query, input) {
|
|
3233
|
+
const strategy = input.strategy === "auto" ? "deep" : input.strategy;
|
|
3234
|
+
if (strategy === "fast") {
|
|
3235
|
+
const keyword = await this.searchByKeyword(query, {
|
|
3236
|
+
limit: Math.max(5, input.topK * 3),
|
|
3237
|
+
sessionId: input.sessionId
|
|
3238
|
+
});
|
|
3239
|
+
return keyword;
|
|
3240
|
+
}
|
|
3241
|
+
const queryEmbedding = await this.embedder.embed(query);
|
|
3242
|
+
return this.vectorStore.search(queryEmbedding.vector, {
|
|
3243
|
+
limit: Math.max(5, input.topK * 3),
|
|
3244
|
+
minScore: input.minScore,
|
|
3245
|
+
sessionId: input.sessionId
|
|
3246
|
+
});
|
|
3247
|
+
}
|
|
3248
|
+
async searchByKeyword(query, input) {
|
|
3249
|
+
if (this.eventStore.keywordSearch) {
|
|
3250
|
+
const rows = await this.eventStore.keywordSearch(query, input.limit);
|
|
3251
|
+
const filtered2 = input.sessionId ? rows.filter((r) => r.event.sessionId === input.sessionId) : rows;
|
|
3252
|
+
return filtered2.map((row, idx) => ({
|
|
3253
|
+
id: `kw-${row.event.id}`,
|
|
3254
|
+
eventId: row.event.id,
|
|
3255
|
+
content: row.event.content,
|
|
3256
|
+
score: Math.max(0.4, 1 - idx * 0.04),
|
|
3257
|
+
sessionId: row.event.sessionId,
|
|
3258
|
+
eventType: row.event.eventType,
|
|
3259
|
+
timestamp: row.event.timestamp.toISOString()
|
|
3260
|
+
}));
|
|
3261
|
+
}
|
|
3262
|
+
const recent = await this.eventStore.getRecentEvents(input.limit * 4);
|
|
3263
|
+
const tokens = this.tokenize(query);
|
|
3264
|
+
const filtered = recent.filter((e) => input.sessionId ? e.sessionId === input.sessionId : true).map((e) => ({ e, overlap: this.keywordOverlap(tokens, this.tokenize(e.content)) })).filter((r) => r.overlap > 0).sort((a, b) => b.overlap - a.overlap).slice(0, input.limit);
|
|
3265
|
+
return filtered.map((row, idx) => ({
|
|
3266
|
+
id: `kw-fallback-${row.e.id}`,
|
|
3267
|
+
eventId: row.e.id,
|
|
3268
|
+
content: row.e.content,
|
|
3269
|
+
score: Math.max(0.3, 0.9 - idx * 0.05),
|
|
3270
|
+
sessionId: row.e.sessionId,
|
|
3271
|
+
eventType: row.e.eventType,
|
|
3272
|
+
timestamp: row.e.timestamp.toISOString()
|
|
3273
|
+
}));
|
|
3274
|
+
}
|
|
3275
|
+
rerankByKeywordOverlap(results, query, weights, decayPolicy) {
|
|
3276
|
+
const q = this.tokenize(query);
|
|
3277
|
+
const now = Date.now();
|
|
3278
|
+
const sw = Math.max(0, weights?.semantic ?? 0.7);
|
|
3279
|
+
const lw = Math.max(0, weights?.lexical ?? 0.2);
|
|
3280
|
+
const rw = Math.max(0, weights?.recency ?? 0.1);
|
|
3281
|
+
const total = sw + lw + rw || 1;
|
|
3282
|
+
const decayEnabled = decayPolicy?.enabled !== false;
|
|
3283
|
+
const decayWindow = Math.max(1, decayPolicy?.windowDays ?? 30);
|
|
3284
|
+
const decayMaxPenalty = Math.max(0, decayPolicy?.maxPenalty ?? 0.15);
|
|
3285
|
+
return [...results].map((r) => {
|
|
3286
|
+
const overlap = this.keywordOverlap(q, this.tokenize(r.content));
|
|
3287
|
+
const recencyDays = Math.max(0, (now - new Date(r.timestamp).getTime()) / (1e3 * 60 * 60 * 24));
|
|
3288
|
+
const recency = Math.max(0, 1 - recencyDays / decayWindow);
|
|
3289
|
+
let blended = (r.score * sw + overlap * lw + recency * rw) / total;
|
|
3290
|
+
if (decayEnabled && recencyDays > decayWindow && overlap < 0.5) {
|
|
3291
|
+
const ageFactor = Math.min(1, (recencyDays - decayWindow) / decayWindow);
|
|
3292
|
+
blended -= decayMaxPenalty * ageFactor;
|
|
3293
|
+
}
|
|
3294
|
+
return { ...r, score: Math.max(0, blended), semanticScore: r.score, lexicalScore: overlap, recencyScore: recency };
|
|
3295
|
+
}).sort((a, b) => b.score - a.score);
|
|
3296
|
+
}
|
|
3297
|
+
async applyScopeFilters(results, options) {
|
|
3298
|
+
const scope = options?.scope;
|
|
3299
|
+
const projectScopeMode = options?.projectScopeMode ?? "global";
|
|
3300
|
+
const allowedProjectHashes = new Set(
|
|
3301
|
+
[options?.projectHash, ...options?.allowedProjectHashes || []].filter(
|
|
3302
|
+
(value) => typeof value === "string" && value.length > 0
|
|
3303
|
+
)
|
|
3304
|
+
);
|
|
3305
|
+
if (!scope && projectScopeMode === "global")
|
|
3306
|
+
return results;
|
|
3307
|
+
const normalizedIncludes = (scope?.contentIncludes || []).map((s) => s.toLowerCase());
|
|
3308
|
+
const filtered = [];
|
|
3309
|
+
for (const result of results) {
|
|
3310
|
+
if (scope?.sessionId && result.sessionId !== scope.sessionId)
|
|
3311
|
+
continue;
|
|
3312
|
+
if (scope?.sessionIdPrefix && !result.sessionId.startsWith(scope.sessionIdPrefix))
|
|
3313
|
+
continue;
|
|
3314
|
+
if (scope?.eventTypes && scope.eventTypes.length > 0 && !scope.eventTypes.includes(result.eventType))
|
|
3315
|
+
continue;
|
|
3316
|
+
const event = await this.eventStore.getEvent(result.eventId);
|
|
3317
|
+
if (!event)
|
|
3318
|
+
continue;
|
|
3319
|
+
if (scope?.canonicalKeyPrefix && !event.canonicalKey.startsWith(scope.canonicalKeyPrefix))
|
|
3320
|
+
continue;
|
|
3321
|
+
if (normalizedIncludes.length > 0) {
|
|
3322
|
+
const lc = event.content.toLowerCase();
|
|
3323
|
+
if (!normalizedIncludes.some((needle) => lc.includes(needle)))
|
|
3324
|
+
continue;
|
|
3325
|
+
}
|
|
3326
|
+
if (scope?.metadata && !this.matchesMetadataScope(event.metadata, scope.metadata))
|
|
3327
|
+
continue;
|
|
3328
|
+
const projectHash = this.extractProjectHash(event.metadata);
|
|
3329
|
+
filtered.push({ result, projectHash });
|
|
3330
|
+
}
|
|
3331
|
+
if (projectScopeMode === "global" || allowedProjectHashes.size === 0) {
|
|
3332
|
+
return filtered.map((x) => x.result);
|
|
3333
|
+
}
|
|
3334
|
+
const projectMatched = filtered.filter((x) => x.projectHash && allowedProjectHashes.has(x.projectHash));
|
|
3335
|
+
if (projectScopeMode === "strict") {
|
|
3336
|
+
return projectMatched.map((x) => x.result);
|
|
3337
|
+
}
|
|
3338
|
+
return (projectMatched.length > 0 ? projectMatched : filtered).map((x) => x.result);
|
|
3339
|
+
}
|
|
3340
|
+
extractProjectHash(metadata) {
|
|
3341
|
+
if (!metadata || typeof metadata !== "object")
|
|
3342
|
+
return void 0;
|
|
3343
|
+
const scope = metadata.scope;
|
|
3344
|
+
if (!scope || typeof scope !== "object")
|
|
3345
|
+
return void 0;
|
|
3346
|
+
const project = scope.project;
|
|
3347
|
+
if (!project || typeof project !== "object")
|
|
3348
|
+
return void 0;
|
|
3349
|
+
const hash = project.hash;
|
|
3350
|
+
return typeof hash === "string" && hash.length > 0 ? hash : void 0;
|
|
2764
3351
|
}
|
|
2765
|
-
/**
|
|
2766
|
-
* Retrieve memories from a specific session
|
|
2767
|
-
*/
|
|
2768
3352
|
async retrieveFromSession(sessionId) {
|
|
2769
3353
|
return this.eventStore.getSessionEvents(sessionId);
|
|
2770
3354
|
}
|
|
2771
|
-
/**
|
|
2772
|
-
* Get recent memories across all sessions
|
|
2773
|
-
*/
|
|
2774
3355
|
async retrieveRecent(limit = 100) {
|
|
2775
3356
|
return this.eventStore.getRecentEvents(limit);
|
|
2776
3357
|
}
|
|
2777
|
-
/**
|
|
2778
|
-
* Enrich search results with full event data
|
|
2779
|
-
*/
|
|
2780
3358
|
async enrichResults(results, options) {
|
|
2781
3359
|
const memories = [];
|
|
2782
3360
|
for (const result of results) {
|
|
@@ -2784,27 +3362,16 @@ var Retriever = class {
|
|
|
2784
3362
|
if (!event)
|
|
2785
3363
|
continue;
|
|
2786
3364
|
if (this.graduation) {
|
|
2787
|
-
this.graduation.recordAccess(
|
|
2788
|
-
event.id,
|
|
2789
|
-
options.sessionId || "unknown",
|
|
2790
|
-
result.score
|
|
2791
|
-
);
|
|
3365
|
+
this.graduation.recordAccess(event.id, options.sessionId || "unknown", result.score);
|
|
2792
3366
|
}
|
|
2793
3367
|
let sessionContext;
|
|
2794
3368
|
if (options.includeSessionContext) {
|
|
2795
3369
|
sessionContext = await this.getSessionContext(event.sessionId, event.id);
|
|
2796
3370
|
}
|
|
2797
|
-
memories.push({
|
|
2798
|
-
event,
|
|
2799
|
-
score: result.score,
|
|
2800
|
-
sessionContext
|
|
2801
|
-
});
|
|
3371
|
+
memories.push({ event, score: result.score, sessionContext });
|
|
2802
3372
|
}
|
|
2803
3373
|
return memories;
|
|
2804
3374
|
}
|
|
2805
|
-
/**
|
|
2806
|
-
* Get surrounding context from the same session
|
|
2807
|
-
*/
|
|
2808
3375
|
async getSessionContext(sessionId, eventId) {
|
|
2809
3376
|
const sessionEvents = await this.eventStore.getSessionEvents(sessionId);
|
|
2810
3377
|
const eventIndex = sessionEvents.findIndex((e) => e.id === eventId);
|
|
@@ -2817,55 +3384,86 @@ var Retriever = class {
|
|
|
2817
3384
|
return void 0;
|
|
2818
3385
|
return contextEvents.filter((e) => e.id !== eventId).map((e) => `[${e.eventType}]: ${e.content.slice(0, 200)}...`).join("\n");
|
|
2819
3386
|
}
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
3387
|
+
buildUnifiedContext(projectResult, sharedMemories) {
|
|
3388
|
+
let context = projectResult.context;
|
|
3389
|
+
if (sharedMemories.length === 0)
|
|
3390
|
+
return context;
|
|
3391
|
+
context += "\n\n## Cross-Project Knowledge\n\n";
|
|
3392
|
+
for (const memory of sharedMemories.slice(0, 3)) {
|
|
3393
|
+
context += `### ${memory.title}
|
|
3394
|
+
`;
|
|
3395
|
+
if (memory.symptoms.length > 0)
|
|
3396
|
+
context += `**Symptoms:** ${memory.symptoms.join(", ")}
|
|
3397
|
+
`;
|
|
3398
|
+
context += `**Root Cause:** ${memory.rootCause}
|
|
3399
|
+
`;
|
|
3400
|
+
context += `**Solution:** ${memory.solution}
|
|
3401
|
+
`;
|
|
3402
|
+
if (memory.technologies && memory.technologies.length > 0)
|
|
3403
|
+
context += `**Technologies:** ${memory.technologies.join(", ")}
|
|
3404
|
+
`;
|
|
3405
|
+
context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
|
|
3406
|
+
|
|
3407
|
+
`;
|
|
3408
|
+
}
|
|
3409
|
+
return context;
|
|
3410
|
+
}
|
|
2823
3411
|
buildContext(memories, maxTokens) {
|
|
2824
3412
|
const parts = [];
|
|
2825
3413
|
let currentTokens = 0;
|
|
2826
3414
|
for (const memory of memories) {
|
|
2827
3415
|
const memoryText = this.formatMemory(memory);
|
|
2828
3416
|
const memoryTokens = this.estimateTokens(memoryText);
|
|
2829
|
-
if (currentTokens + memoryTokens > maxTokens)
|
|
3417
|
+
if (currentTokens + memoryTokens > maxTokens)
|
|
2830
3418
|
break;
|
|
2831
|
-
}
|
|
2832
3419
|
parts.push(memoryText);
|
|
2833
3420
|
currentTokens += memoryTokens;
|
|
2834
3421
|
}
|
|
2835
|
-
if (parts.length === 0)
|
|
3422
|
+
if (parts.length === 0)
|
|
2836
3423
|
return "";
|
|
2837
|
-
}
|
|
2838
3424
|
return `## Relevant Memories
|
|
2839
3425
|
|
|
2840
3426
|
${parts.join("\n\n---\n\n")}`;
|
|
2841
3427
|
}
|
|
2842
|
-
/**
|
|
2843
|
-
* Format a single memory for context
|
|
2844
|
-
*/
|
|
2845
3428
|
formatMemory(memory) {
|
|
2846
3429
|
const { event, score, sessionContext } = memory;
|
|
2847
3430
|
const date = event.timestamp.toISOString().split("T")[0];
|
|
2848
3431
|
let text = `**${event.eventType}** (${date}, score: ${score.toFixed(2)})
|
|
2849
3432
|
${event.content}`;
|
|
2850
|
-
if (sessionContext)
|
|
3433
|
+
if (sessionContext)
|
|
2851
3434
|
text += `
|
|
2852
3435
|
|
|
2853
3436
|
_Context:_ ${sessionContext}`;
|
|
2854
|
-
}
|
|
2855
3437
|
return text;
|
|
2856
3438
|
}
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
3439
|
+
matchesMetadataScope(metadata, expected) {
|
|
3440
|
+
if (!metadata)
|
|
3441
|
+
return false;
|
|
3442
|
+
return Object.entries(expected).every(([path6, value]) => {
|
|
3443
|
+
const actual = path6.split(".").reduce((acc, key) => {
|
|
3444
|
+
if (typeof acc !== "object" || acc === null)
|
|
3445
|
+
return void 0;
|
|
3446
|
+
return acc[key];
|
|
3447
|
+
}, metadata);
|
|
3448
|
+
return actual === value;
|
|
3449
|
+
});
|
|
3450
|
+
}
|
|
3451
|
+
tokenize(text) {
|
|
3452
|
+
return text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter((t) => t.length >= 2).slice(0, 64);
|
|
3453
|
+
}
|
|
3454
|
+
keywordOverlap(a, b) {
|
|
3455
|
+
if (a.length === 0 || b.length === 0)
|
|
3456
|
+
return 0;
|
|
3457
|
+
const bs = new Set(b);
|
|
3458
|
+
let hit = 0;
|
|
3459
|
+
for (const t of a)
|
|
3460
|
+
if (bs.has(t))
|
|
3461
|
+
hit += 1;
|
|
3462
|
+
return hit / a.length;
|
|
3463
|
+
}
|
|
2860
3464
|
estimateTokens(text) {
|
|
2861
3465
|
return Math.ceil(text.length / 4);
|
|
2862
3466
|
}
|
|
2863
|
-
/**
|
|
2864
|
-
* Get event age in days (for recency scoring)
|
|
2865
|
-
*/
|
|
2866
|
-
getEventAgeDays(eventId) {
|
|
2867
|
-
return 0;
|
|
2868
|
-
}
|
|
2869
3467
|
};
|
|
2870
3468
|
function createRetriever(eventStore, vectorStore, embedder, matcher) {
|
|
2871
3469
|
return new Retriever(eventStore, vectorStore, embedder, matcher);
|
|
@@ -4155,6 +4753,59 @@ var ConsolidatedStore = class {
|
|
|
4155
4753
|
[memoryId]
|
|
4156
4754
|
);
|
|
4157
4755
|
}
|
|
4756
|
+
/**
|
|
4757
|
+
* Create a long-term rule promoted from stable summaries
|
|
4758
|
+
*/
|
|
4759
|
+
async createRule(input) {
|
|
4760
|
+
const ruleId = randomUUID6();
|
|
4761
|
+
await dbRun(
|
|
4762
|
+
this.db,
|
|
4763
|
+
`INSERT INTO consolidated_rules
|
|
4764
|
+
(rule_id, rule, topics, source_memory_ids, source_events, confidence, created_at)
|
|
4765
|
+
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
|
|
4766
|
+
[
|
|
4767
|
+
ruleId,
|
|
4768
|
+
input.rule,
|
|
4769
|
+
JSON.stringify(input.topics),
|
|
4770
|
+
JSON.stringify(input.sourceMemoryIds),
|
|
4771
|
+
JSON.stringify(input.sourceEvents),
|
|
4772
|
+
input.confidence
|
|
4773
|
+
]
|
|
4774
|
+
);
|
|
4775
|
+
return ruleId;
|
|
4776
|
+
}
|
|
4777
|
+
async getRules(options) {
|
|
4778
|
+
const limit = options?.limit || 100;
|
|
4779
|
+
const rows = await dbAll(
|
|
4780
|
+
this.db,
|
|
4781
|
+
`SELECT * FROM consolidated_rules ORDER BY confidence DESC, created_at DESC LIMIT ?`,
|
|
4782
|
+
[limit]
|
|
4783
|
+
);
|
|
4784
|
+
return rows.map((row) => ({
|
|
4785
|
+
ruleId: row.rule_id,
|
|
4786
|
+
rule: row.rule,
|
|
4787
|
+
topics: JSON.parse(row.topics || "[]"),
|
|
4788
|
+
sourceMemoryIds: JSON.parse(row.source_memory_ids || "[]"),
|
|
4789
|
+
sourceEvents: JSON.parse(row.source_events || "[]"),
|
|
4790
|
+
confidence: Number(row.confidence ?? 0.5),
|
|
4791
|
+
createdAt: toDate(row.created_at) || /* @__PURE__ */ new Date()
|
|
4792
|
+
}));
|
|
4793
|
+
}
|
|
4794
|
+
async countRules() {
|
|
4795
|
+
const result = await dbAll(
|
|
4796
|
+
this.db,
|
|
4797
|
+
`SELECT COUNT(*) as count FROM consolidated_rules`
|
|
4798
|
+
);
|
|
4799
|
+
return result[0]?.count || 0;
|
|
4800
|
+
}
|
|
4801
|
+
async hasRuleForSourceMemory(memoryId) {
|
|
4802
|
+
const rows = await dbAll(
|
|
4803
|
+
this.db,
|
|
4804
|
+
`SELECT COUNT(*) as count FROM consolidated_rules WHERE source_memory_ids LIKE ?`,
|
|
4805
|
+
[`%"${memoryId}"%`]
|
|
4806
|
+
);
|
|
4807
|
+
return (rows[0]?.count || 0) > 0;
|
|
4808
|
+
}
|
|
4158
4809
|
/**
|
|
4159
4810
|
* Get count of consolidated memories
|
|
4160
4811
|
*/
|
|
@@ -4304,7 +4955,14 @@ var ConsolidationWorker = class {
|
|
|
4304
4955
|
* Force a consolidation run (manual trigger)
|
|
4305
4956
|
*/
|
|
4306
4957
|
async forceRun() {
|
|
4307
|
-
|
|
4958
|
+
const out = await this.consolidateWithReport();
|
|
4959
|
+
return out.consolidatedCount;
|
|
4960
|
+
}
|
|
4961
|
+
/**
|
|
4962
|
+
* Force a consolidation run and return metrics report
|
|
4963
|
+
*/
|
|
4964
|
+
async forceRunWithReport() {
|
|
4965
|
+
return this.consolidateWithReport();
|
|
4308
4966
|
}
|
|
4309
4967
|
/**
|
|
4310
4968
|
* Schedule the next consolidation check
|
|
@@ -4344,12 +5002,21 @@ var ConsolidationWorker = class {
|
|
|
4344
5002
|
* Perform consolidation
|
|
4345
5003
|
*/
|
|
4346
5004
|
async consolidate() {
|
|
5005
|
+
const out = await this.consolidateWithReport();
|
|
5006
|
+
return out.consolidatedCount;
|
|
5007
|
+
}
|
|
5008
|
+
async consolidateWithReport() {
|
|
4347
5009
|
const workingSet = await this.workingSetStore.get();
|
|
4348
5010
|
if (workingSet.recentEvents.length < 3) {
|
|
4349
|
-
return
|
|
5011
|
+
return {
|
|
5012
|
+
consolidatedCount: 0,
|
|
5013
|
+
promotedRuleCount: 0,
|
|
5014
|
+
report: this.buildCostQualityReport(workingSet.recentEvents, [], 0)
|
|
5015
|
+
};
|
|
4350
5016
|
}
|
|
4351
5017
|
const groups = this.groupByTopic(workingSet.recentEvents);
|
|
4352
5018
|
let consolidatedCount = 0;
|
|
5019
|
+
const createdMemoryIds = [];
|
|
4353
5020
|
for (const group of groups) {
|
|
4354
5021
|
if (group.events.length < 3)
|
|
4355
5022
|
continue;
|
|
@@ -4358,14 +5025,16 @@ var ConsolidationWorker = class {
|
|
|
4358
5025
|
if (alreadyConsolidated)
|
|
4359
5026
|
continue;
|
|
4360
5027
|
const summary = await this.summarize(group);
|
|
4361
|
-
await this.consolidatedStore.create({
|
|
5028
|
+
const memoryId = await this.consolidatedStore.create({
|
|
4362
5029
|
summary,
|
|
4363
5030
|
topics: group.topics,
|
|
4364
5031
|
sourceEvents: eventIds,
|
|
4365
5032
|
confidence: this.calculateConfidence(group)
|
|
4366
5033
|
});
|
|
5034
|
+
createdMemoryIds.push(memoryId);
|
|
4367
5035
|
consolidatedCount++;
|
|
4368
5036
|
}
|
|
5037
|
+
const promotedRuleCount = await this.promoteStableSummariesToRules(createdMemoryIds);
|
|
4369
5038
|
if (consolidatedCount > 0) {
|
|
4370
5039
|
const consolidatedEventIds = groups.filter((g) => g.events.length >= 3).flatMap((g) => g.events.map((e) => e.id));
|
|
4371
5040
|
const oldEventIds = consolidatedEventIds.filter((id) => {
|
|
@@ -4379,7 +5048,61 @@ var ConsolidationWorker = class {
|
|
|
4379
5048
|
await this.workingSetStore.prune(oldEventIds);
|
|
4380
5049
|
}
|
|
4381
5050
|
}
|
|
4382
|
-
|
|
5051
|
+
const report = this.buildCostQualityReport(workingSet.recentEvents, groups, consolidatedCount);
|
|
5052
|
+
return { consolidatedCount, promotedRuleCount, report };
|
|
5053
|
+
}
|
|
5054
|
+
async promoteStableSummariesToRules(memoryIds) {
|
|
5055
|
+
let promoted = 0;
|
|
5056
|
+
for (const memoryId of memoryIds) {
|
|
5057
|
+
const memory = await this.consolidatedStore.get(memoryId);
|
|
5058
|
+
if (!memory)
|
|
5059
|
+
continue;
|
|
5060
|
+
if (memory.confidence < 0.55)
|
|
5061
|
+
continue;
|
|
5062
|
+
if (memory.sourceEvents.length < 4)
|
|
5063
|
+
continue;
|
|
5064
|
+
const exists = await this.consolidatedStore.hasRuleForSourceMemory(memoryId);
|
|
5065
|
+
if (exists)
|
|
5066
|
+
continue;
|
|
5067
|
+
const rule = this.buildRuleFromSummary(memory.summary, memory.topics);
|
|
5068
|
+
if (!rule)
|
|
5069
|
+
continue;
|
|
5070
|
+
await this.consolidatedStore.createRule({
|
|
5071
|
+
rule,
|
|
5072
|
+
topics: memory.topics,
|
|
5073
|
+
sourceMemoryIds: [memory.memoryId],
|
|
5074
|
+
sourceEvents: memory.sourceEvents,
|
|
5075
|
+
confidence: Math.min(1, memory.confidence + 0.08)
|
|
5076
|
+
});
|
|
5077
|
+
promoted++;
|
|
5078
|
+
}
|
|
5079
|
+
return promoted;
|
|
5080
|
+
}
|
|
5081
|
+
buildRuleFromSummary(summary, topics) {
|
|
5082
|
+
const lines = summary.split(/\r?\n/).map((l) => l.trim()).filter(Boolean).filter((l) => !l.toLowerCase().startsWith("topics:"));
|
|
5083
|
+
const bullet = lines.find((l) => l.startsWith("- "))?.replace(/^-\s*/, "");
|
|
5084
|
+
const seed = bullet || lines[0];
|
|
5085
|
+
if (!seed || seed.length < 8)
|
|
5086
|
+
return null;
|
|
5087
|
+
const topicPrefix = topics.length > 0 ? `[${topics.slice(0, 2).join(", ")}] ` : "";
|
|
5088
|
+
return `${topicPrefix}${seed}`;
|
|
5089
|
+
}
|
|
5090
|
+
buildCostQualityReport(events, groups, consolidatedCount) {
|
|
5091
|
+
const beforeTokenEstimate = events.reduce((acc, e) => acc + this.estimateTokens(e.content), 0);
|
|
5092
|
+
const afterSummaries = groups.filter((g) => g.events.length >= 3).slice(0, Math.max(consolidatedCount, 1));
|
|
5093
|
+
const afterTokenEstimate = afterSummaries.length > 0 ? afterSummaries.reduce((acc, g) => acc + this.estimateTokens(this.ruleBasedSummary(g)), 0) : beforeTokenEstimate;
|
|
5094
|
+
const reductionRatio = beforeTokenEstimate > 0 ? Math.max(0, (beforeTokenEstimate - afterTokenEstimate) / beforeTokenEstimate) : 0;
|
|
5095
|
+
const qualityGuardPassed = consolidatedCount === 0 ? true : groups.filter((g) => g.events.length >= 3).every((g) => this.calculateConfidence(g) >= 0.55);
|
|
5096
|
+
return {
|
|
5097
|
+
beforeTokenEstimate,
|
|
5098
|
+
afterTokenEstimate,
|
|
5099
|
+
reductionRatio,
|
|
5100
|
+
qualityGuardPassed,
|
|
5101
|
+
details: `groups=${groups.length}, consolidated=${consolidatedCount}`
|
|
5102
|
+
};
|
|
5103
|
+
}
|
|
5104
|
+
estimateTokens(text) {
|
|
5105
|
+
return Math.ceil((text || "").length / 4);
|
|
4383
5106
|
}
|
|
4384
5107
|
/**
|
|
4385
5108
|
* Check if consolidation should run
|
|
@@ -4939,13 +5662,185 @@ function createGraduationWorker(eventStore, graduation, config) {
|
|
|
4939
5662
|
);
|
|
4940
5663
|
}
|
|
4941
5664
|
|
|
5665
|
+
// src/core/md-mirror.ts
|
|
5666
|
+
import * as fs3 from "node:fs";
|
|
5667
|
+
import * as path2 from "node:path";
|
|
5668
|
+
function sanitizeSegment2(input, fallback) {
|
|
5669
|
+
const v = (input || "").trim().toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
5670
|
+
return v || fallback;
|
|
5671
|
+
}
|
|
5672
|
+
function getAtPath(obj, dotted) {
|
|
5673
|
+
if (!obj)
|
|
5674
|
+
return void 0;
|
|
5675
|
+
return dotted.split(".").reduce((acc, key) => {
|
|
5676
|
+
if (!acc || typeof acc !== "object")
|
|
5677
|
+
return void 0;
|
|
5678
|
+
return acc[key];
|
|
5679
|
+
}, obj);
|
|
5680
|
+
}
|
|
5681
|
+
function buildMirrorPath2(rootDir, event) {
|
|
5682
|
+
const meta = event.metadata;
|
|
5683
|
+
const namespaceRaw = getAtPath(meta, "namespace") ?? getAtPath(meta, "scope.namespace") ?? event.eventType;
|
|
5684
|
+
const namespace = sanitizeSegment2(typeof namespaceRaw === "string" ? namespaceRaw : void 0, "general");
|
|
5685
|
+
const categoryRaw = getAtPath(meta, "categoryPath") ?? getAtPath(meta, "scope.categoryPath");
|
|
5686
|
+
const categoryPath = Array.isArray(categoryRaw) && categoryRaw.length > 0 ? categoryRaw.map((x) => sanitizeSegment2(typeof x === "string" ? x : void 0, "uncategorized")) : ["uncategorized"];
|
|
5687
|
+
const d = event.timestamp;
|
|
5688
|
+
const yyyy = d.getFullYear();
|
|
5689
|
+
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
5690
|
+
const dd = String(d.getDate()).padStart(2, "0");
|
|
5691
|
+
return path2.join(rootDir, "memory", namespace, ...categoryPath, `${yyyy}-${mm}-${dd}.md`);
|
|
5692
|
+
}
|
|
5693
|
+
var MarkdownMirror2 = class {
|
|
5694
|
+
constructor(rootDir) {
|
|
5695
|
+
this.rootDir = rootDir;
|
|
5696
|
+
}
|
|
5697
|
+
async append(event, eventId) {
|
|
5698
|
+
const out = buildMirrorPath2(this.rootDir, event);
|
|
5699
|
+
fs3.mkdirSync(path2.dirname(out), { recursive: true });
|
|
5700
|
+
const lines = [
|
|
5701
|
+
"",
|
|
5702
|
+
`## ${event.timestamp.toISOString()} | ${eventId ?? "pending-id"}`,
|
|
5703
|
+
`- type: ${event.eventType}`,
|
|
5704
|
+
`- session: ${event.sessionId}`,
|
|
5705
|
+
event.content
|
|
5706
|
+
];
|
|
5707
|
+
await fs3.promises.appendFile(out, lines.join("\n"), "utf8");
|
|
5708
|
+
await this.refreshIndex();
|
|
5709
|
+
}
|
|
5710
|
+
async refreshIndex() {
|
|
5711
|
+
const memoryRoot = path2.join(this.rootDir, "memory");
|
|
5712
|
+
await fs3.promises.mkdir(memoryRoot, { recursive: true });
|
|
5713
|
+
const files = [];
|
|
5714
|
+
await this.walk(memoryRoot, files);
|
|
5715
|
+
const mdFiles = files.filter((f) => f.endsWith(".md")).map((f) => path2.relative(this.rootDir, f)).filter((rel) => rel !== path2.join("memory", "_index.md")).sort();
|
|
5716
|
+
const index = [
|
|
5717
|
+
"# Memory Index",
|
|
5718
|
+
"",
|
|
5719
|
+
"Generated automatically by MarkdownMirror.",
|
|
5720
|
+
"",
|
|
5721
|
+
...mdFiles.map((rel) => `- ${rel}`),
|
|
5722
|
+
""
|
|
5723
|
+
].join("\n");
|
|
5724
|
+
await fs3.promises.writeFile(path2.join(memoryRoot, "_index.md"), index, "utf8");
|
|
5725
|
+
}
|
|
5726
|
+
async walk(dir, out) {
|
|
5727
|
+
const entries = await fs3.promises.readdir(dir, { withFileTypes: true });
|
|
5728
|
+
for (const e of entries) {
|
|
5729
|
+
const full = path2.join(dir, e.name);
|
|
5730
|
+
if (e.isDirectory()) {
|
|
5731
|
+
await this.walk(full, out);
|
|
5732
|
+
} else {
|
|
5733
|
+
out.push(full);
|
|
5734
|
+
}
|
|
5735
|
+
}
|
|
5736
|
+
}
|
|
5737
|
+
};
|
|
5738
|
+
|
|
5739
|
+
// src/core/ingest-interceptor.ts
|
|
5740
|
+
var IngestInterceptorRegistry = class {
|
|
5741
|
+
before = [];
|
|
5742
|
+
after = [];
|
|
5743
|
+
onError = [];
|
|
5744
|
+
registerBefore(interceptor) {
|
|
5745
|
+
this.before.push(interceptor);
|
|
5746
|
+
return () => {
|
|
5747
|
+
this.before = this.before.filter((i) => i !== interceptor);
|
|
5748
|
+
};
|
|
5749
|
+
}
|
|
5750
|
+
registerAfter(interceptor) {
|
|
5751
|
+
this.after.push(interceptor);
|
|
5752
|
+
return () => {
|
|
5753
|
+
this.after = this.after.filter((i) => i !== interceptor);
|
|
5754
|
+
};
|
|
5755
|
+
}
|
|
5756
|
+
registerOnError(interceptor) {
|
|
5757
|
+
this.onError.push(interceptor);
|
|
5758
|
+
return () => {
|
|
5759
|
+
this.onError = this.onError.filter((i) => i !== interceptor);
|
|
5760
|
+
};
|
|
5761
|
+
}
|
|
5762
|
+
async run(stage, context) {
|
|
5763
|
+
const interceptors = stage === "before" ? this.before : stage === "after" ? this.after : this.onError;
|
|
5764
|
+
for (const interceptor of interceptors) {
|
|
5765
|
+
await interceptor({ ...context, stage });
|
|
5766
|
+
}
|
|
5767
|
+
}
|
|
5768
|
+
};
|
|
5769
|
+
function mergeHierarchicalMetadata(base, patch) {
|
|
5770
|
+
if (!base && !patch)
|
|
5771
|
+
return void 0;
|
|
5772
|
+
if (!base)
|
|
5773
|
+
return patch;
|
|
5774
|
+
if (!patch)
|
|
5775
|
+
return base;
|
|
5776
|
+
const result = { ...base };
|
|
5777
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
5778
|
+
const current = result[key];
|
|
5779
|
+
if (typeof current === "object" && current !== null && !Array.isArray(current) && typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
5780
|
+
result[key] = mergeHierarchicalMetadata(
|
|
5781
|
+
current,
|
|
5782
|
+
value
|
|
5783
|
+
);
|
|
5784
|
+
} else {
|
|
5785
|
+
result[key] = value;
|
|
5786
|
+
}
|
|
5787
|
+
}
|
|
5788
|
+
return result;
|
|
5789
|
+
}
|
|
5790
|
+
|
|
5791
|
+
// src/core/tag-taxonomy.ts
|
|
5792
|
+
var TAG_NAMESPACES = {
|
|
5793
|
+
SYSTEM: "sys:",
|
|
5794
|
+
QUALITY: "q:",
|
|
5795
|
+
PROJECT: "proj:",
|
|
5796
|
+
TOPIC: "topic:",
|
|
5797
|
+
TEMPORAL: "t:",
|
|
5798
|
+
USER: "user:",
|
|
5799
|
+
AGENT: "agent:"
|
|
5800
|
+
};
|
|
5801
|
+
var VALID_TAG_NAMESPACES = new Set(Object.values(TAG_NAMESPACES));
|
|
5802
|
+
function parseTag(tag) {
|
|
5803
|
+
const value = (tag || "").trim();
|
|
5804
|
+
const idx = value.indexOf(":");
|
|
5805
|
+
if (idx <= 0)
|
|
5806
|
+
return { value };
|
|
5807
|
+
const namespace = `${value.slice(0, idx)}:`;
|
|
5808
|
+
const tagValue = value.slice(idx + 1);
|
|
5809
|
+
if (!tagValue)
|
|
5810
|
+
return { value };
|
|
5811
|
+
return { namespace, value: tagValue };
|
|
5812
|
+
}
|
|
5813
|
+
function validateTag(tag) {
|
|
5814
|
+
const normalized = (tag || "").trim();
|
|
5815
|
+
if (!normalized)
|
|
5816
|
+
return false;
|
|
5817
|
+
const { namespace } = parseTag(normalized);
|
|
5818
|
+
if (!namespace)
|
|
5819
|
+
return true;
|
|
5820
|
+
return VALID_TAG_NAMESPACES.has(namespace);
|
|
5821
|
+
}
|
|
5822
|
+
function normalizeTags(tags) {
|
|
5823
|
+
if (!Array.isArray(tags))
|
|
5824
|
+
return [];
|
|
5825
|
+
const dedup = /* @__PURE__ */ new Set();
|
|
5826
|
+
for (const item of tags) {
|
|
5827
|
+
if (typeof item !== "string")
|
|
5828
|
+
continue;
|
|
5829
|
+
const normalized = item.trim();
|
|
5830
|
+
if (!validateTag(normalized))
|
|
5831
|
+
continue;
|
|
5832
|
+
dedup.add(normalized);
|
|
5833
|
+
}
|
|
5834
|
+
return [...dedup];
|
|
5835
|
+
}
|
|
5836
|
+
|
|
4942
5837
|
// src/services/memory-service.ts
|
|
4943
5838
|
function normalizePath(projectPath) {
|
|
4944
|
-
const expanded = projectPath.startsWith("~") ?
|
|
5839
|
+
const expanded = projectPath.startsWith("~") ? path3.join(os.homedir(), projectPath.slice(1)) : projectPath;
|
|
4945
5840
|
try {
|
|
4946
|
-
return
|
|
5841
|
+
return fs4.realpathSync(expanded);
|
|
4947
5842
|
} catch {
|
|
4948
|
-
return
|
|
5843
|
+
return path3.resolve(expanded);
|
|
4949
5844
|
}
|
|
4950
5845
|
}
|
|
4951
5846
|
function hashProjectPath(projectPath) {
|
|
@@ -4954,14 +5849,14 @@ function hashProjectPath(projectPath) {
|
|
|
4954
5849
|
}
|
|
4955
5850
|
function getProjectStoragePath(projectPath) {
|
|
4956
5851
|
const hash = hashProjectPath(projectPath);
|
|
4957
|
-
return
|
|
5852
|
+
return path3.join(os.homedir(), ".claude-code", "memory", "projects", hash);
|
|
4958
5853
|
}
|
|
4959
|
-
var REGISTRY_PATH =
|
|
4960
|
-
var SHARED_STORAGE_PATH =
|
|
5854
|
+
var REGISTRY_PATH = path3.join(os.homedir(), ".claude-code", "memory", "session-registry.json");
|
|
5855
|
+
var SHARED_STORAGE_PATH = path3.join(os.homedir(), ".claude-code", "memory", "shared");
|
|
4961
5856
|
function loadSessionRegistry() {
|
|
4962
5857
|
try {
|
|
4963
|
-
if (
|
|
4964
|
-
const data =
|
|
5858
|
+
if (fs4.existsSync(REGISTRY_PATH)) {
|
|
5859
|
+
const data = fs4.readFileSync(REGISTRY_PATH, "utf-8");
|
|
4965
5860
|
return JSON.parse(data);
|
|
4966
5861
|
}
|
|
4967
5862
|
} catch (error) {
|
|
@@ -4983,6 +5878,7 @@ var MemoryService = class {
|
|
|
4983
5878
|
vectorWorker = null;
|
|
4984
5879
|
graduationWorker = null;
|
|
4985
5880
|
initialized = false;
|
|
5881
|
+
ingestInterceptors = new IngestInterceptorRegistry();
|
|
4986
5882
|
// Endless Mode components
|
|
4987
5883
|
workingSetStore = null;
|
|
4988
5884
|
consolidatedStore = null;
|
|
@@ -4996,20 +5892,27 @@ var MemoryService = class {
|
|
|
4996
5892
|
sharedPromoter = null;
|
|
4997
5893
|
sharedStoreConfig = null;
|
|
4998
5894
|
projectHash = null;
|
|
5895
|
+
projectPath = null;
|
|
4999
5896
|
readOnly;
|
|
5000
5897
|
lightweightMode;
|
|
5898
|
+
mdMirror;
|
|
5001
5899
|
constructor(config) {
|
|
5002
5900
|
const storagePath = this.expandPath(config.storagePath);
|
|
5003
5901
|
this.readOnly = config.readOnly ?? false;
|
|
5004
5902
|
this.lightweightMode = config.lightweightMode ?? false;
|
|
5005
|
-
|
|
5006
|
-
|
|
5903
|
+
this.mdMirror = new MarkdownMirror2(process.cwd());
|
|
5904
|
+
if (!this.readOnly && !fs4.existsSync(storagePath)) {
|
|
5905
|
+
fs4.mkdirSync(storagePath, { recursive: true });
|
|
5007
5906
|
}
|
|
5008
5907
|
this.projectHash = config.projectHash || null;
|
|
5908
|
+
this.projectPath = config.projectPath || null;
|
|
5009
5909
|
this.sharedStoreConfig = config.sharedStoreConfig ?? { enabled: true };
|
|
5010
5910
|
this.sqliteStore = new SQLiteEventStore(
|
|
5011
|
-
|
|
5012
|
-
{
|
|
5911
|
+
path3.join(storagePath, "events.sqlite"),
|
|
5912
|
+
{
|
|
5913
|
+
readonly: this.readOnly,
|
|
5914
|
+
markdownMirrorRoot: storagePath
|
|
5915
|
+
}
|
|
5013
5916
|
);
|
|
5014
5917
|
const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
|
|
5015
5918
|
if (!analyticsEnabled) {
|
|
@@ -5017,7 +5920,7 @@ var MemoryService = class {
|
|
|
5017
5920
|
} else if (this.readOnly) {
|
|
5018
5921
|
try {
|
|
5019
5922
|
this.analyticsStore = new EventStore(
|
|
5020
|
-
|
|
5923
|
+
path3.join(storagePath, "analytics.duckdb"),
|
|
5021
5924
|
{ readOnly: true }
|
|
5022
5925
|
);
|
|
5023
5926
|
} catch {
|
|
@@ -5025,11 +5928,11 @@ var MemoryService = class {
|
|
|
5025
5928
|
}
|
|
5026
5929
|
} else {
|
|
5027
5930
|
this.analyticsStore = new EventStore(
|
|
5028
|
-
|
|
5931
|
+
path3.join(storagePath, "analytics.duckdb"),
|
|
5029
5932
|
{ readOnly: false }
|
|
5030
5933
|
);
|
|
5031
5934
|
}
|
|
5032
|
-
this.vectorStore = new VectorStore(
|
|
5935
|
+
this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
|
|
5033
5936
|
this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
|
|
5034
5937
|
this.matcher = getDefaultMatcher();
|
|
5035
5938
|
this.retriever = createRetriever(
|
|
@@ -5039,6 +5942,7 @@ var MemoryService = class {
|
|
|
5039
5942
|
this.embedder,
|
|
5040
5943
|
this.matcher
|
|
5041
5944
|
);
|
|
5945
|
+
this.retriever.setQueryRewriter((q) => this.rewriteQueryIntent(q));
|
|
5042
5946
|
this.graduation = createGraduationPipeline(this.sqliteStore);
|
|
5043
5947
|
}
|
|
5044
5948
|
/**
|
|
@@ -5098,16 +6002,16 @@ var MemoryService = class {
|
|
|
5098
6002
|
*/
|
|
5099
6003
|
async initializeSharedStore() {
|
|
5100
6004
|
const sharedPath = this.sharedStoreConfig?.sharedStoragePath ? this.expandPath(this.sharedStoreConfig.sharedStoragePath) : SHARED_STORAGE_PATH;
|
|
5101
|
-
if (!
|
|
5102
|
-
|
|
6005
|
+
if (!fs4.existsSync(sharedPath)) {
|
|
6006
|
+
fs4.mkdirSync(sharedPath, { recursive: true });
|
|
5103
6007
|
}
|
|
5104
6008
|
this.sharedEventStore = createSharedEventStore(
|
|
5105
|
-
|
|
6009
|
+
path3.join(sharedPath, "shared.duckdb")
|
|
5106
6010
|
);
|
|
5107
6011
|
await this.sharedEventStore.initialize();
|
|
5108
6012
|
this.sharedStore = createSharedStore(this.sharedEventStore);
|
|
5109
6013
|
this.sharedVectorStore = createSharedVectorStore(
|
|
5110
|
-
|
|
6014
|
+
path3.join(sharedPath, "vectors")
|
|
5111
6015
|
);
|
|
5112
6016
|
await this.sharedVectorStore.initialize();
|
|
5113
6017
|
this.sharedPromoter = createSharedPromoter(
|
|
@@ -5118,6 +6022,86 @@ var MemoryService = class {
|
|
|
5118
6022
|
);
|
|
5119
6023
|
this.retriever.setSharedStores(this.sharedStore, this.sharedVectorStore);
|
|
5120
6024
|
}
|
|
6025
|
+
registerIngestBefore(interceptor) {
|
|
6026
|
+
return this.ingestInterceptors.registerBefore(interceptor);
|
|
6027
|
+
}
|
|
6028
|
+
registerIngestAfter(interceptor) {
|
|
6029
|
+
return this.ingestInterceptors.registerAfter(interceptor);
|
|
6030
|
+
}
|
|
6031
|
+
registerIngestOnError(interceptor) {
|
|
6032
|
+
return this.ingestInterceptors.registerOnError(interceptor);
|
|
6033
|
+
}
|
|
6034
|
+
async ingestWithInterceptors(operation, input, onSuccess) {
|
|
6035
|
+
const normalizedInput = {
|
|
6036
|
+
...input,
|
|
6037
|
+
metadata: mergeHierarchicalMetadata(
|
|
6038
|
+
{
|
|
6039
|
+
ingest: {
|
|
6040
|
+
operation,
|
|
6041
|
+
pipeline: "default",
|
|
6042
|
+
ts: (/* @__PURE__ */ new Date()).toISOString()
|
|
6043
|
+
},
|
|
6044
|
+
...this.projectHash ? {
|
|
6045
|
+
scope: {
|
|
6046
|
+
project: {
|
|
6047
|
+
hash: this.projectHash,
|
|
6048
|
+
...this.projectPath ? { path: this.projectPath } : {}
|
|
6049
|
+
}
|
|
6050
|
+
},
|
|
6051
|
+
tags: [`proj:${this.projectHash}`]
|
|
6052
|
+
} : {}
|
|
6053
|
+
},
|
|
6054
|
+
input.metadata
|
|
6055
|
+
)
|
|
6056
|
+
};
|
|
6057
|
+
if (this.projectHash && normalizedInput.metadata) {
|
|
6058
|
+
const meta = normalizedInput.metadata;
|
|
6059
|
+
const currentTags = Array.isArray(meta.tags) ? meta.tags.filter((x) => typeof x === "string") : [];
|
|
6060
|
+
const projectTag = `proj:${this.projectHash}`;
|
|
6061
|
+
if (!currentTags.includes(projectTag)) {
|
|
6062
|
+
meta.tags = [...currentTags, projectTag];
|
|
6063
|
+
}
|
|
6064
|
+
}
|
|
6065
|
+
if (normalizedInput.metadata) {
|
|
6066
|
+
const meta = normalizedInput.metadata;
|
|
6067
|
+
const normalizedTags = normalizeTags(meta.tags);
|
|
6068
|
+
if (normalizedTags.length > 0) {
|
|
6069
|
+
meta.tags = normalizedTags;
|
|
6070
|
+
}
|
|
6071
|
+
}
|
|
6072
|
+
await this.ingestInterceptors.run("before", {
|
|
6073
|
+
operation,
|
|
6074
|
+
sessionId: normalizedInput.sessionId,
|
|
6075
|
+
event: normalizedInput
|
|
6076
|
+
});
|
|
6077
|
+
try {
|
|
6078
|
+
const result = await this.sqliteStore.append(normalizedInput);
|
|
6079
|
+
if (result.success && !result.isDuplicate) {
|
|
6080
|
+
if (onSuccess) {
|
|
6081
|
+
await onSuccess(result.eventId);
|
|
6082
|
+
}
|
|
6083
|
+
try {
|
|
6084
|
+
await this.mdMirror.append(normalizedInput, result.eventId);
|
|
6085
|
+
} catch {
|
|
6086
|
+
}
|
|
6087
|
+
}
|
|
6088
|
+
await this.ingestInterceptors.run("after", {
|
|
6089
|
+
operation,
|
|
6090
|
+
sessionId: normalizedInput.sessionId,
|
|
6091
|
+
event: normalizedInput
|
|
6092
|
+
});
|
|
6093
|
+
return result;
|
|
6094
|
+
} catch (error) {
|
|
6095
|
+
const normalizedError = error instanceof Error ? error : new Error(String(error));
|
|
6096
|
+
await this.ingestInterceptors.run("error", {
|
|
6097
|
+
operation,
|
|
6098
|
+
sessionId: normalizedInput.sessionId,
|
|
6099
|
+
event: normalizedInput,
|
|
6100
|
+
error: normalizedError
|
|
6101
|
+
});
|
|
6102
|
+
throw error;
|
|
6103
|
+
}
|
|
6104
|
+
}
|
|
5121
6105
|
/**
|
|
5122
6106
|
* Start a new session
|
|
5123
6107
|
*/
|
|
@@ -5145,50 +6129,57 @@ var MemoryService = class {
|
|
|
5145
6129
|
*/
|
|
5146
6130
|
async storeUserPrompt(sessionId, content, metadata) {
|
|
5147
6131
|
await this.initialize();
|
|
5148
|
-
|
|
5149
|
-
|
|
5150
|
-
|
|
5151
|
-
|
|
5152
|
-
|
|
5153
|
-
|
|
5154
|
-
|
|
5155
|
-
|
|
5156
|
-
|
|
5157
|
-
|
|
5158
|
-
|
|
6132
|
+
return this.ingestWithInterceptors(
|
|
6133
|
+
"user_prompt",
|
|
6134
|
+
{
|
|
6135
|
+
eventType: "user_prompt",
|
|
6136
|
+
sessionId,
|
|
6137
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
6138
|
+
content,
|
|
6139
|
+
metadata
|
|
6140
|
+
},
|
|
6141
|
+
async (eventId) => {
|
|
6142
|
+
await this.sqliteStore.enqueueForEmbedding(eventId, content);
|
|
6143
|
+
}
|
|
6144
|
+
);
|
|
5159
6145
|
}
|
|
5160
6146
|
/**
|
|
5161
6147
|
* Store an agent response
|
|
5162
6148
|
*/
|
|
5163
6149
|
async storeAgentResponse(sessionId, content, metadata) {
|
|
5164
6150
|
await this.initialize();
|
|
5165
|
-
|
|
5166
|
-
|
|
5167
|
-
|
|
5168
|
-
|
|
5169
|
-
|
|
5170
|
-
|
|
5171
|
-
|
|
5172
|
-
|
|
5173
|
-
|
|
5174
|
-
|
|
5175
|
-
|
|
6151
|
+
return this.ingestWithInterceptors(
|
|
6152
|
+
"agent_response",
|
|
6153
|
+
{
|
|
6154
|
+
eventType: "agent_response",
|
|
6155
|
+
sessionId,
|
|
6156
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
6157
|
+
content,
|
|
6158
|
+
metadata
|
|
6159
|
+
},
|
|
6160
|
+
async (eventId) => {
|
|
6161
|
+
await this.sqliteStore.enqueueForEmbedding(eventId, content);
|
|
6162
|
+
}
|
|
6163
|
+
);
|
|
5176
6164
|
}
|
|
5177
6165
|
/**
|
|
5178
6166
|
* Store a session summary
|
|
5179
6167
|
*/
|
|
5180
|
-
async storeSessionSummary(sessionId, summary) {
|
|
6168
|
+
async storeSessionSummary(sessionId, summary, metadata) {
|
|
5181
6169
|
await this.initialize();
|
|
5182
|
-
|
|
5183
|
-
|
|
5184
|
-
|
|
5185
|
-
|
|
5186
|
-
|
|
5187
|
-
|
|
5188
|
-
|
|
5189
|
-
|
|
5190
|
-
|
|
5191
|
-
|
|
6170
|
+
return this.ingestWithInterceptors(
|
|
6171
|
+
"session_summary",
|
|
6172
|
+
{
|
|
6173
|
+
eventType: "session_summary",
|
|
6174
|
+
sessionId,
|
|
6175
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
6176
|
+
content: summary,
|
|
6177
|
+
metadata
|
|
6178
|
+
},
|
|
6179
|
+
async (eventId) => {
|
|
6180
|
+
await this.sqliteStore.enqueueForEmbedding(eventId, summary);
|
|
6181
|
+
}
|
|
6182
|
+
);
|
|
5192
6183
|
}
|
|
5193
6184
|
/**
|
|
5194
6185
|
* Store a tool observation
|
|
@@ -5197,40 +6188,181 @@ var MemoryService = class {
|
|
|
5197
6188
|
await this.initialize();
|
|
5198
6189
|
const content = JSON.stringify(payload);
|
|
5199
6190
|
const turnId = payload.metadata?.turnId;
|
|
5200
|
-
|
|
5201
|
-
|
|
5202
|
-
|
|
5203
|
-
|
|
5204
|
-
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
6191
|
+
return this.ingestWithInterceptors(
|
|
6192
|
+
"tool_observation",
|
|
6193
|
+
{
|
|
6194
|
+
eventType: "tool_observation",
|
|
6195
|
+
sessionId,
|
|
6196
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
6197
|
+
content,
|
|
6198
|
+
metadata: {
|
|
6199
|
+
toolName: payload.toolName,
|
|
6200
|
+
success: payload.success,
|
|
6201
|
+
...turnId ? { turnId } : {}
|
|
6202
|
+
}
|
|
6203
|
+
},
|
|
6204
|
+
async (eventId) => {
|
|
6205
|
+
const embeddingContent = createToolObservationEmbedding(
|
|
6206
|
+
payload.toolName,
|
|
6207
|
+
payload.metadata || {},
|
|
6208
|
+
payload.success
|
|
6209
|
+
);
|
|
6210
|
+
await this.sqliteStore.enqueueForEmbedding(eventId, embeddingContent);
|
|
5209
6211
|
}
|
|
5210
|
-
|
|
5211
|
-
if (result.success && !result.isDuplicate) {
|
|
5212
|
-
const embeddingContent = createToolObservationEmbedding(
|
|
5213
|
-
payload.toolName,
|
|
5214
|
-
payload.metadata || {},
|
|
5215
|
-
payload.success
|
|
5216
|
-
);
|
|
5217
|
-
await this.sqliteStore.enqueueForEmbedding(result.eventId, embeddingContent);
|
|
5218
|
-
}
|
|
5219
|
-
return result;
|
|
6212
|
+
);
|
|
5220
6213
|
}
|
|
5221
6214
|
/**
|
|
5222
6215
|
* Retrieve relevant memories for a query
|
|
5223
6216
|
*/
|
|
5224
6217
|
async retrieveMemories(query, options) {
|
|
5225
6218
|
await this.initialize();
|
|
6219
|
+
const rerankWeights = await this.getRerankWeights(options?.adaptiveRerank === true);
|
|
6220
|
+
let result;
|
|
5226
6221
|
if (options?.includeShared && this.sharedStore) {
|
|
5227
|
-
|
|
6222
|
+
result = await this.retriever.retrieveUnified(query, {
|
|
5228
6223
|
...options,
|
|
6224
|
+
intentRewrite: options?.intentRewrite === true,
|
|
6225
|
+
rerankWeights,
|
|
5229
6226
|
includeShared: true,
|
|
5230
|
-
projectHash: this.projectHash || void 0
|
|
6227
|
+
projectHash: this.projectHash || void 0,
|
|
6228
|
+
projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
|
|
6229
|
+
allowedProjectHashes: options?.allowedProjectHashes
|
|
6230
|
+
});
|
|
6231
|
+
} else {
|
|
6232
|
+
result = await this.retriever.retrieve(query, {
|
|
6233
|
+
...options,
|
|
6234
|
+
intentRewrite: options?.intentRewrite === true,
|
|
6235
|
+
rerankWeights,
|
|
6236
|
+
projectHash: this.projectHash || void 0,
|
|
6237
|
+
projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
|
|
6238
|
+
allowedProjectHashes: options?.allowedProjectHashes
|
|
6239
|
+
});
|
|
6240
|
+
}
|
|
6241
|
+
try {
|
|
6242
|
+
const selectedEventIds = result.memories.map((m) => m.event.id);
|
|
6243
|
+
const selectedDetails = (result.selectedDebug || []).map((d) => ({
|
|
6244
|
+
eventId: d.eventId,
|
|
6245
|
+
score: d.score,
|
|
6246
|
+
semanticScore: d.semanticScore,
|
|
6247
|
+
lexicalScore: d.lexicalScore,
|
|
6248
|
+
recencyScore: d.recencyScore
|
|
6249
|
+
}));
|
|
6250
|
+
const candidateDetails = (result.candidateDebug || []).map((d) => ({
|
|
6251
|
+
eventId: d.eventId,
|
|
6252
|
+
score: d.score,
|
|
6253
|
+
semanticScore: d.semanticScore,
|
|
6254
|
+
lexicalScore: d.lexicalScore,
|
|
6255
|
+
recencyScore: d.recencyScore
|
|
6256
|
+
}));
|
|
6257
|
+
const candidateEventIds = candidateDetails.length > 0 ? candidateDetails.map((d) => d.eventId) : selectedEventIds;
|
|
6258
|
+
await this.sqliteStore.recordRetrievalTrace({
|
|
6259
|
+
sessionId: options?.sessionId,
|
|
6260
|
+
projectHash: this.projectHash || void 0,
|
|
6261
|
+
queryText: query,
|
|
6262
|
+
strategy: options?.strategy || "auto",
|
|
6263
|
+
candidateEventIds,
|
|
6264
|
+
selectedEventIds,
|
|
6265
|
+
candidateDetails,
|
|
6266
|
+
selectedDetails,
|
|
6267
|
+
confidence: result.matchResult.confidence,
|
|
6268
|
+
fallbackTrace: result.fallbackTrace || []
|
|
6269
|
+
});
|
|
6270
|
+
} catch {
|
|
6271
|
+
}
|
|
6272
|
+
return result;
|
|
6273
|
+
}
|
|
6274
|
+
getConfiguredRerankWeights() {
|
|
6275
|
+
const semantic = Number(process.env.MEMORY_RERANK_WEIGHT_SEMANTIC ?? "");
|
|
6276
|
+
const lexical = Number(process.env.MEMORY_RERANK_WEIGHT_LEXICAL ?? "");
|
|
6277
|
+
const recency = Number(process.env.MEMORY_RERANK_WEIGHT_RECENCY ?? "");
|
|
6278
|
+
const allFinite = [semantic, lexical, recency].every((v) => Number.isFinite(v));
|
|
6279
|
+
if (!allFinite)
|
|
6280
|
+
return void 0;
|
|
6281
|
+
const nonNegative = [semantic, lexical, recency].every((v) => v >= 0);
|
|
6282
|
+
const total = semantic + lexical + recency;
|
|
6283
|
+
if (!nonNegative || total <= 0)
|
|
6284
|
+
return void 0;
|
|
6285
|
+
return {
|
|
6286
|
+
semantic: semantic / total,
|
|
6287
|
+
lexical: lexical / total,
|
|
6288
|
+
recency: recency / total
|
|
6289
|
+
};
|
|
6290
|
+
}
|
|
6291
|
+
async getRerankWeights(adaptive) {
|
|
6292
|
+
const configured = this.getConfiguredRerankWeights();
|
|
6293
|
+
if (configured)
|
|
6294
|
+
return configured;
|
|
6295
|
+
if (adaptive)
|
|
6296
|
+
return this.getAdaptiveRerankWeights();
|
|
6297
|
+
return void 0;
|
|
6298
|
+
}
|
|
6299
|
+
async rewriteQueryIntent(query) {
|
|
6300
|
+
if (process.env.MEMORY_INTENT_REWRITE_ENABLED !== "1")
|
|
6301
|
+
return null;
|
|
6302
|
+
const apiUrl = process.env.COMPANY_STOCK_API_URL || process.env.COMPANY_INT_API_URL;
|
|
6303
|
+
if (!apiUrl)
|
|
6304
|
+
return null;
|
|
6305
|
+
const controller = new AbortController();
|
|
6306
|
+
const timeoutMs = Number(process.env.MEMORY_INTENT_REWRITE_TIMEOUT_MS || 5e3);
|
|
6307
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
6308
|
+
try {
|
|
6309
|
+
const prompt = [
|
|
6310
|
+
"Rewrite user query for memory retrieval intent expansion.",
|
|
6311
|
+
"Return plain text only, one line, no markdown.",
|
|
6312
|
+
`Query: ${query}`
|
|
6313
|
+
].join("\n");
|
|
6314
|
+
const res = await fetch(apiUrl, {
|
|
6315
|
+
method: "POST",
|
|
6316
|
+
headers: {
|
|
6317
|
+
"Content-Type": "application/json",
|
|
6318
|
+
Accept: "*/*",
|
|
6319
|
+
Origin: process.env.COMPANY_INT_ORIGIN || "http://company-int.aplusai.ai",
|
|
6320
|
+
Referer: process.env.COMPANY_INT_REFERER || "http://company-int.aplusai.ai/"
|
|
6321
|
+
},
|
|
6322
|
+
body: JSON.stringify({
|
|
6323
|
+
question: prompt,
|
|
6324
|
+
company_name: null,
|
|
6325
|
+
conversation_id: null
|
|
6326
|
+
}),
|
|
6327
|
+
signal: controller.signal
|
|
5231
6328
|
});
|
|
6329
|
+
const text = (await res.text()).trim();
|
|
6330
|
+
if (!text)
|
|
6331
|
+
return null;
|
|
6332
|
+
const oneLine = text.replace(/^data:\s*/gm, "").split(/\r?\n/).map((x) => x.trim()).filter(Boolean).join(" ").slice(0, 240);
|
|
6333
|
+
if (!oneLine || oneLine.toLowerCase() === query.toLowerCase())
|
|
6334
|
+
return null;
|
|
6335
|
+
return oneLine;
|
|
6336
|
+
} catch {
|
|
6337
|
+
return null;
|
|
6338
|
+
} finally {
|
|
6339
|
+
clearTimeout(timeout);
|
|
6340
|
+
}
|
|
6341
|
+
}
|
|
6342
|
+
async getAdaptiveRerankWeights() {
|
|
6343
|
+
try {
|
|
6344
|
+
const s = await this.sqliteStore.getHelpfulnessStats();
|
|
6345
|
+
if (s.totalEvaluated < 20)
|
|
6346
|
+
return void 0;
|
|
6347
|
+
let semantic = 0.7;
|
|
6348
|
+
let lexical = 0.2;
|
|
6349
|
+
let recency = 0.1;
|
|
6350
|
+
if (s.avgScore < 0.45) {
|
|
6351
|
+
semantic -= 0.1;
|
|
6352
|
+
lexical += 0.1;
|
|
6353
|
+
} else if (s.avgScore > 0.75) {
|
|
6354
|
+
semantic += 0.05;
|
|
6355
|
+
lexical -= 0.05;
|
|
6356
|
+
}
|
|
6357
|
+
if (s.unhelpful > s.helpful) {
|
|
6358
|
+
recency += 0.05;
|
|
6359
|
+
semantic -= 0.03;
|
|
6360
|
+
lexical -= 0.02;
|
|
6361
|
+
}
|
|
6362
|
+
return { semantic, lexical, recency };
|
|
6363
|
+
} catch {
|
|
6364
|
+
return void 0;
|
|
5232
6365
|
}
|
|
5233
|
-
return this.retriever.retrieve(query, options);
|
|
5234
6366
|
}
|
|
5235
6367
|
/**
|
|
5236
6368
|
* Fast keyword search using SQLite FTS5
|
|
@@ -5272,6 +6404,18 @@ var MemoryService = class {
|
|
|
5272
6404
|
/**
|
|
5273
6405
|
* Get memory statistics
|
|
5274
6406
|
*/
|
|
6407
|
+
async getOutboxStats() {
|
|
6408
|
+
await this.initialize();
|
|
6409
|
+
return this.sqliteStore.getOutboxStats();
|
|
6410
|
+
}
|
|
6411
|
+
async getRetrievalTraceStats() {
|
|
6412
|
+
await this.initialize();
|
|
6413
|
+
return this.sqliteStore.getRetrievalTraceStats();
|
|
6414
|
+
}
|
|
6415
|
+
async getRecentRetrievalTraces(limit = 50) {
|
|
6416
|
+
await this.initialize();
|
|
6417
|
+
return this.sqliteStore.getRecentRetrievalTraces(limit);
|
|
6418
|
+
}
|
|
5275
6419
|
async getStats() {
|
|
5276
6420
|
await this.initialize();
|
|
5277
6421
|
const recentEvents = await this.sqliteStore.getRecentEvents(1e4);
|
|
@@ -5762,7 +6906,7 @@ var MemoryService = class {
|
|
|
5762
6906
|
*/
|
|
5763
6907
|
expandPath(p) {
|
|
5764
6908
|
if (p.startsWith("~")) {
|
|
5765
|
-
return
|
|
6909
|
+
return path3.join(os.homedir(), p.slice(1));
|
|
5766
6910
|
}
|
|
5767
6911
|
return p;
|
|
5768
6912
|
}
|
|
@@ -5785,6 +6929,7 @@ function getMemoryServiceForProject(projectPath, sharedStoreConfig) {
|
|
|
5785
6929
|
serviceCache.set(hash, new MemoryService({
|
|
5786
6930
|
storagePath,
|
|
5787
6931
|
projectHash: hash,
|
|
6932
|
+
projectPath,
|
|
5788
6933
|
// Override shared store config - hooks don't need DuckDB
|
|
5789
6934
|
sharedStoreConfig: sharedStoreConfig ?? { enabled: false },
|
|
5790
6935
|
analyticsEnabled: false
|
|
@@ -5801,12 +6946,12 @@ function getServiceFromQuery(c) {
|
|
|
5801
6946
|
const isHash = /^[a-f0-9]{8}$/.test(project);
|
|
5802
6947
|
let storagePath;
|
|
5803
6948
|
if (isHash) {
|
|
5804
|
-
storagePath =
|
|
6949
|
+
storagePath = path4.join(os2.homedir(), ".claude-code", "memory", "projects", project);
|
|
5805
6950
|
} else {
|
|
5806
6951
|
const crypto3 = __require("crypto");
|
|
5807
6952
|
const normalized = project.replace(/\/+$/, "") || "/";
|
|
5808
6953
|
const hash = crypto3.createHash("sha256").update(normalized).digest("hex").slice(0, 8);
|
|
5809
|
-
storagePath =
|
|
6954
|
+
storagePath = path4.join(os2.homedir(), ".claude-code", "memory", "projects", hash);
|
|
5810
6955
|
}
|
|
5811
6956
|
return new MemoryService({
|
|
5812
6957
|
storagePath,
|
|
@@ -6197,6 +7342,7 @@ statsRouter.get("/", async (c) => {
|
|
|
6197
7342
|
acc[day] = (acc[day] || 0) + 1;
|
|
6198
7343
|
return acc;
|
|
6199
7344
|
}, {});
|
|
7345
|
+
const retrievalTrace = await memoryService.getRetrievalTraceStats();
|
|
6200
7346
|
return c.json({
|
|
6201
7347
|
storage: {
|
|
6202
7348
|
eventCount: stats.totalEvents,
|
|
@@ -6214,7 +7360,8 @@ statsRouter.get("/", async (c) => {
|
|
|
6214
7360
|
heapUsed: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
|
|
6215
7361
|
heapTotal: Math.round(process.memoryUsage().heapTotal / 1024 / 1024)
|
|
6216
7362
|
},
|
|
6217
|
-
levelStats: stats.levelStats
|
|
7363
|
+
levelStats: stats.levelStats,
|
|
7364
|
+
retrievalTrace
|
|
6218
7365
|
});
|
|
6219
7366
|
} catch (error) {
|
|
6220
7367
|
return c.json({ error: error.message }, 500);
|
|
@@ -6316,6 +7463,42 @@ statsRouter.get("/helpfulness", async (c) => {
|
|
|
6316
7463
|
await memoryService.shutdown();
|
|
6317
7464
|
}
|
|
6318
7465
|
});
|
|
7466
|
+
statsRouter.get("/retrieval-traces", async (c) => {
|
|
7467
|
+
const limit = parseInt(c.req.query("limit") || "50", 10);
|
|
7468
|
+
const memoryService = getServiceFromQuery(c);
|
|
7469
|
+
try {
|
|
7470
|
+
await memoryService.initialize();
|
|
7471
|
+
const traces = await memoryService.getRecentRetrievalTraces(limit);
|
|
7472
|
+
const traceStats = await memoryService.getRetrievalTraceStats();
|
|
7473
|
+
return c.json({
|
|
7474
|
+
stats: traceStats,
|
|
7475
|
+
traces: traces.map((t) => ({
|
|
7476
|
+
traceId: t.traceId,
|
|
7477
|
+
sessionId: t.sessionId || null,
|
|
7478
|
+
projectHash: t.projectHash || null,
|
|
7479
|
+
queryText: t.queryText,
|
|
7480
|
+
strategy: t.strategy || null,
|
|
7481
|
+
candidateEventIds: t.candidateEventIds,
|
|
7482
|
+
selectedEventIds: t.selectedEventIds,
|
|
7483
|
+
candidateDetails: t.candidateDetails || [],
|
|
7484
|
+
selectedDetails: t.selectedDetails || [],
|
|
7485
|
+
candidateCount: t.candidateCount,
|
|
7486
|
+
selectedCount: t.selectedCount,
|
|
7487
|
+
confidence: t.confidence || null,
|
|
7488
|
+
fallbackTrace: t.fallbackTrace,
|
|
7489
|
+
createdAt: t.createdAt.toISOString()
|
|
7490
|
+
}))
|
|
7491
|
+
});
|
|
7492
|
+
} catch (error) {
|
|
7493
|
+
return c.json({
|
|
7494
|
+
stats: { totalQueries: 0, avgCandidateCount: 0, avgSelectedCount: 0, selectionRate: 0 },
|
|
7495
|
+
traces: [],
|
|
7496
|
+
error: error.message
|
|
7497
|
+
}, 500);
|
|
7498
|
+
} finally {
|
|
7499
|
+
await memoryService.shutdown();
|
|
7500
|
+
}
|
|
7501
|
+
});
|
|
6319
7502
|
statsRouter.post("/graduation/run", async (c) => {
|
|
6320
7503
|
const memoryService = getServiceFromQuery(c);
|
|
6321
7504
|
try {
|
|
@@ -6563,19 +7746,19 @@ turnsRouter.post("/backfill", async (c) => {
|
|
|
6563
7746
|
|
|
6564
7747
|
// src/server/api/projects.ts
|
|
6565
7748
|
import { Hono as Hono7 } from "hono";
|
|
6566
|
-
import * as
|
|
6567
|
-
import * as
|
|
7749
|
+
import * as fs5 from "fs";
|
|
7750
|
+
import * as path5 from "path";
|
|
6568
7751
|
import * as os3 from "os";
|
|
6569
7752
|
var projectsRouter = new Hono7();
|
|
6570
7753
|
projectsRouter.get("/", async (c) => {
|
|
6571
7754
|
try {
|
|
6572
|
-
const projectsDir =
|
|
6573
|
-
if (!
|
|
7755
|
+
const projectsDir = path5.join(os3.homedir(), ".claude-code", "memory", "projects");
|
|
7756
|
+
if (!fs5.existsSync(projectsDir)) {
|
|
6574
7757
|
return c.json({ projects: [] });
|
|
6575
7758
|
}
|
|
6576
|
-
const projectHashes =
|
|
6577
|
-
const fullPath =
|
|
6578
|
-
return
|
|
7759
|
+
const projectHashes = fs5.readdirSync(projectsDir).filter((name) => {
|
|
7760
|
+
const fullPath = path5.join(projectsDir, name);
|
|
7761
|
+
return fs5.statSync(fullPath).isDirectory();
|
|
6579
7762
|
});
|
|
6580
7763
|
const registry = loadSessionRegistry();
|
|
6581
7764
|
const hashToPath = /* @__PURE__ */ new Map();
|
|
@@ -6585,17 +7768,17 @@ projectsRouter.get("/", async (c) => {
|
|
|
6585
7768
|
}
|
|
6586
7769
|
}
|
|
6587
7770
|
const projects = projectHashes.map((hash) => {
|
|
6588
|
-
const dirPath =
|
|
6589
|
-
const dbPath =
|
|
7771
|
+
const dirPath = path5.join(projectsDir, hash);
|
|
7772
|
+
const dbPath = path5.join(dirPath, "events.sqlite");
|
|
6590
7773
|
let dbSize = 0;
|
|
6591
|
-
if (
|
|
6592
|
-
dbSize =
|
|
7774
|
+
if (fs5.existsSync(dbPath)) {
|
|
7775
|
+
dbSize = fs5.statSync(dbPath).size;
|
|
6593
7776
|
}
|
|
6594
7777
|
const projectPath = hashToPath.get(hash) || `unknown (${hash})`;
|
|
6595
7778
|
return {
|
|
6596
7779
|
hash,
|
|
6597
7780
|
projectPath,
|
|
6598
|
-
projectName:
|
|
7781
|
+
projectName: path5.basename(projectPath),
|
|
6599
7782
|
dbSize,
|
|
6600
7783
|
dbSizeHuman: formatBytes(dbSize)
|
|
6601
7784
|
};
|
|
@@ -6798,8 +7981,50 @@ function streamClaudeResponse(prompt, stream) {
|
|
|
6798
7981
|
});
|
|
6799
7982
|
}
|
|
6800
7983
|
|
|
7984
|
+
// src/server/api/health.ts
|
|
7985
|
+
import { Hono as Hono9 } from "hono";
|
|
7986
|
+
var healthRouter = new Hono9();
|
|
7987
|
+
healthRouter.get("/", async (c) => {
|
|
7988
|
+
const memoryService = getServiceFromQuery(c);
|
|
7989
|
+
try {
|
|
7990
|
+
await memoryService.initialize();
|
|
7991
|
+
const [stats, outbox] = await Promise.all([
|
|
7992
|
+
memoryService.getStats(),
|
|
7993
|
+
memoryService.getOutboxStats()
|
|
7994
|
+
]);
|
|
7995
|
+
const outboxPending = outbox.embedding.pending + outbox.vector.pending;
|
|
7996
|
+
const outboxFailed = outbox.embedding.failed + outbox.vector.failed;
|
|
7997
|
+
const status = outboxFailed > 0 ? "needs-attention" : "ok";
|
|
7998
|
+
return c.json({
|
|
7999
|
+
status,
|
|
8000
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
8001
|
+
storage: {
|
|
8002
|
+
totalEvents: stats.totalEvents,
|
|
8003
|
+
vectorCount: stats.vectorCount
|
|
8004
|
+
},
|
|
8005
|
+
outbox: {
|
|
8006
|
+
embedding: outbox.embedding,
|
|
8007
|
+
vector: outbox.vector,
|
|
8008
|
+
totals: {
|
|
8009
|
+
pending: outboxPending,
|
|
8010
|
+
failed: outboxFailed
|
|
8011
|
+
}
|
|
8012
|
+
},
|
|
8013
|
+
levelStats: stats.levelStats
|
|
8014
|
+
});
|
|
8015
|
+
} catch (error) {
|
|
8016
|
+
return c.json({
|
|
8017
|
+
status: "error",
|
|
8018
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
8019
|
+
error: error.message
|
|
8020
|
+
}, 500);
|
|
8021
|
+
} finally {
|
|
8022
|
+
await memoryService.shutdown();
|
|
8023
|
+
}
|
|
8024
|
+
});
|
|
8025
|
+
|
|
6801
8026
|
// src/server/api/index.ts
|
|
6802
|
-
var apiRouter = new
|
|
8027
|
+
var apiRouter = new Hono10().route("/sessions", sessionsRouter).route("/events", eventsRouter).route("/search", searchRouter).route("/stats", statsRouter).route("/citations", citationsRouter).route("/turns", turnsRouter).route("/projects", projectsRouter).route("/chat", chatRouter).route("/health", healthRouter);
|
|
6803
8028
|
export {
|
|
6804
8029
|
apiRouter
|
|
6805
8030
|
};
|