clawvault 2.6.1 → 3.0.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 (232) hide show
  1. package/bin/command-registration.test.js +1 -3
  2. package/bin/register-core-commands.js +10 -23
  3. package/bin/register-maintenance-commands.js +3 -20
  4. package/bin/register-query-commands.js +23 -0
  5. package/bin/register-task-commands.js +1 -18
  6. package/bin/register-task-commands.test.js +0 -16
  7. package/bin/register-vault-operations-commands.js +1 -29
  8. package/dist/{chunk-QVMXF7FY.js → chunk-3D6BCTP6.js} +39 -1
  9. package/dist/{chunk-R2MIW5G7.js → chunk-3DHXQHYG.js} +1 -1
  10. package/dist/{chunk-Q2J5YTUF.js → chunk-3NSBOUT3.js} +73 -36
  11. package/dist/chunk-3RG5ZIWI.js +10 -0
  12. package/dist/{chunk-AZYOKJYC.js → chunk-62YTUT6J.js} +2 -2
  13. package/dist/chunk-6U6MK36V.js +205 -0
  14. package/dist/{chunk-4QYGFWRM.js → chunk-7R7O6STJ.js} +4 -4
  15. package/dist/{chunk-VXEOHTSL.js → chunk-C7OK5WKP.js} +4 -4
  16. package/dist/chunk-CMB7UL7C.js +327 -0
  17. package/dist/chunk-DEFFDRVP.js +938 -0
  18. package/dist/{chunk-HIHOUSXS.js → chunk-E7MFQB6D.js} +59 -18
  19. package/dist/{chunk-ME37YNW3.js → chunk-F2JEUD4J.js} +6 -4
  20. package/dist/chunk-GAJV4IGR.js +82 -0
  21. package/dist/chunk-GQSLDZTS.js +560 -0
  22. package/dist/{chunk-4OXMU5S2.js → chunk-GUKMRGM7.js} +1 -1
  23. package/dist/{chunk-T76H47ZS.js → chunk-H34S76MB.js} +6 -6
  24. package/dist/{chunk-R6SXNSFD.js → chunk-JY6FYXIT.js} +10 -5
  25. package/dist/chunk-K234IDRJ.js +1073 -0
  26. package/dist/{chunk-IEVLHNLU.js → chunk-LNJA2UGL.js} +86 -9
  27. package/dist/{chunk-MFAWT5O5.js → chunk-LYHGEHXG.js} +1 -0
  28. package/dist/chunk-MFM6K7PU.js +374 -0
  29. package/dist/{chunk-QWQ3TIKS.js → chunk-N2AXRYLC.js} +1 -1
  30. package/dist/chunk-PAH27GSN.js +108 -0
  31. package/dist/{chunk-OIWVQYQF.js → chunk-QBLMXKF2.js} +1 -1
  32. package/dist/{chunk-FHFUXL6G.js → chunk-QK3UCXWL.js} +2 -2
  33. package/dist/{chunk-3BTHWPMB.js → chunk-SJSFRIYS.js} +1 -1
  34. package/dist/{chunk-4VRIMU4O.js → chunk-U55BGUAU.js} +2 -2
  35. package/dist/{chunk-PBEE567J.js → chunk-VGLOTGAS.js} +1 -1
  36. package/dist/{chunk-F55HGNU4.js → chunk-WAZ3NLWL.js} +47 -0
  37. package/dist/{chunk-KL4NAOMO.js → chunk-WGRQ6HDV.js} +1 -1
  38. package/dist/{chunk-UEOUADMO.js → chunk-YKTA5JOJ.js} +13 -10
  39. package/dist/{chunk-XAVB4GB4.js → chunk-ZVVFWOLW.js} +4 -4
  40. package/dist/cli/index.cjs +10033 -0
  41. package/dist/cli/index.d.cts +5 -0
  42. package/dist/cli/index.js +20 -18
  43. package/dist/commands/archive.cjs +287 -0
  44. package/dist/commands/archive.d.cts +11 -0
  45. package/dist/commands/archive.js +1 -0
  46. package/dist/commands/backlog.cjs +721 -0
  47. package/dist/commands/backlog.d.cts +53 -0
  48. package/dist/commands/backlog.js +3 -2
  49. package/dist/commands/blocked.cjs +204 -0
  50. package/dist/commands/blocked.d.cts +26 -0
  51. package/dist/commands/blocked.js +3 -2
  52. package/dist/commands/checkpoint.cjs +244 -0
  53. package/dist/commands/checkpoint.d.cts +41 -0
  54. package/dist/commands/checkpoint.js +2 -1
  55. package/dist/commands/compat.cjs +369 -0
  56. package/dist/commands/compat.d.cts +28 -0
  57. package/dist/commands/compat.js +2 -1
  58. package/dist/commands/context.cjs +2989 -0
  59. package/dist/commands/context.d.cts +2 -0
  60. package/dist/commands/context.js +5 -4
  61. package/dist/commands/doctor.cjs +3062 -0
  62. package/dist/commands/doctor.d.cts +21 -0
  63. package/dist/commands/doctor.d.ts +6 -1
  64. package/dist/commands/doctor.js +13 -11
  65. package/dist/commands/embed.cjs +232 -0
  66. package/dist/commands/embed.d.cts +17 -0
  67. package/dist/commands/embed.js +5 -2
  68. package/dist/commands/entities.cjs +141 -0
  69. package/dist/commands/entities.d.cts +7 -0
  70. package/dist/commands/entities.js +1 -0
  71. package/dist/commands/graph.cjs +501 -0
  72. package/dist/commands/graph.d.cts +21 -0
  73. package/dist/commands/graph.js +1 -0
  74. package/dist/commands/inject.cjs +1636 -0
  75. package/dist/commands/inject.d.cts +2 -0
  76. package/dist/commands/inject.d.ts +1 -1
  77. package/dist/commands/inject.js +4 -2
  78. package/dist/commands/kanban.cjs +884 -0
  79. package/dist/commands/kanban.d.cts +63 -0
  80. package/dist/commands/kanban.js +4 -3
  81. package/dist/commands/link.cjs +965 -0
  82. package/dist/commands/link.d.cts +11 -0
  83. package/dist/commands/link.js +1 -0
  84. package/dist/commands/migrate-observations.cjs +362 -0
  85. package/dist/commands/migrate-observations.d.cts +19 -0
  86. package/dist/commands/migrate-observations.js +3 -2
  87. package/dist/commands/observe.cjs +4099 -0
  88. package/dist/commands/observe.d.cts +23 -0
  89. package/dist/commands/observe.d.ts +1 -0
  90. package/dist/commands/observe.js +11 -9
  91. package/dist/commands/project.cjs +1341 -0
  92. package/dist/commands/project.d.cts +85 -0
  93. package/dist/commands/project.js +5 -4
  94. package/dist/commands/rebuild.cjs +3136 -0
  95. package/dist/commands/rebuild.d.cts +11 -0
  96. package/dist/commands/rebuild.js +10 -8
  97. package/dist/commands/recover.cjs +361 -0
  98. package/dist/commands/recover.d.cts +38 -0
  99. package/dist/commands/recover.js +3 -2
  100. package/dist/commands/reflect.cjs +1008 -0
  101. package/dist/commands/reflect.d.cts +11 -0
  102. package/dist/commands/reflect.js +6 -4
  103. package/dist/commands/repair-session.cjs +457 -0
  104. package/dist/commands/repair-session.d.cts +38 -0
  105. package/dist/commands/repair-session.js +1 -0
  106. package/dist/commands/replay.cjs +4103 -0
  107. package/dist/commands/replay.d.cts +16 -0
  108. package/dist/commands/replay.js +12 -10
  109. package/dist/commands/session-recap.cjs +353 -0
  110. package/dist/commands/session-recap.d.cts +27 -0
  111. package/dist/commands/session-recap.js +1 -0
  112. package/dist/commands/setup.cjs +1345 -0
  113. package/dist/commands/setup.d.cts +100 -0
  114. package/dist/commands/setup.d.ts +90 -2
  115. package/dist/commands/setup.js +21 -2
  116. package/dist/commands/shell-init.cjs +75 -0
  117. package/dist/commands/shell-init.d.cts +7 -0
  118. package/dist/commands/shell-init.js +2 -0
  119. package/dist/commands/sleep.cjs +6028 -0
  120. package/dist/commands/sleep.d.cts +36 -0
  121. package/dist/commands/sleep.d.ts +1 -1
  122. package/dist/commands/sleep.js +17 -15
  123. package/dist/commands/status.cjs +2736 -0
  124. package/dist/commands/status.d.cts +52 -0
  125. package/dist/commands/status.js +12 -10
  126. package/dist/commands/tailscale.cjs +1532 -0
  127. package/dist/commands/tailscale.d.cts +52 -0
  128. package/dist/commands/tailscale.js +1 -0
  129. package/dist/commands/task.cjs +1236 -0
  130. package/dist/commands/task.d.cts +97 -0
  131. package/dist/commands/task.js +4 -3
  132. package/dist/commands/template.cjs +457 -0
  133. package/dist/commands/template.d.cts +36 -0
  134. package/dist/commands/template.js +2 -1
  135. package/dist/commands/wake.cjs +2626 -0
  136. package/dist/commands/wake.d.cts +22 -0
  137. package/dist/commands/wake.d.ts +1 -1
  138. package/dist/commands/wake.js +10 -9
  139. package/dist/context-BUGaWpyL.d.cts +46 -0
  140. package/dist/index.cjs +14526 -0
  141. package/dist/index.d.cts +858 -0
  142. package/dist/index.d.ts +192 -7
  143. package/dist/index.js +101 -75
  144. package/dist/{inject-x65KXWPk.d.ts → inject-Bzi5E-By.d.cts} +1 -1
  145. package/dist/inject-Bzi5E-By.d.ts +137 -0
  146. package/dist/lib/auto-linker.cjs +176 -0
  147. package/dist/lib/auto-linker.d.cts +26 -0
  148. package/dist/lib/auto-linker.js +1 -0
  149. package/dist/lib/canvas-layout.cjs +136 -0
  150. package/dist/lib/canvas-layout.d.cts +31 -0
  151. package/dist/lib/canvas-layout.d.ts +16 -100
  152. package/dist/lib/canvas-layout.js +78 -20
  153. package/dist/lib/config.cjs +78 -0
  154. package/dist/lib/config.d.cts +11 -0
  155. package/dist/lib/config.js +1 -0
  156. package/dist/lib/entity-index.cjs +84 -0
  157. package/dist/lib/entity-index.d.cts +26 -0
  158. package/dist/lib/entity-index.js +1 -0
  159. package/dist/lib/project-utils.cjs +864 -0
  160. package/dist/lib/project-utils.d.cts +97 -0
  161. package/dist/lib/project-utils.js +4 -3
  162. package/dist/lib/session-repair.cjs +239 -0
  163. package/dist/lib/session-repair.d.cts +110 -0
  164. package/dist/lib/session-repair.js +1 -0
  165. package/dist/lib/session-utils.cjs +209 -0
  166. package/dist/lib/session-utils.d.cts +63 -0
  167. package/dist/lib/session-utils.js +1 -0
  168. package/dist/lib/tailscale.cjs +1183 -0
  169. package/dist/lib/tailscale.d.cts +225 -0
  170. package/dist/lib/tailscale.js +1 -0
  171. package/dist/lib/task-utils.cjs +1137 -0
  172. package/dist/lib/task-utils.d.cts +208 -0
  173. package/dist/lib/task-utils.js +3 -2
  174. package/dist/lib/template-engine.cjs +47 -0
  175. package/dist/lib/template-engine.d.cts +11 -0
  176. package/dist/lib/template-engine.js +1 -0
  177. package/dist/lib/webdav.cjs +568 -0
  178. package/dist/lib/webdav.d.cts +109 -0
  179. package/dist/lib/webdav.js +1 -0
  180. package/dist/plugin/index.cjs +1907 -0
  181. package/dist/plugin/index.d.cts +36 -0
  182. package/dist/plugin/index.d.ts +36 -0
  183. package/dist/plugin/index.js +572 -0
  184. package/dist/plugin/inject.cjs +356 -0
  185. package/dist/plugin/inject.d.cts +54 -0
  186. package/dist/plugin/inject.d.ts +54 -0
  187. package/dist/plugin/inject.js +17 -0
  188. package/dist/plugin/observe.cjs +631 -0
  189. package/dist/plugin/observe.d.cts +39 -0
  190. package/dist/plugin/observe.d.ts +39 -0
  191. package/dist/plugin/observe.js +18 -0
  192. package/dist/plugin/templates.cjs +593 -0
  193. package/dist/plugin/templates.d.cts +52 -0
  194. package/dist/plugin/templates.d.ts +52 -0
  195. package/dist/plugin/templates.js +25 -0
  196. package/dist/plugin/types.cjs +18 -0
  197. package/dist/plugin/types.d.cts +209 -0
  198. package/dist/plugin/types.d.ts +209 -0
  199. package/dist/plugin/types.js +0 -0
  200. package/dist/plugin/vault.cjs +927 -0
  201. package/dist/plugin/vault.d.cts +68 -0
  202. package/dist/plugin/vault.d.ts +68 -0
  203. package/dist/plugin/vault.js +22 -0
  204. package/dist/{types-C74wgGL1.d.ts → types-Y2_Um2Ls.d.cts} +44 -1
  205. package/dist/types-Y2_Um2Ls.d.ts +205 -0
  206. package/hooks/clawvault/handler.js +70 -7
  207. package/hooks/clawvault/handler.test.js +91 -0
  208. package/openclaw.plugin.json +56 -0
  209. package/package.json +17 -7
  210. package/templates/memory-event.md +67 -0
  211. package/templates/party.md +63 -0
  212. package/templates/primitive-registry.yaml +551 -0
  213. package/templates/run.md +68 -0
  214. package/templates/trigger.md +68 -0
  215. package/templates/workspace.md +50 -0
  216. package/dashboard/lib/graph-diff.js +0 -104
  217. package/dashboard/lib/graph-diff.test.js +0 -75
  218. package/dashboard/lib/vault-parser.js +0 -556
  219. package/dashboard/lib/vault-parser.test.js +0 -254
  220. package/dashboard/public/app.js +0 -796
  221. package/dashboard/public/index.html +0 -52
  222. package/dashboard/public/styles.css +0 -221
  223. package/dashboard/server.js +0 -374
  224. package/dist/chunk-MAKNAHAW.js +0 -375
  225. package/dist/chunk-MDIH26GC.js +0 -183
  226. package/dist/chunk-MGDEINGP.js +0 -99
  227. package/dist/chunk-RVYA52PY.js +0 -363
  228. package/dist/chunk-TLGBDTYT.js +0 -33
  229. package/dist/commands/canvas.d.ts +0 -15
  230. package/dist/commands/canvas.js +0 -199
  231. package/dist/commands/sync-bd.d.ts +0 -10
  232. package/dist/commands/sync-bd.js +0 -9
