clawvault 2.5.3 → 2.6.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 (95) hide show
  1. package/README.md +159 -159
  2. package/bin/clawvault.js +111 -111
  3. package/bin/command-registration.test.js +166 -166
  4. package/bin/command-runtime.js +93 -93
  5. package/bin/command-runtime.test.js +154 -154
  6. package/bin/help-contract.test.js +39 -39
  7. package/bin/register-config-commands.js +153 -153
  8. package/bin/register-config-route-commands.test.js +121 -121
  9. package/bin/register-core-commands.js +237 -237
  10. package/bin/register-kanban-commands.js +56 -56
  11. package/bin/register-kanban-commands.test.js +83 -83
  12. package/bin/register-maintenance-commands.js +282 -282
  13. package/bin/register-project-commands.js +209 -209
  14. package/bin/register-project-commands.test.js +206 -206
  15. package/bin/register-query-commands.js +317 -317
  16. package/bin/register-query-commands.test.js +65 -65
  17. package/bin/register-resilience-commands.js +182 -182
  18. package/bin/register-resilience-commands.test.js +81 -81
  19. package/bin/register-route-commands.js +114 -114
  20. package/bin/register-session-lifecycle-commands.js +206 -206
  21. package/bin/register-tailscale-commands.js +106 -106
  22. package/bin/register-task-commands.js +348 -348
  23. package/bin/register-task-commands.test.js +69 -69
  24. package/bin/register-template-commands.js +75 -72
  25. package/bin/register-template-commands.test.js +87 -0
  26. package/bin/register-vault-operations-commands.js +300 -300
  27. package/bin/test-helpers/cli-command-fixtures.js +119 -119
  28. package/dashboard/lib/graph-diff.js +104 -104
  29. package/dashboard/lib/graph-diff.test.js +75 -75
  30. package/dashboard/lib/vault-parser.js +556 -556
  31. package/dashboard/lib/vault-parser.test.js +254 -254
  32. package/dashboard/public/app.js +796 -796
  33. package/dashboard/public/index.html +52 -52
  34. package/dashboard/public/styles.css +221 -221
  35. package/dashboard/server.js +374 -374
  36. package/dist/{chunk-J5EMBUPK.js → chunk-4OXMU5S2.js} +1 -1
  37. package/dist/{chunk-3FP5BJ42.js → chunk-4QYGFWRM.js} +1 -1
  38. package/dist/{chunk-4IV3R2F5.js → chunk-4TE4JMLA.js} +1 -1
  39. package/dist/{chunk-5GZFTAL7.js → chunk-AZYOKJYC.js} +128 -42
  40. package/dist/{chunk-FG6RJMCN.js → chunk-HA5M6KJB.js} +4 -4
  41. package/dist/{chunk-IZEY5S74.js → chunk-IEVLHNLU.js} +1 -1
  42. package/dist/{chunk-CLE2HHNT.js → chunk-IVRIKYFE.js} +18 -11
  43. package/dist/{chunk-AY4PGUVL.js → chunk-KL4NAOMO.js} +1 -1
  44. package/dist/{chunk-O7XHXF7F.js → chunk-MAKNAHAW.js} +4 -4
  45. package/dist/{chunk-OSMS7QIG.js → chunk-ME37YNW3.js} +2 -2
  46. package/dist/chunk-MFAWT5O5.js +301 -0
  47. package/dist/{chunk-TPDH3JPP.js → chunk-PBEE567J.js} +1 -1
  48. package/dist/{chunk-S2IG7VNM.js → chunk-Q2J5YTUF.js} +2 -2
  49. package/dist/{chunk-IOALNTAN.js → chunk-QWQ3TIKS.js} +103 -29
  50. package/dist/{chunk-YCVDVI5B.js → chunk-R2MIW5G7.js} +1 -1
  51. package/dist/{chunk-M25QVSJM.js → chunk-RVYA52PY.js} +1 -1
  52. package/dist/{chunk-NZ4ZZNSR.js → chunk-THRJVD4L.js} +1 -1
  53. package/dist/{chunk-4GBPTBFJ.js → chunk-TIGW564L.js} +1 -1
  54. package/dist/{chunk-LMEMZGUV.js → chunk-UEOUADMO.js} +3 -3
  55. package/dist/{chunk-GFJ3LIIB.js → chunk-XAVB4GB4.js} +1 -1
  56. package/dist/cli/index.js +15 -13
  57. package/dist/commands/backlog.js +3 -1
  58. package/dist/commands/blocked.js +3 -1
  59. package/dist/commands/canvas.js +3 -1
  60. package/dist/commands/context.js +3 -3
  61. package/dist/commands/doctor.js +9 -7
  62. package/dist/commands/embed.js +2 -2
  63. package/dist/commands/kanban.js +4 -2
  64. package/dist/commands/observe.js +7 -5
  65. package/dist/commands/project.js +5 -3
  66. package/dist/commands/rebuild.js +6 -4
  67. package/dist/commands/replay.js +6 -4
  68. package/dist/commands/setup.js +2 -2
  69. package/dist/commands/sleep.js +7 -5
  70. package/dist/commands/status.js +8 -6
  71. package/dist/commands/tailscale.js +3 -3
  72. package/dist/commands/task.js +4 -2
  73. package/dist/commands/template.d.ts +10 -1
  74. package/dist/commands/template.js +47 -55
  75. package/dist/commands/wake.js +2 -2
  76. package/dist/index.js +23 -22
  77. package/dist/lib/project-utils.js +4 -2
  78. package/dist/lib/tailscale.js +2 -2
  79. package/dist/lib/task-utils.d.ts +14 -13
  80. package/dist/lib/task-utils.js +3 -1
  81. package/dist/lib/template-engine.d.ts +1 -0
  82. package/dist/lib/webdav.js +1 -1
  83. package/hooks/clawvault/HOOK.md +83 -83
  84. package/hooks/clawvault/handler.js +816 -816
  85. package/hooks/clawvault/handler.test.js +263 -263
  86. package/package.json +94 -94
  87. package/templates/checkpoint.md +34 -19
  88. package/templates/daily-note.md +34 -19
  89. package/templates/daily.md +34 -19
  90. package/templates/decision.md +39 -17
  91. package/templates/handoff.md +34 -19
  92. package/templates/lesson.md +31 -16
  93. package/templates/person.md +37 -19
  94. package/templates/project.md +84 -23
  95. package/templates/task.md +81 -0
