@seanhogg/builderforce-memory 2026.10.76 → 2026.10.77

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 (41) hide show
  1. package/dist/knowledge/facts/factsHash.d.ts +27 -0
  2. package/dist/knowledge/facts/factsHash.d.ts.map +1 -0
  3. package/dist/knowledge/facts/factsHash.js +67 -0
  4. package/dist/knowledge/facts/factsHash.js.map +1 -0
  5. package/dist/knowledge/facts/fileFacts.d.ts +70 -12
  6. package/dist/knowledge/facts/fileFacts.d.ts.map +1 -1
  7. package/dist/knowledge/facts/fileFacts.js +42 -33
  8. package/dist/knowledge/facts/fileFacts.js.map +1 -1
  9. package/dist/knowledge/facts/guards.d.ts +101 -0
  10. package/dist/knowledge/facts/guards.d.ts.map +1 -0
  11. package/dist/knowledge/facts/guards.js +260 -0
  12. package/dist/knowledge/facts/guards.js.map +1 -0
  13. package/dist/knowledge/index.d.ts +9 -1
  14. package/dist/knowledge/index.d.ts.map +1 -1
  15. package/dist/knowledge/index.js +7 -1
  16. package/dist/knowledge/index.js.map +1 -1
  17. package/dist/knowledge/rules/evaluateRules.d.ts.map +1 -1
  18. package/dist/knowledge/rules/evaluateRules.js +2 -1
  19. package/dist/knowledge/rules/evaluateRules.js.map +1 -1
  20. package/dist/knowledge/rules/factGraph.d.ts +2 -0
  21. package/dist/knowledge/rules/factGraph.d.ts.map +1 -1
  22. package/dist/knowledge/rules/factGraph.js +6 -2
  23. package/dist/knowledge/rules/factGraph.js.map +1 -1
  24. package/dist/knowledge/rules/factStore.d.ts +139 -0
  25. package/dist/knowledge/rules/factStore.d.ts.map +1 -0
  26. package/dist/knowledge/rules/factStore.js +252 -0
  27. package/dist/knowledge/rules/factStore.js.map +1 -0
  28. package/package.json +1 -1
  29. package/src/contract/exports.snapshot.json +14 -0
  30. package/src/contract/types.contract.test.ts +57 -2
  31. package/src/knowledge/facts/factsHash.test.ts +50 -0
  32. package/src/knowledge/facts/factsHash.ts +71 -0
  33. package/src/knowledge/facts/fileFacts.test.ts +2 -1
  34. package/src/knowledge/facts/fileFacts.ts +104 -36
  35. package/src/knowledge/facts/guards.test.ts +276 -0
  36. package/src/knowledge/facts/guards.ts +296 -0
  37. package/src/knowledge/index.ts +13 -1
  38. package/src/knowledge/rules/evaluateRules.ts +2 -1
  39. package/src/knowledge/rules/factGraph.ts +8 -2
  40. package/src/knowledge/rules/factStore.test.ts +303 -0
  41. package/src/knowledge/rules/factStore.ts +317 -0
