daftari 3.14.0 → 3.16.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 (51) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/consolidate/index.d.ts.map +1 -1
  3. package/dist/consolidate/index.js +3 -12
  4. package/dist/consolidate/index.js.map +1 -1
  5. package/dist/curation/leak-ledger.d.ts +30 -0
  6. package/dist/curation/leak-ledger.d.ts.map +1 -0
  7. package/dist/curation/leak-ledger.js +190 -0
  8. package/dist/curation/leak-ledger.js.map +1 -0
  9. package/dist/distill/cli.d.ts.map +1 -1
  10. package/dist/distill/cli.js +2 -1
  11. package/dist/distill/cli.js.map +1 -1
  12. package/dist/distill/cost.d.ts +3 -3
  13. package/dist/distill/cost.d.ts.map +1 -1
  14. package/dist/distill/index.d.ts.map +1 -1
  15. package/dist/distill/index.js +3 -15
  16. package/dist/distill/index.js.map +1 -1
  17. package/dist/distill/state.d.ts +12 -0
  18. package/dist/distill/state.d.ts.map +1 -1
  19. package/dist/distill/state.js +16 -6
  20. package/dist/distill/state.js.map +1 -1
  21. package/dist/eval/index.d.ts.map +1 -1
  22. package/dist/eval/index.js +11 -16
  23. package/dist/eval/index.js.map +1 -1
  24. package/dist/eval/llm-openrouter.d.ts +4 -1
  25. package/dist/eval/llm-openrouter.d.ts.map +1 -1
  26. package/dist/eval/llm-openrouter.js +21 -5
  27. package/dist/eval/llm-openrouter.js.map +1 -1
  28. package/dist/eval/transport.d.ts +5 -0
  29. package/dist/eval/transport.d.ts.map +1 -0
  30. package/dist/eval/transport.js +27 -0
  31. package/dist/eval/transport.js.map +1 -0
  32. package/dist/sleep/index.d.ts.map +1 -1
  33. package/dist/sleep/index.js +4 -14
  34. package/dist/sleep/index.js.map +1 -1
  35. package/dist/tools/board.test.js +2 -0
  36. package/dist/tools/board.test.js.map +1 -1
  37. package/dist/tools/read.d.ts.map +1 -1
  38. package/dist/tools/read.js +45 -0
  39. package/dist/tools/read.js.map +1 -1
  40. package/dist/tools/search.d.ts.map +1 -1
  41. package/dist/tools/search.js +77 -8
  42. package/dist/tools/search.js.map +1 -1
  43. package/dist/tools/write.d.ts +2 -0
  44. package/dist/tools/write.d.ts.map +1 -1
  45. package/dist/tools/write.js +181 -1
  46. package/dist/tools/write.js.map +1 -1
  47. package/dist/utils/config.d.ts +10 -0
  48. package/dist/utils/config.d.ts.map +1 -1
  49. package/dist/utils/config.js +73 -0
  50. package/dist/utils/config.js.map +1 -1
  51. package/package.json +1 -1
@@ -13,6 +13,7 @@ import matter from "gray-matter";
13
13
  import { acquireLock, mintLeaseHolder, openLockDb, releaseLock } from "../access/locks.js";
14
14
  import { canPromote, canWrite, isProposeOnly } from "../access/rbac.js";
15
15
  import { mintConsumesEdges } from "../curation/consumes.js";
16
+ import { leakLedgerPath, privateReadsForRun, privateReadsForRunStrict, } from "../curation/leak-ledger.js";
16
17
  import { foreignPositionViolation } from "../curation/positions.js";
17
18
  import { frontmatterDiff, recordProvenance } from "../curation/provenance.js";
18
19
  import { recordShadowAction } from "../curation/shadow.js";
@@ -33,7 +34,7 @@ import { indexDocument } from "../search/reindex.js";
33
34
  import { noteSelfWrite } from "../search/self-write.js";
34
35
  import { allDocumentPaths, getDocumentsByPaths } from "../storage/index-db.js";
