daftari 3.7.0 → 3.8.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 (243) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +4 -0
  3. package/dist/access/rbac.d.ts +1 -0
  4. package/dist/access/rbac.d.ts.map +1 -1
  5. package/dist/access/rbac.js +8 -0
  6. package/dist/access/rbac.js.map +1 -1
  7. package/dist/board/board.d.ts +37 -0
  8. package/dist/board/board.d.ts.map +1 -0
  9. package/dist/board/board.js +233 -0
  10. package/dist/board/board.js.map +1 -0
  11. package/dist/board/board.test.d.ts +2 -0
  12. package/dist/board/board.test.d.ts.map +1 -0
  13. package/dist/board/board.test.js +753 -0
  14. package/dist/board/board.test.js.map +1 -0
  15. package/dist/board/dormancy.test.d.ts +2 -0
  16. package/dist/board/dormancy.test.d.ts.map +1 -0
  17. package/dist/board/dormancy.test.js +79 -0
  18. package/dist/board/dormancy.test.js.map +1 -0
  19. package/dist/board/identity.d.ts +5 -0
  20. package/dist/board/identity.d.ts.map +1 -0
  21. package/dist/board/identity.js +146 -0
  22. package/dist/board/identity.js.map +1 -0
  23. package/dist/board/identity.test.d.ts +2 -0
  24. package/dist/board/identity.test.d.ts.map +1 -0
  25. package/dist/board/identity.test.js +291 -0
  26. package/dist/board/identity.test.js.map +1 -0
  27. package/dist/board/ledger.d.ts +41 -0
  28. package/dist/board/ledger.d.ts.map +1 -0
  29. package/dist/board/ledger.js +197 -0
  30. package/dist/board/ledger.js.map +1 -0
  31. package/dist/board/ledger.test.d.ts +2 -0
  32. package/dist/board/ledger.test.d.ts.map +1 -0
  33. package/dist/board/ledger.test.js +873 -0
  34. package/dist/board/ledger.test.js.map +1 -0
  35. package/dist/board/principals.d.ts +4 -0
  36. package/dist/board/principals.d.ts.map +1 -0
  37. package/dist/board/principals.js +37 -0
  38. package/dist/board/principals.js.map +1 -0
  39. package/dist/board/principals.test.d.ts +2 -0
  40. package/dist/board/principals.test.d.ts.map +1 -0
  41. package/dist/board/principals.test.js +296 -0
  42. package/dist/board/principals.test.js.map +1 -0
  43. package/dist/board/reconcile.d.ts +12 -0
  44. package/dist/board/reconcile.d.ts.map +1 -0
  45. package/dist/board/reconcile.js +293 -0
  46. package/dist/board/reconcile.js.map +1 -0
  47. package/dist/board/reconcile.test.d.ts +2 -0
  48. package/dist/board/reconcile.test.d.ts.map +1 -0
  49. package/dist/board/reconcile.test.js +788 -0
  50. package/dist/board/reconcile.test.js.map +1 -0
  51. package/dist/board/sources/index.d.ts +24 -0
  52. package/dist/board/sources/index.d.ts.map +1 -0
  53. package/dist/board/sources/index.js +64 -0
  54. package/dist/board/sources/index.js.map +1 -0
  55. package/dist/board/sources/lint.d.ts +3 -0
  56. package/dist/board/sources/lint.d.ts.map +1 -0
  57. package/dist/board/sources/lint.js +218 -0
  58. package/dist/board/sources/lint.js.map +1 -0
  59. package/dist/board/sources/lint.test.d.ts +2 -0
  60. package/dist/board/sources/lint.test.d.ts.map +1 -0
  61. package/dist/board/sources/lint.test.js +616 -0
  62. package/dist/board/sources/lint.test.js.map +1 -0
  63. package/dist/board/sources/staged.d.ts +3 -0
  64. package/dist/board/sources/staged.d.ts.map +1 -0
  65. package/dist/board/sources/staged.js +116 -0
  66. package/dist/board/sources/staged.js.map +1 -0
  67. package/dist/board/sources/staged.test.d.ts +2 -0
  68. package/dist/board/sources/staged.test.d.ts.map +1 -0
  69. package/dist/board/sources/staged.test.js +418 -0
  70. package/dist/board/sources/staged.test.js.map +1 -0
  71. package/dist/board/sources/staleness.d.ts +43 -0
  72. package/dist/board/sources/staleness.d.ts.map +1 -0
  73. package/dist/board/sources/staleness.js +346 -0
  74. package/dist/board/sources/staleness.js.map +1 -0
  75. package/dist/board/sources/staleness.test.d.ts +2 -0
  76. package/dist/board/sources/staleness.test.d.ts.map +1 -0
  77. package/dist/board/sources/staleness.test.js +923 -0
  78. package/dist/board/sources/staleness.test.js.map +1 -0
  79. package/dist/board/sources/tension.d.ts +3 -0
  80. package/dist/board/sources/tension.d.ts.map +1 -0
  81. package/dist/board/sources/tension.js +152 -0
  82. package/dist/board/sources/tension.js.map +1 -0
  83. package/dist/board/sources/tension.test.d.ts +2 -0
  84. package/dist/board/sources/tension.test.d.ts.map +1 -0
  85. package/dist/board/sources/tension.test.js +440 -0
  86. package/dist/board/sources/tension.test.js.map +1 -0
  87. package/dist/board/sources/tier2.d.ts +17 -0
  88. package/dist/board/sources/tier2.d.ts.map +1 -0
  89. package/dist/board/sources/tier2.js +233 -0
  90. package/dist/board/sources/tier2.js.map +1 -0
  91. package/dist/board/sources/tier2.test.d.ts +2 -0
  92. package/dist/board/sources/tier2.test.d.ts.map +1 -0
  93. package/dist/board/sources/tier2.test.js +480 -0
  94. package/dist/board/sources/tier2.test.js.map +1 -0
  95. package/dist/board/types.d.ts +161 -0
  96. package/dist/board/types.d.ts.map +1 -0
  97. package/dist/board/types.js +37 -0
  98. package/dist/board/types.js.map +1 -0
  99. package/dist/canon/index.d.ts +3 -1
  100. package/dist/canon/index.d.ts.map +1 -1
  101. package/dist/canon/index.js +35 -8
  102. package/dist/canon/index.js.map +1 -1
  103. package/dist/canon/types.d.ts +3 -0
  104. package/dist/canon/types.d.ts.map +1 -1
  105. package/dist/cli.d.ts.map +1 -1
  106. package/dist/cli.js +9 -0
  107. package/dist/cli.js.map +1 -1
  108. package/dist/consolidate/birth.d.ts +1 -1
  109. package/dist/consolidate/birth.d.ts.map +1 -1
  110. package/dist/consolidate/birth.js +22 -0
  111. package/dist/consolidate/birth.js.map +1 -1
  112. package/dist/consolidate/edge-write.d.ts +11 -0
  113. package/dist/consolidate/edge-write.d.ts.map +1 -1
  114. package/dist/consolidate/edge-write.js +41 -0
  115. package/dist/consolidate/edge-write.js.map +1 -1
  116. package/dist/consolidate/index.js +5 -2
  117. package/dist/consolidate/index.js.map +1 -1
  118. package/dist/consolidate/revision.d.ts +4 -2
  119. package/dist/consolidate/revision.d.ts.map +1 -1
  120. package/dist/consolidate/revision.js +23 -0
  121. package/dist/consolidate/revision.js.map +1 -1
  122. package/dist/curation/edge-staleness.d.ts +4 -2
  123. package/dist/curation/edge-staleness.d.ts.map +1 -1
  124. package/dist/curation/edge-staleness.js +36 -3
  125. package/dist/curation/edge-staleness.js.map +1 -1
  126. package/dist/curation/edges.d.ts +7 -0
  127. package/dist/curation/edges.d.ts.map +1 -1
  128. package/dist/curation/edges.js +13 -7
  129. package/dist/curation/edges.js.map +1 -1
  130. package/dist/curation/tension-access.d.ts +1 -0
  131. package/dist/curation/tension-access.d.ts.map +1 -1
  132. package/dist/curation/tension-access.js +18 -1
  133. package/dist/curation/tension-access.js.map +1 -1
  134. package/dist/curation/tension.d.ts +3 -0
  135. package/dist/curation/tension.d.ts.map +1 -1
  136. package/dist/curation/tension.js +36 -0
  137. package/dist/curation/tension.js.map +1 -1
  138. package/dist/curation/vault-docs.d.ts +1 -0
  139. package/dist/curation/vault-docs.d.ts.map +1 -1
  140. package/dist/curation/vault-docs.js +1 -0
  141. package/dist/curation/vault-docs.js.map +1 -1
  142. package/dist/distill/cli.d.ts.map +1 -1
  143. package/dist/distill/cli.js +6 -0
  144. package/dist/distill/cli.js.map +1 -1
  145. package/dist/distill/cost.d.ts +6 -0
  146. package/dist/distill/cost.d.ts.map +1 -1
  147. package/dist/distill/cost.js +2 -3
  148. package/dist/distill/cost.js.map +1 -1
  149. package/dist/distill/extract.d.ts +52 -0
  150. package/dist/distill/extract.d.ts.map +1 -1
  151. package/dist/distill/extract.js +24 -2
  152. package/dist/distill/extract.js.map +1 -1
  153. package/dist/distill/propose.d.ts +44 -1
  154. package/dist/distill/propose.d.ts.map +1 -1
  155. package/dist/distill/propose.js +84 -2
  156. package/dist/distill/propose.js.map +1 -1
  157. package/dist/distill/reader-fingerprint.d.ts +69 -0
  158. package/dist/distill/reader-fingerprint.d.ts.map +1 -0
  159. package/dist/distill/reader-fingerprint.js +145 -0
  160. package/dist/distill/reader-fingerprint.js.map +1 -0
  161. package/dist/distill/receipt-store.d.ts +5 -0
  162. package/dist/distill/receipt-store.d.ts.map +1 -0
  163. package/dist/distill/receipt-store.js +24 -0
  164. package/dist/distill/receipt-store.js.map +1 -0
  165. package/dist/eval/llm-openrouter.d.ts.map +1 -1
  166. package/dist/eval/llm-openrouter.js +4 -0
  167. package/dist/eval/llm-openrouter.js.map +1 -1
  168. package/dist/eval/llm.d.ts +22 -0
  169. package/dist/eval/llm.d.ts.map +1 -1
  170. package/dist/eval/llm.js +94 -24
  171. package/dist/eval/llm.js.map +1 -1
  172. package/dist/federation/classification.d.ts.map +1 -1
  173. package/dist/federation/classification.js +3 -0
  174. package/dist/federation/classification.js.map +1 -1
  175. package/dist/index.d.ts.map +1 -1
  176. package/dist/index.js +31 -0
  177. package/dist/index.js.map +1 -1
  178. package/dist/search/coverage.d.ts +5 -1
  179. package/dist/search/coverage.d.ts.map +1 -1
  180. package/dist/search/coverage.js +47 -15
  181. package/dist/search/coverage.js.map +1 -1
  182. package/dist/search/hybrid.d.ts +7 -1
  183. package/dist/search/hybrid.d.ts.map +1 -1
  184. package/dist/search/hybrid.js +31 -8
  185. package/dist/search/hybrid.js.map +1 -1
  186. package/dist/search/retrieval-metrics.d.ts +5 -0
  187. package/dist/search/retrieval-metrics.d.ts.map +1 -0
  188. package/dist/search/retrieval-metrics.js +60 -0
  189. package/dist/search/retrieval-metrics.js.map +1 -0
  190. package/dist/search/suppression.d.ts +9 -0
  191. package/dist/search/suppression.d.ts.map +1 -0
  192. package/dist/search/suppression.js +107 -0
  193. package/dist/search/suppression.js.map +1 -0
  194. package/dist/serve/index.d.ts.map +1 -1
  195. package/dist/serve/index.js +273 -4
  196. package/dist/serve/index.js.map +1 -1
  197. package/dist/server.d.ts.map +1 -1
  198. package/dist/server.js +4 -6
  199. package/dist/server.js.map +1 -1
  200. package/dist/tools/board.d.ts +32 -0
  201. package/dist/tools/board.d.ts.map +1 -0
  202. package/dist/tools/board.js +615 -0
  203. package/dist/tools/board.js.map +1 -0
  204. package/dist/tools/board.test.d.ts +2 -0
  205. package/dist/tools/board.test.d.ts.map +1 -0
  206. package/dist/tools/board.test.js +964 -0
  207. package/dist/tools/board.test.js.map +1 -0
  208. package/dist/tools/edge-staleness.d.ts.map +1 -1
  209. package/dist/tools/edge-staleness.js +18 -4
  210. package/dist/tools/edge-staleness.js.map +1 -1
  211. package/dist/tools/read.d.ts +2 -0
  212. package/dist/tools/read.d.ts.map +1 -1
  213. package/dist/tools/read.js +43 -13
  214. package/dist/tools/read.js.map +1 -1
  215. package/dist/tools/search.d.ts.map +1 -1
  216. package/dist/tools/search.js +79 -17
  217. package/dist/tools/search.js.map +1 -1
  218. package/dist/tools/write.d.ts.map +1 -1
  219. package/dist/tools/write.js +172 -2
  220. package/dist/tools/write.js.map +1 -1
  221. package/dist/utils/config.d.ts +13 -0
  222. package/dist/utils/config.d.ts.map +1 -1
  223. package/dist/utils/config.js +88 -0
  224. package/dist/utils/config.js.map +1 -1
  225. package/dist/version.d.ts +2 -0
  226. package/dist/version.d.ts.map +1 -0
  227. package/dist/version.js +8 -0
  228. package/dist/version.js.map +1 -0
  229. package/dist/view/board-page.d.ts +4 -0
  230. package/dist/view/board-page.d.ts.map +1 -0
  231. package/dist/view/board-page.js +278 -0
  232. package/dist/view/board-page.js.map +1 -0
  233. package/dist/view/board-page.test.d.ts +2 -0
  234. package/dist/view/board-page.test.d.ts.map +1 -0
  235. package/dist/view/board-page.test.js +548 -0
  236. package/dist/view/board-page.test.js.map +1 -0
  237. package/dist/view/pages.d.ts.map +1 -1
  238. package/dist/view/pages.js +13 -2
  239. package/dist/view/pages.js.map +1 -1
  240. package/dist/view/render.d.ts.map +1 -1
  241. package/dist/view/render.js +2 -1
  242. package/dist/view/render.js.map +1 -1
  243. package/package.json +2 -2
