@seanhogg/builderforce-memory 2026.10.67 → 2026.10.69
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.
- package/dist/knowledge/build.d.ts.map +1 -1
- package/dist/knowledge/build.js +7 -2
- package/dist/knowledge/build.js.map +1 -1
- package/dist/knowledge/comparability.d.ts +100 -0
- package/dist/knowledge/comparability.d.ts.map +1 -0
- package/dist/knowledge/comparability.js +149 -0
- package/dist/knowledge/comparability.js.map +1 -0
- package/dist/knowledge/contentHash.d.ts +28 -0
- package/dist/knowledge/contentHash.d.ts.map +1 -0
- package/dist/knowledge/contentHash.js +54 -0
- package/dist/knowledge/contentHash.js.map +1 -0
- package/dist/knowledge/extractFile.d.ts +2 -0
- package/dist/knowledge/extractFile.d.ts.map +1 -1
- package/dist/knowledge/extractFile.js +1 -1
- package/dist/knowledge/extractFile.js.map +1 -1
- package/dist/knowledge/extractJob.d.ts +5 -2
- package/dist/knowledge/extractJob.d.ts.map +1 -1
- package/dist/knowledge/extractJob.js +1 -1
- package/dist/knowledge/extractJob.js.map +1 -1
- package/dist/knowledge/findingBuilder.d.ts +12 -2
- package/dist/knowledge/findingBuilder.d.ts.map +1 -1
- package/dist/knowledge/findingBuilder.js +9 -2
- package/dist/knowledge/findingBuilder.js.map +1 -1
- package/dist/knowledge/findingIdentity.d.ts +52 -0
- package/dist/knowledge/findingIdentity.d.ts.map +1 -0
- package/dist/knowledge/findingIdentity.js +70 -0
- package/dist/knowledge/findingIdentity.js.map +1 -0
- package/dist/knowledge/findingModel.d.ts +30 -0
- package/dist/knowledge/findingModel.d.ts.map +1 -0
- package/dist/knowledge/findingModel.js +6 -0
- package/dist/knowledge/findingModel.js.map +1 -0
- package/dist/knowledge/findings.d.ts.map +1 -1
- package/dist/knowledge/findings.js +17 -9
- package/dist/knowledge/findings.js.map +1 -1
- package/dist/knowledge/godModule.js +2 -2
- package/dist/knowledge/godModule.js.map +1 -1
- package/dist/knowledge/index.d.ts +13 -1
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +8 -0
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/lineage.d.ts +35 -0
- package/dist/knowledge/lineage.d.ts.map +1 -0
- package/dist/knowledge/lineage.js +114 -0
- package/dist/knowledge/lineage.js.map +1 -0
- package/dist/knowledge/merkle.d.ts +65 -0
- package/dist/knowledge/merkle.d.ts.map +1 -0
- package/dist/knowledge/merkle.js +158 -0
- package/dist/knowledge/merkle.js.map +1 -0
- package/dist/knowledge/model.d.ts +20 -17
- package/dist/knowledge/model.d.ts.map +1 -1
- package/dist/knowledge/owedFiles.d.ts +5 -2
- package/dist/knowledge/owedFiles.d.ts.map +1 -1
- package/dist/knowledge/owedFiles.js +20 -5
- package/dist/knowledge/owedFiles.js.map +1 -1
- package/dist/knowledge/pack/encode.d.ts +2 -1
- package/dist/knowledge/pack/encode.d.ts.map +1 -1
- package/dist/knowledge/pack/encode.js +2 -1
- package/dist/knowledge/pack/encode.js.map +1 -1
- package/dist/knowledge/pack/extrDiff.d.ts.map +1 -1
- package/dist/knowledge/pack/extrDiff.js +4 -1
- package/dist/knowledge/pack/extrDiff.js.map +1 -1
- package/dist/knowledge/pack/sections/columns.d.ts +15 -2
- package/dist/knowledge/pack/sections/columns.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/columns.js +16 -1
- package/dist/knowledge/pack/sections/columns.js.map +1 -1
- package/dist/knowledge/pack/sections/extrLists.js +2 -2
- package/dist/knowledge/pack/sections/extrLists.js.map +1 -1
- package/dist/knowledge/pack/sections/facts.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/facts.js +2 -2
- package/dist/knowledge/pack/sections/facts.js.map +1 -1
- package/dist/knowledge/pack/sections/index.d.ts +1 -0
- package/dist/knowledge/pack/sections/index.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/index.js +3 -1
- package/dist/knowledge/pack/sections/index.js.map +1 -1
- package/dist/knowledge/pack/sections/rows.d.ts +1 -0
- package/dist/knowledge/pack/sections/rows.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/rows.js +1 -1
- package/dist/knowledge/pack/sections/rows.js.map +1 -1
- package/dist/knowledge/pack/sections/sites.d.ts +24 -0
- package/dist/knowledge/pack/sections/sites.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/sites.js +96 -0
- package/dist/knowledge/pack/sections/sites.js.map +1 -0
- package/dist/knowledge/pack/types.d.ts +2 -2
- package/dist/knowledge/pack/types.d.ts.map +1 -1
- package/dist/knowledge/plan.d.ts +8 -0
- package/dist/knowledge/plan.d.ts.map +1 -1
- package/dist/knowledge/plan.js +20 -6
- package/dist/knowledge/plan.js.map +1 -1
- package/dist/knowledge/refs.d.ts +4 -0
- package/dist/knowledge/refs.d.ts.map +1 -1
- package/dist/knowledge/refs.js +8 -0
- package/dist/knowledge/refs.js.map +1 -1
- package/dist/knowledge/resolve.d.ts.map +1 -1
- package/dist/knowledge/resolve.js +2 -0
- package/dist/knowledge/resolve.js.map +1 -1
- package/dist/knowledge/reviewModel.d.ts +8 -1
- package/dist/knowledge/reviewModel.d.ts.map +1 -1
- package/dist/knowledge/reviewPrefetch.d.ts +2 -1
- package/dist/knowledge/reviewPrefetch.d.ts.map +1 -1
- package/dist/knowledge/reviewPrefetch.js +8 -4
- package/dist/knowledge/reviewPrefetch.js.map +1 -1
- package/dist/knowledge/sources.d.ts +11 -2
- package/dist/knowledge/sources.d.ts.map +1 -1
- package/dist/knowledge/sources.js +19 -3
- package/dist/knowledge/sources.js.map +1 -1
- package/dist/knowledge/symbolEdges.d.ts +39 -0
- package/dist/knowledge/symbolEdges.d.ts.map +1 -0
- package/dist/knowledge/symbolEdges.js +71 -0
- package/dist/knowledge/symbolEdges.js.map +1 -0
- package/dist/knowledge/truncate.d.ts.map +1 -1
- package/dist/knowledge/truncate.js +4 -0
- package/dist/knowledge/truncate.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/exports.snapshot.json +34 -0
- package/src/contract/regression.contentHash.test.ts +30 -0
- package/src/contract/regression.lineage.test.ts +27 -0
- package/src/contract/regression.listing.test.ts +85 -0
- package/src/contract/regression.symbolEdges.test.ts +323 -0
- package/src/contract/symbolEdges.golden.json +345 -0
- package/src/contract/types.contract.test.ts +68 -1
- package/src/knowledge/build.ts +7 -2
- package/src/knowledge/comparability.analysis.test.ts +73 -0
- package/src/knowledge/comparability.test.ts +44 -0
- package/src/knowledge/comparability.ts +182 -0
- package/src/knowledge/contentHash.test.ts +81 -0
- package/src/knowledge/contentHash.ts +55 -0
- package/src/knowledge/extractFile.ts +4 -2
- package/src/knowledge/extractJob.ts +5 -3
- package/src/knowledge/findingBuilder.ts +17 -2
- package/src/knowledge/findingIdentity.test.ts +102 -0
- package/src/knowledge/findingIdentity.ts +92 -0
- package/src/knowledge/findingModel.ts +32 -0
- package/src/knowledge/findings.ts +17 -9
- package/src/knowledge/godModule.ts +2 -2
- package/src/knowledge/index.ts +22 -1
- package/src/knowledge/lineage.test.ts +106 -0
- package/src/knowledge/lineage.ts +123 -0
- package/src/knowledge/merkle.test.ts +137 -0
- package/src/knowledge/merkle.ts +164 -0
- package/src/knowledge/model.ts +22 -19
- package/src/knowledge/owedFiles.contentHash.test.ts +58 -0
- package/src/knowledge/owedFiles.ts +23 -5
- package/src/knowledge/pack/encode.ts +2 -1
- package/src/knowledge/pack/extrDiff.ts +4 -1
- package/src/knowledge/pack/sections/columns.ts +16 -1
- package/src/knowledge/pack/sections/extrLists.ts +2 -2
- package/src/knowledge/pack/sections/facts.ts +2 -2
- package/src/knowledge/pack/sections/index.ts +3 -1
- package/src/knowledge/pack/sections/rows.ts +1 -1
- package/src/knowledge/pack/sections/sites.ts +87 -0
- package/src/knowledge/pack/types.ts +2 -2
- package/src/knowledge/plan.ts +28 -6
- package/src/knowledge/refs.ts +10 -0
- package/src/knowledge/resolve.ts +2 -0
- package/src/knowledge/reviewModel.ts +8 -1
- package/src/knowledge/reviewPrefetch.ts +10 -6
- package/src/knowledge/sources.ts +22 -3
- package/src/knowledge/symbolEdges.ts +72 -0
- package/src/knowledge/truncate.ts +3 -1
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/knowledge/merkle.test.ts — directory Merkle hashes (PRD 41 §B2 step 4).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { jest } from '@jest/globals';
|
|
6
|
+
import { canonicalListing, changedPaths, merkleTree, type MerkleTree } from './merkle.js';
|
|
7
|
+
|
|
8
|
+
const FILES = [
|
|
9
|
+
{ path: 'README.md', hash: 'h-readme' },
|
|
10
|
+
{ path: 'src/a.ts', hash: 'h-a' },
|
|
11
|
+
{ path: 'src/lib/b.ts', hash: 'h-b' },
|
|
12
|
+
{ path: 'src/lib/c.ts', hash: 'h-c' },
|
|
13
|
+
{ path: 'docs/guide/intro.md', hash: 'h-intro' },
|
|
14
|
+
{ path: 'docs/guide/deep/more.md', hash: 'h-more' },
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
/** A copy of `files` with `path` at a new hash. */
|
|
18
|
+
const edited = (files: typeof FILES, path: string, hash: string) => files.map((f) => (f.path === path ? { path, hash } : f));
|
|
19
|
+
|
|
20
|
+
describe('merkleTree', () => {
|
|
21
|
+
it('gives the same root and dirs for any input order', () => {
|
|
22
|
+
const a = merkleTree(FILES);
|
|
23
|
+
const b = merkleTree([...FILES].reverse());
|
|
24
|
+
expect(b.root).toBe(a.root);
|
|
25
|
+
expect([...b.dirs]).toEqual([...a.dirs]);
|
|
26
|
+
expect(a.dirs.get('')).toBe(a.root);
|
|
27
|
+
expect([...a.dirs.keys()].sort()).toEqual(['', 'docs', 'docs/guide', 'docs/guide/deep', 'src', 'src/lib']);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('normalizes paths: backslashes, a leading ./, NFC', () => {
|
|
31
|
+
const a = merkleTree([{ path: 'src/café.ts', hash: 'h' }]);
|
|
32
|
+
expect(merkleTree([{ path: '.\\src\\café.ts', hash: 'h' }]).root).toBe(a.root);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('sorts entries bytewise (code-point order), not by UTF-16 unit', () => {
|
|
36
|
+
const tree = merkleTree([{ path: '\u{1F600}.md', hash: 'h1' }, { path: '~.md', hash: 'h2' }, { path: 'Z.md', hash: 'h3' }]);
|
|
37
|
+
expect(tree.entries.get('')!.map((e) => e.name)).toEqual(['Z.md', '~.md', '\u{1F600}.md']);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('a file change changes only its ancestors', () => {
|
|
41
|
+
const before = merkleTree(FILES);
|
|
42
|
+
const after = merkleTree(edited(FILES, 'src/lib/b.ts', 'h-b2'));
|
|
43
|
+
expect(after.root).not.toBe(before.root);
|
|
44
|
+
for (const dir of before.dirs.keys()) {
|
|
45
|
+
const ancestor = dir === '' || dir === 'src' || dir === 'src/lib';
|
|
46
|
+
expect({ dir, changed: after.dirs.get(dir) !== before.dirs.get(dir) }).toEqual({ dir, changed: ancestor });
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('a file and a directory of one name differ in kind, so they never hash alike', () => {
|
|
51
|
+
expect(merkleTree([{ path: 'x', hash: 'h' }]).root).not.toBe(merkleTree([{ path: 'x/y', hash: 'h' }]).root);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('refuses a path listed twice at different hashes, or as both a file and a directory', () => {
|
|
55
|
+
expect(() => merkleTree([{ path: 'a', hash: '1' }, { path: 'a', hash: '2' }])).toThrow(/listed twice/);
|
|
56
|
+
expect(() => merkleTree([{ path: 'a', hash: '1' }, { path: 'a/b', hash: '2' }])).toThrow(/file and a directory/);
|
|
57
|
+
expect(() => merkleTree([{ path: 'a/b', hash: '2' }, { path: 'a', hash: '1' }])).toThrow(/file and a directory/);
|
|
58
|
+
expect(() => merkleTree([{ path: 'a//b', hash: '1' }])).toThrow(/not a file path/);
|
|
59
|
+
expect(() => merkleTree([{ path: 'a/./b', hash: '1' }])).toThrow(/not a file path/);
|
|
60
|
+
expect(() => merkleTree([{ path: 'a/../b', hash: '1' }])).toThrow(/not a file path/);
|
|
61
|
+
expect(() => merkleTree([{ path: '..', hash: '1' }])).toThrow(/not a file path/);
|
|
62
|
+
expect(merkleTree([{ path: 'a', hash: '1' }, { path: 'a', hash: '1' }]).root).toBe(merkleTree([{ path: 'a', hash: '1' }]).root);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('an empty listing has a root', () => {
|
|
66
|
+
expect(merkleTree([]).root).toMatch(/^[0-9a-f]{64}$/);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('canonicalListing', () => {
|
|
71
|
+
const NFC = 'café.md', NFD = 'café.md';
|
|
72
|
+
|
|
73
|
+
it('keeps the first of a colliding pair by (normalized, listed) bytewise, in any input order, and never makes merkleTree throw', () => {
|
|
74
|
+
const pair = [{ path: NFC, hash: 'h1' }, { path: NFD, hash: 'h2' }];
|
|
75
|
+
for (const input of [pair, [...pair].reverse()]) {
|
|
76
|
+
const { kept, skipped } = canonicalListing(input);
|
|
77
|
+
// NFD's 'e' (U+0065) sorts before NFC's 'é' (U+00E9): the NFD spelling is kept, under its NFC path.
|
|
78
|
+
expect(kept).toEqual([{ path: NFC, hash: 'h2', listedPath: NFD }]);
|
|
79
|
+
expect(skipped).toEqual([{ path: NFC, reason: 'duplicate-path' }]);
|
|
80
|
+
expect(() => merkleTree(kept)).not.toThrow();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('skips invalid paths, a path listed twice, and a path below a kept file', () => {
|
|
85
|
+
const { kept, skipped } = canonicalListing([
|
|
86
|
+
{ path: 'a', hash: '1' }, { path: 'a/b', hash: '2' }, { path: 'x.md', hash: '3' }, { path: './x.md', hash: '4' },
|
|
87
|
+
{ path: '../up.md', hash: '5' }, { path: 'p/./q', hash: '6' }, { path: 'p//q', hash: '7' }, { path: '', hash: '8' }, { path: 'src\\k.ts', hash: '9' },
|
|
88
|
+
]);
|
|
89
|
+
expect(kept).toEqual([{ path: 'a', hash: '1' }, { path: 'src/k.ts', hash: '9', listedPath: 'src\\k.ts' }, { path: 'x.md', hash: '4', listedPath: './x.md' }]);
|
|
90
|
+
expect(skipped).toEqual([
|
|
91
|
+
{ path: '../up.md', reason: 'invalid-path' }, { path: 'p/./q', reason: 'invalid-path' }, { path: 'p//q', reason: 'invalid-path' },
|
|
92
|
+
{ path: '', reason: 'invalid-path' }, { path: 'a/b', reason: 'duplicate-path' }, { path: 'x.md', reason: 'duplicate-path' },
|
|
93
|
+
]);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
/** `tree` with its `entries.get` spied, so a test can see which directories a walk opened. */
|
|
98
|
+
function spied(tree: MerkleTree) {
|
|
99
|
+
const entries = new Map(tree.entries);
|
|
100
|
+
const get = jest.spyOn(entries, 'get');
|
|
101
|
+
return { tree: { ...tree, entries } as MerkleTree, opened: () => new Set(get.mock.calls.map((c) => c[0])) };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
describe('changedPaths', () => {
|
|
105
|
+
it('returns exactly the changed, added and removed files', () => {
|
|
106
|
+
const next = [
|
|
107
|
+
...edited(FILES, 'src/lib/b.ts', 'h-b2').filter((f) => f.path !== 'docs/guide/deep/more.md'),
|
|
108
|
+
{ path: 'src/lib/new.ts', hash: 'h-new' },
|
|
109
|
+
];
|
|
110
|
+
expect(changedPaths(merkleTree(FILES), merkleTree(next))).toEqual(['docs/guide/deep/more.md', 'src/lib/b.ts', 'src/lib/new.ts']);
|
|
111
|
+
expect(changedPaths(merkleTree(next), merkleTree(FILES))).toEqual(['docs/guide/deep/more.md', 'src/lib/b.ts', 'src/lib/new.ts']);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('is empty for equal trees and opens no directory', () => {
|
|
115
|
+
const after = spied(merkleTree(FILES));
|
|
116
|
+
expect(changedPaths(merkleTree(FILES), after.tree)).toEqual([]);
|
|
117
|
+
expect(after.opened().size).toBe(0);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('never descends into a subtree whose hash is equal', () => {
|
|
121
|
+
const before = spied(merkleTree(FILES));
|
|
122
|
+
const after = spied(merkleTree(edited(FILES, 'src/lib/b.ts', 'h-b2')));
|
|
123
|
+
expect(changedPaths(before.tree, after.tree)).toEqual(['src/lib/b.ts']);
|
|
124
|
+
expect([...after.opened()].sort()).toEqual(['', 'src', 'src/lib']);
|
|
125
|
+
expect([...before.opened()].sort()).toEqual(['', 'src', 'src/lib']);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('a file replaced by a directory reports both sides', () => {
|
|
129
|
+
const before = merkleTree([{ path: 'x', hash: 'h' }, { path: 'keep.md', hash: 'k' }]);
|
|
130
|
+
const after = merkleTree([{ path: 'x/y', hash: 'h' }, { path: 'x/z/w', hash: 'h2' }, { path: 'keep.md', hash: 'k' }]);
|
|
131
|
+
expect(changedPaths(before, after)).toEqual(['x', 'x/y', 'x/z/w']);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('from an empty tree, every file is added', () => {
|
|
135
|
+
expect(changedPaths(merkleTree([]), merkleTree(FILES))).toEqual(FILES.map((f) => f.path).sort());
|
|
136
|
+
});
|
|
137
|
+
});
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/merkle.ts — directory Merkle hashes over a root's file listing (PRD 41 §B2 step 4).
|
|
3
|
+
*
|
|
4
|
+
* A directory's hash is sha256 over the canonical JSON of its entries `{ name, kind, hash }`
|
|
5
|
+
* (`kind` 'file' | 'dir'), sorted by name. An equal directory hash means an equal subtree, so
|
|
6
|
+
* {@link changedPaths} skips it and two trees compare in O(changed).
|
|
7
|
+
*
|
|
8
|
+
* Paths go through the ONE `normalizePath` (refs.ts: POSIX, no leading `./` or `/`, NFC) and sort bytewise (§B8.1). The
|
|
9
|
+
* root directory's key is `""`; a nested one's is its path (`a`, `a/b`). Any input order gives
|
|
10
|
+
* the same tree.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { sha256Hex } from '../retrieval/knowledge-index/sha256.js';
|
|
14
|
+
import { canonicalJson, isRootFilePath, normalizePath } from './refs.js';
|
|
15
|
+
|
|
16
|
+
export type MerkleKind = 'file' | 'dir';
|
|
17
|
+
|
|
18
|
+
export interface MerkleEntry {
|
|
19
|
+
name: string;
|
|
20
|
+
kind: MerkleKind;
|
|
21
|
+
/** A file's content hash, or a directory's Merkle hash. */
|
|
22
|
+
hash: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface MerkleTree {
|
|
26
|
+
/** The root directory's hash (`dirs.get('')`). */
|
|
27
|
+
root: string;
|
|
28
|
+
/** Directory path → its Merkle hash; the root is `""`. */
|
|
29
|
+
dirs: ReadonlyMap<string, string>;
|
|
30
|
+
/** Directory path → its entries, sorted by name. */
|
|
31
|
+
entries: ReadonlyMap<string, ReadonlyArray<MerkleEntry>>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Bytewise UTF-8 order (code-point order), which UTF-16 `<` is not for astral characters. */
|
|
35
|
+
export function compareBytewise(a: string, b: string): number {
|
|
36
|
+
const n = Math.min(a.length, b.length);
|
|
37
|
+
for (let i = 0; i < n; i++) {
|
|
38
|
+
const x = a.codePointAt(i)!, y = b.codePointAt(i)!;
|
|
39
|
+
if (x !== y) return x < y ? -1 : 1;
|
|
40
|
+
if (x > 0xffff) i++;
|
|
41
|
+
}
|
|
42
|
+
return a.length - b.length;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const dirOf = (path: string): string => {
|
|
46
|
+
const at = path.lastIndexOf('/');
|
|
47
|
+
return at < 0 ? '' : path.slice(0, at);
|
|
48
|
+
};
|
|
49
|
+
const nameOf = (path: string): string => path.slice(path.lastIndexOf('/') + 1);
|
|
50
|
+
const joinPath = (dir: string, name: string): string => (dir === '' ? name : `${dir}/${name}`);
|
|
51
|
+
|
|
52
|
+
/** Why {@link canonicalListing} left a listed path out. */
|
|
53
|
+
export type ListingSkipReason = 'invalid-path' | 'duplicate-path';
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A host listing made safe for {@link merkleTree} — which then never throws — without depending on input order:
|
|
57
|
+
* • every path goes through the ONE {@link normalizePath} (POSIX, NFC); the source's own spelling is kept as `listedPath`
|
|
58
|
+
* when it differs, so a read still opens the file the source listed;
|
|
59
|
+
* • a path that is empty or has an empty, `.` or `..` segment is skipped as `invalid-path`;
|
|
60
|
+
* • of paths that collide once normalized (an NFC/NFD pair, a path listed twice) the FIRST by (normalized path, listed path),
|
|
61
|
+
* bytewise, is kept and the rest are skipped as `duplicate-path`; so is a path below a kept FILE (`a` and `a/b`).
|
|
62
|
+
* Skipped entries carry the listed spelling. `kept` is sorted bytewise by normalized path.
|
|
63
|
+
*/
|
|
64
|
+
export function canonicalListing<T extends { path: string }>(entries: readonly T[]): {
|
|
65
|
+
kept: Array<T & { listedPath?: string }>;
|
|
66
|
+
skipped: Array<{ path: string; reason: ListingSkipReason }>;
|
|
67
|
+
} {
|
|
68
|
+
const skipped: Array<{ path: string; reason: ListingSkipReason }> = [];
|
|
69
|
+
const candidates: Array<{ entry: T; path: string }> = [];
|
|
70
|
+
for (const entry of entries) {
|
|
71
|
+
const path = normalizePath(entry.path);
|
|
72
|
+
if (isRootFilePath(path)) candidates.push({ entry, path });
|
|
73
|
+
else skipped.push({ path: entry.path, reason: 'invalid-path' });
|
|
74
|
+
}
|
|
75
|
+
candidates.sort((a, b) => compareBytewise(a.path, b.path) || compareBytewise(a.entry.path, b.entry.path));
|
|
76
|
+
const files = new Set<string>();
|
|
77
|
+
const kept: Array<T & { listedPath?: string }> = [];
|
|
78
|
+
for (const { entry, path } of candidates) {
|
|
79
|
+
let clash = files.has(path);
|
|
80
|
+
for (let dir = dirOf(path); !clash && dir !== ''; dir = dirOf(dir)) clash = files.has(dir);
|
|
81
|
+
if (clash) { skipped.push({ path: entry.path, reason: 'duplicate-path' }); continue; }
|
|
82
|
+
files.add(path);
|
|
83
|
+
kept.push({ ...entry, path, ...(entry.path !== path ? { listedPath: entry.path } : {}) });
|
|
84
|
+
}
|
|
85
|
+
return { kept, skipped };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The Merkle tree of a file listing. Throws on an invalid path, a path listed twice with different hashes, or a path
|
|
90
|
+
* that is both a file and a directory — each would make the root depend on input order. {@link canonicalListing}
|
|
91
|
+
* first makes a host listing safe.
|
|
92
|
+
*/
|
|
93
|
+
export function merkleTree(files: ReadonlyArray<{ path: string; hash: string }>): MerkleTree {
|
|
94
|
+
const children = new Map<string, Map<string, { kind: MerkleKind; hash?: string }>>([['', new Map()]]);
|
|
95
|
+
const childrenOf = (dir: string) => {
|
|
96
|
+
let c = children.get(dir);
|
|
97
|
+
if (!c) children.set(dir, (c = new Map()));
|
|
98
|
+
return c;
|
|
99
|
+
};
|
|
100
|
+
for (const f of files) {
|
|
101
|
+
const path = normalizePath(f.path);
|
|
102
|
+
if (!isRootFilePath(path)) throw new Error(`merkleTree: not a file path: ${JSON.stringify(f.path)}`);
|
|
103
|
+
const own = childrenOf(dirOf(path)).get(nameOf(path));
|
|
104
|
+
if (own && (own.kind !== 'file' || own.hash !== f.hash)) throw new Error(`merkleTree: ${path} listed twice or as a file and a directory`);
|
|
105
|
+
childrenOf(dirOf(path)).set(nameOf(path), { kind: 'file', hash: f.hash });
|
|
106
|
+
for (let dir = dirOf(path); dir !== ''; dir = dirOf(dir)) {
|
|
107
|
+
const slot = childrenOf(dirOf(dir));
|
|
108
|
+
const was = slot.get(nameOf(dir));
|
|
109
|
+
if (was?.kind === 'file') throw new Error(`merkleTree: ${dir} is both a file and a directory`);
|
|
110
|
+
if (was) break; // its ancestors are registered already
|
|
111
|
+
slot.set(nameOf(dir), { kind: 'dir' });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const dirs = new Map<string, string>();
|
|
116
|
+
const entries = new Map<string, MerkleEntry[]>();
|
|
117
|
+
// Deepest first, so every child directory is hashed before its parent.
|
|
118
|
+
const order = [...children.keys()].sort((a, b) => depth(b) - depth(a) || compareBytewise(a, b));
|
|
119
|
+
for (const dir of order) {
|
|
120
|
+
const list = [...children.get(dir)!].map(([name, c]): MerkleEntry => ({
|
|
121
|
+
name, kind: c.kind, hash: c.kind === 'file' ? c.hash! : dirs.get(joinPath(dir, name))!,
|
|
122
|
+
})).sort((a, b) => compareBytewise(a.name, b.name));
|
|
123
|
+
entries.set(dir, list);
|
|
124
|
+
dirs.set(dir, sha256Hex(canonicalJson(list)));
|
|
125
|
+
}
|
|
126
|
+
return { root: dirs.get('')!, dirs, entries };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const depth = (dir: string): number => (dir === '' ? 0 : dir.split('/').length);
|
|
130
|
+
|
|
131
|
+
/** Every file path under `dir` in `tree`, appended to `out`. */
|
|
132
|
+
function filesUnder(tree: MerkleTree, dir: string, out: string[]): void {
|
|
133
|
+
for (const e of tree.entries.get(dir) ?? []) {
|
|
134
|
+
const path = joinPath(dir, e.name);
|
|
135
|
+
if (e.kind === 'file') out.push(path);
|
|
136
|
+
else filesUnder(tree, path, out);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The file paths changed, added or removed between two trees, sorted bytewise. Walks top-down
|
|
142
|
+
* and never descends into a directory whose hash is equal on both sides.
|
|
143
|
+
*/
|
|
144
|
+
export function changedPaths(before: MerkleTree, after: MerkleTree): string[] {
|
|
145
|
+
const out: string[] = [];
|
|
146
|
+
const walk = (dir: string): void => {
|
|
147
|
+
if (before.dirs.get(dir) === after.dirs.get(dir)) return;
|
|
148
|
+
const was = new Map((before.entries.get(dir) ?? []).map((e) => [e.name, e] as const));
|
|
149
|
+
const is = new Map((after.entries.get(dir) ?? []).map((e) => [e.name, e] as const));
|
|
150
|
+
for (const name of new Set([...was.keys(), ...is.keys()])) {
|
|
151
|
+
const a = was.get(name), b = is.get(name);
|
|
152
|
+
const path = joinPath(dir, name);
|
|
153
|
+
if (a?.kind === 'dir' && b?.kind === 'dir') { if (a.hash !== b.hash) walk(path); continue; }
|
|
154
|
+
if (a && b && a.kind === 'file' && b.kind === 'file') { if (a.hash !== b.hash) out.push(path); continue; }
|
|
155
|
+
// Added, removed, or a file replaced by a directory (or back): every file on either side.
|
|
156
|
+
for (const [side, e] of [[before, a], [after, b]] as const) {
|
|
157
|
+
if (e?.kind === 'file') out.push(path);
|
|
158
|
+
else if (e) filesUnder(side, path, out);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
walk('');
|
|
163
|
+
return [...new Set(out)].sort(compareBytewise);
|
|
164
|
+
}
|
package/src/knowledge/model.ts
CHANGED
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
|
|
13
13
|
import type { SourceDocument, ParsedSection } from '../ingest/types.js';
|
|
14
14
|
import type { IndexDoc, SimilarPair } from '../retrieval/knowledge-index/types.js';
|
|
15
|
+
import type { Finding, FindingRule } from './findingModel.js';
|
|
16
|
+
|
|
17
|
+
/** The finding types live beside the rules (findingModel.ts); re-exported so one import serves. */
|
|
18
|
+
export type { Finding, FindingRule } from './findingModel.js';
|
|
15
19
|
|
|
16
20
|
/** `src:${rootId}::${path}` · `ent:${pack}:${normalizedName}` · `fact:${key}` (claims are `fact:claim:${hash}`). */
|
|
17
21
|
export type NodeRef = `src:${string}` | `ent:${string}` | `fact:${string}`;
|
|
@@ -48,7 +52,8 @@ export interface KEdge {
|
|
|
48
52
|
from: NodeRef;
|
|
49
53
|
to: NodeRef;
|
|
50
54
|
/** 'references' | 'links' | 'contains' | 'mentions' | 'duplicate' | 'near-duplicate'
|
|
51
|
-
* | 'contradicts' | 'agrees' | 'supersedes' | 'about' | 'co-changed'
|
|
55
|
+
* | 'contradicts' | 'agrees' | 'supersedes' | 'about' | 'co-changed', and the symbol-level
|
|
56
|
+
* 'extends' | 'implements' | 'composes' | 'uses' | 'calls' (`SYMBOL_EDGE_KINDS`) — a new kind is a value. */
|
|
52
57
|
kind: string;
|
|
53
58
|
weight: number;
|
|
54
59
|
origin: Origin;
|
|
@@ -63,6 +68,22 @@ export interface KEdge {
|
|
|
63
68
|
* target) — so the cached extract never depends on which other files exist. Never emitted.
|
|
64
69
|
*/
|
|
65
70
|
byName?: string;
|
|
71
|
+
/**
|
|
72
|
+
* `calls` edges: every call of `to` in the body of `from`, in source order (`order`). One deduped edge per
|
|
73
|
+
* (from, to, kind) — a repeated call is another site, never another edge. Absent elsewhere; never empty.
|
|
74
|
+
*/
|
|
75
|
+
sites?: CallSite[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Where a call is written: the root-relative `path`, 1-based `line` and `column`, and `order` — the call's position
|
|
80
|
+
* among ALL calls in the caller's body (0-based, source order), so a sequence shows the real call order.
|
|
81
|
+
*/
|
|
82
|
+
export interface CallSite {
|
|
83
|
+
path: string;
|
|
84
|
+
line: number;
|
|
85
|
+
column: number;
|
|
86
|
+
order: number;
|
|
66
87
|
}
|
|
67
88
|
|
|
68
89
|
/** Output of a pack's deterministic extractor (or analyzer) for one source. Never stored. */
|
|
@@ -85,24 +106,6 @@ export interface ExtractContext {
|
|
|
85
106
|
resolvePath(fromPath: string, target: string): string | undefined;
|
|
86
107
|
}
|
|
87
108
|
|
|
88
|
-
/** A finding a person can act on. `kind` is pack-defined; `requestKey` names an i18n template. */
|
|
89
|
-
export interface Finding {
|
|
90
|
-
id: string;
|
|
91
|
-
kind: string;
|
|
92
|
-
pack: string;
|
|
93
|
-
nodes: NodeRef[];
|
|
94
|
-
severity: 'info' | 'warn' | 'error';
|
|
95
|
-
/** Numbers/strings for the label and the Fix-with-agent template. */
|
|
96
|
-
params: Record<string, string | number>;
|
|
97
|
-
/** i18n key of the fixed Fix-with-agent request (e.g. 'knowledgeGraph.fix.cloneGroup'). */
|
|
98
|
-
requestKey?: string;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export interface FindingRule {
|
|
102
|
-
id: string;
|
|
103
|
-
find(graph: KnowledgeGraph): Finding[];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
109
|
export interface PartitionRule {
|
|
107
110
|
id: string;
|
|
108
111
|
/** 0 = top. */
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/knowledge/owedFiles.contentHash.test.ts — the content-hash rule in the extractor entry (PRD 41 §B2 step 0).
|
|
3
|
+
* The rule never owes a file by its ABSENCE: a host switching to `contentHash` moves the hash of every CRLF file, and
|
|
4
|
+
* those re-extract by content. Only a RECORDED rule that differs from today's owes every file one re-extraction.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { InMemoryIngestManifest } from '../ingest/IngestionPipeline.js';
|
|
8
|
+
import { CONTENT_HASH_RULE } from './contentHash.js';
|
|
9
|
+
import { isOwed, loadExtractorState, owedCalls, saveExtractorState } from './owedFiles.js';
|
|
10
|
+
import { DOCS_PACK } from './packs/docs.js';
|
|
11
|
+
import { planDetail } from './plan.js';
|
|
12
|
+
import { reviewDirectory } from './review.js';
|
|
13
|
+
import { EXTRACTOR_VERSION, extractorManifestKey } from './sources.js';
|
|
14
|
+
import { ALL, args, fakeJudge, FILES, memSource, memStore, ROOT } from './testkit.test-util.js';
|
|
15
|
+
|
|
16
|
+
const key = extractorManifestKey(ROOT);
|
|
17
|
+
|
|
18
|
+
describe('the content-hash rule in the extractor entry', () => {
|
|
19
|
+
it("an entry without a rule record owes nothing by it, at today's version", async () => {
|
|
20
|
+
const manifest = new InMemoryIngestManifest();
|
|
21
|
+
await manifest.set(key, [{ id: 'version', hash: String(EXTRACTOR_VERSION) }]);
|
|
22
|
+
const state = await loadExtractorState(manifest, ROOT);
|
|
23
|
+
expect(state.contentHashRule).toBeUndefined();
|
|
24
|
+
expect(isOwed(state, 'a.md')).toBe(false);
|
|
25
|
+
expect(owedCalls(state, 'a.md')).toBe(0);
|
|
26
|
+
expect(isOwed({ version: EXTRACTOR_VERSION, owed: new Map() }, 'a.md')).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("today's rule recorded is read back and owes nothing", async () => {
|
|
30
|
+
const manifest = new InMemoryIngestManifest();
|
|
31
|
+
await manifest.set(key, [{ id: 'version', hash: String(EXTRACTOR_VERSION) }, { id: 'contentHash', hash: CONTENT_HASH_RULE }]);
|
|
32
|
+
const state = await loadExtractorState(manifest, ROOT);
|
|
33
|
+
expect(state.contentHashRule).toBe(CONTENT_HASH_RULE);
|
|
34
|
+
expect(isOwed(state, 'a.md')).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("saving today's (first) rule writes no record, so the entry keeps its pre-rule shape", async () => {
|
|
38
|
+
const manifest = new InMemoryIngestManifest();
|
|
39
|
+
await saveExtractorState(manifest, ROOT, new Map());
|
|
40
|
+
expect((await manifest.get(key))!.map((f) => f.id)).toEqual(['version']);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("another recorded rule at today's version owes every file one re-extraction, once", async () => {
|
|
44
|
+
const source = memSource({ ...FILES });
|
|
45
|
+
const store = memStore();
|
|
46
|
+
const manifest = new InMemoryIngestManifest();
|
|
47
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
48
|
+
const owedPaths = async () => (await planDetail(source, manifest, [DOCS_PACK])).files.filter((f) => f.owed).map((f) => f.path);
|
|
49
|
+
expect(await owedPaths()).toEqual([]);
|
|
50
|
+
|
|
51
|
+
await manifest.set(key, [{ id: 'version', hash: String(EXTRACTOR_VERSION) }, { id: 'contentHash', hash: 'eol-lf@0' }]);
|
|
52
|
+
expect(owedCalls(await loadExtractorState(manifest, ROOT), 'a.md')).toBe(1);
|
|
53
|
+
expect(await owedPaths()).toEqual(ALL);
|
|
54
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
55
|
+
expect(await owedPaths()).toEqual([]);
|
|
56
|
+
expect((await manifest.get(key))!.map((f) => f.id)).toEqual(['version']);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
* retired it drops out.
|
|
13
13
|
*
|
|
14
14
|
* Stored under {@link extractorManifestKey} as fingerprints: `{ id: 'version', hash: '<version>' }`
|
|
15
|
-
* plus `{ id: 'documents', hash: '<pdf,docx|->' }` (the document types the packs claimed, see {@link withDocuments})
|
|
15
|
+
* plus `{ id: 'documents', hash: '<pdf,docx|->' }` (the document types the packs claimed, see {@link withDocuments}),
|
|
16
|
+
* `{ id: 'contentHash', hash: '<rule>' }` once {@link CONTENT_HASH_RULE} moves past {@link BASELINE_CONTENT_HASH_RULE} (no record reads as the baseline; another rule is an upgrade),
|
|
16
17
|
* and one `{ id: 'owed:<path>', hash: '<calls>' }` per owed file, where `<calls>` is the
|
|
17
18
|
* decimal number of model calls the file is still expected to cost (the plan's estimate):
|
|
18
19
|
* • a file the run reached but did not finish: the sections the budget or a failed call left
|
|
@@ -27,11 +28,13 @@
|
|
|
27
28
|
*/
|
|
28
29
|
|
|
29
30
|
import type { IngestManifest } from '../ingest/types.js';
|
|
31
|
+
import { BASELINE_CONTENT_HASH_RULE, CONTENT_HASH_RULE, contentHashRuleOf } from './contentHash.js';
|
|
30
32
|
import { compareStr } from './refs.js';
|
|
31
33
|
import { EXTRACTOR_VERSION, extOf, extractorManifestKey, isBinaryPath, isDocumentPath } from './sources.js';
|
|
32
34
|
|
|
33
35
|
const VERSION_ID = 'version';
|
|
34
36
|
const DOCUMENTS_ID = 'documents';
|
|
37
|
+
const CONTENT_HASH_ID = 'contentHash';
|
|
35
38
|
/** No document type claimed — also what an entry with no `documents` record means, so a root that never had a host parser stores nothing extra. */
|
|
36
39
|
const NO_DOCUMENTS = '-';
|
|
37
40
|
const OWED_PREFIX = 'owed:';
|
|
@@ -42,6 +45,8 @@ const UPGRADE_CALLS = 1;
|
|
|
42
45
|
export interface ExtractorState {
|
|
43
46
|
/** The version the files were last reviewed with; undefined when never recorded. */
|
|
44
47
|
version: number | undefined;
|
|
48
|
+
/** The content-hash rule (contentHash.ts) the files were hashed under, when the entry records one; absent for `eol-lf@1` and for every entry written before the rule existed. */
|
|
49
|
+
contentHashRule?: string;
|
|
45
50
|
/** Paths a previous run left unfinished under that version → model calls each still costs. */
|
|
46
51
|
owed: ReadonlyMap<string, number>;
|
|
47
52
|
/** Consecutive transient failures of a path at one content hash (stored beside its owed entry); a success or a new hash resets it. Optional: a caller that builds a state by hand need not know it. */
|
|
@@ -82,6 +87,7 @@ export function withDocuments(state: ExtractorState, now: string | undefined): E
|
|
|
82
87
|
export async function loadExtractorState(manifest: IngestManifest, rootId: string): Promise<ExtractorState> {
|
|
83
88
|
let version: number | undefined;
|
|
84
89
|
let documents: string | undefined;
|
|
90
|
+
let contentHashRule: string | undefined;
|
|
85
91
|
const owed = new Map<string, number>();
|
|
86
92
|
const failures = new Map<string, { hash: string; count: number }>();
|
|
87
93
|
for (const { id, hash } of (await manifest.get(extractorManifestKey(rootId))) ?? []) {
|
|
@@ -90,6 +96,8 @@ export async function loadExtractorState(manifest: IngestManifest, rootId: strin
|
|
|
90
96
|
if (Number.isFinite(v)) version = v;
|
|
91
97
|
} else if (id === DOCUMENTS_ID) {
|
|
92
98
|
documents = hash;
|
|
99
|
+
} else if (id === CONTENT_HASH_ID) {
|
|
100
|
+
contentHashRule = hash;
|
|
93
101
|
} else if (id.startsWith(FAIL_PREFIX)) {
|
|
94
102
|
const at = hash.indexOf(':');
|
|
95
103
|
const count = Number(hash.slice(0, at));
|
|
@@ -99,15 +107,24 @@ export async function loadExtractorState(manifest: IngestManifest, rootId: strin
|
|
|
99
107
|
owed.set(id.slice(OWED_PREFIX.length), Number.isFinite(calls) && calls >= 0 ? calls : UPGRADE_CALLS);
|
|
100
108
|
}
|
|
101
109
|
}
|
|
102
|
-
return { version, owed, failures, documents: documents ?? NO_DOCUMENTS };
|
|
110
|
+
return { version, owed, failures, documents: documents ?? NO_DOCUMENTS, ...(contentHashRule !== undefined ? { contentHashRule } : {}) };
|
|
103
111
|
}
|
|
104
112
|
|
|
113
|
+
/**
|
|
114
|
+
* True while the files were last reviewed under another extractor version than today's, or under another content-hash rule
|
|
115
|
+
* ({@link contentHashRuleOf}: no record is the baseline rule): every file owes one re-extraction. Adopting the baseline rule owes
|
|
116
|
+
* nothing by itself — a host switching to `contentHash` moves the hash of every CRLF file, and those re-extract by content
|
|
117
|
+
* (`contentChanged`, an extract-cache miss). The entry writes no record for the baseline; a later rule writes one.
|
|
118
|
+
*/
|
|
119
|
+
const upgradePending = (state: ExtractorState): boolean =>
|
|
120
|
+
state.version !== EXTRACTOR_VERSION || contentHashRuleOf(state.contentHashRule) !== CONTENT_HASH_RULE;
|
|
121
|
+
|
|
105
122
|
/** The file's document extension when its asset-ness flipped (a host parser arrived or left since the last run). */
|
|
106
123
|
const isFlipped = (state: ExtractorState, path: string): boolean => !!state.flipped?.size && state.flipped.has(extOf(path));
|
|
107
124
|
|
|
108
|
-
/** True when `path` is owed a re-extraction: another extractor version than today's, left unfinished before, or its asset-ness flipped. */
|
|
125
|
+
/** True when `path` is owed a re-extraction: another extractor version or recorded content-hash rule than today's, left unfinished before, or its asset-ness flipped. */
|
|
109
126
|
export const isOwed = (state: ExtractorState, path: string): boolean =>
|
|
110
|
-
state
|
|
127
|
+
upgradePending(state) || state.owed.has(path) || isFlipped(state, path);
|
|
111
128
|
|
|
112
129
|
/** Whether `path` is an asset under the packs the plan was made with: binary, and not a document some parser claims. */
|
|
113
130
|
const isAssetNow = (state: ExtractorState, path: string): boolean => {
|
|
@@ -123,7 +140,7 @@ const isAssetNow = (state: ExtractorState, path: string): boolean => {
|
|
|
123
140
|
* An asset costs none (it is never read or judged); a file that flipped TO a document does.
|
|
124
141
|
*/
|
|
125
142
|
export const owedCalls = (state: ExtractorState, path: string): number =>
|
|
126
|
-
isAssetNow(state, path) ? 0 : (state
|
|
143
|
+
isAssetNow(state, path) ? 0 : (upgradePending(state) || isFlipped(state, path) ? UPGRADE_CALLS : 0) + (state.owed.get(path) ?? 0);
|
|
127
144
|
|
|
128
145
|
/**
|
|
129
146
|
* Paths in the files manifest that the plan does not list yet are owed: ignored, claimed by
|
|
@@ -169,6 +186,7 @@ export async function saveExtractorState(
|
|
|
169
186
|
): Promise<void> {
|
|
170
187
|
await manifest.set(extractorManifestKey(rootId), [
|
|
171
188
|
{ id: VERSION_ID, hash: String(EXTRACTOR_VERSION) },
|
|
189
|
+
...(CONTENT_HASH_RULE !== BASELINE_CONTENT_HASH_RULE ? [{ id: CONTENT_HASH_ID, hash: CONTENT_HASH_RULE }] : []),
|
|
172
190
|
...(documents !== undefined && documents !== NO_DOCUMENTS ? [{ id: DOCUMENTS_ID, hash: documents }] : []),
|
|
173
191
|
...[...owed].sort((a, b) => compareStr(a[0], b[0])).map(([path, calls]) => ({ id: `${OWED_PREFIX}${path}`, hash: String(calls) })),
|
|
174
192
|
...[...failures].filter(([path]) => owed.has(path)).sort((a, b) => compareStr(a[0], b[0])).map(([path, f]) => ({ id: `${FAIL_PREFIX}${path}`, hash: `${f.count}:${f.hash}` })),
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* NORMALISATION (lossless for every value the model produces, documented where it is not): rows are sorted and
|
|
10
10
|
* empty parts dropped; object keys of badges and of the analysis document are written in sorted order; a
|
|
11
11
|
* negative zero is written as zero; a string holding a lone UTF-16 surrogate is written with U+FFFD in its place
|
|
12
|
-
* (UTF-8 cannot carry it)
|
|
12
|
+
* (UTF-8 cannot carry it); an edge's empty `sites` list is not written (the SITE section carries non-empty ones only).
|
|
13
|
+
* Equal normalised input gives equal bytes.
|
|
13
14
|
*/
|
|
14
15
|
|
|
15
16
|
import { deflateRaw } from './deflate.js';
|
|
@@ -65,12 +65,15 @@ const nodeId = (n: KNode): string => n.ref;
|
|
|
65
65
|
*/
|
|
66
66
|
const stamped = (n: KNode, entryHash: string): KNode => (n.sourceHash === entryHash ? { ...n, sourceHash: '\u0000entry' } : n);
|
|
67
67
|
const edgeId = (e: KEdge): string => `${e.from}\u0000${e.to}\u0000${e.kind}`;
|
|
68
|
+
/** An edge with an empty `sites` list is the same edge as one without (the wire never carries an empty list). */
|
|
69
|
+
const withoutEmptySites = (e: KEdge): KEdge => (e.sites && e.sites.length === 0 ? (({ sites: _, ...rest }) => rest)(e) : e);
|
|
70
|
+
const sameEdge = (p: KEdge, n: KEdge): boolean => deepEqual(withoutEmptySites(p), withoutEmptySites(n));
|
|
68
71
|
const keyId = (k: { ref: string; key: string }): string => `${k.ref}\u0000${k.key}`;
|
|
69
72
|
|
|
70
73
|
export function diffEntry(prior: Extracted, next: Extracted): ExtrEntryDiff | undefined {
|
|
71
74
|
if (prior.sourceRef !== next.sourceRef) return undefined;
|
|
72
75
|
const nodes = diffList(prior.nodes, next.nodes, nodeId, (p, n) => deepEqual(stamped(p, prior.sourceHash), stamped(n, next.sourceHash)));
|
|
73
|
-
const edges = diffList(prior.edges, next.edges, edgeId);
|
|
76
|
+
const edges = diffList(prior.edges, next.edges, edgeId, sameEdge);
|
|
74
77
|
const keys = diffList(prior.keys, next.keys, keyId);
|
|
75
78
|
if (!nodes || !edges || !keys) return undefined;
|
|
76
79
|
return {
|
|
@@ -18,11 +18,26 @@ export interface SectionCodec {
|
|
|
18
18
|
export const ORIGINS = ['extracted', 'model', 'human', 'agent'] as const;
|
|
19
19
|
export const STORED_ORIGINS = ['model', 'human', 'agent'] as const;
|
|
20
20
|
export const VERDICTS = ['allowed', 'same', 'skips', 'upward'] as const;
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* The relation/edge kinds a READER knows as one-byte codes; anything else is stored as 0 + a string. APPEND ONLY: a
|
|
23
|
+
* code is its index + 1, so an existing word never moves.
|
|
24
|
+
*
|
|
25
|
+
* Expand/contract across releases (frame.ts: "a new value in an existing closed vocabulary needs every reader
|
|
26
|
+
* upgraded first"): a reader before 2026-11-10 throws on a code past 11, so the symbol kinds (PRD 43 P0b) are KNOWN
|
|
27
|
+
* here but not yet WRITTEN as codes — see {@link WRITTEN_EDGE_KINDS}.
|
|
28
|
+
*/
|
|
22
29
|
export const EDGE_KINDS = [
|
|
23
30
|
'references', 'links', 'contains', 'mentions', 'duplicate', 'near-duplicate', 'contradicts', 'agrees',
|
|
24
31
|
'supersedes', 'about', 'co-changed',
|
|
32
|
+
// PRD 43 P0b (2026-11-10): the symbol-level kinds, in SYMBOL_EDGE_KINDS order.
|
|
33
|
+
'extends', 'implements', 'composes', 'uses', 'calls',
|
|
25
34
|
] as const;
|
|
35
|
+
/**
|
|
36
|
+
* The prefix of {@link EDGE_KINDS} a WRITER codes: what every deployed reader decodes. A later kind is written as 0 + its
|
|
37
|
+
* string, which every reader (old or new) decodes to the same value. Grow it to the whole list once no reader older
|
|
38
|
+
* than this release remains (the contract step) — the bytes change, the decoded pack does not.
|
|
39
|
+
*/
|
|
40
|
+
export const WRITTEN_EDGE_KINDS: readonly string[] = EDGE_KINDS.slice(0, 11);
|
|
26
41
|
/** The node kinds the packs name. */
|
|
27
42
|
export const NODE_KINDS = ['file', 'section', 'symbol', 'area', 'entity', 'claim', 'fact'] as const;
|
|
28
43
|
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import type { KEdge, KNode, NodeRef } from '../types.js';
|
|
8
8
|
import type { ReadTables, Tables } from '../strings.js';
|
|
9
9
|
import { assertFlagsKnown, boundedCount, ByteReader, ByteWriter, PackFormatError, type Budget } from '../varint.js';
|
|
10
|
-
import { EDGE_KINDS, NODE_KINDS, ORIGINS, VERDICTS, readEnum, readWord, unzigzag, writeEnum, writeWord, zigzag } from './columns.js';
|
|
10
|
+
import { EDGE_KINDS, NODE_KINDS, ORIGINS, VERDICTS, WRITTEN_EDGE_KINDS, readEnum, readWord, unzigzag, writeEnum, writeWord, zigzag } from './columns.js';
|
|
11
11
|
|
|
12
12
|
const N_PARTITION = 1, N_AREA = 2, N_STALE = 4, N_STALE_V = 8, N_BADGES = 16, N_SYSTEM = 32, N_APP = 64, N_COPY = 128, N_HASH = 256;
|
|
13
13
|
const N_KNOWN = 0x1ff;
|
|
@@ -92,7 +92,7 @@ export function writeEdges(w: ByteWriter, t: Tables, edges: readonly KEdge[]): v
|
|
|
92
92
|
let prev = 0;
|
|
93
93
|
for (const e of edges) { const i = t.str(e.from); w.varint(zigzag(i - prev)); prev = i; }
|
|
94
94
|
for (const e of edges) w.varint(t.str(e.to));
|
|
95
|
-
for (const e of edges) writeWord(w, t,
|
|
95
|
+
for (const e of edges) writeWord(w, t, WRITTEN_EDGE_KINDS, e.kind);
|
|
96
96
|
for (const e of edges) w.num(e.weight);
|
|
97
97
|
for (const e of edges) writeEnum(w, ORIGINS, e.origin);
|
|
98
98
|
const flags = edges.map((e) =>
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import type { NodeRef, StoredFact, StoredRelation } from '../types.js';
|
|
7
7
|
import { assertConsumed, assertFlagsKnown, boundedCount, ByteReader, ByteWriter } from '../varint.js';
|
|
8
|
-
import { EDGE_KINDS, STORED_ORIGINS, readEnum, readWord, writeEnum, writeWord, type SectionCodec } from './columns.js';
|
|
8
|
+
import { EDGE_KINDS, STORED_ORIGINS, WRITTEN_EDGE_KINDS, readEnum, readWord, writeEnum, writeWord, type SectionCodec } from './columns.js';
|
|
9
9
|
import { TAG } from './rows.js';
|
|
10
10
|
|
|
11
11
|
const F_UPDATED = 1, F_BY_NULL = 2, F_BY = 4;
|
|
@@ -88,7 +88,7 @@ export const relationSection: SectionCodec = {
|
|
|
88
88
|
let prev = 0;
|
|
89
89
|
for (const x of rows) { const i = t.str(x.from); w.varint(i - prev); prev = i; }
|
|
90
90
|
for (const x of rows) w.varint(t.str(x.to));
|
|
91
|
-
for (const x of rows) writeWord(w, t,
|
|
91
|
+
for (const x of rows) writeWord(w, t, WRITTEN_EDGE_KINDS, x.kind);
|
|
92
92
|
for (const x of rows) w.num(x.weight);
|
|
93
93
|
for (const x of rows) writeEnum(w, STORED_ORIGINS, x.origin);
|
|
94
94
|
const flags = rows.map((x) => (x.confidence !== undefined ? R_CONF : 0) | (x.sourceHash !== undefined ? R_HASH : 0)
|
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
import { deleteSection, analysisSection, bucketSection, fileSection, leafSection, scopeSection, staleSection } from './rows.js';
|
|
4
4
|
import { extrSection } from './extr.js';
|
|
5
5
|
import { factSection, relationSection } from './facts.js';
|
|
6
|
+
import { siteSection } from './sites.js';
|
|
6
7
|
import type { SectionCodec } from './columns.js';
|
|
7
8
|
|
|
9
|
+
/** SITE comes right after EXTR: it attaches call sites to the edges EXTR decoded (a reader runs these in this order). */
|
|
8
10
|
export const ROW_SECTIONS: readonly SectionCodec[] = [
|
|
9
|
-
fileSection, extrSection, factSection, relationSection, staleSection, deleteSection, scopeSection, analysisSection, leafSection, bucketSection,
|
|
11
|
+
fileSection, extrSection, siteSection, factSection, relationSection, staleSection, deleteSection, scopeSection, analysisSection, leafSection, bucketSection,
|
|
10
12
|
];
|
|
11
13
|
|
|
12
14
|
export { TAG } from './rows.js';
|
|
@@ -11,7 +11,7 @@ import type { Pack } from '../types.js';
|
|
|
11
11
|
import type { SectionCodec } from './columns.js';
|
|
12
12
|
import { MANIFEST_BUCKETS } from '../manifest.js';
|
|
13
13
|
|
|
14
|
-
export const TAG = { STR: 1, HASH: 2, FILE: 3, EXTR: 4, FACT: 5, REL: 6, STALE: 7, DEL: 8, SCOPE: 9, ANALYSIS: 10, LEAF: 11, BKT: 12 } as const;
|
|
14
|
+
export const TAG = { STR: 1, HASH: 2, FILE: 3, EXTR: 4, FACT: 5, REL: 6, STALE: 7, DEL: 8, SCOPE: 9, ANALYSIS: 10, LEAF: 11, BKT: 12, SITE: 13 } as const;
|
|
15
15
|
|
|
16
16
|
export const fileSection: SectionCodec = {
|
|
17
17
|
tag: TAG.FILE,
|