daftari 3.5.0 → 3.7.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 (151) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +39 -1
  3. package/dist/attest/bundle.d.ts +53 -0
  4. package/dist/attest/bundle.d.ts.map +1 -0
  5. package/dist/attest/bundle.js +141 -0
  6. package/dist/attest/bundle.js.map +1 -0
  7. package/dist/attest/index.d.ts +2 -0
  8. package/dist/attest/index.d.ts.map +1 -0
  9. package/dist/attest/index.js +139 -0
  10. package/dist/attest/index.js.map +1 -0
  11. package/dist/attest/sign.d.ts +16 -0
  12. package/dist/attest/sign.d.ts.map +1 -0
  13. package/dist/attest/sign.js +75 -0
  14. package/dist/attest/sign.js.map +1 -0
  15. package/dist/attest/verify.d.ts +21 -0
  16. package/dist/attest/verify.d.ts.map +1 -0
  17. package/dist/attest/verify.js +148 -0
  18. package/dist/attest/verify.js.map +1 -0
  19. package/dist/cli.d.ts.map +1 -1
  20. package/dist/cli.js +6 -0
  21. package/dist/cli.js.map +1 -1
  22. package/dist/consolidate/decorrelation.js +1 -1
  23. package/dist/consolidate/decorrelation.js.map +1 -1
  24. package/dist/consolidate/index.js +1 -1
  25. package/dist/court/index.d.ts.map +1 -1
  26. package/dist/court/index.js +5 -3
  27. package/dist/court/index.js.map +1 -1
  28. package/dist/curation/lint.d.ts.map +1 -1
  29. package/dist/curation/lint.js +68 -1
  30. package/dist/curation/lint.js.map +1 -1
  31. package/dist/curation/positions.d.ts +14 -0
  32. package/dist/curation/positions.d.ts.map +1 -1
  33. package/dist/curation/positions.js +74 -0
  34. package/dist/curation/positions.js.map +1 -1
  35. package/dist/curation/provenance.d.ts.map +1 -1
  36. package/dist/curation/provenance.js.map +1 -1
  37. package/dist/curation/staged-actions.d.ts +2 -0
  38. package/dist/curation/staged-actions.d.ts.map +1 -1
  39. package/dist/curation/staged-actions.js +5 -0
  40. package/dist/curation/staged-actions.js.map +1 -1
  41. package/dist/curation/tension.d.ts +9 -1
  42. package/dist/curation/tension.d.ts.map +1 -1
  43. package/dist/curation/tension.js +34 -1
  44. package/dist/curation/tension.js.map +1 -1
  45. package/dist/distill/adapters/claude-session.d.ts +6 -0
  46. package/dist/distill/adapters/claude-session.d.ts.map +1 -0
  47. package/dist/distill/adapters/claude-session.js +71 -0
  48. package/dist/distill/adapters/claude-session.js.map +1 -0
  49. package/dist/distill/cli.d.ts.map +1 -1
  50. package/dist/distill/cli.js +113 -9
  51. package/dist/distill/cli.js.map +1 -1
  52. package/dist/distill/index.d.ts +1 -0
  53. package/dist/distill/index.d.ts.map +1 -1
  54. package/dist/distill/index.js +3 -0
  55. package/dist/distill/index.js.map +1 -1
  56. package/dist/distill/propose.d.ts +23 -5
  57. package/dist/distill/propose.d.ts.map +1 -1
  58. package/dist/distill/propose.js +58 -30
  59. package/dist/distill/propose.js.map +1 -1
  60. package/dist/federation/classification.d.ts +9 -0
  61. package/dist/federation/classification.d.ts.map +1 -0
  62. package/dist/federation/classification.js +122 -0
  63. package/dist/federation/classification.js.map +1 -0
  64. package/dist/federation/index-location.d.ts +4 -0
  65. package/dist/federation/index-location.d.ts.map +1 -0
  66. package/dist/federation/index-location.js +30 -0
  67. package/dist/federation/index-location.js.map +1 -0
  68. package/dist/federation/mount-index.d.ts +8 -0
  69. package/dist/federation/mount-index.d.ts.map +1 -0
  70. package/dist/federation/mount-index.js +63 -0
  71. package/dist/federation/mount-index.js.map +1 -0
  72. package/dist/federation/mounts.d.ts +29 -0
  73. package/dist/federation/mounts.d.ts.map +1 -0
  74. package/dist/federation/mounts.js +215 -0
  75. package/dist/federation/mounts.js.map +1 -0
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +27 -0
  78. package/dist/index.js.map +1 -1
  79. package/dist/search/hybrid.d.ts +10 -0
  80. package/dist/search/hybrid.d.ts.map +1 -1
  81. package/dist/search/hybrid.js +28 -15
  82. package/dist/search/hybrid.js.map +1 -1
  83. package/dist/search/reindex.d.ts +2 -0
  84. package/dist/search/reindex.d.ts.map +1 -1
  85. package/dist/search/reindex.js +17 -9
  86. package/dist/search/reindex.js.map +1 -1
  87. package/dist/serve/auth-log.d.ts +16 -0
  88. package/dist/serve/auth-log.d.ts.map +1 -0
  89. package/dist/serve/auth-log.js +53 -0
  90. package/dist/serve/auth-log.js.map +1 -0
  91. package/dist/serve/index.d.ts +2 -0
  92. package/dist/serve/index.d.ts.map +1 -1
  93. package/dist/serve/index.js +141 -10
  94. package/dist/serve/index.js.map +1 -1
  95. package/dist/serve/limits.d.ts +37 -0
  96. package/dist/serve/limits.d.ts.map +1 -0
  97. package/dist/serve/limits.js +79 -0
  98. package/dist/serve/limits.js.map +1 -0
  99. package/dist/server.d.ts.map +1 -1
  100. package/dist/server.js +20 -0
  101. package/dist/server.js.map +1 -1
  102. package/dist/storage/index-db.d.ts +1 -0
  103. package/dist/storage/index-db.d.ts.map +1 -1
  104. package/dist/storage/index-db.js +9 -3
  105. package/dist/storage/index-db.js.map +1 -1
  106. package/dist/tools/curation.d.ts.map +1 -1
  107. package/dist/tools/curation.js +6 -5
  108. package/dist/tools/curation.js.map +1 -1
  109. package/dist/tools/positions.d.ts +8 -2
  110. package/dist/tools/positions.d.ts.map +1 -1
  111. package/dist/tools/positions.js +317 -173
  112. package/dist/tools/positions.js.map +1 -1
  113. package/dist/tools/read.d.ts +11 -0
  114. package/dist/tools/read.d.ts.map +1 -1
  115. package/dist/tools/read.js +188 -10
  116. package/dist/tools/read.js.map +1 -1
  117. package/dist/tools/receipt.d.ts +6 -0
  118. package/dist/tools/receipt.d.ts.map +1 -1
  119. package/dist/tools/receipt.js +47 -1
  120. package/dist/tools/receipt.js.map +1 -1
  121. package/dist/tools/search.d.ts +1 -1
  122. package/dist/tools/search.d.ts.map +1 -1
  123. package/dist/tools/search.js +455 -38
  124. package/dist/tools/search.js.map +1 -1
  125. package/dist/tools/staged-actions.d.ts.map +1 -1
  126. package/dist/tools/staged-actions.js +26 -2
  127. package/dist/tools/staged-actions.js.map +1 -1
  128. package/dist/tools/witness.d.ts.map +1 -1
  129. package/dist/tools/witness.js +78 -7
  130. package/dist/tools/witness.js.map +1 -1
  131. package/dist/tools/write.d.ts +9 -0
  132. package/dist/tools/write.d.ts.map +1 -1
  133. package/dist/tools/write.js +374 -234
  134. package/dist/tools/write.js.map +1 -1
  135. package/dist/utils/config.d.ts +34 -0
  136. package/dist/utils/config.d.ts.map +1 -1
  137. package/dist/utils/config.js +168 -6
  138. package/dist/utils/config.js.map +1 -1
  139. package/dist/utils/git.d.ts +18 -2
  140. package/dist/utils/git.d.ts.map +1 -1
  141. package/dist/utils/git.js +176 -34
  142. package/dist/utils/git.js.map +1 -1
  143. package/dist/utils/vault-gitignore.d.ts +1 -1
  144. package/dist/utils/vault-gitignore.d.ts.map +1 -1
  145. package/dist/utils/vault-gitignore.js +1 -0
  146. package/dist/utils/vault-gitignore.js.map +1 -1
  147. package/dist/witness/track-record.d.ts +23 -0
  148. package/dist/witness/track-record.d.ts.map +1 -1
  149. package/dist/witness/track-record.js +0 -0
  150. package/dist/witness/track-record.js.map +1 -1
  151. package/package.json +1 -1
