@tekmidian/pai 0.8.4 → 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.
Files changed (33) hide show
  1. package/ARCHITECTURE.md +121 -0
  2. package/FEATURE.md +5 -0
  3. package/README.md +54 -0
  4. package/dist/cli/index.mjs +11 -11
  5. package/dist/daemon/index.mjs +3 -3
  6. package/dist/{daemon-nXyhvdzz.mjs → daemon-VIFoKc_z.mjs} +31 -6
  7. package/dist/daemon-VIFoKc_z.mjs.map +1 -0
  8. package/dist/daemon-mcp/index.mjs +51 -0
  9. package/dist/daemon-mcp/index.mjs.map +1 -1
  10. package/dist/{factory-Ygqe_bVZ.mjs → factory-e0k1HWuc.mjs} +2 -2
  11. package/dist/{factory-Ygqe_bVZ.mjs.map → factory-e0k1HWuc.mjs.map} +1 -1
  12. package/dist/hooks/load-project-context.mjs +276 -89
  13. package/dist/hooks/load-project-context.mjs.map +4 -4
  14. package/dist/hooks/stop-hook.mjs +152 -2
  15. package/dist/hooks/stop-hook.mjs.map +3 -3
  16. package/dist/{postgres-CKf-EDtS.mjs → postgres-DvEPooLO.mjs} +45 -10
  17. package/dist/postgres-DvEPooLO.mjs.map +1 -0
  18. package/dist/query-feedback-Dv43XKHM.mjs +76 -0
  19. package/dist/query-feedback-Dv43XKHM.mjs.map +1 -0
  20. package/dist/tools-C4SBZHga.mjs +1731 -0
  21. package/dist/tools-C4SBZHga.mjs.map +1 -0
  22. package/dist/{vault-indexer-Bi2cRmn7.mjs → vault-indexer-B-aJpRZC.mjs} +3 -2
  23. package/dist/{vault-indexer-Bi2cRmn7.mjs.map → vault-indexer-B-aJpRZC.mjs.map} +1 -1
  24. package/dist/{zettelkasten-cdajbnPr.mjs → zettelkasten-DhBKZQHF.mjs} +358 -3
  25. package/dist/zettelkasten-DhBKZQHF.mjs.map +1 -0
  26. package/package.json +1 -1
  27. package/src/hooks/ts/session-start/load-project-context.ts +36 -0
  28. package/src/hooks/ts/stop/stop-hook.ts +203 -1
  29. package/dist/daemon-nXyhvdzz.mjs.map +0 -1
  30. package/dist/postgres-CKf-EDtS.mjs.map +0 -1
  31. package/dist/tools-DcaJlYDN.mjs +0 -869
  32. package/dist/tools-DcaJlYDN.mjs.map +0 -1
  33. package/dist/zettelkasten-cdajbnPr.mjs.map +0 -1
