daftari 3.1.0 → 3.3.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 (152) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/access/rbac.d.ts +1 -0
  3. package/dist/access/rbac.d.ts.map +1 -1
  4. package/dist/access/rbac.js +8 -0
  5. package/dist/access/rbac.js.map +1 -1
  6. package/dist/audit/describes.d.ts +7 -0
  7. package/dist/audit/describes.d.ts.map +1 -1
  8. package/dist/audit/describes.js +48 -16
  9. package/dist/audit/describes.js.map +1 -1
  10. package/dist/backfill/derive.d.ts.map +1 -1
  11. package/dist/backfill/derive.js +2 -0
  12. package/dist/backfill/derive.js.map +1 -1
  13. package/dist/cli.d.ts.map +1 -1
  14. package/dist/cli.js +12 -0
  15. package/dist/cli.js.map +1 -1
  16. package/dist/consolidate/call-budget.d.ts +2 -0
  17. package/dist/consolidate/call-budget.d.ts.map +1 -1
  18. package/dist/consolidate/call-budget.js +8 -1
  19. package/dist/consolidate/call-budget.js.map +1 -1
  20. package/dist/curation/lint-voice.d.ts.map +1 -1
  21. package/dist/curation/lint-voice.js +2 -0
  22. package/dist/curation/lint-voice.js.map +1 -1
  23. package/dist/curation/lint.d.ts +2 -1
  24. package/dist/curation/lint.d.ts.map +1 -1
  25. package/dist/curation/lint.js +57 -0
  26. package/dist/curation/lint.js.map +1 -1
  27. package/dist/curation/staged-actions.d.ts +1 -1
  28. package/dist/curation/staged-actions.d.ts.map +1 -1
  29. package/dist/curation/staged-actions.js +1 -0
  30. package/dist/curation/staged-actions.js.map +1 -1
  31. package/dist/distill/adapters/chat-transcript.d.ts +15 -0
  32. package/dist/distill/adapters/chat-transcript.d.ts.map +1 -0
  33. package/dist/distill/adapters/chat-transcript.js +182 -0
  34. package/dist/distill/adapters/chat-transcript.js.map +1 -0
  35. package/dist/distill/adapters/types.d.ts +39 -0
  36. package/dist/distill/adapters/types.d.ts.map +1 -0
  37. package/dist/distill/adapters/types.js +7 -0
  38. package/dist/distill/adapters/types.js.map +1 -0
  39. package/dist/distill/chunk.d.ts +34 -0
  40. package/dist/distill/chunk.d.ts.map +1 -0
  41. package/dist/distill/chunk.js +46 -0
  42. package/dist/distill/chunk.js.map +1 -0
  43. package/dist/distill/cli.d.ts +13 -0
  44. package/dist/distill/cli.d.ts.map +1 -0
  45. package/dist/distill/cli.js +568 -0
  46. package/dist/distill/cli.js.map +1 -0
  47. package/dist/distill/cost.d.ts +105 -0
  48. package/dist/distill/cost.d.ts.map +1 -0
  49. package/dist/distill/cost.js +168 -0
  50. package/dist/distill/cost.js.map +1 -0
  51. package/dist/distill/extract.d.ts +44 -0
  52. package/dist/distill/extract.d.ts.map +1 -0
  53. package/dist/distill/extract.js +153 -0
  54. package/dist/distill/extract.js.map +1 -0
  55. package/dist/distill/index.d.ts +73 -0
  56. package/dist/distill/index.d.ts.map +1 -0
  57. package/dist/distill/index.js +112 -0
  58. package/dist/distill/index.js.map +1 -0
  59. package/dist/distill/output-fence.d.ts +16 -0
  60. package/dist/distill/output-fence.d.ts.map +1 -0
  61. package/dist/distill/output-fence.js +38 -0
  62. package/dist/distill/output-fence.js.map +1 -0
  63. package/dist/distill/propose.d.ts +95 -0
  64. package/dist/distill/propose.d.ts.map +1 -0
  65. package/dist/distill/propose.js +237 -0
  66. package/dist/distill/propose.js.map +1 -0
  67. package/dist/distill/state.d.ts +92 -0
  68. package/dist/distill/state.d.ts.map +1 -0
  69. package/dist/distill/state.js +198 -0
  70. package/dist/distill/state.js.map +1 -0
  71. package/dist/frontmatter/schema.d.ts.map +1 -1
  72. package/dist/frontmatter/schema.js +1 -0
  73. package/dist/frontmatter/schema.js.map +1 -1
  74. package/dist/frontmatter/types.d.ts +2 -1
  75. package/dist/frontmatter/types.d.ts.map +1 -1
  76. package/dist/frontmatter/types.js +1 -0
  77. package/dist/frontmatter/types.js.map +1 -1
  78. package/dist/interview/transcript.d.ts.map +1 -1
  79. package/dist/interview/transcript.js +1 -0
  80. package/dist/interview/transcript.js.map +1 -1
  81. package/dist/okf/map.d.ts.map +1 -1
  82. package/dist/okf/map.js +1 -0
  83. package/dist/okf/map.js.map +1 -1
  84. package/dist/server.d.ts.map +1 -1
  85. package/dist/server.js +2 -0
  86. package/dist/server.js.map +1 -1
  87. package/dist/sleep/cycle.d.ts +12 -0
  88. package/dist/sleep/cycle.d.ts.map +1 -1
  89. package/dist/sleep/cycle.js +98 -2
  90. package/dist/sleep/cycle.js.map +1 -1
  91. package/dist/sleep/index.d.ts.map +1 -1
  92. package/dist/sleep/index.js +14 -0
  93. package/dist/sleep/index.js.map +1 -1
  94. package/dist/sleep/report.d.ts.map +1 -1
  95. package/dist/sleep/report.js +31 -0
  96. package/dist/sleep/report.js.map +1 -1
  97. package/dist/sleep/run-ledger.d.ts +17 -0
  98. package/dist/sleep/run-ledger.d.ts.map +1 -0
  99. package/dist/sleep/run-ledger.js +119 -0
  100. package/dist/sleep/run-ledger.js.map +1 -0
  101. package/dist/sleep/runs-cli.d.ts +2 -0
  102. package/dist/sleep/runs-cli.d.ts.map +1 -0
  103. package/dist/sleep/runs-cli.js +69 -0
  104. package/dist/sleep/runs-cli.js.map +1 -0
  105. package/dist/tools/anchors.d.ts +12 -0
  106. package/dist/tools/anchors.d.ts.map +1 -0
  107. package/dist/tools/anchors.js +64 -0
  108. package/dist/tools/anchors.js.map +1 -0
  109. package/dist/tools/backlinks.d.ts +28 -0
  110. package/dist/tools/backlinks.d.ts.map +1 -0
  111. package/dist/tools/backlinks.js +207 -0
  112. package/dist/tools/backlinks.js.map +1 -0
  113. package/dist/tools/erase.d.ts +15 -0
  114. package/dist/tools/erase.d.ts.map +1 -0
  115. package/dist/tools/erase.js +127 -0
  116. package/dist/tools/erase.js.map +1 -0
  117. package/dist/tools/pin-mint.d.ts +49 -0
  118. package/dist/tools/pin-mint.d.ts.map +1 -0
  119. package/dist/tools/pin-mint.js +157 -0
  120. package/dist/tools/pin-mint.js.map +1 -0
  121. package/dist/tools/read.d.ts +22 -0
  122. package/dist/tools/read.d.ts.map +1 -1
  123. package/dist/tools/read.js +118 -1
  124. package/dist/tools/read.js.map +1 -1
  125. package/dist/tools/repin.d.ts +29 -0
  126. package/dist/tools/repin.d.ts.map +1 -0
  127. package/dist/tools/repin.js +140 -0
  128. package/dist/tools/repin.js.map +1 -0
  129. package/dist/tools/staged-actions.d.ts.map +1 -1
  130. package/dist/tools/staged-actions.js +102 -6
  131. package/dist/tools/staged-actions.js.map +1 -1
  132. package/dist/tools/write.d.ts +5 -0
  133. package/dist/tools/write.d.ts.map +1 -1
  134. package/dist/tools/write.js +87 -2
  135. package/dist/tools/write.js.map +1 -1
  136. package/dist/utils/config.d.ts +21 -0
  137. package/dist/utils/config.d.ts.map +1 -1
  138. package/dist/utils/config.js +116 -0
  139. package/dist/utils/config.js.map +1 -1
  140. package/dist/utils/git-erase.d.ts +35 -0
  141. package/dist/utils/git-erase.d.ts.map +1 -0
  142. package/dist/utils/git-erase.js +179 -0
  143. package/dist/utils/git-erase.js.map +1 -0
  144. package/dist/utils/git.d.ts +3 -0
  145. package/dist/utils/git.d.ts.map +1 -1
  146. package/dist/utils/git.js +29 -0
  147. package/dist/utils/git.js.map +1 -1
  148. package/dist/utils/vault-gitignore.d.ts +1 -1
  149. package/dist/utils/vault-gitignore.d.ts.map +1 -1
  150. package/dist/utils/vault-gitignore.js +5 -0
  151. package/dist/utils/vault-gitignore.js.map +1 -1
  152. package/package.json +1 -1