@@ -0,0 +1,2736 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/commands/status.ts
31
+ var status_exports = {};
32
+ __export(status_exports, {
33
+ formatStatus: () => formatStatus,
34
+ getStatus: () => getStatus,
35
+ statusCommand: () => statusCommand
36
+ });
37
+ module.exports = __toCommonJS(status_exports);
38
+ var fs7 = __toESM(require("fs"), 1);
39
+ var path8 = __toESM(require("path"), 1);
40
+ var import_child_process2 = require("child_process");
41
+
42
+ // src/lib/vault.ts
43
+ var fs3 = __toESM(require("fs"), 1);
44
+ var path3 = __toESM(require("path"), 1);
45
+ var import_url = require("url");
46
+ var import_gray_matter2 = __toESM(require("gray-matter"), 1);
47
+ var import_glob2 = require("glob");
48
+
49
+ // src/types.ts
50
+ var DEFAULT_CATEGORIES = [
51
+ "rules",
52
+ "preferences",
53
+ "decisions",
54
+ "patterns",
55
+ "people",
56
+ "projects",
57
+ "goals",
58
+ "transcripts",
59
+ "inbox",
60
+ "templates",
61
+ "lessons",
62
+ "agents",
63
+ "commitments",
64
+ "handoffs",
65
+ "research",
66
+ "tasks",
67
+ "backlog"
68
+ ];
69
+ var TYPE_TO_CATEGORY = {
70
+ fact: "facts",
71
+ feeling: "feelings",
72
+ decision: "decisions",
73
+ lesson: "lessons",
74
+ commitment: "commitments",
75
+ preference: "preferences",
76
+ relationship: "people",
77
+ project: "projects"
78
+ };
79
+
80
+ // src/lib/search.ts
81
+ var import_child_process = require("child_process");
82
+ var fs = __toESM(require("fs"), 1);
83
+ var path = __toESM(require("path"), 1);
84
+
85
+ // src/lib/reweave.ts
86
+ var SUPERSEDED_MARKER_RE = /\[superseded\|by=([^\]|]+)\|detected=([^\]]+)\]/;
87
+ function isSuperseded(line) {
88
+ return SUPERSEDED_MARKER_RE.test(line);
89
+ }
90
+
91
+ // src/lib/search.ts
92
+ var QMD_INSTALL_COMMAND = "bun install -g github:tobi/qmd";
93
+ var QMD_NOT_INSTALLED_MESSAGE = `ClawVault requires qmd. Install: ${QMD_INSTALL_COMMAND}`;
94
+ var QMD_INDEX_ENV_VAR = "CLAWVAULT_QMD_INDEX";
95
+ var QmdUnavailableError = class extends Error {
96
+ constructor(message = QMD_NOT_INSTALLED_MESSAGE) {
97
+ super(message);
98
+ this.name = "QmdUnavailableError";
99
+ }
100
+ };
101
+ function ensureJsonArgs(args) {
102
+ return args.includes("--json") ? args : [...args, "--json"];
103
+ }
104
+ function resolveQmdIndexName(indexName) {
105
+ const explicit = indexName?.trim();
106
+ if (explicit) {
107
+ return explicit;
108
+ }
109
+ const fromEnv = process.env[QMD_INDEX_ENV_VAR]?.trim();
110
+ return fromEnv || void 0;
111
+ }
112
+ function withQmdIndexArgs(args, indexName) {
113
+ if (args.includes("--index")) {
114
+ return [...args];
115
+ }
116
+ const resolvedIndexName = resolveQmdIndexName(indexName);
117
+ if (!resolvedIndexName) {
118
+ return [...args];
119
+ }
120
+ return ["--index", resolvedIndexName, ...args];
121
+ }
122
+ function tryParseJson(raw) {
123
+ try {
124
+ return JSON.parse(raw);
125
+ } catch {
126
+ return null;
127
+ }
128
+ }
129
+ function extractJsonPayload(raw) {
130
+ const start = raw.search(/[\[{]/);
131
+ if (start === -1) return null;
132
+ const end = Math.max(raw.lastIndexOf("]"), raw.lastIndexOf("}"));
133
+ if (end <= start) return null;
134
+ return raw.slice(start, end + 1);
135
+ }
136
+ function stripQmdNoise(raw) {
137
+ return raw.split("\n").filter((line) => {
138
+ const t = line.trim();
139
+ if (!t) return true;
140
+ if (t.startsWith("[node-llama-cpp]")) return false;
141
+ if (t.startsWith("Expanding query")) return false;
142
+ if (t.startsWith("Searching ") && t.endsWith("queries...")) return false;
143
+ if (/^[├└─│]/.test(t)) return false;
144
+ return true;
145
+ }).join("\n");
146
+ }
147
+ function parseQmdOutput(raw) {
148
+ const trimmed = stripQmdNoise(raw).trim();
149
+ if (!trimmed) return [];
150
+ const direct = tryParseJson(trimmed);
151
+ const extracted = direct ? null : extractJsonPayload(trimmed);
152
+ const parsed = direct ?? (extracted ? tryParseJson(extracted) : null);
153
+ if (!parsed) {
154
+ throw new Error("qmd returned non-JSON output. Ensure qmd supports --json.");
155
+ }
156
+ if (Array.isArray(parsed)) {
157
+ return parsed;
158
+ }
159
+ if (parsed && typeof parsed === "object") {
160
+ const candidate = parsed.results ?? parsed.items ?? parsed.data;
161
+ if (Array.isArray(candidate)) {
162
+ return candidate;
163
+ }
164
+ }
165
+ throw new Error("qmd returned an unexpected JSON shape.");
166
+ }
167
+ function ensureQmdAvailable() {
168
+ if (!hasQmd()) {
169
+ throw new QmdUnavailableError();
170
+ }
171
+ }
172
+ function execQmd(args, indexName) {
173
+ ensureQmdAvailable();
174
+ const finalArgs = withQmdIndexArgs(ensureJsonArgs(args), indexName);
175
+ try {
176
+ const result = (0, import_child_process.execFileSync)("qmd", finalArgs, {
177
+ encoding: "utf-8",
178
+ stdio: ["ignore", "pipe", "pipe"],
179
+ maxBuffer: 10 * 1024 * 1024
180
+ // 10MB
181
+ });
182
+ return parseQmdOutput(result);
183
+ } catch (err) {
184
+ if (err?.code === "ENOENT") {
185
+ throw new QmdUnavailableError();
186
+ }
187
+ const output = [err?.stdout, err?.stderr].filter(Boolean).join("\n");
188
+ if (output) {
189
+ try {
190
+ return parseQmdOutput(output);
191
+ } catch {
192
+ }
193
+ }
194
+ const message = err?.message ? `qmd failed: ${err.message}` : "qmd failed";
195
+ throw new Error(message);
196
+ }
197
+ }
198
+ function hasQmd() {
199
+ const result = (0, import_child_process.spawnSync)("qmd", ["--version"], { stdio: "ignore" });
200
+ return !result.error;
201
+ }
202
+ function qmdUpdate(collection, indexName) {
203
+ ensureQmdAvailable();
204
+ const args = ["update"];
205
+ if (collection) {
206
+ args.push("-c", collection);
207
+ }
208
+ (0, import_child_process.execFileSync)("qmd", withQmdIndexArgs(args, indexName), { stdio: "inherit" });
209
+ }
210
+ function qmdEmbed(collection, indexName) {
211
+ ensureQmdAvailable();
212
+ const args = ["embed"];
213
+ if (collection) {
214
+ args.push("-c", collection);
215
+ }
216
+ (0, import_child_process.execFileSync)("qmd", withQmdIndexArgs(args, indexName), { stdio: "inherit" });
217
+ }
218
+ function sentenceChunk(text, maxChars = 600, overlapSentences = 1) {
219
+ const sentences = text.split(/(?<=[.!?])\s+|\n{2,}/).map((s) => s.trim()).filter(Boolean);
220
+ if (sentences.length === 0) return text.trim() ? [text] : [];
221
+ const chunks = [];
222
+ let i = 0;
223
+ while (i < sentences.length) {
224
+ const chunkSents = [];
225
+ let chunkLen = 0;
226
+ let j = i;
227
+ while (j < sentences.length && chunkLen + sentences[j].length < maxChars) {
228
+ chunkSents.push(sentences[j]);
229
+ chunkLen += sentences[j].length + 1;
230
+ j++;
231
+ }
232
+ if (chunkSents.length === 0) {
233
+ chunkSents.push(sentences[j].slice(0, maxChars));
234
+ j++;
235
+ }
236
+ chunks.push(chunkSents.join(" "));
237
+ i = Math.max(j - overlapSentences, i + 1);
238
+ }
239
+ return chunks;
240
+ }
241
+ var STOPWORDS = /* @__PURE__ */ new Set([
242
+ "what",
243
+ "when",
244
+ "where",
245
+ "which",
246
+ "that",
247
+ "this",
248
+ "have",
249
+ "from",
250
+ "with",
251
+ "they",
252
+ "been",
253
+ "were",
254
+ "will",
255
+ "about",
256
+ "would",
257
+ "could",
258
+ "should",
259
+ "their",
260
+ "there",
261
+ "does",
262
+ "your",
263
+ "more",
264
+ "some",
265
+ "than",
266
+ "into",
267
+ "also",
268
+ "just",
269
+ "very",
270
+ "much",
271
+ "most",
272
+ "many",
273
+ "only",
274
+ "other",
275
+ "each",
276
+ "every",
277
+ "after",
278
+ "before",
279
+ "did",
280
+ "the",
281
+ "and",
282
+ "for",
283
+ "are",
284
+ "was",
285
+ "not",
286
+ "but",
287
+ "can",
288
+ "had",
289
+ "has",
290
+ "how",
291
+ "who",
292
+ "why",
293
+ "its",
294
+ "you",
295
+ "my",
296
+ "me",
297
+ "is",
298
+ "it",
299
+ "do",
300
+ "so",
301
+ "if",
302
+ "or",
303
+ "an",
304
+ "on",
305
+ "at",
306
+ "by",
307
+ "no",
308
+ "up",
309
+ "to",
310
+ "in",
311
+ "of",
312
+ "am",
313
+ "be"
314
+ ]);
315
+ function tokenize(text) {
316
+ return text.toLowerCase().split(/\s+/).map((w) => w.replace(/^[?.,!"'\-():;[\]{}*]+|[?.,!"'\-():;[\]{}*]+$/g, "")).filter((w) => w.length > 1);
317
+ }
318
+ function queryTerms(query) {
319
+ return tokenize(query).filter((w) => !STOPWORDS.has(w));
320
+ }
321
+ function bm25RankChunks(chunks, terms, max = 5) {
322
+ if (chunks.length === 0) return [];
323
+ const termSet = new Set(terms);
324
+ const scored = chunks.map((text, idx) => {
325
+ const words = new Set(tokenize(text));
326
+ let overlap = 0;
327
+ for (const t of termSet) if (words.has(t)) overlap++;
328
+ return { text, score: overlap, idx };
329
+ });
330
+ scored.sort((a, b) => b.score - a.score);
331
+ const seen = /* @__PURE__ */ new Set();
332
+ const result = [];
333
+ seen.add(0);
334
+ result.push({ text: chunks[0], score: scored.find((s) => s.idx === 0)?.score ?? 0 });
335
+ for (const s of scored) {
336
+ if (result.length >= max) break;
337
+ if (!seen.has(s.idx) && s.score > 0) {
338
+ seen.add(s.idx);
339
+ result.push({ text: s.text, score: s.score });
340
+ }
341
+ }
342
+ return result;
343
+ }
344
+ var MONTH_NAMES = {
345
+ january: 1,
346
+ february: 2,
347
+ march: 3,
348
+ april: 4,
349
+ may: 5,
350
+ june: 6,
351
+ july: 7,
352
+ august: 8,
353
+ september: 9,
354
+ october: 10,
355
+ november: 11,
356
+ december: 12,
357
+ jan: 1,
358
+ feb: 2,
359
+ mar: 3,
360
+ apr: 4,
361
+ jun: 6,
362
+ jul: 7,
363
+ aug: 8,
364
+ sep: 9,
365
+ sept: 9,
366
+ oct: 10,
367
+ nov: 11,
368
+ dec: 12
369
+ };
370
+ var MONTH_RE_PART = Object.keys(MONTH_NAMES).join("|");
371
+ var DATE_ISO_RE = /\b(\d{4})[/-](\d{1,2})[/-](\d{1,2})\b/g;
372
+ var DATE_US_RE = /\b(\d{1,2})\/(\d{1,2})\/(\d{4})\b/g;
373
+ var DATE_MONTH_DAY_YEAR_RE = new RegExp(
374
+ `\\b(${MONTH_RE_PART})\\s+(\\d{1,2})(?:st|nd|rd|th)?,?\\s*(\\d{4})\\b`,
375
+ "gi"
376
+ );
377
+ var DATE_DAY_MONTH_YEAR_RE = new RegExp(
378
+ `\\b(\\d{1,2})(?:st|nd|rd|th)?\\s+(${MONTH_RE_PART}),?\\s*(\\d{4})\\b`,
379
+ "gi"
380
+ );
381
+ var DATE_MONTH_DAY_RE = new RegExp(
382
+ `\\b(${MONTH_RE_PART})\\s+(\\d{1,2})(?:st|nd|rd|th)?\\b`,
383
+ "gi"
384
+ );
385
+ var RELATIVE_AGO_RE = /\b(\d+)\s+(days?|weeks?|months?|years?)\s+ago\b/gi;
386
+ var RELATIVE_IN_RE = /\bin\s+(\d+)\s+(days?|weeks?|months?|years?)\b/gi;
387
+ var DURATION_RE = /(?:for|took|spent|lasted|about|approximately|around)\s+(\d+)\s+(days?|weeks?|months?|years?|hours?|minutes?)/gi;
388
+ function tryParseISODate(y, m, d) {
389
+ const dt = new Date(Date.UTC(y, m - 1, d));
390
+ if (dt.getUTCFullYear() === y && dt.getUTCMonth() === m - 1 && dt.getUTCDate() === d) return dt;
391
+ return null;
392
+ }
393
+ function unitToDays(n, unit) {
394
+ const u = unit.toLowerCase().replace(/s$/, "");
395
+ switch (u) {
396
+ case "day":
397
+ return n;
398
+ case "week":
399
+ return n * 7;
400
+ case "month":
401
+ return n * 30;
402
+ case "year":
403
+ return n * 365;
404
+ default:
405
+ return null;
406
+ }
407
+ }
408
+ function contextSnippet(text, start, end, maxLen = 150) {
409
+ const s = Math.max(0, start - Math.floor(maxLen / 2));
410
+ const e = Math.min(text.length, end + Math.floor(maxLen / 2));
411
+ return text.slice(s, e).replace(/\n/g, " ").trim();
412
+ }
413
+ function isoStr(d) {
414
+ const yy = d.getUTCFullYear();
415
+ const mm = String(d.getUTCMonth() + 1).padStart(2, "0");
416
+ const dd = String(d.getUTCDate()).padStart(2, "0");
417
+ return `${yy}-${mm}-${dd}`;
418
+ }
419
+ function extractDates(text, sessionDateStr) {
420
+ const results = [];
421
+ const sessionDate = sessionDateStr ? new Date(sessionDateStr) : null;
422
+ const seen = /* @__PURE__ */ new Set();
423
+ function push(date, ctx, docId = "") {
424
+ const key = `${date}|${ctx.slice(0, 60)}`;
425
+ if (seen.has(key)) return;
426
+ seen.add(key);
427
+ results.push({ date, context: ctx, documentId: docId });
428
+ }
429
+ for (const m of text.matchAll(DATE_ISO_RE)) {
430
+ const dt = tryParseISODate(+m[1], +m[2], +m[3]);
431
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
432
+ }
433
+ for (const m of text.matchAll(DATE_US_RE)) {
434
+ const dt = tryParseISODate(+m[3], +m[1], +m[2]);
435
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
436
+ }
437
+ for (const m of text.matchAll(DATE_MONTH_DAY_YEAR_RE)) {
438
+ const mon = MONTH_NAMES[m[1].toLowerCase()];
439
+ if (mon) {
440
+ const dt = tryParseISODate(+m[3], mon, +m[2]);
441
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
442
+ }
443
+ }
444
+ for (const m of text.matchAll(DATE_DAY_MONTH_YEAR_RE)) {
445
+ const mon = MONTH_NAMES[m[2].toLowerCase()];
446
+ if (mon) {
447
+ const dt = tryParseISODate(+m[3], mon, +m[1]);
448
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
449
+ }
450
+ }
451
+ if (sessionDate) {
452
+ for (const m of text.matchAll(DATE_MONTH_DAY_RE)) {
453
+ const mon = MONTH_NAMES[m[1].toLowerCase()];
454
+ if (mon) {
455
+ const dt = tryParseISODate(sessionDate.getFullYear(), mon, +m[2]);
456
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
457
+ }
458
+ }
459
+ }
460
+ if (sessionDate) {
461
+ for (const m of text.matchAll(RELATIVE_AGO_RE)) {
462
+ const days = unitToDays(+m[1], m[2]);
463
+ if (days !== null) {
464
+ const dt = new Date(sessionDate.getTime() - days * 864e5);
465
+ push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
466
+ }
467
+ }
468
+ for (const m of text.matchAll(RELATIVE_IN_RE)) {
469
+ const days = unitToDays(+m[1], m[2]);
470
+ if (days !== null) {
471
+ const dt = new Date(sessionDate.getTime() + days * 864e5);
472
+ push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
473
+ }
474
+ }
475
+ }
476
+ for (const m of text.matchAll(DURATION_RE)) {
477
+ push(`duration:${m[1]} ${m[2]}`, contextSnippet(text, m.index, m.index + m[0].length));
478
+ }
479
+ return results;
480
+ }
481
+ var PREF_PATTERNS = [
482
+ // "I use/prefer/like/love/enjoy X"
483
+ /\bi\s+(?:use|prefer|like|love|enjoy|favor|chose|switched to|started using|always use|usually use)\s+(.{3,60}?)(?:[.,;!?\n]|$)/gi,
484
+ // "my favorite X is Y"
485
+ /\bmy\s+(?:favorite|preferred|go-to|usual)\s+\w+\s+(?:is|are|was)\s+(.{3,60}?)(?:[.,;!?\n]|$)/gi,
486
+ // "I'm a big fan of X"
487
+ /\bi(?:'m| am)\s+(?:a )?(?:big |huge )?fan of\s+(.{3,60}?)(?:[.,;!?\n]|$)/gi,
488
+ // "I switched from X to Y"
489
+ /\bi\s+switched\s+from\s+(.{3,40}?)\s+to\s+(.{3,40}?)(?:[.,;!?\n]|$)/gi
490
+ ];
491
+ function extractPreferences(text, documentId = "") {
492
+ const results = [];
493
+ const seen = /* @__PURE__ */ new Set();
494
+ for (const pattern of PREF_PATTERNS) {
495
+ for (const m of text.matchAll(pattern)) {
496
+ const value = (m[1] || "").trim();
497
+ if (!value || value.length < 3) continue;
498
+ const key = value.toLowerCase();
499
+ if (seen.has(key)) continue;
500
+ seen.add(key);
501
+ const ctx = contextSnippet(text, m.index, m.index + m[0].length, 200);
502
+ let category = "general";
503
+ if (/tool|software|app|editor|ide|framework|library|language/i.test(ctx)) category = "tool";
504
+ else if (/hobby|sport|exercise|game|play/i.test(ctx)) category = "hobby";
505
+ else if (/brand|product|model|device|hardware/i.test(ctx)) category = "brand";
506
+ else if (/food|drink|restaurant|cuisine|recipe/i.test(ctx)) category = "food";
507
+ else if (/music|movie|show|book|podcast|artist|band/i.test(ctx)) category = "entertainment";
508
+ results.push({ category, value, documentId, context: ctx });
509
+ }
510
+ }
511
+ return results;
512
+ }
513
+ var PREFERENCE_Q_RE = /(?:can you (?:recommend|suggest)|any (?:tips|advice|suggestions|recommendations)|what .*(?:recommend|suggest)|what should i|where should i|which .* should i|please (?:recommend|suggest)|based on .* (?:interest|preference|taste)|personalized|tailored to (?:my|me))/i;
514
+ var TEMPORAL_Q_RE = /(?:how many (?:days|weeks|months|years|hours|minutes) (?:passed|did|have|ago|between|since|in total|took)|how long (?:did|was|were|have|has|does)|how long ago|what (?:is the )?order|in order|which .* (?:first|last|earlier|later|before|after|most recent|oldest|newest)|chronological|(?:earlier|later|sooner|newer|older) than)/i;
515
+ var AGGREGATION_Q_RE = /(?:how many|how much|total|all the|count|list all|every|what are all|name all)/i;
516
+ function classifyQuestion(q) {
517
+ if (PREFERENCE_Q_RE.test(q)) return "preference";
518
+ if (TEMPORAL_Q_RE.test(q)) return "temporal";
519
+ if (!TEMPORAL_Q_RE.test(q) && AGGREGATION_Q_RE.test(q)) return "aggregation";
520
+ return "default";
521
+ }
522
+ var SearchEngine = class {
523
+ documents = /* @__PURE__ */ new Map();
524
+ collection = "clawvault";
525
+ vaultPath = "";
526
+ collectionRoot = "";
527
+ qmdIndexName;
528
+ /** v2.7 — Per-document date index built at ingest time */
529
+ dateIndex = /* @__PURE__ */ new Map();
530
+ /** v2.7 — Per-document preference index built at ingest time */
531
+ preferenceIndex = /* @__PURE__ */ new Map();
532
+ /** v2.7 — Per-document chunk cache for BM25 pre-filtering */
533
+ chunkCache = /* @__PURE__ */ new Map();
534
+ /**
535
+ * Set the collection name (usually vault name)
536
+ */
537
+ setCollection(name) {
538
+ this.collection = name;
539
+ }
540
+ /**
541
+ * Set the vault path for file resolution
542
+ */
543
+ setVaultPath(vaultPath) {
544
+ this.vaultPath = vaultPath;
545
+ }
546
+ /**
547
+ * Set the collection root for qmd:// URI resolution
548
+ */
549
+ setCollectionRoot(root) {
550
+ this.collectionRoot = path.resolve(root);
551
+ }
552
+ /**
553
+ * Set qmd index name (defaults to qmd global default when omitted)
554
+ */
555
+ setIndexName(indexName) {
556
+ this.qmdIndexName = indexName;
557
+ }
558
+ /**
559
+ * Add or update a document in the local cache.
560
+ * v2.7: also extracts dates, preferences, and chunks at ingest time.
561
+ * Note: qmd indexing happens via qmd update command
562
+ */
563
+ addDocument(doc) {
564
+ this.documents.set(doc.id, doc);
565
+ if (doc.content) {
566
+ const sessionDate = doc.modified ? isoStr(doc.modified) : void 0;
567
+ const dates = extractDates(doc.content, sessionDate);
568
+ for (const d of dates) d.documentId = doc.id;
569
+ if (dates.length > 0) this.dateIndex.set(doc.id, dates);
570
+ const prefs = extractPreferences(doc.content, doc.id);
571
+ if (prefs.length > 0) this.preferenceIndex.set(doc.id, prefs);
572
+ const chunks = sentenceChunk(doc.content, 600, 1);
573
+ if (chunks.length > 0) this.chunkCache.set(doc.id, chunks);
574
+ }
575
+ }
576
+ /**
577
+ * Remove a document from the local cache
578
+ */
579
+ removeDocument(id) {
580
+ this.documents.delete(id);
581
+ this.dateIndex.delete(id);
582
+ this.preferenceIndex.delete(id);
583
+ this.chunkCache.delete(id);
584
+ }
585
+ /**
586
+ * No-op for qmd - indexing is managed externally
587
+ */
588
+ rebuildIDF() {
589
+ }
590
+ /**
591
+ * BM25 search via qmd
592
+ */
593
+ search(query, options = {}) {
594
+ return this.runQmdQuery("search", query, options);
595
+ }
596
+ /**
597
+ * Vector/semantic search via qmd vsearch
598
+ */
599
+ vsearch(query, options = {}) {
600
+ return this.runQmdQuery("vsearch", query, options);
601
+ }
602
+ /**
603
+ * Combined search with query expansion (qmd query command)
604
+ */
605
+ query(query, options = {}) {
606
+ return this.runQmdQuery("query", query, options);
607
+ }
608
+ runQmdQuery(command, query, options) {
609
+ const {
610
+ limit = 10,
611
+ minScore = 0,
612
+ category,
613
+ tags,
614
+ fullContent = false,
615
+ temporalBoost = false,
616
+ relevanceThreshold,
617
+ thresholdMaxResults = 40
618
+ } = options;
619
+ if (!query.trim()) return [];
620
+ const fetchLimit = relevanceThreshold !== void 0 ? thresholdMaxResults * 2 : limit * 2;
621
+ const args = [
622
+ command,
623
+ query,
624
+ "-n",
625
+ String(fetchLimit),
626
+ "--json"
627
+ ];
628
+ if (this.collection) {
629
+ args.push("-c", this.collection);
630
+ }
631
+ const qmdResults = execQmd(args, this.qmdIndexName);
632
+ const effectiveLimit = relevanceThreshold !== void 0 ? thresholdMaxResults : limit;
633
+ const results = this.convertResults(qmdResults, {
634
+ limit: effectiveLimit,
635
+ minScore: relevanceThreshold !== void 0 ? relevanceThreshold : minScore,
636
+ category,
637
+ tags,
638
+ fullContent,
639
+ temporalBoost
640
+ });
641
+ return results;
642
+ }
643
+ // -------------------------------------------------------------------------
644
+ // v2.7 — New public APIs
645
+ // -------------------------------------------------------------------------
646
+ /**
647
+ * v2.7 — Chunk-level BM25 pre-filtered search. Ranks chunks within each
648
+ * document by keyword relevance before semantic ranking, so relevant
649
+ * content deep in long documents isn't missed.
650
+ *
651
+ * Returns results with snippets from the best-matching chunks.
652
+ */
653
+ chunkPrefilterSearch(query, options = {}) {
654
+ const terms = queryTerms(query);
655
+ const results = this.runQmdQuery("query", query, options);
656
+ for (const r of results) {
657
+ const chunks = this.chunkCache.get(r.document.id);
658
+ if (chunks && chunks.length > 0 && terms.length > 0) {
659
+ const ranked = bm25RankChunks(chunks, terms, 3);
660
+ if (ranked.length > 0 && ranked[0].score > 0) {
661
+ r.snippet = ranked.map((c) => c.text).join("\n...\n").slice(0, 600);
662
+ }
663
+ }
664
+ }
665
+ return results;
666
+ }
667
+ /**
668
+ * v2.7 — Exhaustive threshold-based search for aggregation queries.
669
+ * Keeps pulling results until relevance drops below threshold.
670
+ */
671
+ exhaustiveSearch(query, threshold = 0.01, maxResults = 40) {
672
+ return this.runQmdQuery("query", query, {
673
+ relevanceThreshold: threshold,
674
+ thresholdMaxResults: maxResults,
675
+ fullContent: false
676
+ });
677
+ }
678
+ /**
679
+ * v2.7 — Get all extracted dates, optionally filtered by document ids.
680
+ */
681
+ getDates(documentIds) {
682
+ const all = [];
683
+ const iter = documentIds ? documentIds.map((id) => [id, this.dateIndex.get(id)]).filter(([, v]) => v) : this.dateIndex.entries();
684
+ for (const [, dates] of iter) {
685
+ if (dates) all.push(...dates);
686
+ }
687
+ return all;
688
+ }
689
+ /**
690
+ * v2.7 — Get all extracted preferences, optionally filtered by document ids.
691
+ */
692
+ getPreferences(documentIds) {
693
+ const all = [];
694
+ const iter = documentIds ? documentIds.map((id) => [id, this.preferenceIndex.get(id)]).filter(([, v]) => v) : this.preferenceIndex.entries();
695
+ for (const [, prefs] of iter) {
696
+ if (prefs) all.push(...prefs);
697
+ }
698
+ return all;
699
+ }
700
+ /**
701
+ * v2.7 — Search with automatic strategy selection based on question type.
702
+ * Classifies the query and routes to the appropriate pipeline.
703
+ */
704
+ smartQuery(query, options = {}) {
705
+ const qtype = classifyQuestion(query);
706
+ switch (qtype) {
707
+ case "aggregation":
708
+ return this.exhaustiveSearch(query, 0.01, options.thresholdMaxResults ?? 40);
709
+ case "preference":
710
+ case "temporal":
711
+ default:
712
+ return this.chunkPrefilterSearch(query, { ...options, limit: options.limit ?? 10 });
713
+ }
714
+ }
715
+ /**
716
+ * Convert qmd results to ClawVault SearchResult format
717
+ */
718
+ convertResults(qmdResults, options) {
719
+ const { limit = 10, minScore = 0, category, tags, fullContent = false, temporalBoost = false } = options;
720
+ const results = [];
721
+ const maxScore = qmdResults[0]?.score || 1;
722
+ for (const qr of qmdResults) {
723
+ const filePath = this.qmdUriToPath(qr.file);
724
+ const relativePath = this.vaultPath ? path.relative(this.vaultPath, filePath) : filePath;
725
+ const normalizedRelativePath = relativePath.replace(/\\/g, "/");
726
+ if (normalizedRelativePath.startsWith("ledger/archive/") || normalizedRelativePath.includes("/ledger/archive/")) {
727
+ continue;
728
+ }
729
+ const docId = normalizedRelativePath.replace(/\.md$/, "");
730
+ let doc = this.documents.get(docId) ?? this.documents.get(docId.split("/").join(path.sep));
731
+ const modifiedAt = this.resolveModifiedAt(doc, filePath);
732
+ const parts = normalizedRelativePath.split("/");
733
+ const docCategory = parts.length > 1 ? parts[0] : "root";
734
+ if (category && docCategory !== category) continue;
735
+ if (tags && tags.length > 0 && doc) {
736
+ const docTags = new Set(doc.tags);
737
+ if (!tags.some((t) => docTags.has(t))) continue;
738
+ }
739
+ const normalizedScore = maxScore > 0 ? qr.score / maxScore : 0;
740
+ const finalScore = temporalBoost ? normalizedScore * this.getRecencyFactor(modifiedAt) : normalizedScore;
741
+ if (finalScore < minScore) continue;
742
+ if (!doc) {
743
+ doc = {
744
+ id: docId,
745
+ path: filePath,
746
+ category: docCategory,
747
+ title: qr.title || path.basename(relativePath, ".md"),
748
+ content: "",
749
+ // Content loaded separately if needed
750
+ frontmatter: {},
751
+ links: [],
752
+ tags: [],
753
+ modified: modifiedAt
754
+ };
755
+ }
756
+ results.push({
757
+ document: fullContent ? doc : { ...doc, content: "" },
758
+ score: finalScore,
759
+ snippet: this.stripSupersededFromSnippet(this.cleanSnippet(qr.snippet)),
760
+ matchedTerms: []
761
+ // qmd doesn't provide this
762
+ });
763
+ }
764
+ return results.sort((a, b) => b.score - a.score).slice(0, limit);
765
+ }
766
+ resolveModifiedAt(doc, filePath) {
767
+ if (doc) return doc.modified;
768
+ try {
769
+ return fs.statSync(filePath).mtime;
770
+ } catch {
771
+ return /* @__PURE__ */ new Date(0);
772
+ }
773
+ }
774
+ getRecencyFactor(modifiedAt) {
775
+ const ageMs = Math.max(0, Date.now() - modifiedAt.getTime());
776
+ const ageDays = ageMs / (24 * 60 * 60 * 1e3);
777
+ if (ageDays < 1) return 1;
778
+ if (ageDays <= 7) return 0.9;
779
+ return 0.7;
780
+ }
781
+ /**
782
+ * Convert qmd:// URI to file path
783
+ */
784
+ qmdUriToPath(uri) {
785
+ if (uri.startsWith("qmd://")) {
786
+ const withoutScheme = uri.slice(6);
787
+ const slashIndex = withoutScheme.indexOf("/");
788
+ if (slashIndex > -1) {
789
+ const relativePath = withoutScheme.slice(slashIndex + 1);
790
+ const root = this.collectionRoot || this.vaultPath;
791
+ if (root) {
792
+ return path.join(root, relativePath);
793
+ }
794
+ return relativePath;
795
+ }
796
+ }
797
+ return uri;
798
+ }
799
+ /**
800
+ * v2.8 — Filter superseded observation lines from snippet text.
801
+ * Ensures search results prefer the latest version of knowledge.
802
+ */
803
+ stripSupersededFromSnippet(snippet) {
804
+ if (!snippet) return snippet;
805
+ return snippet.split("\n").filter((line) => !isSuperseded(line)).join("\n");
806
+ }
807
+ /**
808
+ * Clean up qmd snippet format
809
+ */
810
+ cleanSnippet(snippet) {
811
+ if (!snippet) return "";
812
+ return snippet.replace(/@@ [-+]?\d+,?\d* @@ \([^)]+\)/g, "").trim().split("\n").slice(0, 3).join("\n").slice(0, 300);
813
+ }
814
+ /**
815
+ * Get all cached documents
816
+ */
817
+ getAllDocuments() {
818
+ return [...this.documents.values()];
819
+ }
820
+ /**
821
+ * Get document count
822
+ */
823
+ get size() {
824
+ return this.documents.size;
825
+ }
826
+ /**
827
+ * Clear the local document cache and all v2.7 indices
828
+ */
829
+ clear() {
830
+ this.documents.clear();
831
+ this.dateIndex.clear();
832
+ this.preferenceIndex.clear();
833
+ this.chunkCache.clear();
834
+ }
835
+ /**
836
+ * Export documents for persistence
837
+ */
838
+ export() {
839
+ return {
840
+ documents: [...this.documents.values()]
841
+ };
842
+ }
843
+ /**
844
+ * Import from persisted data
845
+ */
846
+ import(data) {
847
+ this.clear();
848
+ for (const doc of data.documents) {
849
+ this.addDocument(doc);
850
+ }
851
+ }
852
+ };
853
+ function extractWikiLinks(content) {
854
+ const matches = content.match(/\[\[([^\]]+)\]\]/g) || [];
855
+ return matches.map((m) => m.slice(2, -2).toLowerCase());
856
+ }
857
+ function extractTags(content) {
858
+ const matches = content.match(/#[\w-]+/g) || [];
859
+ return [...new Set(matches.map((m) => m.slice(1).toLowerCase()))];
860
+ }
861
+
862
+ // src/lib/memory-graph.ts
863
+ var fs2 = __toESM(require("fs"), 1);
864
+ var path2 = __toESM(require("path"), 1);
865
+ var import_gray_matter = __toESM(require("gray-matter"), 1);
866
+ var import_glob = require("glob");
867
+ var MEMORY_GRAPH_SCHEMA_VERSION = 1;
868
+ var GRAPH_INDEX_RELATIVE_PATH = path2.join(".clawvault", "graph-index.json");
869
+ var WIKI_LINK_RE = /\[\[([^\]]+)\]\]/g;
870
+ var HASH_TAG_RE = /(^|\s)#([\w-]+)/g;
871
+ var FRONTMATTER_RELATION_FIELDS = [
872
+ "related",
873
+ "depends_on",
874
+ "dependsOn",
875
+ "blocked_by",
876
+ "blocks",
877
+ "owner",
878
+ "project",
879
+ "people",
880
+ "links"
881
+ ];
882
+ function normalizeRelativePath(value) {
883
+ return value.split(path2.sep).join("/").replace(/^\.\//, "").replace(/^\/+/, "");
884
+ }
885
+ function toNoteKey(relativePath) {
886
+ const normalized = normalizeRelativePath(relativePath);
887
+ return normalized.toLowerCase().endsWith(".md") ? normalized.slice(0, -3) : normalized;
888
+ }
889
+ function toNoteNodeId(noteKey) {
890
+ return `note:${noteKey}`;
891
+ }
892
+ function toTagNodeId(tag) {
893
+ return `tag:${tag.toLowerCase()}`;
894
+ }
895
+ function normalizeUnresolvedKey(raw) {
896
+ const normalized = raw.trim().toLowerCase().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "").replace(/\.md$/, "").replace(/[^a-z0-9/_-]+/g, "-").replace(/\/+/g, "/").replace(/-+/g, "-").replace(/^[-/]+|[-/]+$/g, "");
897
+ return normalized || "unknown";
898
+ }
899
+ function toUnresolvedNodeId(raw) {
900
+ return `unresolved:${normalizeUnresolvedKey(raw)}`;
901
+ }
902
+ function titleFromNoteKey(noteKey) {
903
+ const basename3 = noteKey.split("/").pop() ?? noteKey;
904
+ return basename3.replace(/[-_]+/g, " ").replace(/\s+/g, " ").trim().replace(/\b\w/g, (char) => char.toUpperCase());
905
+ }
906
+ function inferNodeType(relativePath, frontmatter) {
907
+ const normalized = normalizeRelativePath(relativePath).toLowerCase();
908
+ const category = normalized.split("/")[0] ?? "note";
909
+ const explicitType = typeof frontmatter.type === "string" ? frontmatter.type.toLowerCase() : "";
910
+ if (category.includes("daily") || explicitType === "daily") return "daily";
911
+ if (category === "observations" || explicitType === "observation") return "observation";
912
+ if (category === "handoffs" || explicitType === "handoff") return "handoff";
913
+ if (category === "decisions" || explicitType === "decision") return "decision";
914
+ if (category === "lessons" || explicitType === "lesson") return "lesson";
915
+ if (category === "projects" || explicitType === "project") return "project";
916
+ if (category === "people" || explicitType === "person") return "person";
917
+ if (category === "commitments" || explicitType === "commitment") return "commitment";
918
+ return "note";
919
+ }
920
+ function ensureClawvaultDir(vaultPath) {
921
+ const dirPath = path2.join(vaultPath, ".clawvault");
922
+ if (!fs2.existsSync(dirPath)) {
923
+ fs2.mkdirSync(dirPath, { recursive: true });
924
+ }
925
+ return dirPath;
926
+ }
927
+ function getGraphIndexPath(vaultPath) {
928
+ return path2.join(vaultPath, GRAPH_INDEX_RELATIVE_PATH);
929
+ }
930
+ function normalizeWikiTarget(target) {
931
+ let value = target.trim();
932
+ if (!value) return "";
933
+ const pipeIndex = value.indexOf("|");
934
+ if (pipeIndex >= 0) {
935
+ value = value.slice(0, pipeIndex);
936
+ }
937
+ const hashIndex = value.indexOf("#");
938
+ if (hashIndex >= 0) {
939
+ value = value.slice(0, hashIndex);
940
+ }
941
+ value = value.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "");
942
+ if (value.toLowerCase().endsWith(".md")) {
943
+ value = value.slice(0, -3);
944
+ }
945
+ return value.trim();
946
+ }
947
+ function collectTags(frontmatter, markdownContent) {
948
+ const tags = /* @__PURE__ */ new Set();
949
+ const fmTags = frontmatter.tags;
950
+ if (Array.isArray(fmTags)) {
951
+ for (const tag of fmTags) {
952
+ if (typeof tag === "string" && tag.trim()) tags.add(tag.trim().toLowerCase());
953
+ }
954
+ } else if (typeof fmTags === "string") {
955
+ for (const token of fmTags.split(",")) {
956
+ const normalized = token.trim().toLowerCase();
957
+ if (normalized) tags.add(normalized);
958
+ }
959
+ }
960
+ const markdownMatches = markdownContent.matchAll(HASH_TAG_RE);
961
+ for (const match of markdownMatches) {
962
+ const tag = match[2]?.trim().toLowerCase();
963
+ if (tag) tags.add(tag);
964
+ }
965
+ return [...tags].sort((a, b) => a.localeCompare(b));
966
+ }
967
+ function extractWikiTargets(markdownContent) {
968
+ const targets = /* @__PURE__ */ new Set();
969
+ for (const match of markdownContent.matchAll(WIKI_LINK_RE)) {
970
+ const candidate = match[1];
971
+ if (!candidate) continue;
972
+ const normalized = normalizeWikiTarget(candidate);
973
+ if (normalized) targets.add(normalized);
974
+ }
975
+ return [...targets];
976
+ }
977
+ function toStringArray(value) {
978
+ if (typeof value === "string") {
979
+ return value.split(",").map((entry) => entry.trim()).filter(Boolean);
980
+ }
981
+ if (Array.isArray(value)) {
982
+ return value.flatMap((entry) => typeof entry === "string" ? entry.split(",") : []).map((entry) => entry.trim()).filter(Boolean);
983
+ }
984
+ return [];
985
+ }
986
+ function extractFrontmatterRelations(frontmatter) {
987
+ const relations = [];
988
+ for (const field of FRONTMATTER_RELATION_FIELDS) {
989
+ const raw = frontmatter[field];
990
+ for (const value of toStringArray(raw)) {
991
+ const normalized = normalizeWikiTarget(value);
992
+ if (normalized) relations.push({ field, target: normalized });
993
+ }
994
+ }
995
+ return relations;
996
+ }
997
+ function buildNoteRegistry(relativePaths) {
998
+ const byLowerPath = /* @__PURE__ */ new Map();
999
+ const byLowerBasename = /* @__PURE__ */ new Map();
1000
+ for (const relativePath of relativePaths) {
1001
+ const noteKey = toNoteKey(relativePath);
1002
+ const lowerKey = noteKey.toLowerCase();
1003
+ if (!byLowerPath.has(lowerKey)) {
1004
+ byLowerPath.set(lowerKey, noteKey);
1005
+ }
1006
+ const base = noteKey.split("/").pop() ?? noteKey;
1007
+ const lowerBase = base.toLowerCase();
1008
+ const existing = byLowerBasename.get(lowerBase) ?? [];
1009
+ existing.push(noteKey);
1010
+ byLowerBasename.set(lowerBase, existing);
1011
+ }
1012
+ return { byLowerPath, byLowerBasename };
1013
+ }
1014
+ function resolveTargetNodeId(rawTarget, registry) {
1015
+ const normalized = normalizeWikiTarget(rawTarget);
1016
+ if (!normalized) {
1017
+ return toUnresolvedNodeId(rawTarget);
1018
+ }
1019
+ const lowerTarget = normalized.toLowerCase();
1020
+ const direct = registry.byLowerPath.get(lowerTarget);
1021
+ if (direct) {
1022
+ return toNoteNodeId(direct);
1023
+ }
1024
+ if (!normalized.includes("/")) {
1025
+ const basenameMatches = registry.byLowerBasename.get(lowerTarget) ?? [];
1026
+ if (basenameMatches.length === 1) {
1027
+ return toNoteNodeId(basenameMatches[0]);
1028
+ }
1029
+ }
1030
+ return toUnresolvedNodeId(normalized);
1031
+ }
1032
+ function createEdgeId(type, source, target, label) {
1033
+ const suffix = label ? `:${label}` : "";
1034
+ return `${type}:${source}->${target}${suffix}`;
1035
+ }
1036
+ function buildFragmentNode(id, title, type, category, pathValue, tags, missing, modifiedAt) {
1037
+ return {
1038
+ id,
1039
+ title,
1040
+ type,
1041
+ category,
1042
+ path: pathValue,
1043
+ tags,
1044
+ missing,
1045
+ degree: 0,
1046
+ modifiedAt
1047
+ };
1048
+ }
1049
+ function parseFileFragment(vaultPath, relativePath, mtimeMs, registry) {
1050
+ const absolutePath = path2.join(vaultPath, relativePath);
1051
+ const raw = fs2.readFileSync(absolutePath, "utf-8");
1052
+ const parsed = (0, import_gray_matter.default)(raw);
1053
+ const frontmatter = parsed.data ?? {};
1054
+ const noteKey = toNoteKey(relativePath);
1055
+ const noteNodeId = toNoteNodeId(noteKey);
1056
+ const noteType = inferNodeType(relativePath, frontmatter);
1057
+ const tags = collectTags(frontmatter, parsed.content);
1058
+ const modifiedAt = new Date(mtimeMs).toISOString();
1059
+ const nodes = /* @__PURE__ */ new Map();
1060
+ const edges = /* @__PURE__ */ new Map();
1061
+ nodes.set(
1062
+ noteNodeId,
1063
+ buildFragmentNode(
1064
+ noteNodeId,
1065
+ typeof frontmatter.title === "string" && frontmatter.title.trim() ? frontmatter.title.trim() : titleFromNoteKey(noteKey),
1066
+ noteType,
1067
+ noteType,
1068
+ normalizeRelativePath(relativePath),
1069
+ tags,
1070
+ false,
1071
+ modifiedAt
1072
+ )
1073
+ );
1074
+ for (const tag of tags) {
1075
+ const tagNodeId = toTagNodeId(tag);
1076
+ if (!nodes.has(tagNodeId)) {
1077
+ nodes.set(tagNodeId, buildFragmentNode(tagNodeId, `#${tag}`, "tag", "tag", null, [], false, null));
1078
+ }
1079
+ const edgeId = createEdgeId("tag", noteNodeId, tagNodeId);
1080
+ edges.set(edgeId, {
1081
+ id: edgeId,
1082
+ source: noteNodeId,
1083
+ target: tagNodeId,
1084
+ type: "tag"
1085
+ });
1086
+ }
1087
+ const wikiTargets = extractWikiTargets(parsed.content);
1088
+ for (const target of wikiTargets) {
1089
+ const targetNodeId = resolveTargetNodeId(target, registry);
1090
+ if (targetNodeId.startsWith("unresolved:") && !nodes.has(targetNodeId)) {
1091
+ nodes.set(
1092
+ targetNodeId,
1093
+ buildFragmentNode(targetNodeId, titleFromNoteKey(normalizeUnresolvedKey(target)), "unresolved", "unresolved", null, [], true, null)
1094
+ );
1095
+ }
1096
+ const edgeId = createEdgeId("wiki_link", noteNodeId, targetNodeId);
1097
+ edges.set(edgeId, {
1098
+ id: edgeId,
1099
+ source: noteNodeId,
1100
+ target: targetNodeId,
1101
+ type: "wiki_link"
1102
+ });
1103
+ }
1104
+ for (const relation of extractFrontmatterRelations(frontmatter)) {
1105
+ const targetNodeId = resolveTargetNodeId(relation.target, registry);
1106
+ if (targetNodeId.startsWith("unresolved:") && !nodes.has(targetNodeId)) {
1107
+ nodes.set(
1108
+ targetNodeId,
1109
+ buildFragmentNode(
1110
+ targetNodeId,
1111
+ titleFromNoteKey(normalizeUnresolvedKey(relation.target)),
1112
+ "unresolved",
1113
+ "unresolved",
1114
+ null,
1115
+ [],
1116
+ true,
1117
+ null
1118
+ )
1119
+ );
1120
+ }
1121
+ const edgeId = createEdgeId("frontmatter_relation", noteNodeId, targetNodeId, relation.field);
1122
+ edges.set(edgeId, {
1123
+ id: edgeId,
1124
+ source: noteNodeId,
1125
+ target: targetNodeId,
1126
+ type: "frontmatter_relation",
1127
+ label: relation.field
1128
+ });
1129
+ }
1130
+ return {
1131
+ relativePath: normalizeRelativePath(relativePath),
1132
+ mtimeMs,
1133
+ nodes: [...nodes.values()],
1134
+ edges: [...edges.values()]
1135
+ };
1136
+ }
1137
+ function combineFragments(fragments, generatedAt) {
1138
+ const nodes = /* @__PURE__ */ new Map();
1139
+ const edges = /* @__PURE__ */ new Map();
1140
+ for (const fragment of Object.values(fragments)) {
1141
+ for (const node of fragment.nodes) {
1142
+ const existing = nodes.get(node.id);
1143
+ if (!existing) {
1144
+ nodes.set(node.id, { ...node, degree: 0 });
1145
+ } else if (node.modifiedAt && (!existing.modifiedAt || node.modifiedAt > existing.modifiedAt)) {
1146
+ nodes.set(node.id, { ...existing, ...node, degree: 0 });
1147
+ }
1148
+ }
1149
+ for (const edge of fragment.edges) {
1150
+ edges.set(edge.id, edge);
1151
+ }
1152
+ }
1153
+ const degreeByNode = /* @__PURE__ */ new Map();
1154
+ for (const edge of edges.values()) {
1155
+ degreeByNode.set(edge.source, (degreeByNode.get(edge.source) ?? 0) + 1);
1156
+ degreeByNode.set(edge.target, (degreeByNode.get(edge.target) ?? 0) + 1);
1157
+ }
1158
+ for (const node of nodes.values()) {
1159
+ node.degree = degreeByNode.get(node.id) ?? 0;
1160
+ }
1161
+ const nodeTypeCounts = {};
1162
+ for (const node of nodes.values()) {
1163
+ nodeTypeCounts[node.type] = (nodeTypeCounts[node.type] ?? 0) + 1;
1164
+ }
1165
+ const edgeTypeCounts = {};
1166
+ for (const edge of edges.values()) {
1167
+ edgeTypeCounts[edge.type] = (edgeTypeCounts[edge.type] ?? 0) + 1;
1168
+ }
1169
+ const sortedNodes = [...nodes.values()].sort((a, b) => a.id.localeCompare(b.id));
1170
+ const sortedEdges = [...edges.values()].sort((a, b) => a.id.localeCompare(b.id));
1171
+ return {
1172
+ schemaVersion: MEMORY_GRAPH_SCHEMA_VERSION,
1173
+ nodes: sortedNodes,
1174
+ edges: sortedEdges,
1175
+ stats: {
1176
+ generatedAt,
1177
+ nodeCount: sortedNodes.length,
1178
+ edgeCount: sortedEdges.length,
1179
+ nodeTypeCounts,
1180
+ edgeTypeCounts
1181
+ }
1182
+ };
1183
+ }
1184
+ function isValidIndex(index) {
1185
+ if (!index || typeof index !== "object") return false;
1186
+ const typed = index;
1187
+ return typed.schemaVersion === MEMORY_GRAPH_SCHEMA_VERSION && typeof typed.vaultPath === "string" && typeof typed.generatedAt === "string" && Boolean(typed.files && typeof typed.files === "object") && Boolean(typed.graph && typeof typed.graph === "object");
1188
+ }
1189
+ function loadMemoryGraphIndex(vaultPath) {
1190
+ const indexPath = getGraphIndexPath(path2.resolve(vaultPath));
1191
+ if (!fs2.existsSync(indexPath)) {
1192
+ return null;
1193
+ }
1194
+ try {
1195
+ const parsed = JSON.parse(fs2.readFileSync(indexPath, "utf-8"));
1196
+ if (!isValidIndex(parsed)) {
1197
+ return null;
1198
+ }
1199
+ return parsed;
1200
+ } catch {
1201
+ return null;
1202
+ }
1203
+ }
1204
+ async function buildOrUpdateMemoryGraphIndex(vaultPathInput, options = {}) {
1205
+ const vaultPath = path2.resolve(vaultPathInput);
1206
+ ensureClawvaultDir(vaultPath);
1207
+ const existing = options.forceFull ? null : loadMemoryGraphIndex(vaultPath);
1208
+ const markdownFiles = await (0, import_glob.glob)("**/*.md", {
1209
+ cwd: vaultPath,
1210
+ ignore: ["**/node_modules/**", "**/.git/**", "**/.obsidian/**", "**/.trash/**", "**/ledger/archive/**"]
1211
+ });
1212
+ const normalizedFiles = markdownFiles.map(normalizeRelativePath).sort((a, b) => a.localeCompare(b));
1213
+ const registry = buildNoteRegistry(normalizedFiles);
1214
+ const nextFragments = {};
1215
+ const existingFragments = existing?.files ?? {};
1216
+ const currentFileSet = new Set(normalizedFiles);
1217
+ for (const relativePath of normalizedFiles) {
1218
+ const absolutePath = path2.join(vaultPath, relativePath);
1219
+ const stat = fs2.statSync(absolutePath);
1220
+ const existingFragment = existingFragments[relativePath];
1221
+ if (!options.forceFull && existingFragment && existingFragment.mtimeMs === stat.mtimeMs) {
1222
+ nextFragments[relativePath] = existingFragment;
1223
+ continue;
1224
+ }
1225
+ nextFragments[relativePath] = parseFileFragment(vaultPath, relativePath, stat.mtimeMs, registry);
1226
+ }
1227
+ for (const [relativePath, fragment] of Object.entries(existingFragments)) {
1228
+ if (!currentFileSet.has(relativePath)) {
1229
+ continue;
1230
+ }
1231
+ if (!nextFragments[relativePath]) {
1232
+ nextFragments[relativePath] = fragment;
1233
+ }
1234
+ }
1235
+ const generatedAt = (/* @__PURE__ */ new Date()).toISOString();
1236
+ const graph = combineFragments(nextFragments, generatedAt);
1237
+ const nextIndex = {
1238
+ schemaVersion: MEMORY_GRAPH_SCHEMA_VERSION,
1239
+ vaultPath,
1240
+ generatedAt,
1241
+ files: nextFragments,
1242
+ graph
1243
+ };
1244
+ fs2.writeFileSync(getGraphIndexPath(vaultPath), JSON.stringify(nextIndex, null, 2));
1245
+ return nextIndex;
1246
+ }
1247
+
1248
+ // src/lib/vault.ts
1249
+ var import_meta = {};
1250
+ var CONFIG_FILE = ".clawvault.json";
1251
+ var INDEX_FILE = ".clawvault-index.json";
1252
+ var ClawVault = class {
1253
+ config;
1254
+ search;
1255
+ initialized = false;
1256
+ constructor(vaultPath) {
1257
+ if (!hasQmd()) {
1258
+ throw new QmdUnavailableError();
1259
+ }
1260
+ this.config = {
1261
+ path: path3.resolve(vaultPath),
1262
+ name: path3.basename(vaultPath),
1263
+ categories: DEFAULT_CATEGORIES,
1264
+ qmdCollection: void 0,
1265
+ qmdRoot: void 0
1266
+ };
1267
+ this.search = new SearchEngine();
1268
+ this.applyQmdConfig();
1269
+ }
1270
+ /**
1271
+ * Initialize a new vault
1272
+ */
1273
+ async init(options = {}, initFlags) {
1274
+ if (!hasQmd()) {
1275
+ throw new QmdUnavailableError();
1276
+ }
1277
+ const vaultPath = this.config.path;
1278
+ const flags = initFlags || {};
1279
+ this.config = { ...this.config, ...options };
1280
+ this.applyQmdConfig();
1281
+ if (flags.skipTasks) {
1282
+ this.config.categories = this.config.categories.filter(
1283
+ (c) => !["tasks", "backlog"].includes(c)
1284
+ );
1285
+ }
1286
+ if (!fs3.existsSync(vaultPath)) {
1287
+ fs3.mkdirSync(vaultPath, { recursive: true });
1288
+ }
1289
+ for (const category of this.config.categories) {
1290
+ const catPath = path3.join(vaultPath, category);
1291
+ if (!fs3.existsSync(catPath)) {
1292
+ fs3.mkdirSync(catPath, { recursive: true });
1293
+ }
1294
+ }
1295
+ const ledgerDirs = ["ledger/raw", "ledger/observations", "ledger/reflections"];
1296
+ for (const dir of ledgerDirs) {
1297
+ const dirPath = path3.join(vaultPath, dir);
1298
+ if (!fs3.existsSync(dirPath)) {
1299
+ fs3.mkdirSync(dirPath, { recursive: true });
1300
+ }
1301
+ }
1302
+ await this.createTemplates();
1303
+ const readmePath = path3.join(vaultPath, "README.md");
1304
+ if (!fs3.existsSync(readmePath)) {
1305
+ fs3.writeFileSync(readmePath, this.generateReadme());
1306
+ }
1307
+ await this.createWelcomeNote();
1308
+ const configPath = path3.join(vaultPath, CONFIG_FILE);
1309
+ const meta = {
1310
+ name: this.config.name,
1311
+ version: "1.0.0",
1312
+ created: (/* @__PURE__ */ new Date()).toISOString(),
1313
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString(),
1314
+ categories: this.config.categories,
1315
+ documentCount: 0,
1316
+ qmdCollection: this.getQmdCollection(),
1317
+ qmdRoot: this.getQmdRoot()
1318
+ };
1319
+ fs3.writeFileSync(configPath, JSON.stringify(meta, null, 2));
1320
+ if (!flags.skipBases && this.config.categories.includes("tasks")) {
1321
+ this.createBasesFiles();
1322
+ }
1323
+ if (!flags.skipGraph) {
1324
+ await this.syncMemoryGraphIndex({ forceFull: true });
1325
+ }
1326
+ this.initialized = true;
1327
+ }
1328
+ createBasesFiles() {
1329
+ const vaultPath = this.config.path;
1330
+ const basesFiles = {
1331
+ "all-tasks.base": [
1332
+ "filters:",
1333
+ " and:",
1334
+ ' - file.inFolder("tasks")',
1335
+ ' - status != "done"',
1336
+ "formulas:",
1337
+ " age: (now() - file.ctime).days",
1338
+ ' status_icon: if(status == "blocked", "\u{1F534}", if(status == "in-progress", "\u{1F528}", if(status == "open", "\u26AA", "\u2705")))',
1339
+ "views:",
1340
+ " - type: table",
1341
+ " name: All Active Tasks",
1342
+ " groupBy:",
1343
+ " property: status",
1344
+ " direction: ASC",
1345
+ " order:",
1346
+ " - formula.status_icon",
1347
+ " - file.name",
1348
+ " - status",
1349
+ " - owner",
1350
+ " - project",
1351
+ " - priority",
1352
+ " - blocked_by",
1353
+ " - formula.age",
1354
+ " - type: cards",
1355
+ " name: Task Board",
1356
+ " groupBy:",
1357
+ " property: status",
1358
+ " direction: ASC",
1359
+ " order:",
1360
+ " - file.name",
1361
+ " - owner",
1362
+ " - project",
1363
+ " - priority"
1364
+ ].join("\n"),
1365
+ "blocked.base": [
1366
+ "filters:",
1367
+ " and:",
1368
+ ' - file.inFolder("tasks")',
1369
+ ' - status == "blocked"',
1370
+ "formulas:",
1371
+ " days_blocked: (now() - file.ctime).days",
1372
+ "views:",
1373
+ " - type: table",
1374
+ " name: Blocked Tasks",
1375
+ " order:",
1376
+ " - file.name",
1377
+ " - owner",
1378
+ " - project",
1379
+ " - blocked_by",
1380
+ " - formula.days_blocked",
1381
+ " - priority"
1382
+ ].join("\n"),
1383
+ "by-project.base": [
1384
+ "filters:",
1385
+ " and:",
1386
+ ' - file.inFolder("tasks")',
1387
+ ' - status != "done"',
1388
+ "formulas:",
1389
+ ' status_icon: if(status == "blocked", "\u{1F534}", if(status == "in-progress", "\u{1F528}", "\u26AA"))',
1390
+ "views:",
1391
+ " - type: table",
1392
+ " name: By Project",
1393
+ " groupBy:",
1394
+ " property: project",
1395
+ " direction: ASC",
1396
+ " order:",
1397
+ " - formula.status_icon",
1398
+ " - file.name",
1399
+ " - status",
1400
+ " - owner",
1401
+ " - priority"
1402
+ ].join("\n"),
1403
+ "backlog.base": [
1404
+ "filters:",
1405
+ " and:",
1406
+ ' - file.inFolder("backlog")',
1407
+ "views:",
1408
+ " - type: table",
1409
+ " name: Backlog",
1410
+ " order:",
1411
+ " - file.name",
1412
+ " - source",
1413
+ " - project",
1414
+ " - file.ctime"
1415
+ ].join("\n")
1416
+ };
1417
+ for (const [filename, content] of Object.entries(basesFiles)) {
1418
+ const filePath = path3.join(vaultPath, filename);
1419
+ if (!fs3.existsSync(filePath)) {
1420
+ fs3.writeFileSync(filePath, content);
1421
+ }
1422
+ }
1423
+ }
1424
+ /**
1425
+ * Load an existing vault
1426
+ */
1427
+ async load() {
1428
+ if (!hasQmd()) {
1429
+ throw new QmdUnavailableError();
1430
+ }
1431
+ const vaultPath = this.config.path;
1432
+ const configPath = path3.join(vaultPath, CONFIG_FILE);
1433
+ if (!fs3.existsSync(configPath)) {
1434
+ throw new Error(`Not a ClawVault: ${vaultPath} (missing ${CONFIG_FILE})`);
1435
+ }
1436
+ const meta = JSON.parse(fs3.readFileSync(configPath, "utf-8"));
1437
+ this.config.name = meta.name;
1438
+ this.config.categories = meta.categories;
1439
+ this.config.qmdCollection = meta.qmdCollection;
1440
+ this.config.qmdRoot = meta.qmdRoot;
1441
+ if (!meta.qmdCollection || !meta.qmdRoot) {
1442
+ meta.qmdCollection = meta.qmdCollection || meta.name;
1443
+ meta.qmdRoot = meta.qmdRoot || this.config.path;
1444
+ fs3.writeFileSync(configPath, JSON.stringify(meta, null, 2));
1445
+ }
1446
+ this.applyQmdConfig(meta);
1447
+ await this.reindex();
1448
+ this.initialized = true;
1449
+ }
1450
+ /**
1451
+ * Reindex all documents
1452
+ */
1453
+ async reindex() {
1454
+ this.search.clear();
1455
+ const files = await (0, import_glob2.glob)("**/*.md", {
1456
+ cwd: this.config.path,
1457
+ ignore: ["**/node_modules/**", "**/.*", "**/ledger/archive/**"]
1458
+ });
1459
+ for (const file of files) {
1460
+ const doc = await this.loadDocument(file);
1461
+ if (doc) {
1462
+ this.search.addDocument(doc);
1463
+ }
1464
+ }
1465
+ await this.saveIndex();
1466
+ await this.syncMemoryGraphIndex();
1467
+ return this.search.size;
1468
+ }
1469
+ /**
1470
+ * Load a document from disk
1471
+ */
1472
+ async loadDocument(relativePath) {
1473
+ try {
1474
+ const fullPath = path3.join(this.config.path, relativePath);
1475
+ const content = fs3.readFileSync(fullPath, "utf-8");
1476
+ const { data: frontmatter, content: body } = (0, import_gray_matter2.default)(content);
1477
+ const stats = fs3.statSync(fullPath);
1478
+ const parts = relativePath.split(path3.sep);
1479
+ const category = parts.length > 1 ? parts[0] : "root";
1480
+ const filename = path3.basename(relativePath, ".md");
1481
+ return {
1482
+ id: relativePath.replace(/\.md$/, ""),
1483
+ path: fullPath,
1484
+ category,
1485
+ title: frontmatter.title || filename,
1486
+ content: body,
1487
+ frontmatter,
1488
+ links: extractWikiLinks(body),
1489
+ tags: extractTags(body),
1490
+ modified: stats.mtime
1491
+ };
1492
+ } catch (err) {
1493
+ console.error(`Error loading ${relativePath}:`, err);
1494
+ return null;
1495
+ }
1496
+ }
1497
+ /**
1498
+ * Store a new document
1499
+ */
1500
+ async store(options) {
1501
+ const {
1502
+ category,
1503
+ title,
1504
+ content,
1505
+ frontmatter = {},
1506
+ overwrite = false,
1507
+ qmdUpdate: triggerUpdate = false,
1508
+ qmdEmbed: triggerEmbed = false,
1509
+ qmdIndexName
1510
+ } = options;
1511
+ const filename = this.slugify(title) + ".md";
1512
+ const relativePath = path3.join(category, filename);
1513
+ const fullPath = path3.join(this.config.path, relativePath);
1514
+ if (fs3.existsSync(fullPath) && !overwrite) {
1515
+ throw new Error(`Document already exists: ${relativePath}. Use overwrite: true to replace.`);
1516
+ }
1517
+ const categoryPath = path3.join(this.config.path, category);
1518
+ if (!fs3.existsSync(categoryPath)) {
1519
+ fs3.mkdirSync(categoryPath, { recursive: true });
1520
+ }
1521
+ const fm = {
1522
+ title,
1523
+ date: (/* @__PURE__ */ new Date()).toISOString().split("T")[0],
1524
+ ...frontmatter
1525
+ };
1526
+ const fileContent = import_gray_matter2.default.stringify(content, fm);
1527
+ fs3.writeFileSync(fullPath, fileContent);
1528
+ const doc = await this.loadDocument(relativePath);
1529
+ if (doc) {
1530
+ this.search.addDocument(doc);
1531
+ await this.saveIndex();
1532
+ await this.syncMemoryGraphIndex();
1533
+ }
1534
+ if (triggerUpdate || triggerEmbed) {
1535
+ qmdUpdate(this.getQmdCollection(), qmdIndexName);
1536
+ if (triggerEmbed) {
1537
+ qmdEmbed(this.getQmdCollection(), qmdIndexName);
1538
+ }
1539
+ }
1540
+ return doc;
1541
+ }
1542
+ /**
1543
+ * Quick store to inbox
1544
+ */
1545
+ async capture(note, title) {
1546
+ const autoTitle = title || `note-${Date.now()}`;
1547
+ return this.store({
1548
+ category: "inbox",
1549
+ title: autoTitle,
1550
+ content: note
1551
+ });
1552
+ }
1553
+ /**
1554
+ * Search the vault (BM25 via qmd)
1555
+ */
1556
+ async find(query, options = {}) {
1557
+ return this.search.search(query, options);
1558
+ }
1559
+ /**
1560
+ * Semantic/vector search (via qmd vsearch)
1561
+ */
1562
+ async vsearch(query, options = {}) {
1563
+ return this.search.vsearch(query, options);
1564
+ }
1565
+ /**
1566
+ * Combined search with query expansion (via qmd query)
1567
+ */
1568
+ async query(query, options = {}) {
1569
+ return this.search.query(query, options);
1570
+ }
1571
+ /**
1572
+ * Get a document by ID or path
1573
+ */
1574
+ async get(idOrPath) {
1575
+ const normalized = idOrPath.replace(/\.md$/, "");
1576
+ const docs = this.search.getAllDocuments();
1577
+ return docs.find((d) => d.id === normalized) || null;
1578
+ }
1579
+ /**
1580
+ * List documents in a category
1581
+ */
1582
+ async list(category) {
1583
+ const docs = this.search.getAllDocuments();
1584
+ if (category) {
1585
+ return docs.filter((d) => d.category === category);
1586
+ }
1587
+ return docs;
1588
+ }
1589
+ /**
1590
+ * Sync vault to another location (for Obsidian on Windows, etc.)
1591
+ */
1592
+ async sync(options) {
1593
+ const { target, deleteOrphans = false, dryRun = false } = options;
1594
+ const result = {
1595
+ copied: [],
1596
+ deleted: [],
1597
+ unchanged: [],
1598
+ errors: []
1599
+ };
1600
+ const sourceFiles = await (0, import_glob2.glob)("**/*.md", {
1601
+ cwd: this.config.path,
1602
+ ignore: ["**/node_modules/**"]
1603
+ });
1604
+ if (!dryRun && !fs3.existsSync(target)) {
1605
+ fs3.mkdirSync(target, { recursive: true });
1606
+ }
1607
+ for (const file of sourceFiles) {
1608
+ const sourcePath = path3.join(this.config.path, file);
1609
+ const targetPath = path3.join(target, file);
1610
+ try {
1611
+ const sourceStats = fs3.statSync(sourcePath);
1612
+ let shouldCopy = true;
1613
+ if (fs3.existsSync(targetPath)) {
1614
+ const targetStats = fs3.statSync(targetPath);
1615
+ if (sourceStats.mtime <= targetStats.mtime) {
1616
+ result.unchanged.push(file);
1617
+ shouldCopy = false;
1618
+ }
1619
+ }
1620
+ if (shouldCopy) {
1621
+ if (!dryRun) {
1622
+ const targetDir = path3.dirname(targetPath);
1623
+ if (!fs3.existsSync(targetDir)) {
1624
+ fs3.mkdirSync(targetDir, { recursive: true });
1625
+ }
1626
+ fs3.copyFileSync(sourcePath, targetPath);
1627
+ }
1628
+ result.copied.push(file);
1629
+ }
1630
+ } catch (err) {
1631
+ result.errors.push(`${file}: ${err}`);
1632
+ }
1633
+ }
1634
+ if (deleteOrphans) {
1635
+ const targetFiles = await (0, import_glob2.glob)("**/*.md", { cwd: target });
1636
+ const sourceSet = new Set(sourceFiles);
1637
+ for (const file of targetFiles) {
1638
+ if (!sourceSet.has(file)) {
1639
+ if (!dryRun) {
1640
+ fs3.unlinkSync(path3.join(target, file));
1641
+ }
1642
+ result.deleted.push(file);
1643
+ }
1644
+ }
1645
+ }
1646
+ return result;
1647
+ }
1648
+ /**
1649
+ * Get vault statistics
1650
+ */
1651
+ async stats() {
1652
+ const docs = this.search.getAllDocuments();
1653
+ const categories = {};
1654
+ const allTags = /* @__PURE__ */ new Set();
1655
+ let totalLinks = 0;
1656
+ for (const doc of docs) {
1657
+ categories[doc.category] = (categories[doc.category] || 0) + 1;
1658
+ totalLinks += doc.links.length;
1659
+ doc.tags.forEach((t) => allTags.add(t));
1660
+ }
1661
+ return {
1662
+ documents: docs.length,
1663
+ categories,
1664
+ links: totalLinks,
1665
+ tags: [...allTags].sort()
1666
+ };
1667
+ }
1668
+ /**
1669
+ * Get all categories
1670
+ */
1671
+ getCategories() {
1672
+ return this.config.categories;
1673
+ }
1674
+ /**
1675
+ * Check if vault is initialized
1676
+ */
1677
+ isInitialized() {
1678
+ return this.initialized;
1679
+ }
1680
+ /**
1681
+ * Get vault path
1682
+ */
1683
+ getPath() {
1684
+ return this.config.path;
1685
+ }
1686
+ /**
1687
+ * Get vault name
1688
+ */
1689
+ getName() {
1690
+ return this.config.name;
1691
+ }
1692
+ /**
1693
+ * Get qmd collection name
1694
+ */
1695
+ getQmdCollection() {
1696
+ return this.config.qmdCollection || this.config.name;
1697
+ }
1698
+ /**
1699
+ * Get qmd collection root
1700
+ */
1701
+ getQmdRoot() {
1702
+ return this.config.qmdRoot || this.config.path;
1703
+ }
1704
+ // === Memory Type System ===
1705
+ /**
1706
+ * Store a memory with type classification
1707
+ * Automatically routes to correct category based on type
1708
+ */
1709
+ async remember(type, title, content, frontmatter = {}) {
1710
+ const category = TYPE_TO_CATEGORY[type];
1711
+ return this.store({
1712
+ category,
1713
+ title,
1714
+ content,
1715
+ frontmatter: { ...frontmatter, memoryType: type }
1716
+ });
1717
+ }
1718
+ // === Handoff System ===
1719
+ /**
1720
+ * Create a session handoff document
1721
+ * Call this before context death or long pauses
1722
+ */
1723
+ async createHandoff(handoff) {
1724
+ const now = /* @__PURE__ */ new Date();
1725
+ const dateStr = now.toISOString().split("T")[0];
1726
+ const timeStr = now.toISOString().split("T")[1].slice(0, 5).replace(":", "");
1727
+ const fullHandoff = {
1728
+ ...handoff,
1729
+ created: now.toISOString()
1730
+ };
1731
+ const content = this.formatHandoff(fullHandoff);
1732
+ const frontmatter = {
1733
+ type: "handoff",
1734
+ workingOn: handoff.workingOn,
1735
+ blocked: handoff.blocked,
1736
+ nextSteps: handoff.nextSteps
1737
+ };
1738
+ if (handoff.sessionKey) frontmatter.sessionKey = handoff.sessionKey;
1739
+ if (handoff.feeling) frontmatter.feeling = handoff.feeling;
1740
+ if (handoff.decisions) frontmatter.decisions = handoff.decisions;
1741
+ if (handoff.openQuestions) frontmatter.openQuestions = handoff.openQuestions;
1742
+ return this.store({
1743
+ category: "handoffs",
1744
+ title: `handoff-${dateStr}-${timeStr}`,
1745
+ content,
1746
+ frontmatter
1747
+ });
1748
+ }
1749
+ /**
1750
+ * Format handoff as readable markdown
1751
+ */
1752
+ formatHandoff(h) {
1753
+ let md = `# Session Handoff
1754
+
1755
+ `;
1756
+ md += `**Created:** ${h.created}
1757
+ `;
1758
+ if (h.sessionKey) md += `**Session:** ${h.sessionKey}
1759
+ `;
1760
+ if (h.feeling) md += `**Feeling:** ${h.feeling}
1761
+ `;
1762
+ md += `
1763
+ `;
1764
+ md += `## Working On
1765
+ `;
1766
+ h.workingOn.forEach((w) => md += `- ${w}
1767
+ `);
1768
+ md += `
1769
+ `;
1770
+ md += `## Blocked
1771
+ `;
1772
+ if (h.blocked.length === 0) md += `- Nothing currently blocked
1773
+ `;
1774
+ else h.blocked.forEach((b) => md += `- ${b}
1775
+ `);
1776
+ md += `
1777
+ `;
1778
+ md += `## Next Steps
1779
+ `;
1780
+ h.nextSteps.forEach((n) => md += `- ${n}
1781
+ `);
1782
+ if (h.decisions && h.decisions.length > 0) {
1783
+ md += `
1784
+ ## Decisions Made
1785
+ `;
1786
+ h.decisions.forEach((d) => md += `- ${d}
1787
+ `);
1788
+ }
1789
+ if (h.openQuestions && h.openQuestions.length > 0) {
1790
+ md += `
1791
+ ## Open Questions
1792
+ `;
1793
+ h.openQuestions.forEach((q) => md += `- ${q}
1794
+ `);
1795
+ }
1796
+ return md;
1797
+ }
1798
+ // === Session Recap (Bootstrap Hook) ===
1799
+ /**
1800
+ * Generate a session recap - who I was
1801
+ * Call this on bootstrap to restore context
1802
+ */
1803
+ async generateRecap(options = {}) {
1804
+ const { handoffLimit = 3, brief = false } = options;
1805
+ const handoffDocs = await this.list("handoffs");
1806
+ const recentHandoffs = handoffDocs.sort((a, b) => b.modified.getTime() - a.modified.getTime()).slice(0, handoffLimit).map((doc) => this.parseHandoff(doc));
1807
+ const projectDocs = await this.list("projects");
1808
+ const activeProjects = projectDocs.filter((d) => d.frontmatter.status !== "completed" && d.frontmatter.status !== "archived").map((d) => d.title);
1809
+ const commitmentDocs = await this.list("commitments");
1810
+ const pendingCommitments = commitmentDocs.filter((d) => d.frontmatter.status !== "done").map((d) => d.title);
1811
+ const decisionDocs = await this.list("decisions");
1812
+ const recentDecisions = decisionDocs.sort((a, b) => b.modified.getTime() - a.modified.getTime()).slice(0, brief ? 3 : 5).map((d) => d.title);
1813
+ const lessonDocs = await this.list("lessons");
1814
+ const recentLessons = lessonDocs.sort((a, b) => b.modified.getTime() - a.modified.getTime()).slice(0, brief ? 3 : 5).map((d) => d.title);
1815
+ let keyRelationships = [];
1816
+ if (!brief) {
1817
+ const peopleDocs = await this.list("people");
1818
+ keyRelationships = peopleDocs.filter((d) => d.frontmatter.importance === "high" || d.frontmatter.role).map((d) => `${d.title}${d.frontmatter.role ? ` (${d.frontmatter.role})` : ""}`);
1819
+ }
1820
+ const feelings = recentHandoffs.map((h) => h.feeling).filter(Boolean);
1821
+ const emotionalArc = feelings.length > 0 ? feelings.join(" \u2192 ") : void 0;
1822
+ return {
1823
+ generated: (/* @__PURE__ */ new Date()).toISOString(),
1824
+ recentHandoffs,
1825
+ activeProjects,
1826
+ pendingCommitments,
1827
+ recentDecisions,
1828
+ recentLessons,
1829
+ keyRelationships,
1830
+ emotionalArc
1831
+ };
1832
+ }
1833
+ /**
1834
+ * Format recap as readable markdown for injection
1835
+ */
1836
+ formatRecap(recap, options = {}) {
1837
+ const { brief = false } = options;
1838
+ let md = `# Who I Was
1839
+
1840
+ `;
1841
+ md += `*Generated: ${recap.generated}*
1842
+
1843
+ `;
1844
+ if (recap.emotionalArc) {
1845
+ md += `**Emotional arc:** ${recap.emotionalArc}
1846
+
1847
+ `;
1848
+ }
1849
+ if (recap.recentHandoffs.length > 0) {
1850
+ md += `## Recent Sessions
1851
+ `;
1852
+ for (const h of recap.recentHandoffs) {
1853
+ if (brief) {
1854
+ md += `- **${h.created.split("T")[0]}:** ${h.workingOn.slice(0, 2).join(", ")}`;
1855
+ if (h.nextSteps.length > 0) md += ` \u2192 ${h.nextSteps[0]}`;
1856
+ md += `
1857
+ `;
1858
+ } else {
1859
+ md += `
1860
+ ### ${h.created.split("T")[0]}
1861
+ `;
1862
+ md += `**Working on:** ${h.workingOn.join(", ")}
1863
+ `;
1864
+ if (h.blocked.length > 0) md += `**Blocked:** ${h.blocked.join(", ")}
1865
+ `;
1866
+ md += `**Next:** ${h.nextSteps.join(", ")}
1867
+ `;
1868
+ }
1869
+ }
1870
+ md += `
1871
+ `;
1872
+ }
1873
+ if (recap.activeProjects.length > 0) {
1874
+ md += `## Active Projects
1875
+ `;
1876
+ recap.activeProjects.forEach((p) => md += `- ${p}
1877
+ `);
1878
+ md += `
1879
+ `;
1880
+ }
1881
+ if (recap.pendingCommitments.length > 0) {
1882
+ md += `## Pending Commitments
1883
+ `;
1884
+ recap.pendingCommitments.forEach((c) => md += `- ${c}
1885
+ `);
1886
+ md += `
1887
+ `;
1888
+ }
1889
+ if (recap.recentDecisions && recap.recentDecisions.length > 0) {
1890
+ md += `## Recent Decisions
1891
+ `;
1892
+ recap.recentDecisions.forEach((d) => md += `- ${d}
1893
+ `);
1894
+ md += `
1895
+ `;
1896
+ }
1897
+ if (recap.recentLessons.length > 0) {
1898
+ md += `## Recent Lessons
1899
+ `;
1900
+ recap.recentLessons.forEach((l) => md += `- ${l}
1901
+ `);
1902
+ md += `
1903
+ `;
1904
+ }
1905
+ if (!brief && recap.keyRelationships.length > 0) {
1906
+ md += `## Key People
1907
+ `;
1908
+ recap.keyRelationships.forEach((r) => md += `- ${r}
1909
+ `);
1910
+ }
1911
+ return md;
1912
+ }
1913
+ /**
1914
+ * Parse a handoff document back into structured form
1915
+ */
1916
+ parseHandoff(doc) {
1917
+ return {
1918
+ created: doc.frontmatter.date || doc.modified.toISOString(),
1919
+ sessionKey: doc.frontmatter.sessionKey,
1920
+ workingOn: doc.frontmatter.workingOn || [],
1921
+ blocked: doc.frontmatter.blocked || [],
1922
+ nextSteps: doc.frontmatter.nextSteps || [],
1923
+ decisions: doc.frontmatter.decisions,
1924
+ openQuestions: doc.frontmatter.openQuestions,
1925
+ feeling: doc.frontmatter.feeling
1926
+ };
1927
+ }
1928
+ // === Private helpers ===
1929
+ applyQmdConfig(meta) {
1930
+ const collection = meta?.qmdCollection || this.config.qmdCollection || this.config.name;
1931
+ const root = meta?.qmdRoot || this.config.qmdRoot || this.config.path;
1932
+ this.config.qmdCollection = collection;
1933
+ this.config.qmdRoot = root;
1934
+ this.search.setVaultPath(this.config.path);
1935
+ this.search.setCollection(collection);
1936
+ this.search.setCollectionRoot(root);
1937
+ }
1938
+ slugify(text) {
1939
+ return text.toLowerCase().replace(/[^\w\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").trim();
1940
+ }
1941
+ async saveIndex() {
1942
+ const indexPath = path3.join(this.config.path, INDEX_FILE);
1943
+ const data = this.search.export();
1944
+ fs3.writeFileSync(indexPath, JSON.stringify(data, null, 2));
1945
+ const configPath = path3.join(this.config.path, CONFIG_FILE);
1946
+ if (fs3.existsSync(configPath)) {
1947
+ const meta = JSON.parse(fs3.readFileSync(configPath, "utf-8"));
1948
+ meta.lastUpdated = (/* @__PURE__ */ new Date()).toISOString();
1949
+ meta.documentCount = this.search.size;
1950
+ fs3.writeFileSync(configPath, JSON.stringify(meta, null, 2));
1951
+ }
1952
+ }
1953
+ async createTemplates() {
1954
+ const templatesPath = path3.join(this.config.path, "templates");
1955
+ if (!fs3.existsSync(templatesPath)) {
1956
+ fs3.mkdirSync(templatesPath, { recursive: true });
1957
+ }
1958
+ const moduleDir = path3.dirname((0, import_url.fileURLToPath)(import_meta.url));
1959
+ const candidates = [
1960
+ path3.resolve(moduleDir, "../templates"),
1961
+ path3.resolve(moduleDir, "../../templates")
1962
+ ];
1963
+ const builtinDir = candidates.find((dir) => fs3.existsSync(dir) && fs3.statSync(dir).isDirectory());
1964
+ if (!builtinDir) return;
1965
+ for (const entry of fs3.readdirSync(builtinDir, { withFileTypes: true })) {
1966
+ if (!entry.isFile() || !entry.name.endsWith(".md")) continue;
1967
+ if (entry.name === "daily.md") continue;
1968
+ const sourcePath = path3.join(builtinDir, entry.name);
1969
+ const targetPath = path3.join(templatesPath, entry.name);
1970
+ if (!fs3.existsSync(targetPath)) {
1971
+ fs3.copyFileSync(sourcePath, targetPath);
1972
+ }
1973
+ }
1974
+ }
1975
+ async createWelcomeNote() {
1976
+ if (!this.config.categories.includes("inbox")) return;
1977
+ const inboxPath = path3.join(this.config.path, "inbox", "welcome.md");
1978
+ if (fs3.existsSync(inboxPath)) return;
1979
+ const now = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
1980
+ const content = `---
1981
+ title: "Welcome to ${this.config.name}"
1982
+ date: ${now}
1983
+ type: fact
1984
+ tags: [welcome, getting-started]
1985
+ ---
1986
+
1987
+ # Welcome to ${this.config.name}
1988
+
1989
+ Your vault is ready. Here's what you can do:
1990
+
1991
+ ## Quick Start
1992
+
1993
+ - **Capture a thought:** \`clawvault capture "your note here"\`
1994
+ - **Store structured memory:** \`clawvault store --category decisions --title "My Choice" --content "..."\`
1995
+ - **Search your vault:** \`clawvault search "query"\`
1996
+ - **See your knowledge graph:** \`clawvault graph\`
1997
+ - **Get context for a topic:** \`clawvault context "topic"\`
1998
+
1999
+ ## Vault Structure
2000
+
2001
+ Your vault organizes memories by type \u2014 decisions, lessons, people, projects, and more.
2002
+ Each category is a folder. Each memory is a markdown file with frontmatter.
2003
+
2004
+ ## Observational Memory
2005
+
2006
+ When connected to an AI agent (like OpenClaw), your vault can automatically observe
2007
+ conversations and extract important memories \u2014 decisions, lessons, commitments \u2014 without
2008
+ manual effort.
2009
+
2010
+ ## Wiki-Links
2011
+
2012
+ Use \`[[double brackets]]\` to link between notes. Your memory graph tracks these
2013
+ connections, building a knowledge network that grows with you.
2014
+
2015
+ ---
2016
+
2017
+ *Delete this file anytime. It's just here to say hello.*
2018
+ `;
2019
+ fs3.writeFileSync(inboxPath, content);
2020
+ }
2021
+ async syncMemoryGraphIndex(options = {}) {
2022
+ try {
2023
+ await buildOrUpdateMemoryGraphIndex(this.config.path, options);
2024
+ } catch {
2025
+ }
2026
+ }
2027
+ generateReadme() {
2028
+ const coreCategories = this.config.categories.filter((c) => !["templates", "tasks", "backlog"].includes(c));
2029
+ const workCategories = this.config.categories.filter((c) => ["tasks", "backlog"].includes(c));
2030
+ return `# ${this.config.name}
2031
+
2032
+ An elephant never forgets.
2033
+
2034
+ ## Structure
2035
+
2036
+ ### Memory Categories
2037
+ ${coreCategories.map((c) => `- \`${c}/\` \u2014 ${this.getCategoryDescription(c)}`).join("\n")}
2038
+
2039
+ ### Work Tracking
2040
+ ${workCategories.map((c) => `- \`${c}/\` \u2014 ${this.getCategoryDescription(c)}`).join("\n")}
2041
+
2042
+ ### Observational Memory
2043
+ - \`ledger/raw/\` \u2014 Raw session transcripts (source of truth)
2044
+ - \`ledger/observations/\` \u2014 Compressed observations with importance scores
2045
+ - \`ledger/reflections/\` \u2014 Weekly reflection summaries
2046
+
2047
+ ## Quick Reference
2048
+
2049
+ \`\`\`bash
2050
+ # Capture a thought
2051
+ clawvault capture "important insight about X"
2052
+
2053
+ # Store structured memory
2054
+ clawvault store --category decisions --title "Choice" --content "We chose X because..."
2055
+
2056
+ # Search
2057
+ clawvault search "query"
2058
+ clawvault vsearch "semantic query" # vector search
2059
+
2060
+ # Knowledge graph
2061
+ clawvault graph # vault stats
2062
+ clawvault context "topic" # graph-aware context retrieval
2063
+
2064
+ # Session lifecycle
2065
+ clawvault checkpoint --working-on "task"
2066
+ clawvault sleep "what I did" --next "what's next"
2067
+ clawvault wake # restore context on startup
2068
+ \`\`\`
2069
+
2070
+ ---
2071
+
2072
+ *Managed by [ClawVault](https://clawvault.dev)*
2073
+ `;
2074
+ }
2075
+ getCategoryDescription(category) {
2076
+ const descriptions = {
2077
+ // Memory type categories (Benthic's taxonomy)
2078
+ facts: "Raw information, data points, things that are true",
2079
+ feelings: "Emotional states, reactions, energy levels",
2080
+ decisions: "Choices made with context and reasoning",
2081
+ rules: "Injectable operational constraints, guardrails, and runbooks",
2082
+ lessons: "What I learned, insights, patterns observed",
2083
+ commitments: "Promises, goals, obligations to fulfill",
2084
+ preferences: "Likes, dislikes, how I want things",
2085
+ people: "Relationships, one file per person",
2086
+ projects: "Active work, ventures, ongoing efforts",
2087
+ // System categories
2088
+ handoffs: "Session bridges \u2014 what I was doing, what comes next",
2089
+ transcripts: "Session summaries and logs",
2090
+ goals: "Long-term and short-term objectives",
2091
+ patterns: "Recurring behaviors (\u2192 lessons)",
2092
+ inbox: "Quick capture \u2192 process later",
2093
+ templates: "Templates for each document type",
2094
+ agents: "Other agents \u2014 capabilities, trust levels, coordination notes",
2095
+ research: "Deep dives, analysis, reference material",
2096
+ tasks: "Active work items with status and context",
2097
+ backlog: "Future work \u2014 ideas and tasks not yet started"
2098
+ };
2099
+ return descriptions[category] || category;
2100
+ }
2101
+ };
2102
+
2103
+ // src/lib/time.ts
2104
+ function formatAge(ms) {
2105
+ if (!Number.isFinite(ms)) return "unknown";
2106
+ const seconds = Math.max(0, Math.floor(ms / 1e3));
2107
+ const minutes = Math.floor(seconds / 60);
2108
+ const hours = Math.floor(minutes / 60);
2109
+ const days = Math.floor(hours / 24);
2110
+ if (days > 0) return `${days}d ${hours % 24}h`;
2111
+ if (hours > 0) return `${hours}h ${minutes % 60}m`;
2112
+ if (minutes > 0) return `${minutes}m ${seconds % 60}s`;
2113
+ return `${seconds}s`;
2114
+ }
2115
+
2116
+ // src/lib/backlinks.ts
2117
+ var fs5 = __toESM(require("fs"), 1);
2118
+ var path5 = __toESM(require("path"), 1);
2119
+
2120
+ // src/lib/entity-index.ts
2121
+ var fs4 = __toESM(require("fs"), 1);
2122
+ var path4 = __toESM(require("path"), 1);
2123
+ var import_gray_matter3 = __toESM(require("gray-matter"), 1);
2124
+ function buildEntityIndex(vaultPath) {
2125
+ const entries = /* @__PURE__ */ new Map();
2126
+ const byPath = /* @__PURE__ */ new Map();
2127
+ const entityFolders = ["people", "projects", "agents", "lessons", "decisions", "commitments"];
2128
+ for (const folder of entityFolders) {
2129
+ const folderPath = path4.join(vaultPath, folder);
2130
+ if (!fs4.existsSync(folderPath)) continue;
2131
+ const files = fs4.readdirSync(folderPath).filter((f) => f.endsWith(".md"));
2132
+ for (const file of files) {
2133
+ const filePath = path4.join(folderPath, file);
2134
+ const content = fs4.readFileSync(filePath, "utf-8");
2135
+ const { data: frontmatter } = (0, import_gray_matter3.default)(content);
2136
+ const relativePath = `${folder}/${file.replace(".md", "")}`;
2137
+ const baseName = file.replace(".md", "");
2138
+ const aliases = [baseName];
2139
+ if (frontmatter.title && frontmatter.title.toLowerCase() !== baseName.toLowerCase()) {
2140
+ aliases.push(frontmatter.title);
2141
+ }
2142
+ if (Array.isArray(frontmatter.aliases)) {
2143
+ aliases.push(...frontmatter.aliases);
2144
+ }
2145
+ for (const alias of aliases) {
2146
+ const key = alias.toLowerCase();
2147
+ if (!entries.has(key)) {
2148
+ entries.set(key, relativePath);
2149
+ }
2150
+ }
2151
+ byPath.set(relativePath, { path: relativePath, aliases });
2152
+ }
2153
+ }
2154
+ return { entries, byPath };
2155
+ }
2156
+
2157
+ // src/lib/backlinks.ts
2158
+ var WIKI_LINK_REGEX = /\[\[([^\]]+)\]\]/g;
2159
+ function toVaultId(vaultPath, filePath) {
2160
+ const relative3 = path5.relative(vaultPath, filePath).replace(/\.md$/, "");
2161
+ return relative3.split(path5.sep).join("/");
2162
+ }
2163
+ function normalizeLinkTarget(raw) {
2164
+ let target = raw.trim();
2165
+ if (!target) return "";
2166
+ if (target.startsWith("[[") && target.endsWith("]]")) {
2167
+ target = target.slice(2, -2);
2168
+ }
2169
+ const pipeIndex = target.indexOf("|");
2170
+ if (pipeIndex !== -1) {
2171
+ target = target.slice(0, pipeIndex);
2172
+ }
2173
+ if (target.startsWith("#")) return "";
2174
+ const hashIndex = target.indexOf("#");
2175
+ if (hashIndex !== -1) {
2176
+ target = target.slice(0, hashIndex);
2177
+ }
2178
+ target = target.trim();
2179
+ if (!target) return "";
2180
+ if (target.endsWith(".md")) {
2181
+ target = target.slice(0, -3);
2182
+ }
2183
+ if (target.startsWith("/")) {
2184
+ target = target.slice(1);
2185
+ }
2186
+ return target.replace(/\\/g, "/");
2187
+ }
2188
+ function listMarkdownFiles(vaultPath) {
2189
+ const files = [];
2190
+ const skipDirs = /* @__PURE__ */ new Set(["archive", "templates", "node_modules"]);
2191
+ function walk(dir) {
2192
+ const entries = fs5.readdirSync(dir, { withFileTypes: true });
2193
+ for (const entry of entries) {
2194
+ const fullPath = path5.join(dir, entry.name);
2195
+ if (entry.isDirectory()) {
2196
+ if (entry.name.startsWith(".") || skipDirs.has(entry.name)) continue;
2197
+ walk(fullPath);
2198
+ } else if (entry.isFile() && entry.name.endsWith(".md")) {
2199
+ files.push(fullPath);
2200
+ }
2201
+ }
2202
+ }
2203
+ walk(vaultPath);
2204
+ return files;
2205
+ }
2206
+ function buildKnownIds(vaultPath, files) {
2207
+ const ids = /* @__PURE__ */ new Set();
2208
+ const idsLower = /* @__PURE__ */ new Map();
2209
+ for (const file of files) {
2210
+ const id = toVaultId(vaultPath, file);
2211
+ ids.add(id);
2212
+ const lower = id.toLowerCase();
2213
+ if (!idsLower.has(lower)) {
2214
+ idsLower.set(lower, id);
2215
+ }
2216
+ }
2217
+ return { ids, idsLower };
2218
+ }
2219
+ function resolveTarget(target, known, entityIndex) {
2220
+ if (!target) return null;
2221
+ if (known.ids.has(target)) return target;
2222
+ const lower = target.toLowerCase();
2223
+ if (known.idsLower.has(lower)) return known.idsLower.get(lower);
2224
+ if (entityIndex?.entries.has(lower)) return entityIndex.entries.get(lower);
2225
+ return null;
2226
+ }
2227
+ function scanVaultLinks(vaultPath, options = {}) {
2228
+ const files = listMarkdownFiles(vaultPath);
2229
+ const known = buildKnownIds(vaultPath, files);
2230
+ const entityIndex = options.entityIndex ?? buildEntityIndex(vaultPath);
2231
+ const backlinks = /* @__PURE__ */ new Map();
2232
+ const orphans = [];
2233
+ let linkCount = 0;
2234
+ for (const file of files) {
2235
+ const sourceId = toVaultId(vaultPath, file);
2236
+ const content = fs5.readFileSync(file, "utf-8");
2237
+ const matches = content.match(WIKI_LINK_REGEX) || [];
2238
+ linkCount += matches.length;
2239
+ for (const match of matches) {
2240
+ const target = normalizeLinkTarget(match);
2241
+ if (!target) continue;
2242
+ const resolved = resolveTarget(target, known, entityIndex);
2243
+ if (!resolved) {
2244
+ orphans.push({ source: sourceId, target });
2245
+ continue;
2246
+ }
2247
+ if (!backlinks.has(resolved)) {
2248
+ backlinks.set(resolved, /* @__PURE__ */ new Set());
2249
+ }
2250
+ backlinks.get(resolved).add(sourceId);
2251
+ }
2252
+ }
2253
+ const backlinksMap = /* @__PURE__ */ new Map();
2254
+ for (const [target, sources] of backlinks) {
2255
+ backlinksMap.set(target, [...sources].sort());
2256
+ }
2257
+ return { backlinks: backlinksMap, orphans, linkCount };
2258
+ }
2259
+
2260
+ // src/observer/active-session-observer.ts
2261
+ var fs6 = __toESM(require("fs"), 1);
2262
+ var path7 = __toESM(require("path"), 1);
2263
+
2264
+ // src/lib/claude-credentials.ts
2265
+ var EXPIRY_BUFFER_MS = 5 * 60 * 1e3;
2266
+
2267
+ // src/lib/task-utils.ts
2268
+ var import_gray_matter5 = __toESM(require("gray-matter"), 1);
2269
+
2270
+ // src/lib/primitive-templates.ts
2271
+ var import_gray_matter4 = __toESM(require("gray-matter"), 1);
2272
+
2273
+ // src/lib/project-utils.ts
2274
+ var import_gray_matter6 = __toESM(require("gray-matter"), 1);
2275
+
2276
+ // src/lib/session-utils.ts
2277
+ var path6 = __toESM(require("path"), 1);
2278
+ var os = __toESM(require("os"), 1);
2279
+ function validateEnvPath(envValue) {
2280
+ if (!envValue) return null;
2281
+ const trimmed = envValue.trim();
2282
+ if (!trimmed) return null;
2283
+ const resolved = path6.resolve(trimmed);
2284
+ if (!path6.isAbsolute(resolved)) return null;
2285
+ return resolved;
2286
+ }
2287
+ function getOpenClawDir() {
2288
+ const customHome = validateEnvPath(process.env.OPENCLAW_HOME);
2289
+ if (customHome) {
2290
+ return customHome;
2291
+ }
2292
+ return path6.join(os.homedir(), ".openclaw");
2293
+ }
2294
+ function getOpenClawAgentsDir() {
2295
+ const stateDir = validateEnvPath(process.env.OPENCLAW_STATE_DIR);
2296
+ if (stateDir) {
2297
+ return path6.join(stateDir, "agents");
2298
+ }
2299
+ return path6.join(getOpenClawDir(), "agents");
2300
+ }
2301
+ function getSessionsDir(agentId) {
2302
+ return path6.join(getOpenClawAgentsDir(), agentId, "sessions");
2303
+ }
2304
+
2305
+ // src/observer/active-session-observer.ts
2306
+ var ONE_KIB = 1024;
2307
+ var ONE_MIB = ONE_KIB * ONE_KIB;
2308
+ var SMALL_SESSION_THRESHOLD_BYTES = 50 * ONE_KIB;
2309
+ var MEDIUM_SESSION_THRESHOLD_BYTES = 150 * ONE_KIB;
2310
+ var LARGE_SESSION_THRESHOLD_BYTES = 300 * ONE_KIB;
2311
+ var DEFAULT_AGENT_ID = "main";
2312
+ var AGENT_ID_RE = /^[a-zA-Z0-9_-]{1,100}$/;
2313
+ var SESSION_ID_RE = /^[a-zA-Z0-9._-]{1,200}$/;
2314
+ var CURSOR_FILE_NAME = "observe-cursors.json";
2315
+ var STALE_CURSOR_THRESHOLD_MS = 12 * 60 * 60 * 1e3;
2316
+ function isFiniteNonNegative(value) {
2317
+ return typeof value === "number" && Number.isFinite(value) && value >= 0;
2318
+ }
2319
+ function getCursorPath(vaultPath) {
2320
+ return path7.join(vaultPath, ".clawvault", CURSOR_FILE_NAME);
2321
+ }
2322
+ function getScaledObservationThresholdBytes(fileSizeBytes) {
2323
+ if (fileSizeBytes < ONE_MIB) {
2324
+ return SMALL_SESSION_THRESHOLD_BYTES;
2325
+ }
2326
+ if (fileSizeBytes <= 5 * ONE_MIB) {
2327
+ return MEDIUM_SESSION_THRESHOLD_BYTES;
2328
+ }
2329
+ return LARGE_SESSION_THRESHOLD_BYTES;
2330
+ }
2331
+ function parseCursorStore(raw) {
2332
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
2333
+ return {};
2334
+ }
2335
+ const input = raw;
2336
+ const store = {};
2337
+ for (const [sessionId, value] of Object.entries(input)) {
2338
+ if (!SESSION_ID_RE.test(sessionId)) continue;
2339
+ if (!value || typeof value !== "object" || Array.isArray(value)) continue;
2340
+ const entry = value;
2341
+ if (!isFiniteNonNegative(entry.lastObservedOffset)) continue;
2342
+ if (!isFiniteNonNegative(entry.lastFileSize)) continue;
2343
+ if (typeof entry.lastObservedAt !== "string" || !entry.lastObservedAt.trim()) continue;
2344
+ if (typeof entry.sessionKey !== "string" || !entry.sessionKey.trim()) continue;
2345
+ store[sessionId] = {
2346
+ lastObservedOffset: entry.lastObservedOffset,
2347
+ lastObservedAt: entry.lastObservedAt,
2348
+ sessionKey: entry.sessionKey,
2349
+ lastFileSize: entry.lastFileSize
2350
+ };
2351
+ }
2352
+ return store;
2353
+ }
2354
+ function loadObserveCursorStore(vaultPath) {
2355
+ const cursorPath = getCursorPath(vaultPath);
2356
+ if (!fs6.existsSync(cursorPath)) {
2357
+ return {};
2358
+ }
2359
+ try {
2360
+ const raw = JSON.parse(fs6.readFileSync(cursorPath, "utf-8"));
2361
+ return parseCursorStore(raw);
2362
+ } catch {
2363
+ return {};
2364
+ }
2365
+ }
2366
+ function parseAgentIdFromSessionKey(sessionKey) {
2367
+ const match = /^agent:([^:]+):/.exec(sessionKey);
2368
+ if (!match?.[1]) {
2369
+ return null;
2370
+ }
2371
+ const candidate = match[1].trim();
2372
+ if (!AGENT_ID_RE.test(candidate)) {
2373
+ return null;
2374
+ }
2375
+ return candidate;
2376
+ }
2377
+ function resolveSessionFileForCursor(sessionId, cursor, sessionsDirOverride) {
2378
+ const candidates = /* @__PURE__ */ new Set();
2379
+ if (sessionsDirOverride?.trim()) {
2380
+ candidates.add(path7.join(path7.resolve(sessionsDirOverride.trim()), `${sessionId}.jsonl`));
2381
+ }
2382
+ const agentId = parseAgentIdFromSessionKey(cursor.sessionKey) ?? DEFAULT_AGENT_ID;
2383
+ candidates.add(path7.join(getSessionsDir(agentId), `${sessionId}.jsonl`));
2384
+ for (const filePath of candidates) {
2385
+ try {
2386
+ const stat = fs6.statSync(filePath);
2387
+ if (stat.isFile()) {
2388
+ return filePath;
2389
+ }
2390
+ } catch {
2391
+ continue;
2392
+ }
2393
+ }
2394
+ return null;
2395
+ }
2396
+ function getObserverStaleness(vaultPath, options = {}) {
2397
+ const nowDate = options.now ? options.now() : /* @__PURE__ */ new Date();
2398
+ const nowMs = nowDate.getTime();
2399
+ if (!Number.isFinite(nowMs)) {
2400
+ return {
2401
+ staleCount: 0,
2402
+ oldestMs: 0,
2403
+ newestMs: 0
2404
+ };
2405
+ }
2406
+ const cursorStore = loadObserveCursorStore(path7.resolve(vaultPath));
2407
+ let staleCount = 0;
2408
+ let oldestMs = 0;
2409
+ let newestMs = Number.POSITIVE_INFINITY;
2410
+ for (const [sessionId, cursor] of Object.entries(cursorStore)) {
2411
+ const observedAtMs = Date.parse(cursor.lastObservedAt);
2412
+ if (!Number.isFinite(observedAtMs)) {
2413
+ continue;
2414
+ }
2415
+ const ageMs = Math.max(0, nowMs - observedAtMs);
2416
+ if (ageMs <= STALE_CURSOR_THRESHOLD_MS) {
2417
+ continue;
2418
+ }
2419
+ const sessionFilePath = resolveSessionFileForCursor(sessionId, cursor, options.sessionsDir);
2420
+ if (!sessionFilePath) {
2421
+ continue;
2422
+ }
2423
+ let sessionStat;
2424
+ try {
2425
+ sessionStat = fs6.statSync(sessionFilePath);
2426
+ } catch {
2427
+ continue;
2428
+ }
2429
+ if (!sessionStat.isFile()) {
2430
+ continue;
2431
+ }
2432
+ const newBytes = sessionStat.size - cursor.lastFileSize;
2433
+ if (newBytes <= 0) {
2434
+ continue;
2435
+ }
2436
+ const processThreshold = getScaledObservationThresholdBytes(sessionStat.size);
2437
+ if (newBytes < processThreshold) {
2438
+ continue;
2439
+ }
2440
+ staleCount += 1;
2441
+ oldestMs = Math.max(oldestMs, ageMs);
2442
+ newestMs = Math.min(newestMs, ageMs);
2443
+ }
2444
+ return {
2445
+ staleCount,
2446
+ oldestMs: staleCount > 0 ? oldestMs : 0,
2447
+ newestMs: staleCount > 0 ? newestMs : 0
2448
+ };
2449
+ }
2450
+
2451
+ // src/commands/status.ts
2452
+ var CLAWVAULT_DIR = ".clawvault";
2453
+ var CHECKPOINT_FILE = "last-checkpoint.json";
2454
+ var DIRTY_DEATH_FLAG = "dirty-death.flag";
2455
+ function findGitRoot(startPath) {
2456
+ let current = path8.resolve(startPath);
2457
+ while (true) {
2458
+ if (fs7.existsSync(path8.join(current, ".git"))) {
2459
+ return current;
2460
+ }
2461
+ const parent = path8.dirname(current);
2462
+ if (parent === current) return null;
2463
+ current = parent;
2464
+ }
2465
+ }
2466
+ function getGitStatus(repoRoot) {
2467
+ const output = (0, import_child_process2.execFileSync)("git", ["-C", repoRoot, "status", "--porcelain"], {
2468
+ encoding: "utf-8"
2469
+ });
2470
+ const lines = output.split("\n").filter(Boolean);
2471
+ return { clean: lines.length === 0, dirtyCount: lines.length };
2472
+ }
2473
+ function getLatestVaultMarkdownMtime(vaultPath) {
2474
+ const skipDirs = /* @__PURE__ */ new Set([".git", ".obsidian", ".trash", "node_modules", ".clawvault"]);
2475
+ let latest = null;
2476
+ function walk(currentPath) {
2477
+ const entries = fs7.readdirSync(currentPath, { withFileTypes: true });
2478
+ for (const entry of entries) {
2479
+ const absolute = path8.join(currentPath, entry.name);
2480
+ if (entry.isDirectory()) {
2481
+ if (!skipDirs.has(entry.name)) {
2482
+ walk(absolute);
2483
+ }
2484
+ continue;
2485
+ }
2486
+ if (!entry.isFile() || !entry.name.endsWith(".md")) {
2487
+ continue;
2488
+ }
2489
+ const mtime = fs7.statSync(absolute).mtime;
2490
+ if (!latest || mtime.getTime() > latest.getTime()) {
2491
+ latest = mtime;
2492
+ }
2493
+ }
2494
+ }
2495
+ walk(vaultPath);
2496
+ return latest;
2497
+ }
2498
+ function parseQmdCollectionsText(raw) {
2499
+ const names = [];
2500
+ const regex = /^(\S+)\s+\(qmd:\/\/\1\/\)/gm;
2501
+ let match;
2502
+ while ((match = regex.exec(raw)) !== null) {
2503
+ names.push(match[1]);
2504
+ }
2505
+ return names;
2506
+ }
2507
+ function getQmdIndexStatus(collection, root, indexName) {
2508
+ const output = (0, import_child_process2.execFileSync)("qmd", withQmdIndexArgs(["collection", "list"], indexName), { encoding: "utf-8" });
2509
+ const names = parseQmdCollectionsText(output);
2510
+ if (names.includes(collection)) {
2511
+ return "present";
2512
+ }
2513
+ return "missing";
2514
+ }
2515
+ function loadCheckpoint(vaultPath) {
2516
+ const checkpointPath = path8.join(vaultPath, CLAWVAULT_DIR, CHECKPOINT_FILE);
2517
+ if (!fs7.existsSync(checkpointPath)) {
2518
+ return { data: null };
2519
+ }
2520
+ try {
2521
+ const data = JSON.parse(fs7.readFileSync(checkpointPath, "utf-8"));
2522
+ return { data };
2523
+ } catch (err) {
2524
+ return { data: null, error: err?.message || "Failed to parse checkpoint" };
2525
+ }
2526
+ }
2527
+ async function getStatus(vaultPath, options = {}) {
2528
+ if (!hasQmd()) {
2529
+ throw new QmdUnavailableError();
2530
+ }
2531
+ const vault = new ClawVault(path8.resolve(vaultPath));
2532
+ await vault.load();
2533
+ const stats = await vault.stats();
2534
+ const linkScan = scanVaultLinks(vault.getPath());
2535
+ const issues = [];
2536
+ const checkpointInfo = loadCheckpoint(vault.getPath());
2537
+ const checkpoint = checkpointInfo.data;
2538
+ if (checkpointInfo.error) {
2539
+ issues.push(`Checkpoint parse error: ${checkpointInfo.error}`);
2540
+ }
2541
+ const checkpointStatus = {
2542
+ exists: Boolean(checkpoint),
2543
+ timestamp: checkpoint?.timestamp,
2544
+ age: checkpoint?.timestamp ? formatAge(Date.now() - new Date(checkpoint.timestamp).getTime()) : void 0,
2545
+ sessionKey: checkpoint?.sessionKey,
2546
+ model: checkpoint?.model,
2547
+ tokenEstimate: checkpoint?.tokenEstimate
2548
+ };
2549
+ if (!checkpointStatus.exists) {
2550
+ issues.push("No checkpoint found");
2551
+ }
2552
+ const dirtyFlagPath = path8.join(vault.getPath(), CLAWVAULT_DIR, DIRTY_DEATH_FLAG);
2553
+ if (fs7.existsSync(dirtyFlagPath)) {
2554
+ issues.push("Dirty death flag is set");
2555
+ }
2556
+ const qmdCollection = vault.getQmdCollection();
2557
+ const qmdRoot = vault.getQmdRoot();
2558
+ let qmdIndexStatus = "missing";
2559
+ let qmdError;
2560
+ try {
2561
+ qmdIndexStatus = getQmdIndexStatus(qmdCollection, qmdRoot, options.qmdIndexName);
2562
+ if (qmdIndexStatus !== "present") {
2563
+ issues.push(`qmd collection ${qmdIndexStatus.replace("-", " ")}`);
2564
+ }
2565
+ } catch (err) {
2566
+ qmdError = err?.message || "Failed to check qmd index";
2567
+ issues.push(`qmd status error: ${qmdError}`);
2568
+ }
2569
+ let gitStatus;
2570
+ const gitRoot = findGitRoot(vault.getPath());
2571
+ if (gitRoot) {
2572
+ try {
2573
+ const gitInfo = getGitStatus(gitRoot);
2574
+ gitStatus = { repoRoot: gitRoot, ...gitInfo };
2575
+ if (!gitInfo.clean) {
2576
+ issues.push(`Uncommitted changes: ${gitInfo.dirtyCount}`);
2577
+ }
2578
+ } catch (err) {
2579
+ issues.push(`Git status error: ${err?.message || "unknown error"}`);
2580
+ }
2581
+ }
2582
+ const graphIndex = loadMemoryGraphIndex(vault.getPath());
2583
+ let graphStatus = {
2584
+ indexStatus: "missing"
2585
+ };
2586
+ if (!graphIndex) {
2587
+ issues.push("Memory graph index missing");
2588
+ } else {
2589
+ const generatedAt = graphIndex.generatedAt;
2590
+ const latestDocMtime = getLatestVaultMarkdownMtime(vault.getPath());
2591
+ const isStale = latestDocMtime ? latestDocMtime.getTime() > new Date(generatedAt).getTime() + 1e3 : false;
2592
+ graphStatus = {
2593
+ indexStatus: isStale ? "stale" : "present",
2594
+ generatedAt,
2595
+ nodeCount: graphIndex.graph.stats.nodeCount,
2596
+ edgeCount: graphIndex.graph.stats.edgeCount
2597
+ };
2598
+ if (isStale) {
2599
+ issues.push("Memory graph index stale");
2600
+ }
2601
+ }
2602
+ const observerStaleness = getObserverStaleness(vault.getPath());
2603
+ if (observerStaleness.staleCount > 0) {
2604
+ issues.push(`Observer stale sessions: ${observerStaleness.staleCount}`);
2605
+ }
2606
+ return {
2607
+ vaultName: vault.getName(),
2608
+ vaultPath: vault.getPath(),
2609
+ health: issues.length === 0 ? "ok" : "warning",
2610
+ issues,
2611
+ checkpoint: checkpointStatus,
2612
+ qmd: {
2613
+ collection: qmdCollection,
2614
+ root: qmdRoot,
2615
+ indexStatus: qmdIndexStatus,
2616
+ error: qmdError
2617
+ },
2618
+ graph: graphStatus,
2619
+ observer: observerStaleness,
2620
+ git: gitStatus,
2621
+ links: {
2622
+ total: linkScan.linkCount,
2623
+ orphans: linkScan.orphans.length
2624
+ },
2625
+ documents: stats.documents,
2626
+ categories: stats.categories
2627
+ };
2628
+ }
2629
+ function formatStatus(status) {
2630
+ let output = "ClawVault Status\n";
2631
+ output += "-".repeat(40) + "\n";
2632
+ output += `Vault: ${status.vaultName}
2633
+ `;
2634
+ output += `Path: ${status.vaultPath}
2635
+ `;
2636
+ output += `Health: ${status.health}
2637
+ `;
2638
+ if (status.issues.length > 0) {
2639
+ output += `Issues: ${status.issues.join("; ")}
2640
+ `;
2641
+ } else {
2642
+ output += "Issues: none\n";
2643
+ }
2644
+ output += "\nCheckpoint:\n";
2645
+ if (!status.checkpoint.exists) {
2646
+ output += " - none\n";
2647
+ } else {
2648
+ output += ` - Timestamp: ${status.checkpoint.timestamp}
2649
+ `;
2650
+ if (status.checkpoint.age) {
2651
+ output += ` - Age: ${status.checkpoint.age}
2652
+ `;
2653
+ }
2654
+ if (status.checkpoint.sessionKey) {
2655
+ output += ` - Session key: ${status.checkpoint.sessionKey}
2656
+ `;
2657
+ }
2658
+ if (status.checkpoint.model) {
2659
+ output += ` - Model: ${status.checkpoint.model}
2660
+ `;
2661
+ }
2662
+ if (status.checkpoint.tokenEstimate !== void 0) {
2663
+ output += ` - Token estimate: ${status.checkpoint.tokenEstimate}
2664
+ `;
2665
+ }
2666
+ }
2667
+ output += "\nqmd:\n";
2668
+ output += ` - Collection: ${status.qmd.collection}
2669
+ `;
2670
+ output += ` - Root: ${status.qmd.root}
2671
+ `;
2672
+ output += ` - Index: ${status.qmd.indexStatus}
2673
+ `;
2674
+ if (status.qmd.error) {
2675
+ output += ` - Error: ${status.qmd.error}
2676
+ `;
2677
+ }
2678
+ if (status.git) {
2679
+ output += "\nGit:\n";
2680
+ output += ` - Repo: ${status.git.repoRoot}
2681
+ `;
2682
+ output += ` - Status: ${status.git.clean ? "clean" : "dirty"} (${status.git.dirtyCount} change(s))
2683
+ `;
2684
+ }
2685
+ output += "\nGraph:\n";
2686
+ output += ` - Index: ${status.graph.indexStatus}
2687
+ `;
2688
+ if (status.graph.generatedAt) {
2689
+ output += ` - Generated: ${status.graph.generatedAt}
2690
+ `;
2691
+ }
2692
+ if (status.graph.nodeCount !== void 0 && status.graph.edgeCount !== void 0) {
2693
+ output += ` - Size: ${status.graph.nodeCount} nodes, ${status.graph.edgeCount} edges
2694
+ `;
2695
+ }
2696
+ output += "\nObserver:\n";
2697
+ output += ` - Stale sessions: ${status.observer.staleCount}
2698
+ `;
2699
+ if (status.observer.staleCount > 0) {
2700
+ output += ` - Oldest stale age: ${formatAge(status.observer.oldestMs)}
2701
+ `;
2702
+ output += ` - Newest stale age: ${formatAge(status.observer.newestMs)}
2703
+ `;
2704
+ }
2705
+ output += "\nLinks:\n";
2706
+ output += ` - Total: ${status.links.total}
2707
+ `;
2708
+ if (status.links.orphans > 0) {
2709
+ output += ` - Orphans: ${status.links.orphans}
2710
+ `;
2711
+ }
2712
+ output += "\nDocuments:\n";
2713
+ output += ` - Total: ${status.documents}
2714
+ `;
2715
+ output += " - By category:\n";
2716
+ for (const [category, count] of Object.entries(status.categories)) {
2717
+ output += ` * ${category}: ${count}
2718
+ `;
2719
+ }
2720
+ output += "-".repeat(40) + "\n";
2721
+ return output;
2722
+ }
2723
+ async function statusCommand(vaultPath, options = {}) {
2724
+ const status = await getStatus(vaultPath, { qmdIndexName: options.qmdIndexName });
2725
+ if (options.json) {
2726
+ console.log(JSON.stringify(status, null, 2));
2727
+ return;
2728
+ }
2729
+ console.log(formatStatus(status));
2730
+ }
2731
+ // Annotate the CommonJS export names for ESM import in node:
2732
+ 0 && (module.exports = {
2733
+ formatStatus,
2734
+ getStatus,
2735
+ statusCommand
2736
+ });