claude-memory-layer 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +60 -0
- package/README.md +166 -2
- package/bootstrap-kb/decisions/decisions.md +244 -0
- package/bootstrap-kb/glossary/glossary.md +46 -0
- package/bootstrap-kb/modules/.claude-plugin.md +22 -0
- package/bootstrap-kb/modules/agents.md.md +15 -0
- package/bootstrap-kb/modules/claude.md.md +15 -0
- package/bootstrap-kb/modules/context.md.md +15 -0
- package/bootstrap-kb/modules/docs.md +18 -0
- package/bootstrap-kb/modules/handoff.md.md +15 -0
- package/bootstrap-kb/modules/package-lock.json.md +15 -0
- package/bootstrap-kb/modules/package.json.md +15 -0
- package/bootstrap-kb/modules/plan.md.md +15 -0
- package/bootstrap-kb/modules/readme.md.md +15 -0
- package/bootstrap-kb/modules/scripts.md +26 -0
- package/bootstrap-kb/modules/spec.md.md +15 -0
- package/bootstrap-kb/modules/specs.md +20 -0
- package/bootstrap-kb/modules/src.md +51 -0
- package/bootstrap-kb/modules/tests.md +42 -0
- package/bootstrap-kb/modules/tsconfig.json.md +15 -0
- package/bootstrap-kb/modules/vitest.config.ts.md +15 -0
- package/bootstrap-kb/overview/overview.md +40 -0
- package/bootstrap-kb/sources/manifest.json +950 -0
- package/bootstrap-kb/sources/manifest.md +227 -0
- package/bootstrap-kb/timeline/timeline.md +57 -0
- package/d.sh +3 -0
- package/deploy.sh +3 -0
- package/dist/cli/index.js +3577 -389
- package/dist/cli/index.js.map +4 -4
- package/dist/core/index.js +1383 -138
- package/dist/core/index.js.map +4 -4
- package/dist/hooks/post-tool-use.js +1917 -214
- package/dist/hooks/post-tool-use.js.map +4 -4
- package/dist/hooks/session-end.js +1813 -231
- package/dist/hooks/session-end.js.map +4 -4
- package/dist/hooks/session-start.js +1802 -205
- package/dist/hooks/session-start.js.map +4 -4
- package/dist/hooks/stop.js +1909 -248
- package/dist/hooks/stop.js.map +4 -4
- package/dist/hooks/user-prompt-submit.js +1861 -206
- package/dist/hooks/user-prompt-submit.js.map +4 -4
- package/dist/server/api/index.js +2341 -217
- package/dist/server/api/index.js.map +4 -4
- package/dist/server/index.js +2350 -226
- package/dist/server/index.js.map +4 -4
- package/dist/services/memory-service.js +1805 -206
- package/dist/services/memory-service.js.map +4 -4
- package/dist/ui/app.js +1447 -55
- package/dist/ui/index.html +318 -147
- package/dist/ui/style.css +892 -0
- package/docs/MCP_MEMORY_SERVICE_COMPARATIVE_REVIEW.md +271 -0
- package/docs/MEMU_ADOPTION.md +40 -0
- package/docs/OPERATIONS.md +18 -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 +9 -2
- package/scripts/build.ts +6 -0
- package/scripts/fix-sync-gap.js +32 -0
- package/scripts/heartbeat-memory-orchestrator.sh +28 -0
- package/scripts/report-sync-gap.js +26 -0
- package/scripts/review-queue-auto-resolve.js +21 -0
- package/scripts/sync-gap-auto-heal.sh +17 -0
- package/specs/20260207-dashboard-upgrade/context.md +38 -0
- package/specs/20260207-dashboard-upgrade/spec.md +96 -0
- package/src/cli/index.ts +391 -60
- 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 +794 -7
- package/src/core/sqlite-wrapper.ts +8 -0
- package/src/core/tag-taxonomy.ts +51 -0
- package/src/core/turn-state.ts +159 -0
- package/src/core/types.ts +51 -8
- package/src/core/vector-store.ts +21 -3
- package/src/hooks/post-tool-use.ts +68 -23
- package/src/hooks/session-end.ts +8 -3
- package/src/hooks/stop.ts +96 -25
- package/src/hooks/user-prompt-submit.ts +44 -5
- package/src/server/api/chat.ts +244 -0
- package/src/server/api/citations.ts +3 -3
- package/src/server/api/events.ts +30 -5
- package/src/server/api/health.ts +53 -0
- package/src/server/api/index.ts +9 -1
- package/src/server/api/projects.ts +74 -0
- package/src/server/api/search.ts +3 -3
- package/src/server/api/sessions.ts +3 -3
- package/src/server/api/stats.ts +89 -8
- package/src/server/api/turns.ts +143 -0
- package/src/server/api/utils.ts +46 -0
- package/src/services/bootstrap-organizer.ts +443 -0
- package/src/services/codex-session-history-importer.ts +474 -0
- package/src/services/memory-service.ts +508 -71
- package/src/services/session-history-importer.ts +215 -51
- package/src/ui/app.js +1447 -55
- package/src/ui/index.html +318 -147
- package/src/ui/style.css +892 -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/.claude/settings.local.json +0 -27
- package/.claude-memory/test.sqlite +0 -0
- package/.history/package_20260201112328.json +0 -45
- package/.history/package_20260201113602.json +0 -45
- package/.history/package_20260201113713.json +0 -45
- package/.history/package_20260201114110.json +0 -45
- package/.history/package_20260201114632.json +0 -46
- package/.history/package_20260201133143.json +0 -45
- package/.history/package_20260201134319.json +0 -45
- package/.history/package_20260201134326.json +0 -45
- package/.history/package_20260201134334.json +0 -45
- package/.history/package_20260201134912.json +0 -45
- package/.history/package_20260201142928.json +0 -46
- package/.history/package_20260201192048.json +0 -47
- package/.history/package_20260202114053.json +0 -49
- package/.history/package_20260202121115.json +0 -49
- package/test_access.js +0 -49
package/dist/core/index.js
CHANGED
|
@@ -487,6 +487,15 @@ var ConsolidatedMemorySchema = z.object({
|
|
|
487
487
|
accessedAt: z.date().optional(),
|
|
488
488
|
accessCount: z.number().default(0)
|
|
489
489
|
});
|
|
490
|
+
var ConsolidationRuleSchema = z.object({
|
|
491
|
+
ruleId: z.string(),
|
|
492
|
+
rule: z.string(),
|
|
493
|
+
topics: z.array(z.string()),
|
|
494
|
+
sourceMemoryIds: z.array(z.string()),
|
|
495
|
+
sourceEvents: z.array(z.string()),
|
|
496
|
+
confidence: z.number(),
|
|
497
|
+
createdAt: z.date()
|
|
498
|
+
});
|
|
490
499
|
var TransitionTypeSchema = z.enum(["seamless", "topic_shift", "break"]);
|
|
491
500
|
var ContinuityLogSchema = z.object({
|
|
492
501
|
logId: z.string(),
|
|
@@ -639,11 +648,11 @@ function toDate(value) {
|
|
|
639
648
|
return new Date(value);
|
|
640
649
|
return new Date(String(value));
|
|
641
650
|
}
|
|
642
|
-
function createDatabase(
|
|
651
|
+
function createDatabase(path2, options) {
|
|
643
652
|
if (options?.readOnly) {
|
|
644
|
-
return new duckdb.Database(
|
|
653
|
+
return new duckdb.Database(path2, { access_mode: "READ_ONLY" });
|
|
645
654
|
}
|
|
646
|
-
return new duckdb.Database(
|
|
655
|
+
return new duckdb.Database(path2);
|
|
647
656
|
}
|
|
648
657
|
function dbRun(db, sql, params = []) {
|
|
649
658
|
return new Promise((resolve, reject) => {
|
|
@@ -907,6 +916,17 @@ var EventStore = class {
|
|
|
907
916
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
908
917
|
)
|
|
909
918
|
`);
|
|
919
|
+
await dbRun(this.db, `
|
|
920
|
+
CREATE TABLE IF NOT EXISTS consolidated_rules (
|
|
921
|
+
rule_id VARCHAR PRIMARY KEY,
|
|
922
|
+
rule TEXT NOT NULL,
|
|
923
|
+
topics JSON,
|
|
924
|
+
source_memory_ids JSON,
|
|
925
|
+
source_events JSON,
|
|
926
|
+
confidence FLOAT DEFAULT 0.5,
|
|
927
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
928
|
+
)
|
|
929
|
+
`);
|
|
910
930
|
await dbRun(this.db, `
|
|
911
931
|
CREATE TABLE IF NOT EXISTS endless_config (
|
|
912
932
|
key VARCHAR PRIMARY KEY,
|
|
@@ -926,6 +946,7 @@ var EventStore = class {
|
|
|
926
946
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
|
|
927
947
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
|
|
928
948
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
|
|
949
|
+
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
|
|
929
950
|
await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
|
|
930
951
|
this.initialized = true;
|
|
931
952
|
}
|
|
@@ -1310,8 +1331,14 @@ var EventStore = class {
|
|
|
1310
1331
|
|
|
1311
1332
|
// src/core/sqlite-wrapper.ts
|
|
1312
1333
|
import Database from "better-sqlite3";
|
|
1313
|
-
|
|
1314
|
-
|
|
1334
|
+
import * as fs from "fs";
|
|
1335
|
+
import * as nodePath from "path";
|
|
1336
|
+
function createSQLiteDatabase(path2, options) {
|
|
1337
|
+
const dir = nodePath.dirname(path2);
|
|
1338
|
+
if (!fs.existsSync(dir)) {
|
|
1339
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
1340
|
+
}
|
|
1341
|
+
const db = new Database(path2, {
|
|
1315
1342
|
readonly: options?.readonly ?? false
|
|
1316
1343
|
});
|
|
1317
1344
|
if (!options?.readonly && (options?.walMode ?? true)) {
|
|
@@ -1357,6 +1384,66 @@ function toSQLiteTimestamp(date) {
|
|
|
1357
1384
|
|
|
1358
1385
|
// src/core/sqlite-event-store.ts
|
|
1359
1386
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
1387
|
+
|
|
1388
|
+
// src/core/markdown-mirror.ts
|
|
1389
|
+
import * as fs2 from "fs/promises";
|
|
1390
|
+
import * as path from "path";
|
|
1391
|
+
var DEFAULT_NAMESPACE = "default";
|
|
1392
|
+
var DEFAULT_CATEGORY = "uncategorized";
|
|
1393
|
+
function sanitizeSegment(input, fallback) {
|
|
1394
|
+
const raw = String(input ?? "").trim().toLowerCase();
|
|
1395
|
+
const safe = raw.normalize("NFKD").replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
1396
|
+
if (!safe || safe === "." || safe === "..")
|
|
1397
|
+
return fallback;
|
|
1398
|
+
return safe;
|
|
1399
|
+
}
|
|
1400
|
+
function getCategorySegments(metadata, eventType) {
|
|
1401
|
+
const raw = metadata?.categoryPath;
|
|
1402
|
+
if (Array.isArray(raw) && raw.length > 0) {
|
|
1403
|
+
return raw.map((s) => sanitizeSegment(s, DEFAULT_CATEGORY));
|
|
1404
|
+
}
|
|
1405
|
+
const single = metadata?.category;
|
|
1406
|
+
if (typeof single === "string" && single.trim()) {
|
|
1407
|
+
return [sanitizeSegment(single, DEFAULT_CATEGORY)];
|
|
1408
|
+
}
|
|
1409
|
+
return [sanitizeSegment(eventType, DEFAULT_CATEGORY)];
|
|
1410
|
+
}
|
|
1411
|
+
function buildMirrorPath(rootDir, event) {
|
|
1412
|
+
const metadata = event.metadata;
|
|
1413
|
+
const namespace = sanitizeSegment(metadata?.namespace, DEFAULT_NAMESPACE);
|
|
1414
|
+
const categories = getCategorySegments(metadata, event.eventType);
|
|
1415
|
+
const d = event.timestamp;
|
|
1416
|
+
const yyyy = d.getFullYear();
|
|
1417
|
+
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
1418
|
+
const dd = String(d.getDate()).padStart(2, "0");
|
|
1419
|
+
return path.join(rootDir, "memory", namespace, ...categories, `${yyyy}-${mm}-${dd}.md`);
|
|
1420
|
+
}
|
|
1421
|
+
function formatMirrorEntry(event) {
|
|
1422
|
+
const category = Array.isArray(event.metadata?.categoryPath) ? event.metadata.categoryPath.join("/") : String(event.metadata?.category ?? event.eventType);
|
|
1423
|
+
return [
|
|
1424
|
+
"",
|
|
1425
|
+
`- ts: ${event.timestamp.toISOString()}`,
|
|
1426
|
+
` id: ${event.id}`,
|
|
1427
|
+
` type: ${event.eventType}`,
|
|
1428
|
+
` session: ${event.sessionId}`,
|
|
1429
|
+
` category: ${category}`,
|
|
1430
|
+
" content: |",
|
|
1431
|
+
...event.content.split("\n").map((line) => ` ${line}`)
|
|
1432
|
+
].join("\n") + "\n";
|
|
1433
|
+
}
|
|
1434
|
+
var MarkdownMirror = class {
|
|
1435
|
+
constructor(rootDir) {
|
|
1436
|
+
this.rootDir = rootDir;
|
|
1437
|
+
}
|
|
1438
|
+
async append(event) {
|
|
1439
|
+
const outPath = buildMirrorPath(this.rootDir, event);
|
|
1440
|
+
await fs2.mkdir(path.dirname(outPath), { recursive: true });
|
|
1441
|
+
await fs2.appendFile(outPath, formatMirrorEntry(event), "utf8");
|
|
1442
|
+
return outPath;
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
|
|
1446
|
+
// src/core/sqlite-event-store.ts
|
|
1360
1447
|
var SQLiteEventStore = class {
|
|
1361
1448
|
constructor(dbPath, options) {
|
|
1362
1449
|
this.dbPath = dbPath;
|
|
@@ -1365,10 +1452,12 @@ var SQLiteEventStore = class {
|
|
|
1365
1452
|
readonly: this.readOnly,
|
|
1366
1453
|
walMode: !this.readOnly
|
|
1367
1454
|
});
|
|
1455
|
+
this.markdownMirror = this.readOnly || !options?.markdownMirrorRoot ? null : new MarkdownMirror(options.markdownMirrorRoot);
|
|
1368
1456
|
}
|
|
1369
1457
|
db;
|
|
1370
1458
|
initialized = false;
|
|
1371
1459
|
readOnly;
|
|
1460
|
+
markdownMirror;
|
|
1372
1461
|
/**
|
|
1373
1462
|
* Initialize database schema
|
|
1374
1463
|
*/
|
|
@@ -1575,6 +1664,17 @@ var SQLiteEventStore = class {
|
|
|
1575
1664
|
created_at TEXT DEFAULT (datetime('now'))
|
|
1576
1665
|
);
|
|
1577
1666
|
|
|
1667
|
+
-- Consolidated Rules table (long-term stable memory)
|
|
1668
|
+
CREATE TABLE IF NOT EXISTS consolidated_rules (
|
|
1669
|
+
rule_id TEXT PRIMARY KEY,
|
|
1670
|
+
rule TEXT NOT NULL,
|
|
1671
|
+
topics TEXT,
|
|
1672
|
+
source_memory_ids TEXT,
|
|
1673
|
+
source_events TEXT,
|
|
1674
|
+
confidence REAL DEFAULT 0.5,
|
|
1675
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
1676
|
+
);
|
|
1677
|
+
|
|
1578
1678
|
-- Endless Mode Config table
|
|
1579
1679
|
CREATE TABLE IF NOT EXISTS endless_config (
|
|
1580
1680
|
key TEXT PRIMARY KEY,
|
|
@@ -1582,6 +1682,41 @@ var SQLiteEventStore = class {
|
|
|
1582
1682
|
updated_at TEXT DEFAULT (datetime('now'))
|
|
1583
1683
|
);
|
|
1584
1684
|
|
|
1685
|
+
-- Memory Helpfulness tracking
|
|
1686
|
+
CREATE TABLE IF NOT EXISTS memory_helpfulness (
|
|
1687
|
+
id TEXT PRIMARY KEY,
|
|
1688
|
+
event_id TEXT NOT NULL,
|
|
1689
|
+
session_id TEXT NOT NULL,
|
|
1690
|
+
retrieval_score REAL DEFAULT 0,
|
|
1691
|
+
query_preview TEXT,
|
|
1692
|
+
session_continued INTEGER DEFAULT 0,
|
|
1693
|
+
prompt_count_after INTEGER DEFAULT 0,
|
|
1694
|
+
tool_success_count INTEGER DEFAULT 0,
|
|
1695
|
+
tool_total_count INTEGER DEFAULT 0,
|
|
1696
|
+
was_reasked INTEGER DEFAULT 0,
|
|
1697
|
+
helpfulness_score REAL DEFAULT 0.5,
|
|
1698
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
1699
|
+
measured_at TEXT
|
|
1700
|
+
);
|
|
1701
|
+
|
|
1702
|
+
-- Retrieval trace log (query -> candidates -> selected for context)
|
|
1703
|
+
CREATE TABLE IF NOT EXISTS retrieval_traces (
|
|
1704
|
+
trace_id TEXT PRIMARY KEY,
|
|
1705
|
+
session_id TEXT,
|
|
1706
|
+
project_hash TEXT,
|
|
1707
|
+
query_text TEXT NOT NULL,
|
|
1708
|
+
strategy TEXT,
|
|
1709
|
+
candidate_event_ids TEXT,
|
|
1710
|
+
selected_event_ids TEXT,
|
|
1711
|
+
candidate_details_json TEXT,
|
|
1712
|
+
selected_details_json TEXT,
|
|
1713
|
+
candidate_count INTEGER DEFAULT 0,
|
|
1714
|
+
selected_count INTEGER DEFAULT 0,
|
|
1715
|
+
confidence TEXT,
|
|
1716
|
+
fallback_trace TEXT,
|
|
1717
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
1718
|
+
);
|
|
1719
|
+
|
|
1585
1720
|
-- Sync position tracking (for SQLite -> DuckDB sync)
|
|
1586
1721
|
CREATE TABLE IF NOT EXISTS sync_positions (
|
|
1587
1722
|
target_name TEXT PRIMARY KEY,
|
|
@@ -1606,7 +1741,14 @@ var SQLiteEventStore = class {
|
|
|
1606
1741
|
CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score);
|
|
1607
1742
|
CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
|
|
1608
1743
|
CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
|
|
1744
|
+
CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence);
|
|
1609
1745
|
CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
|
|
1746
|
+
CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
|
|
1747
|
+
CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
|
|
1748
|
+
CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
|
|
1749
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_created_at ON retrieval_traces(created_at DESC);
|
|
1750
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_project_hash ON retrieval_traces(project_hash);
|
|
1751
|
+
CREATE INDEX IF NOT EXISTS idx_retrieval_traces_session_id ON retrieval_traces(session_id);
|
|
1610
1752
|
|
|
1611
1753
|
-- FTS5 Full-Text Search for fast keyword search
|
|
1612
1754
|
CREATE VIRTUAL TABLE IF NOT EXISTS events_fts USING fts5(
|
|
@@ -1630,6 +1772,14 @@ var SQLiteEventStore = class {
|
|
|
1630
1772
|
INSERT INTO events_fts(rowid, content, event_id) VALUES (NEW.rowid, NEW.content, NEW.id);
|
|
1631
1773
|
END;
|
|
1632
1774
|
`);
|
|
1775
|
+
try {
|
|
1776
|
+
sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN selected_details_json TEXT;`);
|
|
1777
|
+
} catch {
|
|
1778
|
+
}
|
|
1779
|
+
try {
|
|
1780
|
+
sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN candidate_details_json TEXT;`);
|
|
1781
|
+
} catch {
|
|
1782
|
+
}
|
|
1633
1783
|
const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
|
|
1634
1784
|
const columnNames = tableInfo.map((col) => col.name);
|
|
1635
1785
|
if (!columnNames.includes("access_count")) {
|
|
@@ -1650,6 +1800,15 @@ var SQLiteEventStore = class {
|
|
|
1650
1800
|
console.error("Error adding last_accessed_at column:", err);
|
|
1651
1801
|
}
|
|
1652
1802
|
}
|
|
1803
|
+
if (!columnNames.includes("turn_id")) {
|
|
1804
|
+
try {
|
|
1805
|
+
sqliteExec(this.db, `
|
|
1806
|
+
ALTER TABLE events ADD COLUMN turn_id TEXT;
|
|
1807
|
+
`);
|
|
1808
|
+
} catch (err) {
|
|
1809
|
+
console.error("Error adding turn_id column:", err);
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1653
1812
|
try {
|
|
1654
1813
|
sqliteExec(this.db, `
|
|
1655
1814
|
CREATE INDEX IF NOT EXISTS idx_events_access_count ON events(access_count DESC);
|
|
@@ -1662,6 +1821,12 @@ var SQLiteEventStore = class {
|
|
|
1662
1821
|
`);
|
|
1663
1822
|
} catch (err) {
|
|
1664
1823
|
}
|
|
1824
|
+
try {
|
|
1825
|
+
sqliteExec(this.db, `
|
|
1826
|
+
CREATE INDEX IF NOT EXISTS idx_events_turn_id ON events(turn_id);
|
|
1827
|
+
`);
|
|
1828
|
+
} catch (err) {
|
|
1829
|
+
}
|
|
1665
1830
|
this.initialized = true;
|
|
1666
1831
|
}
|
|
1667
1832
|
/**
|
|
@@ -1686,9 +1851,11 @@ var SQLiteEventStore = class {
|
|
|
1686
1851
|
const id = randomUUID2();
|
|
1687
1852
|
const timestamp = toSQLiteTimestamp(input.timestamp);
|
|
1688
1853
|
try {
|
|
1854
|
+
const metadata = input.metadata || {};
|
|
1855
|
+
const turnId = metadata.turnId || null;
|
|
1689
1856
|
const insertEvent = this.db.prepare(`
|
|
1690
|
-
INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
|
|
1691
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
1857
|
+
INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
|
|
1858
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1692
1859
|
`);
|
|
1693
1860
|
const insertDedup = this.db.prepare(`
|
|
1694
1861
|
INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
|
|
@@ -1705,12 +1872,28 @@ var SQLiteEventStore = class {
|
|
|
1705
1872
|
input.content,
|
|
1706
1873
|
canonicalKey,
|
|
1707
1874
|
dedupeKey,
|
|
1708
|
-
JSON.stringify(
|
|
1875
|
+
JSON.stringify(metadata),
|
|
1876
|
+
turnId
|
|
1709
1877
|
);
|
|
1710
1878
|
insertDedup.run(dedupeKey, id);
|
|
1711
1879
|
insertLevel.run(id);
|
|
1712
1880
|
});
|
|
1713
1881
|
transaction();
|
|
1882
|
+
if (this.markdownMirror) {
|
|
1883
|
+
const event = {
|
|
1884
|
+
id,
|
|
1885
|
+
eventType: input.eventType,
|
|
1886
|
+
sessionId: input.sessionId,
|
|
1887
|
+
timestamp: input.timestamp,
|
|
1888
|
+
content: input.content,
|
|
1889
|
+
canonicalKey,
|
|
1890
|
+
dedupeKey,
|
|
1891
|
+
metadata
|
|
1892
|
+
};
|
|
1893
|
+
this.markdownMirror.append(event).catch((err) => {
|
|
1894
|
+
console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
|
|
1895
|
+
});
|
|
1896
|
+
}
|
|
1714
1897
|
return { success: true, eventId: id, isDuplicate: false };
|
|
1715
1898
|
} catch (error) {
|
|
1716
1899
|
return {
|
|
@@ -1769,6 +1952,92 @@ var SQLiteEventStore = class {
|
|
|
1769
1952
|
);
|
|
1770
1953
|
return rows.map(this.rowToEvent);
|
|
1771
1954
|
}
|
|
1955
|
+
/**
|
|
1956
|
+
* Get events since a SQLite rowid (for robust incremental replication).
|
|
1957
|
+
* Rowid is monotonic for append-only tables, independent of client timestamps.
|
|
1958
|
+
*/
|
|
1959
|
+
async getEventsSinceRowid(lastRowid, limit = 1e3) {
|
|
1960
|
+
await this.initialize();
|
|
1961
|
+
const rows = sqliteAll(
|
|
1962
|
+
this.db,
|
|
1963
|
+
`SELECT rowid as _rowid, * FROM events WHERE rowid > ? ORDER BY rowid ASC LIMIT ?`,
|
|
1964
|
+
[lastRowid, limit]
|
|
1965
|
+
);
|
|
1966
|
+
return rows.map((row) => ({
|
|
1967
|
+
rowid: row._rowid,
|
|
1968
|
+
event: this.rowToEvent(row)
|
|
1969
|
+
}));
|
|
1970
|
+
}
|
|
1971
|
+
/**
|
|
1972
|
+
* Import events with fixed IDs (used for cross-machine replication).
|
|
1973
|
+
* Idempotent: skips if event id or dedupeKey already exists.
|
|
1974
|
+
*
|
|
1975
|
+
* NOTE: This bypasses the append() id generation to preserve stable IDs.
|
|
1976
|
+
*/
|
|
1977
|
+
async importEvents(events) {
|
|
1978
|
+
if (events.length === 0)
|
|
1979
|
+
return { inserted: 0, skipped: 0 };
|
|
1980
|
+
if (this.readOnly)
|
|
1981
|
+
return { inserted: 0, skipped: events.length };
|
|
1982
|
+
await this.initialize();
|
|
1983
|
+
const getById = this.db.prepare(`SELECT id FROM events WHERE id = ?`);
|
|
1984
|
+
const getByDedupe = this.db.prepare(`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`);
|
|
1985
|
+
const insertEvent = this.db.prepare(`
|
|
1986
|
+
INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
|
|
1987
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1988
|
+
`);
|
|
1989
|
+
const insertDedup = this.db.prepare(`
|
|
1990
|
+
INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
|
|
1991
|
+
`);
|
|
1992
|
+
const insertLevel = this.db.prepare(`
|
|
1993
|
+
INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')
|
|
1994
|
+
`);
|
|
1995
|
+
let inserted = 0;
|
|
1996
|
+
let skipped = 0;
|
|
1997
|
+
const insertedEvents = [];
|
|
1998
|
+
const tx = this.db.transaction((batch) => {
|
|
1999
|
+
for (const ev of batch) {
|
|
2000
|
+
const existingById = getById.get(ev.id);
|
|
2001
|
+
if (existingById) {
|
|
2002
|
+
skipped++;
|
|
2003
|
+
continue;
|
|
2004
|
+
}
|
|
2005
|
+
const canonicalKey = ev.canonicalKey || makeCanonicalKey(ev.content);
|
|
2006
|
+
const dedupeKey = ev.dedupeKey || makeDedupeKey(ev.content, ev.sessionId);
|
|
2007
|
+
const existingByDedupe = getByDedupe.get(dedupeKey);
|
|
2008
|
+
if (existingByDedupe) {
|
|
2009
|
+
skipped++;
|
|
2010
|
+
continue;
|
|
2011
|
+
}
|
|
2012
|
+
const metadata = ev.metadata || {};
|
|
2013
|
+
const turnId = metadata.turnId;
|
|
2014
|
+
insertEvent.run(
|
|
2015
|
+
ev.id,
|
|
2016
|
+
ev.eventType,
|
|
2017
|
+
ev.sessionId,
|
|
2018
|
+
toSQLiteTimestamp(ev.timestamp),
|
|
2019
|
+
ev.content,
|
|
2020
|
+
canonicalKey,
|
|
2021
|
+
dedupeKey,
|
|
2022
|
+
JSON.stringify(metadata),
|
|
2023
|
+
turnId ?? null
|
|
2024
|
+
);
|
|
2025
|
+
insertDedup.run(dedupeKey, ev.id);
|
|
2026
|
+
insertLevel.run(ev.id);
|
|
2027
|
+
inserted++;
|
|
2028
|
+
insertedEvents.push(ev);
|
|
2029
|
+
}
|
|
2030
|
+
});
|
|
2031
|
+
tx(events);
|
|
2032
|
+
if (this.markdownMirror && insertedEvents.length > 0) {
|
|
2033
|
+
for (const ev of insertedEvents) {
|
|
2034
|
+
this.markdownMirror.append(ev).catch((err) => {
|
|
2035
|
+
console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
|
|
2036
|
+
});
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
return { inserted, skipped };
|
|
2040
|
+
}
|
|
1772
2041
|
/**
|
|
1773
2042
|
* Create or update session
|
|
1774
2043
|
*/
|
|
@@ -1931,6 +2200,35 @@ var SQLiteEventStore = class {
|
|
|
1931
2200
|
[error, ...ids]
|
|
1932
2201
|
);
|
|
1933
2202
|
}
|
|
2203
|
+
/**
|
|
2204
|
+
* Get embedding/vector outbox health statistics
|
|
2205
|
+
*/
|
|
2206
|
+
async getOutboxStats() {
|
|
2207
|
+
await this.initialize();
|
|
2208
|
+
const embeddingRows = sqliteAll(
|
|
2209
|
+
this.db,
|
|
2210
|
+
`SELECT status, COUNT(*) as count FROM embedding_outbox GROUP BY status`
|
|
2211
|
+
);
|
|
2212
|
+
const vectorRows = sqliteAll(
|
|
2213
|
+
this.db,
|
|
2214
|
+
`SELECT status, COUNT(*) as count FROM vector_outbox GROUP BY status`
|
|
2215
|
+
);
|
|
2216
|
+
const fromRows = (rows) => {
|
|
2217
|
+
const out = { pending: 0, processing: 0, failed: 0, total: 0 };
|
|
2218
|
+
for (const row of rows) {
|
|
2219
|
+
const key = row.status;
|
|
2220
|
+
if (key === "pending" || key === "processing" || key === "failed") {
|
|
2221
|
+
out[key] += row.count;
|
|
2222
|
+
}
|
|
2223
|
+
out.total += row.count;
|
|
2224
|
+
}
|
|
2225
|
+
return out;
|
|
2226
|
+
};
|
|
2227
|
+
return {
|
|
2228
|
+
embedding: fromRows(embeddingRows),
|
|
2229
|
+
vector: fromRows(vectorRows)
|
|
2230
|
+
};
|
|
2231
|
+
}
|
|
1934
2232
|
/**
|
|
1935
2233
|
* Update memory level
|
|
1936
2234
|
*/
|
|
@@ -2055,11 +2353,11 @@ var SQLiteEventStore = class {
|
|
|
2055
2353
|
);
|
|
2056
2354
|
}
|
|
2057
2355
|
/**
|
|
2058
|
-
* Get most accessed memories
|
|
2356
|
+
* Get most accessed memories (falls back to recent events if none accessed)
|
|
2059
2357
|
*/
|
|
2060
2358
|
async getMostAccessed(limit = 10) {
|
|
2061
2359
|
await this.initialize();
|
|
2062
|
-
|
|
2360
|
+
let rows = sqliteAll(
|
|
2063
2361
|
this.db,
|
|
2064
2362
|
`SELECT * FROM events
|
|
2065
2363
|
WHERE access_count > 0
|
|
@@ -2067,8 +2365,166 @@ var SQLiteEventStore = class {
|
|
|
2067
2365
|
LIMIT ?`,
|
|
2068
2366
|
[limit]
|
|
2069
2367
|
);
|
|
2368
|
+
if (rows.length === 0) {
|
|
2369
|
+
rows = sqliteAll(
|
|
2370
|
+
this.db,
|
|
2371
|
+
`SELECT * FROM events
|
|
2372
|
+
ORDER BY timestamp DESC
|
|
2373
|
+
LIMIT ?`,
|
|
2374
|
+
[limit]
|
|
2375
|
+
);
|
|
2376
|
+
}
|
|
2070
2377
|
return rows.map((row) => this.rowToEvent(row));
|
|
2071
2378
|
}
|
|
2379
|
+
/**
|
|
2380
|
+
* Record a memory retrieval for helpfulness tracking
|
|
2381
|
+
*/
|
|
2382
|
+
async recordRetrieval(eventId, sessionId, score, query) {
|
|
2383
|
+
if (this.readOnly)
|
|
2384
|
+
return;
|
|
2385
|
+
await this.initialize();
|
|
2386
|
+
const id = randomUUID2();
|
|
2387
|
+
sqliteRun(
|
|
2388
|
+
this.db,
|
|
2389
|
+
`INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
|
|
2390
|
+
VALUES (?, ?, ?, ?, ?, datetime('now'))`,
|
|
2391
|
+
[id, eventId, sessionId, score, query.slice(0, 100)]
|
|
2392
|
+
);
|
|
2393
|
+
}
|
|
2394
|
+
/**
|
|
2395
|
+
* Evaluate helpfulness for all retrievals in a session
|
|
2396
|
+
* Called at session end - uses behavioral signals to compute score
|
|
2397
|
+
*/
|
|
2398
|
+
async evaluateSessionHelpfulness(sessionId) {
|
|
2399
|
+
if (this.readOnly)
|
|
2400
|
+
return;
|
|
2401
|
+
await this.initialize();
|
|
2402
|
+
const retrievals = sqliteAll(
|
|
2403
|
+
this.db,
|
|
2404
|
+
`SELECT * FROM memory_helpfulness WHERE session_id = ? AND measured_at IS NULL`,
|
|
2405
|
+
[sessionId]
|
|
2406
|
+
);
|
|
2407
|
+
if (retrievals.length === 0)
|
|
2408
|
+
return;
|
|
2409
|
+
const sessionEvents = sqliteAll(
|
|
2410
|
+
this.db,
|
|
2411
|
+
`SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
|
|
2412
|
+
[sessionId]
|
|
2413
|
+
);
|
|
2414
|
+
const promptEvents = sessionEvents.filter((e) => e.event_type === "user_prompt");
|
|
2415
|
+
const toolEvents = sessionEvents.filter((e) => e.event_type === "tool_observation");
|
|
2416
|
+
let toolSuccessCount = 0;
|
|
2417
|
+
let toolTotalCount = toolEvents.length;
|
|
2418
|
+
for (const t of toolEvents) {
|
|
2419
|
+
try {
|
|
2420
|
+
const content = JSON.parse(t.content);
|
|
2421
|
+
if (content.success !== false)
|
|
2422
|
+
toolSuccessCount++;
|
|
2423
|
+
} catch {
|
|
2424
|
+
toolSuccessCount++;
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
const toolSuccessRatio = toolTotalCount > 0 ? toolSuccessCount / toolTotalCount : 0.5;
|
|
2428
|
+
for (const retrieval of retrievals) {
|
|
2429
|
+
const retrievalTime = retrieval.created_at;
|
|
2430
|
+
const eventsAfter = sessionEvents.filter((e) => e.timestamp > retrievalTime);
|
|
2431
|
+
const sessionContinued = eventsAfter.length > 0 ? 1 : 0;
|
|
2432
|
+
const promptsAfter = promptEvents.filter((e) => e.timestamp > retrievalTime);
|
|
2433
|
+
const promptCountAfter = promptsAfter.length;
|
|
2434
|
+
const queryWords = new Set((retrieval.query_preview || "").toLowerCase().split(/\s+/).filter((w) => w.length > 2));
|
|
2435
|
+
let wasReasked = 0;
|
|
2436
|
+
for (const p of promptsAfter) {
|
|
2437
|
+
const pWords = new Set(p.content.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
|
|
2438
|
+
let overlap = 0;
|
|
2439
|
+
for (const w of queryWords) {
|
|
2440
|
+
if (pWords.has(w))
|
|
2441
|
+
overlap++;
|
|
2442
|
+
}
|
|
2443
|
+
if (queryWords.size > 0 && overlap / queryWords.size > 0.5) {
|
|
2444
|
+
wasReasked = 1;
|
|
2445
|
+
break;
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
const retrievalScore = retrieval.retrieval_score || 0;
|
|
2449
|
+
const helpfulnessScore = 0.3 * Math.min(retrievalScore, 1) + 0.25 * (sessionContinued ? 1 : 0) + 0.25 * toolSuccessRatio + 0.2 * (wasReasked ? 0 : 1);
|
|
2450
|
+
sqliteRun(
|
|
2451
|
+
this.db,
|
|
2452
|
+
`UPDATE memory_helpfulness
|
|
2453
|
+
SET session_continued = ?, prompt_count_after = ?,
|
|
2454
|
+
tool_success_count = ?, tool_total_count = ?,
|
|
2455
|
+
was_reasked = ?, helpfulness_score = ?,
|
|
2456
|
+
measured_at = datetime('now')
|
|
2457
|
+
WHERE id = ?`,
|
|
2458
|
+
[
|
|
2459
|
+
sessionContinued,
|
|
2460
|
+
promptCountAfter,
|
|
2461
|
+
toolSuccessCount,
|
|
2462
|
+
toolTotalCount,
|
|
2463
|
+
wasReasked,
|
|
2464
|
+
helpfulnessScore,
|
|
2465
|
+
retrieval.id
|
|
2466
|
+
]
|
|
2467
|
+
);
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
/**
|
|
2471
|
+
* Get most helpful memories ranked by helpfulness score
|
|
2472
|
+
*/
|
|
2473
|
+
async getHelpfulMemories(limit = 10) {
|
|
2474
|
+
await this.initialize();
|
|
2475
|
+
const rows = sqliteAll(
|
|
2476
|
+
this.db,
|
|
2477
|
+
`SELECT
|
|
2478
|
+
mh.event_id,
|
|
2479
|
+
AVG(mh.helpfulness_score) as avg_score,
|
|
2480
|
+
COUNT(*) as eval_count,
|
|
2481
|
+
e.content,
|
|
2482
|
+
e.access_count
|
|
2483
|
+
FROM memory_helpfulness mh
|
|
2484
|
+
JOIN events e ON e.id = mh.event_id
|
|
2485
|
+
WHERE mh.measured_at IS NOT NULL
|
|
2486
|
+
GROUP BY mh.event_id
|
|
2487
|
+
ORDER BY avg_score DESC
|
|
2488
|
+
LIMIT ?`,
|
|
2489
|
+
[limit]
|
|
2490
|
+
);
|
|
2491
|
+
return rows.map((r) => ({
|
|
2492
|
+
eventId: r.event_id,
|
|
2493
|
+
summary: r.content.substring(0, 200) + (r.content.length > 200 ? "..." : ""),
|
|
2494
|
+
helpfulnessScore: Math.round(r.avg_score * 100) / 100,
|
|
2495
|
+
accessCount: r.access_count || 0,
|
|
2496
|
+
evaluationCount: r.eval_count
|
|
2497
|
+
}));
|
|
2498
|
+
}
|
|
2499
|
+
/**
|
|
2500
|
+
* Get helpfulness statistics for dashboard
|
|
2501
|
+
*/
|
|
2502
|
+
async getHelpfulnessStats() {
|
|
2503
|
+
await this.initialize();
|
|
2504
|
+
const stats = sqliteGet(
|
|
2505
|
+
this.db,
|
|
2506
|
+
`SELECT
|
|
2507
|
+
AVG(helpfulness_score) as avg_score,
|
|
2508
|
+
COUNT(*) as total_evaluated,
|
|
2509
|
+
SUM(CASE WHEN helpfulness_score >= 0.7 THEN 1 ELSE 0 END) as helpful,
|
|
2510
|
+
SUM(CASE WHEN helpfulness_score >= 0.4 AND helpfulness_score < 0.7 THEN 1 ELSE 0 END) as neutral,
|
|
2511
|
+
SUM(CASE WHEN helpfulness_score < 0.4 THEN 1 ELSE 0 END) as unhelpful
|
|
2512
|
+
FROM memory_helpfulness
|
|
2513
|
+
WHERE measured_at IS NOT NULL`
|
|
2514
|
+
);
|
|
2515
|
+
const totalRow = sqliteGet(
|
|
2516
|
+
this.db,
|
|
2517
|
+
`SELECT COUNT(*) as total FROM memory_helpfulness`
|
|
2518
|
+
);
|
|
2519
|
+
return {
|
|
2520
|
+
avgScore: Math.round((stats?.avg_score || 0) * 100) / 100,
|
|
2521
|
+
totalEvaluated: stats?.total_evaluated || 0,
|
|
2522
|
+
totalRetrievals: totalRow?.total || 0,
|
|
2523
|
+
helpful: stats?.helpful || 0,
|
|
2524
|
+
neutral: stats?.neutral || 0,
|
|
2525
|
+
unhelpful: stats?.unhelpful || 0
|
|
2526
|
+
};
|
|
2527
|
+
}
|
|
2072
2528
|
/**
|
|
2073
2529
|
* Fast keyword search using FTS5
|
|
2074
2530
|
* Returns events matching the search query, ranked by relevance
|
|
@@ -2131,12 +2587,222 @@ var SQLiteEventStore = class {
|
|
|
2131
2587
|
getDatabase() {
|
|
2132
2588
|
return this.db;
|
|
2133
2589
|
}
|
|
2590
|
+
async recordRetrievalTrace(input) {
|
|
2591
|
+
await this.initialize();
|
|
2592
|
+
const traceId = randomUUID2();
|
|
2593
|
+
sqliteRun(
|
|
2594
|
+
this.db,
|
|
2595
|
+
`INSERT INTO retrieval_traces (
|
|
2596
|
+
trace_id, session_id, project_hash, query_text, strategy,
|
|
2597
|
+
candidate_event_ids, selected_event_ids, candidate_details_json, selected_details_json,
|
|
2598
|
+
candidate_count, selected_count, confidence, fallback_trace
|
|
2599
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
2600
|
+
[
|
|
2601
|
+
traceId,
|
|
2602
|
+
input.sessionId || null,
|
|
2603
|
+
input.projectHash || null,
|
|
2604
|
+
input.queryText,
|
|
2605
|
+
input.strategy || null,
|
|
2606
|
+
JSON.stringify(input.candidateEventIds || []),
|
|
2607
|
+
JSON.stringify(input.selectedEventIds || []),
|
|
2608
|
+
JSON.stringify(input.candidateDetails || []),
|
|
2609
|
+
JSON.stringify(input.selectedDetails || []),
|
|
2610
|
+
(input.candidateEventIds || []).length,
|
|
2611
|
+
(input.selectedEventIds || []).length,
|
|
2612
|
+
input.confidence || null,
|
|
2613
|
+
JSON.stringify(input.fallbackTrace || [])
|
|
2614
|
+
]
|
|
2615
|
+
);
|
|
2616
|
+
}
|
|
2617
|
+
async getRecentRetrievalTraces(limit = 50) {
|
|
2618
|
+
await this.initialize();
|
|
2619
|
+
const rows = sqliteAll(
|
|
2620
|
+
this.db,
|
|
2621
|
+
`SELECT * FROM retrieval_traces ORDER BY created_at DESC LIMIT ?`,
|
|
2622
|
+
[limit]
|
|
2623
|
+
);
|
|
2624
|
+
return rows.map((row) => ({
|
|
2625
|
+
traceId: row.trace_id,
|
|
2626
|
+
sessionId: row.session_id || void 0,
|
|
2627
|
+
projectHash: row.project_hash || void 0,
|
|
2628
|
+
queryText: row.query_text,
|
|
2629
|
+
strategy: row.strategy || void 0,
|
|
2630
|
+
candidateEventIds: row.candidate_event_ids ? JSON.parse(row.candidate_event_ids) : [],
|
|
2631
|
+
selectedEventIds: row.selected_event_ids ? JSON.parse(row.selected_event_ids) : [],
|
|
2632
|
+
candidateDetails: row.candidate_details_json ? JSON.parse(row.candidate_details_json) : [],
|
|
2633
|
+
selectedDetails: row.selected_details_json ? JSON.parse(row.selected_details_json) : [],
|
|
2634
|
+
candidateCount: Number(row.candidate_count || 0),
|
|
2635
|
+
selectedCount: Number(row.selected_count || 0),
|
|
2636
|
+
confidence: row.confidence || void 0,
|
|
2637
|
+
fallbackTrace: row.fallback_trace ? JSON.parse(row.fallback_trace) : [],
|
|
2638
|
+
createdAt: toDateFromSQLite(row.created_at)
|
|
2639
|
+
}));
|
|
2640
|
+
}
|
|
2641
|
+
async getRetrievalTraceStats() {
|
|
2642
|
+
await this.initialize();
|
|
2643
|
+
const row = sqliteGet(
|
|
2644
|
+
this.db,
|
|
2645
|
+
`SELECT
|
|
2646
|
+
COUNT(*) as total_queries,
|
|
2647
|
+
AVG(candidate_count) as avg_candidate_count,
|
|
2648
|
+
AVG(selected_count) as avg_selected_count,
|
|
2649
|
+
CASE
|
|
2650
|
+
WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
|
|
2651
|
+
ELSE 0
|
|
2652
|
+
END as selection_rate
|
|
2653
|
+
FROM retrieval_traces`,
|
|
2654
|
+
[]
|
|
2655
|
+
);
|
|
2656
|
+
return {
|
|
2657
|
+
totalQueries: Number(row?.total_queries || 0),
|
|
2658
|
+
avgCandidateCount: Number(row?.avg_candidate_count || 0),
|
|
2659
|
+
avgSelectedCount: Number(row?.avg_selected_count || 0),
|
|
2660
|
+
selectionRate: Number(row?.selection_rate || 0)
|
|
2661
|
+
};
|
|
2662
|
+
}
|
|
2134
2663
|
/**
|
|
2135
2664
|
* Close database connection
|
|
2136
2665
|
*/
|
|
2137
2666
|
async close() {
|
|
2138
2667
|
sqliteClose(this.db);
|
|
2139
2668
|
}
|
|
2669
|
+
/**
|
|
2670
|
+
* Get events grouped by turn_id for a session
|
|
2671
|
+
* Returns turns ordered by first event timestamp (newest first)
|
|
2672
|
+
*/
|
|
2673
|
+
async getSessionTurns(sessionId, options) {
|
|
2674
|
+
await this.initialize();
|
|
2675
|
+
const limit = options?.limit || 20;
|
|
2676
|
+
const offset = options?.offset || 0;
|
|
2677
|
+
const turnRows = sqliteAll(
|
|
2678
|
+
this.db,
|
|
2679
|
+
`SELECT turn_id, MIN(timestamp) as min_ts
|
|
2680
|
+
FROM events
|
|
2681
|
+
WHERE session_id = ? AND turn_id IS NOT NULL
|
|
2682
|
+
GROUP BY turn_id
|
|
2683
|
+
ORDER BY min_ts DESC
|
|
2684
|
+
LIMIT ? OFFSET ?`,
|
|
2685
|
+
[sessionId, limit, offset]
|
|
2686
|
+
);
|
|
2687
|
+
const turns = [];
|
|
2688
|
+
for (const turnRow of turnRows) {
|
|
2689
|
+
const events = await this.getEventsByTurn(turnRow.turn_id);
|
|
2690
|
+
const promptEvent = events.find((e) => e.eventType === "user_prompt");
|
|
2691
|
+
const toolEvents = events.filter((e) => e.eventType === "tool_observation");
|
|
2692
|
+
const hasResponse = events.some((e) => e.eventType === "agent_response");
|
|
2693
|
+
turns.push({
|
|
2694
|
+
turnId: turnRow.turn_id,
|
|
2695
|
+
events,
|
|
2696
|
+
startedAt: toDateFromSQLite(turnRow.min_ts),
|
|
2697
|
+
promptPreview: promptEvent ? promptEvent.content.slice(0, 200) + (promptEvent.content.length > 200 ? "..." : "") : "(no prompt)",
|
|
2698
|
+
eventCount: events.length,
|
|
2699
|
+
toolCount: toolEvents.length,
|
|
2700
|
+
hasResponse
|
|
2701
|
+
});
|
|
2702
|
+
}
|
|
2703
|
+
return turns;
|
|
2704
|
+
}
|
|
2705
|
+
/**
|
|
2706
|
+
* Get all events for a specific turn_id
|
|
2707
|
+
*/
|
|
2708
|
+
async getEventsByTurn(turnId) {
|
|
2709
|
+
await this.initialize();
|
|
2710
|
+
const rows = sqliteAll(
|
|
2711
|
+
this.db,
|
|
2712
|
+
`SELECT * FROM events WHERE turn_id = ? ORDER BY timestamp ASC`,
|
|
2713
|
+
[turnId]
|
|
2714
|
+
);
|
|
2715
|
+
return rows.map(this.rowToEvent);
|
|
2716
|
+
}
|
|
2717
|
+
/**
|
|
2718
|
+
* Count total turns for a session
|
|
2719
|
+
*/
|
|
2720
|
+
async countSessionTurns(sessionId) {
|
|
2721
|
+
await this.initialize();
|
|
2722
|
+
const row = sqliteGet(
|
|
2723
|
+
this.db,
|
|
2724
|
+
`SELECT COUNT(DISTINCT turn_id) as count
|
|
2725
|
+
FROM events
|
|
2726
|
+
WHERE session_id = ? AND turn_id IS NOT NULL`,
|
|
2727
|
+
[sessionId]
|
|
2728
|
+
);
|
|
2729
|
+
return row?.count || 0;
|
|
2730
|
+
}
|
|
2731
|
+
/**
|
|
2732
|
+
* Migrate existing events: backfill turn_id for events that have turnId in metadata
|
|
2733
|
+
* but no turn_id column value (for events stored before this migration)
|
|
2734
|
+
*/
|
|
2735
|
+
async backfillTurnIds() {
|
|
2736
|
+
await this.initialize();
|
|
2737
|
+
const rows = sqliteAll(
|
|
2738
|
+
this.db,
|
|
2739
|
+
`SELECT id, metadata FROM events
|
|
2740
|
+
WHERE turn_id IS NULL AND metadata IS NOT NULL AND metadata LIKE '%turnId%'`
|
|
2741
|
+
);
|
|
2742
|
+
let updated = 0;
|
|
2743
|
+
for (const row of rows) {
|
|
2744
|
+
try {
|
|
2745
|
+
const metadata = JSON.parse(row.metadata);
|
|
2746
|
+
if (metadata.turnId) {
|
|
2747
|
+
sqliteRun(
|
|
2748
|
+
this.db,
|
|
2749
|
+
`UPDATE events SET turn_id = ? WHERE id = ?`,
|
|
2750
|
+
[metadata.turnId, row.id]
|
|
2751
|
+
);
|
|
2752
|
+
updated++;
|
|
2753
|
+
}
|
|
2754
|
+
} catch {
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
return updated;
|
|
2758
|
+
}
|
|
2759
|
+
/**
|
|
2760
|
+
* Delete all events for a session (for force reimport)
|
|
2761
|
+
*/
|
|
2762
|
+
async deleteSessionEvents(sessionId) {
|
|
2763
|
+
await this.initialize();
|
|
2764
|
+
const events = sqliteAll(
|
|
2765
|
+
this.db,
|
|
2766
|
+
`SELECT id FROM events WHERE session_id = ?`,
|
|
2767
|
+
[sessionId]
|
|
2768
|
+
);
|
|
2769
|
+
if (events.length === 0)
|
|
2770
|
+
return 0;
|
|
2771
|
+
const eventIds = events.map((e) => e.id);
|
|
2772
|
+
const placeholders = eventIds.map(() => "?").join(",");
|
|
2773
|
+
const ftsTriggersDropped = [];
|
|
2774
|
+
for (const triggerName of ["events_fts_delete", "events_fts_update", "events_fts_insert"]) {
|
|
2775
|
+
try {
|
|
2776
|
+
sqliteRun(this.db, `DROP TRIGGER IF EXISTS ${triggerName}`);
|
|
2777
|
+
ftsTriggersDropped.push(triggerName);
|
|
2778
|
+
} catch {
|
|
2779
|
+
}
|
|
2780
|
+
}
|
|
2781
|
+
for (const table of ["event_dedup", "memory_levels", "embedding_queue", "embedding_outbox", "vector_outbox"]) {
|
|
2782
|
+
try {
|
|
2783
|
+
sqliteRun(this.db, `DELETE FROM ${table} WHERE event_id IN (${placeholders})`, eventIds);
|
|
2784
|
+
} catch {
|
|
2785
|
+
}
|
|
2786
|
+
}
|
|
2787
|
+
const result = sqliteRun(this.db, `DELETE FROM events WHERE session_id = ?`, [sessionId]);
|
|
2788
|
+
if (ftsTriggersDropped.length > 0) {
|
|
2789
|
+
try {
|
|
2790
|
+
sqliteRun(this.db, `INSERT INTO events_fts(events_fts) VALUES('rebuild')`);
|
|
2791
|
+
sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_insert AFTER INSERT ON events BEGIN
|
|
2792
|
+
INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
|
|
2793
|
+
END`);
|
|
2794
|
+
sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_delete AFTER DELETE ON events BEGIN
|
|
2795
|
+
INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
|
|
2796
|
+
END`);
|
|
2797
|
+
sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_update AFTER UPDATE ON events BEGIN
|
|
2798
|
+
INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
|
|
2799
|
+
INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
|
|
2800
|
+
END`);
|
|
2801
|
+
} catch {
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
return result.changes || 0;
|
|
2805
|
+
}
|
|
2140
2806
|
/**
|
|
2141
2807
|
* Convert database row to MemoryEvent
|
|
2142
2808
|
*/
|
|
@@ -2157,6 +2823,9 @@ var SQLiteEventStore = class {
|
|
|
2157
2823
|
if (row.last_accessed_at !== void 0) {
|
|
2158
2824
|
event.last_accessed_at = row.last_accessed_at;
|
|
2159
2825
|
}
|
|
2826
|
+
if (row.turn_id !== void 0 && row.turn_id !== null) {
|
|
2827
|
+
event.turn_id = row.turn_id;
|
|
2828
|
+
}
|
|
2160
2829
|
return event;
|
|
2161
2830
|
}
|
|
2162
2831
|
};
|
|
@@ -2324,8 +2993,282 @@ var SyncWorker = class {
|
|
|
2324
2993
|
}
|
|
2325
2994
|
};
|
|
2326
2995
|
|
|
2327
|
-
// src/core/
|
|
2996
|
+
// src/core/mongo-sync-worker.ts
|
|
2328
2997
|
import { randomUUID as randomUUID3 } from "crypto";
|
|
2998
|
+
import * as os from "os";
|
|
2999
|
+
import { MongoClient } from "mongodb";
|
|
3000
|
+
function redactMongoUri(uri) {
|
|
3001
|
+
const schemeIdx = uri.indexOf("://");
|
|
3002
|
+
if (schemeIdx === -1)
|
|
3003
|
+
return uri;
|
|
3004
|
+
const atIdx = uri.indexOf("@", schemeIdx + 3);
|
|
3005
|
+
if (atIdx === -1)
|
|
3006
|
+
return uri;
|
|
3007
|
+
const creds = uri.slice(schemeIdx + 3, atIdx);
|
|
3008
|
+
const colonIdx = creds.indexOf(":");
|
|
3009
|
+
if (colonIdx === -1)
|
|
3010
|
+
return uri;
|
|
3011
|
+
const prefix = uri.slice(0, schemeIdx + 3 + colonIdx + 1);
|
|
3012
|
+
const suffix = uri.slice(atIdx);
|
|
3013
|
+
return `${prefix}***${suffix}`;
|
|
3014
|
+
}
|
|
3015
|
+
function parseIntOrZero(value) {
|
|
3016
|
+
if (!value)
|
|
3017
|
+
return 0;
|
|
3018
|
+
const n = parseInt(value, 10);
|
|
3019
|
+
return Number.isFinite(n) ? n : 0;
|
|
3020
|
+
}
|
|
3021
|
+
var MongoSyncWorker = class {
|
|
3022
|
+
constructor(sqliteStore, config) {
|
|
3023
|
+
this.sqliteStore = sqliteStore;
|
|
3024
|
+
this.config = {
|
|
3025
|
+
uri: config.uri,
|
|
3026
|
+
dbName: config.dbName,
|
|
3027
|
+
projectKey: config.projectKey,
|
|
3028
|
+
direction: config.direction ?? "both",
|
|
3029
|
+
intervalMs: config.intervalMs ?? 3e4,
|
|
3030
|
+
batchSize: config.batchSize ?? 500,
|
|
3031
|
+
instanceId: config.instanceId ?? randomUUID3()
|
|
3032
|
+
};
|
|
3033
|
+
}
|
|
3034
|
+
config;
|
|
3035
|
+
intervalHandle = null;
|
|
3036
|
+
running = false;
|
|
3037
|
+
client = null;
|
|
3038
|
+
db = null;
|
|
3039
|
+
counters = null;
|
|
3040
|
+
events = null;
|
|
3041
|
+
indexesEnsured = false;
|
|
3042
|
+
stats = {
|
|
3043
|
+
lastSyncAt: null,
|
|
3044
|
+
pushedEvents: 0,
|
|
3045
|
+
pulledEvents: 0,
|
|
3046
|
+
errors: 0,
|
|
3047
|
+
status: "idle"
|
|
3048
|
+
};
|
|
3049
|
+
start() {
|
|
3050
|
+
if (this.running)
|
|
3051
|
+
return;
|
|
3052
|
+
this.running = true;
|
|
3053
|
+
this.stats.status = "idle";
|
|
3054
|
+
this.syncNow().catch((err) => {
|
|
3055
|
+
console.error("[MongoSyncWorker] Initial sync failed:", err);
|
|
3056
|
+
});
|
|
3057
|
+
this.intervalHandle = setInterval(() => {
|
|
3058
|
+
this.syncNow().catch((err) => {
|
|
3059
|
+
console.error("[MongoSyncWorker] Periodic sync failed:", err);
|
|
3060
|
+
});
|
|
3061
|
+
}, this.config.intervalMs);
|
|
3062
|
+
}
|
|
3063
|
+
stop() {
|
|
3064
|
+
this.running = false;
|
|
3065
|
+
this.stats.status = "stopped";
|
|
3066
|
+
if (this.intervalHandle) {
|
|
3067
|
+
clearInterval(this.intervalHandle);
|
|
3068
|
+
this.intervalHandle = null;
|
|
3069
|
+
}
|
|
3070
|
+
}
|
|
3071
|
+
async shutdown() {
|
|
3072
|
+
this.stop();
|
|
3073
|
+
await this.disconnect();
|
|
3074
|
+
}
|
|
3075
|
+
getStats() {
|
|
3076
|
+
return { ...this.stats };
|
|
3077
|
+
}
|
|
3078
|
+
isRunning() {
|
|
3079
|
+
return this.running;
|
|
3080
|
+
}
|
|
3081
|
+
async syncNow() {
|
|
3082
|
+
if (this.stats.status === "syncing")
|
|
3083
|
+
return { pushed: 0, pulled: 0 };
|
|
3084
|
+
this.stats.status = "syncing";
|
|
3085
|
+
let pushed = 0;
|
|
3086
|
+
let pulled = 0;
|
|
3087
|
+
try {
|
|
3088
|
+
await this.sqliteStore.initialize();
|
|
3089
|
+
await this.ensureConnected();
|
|
3090
|
+
await this.ensureIndexes();
|
|
3091
|
+
if (this.config.direction === "push" || this.config.direction === "both") {
|
|
3092
|
+
pushed = await this.pushEvents();
|
|
3093
|
+
this.stats.pushedEvents += pushed;
|
|
3094
|
+
}
|
|
3095
|
+
if (this.config.direction === "pull" || this.config.direction === "both") {
|
|
3096
|
+
pulled = await this.pullEvents();
|
|
3097
|
+
this.stats.pulledEvents += pulled;
|
|
3098
|
+
}
|
|
3099
|
+
this.stats.lastSyncAt = /* @__PURE__ */ new Date();
|
|
3100
|
+
this.stats.status = "idle";
|
|
3101
|
+
return { pushed, pulled };
|
|
3102
|
+
} catch (error) {
|
|
3103
|
+
this.stats.errors++;
|
|
3104
|
+
this.stats.status = "error";
|
|
3105
|
+
throw error;
|
|
3106
|
+
}
|
|
3107
|
+
}
|
|
3108
|
+
async ensureConnected() {
|
|
3109
|
+
if (this.client && this.db && this.counters && this.events)
|
|
3110
|
+
return;
|
|
3111
|
+
try {
|
|
3112
|
+
this.client = new MongoClient(this.config.uri, {
|
|
3113
|
+
appName: "claude-memory-layer",
|
|
3114
|
+
serverSelectionTimeoutMS: 5e3
|
|
3115
|
+
});
|
|
3116
|
+
await this.client.connect();
|
|
3117
|
+
this.db = this.client.db(this.config.dbName);
|
|
3118
|
+
this.counters = this.db.collection("cml_counters");
|
|
3119
|
+
this.events = this.db.collection("cml_events");
|
|
3120
|
+
} catch (err) {
|
|
3121
|
+
const safeUri = redactMongoUri(this.config.uri);
|
|
3122
|
+
throw new Error(`MongoDB connection failed (${safeUri}, db=${this.config.dbName}): ${String(err)}`);
|
|
3123
|
+
}
|
|
3124
|
+
}
|
|
3125
|
+
async disconnect() {
|
|
3126
|
+
try {
|
|
3127
|
+
await this.client?.close();
|
|
3128
|
+
} finally {
|
|
3129
|
+
this.client = null;
|
|
3130
|
+
this.db = null;
|
|
3131
|
+
this.counters = null;
|
|
3132
|
+
this.events = null;
|
|
3133
|
+
this.indexesEnsured = false;
|
|
3134
|
+
}
|
|
3135
|
+
}
|
|
3136
|
+
async ensureIndexes() {
|
|
3137
|
+
if (this.indexesEnsured)
|
|
3138
|
+
return;
|
|
3139
|
+
if (!this.events || !this.counters)
|
|
3140
|
+
throw new Error("Mongo not connected");
|
|
3141
|
+
try {
|
|
3142
|
+
await this.events.createIndex({ projectKey: 1, seq: 1 }, { unique: true });
|
|
3143
|
+
await this.events.createIndex({ projectKey: 1, eventId: 1 }, { unique: true });
|
|
3144
|
+
await this.events.createIndex({ projectKey: 1, dedupeKey: 1 });
|
|
3145
|
+
} catch (err) {
|
|
3146
|
+
console.warn("[MongoSyncWorker] Failed to ensure indexes (continuing):", err);
|
|
3147
|
+
}
|
|
3148
|
+
this.indexesEnsured = true;
|
|
3149
|
+
}
|
|
3150
|
+
counterKey(kind) {
|
|
3151
|
+
return `${kind}:${this.config.projectKey}`;
|
|
3152
|
+
}
|
|
3153
|
+
async allocateSeqRange(kind, count) {
|
|
3154
|
+
if (!this.counters)
|
|
3155
|
+
throw new Error("Mongo not connected");
|
|
3156
|
+
if (count <= 0)
|
|
3157
|
+
return 1;
|
|
3158
|
+
const key = this.counterKey(kind);
|
|
3159
|
+
const doc = await this.counters.findOneAndUpdate(
|
|
3160
|
+
{ _id: key },
|
|
3161
|
+
{ $inc: { seq: count } },
|
|
3162
|
+
{ upsert: true, returnDocument: "after" }
|
|
3163
|
+
);
|
|
3164
|
+
const endSeq = doc?.seq;
|
|
3165
|
+
if (typeof endSeq !== "number") {
|
|
3166
|
+
throw new Error(`Failed to allocate seq range for ${key}`);
|
|
3167
|
+
}
|
|
3168
|
+
return endSeq - count + 1;
|
|
3169
|
+
}
|
|
3170
|
+
pushTargetName() {
|
|
3171
|
+
return `mongo_push_events_rowid:${this.config.projectKey}`;
|
|
3172
|
+
}
|
|
3173
|
+
pullTargetName() {
|
|
3174
|
+
return `mongo_pull_events_seq:${this.config.projectKey}`;
|
|
3175
|
+
}
|
|
3176
|
+
async pushEvents() {
|
|
3177
|
+
if (!this.events)
|
|
3178
|
+
throw new Error("Mongo not connected");
|
|
3179
|
+
const position = await this.sqliteStore.getSyncPosition(this.pushTargetName());
|
|
3180
|
+
let lastRowid = parseIntOrZero(position.lastEventId);
|
|
3181
|
+
let pushed = 0;
|
|
3182
|
+
while (true) {
|
|
3183
|
+
const batch = await this.sqliteStore.getEventsSinceRowid(lastRowid, this.config.batchSize);
|
|
3184
|
+
if (batch.length === 0)
|
|
3185
|
+
break;
|
|
3186
|
+
const startSeq = await this.allocateSeqRange("events", batch.length);
|
|
3187
|
+
const now = /* @__PURE__ */ new Date();
|
|
3188
|
+
const hostname2 = os.hostname();
|
|
3189
|
+
const ops = batch.map((item, idx) => {
|
|
3190
|
+
const event = item.event;
|
|
3191
|
+
const seq = startSeq + idx;
|
|
3192
|
+
const docId = `${this.config.projectKey}:${event.id}`;
|
|
3193
|
+
return {
|
|
3194
|
+
updateOne: {
|
|
3195
|
+
filter: { _id: docId },
|
|
3196
|
+
update: {
|
|
3197
|
+
$setOnInsert: {
|
|
3198
|
+
_id: docId,
|
|
3199
|
+
projectKey: this.config.projectKey,
|
|
3200
|
+
seq,
|
|
3201
|
+
eventId: event.id,
|
|
3202
|
+
eventType: event.eventType,
|
|
3203
|
+
sessionId: event.sessionId,
|
|
3204
|
+
timestamp: event.timestamp,
|
|
3205
|
+
content: event.content,
|
|
3206
|
+
canonicalKey: event.canonicalKey,
|
|
3207
|
+
dedupeKey: event.dedupeKey,
|
|
3208
|
+
metadata: event.metadata ?? null,
|
|
3209
|
+
insertedAt: now,
|
|
3210
|
+
updatedAt: now,
|
|
3211
|
+
source: { hostname: hostname2, instanceId: this.config.instanceId }
|
|
3212
|
+
}
|
|
3213
|
+
},
|
|
3214
|
+
upsert: true
|
|
3215
|
+
}
|
|
3216
|
+
};
|
|
3217
|
+
});
|
|
3218
|
+
await this.events.bulkWrite(ops, { ordered: false });
|
|
3219
|
+
const last = batch[batch.length - 1];
|
|
3220
|
+
lastRowid = last.rowid;
|
|
3221
|
+
await this.sqliteStore.updateSyncPosition(
|
|
3222
|
+
this.pushTargetName(),
|
|
3223
|
+
String(lastRowid),
|
|
3224
|
+
last.event.timestamp.toISOString()
|
|
3225
|
+
);
|
|
3226
|
+
pushed += batch.length;
|
|
3227
|
+
if (batch.length < this.config.batchSize)
|
|
3228
|
+
break;
|
|
3229
|
+
}
|
|
3230
|
+
return pushed;
|
|
3231
|
+
}
|
|
3232
|
+
async pullEvents() {
|
|
3233
|
+
if (!this.events)
|
|
3234
|
+
throw new Error("Mongo not connected");
|
|
3235
|
+
const position = await this.sqliteStore.getSyncPosition(this.pullTargetName());
|
|
3236
|
+
let lastSeq = parseIntOrZero(position.lastEventId);
|
|
3237
|
+
let pulled = 0;
|
|
3238
|
+
while (true) {
|
|
3239
|
+
const docs = await this.events.find(
|
|
3240
|
+
{ projectKey: this.config.projectKey, seq: { $gt: lastSeq } },
|
|
3241
|
+
{ sort: { seq: 1 }, limit: this.config.batchSize }
|
|
3242
|
+
).toArray();
|
|
3243
|
+
if (docs.length === 0)
|
|
3244
|
+
break;
|
|
3245
|
+
const events = docs.map((d) => ({
|
|
3246
|
+
id: d.eventId,
|
|
3247
|
+
eventType: d.eventType,
|
|
3248
|
+
sessionId: d.sessionId,
|
|
3249
|
+
timestamp: d.timestamp instanceof Date ? d.timestamp : new Date(d.timestamp),
|
|
3250
|
+
content: d.content,
|
|
3251
|
+
canonicalKey: d.canonicalKey,
|
|
3252
|
+
dedupeKey: d.dedupeKey,
|
|
3253
|
+
metadata: d.metadata ?? void 0
|
|
3254
|
+
}));
|
|
3255
|
+
const result = await this.sqliteStore.importEvents(events);
|
|
3256
|
+
pulled += result.inserted;
|
|
3257
|
+
lastSeq = docs[docs.length - 1].seq;
|
|
3258
|
+
await this.sqliteStore.updateSyncPosition(
|
|
3259
|
+
this.pullTargetName(),
|
|
3260
|
+
String(lastSeq),
|
|
3261
|
+
(/* @__PURE__ */ new Date()).toISOString()
|
|
3262
|
+
);
|
|
3263
|
+
if (docs.length < this.config.batchSize)
|
|
3264
|
+
break;
|
|
3265
|
+
}
|
|
3266
|
+
return pulled;
|
|
3267
|
+
}
|
|
3268
|
+
};
|
|
3269
|
+
|
|
3270
|
+
// src/core/entity-repo.ts
|
|
3271
|
+
import { randomUUID as randomUUID4 } from "crypto";
|
|
2329
3272
|
var EntityRepo = class {
|
|
2330
3273
|
constructor(db) {
|
|
2331
3274
|
this.db = db;
|
|
@@ -2334,7 +3277,7 @@ var EntityRepo = class {
|
|
|
2334
3277
|
* Create a new entity
|
|
2335
3278
|
*/
|
|
2336
3279
|
async create(input) {
|
|
2337
|
-
const entityId =
|
|
3280
|
+
const entityId = randomUUID4();
|
|
2338
3281
|
const canonicalKey = makeEntityCanonicalKey(input.entityType, input.title, {
|
|
2339
3282
|
project: input.project
|
|
2340
3283
|
});
|
|
@@ -2588,7 +3531,7 @@ var EntityRepo = class {
|
|
|
2588
3531
|
};
|
|
2589
3532
|
|
|
2590
3533
|
// src/core/edge-repo.ts
|
|
2591
|
-
import { randomUUID as
|
|
3534
|
+
import { randomUUID as randomUUID5 } from "crypto";
|
|
2592
3535
|
var EdgeRepo = class {
|
|
2593
3536
|
constructor(db) {
|
|
2594
3537
|
this.db = db;
|
|
@@ -2597,7 +3540,7 @@ var EdgeRepo = class {
|
|
|
2597
3540
|
* Create a new edge (idempotent - ignores duplicates)
|
|
2598
3541
|
*/
|
|
2599
3542
|
async create(input) {
|
|
2600
|
-
const edgeId =
|
|
3543
|
+
const edgeId = randomUUID5();
|
|
2601
3544
|
const now = /* @__PURE__ */ new Date();
|
|
2602
3545
|
await dbRun(
|
|
2603
3546
|
this.db,
|
|
@@ -2886,7 +3829,16 @@ var VectorStore = class {
|
|
|
2886
3829
|
metadata: JSON.stringify(record.metadata || {})
|
|
2887
3830
|
};
|
|
2888
3831
|
if (!this.table) {
|
|
2889
|
-
|
|
3832
|
+
try {
|
|
3833
|
+
this.table = await this.db.createTable(this.tableName, [data]);
|
|
3834
|
+
} catch (e) {
|
|
3835
|
+
if (e?.message?.includes("already exists")) {
|
|
3836
|
+
this.table = await this.db.openTable(this.tableName);
|
|
3837
|
+
await this.table.add([data]);
|
|
3838
|
+
} else {
|
|
3839
|
+
throw e;
|
|
3840
|
+
}
|
|
3841
|
+
}
|
|
2890
3842
|
} else {
|
|
2891
3843
|
await this.table.add([data]);
|
|
2892
3844
|
}
|
|
@@ -2912,7 +3864,16 @@ var VectorStore = class {
|
|
|
2912
3864
|
metadata: JSON.stringify(record.metadata || {})
|
|
2913
3865
|
}));
|
|
2914
3866
|
if (!this.table) {
|
|
2915
|
-
|
|
3867
|
+
try {
|
|
3868
|
+
this.table = await this.db.createTable(this.tableName, data);
|
|
3869
|
+
} catch (e) {
|
|
3870
|
+
if (e?.message?.includes("already exists")) {
|
|
3871
|
+
this.table = await this.db.openTable(this.tableName);
|
|
3872
|
+
await this.table.add(data);
|
|
3873
|
+
} else {
|
|
3874
|
+
throw e;
|
|
3875
|
+
}
|
|
3876
|
+
}
|
|
2916
3877
|
} else {
|
|
2917
3878
|
await this.table.add(data);
|
|
2918
3879
|
}
|
|
@@ -3070,7 +4031,7 @@ function getDefaultEmbedder() {
|
|
|
3070
4031
|
}
|
|
3071
4032
|
|
|
3072
4033
|
// src/core/vector-outbox.ts
|
|
3073
|
-
import { randomUUID as
|
|
4034
|
+
import { randomUUID as randomUUID6 } from "crypto";
|
|
3074
4035
|
var DEFAULT_CONFIG2 = {
|
|
3075
4036
|
embeddingVersion: "v1",
|
|
3076
4037
|
maxRetries: 3,
|
|
@@ -3089,7 +4050,7 @@ var VectorOutbox = class {
|
|
|
3089
4050
|
*/
|
|
3090
4051
|
async enqueue(itemKind, itemId, embeddingVersion) {
|
|
3091
4052
|
const version = embeddingVersion ?? this.config.embeddingVersion;
|
|
3092
|
-
const jobId =
|
|
4053
|
+
const jobId = randomUUID6();
|
|
3093
4054
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
3094
4055
|
await dbRun(
|
|
3095
4056
|
this.db,
|
|
@@ -4250,7 +5211,20 @@ var DEFAULT_OPTIONS2 = {
|
|
|
4250
5211
|
topK: 5,
|
|
4251
5212
|
minScore: 0.7,
|
|
4252
5213
|
maxTokens: 2e3,
|
|
4253
|
-
includeSessionContext: true
|
|
5214
|
+
includeSessionContext: true,
|
|
5215
|
+
strategy: "auto",
|
|
5216
|
+
rerankWithKeyword: true,
|
|
5217
|
+
decayPolicy: {
|
|
5218
|
+
enabled: true,
|
|
5219
|
+
windowDays: 30,
|
|
5220
|
+
maxPenalty: 0.15
|
|
5221
|
+
},
|
|
5222
|
+
graphHop: {
|
|
5223
|
+
enabled: true,
|
|
5224
|
+
maxHops: 1,
|
|
5225
|
+
hopPenalty: 0.08
|
|
5226
|
+
},
|
|
5227
|
+
projectScopeMode: "global"
|
|
4254
5228
|
};
|
|
4255
5229
|
var Retriever = class {
|
|
4256
5230
|
eventStore;
|
|
@@ -4260,6 +5234,7 @@ var Retriever = class {
|
|
|
4260
5234
|
sharedStore;
|
|
4261
5235
|
sharedVectorStore;
|
|
4262
5236
|
graduation;
|
|
5237
|
+
queryRewriter;
|
|
4263
5238
|
constructor(eventStore, vectorStore, embedder, matcher, sharedOptions) {
|
|
4264
5239
|
this.eventStore = eventStore;
|
|
4265
5240
|
this.vectorStore = vectorStore;
|
|
@@ -4268,47 +5243,105 @@ var Retriever = class {
|
|
|
4268
5243
|
this.sharedStore = sharedOptions?.sharedStore;
|
|
4269
5244
|
this.sharedVectorStore = sharedOptions?.sharedVectorStore;
|
|
4270
5245
|
}
|
|
4271
|
-
/**
|
|
4272
|
-
* Set graduation pipeline for access tracking
|
|
4273
|
-
*/
|
|
4274
5246
|
setGraduationPipeline(graduation) {
|
|
4275
5247
|
this.graduation = graduation;
|
|
4276
5248
|
}
|
|
4277
|
-
/**
|
|
4278
|
-
* Set shared stores after construction
|
|
4279
|
-
*/
|
|
4280
5249
|
setSharedStores(sharedStore, sharedVectorStore) {
|
|
4281
5250
|
this.sharedStore = sharedStore;
|
|
4282
5251
|
this.sharedVectorStore = sharedVectorStore;
|
|
4283
5252
|
}
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
5253
|
+
setQueryRewriter(rewriter) {
|
|
5254
|
+
this.queryRewriter = rewriter;
|
|
5255
|
+
}
|
|
4287
5256
|
async retrieve(query, options = {}) {
|
|
4288
5257
|
const opts = { ...DEFAULT_OPTIONS2, ...options };
|
|
4289
|
-
const
|
|
4290
|
-
const
|
|
4291
|
-
|
|
4292
|
-
|
|
5258
|
+
const sessionFilter = opts.scope?.sessionId ?? opts.sessionId;
|
|
5259
|
+
const fallbackTrace = [];
|
|
5260
|
+
const fallbackEnabled = (opts.strategy ?? "auto") === "auto";
|
|
5261
|
+
const primaryStrategy = opts.strategy === "auto" ? "fast" : opts.strategy || "fast";
|
|
5262
|
+
let current = await this.runStage(query, {
|
|
5263
|
+
strategy: primaryStrategy,
|
|
5264
|
+
topK: opts.topK,
|
|
4293
5265
|
minScore: opts.minScore,
|
|
4294
|
-
sessionId:
|
|
5266
|
+
sessionId: sessionFilter,
|
|
5267
|
+
scope: opts.scope,
|
|
5268
|
+
rerankWithKeyword: opts.rerankWithKeyword !== false,
|
|
5269
|
+
rerankWeights: opts.rerankWeights,
|
|
5270
|
+
decayPolicy: opts.decayPolicy,
|
|
5271
|
+
intentRewrite: opts.intentRewrite === true,
|
|
5272
|
+
graphHop: opts.graphHop,
|
|
5273
|
+
projectScopeMode: opts.projectScopeMode,
|
|
5274
|
+
projectHash: opts.projectHash,
|
|
5275
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
4295
5276
|
});
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
5277
|
+
fallbackTrace.push(`stage:primary:${primaryStrategy}`);
|
|
5278
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results) && primaryStrategy !== "deep") {
|
|
5279
|
+
current = await this.runStage(query, {
|
|
5280
|
+
strategy: "deep",
|
|
5281
|
+
topK: opts.topK,
|
|
5282
|
+
minScore: opts.minScore,
|
|
5283
|
+
sessionId: sessionFilter,
|
|
5284
|
+
scope: opts.scope,
|
|
5285
|
+
rerankWithKeyword: opts.rerankWithKeyword !== false,
|
|
5286
|
+
rerankWeights: opts.rerankWeights,
|
|
5287
|
+
decayPolicy: opts.decayPolicy,
|
|
5288
|
+
graphHop: opts.graphHop,
|
|
5289
|
+
projectScopeMode: opts.projectScopeMode,
|
|
5290
|
+
projectHash: opts.projectHash,
|
|
5291
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
5292
|
+
});
|
|
5293
|
+
fallbackTrace.push("fallback:deep");
|
|
5294
|
+
}
|
|
5295
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
|
|
5296
|
+
current = await this.runStage(query, {
|
|
5297
|
+
strategy: "deep",
|
|
5298
|
+
topK: opts.topK,
|
|
5299
|
+
minScore: Math.max(0.5, opts.minScore - 0.15),
|
|
5300
|
+
sessionId: void 0,
|
|
5301
|
+
scope: void 0,
|
|
5302
|
+
rerankWithKeyword: true,
|
|
5303
|
+
rerankWeights: opts.rerankWeights,
|
|
5304
|
+
decayPolicy: opts.decayPolicy,
|
|
5305
|
+
graphHop: opts.graphHop,
|
|
5306
|
+
projectScopeMode: opts.projectScopeMode,
|
|
5307
|
+
projectHash: opts.projectHash,
|
|
5308
|
+
allowedProjectHashes: opts.allowedProjectHashes
|
|
5309
|
+
});
|
|
5310
|
+
fallbackTrace.push("fallback:scope-expanded");
|
|
5311
|
+
}
|
|
5312
|
+
if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
|
|
5313
|
+
const summary = await this.buildSummaryFallback(query, opts.topK);
|
|
5314
|
+
current = {
|
|
5315
|
+
results: summary,
|
|
5316
|
+
candidateResults: summary,
|
|
5317
|
+
matchResult: this.matcher.matchSearchResults(summary, () => 0)
|
|
5318
|
+
};
|
|
5319
|
+
fallbackTrace.push("fallback:summary");
|
|
5320
|
+
}
|
|
5321
|
+
const memories = await this.enrichResults(current.results.slice(0, opts.topK), opts);
|
|
4301
5322
|
const context = this.buildContext(memories, opts.maxTokens);
|
|
4302
5323
|
return {
|
|
4303
5324
|
memories,
|
|
4304
|
-
matchResult,
|
|
5325
|
+
matchResult: current.matchResult,
|
|
4305
5326
|
totalTokens: this.estimateTokens(context),
|
|
4306
|
-
context
|
|
5327
|
+
context,
|
|
5328
|
+
fallbackTrace,
|
|
5329
|
+
selectedDebug: current.results.slice(0, opts.topK).map((r) => ({
|
|
5330
|
+
eventId: r.eventId,
|
|
5331
|
+
score: r.score,
|
|
5332
|
+
semanticScore: r.semanticScore,
|
|
5333
|
+
lexicalScore: r.lexicalScore,
|
|
5334
|
+
recencyScore: r.recencyScore
|
|
5335
|
+
})),
|
|
5336
|
+
candidateDebug: (current.candidateResults || []).slice(0, Math.max(opts.topK * 3, 20)).map((r) => ({
|
|
5337
|
+
eventId: r.eventId,
|
|
5338
|
+
score: r.score,
|
|
5339
|
+
semanticScore: r.semanticScore,
|
|
5340
|
+
lexicalScore: r.lexicalScore,
|
|
5341
|
+
recencyScore: r.recencyScore
|
|
5342
|
+
}))
|
|
4307
5343
|
};
|
|
4308
5344
|
}
|
|
4309
|
-
/**
|
|
4310
|
-
* Retrieve with unified search (project + shared)
|
|
4311
|
-
*/
|
|
4312
5345
|
async retrieveUnified(query, options = {}) {
|
|
4313
5346
|
const projectResult = await this.retrieve(query, options);
|
|
4314
5347
|
if (!options.includeShared || !this.sharedStore || !this.sharedVectorStore) {
|
|
@@ -4316,22 +5349,19 @@ var Retriever = class {
|
|
|
4316
5349
|
}
|
|
4317
5350
|
try {
|
|
4318
5351
|
const queryEmbedding = await this.embedder.embed(query);
|
|
4319
|
-
const sharedVectorResults = await this.sharedVectorStore.search(
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
excludeProjectHash: options.projectHash
|
|
4325
|
-
}
|
|
4326
|
-
);
|
|
5352
|
+
const sharedVectorResults = await this.sharedVectorStore.search(queryEmbedding.vector, {
|
|
5353
|
+
limit: options.topK || 5,
|
|
5354
|
+
minScore: options.minScore || 0.7,
|
|
5355
|
+
excludeProjectHash: options.projectHash
|
|
5356
|
+
});
|
|
4327
5357
|
const sharedMemories = [];
|
|
4328
5358
|
for (const result of sharedVectorResults) {
|
|
4329
5359
|
const entry = await this.sharedStore.get(result.entryId);
|
|
4330
|
-
if (entry)
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
5360
|
+
if (!entry)
|
|
5361
|
+
continue;
|
|
5362
|
+
if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
|
|
5363
|
+
sharedMemories.push(entry);
|
|
5364
|
+
await this.sharedStore.recordUsage(entry.entryId);
|
|
4335
5365
|
}
|
|
4336
5366
|
}
|
|
4337
5367
|
const unifiedContext = this.buildUnifiedContext(projectResult, sharedMemories);
|
|
@@ -4346,50 +5376,243 @@ var Retriever = class {
|
|
|
4346
5376
|
return projectResult;
|
|
4347
5377
|
}
|
|
4348
5378
|
}
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
5379
|
+
async runStage(query, input) {
|
|
5380
|
+
let initialResults = await this.searchByStrategy(query, {
|
|
5381
|
+
strategy: input.strategy,
|
|
5382
|
+
topK: input.topK,
|
|
5383
|
+
minScore: input.minScore,
|
|
5384
|
+
sessionId: input.sessionId
|
|
5385
|
+
});
|
|
5386
|
+
if (input.intentRewrite && input.strategy === "deep" && this.queryRewriter) {
|
|
5387
|
+
const rewritten = (await this.queryRewriter(query))?.trim();
|
|
5388
|
+
if (rewritten && rewritten !== query) {
|
|
5389
|
+
const rewrittenResults = await this.searchByStrategy(rewritten, {
|
|
5390
|
+
strategy: "deep",
|
|
5391
|
+
topK: input.topK,
|
|
5392
|
+
minScore: Math.max(0.5, input.minScore - 0.1),
|
|
5393
|
+
sessionId: input.sessionId
|
|
5394
|
+
});
|
|
5395
|
+
initialResults = this.mergeResults(initialResults, rewrittenResults, input.topK * 3);
|
|
5396
|
+
}
|
|
5397
|
+
}
|
|
5398
|
+
const expandedResults = input.graphHop?.enabled === false ? initialResults : await this.expandGraphHops(initialResults, {
|
|
5399
|
+
maxHops: Math.max(1, input.graphHop?.maxHops ?? 1),
|
|
5400
|
+
hopPenalty: Math.max(0, input.graphHop?.hopPenalty ?? 0.08),
|
|
5401
|
+
limit: input.topK * 4
|
|
5402
|
+
});
|
|
5403
|
+
const rerankedResults = input.rerankWithKeyword ? this.rerankByKeywordOverlap(expandedResults, query, input.rerankWeights, input.decayPolicy) : expandedResults;
|
|
5404
|
+
const filtered = await this.applyScopeFilters(rerankedResults, {
|
|
5405
|
+
scope: input.scope,
|
|
5406
|
+
projectScopeMode: input.projectScopeMode,
|
|
5407
|
+
projectHash: input.projectHash,
|
|
5408
|
+
allowedProjectHashes: input.allowedProjectHashes
|
|
5409
|
+
});
|
|
5410
|
+
const top = filtered.slice(0, input.topK);
|
|
5411
|
+
const matchResult = this.matcher.matchSearchResults(top, () => 0);
|
|
5412
|
+
return { results: top, candidateResults: filtered, matchResult };
|
|
5413
|
+
}
|
|
5414
|
+
mergeResults(primary, secondary, limit) {
|
|
5415
|
+
const byId = /* @__PURE__ */ new Map();
|
|
5416
|
+
for (const row of primary)
|
|
5417
|
+
byId.set(row.eventId, row);
|
|
5418
|
+
for (const row of secondary) {
|
|
5419
|
+
const prev = byId.get(row.eventId);
|
|
5420
|
+
if (!prev || row.score > prev.score) {
|
|
5421
|
+
byId.set(row.eventId, row);
|
|
5422
|
+
}
|
|
5423
|
+
}
|
|
5424
|
+
return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, limit);
|
|
5425
|
+
}
|
|
5426
|
+
async expandGraphHops(seeds, opts) {
|
|
5427
|
+
const byId = /* @__PURE__ */ new Map();
|
|
5428
|
+
for (const s of seeds)
|
|
5429
|
+
byId.set(s.eventId, s);
|
|
5430
|
+
let frontier = seeds.map((s) => ({ row: s, hop: 0 }));
|
|
5431
|
+
for (let hop = 1; hop <= opts.maxHops; hop += 1) {
|
|
5432
|
+
const next = [];
|
|
5433
|
+
for (const f of frontier) {
|
|
5434
|
+
const ev = await this.eventStore.getEvent(f.row.eventId);
|
|
5435
|
+
if (!ev)
|
|
5436
|
+
continue;
|
|
5437
|
+
const rel = ev.metadata?.relatedEventIds ?? [];
|
|
5438
|
+
const relatedIds = Array.isArray(rel) ? rel.filter((x) => typeof x === "string") : [];
|
|
5439
|
+
for (const rid of relatedIds) {
|
|
5440
|
+
if (byId.has(rid))
|
|
5441
|
+
continue;
|
|
5442
|
+
const target = await this.eventStore.getEvent(rid);
|
|
5443
|
+
if (!target)
|
|
5444
|
+
continue;
|
|
5445
|
+
const score = Math.max(0, f.row.score - opts.hopPenalty * hop);
|
|
5446
|
+
const row = {
|
|
5447
|
+
id: `hop-${hop}-${rid}`,
|
|
5448
|
+
eventId: target.id,
|
|
5449
|
+
content: target.content,
|
|
5450
|
+
score,
|
|
5451
|
+
sessionId: target.sessionId,
|
|
5452
|
+
eventType: target.eventType,
|
|
5453
|
+
timestamp: target.timestamp.toISOString()
|
|
5454
|
+
};
|
|
5455
|
+
byId.set(row.eventId, row);
|
|
5456
|
+
next.push({ row, hop });
|
|
5457
|
+
if (byId.size >= opts.limit)
|
|
5458
|
+
break;
|
|
4370
5459
|
}
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
`;
|
|
5460
|
+
if (byId.size >= opts.limit)
|
|
5461
|
+
break;
|
|
4374
5462
|
}
|
|
5463
|
+
frontier = next;
|
|
5464
|
+
if (frontier.length === 0 || byId.size >= opts.limit)
|
|
5465
|
+
break;
|
|
4375
5466
|
}
|
|
4376
|
-
return
|
|
5467
|
+
return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, opts.limit);
|
|
5468
|
+
}
|
|
5469
|
+
shouldFallback(matchResult, results) {
|
|
5470
|
+
if (results.length === 0)
|
|
5471
|
+
return true;
|
|
5472
|
+
if (matchResult.confidence === "none")
|
|
5473
|
+
return true;
|
|
5474
|
+
return false;
|
|
5475
|
+
}
|
|
5476
|
+
async buildSummaryFallback(query, topK) {
|
|
5477
|
+
const recent = await this.eventStore.getRecentEvents(Math.max(topK * 6, 20));
|
|
5478
|
+
const q = this.tokenize(query);
|
|
5479
|
+
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) => ({
|
|
5480
|
+
id: `summary-${row.e.id}`,
|
|
5481
|
+
eventId: row.e.id,
|
|
5482
|
+
content: row.e.content,
|
|
5483
|
+
score: Math.max(0.25, 0.6 - idx * 0.05),
|
|
5484
|
+
sessionId: row.e.sessionId,
|
|
5485
|
+
eventType: row.e.eventType,
|
|
5486
|
+
timestamp: row.e.timestamp.toISOString()
|
|
5487
|
+
}));
|
|
5488
|
+
return ranked;
|
|
5489
|
+
}
|
|
5490
|
+
async searchByStrategy(query, input) {
|
|
5491
|
+
const strategy = input.strategy === "auto" ? "deep" : input.strategy;
|
|
5492
|
+
if (strategy === "fast") {
|
|
5493
|
+
const keyword = await this.searchByKeyword(query, {
|
|
5494
|
+
limit: Math.max(5, input.topK * 3),
|
|
5495
|
+
sessionId: input.sessionId
|
|
5496
|
+
});
|
|
5497
|
+
return keyword;
|
|
5498
|
+
}
|
|
5499
|
+
const queryEmbedding = await this.embedder.embed(query);
|
|
5500
|
+
return this.vectorStore.search(queryEmbedding.vector, {
|
|
5501
|
+
limit: Math.max(5, input.topK * 3),
|
|
5502
|
+
minScore: input.minScore,
|
|
5503
|
+
sessionId: input.sessionId
|
|
5504
|
+
});
|
|
5505
|
+
}
|
|
5506
|
+
async searchByKeyword(query, input) {
|
|
5507
|
+
if (this.eventStore.keywordSearch) {
|
|
5508
|
+
const rows = await this.eventStore.keywordSearch(query, input.limit);
|
|
5509
|
+
const filtered2 = input.sessionId ? rows.filter((r) => r.event.sessionId === input.sessionId) : rows;
|
|
5510
|
+
return filtered2.map((row, idx) => ({
|
|
5511
|
+
id: `kw-${row.event.id}`,
|
|
5512
|
+
eventId: row.event.id,
|
|
5513
|
+
content: row.event.content,
|
|
5514
|
+
score: Math.max(0.4, 1 - idx * 0.04),
|
|
5515
|
+
sessionId: row.event.sessionId,
|
|
5516
|
+
eventType: row.event.eventType,
|
|
5517
|
+
timestamp: row.event.timestamp.toISOString()
|
|
5518
|
+
}));
|
|
5519
|
+
}
|
|
5520
|
+
const recent = await this.eventStore.getRecentEvents(input.limit * 4);
|
|
5521
|
+
const tokens = this.tokenize(query);
|
|
5522
|
+
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);
|
|
5523
|
+
return filtered.map((row, idx) => ({
|
|
5524
|
+
id: `kw-fallback-${row.e.id}`,
|
|
5525
|
+
eventId: row.e.id,
|
|
5526
|
+
content: row.e.content,
|
|
5527
|
+
score: Math.max(0.3, 0.9 - idx * 0.05),
|
|
5528
|
+
sessionId: row.e.sessionId,
|
|
5529
|
+
eventType: row.e.eventType,
|
|
5530
|
+
timestamp: row.e.timestamp.toISOString()
|
|
5531
|
+
}));
|
|
5532
|
+
}
|
|
5533
|
+
rerankByKeywordOverlap(results, query, weights, decayPolicy) {
|
|
5534
|
+
const q = this.tokenize(query);
|
|
5535
|
+
const now = Date.now();
|
|
5536
|
+
const sw = Math.max(0, weights?.semantic ?? 0.7);
|
|
5537
|
+
const lw = Math.max(0, weights?.lexical ?? 0.2);
|
|
5538
|
+
const rw = Math.max(0, weights?.recency ?? 0.1);
|
|
5539
|
+
const total = sw + lw + rw || 1;
|
|
5540
|
+
const decayEnabled = decayPolicy?.enabled !== false;
|
|
5541
|
+
const decayWindow = Math.max(1, decayPolicy?.windowDays ?? 30);
|
|
5542
|
+
const decayMaxPenalty = Math.max(0, decayPolicy?.maxPenalty ?? 0.15);
|
|
5543
|
+
return [...results].map((r) => {
|
|
5544
|
+
const overlap = this.keywordOverlap(q, this.tokenize(r.content));
|
|
5545
|
+
const recencyDays = Math.max(0, (now - new Date(r.timestamp).getTime()) / (1e3 * 60 * 60 * 24));
|
|
5546
|
+
const recency = Math.max(0, 1 - recencyDays / decayWindow);
|
|
5547
|
+
let blended = (r.score * sw + overlap * lw + recency * rw) / total;
|
|
5548
|
+
if (decayEnabled && recencyDays > decayWindow && overlap < 0.5) {
|
|
5549
|
+
const ageFactor = Math.min(1, (recencyDays - decayWindow) / decayWindow);
|
|
5550
|
+
blended -= decayMaxPenalty * ageFactor;
|
|
5551
|
+
}
|
|
5552
|
+
return { ...r, score: Math.max(0, blended), semanticScore: r.score, lexicalScore: overlap, recencyScore: recency };
|
|
5553
|
+
}).sort((a, b) => b.score - a.score);
|
|
5554
|
+
}
|
|
5555
|
+
async applyScopeFilters(results, options) {
|
|
5556
|
+
const scope = options?.scope;
|
|
5557
|
+
const projectScopeMode = options?.projectScopeMode ?? "global";
|
|
5558
|
+
const allowedProjectHashes = new Set(
|
|
5559
|
+
[options?.projectHash, ...options?.allowedProjectHashes || []].filter(
|
|
5560
|
+
(value) => typeof value === "string" && value.length > 0
|
|
5561
|
+
)
|
|
5562
|
+
);
|
|
5563
|
+
if (!scope && projectScopeMode === "global")
|
|
5564
|
+
return results;
|
|
5565
|
+
const normalizedIncludes = (scope?.contentIncludes || []).map((s) => s.toLowerCase());
|
|
5566
|
+
const filtered = [];
|
|
5567
|
+
for (const result of results) {
|
|
5568
|
+
if (scope?.sessionId && result.sessionId !== scope.sessionId)
|
|
5569
|
+
continue;
|
|
5570
|
+
if (scope?.sessionIdPrefix && !result.sessionId.startsWith(scope.sessionIdPrefix))
|
|
5571
|
+
continue;
|
|
5572
|
+
if (scope?.eventTypes && scope.eventTypes.length > 0 && !scope.eventTypes.includes(result.eventType))
|
|
5573
|
+
continue;
|
|
5574
|
+
const event = await this.eventStore.getEvent(result.eventId);
|
|
5575
|
+
if (!event)
|
|
5576
|
+
continue;
|
|
5577
|
+
if (scope?.canonicalKeyPrefix && !event.canonicalKey.startsWith(scope.canonicalKeyPrefix))
|
|
5578
|
+
continue;
|
|
5579
|
+
if (normalizedIncludes.length > 0) {
|
|
5580
|
+
const lc = event.content.toLowerCase();
|
|
5581
|
+
if (!normalizedIncludes.some((needle) => lc.includes(needle)))
|
|
5582
|
+
continue;
|
|
5583
|
+
}
|
|
5584
|
+
if (scope?.metadata && !this.matchesMetadataScope(event.metadata, scope.metadata))
|
|
5585
|
+
continue;
|
|
5586
|
+
const projectHash = this.extractProjectHash(event.metadata);
|
|
5587
|
+
filtered.push({ result, projectHash });
|
|
5588
|
+
}
|
|
5589
|
+
if (projectScopeMode === "global" || allowedProjectHashes.size === 0) {
|
|
5590
|
+
return filtered.map((x) => x.result);
|
|
5591
|
+
}
|
|
5592
|
+
const projectMatched = filtered.filter((x) => x.projectHash && allowedProjectHashes.has(x.projectHash));
|
|
5593
|
+
if (projectScopeMode === "strict") {
|
|
5594
|
+
return projectMatched.map((x) => x.result);
|
|
5595
|
+
}
|
|
5596
|
+
return (projectMatched.length > 0 ? projectMatched : filtered).map((x) => x.result);
|
|
5597
|
+
}
|
|
5598
|
+
extractProjectHash(metadata) {
|
|
5599
|
+
if (!metadata || typeof metadata !== "object")
|
|
5600
|
+
return void 0;
|
|
5601
|
+
const scope = metadata.scope;
|
|
5602
|
+
if (!scope || typeof scope !== "object")
|
|
5603
|
+
return void 0;
|
|
5604
|
+
const project = scope.project;
|
|
5605
|
+
if (!project || typeof project !== "object")
|
|
5606
|
+
return void 0;
|
|
5607
|
+
const hash = project.hash;
|
|
5608
|
+
return typeof hash === "string" && hash.length > 0 ? hash : void 0;
|
|
4377
5609
|
}
|
|
4378
|
-
/**
|
|
4379
|
-
* Retrieve memories from a specific session
|
|
4380
|
-
*/
|
|
4381
5610
|
async retrieveFromSession(sessionId) {
|
|
4382
5611
|
return this.eventStore.getSessionEvents(sessionId);
|
|
4383
5612
|
}
|
|
4384
|
-
/**
|
|
4385
|
-
* Get recent memories across all sessions
|
|
4386
|
-
*/
|
|
4387
5613
|
async retrieveRecent(limit = 100) {
|
|
4388
5614
|
return this.eventStore.getRecentEvents(limit);
|
|
4389
5615
|
}
|
|
4390
|
-
/**
|
|
4391
|
-
* Enrich search results with full event data
|
|
4392
|
-
*/
|
|
4393
5616
|
async enrichResults(results, options) {
|
|
4394
5617
|
const memories = [];
|
|
4395
5618
|
for (const result of results) {
|
|
@@ -4397,27 +5620,16 @@ var Retriever = class {
|
|
|
4397
5620
|
if (!event)
|
|
4398
5621
|
continue;
|
|
4399
5622
|
if (this.graduation) {
|
|
4400
|
-
this.graduation.recordAccess(
|
|
4401
|
-
event.id,
|
|
4402
|
-
options.sessionId || "unknown",
|
|
4403
|
-
result.score
|
|
4404
|
-
);
|
|
5623
|
+
this.graduation.recordAccess(event.id, options.sessionId || "unknown", result.score);
|
|
4405
5624
|
}
|
|
4406
5625
|
let sessionContext;
|
|
4407
5626
|
if (options.includeSessionContext) {
|
|
4408
5627
|
sessionContext = await this.getSessionContext(event.sessionId, event.id);
|
|
4409
5628
|
}
|
|
4410
|
-
memories.push({
|
|
4411
|
-
event,
|
|
4412
|
-
score: result.score,
|
|
4413
|
-
sessionContext
|
|
4414
|
-
});
|
|
5629
|
+
memories.push({ event, score: result.score, sessionContext });
|
|
4415
5630
|
}
|
|
4416
5631
|
return memories;
|
|
4417
5632
|
}
|
|
4418
|
-
/**
|
|
4419
|
-
* Get surrounding context from the same session
|
|
4420
|
-
*/
|
|
4421
5633
|
async getSessionContext(sessionId, eventId) {
|
|
4422
5634
|
const sessionEvents = await this.eventStore.getSessionEvents(sessionId);
|
|
4423
5635
|
const eventIndex = sessionEvents.findIndex((e) => e.id === eventId);
|
|
@@ -4430,55 +5642,86 @@ var Retriever = class {
|
|
|
4430
5642
|
return void 0;
|
|
4431
5643
|
return contextEvents.filter((e) => e.id !== eventId).map((e) => `[${e.eventType}]: ${e.content.slice(0, 200)}...`).join("\n");
|
|
4432
5644
|
}
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
5645
|
+
buildUnifiedContext(projectResult, sharedMemories) {
|
|
5646
|
+
let context = projectResult.context;
|
|
5647
|
+
if (sharedMemories.length === 0)
|
|
5648
|
+
return context;
|
|
5649
|
+
context += "\n\n## Cross-Project Knowledge\n\n";
|
|
5650
|
+
for (const memory of sharedMemories.slice(0, 3)) {
|
|
5651
|
+
context += `### ${memory.title}
|
|
5652
|
+
`;
|
|
5653
|
+
if (memory.symptoms.length > 0)
|
|
5654
|
+
context += `**Symptoms:** ${memory.symptoms.join(", ")}
|
|
5655
|
+
`;
|
|
5656
|
+
context += `**Root Cause:** ${memory.rootCause}
|
|
5657
|
+
`;
|
|
5658
|
+
context += `**Solution:** ${memory.solution}
|
|
5659
|
+
`;
|
|
5660
|
+
if (memory.technologies && memory.technologies.length > 0)
|
|
5661
|
+
context += `**Technologies:** ${memory.technologies.join(", ")}
|
|
5662
|
+
`;
|
|
5663
|
+
context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
|
|
5664
|
+
|
|
5665
|
+
`;
|
|
5666
|
+
}
|
|
5667
|
+
return context;
|
|
5668
|
+
}
|
|
4436
5669
|
buildContext(memories, maxTokens) {
|
|
4437
5670
|
const parts = [];
|
|
4438
5671
|
let currentTokens = 0;
|
|
4439
5672
|
for (const memory of memories) {
|
|
4440
5673
|
const memoryText = this.formatMemory(memory);
|
|
4441
5674
|
const memoryTokens = this.estimateTokens(memoryText);
|
|
4442
|
-
if (currentTokens + memoryTokens > maxTokens)
|
|
5675
|
+
if (currentTokens + memoryTokens > maxTokens)
|
|
4443
5676
|
break;
|
|
4444
|
-
}
|
|
4445
5677
|
parts.push(memoryText);
|
|
4446
5678
|
currentTokens += memoryTokens;
|
|
4447
5679
|
}
|
|
4448
|
-
if (parts.length === 0)
|
|
5680
|
+
if (parts.length === 0)
|
|
4449
5681
|
return "";
|
|
4450
|
-
}
|
|
4451
5682
|
return `## Relevant Memories
|
|
4452
5683
|
|
|
4453
5684
|
${parts.join("\n\n---\n\n")}`;
|
|
4454
5685
|
}
|
|
4455
|
-
/**
|
|
4456
|
-
* Format a single memory for context
|
|
4457
|
-
*/
|
|
4458
5686
|
formatMemory(memory) {
|
|
4459
5687
|
const { event, score, sessionContext } = memory;
|
|
4460
5688
|
const date = event.timestamp.toISOString().split("T")[0];
|
|
4461
5689
|
let text = `**${event.eventType}** (${date}, score: ${score.toFixed(2)})
|
|
4462
5690
|
${event.content}`;
|
|
4463
|
-
if (sessionContext)
|
|
5691
|
+
if (sessionContext)
|
|
4464
5692
|
text += `
|
|
4465
5693
|
|
|
4466
5694
|
_Context:_ ${sessionContext}`;
|
|
4467
|
-
}
|
|
4468
5695
|
return text;
|
|
4469
5696
|
}
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
5697
|
+
matchesMetadataScope(metadata, expected) {
|
|
5698
|
+
if (!metadata)
|
|
5699
|
+
return false;
|
|
5700
|
+
return Object.entries(expected).every(([path2, value]) => {
|
|
5701
|
+
const actual = path2.split(".").reduce((acc, key) => {
|
|
5702
|
+
if (typeof acc !== "object" || acc === null)
|
|
5703
|
+
return void 0;
|
|
5704
|
+
return acc[key];
|
|
5705
|
+
}, metadata);
|
|
5706
|
+
return actual === value;
|
|
5707
|
+
});
|
|
5708
|
+
}
|
|
5709
|
+
tokenize(text) {
|
|
5710
|
+
return text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter((t) => t.length >= 2).slice(0, 64);
|
|
5711
|
+
}
|
|
5712
|
+
keywordOverlap(a, b) {
|
|
5713
|
+
if (a.length === 0 || b.length === 0)
|
|
5714
|
+
return 0;
|
|
5715
|
+
const bs = new Set(b);
|
|
5716
|
+
let hit = 0;
|
|
5717
|
+
for (const t of a)
|
|
5718
|
+
if (bs.has(t))
|
|
5719
|
+
hit += 1;
|
|
5720
|
+
return hit / a.length;
|
|
5721
|
+
}
|
|
4473
5722
|
estimateTokens(text) {
|
|
4474
5723
|
return Math.ceil(text.length / 4);
|
|
4475
5724
|
}
|
|
4476
|
-
/**
|
|
4477
|
-
* Get event age in days (for recency scoring)
|
|
4478
|
-
*/
|
|
4479
|
-
getEventAgeDays(eventId) {
|
|
4480
|
-
return 0;
|
|
4481
|
-
}
|
|
4482
5725
|
};
|
|
4483
5726
|
function createRetriever(eventStore, vectorStore, embedder, matcher) {
|
|
4484
5727
|
return new Retriever(eventStore, vectorStore, embedder, matcher);
|
|
@@ -5043,7 +6286,7 @@ var TaskMatcher = class {
|
|
|
5043
6286
|
};
|
|
5044
6287
|
|
|
5045
6288
|
// src/core/task/blocker-resolver.ts
|
|
5046
|
-
import { randomUUID as
|
|
6289
|
+
import { randomUUID as randomUUID7 } from "crypto";
|
|
5047
6290
|
var URL_PATTERN = /^https?:\/\/.+/;
|
|
5048
6291
|
var JIRA_PATTERN = /^[A-Z]+-\d+$/;
|
|
5049
6292
|
var GITHUB_ISSUE_PATTERN = /^[^\/]+\/[^#]+#\d+$/;
|
|
@@ -5180,7 +6423,7 @@ var BlockerResolver = class {
|
|
|
5180
6423
|
* Declare a new condition entity
|
|
5181
6424
|
*/
|
|
5182
6425
|
async declareCondition(text, canonicalKey, candidates) {
|
|
5183
|
-
const entityId =
|
|
6426
|
+
const entityId = randomUUID7();
|
|
5184
6427
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5185
6428
|
const currentJson = {
|
|
5186
6429
|
text,
|
|
@@ -5223,7 +6466,7 @@ var BlockerResolver = class {
|
|
|
5223
6466
|
* Declare a new artifact entity
|
|
5224
6467
|
*/
|
|
5225
6468
|
async declareArtifact(identifier, canonicalKey) {
|
|
5226
|
-
const entityId =
|
|
6469
|
+
const entityId = randomUUID7();
|
|
5227
6470
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5228
6471
|
let artifactType = "generic";
|
|
5229
6472
|
if (URL_PATTERN.test(identifier)) {
|
|
@@ -5288,7 +6531,7 @@ var BlockerResolver = class {
|
|
|
5288
6531
|
};
|
|
5289
6532
|
|
|
5290
6533
|
// src/core/task/task-resolver.ts
|
|
5291
|
-
import { randomUUID as
|
|
6534
|
+
import { randomUUID as randomUUID8 } from "crypto";
|
|
5292
6535
|
var VALID_TRANSITIONS = {
|
|
5293
6536
|
pending: ["in_progress", "cancelled"],
|
|
5294
6537
|
in_progress: ["blocked", "done", "cancelled"],
|
|
@@ -5363,7 +6606,7 @@ var TaskResolver = class {
|
|
|
5363
6606
|
isNew: false
|
|
5364
6607
|
};
|
|
5365
6608
|
}
|
|
5366
|
-
const taskId =
|
|
6609
|
+
const taskId = randomUUID8();
|
|
5367
6610
|
const canonicalKey = makeEntityCanonicalKey("task", extracted.title, {
|
|
5368
6611
|
project: extracted.project
|
|
5369
6612
|
});
|
|
@@ -5516,7 +6759,7 @@ var TaskResolver = class {
|
|
|
5516
6759
|
* Emit task event to events table
|
|
5517
6760
|
*/
|
|
5518
6761
|
async emitTaskEvent(eventType, payload) {
|
|
5519
|
-
const eventId =
|
|
6762
|
+
const eventId = randomUUID8();
|
|
5520
6763
|
const now = /* @__PURE__ */ new Date();
|
|
5521
6764
|
const dedupeKey = makeTaskEventDedupeKey(
|
|
5522
6765
|
eventType,
|
|
@@ -5574,14 +6817,14 @@ var TaskResolver = class {
|
|
|
5574
6817
|
`INSERT INTO edges (edge_id, src_type, src_id, rel_type, dst_type, dst_id, meta_json)
|
|
5575
6818
|
VALUES (?, 'entity', ?, 'resolves_to', 'entity', ?, ?)
|
|
5576
6819
|
ON CONFLICT DO NOTHING`,
|
|
5577
|
-
[
|
|
6820
|
+
[randomUUID8(), conditionId, taskId, JSON.stringify({ resolved_at: (/* @__PURE__ */ new Date()).toISOString() })]
|
|
5578
6821
|
);
|
|
5579
6822
|
return eventId;
|
|
5580
6823
|
}
|
|
5581
6824
|
};
|
|
5582
6825
|
|
|
5583
6826
|
// src/core/task/task-projector.ts
|
|
5584
|
-
import { randomUUID as
|
|
6827
|
+
import { randomUUID as randomUUID9 } from "crypto";
|
|
5585
6828
|
var PROJECTOR_NAME = "task_projector";
|
|
5586
6829
|
var TASK_EVENT_TYPES = [
|
|
5587
6830
|
"task_created",
|
|
@@ -5777,7 +7020,7 @@ var TaskProjector = class {
|
|
|
5777
7020
|
* Create blocker edge
|
|
5778
7021
|
*/
|
|
5779
7022
|
async createBlockerEdge(taskId, blocker, relType) {
|
|
5780
|
-
const edgeId =
|
|
7023
|
+
const edgeId = randomUUID9();
|
|
5781
7024
|
await dbRun(
|
|
5782
7025
|
this.db,
|
|
5783
7026
|
`INSERT INTO edges (edge_id, src_type, src_id, rel_type, dst_type, dst_id, meta_json, created_at)
|
|
@@ -5815,7 +7058,7 @@ var TaskProjector = class {
|
|
|
5815
7058
|
* Enqueue entity for vectorization
|
|
5816
7059
|
*/
|
|
5817
7060
|
async enqueueForVectorization(itemId, itemKind) {
|
|
5818
|
-
const jobId =
|
|
7061
|
+
const jobId = randomUUID9();
|
|
5819
7062
|
const embeddingVersion = "v1";
|
|
5820
7063
|
await dbRun(
|
|
5821
7064
|
this.db,
|
|
@@ -5935,7 +7178,7 @@ function createSharedEventStore(dbPath) {
|
|
|
5935
7178
|
}
|
|
5936
7179
|
|
|
5937
7180
|
// src/core/shared-store.ts
|
|
5938
|
-
import { randomUUID as
|
|
7181
|
+
import { randomUUID as randomUUID10 } from "crypto";
|
|
5939
7182
|
var SharedStore = class {
|
|
5940
7183
|
constructor(sharedEventStore) {
|
|
5941
7184
|
this.sharedEventStore = sharedEventStore;
|
|
@@ -5947,7 +7190,7 @@ var SharedStore = class {
|
|
|
5947
7190
|
* Promote a verified troubleshooting entry to shared storage
|
|
5948
7191
|
*/
|
|
5949
7192
|
async promoteEntry(input) {
|
|
5950
|
-
const entryId =
|
|
7193
|
+
const entryId = randomUUID10();
|
|
5951
7194
|
await dbRun(
|
|
5952
7195
|
this.db,
|
|
5953
7196
|
`INSERT INTO shared_troubleshooting (
|
|
@@ -6312,7 +7555,7 @@ function createSharedVectorStore(dbPath) {
|
|
|
6312
7555
|
}
|
|
6313
7556
|
|
|
6314
7557
|
// src/core/shared-promoter.ts
|
|
6315
|
-
import { randomUUID as
|
|
7558
|
+
import { randomUUID as randomUUID11 } from "crypto";
|
|
6316
7559
|
var SharedPromoter = class {
|
|
6317
7560
|
constructor(sharedStore, sharedVectorStore, embedder, config) {
|
|
6318
7561
|
this.sharedStore = sharedStore;
|
|
@@ -6380,7 +7623,7 @@ var SharedPromoter = class {
|
|
|
6380
7623
|
const embeddingContent = this.createEmbeddingContent(input);
|
|
6381
7624
|
const embedding = await this.embedder.embed(embeddingContent);
|
|
6382
7625
|
await this.sharedVectorStore.upsert({
|
|
6383
|
-
id:
|
|
7626
|
+
id: randomUUID11(),
|
|
6384
7627
|
entryId,
|
|
6385
7628
|
entryType: "troubleshooting",
|
|
6386
7629
|
content: embeddingContent,
|
|
@@ -6505,6 +7748,7 @@ export {
|
|
|
6505
7748
|
CitationUsageSchema,
|
|
6506
7749
|
ConfigSchema,
|
|
6507
7750
|
ConsolidatedMemorySchema,
|
|
7751
|
+
ConsolidationRuleSchema,
|
|
6508
7752
|
ContinuityLogSchema,
|
|
6509
7753
|
DefaultContentProvider,
|
|
6510
7754
|
EdgeRepo,
|
|
@@ -6541,6 +7785,7 @@ export {
|
|
|
6541
7785
|
MemoryLevelSchema,
|
|
6542
7786
|
MemoryMatchSchema,
|
|
6543
7787
|
MemoryModeSchema,
|
|
7788
|
+
MongoSyncWorker,
|
|
6544
7789
|
NodeTypeSchema,
|
|
6545
7790
|
OutboxItemKindSchema,
|
|
6546
7791
|
OutboxJobSchema,
|