35
36
  import { readFile, resolveVaultPath } from "../storage/local.js";
36
- import { loadConfig } from "../utils/config.js";
37
+ import { loadConfig, } from "../utils/config.js";
37
38
  import { normalizeIsoDate } from "../utils/dates.js";
38
39
  import { commit } from "../utils/git.js";
39
40
  import { sha256Hex } from "../utils/hash.js";
@@ -324,6 +325,29 @@ async function performWrite(params) {
324
325
  return err(new Error(`${STALE_WRITE_PREFIX} ${params.relPath} changed since base_version`));
325
326
  }
326
327
  }
328
+ // Cross-vault leak gate (U3): the write-time refusal. Runs AFTER the
329
+ // stale-write check (a stale write should surface as staleness, not a
330
+ // leak refusal) and BEFORE anything durable — writeFile is the first
331
+ // side effect below, and mintConsumesEdges (the other candidate
332
+ // insertion point) runs post-commit, which is too late for a refusal.
333
+ const gate = await checkLeakGate({
334
+ visibility: params.visibility,
335
+ leakGate: params.leakGate,
336
+ hasAccessContext: params.hasAccessContext,
337
+ runId: params.runId,
338
+ });
339
+ if (gate.outcome === "refuse") {
340
+ await recordProvenance(params.vaultRoot, {
341
+ tool: params.tool,
342
+ file: params.relPath,
343
+ agent: params.agent,
344
+ ...(params.principal ? { principal: params.principal } : {}),
345
+ ...(params.runId ? { run_id: params.runId } : {}),
346
+ action: "rejected_leak",
347
+ reason: gate.reason,
348
+ });
349
+ return err(new Error(`${LEAK_GATE_PREFIX} ${params.relPath}: ${gate.reason}`));
350
+ }
327
351
  await mkdir(dirname(params.absPath), { recursive: true });
328
352
  await writeFile(params.absPath, params.fileText, "utf-8");
329
353
  const indexed = await indexDocument(params.vaultRoot, params.relPath);
@@ -384,6 +408,12 @@ async function performWrite(params) {
384
408
  updated: params.newFrontmatter.updated,
385
409
  validation: params.validation,
386
410
  indexUpdated: indexed.ok,
411
+ ...(gate.outcome === "warn"
412
+ ? {
413
+ leak_warning: `this run read ${gate.count} private source(s) this session before ` +
414
+ `writing into a shared vault (leak_gate.mode: "warn" — the write landed anyway)`,
415
+ }
416
+ : {}),
387
417
  });
388
418
  }