@@ -0,0 +1,923 @@
1
+ // staleness.test.ts — TDD test suite for U6: staleness + edge-staleness
2
+ // source adapters.
3
+ //
4
+ // Two adapters under test:
5
+ // ttlStalenessAdapter — wraps computeStaleness over expired docs.
6
+ // target: StalenessTarget { kind:"staleness", path }
7
+ // edgeStalenessAdapter — wraps upstreamStaleness (dependency-injected for
8
+ // testability) for pending-broken rows.
9
+ // target: Tier2Target { kind:"tier2", artifact, unit, edgeClass }
10
+ //
11
+ // Edge-staleness fixture strategy: DEPENDENCY INJECTION via factory.
12
+ // Constructing a full edge-staleness graph fixture (consumes.jsonl +
13
+ // curation-log.jsonl + edges.jsonl + tier-2 verdicts) is disproportionate
14
+ // for unit-testing the adapter's mapping logic. Instead,
15
+ // `makeEdgeStalenessAdapter` accepts a `_upstreamStalenessFn` seam
16
+ // (artifact → UpstreamStaleness[]) that tests inject with a pre-canned
17
+ // result. The production export `edgeStalenessAdapter` uses the real
18
+ // upstreamStaleness loading path.
19
+ // This approach is documented in staleness.ts.
20
+ //
21
+ // Edge states surfaced: ONLY "pending-broken".
22
+ // "pending-unchecked", "pending-compatible", "current", and "unverifiable"
23
+ // are NOT surfaced on the board. Documented in staleness.ts.
24
+ //
25
+ // Run with:
26
+ // npx vitest run src/board/sources/staleness.test.ts
27
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
28
+ import { tmpdir } from "node:os";
29
+ import { join } from "node:path";
30
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
31
+ import { deriveIdentity, fingerprint } from "../identity.js";
32
+ import { resolveAdapterForIdentity, SOURCE_ADAPTERS } from "./index.js";
33
+ import { edgeStalenessAdapter, makeEdgeStalenessAdapter, ttlStalenessAdapter, } from "./staleness.js";
34
+ // ---------------------------------------------------------------------------
35
+ // Vault fixture helpers (mirrors lint.test.ts)
36
+ // ---------------------------------------------------------------------------
37
+ function frontmatter(overrides = {}) {
38
+ const fm = {
39
+ title: "Test Document",
40
+ domain: "accumulation",
41
+ collection: "notes",
42
+ status: "canonical",
43
+ confidence: "medium",
44
+ created: "2020-01-01",
45
+ updated: "2020-01-01",
46
+ updated_by: "agent:seed",
47
+ provenance: "direct",
48
+ sources: [],
49
+ superseded_by: null,
50
+ ttl_days: null,
51
+ ...overrides,
52
+ };
53
+ const lines = [];
54
+ for (const [k, v] of Object.entries(fm)) {
55
+ if (v === null) {
56
+ lines.push(`${k}: null`);
57
+ }
58
+ else if (Array.isArray(v)) {
59
+ if (v.length === 0) {
60
+ lines.push(`${k}: []`);
61
+ }
62
+ else {
63
+ lines.push(`${k}:`);
64
+ for (const item of v) {
65
+ lines.push(` - ${JSON.stringify(item)}`);
66
+ }
67
+ }
68
+ }
69
+ else {
70
+ lines.push(`${k}: ${JSON.stringify(v)}`);
71
+ }
72
+ }
73
+ return `---\n${lines.join("\n")}\n---\n`;
74
+ }
75
+ function writeConfig(vaultRoot, roles) {
76
+ const daftariDir = join(vaultRoot, ".daftari");
77
+ mkdirSync(daftariDir, { recursive: true });
78
+ const roleLines = Object.entries(roles)
79
+ .map(([name, cfg]) => {
80
+ const lines = [` ${name}:`];
81
+ for (const [k, v] of Object.entries(cfg)) {
82
+ if (Array.isArray(v)) {
83
+ lines.push(` ${k}: [${v.map((s) => JSON.stringify(s)).join(", ")}]`);
84
+ }
85
+ else {
86
+ lines.push(` ${k}: ${JSON.stringify(v)}`);
87
+ }
88
+ }
89
+ return lines.join("\n");
90
+ })
91
+ .join("\n");
92
+ const yaml = `version: 1\nvault_name: test-vault\nroles:\n${roleLines}\n`;
93
+ writeFileSync(join(daftariDir, "config.yaml"), yaml, "utf-8");
94
+ }
95
+ function writeDoc(vaultRoot, path, content) {
96
+ const full = join(vaultRoot, path);
97
+ mkdirSync(join(vaultRoot, path.split("/").slice(0, -1).join("/")), { recursive: true });
98
+ writeFileSync(full, content, "utf-8");
99
+ }
100
+ // ---------------------------------------------------------------------------
101
+ // Role fixtures
102
+ // ---------------------------------------------------------------------------
103
+ const adminRole = {
104
+ read: ["*"],
105
+ write: ["*"],
106
+ promote: true,
107
+ ratify: true,
108
+ };
109
+ const scopedRole = {
110
+ // can read "notes" but not "restricted"
111
+ read: ["notes"],
112
+ write: ["notes"],
113
+ promote: false,
114
+ ratify: false,
115
+ };
116
+ const adminAccess = {
117
+ user: "human:admin",
118
+ roleName: "admin",
119
+ role: adminRole,
120
+ };
121
+ const scopedAccess = {
122
+ user: "human:analyst",
123
+ roleName: "analyst",
124
+ role: scopedRole,
125
+ };
126
+ // ---------------------------------------------------------------------------
127
+ // TTL Staleness Adapter tests
128
+ // ---------------------------------------------------------------------------
129
+ describe("ttlStalenessAdapter", () => {
130
+ let vaultRoot;
131
+ const FIXED_NOW = new Date("2026-01-01T00:00:00Z");
132
+ beforeEach(() => {
133
+ vaultRoot = mkdtempSync(join(tmpdir(), "daftari-ttl-staleness-test-"));
134
+ writeConfig(vaultRoot, {
135
+ admin: { read: ["*"], write: ["*"], promote: true, ratify: true },
136
+ analyst: { read: ["notes"], write: ["notes"] },
137
+ });
138
+ });
139
+ afterEach(() => {
140
+ rmSync(vaultRoot, { recursive: true, force: true });
141
+ });
142
+ // -------------------------------------------------------------------------
143
+ // Scenario 1: Expired-TTL doc → staleness finding emitted.
144
+ // Identity is stable; fingerprint drifts with score/ageDays.
145
+ // -------------------------------------------------------------------------
146
+ describe("Scenario 1: expired-TTL doc → finding with identity-stable / fingerprint-drifts", () => {
147
+ // doc updated 200 days before FIXED_NOW, ttl_days: 90 → expired
148
+ const staleDate = new Date(new Date("2026-01-01T00:00:00Z").getTime() - 200 * 86_400_000)
149
+ .toISOString()
150
+ .slice(0, 10);
151
+ beforeEach(() => {
152
+ writeDoc(vaultRoot, "notes/expired-doc.md", `${frontmatter({
153
+ title: "Expired",
154
+ collection: "notes",
155
+ updated: staleDate,
156
+ ttl_days: 90,
157
+ })}# Expired\n`);
158
+ });
159
+ it("emits a ttl-staleness finding for the expired doc", async () => {
160
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
161
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
162
+ f.target.kind === "staleness" &&
163
+ f.target.path === "notes/expired-doc.md");
164
+ expect(f).toBeDefined();
165
+ });
166
+ it("finding has source='staleness', StalenessTarget with correct path", async () => {
167
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
168
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
169
+ f.target.kind === "staleness" &&
170
+ f.target.path === "notes/expired-doc.md");
171
+ expect(f).toBeDefined();
172
+ expect(f.source).toBe("staleness");
173
+ expect(f.target).toEqual({ kind: "staleness", path: "notes/expired-doc.md" });
174
+ });
175
+ it("identity_key = deriveIdentity('staleness','ttl-staleness',target) — no discriminator", async () => {
176
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
177
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
178
+ f.target.kind === "staleness" &&
179
+ f.target.path === "notes/expired-doc.md");
180
+ expect(f).toBeDefined();
181
+ const expected = deriveIdentity("staleness", "ttl-staleness", {
182
+ kind: "staleness",
183
+ path: "notes/expired-doc.md",
184
+ });
185
+ expect(f.identity_key).toBe(expected);
186
+ expect(f.discriminator).toBeUndefined();
187
+ });
188
+ it("fingerprint = fingerprint(evidence) where evidence is volatile (score,ageDays,ttlDays)", async () => {
189
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
190
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
191
+ f.target.kind === "staleness" &&
192
+ f.target.path === "notes/expired-doc.md");
193
+ expect(f).toBeDefined();
194
+ // fingerprint covers full evidence — score and ageDays are the volatile parts,
195
+ // ttlDays is stable but included. fingerprintOf uses fingerprint(raw.evidence)
196
+ // which must be consistent with how list() stored the fingerprint.
197
+ const expectedFp = fingerprint(f.evidence);
198
+ expect(f.fingerprint).toBe(expectedFp);
199
+ });
200
+ it("identity STABLE across two runs while fingerprint DRIFTS as ageDays grows", async () => {
201
+ // Run 1: 200 days past update
202
+ const findings1 = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
203
+ const f1 = findings1.find((f) => f.check === "ttl-staleness" &&
204
+ f.target.kind === "staleness" &&
205
+ f.target.path === "notes/expired-doc.md");
206
+ expect(f1).toBeDefined();
207
+ // Run 2: 210 days past update (10 more days → ageDays changed)
208
+ const now2 = new Date(FIXED_NOW.getTime() + 10 * 86_400_000);
209
+ const findings2 = await ttlStalenessAdapter.list(vaultRoot, adminAccess, now2);
210
+ const f2 = findings2.find((f) => f.check === "ttl-staleness" &&
211
+ f.target.kind === "staleness" &&
212
+ f.target.path === "notes/expired-doc.md");
213
+ expect(f2).toBeDefined();
214
+ // Identity is STABLE
215
+ expect(f1.identity_key).toBe(f2.identity_key);
216
+ // Fingerprint DRIFTS because ageDays changed (200 → 210)
217
+ expect(f1.evidence.ageDays).toBe(200);
218
+ expect(f2.evidence.ageDays).toBe(210);
219
+ expect(f1.fingerprint).not.toBe(f2.fingerprint);
220
+ });
221
+ it("evidence carries score, ageDays, ttlDays with correct values", async () => {
222
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
223
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
224
+ f.target.kind === "staleness" &&
225
+ f.target.path === "notes/expired-doc.md");
226
+ expect(f).toBeDefined();
227
+ expect(typeof f.evidence.score).toBe("number");
228
+ expect(typeof f.evidence.ageDays).toBe("number");
229
+ expect(f.evidence.ttlDays).toBe(90);
230
+ expect(f.evidence.ageDays).toBe(200);
231
+ expect(f.evidence.score).toBe(1); // min(1, 200/90) = 1
232
+ });
233
+ it("identityOf(raw) returns the same identity_key as stored", async () => {
234
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
235
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
236
+ f.target.kind === "staleness" &&
237
+ f.target.path === "notes/expired-doc.md");
238
+ expect(f).toBeDefined();
239
+ expect(ttlStalenessAdapter.identityOf(f)).toBe(f.identity_key);
240
+ });
241
+ it("fingerprintOf(raw) returns the same fingerprint as stored", async () => {
242
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
243
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
244
+ f.target.kind === "staleness" &&
245
+ f.target.path === "notes/expired-doc.md");
246
+ expect(f).toBeDefined();
247
+ expect(ttlStalenessAdapter.fingerprintOf(f)).toBe(f.fingerprint);
248
+ });
249
+ it("certainty is 'medium'", async () => {
250
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
251
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
252
+ f.target.kind === "staleness" &&
253
+ f.target.path === "notes/expired-doc.md");
254
+ expect(f).toBeDefined();
255
+ expect(f.certainty).toBe("medium");
256
+ });
257
+ it("suggested_action and verify_predicate are non-empty strings", async () => {
258
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
259
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
260
+ f.target.kind === "staleness" &&
261
+ f.target.path === "notes/expired-doc.md");
262
+ expect(f).toBeDefined();
263
+ expect(typeof f.suggested_action).toBe("string");
264
+ expect(f.suggested_action.length).toBeGreaterThan(0);
265
+ expect(typeof f.verify_predicate).toBe("string");
266
+ expect(f.verify_predicate.length).toBeGreaterThan(0);
267
+ });
268
+ });
269
+ // -------------------------------------------------------------------------
270
+ // Scenario 2: Non-expired docs → no finding.
271
+ // -------------------------------------------------------------------------
272
+ describe("Scenario 2: non-expired doc → no finding", () => {
273
+ it("doc with ttl_days=null is never emitted", async () => {
274
+ writeDoc(vaultRoot, "notes/no-ttl.md", `${frontmatter({
275
+ title: "No TTL",
276
+ collection: "notes",
277
+ updated: "2020-01-01",
278
+ ttl_days: null,
279
+ })}# No TTL\n`);
280
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
281
+ const paths = findings.map((f) => (f.target.kind === "staleness" ? f.target.path : ""));
282
+ expect(paths).not.toContain("notes/no-ttl.md");
283
+ });
284
+ it("doc updated recently within TTL is not emitted", async () => {
285
+ // updated 10 days before FIXED_NOW, ttl=90 → NOT expired
286
+ const recentDate = new Date(FIXED_NOW.getTime() - 10 * 86_400_000).toISOString().slice(0, 10);
287
+ writeDoc(vaultRoot, "notes/fresh-doc.md", `${frontmatter({
288
+ title: "Fresh",
289
+ collection: "notes",
290
+ updated: recentDate,
291
+ ttl_days: 90,
292
+ })}# Fresh\n`);
293
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
294
+ const paths = findings.map((f) => (f.target.kind === "staleness" ? f.target.path : ""));
295
+ expect(paths).not.toContain("notes/fresh-doc.md");
296
+ });
297
+ });
298
+ // -------------------------------------------------------------------------
299
+ // Scenario 3: RBAC — denied collection finding omitted entirely (R17).
300
+ // -------------------------------------------------------------------------
301
+ describe("Scenario 3: RBAC omits denied-collection TTL findings", () => {
302
+ const staleDate = new Date(new Date("2026-01-01T00:00:00Z").getTime() - 200 * 86_400_000)
303
+ .toISOString()
304
+ .slice(0, 10);
305
+ beforeEach(() => {
306
+ // Expired doc in "notes" — readable by scopedRole
307
+ writeDoc(vaultRoot, "notes/expired-readable.md", `${frontmatter({
308
+ title: "Expired Readable",
309
+ collection: "notes",
310
+ updated: staleDate,
311
+ ttl_days: 90,
312
+ })}# Expired Readable\n`);
313
+ // Expired doc in "restricted" — NOT readable by scopedRole
314
+ writeDoc(vaultRoot, "restricted/expired-secret.md", `${frontmatter({
315
+ title: "Expired Secret",
316
+ collection: "restricted",
317
+ updated: staleDate,
318
+ ttl_days: 90,
319
+ })}# Expired Secret\n`);
320
+ });
321
+ it("admin sees findings from both collections", async () => {
322
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
323
+ const paths = findings.map((f) => (f.target.kind === "staleness" ? f.target.path : ""));
324
+ expect(paths).toContain("notes/expired-readable.md");
325
+ expect(paths).toContain("restricted/expired-secret.md");
326
+ });
327
+ it("scoped role sees 'notes' finding but not 'restricted' finding", async () => {
328
+ const findings = await ttlStalenessAdapter.list(vaultRoot, scopedAccess, FIXED_NOW);
329
+ const paths = findings.map((f) => (f.target.kind === "staleness" ? f.target.path : ""));
330
+ expect(paths).toContain("notes/expired-readable.md");
331
+ expect(paths).not.toContain("restricted/expired-secret.md");
332
+ });
333
+ it("omission is total — no placeholder for denied finding", async () => {
334
+ const adminFindings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
335
+ const scopedFindings = await ttlStalenessAdapter.list(vaultRoot, scopedAccess, FIXED_NOW);
336
+ expect(scopedFindings.length).toBeLessThanOrEqual(adminFindings.length);
337
+ for (const f of scopedFindings) {
338
+ if (f.target.kind === "staleness") {
339
+ expect(f.target.path).not.toMatch(/^restricted\//);
340
+ }
341
+ }
342
+ });
343
+ });
344
+ // -------------------------------------------------------------------------
345
+ // Scenario 4: reproduces() lifecycle.
346
+ // -------------------------------------------------------------------------
347
+ describe("Scenario 4: reproduces() returns true while expired, false after refresh", () => {
348
+ const staleDate = new Date(new Date("2026-01-01T00:00:00Z").getTime() - 200 * 86_400_000)
349
+ .toISOString()
350
+ .slice(0, 10);
351
+ beforeEach(() => {
352
+ writeDoc(vaultRoot, "notes/reproduced-doc.md", `${frontmatter({
353
+ title: "Reproduced",
354
+ collection: "notes",
355
+ updated: staleDate,
356
+ ttl_days: 90,
357
+ })}# Reproduced\n`);
358
+ });
359
+ it("reproduces returns true when doc is still expired", async () => {
360
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
361
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
362
+ f.target.kind === "staleness" &&
363
+ f.target.path === "notes/reproduced-doc.md");
364
+ expect(f).toBeDefined();
365
+ const result = await ttlStalenessAdapter.reproduces(f.identity_key, vaultRoot, adminAccess, FIXED_NOW);
366
+ expect(result).toBe(true);
367
+ });
368
+ it("reproduces returns false when doc is refreshed within TTL (different now)", async () => {
369
+ // Use a now close to the update date so the doc is NOT expired
370
+ const freshNow = new Date(`${staleDate}T00:00:00Z`);
371
+ freshNow.setDate(freshNow.getDate() + 10); // 10 days after update < 90 day TTL → not expired
372
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
373
+ const f = findings.find((f) => f.check === "ttl-staleness" &&
374
+ f.target.kind === "staleness" &&
375
+ f.target.path === "notes/reproduced-doc.md");
376
+ expect(f).toBeDefined();
377
+ const result = await ttlStalenessAdapter.reproduces(f.identity_key, vaultRoot, adminAccess, freshNow);
378
+ expect(result).toBe(false);
379
+ });
380
+ });
381
+ // -------------------------------------------------------------------------
382
+ // Scenario 5: FindingSourceAdapter interface — now injection compiles.
383
+ // -------------------------------------------------------------------------
384
+ describe("Scenario 5: FindingSourceAdapter interface supports now injection", () => {
385
+ it("ttlStalenessAdapter is assignable to FindingSourceAdapter", () => {
386
+ const adapter = ttlStalenessAdapter;
387
+ expect(typeof adapter.list).toBe("function");
388
+ expect(typeof adapter.reproduces).toBe("function");
389
+ });
390
+ it("engine can inject now via FindingSourceAdapter interface", async () => {
391
+ const adapter = ttlStalenessAdapter;
392
+ const findings = await adapter.list(vaultRoot, adminAccess, FIXED_NOW);
393
+ expect(Array.isArray(findings)).toBe(true);
394
+ });
395
+ });
396
+ // -------------------------------------------------------------------------
397
+ // Scenario 6: Finding shape defaults.
398
+ // -------------------------------------------------------------------------
399
+ describe("Scenario 6: Finding shape defaults (disposition, owner, history)", () => {
400
+ const staleDate = new Date(new Date("2026-01-01T00:00:00Z").getTime() - 200 * 86_400_000)
401
+ .toISOString()
402
+ .slice(0, 10);
403
+ beforeEach(() => {
404
+ writeDoc(vaultRoot, "notes/shape-doc.md", `${frontmatter({
405
+ title: "Shape",
406
+ collection: "notes",
407
+ updated: staleDate,
408
+ ttl_days: 90,
409
+ })}# Shape\n`);
410
+ });
411
+ it("findings have disposition='new', owner='', history=[]", async () => {
412
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
413
+ for (const f of findings) {
414
+ expect(f.disposition).toBe("new");
415
+ expect(f.owner).toBe("");
416
+ expect(f.history).toEqual([]);
417
+ }
418
+ });
419
+ it("first_seen and last_seen are ISO 8601 strings", async () => {
420
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
421
+ for (const f of findings) {
422
+ expect(typeof f.first_seen).toBe("string");
423
+ expect(new Date(f.first_seen).toISOString()).toBe(f.first_seen);
424
+ expect(typeof f.last_seen).toBe("string");
425
+ expect(new Date(f.last_seen).toISOString()).toBe(f.last_seen);
426
+ }
427
+ });
428
+ });
429
+ });
430
+ // ---------------------------------------------------------------------------
431
+ // Edge Staleness Adapter tests (dependency-injection via makeEdgeStalenessAdapter)
432
+ // ---------------------------------------------------------------------------
433
+ //
434
+ // Strategy: `makeEdgeStalenessAdapter(fn)` produces an adapter that calls
435
+ // `fn(artifact)` instead of the real upstreamStaleness loading path.
436
+ // Tests inject a pre-canned function to drive the mapping logic.
437
+ //
438
+ // Surfaced states: ONLY "pending-broken". Other states are NOT surfaced.
439
+ describe("edgeStalenessAdapter (via makeEdgeStalenessAdapter injection)", () => {
440
+ let vaultRoot;
441
+ beforeEach(() => {
442
+ vaultRoot = mkdtempSync(join(tmpdir(), "daftari-edge-staleness-test-"));
443
+ writeConfig(vaultRoot, {
444
+ admin: { read: ["*"], write: ["*"], promote: true, ratify: true },
445
+ analyst: { read: ["notes"], write: ["notes"] },
446
+ });
447
+ // Artifact doc in "notes" collection
448
+ writeDoc(vaultRoot, "notes/artifact.md", frontmatter({ title: "Artifact", collection: "notes", updated: "2025-01-01" }) +
449
+ "# Artifact\n");
450
+ // Unit (upstream) doc
451
+ writeDoc(vaultRoot, "notes/unit.md", `${frontmatter({ title: "Unit", collection: "notes", updated: "2025-01-01" })}# Unit\n`);
452
+ // Artifact in "restricted" collection (for RBAC tests)
453
+ writeDoc(vaultRoot, "restricted/secret-artifact.md", `${frontmatter({
454
+ title: "Secret Artifact",
455
+ collection: "restricted",
456
+ updated: "2025-01-01",
457
+ })}# Secret\n`);
458
+ });
459
+ afterEach(() => {
460
+ rmSync(vaultRoot, { recursive: true, force: true });
461
+ });
462
+ // -------------------------------------------------------------------------
463
+ // Scenario 7: pending-broken row → finding with Tier2Target tuple identity.
464
+ // -------------------------------------------------------------------------
465
+ describe("Scenario 7: pending-broken row → finding with tuple identity", () => {
466
+ const pendingBrokenRow = {
467
+ unit: "notes/unit.md",
468
+ edge_class: "compiled",
469
+ staleness: "pending-broken",
470
+ baseline: "2025-01-01T00:00:00.000Z",
471
+ changed_fields: ["body"],
472
+ reason: "tier-1: compiled edge affected by body change",
473
+ };
474
+ it("emits a finding for the pending-broken row", async () => {
475
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
476
+ const findings = await adapter.list(vaultRoot, adminAccess);
477
+ const f = findings.find((f) => f.check === "edge-staleness" &&
478
+ f.target.kind === "tier2" &&
479
+ f.target.artifact === "notes/artifact.md" &&
480
+ f.target.unit === "notes/unit.md" &&
481
+ f.target.edgeClass === "compiled");
482
+ expect(f).toBeDefined();
483
+ });
484
+ it("finding has source='staleness', check='edge-staleness', Tier2Target shape", async () => {
485
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
486
+ const findings = await adapter.list(vaultRoot, adminAccess);
487
+ const f = findings.find((f) => f.check === "edge-staleness" &&
488
+ f.target.kind === "tier2" &&
489
+ f.target.artifact === "notes/artifact.md");
490
+ expect(f).toBeDefined();
491
+ expect(f.source).toBe("staleness");
492
+ expect(f.check).toBe("edge-staleness");
493
+ const target = f.target;
494
+ expect(target.kind).toBe("tier2");
495
+ expect(target.artifact).toBe("notes/artifact.md");
496
+ expect(target.unit).toBe("notes/unit.md");
497
+ expect(target.edgeClass).toBe("compiled");
498
+ });
499
+ it("identity_key = deriveIdentity('staleness','edge-staleness',Tier2Target) — no discriminator", async () => {
500
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
501
+ const findings = await adapter.list(vaultRoot, adminAccess);
502
+ const f = findings.find((f) => f.check === "edge-staleness" &&
503
+ f.target.kind === "tier2" &&
504
+ f.target.artifact === "notes/artifact.md");
505
+ expect(f).toBeDefined();
506
+ const expectedTarget = {
507
+ kind: "tier2",
508
+ artifact: "notes/artifact.md",
509
+ unit: "notes/unit.md",
510
+ edgeClass: "compiled",
511
+ };
512
+ const expected = deriveIdentity("staleness", "edge-staleness", expectedTarget);
513
+ expect(f.identity_key).toBe(expected);
514
+ expect(f.discriminator).toBeUndefined();
515
+ });
516
+ it("fingerprint = fingerprint(evidence) covering volatile fields (staleness,changed_fields,baseline,reason)", async () => {
517
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
518
+ const findings = await adapter.list(vaultRoot, adminAccess);
519
+ const f = findings.find((f) => f.check === "edge-staleness" &&
520
+ f.target.kind === "tier2" &&
521
+ f.target.artifact === "notes/artifact.md");
522
+ expect(f).toBeDefined();
523
+ // fingerprint covers full evidence — fingerprintOf must use fingerprint(raw.evidence)
524
+ // which must be consistent with how list() stored the fingerprint.
525
+ const expectedFp = fingerprint(f.evidence);
526
+ expect(f.fingerprint).toBe(expectedFp);
527
+ });
528
+ it("identityOf(raw) returns the same identity_key", async () => {
529
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
530
+ const findings = await adapter.list(vaultRoot, adminAccess);
531
+ const f = findings.find((f) => f.check === "edge-staleness" &&
532
+ f.target.kind === "tier2" &&
533
+ f.target.artifact === "notes/artifact.md");
534
+ expect(f).toBeDefined();
535
+ expect(adapter.identityOf(f)).toBe(f.identity_key);
536
+ });
537
+ it("fingerprintOf(raw) returns the same fingerprint", async () => {
538
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
539
+ const findings = await adapter.list(vaultRoot, adminAccess);
540
+ const f = findings.find((f) => f.check === "edge-staleness" &&
541
+ f.target.kind === "tier2" &&
542
+ f.target.artifact === "notes/artifact.md");
543
+ expect(f).toBeDefined();
544
+ expect(adapter.fingerprintOf(f)).toBe(f.fingerprint);
545
+ });
546
+ it("tuple identity is unique per (artifact, unit, edgeClass)", async () => {
547
+ const row2 = {
548
+ unit: "notes/other-unit.md",
549
+ edge_class: "declared",
550
+ staleness: "pending-broken",
551
+ baseline: null,
552
+ changed_fields: ["confidence"],
553
+ reason: "tier-2: broken",
554
+ };
555
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow, row2]);
556
+ const findings = await adapter.list(vaultRoot, adminAccess);
557
+ const edgeFindings = findings.filter((f) => f.check === "edge-staleness" &&
558
+ f.target.kind === "tier2" &&
559
+ f.target.artifact === "notes/artifact.md");
560
+ expect(edgeFindings.length).toBeGreaterThanOrEqual(2);
561
+ const keys = edgeFindings.map((f) => f.identity_key);
562
+ const uniqueKeys = new Set(keys);
563
+ expect(uniqueKeys.size).toBe(keys.length);
564
+ });
565
+ it("evidence carries staleness, changed_fields, reason, baseline", async () => {
566
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
567
+ const findings = await adapter.list(vaultRoot, adminAccess);
568
+ const f = findings.find((f) => f.check === "edge-staleness" &&
569
+ f.target.kind === "tier2" &&
570
+ f.target.artifact === "notes/artifact.md");
571
+ expect(f).toBeDefined();
572
+ expect(f.evidence.staleness).toBe("pending-broken");
573
+ expect(f.evidence.changed_fields).toEqual(["body"]);
574
+ expect(f.evidence.baseline).toBe("2025-01-01T00:00:00.000Z");
575
+ expect(typeof f.evidence.reason).toBe("string");
576
+ });
577
+ });
578
+ // -------------------------------------------------------------------------
579
+ // Scenario 8: Only pending-broken surfaced; other states are NOT.
580
+ // -------------------------------------------------------------------------
581
+ describe("Scenario 8: only pending-broken rows are surfaced", () => {
582
+ const statesNotSurfaced = [
583
+ "pending-unchecked",
584
+ "pending-compatible",
585
+ "current",
586
+ "unverifiable",
587
+ ];
588
+ for (const staleness of statesNotSurfaced) {
589
+ it(`${staleness} row → NOT emitted as a board finding`, async () => {
590
+ const row = {
591
+ unit: "notes/unit.md",
592
+ edge_class: "declared",
593
+ staleness,
594
+ baseline: "2025-01-01T00:00:00.000Z",
595
+ changed_fields: [],
596
+ reason: "test",
597
+ };
598
+ const adapter = makeEdgeStalenessAdapter(() => [row]);
599
+ const findings = await adapter.list(vaultRoot, adminAccess);
600
+ const edgeFindings = findings.filter((f) => f.check === "edge-staleness");
601
+ expect(edgeFindings.length).toBe(0);
602
+ });
603
+ }
604
+ });
605
+ // -------------------------------------------------------------------------
606
+ // Scenario 9: RBAC — denied ARTIFACT collection → finding omitted (R17).
607
+ // -------------------------------------------------------------------------
608
+ describe("Scenario 9: RBAC filters by artifact collection", () => {
609
+ const pendingBrokenRow = {
610
+ unit: "notes/unit.md",
611
+ edge_class: "compiled",
612
+ staleness: "pending-broken",
613
+ baseline: "2025-01-01T00:00:00.000Z",
614
+ changed_fields: ["body"],
615
+ reason: "tier-1: affected",
616
+ };
617
+ it("admin sees findings for both notes and restricted artifacts", async () => {
618
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
619
+ const findings = await adapter.list(vaultRoot, adminAccess);
620
+ const artifacts = findings
621
+ .filter((f) => f.check === "edge-staleness" && f.target.kind === "tier2")
622
+ .map((f) => f.target.artifact);
623
+ expect(artifacts).toContain("notes/artifact.md");
624
+ expect(artifacts).toContain("restricted/secret-artifact.md");
625
+ });
626
+ it("scoped role sees notes artifact but NOT restricted artifact", async () => {
627
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
628
+ const findings = await adapter.list(vaultRoot, scopedAccess);
629
+ const artifacts = findings
630
+ .filter((f) => f.check === "edge-staleness" && f.target.kind === "tier2")
631
+ .map((f) => f.target.artifact);
632
+ expect(artifacts).toContain("notes/artifact.md");
633
+ expect(artifacts).not.toContain("restricted/secret-artifact.md");
634
+ });
635
+ it("omission is total — no placeholder for denied artifact finding", async () => {
636
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
637
+ const adminFindings = await adapter.list(vaultRoot, adminAccess);
638
+ const scopedFindings = await adapter.list(vaultRoot, scopedAccess);
639
+ expect(scopedFindings.length).toBeLessThanOrEqual(adminFindings.length);
640
+ for (const f of scopedFindings) {
641
+ if (f.target.kind === "tier2") {
642
+ expect(f.target.artifact).not.toMatch(/^restricted\//);
643
+ }
644
+ }
645
+ });
646
+ });
647
+ // -------------------------------------------------------------------------
648
+ // Scenario 9b: RBAC — denied UNIT collection → finding omitted entirely.
649
+ //
650
+ // The finding target exposes the unit path; if the caller cannot read the
651
+ // unit's collection, the finding is omitted even when the artifact is
652
+ // readable. This matches vaultTier2Queue's both-endpoints-readable policy
653
+ // and prevents upstream existence disclosure (#455).
654
+ // -------------------------------------------------------------------------
655
+ describe("Scenario 9b: RBAC omits finding when unit collection is denied", () => {
656
+ const pendingBrokenRowRestrictedUnit = {
657
+ unit: "restricted/secret-unit.md",
658
+ edge_class: "declared",
659
+ staleness: "pending-broken",
660
+ baseline: "2025-01-01T00:00:00.000Z",
661
+ changed_fields: ["body"],
662
+ reason: "tier-1: affected",
663
+ };
664
+ const pendingBrokenRowReadableUnit = {
665
+ unit: "notes/unit.md",
666
+ edge_class: "declared",
667
+ staleness: "pending-broken",
668
+ baseline: "2025-01-01T00:00:00.000Z",
669
+ changed_fields: ["body"],
670
+ reason: "tier-1: affected",
671
+ };
672
+ beforeEach(() => {
673
+ // Add a restricted unit doc to the vault for RBAC resolution
674
+ writeDoc(vaultRoot, "restricted/secret-unit.md", frontmatter({ title: "Secret Unit", collection: "restricted", updated: "2025-01-01" }) +
675
+ "# Secret\n");
676
+ });
677
+ it("admin sees finding when unit is in restricted collection", async () => {
678
+ const adapter = makeEdgeStalenessAdapter((artifact) => artifact === "notes/artifact.md" ? [pendingBrokenRowRestrictedUnit] : []);
679
+ const findings = await adapter.list(vaultRoot, adminAccess);
680
+ const edgeFindings = findings.filter((f) => f.check === "edge-staleness" &&
681
+ f.target.kind === "tier2" &&
682
+ f.target.artifact === "notes/artifact.md" &&
683
+ f.target.unit === "restricted/secret-unit.md");
684
+ expect(edgeFindings.length).toBeGreaterThanOrEqual(1);
685
+ });
686
+ it("scoped analyst: artifact readable but unit in denied collection → finding OMITTED", async () => {
687
+ // artifact=notes/artifact.md is readable by scopedRole (notes collection)
688
+ // unit=restricted/secret-unit.md is NOT readable by scopedRole
689
+ const adapter = makeEdgeStalenessAdapter((artifact) => artifact === "notes/artifact.md" ? [pendingBrokenRowRestrictedUnit] : []);
690
+ const findings = await adapter.list(vaultRoot, scopedAccess);
691
+ const denied = findings.filter((f) => f.check === "edge-staleness" &&
692
+ f.target.kind === "tier2" &&
693
+ f.target.unit === "restricted/secret-unit.md");
694
+ // Must be zero — unit existence must not leak
695
+ expect(denied).toHaveLength(0);
696
+ });
697
+ it("scoped analyst sees readable-unit finding but not restricted-unit finding", async () => {
698
+ const adapter = makeEdgeStalenessAdapter((artifact) => artifact === "notes/artifact.md"
699
+ ? [pendingBrokenRowRestrictedUnit, pendingBrokenRowReadableUnit]
700
+ : []);
701
+ const findings = await adapter.list(vaultRoot, scopedAccess);
702
+ const edgeFindings = findings.filter((f) => f.check === "edge-staleness");
703
+ const units = edgeFindings.map((f) => f.target.unit);
704
+ expect(units).toContain("notes/unit.md");
705
+ expect(units).not.toContain("restricted/secret-unit.md");
706
+ });
707
+ it("omission is total — restricted unit path does not appear anywhere in denied findings", async () => {
708
+ const adapter = makeEdgeStalenessAdapter((artifact) => artifact === "notes/artifact.md" ? [pendingBrokenRowRestrictedUnit] : []);
709
+ const scopedFindings = await adapter.list(vaultRoot, scopedAccess);
710
+ for (const f of scopedFindings) {
711
+ if (f.target.kind === "tier2") {
712
+ expect(f.target.unit).not.toMatch(/^restricted\//);
713
+ }
714
+ }
715
+ });
716
+ });
717
+ // -------------------------------------------------------------------------
718
+ // Scenario 10: reproduces() — true when row still pending-broken, false after.
719
+ // -------------------------------------------------------------------------
720
+ describe("Scenario 10: reproduces() for edge-staleness", () => {
721
+ const pendingBrokenRow = {
722
+ unit: "notes/unit.md",
723
+ edge_class: "compiled",
724
+ staleness: "pending-broken",
725
+ baseline: "2025-01-01T00:00:00.000Z",
726
+ changed_fields: ["body"],
727
+ reason: "tier-1: affected",
728
+ };
729
+ it("reproduces returns true when the row is still pending-broken", async () => {
730
+ const adapter = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
731
+ const findings = await adapter.list(vaultRoot, adminAccess);
732
+ const f = findings.find((f) => f.check === "edge-staleness" &&
733
+ f.target.kind === "tier2" &&
734
+ f.target.artifact === "notes/artifact.md");
735
+ expect(f).toBeDefined();
736
+ const result = await adapter.reproduces(f.identity_key, vaultRoot, adminAccess);
737
+ expect(result).toBe(true);
738
+ });
739
+ it("reproduces returns false when the row resolves (no longer pending-broken)", async () => {
740
+ const adapterBroken = makeEdgeStalenessAdapter(() => [pendingBrokenRow]);
741
+ const findings = await adapterBroken.list(vaultRoot, adminAccess);
742
+ const f = findings.find((f) => f.check === "edge-staleness" &&
743
+ f.target.kind === "tier2" &&
744
+ f.target.artifact === "notes/artifact.md");
745
+ expect(f).toBeDefined();
746
+ // Now inject no rows (row resolved to current)
747
+ const adapterResolved = makeEdgeStalenessAdapter(() => []);
748
+ const result = await adapterResolved.reproduces(f.identity_key, vaultRoot, adminAccess);
749
+ expect(result).toBe(false);
750
+ });
751
+ });
752
+ // -------------------------------------------------------------------------
753
+ // Scenario 11: makeEdgeStalenessAdapter conforms to FindingSourceAdapter.
754
+ // -------------------------------------------------------------------------
755
+ describe("Scenario 11: makeEdgeStalenessAdapter conforms to FindingSourceAdapter", () => {
756
+ it("produced adapter is assignable to FindingSourceAdapter", () => {
757
+ const adapter = makeEdgeStalenessAdapter(() => []);
758
+ expect(typeof adapter.list).toBe("function");
759
+ expect(typeof adapter.reproduces).toBe("function");
760
+ });
761
+ });
762
+ });
763
+ // ---------------------------------------------------------------------------
764
+ // Registry tests — SOURCE_ADAPTERS array
765
+ // ---------------------------------------------------------------------------
766
+ describe("SOURCE_ADAPTERS registry (R22) after U6", () => {
767
+ it("SOURCE_ADAPTERS contains ttlStalenessAdapter", () => {
768
+ expect(SOURCE_ADAPTERS).toContain(ttlStalenessAdapter);
769
+ });
770
+ it("SOURCE_ADAPTERS contains edgeStalenessAdapter", () => {
771
+ expect(SOURCE_ADAPTERS).toContain(edgeStalenessAdapter);
772
+ });
773
+ it("every adapter in SOURCE_ADAPTERS has all FindingSourceAdapter methods", () => {
774
+ for (const adapter of SOURCE_ADAPTERS) {
775
+ expect(typeof adapter.list).toBe("function");
776
+ expect(typeof adapter.identityOf).toBe("function");
777
+ expect(typeof adapter.fingerprintOf).toBe("function");
778
+ expect(typeof adapter.reproduces).toBe("function");
779
+ }
780
+ });
781
+ });
782
+ // ---------------------------------------------------------------------------
783
+ // Scenario 12: resolveAdapterForIdentity (C1)
784
+ // Two adapters share source:"staleness" — a source→adapter map cannot
785
+ // disambiguate. The resolver iterates SOURCE_ADAPTERS and returns the FIRST
786
+ // adapter whose reproduces() recognises the key; an unknown key → null.
787
+ // ---------------------------------------------------------------------------
788
+ describe("resolveAdapterForIdentity (C1)", () => {
789
+ let vaultRoot;
790
+ const FIXED_NOW = new Date("2026-01-01T00:00:00Z");
791
+ const staleDate = new Date(new Date("2026-01-01T00:00:00Z").getTime() - 200 * 86_400_000)
792
+ .toISOString()
793
+ .slice(0, 10);
794
+ beforeEach(() => {
795
+ vaultRoot = mkdtempSync(join(tmpdir(), "daftari-resolver-test-"));
796
+ writeConfig(vaultRoot, {
797
+ admin: { read: ["*"], write: ["*"], promote: true, ratify: true },
798
+ });
799
+ // Expired TTL doc → produces a ttlStalenessAdapter identity_key
800
+ writeDoc(vaultRoot, "notes/expired-for-resolver.md", `${frontmatter({
801
+ title: "Expired Resolver",
802
+ collection: "notes",
803
+ updated: staleDate,
804
+ ttl_days: 90,
805
+ })}# Expired\n`);
806
+ });
807
+ afterEach(() => {
808
+ rmSync(vaultRoot, { recursive: true, force: true });
809
+ });
810
+ it("identity_key from ttlStalenessAdapter resolves to ttlStalenessAdapter (not edgeStalenessAdapter)", async () => {
811
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
812
+ const f = findings.find((f) => f.check === "ttl-staleness" && f.target.kind === "staleness");
813
+ expect(f).toBeDefined();
814
+ const resolved = await resolveAdapterForIdentity(f.identity_key, vaultRoot, adminAccess, FIXED_NOW);
815
+ expect(resolved).toBe(ttlStalenessAdapter);
816
+ expect(resolved).not.toBe(edgeStalenessAdapter);
817
+ });
818
+ it("edge-staleness identity_key for phantom paths resolves to null (not ttlStalenessAdapter)", async () => {
819
+ // Build an edge-staleness identity_key manually — the identity is derived
820
+ // purely from source/check/target. The docs don't actually exist in the
821
+ // vault, so no adapter will reproduce this key.
822
+ // Key validation: the resolver must NOT return ttlStalenessAdapter for an
823
+ // edge-staleness key (the two adapters share source:"staleness" but their
824
+ // identity_key namespaces are distinct).
825
+ const target = {
826
+ kind: "tier2",
827
+ artifact: "notes/artifact-does-not-exist.md",
828
+ unit: "notes/unit-does-not-exist.md",
829
+ edgeClass: "declared",
830
+ };
831
+ const edgeIdentityKey = deriveIdentity("staleness", "edge-staleness", target);
832
+ const resolved = await resolveAdapterForIdentity(edgeIdentityKey, vaultRoot, adminAccess, FIXED_NOW);
833
+ // No real pending-broken row exists for these phantom paths → null.
834
+ // Critically: it must NOT be ttlStalenessAdapter (wrong adapter).
835
+ expect(resolved).toBeNull();
836
+ expect(resolved).not.toBe(ttlStalenessAdapter);
837
+ });
838
+ it("an unknown identity_key resolves to null", async () => {
839
+ const resolved = await resolveAdapterForIdentity("staleness:completely-unknown-identity-key-xyz", vaultRoot, adminAccess, FIXED_NOW);
840
+ expect(resolved).toBeNull();
841
+ });
842
+ it("ttl identity_key does NOT resolve to edgeStalenessAdapter — disambiguates correctly", async () => {
843
+ // Both adapters emit source:"staleness" — a map keyed by source cannot
844
+ // distinguish them. The resolver must not return the wrong adapter.
845
+ const findings = await ttlStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
846
+ const f = findings.find((f) => f.check === "ttl-staleness");
847
+ expect(f).toBeDefined();
848
+ // edgeStalenessAdapter.reproduces should return false for a ttl key.
849
+ const edgeReproduces = await edgeStalenessAdapter.reproduces(f.identity_key, vaultRoot, adminAccess, FIXED_NOW);
850
+ expect(edgeReproduces).toBe(false);
851
+ // Only ttlStalenessAdapter.reproduces should return true.
852
+ const ttlReproduces = await ttlStalenessAdapter.reproduces(f.identity_key, vaultRoot, adminAccess, FIXED_NOW);
853
+ expect(ttlReproduces).toBe(true);
854
+ });
855
+ });
856
+ // ---------------------------------------------------------------------------
857
+ // Scenario 13: edgeStalenessAdapter production smoke test (I1)
858
+ // Exercises the real load path (readProvenanceLog / readTier2Verdicts /
859
+ // listEdges / loadDocuments / upstreamStaleness) on a real temp vault.
860
+ // Goal: a wiring bug (wrong import, wrong arg order) must not hide behind
861
+ // injected-fn tests.
862
+ //
863
+ // Vault seeded with: one doc + a minimal provenance log (no edges).
864
+ // Expected: list() returns an array (possibly empty) WITHOUT throwing.
865
+ // We do NOT seed a full edge because constructing a valid provenance/
866
+ // consumes/edges fixture for the declared-edge path is disproportionate for
867
+ // a smoke test; the injected-fn tests cover that mapping logic exhaustively.
868
+ // ---------------------------------------------------------------------------
869
+ describe("edgeStalenessAdapter production smoke test (I1)", () => {
870
+ let vaultRoot;
871
+ const FIXED_NOW = new Date("2026-01-01T00:00:00Z");
872
+ beforeEach(() => {
873
+ vaultRoot = mkdtempSync(join(tmpdir(), "daftari-edge-smoke-test-"));
874
+ writeConfig(vaultRoot, {
875
+ admin: { read: ["*"], write: ["*"], promote: true, ratify: true },
876
+ });
877
+ // One doc with sources: [] (no declared upstream)
878
+ writeDoc(vaultRoot, "notes/smoke-doc.md", `${frontmatter({
879
+ title: "Smoke",
880
+ collection: "notes",
881
+ updated: "2025-06-01",
882
+ sources: [],
883
+ })}# Smoke\n`);
884
+ // Minimal provenance log: one entry for smoke-doc (no upstream to be stale on)
885
+ const daftariDir = join(vaultRoot, ".daftari");
886
+ mkdirSync(daftariDir, { recursive: true });
887
+ const provenanceEntry = JSON.stringify({
888
+ file: "notes/smoke-doc.md",
889
+ action: "write",
890
+ timestamp: "2025-06-01T00:00:00.000Z",
891
+ changed_fields: ["body"],
892
+ });
893
+ writeFileSync(join(daftariDir, "curation-log.jsonl"), `${provenanceEntry}\n`, "utf-8");
894
+ // Note: no edges.jsonl and no tier2-verdicts.jsonl seeded — these are
895
+ // optional (absent-file is treated as "no entries" by the loaders).
896
+ });
897
+ afterEach(() => {
898
+ rmSync(vaultRoot, { recursive: true, force: true });
899
+ });
900
+ it("production edgeStalenessAdapter.list() runs without throwing on a vault with a provenance log", async () => {
901
+ // The real load path must complete without error even when edges/verdicts
902
+ // are absent. Result is [] (no declared sources → no rows to classify).
903
+ let findings;
904
+ await expect((async () => {
905
+ findings = await edgeStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
906
+ })()).resolves.not.toThrow();
907
+ expect(Array.isArray(findings)).toBe(true);
908
+ });
909
+ it("production edgeStalenessAdapter.list() returns an array (zero or more findings)", async () => {
910
+ const findings = await edgeStalenessAdapter.list(vaultRoot, adminAccess, FIXED_NOW);
911
+ // With sources:[] and no edges, no pending-broken rows exist → empty array.
912
+ expect(Array.isArray(findings)).toBe(true);
913
+ expect(findings.length).toBe(0);
914
+ });
915
+ it("production wiring loads without error — identityOf and fingerprintOf are functions", () => {
916
+ // Basic structural check: the production singleton exports correct methods.
917
+ expect(typeof edgeStalenessAdapter.list).toBe("function");
918
+ expect(typeof edgeStalenessAdapter.identityOf).toBe("function");
919
+ expect(typeof edgeStalenessAdapter.fingerprintOf).toBe("function");
920
+ expect(typeof edgeStalenessAdapter.reproduces).toBe("function");
921
+ });
922
+ });
923
+ //# sourceMappingURL=staleness.test.js.map