pi-vault-mind 0.14.2 → 0.16.3

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 (164) hide show
  1. package/CHANGELOG.md +293 -0
  2. package/README.md +5 -5
  3. package/agents/broadcaster.agent.md +2 -2
  4. package/agents/heavy-lifter.agent.md +1 -1
  5. package/agents/main.agent.md +29 -0
  6. package/agents/manager.agent.md +1 -1
  7. package/agents/miner.agent.md +1 -1
  8. package/dist/packages/obsidian/src/client.js +331 -0
  9. package/dist/packages/obsidian/src/types.js +1 -0
  10. package/dist/src/activity.d.ts +49 -0
  11. package/dist/src/activity.js +175 -0
  12. package/dist/src/agent-bus/Agent.d.ts +9 -1
  13. package/dist/src/agent-bus/Agent.js +18 -2
  14. package/dist/src/agent-bus/index.d.ts +5 -5
  15. package/dist/src/agent-bus/index.js +1 -1
  16. package/dist/src/agent-queue.js +12 -3
  17. package/dist/src/agents/BroadcasterAgent.d.ts +1 -1
  18. package/dist/src/agents/BroadcasterAgent.js +2 -2
  19. package/dist/src/agents/HeavyLifterAgent.d.ts +1 -1
  20. package/dist/src/agents/HeavyLifterAgent.js +2 -2
  21. package/dist/src/agents/ManagerAgent.js +1 -1
  22. package/dist/src/agents/MinerAgent.d.ts +1 -1
  23. package/dist/src/agents/MinerAgent.js +2 -2
  24. package/dist/src/agents/index.d.ts +2 -2
  25. package/dist/src/agents/index.js +2 -2
  26. package/dist/src/auth.d.ts +17 -15
  27. package/dist/src/auth.js +26 -37
  28. package/dist/src/autosync.js +2 -2
  29. package/dist/src/commands.d.ts +2 -19
  30. package/dist/src/commands.js +301 -231
  31. package/dist/src/config-keys.d.ts +16 -0
  32. package/dist/src/config-keys.js +45 -0
  33. package/dist/src/discover-schema.js +4 -4
  34. package/dist/src/edits.d.ts +21 -0
  35. package/dist/src/edits.js +75 -0
  36. package/dist/src/embedding-probe.d.ts +34 -0
  37. package/dist/src/embedding-probe.js +142 -0
  38. package/dist/src/embedding-providers.d.ts +133 -0
  39. package/dist/src/embedding-providers.js +173 -0
  40. package/dist/src/embedding-secrets.d.ts +38 -0
  41. package/dist/src/embedding-secrets.js +291 -0
  42. package/dist/src/engine.d.ts +6 -0
  43. package/dist/src/engine.js +78 -6
  44. package/dist/src/events.js +7 -10
  45. package/dist/src/extension-packages.d.ts +9 -0
  46. package/dist/src/extension-packages.js +14 -0
  47. package/dist/src/git.d.ts +19 -0
  48. package/dist/src/git.js +78 -0
  49. package/dist/src/graph.d.ts +2 -1
  50. package/dist/src/graph.js +47 -13
  51. package/dist/src/identities-config.d.ts +6 -0
  52. package/dist/src/identities-config.js +39 -0
  53. package/dist/src/identity-injector.js +1 -1
  54. package/dist/src/index.js +9 -26
  55. package/dist/src/lance.d.ts +9 -0
  56. package/dist/src/lance.js +143 -203
  57. package/dist/src/modal-client.d.ts +16 -0
  58. package/dist/src/modal-client.js +16 -0
  59. package/dist/src/modal-config.d.ts +37 -14
  60. package/dist/src/modal-config.js +59 -32
  61. package/dist/src/model-router.d.ts +27 -0
  62. package/dist/src/model-router.js +67 -0
  63. package/dist/src/models.d.ts +42 -0
  64. package/dist/src/models.js +78 -0
  65. package/dist/src/personalize.js +17 -3
  66. package/dist/src/scaffold.d.ts +48 -0
  67. package/dist/src/scaffold.js +246 -0
  68. package/dist/src/server.d.ts +47 -4
  69. package/dist/src/server.js +988 -221
  70. package/dist/src/session-search.d.ts +63 -0
  71. package/dist/src/session-search.js +379 -0
  72. package/dist/src/settings-ui.d.ts +2 -2
  73. package/dist/src/settings-ui.js +76 -135
  74. package/dist/src/sync.d.ts +3 -3
  75. package/dist/src/sync.js +19 -15
  76. package/dist/src/tool-catalog.d.ts +18 -0
  77. package/dist/src/tool-catalog.js +232 -0
  78. package/dist/src/tools/marksman.js +1 -1
  79. package/dist/src/tools.js +111 -65
  80. package/dist/src/types.d.ts +127 -71
  81. package/dist/src/utils.d.ts +57 -6
  82. package/dist/src/utils.js +114 -28
  83. package/dist/src/vault-tools.d.ts +34 -0
  84. package/dist/src/vault-tools.js +99 -0
  85. package/dist/src/vault-writer.js +4 -1
  86. package/dist/src/vm-handlers.js +140 -43
  87. package/dist/src/watcher.js +9 -9
  88. package/dist/test/activity-tool.test.js +54 -0
  89. package/dist/test/agent-bus/Agent.test.js +174 -0
  90. package/dist/test/agent-bus/AgentLoader.test.js +180 -0
  91. package/dist/test/agent-bus/AgentRegistry.test.js +86 -0
  92. package/dist/test/agent-bus/LLMProvider.test.js +162 -0
  93. package/dist/test/agent-bus/TaskQueue.test.js +256 -0
  94. package/dist/test/agent-dispatch-context.test.js +73 -0
  95. package/dist/test/agent-queue.test.js +1 -1
  96. package/dist/test/agents/Agents.test.js +282 -0
  97. package/dist/test/auth.test.js +57 -56
  98. package/dist/test/bridge-identity.test.js +34 -3
  99. package/dist/test/classify-search-mode.test.js +51 -0
  100. package/dist/test/commands.test.js +410 -0
  101. package/dist/test/config-merge.test.js +98 -0
  102. package/dist/test/context-capture.test.js +56 -0
  103. package/dist/test/discover-schema.test.js +8 -5
  104. package/dist/test/dispatch.test.js +45 -91
  105. package/dist/test/edits.test.js +144 -0
  106. package/dist/test/embedding-probe.test.js +424 -0
  107. package/dist/test/embedding-providers.test.js +157 -0
  108. package/dist/test/embedding-secrets.test.js +210 -0
  109. package/dist/test/engine-identity.test.js +1 -1
  110. package/dist/test/events.test.js +675 -0
  111. package/dist/test/git-bridge.test.js +160 -0
  112. package/dist/test/graph.test.js +464 -0
  113. package/dist/test/helpers/server.js +81 -0
  114. package/dist/test/identity-injector.test.js +197 -0
  115. package/dist/test/index.test.js +5 -7
  116. package/dist/test/lance-modal.test.js +67 -2
  117. package/dist/test/lance.test.js +429 -0
  118. package/dist/test/modal-client.test.js +1 -1
  119. package/dist/test/modal-config.test.js +92 -79
  120. package/dist/test/model-readiness.test.js +122 -0
  121. package/dist/test/model-router.test.js +99 -0
  122. package/dist/test/obsidian-client.test.js +42 -0
  123. package/dist/test/personalize.test.js +1 -1
  124. package/dist/test/rest-models.test.js +153 -0
  125. package/dist/test/rest-queue.test.js +135 -87
  126. package/dist/test/rest-setup.test.js +625 -115
  127. package/dist/test/rest-vm.test.js +355 -94
  128. package/dist/test/server-cors.test.js +48 -0
  129. package/dist/test/server-ws.test.js +61 -34
  130. package/dist/test/session-crud.test.js +323 -0
  131. package/dist/test/session-search.test.js +139 -0
  132. package/dist/test/settings-ui.test.js +113 -4
  133. package/dist/test/sync.test.js +12 -12
  134. package/dist/test/tombstone.test.js +8 -10
  135. package/dist/test/tool-catalog.test.js +86 -0
  136. package/dist/test/utils.test.js +588 -0
  137. package/dist/test/vault-tools.test.js +103 -12
  138. package/dist/test/vault-writer.test.js +371 -0
  139. package/dist/test/vm-handlers.test.js +620 -0
  140. package/extension-packages.json +9 -0
  141. package/package.json +107 -90
  142. package/pi-vault-mind.config.example.json +24 -2
  143. package/scripts/check-deps.sh +35 -32
  144. package/scripts/cli-e2e.mjs +215 -0
  145. package/scripts/configuration-e2e.mjs +783 -0
  146. package/scripts/e2e-commands.mjs +391 -0
  147. package/scripts/e2e-ws.mjs +3 -2
  148. package/scripts/fetch-modal-token.sh +4 -3
  149. package/scripts/generate-config-keys-json.mjs +45 -0
  150. package/scripts/generate-extension-packages-json.mjs +55 -0
  151. package/scripts/pi-session-query +0 -0
  152. package/scripts/publish-obsidian-plugin.sh +0 -0
  153. package/scripts/reset-test-vault.sh +2 -2
  154. package/scripts/setup-test-config.sh +0 -0
  155. package/scripts/setup-vault-pi.sh +48 -9
  156. package/scripts/test-slash-commands.mjs +2 -2
  157. package/scripts/vault-pi.sh +12 -3
  158. package/skills/github-release-notes-file/SKILL.md +42 -0
  159. package/skills/obsidian-guided-test-session/SKILL.md +110 -0
  160. package/skills/obsidian-interactive-test/SKILL.md +88 -0
  161. package/skills/obsidian-plugin-publish/SKILL.md +58 -0
  162. package/skills/pi-vault-mind-release/SKILL.md +141 -0
  163. package/skills/pre-test-release/SKILL.md +116 -0
  164. package/dist/test/auto-indexer.test.js +0 -2
@@ -9,30 +9,44 @@
9
9
  * GET /vault-mind/context → latest editor context (file, cursor, selection) for pi agents
10
10
  *