389
419
  finally {
@@ -452,6 +482,83 @@ export function commitIdentity(agent, principal) {
452
482
  return { author: agent, trailer: "" };
453
483
  return { author: principal, trailer: `\n\nDaftari-Agent: ${agent}` };
454
484
  }
485
+ // The one place the leak-gate rejection message prefix is defined, mirroring
486
+ // STALE_WRITE_PREFIX below. `mode: refuse` is the only mode that ever
487
+ // produces this error — `warn` lands the write with a `leak_warning` instead,
488
+ // `off` never runs the check.
489
+ export const LEAK_GATE_PREFIX = "leak gate:";
490
+ // Cross-vault leak gate (U3): decides whether a write into `visibility`
491
+ // should be refused, landed-with-a-warning, or allowed outright, given the
492
+ // run that produced it. Never returns which private document was read —
493
+ // only a count — so a refusal surfacing in a shared context cannot itself
494
+ // leak the private source's path (spec: refusal names the run_id and count
495
+ // only).
496
+ //
497
+ // Known, accepted structural limitation: this can only see what the CURRENT
498
+ // run_id read. An agent that reads a private source under run R and then
499
+ // writes under a DIFFERENT run_id R2 is not caught — the ledger has nothing
500
+ // under R2 to find. Closing that gap needs a stronger caller-identity signal
501
+ // than the free-text, caller-supplied run_id this gate is built on.
502
+ async function checkLeakGate(params) {
503
+ const mode = params.leakGate.mode;
504
+ if (mode === "off")
505
+ return { outcome: "allow" };
506
+ if (params.visibility !== "shared")
507
+ return { outcome: "allow" };
508
+ // Operator servers (no AccessContext) bypass the gate entirely, matching
509
+ // the existing operator-bypass convention on the foreign-position check
510
+ // above (`if (access && ...)`) — there is no authenticated agent identity
511
+ // to hold accountable for an omitted run_id.
512
+ if (!params.hasAccessContext)
513
+ return { outcome: "allow" };
514
+ if (!params.runId) {
515
+ // An agent caller writing into a shared vault with no run_id at all
516
+ // cannot be checked against the ledger — and omitting run_id is exactly
517
+ // how an agent would dodge this gate if silence were allowed to pass.
518
+ // `refuse` treats "cannot verify" as "deny"; `warn` has nothing to warn
519
+ // about and lets the write through unannotated.
520
+ if (mode === "refuse") {
521
+ return {
522
+ outcome: "refuse",
523
+ reason: "no run_id was supplied for a write into a shared vault — cannot verify " +
524
+ "this run read no private source, so the write is refused",
525
+ };
526
+ }
527
+ return { outcome: "allow" };
528
+ }
529
+ const ledgerPath = leakLedgerPath(params.leakGate.sessionLedgerPath);
530
+ if (mode === "refuse") {
531
+ // Fail-closed: a ledger the gate cannot read is treated as a possible
532
+ // leak, not an absence of one. privateReadsForRun's fail-OPEN posture
533
+ // (used by warn mode and by the read/search instrumentation) is
534
+ // deliberately not used here — see privateReadsForRunStrict's docs.
535
+ const scanned = await privateReadsForRunStrict(ledgerPath, params.runId);
536
+ if (!scanned.ok) {
537
+ return {
538
+ outcome: "refuse",
539
+ reason: `the leak ledger could not be read (${scanned.error.message}) — cannot ` +
540
+ `verify run ${params.runId} read no private source, so the write into ` +
541
+ "this shared vault is refused",
542
+ };
543
+ }
544
+ if (scanned.value.count > 0) {
545
+ return {
546
+ outcome: "refuse",
547
+ reason: `run ${params.runId} read ${scanned.value.count} private-visibility source(s) ` +
548
+ "this session — refusing to write that into a shared vault",
549
+ };
550
+ }
551
+ return { outcome: "allow" };
552
+ }
553
+ // mode === "warn": best-effort, never blocks. A ledger read failure
554
+ // degrades to "nothing seen" (privateReadsForRun's own fail-open
555
+ // contract) — appropriate here because warn mode's whole point is to
556
+ // never refuse a write.
557
+ const scanned = await privateReadsForRun(ledgerPath, params.runId);
558
+ if (scanned.count > 0)
559
+ return { outcome: "warn", count: scanned.count };
560
+ return { outcome: "allow" };
561
+ }
455
562
  // The one place the stale-rejection message prefix is defined: the producer
456
563
  // (performWrite's base_version check) and the classifier below must agree,
457
564
  // or retryOnStale silently stops retrying and the lost-update race returns.
@@ -544,6 +651,9 @@ export function performFrontmatterWrite(opts) {
544
651
  shadowMode: target.config.shadowMode,
545
652
  principal: opts.access?.user,
546
653
  bodyChanged: false,
654
+ visibility: target.config.visibility,
655
+ leakGate: target.config.leakGate,
656
+ hasAccessContext: opts.access !== undefined,
547
657
  ...(opts.runId !== undefined ? { runId: opts.runId } : {}),
548
658
  });
549
659
  }