@@ -0,0 +1,1731 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-95iHPtFO.mjs";
2
+ import { t as STOP_WORDS } from "./stop-words-BaMEGVeY.mjs";
3
+ import { i as searchMemoryHybrid, n as populateSlugs } from "./search-DC1qhkKn.mjs";
4
+ import { r as formatDetectionJson, t as detectProject } from "./detect-CdaA48EI.mjs";
5
+ import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
6
+ import { homedir } from "node:os";
7
+ import { basename, isAbsolute, join, resolve } from "node:path";
8
+
9
+ //#region src/mcp/tools/types.ts
10
+ /**
11
+ * Shared types and project-row helpers used across all MCP tool handler modules.
12
+ */
13
+ function lookupProjectId(registryDb, slug) {
14
+ const bySlug = registryDb.prepare("SELECT id FROM projects WHERE slug = ?").get(slug);
15
+ if (bySlug) return bySlug.id;
16
+ const byAlias = registryDb.prepare("SELECT project_id FROM aliases WHERE alias = ?").get(slug);
17
+ if (byAlias) return byAlias.project_id;
18
+ return null;
19
+ }
20
+ function detectProjectFromPath(registryDb, fsPath) {
21
+ const resolved = resolve(fsPath);
22
+ const exact = registryDb.prepare("SELECT id, slug, display_name, root_path, type, status, created_at, updated_at FROM projects WHERE root_path = ?").get(resolved);
23
+ if (exact) return exact;
24
+ const all = registryDb.prepare("SELECT id, slug, display_name, root_path, type, status, created_at, updated_at FROM projects ORDER BY LENGTH(root_path) DESC").all();
25
+ for (const project of all) if (resolved.startsWith(project.root_path + "/") || resolved === project.root_path) return project;
26
+ return null;
27
+ }
28
+ function formatProject(registryDb, project) {
29
+ const sessionCount = registryDb.prepare("SELECT COUNT(*) AS n FROM sessions WHERE project_id = ?").get(project.id).n;
30
+ const lastSession = registryDb.prepare("SELECT date FROM sessions WHERE project_id = ? ORDER BY date DESC LIMIT 1").get(project.id);
31
+ const tags = registryDb.prepare(`SELECT t.name FROM tags t
32
+ JOIN project_tags pt ON pt.tag_id = t.id
33
+ WHERE pt.project_id = ?
34
+ ORDER BY t.name`).all(project.id).map((r) => r.name);
35
+ const aliases = registryDb.prepare("SELECT alias FROM aliases WHERE project_id = ? ORDER BY alias").all(project.id).map((r) => r.alias);
36
+ const lines = [
37
+ `slug: ${project.slug}`,
38
+ `display_name: ${project.display_name}`,
39
+ `root_path: ${project.root_path}`,
40
+ `type: ${project.type}`,
41
+ `status: ${project.status}`,
42
+ `sessions: ${sessionCount}`
43
+ ];
44
+ if (lastSession) lines.push(`last_session: ${lastSession.date}`);
45
+ if (tags.length) lines.push(`tags: ${tags.join(", ")}`);
46
+ if (aliases.length) lines.push(`aliases: ${aliases.join(", ")}`);
47
+ if (project.obsidian_link) lines.push(`obsidian_link: ${project.obsidian_link}`);
48
+ if (project.archived_at) lines.push(`archived_at: ${new Date(project.archived_at).toISOString().slice(0, 10)}`);
49
+ return lines.join("\n");
50
+ }
51
+
52
+ //#endregion
53
+ //#region src/mcp/tools/memory.ts
54
+ /**
55
+ * MCP tool handlers: memory_search, memory_get
56
+ */
57
+ async function toolMemorySearch(registryDb, federation, params, searchDefaults) {
58
+ try {
59
+ const projectIds = params.project ? (() => {
60
+ const id = lookupProjectId(registryDb, params.project);
61
+ return id != null ? [id] : [];
62
+ })() : void 0;
63
+ if (params.project && (!projectIds || projectIds.length === 0)) return {
64
+ content: [{
65
+ type: "text",
66
+ text: `Project not found: ${params.project}`
67
+ }],
68
+ isError: true
69
+ };
70
+ const mode = params.mode ?? searchDefaults?.mode ?? "keyword";
71
+ const snippetLength = params.snippetLength ?? searchDefaults?.snippetLength ?? 200;
72
+ const searchOpts = {
73
+ projectIds,
74
+ sources: params.sources,
75
+ maxResults: params.limit ?? searchDefaults?.defaultLimit ?? 5
76
+ };
77
+ let results;
78
+ const isBackend = (x) => "backendType" in x;
79
+ if (isBackend(federation)) if (mode === "keyword") results = await federation.searchKeyword(params.query, searchOpts);
80
+ else if (mode === "semantic" || mode === "hybrid") {
81
+ const { generateEmbedding } = await import("./embeddings-DGRAPAYb.mjs").then((n) => n.i);
82
+ const queryEmbedding = await generateEmbedding(params.query, true);
83
+ if (mode === "semantic") results = await federation.searchSemantic(queryEmbedding, searchOpts);
84
+ else {
85
+ const [kwResults, semResults] = await Promise.all([federation.searchKeyword(params.query, {
86
+ ...searchOpts,
87
+ maxResults: 50
88
+ }), federation.searchSemantic(queryEmbedding, {
89
+ ...searchOpts,
90
+ maxResults: 50
91
+ })]);
92
+ results = combineHybridResults(kwResults, semResults, searchOpts.maxResults ?? 10);
93
+ }
94
+ } else results = await federation.searchKeyword(params.query, searchOpts);
95
+ else {
96
+ const { searchMemory, searchMemorySemantic } = await import("./search-DC1qhkKn.mjs").then((n) => n.o);
97
+ if (mode === "keyword") results = searchMemory(federation, params.query, searchOpts);
98
+ else if (mode === "semantic" || mode === "hybrid") {
99
+ const { generateEmbedding } = await import("./embeddings-DGRAPAYb.mjs").then((n) => n.i);
100
+ const queryEmbedding = await generateEmbedding(params.query, true);
101
+ if (mode === "semantic") results = searchMemorySemantic(federation, queryEmbedding, searchOpts);
102
+ else results = searchMemoryHybrid(federation, params.query, queryEmbedding, searchOpts);
103
+ } else results = searchMemory(federation, params.query, searchOpts);
104
+ }
105
+ const shouldRerank = params.rerank ?? searchDefaults?.rerank ?? true;
106
+ if (shouldRerank && results.length > 0) {
107
+ const { rerankResults } = await import("./reranker-CMNZcfVx.mjs").then((n) => n.r);
108
+ results = await rerankResults(params.query, results, { topK: searchOpts.maxResults ?? 5 });
109
+ }
110
+ const recencyDays = params.recencyBoost ?? searchDefaults?.recencyBoostDays ?? 0;
111
+ if (recencyDays > 0 && results.length > 0) {
112
+ const { applyRecencyBoost } = await import("./search-DC1qhkKn.mjs").then((n) => n.o);
113
+ results = applyRecencyBoost(results, recencyDays);
114
+ }
115
+ const withSlugs = populateSlugs(results, registryDb);
116
+ if (withSlugs.length === 0) return { content: [{
117
+ type: "text",
118
+ text: `No results found for query: "${params.query}" (mode: ${mode})`
119
+ }] };
120
+ const rerankLabel = shouldRerank ? " +rerank" : "";
121
+ const formatted = withSlugs.map((r, i) => {
122
+ const header = `[${i + 1}] ${r.projectSlug ?? `project:${r.projectId}`} — ${r.path} (lines ${r.startLine}-${r.endLine}) score=${r.score.toFixed(4)} tier=${r.tier} source=${r.source}`;
123
+ const raw = r.snippet.trim();
124
+ return `${header}\n${raw.length > snippetLength ? raw.slice(0, snippetLength) + "..." : raw}`;
125
+ }).join("\n\n---\n\n");
126
+ try {
127
+ const { saveQueryResult } = await import("./query-feedback-Dv43XKHM.mjs").then((n) => n.t);
128
+ saveQueryResult({
129
+ query: params.query,
130
+ timestamp: Date.now(),
131
+ source: "memory_search",
132
+ sourceSlugs: withSlugs.slice(0, 5).map((r) => r.path),
133
+ answerPreview: withSlugs.slice(0, 3).map((r) => r.snippet.trim().slice(0, 150)).join(" | "),
134
+ resultCount: withSlugs.length
135
+ });
136
+ } catch {}
137
+ return { content: [{
138
+ type: "text",
139
+ text: `Found ${withSlugs.length} result(s) for "${params.query}" (mode: ${mode}${rerankLabel}):\n\n${formatted}`
140
+ }] };
141
+ } catch (e) {
142
+ return {
143
+ content: [{
144
+ type: "text",
145
+ text: `Search error: ${String(e)}`
146
+ }],
147
+ isError: true
148
+ };
149
+ }
150
+ }
151
+ function toolMemoryGet(registryDb, params) {
152
+ try {
153
+ const projectId = lookupProjectId(registryDb, params.project);
154
+ if (projectId == null) return {
155
+ content: [{
156
+ type: "text",
157
+ text: `Project not found: ${params.project}`
158
+ }],
159
+ isError: true
160
+ };
161
+ const project = registryDb.prepare("SELECT root_path FROM projects WHERE id = ?").get(projectId);
162
+ if (!project) return {
163
+ content: [{
164
+ type: "text",
165
+ text: `Project not found: ${params.project}`
166
+ }],
167
+ isError: true
168
+ };
169
+ const requestedPath = params.path;
170
+ if (requestedPath.includes("..") || isAbsolute(requestedPath)) return {
171
+ content: [{
172
+ type: "text",
173
+ text: `Invalid path: ${params.path} (must be a relative path within the project root, no ../ allowed)`
174
+ }],
175
+ isError: true
176
+ };
177
+ const fullPath = join(project.root_path, requestedPath);
178
+ const resolvedFull = resolve(fullPath);
179
+ const resolvedRoot = resolve(project.root_path);
180
+ if (!resolvedFull.startsWith(resolvedRoot + "/") && resolvedFull !== resolvedRoot) return {
181
+ content: [{
182
+ type: "text",
183
+ text: `Path traversal blocked: ${params.path}`
184
+ }],
185
+ isError: true
186
+ };
187
+ if (!existsSync(fullPath)) return {
188
+ content: [{
189
+ type: "text",
190
+ text: `File not found: ${requestedPath} (project: ${params.project})`
191
+ }],
192
+ isError: true
193
+ };
194
+ const stat = statSync(fullPath);
195
+ if (stat.size > 5 * 1024 * 1024) return { content: [{
196
+ type: "text",
197
+ text: `Error: file too large (${(stat.size / 1024 / 1024).toFixed(1)} MB). Maximum 5 MB.`
198
+ }] };
199
+ const allLines = readFileSync(fullPath, "utf8").split("\n");
200
+ const fromLine = (params.from ?? 1) - 1;
201
+ const toLine = params.lines != null ? Math.min(fromLine + params.lines, allLines.length) : allLines.length;
202
+ const text = allLines.slice(fromLine, toLine).join("\n");
203
+ return { content: [{
204
+ type: "text",
205
+ text: `${params.from != null ? `${params.project}/${requestedPath} (lines ${fromLine + 1}-${toLine}):` : `${params.project}/${requestedPath}:`}\n\n${text}`
206
+ }] };
207
+ } catch (e) {
208
+ return {
209
+ content: [{
210
+ type: "text",
211
+ text: `Read error: ${String(e)}`
212
+ }],
213
+ isError: true
214
+ };
215
+ }
216
+ }
217
+ /**
218
+ * Combine keyword + semantic results using min-max normalized scoring.
219
+ * Mirrors the logic in searchMemoryHybrid() from memory/search.ts,
220
+ * but works on pre-computed result arrays so it works for any backend.
221
+ */
222
+ function combineHybridResults(keywordResults, semanticResults, maxResults, keywordWeight = .5, semanticWeight = .5) {
223
+ if (keywordResults.length === 0 && semanticResults.length === 0) return [];
224
+ const keyFor = (r) => `${r.projectId}:${r.path}:${r.startLine}:${r.endLine}`;
225
+ function minMaxNormalize(items) {
226
+ if (items.length === 0) return /* @__PURE__ */ new Map();
227
+ const min = Math.min(...items.map((r) => r.score));
228
+ const range = Math.max(...items.map((r) => r.score)) - min;
229
+ const m = /* @__PURE__ */ new Map();
230
+ for (const r of items) m.set(keyFor(r), range === 0 ? 1 : (r.score - min) / range);
231
+ return m;
232
+ }
233
+ const kwNorm = minMaxNormalize(keywordResults);
234
+ const semNorm = minMaxNormalize(semanticResults);
235
+ const allKeys = new Set([...keywordResults.map(keyFor), ...semanticResults.map(keyFor)]);
236
+ const metaMap = /* @__PURE__ */ new Map();
237
+ for (const r of [...keywordResults, ...semanticResults]) metaMap.set(keyFor(r), r);
238
+ const combined = [];
239
+ for (const key of allKeys) {
240
+ const meta = metaMap.get(key);
241
+ const kwScore = kwNorm.get(key) ?? 0;
242
+ const semScore = semNorm.get(key) ?? 0;
243
+ const combinedScore = keywordWeight * kwScore + semanticWeight * semScore;
244
+ combined.push({
245
+ ...meta,
246
+ score: combinedScore,
247
+ combinedScore
248
+ });
249
+ }
250
+ return combined.sort((a, b) => b.score - a.score).slice(0, maxResults).map(({ combinedScore: _unused, ...r }) => r);
251
+ }
252
+
253
+ //#endregion
254
+ //#region src/mcp/tools/projects.ts
255
+ /**
256
+ * MCP tool handlers: project_info, project_list, project_detect,
257
+ * project_health, project_todo
258
+ */
259
+ function toolProjectInfo(registryDb, params) {
260
+ try {
261
+ let project = null;
262
+ if (params.slug) {
263
+ const projectId = lookupProjectId(registryDb, params.slug);
264
+ if (projectId != null) project = registryDb.prepare("SELECT id, slug, display_name, root_path, type, status, created_at, updated_at, archived_at, parent_id, obsidian_link FROM projects WHERE id = ?").get(projectId);
265
+ } else project = detectProjectFromPath(registryDb, process.cwd());
266
+ if (!project) return {
267
+ content: [{
268
+ type: "text",
269
+ text: params.slug ? `Project not found: ${params.slug}` : `No PAI project found matching the current directory: ${process.cwd()}`
270
+ }],
271
+ isError: !params.slug
272
+ };
273
+ return { content: [{
274
+ type: "text",
275
+ text: formatProject(registryDb, project)
276
+ }] };
277
+ } catch (e) {
278
+ return {
279
+ content: [{
280
+ type: "text",
281
+ text: `project_info error: ${String(e)}`
282
+ }],
283
+ isError: true
284
+ };
285
+ }
286
+ }
287
+ function toolProjectList(registryDb, params) {
288
+ try {
289
+ const conditions = [];
290
+ const queryParams = [];
291
+ if (params.status) {
292
+ conditions.push("p.status = ?");
293
+ queryParams.push(params.status);
294
+ }
295
+ if (params.tag) {
296
+ conditions.push("p.id IN (SELECT pt.project_id FROM project_tags pt JOIN tags t ON pt.tag_id = t.id WHERE t.name = ?)");
297
+ queryParams.push(params.tag);
298
+ }
299
+ const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
300
+ const limit = params.limit ?? 50;
301
+ queryParams.push(limit);
302
+ const projects = registryDb.prepare(`SELECT p.id, p.slug, p.display_name, p.root_path, p.type, p.status, p.updated_at
303
+ FROM projects p
304
+ ${where}
305
+ ORDER BY p.updated_at DESC
306
+ LIMIT ?`).all(...queryParams);
307
+ if (projects.length === 0) return { content: [{
308
+ type: "text",
309
+ text: "No projects found matching the given filters."
310
+ }] };
311
+ const lines = projects.map((p) => `${p.slug} [${p.status}] ${p.root_path} (updated: ${new Date(p.updated_at).toISOString().slice(0, 10)})`);
312
+ return { content: [{
313
+ type: "text",
314
+ text: `${projects.length} project(s):\n\n${lines.join("\n")}`
315
+ }] };
316
+ } catch (e) {
317
+ return {
318
+ content: [{
319
+ type: "text",
320
+ text: `project_list error: ${String(e)}`
321
+ }],
322
+ isError: true
323
+ };
324
+ }
325
+ }
326
+ function toolProjectDetect(registryDb, params) {
327
+ try {
328
+ const detection = detectProject(registryDb, params.cwd);
329
+ if (!detection) return { content: [{
330
+ type: "text",
331
+ text: `No registered project found for path: ${params.cwd ?? process.cwd()}\n\nRun 'pai project add .' to register this directory.`
332
+ }] };
333
+ return { content: [{
334
+ type: "text",
335
+ text: formatDetectionJson(detection)
336
+ }] };
337
+ } catch (e) {
338
+ return {
339
+ content: [{
340
+ type: "text",
341
+ text: `project_detect error: ${String(e)}`
342
+ }],
343
+ isError: true
344
+ };
345
+ }
346
+ }
347
+ async function toolProjectHealth(registryDb, params) {
348
+ try {
349
+ const { existsSync: fsExists, readdirSync, statSync } = await import("node:fs");
350
+ const { join: pathJoin, basename: pathBasename } = await import("node:path");
351
+ const { homedir } = await import("node:os");
352
+ const { encodeDir: enc } = await import("./utils-QSfKagcj.mjs").then((n) => n.g);
353
+ const rows = registryDb.prepare(`SELECT p.id, p.slug, p.display_name, p.root_path, p.encoded_dir, p.status, p.type,
354
+ (SELECT COUNT(*) FROM sessions s WHERE s.project_id = p.id) AS session_count
355
+ FROM projects p
356
+ ORDER BY p.slug ASC`).all();
357
+ const home = homedir();
358
+ const claudeProjects = pathJoin(home, ".claude", "projects");
359
+ function suggestMoved(rootPath) {
360
+ const name = pathBasename(rootPath);
361
+ return [
362
+ pathJoin(home, "dev", name),
363
+ pathJoin(home, "dev", "ai", name),
364
+ pathJoin(home, "Desktop", name),
365
+ pathJoin(home, "Projects", name)
366
+ ].find((c) => fsExists(c));
367
+ }
368
+ function hasClaudeNotes(encodedDir) {
369
+ if (!fsExists(claudeProjects)) return false;
370
+ try {
371
+ for (const entry of readdirSync(claudeProjects)) {
372
+ if (entry !== encodedDir && !entry.startsWith(encodedDir)) continue;
373
+ const full = pathJoin(claudeProjects, entry);
374
+ try {
375
+ if (!statSync(full).isDirectory()) continue;
376
+ } catch {
377
+ continue;
378
+ }
379
+ if (fsExists(pathJoin(full, "Notes"))) return true;
380
+ }
381
+ } catch {}
382
+ return false;
383
+ }
384
+ function findTodoForProject(rootPath) {
385
+ for (const rel of [
386
+ "Notes/TODO.md",
387
+ ".claude/Notes/TODO.md",
388
+ "tasks/todo.md",
389
+ "TODO.md"
390
+ ]) {
391
+ const full = pathJoin(rootPath, rel);
392
+ if (fsExists(full)) try {
393
+ const raw = readFileSync(full, "utf8");
394
+ return {
395
+ found: true,
396
+ path: rel,
397
+ has_continue: /^## Continue$/m.test(raw)
398
+ };
399
+ } catch {
400
+ return {
401
+ found: true,
402
+ path: rel,
403
+ has_continue: false
404
+ };
405
+ }
406
+ }
407
+ return {
408
+ found: false,
409
+ path: null,
410
+ has_continue: false
411
+ };
412
+ }
413
+ const results = rows.map((p) => {
414
+ const pathExists = fsExists(p.root_path);
415
+ let health;
416
+ let suggestedPath = null;
417
+ if (pathExists) health = "active";
418
+ else {
419
+ suggestedPath = suggestMoved(p.root_path) ?? null;
420
+ health = suggestedPath ? "stale" : "dead";
421
+ }
422
+ const todo = pathExists ? findTodoForProject(p.root_path) : {
423
+ found: false,
424
+ path: null,
425
+ has_continue: false
426
+ };
427
+ return {
428
+ slug: p.slug,
429
+ display_name: p.display_name,
430
+ root_path: p.root_path,
431
+ status: p.status,
432
+ type: p.type,
433
+ session_count: p.session_count,
434
+ health,
435
+ suggested_path: suggestedPath,
436
+ has_claude_notes: hasClaudeNotes(p.encoded_dir),
437
+ todo
438
+ };
439
+ });
440
+ const filtered = !params.category || params.category === "all" ? results : results.filter((r) => r.health === params.category);
441
+ const summary = {
442
+ total: rows.length,
443
+ active: results.filter((r) => r.health === "active").length,
444
+ stale: results.filter((r) => r.health === "stale").length,
445
+ dead: results.filter((r) => r.health === "dead").length
446
+ };
447
+ return { content: [{
448
+ type: "text",
449
+ text: JSON.stringify({
450
+ summary,
451
+ projects: filtered
452
+ }, null, 2)
453
+ }] };
454
+ } catch (e) {
455
+ return {
456
+ content: [{
457
+ type: "text",
458
+ text: `project_health error: ${String(e)}`
459
+ }],
460
+ isError: true
461
+ };
462
+ }
463
+ }
464
+ /**
465
+ * TODO candidate locations searched in priority order.
466
+ * Returns the first one that exists, along with its label.
467
+ */
468
+ const TODO_LOCATIONS = [
469
+ {
470
+ rel: "Notes/TODO.md",
471
+ label: "Notes/TODO.md"
472
+ },
473
+ {
474
+ rel: ".claude/Notes/TODO.md",
475
+ label: ".claude/Notes/TODO.md"
476
+ },
477
+ {
478
+ rel: "tasks/todo.md",
479
+ label: "tasks/todo.md"
480
+ },
481
+ {
482
+ rel: "TODO.md",
483
+ label: "TODO.md"
484
+ }
485
+ ];
486
+ /**
487
+ * Given TODO file content, extract and surface the ## Continue section first,
488
+ * then return the remaining content. Returns an object with:
489
+ * continueSection: string | null
490
+ * fullContent: string
491
+ * hasContinue: boolean
492
+ */
493
+ function parseTodoContent(raw) {
494
+ const lines = raw.split("\n");
495
+ const continueIdx = lines.findIndex((l) => l.trim() === "## Continue");
496
+ if (continueIdx === -1) return {
497
+ continueSection: null,
498
+ fullContent: raw,
499
+ hasContinue: false
500
+ };
501
+ let endIdx = lines.length;
502
+ for (let i = continueIdx + 1; i < lines.length; i++) {
503
+ const trimmed = lines[i].trim();
504
+ if (trimmed === "---" || trimmed.startsWith("##") && trimmed !== "## Continue") {
505
+ endIdx = i;
506
+ break;
507
+ }
508
+ }
509
+ return {
510
+ continueSection: lines.slice(continueIdx, endIdx).join("\n").trim(),
511
+ fullContent: raw,
512
+ hasContinue: true
513
+ };
514
+ }
515
+ function toolProjectTodo(registryDb, params) {
516
+ try {
517
+ let rootPath;
518
+ let projectSlug;
519
+ if (params.project) {
520
+ const projectId = lookupProjectId(registryDb, params.project);
521
+ if (projectId == null) return {
522
+ content: [{
523
+ type: "text",
524
+ text: `Project not found: ${params.project}`
525
+ }],
526
+ isError: true
527
+ };
528
+ const row = registryDb.prepare("SELECT root_path, slug FROM projects WHERE id = ?").get(projectId);
529
+ if (!row) return {
530
+ content: [{
531
+ type: "text",
532
+ text: `Project not found: ${params.project}`
533
+ }],
534
+ isError: true
535
+ };
536
+ rootPath = row.root_path;
537
+ projectSlug = row.slug;
538
+ } else {
539
+ const project = detectProjectFromPath(registryDb, process.cwd());
540
+ if (!project) return { content: [{
541
+ type: "text",
542
+ text: `No PAI project found matching the current directory: ${process.cwd()}\n\nProvide a project slug or run 'pai project add .' to register this directory.`
543
+ }] };
544
+ rootPath = project.root_path;
545
+ projectSlug = project.slug;
546
+ }
547
+ for (const loc of TODO_LOCATIONS) {
548
+ const fullPath = join(rootPath, loc.rel);
549
+ if (existsSync(fullPath)) {
550
+ const { continueSection, fullContent, hasContinue } = parseTodoContent(readFileSync(fullPath, "utf8"));
551
+ let output;
552
+ if (hasContinue && continueSection) output = [
553
+ `TODO found: ${projectSlug}/${loc.label}`,
554
+ "",
555
+ "=== CONTINUE SECTION (surfaced first) ===",
556
+ continueSection,
557
+ "",
558
+ "=== FULL TODO CONTENT ===",
559
+ fullContent
560
+ ].join("\n");
561
+ else output = [
562
+ `TODO found: ${projectSlug}/${loc.label}`,
563
+ "",
564
+ fullContent
565
+ ].join("\n");
566
+ return { content: [{
567
+ type: "text",
568
+ text: output
569
+ }] };
570
+ }
571
+ }
572
+ const searched = TODO_LOCATIONS.map((l) => ` ${rootPath}/${l.rel}`).join("\n");
573
+ return { content: [{
574
+ type: "text",
575
+ text: [
576
+ `No TODO.md found for project: ${projectSlug}`,
577
+ "",
578
+ "Searched locations (in order):",
579
+ searched,
580
+ "",
581
+ "Create a TODO with: echo '## Tasks\\n- [ ] First task' > Notes/TODO.md"
582
+ ].join("\n")
583
+ }] };
584
+ } catch (e) {
585
+ return {
586
+ content: [{
587
+ type: "text",
588
+ text: `project_todo error: ${String(e)}`
589
+ }],
590
+ isError: true
591
+ };
592
+ }
593
+ }
594
+
595
+ //#endregion
596
+ //#region src/mcp/tools/sessions.ts
597
+ function toolSessionList(registryDb, params) {
598
+ try {
599
+ const projectId = lookupProjectId(registryDb, params.project);
600
+ if (projectId == null) return {
601
+ content: [{
602
+ type: "text",
603
+ text: `Project not found: ${params.project}`
604
+ }],
605
+ isError: true
606
+ };
607
+ const conditions = ["project_id = ?"];
608
+ const queryParams = [projectId];
609
+ if (params.status) {
610
+ conditions.push("status = ?");
611
+ queryParams.push(params.status);
612
+ }
613
+ const limit = params.limit ?? 10;
614
+ queryParams.push(limit);
615
+ const sessions = registryDb.prepare(`SELECT number, date, title, filename, status
616
+ FROM sessions
617
+ WHERE ${conditions.join(" AND ")}
618
+ ORDER BY number DESC
619
+ LIMIT ?`).all(...queryParams);
620
+ if (sessions.length === 0) return { content: [{
621
+ type: "text",
622
+ text: `No sessions found for project: ${params.project}`
623
+ }] };
624
+ const lines = sessions.map((s) => `#${String(s.number).padStart(4, "0")} ${s.date} [${s.status}] ${s.title}\n file: Notes/${s.filename}`);
625
+ return { content: [{
626
+ type: "text",
627
+ text: `${sessions.length} session(s) for ${params.project}:\n\n${lines.join("\n\n")}`
628
+ }] };
629
+ } catch (e) {
630
+ return {
631
+ content: [{
632
+ type: "text",
633
+ text: `session_list error: ${String(e)}`
634
+ }],
635
+ isError: true
636
+ };
637
+ }
638
+ }
639
+ /**
640
+ * Automatically suggest which project a session belongs to.
641
+ *
642
+ * Strategy (in priority order):
643
+ * 1. path — exact or parent-directory match in the project registry
644
+ * 2. marker — walk up from cwd looking for Notes/PAI.md
645
+ * 3. topic — BM25 keyword search against memory (requires context)
646
+ *
647
+ * Call this at session start (e.g., from CLAUDE.md or a session-start hook)
648
+ * to automatically route the session to the correct project.
649
+ */
650
+ async function toolSessionRoute(registryDb, federation, params) {
651
+ try {
652
+ const { autoRoute, formatAutoRouteJson } = await import("./auto-route-C-DrW6BL.mjs");
653
+ const result = await autoRoute(registryDb, federation, params.cwd, params.context);
654
+ if (!result) return { content: [{
655
+ type: "text",
656
+ text: [
657
+ `No project match found for: ${params.cwd ?? process.cwd()}`,
658
+ "",
659
+ "Tried: path match, PAI.md marker walk" + (params.context ? ", topic detection" : ""),
660
+ "",
661
+ "Run 'pai project add .' to register this directory,",
662
+ "or provide conversation context for topic-based routing."
663
+ ].join("\n")
664
+ }] };
665
+ return { content: [{
666
+ type: "text",
667
+ text: formatAutoRouteJson(result)
668
+ }] };
669
+ } catch (e) {
670
+ return {
671
+ content: [{
672
+ type: "text",
673
+ text: `session_route error: ${String(e)}`
674
+ }],
675
+ isError: true
676
+ };
677
+ }
678
+ }
679
+
680
+ //#endregion
681
+ //#region src/mcp/tools/registry.ts
682
+ function toolRegistrySearch(registryDb, params) {
683
+ try {
684
+ const q = `%${params.query}%`;
685
+ const projects = registryDb.prepare(`SELECT id, slug, display_name, root_path, type, status, updated_at
686
+ FROM projects
687
+ WHERE slug LIKE ?
688
+ OR display_name LIKE ?
689
+ OR root_path LIKE ?
690
+ ORDER BY updated_at DESC
691
+ LIMIT 20`).all(q, q, q);
692
+ if (projects.length === 0) return { content: [{
693
+ type: "text",
694
+ text: `No projects found matching: "${params.query}"`
695
+ }] };
696
+ const lines = projects.map((p) => `${p.slug} [${p.status}] ${p.root_path}`);
697
+ return { content: [{
698
+ type: "text",
699
+ text: `${projects.length} match(es) for "${params.query}":\n\n${lines.join("\n")}`
700
+ }] };
701
+ } catch (e) {
702
+ return {
703
+ content: [{
704
+ type: "text",
705
+ text: `registry_search error: ${String(e)}`
706
+ }],
707
+ isError: true
708
+ };
709
+ }
710
+ }
711
+
712
+ //#endregion
713
+ //#region src/mcp/tools/zettel.ts
714
+ async function toolZettelExplore(backend, params) {
715
+ try {
716
+ const { zettelExplore } = await import("./zettelkasten-DhBKZQHF.mjs");
717
+ const result = await zettelExplore(backend, {
718
+ startNote: params.start_note,
719
+ depth: params.depth,
720
+ direction: params.direction,
721
+ mode: params.mode
722
+ });
723
+ return { content: [{
724
+ type: "text",
725
+ text: JSON.stringify(result, null, 2)
726
+ }] };
727
+ } catch (e) {
728
+ return {
729
+ content: [{
730
+ type: "text",
731
+ text: `zettel_explore error: ${String(e)}`
732
+ }],
733
+ isError: true
734
+ };
735
+ }
736
+ }
737
+ async function toolZettelHealth(backend, params) {
738
+ try {
739
+ const { zettelHealth } = await import("./zettelkasten-DhBKZQHF.mjs");
740
+ const result = await zettelHealth(backend, {
741
+ scope: params.scope,
742
+ projectPath: params.project_path,
743
+ recentDays: params.recent_days,
744
+ include: params.include
745
+ });
746
+ return { content: [{
747
+ type: "text",
748
+ text: JSON.stringify(result, null, 2)
749
+ }] };
750
+ } catch (e) {
751
+ return {
752
+ content: [{
753
+ type: "text",
754
+ text: `zettel_health error: ${String(e)}`
755
+ }],
756
+ isError: true
757
+ };
758
+ }
759
+ }
760
+ async function toolZettelSurprise(backend, params) {
761
+ try {
762
+ const { zettelSurprise } = await import("./zettelkasten-DhBKZQHF.mjs");
763
+ const results = await zettelSurprise(backend, {
764
+ referencePath: params.reference_path,
765
+ vaultProjectId: params.vault_project_id,
766
+ limit: params.limit,
767
+ minSimilarity: params.min_similarity,
768
+ minGraphDistance: params.min_graph_distance
769
+ });
770
+ return { content: [{
771
+ type: "text",
772
+ text: JSON.stringify(results, null, 2)
773
+ }] };
774
+ } catch (e) {
775
+ return {
776
+ content: [{
777
+ type: "text",
778
+ text: `zettel_surprise error: ${String(e)}`
779
+ }],
780
+ isError: true
781
+ };
782
+ }
783
+ }
784
+ async function toolZettelSuggest(backend, params) {
785
+ try {
786
+ const { zettelSuggest } = await import("./zettelkasten-DhBKZQHF.mjs");
787
+ const results = await zettelSuggest(backend, {
788
+ notePath: params.note_path,
789
+ vaultProjectId: params.vault_project_id,
790
+ limit: params.limit,
791
+ excludeLinked: params.exclude_linked
792
+ });
793
+ return { content: [{
794
+ type: "text",
795
+ text: JSON.stringify(results, null, 2)
796
+ }] };
797
+ } catch (e) {
798
+ return {
799
+ content: [{
800
+ type: "text",
801
+ text: `zettel_suggest error: ${String(e)}`
802
+ }],
803
+ isError: true
804
+ };
805
+ }
806
+ }
807
+ async function toolZettelConverse(backend, params) {
808
+ try {
809
+ const { zettelConverse } = await import("./zettelkasten-DhBKZQHF.mjs");
810
+ const result = await zettelConverse(backend, {
811
+ question: params.question,
812
+ vaultProjectId: params.vault_project_id,
813
+ depth: params.depth,
814
+ limit: params.limit
815
+ });
816
+ try {
817
+ const { saveQueryResult } = await import("./query-feedback-Dv43XKHM.mjs").then((n) => n.t);
818
+ saveQueryResult({
819
+ query: params.question,
820
+ timestamp: Date.now(),
821
+ source: "zettel_converse",
822
+ sourceSlugs: result.relevantNotes.slice(0, 5).map((n) => n.path),
823
+ answerPreview: result.relevantNotes.slice(0, 3).map((n) => {
824
+ return `${n.title ?? "(untitled)"}: ${n.snippet.trim().slice(0, 100)}`;
825
+ }).join(" | "),
826
+ resultCount: result.relevantNotes.length
827
+ });
828
+ } catch {}
829
+ return { content: [{
830
+ type: "text",
831
+ text: JSON.stringify(result, null, 2)
832
+ }] };
833
+ } catch (e) {
834
+ return {
835
+ content: [{
836
+ type: "text",
837
+ text: `zettel_converse error: ${String(e)}`
838
+ }],
839
+ isError: true
840
+ };
841
+ }
842
+ }
843
+ async function toolZettelThemes(backend, params) {
844
+ try {
845
+ const { zettelThemes } = await import("./zettelkasten-DhBKZQHF.mjs");
846
+ const result = await zettelThemes(backend, {
847
+ vaultProjectId: params.vault_project_id,
848
+ lookbackDays: params.lookback_days,
849
+ minClusterSize: params.min_cluster_size,
850
+ maxThemes: params.max_themes,
851
+ similarityThreshold: params.similarity_threshold
852
+ });
853
+ return { content: [{
854
+ type: "text",
855
+ text: JSON.stringify(result, null, 2)
856
+ }] };
857
+ } catch (e) {
858
+ return {
859
+ content: [{
860
+ type: "text",
861
+ text: `zettel_themes error: ${String(e)}`
862
+ }],
863
+ isError: true
864
+ };
865
+ }
866
+ }
867
+ async function toolZettelGodNotes(backend, params) {
868
+ try {
869
+ const { zettelGodNotes } = await import("./zettelkasten-DhBKZQHF.mjs");
870
+ const result = await zettelGodNotes(backend, {
871
+ limit: params.limit,
872
+ minInbound: params.min_inbound
873
+ });
874
+ return { content: [{
875
+ type: "text",
876
+ text: JSON.stringify(result, null, 2)
877
+ }] };
878
+ } catch (e) {
879
+ return {
880
+ content: [{
881
+ type: "text",
882
+ text: `zettel_god_notes error: ${String(e)}`
883
+ }],
884
+ isError: true
885
+ };
886
+ }
887
+ }
888
+ async function toolZettelCommunities(backend, params) {
889
+ try {
890
+ const { zettelCommunities } = await import("./zettelkasten-DhBKZQHF.mjs");
891
+ const result = await zettelCommunities(backend, {
892
+ minSize: params.min_size,
893
+ maxCommunities: params.max_communities,
894
+ resolution: params.resolution
895
+ });
896
+ return { content: [{
897
+ type: "text",
898
+ text: JSON.stringify(result, null, 2)
899
+ }] };
900
+ } catch (e) {
901
+ return {
902
+ content: [{
903
+ type: "text",
904
+ text: `zettel_communities error: ${String(e)}`
905
+ }],
906
+ isError: true
907
+ };
908
+ }
909
+ }
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
+
1695
+ //#endregion
1696
+ //#region src/mcp/tools.ts
1697
+ var tools_exports = /* @__PURE__ */ __exportAll({
1698
+ combineHybridResults: () => combineHybridResults,
1699
+ detectProjectFromPath: () => detectProjectFromPath,
1700
+ formatProject: () => formatProject,
1701
+ lookupProjectId: () => lookupProjectId,
1702
+ toolKgAdd: () => toolKgAdd,
1703
+ toolKgContradictions: () => toolKgContradictions,
1704
+ toolKgInvalidate: () => toolKgInvalidate,
1705
+ toolKgQuery: () => toolKgQuery,
1706
+ toolMemoryGet: () => toolMemoryGet,
1707
+ toolMemorySearch: () => toolMemorySearch,
1708
+ toolMemoryTaxonomy: () => toolMemoryTaxonomy,
1709
+ toolMemoryTunnels: () => toolMemoryTunnels,
1710
+ toolMemoryWakeup: () => toolMemoryWakeup,
1711
+ toolProjectDetect: () => toolProjectDetect,
1712
+ toolProjectHealth: () => toolProjectHealth,
1713
+ toolProjectInfo: () => toolProjectInfo,
1714
+ toolProjectList: () => toolProjectList,
1715
+ toolProjectTodo: () => toolProjectTodo,
1716
+ toolRegistrySearch: () => toolRegistrySearch,
1717
+ toolSessionList: () => toolSessionList,
1718
+ toolSessionRoute: () => toolSessionRoute,
1719
+ toolZettelCommunities: () => toolZettelCommunities,
1720
+ toolZettelConverse: () => toolZettelConverse,
1721
+ toolZettelExplore: () => toolZettelExplore,
1722
+ toolZettelGodNotes: () => toolZettelGodNotes,
1723
+ toolZettelHealth: () => toolZettelHealth,
1724
+ toolZettelSuggest: () => toolZettelSuggest,
1725
+ toolZettelSurprise: () => toolZettelSurprise,
1726
+ toolZettelThemes: () => toolZettelThemes
1727
+ });
1728
+
1729
+ //#endregion
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