@@ -14,11 +14,13 @@ import { TENSION_KINDS } from "../curation/tension.js";
14
14
  import { sourceReadable } from "../curation/tension-access.js";
15
15
  import { bucketHiddenDownstream } from "../curation/tension-blast.js";
16
16
  import { computeValidity } from "../curation/validity.js";
17
+ import { ensureMountIndexFresh, reindexMount } from "../federation/mount-index.js";
18
+ import { federatedPathOf, getMountRegistry, parseFederatedPath, } from "../federation/mounts.js";
17
19
  import { err, ok } from "../frontmatter/types.js";
18
20
  import { contestedFor } from "../search/contested.js";
19
21
  import { applyCoveragePass, DEFAULT_COVERAGE_OPTIONS, enforceTokenCap, } from "../search/coverage.js";
20
22
  import { resolveCurrentSource } from "../search/current-source.js";
21
- import { DEFAULT_WEIGHTS, hybridSearch, relatedSearch, } from "../search/hybrid.js";
23
+ import { DEFAULT_WEIGHTS, extractRelatedSeed, hybridSearch, relatedSearch, relatedSearchFromSeed, } from "../search/hybrid.js";
22
24
  import { getIndexStatus, indexingBusyMessage, markIndexError, markIndexing, markIndexReady, onceIndexReady, } from "../search/index-state.js";