@@ -1,556 +1,556 @@
1
- import * as fs from 'node:fs/promises';
2
- import * as path from 'node:path';
3
- import matter from 'gray-matter';
4
-
5
- export const WIKI_LINK_REGEX = /\[\[([^\]|]+)(\|[^\]]+)?\]\]/g;
6
- const HASH_TAG_REGEX = /(^|\s)#([\w-]+)/g;
7
- const MEMORY_GRAPH_INDEX_PATH = ['.clawvault', 'graph-index.json'];
8
-
9
- const MARKDOWN_EXT = '.md';
10
- const IGNORED_DIRECTORIES = new Set([
11
- '.git',
12
- '.obsidian',
13
- '.trash',
14
- 'node_modules'
15
- ]);
16
- const FRONTMATTER_RELATION_FIELDS = [
17
- 'related',
18
- 'depends_on',
19
- 'dependsOn',
20
- 'blocked_by',
21
- 'blocks',
22
- 'owner',
23
- 'project',
24
- 'people',
25
- 'links'
26
- ];
27
-
28
- /**
29
- * Build a graph from markdown notes in a vault.
30
- * @param {string} vaultPath
31
- * @param {{ includeDangling?: boolean }} [options]
32
- */
33
- export async function buildVaultGraph(vaultPath, options = {}) {
34
- const includeDangling = options.includeDangling !== false;
35
- const root = path.resolve(vaultPath);
36
- const preferIndex = options.preferIndex !== false;
37
- const validateIndexFreshness = options.validateIndexFreshness !== false;
38
-
39
- if (preferIndex) {
40
- const indexedGraph = await loadGraphFromMemoryIndex(root, {
41
- includeDangling,
42
- validateFreshness: validateIndexFreshness
43
- });
44
- if (indexedGraph) {
45
- return indexedGraph;
46
- }
47
- }
48
-
49
- const markdownFiles = await collectMarkdownFiles(root);
50
-
51
- const nodesById = new Map();
52
- const edgesByKey = new Map();
53
- const edgeSet = new Set();
54
-
55
- for (const absoluteFilePath of markdownFiles) {
56
- const raw = await fs.readFile(absoluteFilePath, 'utf8');
57
- const parsed = matter(raw);
58
- const relativePath = path.relative(root, absoluteFilePath);
59
- const id = toNodeId(relativePath);
60
- const frontmatter = parsed.data ?? {};
61
-
62
- nodesById.set(id, {
63
- id,
64
- title: normalizeString(frontmatter.title) || toDisplayTitle(id),
65
- category: normalizeString(frontmatter.category) || inferCategory(id),
66
- type: inferNodeType(id, frontmatter),
67
- tags: normalizeTags(frontmatter.tags),
68
- path: toPosixPath(relativePath),
69
- missing: false,
70
- _outboundTargets: extractWikiLinks(parsed.content),
71
- _frontmatterRelations: extractFrontmatterRelations(frontmatter),
72
- _inlineTags: extractInlineTags(parsed.content)
73
- });
74
- }
75
-
76
- const idsByLowercase = new Map();
77
- const idsByBaseName = new Map();
78
- for (const id of nodesById.keys()) {
79
- idsByLowercase.set(id.toLowerCase(), id);
80
- const baseName = path.posix.basename(id).toLowerCase();
81
- const existing = idsByBaseName.get(baseName) ?? new Set();
82
- existing.add(id);
83
- idsByBaseName.set(baseName, existing);
84
- }
85
-
86
- function ensureUnresolvedNode(targetId) {
87
- if (nodesById.has(targetId) || !includeDangling) {
88
- return;
89
- }
90
- nodesById.set(targetId, {
91
- id: targetId,
92
- title: toDisplayTitle(targetId),
93
- category: 'unresolved',
94
- type: 'unresolved',
95
- tags: [],
96
- path: null,
97
- missing: true,
98
- _outboundTargets: [],
99
- _frontmatterRelations: [],
100
- _inlineTags: []
101
- });
102
- }
103
-
104
- function addEdge(sourceId, targetId, type, label) {
105
- const edgeKey = `${type}:${sourceId}=>${targetId}${label ? `:${label}` : ''}`;
106
- if (edgeSet.has(edgeKey)) {
107
- return;
108
- }
109
- edgeSet.add(edgeKey);
110
- edgesByKey.set(edgeKey, {
111
- source: sourceId,
112
- target: targetId,
113
- type,
114
- label
115
- });
116
- }
117
-
118
- for (const node of nodesById.values()) {
119
- const tagSet = new Set([...(node.tags ?? []), ...(node._inlineTags ?? [])]);
120
- for (const inlineTag of tagSet) {
121
- const tagNodeId = `tag:${inlineTag.toLowerCase()}`;
122
- if (!nodesById.has(tagNodeId)) {
123
- nodesById.set(tagNodeId, {
124
- id: tagNodeId,
125
- title: `#${inlineTag}`,
126
- category: 'tag',
127
- type: 'tag',
128
- tags: [],
129
- path: null,
130
- missing: false,
131
- _outboundTargets: [],
132
- _frontmatterRelations: [],
133
- _inlineTags: []
134
- });
135
- }
136
- addEdge(node.id, tagNodeId, 'tag');
137
- }
138
-
139
- for (const rawTarget of node._outboundTargets) {
140
- const targetId = resolveTargetId(rawTarget, {
141
- idsByLowercase,
142
- idsByBaseName,
143
- includeDangling
144
- });
145
- if (!targetId) {
146
- continue;
147
- }
148
- ensureUnresolvedNode(targetId);
149
- addEdge(node.id, targetId, 'wiki_link');
150
- }
151
-
152
- for (const relation of node._frontmatterRelations) {
153
- const targetId = resolveTargetId(relation.target, {
154
- idsByLowercase,
155
- idsByBaseName,
156
- includeDangling
157
- });
158
- if (!targetId) {
159
- continue;
160
- }
161
- ensureUnresolvedNode(targetId);
162
- addEdge(node.id, targetId, 'frontmatter_relation', relation.field);
163
- }
164
- }
165
-
166
- const edges = Array.from(edgesByKey.values());
167
- const degreeByNodeId = new Map();
168
- for (const edge of edges) {
169
- degreeByNodeId.set(edge.source, (degreeByNodeId.get(edge.source) ?? 0) + 1);
170
- degreeByNodeId.set(edge.target, (degreeByNodeId.get(edge.target) ?? 0) + 1);
171
- }
172
-
173
- const nodeTypeCounts = {};
174
- const nodes = Array.from(nodesById.values())
175
- .map(({ _outboundTargets, _frontmatterRelations, _inlineTags, ...node }) => {
176
- nodeTypeCounts[node.type] = (nodeTypeCounts[node.type] ?? 0) + 1;
177
- return {
178
- ...node,
179
- degree: degreeByNodeId.get(node.id) ?? 0
180
- };
181
- })
182
- .sort((a, b) => a.id.localeCompare(b.id));
183
-
184
- const edgeTypeCounts = {};
185
- for (const edge of edges) {
186
- edgeTypeCounts[edge.type] = (edgeTypeCounts[edge.type] ?? 0) + 1;
187
- }
188
-
189
- edges.sort((a, b) => {
190
- const sourceSort = String(a.source).localeCompare(String(b.source));
191
- if (sourceSort !== 0) return sourceSort;
192
- const targetSort = String(a.target).localeCompare(String(b.target));
193
- if (targetSort !== 0) return targetSort;
194
- const typeSort = String(a.type || '').localeCompare(String(b.type || ''));
195
- if (typeSort !== 0) return typeSort;
196
- return String(a.label || '').localeCompare(String(b.label || ''));
197
- });
198
-
199
- return {
200
- nodes,
201
- edges,
202
- stats: {
203
- generatedAt: new Date().toISOString(),
204
- nodeCount: nodes.length,
205
- edgeCount: edges.length,
206
- fileCount: markdownFiles.length,
207
- nodeTypeCounts,
208
- edgeTypeCounts
209
- }
210
- };
211
- }
212
-
213
- async function loadGraphFromMemoryIndex(root, options = {}) {
214
- const includeDangling = options.includeDangling !== false;
215
- const validateFreshness = options.validateFreshness !== false;
216
- const indexPath = path.join(root, ...MEMORY_GRAPH_INDEX_PATH);
217
-
218
- let parsed;
219
- try {
220
- const raw = await fs.readFile(indexPath, 'utf8');
221
- parsed = JSON.parse(raw);
222
- } catch {
223
- return null;
224
- }
225
-
226
- const graph = parsed?.graph;
227
- if (!graph || !Array.isArray(graph.nodes) || !Array.isArray(graph.edges)) {
228
- return null;
229
- }
230
-
231
- if (validateFreshness) {
232
- const fresh = await isIndexFresh(root, parsed);
233
- if (!fresh) {
234
- return null;
235
- }
236
- }
237
-
238
- const nodeById = new Map();
239
- for (const node of graph.nodes) {
240
- const mappedId = fromIndexedNodeId(node?.id, node?.type);
241
- if (!mappedId) continue;
242
- if (node?.missing && !includeDangling) continue;
243
-
244
- nodeById.set(mappedId, {
245
- id: mappedId,
246
- title: normalizeString(node?.title) || toDisplayTitle(mappedId),
247
- category: normalizeString(node?.category) || 'root',
248
- type: normalizeString(node?.type) || 'note',
249
- tags: normalizeTags(node?.tags),
250
- path: typeof node?.path === 'string' ? toPosixPath(node.path) : null,
251
- missing: Boolean(node?.missing),
252
- degree: Number(node?.degree ?? 0)
253
- });
254
- }
255
-
256
- const edges = [];
257
- for (const edge of graph.edges) {
258
- const source = fromIndexedNodeId(edge?.source);
259
- const target = fromIndexedNodeId(edge?.target);
260
- if (!source || !target) continue;
261
- if (!nodeById.has(source) || !nodeById.has(target)) continue;
262
- edges.push({
263
- source,
264
- target,
265
- type: normalizeString(edge?.type) || 'wiki_link',
266
- label: normalizeString(edge?.label) || undefined
267
- });
268
- }
269
-
270
- const degreeByNodeId = new Map();
271
- for (const edge of edges) {
272
- degreeByNodeId.set(edge.source, (degreeByNodeId.get(edge.source) ?? 0) + 1);
273
- degreeByNodeId.set(edge.target, (degreeByNodeId.get(edge.target) ?? 0) + 1);
274
- }
275
-
276
- const nodeTypeCounts = {};
277
- const nodes = Array.from(nodeById.values())
278
- .map((node) => {
279
- node.degree = degreeByNodeId.get(node.id) ?? 0;
280
- nodeTypeCounts[node.type] = (nodeTypeCounts[node.type] ?? 0) + 1;
281
- return node;
282
- })
283
- .sort((a, b) => a.id.localeCompare(b.id));
284
-
285
- const edgeTypeCounts = {};
286
- for (const edge of edges) {
287
- edgeTypeCounts[edge.type] = (edgeTypeCounts[edge.type] ?? 0) + 1;
288
- }
289
-
290
- edges.sort((a, b) => {
291
- const sourceSort = String(a.source).localeCompare(String(b.source));
292
- if (sourceSort !== 0) return sourceSort;
293
- const targetSort = String(a.target).localeCompare(String(b.target));
294
- if (targetSort !== 0) return targetSort;
295
- const typeSort = String(a.type || '').localeCompare(String(b.type || ''));
296
- if (typeSort !== 0) return typeSort;
297
- return String(a.label || '').localeCompare(String(b.label || ''));
298
- });
299
-
300
- return {
301
- nodes,
302
- edges,
303
- stats: {
304
- generatedAt: normalizeString(graph?.stats?.generatedAt) || new Date().toISOString(),
305
- nodeCount: nodes.length,
306
- edgeCount: edges.length,
307
- fileCount: Number.isFinite(Number(parsed?.files ? Object.keys(parsed.files).length : graph?.stats?.fileCount))
308
- ? Number(parsed?.files ? Object.keys(parsed.files).length : graph?.stats?.fileCount)
309
- : 0,
310
- nodeTypeCounts,
311
- edgeTypeCounts
312
- }
313
- };
314
- }
315
-
316
- async function isIndexFresh(root, parsedIndex) {
317
- const indexedFiles = parsedIndex?.files;
318
- if (!indexedFiles || typeof indexedFiles !== 'object') {
319
- return false;
320
- }
321
-
322
- const markdownFiles = await collectMarkdownFiles(root);
323
- const normalizedCurrent = markdownFiles
324
- .map((absolutePath) => toPosixPath(path.relative(root, absolutePath)))
325
- .sort((a, b) => a.localeCompare(b));
326
- const indexedPaths = Object.keys(indexedFiles).sort((a, b) => a.localeCompare(b));
327
-
328
- if (normalizedCurrent.length !== indexedPaths.length) {
329
- return false;
330
- }
331
-
332
- for (let index = 0; index < normalizedCurrent.length; index += 1) {
333
- if (normalizedCurrent[index] !== indexedPaths[index]) {
334
- return false;
335
- }
336
- }
337
-
338
- for (const relativePath of indexedPaths) {
339
- const fragment = indexedFiles[relativePath];
340
- const expectedMtime = Number(fragment?.mtimeMs);
341
- if (!Number.isFinite(expectedMtime)) {
342
- return false;
343
- }
344
- const absolutePath = path.join(root, relativePath);
345
- let stat;
346
- try {
347
- stat = await fs.stat(absolutePath);
348
- } catch {
349
- return false;
350
- }
351
- if (Math.abs(stat.mtimeMs - expectedMtime) > 1) {
352
- return false;
353
- }
354
- }
355
-
356
- return true;
357
- }
358
-
359
- async function collectMarkdownFiles(root) {
360
- const pending = [root];
361
- const files = [];
362
-
363
- while (pending.length > 0) {
364
- const currentDir = pending.pop();
365
- const entries = await fs.readdir(currentDir, { withFileTypes: true });
366
-
367
- for (const entry of entries) {
368
- const absolutePath = path.join(currentDir, entry.name);
369
- if (entry.isDirectory()) {
370
- if (!IGNORED_DIRECTORIES.has(entry.name)) {
371
- pending.push(absolutePath);
372
- }
373
- continue;
374
- }
375
-
376
- if (entry.isFile() && entry.name.toLowerCase().endsWith(MARKDOWN_EXT)) {
377
- files.push(absolutePath);
378
- }
379
- }
380
- }
381
-
382
- return files;
383
- }
384
-
385
- function extractWikiLinks(markdown) {
386
- const links = [];
387
- const regex = new RegExp(WIKI_LINK_REGEX.source, 'g');
388
- let match = regex.exec(markdown);
389
-
390
- while (match) {
391
- const rawTarget = match[1]?.trim();
392
- if (rawTarget) {
393
- links.push(rawTarget);
394
- }
395
- match = regex.exec(markdown);
396
- }
397
-
398
- return links;
399
- }
400
-
401
- function extractInlineTags(markdown) {
402
- const tags = new Set();
403
- for (const match of markdown.matchAll(HASH_TAG_REGEX)) {
404
- const tag = normalizeString(match[2])?.toLowerCase();
405
- if (tag) {
406
- tags.add(tag);
407
- }
408
- }
409
- return [...tags];
410
- }
411
-
412
- function extractFrontmatterRelations(frontmatter) {
413
- const relations = [];
414
- for (const field of FRONTMATTER_RELATION_FIELDS) {
415
- const raw = frontmatter?.[field];
416
- if (typeof raw === 'string') {
417
- for (const target of raw.split(',').map((value) => normalizeWikiTarget(value)).filter(Boolean)) {
418
- relations.push({ field, target });
419
- }
420
- continue;
421
- }
422
- if (Array.isArray(raw)) {
423
- for (const entry of raw) {
424
- if (typeof entry !== 'string') continue;
425
- for (const target of entry.split(',').map((value) => normalizeWikiTarget(value)).filter(Boolean)) {
426
- relations.push({ field, target });
427
- }
428
- }
429
- }
430
- }
431
- return relations;
432
- }
433
-
434
- function resolveTargetId(target, context) {
435
- const normalized = normalizeWikiTarget(target);
436
- if (!normalized) {
437
- return null;
438
- }
439
-
440
- const lower = normalized.toLowerCase();
441
- const exact = context.idsByLowercase.get(lower);
442
- if (exact) {
443
- return exact;
444
- }
445
-
446
- if (!normalized.includes('/')) {
447
- const maybeMatches = context.idsByBaseName.get(lower);
448
- if (maybeMatches?.size === 1) {
449
- return Array.from(maybeMatches)[0];
450
- }
451
- }
452
-
453
- return context.includeDangling ? normalized : null;
454
- }
455
-
456
- function normalizeWikiTarget(target) {
457
- let value = normalizeString(target);
458
- if (!value) {
459
- return null;
460
- }
461
-
462
- const hashIndex = value.indexOf('#');
463
- if (hashIndex >= 0) {
464
- value = value.slice(0, hashIndex);
465
- }
466
-
467
- const caretIndex = value.indexOf('^');
468
- if (caretIndex >= 0) {
469
- value = value.slice(0, caretIndex);
470
- }
471
-
472
- value = value.replace(/\\/g, '/');
473
- value = value.replace(/^\.\//, '');
474
- value = value.replace(/^\/+/, '');
475
- value = value.replace(/\/+/g, '/');
476
-
477
- if (value.toLowerCase().endsWith(MARKDOWN_EXT)) {
478
- value = value.slice(0, -MARKDOWN_EXT.length);
479
- }
480
-
481
- return normalizeString(value);
482
- }
483
-
484
- function fromIndexedNodeId(value, nodeType = '') {
485
- const raw = normalizeString(value);
486
- if (!raw) return '';
487
- if (raw.startsWith('note:')) {
488
- return raw.slice(5);
489
- }
490
- if (raw.startsWith('tag:')) {
491
- return raw;
492
- }
493
- if (raw.startsWith('unresolved:')) {
494
- return raw.slice('unresolved:'.length) || raw;
495
- }
496
- if (normalizeString(nodeType).toLowerCase() === 'tag') {
497
- return raw.startsWith('tag:') ? raw : `tag:${raw}`;
498
- }
499
- return raw;
500
- }
501
-
502
- function toNodeId(relativePath) {
503
- const normalized = toPosixPath(relativePath);
504
- return normalized.toLowerCase().endsWith(MARKDOWN_EXT)
505
- ? normalized.slice(0, -MARKDOWN_EXT.length)
506
- : normalized;
507
- }
508
-
509
- function inferCategory(id) {
510
- const category = id.split('/')[0];
511
- return normalizeString(category) || 'root';
512
- }
513
-
514
- function inferNodeType(id, frontmatter) {
515
- const category = inferCategory(id).toLowerCase();
516
- const explicitType = normalizeString(frontmatter?.type).toLowerCase();
517
- if (category.includes('daily') || explicitType === 'daily') return 'daily';
518
- if (category === 'observations' || explicitType === 'observation') return 'observation';
519
- if (category === 'handoffs' || explicitType === 'handoff') return 'handoff';
520
- if (category === 'decisions' || explicitType === 'decision') return 'decision';
521
- if (category === 'lessons' || explicitType === 'lesson') return 'lesson';
522
- if (category === 'projects' || explicitType === 'project') return 'project';
523
- if (category === 'people' || explicitType === 'person') return 'person';
524
- if (category === 'commitments' || explicitType === 'commitment') return 'commitment';
525
- return 'note';
526
- }
527
-
528
- function normalizeTags(tags) {
529
- if (Array.isArray(tags)) {
530
- return tags.map(normalizeString).filter(Boolean);
531
- }
532
- if (typeof tags === 'string') {
533
- return tags
534
- .split(',')
535
- .map((tag) => normalizeString(tag))
536
- .filter(Boolean);
537
- }
538
- return [];
539
- }
540
-
541
- function normalizeString(value) {
542
- return typeof value === 'string' ? value.trim() : '';
543
- }
544
-
545
- function toDisplayTitle(id) {
546
- const base = path.posix.basename(id);
547
- return base
548
- .replace(/[-_]+/g, ' ')
549
- .replace(/\s+/g, ' ')
550
- .trim()
551
- .replace(/\b\w/g, (char) => char.toUpperCase());
552
- }
553
-
554
- function toPosixPath(filePath) {
555
- return filePath.split(path.sep).join('/');
556
- }
1
+ import * as fs from 'node:fs/promises';
2
+ import * as path from 'node:path';
3
+ import matter from 'gray-matter';
4
+
5
+ export const WIKI_LINK_REGEX = /\[\[([^\]|]+)(\|[^\]]+)?\]\]/g;
6
+ const HASH_TAG_REGEX = /(^|\s)#([\w-]+)/g;
7
+ const MEMORY_GRAPH_INDEX_PATH = ['.clawvault', 'graph-index.json'];
8
+
9
+ const MARKDOWN_EXT = '.md';
10
+ const IGNORED_DIRECTORIES = new Set([
11
+ '.git',
12
+ '.obsidian',
13
+ '.trash',
14
+ 'node_modules'
15
+ ]);
16
+ const FRONTMATTER_RELATION_FIELDS = [
17
+ 'related',
18
+ 'depends_on',
19
+ 'dependsOn',
20
+ 'blocked_by',
21
+ 'blocks',
22
+ 'owner',
23
+ 'project',
24
+ 'people',
25
+ 'links'
26
+ ];
27
+
28
+ /**
29
+ * Build a graph from markdown notes in a vault.
30
+ * @param {string} vaultPath
31
+ * @param {{ includeDangling?: boolean }} [options]
32
+ */
33
+ export async function buildVaultGraph(vaultPath, options = {}) {
34
+ const includeDangling = options.includeDangling !== false;
35
+ const root = path.resolve(vaultPath);
36
+ const preferIndex = options.preferIndex !== false;
37
+ const validateIndexFreshness = options.validateIndexFreshness !== false;
38
+
39
+ if (preferIndex) {
40
+ const indexedGraph = await loadGraphFromMemoryIndex(root, {
41
+ includeDangling,
42
+ validateFreshness: validateIndexFreshness
43
+ });
44
+ if (indexedGraph) {
45
+ return indexedGraph;
46
+ }
47
+ }
48
+
49
+ const markdownFiles = await collectMarkdownFiles(root);
50
+
51
+ const nodesById = new Map();
52
+ const edgesByKey = new Map();
53
+ const edgeSet = new Set();
54
+
55
+ for (const absoluteFilePath of markdownFiles) {
56
+ const raw = await fs.readFile(absoluteFilePath, 'utf8');
57
+ const parsed = matter(raw);
58
+ const relativePath = path.relative(root, absoluteFilePath);
59
+ const id = toNodeId(relativePath);
60
+ const frontmatter = parsed.data ?? {};
61
+
62
+ nodesById.set(id, {
63
+ id,
64
+ title: normalizeString(frontmatter.title) || toDisplayTitle(id),
65
+ category: normalizeString(frontmatter.category) || inferCategory(id),
66
+ type: inferNodeType(id, frontmatter),
67
+ tags: normalizeTags(frontmatter.tags),
68
+ path: toPosixPath(relativePath),
69
+ missing: false,
70
+ _outboundTargets: extractWikiLinks(parsed.content),
71
+ _frontmatterRelations: extractFrontmatterRelations(frontmatter),
72
+ _inlineTags: extractInlineTags(parsed.content)
73
+ });
74
+ }
75
+
76
+ const idsByLowercase = new Map();
77
+ const idsByBaseName = new Map();
78
+ for (const id of nodesById.keys()) {
79
+ idsByLowercase.set(id.toLowerCase(), id);
80
+ const baseName = path.posix.basename(id).toLowerCase();
81
+ const existing = idsByBaseName.get(baseName) ?? new Set();
82
+ existing.add(id);
83
+ idsByBaseName.set(baseName, existing);
84
+ }
85
+
86
+ function ensureUnresolvedNode(targetId) {
87
+ if (nodesById.has(targetId) || !includeDangling) {
88
+ return;
89
+ }
90
+ nodesById.set(targetId, {
91
+ id: targetId,
92
+ title: toDisplayTitle(targetId),
93
+ category: 'unresolved',
94
+ type: 'unresolved',
95
+ tags: [],
96
+ path: null,
97
+ missing: true,
98
+ _outboundTargets: [],
99
+ _frontmatterRelations: [],
100
+ _inlineTags: []
101
+ });
102
+ }
103
+
104
+ function addEdge(sourceId, targetId, type, label) {
105
+ const edgeKey = `${type}:${sourceId}=>${targetId}${label ? `:${label}` : ''}`;
106
+ if (edgeSet.has(edgeKey)) {
107
+ return;
108
+ }
109
+ edgeSet.add(edgeKey);
110
+ edgesByKey.set(edgeKey, {
111
+ source: sourceId,
112
+ target: targetId,
113
+ type,
114
+ label
115
+ });
116
+ }
117
+
118
+ for (const node of nodesById.values()) {
119
+ const tagSet = new Set([...(node.tags ?? []), ...(node._inlineTags ?? [])]);
120
+ for (const inlineTag of tagSet) {
121
+ const tagNodeId = `tag:${inlineTag.toLowerCase()}`;
122
+ if (!nodesById.has(tagNodeId)) {
123
+ nodesById.set(tagNodeId, {
124
+ id: tagNodeId,
125
+ title: `#${inlineTag}`,
126
+ category: 'tag',
127
+ type: 'tag',
128
+ tags: [],
129
+ path: null,
130
+ missing: false,
131
+ _outboundTargets: [],
132
+ _frontmatterRelations: [],
133
+ _inlineTags: []
134
+ });
135
+ }
136
+ addEdge(node.id, tagNodeId, 'tag');
137
+ }
138
+
139
+ for (const rawTarget of node._outboundTargets) {
140
+ const targetId = resolveTargetId(rawTarget, {
141
+ idsByLowercase,
142
+ idsByBaseName,
143
+ includeDangling
144
+ });
145
+ if (!targetId) {
146
+ continue;
147
+ }
148
+ ensureUnresolvedNode(targetId);
149
+ addEdge(node.id, targetId, 'wiki_link');
150
+ }
151
+
152
+ for (const relation of node._frontmatterRelations) {
153
+ const targetId = resolveTargetId(relation.target, {
154
+ idsByLowercase,
155
+ idsByBaseName,
156
+ includeDangling
157
+ });
158
+ if (!targetId) {
159
+ continue;
160
+ }
161
+ ensureUnresolvedNode(targetId);
162
+ addEdge(node.id, targetId, 'frontmatter_relation', relation.field);
163
+ }
164
+ }
165
+
166
+ const edges = Array.from(edgesByKey.values());
167
+ const degreeByNodeId = new Map();
168
+ for (const edge of edges) {
169
+ degreeByNodeId.set(edge.source, (degreeByNodeId.get(edge.source) ?? 0) + 1);
170
+ degreeByNodeId.set(edge.target, (degreeByNodeId.get(edge.target) ?? 0) + 1);
171
+ }
172
+
173
+ const nodeTypeCounts = {};
174
+ const nodes = Array.from(nodesById.values())
175
+ .map(({ _outboundTargets, _frontmatterRelations, _inlineTags, ...node }) => {
176
+ nodeTypeCounts[node.type] = (nodeTypeCounts[node.type] ?? 0) + 1;
177
+ return {
178
+ ...node,
179
+ degree: degreeByNodeId.get(node.id) ?? 0
180
+ };
181
+ })
182
+ .sort((a, b) => a.id.localeCompare(b.id));
183
+
184
+ const edgeTypeCounts = {};
185
+ for (const edge of edges) {
186
+ edgeTypeCounts[edge.type] = (edgeTypeCounts[edge.type] ?? 0) + 1;
187
+ }
188
+
189
+ edges.sort((a, b) => {
190
+ const sourceSort = String(a.source).localeCompare(String(b.source));
191
+ if (sourceSort !== 0) return sourceSort;
192
+ const targetSort = String(a.target).localeCompare(String(b.target));
193
+ if (targetSort !== 0) return targetSort;
194
+ const typeSort = String(a.type || '').localeCompare(String(b.type || ''));
195
+ if (typeSort !== 0) return typeSort;
196
+ return String(a.label || '').localeCompare(String(b.label || ''));
197
+ });
198
+
199
+ return {
200
+ nodes,
201
+ edges,
202
+ stats: {
203
+ generatedAt: new Date().toISOString(),
204
+ nodeCount: nodes.length,
205
+ edgeCount: edges.length,
206
+ fileCount: markdownFiles.length,
207
+ nodeTypeCounts,
208
+ edgeTypeCounts
209
+ }
210
+ };
211
+ }
212
+
213
+ async function loadGraphFromMemoryIndex(root, options = {}) {
214
+ const includeDangling = options.includeDangling !== false;
215
+ const validateFreshness = options.validateFreshness !== false;
216
+ const indexPath = path.join(root, ...MEMORY_GRAPH_INDEX_PATH);
217
+
218
+ let parsed;
219
+ try {
220
+ const raw = await fs.readFile(indexPath, 'utf8');
221
+ parsed = JSON.parse(raw);
222
+ } catch {
223
+ return null;
224
+ }
225
+
226
+ const graph = parsed?.graph;
227
+ if (!graph || !Array.isArray(graph.nodes) || !Array.isArray(graph.edges)) {
228
+ return null;
229
+ }
230
+
231
+ if (validateFreshness) {
232
+ const fresh = await isIndexFresh(root, parsed);
233
+ if (!fresh) {
234
+ return null;
235
+ }
236
+ }
237
+
238
+ const nodeById = new Map();
239
+ for (const node of graph.nodes) {
240
+ const mappedId = fromIndexedNodeId(node?.id, node?.type);
241
+ if (!mappedId) continue;
242
+ if (node?.missing && !includeDangling) continue;
243
+
244
+ nodeById.set(mappedId, {
245
+ id: mappedId,
246
+ title: normalizeString(node?.title) || toDisplayTitle(mappedId),
247
+ category: normalizeString(node?.category) || 'root',
248
+ type: normalizeString(node?.type) || 'note',
249
+ tags: normalizeTags(node?.tags),
250
+ path: typeof node?.path === 'string' ? toPosixPath(node.path) : null,
251
+ missing: Boolean(node?.missing),
252
+ degree: Number(node?.degree ?? 0)
253
+ });
254
+ }
255
+
256
+ const edges = [];
257
+ for (const edge of graph.edges) {
258
+ const source = fromIndexedNodeId(edge?.source);
259
+ const target = fromIndexedNodeId(edge?.target);
260
+ if (!source || !target) continue;
261
+ if (!nodeById.has(source) || !nodeById.has(target)) continue;
262
+ edges.push({
263
+ source,
264
+ target,
265
+ type: normalizeString(edge?.type) || 'wiki_link',
266
+ label: normalizeString(edge?.label) || undefined
267
+ });
268
+ }
269
+
270
+ const degreeByNodeId = new Map();
271
+ for (const edge of edges) {
272
+ degreeByNodeId.set(edge.source, (degreeByNodeId.get(edge.source) ?? 0) + 1);
273
+ degreeByNodeId.set(edge.target, (degreeByNodeId.get(edge.target) ?? 0) + 1);
274
+ }
275
+
276
+ const nodeTypeCounts = {};
277
+ const nodes = Array.from(nodeById.values())
278
+ .map((node) => {
279
+ node.degree = degreeByNodeId.get(node.id) ?? 0;
280
+ nodeTypeCounts[node.type] = (nodeTypeCounts[node.type] ?? 0) + 1;
281
+ return node;
282
+ })
283
+ .sort((a, b) => a.id.localeCompare(b.id));
284
+
285
+ const edgeTypeCounts = {};
286
+ for (const edge of edges) {
287
+ edgeTypeCounts[edge.type] = (edgeTypeCounts[edge.type] ?? 0) + 1;
288
+ }
289
+
290
+ edges.sort((a, b) => {
291
+ const sourceSort = String(a.source).localeCompare(String(b.source));
292
+ if (sourceSort !== 0) return sourceSort;
293
+ const targetSort = String(a.target).localeCompare(String(b.target));
294
+ if (targetSort !== 0) return targetSort;
295
+ const typeSort = String(a.type || '').localeCompare(String(b.type || ''));
296
+ if (typeSort !== 0) return typeSort;
297
+ return String(a.label || '').localeCompare(String(b.label || ''));
298
+ });
299
+
300
+ return {
301
+ nodes,
302
+ edges,
303
+ stats: {
304
+ generatedAt: normalizeString(graph?.stats?.generatedAt) || new Date().toISOString(),
305
+ nodeCount: nodes.length,
306
+ edgeCount: edges.length,
307
+ fileCount: Number.isFinite(Number(parsed?.files ? Object.keys(parsed.files).length : graph?.stats?.fileCount))
308
+ ? Number(parsed?.files ? Object.keys(parsed.files).length : graph?.stats?.fileCount)
309
+ : 0,
310
+ nodeTypeCounts,
311
+ edgeTypeCounts
312
+ }
313
+ };
314
+ }
315
+
316
+ async function isIndexFresh(root, parsedIndex) {
317
+ const indexedFiles = parsedIndex?.files;
318
+ if (!indexedFiles || typeof indexedFiles !== 'object') {
319
+ return false;
320
+ }
321
+
322
+ const markdownFiles = await collectMarkdownFiles(root);
323
+ const normalizedCurrent = markdownFiles
324
+ .map((absolutePath) => toPosixPath(path.relative(root, absolutePath)))
325
+ .sort((a, b) => a.localeCompare(b));
326
+ const indexedPaths = Object.keys(indexedFiles).sort((a, b) => a.localeCompare(b));
327
+
328
+ if (normalizedCurrent.length !== indexedPaths.length) {
329
+ return false;
330
+ }
331
+
332
+ for (let index = 0; index < normalizedCurrent.length; index += 1) {
333
+ if (normalizedCurrent[index] !== indexedPaths[index]) {
334
+ return false;
335
+ }
336
+ }
337
+
338
+ for (const relativePath of indexedPaths) {
339
+ const fragment = indexedFiles[relativePath];
340
+ const expectedMtime = Number(fragment?.mtimeMs);
341
+ if (!Number.isFinite(expectedMtime)) {
342
+ return false;
343
+ }
344
+ const absolutePath = path.join(root, relativePath);
345
+ let stat;
346
+ try {
347
+ stat = await fs.stat(absolutePath);
348
+ } catch {
349
+ return false;
350
+ }
351
+ if (Math.abs(stat.mtimeMs - expectedMtime) > 1) {
352
+ return false;
353
+ }
354
+ }
355
+
356
+ return true;
357
+ }
358
+
359
+ async function collectMarkdownFiles(root) {
360
+ const pending = [root];
361
+ const files = [];
362
+
363
+ while (pending.length > 0) {
364
+ const currentDir = pending.pop();
365
+ const entries = await fs.readdir(currentDir, { withFileTypes: true });
366
+
367
+ for (const entry of entries) {
368
+ const absolutePath = path.join(currentDir, entry.name);
369
+ if (entry.isDirectory()) {
370
+ if (!IGNORED_DIRECTORIES.has(entry.name)) {
371
+ pending.push(absolutePath);
372
+ }
373
+ continue;
374
+ }
375
+
376
+ if (entry.isFile() && entry.name.toLowerCase().endsWith(MARKDOWN_EXT)) {
377
+ files.push(absolutePath);
378
+ }
379
+ }
380
+ }
381
+
382
+ return files;
383
+ }
384
+
385
+ function extractWikiLinks(markdown) {
386
+ const links = [];
387
+ const regex = new RegExp(WIKI_LINK_REGEX.source, 'g');
388
+ let match = regex.exec(markdown);
389
+
390
+ while (match) {
391
+ const rawTarget = match[1]?.trim();
392
+ if (rawTarget) {
393
+ links.push(rawTarget);
394
+ }
395
+ match = regex.exec(markdown);
396
+ }
397
+
398
+ return links;
399
+ }
400
+
401
+ function extractInlineTags(markdown) {
402
+ const tags = new Set();
403
+ for (const match of markdown.matchAll(HASH_TAG_REGEX)) {
404
+ const tag = normalizeString(match[2])?.toLowerCase();
405
+ if (tag) {
406
+ tags.add(tag);
407
+ }
408
+ }
409
+ return [...tags];
410
+ }
411
+
412
+ function extractFrontmatterRelations(frontmatter) {
413
+ const relations = [];
414
+ for (const field of FRONTMATTER_RELATION_FIELDS) {
415
+ const raw = frontmatter?.[field];
416
+ if (typeof raw === 'string') {
417
+ for (const target of raw.split(',').map((value) => normalizeWikiTarget(value)).filter(Boolean)) {
418
+ relations.push({ field, target });
419
+ }
420
+ continue;
421
+ }
422
+ if (Array.isArray(raw)) {
423
+ for (const entry of raw) {
424
+ if (typeof entry !== 'string') continue;
425
+ for (const target of entry.split(',').map((value) => normalizeWikiTarget(value)).filter(Boolean)) {
426
+ relations.push({ field, target });
427
+ }
428
+ }
429
+ }
430
+ }
431
+ return relations;
432
+ }
433
+
434
+ function resolveTargetId(target, context) {
435
+ const normalized = normalizeWikiTarget(target);
436
+ if (!normalized) {
437
+ return null;
438
+ }
439
+
440
+ const lower = normalized.toLowerCase();
441
+ const exact = context.idsByLowercase.get(lower);
442
+ if (exact) {
443
+ return exact;
444
+ }
445
+
446
+ if (!normalized.includes('/')) {
447
+ const maybeMatches = context.idsByBaseName.get(lower);
448
+ if (maybeMatches?.size === 1) {
449
+ return Array.from(maybeMatches)[0];
450
+ }
451
+ }
452
+
453
+ return context.includeDangling ? normalized : null;
454
+ }
455
+
456
+ function normalizeWikiTarget(target) {
457
+ let value = normalizeString(target);
458
+ if (!value) {
459
+ return null;
460
+ }
461
+
462
+ const hashIndex = value.indexOf('#');
463
+ if (hashIndex >= 0) {
464
+ value = value.slice(0, hashIndex);
465
+ }
466
+
467
+ const caretIndex = value.indexOf('^');
468
+ if (caretIndex >= 0) {
469
+ value = value.slice(0, caretIndex);
470
+ }
471
+
472
+ value = value.replace(/\\/g, '/');
473
+ value = value.replace(/^\.\//, '');
474
+ value = value.replace(/^\/+/, '');
475
+ value = value.replace(/\/+/g, '/');
476
+
477
+ if (value.toLowerCase().endsWith(MARKDOWN_EXT)) {
478
+ value = value.slice(0, -MARKDOWN_EXT.length);
479
+ }
480
+
481
+ return normalizeString(value);
482
+ }
483
+
484
+ function fromIndexedNodeId(value, nodeType = '') {
485
+ const raw = normalizeString(value);
486
+ if (!raw) return '';
487
+ if (raw.startsWith('note:')) {
488
+ return raw.slice(5);
489
+ }
490
+ if (raw.startsWith('tag:')) {
491
+ return raw;
492
+ }
493
+ if (raw.startsWith('unresolved:')) {
494
+ return raw.slice('unresolved:'.length) || raw;
495
+ }
496
+ if (normalizeString(nodeType).toLowerCase() === 'tag') {
497
+ return raw.startsWith('tag:') ? raw : `tag:${raw}`;
498
+ }
499
+ return raw;
500
+ }
501
+
502
+ function toNodeId(relativePath) {
503
+ const normalized = toPosixPath(relativePath);
504
+ return normalized.toLowerCase().endsWith(MARKDOWN_EXT)
505
+ ? normalized.slice(0, -MARKDOWN_EXT.length)
506
+ : normalized;
507
+ }
508
+
509
+ function inferCategory(id) {
510
+ const category = id.split('/')[0];
511
+ return normalizeString(category) || 'root';
512
+ }
513
+
514
+ function inferNodeType(id, frontmatter) {
515
+ const category = inferCategory(id).toLowerCase();
516
+ const explicitType = normalizeString(frontmatter?.type).toLowerCase();
517
+ if (category.includes('daily') || explicitType === 'daily') return 'daily';
518
+ if (category === 'observations' || explicitType === 'observation') return 'observation';
519
+ if (category === 'handoffs' || explicitType === 'handoff') return 'handoff';
520
+ if (category === 'decisions' || explicitType === 'decision') return 'decision';
521
+ if (category === 'lessons' || explicitType === 'lesson') return 'lesson';
522
+ if (category === 'projects' || explicitType === 'project') return 'project';
523
+ if (category === 'people' || explicitType === 'person') return 'person';
524
+ if (category === 'commitments' || explicitType === 'commitment') return 'commitment';
525
+ return 'note';
526
+ }
527
+
528
+ function normalizeTags(tags) {
529
+ if (Array.isArray(tags)) {
530
+ return tags.map(normalizeString).filter(Boolean);
531
+ }
532
+ if (typeof tags === 'string') {
533
+ return tags
534
+ .split(',')
535
+ .map((tag) => normalizeString(tag))
536
+ .filter(Boolean);
537
+ }
538
+ return [];
539
+ }
540
+
541
+ function normalizeString(value) {
542
+ return typeof value === 'string' ? value.trim() : '';
543
+ }
544
+
545
+ function toDisplayTitle(id) {
546
+ const base = path.posix.basename(id);
547
+ return base
548
+ .replace(/[-_]+/g, ' ')
549
+ .replace(/\s+/g, ' ')
550
+ .trim()
551
+ .replace(/\b\w/g, (char) => char.toUpperCase());
552
+ }
553
+
554
+ function toPosixPath(filePath) {
555
+ return filePath.split(path.sep).join('/');
556
+ }