@@ -0,0 +1,112 @@
1
+ // src/distill/index.ts
2
+ //
3
+ // `distill` module — compile-on-ingest pipeline orchestrator.
4
+ //
5
+ // Pipeline shape:
6
+ //
7
+ // distill(raw, adapter)
8
+ // -> parse : adapter.parse(raw) → NormalizedMessage[] [U1 DONE]
9
+ // -> chunk : split messages into chunks → Chunk[] [U3 DONE]
10
+ // -> extract : LLM-driven claim extraction → RawClaim[] [U3 DONE — extractClaims, CLI-wired in U7]
11
+ // -> propose : map to vault proposals → VaultProposal[] [U4 TODO]
12
+ //
13
+ // The orchestrator stays a thin synchronous stub: it parses and chunks, but
14
+ // extraction (needs an LLM client + call budget) and proposals return empty
15
+ // arrays until the CLI orchestration wires them.
16
+ //
17
+ // The async CLI front door (runDistill, U7) and its argv parsing live in
18
+ // ./cli.ts (split out in U9); this module owns the pipeline stub plus the
19
+ // config/client resolver it shares with the CLI.
20
+ export { ChatTranscriptAdapter } from "./adapters/chat-transcript.js";
21
+ export { CHUNK_WINDOW, chunkMessages } from "./chunk.js";
22
+ export { extractClaims, } from "./extract.js";
23
+ // ---------------------------------------------------------------------------
24
+ // Imports
25
+ // ---------------------------------------------------------------------------
26
+ import { createAnthropicClient } from "../eval/llm.js";
27
+ import { createOpenRouterClient, resolveTransport, } from "../eval/llm-openrouter.js";
28
+ import { err, ok } from "../frontmatter/types.js";
29
+ import { loadConfig } from "../utils/config.js";
30
+ import { ChatTranscriptAdapter } from "./adapters/chat-transcript.js";
31
+ import { chunkMessages } from "./chunk.js";
32
+ // ---------------------------------------------------------------------------
33
+ // Adapter registry
34
+ // ---------------------------------------------------------------------------
35
+ /** Built-in adapter registry, keyed by sourceId(). */
36
+ export const ADAPTER_REGISTRY = {
37
+ "chat-transcript": new ChatTranscriptAdapter(),
38
+ };
39
+ // ---------------------------------------------------------------------------
40
+ // Shared error messages (m1 — deduplicate refuse string)
41
+ // ---------------------------------------------------------------------------
42
+ export const DISTILL_NOT_CONFIGURED_MSG = "distill not configured: add a 'distill:' block to .daftari/config.yaml " +
43
+ "with at least 'model' set before running the distill pipeline";
44
+ // Transport-aware LLM construction — mirrors the pattern in src/consolidate
45
+ // and src/sleep: check the API key BEFORE calling the constructor so a
46
+ // missing key produces a clear error rather than the client's internal throw.
47
+ function constructLlm(transport) {
48
+ const keyVar = transport === "openrouter" ? "OPENROUTER_API_KEY" : "ANTHROPIC_API_KEY";
49
+ if (!process.env[keyVar]) {
50
+ return err(new Error(`${keyVar} env var is required (transport: ${transport})`));
51
+ }
52
+ try {
53
+ return ok(transport === "openrouter" ? createOpenRouterClient() : createAnthropicClient());
54
+ }
55
+ catch (e) {
56
+ return err(e instanceof Error ? e : new Error(String(e)));
57
+ }
58
+ }
59
+ /**
60
+ * Resolves the distill config from `vaultRoot` and constructs an LLM client.
61
+ *
62
+ * Refuses to run (returns `err`) when:
63
+ * - The config cannot be loaded (malformed yaml, I/O error).
64
+ * - The `distill:` block is absent — no silent default spend.
65
+ * - The resolved transport's API key is missing — fail-fast before any
66
+ * network call (mirrors the consolidate/sleep posture).
67
+ *
68
+ * Transport defaults to `anthropic`; the caller may override via the
69
+ * `DAFTARI_LLM_TRANSPORT` env var or by passing an explicit value.
70
+ *
71
+ * @param vaultRoot - Absolute path to the vault root.
72
+ * @param transport - Optional explicit transport override (CLI flag value).
73
+ */
74
+ export function resolveDistillClient(vaultRoot, transport) {
75
+ const cfg = loadConfig(vaultRoot);
76
+ if (!cfg.ok)
77
+ return err(cfg.error);
78
+ if (!cfg.value.distill) {
79
+ return err(new Error(DISTILL_NOT_CONFIGURED_MSG));
80
+ }
81
+ const distillCfg = cfg.value.distill;
82
+ const transportRes = resolveTransport(transport);
83
+ if (!transportRes.ok)
84
+ return err(transportRes.error);
85
+ const llmRes = constructLlm(transportRes.value);
86
+ if (!llmRes.ok)
87
+ return err(llmRes.error);
88
+ return ok({ client: llmRes.value, config: distillCfg, transport: transportRes.value });
89
+ }
90
+ /**
91
+ * Entry point for the distill pipeline.
92
+ *
93
+ * Currently implements only the parse stage (U1). Chunk, extract, and propose
94
+ * stages are stubs that return empty arrays — later units will replace them
95
+ * in-place without changing this function's signature.
96
+ *
97
+ * @param raw - Raw content of the source file (e.g. a chat export .txt).
98
+ * @param adapter - SourceAdapter instance to use for parsing.
99
+ */
100
+ export function distill(raw, adapter) {
101
+ // U1: parse
102
+ const messages = adapter.parse(raw);
103
+ // U3: chunk messages into turn windows
104
+ const chunks = chunkMessages(messages);
105
+ // U3: extraction lives in extractClaims (needs an LLM client + budget) —
106
+ // the CLI (U7) wires it; this synchronous stub keeps claims empty.
107
+ const claims = [];
108
+ // U4 TODO: map raw claims to vault write proposals
109
+ const proposals = [];
110
+ return { messages, chunks, claims, proposals };
111
+ }
112
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/distill/index.ts"],"names":[],"mappings":"AAAA,uBAAuB;AACvB,EAAE;AACF,8DAA8D;AAC9D,EAAE;AACF,kBAAkB;AAClB,EAAE;AACF,0BAA0B;AAC1B,gFAAgF;AAChF,iFAAiF;AACjF,kHAAkH;AAClH,iFAAiF;AACjF,EAAE;AACF,4EAA4E;AAC5E,4EAA4E;AAC5E,iDAAiD;AACjD,EAAE;AACF,yEAAyE;AACzE,0EAA0E;AAC1E,iDAAiD;AAGjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAEtE,OAAO,EAAE,YAAY,EAAc,aAAa,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,EAIL,aAAa,GACd,MAAM,cAAc,CAAC;AAEtB,8EAA8E;AAC9E,UAAU;AACV,8EAA8E;AAE9E,OAAO,EAAE,qBAAqB,EAAkB,MAAM,gBAAgB,CAAC;AACvE,OAAO,EACL,sBAAsB,EAEtB,gBAAgB,GACjB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,EAAE,EAAe,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAsB,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAEtE,OAAO,EAAc,aAAa,EAAE,MAAM,YAAY,CAAC;AAGvD,8EAA8E;AAC9E,mBAAmB;AACnB,8EAA8E;AAE9E,sDAAsD;AACtD,MAAM,CAAC,MAAM,gBAAgB,GAAkC;IAC7D,iBAAiB,EAAE,IAAI,qBAAqB,EAAE;CAC/C,CAAC;AAkBF,8EAA8E;AAC9E,yDAAyD;AACzD,8EAA8E;AAE9E,MAAM,CAA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IAAI,CAAC,YAAY,CAAC,EAAE;QAAE,OAAO,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAErD,MAAM,MAAM,GAAG,YAAY,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAChD,IAAI,CAAC,MAAM,CAAC,EAAE;QAAE,OAAO,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAEzC,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,CAAC,KAAK,EAAE,CAAC,CAAC;AACzF,CAAC;AA+BD;;;;;;;;;GASG;AACH,MAAM,UAAU,OAAO,CAAC,GAAW,EAAE,OAAsB;IACzD,YAAY;IACZ,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAEpC,uCAAuC;IACvC,MAAM,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IAEvC,yEAAyE;IACzE,mEAAmE;IACnE,MAAM,MAAM,GAAe,EAAE,CAAC;IAE9B,mDAAmD;IACnD,MAAM,SAAS,GAAoB,EAAE,CAAC;IAEtC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;AACjD,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { type Result } from "../frontmatter/types.js";
2
+ /**
3
+ * Refuse a distill output landing that would write raw/source material.
4
+ *
5
+ * Two invariants:
6
+ * 1. The target path's top-level segment must not be `raw/` — that namespace
7
+ * is reserved for `daftari import`'s verbatim ingest. (A non-leading
8
+ * `raw` segment or a slug that merely contains "raw" is fine.)
9
+ * 2. The frontmatter must not be `tier: source` — that is the raw-ingested
10
+ * marker, immutable to every writer (src/tools/write.ts). Distill output
11
+ * is compiled, never source.
12
+ *
13
+ * Returns `ok` when the landing is safe, `err` (to skip the proposal) otherwise.
14
+ */
15
+ export declare function refuseRawDistillOutput(targetPath: string, frontmatter: Record<string, unknown>): Result<void, Error>;
16
+ //# sourceMappingURL=output-fence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output-fence.d.ts","sourceRoot":"","sources":["../../src/distill/output-fence.ts"],"names":[],"mappings":"AAcA,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAE/D;;;;;;;;;;;;GAYG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACnC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAarB"}
@@ -0,0 +1,38 @@
1
+ // src/distill/output-fence.ts
2
+ //
3
+ // U11 — distill-and-discard fence (R8). Distill COMPILES conversation into
4
+ // graded belief; it must never emit RAW source material. This is the emit-time
5
+ // guard that keeps that invariant enforceable: it refuses any proposal whose
6
+ // landing would put it in the raw/source tier that `daftari import` and the
7
+ // interview transcript path own.
8
+ //
9
+ // The emitter (propose.ts) hardcodes a `provenance: synthesized`, distill-
10
+ // collection proposal with no tier, so in normal operation this never fires.
11
+ // It exists as defense-in-depth: a bad path override (U5's update-in-place
12
+ // threads caller-supplied paths) or a future refactor must fail LOUD rather
13
+ // than silently poison the import-reserved namespace.
14
+ import { err, ok } from "../frontmatter/types.js";
15
+ /**
16
+ * Refuse a distill output landing that would write raw/source material.
17
+ *
18
+ * Two invariants:
19
+ * 1. The target path's top-level segment must not be `raw/` — that namespace
20
+ * is reserved for `daftari import`'s verbatim ingest. (A non-leading
21
+ * `raw` segment or a slug that merely contains "raw" is fine.)
22
+ * 2. The frontmatter must not be `tier: source` — that is the raw-ingested
23
+ * marker, immutable to every writer (src/tools/write.ts). Distill output
24
+ * is compiled, never source.
25
+ *
26
+ * Returns `ok` when the landing is safe, `err` (to skip the proposal) otherwise.
27
+ */
28
+ export function refuseRawDistillOutput(targetPath, frontmatter) {
29
+ const leadingSegment = targetPath.replace(/\\/g, "/").split("/")[0];
30
+ if (leadingSegment === "raw") {
31
+ return err(new Error(`distill output path is under the import-reserved raw/ namespace: ${targetPath}`));
32
+ }
33
+ if (frontmatter.tier === "source") {
34
+ return err(new Error(`distill output must not be tier: source (raw-ingested material): ${targetPath}`));
35
+ }
36
+ return ok(undefined);
37
+ }
38
+ //# sourceMappingURL=output-fence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output-fence.js","sourceRoot":"","sources":["../../src/distill/output-fence.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,EAAE;AACF,2EAA2E;AAC3E,+EAA+E;AAC/E,6EAA6E;AAC7E,4EAA4E;AAC5E,iCAAiC;AACjC,EAAE;AACF,2EAA2E;AAC3E,6EAA6E;AAC7E,2EAA2E;AAC3E,4EAA4E;AAC5E,sDAAsD;AAEtD,OAAO,EAAE,GAAG,EAAE,EAAE,EAAe,MAAM,yBAAyB,CAAC;AAE/D;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,sBAAsB,CACpC,UAAkB,EAClB,WAAoC;IAEpC,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACpE,IAAI,cAAc,KAAK,KAAK,EAAE,CAAC;QAC7B,OAAO,GAAG,CACR,IAAI,KAAK,CAAC,oEAAoE,UAAU,EAAE,CAAC,CAC5F,CAAC;IACJ,CAAC;IACD,IAAI,WAAW,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAClC,OAAO,GAAG,CACR,IAAI,KAAK,CAAC,oEAAoE,UAAU,EAAE,CAAC,CAC5F,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,CAAC,SAAS,CAAC,CAAC;AACvB,CAAC"}
@@ -0,0 +1,95 @@
1
+ import type { AccessContext } from "../access/rbac.js";
2
+ import { type StageOutcome } from "../curation/staged-actions.js";
3
+ import type { ExtractedClaim } from "./extract.js";
4
+ /** Target collection for all distill proposals. */
5
+ export declare const DISTILL_COLLECTION = "distill";
6
+ /** The proposing agent identity, recorded on every proposal. */
7
+ export declare const DISTILL_AGENT = "agent:distill";
8
+ /**
9
+ * Maximum number of overlap paths attached to a proposal rationale (U8).
10
+ * Small and bounded: the hint is advisory context for the ratifier, not a
11
+ * full search result set.
12
+ */
13
+ export declare const OVERLAP_HINT_TOP_K = 3;
14
+ /**
15
+ * A function that, given a claim statement, returns the vault-relative paths
16
+ * of documents that are likely to overlap with the claim (no LLM, no tension
17
+ * scan — pure local index query). An empty return means no overlaps found.
18
+ *
19
+ * Errors thrown by this function are caught by proposeAllClaims and cause a
20
+ * graceful no-hint degradation (the claim still stages).
21
+ */
22
+ export type OverlapSearchFn = (statement: string) => Promise<string[]>;
23
+ /**
24
+ * Build an overlap-search function backed by the vault's hybrid search index.
25
+ * The CLI wires this into proposeAllClaims; tests inject stubs instead.
26
+ *
27
+ * Design note: vaultSearch is query-based (statement → top-K existing docs).
28
+ * vaultSearchRelated is path-based and requires an already-indexed document.
29
+ * Distilled claims do not exist in the index yet, so vaultSearch is the
30
+ * correct primitive — confirmed deviation from the plan's `vault_search_related`
31
+ * wording.
32
+ *
33
+ * @param vaultRoot Absolute path to the vault root.
34
+ * @param access Optional RBAC context forwarded to vaultSearch.
35
+ */
36
+ export declare function makeOverlapHinter(vaultRoot: string, access?: AccessContext): OverlapSearchFn;
37
+ /** The two distill identifiers, deliberately distinct (U5). */
38
+ export interface DistillIds {
39
+ /**
40
+ * Stable identity of the ingested source (e.g. one chat export). The
41
+ * idempotency key: the durable `sources` ref and the run-group folder are
42
+ * derived from it so a re-distill joins on the same identifier.
43
+ */
44
+ sourceId: string;
45
+ /** Per-run trace stamp (StageActionInput.runId). Never the idempotency key. */
46
+ runId: string;
47
+ /**
48
+ * Optional `created`/`updated` date (YYYY-MM-DD) for the emitted proposals.
49
+ * Defaults to today. Set it when the source has a meaningful as-of date that
50
+ * differs from the run date — e.g. the recall-bench distill arm stamps each
51
+ * benchmark day's own date so temporal signals are correct and landed content
52
+ * is date-stable across runs.
53
+ */
54
+ asOf?: string;
55
+ }
56
+ /** Per-claim staging outcome (the StageOutcome from the queue, or an error). */
57
+ export interface ClaimProposalResult extends StageOutcome {
58
+ claim_key: string;
59
+ targetPath: string;
60
+ }
61
+ /** Aggregate result for a proposeAllClaims call. */
62
+ export interface ProposeOutcome {
63
+ /** Number of claims successfully staged. */
64
+ proposed: number;
65
+ /** Per-claim results (one entry per claim, success or skipped). */
66
+ results: ClaimProposalResult[];
67
+ /** Per-claim errors (claim_key + message). Claims that error are not in results. */
68
+ errors: Array<{
69
+ claim_key: string;
70
+ error: string;
71
+ }>;
72
+ }
73
+ /**
74
+ * Emit each claim as a `write` staged-action proposal at draft/low/synthesized.
75
+ * Routes through `stageActionWithConflictCheck` so inter-proposal conflicts
76
+ * are detected and logged. Never calls performWrite.
77
+ *
78
+ * @param vaultRoot Absolute path to the vault root.
79
+ * @param claims Extracted claims from the extraction stage (U3).
80
+ * @param ids Stable source-id (idempotency key) + per-run trace id.
81
+ * @param pathOverrides claim_key -> target path, for U5 update-in-place
82
+ * proposals that must land on an already-landed path.
83
+ * @param overlapSearch Optional injected function (U8/R5): given a claim
84
+ * statement, returns vault-relative paths of likely
85
+ * overlapping documents. When provided, the top-K paths
86
+ * are appended to the proposal rationale so the ratifier
87
+ * can see possible collisions. No LLM or tension-scan
88
+ * runs here — the function is a pure local index query.
89
+ * When absent, rationale is the statement alone (the
90
+ * original U4 behaviour, fully backward-compatible).
91
+ * Errors thrown by overlapSearch degrade to no-hint;
92
+ * the claim still stages.
93
+ */
94
+ export declare function proposeAllClaims(vaultRoot: string, claims: ExtractedClaim[], ids: DistillIds, pathOverrides?: Record<string, string>, overlapSearch?: OverlapSearchFn): Promise<ProposeOutcome>;
95
+ //# sourceMappingURL=propose.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"propose.d.ts","sourceRoot":"","sources":["../../src/distill/propose.ts"],"names":[],"mappings":"AAqBA,OAA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AE,cAAc,EAAE,EACxB,GAAG,EAAE,UAAU,EACf,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACtC,aAAa,CAAC,EAAE,eAAe,GAC9B,OAAO,CAAC,cAAc,CAAC,CA0EzB"}
@@ -0,0 +1,237 @@
1
+ // src/distill/propose.ts
2
+ //
3
+ // Proposal emitter (U4). Maps each ExtractedClaim to a `vault_stage_action`
4
+ // "write" proposal at status:draft / confidence:low / provenance:synthesized,
5
+ // stamped with the distill run_id. Routes through stageActionWithConflictCheck
6
+ // so inter-proposal conflicts and the staged-action queue's built-in guards
7
+ // fire without any new gate.
8
+ //
9
+ // Design note on the tier-0 canonical gate:
10
+ // The tier-0 canonical gate fires at RATIFY time (vault_ratify) when a
11
+ // staged write proposal declares status:canonical in its merged post-state.
12
+ // It does NOT fire at stage time. The emitter enforces R3 by construction:
13
+ // every proposal is hardcoded to status:draft. The human gate (vault_ratify)
14
+ // is the existing boundary — no new gate is introduced here.
15
+ //
16
+ // Collection:
17
+ // Defaulting to "distill". This is a named constant (DISTILL_COLLECTION)
18
+ // so a future config hook can override it without touching call sites.
19
+ import { join } from "node:path";
20
+ import { dump } from "js-yaml";
21
+ import { stageActionWithConflictCheck } from "../curation/staged-actions.js";
22
+ import { slugifyKey } from "../import/langgraph-store.js";
23
+ import { vaultSearch } from "../tools/search.js";
24
+ import { refuseRawDistillOutput } from "./output-fence.js";
25
+ // ---------------------------------------------------------------------------
26
+ // Public constants
27
+ // ---------------------------------------------------------------------------
28
+ /** Target collection for all distill proposals. */
29
+ export const DISTILL_COLLECTION = "distill";
30
+ /** The proposing agent identity, recorded on every proposal. */
31
+ export const DISTILL_AGENT = "agent:distill";
32
+ /**
33
+ * Maximum number of overlap paths attached to a proposal rationale (U8).
34
+ * Small and bounded: the hint is advisory context for the ratifier, not a
35
+ * full search result set.
36
+ */
37
+ export const OVERLAP_HINT_TOP_K = 3;
38
+ /**
39
+ * Build an overlap-search function backed by the vault's hybrid search index.
40
+ * The CLI wires this into proposeAllClaims; tests inject stubs instead.
41
+ *
42
+ * Design note: vaultSearch is query-based (statement → top-K existing docs).
43
+ * vaultSearchRelated is path-based and requires an already-indexed document.
44
+ * Distilled claims do not exist in the index yet, so vaultSearch is the
45
+ * correct primitive — confirmed deviation from the plan's `vault_search_related`
46
+ * wording.
47
+ *
48
+ * @param vaultRoot Absolute path to the vault root.
49
+ * @param access Optional RBAC context forwarded to vaultSearch.
50
+ */
51
+ export function makeOverlapHinter(vaultRoot, access) {
52
+ return async (statement) => {
53
+ const result = await vaultSearch(vaultRoot, { query: statement, limit: OVERLAP_HINT_TOP_K }, access);
54
+ if (!result.ok)
55
+ return [];
56
+ return result.value.hits.slice(0, OVERLAP_HINT_TOP_K).map((h) => h.path);
57
+ };
58
+ }
59
+ // ---------------------------------------------------------------------------
60
+ // Path derivation
61
+ // ---------------------------------------------------------------------------
62
+ // Pull the 8-char hash suffix already embedded at the end of the claim_key
63
+ // (format: `<anchor>:<slug>-<hash8>`). If the format is unexpected, fall
64
+ // back to a hash of the full key so paths remain collision-resistant.
65
+ function hash8FromClaimKey(claimKey) {
66
+ const lastDash = claimKey.lastIndexOf("-");
67
+ if (lastDash !== -1 && claimKey.length - lastDash - 1 === 8) {
68
+ return claimKey.slice(lastDash + 1);
69
+ }
70
+ // Fallback: take last 8 chars of the key itself. This branch is only
71
+ // reachable if claim_key deviates from U3's documented
72
+ // `<anchor>:<slug>-<sha256[:8]>` format — intentionally best-effort.
73
+ return claimKey.slice(-8).replace(/[^a-z0-9]/g, "x");
74
+ }
75
+ // Derive the vault-relative path for a claim. Mirrors the langgraph adapter:
76
+ // <collection>/<source_group>/<slug(title)>--<hash8>.md
77
+ // The source_group is the stable source-id slugified (keeps a source's claims
78
+ // co-located AND stable across runs — U5's re-distill join relies on it);
79
+ // falls back to "claims" if the source-id is empty or non-slug-friendly.
80
+ //
81
+ // Path-traversal safety: slugifyKey strips everything except [a-z0-9-], so
82
+ // none of the join components can contain ".." or path separators — the
83
+ // sanitizer is the invariant; don't remove it in a future refactor.
84
+ function derivePath(claim, sourceId) {
85
+ const title = claim.proposed_frontmatter.title;
86
+ const hash8 = hash8FromClaimKey(claim.claim_key);
87
+ const sourceGroup = slugifyKey(sourceId) || "claims";
88
+ // Fall back to the claim_key slug when the title is empty. slugifyKey
89
+ // returns the sentinel "memory" for an empty/whitespace-only string — a
90
+ // truthy value — so we cannot use || on its return value. Guard on the
91
+ // raw title instead. Avoids every empty-title claim collapsing to
92
+ // "memory", which makes U5's targetPath-based upsert join harder to
93
+ // reason about and produces semantically useless names.
94
+ const titleSlug = title.trim() ? slugifyKey(title) : slugifyKey(claim.claim_key);
95
+ return join(DISTILL_COLLECTION, sourceGroup, `${titleSlug}--${hash8}.md`);
96
+ }
97
+ // ---------------------------------------------------------------------------
98
+ // Note assembly (mirrors langgraph-store.ts deriveNotes)
99
+ // ---------------------------------------------------------------------------
100
+ function assembleBody(claim, frontmatter, ids) {
101
+ return [
102
+ "---",
103
+ dump(frontmatter).trimEnd(),
104
+ "---",
105
+ "",
106
+ claim.statement.trim(),
107
+ "",
108
+ "## Provenance",
109
+ "",
110
+ `- **Pipeline:** distill (compile-on-ingest)`,
111
+ `- **Claim key:** \`${claim.claim_key}\``,
112
+ `- **Run id:** \`${ids.runId}\``,
113
+ `- **Source ref:** \`distill:${ids.sourceId}#${claim.claim_key}\``,
114
+ "",
115
+ ].join("\n");
116
+ }
117
+ // ---------------------------------------------------------------------------
118
+ // Rationale builder (U8)
119
+ // ---------------------------------------------------------------------------
120
+ // Build the proposal rationale, optionally appending an overlap-hint line.
121
+ // The statement is always the lead so stageActionWithConflictCheck's
122
+ // firstSentence() extraction and the ratifier's first read both land on the
123
+ // claim itself. A non-empty overlap list appends a "Possible overlaps: ..."
124
+ // line separated by a blank line. An empty list or a throwing overlapSearch
125
+ // produces the statement alone — no "Possible overlaps:" line at all.
126
+ async function buildRationale(statement, overlapSearch) {
127
+ if (!overlapSearch)
128
+ return statement;
129
+ let raw;
130
+ try {
131
+ raw = await overlapSearch(statement);
132
+ }
133
+ catch {
134
+ // Degrade to no-hint: a search failure must never block staging.
135
+ return statement;
136
+ }
137
+ // Surface non-array / unexpected returns rather than silently swallowing them.
138
+ if (!raw || raw.length === 0)
139
+ return statement;
140
+ const paths = raw.slice(0, OVERLAP_HINT_TOP_K);
141
+ // Sanitize newlines in paths — a path containing \r or \n would break the
142
+ // "statement is the lead" invariant that stageActionWithConflictCheck's
143
+ // firstSentence() relies on.
144
+ const safePaths = paths.map((p) => p.replace(/[\r\n]+/g, " "));
145
+ return `${statement}\n\nPossible overlaps: ${safePaths.join(", ")}`;
146
+ }
147
+ // ---------------------------------------------------------------------------
148
+ // Core emitter
149
+ // ---------------------------------------------------------------------------
150
+ /**
151
+ * Emit each claim as a `write` staged-action proposal at draft/low/synthesized.
152
+ * Routes through `stageActionWithConflictCheck` so inter-proposal conflicts
153
+ * are detected and logged. Never calls performWrite.
154
+ *
155
+ * @param vaultRoot Absolute path to the vault root.
156
+ * @param claims Extracted claims from the extraction stage (U3).
157
+ * @param ids Stable source-id (idempotency key) + per-run trace id.
158
+ * @param pathOverrides claim_key -> target path, for U5 update-in-place
159
+ * proposals that must land on an already-landed path.
160
+ * @param overlapSearch Optional injected function (U8/R5): given a claim
161
+ * statement, returns vault-relative paths of likely
162
+ * overlapping documents. When provided, the top-K paths
163
+ * are appended to the proposal rationale so the ratifier
164
+ * can see possible collisions. No LLM or tension-scan
165
+ * runs here — the function is a pure local index query.
166
+ * When absent, rationale is the statement alone (the
167
+ * original U4 behaviour, fully backward-compatible).
168
+ * Errors thrown by overlapSearch degrade to no-hint;
169
+ * the claim still stages.
170
+ */
171
+ export async function proposeAllClaims(vaultRoot, claims, ids, pathOverrides, overlapSearch) {
172
+ const results = [];
173
+ const errors = [];
174
+ for (const claim of claims) {
175
+ const targetPath = pathOverrides?.[claim.claim_key] ?? derivePath(claim, ids.sourceId);
176
+ // R3: frontmatter is hardcoded to draft/low/synthesized. No caller can
177
+ // override these — the emitter owns the invariant.
178
+ const frontmatter = {
179
+ title: claim.proposed_frontmatter.title,
180
+ // Required date field: stamp the as-of date (falling back to the emit
181
+ // date) so the proposal is a complete, ratifiable document (vault_ratify
182
+ // validates the full frontmatter before it lands the write — a proposal
183
+ // missing `created` cannot be approved).
184
+ created: ids.asOf ?? new Date().toISOString().slice(0, 10),
185
+ domain: "accumulation",
186
+ collection: DISTILL_COLLECTION,
187
+ status: "draft",
188
+ confidence: "low",
189
+ provenance: "synthesized",
190
+ // proposed_by (vault document field) and proposedBy on the StageActionInput
191
+ // below (the actor recorded on the JSONL staged-action record) are both set
192
+ // to DISTILL_AGENT. They are DIFFERENT concepts — document metadata vs
193
+ // queue actor — intentionally kept in sync. Update both together if either
194
+ // ever needs to change.
195
+ proposed_by: DISTILL_AGENT,
196
+ // Keyed on the STABLE source-id (U5/R4) — a re-distill of the same
197
+ // source in a later run must produce the same ref. Never the run-id.
198
+ sources: [`distill:${ids.sourceId}#${claim.claim_key}`],
199
+ superseded_by: null,
200
+ ttl_days: null,
201
+ };
202
+ // U11/R8: distill-and-discard fence. Refuse before staging any landing that
203
+ // would write raw/source material (top-level raw/ path or tier: source) —
204
+ // defense-in-depth against a bad path override or future refactor.
205
+ const fence = refuseRawDistillOutput(targetPath, frontmatter);
206
+ if (!fence.ok) {
207
+ errors.push({ claim_key: claim.claim_key, error: fence.error.message });
208
+ continue;
209
+ }
210
+ const body = assembleBody(claim, frontmatter, ids);
211
+ // U8/R5: overlap-hint. Attach top-K likely-collision neighbor paths to
212
+ // the rationale so the ratifier can see possible overlaps at a glance.
213
+ // No LLM or tension-scan runs here — overlapSearch is a pure local index
214
+ // query injected by the caller. A missing or throwing overlapSearch
215
+ // degrades to no-hint (claim still stages, original rationale preserved).
216
+ const rationale = await buildRationale(claim.statement, overlapSearch);
217
+ const staged = await stageActionWithConflictCheck(vaultRoot, {
218
+ actionType: "write",
219
+ targetPath,
220
+ proposedBy: DISTILL_AGENT,
221
+ rationale,
222
+ proposedDiff: { frontmatter, body },
223
+ runId: ids.runId,
224
+ });
225
+ if (!staged.ok) {
226
+ errors.push({ claim_key: claim.claim_key, error: staged.error.message });
227
+ continue;
228
+ }
229
+ results.push({
230
+ ...staged.value,
231
+ claim_key: claim.claim_key,
232
+ targetPath,
233
+ });
234
+ }
235
+ return { proposed: results.length, results, errors };
236
+ }
237
+ //# sourceMappingURL=propose.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"propose.js","sourceRoot":"","sources":["../../src/distill/propose.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,+EAA+E;AAC/E,4EAA4E;AAC5E,6BAA6B;AAC7B,EAAE;AACF,4CAA4C;AAC5C,yEAAyE;AACzE,8EAA8E;AAC9E,6EAA6E;AAC7E,+EAA+E;AAC/E,+DAA+D;AAC/D,EAAE;AACF,cAAc;AACd,2EAA2E;AAC3E,yEAAyE;AAEzE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,OAAO,EAAqB,4BAA4B,EAAE,MAAM,+BAA+B,CAAC;AAChG,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAE3D,8EAA8E;AAC9E,mBAAmB;AACnB,8EAA8E;AAE9E,mDAAmD;AACnD,MAAM,CAAC,MAAM,kBAAkB,GAAG,SAAS,CAAC;AAE5C,gEAAgE;AAChE,MAAM,CAAC,MAAM,aAAa,GAAG,eAAe,CAAC;AAE7C;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAgBpC;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,iBAAiB,CAAC,SAAiB,EAAE,MAAsB;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GAAG,CAAC,KAAK;SACjB,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;YACf,MAAM,CAAC,IAAI,CAAC,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YACzE,SAAS;QACX,CAAC;QAED,OAAO,CAAC,IAAI,CAAC;YACX,GAAG,MAAM,CAAC,KAAK;YACf,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,UAAU;SACX,CAAC,CAAC;IACL,CAAC;IAED,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACvD,CAAC"}
@@ -0,0 +1,92 @@
1
+ import { type Result } from "../frontmatter/types.js";
2
+ import type { ExtractedClaim } from "./extract.js";
3
+ import { type OverlapSearchFn, type ProposeOutcome } from "./propose.js";
4
+ export interface DistillSourceState {
5
+ /** Hash of the normalized source content at last emit. */
6
+ content_hash: string;
7
+ /** claim_key -> landed vault-relative path, recorded at ratify time only. */
8
+ claims: Record<string, string>;
9
+ }
10
+ export interface DistillState {
11
+ /** Per stable source-id (the identity of the input, never a run-id). */
12
+ sources: Record<string, DistillSourceState>;
13
+ }
14
+ export declare function distillStatePath(vaultRoot: string): string;
15
+ /**
16
+ * Same sha256-sliced shape as consolidate's docContentHash.
17
+ *
18
+ * Line endings are normalized (CRLF and bare CR → LF) before hashing so that
19
+ * the same logical file produces the same hash regardless of OS line endings.
20
+ * The "normalized" comment in `content_hash`'s field declaration is therefore
21
+ * accurate: a Windows checkout and a macOS checkout of the same file hash
22
+ * identically and do not trigger a redundant re-distill.
23
+ */
24
+ export declare function sourceContentHash(content: string): string;
25
+ export declare function readDistillState(vaultRoot: string): DistillState;
26
+ export declare function writeDistillState(vaultRoot: string, state: DistillState): Result<void, Error>;
27
+ /**
28
+ * Record that a claim's proposal has LANDED (ratified) at `landedPath`.
29
+ * The mark-after-land hook: called by the ratify path, never at emit time.
30
+ * An update-in-place lands at a path an older claim_key already maps to —
31
+ * that older entry is removed so the map keeps one live key per path.
32
+ */
33
+ export declare function recordLandedClaim(vaultRoot: string, sourceId: string, claimKey: string, landedPath: string): Result<void, Error>;
34
+ export type ClaimAction = {
35
+ kind: "skip";
36
+ claim: ExtractedClaim;
37
+ landedPath: string;
38
+ } | {
39
+ kind: "update";
40
+ claim: ExtractedClaim;
41
+ landedPath: string;
42
+ } | {
43
+ kind: "new";
44
+ claim: ExtractedClaim;
45
+ };
46
+ /** Join new claims against the landed map. Pure — no I/O, no LLM. */
47
+ export declare function joinClaims(prior: DistillSourceState | undefined, claims: ExtractedClaim[]): ClaimAction[];
48
+ export interface DistillUpsertInput {
49
+ /** Stable identity of the ingested source (the idempotency key). */
50
+ sourceId: string;
51
+ /** Normalized source content — hashed for the whole-source fast path. */
52
+ sourceContent: string;
53
+ claims: ExtractedClaim[];
54
+ /** Per-run trace stamp (StageActionInput.runId). Never the idempotency key. */
55
+ runId: string;
56
+ /**
57
+ * Optional overlap-search function (U8/R5): given a claim statement, returns
58
+ * vault-relative paths of likely overlapping documents. When provided, the
59
+ * top-K paths are appended to each proposal's rationale so the ratifier can
60
+ * see possible collisions. When absent, rationale is the statement alone
61
+ * (original U4 behaviour, fully backward-compatible). All existing 4-field
62
+ * callers remain valid — this field is optional.
63
+ */
64
+ overlapSearch?: OverlapSearchFn;
65
+ }
66
+ export interface DistillUpsertOutcome {
67
+ /** True when the source content hash is unchanged — nothing was staged. */
68
+ noop: boolean;
69
+ /** claim_keys already landed unchanged — not re-emitted. */
70
+ skipped: string[];
71
+ /** Edited claims staged as update-in-place writes to their landed path. */
72
+ updated: Array<{
73
+ claim_key: string;
74
+ landedPath: string;
75
+ }>;
76
+ /** claim_keys staged as new writes. */
77
+ created: string[];
78
+ /** The emitter outcome for the updated+created claims; null on no-op. */
79
+ propose: ProposeOutcome | null;
80
+ /** False when the state file could not be written (proposals still staged). */
81
+ stateWritten: boolean;
82
+ stateError?: string;
83
+ }
84
+ /**
85
+ * Re-distill join for one source: unchanged content hash ⇒ free no-op;
86
+ * changed ⇒ per claim skip / update-in-place / new-write (R4). Skipped
87
+ * claims are never re-emitted. Returns err only on input validation — a
88
+ * failed state write degrades to `stateWritten: false` because the
89
+ * proposals are already durably staged (retry would duplicate them).
90
+ */
91
+ export declare function distillUpsert(vaultRoot: string, input: DistillUpsertInput): Promise<Result<DistillUpsertOutcome, Error>>;
92
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../src/distill/state.ts"],"names":[],"mappings":"AAmCA,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,cAAc,EAAoB,MAAM,cAAc,CAAC;AAM3F,MAAM,WAAW,kBAAkB;IACjC,0DAA0D;IAC1D,YAAY,EAAE,MAAM,CAAC;IACrB,6EAA6E;IAC7E,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,YAAY;IAC3B,wEAAwE;IACxE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;CAC7C;AAMD,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAIzD;AAID,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,YAAY,CAShE;AAKD,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAa7F;AAED;;;;;GAKG;AACH,wBAAgB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