23
25
  import { reindexVault } from "../search/reindex.js";
24
26
  import { resolveValidAtSource } from "../search/valid-at-source.js";
@@ -196,6 +198,151 @@ function validityForPath(db, path, at) {
196
198
  return null;
197
199
  return computeValidity({ valid_from: doc.validFrom, valid_until: doc.validUntil }, at);
198
200
  }
201
+ // Resolves the optional `vaults` argument against the mount registry. Absent
202
+ // ⇒ local plus every available mount; an explicit list names exactly what it
203
+ // wants ("local" and/or declared aliases) and fails loud on an unknown alias
204
+ // or an explicitly named unavailable mount.
205
+ function resolveVaultScope(raw, registry, tool) {
206
+ if (registry === null) {
207
+ if (raw !== undefined) {
208
+ return err(new Error(`${tool} 'vaults' requires federation to be configured`));
209
+ }
210
+ return ok({ includeLocal: true, mounts: [] });
211
+ }
212
+ if (raw === undefined) {
213
+ return ok({
214
+ includeLocal: true,
215
+ mounts: [...registry.mounts.values()].filter((m) => m.state === "ok"),
216
+ });
217
+ }
218
+ if (!Array.isArray(raw) ||
219
+ raw.length === 0 ||
220
+ !raw.every((v) => typeof v === "string")) {
221
+ return err(new Error(`${tool} 'vaults' must be a non-empty array of mount aliases (or "local")`));
222
+ }
223
+ const scope = { includeLocal: false, mounts: [] };
224
+ const seen = new Set();
225
+ for (const name of raw) {
226
+ if (seen.has(name))
227
+ continue;
228
+ seen.add(name);
229
+ if (name === "local") {
230
+ scope.includeLocal = true;
231
+ continue;
232
+ }
233
+ const mount = registry.mounts.get(name);
234
+ if (!mount) {
235
+ return err(new Error(`${tool} 'vaults' names unknown mount alias "${name}"`));
236
+ }
237
+ if (mount.root === null) {
238
+ return err(new Error(`mount "${name}" is unavailable — its path was not found`));
239
+ }
240
+ scope.mounts.push(mount);
241
+ }
242
+ return ok(scope);
243
+ }
244
+ // The mount's principal-resolved identity in AccessContext shape, so the
245
+ // per-mount pipeline reuses the same helpers (currentSource's restricted
246
+ // degrade, readable-collection pushdown) the canonical path uses.
247
+ function mountAccess(mount, user) {
248
+ return { user: user ?? "guest", roleName: mount.roleName, role: mount.role };
249
+ }
250
+ // Rewrites every addressable path a mount hit carries into `alias:path` form
251
+ // — the round-trip property: any path a federated tool returns is directly
252
+ // usable as the path argument to any federated read tool.
253
+ function labelMountHit(hit, alias) {
254
+ hit.vault = alias;
255
+ hit.path = federatedPathOf(alias, hit.path);
256
+ if (hit.currentSource?.kind === "resolved") {
257
+ hit.currentSource = {
258
+ ...hit.currentSource,
259
+ path: federatedPathOf(alias, hit.currentSource.path),
260
+ };
261
+ }
262
+ if (hit.validAtSource?.kind === "resolved") {
263
+ hit.validAtSource = {
264
+ ...hit.validAtSource,
265
+ path: federatedPathOf(alias, hit.validAtSource.path),
266
+ };
267
+ }
268
+ }
269
+ // Cross-vault fusion (spec Decision 4): RRF over the per-vault FINAL rank
270
+ // lists. Rank fusion consumes only orderings, so it is indifferent to BM25
271
+ // magnitude differences across corpora; there is no cross-provider score
272
+ // problem because one embedding provider serves the whole process. Lists
273
+ // arrive in priority order (canonical first), which is the deterministic
274
+ // tiebreak for equal RRF scores.
275
+ const RRF_K = 60;
276
+ function rrfFuse(lists, limit) {
277
+ const scored = [];
278
+ lists.forEach((hits, listIdx) => {
279
+ hits.forEach((hit, rank) => {
280
+ scored.push({ hit, score: 1 / (RRF_K + rank + 1), listIdx, rank });
281
+ });
282
+ });
283
+ scored.sort((a, b) => b.score - a.score || a.listIdx - b.listIdx || a.rank - b.rank);
284
+ return scored.slice(0, limit).map((s) => s.hit);
285
+ }
286
+ // One mount's search pipeline: the same hybrid ranking, RBAC filtering,
287
+ // validity pass, coverage pass, and current-source foregrounding the
288
+ // canonical vault gets — under the MOUNT's granted role and against the
289
+ // mount's own index. Deliberately absent: contested/structural annotations
290
+ // and serve logging, which read or write vault state (documents only).
291
+ async function searchMount(mount, opts) {
292
+ if (mount.root === null) {
293
+ return err(new Error(`mount "${mount.alias}" is unavailable — its path was not found`));
294
+ }
295
+ const fresh = await ensureMountIndexFresh(mount);
296
+ if (!fresh.ok)
297
+ return fresh;
298
+ const dbResult = openIndexForActiveProvider(mount.root);
299
+ if (!dbResult.ok)
300
+ return dbResult;
301
+ const db = dbResult.value;
302
+ try {
303
+ const result = await hybridSearch(db, opts.query, {
304
+ weights: opts.weights,
305
+ limit: opts.limit,
306
+ overFetch: true,
307
+ readableCollections: readableCollections(mount.role),
308
+ });
309
+ if (!result.ok)
310
+ return result;
311
+ let permitted = result.value.hits.filter((h) => canRead(mount.role, h.collection));
312
+ if (opts.validAt !== null) {
313
+ for (const hit of permitted) {
314
+ hit.validity = validityForPath(db, hit.path, opts.validAt);
315
+ }
316
+ if (opts.validOnly) {
317
+ permitted = permitted.filter((h) => h.validity == null ||
318
+ (h.validity.state !== "expired" && h.validity.state !== "not-yet"));
319
+ }
320
+ }
321
+ const ranked = permitted.slice(0, opts.limit);
322
+ const widened = applyCoveragePass(db, ranked, DEFAULT_COVERAGE_OPTIONS).filter((h) => h.viaCoverage ? canRead(mount.role, h.collection) : true);
323
+ const access = mountAccess(mount, opts.user);
324
+ for (const hit of widened) {
325
+ if (opts.validAt !== null && hit.validity === undefined) {
326
+ hit.validity = validityForPath(db, hit.path, opts.validAt);
327
+ }
328
+ const cs = resolveCurrentSource(db, hit.path, access);
329
+ if (cs)
330
+ hit.currentSource = cs;
331
+ if (opts.validAt !== null) {
332
+ const vas = resolveValidAtSource(db, hit.path, opts.validAt, access);
333
+ if (vas)
334
+ hit.validAtSource = vas;
335
+ }
336
+ }
337
+ const capped = enforceTokenCap(widened, DEFAULT_COVERAGE_OPTIONS);
338
+ for (const hit of capped)
339
+ labelMountHit(hit, mount.alias);
340
+ return ok({ hits: capped, vectorUsed: result.value.vectorUsed });
341
+ }
342
+ finally {
343
+ db.close();
344
+ }
345
+ }
199
346
  export async function vaultSearch(vaultRoot, args, access) {
200
347
  const query = args.query;
201
348
  if (typeof query !== "string" || query.trim().length === 0) {
@@ -228,6 +375,85 @@ export async function vaultSearch(vaultRoot, args, access) {
228
375
  error: new Error("vault_search 'valid_only' requires 'valid_at' — there is no date to filter against"),
229
376
  };
230
377
  }
378
+ // Federation scope (#297). No registry ⇒ the canonical-only path below,
379
+ // byte-identical to pre-federation behavior apart from the vault labels.
380
+ const registry = getMountRegistry();
381
+ const scope = resolveVaultScope(args.vaults, registry, "vault_search");
382
+ if (!scope.ok)
383
+ return scope;
384
+ const { includeLocal, mounts } = scope.value;
385
+ let localResult = null;
386
+ if (includeLocal) {
387
+ const local = await searchLocalVault(vaultRoot, args, query, validAt, validOnly, access);
388
+ if (!local.ok)
389
+ return local;
390
+ localResult = local.value;
391
+ for (const hit of localResult.hits)
392
+ hit.vault = "local";
393
+ }
394
+ if (mounts.length === 0) {
395
+ return localResult
396
+ ? ok(localResult)
397
+ : err(new Error("vault_search 'vaults' selected no vault"));
398
+ }
399
+ // Per-vault pipelines, then RRF over the final rank lists (Decision 4).
400
+ const limit = parseLimit(args.limit);
401
+ const weights = parseWeights(args.weights);
402
+ const lists = [];
403
+ if (localResult)
404
+ lists.push(localResult.hits);
405
+ let vectorUsed = localResult?.vectorUsed ?? false;
406
+ for (const mount of mounts) {
407
+ const m = await searchMount(mount, {
408
+ query,
409
+ weights,
410
+ limit,
411
+ validAt,
412
+ validOnly,
413
+ user: access?.user,
414
+ });
415
+ if (!m.ok)
416
+ return err(new Error(`mount "${mount.alias}": ${m.error.message}`));
417
+ lists.push(m.value.hits);
418
+ vectorUsed = vectorUsed || m.value.vectorUsed;
419
+ }
420
+ const fused = rrfFuse(lists, limit);
421
+ // #3 rerank over the SAME fused cross-vault ranking the hits came from,
422
+ // still excluding coverage additions (recall, not ranking).
423
+ const rerankK = parseRerankCandidates(args.rerank_candidates);
424
+ const rerank = rerankK > 0
425
+ ? {
426
+ instructions: RERANK_INSTRUCTIONS,
427
+ candidates: fused
428
+ .filter((h) => !h.viaCoverage)
429
+ .slice(0, rerankK)
430
+ .map((h, i) => ({
431
+ rank: i + 1,
432
+ path: h.path,
433
+ title: h.title,
434
+ collection: h.collection,
435
+ status: h.status,
436
+ score: h.score,
437
+ bm25Score: h.bm25Score,
438
+ vectorScore: h.vectorScore,
439
+ snippet: h.snippet,
440
+ })),
441
+ }
442
+ : undefined;
443
+ return ok({
444
+ query,
445
+ count: fused.length,
446
+ vectorUsed,
447
+ weights: localResult?.weights ?? (vectorUsed ? weights : { bm25: 1, vector: 0 }),
448
+ hits: fused,
449
+ ...(rerank ? { rerank } : {}),
450
+ });
451
+ }
452
+ // The canonical vault's search pipeline — the pre-federation vault_search
453
+ // body, unchanged: ranking, RBAC filter, validity pass, coverage, current-
454
+ // source foregrounding, contested/structural enrichment, serve logging, and
455
+ // the local rerank pool.
456
+ async function searchLocalVault(vaultRoot, args, query, validAt, validOnly, access) {
231
457
  const ready = await ensureIndexReady(vaultRoot);
232
458
  if (!ready.ok)
233
459
  return ready;
@@ -384,40 +610,191 @@ export async function vaultSearchRelated(vaultRoot, args, access) {
384
610
  error: new Error("vault_search_related requires a non-empty 'path' argument"),
385
611
  };
386
612
  }