@@ -1075,6 +1185,9 @@ export async function vaultWrite(vaultRoot, args, access) {
1075
1185
  principal: access?.user,
1076
1186
  ...(runId.value !== undefined ? { runId: runId.value } : {}),
1077
1187
  bodyChanged: !isUpdate || !sameBody(oldContent, body),
1188
+ visibility: config.value.visibility,
1189
+ leakGate: config.value.leakGate,
1190
+ hasAccessContext: access !== undefined,
1078
1191
  });
1079
1192
  // #169: an in-place overwrite destroys the prior version's lineage, and
1080
1193
  // daftari HAS the preserve-not-overwrite primitive — steer toward it at
@@ -1188,6 +1301,9 @@ export async function vaultAppend(vaultRoot, args, access) {
1188
1301
  principal: access?.user,
1189
1302
  ...(runId.value !== undefined ? { runId: runId.value } : {}),
1190
1303
  bodyChanged: true,
1304
+ visibility: target.value.config.visibility,
1305
+ leakGate: target.value.config.leakGate,
1306
+ hasAccessContext: access !== undefined,
1191
1307
  });
1192
1308
  if (!written.ok)
1193
1309
  return written;
@@ -1234,6 +1350,9 @@ export async function vaultPromote(vaultRoot, args, access) {
1234
1350
  const agent = requireString(args, "agent", "vault_promote");
1235
1351
  if (!agent.ok)
1236
1352
  return agent;
1353
+ const runId = readRunId(args, "vault_promote");
1354
+ if (!runId.ok)
1355
+ return runId;
1237
1356
  const baseVersion = readBaseVersion(args, "vault_promote");
1238
1357
  if (!baseVersion.ok)
1239
1358
  return baseVersion;
@@ -1283,6 +1402,7 @@ export async function vaultPromote(vaultRoot, args, access) {
1283
1402
  commitMessage: `vault_promote: ${path.value} draft→canonical by ${agent.value}`,
1284
1403
  baseVersion: baseVersion.value ?? target.value.contentHash,
1285
1404
  access,
1405
+ ...(runId.value !== undefined ? { runId: runId.value } : {}),
1286
1406
  });
1287
1407
  };
1288
1408
  return retryOnStaleIfDefaulted(baseVersion.value, attemptPromote);
@@ -1355,6 +1475,9 @@ export async function vaultDeprecate(vaultRoot, args, access) {
1355
1475
  const agent = requireString(args, "agent", "vault_deprecate");
1356
1476
  if (!agent.ok)
1357
1477
  return agent;
1478
+ const runId = readRunId(args, "vault_deprecate");
1479
+ if (!runId.ok)
1480
+ return runId;
1358
1481
  const reason = requireString(args, "reason", "vault_deprecate");
1359
1482
  if (!reason.ok)
1360
1483
  return reason;
@@ -1412,6 +1535,7 @@ export async function vaultDeprecate(vaultRoot, args, access) {
1412
1535
  (supersededBy ? ` (superseded by ${supersededBy})` : ""),
1413
1536
  baseVersion: baseVersion.value ?? target.value.contentHash,
1414
1537
  access,
1538
+ ...(runId.value !== undefined ? { runId: runId.value } : {}),
1415
1539
  });
1416
1540
  };
1417
1541
  const written = await retryOnStaleIfDefaulted(baseVersion.value, attemptDeprecate);
@@ -1445,6 +1569,9 @@ export async function vaultSetConfidence(vaultRoot, args, access) {
1445
1569
  const agent = requireString(args, "agent", "vault_set_confidence");
1446
1570
  if (!agent.ok)
1447
1571
  return agent;
1572
+ const runId = readRunId(args, "vault_set_confidence");
1573
+ if (!runId.ok)
1574
+ return runId;
1448
1575
  const reason = requireString(args, "reason", "vault_set_confidence");
1449
1576
  if (!reason.ok)
1450
1577
  return reason;
@@ -1503,6 +1630,7 @@ export async function vaultSetConfidence(vaultRoot, args, access) {
1503
1630
  `by ${agent.value} — ${reason.value}`,
1504
1631
  baseVersion: baseVersion.value ?? target.value.contentHash,
1505
1632
  access,
1633
+ ...(runId.value !== undefined ? { runId: runId.value } : {}),
1506
1634
  });
