@tekmidian/pai 0.8.5 → 0.9.0

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.
@@ -1,8 +1,10 @@
1
1
  import { t as __exportAll } from "./rolldown-runtime-95iHPtFO.mjs";
2
+ import { t as STOP_WORDS } from "./stop-words-BaMEGVeY.mjs";
2
3
  import { i as searchMemoryHybrid, n as populateSlugs } from "./search-DC1qhkKn.mjs";
3
4
  import { r as formatDetectionJson, t as detectProject } from "./detect-CdaA48EI.mjs";
4
- import { existsSync, readFileSync, statSync } from "node:fs";
5
- import { isAbsolute, join, resolve } from "node:path";
5
+ import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
6
+ import { homedir } from "node:os";
7
+ import { basename, isAbsolute, join, resolve } from "node:path";
6
8
 
7
9
  //#region src/mcp/tools/types.ts
8
10
  /**
@@ -906,6 +908,790 @@ async function toolZettelCommunities(backend, params) {
906
908
  }
907
909
  }
908
910
 
911
+ //#endregion
912
+ //#region src/memory/kg.ts
913
+ function rowToTriple(row) {
914
+ return {
915
+ id: row.id,
916
+ subject: row.subject,
917
+ predicate: row.predicate,
918
+ object: row.object,
919
+ project_id: row.project_id,
920
+ source_session: row.source_session,
921
+ valid_from: new Date(row.valid_from),
922
+ valid_to: row.valid_to ? new Date(row.valid_to) : void 0,
923
+ confidence: row.confidence,
924
+ created_at: new Date(row.created_at)
925
+ };
926
+ }
927
+ /**
928
+ * Add a new triple to the knowledge graph.
929
+ * Returns the inserted triple.
930
+ */
931
+ async function kgAdd(pool, params) {
932
+ const confidence = params.confidence ?? "EXTRACTED";
933
+ return rowToTriple((await pool.query(`INSERT INTO kg_triples
934
+ (subject, predicate, object, project_id, source_session, confidence)
935
+ VALUES ($1, $2, $3, $4, $5, $6)
936
+ RETURNING *`, [
937
+ params.subject,
938
+ params.predicate,
939
+ params.object,
940
+ params.project_id ?? null,
941
+ params.source_session ?? null,
942
+ confidence
943
+ ])).rows[0]);
944
+ }
945
+ /**
946
+ * Query triples by subject, predicate, object, and/or project.
947
+ * Supports point-in-time queries via as_of.
948
+ * By default only returns currently-valid triples (valid_to IS NULL).
949
+ */
950
+ async function kgQuery(pool, params) {
951
+ const conditions = [];
952
+ const values = [];
953
+ let idx = 1;
954
+ if (params.subject !== void 0) {
955
+ conditions.push(`subject = $${idx++}`);
956
+ values.push(params.subject);
957
+ }
958
+ if (params.predicate !== void 0) {
959
+ conditions.push(`predicate = $${idx++}`);
960
+ values.push(params.predicate);
961
+ }
962
+ if (params.object !== void 0) {
963
+ conditions.push(`object = $${idx++}`);
964
+ values.push(params.object);
965
+ }
966
+ if (params.project_id !== void 0) {
967
+ conditions.push(`project_id = $${idx++}`);
968
+ values.push(params.project_id);
969
+ }
970
+ if (params.as_of !== void 0) {
971
+ conditions.push(`valid_from <= $${idx++}`);
972
+ values.push(params.as_of);
973
+ conditions.push(`(valid_to IS NULL OR valid_to > $${idx++})`);
974
+ values.push(params.as_of);
975
+ } else if (!params.include_invalidated) conditions.push(`valid_to IS NULL`);
976
+ const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
977
+ return (await pool.query(`SELECT * FROM kg_triples ${where} ORDER BY valid_from DESC`, values)).rows.map(rowToTriple);
978
+ }
979
+ /**
980
+ * Invalidate a triple by setting valid_to = NOW().
981
+ * Does not delete the row — preserves history.
982
+ */
983
+ async function kgInvalidate(pool, tripleId) {
984
+ await pool.query(`UPDATE kg_triples SET valid_to = NOW() WHERE id = $1 AND valid_to IS NULL`, [tripleId]);
985
+ }
986
+ /**
987
+ * Find contradictions: cases where the same (subject, predicate) pair has
988
+ * multiple currently-valid objects.
989
+ */
990
+ async function kgContradictions(pool, subject) {
991
+ return (await pool.query(`SELECT subject, predicate, array_agg(object ORDER BY object) AS objects
992
+ FROM kg_triples
993
+ WHERE subject = $1
994
+ AND valid_to IS NULL
995
+ GROUP BY subject, predicate
996
+ HAVING COUNT(*) > 1`, [subject])).rows.map((row) => ({
997
+ subject: row.subject,
998
+ predicate: row.predicate,
999
+ objects: row.objects
1000
+ }));
1001
+ }
1002
+
1003
+ //#endregion
1004
+ //#region src/mcp/tools/kg.ts
1005
+ async function toolKgAdd(pool, params) {
1006
+ try {
1007
+ if (!params.subject || !params.predicate || !params.object) return {
1008
+ content: [{
1009
+ type: "text",
1010
+ text: "kg_add error: subject, predicate, and object are required"
1011
+ }],
1012
+ isError: true
1013
+ };
1014
+ const triple = await kgAdd(pool, params);
1015
+ return { content: [{
1016
+ type: "text",
1017
+ text: JSON.stringify(triple, null, 2)
1018
+ }] };
1019
+ } catch (e) {
1020
+ return {
1021
+ content: [{
1022
+ type: "text",
1023
+ text: `kg_add error: ${String(e)}`
1024
+ }],
1025
+ isError: true
1026
+ };
1027
+ }
1028
+ }
1029
+ async function toolKgQuery(pool, params) {
1030
+ try {
1031
+ const asOf = params.as_of ? new Date(params.as_of) : void 0;
1032
+ if (asOf && isNaN(asOf.getTime())) return {
1033
+ content: [{
1034
+ type: "text",
1035
+ text: `kg_query error: invalid as_of date: ${params.as_of}`
1036
+ }],
1037
+ isError: true
1038
+ };
1039
+ const triples = await kgQuery(pool, {
1040
+ subject: params.subject,
1041
+ predicate: params.predicate,
1042
+ object: params.object,
1043
+ project_id: params.project_id,
1044
+ as_of: asOf,
1045
+ include_invalidated: params.include_invalidated
1046
+ });
1047
+ return { content: [{
1048
+ type: "text",
1049
+ text: JSON.stringify(triples, null, 2)
1050
+ }] };
1051
+ } catch (e) {
1052
+ return {
1053
+ content: [{
1054
+ type: "text",
1055
+ text: `kg_query error: ${String(e)}`
1056
+ }],
1057
+ isError: true
1058
+ };
1059
+ }
1060
+ }
1061
+ async function toolKgInvalidate(pool, params) {
1062
+ try {
1063
+ if (params.triple_id === void 0 || params.triple_id === null) return {
1064
+ content: [{
1065
+ type: "text",
1066
+ text: "kg_invalidate error: triple_id is required"
1067
+ }],
1068
+ isError: true
1069
+ };
1070
+ await kgInvalidate(pool, params.triple_id);
1071
+ return { content: [{
1072
+ type: "text",
1073
+ text: JSON.stringify({
1074
+ invalidated: true,
1075
+ triple_id: params.triple_id
1076
+ })
1077
+ }] };
1078
+ } catch (e) {
1079
+ return {
1080
+ content: [{
1081
+ type: "text",
1082
+ text: `kg_invalidate error: ${String(e)}`
1083
+ }],
1084
+ isError: true
1085
+ };
1086
+ }
1087
+ }
1088
+ async function toolKgContradictions(pool, params) {
1089
+ try {
1090
+ if (!params.subject) return {
1091
+ content: [{
1092
+ type: "text",
1093
+ text: "kg_contradictions error: subject is required"
1094
+ }],
1095
+ isError: true
1096
+ };
1097
+ const contradictions = await kgContradictions(pool, params.subject);
1098
+ return { content: [{
1099
+ type: "text",
1100
+ text: JSON.stringify(contradictions, null, 2)
1101
+ }] };
1102
+ } catch (e) {
1103
+ return {
1104
+ content: [{
1105
+ type: "text",
1106
+ text: `kg_contradictions error: ${String(e)}`
1107
+ }],
1108
+ isError: true
1109
+ };
1110
+ }
1111
+ }
1112
+
1113
+ //#endregion
1114
+ //#region src/memory/wakeup.ts
1115
+ /**
1116
+ * Wake-up context system — progressive context loading inspired by mempalace.
1117
+ *
1118
+ * Layers:
1119
+ * L0 Identity (~100 tokens) — user identity from ~/.pai/identity.txt. Always loaded.
1120
+ * L1 Essential Story (~500-800t) — top session notes for the project, key lines extracted.
1121
+ * L2 On-Demand — triggered by topic queries (handled by memory_search).
1122
+ * L3 Deep Search — unlimited federated memory search (memory_search tool).
1123
+ */
1124
+ /** Maximum tokens for the L1 essential story block. Approx 4 chars/token. */
1125
+ const L1_TOKEN_BUDGET = 800;
1126
+ L1_TOKEN_BUDGET * 4;
1127
+ /** Maximum session notes to scan when building L1. */
1128
+ const L1_MAX_NOTES = 10;
1129
+ /** Sections to extract from session notes (in priority order). */
1130
+ const EXTRACT_SECTIONS = [
1131
+ "Work Done",
1132
+ "Key Decisions",
1133
+ "Next Steps",
1134
+ "Checkpoint"
1135
+ ];
1136
+ /** Identity file location. */
1137
+ const IDENTITY_FILE = join(homedir(), ".pai", "identity.txt");
1138
+ /**
1139
+ * Load L0 identity from ~/.pai/identity.txt.
1140
+ * Returns the file content, or an empty string if the file does not exist.
1141
+ * Never throws.
1142
+ */
1143
+ function loadL0Identity() {
1144
+ if (!existsSync(IDENTITY_FILE)) return "";
1145
+ try {
1146
+ return readFileSync(IDENTITY_FILE, "utf-8").trim();
1147
+ } catch {
1148
+ return "";
1149
+ }
1150
+ }
1151
+ /**
1152
+ * Find the Notes directory for a project given its root_path from the registry.
1153
+ * Checks local Notes/ first, then central ~/.claude/projects/... path.
1154
+ */
1155
+ function findNotesDirForProject(rootPath) {
1156
+ const localCandidates = [
1157
+ join(rootPath, "Notes"),
1158
+ join(rootPath, "notes"),
1159
+ join(rootPath, ".claude", "Notes")
1160
+ ];
1161
+ for (const p of localCandidates) if (existsSync(p)) return p;
1162
+ const encoded = rootPath.replace(/\//g, "-").replace(/\./g, "-").replace(/ /g, "-");
1163
+ const centralNotes = join(homedir(), ".claude", "projects", encoded, "Notes");
1164
+ if (existsSync(centralNotes)) return centralNotes;
1165
+ return null;
1166
+ }
1167
+ /**
1168
+ * Recursively find all .md session note files in a Notes directory.
1169
+ * Handles both flat layout (Notes/*.md) and month-subdirectory layout
1170
+ * (Notes/YYYY/MM/*.md). Returns files sorted newest-first by filename
1171
+ * (note numbers are monotonically increasing, so lexicographic = newest-last,
1172
+ * so we reverse).
1173
+ */
1174
+ function findSessionNotes(notesDir) {
1175
+ const result = [];
1176
+ const scanDir = (dir) => {
1177
+ if (!existsSync(dir)) return;
1178
+ let entries;
1179
+ try {
1180
+ entries = readdirSync(dir, { withFileTypes: true }).map((e) => ({
1181
+ name: e.name,
1182
+ isDir: e.isDirectory()
1183
+ }));
1184
+ } catch {
1185
+ return;
1186
+ }
1187
+ for (const entry of entries) {
1188
+ const fullPath = join(dir, entry.name);
1189
+ if (entry.isDir) scanDir(fullPath);
1190
+ else if (entry.name.match(/^\d{3,4}[\s_-].*\.md$/)) result.push(fullPath);
1191
+ }
1192
+ };
1193
+ scanDir(notesDir);
1194
+ result.sort((a, b) => {
1195
+ const numA = parseInt(basename(a).match(/^(\d+)/)?.[1] ?? "0", 10);
1196
+ return parseInt(basename(b).match(/^(\d+)/)?.[1] ?? "0", 10) - numA;
1197
+ });
1198
+ return result;
1199
+ }
1200
+ /**
1201
+ * Extract the most important lines from a session note.
1202
+ * Prioritises: Work Done items, Key Decisions, Next Steps, Checkpoint headings.
1203
+ * Returns a condensed string under maxChars.
1204
+ */
1205
+ function extractKeyLines(content, maxChars) {
1206
+ const lines = content.split("\n");
1207
+ const selected = [];
1208
+ let inTargetSection = false;
1209
+ let currentSection = "";
1210
+ let charCount = 0;
1211
+ for (const line of lines) {
1212
+ const h2Match = line.match(/^## (.+)$/);
1213
+ const h3Match = line.match(/^### (.+)$/);
1214
+ if (h2Match) {
1215
+ currentSection = h2Match[1];
1216
+ inTargetSection = EXTRACT_SECTIONS.some((s) => currentSection.toLowerCase().includes(s.toLowerCase()));
1217
+ continue;
1218
+ }
1219
+ if (h3Match) {
1220
+ if (inTargetSection) {
1221
+ const label = `[${h3Match[1]}]`;
1222
+ if (charCount + label.length < maxChars) {
1223
+ selected.push(label);
1224
+ charCount += label.length + 1;
1225
+ }
1226
+ }
1227
+ continue;
1228
+ }
1229
+ if (!inTargetSection) continue;
1230
+ const trimmed = line.trim();
1231
+ if (!trimmed || trimmed.startsWith("<!--") || trimmed === "---") continue;
1232
+ if (trimmed.startsWith("- ") || trimmed.startsWith("* ") || trimmed.match(/^\d+\./) || trimmed.startsWith("**")) {
1233
+ if (charCount + trimmed.length + 1 > maxChars) break;
1234
+ selected.push(trimmed);
1235
+ charCount += trimmed.length + 1;
1236
+ }
1237
+ }
1238
+ return selected.join("\n");
1239
+ }
1240
+ /**
1241
+ * Build the L1 essential story block.
1242
+ *
1243
+ * Reads the most recent session notes for the project and extracts the key
1244
+ * lines (Work Done, Key Decisions, Next Steps) within the token budget.
1245
+ *
1246
+ * @param rootPath The project root path (from the registry).
1247
+ * @param tokenBudget Max tokens to consume. Default 800 (~3200 chars).
1248
+ * @returns Formatted L1 block, or empty string if no notes found.
1249
+ */
1250
+ function buildL1EssentialStory(rootPath, tokenBudget = L1_TOKEN_BUDGET) {
1251
+ const charBudget = tokenBudget * 4;
1252
+ const notesDir = findNotesDirForProject(rootPath);
1253
+ if (!notesDir) return "";
1254
+ const noteFiles = findSessionNotes(notesDir).slice(0, L1_MAX_NOTES);
1255
+ if (noteFiles.length === 0) return "";
1256
+ const sections = [];
1257
+ let remaining = charBudget;
1258
+ for (const noteFile of noteFiles) {
1259
+ if (remaining <= 50) break;
1260
+ let content;
1261
+ try {
1262
+ content = readFileSync(noteFile, "utf-8");
1263
+ } catch {
1264
+ continue;
1265
+ }
1266
+ const name = basename(noteFile);
1267
+ const titleMatch = name.match(/^\d+ - (\d{4}-\d{2}-\d{2}) - (.+)\.md$/);
1268
+ const dateLabel = titleMatch ? titleMatch[1] : "";
1269
+ const titleLabel = titleMatch ? titleMatch[2] : name.replace(/^\d+ - /, "").replace(/\.md$/, "");
1270
+ const perNoteChars = Math.min(remaining, Math.floor(charBudget / noteFiles.length) + 200);
1271
+ const extracted = extractKeyLines(content, perNoteChars);
1272
+ if (!extracted) continue;
1273
+ const noteBlock = `[${dateLabel} - ${titleLabel}]\n${extracted}`;
1274
+ sections.push(noteBlock);
1275
+ remaining -= noteBlock.length + 1;
1276
+ }
1277
+ if (sections.length === 0) return "";
1278
+ return sections.join("\n\n");
1279
+ }
1280
+ /**
1281
+ * Build the combined wake-up context block (L0 + L1).
1282
+ *
1283
+ * Returns a formatted string suitable for injection as a system-reminder,
1284
+ * or an empty string if both layers are empty.
1285
+ *
1286
+ * @param rootPath Project root path for L1 note lookup. Optional.
1287
+ * @param tokenBudget L1 token budget. Default 800.
1288
+ */
1289
+ function buildWakeupContext(rootPath, tokenBudget = L1_TOKEN_BUDGET) {
1290
+ const identity = loadL0Identity();
1291
+ const essentialStory = rootPath ? buildL1EssentialStory(rootPath, tokenBudget) : "";
1292
+ if (!identity && !essentialStory) return "";
1293
+ const parts = [];
1294
+ if (identity) parts.push(`## L0 Identity\n\n${identity}`);
1295
+ if (essentialStory) parts.push(`## L1 Essential Story\n\n${essentialStory}`);
1296
+ return parts.join("\n\n");
1297
+ }
1298
+
1299
+ //#endregion
1300
+ //#region src/mcp/tools/wakeup.ts
1301
+ const DEFAULT_TOKEN_BUDGET = 800;
1302
+ function toolMemoryWakeup(registryDb, params) {
1303
+ try {
1304
+ const tokenBudget = params.token_budget ?? DEFAULT_TOKEN_BUDGET;
1305
+ let rootPath;
1306
+ if (params.project) {
1307
+ const bySlug = registryDb.prepare("SELECT root_path FROM projects WHERE slug = ?").get(params.project);
1308
+ if (bySlug) rootPath = bySlug.root_path;
1309
+ else {
1310
+ const detected = detectProjectFromPath(registryDb, params.project);
1311
+ if (detected) rootPath = detected.root_path;
1312
+ }
1313
+ } else {
1314
+ const detected = detectProjectFromPath(registryDb, process.cwd());
1315
+ if (detected) rootPath = detected.root_path;
1316
+ }
1317
+ const wakeupBlock = buildWakeupContext(rootPath, tokenBudget);
1318
+ if (!wakeupBlock) return { content: [{
1319
+ type: "text",
1320
+ text: "No wake-up context available. Create ~/.pai/identity.txt for L0 identity, or ensure session notes exist for L1 story."
1321
+ }] };
1322
+ return { content: [{
1323
+ type: "text",
1324
+ text: `WAKEUP CONTEXT\n\n${wakeupBlock}`
1325
+ }] };
1326
+ } catch (e) {
1327
+ return {
1328
+ content: [{
1329
+ type: "text",
1330
+ text: `Wakeup context error: ${String(e)}`
1331
+ }],
1332
+ isError: true
1333
+ };
1334
+ }
1335
+ }
1336
+
1337
+ //#endregion
1338
+ //#region src/memory/taxonomy.ts
1339
+ /**
1340
+ * Build a taxonomy of stored memory — what projects exist, how much is stored,
1341
+ * and what has been active recently.
1342
+ *
1343
+ * Registry queries (projects, sessions) are synchronous (better-sqlite3).
1344
+ * Storage backend queries (files, chunks) are async.
1345
+ */
1346
+ async function getTaxonomy(registryDb, storage, options = {}) {
1347
+ const includeArchived = options.include_archived ?? false;
1348
+ const limit = options.limit ?? 50;
1349
+ const statusFilter = includeArchived ? "status IN ('active', 'archived', 'migrating')" : "status = 'active'";
1350
+ const projectRows = registryDb.prepare(`SELECT id, slug, display_name, status, created_at, updated_at
1351
+ FROM projects
1352
+ WHERE ${statusFilter}
1353
+ ORDER BY updated_at DESC
1354
+ LIMIT ?`).all(limit);
1355
+ if (projectRows.length === 0) return {
1356
+ projects: [],
1357
+ totals: {
1358
+ projects: 0,
1359
+ sessions: 0,
1360
+ notes: 0,
1361
+ chunks: 0
1362
+ },
1363
+ recent_activity: []
1364
+ };
1365
+ const projectIds = projectRows.map((p) => p.id);
1366
+ const sessionCountsByProject = /* @__PURE__ */ new Map();
1367
+ const lastSessionDateByProject = /* @__PURE__ */ new Map();
1368
+ for (const projectId of projectIds) {
1369
+ const countRow = registryDb.prepare("SELECT COUNT(*) AS n FROM sessions WHERE project_id = ?").get(projectId);
1370
+ sessionCountsByProject.set(projectId, countRow.n);
1371
+ const lastRow = registryDb.prepare("SELECT date FROM sessions WHERE project_id = ? ORDER BY number DESC LIMIT 1").get(projectId);
1372
+ lastSessionDateByProject.set(projectId, lastRow?.date ?? null);
1373
+ }
1374
+ const tagsByProject = /* @__PURE__ */ new Map();
1375
+ for (const projectId of projectIds) {
1376
+ const tags = registryDb.prepare(`SELECT t.name
1377
+ FROM tags t
1378
+ JOIN project_tags pt ON pt.tag_id = t.id
1379
+ WHERE pt.project_id = ?
1380
+ ORDER BY t.name`).all(projectId);
1381
+ tagsByProject.set(projectId, tags.map((t) => t.name));
1382
+ }
1383
+ const noteCountsByProject = /* @__PURE__ */ new Map();
1384
+ const chunkCountsByProject = /* @__PURE__ */ new Map();
1385
+ const isBackend = (x) => x.backendType === "sqlite";
1386
+ if (isBackend(storage)) {
1387
+ const rawDb = storage.getRawDb?.();
1388
+ if (rawDb) for (const projectId of projectIds) {
1389
+ const noteRow = rawDb.prepare("SELECT COUNT(*) AS n FROM memory_files WHERE project_id = ?").get(projectId);
1390
+ noteCountsByProject.set(projectId, noteRow.n);
1391
+ const chunkRow = rawDb.prepare("SELECT COUNT(*) AS n FROM memory_chunks WHERE project_id = ?").get(projectId);
1392
+ chunkCountsByProject.set(projectId, chunkRow.n);
1393
+ }
1394
+ } else for (const projectId of projectIds) {
1395
+ noteCountsByProject.set(projectId, 0);
1396
+ chunkCountsByProject.set(projectId, 0);
1397
+ }
1398
+ const stats = await storage.getStats();
1399
+ const totalProjects = registryDb.prepare(`SELECT COUNT(*) AS n FROM projects WHERE ${statusFilter}`).get().n;
1400
+ const totalSessions = registryDb.prepare("SELECT COUNT(*) AS n FROM sessions").get().n;
1401
+ const recentActivity = registryDb.prepare(`SELECT s.date, s.title, p.slug
1402
+ FROM sessions s
1403
+ JOIN projects p ON p.id = s.project_id
1404
+ WHERE p.${statusFilter.replace("status", "p.status")}
1405
+ ORDER BY s.created_at DESC
1406
+ LIMIT 10`).all().map((row) => ({
1407
+ project_slug: row.slug,
1408
+ action: `session: ${row.title || "(untitled)"}`,
1409
+ timestamp: row.date
1410
+ }));
1411
+ return {
1412
+ projects: projectRows.map((row) => ({
1413
+ slug: row.slug,
1414
+ display_name: row.display_name,
1415
+ session_count: sessionCountsByProject.get(row.id) ?? 0,
1416
+ note_count: noteCountsByProject.get(row.id) ?? 0,
1417
+ last_activity: lastSessionDateByProject.get(row.id) ?? null,
1418
+ top_tags: tagsByProject.get(row.id) ?? []
1419
+ })),
1420
+ totals: {
1421
+ projects: totalProjects,
1422
+ sessions: totalSessions,
1423
+ notes: stats.files,
1424
+ chunks: stats.chunks
1425
+ },
1426
+ recent_activity: recentActivity
1427
+ };
1428
+ }
1429
+
1430
+ //#endregion
1431
+ //#region src/mcp/tools/taxonomy.ts
1432
+ async function toolMemoryTaxonomy(registryDb, storage, params = {}) {
1433
+ try {
1434
+ const result = await getTaxonomy(registryDb, storage, {
1435
+ include_archived: params.include_archived,
1436
+ limit: params.limit
1437
+ });
1438
+ const lines = [];
1439
+ lines.push(`PAI Memory Taxonomy — ${result.totals.projects} project(s), ${result.totals.sessions} session(s), ${result.totals.notes} indexed file(s), ${result.totals.chunks} chunk(s)`);
1440
+ lines.push("");
1441
+ if (result.projects.length === 0) lines.push("No active projects found.");
1442
+ else {
1443
+ lines.push("Projects:");
1444
+ for (const p of result.projects) {
1445
+ const tagStr = p.top_tags.length > 0 ? ` [${p.top_tags.join(", ")}]` : "";
1446
+ const activityStr = p.last_activity ? ` last: ${p.last_activity}` : "";
1447
+ lines.push(` ${p.slug} — ${p.display_name} sessions=${p.session_count}` + (p.note_count > 0 ? ` files=${p.note_count}` : "") + activityStr + tagStr);
1448
+ }
1449
+ }
1450
+ if (result.recent_activity.length > 0) {
1451
+ lines.push("");
1452
+ lines.push("Recent activity (last 10 sessions across all projects):");
1453
+ for (const a of result.recent_activity) lines.push(` ${a.timestamp} ${a.project_slug} ${a.action}`);
1454
+ }
1455
+ return { content: [{
1456
+ type: "text",
1457
+ text: lines.join("\n")
1458
+ }] };
1459
+ } catch (e) {
1460
+ return {
1461
+ content: [{
1462
+ type: "text",
1463
+ text: `memory_taxonomy error: ${String(e)}`
1464
+ }],
1465
+ isError: true
1466
+ };
1467
+ }
1468
+ }
1469
+
1470
+ //#endregion
1471
+ //#region src/memory/tunnels.ts
1472
+ /**
1473
+ * tunnels.ts — cross-project concept detection ("palace graph / tunnel detection")
1474
+ *
1475
+ * A "tunnel" is a concept (word or short phrase) that appears in chunks from
1476
+ * at least two distinct projects. These serendipitous cross-project connections
1477
+ * are surfaced so the user can discover unexpected relationships between their
1478
+ * work streams.
1479
+ *
1480
+ * Algorithm:
1481
+ * 1. Pull the top-N most frequent significant terms from memory_chunks via BM25 FTS.
1482
+ * We use the FTS5 vocab table (if available) or fall back to term frequency
1483
+ * aggregation over the raw text via a trigram approach.
1484
+ * 2. For each candidate term, count how many distinct projects have at least one
1485
+ * chunk containing it and aggregate occurrence stats.
1486
+ * 3. Filter by min_projects and min_occurrences, sort by project breadth then
1487
+ * frequency, return top limit results.
1488
+ *
1489
+ * Backend support:
1490
+ * - SQLite — uses `memory_fts` MATCH to count per-project occurrences.
1491
+ * - Postgres — uses `memory_chunks` tsvector + ts_stat for term extraction and
1492
+ * per-project term frequency counting via plainto_tsquery.
1493
+ */
1494
+ /**
1495
+ * Extract candidate terms from the SQLite FTS5 index using the vocabulary
1496
+ * approach: iterate the fts5vocab table (if it exists) for the most common
1497
+ * terms, then per-term count distinct projects.
1498
+ */
1499
+ async function findTunnelsSqlite(db, slugMap, opts) {
1500
+ const projectIds = Object.keys(slugMap).map(Number);
1501
+ if (projectIds.length < 2) return {
1502
+ tunnels: [],
1503
+ projects_analyzed: projectIds.length,
1504
+ total_concepts_evaluated: 0
1505
+ };
1506
+ let candidateTerms = [];
1507
+ try {
1508
+ candidateTerms = db.prepare(`SELECT term, SUM(doc) AS doc_count, SUM(cnt) AS total_cnt
1509
+ FROM memory_fts_v
1510
+ GROUP BY term
1511
+ HAVING SUM(cnt) >= ?
1512
+ ORDER BY SUM(doc) DESC
1513
+ LIMIT 500`).all(opts.min_occurrences).map((r) => r.term).filter((t) => t.length >= 3 && !STOP_WORDS.has(t));
1514
+ } catch {
1515
+ const sampleRows = db.prepare(`SELECT LOWER(text) AS text FROM memory_chunks
1516
+ WHERE LENGTH(text) > 20
1517
+ ORDER BY RANDOM()
1518
+ LIMIT 2000`).all();
1519
+ const freq = /* @__PURE__ */ new Map();
1520
+ for (const { text } of sampleRows) {
1521
+ const tokens = text.split(/[\s\p{P}]+/u).filter(Boolean).filter((t) => t.length >= 3 && !STOP_WORDS.has(t));
1522
+ for (const t of tokens) freq.set(t, (freq.get(t) ?? 0) + 1);
1523
+ }
1524
+ candidateTerms = [...freq.entries()].filter(([, n]) => n >= opts.min_occurrences).sort((a, b) => b[1] - a[1]).slice(0, 200).map(([t]) => t);
1525
+ }
1526
+ if (candidateTerms.length === 0) return {
1527
+ tunnels: [],
1528
+ projects_analyzed: projectIds.length,
1529
+ total_concepts_evaluated: 0
1530
+ };
1531
+ const tunnels = [];
1532
+ for (const term of candidateTerms) try {
1533
+ const rows = db.prepare(`SELECT c.project_id, COUNT(*) AS cnt,
1534
+ MIN(c.updated_at) AS first_seen,
1535
+ MAX(c.updated_at) AS last_seen
1536
+ FROM memory_fts f
1537
+ JOIN memory_chunks c ON c.id = f.id
1538
+ WHERE memory_fts MATCH ?
1539
+ AND c.project_id IN (${projectIds.map(() => "?").join(", ")})
1540
+ GROUP BY c.project_id`).all(`"${term.replace(/"/g, "\"\"")}"`, ...projectIds);
1541
+ if (rows.length < opts.min_projects) continue;
1542
+ const totalOccurrences = rows.reduce((s, r) => s + Number(r.cnt), 0);
1543
+ if (totalOccurrences < opts.min_occurrences) continue;
1544
+ const projects = rows.map((r) => slugMap[r.project_id] ?? String(r.project_id)).filter(Boolean);
1545
+ const firstSeen = Math.min(...rows.map((r) => r.first_seen));
1546
+ const lastSeen = Math.max(...rows.map((r) => r.last_seen));
1547
+ tunnels.push({
1548
+ concept: term,
1549
+ projects,
1550
+ occurrences: totalOccurrences,
1551
+ first_seen: firstSeen,
1552
+ last_seen: lastSeen
1553
+ });
1554
+ } catch {
1555
+ continue;
1556
+ }
1557
+ tunnels.sort((a, b) => {
1558
+ const byProjects = b.projects.length - a.projects.length;
1559
+ if (byProjects !== 0) return byProjects;
1560
+ return b.occurrences - a.occurrences;
1561
+ });
1562
+ return {
1563
+ tunnels: tunnels.slice(0, opts.limit),
1564
+ projects_analyzed: projectIds.length,
1565
+ total_concepts_evaluated: candidateTerms.length
1566
+ };
1567
+ }
1568
+ /**
1569
+ * Use Postgres ts_stat() + plainto_tsquery to efficiently find terms that
1570
+ * appear across multiple projects.
1571
+ */
1572
+ async function findTunnelsPostgres(pool, slugMap, opts) {
1573
+ const projectIds = Object.keys(slugMap).map(Number);
1574
+ if (projectIds.length < 2) return {
1575
+ tunnels: [],
1576
+ projects_analyzed: projectIds.length,
1577
+ total_concepts_evaluated: 0
1578
+ };
1579
+ let candidateTerms = (await pool.query(`SELECT word, ndoc, nentry
1580
+ FROM ts_stat(
1581
+ 'SELECT to_tsvector(''simple'', text) FROM memory_chunks WHERE project_id = ANY($1)'
1582
+ )
1583
+ WHERE length(word) >= 3
1584
+ AND nentry >= $2
1585
+ ORDER BY ndoc DESC
1586
+ LIMIT 500`, [projectIds, opts.min_occurrences])).rows.map((r) => r.word).filter((t) => !STOP_WORDS.has(t));
1587
+ if (candidateTerms.length === 0) return {
1588
+ tunnels: [],
1589
+ projects_analyzed: projectIds.length,
1590
+ total_concepts_evaluated: 0
1591
+ };
1592
+ candidateTerms = candidateTerms.slice(0, 200);
1593
+ const valuesClause = candidateTerms.map((t, i) => `($${i + 2}::text)`).join(", ");
1594
+ const batchResult = await pool.query(`SELECT v.concept, c.project_id::text, COUNT(*) AS cnt,
1595
+ MIN(c.updated_at) AS first_seen,
1596
+ MAX(c.updated_at) AS last_seen
1597
+ FROM (VALUES ${valuesClause}) AS v(concept)
1598
+ JOIN memory_chunks c
1599
+ ON to_tsvector('simple', c.text) @@ plainto_tsquery('simple', v.concept)
1600
+ AND c.project_id = ANY($1)
1601
+ GROUP BY v.concept, c.project_id`, [projectIds, ...candidateTerms]);
1602
+ const byConceptMap = /* @__PURE__ */ new Map();
1603
+ for (const row of batchResult.rows) {
1604
+ const existing = byConceptMap.get(row.concept) ?? {
1605
+ projects: /* @__PURE__ */ new Set(),
1606
+ occurrences: 0,
1607
+ firstSeen: Infinity,
1608
+ lastSeen: -Infinity
1609
+ };
1610
+ existing.projects.add(parseInt(row.project_id, 10));
1611
+ existing.occurrences += parseInt(row.cnt, 10);
1612
+ const fs = parseInt(row.first_seen, 10);
1613
+ const ls = parseInt(row.last_seen, 10);
1614
+ if (fs < existing.firstSeen) existing.firstSeen = fs;
1615
+ if (ls > existing.lastSeen) existing.lastSeen = ls;
1616
+ byConceptMap.set(row.concept, existing);
1617
+ }
1618
+ const tunnels = [];
1619
+ for (const [concept, data] of byConceptMap) {
1620
+ if (data.projects.size < opts.min_projects) continue;
1621
+ if (data.occurrences < opts.min_occurrences) continue;
1622
+ const projects = [...data.projects].map((id) => slugMap[id] ?? String(id)).filter(Boolean);
1623
+ tunnels.push({
1624
+ concept,
1625
+ projects,
1626
+ occurrences: data.occurrences,
1627
+ first_seen: data.firstSeen === Infinity ? 0 : data.firstSeen,
1628
+ last_seen: data.lastSeen === -Infinity ? 0 : data.lastSeen
1629
+ });
1630
+ }
1631
+ tunnels.sort((a, b) => {
1632
+ const byProjects = b.projects.length - a.projects.length;
1633
+ if (byProjects !== 0) return byProjects;
1634
+ return b.occurrences - a.occurrences;
1635
+ });
1636
+ return {
1637
+ tunnels: tunnels.slice(0, opts.limit),
1638
+ projects_analyzed: projectIds.length,
1639
+ total_concepts_evaluated: candidateTerms.length
1640
+ };
1641
+ }
1642
+ /**
1643
+ * Find cross-project concept tunnels.
1644
+ *
1645
+ * Works with both SQLite and Postgres storage backends.
1646
+ * Requires the `registryDb` (better-sqlite3) for project slug resolution.
1647
+ *
1648
+ * @param backend Active PAI storage backend.
1649
+ * @param registryDb Registry database for project slug resolution.
1650
+ * @param options Filter and limit options.
1651
+ */
1652
+ async function findTunnels(backend, registryDb, options) {
1653
+ const opts = {
1654
+ min_projects: options?.min_projects ?? 2,
1655
+ min_occurrences: options?.min_occurrences ?? 3,
1656
+ limit: options?.limit ?? 20
1657
+ };
1658
+ const projectRows = registryDb.prepare("SELECT id, slug FROM projects WHERE status != 'archived'").all();
1659
+ const slugMap = {};
1660
+ for (const { id, slug } of projectRows) slugMap[id] = slug;
1661
+ if (backend.backendType === "postgres") {
1662
+ const pool = backend.getPool?.();
1663
+ if (!pool) throw new Error("findTunnels: Postgres backend does not expose getPool()");
1664
+ return findTunnelsPostgres(pool, slugMap, opts);
1665
+ }
1666
+ const rawDb = backend.getRawDb?.();
1667
+ if (!rawDb) throw new Error("findTunnels: SQLite backend does not expose getRawDb()");
1668
+ return findTunnelsSqlite(rawDb, slugMap, opts);
1669
+ }
1670
+
1671
+ //#endregion
1672
+ //#region src/mcp/tools/tunnels.ts
1673
+ async function toolMemoryTunnels(registryDb, backend, params) {
1674
+ try {
1675
+ const result = await findTunnels(backend, registryDb, {
1676
+ min_projects: params.min_projects,
1677
+ min_occurrences: params.min_occurrences,
1678
+ limit: params.limit
1679
+ });
1680
+ return { content: [{
1681
+ type: "text",
1682
+ text: JSON.stringify(result, null, 2)
1683
+ }] };
1684
+ } catch (e) {
1685
+ return {
1686
+ content: [{
1687
+ type: "text",
1688
+ text: `memory_tunnels error: ${String(e)}`
1689
+ }],
1690
+ isError: true
1691
+ };
1692
+ }
1693
+ }
1694
+
909
1695
  //#endregion
910
1696
  //#region src/mcp/tools.ts
911
1697
  var tools_exports = /* @__PURE__ */ __exportAll({
@@ -913,8 +1699,15 @@ var tools_exports = /* @__PURE__ */ __exportAll({
913
1699
  detectProjectFromPath: () => detectProjectFromPath,
914
1700
  formatProject: () => formatProject,
915
1701
  lookupProjectId: () => lookupProjectId,
1702
+ toolKgAdd: () => toolKgAdd,
1703
+ toolKgContradictions: () => toolKgContradictions,
1704
+ toolKgInvalidate: () => toolKgInvalidate,
1705
+ toolKgQuery: () => toolKgQuery,
916
1706
  toolMemoryGet: () => toolMemoryGet,
917
1707
  toolMemorySearch: () => toolMemorySearch,
1708
+ toolMemoryTaxonomy: () => toolMemoryTaxonomy,
1709
+ toolMemoryTunnels: () => toolMemoryTunnels,
1710
+ toolMemoryWakeup: () => toolMemoryWakeup,
918
1711
  toolProjectDetect: () => toolProjectDetect,
919
1712
  toolProjectHealth: () => toolProjectHealth,
920
1713
  toolProjectInfo: () => toolProjectInfo,
@@ -934,5 +1727,5 @@ var tools_exports = /* @__PURE__ */ __exportAll({
934
1727
  });
935
1728
 
936
1729
  //#endregion
937
- export { toolProjectDetect as a, toolProjectList as c, toolMemorySearch as d, toolSessionRoute as i, toolProjectTodo as l, toolRegistrySearch as n, toolProjectHealth as o, toolSessionList as r, toolProjectInfo as s, tools_exports as t, toolMemoryGet as u };
938
- //# sourceMappingURL=tools-BXSwlzeH.mjs.map
1730
+ export { toolSessionList as a, toolProjectHealth as c, toolProjectTodo as d, toolMemoryGet as f, toolRegistrySearch as i, toolProjectInfo as l, toolMemoryTaxonomy as n, toolSessionRoute as o, toolMemorySearch as p, toolMemoryWakeup as r, toolProjectDetect as s, tools_exports as t, toolProjectList as u };
1731
+ //# sourceMappingURL=tools-C4SBZHga.mjs.map