11
11
  * Binds 127.0.0.1 only — no network exposure. Port defaults to 11435,
12
- * configurable via vaultMind.httpPort in pi-vault-mind.config.json.
12
+ * configurable via vaultMind.httpPort in .vault-mind/vault-mind.config.json.
13
13
  *
14
14
  * Used by the Vault Mind Obsidian plugin and local automation scripts to notify
15
15
  * pi of file saves via explicit push notifications.
16
16
  *
17
17
  * New Stage-1 routes (auth-gated by `PVM_API_TOKEN`):
18
- * GET /vm/status, POST /vm/init, POST /vm/setup, POST /server/start,
18
+ * GET /vm/status, POST /vm/setup, POST /server/start,
19
19
  * GET /vault-mind/config, GET /agent/queue, GET /agent/jobs/:id,
20
20
  * POST /agent/jobs/:id/retry, POST /agent/jobs/:id/cancel,
21
21
  * POST /vm/search, POST /vm/append, WS /agent/stream,
22
22
  * GET /vm/pending, POST /vm/approve.
23
+ * GET /vault-mind/models.
23
24
  */
24
25
  import * as fs from "node:fs";
25
26
  import * as http from "node:http";
26
- import * as os from "node:os";
27
27
  import * as path from "node:path";
28
28
  import { WebSocket, WebSocketServer } from "ws";
29
+ import { appendActivity, listActivity, mapQueueEventToActivity, mapToolCallToActivity, } from "./activity.js";
29
30
  import { cancelJobHelper, getJob, listJobs, retryJob, subscribeQueue, } from "./agent-queue.js";
30
31
  import { authoriseRequest, resolveAuthToken } from "./auth.js";
31
- import { GITIGNORE_ENTRIES } from "./commands.js";
32
+ import { writeTokenEnv } from "./commands.js";
33
+ import { EMBEDDING_FLAT_KEYS } from "./config-keys.js";
34
+ import { applyEdit, createEdit, getEdit, listEdits, rejectEdit, } from "./edits.js";
35
+ import { probeEmbeddingEndpoint, } from "./embedding-probe.js";
36
+ import { readEmbeddingSecretStatus, redactEmbeddingSecrets, writeEmbeddingSecrets, } from "./embedding-secrets.js";
37
+ import { buildIdentityFromRole, mergeIdentityProfiles, refreshAgentIdentities } from "./engine.js";
38
+ import { getGitBranches, getGitStatus, gitCheckout } from "./git.js";
32
39
  import { queryGraph } from "./graph.js";
33
- import { searchFts, searchHybrid, upsertEntry } from "./lance.js";
34
- import { DEFAULT_CONFIG } from "./types.js";
35
- import { ensureDir, expandHome, findConfig, loadConfig, resolveInitCollectionName, shrinkHome, } from "./utils.js";
40
+ import { writeIdentitiesLayer } from "./identities-config.js";
41
+ import { searchFts, searchHybrid, searchHybridRanked, upsertEntry } from "./lance.js";
42
+ import { createModalClient, isModalConfigured, modalTokenEnvPath, modalUrl, resolveCollectionPrefix, resolveDim, resolveModel, serverCollectionName, } from "./modal-config.js";
43
+ import { scaffoldModelRouterConfig } from "./model-router.js";
44
+ import { readModelProviders } from "./models.js";
45
+ import { scaffoldVaultConfig } from "./scaffold.js";
46
+ import { archiveSession, deleteSession, exportSession, getSession, listSessions, renameSession, resolveSessionsDir, searchSessions, } from "./session-search.js";
47
+ import { listAllTools } from "./tool-catalog.js";
48
+ import { ensureDir, expandHome, findConfig, getConfigPath, loadConfig, resolveVaultFile, resolveVaultMindPaths, shrinkHome, } from "./utils.js";
49
+ import { classifySearchMode, normalizeGraphResult, searchVaultNative } from "./vault-tools.js";
36
50
  import { createManualDispatch, processQueue, scanFile, startWatcher, stopWatcher, } from "./watcher.js";