387
- const ready = await ensureIndexReady(vaultRoot);
388
- if (!ready.ok)
389
- return ready;
390
- const dbResult = openIndexForActiveProvider(vaultRoot);
391
- if (!dbResult.ok)
392
- return dbResult;
393
- const db = dbResult.value;
394
- try {
395
- const limit = parseLimit(args.limit);
396
- // Over-fetch, then RBAC-filter, then slice — same ordering as vaultSearch so
397
- // restricted docs in the top-`limit` slots can't shrink the permitted page.
398
- const result = relatedSearch(db, path, {
399
- weights: parseWeights(args.weights),
400
- limit,
401
- overFetch: true,
402
- readableCollections: access ? readableCollections(access.role) : undefined,
403
- });
404
- if (!result.ok)
405
- return result;
406
- // RBAC: drop related hits in collections the role cannot read (when an
407
- // access context is present), THEN slice to the user-facing limit. The slice
408
- // runs unconditionally because over-fetch returned the full candidate set.
409
- const permitted = access
410
- ? result.value.hits.filter((h) => canRead(access.role, h.collection))
411
- : result.value.hits;
412
- const hits = permitted.slice(0, limit);
413
- await annotateAndLogServedHits(vaultRoot, db, "vault_search_related", hits, access);
414
- return ok({ ...result.value, count: hits.length, hits });
613
+ const registry = getMountRegistry();
614
+ const scope = resolveVaultScope(args.vaults, registry, "vault_search_related");
615
+ if (!scope.ok)
616
+ return scope;
617
+ const { includeLocal, mounts } = scope.value;
618
+ const fed = registry ? parseFederatedPath(path, registry) : null;
619
+ // Canonical seed, canonical-only scope: the pre-federation path, unchanged
620
+ // apart from the vault labels.
621
+ if (fed === null && mounts.length === 0) {
622
+ const ready = await ensureIndexReady(vaultRoot);
623
+ if (!ready.ok)
624
+ return ready;
625
+ const dbResult = openIndexForActiveProvider(vaultRoot);
626
+ if (!dbResult.ok)
627
+ return dbResult;
628
+ const db = dbResult.value;
629
+ try {
630
+ const limit = parseLimit(args.limit);
631
+ // Over-fetch, then RBAC-filter, then slice — same ordering as vaultSearch so
632
+ // restricted docs in the top-`limit` slots can't shrink the permitted page.
633
+ const result = relatedSearch(db, path, {
634
+ weights: parseWeights(args.weights),
635
+ limit,
636
+ overFetch: true,
637
+ readableCollections: access ? readableCollections(access.role) : undefined,
638
+ });
639
+ if (!result.ok)
640
+ return result;
641
+ // RBAC: drop related hits in collections the role cannot read (when an
642
+ // access context is present), THEN slice to the user-facing limit. The slice
643
+ // runs unconditionally because over-fetch returned the full candidate set.
644
+ const permitted = access
645
+ ? result.value.hits.filter((h) => canRead(access.role, h.collection))
646
+ : result.value.hits;
647
+ const hits = permitted.slice(0, limit);
648
+ for (const hit of hits)
649
+ hit.vault = "local";
650
+ await annotateAndLogServedHits(vaultRoot, db, "vault_search_related", hits, access);
651
+ return ok({ ...result.value, count: hits.length, hits });
652
+ }
653
+ finally {
654
+ db.close();
655
+ }
415
656
  }
416
- finally {
417
- db.close();
657
+ // Federated: extract the seed from its HOME vault, then rank every in-scope
658
+ // vault's candidates against it (Decision 4). Coherent across indexes
659
+ // because one embedding provider serves the whole process.
660
+ const limit = parseLimit(args.limit);
661
+ const weights = parseWeights(args.weights);
662
+ let seed;
663
+ if (fed !== null && registry !== null) {
664
+ const mount = registry.mounts.get(fed.alias);
665
+ if (!mount || mount.root === null) {
666
+ return err(new Error(`mount "${fed.alias}" is unavailable — its path was not found`));
667
+ }
668
+ const fresh = await ensureMountIndexFresh(mount);
669
+ if (!fresh.ok)
670
+ return fresh;
671
+ const homeDbResult = openIndexForActiveProvider(mount.root);
672
+ if (!homeDbResult.ok)
673
+ return homeDbResult;
674
+ const homeDb = homeDbResult.value;
675
+ try {
676
+ // Seed gate under the MOUNT's granted role — a caller must not use an
677
+ // unreadable mount doc as a query proxy.
678
+ const doc = getDocument(homeDb, fed.relPath);
679
+ if (!doc) {
680
+ return err(new Error(`document not indexed: ${path} (try vault_reindex)`));
681
+ }
682
+ if (!canRead(mount.role, doc.collection)) {
683
+ return err(new Error(`access denied: role '${mount.roleName}' cannot read collection '${doc.collection}'`));
684
+ }
685
+ const extracted = extractRelatedSeed(homeDb, fed.relPath);
686
+ if (!extracted.ok)
687
+ return extracted;
688
+ seed = extracted.value;
689
+ }
690
+ finally {
691
+ homeDb.close();
692
+ }
418
693
  }
694
+ else {
695
+ const ready = await ensureIndexReady(vaultRoot);
696
+ if (!ready.ok)
697
+ return ready;
698
+ const homeDbResult = openIndexForActiveProvider(vaultRoot);
699
+ if (!homeDbResult.ok)
700
+ return homeDbResult;
701
+ const homeDb = homeDbResult.value;
702
+ try {
703
+ const extracted = extractRelatedSeed(homeDb, path);
704
+ if (!extracted.ok)
705
+ return extracted;
706
+ seed = extracted.value;
707
+ }
708
+ finally {
709
+ homeDb.close();
710
+ }
711
+ }
712
+ const lists = [];
713
+ let vectorUsed = false;
714
+ if (includeLocal) {
715
+ const ready = await ensureIndexReady(vaultRoot);
716
+ if (!ready.ok)
717
+ return ready;
718
+ const dbResult = openIndexForActiveProvider(vaultRoot);
719
+ if (!dbResult.ok)
720
+ return dbResult;
721
+ const db = dbResult.value;
722
+ try {
723
+ const ranked = relatedSearchFromSeed(db, seed, fed === null ? path : null, {
724
+ weights,
725
+ limit,
726
+ overFetch: true,
727
+ readableCollections: access ? readableCollections(access.role) : undefined,
728
+ });
729
+ const permitted = access
730
+ ? ranked.hits.filter((h) => canRead(access.role, h.collection))
731
+ : ranked.hits;
732
+ const hits = permitted.slice(0, limit);
733
+ for (const hit of hits)
734
+ hit.vault = "local";
735
+ await annotateAndLogServedHits(vaultRoot, db, "vault_search_related", hits, access);
736
+ lists.push(hits);
737
+ vectorUsed = vectorUsed || ranked.vectorUsed;
738
+ }
739
+ finally {
740
+ db.close();
741
+ }
742
+ }
743
+ for (const mount of mounts) {
744
+ if (mount.root === null)
745
+ continue;
746
+ const fresh = await ensureMountIndexFresh(mount);
747
+ if (!fresh.ok)
748
+ return err(new Error(`mount "${mount.alias}": ${fresh.error.message}`));
749
+ const dbResult = openIndexForActiveProvider(mount.root);
750
+ if (!dbResult.ok)
751
+ return err(new Error(`mount "${mount.alias}": ${dbResult.error.message}`));
752
+ const db = dbResult.value;
753
+ try {
754
+ const ranked = relatedSearchFromSeed(db, seed, fed !== null && fed.alias === mount.alias ? fed.relPath : null, { weights, limit, overFetch: true, readableCollections: readableCollections(mount.role) });
755
+ const hits = ranked.hits.filter((h) => canRead(mount.role, h.collection)).slice(0, limit);
756
+ for (const hit of hits)
757
+ labelMountHit(hit, mount.alias);
758
+ lists.push(hits);
759
+ vectorUsed = vectorUsed || ranked.vectorUsed;
760
+ }
761
+ finally {
762
+ db.close();
763
+ }
764
+ }
765
+ const fused = rrfFuse(lists, limit);
766
+ return ok({
767
+ path,
768
+ count: fused.length,
769
+ vectorUsed,
770
+ weights: vectorUsed ? weights : { bm25: 1, vector: 0 },
771
+ hits: fused,
772
+ });
419
773
  }
420
- export async function vaultReindex(vaultRoot) {
774
+ export async function vaultReindex(vaultRoot, args = {}) {
775
+ // #297: `mount: <alias>` rebuilds one mount's index (under the canonical
776
+ // .daftari/federation/<alias>/ redirect) — the manual freshness lever for
777
+ // startup-only mounts. Named `mount`, not `vault`: the multi-vault router
778
+ // package reserves a `vault` input property on every routed tool. The
779
+ // default remains canonical-only, and the mount path deliberately bypasses
780
+ // the global IndexState, which tracks the canonical index's lifecycle.
781
+ if (args.mount !== undefined && args.mount !== "local") {
782
+ if (typeof args.mount !== "string") {
783
+ return err(new Error("vault_reindex 'mount' must be a mount alias string"));
784
+ }
785
+ const registry = getMountRegistry();
786
+ if (!registry) {
787
+ return err(new Error("vault_reindex 'mount' requires federation to be configured"));
788
+ }
789
+ const mount = registry.mounts.get(args.mount);
790
+ if (!mount) {
791
+ return err(new Error(`vault_reindex 'mount' names unknown mount alias "${args.mount}"`));
792
+ }
793
+ const rebuilt = await reindexMount(mount);
794
+ if (!rebuilt.ok)
795
+ return rebuilt;
796
+ return ok({ ...rebuilt.value, vault: mount.alias });
797
+ }
421
798
  // Coalesce with any in-flight indexing pass — e.g. the startup-time
422
799
  // background reindex from main(). An agent that calls vault_reindex should
423
800
  // not get a busy error just because the server is finishing its own
@@ -546,7 +923,16 @@ const contestedTensionSchema = {
546
923
  const hybridHitSchema = {
547
924
  type: "object",
548
925
  properties: {
549
- path: { type: "string", description: "Vault-relative document path." },
926
+ path: {
927
+ type: "string",
928
+ description: "Vault-relative document path; for a federation mount hit, the " +
929
+ "addressable '<alias>:<path>' form (#297).",
930
+ },
931
+ vault: {
932
+ type: "string",
933
+ description: 'Which vault served the hit: a federation mount alias, or "local" ' +
934
+ "for the canonical vault (#297).",
935
+ },
550
936
  title: { type: "string" },
551
937
  collection: { type: "string" },
552
938
  status: { type: "string" },
@@ -742,6 +1128,14 @@ export const searchTools = [
742
1128
  "(max 30) with judging context so YOU reorder by answer quality. " +
743
1129
  "Omit to skip.",
744
1130
  },
1131
+ vaults: {
1132
+ type: "array",
1133
+ items: { type: "string" },
1134
+ description: "Federation scope (#297): which vaults to search — mount aliases " +
1135
+ 'and/or "local" for the canonical vault. Omit to search the local ' +
1136
+ "vault plus every available mount. Requires federation to be " +
1137
+ "configured.",
1138
+ },
745
1139
  },
746
1140
  required: ["query"],
747
1141
  additionalProperties: false,
@@ -803,13 +1197,22 @@ export const searchTools = [
803
1197
  properties: {
804
1198
  path: {
805
1199
  type: "string",
806
- description: "Vault-relative path of the reference document",
1200
+ description: "Vault-relative path of the reference document; with federation " +
1201
+ "configured, an '<alias>:<path>' form seeds from that mounted " +
1202
+ "vault (#297).",
807
1203
  },
808
1204
  limit: {
809
1205
  type: "number",
810
1206
  description: "Maximum results to return (default 10, max 50)",
811
1207
  },
812
1208
  weights: weightsSchema,
1209
+ vaults: {
1210
+ type: "array",
1211
+ items: { type: "string" },
1212
+ description: "Federation scope (#297): which vaults to rank candidates from — " +
1213
+ 'mount aliases and/or "local". Omit for the local vault plus ' +
1214
+ "every available mount.",
1215
+ },
813
1216
  },
814
1217
  required: ["path"],
815
1218
  additionalProperties: false,
@@ -850,16 +1253,30 @@ export const searchTools = [
850
1253
  annotations: { destructiveHint: false, idempotentHint: true },
851
1254
  description: "Rebuild the search index from the markdown files on disk. The index " +
852
1255
  "is a derived cache; this clears and rebuilds it, re-embedding all " +
853
- "document chunks. Run after bulk edits made outside Daftari.",
1256
+ "document chunks. Run after bulk edits made outside Daftari. With " +
1257
+ "federation configured, pass 'mount: <alias>' to rebuild one mount's " +
1258
+ "index instead — the manual freshness lever for startup-only mounts (#297).",
854
1259
  inputSchema: {
855
1260
  type: "object",
856
- properties: {},
1261
+ properties: {
1262
+ // Named `mount`, not `vault`: the multi-vault router package reserves
1263
+ // a `vault` input property on every routed tool for its own dispatch.
1264
+ mount: {
1265
+ type: "string",
1266
+ description: "Federation mount alias to reindex instead of the local vault " +
1267
+ '(#297). Omit (or pass "local") for the canonical vault.',
1268
+ },
1269
+ },
857
1270
  additionalProperties: false,
858
1271
  },
859
1272
  outputSchema: {
860
1273
  type: "object",
861
1274
  properties: {
862
- vault: { type: "string", description: "Absolute path of the reindexed vault root." },
1275
+ vault: {
1276
+ type: "string",
1277
+ description: "Absolute path of the reindexed vault root, or the mount alias " +
1278
+ "for a federated reindex (#297).",
1279
+ },
863
1280
  documentCount: { type: "integer" },
864
1281
  chunkCount: { type: "integer" },
865
1282
  vectorEnabled: { type: "boolean" },
@@ -893,7 +1310,7 @@ export const searchTools = [
893
1310
  ],
894
1311
  additionalProperties: false,
895
1312
  },
896
- handler: (vaultRoot) => vaultReindex(vaultRoot),
1313
+ handler: (vaultRoot, args) => vaultReindex(vaultRoot, args),
897
1314
  },
898
1315
  ];
899
1316
  //# sourceMappingURL=search.js.map