1507
1635
  };
1508
1636
  return retryOnStaleIfDefaulted(baseVersion.value, attemptSetConfidence);
@@ -1530,6 +1658,9 @@ export async function vaultSetTier(vaultRoot, args, access) {
1530
1658
  const agent = requireString(args, "agent", "vault_set_tier");
1531
1659
  if (!agent.ok)
1532
1660
  return agent;
1661
+ const runId = readRunId(args, "vault_set_tier");
1662
+ if (!runId.ok)
1663
+ return runId;
1533
1664
  const reason = requireString(args, "reason", "vault_set_tier");
1534
1665
  if (!reason.ok)
1535
1666
  return reason;
@@ -1590,6 +1721,7 @@ export async function vaultSetTier(vaultRoot, args, access) {
1590
1721
  `by ${agent.value} — ${reason.value}`,
1591
1722
  baseVersion: baseVersion.value ?? target.value.contentHash,
1592
1723
  access,
1724
+ ...(runId.value !== undefined ? { runId: runId.value } : {}),
1593
1725
  });
1594
1726
  };
1595
1727
  return retryOnStaleIfDefaulted(baseVersion.value, attemptSetTier);
@@ -1617,6 +1749,9 @@ export async function vaultSupersede(vaultRoot, args, access) {
1617
1749
  const agent = requireString(args, "agent", "vault_supersede");
1618
1750
  if (!agent.ok)
1619
1751
  return agent;
1752
+ const runId = readRunId(args, "vault_supersede");
1753
+ if (!runId.ok)
1754
+ return runId;
1620
1755
  const baseVersion = readBaseVersion(args, "vault_supersede");
1621
1756
  if (!baseVersion.ok)
1622
1757
  return baseVersion;
@@ -1693,6 +1828,7 @@ export async function vaultSupersede(vaultRoot, args, access) {
1693
1828
  `by ${agent.value}${reason ? ` — ${reason}` : ""}`,
1694
1829
  baseVersion: baseVersion.value,
1695
1830
  access,
1831
+ ...(runId.value !== undefined ? { runId: runId.value } : {}),
1696
1832
  });
1697
1833
  if (!written.ok)
1698
1834
  return written;
@@ -1732,6 +1868,15 @@ export async function vaultMerge(vaultRoot, args, access) {
1732
1868
  const agent = requireString(args, "agent", "vault_merge");
1733
1869
  if (!agent.ok)
1734
1870
  return agent;
1871
+ // Cross-vault leak gate (U3, FIX 1): mirrors vault_write's run_id parsing —
1872
+ // vault_merge writes directly (three files, one commit) rather than going
1873
+ // through performWrite, so it must thread run_id and run the same gate
1874
+ // check itself. Without this, vault_merge was an ungated shared-write path:
1875
+ // a caller-supplied body could land verbatim in a shared/refuse vault with
1876
+ // no leak check at all.
1877
+ const runId = readRunId(args, "vault_merge");
1878
+ if (!runId.ok)
1879
+ return runId;
1735
1880
  const boundary = readBoundary(args, "vault_merge");
1736
1881
  if (!boundary.ok)
1737
1882
  return boundary;
@@ -2080,6 +2225,29 @@ export async function vaultMerge(vaultRoot, args, access) {
2080
2225
  "retry against the current documents"));
2081
2226
  }
2082
2227
  }