37
51
  export function createServerState(port = 11435) {
38
52
  return { server: null, wss: null, port, startTime: 0 };
@@ -56,48 +70,198 @@ export const emitJobNotification = (wss, jobId, status, message) => {
56
70
  export const emitContextRequest = (wss, requestId) => {
57
71
  broadcastEvent(wss, { type: "context-request", requestId });
58
72
  };
59
- export function startServer(pi, serverState, watcherState) {
73
+ /** Apply the browser policy for the local bridge without reflecting arbitrary origins. */
74
+ export function applyCorsPolicy(origin, serverPort, res) {
75
+ const allowedOrigins = new Set(["app://obsidian.md", `http://localhost:${serverPort}`]);
76
+ if (origin && allowedOrigins.has(origin)) {
77
+ res.setHeader("Access-Control-Allow-Origin", origin);
78
+ }
79
+ else if (!origin) {
80
+ res.setHeader("Access-Control-Allow-Origin", `http://localhost:${serverPort}`);
81
+ }
82
+ res.setHeader("Vary", "Origin");
83
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS");
84
+ res.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Active-Session");
85
+ }
86
+ export function startServer(pi, serverState, watcherState, readinessCallback) {
60
87
  if (serverState.server) {
88
+ if (readinessCallback) {
89
+ serverState.getModelReadiness = readinessCallback;
90
+ }
61
91
  console.warn("[pi-vault-mind] HTTP server already running.");
62
92
  return;
63
93
  }
64
94
  serverState.startTime = Date.now();
65
95
  serverState.pi = pi;
66
96
  serverState.watcherState = watcherState;
67
- // Resolve the vault path from the global config's default vault, falling back to cwd.
68
- const home = os.homedir();
69
- const globalCfgPath = home ? path.join(home, ".pi", "agent", "vault-mind.config.json") : null;
70
- let vaultPath = process.cwd();
71
- if (globalCfgPath && fs.existsSync(globalCfgPath)) {
72
- try {
73
- const globalCfg = JSON.parse(fs.readFileSync(globalCfgPath, "utf-8"));
74
- const defaultVault = globalCfg?.vaultMind?.vaults?.default?.path;
75
- if (defaultVault) {
76
- const resolved = defaultVault.startsWith("~")
77
- ? path.join(home, defaultVault.slice(1))
78
- : defaultVault;
79
- if (fs.existsSync(resolved))
80
- vaultPath = resolved;
81
- }
82
- }
83
- catch {
84
- // config parse error — keep cwd
85
- }
86
- }
97
+ serverState.getModelReadiness = readinessCallback;
98
+ // Resolve the vault path from the loaded config's default vault, else cwd.
99
+ const startupCfg = loadConfig(process.cwd());
100
+ const defaultVault = startupCfg.vaultMind.vaults?.default?.path;
101
+ const vaultPath = defaultVault && fs.existsSync(defaultVault) ? defaultVault : process.cwd();
87
102
  serverState.vaultPath = vaultPath;
88
103
  serverState.server = http.createServer(async (req, res) => {
89
- // CORS for localhost-only access
90
- res.setHeader("Access-Control-Allow-Origin", `http://localhost:${serverState.port}`);
91
- res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
92
- res.setHeader("Access-Control-Allow-Headers", "Content-Type");
104
+ applyCorsPolicy(req.headers.origin, serverState.port, res);
93
105
  res.setHeader("Content-Type", "application/json");
94
106
  if (req.method === "OPTIONS") {
95
107
  res.writeHead(204);
96
108
  res.end();
97
109
  return;
98
110
  }
111
+ // Active-session guard input. The plugin signals the active session
112
+ // via the X-Active-Session header on each request; the persistent
113
+ // serverState.activeSessionId remains a fallback for callers that
114
+ // set it directly (tests, future plugin port). We do NOT persist
115
+ // the header value into serverState here — that would race with
116
+ // concurrent requests. The DELETE/PATCH/archive guards read both
117
+ // the header and serverState to compute the active session id.
118
+ const activeSessionHeader = typeof req.headers["x-active-session"] === "string"
119
+ ? req.headers["x-active-session"]
120
+ : undefined;
99
121
  const url = new URL(req.url || "/", `http://127.0.0.1:${serverState.port}`);
100
122
  try {
123
+ // ── Session CRUD routes (BC-7) ────────────────────────────────
124
+ // Handled as plain if-blocks before the switch because they use path
125
+ // parameters (`:id`, `/export`, `/archive`) and the switch only matches
126
+ // exact strings. PATCH/DELETE/archive require requireWrite: true and
127
+ // reject the active session id (409) to prevent corrupting the live
128
+ // pi RPC connection.
129
+ {
130
+ const sessionNotFoundOrInvalid = (e) => {
131
+ const msg = e.message ?? "";
132
+ return msg.startsWith("Session not found") || msg.startsWith("Invalid");
133
+ };
134
+ if (url.pathname === "/sessions") {
135
+ if (req.method !== "GET") {
136
+ res.writeHead(405);
137
+ res.end(JSON.stringify({ error: "Method not allowed" }));
138
+ return;
139
+ }
140
+ withAuth(req, res, () => {
141
+ try {
142
+ res.writeHead(200, { "Content-Type": "application/json" });
143
+ res.end(JSON.stringify(listSessions(resolveSessionsDir())));
144
+ }
145
+ catch (e) {
146
+ res.writeHead(500);
147
+ res.end(JSON.stringify({ error: e.message }));
148
+ }
149
+ });
150
+ return;
151
+ }
152
+ const exportMatch = url.pathname.match(/^\/sessions\/([^/]+)\/export$/);
153
+ if (exportMatch) {
154
+ const id = exportMatch[1];
155
+ if (req.method !== "POST") {
156
+ res.writeHead(405);
157
+ res.end(JSON.stringify({ error: "Method not allowed" }));
158
+ return;
159
+ }
160
+ withAuth(req, res, () => {
161
+ try {
162
+ const r = exportSession(resolveSessionsDir(), id, serverState.vaultPath || process.cwd());
163
+ res.writeHead(200, { "Content-Type": "application/json" });
164
+ res.end(JSON.stringify(r));
165
+ }
166
+ catch (e) {
167
+ res.writeHead(sessionNotFoundOrInvalid(e) ? 404 : 500);
168
+ res.end(JSON.stringify({ error: e.message }));
169
+ }
170
+ });
171
+ return;
172
+ }
173
+ const archiveMatch = url.pathname.match(/^\/sessions\/([^/]+)\/archive$/);
174
+ if (archiveMatch) {
175
+ const id = archiveMatch[1];
176
+ if (req.method !== "POST") {
177
+ res.writeHead(405);
178
+ res.end(JSON.stringify({ error: "Method not allowed" }));
179
+ return;
180
+ }
181
+ withAuth(req, res, () => {
182
+ if ((activeSessionHeader ?? serverState.activeSessionId) === id) {
183
+ res.writeHead(409);
184
+ res.end(JSON.stringify({ error: "Cannot archive the active session" }));
185
+ return;
186
+ }
187
+ try {
188
+ const r = archiveSession(resolveSessionsDir(), id);
189
+ res.writeHead(200, { "Content-Type": "application/json" });
190
+ res.end(JSON.stringify(r));
191
+ }
192
+ catch (e) {
193
+ res.writeHead(sessionNotFoundOrInvalid(e) ? 404 : 500);
194
+ res.end(JSON.stringify({ error: e.message }));
195
+ }
196
+ }, { requireWrite: true });
197
+ return;
198
+ }
199
+ const sessionMatch = url.pathname.match(/^\/sessions\/([^/]+)$/);
200
+ if (sessionMatch) {
201
+ const id = sessionMatch[1];
202
+ if (req.method === "GET") {
203
+ withAuth(req, res, () => {
204
+ try {
205
+ const detail = getSession(resolveSessionsDir(), id);
206
+ res.writeHead(200, { "Content-Type": "application/json" });
207
+ res.end(JSON.stringify(detail));
208
+ }
209
+ catch (e) {
210
+ res.writeHead(sessionNotFoundOrInvalid(e) ? 404 : 500);
211
+ res.end(JSON.stringify({ error: e.message }));
212
+ }
213
+ });
214
+ return;
215
+ }
216
+ if (req.method === "PATCH") {
217
+ withAuth(req, res, async () => {
218
+ if ((activeSessionHeader ?? serverState.activeSessionId) === id) {
219
+ res.writeHead(409);
220
+ res.end(JSON.stringify({ error: "Cannot rename the active session" }));
221
+ return;
222
+ }
223
+ const body = (await readJsonBody(req));
224
+ if (!body.name || typeof body.name !== "string") {
225
+ res.writeHead(400);
226
+ res.end(JSON.stringify({ error: "Missing name" }));
227
+ return;
228
+ }
229
+ try {
230
+ const r = renameSession(resolveSessionsDir(), id, body.name);
231
+ res.writeHead(200, { "Content-Type": "application/json" });
232
+ res.end(JSON.stringify(r));
233
+ }
234
+ catch (e) {
235
+ res.writeHead(sessionNotFoundOrInvalid(e) ? 404 : 400);
236
+ res.end(JSON.stringify({ error: e.message }));
237
+ }
238
+ }, { requireWrite: true });
239
+ return;
240
+ }
241
+ if (req.method === "DELETE") {
242
+ withAuth(req, res, () => {
243
+ if ((activeSessionHeader ?? serverState.activeSessionId) === id) {
244
+ res.writeHead(409);
245
+ res.end(JSON.stringify({ error: "Cannot delete the active session" }));
246
+ return;
247
+ }
248
+ try {
249
+ const r = deleteSession(resolveSessionsDir(), id);
250
+ res.writeHead(200, { "Content-Type": "application/json" });
251
+ res.end(JSON.stringify(r));
252
+ }
253
+ catch (e) {
254
+ res.writeHead(sessionNotFoundOrInvalid(e) ? 404 : 500);
255
+ res.end(JSON.stringify({ error: e.message }));
256
+ }
257
+ }, { requireWrite: true });
258
+ return;
259
+ }
260
+ res.writeHead(405);
261
+ res.end(JSON.stringify({ error: "Method not allowed" }));
262
+ return;
263
+ }
264
+ }
101
265
  switch (url.pathname) {
102
266
  case "/vault-mind/status":
103
267
  handleStatus(res, serverState, watcherState);
@@ -144,21 +308,68 @@ export function startServer(pi, serverState, watcherState) {
144
308
  }
145
309
  withAuth(req, res, () => handleWatcherToggle(res, serverState), { requireWrite: true });
146
310
  break;
147
- case "/vm/init":
311
+ case "/vm/setup":
148
312
  if (req.method !== "POST") {
149
313
  res.writeHead(405);
150
314
  res.end(JSON.stringify({ error: "Method not allowed" }));
151
315
  return;
152
316
  }
153
- withAuth(req, res, () => handleVmInit(req, res), { requireWrite: true });
317
+ withAuth(req, res, () => handleVmSetup(req, res), { requireWrite: true });
154
318
  break;
155
- case "/vm/setup":
319
+ case "/vm/token":
156
320
  if (req.method !== "POST") {
157
321
  res.writeHead(405);
158
322
  res.end(JSON.stringify({ error: "Method not allowed" }));
159
323
  return;
160
324
  }
161
- withAuth(req, res, () => handleVmSetup(req, res), { requireWrite: true });
325
+ withAuth(req, res, async () => {
326
+ const body = (await readJsonBody(req));
327
+ const token = body?.token;
328
+ if (!token || typeof token !== "string") {
329
+ res.writeHead(400, { "Content-Type": "application/json" });
330
+ res.end(JSON.stringify({ error: "Missing token in request body" }));
331
+ return;
332
+ }
333
+ writeTokenEnv(token, process.cwd());
334
+ res.writeHead(200, { "Content-Type": "application/json" });
335
+ res.end(JSON.stringify({
336
+ ok: true,
337
+ path: modalTokenEnvPath(process.cwd()),
338
+ }));
339
+ }, { requireWrite: true });
340
+ break;
341
+ case "/vm/config":
342
+ if (req.method === "PATCH") {
343
+ withAuth(req, res, async () => {
344
+ const body = (await readJsonBody(req));
345
+ const configPath = getConfigPath(process.cwd());
346
+ const existing = fs.existsSync(configPath)
347
+ ? JSON.parse(fs.readFileSync(configPath, "utf-8"))
348
+ : {};
349
+ // Shallow merge at top level
350
+ const merged = { ...existing };
351
+ for (const key of Object.keys(body)) {
352
+ if (key === "vaultMind" &&
353
+ typeof body.vaultMind === "object" &&
354
+ body.vaultMind !== null) {
355
+ // Deep merge vaultMind
356
+ const existingVm = existing.vaultMind || {};
357
+ const incomingVm = body.vaultMind;
358
+ merged.vaultMind = deepMerge(existingVm, incomingVm);
359
+ }
360
+ else {
361
+ merged[key] = body[key];
362
+ }
363
+ }
364
+ const dir = path.dirname(configPath);
365
+ if (!fs.existsSync(dir))
366
+ fs.mkdirSync(dir, { recursive: true });
367
+ fs.writeFileSync(configPath, `${JSON.stringify(merged, null, 2)}\n`, "utf-8");
368
+ res.writeHead(200, { "Content-Type": "application/json" });
369
+ res.end(JSON.stringify({ ok: true }));
370
+ }, { requireWrite: true });
371
+ return;
372
+ }
162
373
  break;
163
374
  case "/server/start":
164
375
  if (req.method !== "POST") {
@@ -171,10 +382,48 @@ export function startServer(pi, serverState, watcherState) {
171
382
  case "/vault-mind/config":
172
383
  withAuth(req, res, () => handleVaultMindConfig(res));
173
384
  break;
385
+ case "/vm/embedding/secrets":
386
+ if (req.method !== "PUT") {
387
+ res.writeHead(405, { "Content-Type": "application/json" });
388
+ res.end(JSON.stringify({ error: "Method not allowed" }));
389
+ return;
390
+ }
391
+ withAuth(req, res, () => handlePutEmbeddingSecrets(req, res), { requireWrite: true });
392
+ break;
393
+ case "/vm/embedding/probe":
394
+ if (req.method !== "POST") {
395
+ res.writeHead(405);
396
+ res.end(JSON.stringify({ error: "Method not allowed" }));
397
+ return;
398
+ }
399
+ withAuth(req, res, () => handleVmEmbeddingProbe(req, res), { requireWrite: true });
400
+ break;
401
+ case "/vault-mind/paths":
402
+ withAuth(req, res, () => handleVaultMindPaths(res, serverState));
403
+ break;
404
+ case "/vault-mind/models":
405
+ withAuth(req, res, () => handleVaultMindModels(res, serverState));
406
+ break;
407
+ case "/vault-mind/model-readiness":
408
+ if (req.method !== "GET") {
409
+ res.writeHead(405);
410
+ res.end(JSON.stringify({ error: "Method not allowed" }));
411
+ return;
412
+ }
413
+ withAuth(req, res, async () => await handleModelReadiness(res, serverState));
414
+ break;
174
415
  // ── Agent queue routes ────────────────────────────────
175
416
  case "/agent/queue":
176
417
  withAuth(req, res, () => handleAgentQueue(req, res));
177
418
  break;
419
+ case "/agent/activity":
420
+ if (req.method !== "GET") {
421
+ res.writeHead(405);
422
+ res.end(JSON.stringify({ error: "Method not allowed" }));
423
+ return;
424
+ }
425
+ withAuth(req, res, () => handleAgentActivity(req, res, serverState));
426
+ break;
178
427
  // ── Tool routes ───────────────────────────────────
179
428
  case "/vm/search":
180
429
  if (req.method !== "POST") {
@@ -184,29 +433,112 @@ export function startServer(pi, serverState, watcherState) {
184
433
  }
185
434
  withAuth(req, res, () => handleVmSearch(req, res, serverState));
186
435
  break;
187
- case "/vm/append":
436
+ case "/vm/reindex":
188
437
  if (req.method !== "POST") {
189
438
  res.writeHead(405);
190
439
  res.end(JSON.stringify({ error: "Method not allowed" }));
191
- return;
440
+ break;
192
441
  }
193
- withAuth(req, res, () => handleVmAppend(req, res, serverState), { requireWrite: true });
442
+ withAuth(req, res, async () => {
443
+ try {
444
+ const cfg = loadConfig(process.cwd());
445
+ if (!isModalConfigured(cfg.vaultMind)) {
446
+ res.writeHead(400, { "Content-Type": "application/json" });
447
+ res.end(JSON.stringify({
448
+ error: "Modal is not configured. Configure a remote embedding provider first.",
449
+ }));
450
+ return;
451
+ }
452
+ const client = createModalClient(cfg.vaultMind);
453
+ if (!client) {
454
+ res.writeHead(500, { "Content-Type": "application/json" });
455
+ res.end(JSON.stringify({ error: "Failed to create Modal client." }));
456
+ return;
457
+ }
458
+ const model = resolveModel(cfg.vaultMind);
459
+ const dim = resolveDim(cfg.vaultMind);
460
+ const prefix = resolveCollectionPrefix(cfg.vaultMind, process.cwd());
461
+ const jobs = [];
462
+ for (const [name, def] of Object.entries(cfg.collections)) {
463
+ const lines = fs.existsSync(def.path)
464
+ ? fs.readFileSync(def.path, "utf-8").split("\n").filter(Boolean)
465
+ : [];
466
+ const records = [];
467
+ for (const line of lines) {
468
+ try {
469
+ const entry = JSON.parse(line);
470
+ const text = String(entry.fact ?? entry.content ?? "");
471
+ if (!text)
472
+ continue;
473
+ const { id, fact, content, ...rest } = entry;
474
+ records.push({
475
+ id: String(id ?? crypto.randomUUID()),
476
+ text,
477
+ metadata: rest,
478
+ });
479
+ }
480
+ catch {
481
+ /* skip malformed */
482
+ }
483
+ }
484
+ if (records.length > 0) {
485
+ const remoteCollection = serverCollectionName(prefix, name);
486
+ jobs.push({ collection: remoteCollection, records });
487
+ }
488
+ }
489
+ const results = await client.reindexAll(jobs, { model, dim });
490
+ res.writeHead(200, { "Content-Type": "application/json" });
491
+ res.end(JSON.stringify({ ok: true, results }));
492
+ }
493
+ catch (err) {
494
+ res.writeHead(500, { "Content-Type": "application/json" });
495
+ res.end(JSON.stringify({ error: `Reindex failed: ${err.message}` }));
496
+ }
497
+ }, { requireWrite: true });
194
498
  break;
195
- case "/vm/fts-search":
499
+ case "/vm/append":
196
500
  if (req.method !== "POST") {
197
501
  res.writeHead(405);
198
502
  res.end(JSON.stringify({ error: "Method not allowed" }));
199
503
  return;
200
504
  }
201
- withAuth(req, res, () => handleVmFtsSearch(req, res, serverState));
505
+ withAuth(req, res, () => handleVmAppend(req, res, serverState), { requireWrite: true });
202
506
  break;
203
- case "/vm/graph":
204
- if (req.method !== "POST") {
507
+ // ── Edit routes ───────────────────────────────────
508
+ case "/vm/edits":
509
+ if (req.method === "GET") {
510
+ withAuth(req, res, () => {
511
+ const editsUrl = new URL(req.url ?? "", `http://${req.headers.host}`);
512
+ const filePath = editsUrl.searchParams.get("path") ?? undefined;
513
+ const edits = listEdits(process.cwd(), filePath);
514
+ res.writeHead(200, { "Content-Type": "application/json" });
515
+ res.end(JSON.stringify({ edits }));
516
+ });
517
+ }
518
+ else if (req.method === "POST") {
519
+ withAuth(req, res, async () => {
520
+ const body = (await readJsonBody(req));
521
+ if (!body.path || !body.oldText || !body.newText) {
522
+ res.writeHead(400, { "Content-Type": "application/json" });
523
+ res.end(JSON.stringify({ error: "Missing required fields: path, oldText, newText" }));
524
+ return;
525
+ }
526
+ const edit = createEdit(process.cwd(), {
527
+ path: body.path,
528
+ agentId: body.agentId ?? "unknown",
529
+ role: body.role ?? "unknown",
530
+ dispatchId: body.dispatchId ?? "unknown",
531
+ oldText: body.oldText,
532
+ newText: body.newText,
533
+ });
534
+ res.writeHead(201, { "Content-Type": "application/json" });
535
+ res.end(JSON.stringify({ edit }));
536
+ }, { requireWrite: true });
537
+ }
538
+ else {
205
539
  res.writeHead(405);
206
540
  res.end(JSON.stringify({ error: "Method not allowed" }));
207
- return;
208
541
  }
209
- withAuth(req, res, () => handleVmGraph(req, res, serverState));
210
542
  break;
211
543
  case "/vm/approve":
212
544
  if (req.method !== "POST") {
@@ -232,12 +564,179 @@ export function startServer(pi, serverState, watcherState) {
232
564
  }
233
565
  withAuth(req, res, () => handleVmStatsRoute(res, serverState));
234
566
  break;
567
+ // ── Identity + tool catalog routes ──────────────────
568
+ case "/vault-mind/tools":
569
+ withAuth(req, res, () => {
570
+ const tools = listAllTools();
571
+ const cfg = loadConfig(process.cwd());
572
+ const identities = cfg.vaultMind.identities;
573
+ const roles = {};
574
+ if (identities?.roles) {
575
+ for (const [role, profile] of Object.entries(identities.roles)) {
576
+ roles[role] = { allowedTools: profile.allowedTools };
577
+ }
578
+ }
579
+ res.writeHead(200, { "Content-Type": "application/json" });
580
+ res.end(JSON.stringify({
581
+ tools,
582
+ identities: { roles, defaultProfile: identities?.defaultProfile },
583
+ }));
584
+ });
585
+ break;
586
+ case "/vault-mind/identities":
587
+ withAuth(req, res, () => {
588
+ const cfg = loadConfig(process.cwd());
589
+ const identities = cfg.vaultMind.identities;
590
+ res.writeHead(200, { "Content-Type": "application/json" });
591
+ res.end(JSON.stringify({
592
+ roles: identities?.roles ?? {},
593
+ defaultProfile: identities?.defaultProfile,
594
+ }));
595
+ });
596
+ break;
597
+ case "/vault-mind/reload-identities":
598
+ if (req.method !== "POST") {
599
+ res.writeHead(405);
600
+ res.end(JSON.stringify({ error: "Method not allowed" }));
601
+ return;
602
+ }
603
+ withAuth(req, res, () => {
604
+ refreshAgentIdentities();
605
+ const cfg = loadConfig(process.cwd());
606
+ const identities = cfg.vaultMind.identities;
607
+ const roleNames = Object.keys(identities?.roles ?? {});
608
+ res.writeHead(200, { "Content-Type": "application/json" });
609
+ res.end(JSON.stringify({ ok: true, roles: roleNames }));
610
+ }, { requireWrite: true });
611
+ break;
612
+ // ── Git bridge routes ──
613
+ case "/git/status":
614
+ if (req.method !== "GET") {
615
+ res.writeHead(405);
616
+ res.end(JSON.stringify({ error: "Method not allowed" }));
617
+ return;
618
+ }
619
+ withAuth(req, res, async () => {
620
+ try {
621
+ const s = await getGitStatus(serverState.vaultPath || process.cwd());
622
+ res.writeHead(200);
623
+ res.end(JSON.stringify(s));
624
+ }
625
+ catch (e) {
626
+ res.writeHead(500);
627
+ res.end(JSON.stringify({ error: e.message }));
628
+ }
629
+ });
630
+ break;
631
+ case "/git/branches":
632
+ if (req.method !== "GET") {
633
+ res.writeHead(405);
634
+ res.end(JSON.stringify({ error: "Method not allowed" }));
635
+ return;
636
+ }
637
+ withAuth(req, res, async () => {
638
+ try {
639
+ const b = await getGitBranches(serverState.vaultPath || process.cwd());
640
+ res.writeHead(200);
641
+ res.end(JSON.stringify(b));
642
+ }
643
+ catch (e) {
644
+ res.writeHead(500);
645
+ res.end(JSON.stringify({ error: e.message }));
646
+ }
647
+ });
648
+ break;
649
+ case "/git/checkout":
650
+ if (req.method !== "POST") {
651
+ res.writeHead(405);
652
+ res.end(JSON.stringify({ error: "Method not allowed" }));
653
+ return;
654
+ }
655
+ withAuth(req, res, async () => {
656
+ const body = (await readJsonBody(req));
657
+ if (!body.branch || typeof body.branch !== "string") {
658
+ res.writeHead(400);
659
+ res.end(JSON.stringify({ error: "Missing branch" }));
660
+ return;
661
+ }
662
+ try {
663
+ const r = await gitCheckout(serverState.vaultPath || process.cwd(), body.branch);
664
+ res.writeHead(200);
665
+ res.end(JSON.stringify(r));
666
+ }
667
+ catch (e) {
668
+ res.writeHead(400);
669
+ res.end(JSON.stringify({ error: e.message }));
670
+ }
671
+ }, { requireWrite: true });
672
+ break;
235
673
  default:
236
- if (url.pathname.startsWith("/agent/jobs/")) {
674
+ if (url.pathname.startsWith("/vm/edits/") &&
675
+ url.pathname.endsWith("/apply") &&
676
+ req.method === "POST") {
677
+ const id = url.pathname.split("/")[3];
678
+ withAuth(req, res, () => {
679
+ const result = applyEdit(process.cwd(), id);
680
+ res.writeHead(result.ok ? 200 : 400, { "Content-Type": "application/json" });
681
+ res.end(JSON.stringify(result));
682
+ }, { requireWrite: true });
683
+ }
684
+ else if (url.pathname.startsWith("/vm/edits/") &&
685
+ url.pathname.endsWith("/reject") &&
686
+ req.method === "POST") {
687
+ const id = url.pathname.split("/")[3];
688
+ withAuth(req, res, () => {
689
+ const ok = rejectEdit(process.cwd(), id);
690
+ res.writeHead(ok ? 200 : 400, { "Content-Type": "application/json" });
691
+ res.end(JSON.stringify(ok ? { ok: true } : { error: "Edit not found or already resolved" }));
692
+ }, { requireWrite: true });
693
+ }
694
+ else if (url.pathname.startsWith("/vm/edits/") && req.method === "GET") {
695
+ const id = url.pathname.split("/")[3];
696
+ withAuth(req, res, () => {
697
+ const edit = getEdit(process.cwd(), id);
698
+ if (!edit) {
699
+ res.writeHead(404);
700
+ res.end(JSON.stringify({ error: "Edit not found" }));
701
+ return;
702
+ }
703
+ res.writeHead(200, { "Content-Type": "application/json" });
704
+ res.end(JSON.stringify({ edit }));
705
+ });
706
+ }
707
+ else if (url.pathname.startsWith("/agent/jobs/")) {
237
708
  withAuth(req, res, () => handleAgentJob(req, res, url.pathname), {
238
709
  requireWrite: req.method === "POST",
239
710
  });
240
711
  }
712
+ else if (req.method === "PUT" &&
713
+ url.pathname.startsWith("/vault-mind/identities/") &&
714
+ url.pathname.endsWith("/allowedTools")) {
715
+ const parts = url.pathname.split("/");
716
+ // path: /vault-mind/identities/<role>/allowedTools → 5 parts
717
+ if (parts.length === 5) {
718
+ const role = parts[3];
719
+ withAuth(req, res, async () => {
720
+ const body = (await readJsonBody(req));
721
+ if (!body.allowedTools || !Array.isArray(body.allowedTools)) {
722
+ res.writeHead(400, { "Content-Type": "application/json" });
723
+ res.end(JSON.stringify({ error: "Missing allowedTools array in request body" }));
724
+ return;
725
+ }
726
+ // writeIdentitiesLayer deep-merges; pass a partial role config
727
+ writeIdentitiesLayer(process.cwd(), {
728
+ roles: { [role]: { allowedTools: body.allowedTools } },
729
+ });
730
+ refreshAgentIdentities([role]);
731
+ res.writeHead(200, { "Content-Type": "application/json" });
732
+ res.end(JSON.stringify({ ok: true, role, allowedTools: body.allowedTools }));
733
+ }, { requireWrite: true });
734
+ }
735
+ else {
736
+ res.writeHead(404);
737
+ res.end(JSON.stringify({ error: "Not found" }));
738
+ }
739
+ }
241
740
  else {
242
741
  res.writeHead(404);
243
742
  res.end(JSON.stringify({ error: "Not found" }));
@@ -269,11 +768,69 @@ export function startServer(pi, serverState, watcherState) {
269
768
  const wss = new WebSocketServer({ noServer: true });
270
769
  serverState.wss = wss;
271
770
  serverState.unsubQueue = subscribeQueue((e) => {
771
+ const vp = serverState.vaultPath;
772
+ if (vp) {
773
+ const activity = mapQueueEventToActivity(e, vp);
774
+ if (activity) {
775
+ try {
776
+ appendActivity(vp, activity);
777
+ }
778
+ catch {
779
+ // never let a persistence failure crash the server
780
+ }
781
+ // Use a short timer (not setImmediate) so that synchronous queue frames
782
+ // (job-created, job-updated, job-notification, job-completed) are
783
+ // delivered and processed by the client in an earlier poll phase before
784
+ // the activity-event frame arrives. On loopback, TCP delivery is <1 ms,
785
+ // so 20 ms is safely beyond that boundary.
786
+ setTimeout(() => {
787
+ if (!wss)
788
+ return;
789
+ const payload = JSON.stringify({ type: "activity-event", event: activity });
790
+ for (const client of wss.clients) {
791
+ if (client.readyState === WebSocket.OPEN) {
792
+ client.send(payload);
793
+ }
794
+ }
795
+ }, 20);
796
+ }
797
+ }
272
798
  if (e.type === "job-completed") {
273
799
  const message = e.job.status === "failed" ? (e.job.lastError ?? "Job failed") : `Job ${e.job.status}`;
274
800
  emitJobNotification(wss, e.job.id, e.job.status, message);
275
801
  }
276
802
  });
803
+ // ── Tool-call activity tracking (BC-4.3) ─────────────────────────
804
+ // Subscribe to pi_tool_bridge_requests on the agent bus so each tool
805
+ // invocation produces an ActivityEvent with kind: "tool".
806
+ const bus = watcherState.bus;
807
+ if (bus) {
808
+ const toolHandler = async (message) => {
809
+ const payload = message.payload;
810
+ const vp = serverState.vaultPath;
811
+ if (!vp)
812
+ return;
813
+ const activity = mapToolCallToActivity(payload.toolName, payload.agentId, payload.dispatchId, payload.args, vp);
814
+ try {
815
+ appendActivity(vp, activity);
816
+ }
817
+ catch {
818
+ // never let a persistence failure crash the server
819
+ }
820
+ setTimeout(() => {
821
+ if (!wss)
822
+ return;
823
+ const frame = JSON.stringify({ type: "activity-event", event: activity });
824
+ for (const client of wss.clients) {
825
+ if (client.readyState === WebSocket.OPEN) {
826
+ client.send(frame);
827
+ }
828
+ }
829
+ }, 20);
830
+ };
831
+ bus.subscribe("pi_tool_bridge_requests", toolHandler);
832
+ serverState.unsubToolBus = () => bus.unsubscribe("pi_tool_bridge_requests", toolHandler);
833
+ }
277
834
  wss.on("connection", (ws, req) => {
278
835
  const auth = authoriseRequest(req);
279
836
  if (!auth.ok) {
@@ -315,10 +872,17 @@ export function startServer(pi, serverState, watcherState) {
315
872
  serverState.server.listen(serverState.port, "127.0.0.1");
316
873
  }
317
874
  export function stopServer(serverState) {
875
+ if (serverState.getModelReadiness) {
876
+ serverState.getModelReadiness = undefined;
877
+ }
318
878
  if (serverState.unsubQueue) {
319
879
  serverState.unsubQueue();
320
880
  serverState.unsubQueue = undefined;
321
881
  }
882
+ if (serverState.unsubToolBus) {
883
+ serverState.unsubToolBus();
884
+ serverState.unsubToolBus = undefined;
885
+ }
322
886
  if (serverState.wss) {
323
887
  serverState.wss.close();
324
888
  serverState.wss = null;
@@ -331,7 +895,8 @@ export function stopServer(serverState) {
331
895
  // Best-effort cleanup of discovery file
332
896
  if (serverState.vaultPath) {
333
897
  try {
334
- fs.unlinkSync(path.join(serverState.vaultPath, ".vault-mind", "server.json"));
898
+ const serverStatePath = path.join(serverState.vaultPath, resolveVaultFile(loadConfig(serverState.vaultPath).vaultMind, "serverState"));
899
+ fs.unlinkSync(serverStatePath);
335
900
  }
336
901
  catch {
337
902
  // file may not exist — ignore
@@ -342,9 +907,9 @@ function writeDiscoveryFile(serverState) {
342
907
  if (!serverState.vaultPath)
343
908
  return;
344
909
  try {
345
- const dir = path.join(serverState.vaultPath, ".vault-mind");
346
- fs.mkdirSync(dir, { recursive: true });
347
- fs.writeFileSync(path.join(dir, "server.json"), JSON.stringify({ port: serverState.port, pid: process.pid, startedAt: Date.now() }));
910
+ const serverStatePath = path.join(serverState.vaultPath, resolveVaultFile(loadConfig(serverState.vaultPath).vaultMind, "serverState"));
911
+ fs.mkdirSync(path.dirname(serverStatePath), { recursive: true });
912
+ fs.writeFileSync(serverStatePath, JSON.stringify({ port: serverState.port, pid: process.pid, startedAt: Date.now() }));
348
913
  }
349
914
  catch {
350
915
  // never let discovery file errors crash startup
@@ -514,8 +1079,8 @@ function handleVmStatus(res, serverState) {
514
1079
  const ws = serverState.watcherState;
515
1080
  const cfg = loadConfig(process.cwd());
516
1081
  const configured = !!findConfig(process.cwd()).project;
517
- const model = cfg.vaultMind.embedding.model || "embeddinggemma";
518
- const dim = cfg.vaultMind.embedding.dim ?? 768;
1082
+ const model = cfg.vaultMind.embedding[EMBEDDING_FLAT_KEYS[2]] || "embeddinggemma";
1083
+ const dim = cfg.vaultMind.embedding[EMBEDDING_FLAT_KEYS[4]] ?? 768;
519
1084
  res.writeHead(200);
520
1085
  res.end(JSON.stringify({
521
1086
  ok: true,
@@ -567,89 +1132,6 @@ function handleWatcherToggle(res, serverState) {
567
1132
  res.writeHead(200);
568
1133
  res.end(JSON.stringify({ ok: true, watcher: true }));
569
1134
  }
570
- function handleVmInit(req, res) {
571
- readBody(req)
572
- .then((raw) => {
573
- let parsed;
574
- try {
575
- parsed = JSON.parse(raw || "{}");
576
- }
577
- catch {
578
- res.writeHead(400);
579
- res.end(JSON.stringify({ error: "Invalid JSON body" }));
580
- return;
581
- }
582
- const vaultPath = parsed.vaultPath || process.cwd();
583
- if (!fs.existsSync(vaultPath)) {
584
- res.writeHead(400);
585
- res.end(JSON.stringify({ error: `Vault path does not exist: ${vaultPath}` }));
586
- return;
587
- }
588
- const initCollection = resolveInitCollectionName(vaultPath, parsed.collection);
589
- // Scaffold project config
590
- const cfgPath = path.join(vaultPath, "pi-vault-mind.config.json");
591
- if (!fs.existsSync(cfgPath)) {
592
- const tmpl = {
593
- version: 2,
594
- collections: {
595
- main: {
596
- path: `collections/${initCollection}.jsonl`,
597
- schema: ["id", "domain", "source", "fact", "tag", "artifact"],
598
- dedupField: "fact",
599
- },
600
- pending: { path: "collections/pending.jsonl", schema: "main" },
601
- context_events: {
602
- path: "collections/context_events.jsonl",
603
- schema: ["id", "type", "session_entry_id", "content", "timestamp", "tags"],
604
- dedupField: "id",
605
- },
606
- },
607
- injectors: [
608
- {
609
- name: "draft-context",
610
- regex: "draft\\s+(\\S+)",
611
- collection: "main",
612
- filterField: "tag",
613
- artifactPath: "collections/artifact.md",
614
- },
615
- ],
616
- vaultMind: DEFAULT_CONFIG.vaultMind,
617
- };
618
- fs.writeFileSync(cfgPath, `${JSON.stringify(tmpl, null, 2)}\n`, "utf-8");
619
- }
620
- // Scaffold collections dir from config
621
- const projectCfg = JSON.parse(fs.readFileSync(cfgPath, "utf-8"));
622
- for (const def of Object.values(projectCfg.collections || {})) {
623
- if (!def.path)
624
- continue;
625
- const p = path.join(vaultPath, def.path);
626
- if (!fs.existsSync(p)) {
627
- fs.mkdirSync(path.dirname(p), { recursive: true });
628
- fs.writeFileSync(p, "", "utf-8");
629
- }
630
- }
631
- // Scaffold .gitignore
632
- const giPath = path.join(vaultPath, ".gitignore");
633
- const existing = fs.existsSync(giPath) ? fs.readFileSync(giPath, "utf-8").split(/\r?\n/) : [];
634
- let changed = false;
635
- for (const entry of GITIGNORE_ENTRIES) {
636
- if (!existing.some((l) => l.trim() === entry)) {
637
- existing.push(entry);
638
- changed = true;
639
- }
640
- }
641
- if (changed)
642
- fs.writeFileSync(giPath, `${existing.join("\n")}\n`, "utf-8");
643
- res.writeHead(200);
644
- res.end(JSON.stringify({ ok: true, path: vaultPath }));
645
- })
646
- .catch((err) => {
647
- if (!res.headersSent) {
648
- res.writeHead(500);
649
- res.end(JSON.stringify({ error: String(err) }));
650
- }
651
- });
652
- }
653
1135
  function handleVmSetup(req, res) {
654
1136
  readBody(req)
655
1137
  .then((raw) => {
@@ -662,10 +1144,10 @@ function handleVmSetup(req, res) {
662
1144
  res.end(JSON.stringify({ error: "Invalid JSON body" }));
663
1145
  return;
664
1146
  }
665
- const globalPath = path.join(os.homedir(), ".pi", "agent", "vault-mind.config.json");
666
- const existingGlobal = fs.existsSync(globalPath);
667
- const config = existingGlobal
668
- ? JSON.parse(fs.readFileSync(globalPath, "utf-8"))
1147
+ const configPath = getConfigPath(process.cwd());
1148
+ const existingConfig = fs.existsSync(configPath);
1149
+ const config = existingConfig
1150
+ ? JSON.parse(fs.readFileSync(configPath, "utf-8"))
669
1151
  : { vaultMind: { embedding: {}, vaults: {} } };
670
1152
  config.vaultMind = config.vaultMind || {};
671
1153
  config.vaultMind.embedding = config.vaultMind.embedding || {};
@@ -674,20 +1156,25 @@ function handleVmSetup(req, res) {
674
1156
  config.vaultMind.vaults.default = { path: shrinkHome(parsed.vault), autoSync: true };
675
1157
  }
676
1158
  if (parsed.remoteUrl) {
677
- config.vaultMind.embedding.remoteUrl = parsed.remoteUrl;
1159
+ config.vaultMind.embedding[EMBEDDING_FLAT_KEYS[0]] = parsed.remoteUrl;
678
1160
  }
679
1161
  if (parsed.localUrl) {
680
- config.vaultMind.embedding.localUrl = parsed.localUrl;
1162
+ config.vaultMind.embedding[EMBEDDING_FLAT_KEYS[1]] = parsed.localUrl;
681
1163
  }
682
1164
  if (parsed.model) {
683
- config.vaultMind.embedding.model = parsed.model;
1165
+ config.vaultMind.embedding[EMBEDDING_FLAT_KEYS[2]] = parsed.model;
684
1166
  }
685
1167
  if (typeof parsed.useTransformers === "boolean") {
686
- config.vaultMind.embedding.useTransformers = parsed.useTransformers;
1168
+ config.vaultMind.embedding[EMBEDDING_FLAT_KEYS[5]] = parsed.useTransformers;
687
1169
  }
688
1170
  if (parsed.workspace) {
689
- config.vaultMind.embedding.modal = config.vaultMind.embedding.modal || {};
690
- config.vaultMind.embedding.modal.workspace = parsed.workspace;
1171
+ config.vaultMind.embedding[EMBEDDING_FLAT_KEYS[6]] = parsed.workspace;
1172
+ if (!parsed.remoteUrl) {
1173
+ config.vaultMind.embedding[EMBEDDING_FLAT_KEYS[0]] = modalUrl(parsed.workspace);
1174
+ }
1175
+ }
1176
+ if (parsed.remoteApiKey) {
1177
+ config.vaultMind.embedding[EMBEDDING_FLAT_KEYS[7]] = parsed.remoteApiKey;
691
1178
  }
692
1179
  if (parsed.folders) {
693
1180
  config.vaultMind.folders = config.vaultMind.folders || {};
@@ -698,12 +1185,18 @@ function handleVmSetup(req, res) {
698
1185
  }
699
1186
  }
700
1187
  }
701
- const dir = path.dirname(globalPath);
1188
+ const dir = path.dirname(configPath);
702
1189
  if (!fs.existsSync(dir))
703
1190
  fs.mkdirSync(dir, { recursive: true });
704
- fs.writeFileSync(globalPath, `${JSON.stringify(config, null, 2)}\n`, "utf-8");
1191
+ fs.writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`, "utf-8");
1192
+ // Scaffold default collections/injectors/.gitignore — the same
1193
+ // shared implementation the CLI /vm setup command uses. Order-
1194
+ // independent: seeds collections/injectors whenever they're empty,
1195
+ // regardless of whether this config write happened first.
1196
+ const scaffold = scaffoldVaultConfig(process.cwd(), parsed.collection);
1197
+ scaffoldModelRouterConfig(process.cwd());
705
1198
  res.writeHead(200);
706
- res.end(JSON.stringify({ ok: true }));
1199
+ res.end(JSON.stringify({ ok: true, ...scaffold }));
707
1200
  })
708
1201
  .catch((err) => {
709
1202
  if (!res.headersSent) {
@@ -732,32 +1225,201 @@ function handleServerStart(res, serverState) {
732
1225
  res.end(JSON.stringify({ ok: true, port: serverState.port }));
733
1226
  }
734
1227
  function handleVaultMindConfig(res) {
735
- const globalPath2 = path.join(os.homedir(), ".pi", "agent", "vault-mind.config.json");
736
- const globalCfg = fs.existsSync(globalPath2)
737
- ? JSON.parse(fs.readFileSync(globalPath2, "utf-8"))
738
- : null;
739
- const projectCfg = findConfig(process.cwd()).project;
740
- const project = projectCfg ? JSON.parse(fs.readFileSync(projectCfg, "utf-8")) : null;
741
- const hasToken = !!resolveAuthToken();
742
- const modal = globalCfg?.vaultMind?.embedding?.modal ?? project?.vaultMind?.embedding?.modal ?? null;
743
- res.writeHead(200);
1228
+ const loaded = loadConfig(process.cwd());
1229
+ const config = redactEmbeddingSecrets(loaded);
1230
+ const embeddingSecrets = readEmbeddingSecretStatus(loaded.vaultMind);
1231
+ const emb = config.vaultMind.embedding;
1232
+ res.writeHead(200, { "Content-Type": "application/json" });
744
1233
  res.end(JSON.stringify({
745
- global: globalCfg,
746
- project,
747
- hasToken,
748
- modal: modal
1234
+ config,
1235
+ hasToken: resolveAuthToken() !== null,
1236
+ embeddingSecrets,
1237
+ remote: emb.remoteUrl
749
1238
  ? {
750
- workspace: modal.workspace ?? null,
751
- baseUrl: modal.baseUrl ?? null,
752
- model: modal.model ?? "embeddinggemma",
753
- dim: modal.dim ?? null,
754
- fallback: modal.fallback ?? null,
755
- sync: modal.sync ?? null,
1239
+ remoteUrl: emb.remoteUrl,
1240
+ workspace: emb.workspace ?? null,
1241
+ model: emb.model ?? "embeddinggemma",
1242
+ dim: emb.dim ?? null,
1243
+ fallback: emb.fallback ?? null,
1244
+ sync: emb.sync ?? null,
756
1245
  }
757
1246
  : null,
758
1247
  version: "0.8.0",
759
1248
  }));
760
1249
  }
1250
+ const EMBEDDING_SECRET_KINDS = [
1251
+ "localApiKey",
1252
+ "remoteApiKey",
1253
+ "remoteReadApiKey",
1254
+ "remoteWriteApiKey",
1255
+ ];
1256
+ async function handlePutEmbeddingSecrets(req, res) {
1257
+ try {
1258
+ const parsed = await readJsonBody(req);
1259
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
1260
+ res.writeHead(400, { "Content-Type": "application/json" });
1261
+ res.end(JSON.stringify({ error: "invalid_request", message: "Expected JSON object body." }));
1262
+ return;
1263
+ }
1264
+ const body = parsed;
1265
+ for (const kind of EMBEDDING_SECRET_KINDS) {
1266
+ const value = body[kind];
1267
+ if (value === undefined)
1268
+ continue;
1269
+ if (value === null)
1270
+ continue;
1271
+ if (typeof value !== "string" || value === "") {
1272
+ res.writeHead(400, { "Content-Type": "application/json" });
1273
+ res.end(JSON.stringify({
1274
+ error: "invalid_request",
1275
+ message: `Empty or invalid value rejected for ${kind}.`,
1276
+ }));
1277
+ return;
1278
+ }
1279
+ }
1280
+ const result = writeEmbeddingSecrets(body);
1281
+ res.writeHead(result.ok ? 200 : 500, { "Content-Type": "application/json" });
1282
+ res.end(JSON.stringify(result));
1283
+ }
1284
+ catch (err) {
1285
+ const message = err instanceof Error ? err.message : String(err);
1286
+ res.writeHead(400, { "Content-Type": "application/json" });
1287
+ res.end(JSON.stringify({ error: "invalid_request", message }));
1288
+ }
1289
+ }
1290
+ async function handleVmEmbeddingProbe(req, res) {
1291
+ try {
1292
+ const parsed = await readJsonBody(req);
1293
+ const validation = validateProbeRequest(parsed);
1294
+ if ("error" in validation) {
1295
+ res.writeHead(400, { "Content-Type": "application/json" });
1296
+ res.end(JSON.stringify(validation));
1297
+ return;
1298
+ }
1299
+ const cfg = loadConfig(process.cwd()).vaultMind;
1300
+ const result = await probeEmbeddingEndpoint(validation, { config: cfg });
1301
+ const status = result.error
1302
+ ? result.error.code === "invalid_request"
1303
+ ? 400
1304
+ : result.error.code === "unauthorized"
1305
+ ? 401
1306
+ : 502
1307
+ : 200;
1308
+ res.writeHead(status, { "Content-Type": "application/json" });
1309
+ res.end(JSON.stringify(result));
1310
+ }
1311
+ catch (err) {
1312
+ const message = err instanceof Error ? err.message : String(err);
1313
+ res.writeHead(400, { "Content-Type": "application/json" });
1314
+ res.end(JSON.stringify(buildProbeErrorResponse("local", "", "invalid_request", message)));
1315
+ }
1316
+ }
1317
+ function buildProbeErrorResponse(target, endpoint, code, message) {
1318
+ return {
1319
+ ok: false,
1320
+ target,
1321
+ endpoint,
1322
+ models: [],
1323
+ selectedModelAvailable: null,
1324
+ latencyMs: null,
1325
+ error: { code, message },
1326
+ };
1327
+ }
1328
+ function validateProbeRequest(body) {
1329
+ if (!body || typeof body !== "object" || Array.isArray(body)) {
1330
+ return buildProbeErrorResponse("local", "", "invalid_request", "Invalid request body");
1331
+ }
1332
+ const obj = body;
1333
+ const target = obj.target;
1334
+ if (target !== "local" && target !== "remote") {
1335
+ return buildProbeErrorResponse("local", "", "invalid_request", "Invalid target");
1336
+ }
1337
+ const url = obj.url;
1338
+ if (typeof url !== "string") {
1339
+ return buildProbeErrorResponse(target, "", "invalid_request", "Invalid URL");
1340
+ }
1341
+ try {
1342
+ const parsed = new URL(url);
1343
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
1344
+ return buildProbeErrorResponse(target, "", "invalid_request", "Invalid URL");
1345
+ }
1346
+ }
1347
+ catch {
1348
+ return buildProbeErrorResponse(target, "", "invalid_request", "Invalid URL");
1349
+ }
1350
+ if (obj.workspace !== undefined && typeof obj.workspace !== "string") {
1351
+ return buildProbeErrorResponse(target, url, "invalid_request", "Invalid workspace");
1352
+ }
1353
+ if (obj.model !== undefined && typeof obj.model !== "string") {
1354
+ return buildProbeErrorResponse(target, url, "invalid_request", "Invalid model");
1355
+ }
1356
+ const transientSecrets = obj.transientSecrets;
1357
+ if (transientSecrets !== undefined) {
1358
+ if (!transientSecrets ||
1359
+ typeof transientSecrets !== "object" ||
1360
+ Array.isArray(transientSecrets)) {
1361
+ return buildProbeErrorResponse(target, url, "invalid_request", "Invalid transientSecrets");
1362
+ }
1363
+ const ts = transientSecrets;
1364
+ for (const key of ["apiKey", "readApiKey", "writeApiKey"]) {
1365
+ if (ts[key] !== undefined && typeof ts[key] !== "string") {
1366
+ return buildProbeErrorResponse(target, url, "invalid_request", `Invalid transient credential ${key}`);
1367
+ }
1368
+ }
1369
+ }
1370
+ return {
1371
+ target,
1372
+ url,
1373
+ workspace: obj.workspace === undefined ? undefined : String(obj.workspace),
1374
+ model: obj.model === undefined ? undefined : String(obj.model),
1375
+ transientSecrets: transientSecrets === undefined
1376
+ ? undefined
1377
+ : transientSecrets,
1378
+ };
1379
+ }
1380
+ function handleVaultMindPaths(res, serverState) {
1381
+ const vaultPath = serverState.vaultPath || process.cwd();
1382
+ const paths = resolveVaultMindPaths(vaultPath);
1383
+ res.writeHead(200);
1384
+ res.end(JSON.stringify(paths));
1385
+ }
1386
+ function handleVaultMindModels(res, serverState) {
1387
+ const vaultPath = serverState.vaultPath || process.cwd();
1388
+ const { providers, error } = readModelProviders(vaultPath);
1389
+ res.writeHead(200, { "Content-Type": "application/json" });
1390
+ res.end(JSON.stringify(error ? { providers, error } : { providers }));
1391
+ }
1392
+ async function handleModelReadiness(res, serverState) {
1393
+ if (!serverState.getModelReadiness) {
1394
+ res.writeHead(500);
1395
+ res.end(JSON.stringify({ error: "Readiness callback not installed" }));
1396
+ return;
1397
+ }
1398
+ try {
1399
+ const readiness = await serverState.getModelReadiness();
1400
+ res.writeHead(200);
1401
+ res.end(JSON.stringify(readiness));
1402
+ }
1403
+ catch {
1404
+ res.writeHead(200);
1405
+ res.end(JSON.stringify({ status: "model-registry-error" }));
1406
+ }
1407
+ }
1408
+ /**
1409
+ * Build an AgentIdentity for an authenticated HTTP request from the resolved
1410
+ * per-role token identity. Falls back to the hard-coded role profile, merged
1411
+ * with any user-configured identity overrides. Returns undefined for legacy
1412
+ * single-token or auth-off requests that carry no role identity.
1413
+ */
1414
+ function resolveHttpIdentity(req, vaultMind) {
1415
+ const result = authoriseRequest(req);
1416
+ if (!result.ok || !result.identity)
1417
+ return undefined;
1418
+ const role = result.identity.identity;
1419
+ const base = buildIdentityFromRole(role, role);
1420
+ const identitiesCfg = vaultMind.identities ?? {};
1421
+ return mergeIdentityProfiles(base, identitiesCfg.defaultProfile, identitiesCfg.roles?.[role]);
1422
+ }
761
1423
  /**
762
1424
  * Wrap a request handler with bearer-token auth. Use this for every
763
1425
  * new Stage-1 route so auth is consistent and testable in one place.
@@ -776,7 +1438,7 @@ export const withAuth = (req, res, handler, opts) => {
776
1438
  reason: result.reason,
777
1439
  hint: resolveAuthToken()
778
1440
  ? "Send Authorization: Bearer <PVM_API_TOKEN>"
779
- : "Set PVM_API_TOKEN in env or ~/.pi/agent/vault-mind.env, or in pi-vault-mind.config.json (vaultMind.embedding.modal.apiToken), then restart pi.",
1441
+ : "Set PVM_API_TOKEN in env or .vault-mind/vault-mind.env, then restart pi.",
780
1442
  }));
781
1443
  return;
782
1444
  }
@@ -793,6 +1455,24 @@ export const withAuth = (req, res, handler, opts) => {
793
1455
  };
794
1456
  /** Read the full request body as UTF-8. Already used by handleScan; exposed for new routes. */
795
1457
  export const readJsonBody = (req) => readBody(req).then((text) => (text ? JSON.parse(text) : {}));
1458
+ function deepMerge(base, overlay) {
1459
+ const result = { ...base };
1460
+ for (const key of Object.keys(overlay)) {
1461
+ const ov = overlay[key];
1462
+ if (ov &&
1463
+ typeof ov === "object" &&
1464
+ !Array.isArray(ov) &&
1465
+ typeof result[key] === "object" &&
1466
+ result[key] !== null &&
1467
+ !Array.isArray(result[key])) {
1468
+ result[key] = deepMerge(result[key], ov);
1469
+ }
1470
+ else {
1471
+ result[key] = ov;
1472
+ }
1473
+ }
1474
+ return result;
1475
+ }
796
1476
  // ── Agent queue handlers ────────────────────────────────────────────────────
797
1477
  const defaultVaultPath = () => {
798
1478
  const cfg = loadConfig(process.cwd());
@@ -816,6 +1496,21 @@ function handleAgentQueue(req, res) {
816
1496
  res.writeHead(200);
817
1497
  res.end(JSON.stringify(jobs));
818
1498
  }
1499
+ function handleAgentActivity(req, res, serverState) {
1500
+ const vaultPath = serverState.vaultPath ?? defaultVaultPath();
1501
+ if (!vaultPath) {
1502
+ res.writeHead(500);
1503
+ res.end(JSON.stringify({ error: "No default vault configured." }));
1504
+ return;
1505
+ }
1506
+ const url = new URL(req.url || "/", "http://127.0.0.1");
1507
+ const jobId = url.searchParams.get("jobId") ?? undefined;
1508
+ const limitRaw = url.searchParams.get("limit");
1509
+ const limit = limitRaw !== null ? Number(limitRaw) : undefined;
1510
+ const events = listActivity(vaultPath, { jobId, limit });
1511
+ res.writeHead(200);
1512
+ res.end(JSON.stringify(events));
1513
+ }
819
1514
  function handleAgentJob(req, res, pathname) {
820
1515
  // pathname is /agent/jobs/<id> or /agent/jobs/<id>/retry or /agent/jobs/<id>/cancel
821
1516
  const parts = pathname.split("/").filter(Boolean);
@@ -876,21 +1571,135 @@ function handleAgentJob(req, res, pathname) {
876
1571
  res.end(JSON.stringify({ error: `Unknown action: ${action}` }));
877
1572
  }
878
1573
  // ── Tool routes ─────────────────────────────────────────────────────────
879
- async function handleVmSearch(req, res, serverState) {
880
- const body = (await readJsonBody(req));
881
- if (!body.query) {
882
- res.writeHead(400);
883
- res.end(JSON.stringify({ error: "Missing query." }));
884
- return;
885
- }
1574
+ async function handleVmSearch(req, res, serverState, overrideBody) {
1575
+ const rawBody = (await readJsonBody(req));
1576
+ const body = { ...rawBody, ...overrideBody };
1577
+ const mode = body.mode ?? "hybrid";
886
1578
  const vaultPath = serverState.vaultPath || process.cwd();
887
1579
  const cfg = loadConfig(vaultPath);
1580
+ const identity = resolveHttpIdentity(req, cfg.vaultMind);
888
1581
  const collection = body.collection || "main";
889
1582
  const limit = body.limit ?? 5;
1583
+ if (!body.query && mode !== "graph") {
1584
+ res.writeHead(400);
1585
+ res.end(JSON.stringify({ error: "Missing query." }));
1586
+ return;
1587
+ }
1588
+ if (mode === "graph" && !body.query && !body.entity) {
1589
+ res.writeHead(400);
1590
+ res.end(JSON.stringify({ error: "Missing query or entity." }));
1591
+ return;
1592
+ }
1593
+ const entity = body.entity ?? body.query ?? "";
1594
+ const query = body.query ?? entity;
1595
+ let resolvedMode = mode;
1596
+ let rationale;
1597
+ if (mode === "auto") {
1598
+ const c = classifySearchMode(query);
1599
+ resolvedMode = c.mode;
1600
+ rationale = c.rationale;
1601
+ }
1602
+ const VALID_MODES = {
1603
+ auto: true,
1604
+ hybrid: true,
1605
+ semantic: true,
1606
+ fts: true,
1607
+ graph: true,
1608
+ full: true,
1609
+ vault: true,
1610
+ };
1611
+ if (!VALID_MODES[mode]) {
1612
+ res.writeHead(400);
1613
+ res.end(JSON.stringify({ error: `Unknown mode: ${mode}` }));
1614
+ return;
1615
+ }
890
1616
  try {
891
- const results = await searchHybrid(cfg.vaultMind.dataDir, collection, body.query, limit, cfg.vaultMind);
1617
+ let results;
1618
+ let relations;
1619
+ let message;
1620
+ if (resolvedMode === "hybrid") {
1621
+ results = await searchHybridRanked(cfg.vaultMind.dataDir, collection, query, limit, cfg.vaultMind, identity);
1622
+ }
1623
+ else if (resolvedMode === "semantic") {
1624
+ results = await searchHybrid(cfg.vaultMind.dataDir, collection, query, limit, cfg.vaultMind, identity);
1625
+ }
1626
+ else if (resolvedMode === "fts") {
1627
+ results = await searchFts(cfg.vaultMind.dataDir, collection, query, limit, cfg.vaultMind, identity);
1628
+ }
1629
+ else if (resolvedMode === "graph") {
1630
+ const depth = body.depth ?? 1;
1631
+ const norm = normalizeGraphResult(await queryGraph(cfg.vaultMind.dataDir, cfg.vaultMind, entity, depth, identity));
1632
+ results = norm.hits;
1633
+ relations = norm.relations;
1634
+ message = norm.message;
1635
+ }
1636
+ else if (resolvedMode === "vault") {
1637
+ results = await searchVaultNative(vaultPath, query, limit, body.tags);
1638
+ }
1639
+ else {
1640
+ // mode === "full"
1641
+ const depth = body.depth ?? 1;
1642
+ const [hybrid, semantic, fts, graph, vault] = await Promise.all([
1643
+ searchHybridRanked(cfg.vaultMind.dataDir, collection, query, limit, cfg.vaultMind, identity).catch((err) => {
1644
+ console.error("[pi-vault-mind] Full-mode hybrid search failed:", err);
1645
+ return [];
1646
+ }),
1647
+ searchHybrid(cfg.vaultMind.dataDir, collection, query, limit, cfg.vaultMind, identity).catch((err) => {
1648
+ console.error("[pi-vault-mind] Full-mode semantic search failed:", err);
1649
+ return [];
1650
+ }),
1651
+ searchFts(cfg.vaultMind.dataDir, collection, query, limit, cfg.vaultMind, identity).catch((err) => {
1652
+ console.error("[pi-vault-mind] Full-mode FTS search failed:", err);
1653
+ return [];
1654
+ }),
1655
+ queryGraph(cfg.vaultMind.dataDir, cfg.vaultMind, entity, depth, identity).catch((err) => {
1656
+ console.error("[pi-vault-mind] Full-mode graph search failed:", err);
1657
+ return { entities: [], relations: [] };
1658
+ }),
1659
+ searchVaultNative(vaultPath, query, limit, body.tags).catch((err) => {
1660
+ console.error("[pi-vault-mind] Full-mode vault search failed:", err);
1661
+ return [];
1662
+ }),
1663
+ ]);
1664
+ const graphNorm = normalizeGraphResult(graph);
1665
+ relations = graphNorm.relations;
1666
+ message = graphNorm.message;
1667
+ const seen = new Set();
1668
+ const merged = [];
1669
+ const addResults = (items, source) => {
1670
+ for (const r of items) {
1671
+ const row = r;
1672
+ const key = (row.id ?? row.fact ?? JSON.stringify(row));
1673
+ if (!seen.has(key)) {
1674
+ seen.add(key);
1675
+ if (row._source === undefined)
1676
+ row._source = source;
1677
+ merged.push(row);
1678
+ }
1679
+ }
1680
+ };
1681
+ addResults(hybrid, "hybrid");
1682
+ addResults(semantic, "semantic");
1683
+ addResults(fts, "fts");
1684
+ addResults(graphNorm.hits, "graph");
1685
+ addResults(vault, "vault");
1686
+ results = merged.slice(0, limit);
1687
+ }
1688
+ const sources = Array.isArray(body.sources) ? body.sources : ["vault"];
1689
+ if (sources.includes("sessions")) {
1690
+ results = [...results, ...searchSessions(resolveSessionsDir(), query, limit)];
1691
+ }
892
1692
  res.writeHead(200);
893
- res.end(JSON.stringify({ hits: results, collection, query: body.query }));
1693
+ res.end(JSON.stringify({
1694
+ hits: results,
1695
+ ...(relations !== undefined ? { relations } : {}),
1696
+ ...(message !== undefined ? { message } : {}),
1697
+ ...(mode === "auto" ? { resolvedMode, rationale } : {}),
1698
+ collection,
1699
+ query,
1700
+ mode,
1701
+ sources,
1702
+ }));
894
1703
  }
895
1704
  catch (err) {
896
1705
  const message = err instanceof Error ? err.message : String(err);
@@ -907,6 +1716,7 @@ async function handleVmAppend(req, res, serverState) {
907
1716
  }
908
1717
  const vaultPath = serverState.vaultPath || process.cwd();
909
1718
  const cfg = loadConfig(vaultPath);
1719
+ const identity = resolveHttpIdentity(req, cfg.vaultMind);
910
1720
  const def = cfg.collections[body.collection];
911
1721
  if (!def) {
912
1722
  res.writeHead(400);
@@ -920,7 +1730,7 @@ async function handleVmAppend(req, res, serverState) {
920
1730
  ensureDir(def.path);
921
1731
  fs.appendFileSync(def.path, line, "utf-8");
922
1732
  try {
923
- await upsertEntry(cfg.vaultMind.dataDir, body.collection, body.entry, cfg.vaultMind);
1733
+ await upsertEntry(cfg.vaultMind.dataDir, body.collection, body.entry, cfg.vaultMind, identity);
924
1734
  res.writeHead(200);
925
1735
  res.end(JSON.stringify({ ok: true, collection: body.collection }));
926
1736
  }
@@ -930,49 +1740,6 @@ async function handleVmAppend(req, res, serverState) {
930
1740
  res.end(JSON.stringify({ error: message }));
931
1741
  }
932
1742
  }
933
- async function handleVmFtsSearch(req, res, serverState) {
934
- const body = (await readJsonBody(req));
935
- if (!body.query) {
936
- res.writeHead(400);
937
- res.end(JSON.stringify({ error: "query required" }));
938
- return;
939
- }
940
- const vaultPath = serverState.vaultPath || process.cwd();
941
- const cfg = loadConfig(vaultPath);
942
- const collection = body.collection || "main";
943
- const limit = body.limit ?? 10;
944
- try {
945
- const results = await searchFts(cfg.vaultMind.dataDir, collection, body.query, limit, cfg.vaultMind);
946
- res.writeHead(200);
947
- res.end(JSON.stringify({ hits: results, collection, query: body.query }));
948
- }
949
- catch (err) {
950
- const message = err instanceof Error ? err.message : String(err);
951
- res.writeHead(500);
952
- res.end(JSON.stringify({ error: message }));
953
- }
954
- }
955
- async function handleVmGraph(req, res, serverState) {
956
- const body = (await readJsonBody(req));
957
- if (!body.entity) {
958
- res.writeHead(400);
959
- res.end(JSON.stringify({ error: "entity required" }));
960
- return;
961
- }
962
- const vaultPath = serverState.vaultPath || process.cwd();
963
- const cfg = loadConfig(vaultPath);
964
- const depth = body.depth ?? 1;
965
- try {
966
- const results = await queryGraph(cfg.vaultMind.dataDir, cfg.vaultMind, body.entity, depth);
967
- res.writeHead(200);
968
- res.end(JSON.stringify({ entity: body.entity, depth, results }));
969
- }
970
- catch (err) {
971
- const message = err instanceof Error ? err.message : String(err);
972
- res.writeHead(500);
973
- res.end(JSON.stringify({ error: message }));
974
- }
975
- }
976
1743
  // ── Pending review route ─────────────────────────────────────────────────────
977
1744
  async function handleVmApprove(req, res, serverState) {
978
1745
  const body = (await readJsonBody(req));