claude-memory-layer 1.0.11 → 1.0.13
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/scripts/bump-patch-version.sh +18 -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/services/session-history-importer.ts +53 -25
- 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/index.js
CHANGED
|
@@ -13,28 +13,28 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
// src/server/index.ts
|
|
16
|
-
import { Hono as
|
|
16
|
+
import { Hono as Hono11 } from "hono";
|
|
17
17
|
import { cors } from "hono/cors";
|
|
18
18
|
import { logger } from "hono/logger";
|
|
19
19
|
import { serve } from "@hono/node-server";
|
|
20
20
|
import { serveStatic } from "@hono/node-server/serve-static";
|
|
21
|
-
import * as
|
|
22
|
-
import * as
|
|
21
|
+
import * as path6 from "path";
|
|
22
|
+
import * as fs6 from "fs";
|
|
23
23
|
|
|
24
24
|
// src/server/api/index.ts
|
|
25
|
-
import { Hono as
|
|
25
|
+
import { Hono as Hono10 } from "hono";
|
|
26
26
|
|
|
27
27
|
// src/server/api/sessions.ts
|
|
28
28
|
import { Hono } from "hono";
|
|
29
29
|
|
|
30
30
|
// src/server/api/utils.ts
|
|
31
|
-
import * as
|
|
31
|
+
import * as path4 from "path";
|
|
32
32
|
import * as os2 from "os";
|
|
33
33
|
|
|
34
34
|
// src/services/memory-service.ts
|
|
35
|
-
import * as
|
|
35
|
+
import * as path3 from "path";
|
|
36
36
|
import * as os from "os";
|
|
37
|
-
import * as
|
|
37
|
+
import * as fs4 from "fs";
|
|
38
38
|
import * as crypto2 from "crypto";
|
|
39
39
|
|
|
40
40
|
// src/core/event-store.ts
|
|
@@ -92,11 +92,11 @@ function toDate(value) {
|
|
|
92
92
|
return new Date(value);
|
|
93
93
|
return new Date(String(value));
|
|
94
94
|
}
|
|
95
|
-
function createDatabase(
|
|
95
|
+
function createDatabase(path7, options) {
|
|
96
96
|
if (options?.readOnly) {
|
|
97
|
-
return new duckdb.Database(
|
|
97
|
+
return new duckdb.Database(path7, { access_mode: "READ_ONLY" });
|
|
98
98
|
}
|
|
99
|
-
return new duckdb.Database(
|
|
99
|
+
return new duckdb.Database(path7);
|
|
100
100
|
}
|
|
101
101
|
function dbRun(db, sql, params = []) {
|
|
102
102
|
return new Promise((resolve2, reject) => {
|
|
@@ -360,6 +360,17 @@ var EventStore = class {
|
|
|
360
360
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
361
361
|
)
|
|
362
362
|
`);
|
|
363
|
+
await dbRun(this.db, `
|
|
364
|
+
CREATE TABLE IF NOT EXISTS consolidated_rules (
|
|
365
|
+
rule_id VARCHAR PRIMARY KEY,
|
|
366
|
+
rule TEXT NOT NULL,
|
|
367
|
+
topics JSON,
|
|
368
|
+
source_memory_ids JSON,
|
|
369
|
+
source_events JSON,
|
|
370
|
+
confidence FLOAT DEFAULT 0.5,
|
|
371
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
372
|
+
)
|
|
373
|
+
`);
|
|
363
374
|
await dbRun(this.db, `
|
|
364
375
|
CREATE TABLE IF NOT EXISTS endless_config (
|
|
365
376
|
key VARCHAR PRIMARY KEY,
|
|
@@ -379,6 +390,7 @@ var EventStore = class {
|
|
|
379
390
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
|
|
380
391
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
|
|
381
392
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
|
|
393
|
+
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
|
|
382
394
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
|
|
383
395
|
this.initialized = true;
|
|
384
396
|
}
|
|
@@ -768,12 +780,12 @@ import { randomUUID as randomUUID2 } from "crypto";
|
|
|
768
780
|
import Database from "better-sqlite3";
|
|
769
781
|
import * as fs from "fs";
|
|
770
782
|
import * as nodePath from "path";
|
|
771
|
-
function createSQLiteDatabase(
|
|
772
|
-
const dir = nodePath.dirname(
|
|
783
|
+
function createSQLiteDatabase(path7, options) {
|
|
784
|
+
const dir = nodePath.dirname(path7);
|
|
773
785
|
if (!fs.existsSync(dir)) {
|
|
774
786
|
fs.mkdirSync(dir, { recursive: true });
|
|
775
787
|
}
|
|
776
|
-
const db = new Database(
|
|
788
|
+
const db = new Database(path7, {
|
|
777
789
|
readonly: options?.readonly ?? false
|
|
778
790
|
});
|
|
779
791
|
if (!options?.readonly && (options?.walMode ?? true)) {
|
|
@@ -814,6 +826,64 @@ function toSQLiteTimestamp(date) {
|
|
|
814
826
|
return date.toISOString();
|
|
815
827
|
}
|
|
816
828
|
|
|
829
|
+
// src/core/markdown-mirror.ts
|
|
830
|
+
import * as fs2 from "fs/promises";
|
|
831
|
+
import * as path from "path";
|
|
832
|
+
var DEFAULT_NAMESPACE = "default";
|
|
833
|
+
var DEFAULT_CATEGORY = "uncategorized";
|
|
834
|
+
function sanitizeSegment(input, fallback) {
|
|
835
|
+
const raw = String(input ?? "").trim().toLowerCase();
|
|
836
|
+
const safe = raw.normalize("NFKD").replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
837
|
+
if (!safe || safe === "." || safe === "..")
|
|
838
|
+
return fallback;
|
|
839
|
+
return safe;
|
|
840
|
+
}
|
|
841
|
+
function getCategorySegments(metadata, eventType) {
|
|
842
|
+
const raw = metadata?.categoryPath;
|
|
843
|
+
if (Array.isArray(raw) && raw.length > 0) {
|
|
844
|
+
return raw.map((s) => sanitizeSegment(s, DEFAULT_CATEGORY));
|
|
845
|
+
}
|
|
846
|
+
const single = metadata?.category;
|
|
847
|
+
if (typeof single === "string" && single.trim()) {
|
|
848
|
+
return [sanitizeSegment(single, DEFAULT_CATEGORY)];
|
|
849
|
+
}
|
|
850
|
+
return [sanitizeSegment(eventType, DEFAULT_CATEGORY)];
|
|
851
|
+
}
|
|
852
|
+
function buildMirrorPath(rootDir, event) {
|
|
853
|
+
const metadata = event.metadata;
|
|
854
|
+
const namespace = sanitizeSegment(metadata?.namespace, DEFAULT_NAMESPACE);
|
|
855
|
+
const categories = getCategorySegments(metadata, event.eventType);
|
|
856
|
+
const d = event.timestamp;
|
|
857
|
+
const yyyy = d.getFullYear();
|
|
858
|
+
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
859
|
+
const dd = String(d.getDate()).padStart(2, "0");
|
|
860
|
+
return path.join(rootDir, "memory", namespace, ...categories, `${yyyy}-${mm}-${dd}.md`);
|
|
861
|
+
}
|
|
862
|
+
function formatMirrorEntry(event) {
|
|
863
|
+
const category = Array.isArray(event.metadata?.categoryPath) ? event.metadata.categoryPath.join("/") : String(event.metadata?.category ?? event.eventType);
|
|
864
|
+
return [
|
|
865
|
+
"",
|
|
866
|
+
`- ts: ${event.timestamp.toISOString()}`,
|
|
867
|
+
` id: ${event.id}`,
|
|
868
|
+
` type: ${event.eventType}`,
|
|
869
|
+
` session: ${event.sessionId}`,
|
|
870
|
+
` category: ${category}`,
|
|
871
|
+
" content: |",
|
|
872
|
+
...event.content.split("\n").map((line) => ` ${line}`)
|
|
873
|
+
].join("\n") + "\n";
|
|
874
|
+
}
|
|
875
|
+
var MarkdownMirror = class {
|
|
876
|
+
constructor(rootDir) {
|
|
877
|
+
this.rootDir = rootDir;
|
|
878
|
+
}
|
|
879
|
+
async append(event) {
|
|
880
|
+
const outPath = buildMirrorPath(this.rootDir, event);
|
|
881
|
+
await fs2.mkdir(path.dirname(outPath), { recursive: true });
|
|
882
|
+
await fs2.appendFile(outPath, formatMirrorEntry(event), "utf8");
|
|
883
|
+
return outPath;
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
|
|
817
887
|
// src/core/sqlite-event-store.ts
|
|
818
888
|
var SQLiteEventStore = class {
|
|
819
889
|
constructor(dbPath, options) {
|
|
@@ -823,10 +893,12 @@ var SQLiteEventStore = class {
|
|
|
823
893
|
readonly: this.readOnly,
|
|
824
894
|
walMode: !this.readOnly
|
|
825
895
|
});
|
|
896
|
+
this.markdownMirror = this.readOnly || !options?.markdownMirrorRoot ? null : new MarkdownMirror(options.markdownMirrorRoot);
|
|
826
897
|
}
|
|
827
898
|
db;
|
|
828
899
|
initialized = false;
|
|
829
900
|
readOnly;
|
|
901
|
+
markdownMirror;
|
|
830
902
|
/**
|
|
831
903
|
* Initialize database schema
|
|
832
904
|
*/
|
|
@@ -1033,6 +1105,17 @@ var SQLiteEventStore = class {
|
|
|
1033
1105
|
created_at TEXT DEFAULT (datetime('now'))
|
|
1034
1106
|
);
|
|
1035
1107
|
|
|
1108
|
+
-- Consolidated Rules table (long-term stable memory)
|
|
1109
|
+
CREATE TABLE IF NOT EXISTS consolidated_rules (
|
|
1110
|
+
rule_id TEXT PRIMARY KEY,
|
|
1111
|
+
rule TEXT NOT NULL,
|
|
1112
|
+
topics TEXT,
|
|
1113
|
+
source_memory_ids TEXT,
|
|
1114
|
+
source_events TEXT,
|
|
1115
|
+
confidence REAL DEFAULT 0.5,
|
|
1116
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
1117
|
+
);
|
|
1118
|
+
|
|
1036
1119
|
-- Endless Mode Config table
|
|
1037
1120
|
CREATE TABLE IF NOT EXISTS endless_config (
|
|
1038
1121
|
key TEXT PRIMARY KEY,
|
|
@@ -1057,6 +1140,24 @@ var SQLiteEventStore = class {
|
|
|
1057
1140
|
measured_at TEXT
|
|
1058
1141
|
);
|
|
1059
1142
|
|
|
1143
|
+
-- Retrieval trace log (query -> candidates -> selected for context)
|
|
1144
|
+
CREATE TABLE IF NOT EXISTS retrieval_traces (
|
|
1145
|
+
trace_id TEXT PRIMARY KEY,
|
|
1146
|
+
session_id TEXT,
|
|
1147
|
+
project_hash TEXT,
|
|
1148
|
+
query_text TEXT NOT NULL,
|
|
1149
|
+
strategy TEXT,
|
|
1150
|
+
candidate_event_ids TEXT,
|
|
1151
|
+
selected_event_ids TEXT,
|
|
1152
|
+
candidate_details_json TEXT,
|
|
1153
|
+
selected_details_json TEXT,
|
|
1154
|
+
candidate_count INTEGER DEFAULT 0,
|
|
1155
|
+
selected_count INTEGER DEFAULT 0,
|
|
1156
|
+
confidence TEXT,
|
|
1157
|
+
fallback_trace TEXT,
|
|
1158
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
1159
|
+
);
|
|
1160
|
+
|
|
1060
1161
|
-- Sync position tracking (for SQLite -> DuckDB sync)
|
|
1061
1162
|
CREATE TABLE IF NOT EXISTS sync_positions (
|
|
1062
1163
|
target_name TEXT PRIMARY KEY,
|
|
@@ -1081,10 +1182,14 @@ var SQLiteEventStore = class {
|
|
|
1081
1182
|
CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score);
|
|
1082
1183
|
CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
|
|
1083
1184
|
CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
|
|
1185
|
+
CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence);
|
|
1084
1186
|
CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
|
|
1085
1187
|
CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
|
|
1086
1188
|
CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
|
|
1087
1189
|
CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
|
|
1190
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_created_at ON retrieval_traces(created_at DESC);
|
|
1191
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_project_hash ON retrieval_traces(project_hash);
|
|
1192
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_session_id ON retrieval_traces(session_id);
|
|
1088
1193
|
|
|
1089
1194
|
-- FTS5 Full-Text Search for fast keyword search
|
|
1090
1195
|
CREATE VIRTUAL TABLE IF NOT EXISTS events_fts USING fts5(
|
|
@@ -1108,6 +1213,14 @@ var SQLiteEventStore = class {
|
|
|
1108
1213
|
INSERT INTO events_fts(rowid, content, event_id) VALUES (NEW.rowid, NEW.content, NEW.id);
|
|
1109
1214
|
END;
|
|
1110
1215
|
`);
|
|
1216
|
+
try {
|
|
1217
|
+
sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN selected_details_json TEXT;`);
|
|
1218
|
+
} catch {
|
|
1219
|
+
}
|
|
1220
|
+
try {
|
|
1221
|
+
sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN candidate_details_json TEXT;`);
|
|
1222
|
+
} catch {
|
|
1223
|
+
}
|
|
1111
1224
|
const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
|
|
1112
1225
|
const columnNames = tableInfo.map((col) => col.name);
|
|
1113
1226
|
if (!columnNames.includes("access_count")) {
|
|
@@ -1207,6 +1320,21 @@ var SQLiteEventStore = class {
|
|
|
1207
1320
|
insertLevel.run(id);
|
|
1208
1321
|
});
|
|
1209
1322
|
transaction();
|
|
1323
|
+
if (this.markdownMirror) {
|
|
1324
|
+
const event = {
|
|
1325
|
+
id,
|
|
1326
|
+
eventType: input.eventType,
|
|
1327
|
+
sessionId: input.sessionId,
|
|
1328
|
+
timestamp: input.timestamp,
|
|
1329
|
+
content: input.content,
|
|
1330
|
+
canonicalKey,
|
|
1331
|
+
dedupeKey,
|
|
1332
|
+
metadata
|
|
1333
|
+
};
|
|
1334
|
+
this.markdownMirror.append(event).catch((err) => {
|
|
1335
|
+
console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1210
1338
|
return { success: true, eventId: id, isDuplicate: false };
|
|
1211
1339
|
} catch (error) {
|
|
1212
1340
|
return {
|
|
@@ -1265,6 +1393,92 @@ var SQLiteEventStore = class {
|
|
|
1265
1393
|
);
|
|
1266
1394
|
return rows.map(this.rowToEvent);
|
|
1267
1395
|
}
|
|
1396
|
+
/**
|
|
1397
|
+
* Get events since a SQLite rowid (for robust incremental replication).
|
|
1398
|
+
* Rowid is monotonic for append-only tables, independent of client timestamps.
|
|
1399
|
+
*/
|
|
1400
|
+
async getEventsSinceRowid(lastRowid, limit = 1e3) {
|
|
1401
|
+
await this.initialize();
|
|
1402
|
+
const rows = sqliteAll(
|
|
1403
|
+
this.db,
|
|
1404
|
+
`SELECT rowid as _rowid, * FROM events WHERE rowid > ? ORDER BY rowid ASC LIMIT ?`,
|
|
1405
|
+
[lastRowid, limit]
|
|
1406
|
+
);
|
|
1407
|
+
return rows.map((row) => ({
|
|
1408
|
+
rowid: row._rowid,
|
|
1409
|
+
event: this.rowToEvent(row)
|
|
1410
|
+
}));
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* Import events with fixed IDs (used for cross-machine replication).
|
|
1414
|
+
* Idempotent: skips if event id or dedupeKey already exists.
|
|
1415
|
+
*
|
|
1416
|
+
* NOTE: This bypasses the append() id generation to preserve stable IDs.
|
|
1417
|
+
*/
|
|
1418
|
+
async importEvents(events) {
|
|
1419
|
+
if (events.length === 0)
|
|
1420
|
+
return { inserted: 0, skipped: 0 };
|
|
1421
|
+
if (this.readOnly)
|
|
1422
|
+
return { inserted: 0, skipped: events.length };
|
|
1423
|
+
await this.initialize();
|
|
1424
|
+
const getById = this.db.prepare(`SELECT id FROM events WHERE id = ?`);
|
|
1425
|
+
const getByDedupe = this.db.prepare(`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`);
|
|
1426
|
+
const insertEvent = this.db.prepare(`
|
|
1427
|
+
INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
|
|
1428
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1429
|
+
`);
|
|
1430
|
+
const insertDedup = this.db.prepare(`
|
|
1431
|
+
INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
|
|
1432
|
+
`);
|
|
1433
|
+
const insertLevel = this.db.prepare(`
|
|
1434
|
+
INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')
|
|
1435
|
+
`);
|
|
1436
|
+
let inserted = 0;
|
|
1437
|
+
let skipped = 0;
|
|
1438
|
+
const insertedEvents = [];
|
|
1439
|
+
const tx = this.db.transaction((batch) => {
|
|
1440
|
+
for (const ev of batch) {
|
|
1441
|
+
const existingById = getById.get(ev.id);
|
|
1442
|
+
if (existingById) {
|
|
1443
|
+
skipped++;
|
|
1444
|
+
continue;
|
|
1445
|
+
}
|
|
1446
|
+
const canonicalKey = ev.canonicalKey || makeCanonicalKey(ev.content);
|
|
1447
|
+
const dedupeKey = ev.dedupeKey || makeDedupeKey(ev.content, ev.sessionId);
|
|
1448
|
+
const existingByDedupe = getByDedupe.get(dedupeKey);
|
|
1449
|
+
if (existingByDedupe) {
|
|
1450
|
+
skipped++;
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
const metadata = ev.metadata || {};
|
|
1454
|
+
const turnId = metadata.turnId;
|
|
1455
|
+
insertEvent.run(
|
|
1456
|
+
ev.id,
|
|
1457
|
+
ev.eventType,
|
|
1458
|
+
ev.sessionId,
|
|
1459
|
+
toSQLiteTimestamp(ev.timestamp),
|
|
1460
|
+
ev.content,
|
|
1461
|
+
canonicalKey,
|
|
1462
|
+
dedupeKey,
|
|
1463
|
+
JSON.stringify(metadata),
|
|
1464
|
+
turnId ?? null
|
|
1465
|
+
);
|
|
1466
|
+
insertDedup.run(dedupeKey, ev.id);
|
|
1467
|
+
insertLevel.run(ev.id);
|
|
1468
|
+
inserted++;
|
|
1469
|
+
insertedEvents.push(ev);
|
|
1470
|
+
}
|
|
1471
|
+
});
|
|
1472
|
+
tx(events);
|
|
1473
|
+
if (this.markdownMirror && insertedEvents.length > 0) {
|
|
1474
|
+
for (const ev of insertedEvents) {
|
|
1475
|
+
this.markdownMirror.append(ev).catch((err) => {
|
|
1476
|
+
console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
|
|
1477
|
+
});
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
return { inserted, skipped };
|
|
1481
|
+
}
|
|
1268
1482
|
/**
|
|
1269
1483
|
* Create or update session
|
|
1270
1484
|
*/
|
|
@@ -1427,6 +1641,35 @@ var SQLiteEventStore = class {
|
|
|
1427
1641
|
[error, ...ids]
|
|
1428
1642
|
);
|
|
1429
1643
|
}
|
|
1644
|
+
/**
|
|
1645
|
+
* Get embedding/vector outbox health statistics
|
|
1646
|
+
*/
|
|
1647
|
+
async getOutboxStats() {
|
|
1648
|
+
await this.initialize();
|
|
1649
|
+
const embeddingRows = sqliteAll(
|
|
1650
|
+
this.db,
|
|
1651
|
+
`SELECT status, COUNT(*) as count FROM embedding_outbox GROUP BY status`
|
|
1652
|
+
);
|
|
1653
|
+
const vectorRows = sqliteAll(
|
|
1654
|
+
this.db,
|
|
1655
|
+
`SELECT status, COUNT(*) as count FROM vector_outbox GROUP BY status`
|
|
1656
|
+
);
|
|
1657
|
+
const fromRows = (rows) => {
|
|
1658
|
+
const out = { pending: 0, processing: 0, failed: 0, total: 0 };
|
|
1659
|
+
for (const row of rows) {
|
|
1660
|
+
const key = row.status;
|
|
1661
|
+
if (key === "pending" || key === "processing" || key === "failed") {
|
|
1662
|
+
out[key] += row.count;
|
|
1663
|
+
}
|
|
1664
|
+
out.total += row.count;
|
|
1665
|
+
}
|
|
1666
|
+
return out;
|
|
1667
|
+
};
|
|
1668
|
+
return {
|
|
1669
|
+
embedding: fromRows(embeddingRows),
|
|
1670
|
+
vector: fromRows(vectorRows)
|
|
1671
|
+
};
|
|
1672
|
+
}
|
|
1430
1673
|
/**
|
|
1431
1674
|
* Update memory level
|
|
1432
1675
|
*/
|
|
@@ -1785,6 +2028,79 @@ var SQLiteEventStore = class {
|
|
|
1785
2028
|
getDatabase() {
|
|
1786
2029
|
return this.db;
|
|
1787
2030
|
}
|
|
2031
|
+
async recordRetrievalTrace(input) {
|
|
2032
|
+
await this.initialize();
|
|
2033
|
+
const traceId = randomUUID2();
|
|
2034
|
+
sqliteRun(
|
|
2035
|
+
this.db,
|
|
2036
|
+
`INSERT INTO retrieval_traces (
|
|
2037
|
+
trace_id, session_id, project_hash, query_text, strategy,
|
|
2038
|
+
candidate_event_ids, selected_event_ids, candidate_details_json, selected_details_json,
|
|
2039
|
+
candidate_count, selected_count, confidence, fallback_trace
|
|
2040
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
2041
|
+
[
|
|
2042
|
+
traceId,
|
|
2043
|
+
input.sessionId || null,
|
|
2044
|
+
input.projectHash || null,
|
|
2045
|
+
input.queryText,
|
|
2046
|
+
input.strategy || null,
|
|
2047
|
+
JSON.stringify(input.candidateEventIds || []),
|
|
2048
|
+
JSON.stringify(input.selectedEventIds || []),
|
|
2049
|
+
JSON.stringify(input.candidateDetails || []),
|
|
2050
|
+
JSON.stringify(input.selectedDetails || []),
|
|
2051
|
+
(input.candidateEventIds || []).length,
|
|
2052
|
+
(input.selectedEventIds || []).length,
|
|
2053
|
+
input.confidence || null,
|
|
2054
|
+
JSON.stringify(input.fallbackTrace || [])
|
|
2055
|
+
]
|
|
2056
|
+
);
|
|
2057
|
+
}
|
|
2058
|
+
async getRecentRetrievalTraces(limit = 50) {
|
|
2059
|
+
await this.initialize();
|
|
2060
|
+
const rows = sqliteAll(
|
|
2061
|
+
this.db,
|
|
2062
|
+
`SELECT * FROM retrieval_traces ORDER BY created_at DESC LIMIT ?`,
|
|
2063
|
+
[limit]
|
|
2064
|
+
);
|
|
2065
|
+
return rows.map((row) => ({
|
|
2066
|
+
traceId: row.trace_id,
|
|
2067
|
+
sessionId: row.session_id || void 0,
|
|
2068
|
+
projectHash: row.project_hash || void 0,
|
|
2069
|
+
queryText: row.query_text,
|
|
2070
|
+
strategy: row.strategy || void 0,
|
|
2071
|
+
candidateEventIds: row.candidate_event_ids ? JSON.parse(row.candidate_event_ids) : [],
|
|
2072
|
+
selectedEventIds: row.selected_event_ids ? JSON.parse(row.selected_event_ids) : [],
|
|
2073
|
+
candidateDetails: row.candidate_details_json ? JSON.parse(row.candidate_details_json) : [],
|
|
2074
|
+
selectedDetails: row.selected_details_json ? JSON.parse(row.selected_details_json) : [],
|
|
2075
|
+
candidateCount: Number(row.candidate_count || 0),
|
|
2076
|
+
selectedCount: Number(row.selected_count || 0),
|
|
2077
|
+
confidence: row.confidence || void 0,
|
|
2078
|
+
fallbackTrace: row.fallback_trace ? JSON.parse(row.fallback_trace) : [],
|
|
2079
|
+
createdAt: toDateFromSQLite(row.created_at)
|
|
2080
|
+
}));
|
|
2081
|
+
}
|
|
2082
|
+
async getRetrievalTraceStats() {
|
|
2083
|
+
await this.initialize();
|
|
2084
|
+
const row = sqliteGet(
|
|
2085
|
+
this.db,
|
|
2086
|
+
`SELECT
|
|
2087
|
+
COUNT(*) as total_queries,
|
|
2088
|
+
AVG(candidate_count) as avg_candidate_count,
|
|
2089
|
+
AVG(selected_count) as avg_selected_count,
|
|
2090
|
+
CASE
|
|
2091
|
+
WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
|
|
2092
|
+
ELSE 0
|
|
2093
|
+
END as selection_rate
|
|
2094
|
+
FROM retrieval_traces`,
|
|
2095
|
+
[]
|
|
2096
|
+
);
|
|
2097
|
+
return {
|
|
2098
|
+
totalQueries: Number(row?.total_queries || 0),
|
|
2099
|
+
avgCandidateCount: Number(row?.avg_candidate_count || 0),
|
|
2100
|
+
avgSelectedCount: Number(row?.avg_selected_count || 0),
|
|
2101
|
+
selectionRate: Number(row?.selection_rate || 0)
|
|
2102
|
+
};
|
|
2103
|
+
}
|
|
1788
2104
|
/**
|
|
1789
2105
|
* Close database connection
|
|
1790
2106
|
*/
|
|
@@ -2646,7 +2962,20 @@ var DEFAULT_OPTIONS = {
|
|
|
2646
2962
|
topK: 5,
|
|
2647
2963
|
minScore: 0.7,
|
|
2648
2964
|
maxTokens: 2e3,
|
|
2649
|
-
includeSessionContext: true
|
|
2965
|
+
includeSessionContext: true,
|
|
2966
|
+
strategy: "auto",
|
|
2967
|
+
rerankWithKeyword: true,
|
|
2968
|
+
decayPolicy: {
|
|
2969
|
+
enabled: true,
|
|
2970
|
+
windowDays: 30,
|
|
2971
|
+
maxPenalty: 0.15
|
|
2972
|
+
},
|
|
2973
|
+
graphHop: {
|
|
2974
|
+
enabled: true,
|
|
2975
|
+
maxHops: 1,
|
|
2976
|
+
hopPenalty: 0.08
|
|
2977
|
+
},
|
|
2978
|
+
projectScopeMode: "global"
|
|
2650
2979
|
};
|
|
2651
2980
|
var Retriever = class {
|
|
2652
2981
|
eventStore;
|
|
@@ -2656,6 +2985,7 @@ var Retriever = class {
|
|
|
2656
2985
|
sharedStore;
|
|
2657
2986
|
sharedVectorStore;
|
|
2658
2987
|
graduation;
|
|
2988
|
+
queryRewriter;
|
|
2659
2989
|
constructor(eventStore, vectorStore, embedder, matcher, sharedOptions) {
|
|
2660
2990
|
this.eventStore = eventStore;
|
|
2661
2991
|
this.vectorStore = vectorStore;
|
|
@@ -2664,47 +2994,105 @@ var Retriever = class {
|
|
|
2664
2994
|
this.sharedStore = sharedOptions?.sharedStore;
|
|
2665
2995
|
this.sharedVectorStore = sharedOptions?.sharedVectorStore;
|
|
2666
2996
|
}
|
|
2667
|
-
/**
|
|
2668
|
-
* Set graduation pipeline for access tracking
|
|
2669
|
-
*/
|
|
2670
2997
|
setGraduationPipeline(graduation) {
|
|
2671
2998
|
this.graduation = graduation;
|
|
2672
2999
|
}
|
|
2673
|
-
/**
|
|
2674
|
-
* Set shared stores after construction
|
|
2675
|
-
*/
|
|
2676
3000
|
setSharedStores(sharedStore, sharedVectorStore) {
|
|
2677
3001
|
this.sharedStore = sharedStore;
|
|
2678
3002
|
this.sharedVectorStore = sharedVectorStore;
|
|
2679
3003
|
}
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
3004
|
+
setQueryRewriter(rewriter) {
|
|
3005
|
+
this.queryRewriter = rewriter;
|
|
3006
|
+
}
|
|
2683
3007
|
async retrieve(query, options = {}) {
|
|
2684
3008
|
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
2685
|
-
const
|
|
2686
|
-
const
|
|
2687
|
-
|
|
2688
|
-
|
|
3009
|
+
const sessionFilter = opts.scope?.sessionId ?? opts.sessionId;
|
|
3010
|
+
const fallbackTrace = [];
|
|
3011
|
+
const fallbackEnabled = (opts.strategy ?? "auto") === "auto";
|
|
3012
|
+
const primaryStrategy = opts.strategy === "auto" ? "fast" : opts.strategy || "fast";
|
|
3013
|
+
let current = await this.runStage(query, {
|
|
3014
|
+
strategy: primaryStrategy,
|
|
3015
|
+
topK: opts.topK,
|
|
2689
3016
|
minScore: opts.minScore,
|
|
2690
|
-
sessionId:
|
|
3017
|
+
sessionId: sessionFilter,
|
|
3018
|
+
scope: opts.scope,
|
|
3019
|
+
rerankWithKeyword: opts.rerankWithKeyword !== false,
|
|
3020
|
+
rerankWeights: opts.rerankWeights,
|
|
3021
|
+
decayPolicy: opts.decayPolicy,
|
|
3022
|
+
intentRewrite: opts.intentRewrite === true,
|
|
3023
|
+
graphHop: opts.graphHop,
|
|
3024
|
+
projectScopeMode: opts.projectScopeMode,
|
|
3025
|
+
projectHash: opts.projectHash,
|
|
3026
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
2691
3027
|
});
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
3028
|
+
fallbackTrace.push(`stage:primary:${primaryStrategy}`);
|
|
3029
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results) && primaryStrategy !== "deep") {
|
|
3030
|
+
current = await this.runStage(query, {
|
|
3031
|
+
strategy: "deep",
|
|
3032
|
+
topK: opts.topK,
|
|
3033
|
+
minScore: opts.minScore,
|
|
3034
|
+
sessionId: sessionFilter,
|
|
3035
|
+
scope: opts.scope,
|
|
3036
|
+
rerankWithKeyword: opts.rerankWithKeyword !== false,
|
|
3037
|
+
rerankWeights: opts.rerankWeights,
|
|
3038
|
+
decayPolicy: opts.decayPolicy,
|
|
3039
|
+
graphHop: opts.graphHop,
|
|
3040
|
+
projectScopeMode: opts.projectScopeMode,
|
|
3041
|
+
projectHash: opts.projectHash,
|
|
3042
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
3043
|
+
});
|
|
3044
|
+
fallbackTrace.push("fallback:deep");
|
|
3045
|
+
}
|
|
3046
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
|
|
3047
|
+
current = await this.runStage(query, {
|
|
3048
|
+
strategy: "deep",
|
|
3049
|
+
topK: opts.topK,
|
|
3050
|
+
minScore: Math.max(0.5, opts.minScore - 0.15),
|
|
3051
|
+
sessionId: void 0,
|
|
3052
|
+
scope: void 0,
|
|
3053
|
+
rerankWithKeyword: true,
|
|
3054
|
+
rerankWeights: opts.rerankWeights,
|
|
3055
|
+
decayPolicy: opts.decayPolicy,
|
|
3056
|
+
graphHop: opts.graphHop,
|
|
3057
|
+
projectScopeMode: opts.projectScopeMode,
|
|
3058
|
+
projectHash: opts.projectHash,
|
|
3059
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
3060
|
+
});
|
|
3061
|
+
fallbackTrace.push("fallback:scope-expanded");
|
|
3062
|
+
}
|
|
3063
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
|
|
3064
|
+
const summary = await this.buildSummaryFallback(query, opts.topK);
|
|
3065
|
+
current = {
|
|
3066
|
+
results: summary,
|
|
3067
|
+
candidateResults: summary,
|
|
3068
|
+
matchResult: this.matcher.matchSearchResults(summary, () => 0)
|
|
3069
|
+
};
|
|
3070
|
+
fallbackTrace.push("fallback:summary");
|
|
3071
|
+
}
|
|
3072
|
+
const memories = await this.enrichResults(current.results.slice(0, opts.topK), opts);
|
|
2697
3073
|
const context = this.buildContext(memories, opts.maxTokens);
|
|
2698
3074
|
return {
|
|
2699
3075
|
memories,
|
|
2700
|
-
matchResult,
|
|
3076
|
+
matchResult: current.matchResult,
|
|
2701
3077
|
totalTokens: this.estimateTokens(context),
|
|
2702
|
-
context
|
|
3078
|
+
context,
|
|
3079
|
+
fallbackTrace,
|
|
3080
|
+
selectedDebug: current.results.slice(0, opts.topK).map((r) => ({
|
|
3081
|
+
eventId: r.eventId,
|
|
3082
|
+
score: r.score,
|
|
3083
|
+
semanticScore: r.semanticScore,
|
|
3084
|
+
lexicalScore: r.lexicalScore,
|
|
3085
|
+
recencyScore: r.recencyScore
|
|
3086
|
+
})),
|
|
3087
|
+
candidateDebug: (current.candidateResults || []).slice(0, Math.max(opts.topK * 3, 20)).map((r) => ({
|
|
3088
|
+
eventId: r.eventId,
|
|
3089
|
+
score: r.score,
|
|
3090
|
+
semanticScore: r.semanticScore,
|
|
3091
|
+
lexicalScore: r.lexicalScore,
|
|
3092
|
+
recencyScore: r.recencyScore
|
|
3093
|
+
}))
|
|
2703
3094
|
};
|
|
2704
3095
|
}
|
|
2705
|
-
/**
|
|
2706
|
-
* Retrieve with unified search (project + shared)
|
|
2707
|
-
*/
|
|
2708
3096
|
async retrieveUnified(query, options = {}) {
|
|
2709
3097
|
const projectResult = await this.retrieve(query, options);
|
|
2710
3098
|
if (!options.includeShared || !this.sharedStore || !this.sharedVectorStore) {
|
|
@@ -2712,22 +3100,19 @@ var Retriever = class {
|
|
|
2712
3100
|
}
|
|
2713
3101
|
try {
|
|
2714
3102
|
const queryEmbedding = await this.embedder.embed(query);
|
|
2715
|
-
const sharedVectorResults = await this.sharedVectorStore.search(
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
excludeProjectHash: options.projectHash
|
|
2721
|
-
}
|
|
2722
|
-
);
|
|
3103
|
+
const sharedVectorResults = await this.sharedVectorStore.search(queryEmbedding.vector, {
|
|
3104
|
+
limit: options.topK || 5,
|
|
3105
|
+
minScore: options.minScore || 0.7,
|
|
3106
|
+
excludeProjectHash: options.projectHash
|
|
3107
|
+
});
|
|
2723
3108
|
const sharedMemories = [];
|
|
2724
3109
|
for (const result of sharedVectorResults) {
|
|
2725
3110
|
const entry = await this.sharedStore.get(result.entryId);
|
|
2726
|
-
if (entry)
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
3111
|
+
if (!entry)
|
|
3112
|
+
continue;
|
|
3113
|
+
if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
|
|
3114
|
+
sharedMemories.push(entry);
|
|
3115
|
+
await this.sharedStore.recordUsage(entry.entryId);
|
|
2731
3116
|
}
|
|
2732
3117
|
}
|
|
2733
3118
|
const unifiedContext = this.buildUnifiedContext(projectResult, sharedMemories);
|
|
@@ -2742,50 +3127,243 @@ var Retriever = class {
|
|
|
2742
3127
|
return projectResult;
|
|
2743
3128
|
}
|
|
2744
3129
|
}
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
3130
|
+
async runStage(query, input) {
|
|
3131
|
+
let initialResults = await this.searchByStrategy(query, {
|
|
3132
|
+
strategy: input.strategy,
|
|
3133
|
+
topK: input.topK,
|
|
3134
|
+
minScore: input.minScore,
|
|
3135
|
+
sessionId: input.sessionId
|
|
3136
|
+
});
|
|
3137
|
+
if (input.intentRewrite && input.strategy === "deep" && this.queryRewriter) {
|
|
3138
|
+
const rewritten = (await this.queryRewriter(query))?.trim();
|
|
3139
|
+
if (rewritten && rewritten !== query) {
|
|
3140
|
+
const rewrittenResults = await this.searchByStrategy(rewritten, {
|
|
3141
|
+
strategy: "deep",
|
|
3142
|
+
topK: input.topK,
|
|
3143
|
+
minScore: Math.max(0.5, input.minScore - 0.1),
|
|
3144
|
+
sessionId: input.sessionId
|
|
3145
|
+
});
|
|
3146
|
+
initialResults = this.mergeResults(initialResults, rewrittenResults, input.topK * 3);
|
|
3147
|
+
}
|
|
3148
|
+
}
|
|
3149
|
+
const expandedResults = input.graphHop?.enabled === false ? initialResults : await this.expandGraphHops(initialResults, {
|
|
3150
|
+
maxHops: Math.max(1, input.graphHop?.maxHops ?? 1),
|
|
3151
|
+
hopPenalty: Math.max(0, input.graphHop?.hopPenalty ?? 0.08),
|
|
3152
|
+
limit: input.topK * 4
|
|
3153
|
+
});
|
|
3154
|
+
const rerankedResults = input.rerankWithKeyword ? this.rerankByKeywordOverlap(expandedResults, query, input.rerankWeights, input.decayPolicy) : expandedResults;
|
|
3155
|
+
const filtered = await this.applyScopeFilters(rerankedResults, {
|
|
3156
|
+
scope: input.scope,
|
|
3157
|
+
projectScopeMode: input.projectScopeMode,
|
|
3158
|
+
projectHash: input.projectHash,
|
|
3159
|
+
allowedProjectHashes: input.allowedProjectHashes
|
|
3160
|
+
});
|
|
3161
|
+
const top = filtered.slice(0, input.topK);
|
|
3162
|
+
const matchResult = this.matcher.matchSearchResults(top, () => 0);
|
|
3163
|
+
return { results: top, candidateResults: filtered, matchResult };
|
|
3164
|
+
}
|
|
3165
|
+
mergeResults(primary, secondary, limit) {
|
|
3166
|
+
const byId = /* @__PURE__ */ new Map();
|
|
3167
|
+
for (const row of primary)
|
|
3168
|
+
byId.set(row.eventId, row);
|
|
3169
|
+
for (const row of secondary) {
|
|
3170
|
+
const prev = byId.get(row.eventId);
|
|
3171
|
+
if (!prev || row.score > prev.score) {
|
|
3172
|
+
byId.set(row.eventId, row);
|
|
3173
|
+
}
|
|
3174
|
+
}
|
|
3175
|
+
return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, limit);
|
|
3176
|
+
}
|
|
3177
|
+
async expandGraphHops(seeds, opts) {
|
|
3178
|
+
const byId = /* @__PURE__ */ new Map();
|
|
3179
|
+
for (const s of seeds)
|
|
3180
|
+
byId.set(s.eventId, s);
|
|
3181
|
+
let frontier = seeds.map((s) => ({ row: s, hop: 0 }));
|
|
3182
|
+
for (let hop = 1; hop <= opts.maxHops; hop += 1) {
|
|
3183
|
+
const next = [];
|
|
3184
|
+
for (const f of frontier) {
|
|
3185
|
+
const ev = await this.eventStore.getEvent(f.row.eventId);
|
|
3186
|
+
if (!ev)
|
|
3187
|
+
continue;
|
|
3188
|
+
const rel = ev.metadata?.relatedEventIds ?? [];
|
|
3189
|
+
const relatedIds = Array.isArray(rel) ? rel.filter((x) => typeof x === "string") : [];
|
|
3190
|
+
for (const rid of relatedIds) {
|
|
3191
|
+
if (byId.has(rid))
|
|
3192
|
+
continue;
|
|
3193
|
+
const target = await this.eventStore.getEvent(rid);
|
|
3194
|
+
if (!target)
|
|
3195
|
+
continue;
|
|
3196
|
+
const score = Math.max(0, f.row.score - opts.hopPenalty * hop);
|
|
3197
|
+
const row = {
|
|
3198
|
+
id: `hop-${hop}-${rid}`,
|
|
3199
|
+
eventId: target.id,
|
|
3200
|
+
content: target.content,
|
|
3201
|
+
score,
|
|
3202
|
+
sessionId: target.sessionId,
|
|
3203
|
+
eventType: target.eventType,
|
|
3204
|
+
timestamp: target.timestamp.toISOString()
|
|
3205
|
+
};
|
|
3206
|
+
byId.set(row.eventId, row);
|
|
3207
|
+
next.push({ row, hop });
|
|
3208
|
+
if (byId.size >= opts.limit)
|
|
3209
|
+
break;
|
|
2766
3210
|
}
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
`;
|
|
3211
|
+
if (byId.size >= opts.limit)
|
|
3212
|
+
break;
|
|
2770
3213
|
}
|
|
3214
|
+
frontier = next;
|
|
3215
|
+
if (frontier.length === 0 || byId.size >= opts.limit)
|
|
3216
|
+
break;
|
|
2771
3217
|
}
|
|
2772
|
-
return
|
|
3218
|
+
return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, opts.limit);
|
|
3219
|
+
}
|
|
3220
|
+
shouldFallback(matchResult, results) {
|
|
3221
|
+
if (results.length === 0)
|
|
3222
|
+
return true;
|
|
3223
|
+
if (matchResult.confidence === "none")
|
|
3224
|
+
return true;
|
|
3225
|
+
return false;
|
|
3226
|
+
}
|
|
3227
|
+
async buildSummaryFallback(query, topK) {
|
|
3228
|
+
const recent = await this.eventStore.getRecentEvents(Math.max(topK * 6, 20));
|
|
3229
|
+
const q = this.tokenize(query);
|
|
3230
|
+
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) => ({
|
|
3231
|
+
id: `summary-${row.e.id}`,
|
|
3232
|
+
eventId: row.e.id,
|
|
3233
|
+
content: row.e.content,
|
|
3234
|
+
score: Math.max(0.25, 0.6 - idx * 0.05),
|
|
3235
|
+
sessionId: row.e.sessionId,
|
|
3236
|
+
eventType: row.e.eventType,
|
|
3237
|
+
timestamp: row.e.timestamp.toISOString()
|
|
3238
|
+
}));
|
|
3239
|
+
return ranked;
|
|
3240
|
+
}
|
|
3241
|
+
async searchByStrategy(query, input) {
|
|
3242
|
+
const strategy = input.strategy === "auto" ? "deep" : input.strategy;
|
|
3243
|
+
if (strategy === "fast") {
|
|
3244
|
+
const keyword = await this.searchByKeyword(query, {
|
|
3245
|
+
limit: Math.max(5, input.topK * 3),
|
|
3246
|
+
sessionId: input.sessionId
|
|
3247
|
+
});
|
|
3248
|
+
return keyword;
|
|
3249
|
+
}
|
|
3250
|
+
const queryEmbedding = await this.embedder.embed(query);
|
|
3251
|
+
return this.vectorStore.search(queryEmbedding.vector, {
|
|
3252
|
+
limit: Math.max(5, input.topK * 3),
|
|
3253
|
+
minScore: input.minScore,
|
|
3254
|
+
sessionId: input.sessionId
|
|
3255
|
+
});
|
|
3256
|
+
}
|
|
3257
|
+
async searchByKeyword(query, input) {
|
|
3258
|
+
if (this.eventStore.keywordSearch) {
|
|
3259
|
+
const rows = await this.eventStore.keywordSearch(query, input.limit);
|
|
3260
|
+
const filtered2 = input.sessionId ? rows.filter((r) => r.event.sessionId === input.sessionId) : rows;
|
|
3261
|
+
return filtered2.map((row, idx) => ({
|
|
3262
|
+
id: `kw-${row.event.id}`,
|
|
3263
|
+
eventId: row.event.id,
|
|
3264
|
+
content: row.event.content,
|
|
3265
|
+
score: Math.max(0.4, 1 - idx * 0.04),
|
|
3266
|
+
sessionId: row.event.sessionId,
|
|
3267
|
+
eventType: row.event.eventType,
|
|
3268
|
+
timestamp: row.event.timestamp.toISOString()
|
|
3269
|
+
}));
|
|
3270
|
+
}
|
|
3271
|
+
const recent = await this.eventStore.getRecentEvents(input.limit * 4);
|
|
3272
|
+
const tokens = this.tokenize(query);
|
|
3273
|
+
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);
|
|
3274
|
+
return filtered.map((row, idx) => ({
|
|
3275
|
+
id: `kw-fallback-${row.e.id}`,
|
|
3276
|
+
eventId: row.e.id,
|
|
3277
|
+
content: row.e.content,
|
|
3278
|
+
score: Math.max(0.3, 0.9 - idx * 0.05),
|
|
3279
|
+
sessionId: row.e.sessionId,
|
|
3280
|
+
eventType: row.e.eventType,
|
|
3281
|
+
timestamp: row.e.timestamp.toISOString()
|
|
3282
|
+
}));
|
|
3283
|
+
}
|
|
3284
|
+
rerankByKeywordOverlap(results, query, weights, decayPolicy) {
|
|
3285
|
+
const q = this.tokenize(query);
|
|
3286
|
+
const now = Date.now();
|
|
3287
|
+
const sw = Math.max(0, weights?.semantic ?? 0.7);
|
|
3288
|
+
const lw = Math.max(0, weights?.lexical ?? 0.2);
|
|
3289
|
+
const rw = Math.max(0, weights?.recency ?? 0.1);
|
|
3290
|
+
const total = sw + lw + rw || 1;
|
|
3291
|
+
const decayEnabled = decayPolicy?.enabled !== false;
|
|
3292
|
+
const decayWindow = Math.max(1, decayPolicy?.windowDays ?? 30);
|
|
3293
|
+
const decayMaxPenalty = Math.max(0, decayPolicy?.maxPenalty ?? 0.15);
|
|
3294
|
+
return [...results].map((r) => {
|
|
3295
|
+
const overlap = this.keywordOverlap(q, this.tokenize(r.content));
|
|
3296
|
+
const recencyDays = Math.max(0, (now - new Date(r.timestamp).getTime()) / (1e3 * 60 * 60 * 24));
|
|
3297
|
+
const recency = Math.max(0, 1 - recencyDays / decayWindow);
|
|
3298
|
+
let blended = (r.score * sw + overlap * lw + recency * rw) / total;
|
|
3299
|
+
if (decayEnabled && recencyDays > decayWindow && overlap < 0.5) {
|
|
3300
|
+
const ageFactor = Math.min(1, (recencyDays - decayWindow) / decayWindow);
|
|
3301
|
+
blended -= decayMaxPenalty * ageFactor;
|
|
3302
|
+
}
|
|
3303
|
+
return { ...r, score: Math.max(0, blended), semanticScore: r.score, lexicalScore: overlap, recencyScore: recency };
|
|
3304
|
+
}).sort((a, b) => b.score - a.score);
|
|
3305
|
+
}
|
|
3306
|
+
async applyScopeFilters(results, options) {
|
|
3307
|
+
const scope = options?.scope;
|
|
3308
|
+
const projectScopeMode = options?.projectScopeMode ?? "global";
|
|
3309
|
+
const allowedProjectHashes = new Set(
|
|
3310
|
+
[options?.projectHash, ...options?.allowedProjectHashes || []].filter(
|
|
3311
|
+
(value) => typeof value === "string" && value.length > 0
|
|
3312
|
+
)
|
|
3313
|
+
);
|
|
3314
|
+
if (!scope && projectScopeMode === "global")
|
|
3315
|
+
return results;
|
|
3316
|
+
const normalizedIncludes = (scope?.contentIncludes || []).map((s) => s.toLowerCase());
|
|
3317
|
+
const filtered = [];
|
|
3318
|
+
for (const result of results) {
|
|
3319
|
+
if (scope?.sessionId && result.sessionId !== scope.sessionId)
|
|
3320
|
+
continue;
|
|
3321
|
+
if (scope?.sessionIdPrefix && !result.sessionId.startsWith(scope.sessionIdPrefix))
|
|
3322
|
+
continue;
|
|
3323
|
+
if (scope?.eventTypes && scope.eventTypes.length > 0 && !scope.eventTypes.includes(result.eventType))
|
|
3324
|
+
continue;
|
|
3325
|
+
const event = await this.eventStore.getEvent(result.eventId);
|
|
3326
|
+
if (!event)
|
|
3327
|
+
continue;
|
|
3328
|
+
if (scope?.canonicalKeyPrefix && !event.canonicalKey.startsWith(scope.canonicalKeyPrefix))
|
|
3329
|
+
continue;
|
|
3330
|
+
if (normalizedIncludes.length > 0) {
|
|
3331
|
+
const lc = event.content.toLowerCase();
|
|
3332
|
+
if (!normalizedIncludes.some((needle) => lc.includes(needle)))
|
|
3333
|
+
continue;
|
|
3334
|
+
}
|
|
3335
|
+
if (scope?.metadata && !this.matchesMetadataScope(event.metadata, scope.metadata))
|
|
3336
|
+
continue;
|
|
3337
|
+
const projectHash = this.extractProjectHash(event.metadata);
|
|
3338
|
+
filtered.push({ result, projectHash });
|
|
3339
|
+
}
|
|
3340
|
+
if (projectScopeMode === "global" || allowedProjectHashes.size === 0) {
|
|
3341
|
+
return filtered.map((x) => x.result);
|
|
3342
|
+
}
|
|
3343
|
+
const projectMatched = filtered.filter((x) => x.projectHash && allowedProjectHashes.has(x.projectHash));
|
|
3344
|
+
if (projectScopeMode === "strict") {
|
|
3345
|
+
return projectMatched.map((x) => x.result);
|
|
3346
|
+
}
|
|
3347
|
+
return (projectMatched.length > 0 ? projectMatched : filtered).map((x) => x.result);
|
|
3348
|
+
}
|
|
3349
|
+
extractProjectHash(metadata) {
|
|
3350
|
+
if (!metadata || typeof metadata !== "object")
|
|
3351
|
+
return void 0;
|
|
3352
|
+
const scope = metadata.scope;
|
|
3353
|
+
if (!scope || typeof scope !== "object")
|
|
3354
|
+
return void 0;
|
|
3355
|
+
const project = scope.project;
|
|
3356
|
+
if (!project || typeof project !== "object")
|
|
3357
|
+
return void 0;
|
|
3358
|
+
const hash = project.hash;
|
|
3359
|
+
return typeof hash === "string" && hash.length > 0 ? hash : void 0;
|
|
2773
3360
|
}
|
|
2774
|
-
/**
|
|
2775
|
-
* Retrieve memories from a specific session
|
|
2776
|
-
*/
|
|
2777
3361
|
async retrieveFromSession(sessionId) {
|
|
2778
3362
|
return this.eventStore.getSessionEvents(sessionId);
|
|
2779
3363
|
}
|
|
2780
|
-
/**
|
|
2781
|
-
* Get recent memories across all sessions
|
|
2782
|
-
*/
|
|
2783
3364
|
async retrieveRecent(limit = 100) {
|
|
2784
3365
|
return this.eventStore.getRecentEvents(limit);
|
|
2785
3366
|
}
|
|
2786
|
-
/**
|
|
2787
|
-
* Enrich search results with full event data
|
|
2788
|
-
*/
|
|
2789
3367
|
async enrichResults(results, options) {
|
|
2790
3368
|
const memories = [];
|
|
2791
3369
|
for (const result of results) {
|
|
@@ -2793,27 +3371,16 @@ var Retriever = class {
|
|
|
2793
3371
|
if (!event)
|
|
2794
3372
|
continue;
|
|
2795
3373
|
if (this.graduation) {
|
|
2796
|
-
this.graduation.recordAccess(
|
|
2797
|
-
event.id,
|
|
2798
|
-
options.sessionId || "unknown",
|
|
2799
|
-
result.score
|
|
2800
|
-
);
|
|
3374
|
+
this.graduation.recordAccess(event.id, options.sessionId || "unknown", result.score);
|
|
2801
3375
|
}
|
|
2802
3376
|
let sessionContext;
|
|
2803
3377
|
if (options.includeSessionContext) {
|
|
2804
3378
|
sessionContext = await this.getSessionContext(event.sessionId, event.id);
|
|
2805
3379
|
}
|
|
2806
|
-
memories.push({
|
|
2807
|
-
event,
|
|
2808
|
-
score: result.score,
|
|
2809
|
-
sessionContext
|
|
2810
|
-
});
|
|
3380
|
+
memories.push({ event, score: result.score, sessionContext });
|
|
2811
3381
|
}
|
|
2812
3382
|
return memories;
|
|
2813
3383
|
}
|
|
2814
|
-
/**
|
|
2815
|
-
* Get surrounding context from the same session
|
|
2816
|
-
*/
|
|
2817
3384
|
async getSessionContext(sessionId, eventId) {
|
|
2818
3385
|
const sessionEvents = await this.eventStore.getSessionEvents(sessionId);
|
|
2819
3386
|
const eventIndex = sessionEvents.findIndex((e) => e.id === eventId);
|
|
@@ -2826,55 +3393,86 @@ var Retriever = class {
|
|
|
2826
3393
|
return void 0;
|
|
2827
3394
|
return contextEvents.filter((e) => e.id !== eventId).map((e) => `[${e.eventType}]: ${e.content.slice(0, 200)}...`).join("\n");
|
|
2828
3395
|
}
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
3396
|
+
buildUnifiedContext(projectResult, sharedMemories) {
|
|
3397
|
+
let context = projectResult.context;
|
|
3398
|
+
if (sharedMemories.length === 0)
|
|
3399
|
+
return context;
|
|
3400
|
+
context += "\n\n## Cross-Project Knowledge\n\n";
|
|
3401
|
+
for (const memory of sharedMemories.slice(0, 3)) {
|
|
3402
|
+
context += `### ${memory.title}
|
|
3403
|
+
`;
|
|
3404
|
+
if (memory.symptoms.length > 0)
|
|
3405
|
+
context += `**Symptoms:** ${memory.symptoms.join(", ")}
|
|
3406
|
+
`;
|
|
3407
|
+
context += `**Root Cause:** ${memory.rootCause}
|
|
3408
|
+
`;
|
|
3409
|
+
context += `**Solution:** ${memory.solution}
|
|
3410
|
+
`;
|
|
3411
|
+
if (memory.technologies && memory.technologies.length > 0)
|
|
3412
|
+
context += `**Technologies:** ${memory.technologies.join(", ")}
|
|
3413
|
+
`;
|
|
3414
|
+
context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
|
|
3415
|
+
|
|
3416
|
+
`;
|
|
3417
|
+
}
|
|
3418
|
+
return context;
|
|
3419
|
+
}
|
|
2832
3420
|
buildContext(memories, maxTokens) {
|
|
2833
3421
|
const parts = [];
|
|
2834
3422
|
let currentTokens = 0;
|
|
2835
3423
|
for (const memory of memories) {
|
|
2836
3424
|
const memoryText = this.formatMemory(memory);
|
|
2837
3425
|
const memoryTokens = this.estimateTokens(memoryText);
|
|
2838
|
-
if (currentTokens + memoryTokens > maxTokens)
|
|
3426
|
+
if (currentTokens + memoryTokens > maxTokens)
|
|
2839
3427
|
break;
|
|
2840
|
-
}
|
|
2841
3428
|
parts.push(memoryText);
|
|
2842
3429
|
currentTokens += memoryTokens;
|
|
2843
3430
|
}
|
|
2844
|
-
if (parts.length === 0)
|
|
3431
|
+
if (parts.length === 0)
|
|
2845
3432
|
return "";
|
|
2846
|
-
}
|
|
2847
3433
|
return `## Relevant Memories
|
|
2848
3434
|
|
|
2849
3435
|
${parts.join("\n\n---\n\n")}`;
|
|
2850
3436
|
}
|
|
2851
|
-
/**
|
|
2852
|
-
* Format a single memory for context
|
|
2853
|
-
*/
|
|
2854
3437
|
formatMemory(memory) {
|
|
2855
3438
|
const { event, score, sessionContext } = memory;
|
|
2856
3439
|
const date = event.timestamp.toISOString().split("T")[0];
|
|
2857
3440
|
let text = `**${event.eventType}** (${date}, score: ${score.toFixed(2)})
|
|
2858
3441
|
${event.content}`;
|
|
2859
|
-
if (sessionContext)
|
|
3442
|
+
if (sessionContext)
|
|
2860
3443
|
text += `
|
|
2861
3444
|
|
|
2862
3445
|
_Context:_ ${sessionContext}`;
|
|
2863
|
-
}
|
|
2864
3446
|
return text;
|
|
2865
3447
|
}
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
3448
|
+
matchesMetadataScope(metadata, expected) {
|
|
3449
|
+
if (!metadata)
|
|
3450
|
+
return false;
|
|
3451
|
+
return Object.entries(expected).every(([path7, value]) => {
|
|
3452
|
+
const actual = path7.split(".").reduce((acc, key) => {
|
|
3453
|
+
if (typeof acc !== "object" || acc === null)
|
|
3454
|
+
return void 0;
|
|
3455
|
+
return acc[key];
|
|
3456
|
+
}, metadata);
|
|
3457
|
+
return actual === value;
|
|
3458
|
+
});
|
|
3459
|
+
}
|
|
3460
|
+
tokenize(text) {
|
|
3461
|
+
return text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter((t) => t.length >= 2).slice(0, 64);
|
|
3462
|
+
}
|
|
3463
|
+
keywordOverlap(a, b) {
|
|
3464
|
+
if (a.length === 0 || b.length === 0)
|
|
3465
|
+
return 0;
|
|
3466
|
+
const bs = new Set(b);
|
|
3467
|
+
let hit = 0;
|
|
3468
|
+
for (const t of a)
|
|
3469
|
+
if (bs.has(t))
|
|
3470
|
+
hit += 1;
|
|
3471
|
+
return hit / a.length;
|
|
3472
|
+
}
|
|
2869
3473
|
estimateTokens(text) {
|
|
2870
3474
|
return Math.ceil(text.length / 4);
|
|
2871
3475
|
}
|
|
2872
|
-
/**
|
|
2873
|
-
* Get event age in days (for recency scoring)
|
|
2874
|
-
*/
|
|
2875
|
-
getEventAgeDays(eventId) {
|
|
2876
|
-
return 0;
|
|
2877
|
-
}
|
|
2878
3476
|
};
|
|
2879
3477
|
function createRetriever(eventStore, vectorStore, embedder, matcher) {
|
|
2880
3478
|
return new Retriever(eventStore, vectorStore, embedder, matcher);
|
|
@@ -4164,6 +4762,59 @@ var ConsolidatedStore = class {
|
|
|
4164
4762
|
[memoryId]
|
|
4165
4763
|
);
|
|
4166
4764
|
}
|
|
4765
|
+
/**
|
|
4766
|
+
* Create a long-term rule promoted from stable summaries
|
|
4767
|
+
*/
|
|
4768
|
+
async createRule(input) {
|
|
4769
|
+
const ruleId = randomUUID6();
|
|
4770
|
+
await dbRun(
|
|
4771
|
+
this.db,
|
|
4772
|
+
`INSERT INTO consolidated_rules
|
|
4773
|
+
(rule_id, rule, topics, source_memory_ids, source_events, confidence, created_at)
|
|
4774
|
+
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
|
|
4775
|
+
[
|
|
4776
|
+
ruleId,
|
|
4777
|
+
input.rule,
|
|
4778
|
+
JSON.stringify(input.topics),
|
|
4779
|
+
JSON.stringify(input.sourceMemoryIds),
|
|
4780
|
+
JSON.stringify(input.sourceEvents),
|
|
4781
|
+
input.confidence
|
|
4782
|
+
]
|
|
4783
|
+
);
|
|
4784
|
+
return ruleId;
|
|
4785
|
+
}
|
|
4786
|
+
async getRules(options) {
|
|
4787
|
+
const limit = options?.limit || 100;
|
|
4788
|
+
const rows = await dbAll(
|
|
4789
|
+
this.db,
|
|
4790
|
+
`SELECT * FROM consolidated_rules ORDER BY confidence DESC, created_at DESC LIMIT ?`,
|
|
4791
|
+
[limit]
|
|
4792
|
+
);
|
|
4793
|
+
return rows.map((row) => ({
|
|
4794
|
+
ruleId: row.rule_id,
|
|
4795
|
+
rule: row.rule,
|
|
4796
|
+
topics: JSON.parse(row.topics || "[]"),
|
|
4797
|
+
sourceMemoryIds: JSON.parse(row.source_memory_ids || "[]"),
|
|
4798
|
+
sourceEvents: JSON.parse(row.source_events || "[]"),
|
|
4799
|
+
confidence: Number(row.confidence ?? 0.5),
|
|
4800
|
+
createdAt: toDate(row.created_at) || /* @__PURE__ */ new Date()
|
|
4801
|
+
}));
|
|
4802
|
+
}
|
|
4803
|
+
async countRules() {
|
|
4804
|
+
const result = await dbAll(
|
|
4805
|
+
this.db,
|
|
4806
|
+
`SELECT COUNT(*) as count FROM consolidated_rules`
|
|
4807
|
+
);
|
|
4808
|
+
return result[0]?.count || 0;
|
|
4809
|
+
}
|
|
4810
|
+
async hasRuleForSourceMemory(memoryId) {
|
|
4811
|
+
const rows = await dbAll(
|
|
4812
|
+
this.db,
|
|
4813
|
+
`SELECT COUNT(*) as count FROM consolidated_rules WHERE source_memory_ids LIKE ?`,
|
|
4814
|
+
[`%"${memoryId}"%`]
|
|
4815
|
+
);
|
|
4816
|
+
return (rows[0]?.count || 0) > 0;
|
|
4817
|
+
}
|
|
4167
4818
|
/**
|
|
4168
4819
|
* Get count of consolidated memories
|
|
4169
4820
|
*/
|
|
@@ -4313,7 +4964,14 @@ var ConsolidationWorker = class {
|
|
|
4313
4964
|
* Force a consolidation run (manual trigger)
|
|
4314
4965
|
*/
|
|
4315
4966
|
async forceRun() {
|
|
4316
|
-
|
|
4967
|
+
const out = await this.consolidateWithReport();
|
|
4968
|
+
return out.consolidatedCount;
|
|
4969
|
+
}
|
|
4970
|
+
/**
|
|
4971
|
+
* Force a consolidation run and return metrics report
|
|
4972
|
+
*/
|
|
4973
|
+
async forceRunWithReport() {
|
|
4974
|
+
return this.consolidateWithReport();
|
|
4317
4975
|
}
|
|
4318
4976
|
/**
|
|
4319
4977
|
* Schedule the next consolidation check
|
|
@@ -4353,12 +5011,21 @@ var ConsolidationWorker = class {
|
|
|
4353
5011
|
* Perform consolidation
|
|
4354
5012
|
*/
|
|
4355
5013
|
async consolidate() {
|
|
5014
|
+
const out = await this.consolidateWithReport();
|
|
5015
|
+
return out.consolidatedCount;
|
|
5016
|
+
}
|
|
5017
|
+
async consolidateWithReport() {
|
|
4356
5018
|
const workingSet = await this.workingSetStore.get();
|
|
4357
5019
|
if (workingSet.recentEvents.length < 3) {
|
|
4358
|
-
return
|
|
5020
|
+
return {
|
|
5021
|
+
consolidatedCount: 0,
|
|
5022
|
+
promotedRuleCount: 0,
|
|
5023
|
+
report: this.buildCostQualityReport(workingSet.recentEvents, [], 0)
|
|
5024
|
+
};
|
|
4359
5025
|
}
|
|
4360
5026
|
const groups = this.groupByTopic(workingSet.recentEvents);
|
|
4361
5027
|
let consolidatedCount = 0;
|
|
5028
|
+
const createdMemoryIds = [];
|
|
4362
5029
|
for (const group of groups) {
|
|
4363
5030
|
if (group.events.length < 3)
|
|
4364
5031
|
continue;
|
|
@@ -4367,14 +5034,16 @@ var ConsolidationWorker = class {
|
|
|
4367
5034
|
if (alreadyConsolidated)
|
|
4368
5035
|
continue;
|
|
4369
5036
|
const summary = await this.summarize(group);
|
|
4370
|
-
await this.consolidatedStore.create({
|
|
5037
|
+
const memoryId = await this.consolidatedStore.create({
|
|
4371
5038
|
summary,
|
|
4372
5039
|
topics: group.topics,
|
|
4373
5040
|
sourceEvents: eventIds,
|
|
4374
5041
|
confidence: this.calculateConfidence(group)
|
|
4375
5042
|
});
|
|
5043
|
+
createdMemoryIds.push(memoryId);
|
|
4376
5044
|
consolidatedCount++;
|
|
4377
5045
|
}
|
|
5046
|
+
const promotedRuleCount = await this.promoteStableSummariesToRules(createdMemoryIds);
|
|
4378
5047
|
if (consolidatedCount > 0) {
|
|
4379
5048
|
const consolidatedEventIds = groups.filter((g) => g.events.length >= 3).flatMap((g) => g.events.map((e) => e.id));
|
|
4380
5049
|
const oldEventIds = consolidatedEventIds.filter((id) => {
|
|
@@ -4388,7 +5057,61 @@ var ConsolidationWorker = class {
|
|
|
4388
5057
|
await this.workingSetStore.prune(oldEventIds);
|
|
4389
5058
|
}
|
|
4390
5059
|
}
|
|
4391
|
-
|
|
5060
|
+
const report = this.buildCostQualityReport(workingSet.recentEvents, groups, consolidatedCount);
|
|
5061
|
+
return { consolidatedCount, promotedRuleCount, report };
|
|
5062
|
+
}
|
|
5063
|
+
async promoteStableSummariesToRules(memoryIds) {
|
|
5064
|
+
let promoted = 0;
|
|
5065
|
+
for (const memoryId of memoryIds) {
|
|
5066
|
+
const memory = await this.consolidatedStore.get(memoryId);
|
|
5067
|
+
if (!memory)
|
|
5068
|
+
continue;
|
|
5069
|
+
if (memory.confidence < 0.55)
|
|
5070
|
+
continue;
|
|
5071
|
+
if (memory.sourceEvents.length < 4)
|
|
5072
|
+
continue;
|
|
5073
|
+
const exists = await this.consolidatedStore.hasRuleForSourceMemory(memoryId);
|
|
5074
|
+
if (exists)
|
|
5075
|
+
continue;
|
|
5076
|
+
const rule = this.buildRuleFromSummary(memory.summary, memory.topics);
|
|
5077
|
+
if (!rule)
|
|
5078
|
+
continue;
|
|
5079
|
+
await this.consolidatedStore.createRule({
|
|
5080
|
+
rule,
|
|
5081
|
+
topics: memory.topics,
|
|
5082
|
+
sourceMemoryIds: [memory.memoryId],
|
|
5083
|
+
sourceEvents: memory.sourceEvents,
|
|
5084
|
+
confidence: Math.min(1, memory.confidence + 0.08)
|
|
5085
|
+
});
|
|
5086
|
+
promoted++;
|
|
5087
|
+
}
|
|
5088
|
+
return promoted;
|
|
5089
|
+
}
|
|
5090
|
+
buildRuleFromSummary(summary, topics) {
|
|
5091
|
+
const lines = summary.split(/\r?\n/).map((l) => l.trim()).filter(Boolean).filter((l) => !l.toLowerCase().startsWith("topics:"));
|
|
5092
|
+
const bullet = lines.find((l) => l.startsWith("- "))?.replace(/^-\s*/, "");
|
|
5093
|
+
const seed = bullet || lines[0];
|
|
5094
|
+
if (!seed || seed.length < 8)
|
|
5095
|
+
return null;
|
|
5096
|
+
const topicPrefix = topics.length > 0 ? `[${topics.slice(0, 2).join(", ")}] ` : "";
|
|
5097
|
+
return `${topicPrefix}${seed}`;
|
|
5098
|
+
}
|
|
5099
|
+
buildCostQualityReport(events, groups, consolidatedCount) {
|
|
5100
|
+
const beforeTokenEstimate = events.reduce((acc, e) => acc + this.estimateTokens(e.content), 0);
|
|
5101
|
+
const afterSummaries = groups.filter((g) => g.events.length >= 3).slice(0, Math.max(consolidatedCount, 1));
|
|
5102
|
+
const afterTokenEstimate = afterSummaries.length > 0 ? afterSummaries.reduce((acc, g) => acc + this.estimateTokens(this.ruleBasedSummary(g)), 0) : beforeTokenEstimate;
|
|
5103
|
+
const reductionRatio = beforeTokenEstimate > 0 ? Math.max(0, (beforeTokenEstimate - afterTokenEstimate) / beforeTokenEstimate) : 0;
|
|
5104
|
+
const qualityGuardPassed = consolidatedCount === 0 ? true : groups.filter((g) => g.events.length >= 3).every((g) => this.calculateConfidence(g) >= 0.55);
|
|
5105
|
+
return {
|
|
5106
|
+
beforeTokenEstimate,
|
|
5107
|
+
afterTokenEstimate,
|
|
5108
|
+
reductionRatio,
|
|
5109
|
+
qualityGuardPassed,
|
|
5110
|
+
details: `groups=${groups.length}, consolidated=${consolidatedCount}`
|
|
5111
|
+
};
|
|
5112
|
+
}
|
|
5113
|
+
estimateTokens(text) {
|
|
5114
|
+
return Math.ceil((text || "").length / 4);
|
|
4392
5115
|
}
|
|
4393
5116
|
/**
|
|
4394
5117
|
* Check if consolidation should run
|
|
@@ -4948,13 +5671,185 @@ function createGraduationWorker(eventStore, graduation, config) {
|
|
|
4948
5671
|
);
|
|
4949
5672
|
}
|
|
4950
5673
|
|
|
5674
|
+
// src/core/md-mirror.ts
|
|
5675
|
+
import * as fs3 from "node:fs";
|
|
5676
|
+
import * as path2 from "node:path";
|
|
5677
|
+
function sanitizeSegment2(input, fallback) {
|
|
5678
|
+
const v = (input || "").trim().toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
5679
|
+
return v || fallback;
|
|
5680
|
+
}
|
|
5681
|
+
function getAtPath(obj, dotted) {
|
|
5682
|
+
if (!obj)
|
|
5683
|
+
return void 0;
|
|
5684
|
+
return dotted.split(".").reduce((acc, key) => {
|
|
5685
|
+
if (!acc || typeof acc !== "object")
|
|
5686
|
+
return void 0;
|
|
5687
|
+
return acc[key];
|
|
5688
|
+
}, obj);
|
|
5689
|
+
}
|
|
5690
|
+
function buildMirrorPath2(rootDir, event) {
|
|
5691
|
+
const meta = event.metadata;
|
|
5692
|
+
const namespaceRaw = getAtPath(meta, "namespace") ?? getAtPath(meta, "scope.namespace") ?? event.eventType;
|
|
5693
|
+
const namespace = sanitizeSegment2(typeof namespaceRaw === "string" ? namespaceRaw : void 0, "general");
|
|
5694
|
+
const categoryRaw = getAtPath(meta, "categoryPath") ?? getAtPath(meta, "scope.categoryPath");
|
|
5695
|
+
const categoryPath = Array.isArray(categoryRaw) && categoryRaw.length > 0 ? categoryRaw.map((x) => sanitizeSegment2(typeof x === "string" ? x : void 0, "uncategorized")) : ["uncategorized"];
|
|
5696
|
+
const d = event.timestamp;
|
|
5697
|
+
const yyyy = d.getFullYear();
|
|
5698
|
+
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
5699
|
+
const dd = String(d.getDate()).padStart(2, "0");
|
|
5700
|
+
return path2.join(rootDir, "memory", namespace, ...categoryPath, `${yyyy}-${mm}-${dd}.md`);
|
|
5701
|
+
}
|
|
5702
|
+
var MarkdownMirror2 = class {
|
|
5703
|
+
constructor(rootDir) {
|
|
5704
|
+
this.rootDir = rootDir;
|
|
5705
|
+
}
|
|
5706
|
+
async append(event, eventId) {
|
|
5707
|
+
const out = buildMirrorPath2(this.rootDir, event);
|
|
5708
|
+
fs3.mkdirSync(path2.dirname(out), { recursive: true });
|
|
5709
|
+
const lines = [
|
|
5710
|
+
"",
|
|
5711
|
+
`## ${event.timestamp.toISOString()} | ${eventId ?? "pending-id"}`,
|
|
5712
|
+
`- type: ${event.eventType}`,
|
|
5713
|
+
`- session: ${event.sessionId}`,
|
|
5714
|
+
event.content
|
|
5715
|
+
];
|
|
5716
|
+
await fs3.promises.appendFile(out, lines.join("\n"), "utf8");
|
|
5717
|
+
await this.refreshIndex();
|
|
5718
|
+
}
|
|
5719
|
+
async refreshIndex() {
|
|
5720
|
+
const memoryRoot = path2.join(this.rootDir, "memory");
|
|
5721
|
+
await fs3.promises.mkdir(memoryRoot, { recursive: true });
|
|
5722
|
+
const files = [];
|
|
5723
|
+
await this.walk(memoryRoot, files);
|
|
5724
|
+
const mdFiles = files.filter((f) => f.endsWith(".md")).map((f) => path2.relative(this.rootDir, f)).filter((rel) => rel !== path2.join("memory", "_index.md")).sort();
|
|
5725
|
+
const index = [
|
|
5726
|
+
"# Memory Index",
|
|
5727
|
+
"",
|
|
5728
|
+
"Generated automatically by MarkdownMirror.",
|
|
5729
|
+
"",
|
|
5730
|
+
...mdFiles.map((rel) => `- ${rel}`),
|
|
5731
|
+
""
|
|
5732
|
+
].join("\n");
|
|
5733
|
+
await fs3.promises.writeFile(path2.join(memoryRoot, "_index.md"), index, "utf8");
|
|
5734
|
+
}
|
|
5735
|
+
async walk(dir, out) {
|
|
5736
|
+
const entries = await fs3.promises.readdir(dir, { withFileTypes: true });
|
|
5737
|
+
for (const e of entries) {
|
|
5738
|
+
const full = path2.join(dir, e.name);
|
|
5739
|
+
if (e.isDirectory()) {
|
|
5740
|
+
await this.walk(full, out);
|
|
5741
|
+
} else {
|
|
5742
|
+
out.push(full);
|
|
5743
|
+
}
|
|
5744
|
+
}
|
|
5745
|
+
}
|
|
5746
|
+
};
|
|
5747
|
+
|
|
5748
|
+
// src/core/ingest-interceptor.ts
|
|
5749
|
+
var IngestInterceptorRegistry = class {
|
|
5750
|
+
before = [];
|
|
5751
|
+
after = [];
|
|
5752
|
+
onError = [];
|
|
5753
|
+
registerBefore(interceptor) {
|
|
5754
|
+
this.before.push(interceptor);
|
|
5755
|
+
return () => {
|
|
5756
|
+
this.before = this.before.filter((i) => i !== interceptor);
|
|
5757
|
+
};
|
|
5758
|
+
}
|
|
5759
|
+
registerAfter(interceptor) {
|
|
5760
|
+
this.after.push(interceptor);
|
|
5761
|
+
return () => {
|
|
5762
|
+
this.after = this.after.filter((i) => i !== interceptor);
|
|
5763
|
+
};
|
|
5764
|
+
}
|
|
5765
|
+
registerOnError(interceptor) {
|
|
5766
|
+
this.onError.push(interceptor);
|
|
5767
|
+
return () => {
|
|
5768
|
+
this.onError = this.onError.filter((i) => i !== interceptor);
|
|
5769
|
+
};
|
|
5770
|
+
}
|
|
5771
|
+
async run(stage, context) {
|
|
5772
|
+
const interceptors = stage === "before" ? this.before : stage === "after" ? this.after : this.onError;
|
|
5773
|
+
for (const interceptor of interceptors) {
|
|
5774
|
+
await interceptor({ ...context, stage });
|
|
5775
|
+
}
|
|
5776
|
+
}
|
|
5777
|
+
};
|
|
5778
|
+
function mergeHierarchicalMetadata(base, patch) {
|
|
5779
|
+
if (!base && !patch)
|
|
5780
|
+
return void 0;
|
|
5781
|
+
if (!base)
|
|
5782
|
+
return patch;
|
|
5783
|
+
if (!patch)
|
|
5784
|
+
return base;
|
|
5785
|
+
const result = { ...base };
|
|
5786
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
5787
|
+
const current = result[key];
|
|
5788
|
+
if (typeof current === "object" && current !== null && !Array.isArray(current) && typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
5789
|
+
result[key] = mergeHierarchicalMetadata(
|
|
5790
|
+
current,
|
|
5791
|
+
value
|
|
5792
|
+
);
|
|
5793
|
+
} else {
|
|
5794
|
+
result[key] = value;
|
|
5795
|
+
}
|
|
5796
|
+
}
|
|
5797
|
+
return result;
|
|
5798
|
+
}
|
|
5799
|
+
|
|
5800
|
+
// src/core/tag-taxonomy.ts
|
|
5801
|
+
var TAG_NAMESPACES = {
|
|
5802
|
+
SYSTEM: "sys:",
|
|
5803
|
+
QUALITY: "q:",
|
|
5804
|
+
PROJECT: "proj:",
|
|
5805
|
+
TOPIC: "topic:",
|
|
5806
|
+
TEMPORAL: "t:",
|
|
5807
|
+
USER: "user:",
|
|
5808
|
+
AGENT: "agent:"
|
|
5809
|
+
};
|
|
5810
|
+
var VALID_TAG_NAMESPACES = new Set(Object.values(TAG_NAMESPACES));
|
|
5811
|
+
function parseTag(tag) {
|
|
5812
|
+
const value = (tag || "").trim();
|
|
5813
|
+
const idx = value.indexOf(":");
|
|
5814
|
+
if (idx <= 0)
|
|
5815
|
+
return { value };
|
|
5816
|
+
const namespace = `${value.slice(0, idx)}:`;
|
|
5817
|
+
const tagValue = value.slice(idx + 1);
|
|
5818
|
+
if (!tagValue)
|
|
5819
|
+
return { value };
|
|
5820
|
+
return { namespace, value: tagValue };
|
|
5821
|
+
}
|
|
5822
|
+
function validateTag(tag) {
|
|
5823
|
+
const normalized = (tag || "").trim();
|
|
5824
|
+
if (!normalized)
|
|
5825
|
+
return false;
|
|
5826
|
+
const { namespace } = parseTag(normalized);
|
|
5827
|
+
if (!namespace)
|
|
5828
|
+
return true;
|
|
5829
|
+
return VALID_TAG_NAMESPACES.has(namespace);
|
|
5830
|
+
}
|
|
5831
|
+
function normalizeTags(tags) {
|
|
5832
|
+
if (!Array.isArray(tags))
|
|
5833
|
+
return [];
|
|
5834
|
+
const dedup = /* @__PURE__ */ new Set();
|
|
5835
|
+
for (const item of tags) {
|
|
5836
|
+
if (typeof item !== "string")
|
|
5837
|
+
continue;
|
|
5838
|
+
const normalized = item.trim();
|
|
5839
|
+
if (!validateTag(normalized))
|
|
5840
|
+
continue;
|
|
5841
|
+
dedup.add(normalized);
|
|
5842
|
+
}
|
|
5843
|
+
return [...dedup];
|
|
5844
|
+
}
|
|
5845
|
+
|
|
4951
5846
|
// src/services/memory-service.ts
|
|
4952
5847
|
function normalizePath(projectPath) {
|
|
4953
|
-
const expanded = projectPath.startsWith("~") ?
|
|
5848
|
+
const expanded = projectPath.startsWith("~") ? path3.join(os.homedir(), projectPath.slice(1)) : projectPath;
|
|
4954
5849
|
try {
|
|
4955
|
-
return
|
|
5850
|
+
return fs4.realpathSync(expanded);
|
|
4956
5851
|
} catch {
|
|
4957
|
-
return
|
|
5852
|
+
return path3.resolve(expanded);
|
|
4958
5853
|
}
|
|
4959
5854
|
}
|
|
4960
5855
|
function hashProjectPath(projectPath) {
|
|
@@ -4963,14 +5858,14 @@ function hashProjectPath(projectPath) {
|
|
|
4963
5858
|
}
|
|
4964
5859
|
function getProjectStoragePath(projectPath) {
|
|
4965
5860
|
const hash = hashProjectPath(projectPath);
|
|
4966
|
-
return
|
|
5861
|
+
return path3.join(os.homedir(), ".claude-code", "memory", "projects", hash);
|
|
4967
5862
|
}
|
|
4968
|
-
var REGISTRY_PATH =
|
|
4969
|
-
var SHARED_STORAGE_PATH =
|
|
5863
|
+
var REGISTRY_PATH = path3.join(os.homedir(), ".claude-code", "memory", "session-registry.json");
|
|
5864
|
+
var SHARED_STORAGE_PATH = path3.join(os.homedir(), ".claude-code", "memory", "shared");
|
|
4970
5865
|
function loadSessionRegistry() {
|
|
4971
5866
|
try {
|
|
4972
|
-
if (
|
|
4973
|
-
const data =
|
|
5867
|
+
if (fs4.existsSync(REGISTRY_PATH)) {
|
|
5868
|
+
const data = fs4.readFileSync(REGISTRY_PATH, "utf-8");
|
|
4974
5869
|
return JSON.parse(data);
|
|
4975
5870
|
}
|
|
4976
5871
|
} catch (error) {
|
|
@@ -4992,6 +5887,7 @@ var MemoryService = class {
|
|
|
4992
5887
|
vectorWorker = null;
|
|
4993
5888
|
graduationWorker = null;
|
|
4994
5889
|
initialized = false;
|
|
5890
|
+
ingestInterceptors = new IngestInterceptorRegistry();
|
|
4995
5891
|
// Endless Mode components
|
|
4996
5892
|
workingSetStore = null;
|
|
4997
5893
|
consolidatedStore = null;
|
|
@@ -5005,20 +5901,27 @@ var MemoryService = class {
|
|
|
5005
5901
|
sharedPromoter = null;
|
|
5006
5902
|
sharedStoreConfig = null;
|
|
5007
5903
|
projectHash = null;
|
|
5904
|
+
projectPath = null;
|
|
5008
5905
|
readOnly;
|
|
5009
5906
|
lightweightMode;
|
|
5907
|
+
mdMirror;
|
|
5010
5908
|
constructor(config) {
|
|
5011
5909
|
const storagePath = this.expandPath(config.storagePath);
|
|
5012
5910
|
this.readOnly = config.readOnly ?? false;
|
|
5013
5911
|
this.lightweightMode = config.lightweightMode ?? false;
|
|
5014
|
-
|
|
5015
|
-
|
|
5912
|
+
this.mdMirror = new MarkdownMirror2(process.cwd());
|
|
5913
|
+
if (!this.readOnly && !fs4.existsSync(storagePath)) {
|
|
5914
|
+
fs4.mkdirSync(storagePath, { recursive: true });
|
|
5016
5915
|
}
|
|
5017
5916
|
this.projectHash = config.projectHash || null;
|
|
5917
|
+
this.projectPath = config.projectPath || null;
|
|
5018
5918
|
this.sharedStoreConfig = config.sharedStoreConfig ?? { enabled: true };
|
|
5019
5919
|
this.sqliteStore = new SQLiteEventStore(
|
|
5020
|
-
|
|
5021
|
-
{
|
|
5920
|
+
path3.join(storagePath, "events.sqlite"),
|
|
5921
|
+
{
|
|
5922
|
+
readonly: this.readOnly,
|
|
5923
|
+
markdownMirrorRoot: storagePath
|
|
5924
|
+
}
|
|
5022
5925
|
);
|
|
5023
5926
|
const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
|
|
5024
5927
|
if (!analyticsEnabled) {
|
|
@@ -5026,7 +5929,7 @@ var MemoryService = class {
|
|
|
5026
5929
|
} else if (this.readOnly) {
|
|
5027
5930
|
try {
|
|
5028
5931
|
this.analyticsStore = new EventStore(
|
|
5029
|
-
|
|
5932
|
+
path3.join(storagePath, "analytics.duckdb"),
|
|
5030
5933
|
{ readOnly: true }
|
|
5031
5934
|
);
|
|
5032
5935
|
} catch {
|
|
@@ -5034,11 +5937,11 @@ var MemoryService = class {
|
|
|
5034
5937
|
}
|
|
5035
5938
|
} else {
|
|
5036
5939
|
this.analyticsStore = new EventStore(
|
|
5037
|
-
|
|
5940
|
+
path3.join(storagePath, "analytics.duckdb"),
|
|
5038
5941
|
{ readOnly: false }
|
|
5039
5942
|
);
|
|
5040
5943
|
}
|
|
5041
|
-
this.vectorStore = new VectorStore(
|
|
5944
|
+
this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
|
|
5042
5945
|
this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
|
|
5043
5946
|
this.matcher = getDefaultMatcher();
|
|
5044
5947
|
this.retriever = createRetriever(
|
|
@@ -5048,6 +5951,7 @@ var MemoryService = class {
|
|
|
5048
5951
|
this.embedder,
|
|
5049
5952
|
this.matcher
|
|
5050
5953
|
);
|
|
5954
|
+
this.retriever.setQueryRewriter((q) => this.rewriteQueryIntent(q));
|
|
5051
5955
|
this.graduation = createGraduationPipeline(this.sqliteStore);
|
|
5052
5956
|
}
|
|
5053
5957
|
/**
|
|
@@ -5107,16 +6011,16 @@ var MemoryService = class {
|
|
|
5107
6011
|
*/
|
|
5108
6012
|
async initializeSharedStore() {
|
|
5109
6013
|
const sharedPath = this.sharedStoreConfig?.sharedStoragePath ? this.expandPath(this.sharedStoreConfig.sharedStoragePath) : SHARED_STORAGE_PATH;
|
|
5110
|
-
if (!
|
|
5111
|
-
|
|
6014
|
+
if (!fs4.existsSync(sharedPath)) {
|
|
6015
|
+
fs4.mkdirSync(sharedPath, { recursive: true });
|
|
5112
6016
|
}
|
|
5113
6017
|
this.sharedEventStore = createSharedEventStore(
|
|
5114
|
-
|
|
6018
|
+
path3.join(sharedPath, "shared.duckdb")
|
|
5115
6019
|
);
|
|
5116
6020
|
await this.sharedEventStore.initialize();
|
|
5117
6021
|
this.sharedStore = createSharedStore(this.sharedEventStore);
|
|
5118
6022
|
this.sharedVectorStore = createSharedVectorStore(
|
|
5119
|
-
|
|
6023
|
+
path3.join(sharedPath, "vectors")
|
|
5120
6024
|
);
|
|
5121
6025
|
await this.sharedVectorStore.initialize();
|
|
5122
6026
|
this.sharedPromoter = createSharedPromoter(
|
|
@@ -5127,6 +6031,86 @@ var MemoryService = class {
|
|
|
5127
6031
|
);
|
|
5128
6032
|
this.retriever.setSharedStores(this.sharedStore, this.sharedVectorStore);
|
|
5129
6033
|
}
|
|
6034
|
+
registerIngestBefore(interceptor) {
|
|
6035
|
+
return this.ingestInterceptors.registerBefore(interceptor);
|
|
6036
|
+
}
|
|
6037
|
+
registerIngestAfter(interceptor) {
|
|
6038
|
+
return this.ingestInterceptors.registerAfter(interceptor);
|
|
6039
|
+
}
|
|
6040
|
+
registerIngestOnError(interceptor) {
|
|
6041
|
+
return this.ingestInterceptors.registerOnError(interceptor);
|
|
6042
|
+
}
|
|
6043
|
+
async ingestWithInterceptors(operation, input, onSuccess) {
|
|
6044
|
+
const normalizedInput = {
|
|
6045
|
+
...input,
|
|
6046
|
+
metadata: mergeHierarchicalMetadata(
|
|
6047
|
+
{
|
|
6048
|
+
ingest: {
|
|
6049
|
+
operation,
|
|
6050
|
+
pipeline: "default",
|
|
6051
|
+
ts: (/* @__PURE__ */ new Date()).toISOString()
|
|
6052
|
+
},
|
|
6053
|
+
...this.projectHash ? {
|
|
6054
|
+
scope: {
|
|
6055
|
+
project: {
|
|
6056
|
+
hash: this.projectHash,
|
|
6057
|
+
...this.projectPath ? { path: this.projectPath } : {}
|
|
6058
|
+
}
|
|
6059
|
+
},
|
|
6060
|
+
tags: [`proj:${this.projectHash}`]
|
|
6061
|
+
} : {}
|
|
6062
|
+
},
|
|
6063
|
+
input.metadata
|
|
6064
|
+
)
|
|
6065
|
+
};
|
|
6066
|
+
if (this.projectHash && normalizedInput.metadata) {
|
|
6067
|
+
const meta = normalizedInput.metadata;
|
|
6068
|
+
const currentTags = Array.isArray(meta.tags) ? meta.tags.filter((x) => typeof x === "string") : [];
|
|
6069
|
+
const projectTag = `proj:${this.projectHash}`;
|
|
6070
|
+
if (!currentTags.includes(projectTag)) {
|
|
6071
|
+
meta.tags = [...currentTags, projectTag];
|
|
6072
|
+
}
|
|
6073
|
+
}
|
|
6074
|
+
if (normalizedInput.metadata) {
|
|
6075
|
+
const meta = normalizedInput.metadata;
|
|
6076
|
+
const normalizedTags = normalizeTags(meta.tags);
|
|
6077
|
+
if (normalizedTags.length > 0) {
|
|
6078
|
+
meta.tags = normalizedTags;
|
|
6079
|
+
}
|
|
6080
|
+
}
|
|
6081
|
+
await this.ingestInterceptors.run("before", {
|
|
6082
|
+
operation,
|
|
6083
|
+
sessionId: normalizedInput.sessionId,
|
|
6084
|
+
event: normalizedInput
|
|
6085
|
+
});
|
|
6086
|
+
try {
|
|
6087
|
+
const result = await this.sqliteStore.append(normalizedInput);
|
|
6088
|
+
if (result.success && !result.isDuplicate) {
|
|
6089
|
+
if (onSuccess) {
|
|
6090
|
+
await onSuccess(result.eventId);
|
|
6091
|
+
}
|
|
6092
|
+
try {
|
|
6093
|
+
await this.mdMirror.append(normalizedInput, result.eventId);
|
|
6094
|
+
} catch {
|
|
6095
|
+
}
|
|
6096
|
+
}
|
|
6097
|
+
await this.ingestInterceptors.run("after", {
|
|
6098
|
+
operation,
|
|
6099
|
+
sessionId: normalizedInput.sessionId,
|
|
6100
|
+
event: normalizedInput
|
|
6101
|
+
});
|
|
6102
|
+
return result;
|
|
6103
|
+
} catch (error) {
|
|
6104
|
+
const normalizedError = error instanceof Error ? error : new Error(String(error));
|
|
6105
|
+
await this.ingestInterceptors.run("error", {
|
|
6106
|
+
operation,
|
|
6107
|
+
sessionId: normalizedInput.sessionId,
|
|
6108
|
+
event: normalizedInput,
|
|
6109
|
+
error: normalizedError
|
|
6110
|
+
});
|
|
6111
|
+
throw error;
|
|
6112
|
+
}
|
|
6113
|
+
}
|
|
5130
6114
|
/**
|
|
5131
6115
|
* Start a new session
|
|
5132
6116
|
*/
|
|
@@ -5154,50 +6138,57 @@ var MemoryService = class {
|
|
|
5154
6138
|
*/
|
|
5155
6139
|
async storeUserPrompt(sessionId, content, metadata) {
|
|
5156
6140
|
await this.initialize();
|
|
5157
|
-
|
|
5158
|
-
|
|
5159
|
-
|
|
5160
|
-
|
|
5161
|
-
|
|
5162
|
-
|
|
5163
|
-
|
|
5164
|
-
|
|
5165
|
-
|
|
5166
|
-
|
|
5167
|
-
|
|
6141
|
+
return this.ingestWithInterceptors(
|
|
6142
|
+
"user_prompt",
|
|
6143
|
+
{
|
|
6144
|
+
eventType: "user_prompt",
|
|
6145
|
+
sessionId,
|
|
6146
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
6147
|
+
content,
|
|
6148
|
+
metadata
|
|
6149
|
+
},
|
|
6150
|
+
async (eventId) => {
|
|
6151
|
+
await this.sqliteStore.enqueueForEmbedding(eventId, content);
|
|
6152
|
+
}
|
|
6153
|
+
);
|
|
5168
6154
|
}
|
|
5169
6155
|
/**
|
|
5170
6156
|
* Store an agent response
|
|
5171
6157
|
*/
|
|
5172
6158
|
async storeAgentResponse(sessionId, content, metadata) {
|
|
5173
6159
|
await this.initialize();
|
|
5174
|
-
|
|
5175
|
-
|
|
5176
|
-
|
|
5177
|
-
|
|
5178
|
-
|
|
5179
|
-
|
|
5180
|
-
|
|
5181
|
-
|
|
5182
|
-
|
|
5183
|
-
|
|
5184
|
-
|
|
6160
|
+
return this.ingestWithInterceptors(
|
|
6161
|
+
"agent_response",
|
|
6162
|
+
{
|
|
6163
|
+
eventType: "agent_response",
|
|
6164
|
+
sessionId,
|
|
6165
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
6166
|
+
content,
|
|
6167
|
+
metadata
|
|
6168
|
+
},
|
|
6169
|
+
async (eventId) => {
|
|
6170
|
+
await this.sqliteStore.enqueueForEmbedding(eventId, content);
|
|
6171
|
+
}
|
|
6172
|
+
);
|
|
5185
6173
|
}
|
|
5186
6174
|
/**
|
|
5187
6175
|
* Store a session summary
|
|
5188
6176
|
*/
|
|
5189
|
-
async storeSessionSummary(sessionId, summary) {
|
|
6177
|
+
async storeSessionSummary(sessionId, summary, metadata) {
|
|
5190
6178
|
await this.initialize();
|
|
5191
|
-
|
|
5192
|
-
|
|
5193
|
-
|
|
5194
|
-
|
|
5195
|
-
|
|
5196
|
-
|
|
5197
|
-
|
|
5198
|
-
|
|
5199
|
-
|
|
5200
|
-
|
|
6179
|
+
return this.ingestWithInterceptors(
|
|
6180
|
+
"session_summary",
|
|
6181
|
+
{
|
|
6182
|
+
eventType: "session_summary",
|
|
6183
|
+
sessionId,
|
|
6184
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
6185
|
+
content: summary,
|
|
6186
|
+
metadata
|
|
6187
|
+
},
|
|
6188
|
+
async (eventId) => {
|
|
6189
|
+
await this.sqliteStore.enqueueForEmbedding(eventId, summary);
|
|
6190
|
+
}
|
|
6191
|
+
);
|
|
5201
6192
|
}
|
|
5202
6193
|
/**
|
|
5203
6194
|
* Store a tool observation
|
|
@@ -5206,40 +6197,181 @@ var MemoryService = class {
|
|
|
5206
6197
|
await this.initialize();
|
|
5207
6198
|
const content = JSON.stringify(payload);
|
|
5208
6199
|
const turnId = payload.metadata?.turnId;
|
|
5209
|
-
|
|
5210
|
-
|
|
5211
|
-
|
|
5212
|
-
|
|
5213
|
-
|
|
5214
|
-
|
|
5215
|
-
|
|
5216
|
-
|
|
5217
|
-
|
|
6200
|
+
return this.ingestWithInterceptors(
|
|
6201
|
+
"tool_observation",
|
|
6202
|
+
{
|
|
6203
|
+
eventType: "tool_observation",
|
|
6204
|
+
sessionId,
|
|
6205
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
6206
|
+
content,
|
|
6207
|
+
metadata: {
|
|
6208
|
+
toolName: payload.toolName,
|
|
6209
|
+
success: payload.success,
|
|
6210
|
+
...turnId ? { turnId } : {}
|
|
6211
|
+
}
|
|
6212
|
+
},
|
|
6213
|
+
async (eventId) => {
|
|
6214
|
+
const embeddingContent = createToolObservationEmbedding(
|
|
6215
|
+
payload.toolName,
|
|
6216
|
+
payload.metadata || {},
|
|
6217
|
+
payload.success
|
|
6218
|
+
);
|
|
6219
|
+
await this.sqliteStore.enqueueForEmbedding(eventId, embeddingContent);
|
|
5218
6220
|
}
|
|
5219
|
-
|
|
5220
|
-
if (result.success && !result.isDuplicate) {
|
|
5221
|
-
const embeddingContent = createToolObservationEmbedding(
|
|
5222
|
-
payload.toolName,
|
|
5223
|
-
payload.metadata || {},
|
|
5224
|
-
payload.success
|
|
5225
|
-
);
|
|
5226
|
-
await this.sqliteStore.enqueueForEmbedding(result.eventId, embeddingContent);
|
|
5227
|
-
}
|
|
5228
|
-
return result;
|
|
6221
|
+
);
|
|
5229
6222
|
}
|
|
5230
6223
|
/**
|
|
5231
6224
|
* Retrieve relevant memories for a query
|
|
5232
6225
|
*/
|
|
5233
6226
|
async retrieveMemories(query, options) {
|
|
5234
6227
|
await this.initialize();
|
|
6228
|
+
const rerankWeights = await this.getRerankWeights(options?.adaptiveRerank === true);
|
|
6229
|
+
let result;
|
|
5235
6230
|
if (options?.includeShared && this.sharedStore) {
|
|
5236
|
-
|
|
6231
|
+
result = await this.retriever.retrieveUnified(query, {
|
|
5237
6232
|
...options,
|
|
6233
|
+
intentRewrite: options?.intentRewrite === true,
|
|
6234
|
+
rerankWeights,
|
|
5238
6235
|
includeShared: true,
|
|
5239
|
-
projectHash: this.projectHash || void 0
|
|
6236
|
+
projectHash: this.projectHash || void 0,
|
|
6237
|
+
projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
|
|
6238
|
+
allowedProjectHashes: options?.allowedProjectHashes
|
|
6239
|
+
});
|
|
6240
|
+
} else {
|
|
6241
|
+
result = await this.retriever.retrieve(query, {
|
|
6242
|
+
...options,
|
|
6243
|
+
intentRewrite: options?.intentRewrite === true,
|
|
6244
|
+
rerankWeights,
|
|
6245
|
+
projectHash: this.projectHash || void 0,
|
|
6246
|
+
projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
|
|
6247
|
+
allowedProjectHashes: options?.allowedProjectHashes
|
|
6248
|
+
});
|
|
6249
|
+
}
|
|
6250
|
+
try {
|
|
6251
|
+
const selectedEventIds = result.memories.map((m) => m.event.id);
|
|
6252
|
+
const selectedDetails = (result.selectedDebug || []).map((d) => ({
|
|
6253
|
+
eventId: d.eventId,
|
|
6254
|
+
score: d.score,
|
|
6255
|
+
semanticScore: d.semanticScore,
|
|
6256
|
+
lexicalScore: d.lexicalScore,
|
|
6257
|
+
recencyScore: d.recencyScore
|
|
6258
|
+
}));
|
|
6259
|
+
const candidateDetails = (result.candidateDebug || []).map((d) => ({
|
|
6260
|
+
eventId: d.eventId,
|
|
6261
|
+
score: d.score,
|
|
6262
|
+
semanticScore: d.semanticScore,
|
|
6263
|
+
lexicalScore: d.lexicalScore,
|
|
6264
|
+
recencyScore: d.recencyScore
|
|
6265
|
+
}));
|
|
6266
|
+
const candidateEventIds = candidateDetails.length > 0 ? candidateDetails.map((d) => d.eventId) : selectedEventIds;
|
|
6267
|
+
await this.sqliteStore.recordRetrievalTrace({
|
|
6268
|
+
sessionId: options?.sessionId,
|
|
6269
|
+
projectHash: this.projectHash || void 0,
|
|
6270
|
+
queryText: query,
|
|
6271
|
+
strategy: options?.strategy || "auto",
|
|
6272
|
+
candidateEventIds,
|
|
6273
|
+
selectedEventIds,
|
|
6274
|
+
candidateDetails,
|
|
6275
|
+
selectedDetails,
|
|
6276
|
+
confidence: result.matchResult.confidence,
|
|
6277
|
+
fallbackTrace: result.fallbackTrace || []
|
|
6278
|
+
});
|
|
6279
|
+
} catch {
|
|
6280
|
+
}
|
|
6281
|
+
return result;
|
|
6282
|
+
}
|
|
6283
|
+
getConfiguredRerankWeights() {
|
|
6284
|
+
const semantic = Number(process.env.MEMORY_RERANK_WEIGHT_SEMANTIC ?? "");
|
|
6285
|
+
const lexical = Number(process.env.MEMORY_RERANK_WEIGHT_LEXICAL ?? "");
|
|
6286
|
+
const recency = Number(process.env.MEMORY_RERANK_WEIGHT_RECENCY ?? "");
|
|
6287
|
+
const allFinite = [semantic, lexical, recency].every((v) => Number.isFinite(v));
|
|
6288
|
+
if (!allFinite)
|
|
6289
|
+
return void 0;
|
|
6290
|
+
const nonNegative = [semantic, lexical, recency].every((v) => v >= 0);
|
|
6291
|
+
const total = semantic + lexical + recency;
|
|
6292
|
+
if (!nonNegative || total <= 0)
|
|
6293
|
+
return void 0;
|
|
6294
|
+
return {
|
|
6295
|
+
semantic: semantic / total,
|
|
6296
|
+
lexical: lexical / total,
|
|
6297
|
+
recency: recency / total
|
|
6298
|
+
};
|
|
6299
|
+
}
|
|
6300
|
+
async getRerankWeights(adaptive) {
|
|
6301
|
+
const configured = this.getConfiguredRerankWeights();
|
|
6302
|
+
if (configured)
|
|
6303
|
+
return configured;
|
|
6304
|
+
if (adaptive)
|
|
6305
|
+
return this.getAdaptiveRerankWeights();
|
|
6306
|
+
return void 0;
|
|
6307
|
+
}
|
|
6308
|
+
async rewriteQueryIntent(query) {
|
|
6309
|
+
if (process.env.MEMORY_INTENT_REWRITE_ENABLED !== "1")
|
|
6310
|
+
return null;
|
|
6311
|
+
const apiUrl = process.env.COMPANY_STOCK_API_URL || process.env.COMPANY_INT_API_URL;
|
|
6312
|
+
if (!apiUrl)
|
|
6313
|
+
return null;
|
|
6314
|
+
const controller = new AbortController();
|
|
6315
|
+
const timeoutMs = Number(process.env.MEMORY_INTENT_REWRITE_TIMEOUT_MS || 5e3);
|
|
6316
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
6317
|
+
try {
|
|
6318
|
+
const prompt = [
|
|
6319
|
+
"Rewrite user query for memory retrieval intent expansion.",
|
|
6320
|
+
"Return plain text only, one line, no markdown.",
|
|
6321
|
+
`Query: ${query}`
|
|
6322
|
+
].join("\n");
|
|
6323
|
+
const res = await fetch(apiUrl, {
|
|
6324
|
+
method: "POST",
|
|
6325
|
+
headers: {
|
|
6326
|
+
"Content-Type": "application/json",
|
|
6327
|
+
Accept: "*/*",
|
|
6328
|
+
Origin: process.env.COMPANY_INT_ORIGIN || "http://company-int.aplusai.ai",
|
|
6329
|
+
Referer: process.env.COMPANY_INT_REFERER || "http://company-int.aplusai.ai/"
|
|
6330
|
+
},
|
|
6331
|
+
body: JSON.stringify({
|
|
6332
|
+
question: prompt,
|
|
6333
|
+
company_name: null,
|
|
6334
|
+
conversation_id: null
|
|
6335
|
+
}),
|
|
6336
|
+
signal: controller.signal
|
|
5240
6337
|
});
|
|
6338
|
+
const text = (await res.text()).trim();
|
|
6339
|
+
if (!text)
|
|
6340
|
+
return null;
|
|
6341
|
+
const oneLine = text.replace(/^data:\s*/gm, "").split(/\r?\n/).map((x) => x.trim()).filter(Boolean).join(" ").slice(0, 240);
|
|
6342
|
+
if (!oneLine || oneLine.toLowerCase() === query.toLowerCase())
|
|
6343
|
+
return null;
|
|
6344
|
+
return oneLine;
|
|
6345
|
+
} catch {
|
|
6346
|
+
return null;
|
|
6347
|
+
} finally {
|
|
6348
|
+
clearTimeout(timeout);
|
|
6349
|
+
}
|
|
6350
|
+
}
|
|
6351
|
+
async getAdaptiveRerankWeights() {
|
|
6352
|
+
try {
|
|
6353
|
+
const s = await this.sqliteStore.getHelpfulnessStats();
|
|
6354
|
+
if (s.totalEvaluated < 20)
|
|
6355
|
+
return void 0;
|
|
6356
|
+
let semantic = 0.7;
|
|
6357
|
+
let lexical = 0.2;
|
|
6358
|
+
let recency = 0.1;
|
|
6359
|
+
if (s.avgScore < 0.45) {
|
|
6360
|
+
semantic -= 0.1;
|
|
6361
|
+
lexical += 0.1;
|
|
6362
|
+
} else if (s.avgScore > 0.75) {
|
|
6363
|
+
semantic += 0.05;
|
|
6364
|
+
lexical -= 0.05;
|
|
6365
|
+
}
|
|
6366
|
+
if (s.unhelpful > s.helpful) {
|
|
6367
|
+
recency += 0.05;
|
|
6368
|
+
semantic -= 0.03;
|
|
6369
|
+
lexical -= 0.02;
|
|
6370
|
+
}
|
|
6371
|
+
return { semantic, lexical, recency };
|
|
6372
|
+
} catch {
|
|
6373
|
+
return void 0;
|
|
5241
6374
|
}
|
|
5242
|
-
return this.retriever.retrieve(query, options);
|
|
5243
6375
|
}
|
|
5244
6376
|
/**
|
|
5245
6377
|
* Fast keyword search using SQLite FTS5
|
|
@@ -5281,6 +6413,18 @@ var MemoryService = class {
|
|
|
5281
6413
|
/**
|
|
5282
6414
|
* Get memory statistics
|
|
5283
6415
|
*/
|
|
6416
|
+
async getOutboxStats() {
|
|
6417
|
+
await this.initialize();
|
|
6418
|
+
return this.sqliteStore.getOutboxStats();
|
|
6419
|
+
}
|
|
6420
|
+
async getRetrievalTraceStats() {
|
|
6421
|
+
await this.initialize();
|
|
6422
|
+
return this.sqliteStore.getRetrievalTraceStats();
|
|
6423
|
+
}
|
|
6424
|
+
async getRecentRetrievalTraces(limit = 50) {
|
|
6425
|
+
await this.initialize();
|
|
6426
|
+
return this.sqliteStore.getRecentRetrievalTraces(limit);
|
|
6427
|
+
}
|
|
5284
6428
|
async getStats() {
|
|
5285
6429
|
await this.initialize();
|
|
5286
6430
|
const recentEvents = await this.sqliteStore.getRecentEvents(1e4);
|
|
@@ -5771,7 +6915,7 @@ var MemoryService = class {
|
|
|
5771
6915
|
*/
|
|
5772
6916
|
expandPath(p) {
|
|
5773
6917
|
if (p.startsWith("~")) {
|
|
5774
|
-
return
|
|
6918
|
+
return path3.join(os.homedir(), p.slice(1));
|
|
5775
6919
|
}
|
|
5776
6920
|
return p;
|
|
5777
6921
|
}
|
|
@@ -5794,6 +6938,7 @@ function getMemoryServiceForProject(projectPath, sharedStoreConfig) {
|
|
|
5794
6938
|
serviceCache.set(hash, new MemoryService({
|
|
5795
6939
|
storagePath,
|
|
5796
6940
|
projectHash: hash,
|
|
6941
|
+
projectPath,
|
|
5797
6942
|
// Override shared store config - hooks don't need DuckDB
|
|
5798
6943
|
sharedStoreConfig: sharedStoreConfig ?? { enabled: false },
|
|
5799
6944
|
analyticsEnabled: false
|
|
@@ -5810,12 +6955,12 @@ function getServiceFromQuery(c) {
|
|
|
5810
6955
|
const isHash = /^[a-f0-9]{8}$/.test(project);
|
|
5811
6956
|
let storagePath;
|
|
5812
6957
|
if (isHash) {
|
|
5813
|
-
storagePath =
|
|
6958
|
+
storagePath = path4.join(os2.homedir(), ".claude-code", "memory", "projects", project);
|
|
5814
6959
|
} else {
|
|
5815
6960
|
const crypto3 = __require("crypto");
|
|
5816
6961
|
const normalized = project.replace(/\/+$/, "") || "/";
|
|
5817
6962
|
const hash = crypto3.createHash("sha256").update(normalized).digest("hex").slice(0, 8);
|
|
5818
|
-
storagePath =
|
|
6963
|
+
storagePath = path4.join(os2.homedir(), ".claude-code", "memory", "projects", hash);
|
|
5819
6964
|
}
|
|
5820
6965
|
return new MemoryService({
|
|
5821
6966
|
storagePath,
|
|
@@ -6206,6 +7351,7 @@ statsRouter.get("/", async (c) => {
|
|
|
6206
7351
|
acc[day] = (acc[day] || 0) + 1;
|
|
6207
7352
|
return acc;
|
|
6208
7353
|
}, {});
|
|
7354
|
+
const retrievalTrace = await memoryService.getRetrievalTraceStats();
|
|
6209
7355
|
return c.json({
|
|
6210
7356
|
storage: {
|
|
6211
7357
|
eventCount: stats.totalEvents,
|
|
@@ -6223,7 +7369,8 @@ statsRouter.get("/", async (c) => {
|
|
|
6223
7369
|
heapUsed: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
|
|
6224
7370
|
heapTotal: Math.round(process.memoryUsage().heapTotal / 1024 / 1024)
|
|
6225
7371
|
},
|
|
6226
|
-
levelStats: stats.levelStats
|
|
7372
|
+
levelStats: stats.levelStats,
|
|
7373
|
+
retrievalTrace
|
|
6227
7374
|
});
|
|
6228
7375
|
} catch (error) {
|
|
6229
7376
|
return c.json({ error: error.message }, 500);
|
|
@@ -6325,6 +7472,42 @@ statsRouter.get("/helpfulness", async (c) => {
|
|
|
6325
7472
|
await memoryService.shutdown();
|
|
6326
7473
|
}
|
|
6327
7474
|
});
|
|
7475
|
+
statsRouter.get("/retrieval-traces", async (c) => {
|
|
7476
|
+
const limit = parseInt(c.req.query("limit") || "50", 10);
|
|
7477
|
+
const memoryService = getServiceFromQuery(c);
|
|
7478
|
+
try {
|
|
7479
|
+
await memoryService.initialize();
|
|
7480
|
+
const traces = await memoryService.getRecentRetrievalTraces(limit);
|
|
7481
|
+
const traceStats = await memoryService.getRetrievalTraceStats();
|
|
7482
|
+
return c.json({
|
|
7483
|
+
stats: traceStats,
|
|
7484
|
+
traces: traces.map((t) => ({
|
|
7485
|
+
traceId: t.traceId,
|
|
7486
|
+
sessionId: t.sessionId || null,
|
|
7487
|
+
projectHash: t.projectHash || null,
|
|
7488
|
+
queryText: t.queryText,
|
|
7489
|
+
strategy: t.strategy || null,
|
|
7490
|
+
candidateEventIds: t.candidateEventIds,
|
|
7491
|
+
selectedEventIds: t.selectedEventIds,
|
|
7492
|
+
candidateDetails: t.candidateDetails || [],
|
|
7493
|
+
selectedDetails: t.selectedDetails || [],
|
|
7494
|
+
candidateCount: t.candidateCount,
|
|
7495
|
+
selectedCount: t.selectedCount,
|
|
7496
|
+
confidence: t.confidence || null,
|
|
7497
|
+
fallbackTrace: t.fallbackTrace,
|
|
7498
|
+
createdAt: t.createdAt.toISOString()
|
|
7499
|
+
}))
|
|
7500
|
+
});
|
|
7501
|
+
} catch (error) {
|
|
7502
|
+
return c.json({
|
|
7503
|
+
stats: { totalQueries: 0, avgCandidateCount: 0, avgSelectedCount: 0, selectionRate: 0 },
|
|
7504
|
+
traces: [],
|
|
7505
|
+
error: error.message
|
|
7506
|
+
}, 500);
|
|
7507
|
+
} finally {
|
|
7508
|
+
await memoryService.shutdown();
|
|
7509
|
+
}
|
|
7510
|
+
});
|
|
6328
7511
|
statsRouter.post("/graduation/run", async (c) => {
|
|
6329
7512
|
const memoryService = getServiceFromQuery(c);
|
|
6330
7513
|
try {
|
|
@@ -6572,19 +7755,19 @@ turnsRouter.post("/backfill", async (c) => {
|
|
|
6572
7755
|
|
|
6573
7756
|
// src/server/api/projects.ts
|
|
6574
7757
|
import { Hono as Hono7 } from "hono";
|
|
6575
|
-
import * as
|
|
6576
|
-
import * as
|
|
7758
|
+
import * as fs5 from "fs";
|
|
7759
|
+
import * as path5 from "path";
|
|
6577
7760
|
import * as os3 from "os";
|
|
6578
7761
|
var projectsRouter = new Hono7();
|
|
6579
7762
|
projectsRouter.get("/", async (c) => {
|
|
6580
7763
|
try {
|
|
6581
|
-
const projectsDir =
|
|
6582
|
-
if (!
|
|
7764
|
+
const projectsDir = path5.join(os3.homedir(), ".claude-code", "memory", "projects");
|
|
7765
|
+
if (!fs5.existsSync(projectsDir)) {
|
|
6583
7766
|
return c.json({ projects: [] });
|
|
6584
7767
|
}
|
|
6585
|
-
const projectHashes =
|
|
6586
|
-
const fullPath =
|
|
6587
|
-
return
|
|
7768
|
+
const projectHashes = fs5.readdirSync(projectsDir).filter((name) => {
|
|
7769
|
+
const fullPath = path5.join(projectsDir, name);
|
|
7770
|
+
return fs5.statSync(fullPath).isDirectory();
|
|
6588
7771
|
});
|
|
6589
7772
|
const registry = loadSessionRegistry();
|
|
6590
7773
|
const hashToPath = /* @__PURE__ */ new Map();
|
|
@@ -6594,17 +7777,17 @@ projectsRouter.get("/", async (c) => {
|
|
|
6594
7777
|
}
|
|
6595
7778
|
}
|
|
6596
7779
|
const projects = projectHashes.map((hash) => {
|
|
6597
|
-
const dirPath =
|
|
6598
|
-
const dbPath =
|
|
7780
|
+
const dirPath = path5.join(projectsDir, hash);
|
|
7781
|
+
const dbPath = path5.join(dirPath, "events.sqlite");
|
|
6599
7782
|
let dbSize = 0;
|
|
6600
|
-
if (
|
|
6601
|
-
dbSize =
|
|
7783
|
+
if (fs5.existsSync(dbPath)) {
|
|
7784
|
+
dbSize = fs5.statSync(dbPath).size;
|
|
6602
7785
|
}
|
|
6603
7786
|
const projectPath = hashToPath.get(hash) || `unknown (${hash})`;
|
|
6604
7787
|
return {
|
|
6605
7788
|
hash,
|
|
6606
7789
|
projectPath,
|
|
6607
|
-
projectName:
|
|
7790
|
+
projectName: path5.basename(projectPath),
|
|
6608
7791
|
dbSize,
|
|
6609
7792
|
dbSizeHuman: formatBytes(dbSize)
|
|
6610
7793
|
};
|
|
@@ -6807,23 +7990,65 @@ function streamClaudeResponse(prompt, stream) {
|
|
|
6807
7990
|
});
|
|
6808
7991
|
}
|
|
6809
7992
|
|
|
7993
|
+
// src/server/api/health.ts
|
|
7994
|
+
import { Hono as Hono9 } from "hono";
|
|
7995
|
+
var healthRouter = new Hono9();
|
|
7996
|
+
healthRouter.get("/", async (c) => {
|
|
7997
|
+
const memoryService = getServiceFromQuery(c);
|
|
7998
|
+
try {
|
|
7999
|
+
await memoryService.initialize();
|
|
8000
|
+
const [stats, outbox] = await Promise.all([
|
|
8001
|
+
memoryService.getStats(),
|
|
8002
|
+
memoryService.getOutboxStats()
|
|
8003
|
+
]);
|
|
8004
|
+
const outboxPending = outbox.embedding.pending + outbox.vector.pending;
|
|
8005
|
+
const outboxFailed = outbox.embedding.failed + outbox.vector.failed;
|
|
8006
|
+
const status = outboxFailed > 0 ? "needs-attention" : "ok";
|
|
8007
|
+
return c.json({
|
|
8008
|
+
status,
|
|
8009
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
8010
|
+
storage: {
|
|
8011
|
+
totalEvents: stats.totalEvents,
|
|
8012
|
+
vectorCount: stats.vectorCount
|
|
8013
|
+
},
|
|
8014
|
+
outbox: {
|
|
8015
|
+
embedding: outbox.embedding,
|
|
8016
|
+
vector: outbox.vector,
|
|
8017
|
+
totals: {
|
|
8018
|
+
pending: outboxPending,
|
|
8019
|
+
failed: outboxFailed
|
|
8020
|
+
}
|
|
8021
|
+
},
|
|
8022
|
+
levelStats: stats.levelStats
|
|
8023
|
+
});
|
|
8024
|
+
} catch (error) {
|
|
8025
|
+
return c.json({
|
|
8026
|
+
status: "error",
|
|
8027
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
8028
|
+
error: error.message
|
|
8029
|
+
}, 500);
|
|
8030
|
+
} finally {
|
|
8031
|
+
await memoryService.shutdown();
|
|
8032
|
+
}
|
|
8033
|
+
});
|
|
8034
|
+
|
|
6810
8035
|
// src/server/api/index.ts
|
|
6811
|
-
var apiRouter = new
|
|
8036
|
+
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);
|
|
6812
8037
|
|
|
6813
8038
|
// src/server/index.ts
|
|
6814
|
-
var app = new
|
|
8039
|
+
var app = new Hono11();
|
|
6815
8040
|
app.use("/*", cors());
|
|
6816
8041
|
app.use("/*", logger());
|
|
6817
8042
|
app.route("/api", apiRouter);
|
|
6818
8043
|
app.get("/health", (c) => c.json({ status: "ok", timestamp: (/* @__PURE__ */ new Date()).toISOString() }));
|
|
6819
|
-
var uiPath =
|
|
6820
|
-
if (
|
|
8044
|
+
var uiPath = path6.join(__dirname, "../../dist/ui");
|
|
8045
|
+
if (fs6.existsSync(uiPath)) {
|
|
6821
8046
|
app.use("/*", serveStatic({ root: uiPath }));
|
|
6822
8047
|
}
|
|
6823
8048
|
app.get("*", (c) => {
|
|
6824
|
-
const indexPath =
|
|
6825
|
-
if (
|
|
6826
|
-
return c.html(
|
|
8049
|
+
const indexPath = path6.join(uiPath, "index.html");
|
|
8050
|
+
if (fs6.existsSync(indexPath)) {
|
|
8051
|
+
return c.html(fs6.readFileSync(indexPath, "utf-8"));
|
|
6827
8052
|
}
|
|
6828
8053
|
return c.text('UI not built. Run "npm run build:ui" first.', 404);
|
|
6829
8054
|
});
|