2228
+ // Cross-vault leak gate (U3, FIX 1): the same write-time refusal
2229
+ // performWrite runs, mirrored here because vault_merge writes directly.
2230
+ // Runs AFTER the stale-merge revalidation and BEFORE any writeFile call
2231
+ // below — the merge target's `body` is entirely caller-supplied, so an
2232
+ // ungated merge is exactly as dangerous as an ungated vault_write.
2233
+ const gate = await checkLeakGate({
2234
+ visibility: config.value.visibility,
2235
+ leakGate: config.value.leakGate,
2236
+ hasAccessContext: access !== undefined,
2237
+ runId: runId.value,
2238
+ });
2239
+ if (gate.outcome === "refuse") {
2240
+ await recordProvenance(vaultRoot, {
2241
+ tool: "vault_merge",
2242
+ file: targetPath.value,
2243
+ agent: agent.value,
2244
+ ...(access?.user ? { principal: access.user } : {}),
2245
+ ...(runId.value ? { run_id: runId.value } : {}),
2246
+ action: "rejected_leak",
2247
+ reason: gate.reason,
2248
+ });
2249
+ return err(new Error(`${LEAK_GATE_PREFIX} ${targetPath.value}: ${gate.reason}`));
2250
+ }
2083
2251
  // Write all files, then a single git commit. This is NOT crash-atomic on
2084
2252
  // the disk-write phase: like the single-file write path (performWrite), a
2085
2253
  // throw mid-loop — or a commit() failure after the files are on disk —
@@ -2143,6 +2311,12 @@ export async function vaultMerge(vaultRoot, args, access) {
2143
2311
  updated: stampedTarget.updated,
2144
2312
  validation: targetReport,
2145
2313
  indexUpdated: allIndexed,
2314
+ ...(gate.outcome === "warn"
2315
+ ? {
2316
+ leak_warning: `this run read ${gate.count} private source(s) this session before ` +
2317
+ `merging into a shared vault (leak_gate.mode: "warn" — the write landed anyway)`,
2318
+ }
2319
+ : {}),
2146
2320
  });
2147
2321
  }
2148
2322
  catch (e) {
@@ -2593,6 +2767,7 @@ export const writeTools = [
2593
2767
  description: "Vault-relative path of the draft document to promote",
2594
2768
  },
2595
2769
  agent: agentProperty,
2770
+ run_id: runIdProperty,
2596
2771
  base_version: baseVersionProperty,
2597
2772
  },
2598
2773
  required: ["path", "agent"],
@@ -2630,6 +2805,7 @@ export const writeTools = [
2630
2805
  },
2631
2806
  predecessor_valid_until: boundaryProperty,
2632
2807
  agent: agentProperty,
2808
+ run_id: runIdProperty,
2633
2809
  base_version: baseVersionProperty,
2634
2810
  },
2635
2811
  required: ["path", "reason", "agent"],
@@ -2668,6 +2844,7 @@ export const writeTools = [
2668
2844
  description: "Why the confidence is changing (recorded in the commit and provenance)",
2669
2845
  },
2670
2846
  agent: agentProperty,
2847
+ run_id: runIdProperty,
2671
2848
  base_version: baseVersionProperty,
2672
2849
  },
2673
2850
  required: ["path", "confidence", "reason", "agent"],
@@ -2711,6 +2888,7 @@ export const writeTools = [
2711
2888
  description: "Why the tier is changing (recorded in the commit and provenance)",
2712
2889
  },
2713
2890
  agent: agentProperty,
2891
+ run_id: runIdProperty,
2714
2892
  base_version: baseVersionProperty,
2715
2893
  },
2716
2894
  required: ["path", "tier", "reason", "agent"],
@@ -2749,6 +2927,7 @@ export const writeTools = [
2749
2927
  },
2750
2928
  predecessor_valid_until: boundaryProperty,
2751
2929
  agent: agentProperty,
2930
+ run_id: runIdProperty,
2752
2931
  base_version: baseVersionProperty,
2753
2932
  },
2754
2933
  required: ["old_path", "new_path", "agent"],
@@ -2799,6 +2978,7 @@ export const writeTools = [
2799
2978
  },
2800
2979
  predecessor_valid_until: boundaryProperty,
2801
2980
  agent: agentProperty,
2981
+ run_id: runIdProperty,
2802
2982
  },
2803
2983
  required: ["path_a", "path_b", "target_path", "body", "agent"],
2804
2984
  additionalProperties: false,