@@ -0,0 +1,296 @@
1
+ /**
2
+ * knowledge/facts/guards.ts — the guard-chain semantics of entry points across `use()` registrations and router mounts
3
+ * (PRD 41 §C5.1). Pure: the host resolves each imported mount's target to a file and its facts; this module composes.
4
+ *
5
+ * The rules, explicit:
6
+ * 1. ROUTER IDENTITY. Routes, uses and mounts belong to the router binding that registers them (`router`; absent = the
7
+ * file's one default router). A `use` on `adminRouter` never guards a route on `publicRouter`, even in one file.
8
+ * 2. ORDER. A `use(path?, guard)` ({@link UseFact}) guards a route of its router only when it was registered BEFORE the
9
+ * route (`order`) and its path pattern covers the route's path. A path-scoped `use` registered after a route never
10
+ * guards it. A route with no `order` takes every covering `use`, and those guards are reported in `guardsInferred`
11
+ * (a C5.1 rule downgrades them to heuristic). A producer that emits `uses` MUST set `order` on every entry point.
12
+ * 3. MOUNTS. A mounted sub-router (`app.route('/api', sub)`, `app.use('/api', subRouter)`, {@link MountFact}) answers its
13
+ * routes under the prefix-joined path, and inherits every `use` of the mounting router registered before the mount
14
+ * that covers the joined path, plus the mount's own inline `guards` — transitively, up to {@link MAX_MOUNT_DEPTH}
15
+ * levels. A target naming a local router binding of the same file is resolved here; other targets come from the host
16
+ * ({@link ResolvedMount}). Cycles are cut by the (file, router) pairs on the stack.
17
+ * 4. A route's own `guards` (inline middleware, wrappers) always apply.
18
+ *
19
+ * CALL SITES. `effectiveGuards` is called on ROOT routers only: files no other file mounts ({@link rootRouters}), and
20
+ * within a file the routers no local mount targets. A sub-router's routes are reported through their root (prefix-joined,
21
+ * with the inherited guards); calling it on a mounted file as well would report a bare, under-guarded copy of each route.
22
+ * Consumers that read `entryPoints[].guards` directly must switch to this function in the same change in which a producer
23
+ * starts emitting `uses`.
24
+ *
25
+ * Route paths come from the entry key: `'<METHOD> /path'` or `'/path'`. An entry point without a path (a VS Code command,
26
+ * an exported API) gets only path-less (`''`, `'*'`, `'/*'`) uses of its router, and is never mounted.
27
+ */
28
+
29
+ import { compareStr, sortBy } from '../refs.js';
30
+ import { canonicalFileFacts, type EntryPointFact, type FileFacts, type MountFact, type UseFact } from './fileFacts.js';
31
+
32
+ /** How many mount levels `effectiveGuards` descends at most (mount nesting, not import hops). */
33
+ export const MAX_MOUNT_DEPTH = 8;
34
+
35
+ /** A mount the host resolved: the target (as written in {@link MountFact.target}) → the sub-router's file and facts. */
36
+ export interface ResolvedMount {
37
+ /** The {@link MountFact.target} this resolves (a file may mount one target at several prefixes). */
38
+ target: string;
39
+ /** The sub-router's root-relative file. */
40
+ path: string;
41
+ facts: FileFacts;
42
+ /** The router binding in that file that is mounted. Absent ⇒ that file's root routers. */
43
+ router?: string;
44
+ /** The sub-router file's own imported mounts, resolved the same way (nested routers). */
45
+ mounts?: readonly ResolvedMount[];
46
+ }
47
+
48
+ /** An entry point with its effective guards: prefix-joined key, composed guards, the mounting files on its chain. */
49
+ export interface EffectiveEntryPoint extends EntryPointFact {
50
+ /** The file that registers the handler: `opts.path` (default '') for the given file, the sub-router's path otherwise. */
51
+ file: string;
52
+ /** The mount prefixes from the root router down to the registering router, outermost first; empty for root routes. */
53
+ via: string[];
54
+ /** Guards in `guards` that apply only because the route has no `order` (rule 2), sorted. Empty when the order is known. */
55
+ guardsInferred: string[];
56
+ }
57
+
58
+ export interface EffectiveGuardsOptions {
59
+ /** The given file's root-relative path: reported as `file`, and added to a mounted route's `chain`. */
60
+ path?: string;
61
+ /** The root router binding to start from ('' = the file's default router). Absent ⇒ every router of the file that no local mount targets. */
62
+ router?: string;
63
+ /** Mount levels to descend (0..{@link MAX_MOUNT_DEPTH}). Default {@link MAX_MOUNT_DEPTH}. */
64
+ maxDepth?: number;
65
+ }
66
+
67
+ export interface EffectiveGuardsResult {
68
+ /** Sorted by file, kind, key, symbol, via. */
69
+ entryPoints: EffectiveEntryPoint[];
70
+ /** Mounts not followed because of the depth limit or a cycle. */
71
+ truncated: number;
72
+ /** Mounts whose target was neither a local router nor in `resolvedMounts`. */
73
+ unresolved: number;
74
+ }
75
+
76
+ const WILDCARD_PATHS = new Set(['', '*', '/*']);
77
+ const segments = (path: string) => path.split('/').filter((s) => s !== '');
78
+
79
+ /** Joins a mount prefix and a route path into one `/`-rooted path ('/api' + '/x' → '/api/x'; '/api' + '/' → '/api'). */
80
+ export function joinRoutePath(prefix: string, path: string): string {
81
+ return `/${[...segments(prefix), ...segments(path)].join('/')}`;
82
+ }
83
+
84
+ const isParam = (seg: string) => seg.startsWith(':');
85
+
86
+ /**
87
+ * Whether a `use` path pattern covers `path` (rule 2's path half). Segments: a literal matches itself; `:x`, `:x{regex}`
88
+ * match one segment; `:x?` matches one segment or none; `*` as the last segment matches the rest (zero or more segments);
89
+ * a last segment ending in `*` (`api*`) matches any segment starting with its stem, and the rest. `prefix` (default)
90
+ * covers every path under the pattern; `exact` that path only.
91
+ */
92
+ export function routePathCovers(pattern: string, path: string, match: UseFact['match'] = 'prefix'): boolean {
93
+ if (WILDCARD_PATHS.has(pattern)) return true;
94
+ const pat = segments(pattern);
95
+ const segs = segments(path);
96
+ const last = pat.length - 1;
97
+ const at = (pi: number, si: number): boolean => {
98
+ if (pi === pat.length) return match === 'exact' ? si === segs.length : true;
99
+ const p = pat[pi]!;
100
+ if (pi === last && p === '*') return true;
101
+ if (pi === last && p.endsWith('*') && !isParam(p)) return si < segs.length && segs[si]!.startsWith(p.slice(0, -1));
102
+ if (isParam(p)) {
103
+ if (si < segs.length && at(pi + 1, si + 1)) return true;
104
+ return p.endsWith('?') && at(pi + 1, si);
105
+ }
106
+ if (p === '*') return si < segs.length && at(pi + 1, si + 1);
107
+ return si < segs.length && segs[si] === p && at(pi + 1, si + 1);
108
+ };
109
+ return at(0, 0);
110
+ }
111
+
112
+ /** `'<METHOD> /path'` → method + path; `'/path'` → path; anything else → null (not a routed entry point). */
113
+ function parseRouteKey(key: string): { method: string; path: string } | null {
114
+ const m = /^(?:(\S+)\s+)?(\/\S*)$/.exec(key.trim());
115
+ return m ? { method: m[1] ?? '', path: m[2]! } : null;
116
+ }
117
+
118
+ /** A use lifted to absolute paths: its pattern joined with the prefix of the router that registered it. */
119
+ interface ScopedUse { pattern: string; match: UseFact['match']; guards: readonly string[]; wildcard: boolean }
120
+
121
+ const everything = (prefix: string, guards: readonly string[], wildcard: boolean): ScopedUse =>
122
+ ({ pattern: `${joinRoutePath(prefix, '')}/*`, match: 'prefix', guards, wildcard });
123
+
124
+ const scoped = (u: UseFact, prefix: string): ScopedUse => (WILDCARD_PATHS.has(u.path)
125
+ ? everything(prefix, u.guards, true)
126
+ : { pattern: joinRoutePath(prefix, u.path), match: u.match, guards: u.guards, wildcard: false });
127
+
128
+ /** The router key of a registration ('' = the file's default router). */
129
+ const routerOf = (x: { router?: string }) => x.router ?? '';
130
+
131
+ /** Every router binding that registers something in `f`. */
132
+ function routersOf(f: FileFacts): Set<string> {
133
+ return new Set([...f.entryPoints.map(routerOf), ...(f.uses ?? []).map(routerOf), ...(f.mounts ?? []).map(routerOf)]);
134
+ }
135
+
136
+ /** The routers of `f` no local mount targets (rule 3), sorted. */
137
+ function rootRoutersOf(f: FileFacts): string[] {
138
+ const routers = routersOf(f);
139
+ for (const m of f.mounts ?? []) if (routers.has(m.target) && m.target !== routerOf(m)) routers.delete(m.target);
140
+ return [...routers].sort(compareStr);
141
+ }
142
+
143
+ /**
144
+ * Every entry point of the root router(s) of `fileFacts` and of the sub-routers they mount, with its EFFECTIVE guards
145
+ * (rules 1–4 of the header). Mounted routes get prefix-joined keys and the mounting files added to their `chain`.
146
+ */
147
+ export function effectiveGuards(fileFacts: FileFacts, resolvedMounts: readonly ResolvedMount[], opts: EffectiveGuardsOptions = {}): EffectiveGuardsResult {
148
+ const requested = opts.maxDepth ?? MAX_MOUNT_DEPTH;
149
+ const maxDepth = Number.isFinite(requested) ? Math.max(0, Math.min(MAX_MOUNT_DEPTH, Math.floor(requested))) : MAX_MOUNT_DEPTH;
150
+ const out: EffectiveEntryPoint[] = [];
151
+ let truncated = 0;
152
+ let unresolved = 0;
153
+
154
+ interface Frame {
155
+ facts: FileFacts; file: string; router: string; prefix: string; via: readonly string[];
156
+ inherited: readonly ScopedUse[]; chainFiles: readonly string[]; resolved: readonly ResolvedMount[]; depth: number; stack: ReadonlySet<string>;
157
+ }
158
+
159
+ const visit = (fr: Frame) => {
160
+ const f = fr.facts;
161
+ const uses = (f.uses ?? []).filter((u) => routerOf(u) === fr.router);
162
+ for (const ep of f.entryPoints) {
163
+ if (routerOf(ep) !== fr.router) continue;
164
+ const route = parseRouteKey(ep.key);
165
+ if (fr.depth > 0 && !route) continue; // only routes answer under a mount
166
+ const absPath = route ? joinRoutePath(fr.prefix, route.path) : null;
167
+ const applies = (u: ScopedUse) => (absPath === null ? u.wildcard : routePathCovers(u.pattern, absPath, u.match));
168
+ const known = new Set(ep.guards);
169
+ for (const u of fr.inherited) if (applies(u)) for (const g of u.guards) known.add(g);
170
+ const inferred = new Set<string>();
171
+ for (const u of uses) {
172
+ if (ep.order !== undefined && u.order >= ep.order) continue;
173
+ if (!applies(scoped(u, fr.prefix))) continue;
174
+ for (const g of u.guards) (ep.order === undefined && !known.has(g) ? inferred : known).add(g);
175
+ }
176
+ const key = route && fr.depth > 0 ? (route.method ? `${route.method} ${absPath}` : absPath!) : ep.key;
177
+ out.push({
178
+ ...ep,
179
+ key,
180
+ guards: [...new Set([...known, ...inferred])].sort(compareStr),
181
+ guardsInferred: [...inferred].sort(compareStr),
182
+ chain: [...new Set([...ep.chain, ...fr.chainFiles.filter((c) => c !== fr.file)])].sort(compareStr),
183
+ file: fr.file,
184
+ via: [...fr.via],
185
+ });
186
+ }
187
+ const local = routersOf(f);
188
+ for (const m of f.mounts ?? []) {
189
+ if (routerOf(m) !== fr.router) continue;
190
+ const prefix = joinRoutePath(fr.prefix, m.prefix);
191
+ const inherited = [
192
+ ...fr.inherited,
193
+ ...uses.filter((u) => u.order < m.order).map((u) => scoped(u, fr.prefix)),
194
+ ...((m.guards?.length ?? 0) > 0 ? [everything(prefix, m.guards!, false)] : []),
195
+ ];
196
+ let targets = fr.resolved.filter((r) => r.target === m.target);
197
+ if (targets.length === 0 && local.has(m.target)) {
198
+ targets = [{ target: m.target, path: fr.file, facts: f, router: m.target, mounts: fr.resolved }];
199
+ }
200
+ if (targets.length === 0) { unresolved++; continue; }
201
+ if (fr.depth >= maxDepth) { truncated += targets.length; continue; }
202
+ const chainFiles = fr.file === '' || fr.chainFiles.includes(fr.file) ? fr.chainFiles : [...fr.chainFiles, fr.file];
203
+ for (const r of targets) {
204
+ const sub = canonicalFileFacts(r.facts);
205
+ for (const router of r.router !== undefined ? [r.router] : rootRoutersOf(sub)) {
206
+ const id = `${r.path}\u0000${router}`;
207
+ if (fr.stack.has(id)) { truncated++; continue; }
208
+ visit({
209
+ facts: sub, file: r.path, router, prefix, via: [...fr.via, m.prefix], inherited, chainFiles,
210
+ resolved: r.mounts ?? [], depth: fr.depth + 1, stack: new Set([...fr.stack, id]),
211
+ });
212
+ }
213
+ }
214
+ }
215
+ };
216
+
217
+ const root = canonicalFileFacts(fileFacts);
218
+ const file = opts.path ?? '';
219
+ for (const router of opts.router !== undefined ? [opts.router] : rootRoutersOf(root)) {
220
+ visit({
221
+ facts: root, file, router, prefix: '', via: [], inherited: [], chainFiles: [], resolved: resolvedMounts, depth: 0,
222
+ stack: new Set([`${file}\u0000${router}`]),
223
+ });
224
+ }
225
+ return {
226
+ entryPoints: sortBy(out, (e) => e.file, (e) => e.kind, (e) => e.key, (e) => e.symbol, (e) => e.via.join('\u0000')),
227
+ truncated,
228
+ unresolved,
229
+ };
230
+ }
231
+
232
+ /** One root router: a file and the router binding in it ('' = the file's default router). */
233
+ export interface RootRouter {
234
+ path: string;
235
+ router: string;
236
+ }
237
+
238
+ /** Where an imported mount lands: the mounted file, and the router binding in it (absent ⇒ that file's root routers). */
239
+ export interface MountTarget {
240
+ path: string;
241
+ router?: string;
242
+ }
243
+
244
+ /**
245
+ * The ROOT routers of a set of files, PER ROUTER: every (file, router) that registers something and that no other router's
246
+ * mount reaches — so a mounted file's second, unmounted router is still a root. Routers mounted only inside a cycle (files
247
+ * mounting each other) are not dropped: the lowest unreached (path, router) becomes a root, then everything it reaches is
248
+ * covered, repeated until every router is covered (deterministic). `resolveMount` maps a mount of the file at `from` to
249
+ * its target (undefined when unresolved); local-binding mounts stay within their file. Sorted by path, then router. Call
250
+ * `effectiveGuards(facts, mounts, { path, router })` on each (see CALL SITES in the header).
251
+ */
252
+ export function rootRouters(files: ReadonlyMap<string, FileFacts>, resolveMount: (from: string, mount: MountFact) => MountTarget | undefined): RootRouter[] {
253
+ const id = (path: string, router: string) => `${path}\u0000${router}`;
254
+ const nodes = new Map<string, RootRouter>();
255
+ const canon = new Map<string, FileFacts>();
256
+ for (const [path, raw] of files) {
257
+ const f = canonicalFileFacts(raw);
258
+ canon.set(path, f);
259
+ for (const router of routersOf(f)) nodes.set(id(path, router), { path, router });
260
+ }
261
+ const edges = new Map<string, Set<string>>();
262
+ const incoming = new Set<string>();
263
+ const link = (from: string, to: string) => {
264
+ if (to === from || !nodes.has(to)) return;
265
+ let s = edges.get(from);
266
+ if (!s) edges.set(from, (s = new Set()));
267
+ s.add(to);
268
+ incoming.add(to);
269
+ };
270
+ for (const [path, f] of canon) {
271
+ const local = routersOf(f);
272
+ for (const m of f.mounts ?? []) {
273
+ const from = id(path, routerOf(m));
274
+ if (local.has(m.target)) { link(from, id(path, m.target)); continue; }
275
+ const target = resolveMount(path, m);
276
+ const sub = target ? canon.get(target.path) : undefined;
277
+ if (!target || !sub) continue;
278
+ for (const router of target.router !== undefined ? [target.router] : rootRoutersOf(sub)) link(from, id(target.path, router));
279
+ }
280
+ }
281
+ const order = [...nodes.keys()].sort(compareStr);
282
+ const covered = new Set<string>();
283
+ const cover = (start: string) => {
284
+ const stack = [start];
285
+ while (stack.length > 0) {
286
+ const n = stack.pop()!;
287
+ if (covered.has(n)) continue;
288
+ covered.add(n);
289
+ for (const next of edges.get(n) ?? []) stack.push(next);
290
+ }
291
+ };
292
+ const roots: string[] = order.filter((n) => !incoming.has(n));
293
+ for (const r of roots) cover(r);
294
+ for (const n of order) if (!covered.has(n)) { roots.push(n); cover(n); }
295
+ return roots.sort(compareStr).map((n) => nodes.get(n)!);
296
+ }
@@ -53,8 +53,9 @@ export {
53
53
  } from './findings.js';
54
54
  // PRD 41 §B1/§B3/§B5: FileFacts v3, the composite extractor version, FactRule + the incremental evaluator, the fact graph.
55
55
  export {
56
- FACT_KINDS, FACT_KIND_FIELDS, MATCHER_STRENGTH_ORDER, canonicalFileFacts, factKindHashes, factsHash, readFileFacts, isFileFactsV3, projectFileFacts,
56
+ FACT_KINDS, FACT_KIND_FIELDS, MATCHER_STRENGTH_ORDER, canonicalFileFacts, readFileFacts, isFileFactsV3, projectFileFacts,
57
57
  } from './facts/fileFacts.js';
58
+ export { factKindHashes, factsHash } from './facts/factsHash.js';
58
59
  export type {
59
60
  FactKind, FileFacts, SymbolFact, ImportFact, ExportFact, CallFact, ComplexityFact, EntryPointKind, EntryPointFact, CapabilityKind,
60
61
  CapabilityImportFact, MatcherStrength, MatcherUse, TestModifier, TestCaseFact, TestFacts,
@@ -69,6 +70,17 @@ export { emptyFactGraph, updateFactGraph, buildFactGraph, factGraphDump, FACT_GR
69
70
  export type { FactGraph, FactManifestEntry, FactChange, FactGraphOptions, FactGraphUpdate } from './rules/factGraph.js';
70
71
  export { knowledgeFacts, knowledgeInputHash, runKnowledgeRules, packOfRef, KNOWLEDGE_INPUT } from './rules/knowledgeInput.js';
71
72
  export type { KnowledgeFacts, KnowledgeRuleExtra, KnowledgeRule } from './rules/knowledgeInput.js';
73
+ // PRD 41 §B4: the fact store port (pure; hosts implement it) + §C5.1: router mounts and `use` guard semantics.
74
+ export { saveFactGraph, loadFactGraph, factStoreDump, sweepFactStore, memoryFactStore, FACT_STORE_LOAD_ATTEMPTS } from './rules/factStore.js';
75
+ export type {
76
+ Awaitable, FactStorePort, FactStoreVersion, FactStoreManifest, FactManifestDelta, FactStoreSaveResult, StoredFactGraph,
77
+ LoadFactGraphOptions, MemoryFactStore,
78
+ } from './rules/factStore.js';
79
+ export { factsJson, factsWireForm } from './facts/factsHash.js';
80
+ export { normalizeMountPrefix } from './facts/fileFacts.js';
81
+ export type { MountFact, UseFact } from './facts/fileFacts.js';
82
+ export { effectiveGuards, rootRouters, routePathCovers, joinRoutePath, MAX_MOUNT_DEPTH } from './facts/guards.js';
83
+ export type { ResolvedMount, EffectiveEntryPoint, EffectiveGuardsOptions, EffectiveGuardsResult, RootRouter, MountTarget } from './facts/guards.js';
72
84
  export { refAnchor, type FindingAnchor } from './findingBuilder.js';
73
85
  export {
74
86
  identityOf, defaultAnchorOf, composeAnchor, anchorWithSnippet, anchorKindOfRef, kindOfAnchor, normalizeSnippet, DEFAULT_RULE_VERSION,
@@ -19,7 +19,8 @@
19
19
  import { compareStr } from '../refs.js';
20
20
  import { sha256Hex } from '../../retrieval/knowledge-index/sha256.js';
21
21
  import type { Finding } from '../findingModel.js';
22
- import { canonicalFileFacts, factKindHashes, projectFileFacts, type FactKind, type FileFacts } from '../facts/fileFacts.js';
22
+ import { canonicalFileFacts, projectFileFacts, type FactKind, type FileFacts } from '../facts/fileFacts.js';
23
+ import { factKindHashes } from '../facts/factsHash.js';
23
24
  import { DEFAULT_RULE_HOPS, type FactRule } from './factRule.js';
24
25
 
25
26
  /** What the evaluator reads: the current facts by path, the import neighbourhood, and the hashed extra inputs. */
@@ -11,7 +11,8 @@
11
11
 
12
12
  import { canonicalJson, compareStr, isRootFilePath, normalizePath } from '../refs.js';
13
13
  import type { Finding } from '../findingModel.js';
14
- import { canonicalFileFacts, factKindHashes, factsHash, type FactKind, type FileFacts } from '../facts/fileFacts.js';
14
+ import { canonicalFileFacts, type FactKind, type FileFacts } from '../facts/fileFacts.js';
15
+ import { factKindHashes, factsHash, factsWireForm } from '../facts/factsHash.js';
15
16
  import type { FactRule } from './factRule.js';
16
17
  import { evaluateRules, type DerivedRows, type RuleTraceEntry, type RuleWorld } from './evaluateRules.js';
17
18
 
@@ -21,6 +22,11 @@ export interface FactManifestEntry {
21
22
  kinds: Readonly<Record<FactKind, string>>;
22
23
  }
23
24
 
25
+ /** Whether two manifest entries are the same (facts hash, content hash and every kind hash). */
26
+ export const sameManifestEntry = (a: FactManifestEntry | undefined, b: FactManifestEntry | undefined): boolean =>
27
+ a !== undefined && b !== undefined && a.factsHash === b.factsHash && a.contentHash === b.contentHash
28
+ && a.kinds.body === b.kinds.body && a.kinds.content === b.kinds.content && a.kinds.structure === b.kinds.structure;
29
+
24
30
  export interface FactGraph {
25
31
  manifest: ReadonlyMap<string, FactManifestEntry>;
26
32
  /** Facts by facts hash — only those the manifest references. */
@@ -101,7 +107,7 @@ export function factGraphDump(graph: FactGraph): string {
101
107
  return canonicalJson({
102
108
  version: FACT_GRAPH_DUMP_VERSION,
103
109
  manifest: sortedEntries(graph.manifest).map(([path, e]) => ({ path, ...e })),
104
- objects: sortedEntries(graph.objects).map(([hash, facts]) => ({ hash, facts })),
110
+ objects: sortedEntries(graph.objects).map(([hash, facts]) => ({ hash, facts: factsWireForm(facts) })),
105
111
  derived: sortedEntries(graph.derived).map(([, row]) => row),
106
112
  });
107
113
  }