@seanhogg/builderforce-memory 2026.10.44 → 2026.10.46
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 +34 -2
- package/dist/knowledge/build.js.map +1 -1
- package/dist/knowledge/extractFile.d.ts +9 -1
- package/dist/knowledge/extractFile.d.ts.map +1 -1
- package/dist/knowledge/extractFile.js +22 -5
- package/dist/knowledge/extractFile.js.map +1 -1
- package/dist/knowledge/extractJob.d.ts +30 -5
- package/dist/knowledge/extractJob.d.ts.map +1 -1
- package/dist/knowledge/extractJob.js +9 -1
- package/dist/knowledge/extractJob.js.map +1 -1
- package/dist/knowledge/findings.d.ts.map +1 -1
- package/dist/knowledge/findings.js +9 -1
- package/dist/knowledge/findings.js.map +1 -1
- package/dist/knowledge/index.d.ts +7 -4
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +3 -1
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/jobDeadline.d.ts +17 -0
- package/dist/knowledge/jobDeadline.d.ts.map +1 -0
- package/dist/knowledge/jobDeadline.js +46 -0
- package/dist/knowledge/jobDeadline.js.map +1 -0
- package/dist/knowledge/judgeCalls.d.ts +16 -0
- package/dist/knowledge/judgeCalls.d.ts.map +1 -1
- package/dist/knowledge/judgeCalls.js +20 -1
- package/dist/knowledge/judgeCalls.js.map +1 -1
- package/dist/knowledge/judgePriority.d.ts +22 -0
- package/dist/knowledge/judgePriority.d.ts.map +1 -0
- package/dist/knowledge/judgePriority.js +28 -0
- package/dist/knowledge/judgePriority.js.map +1 -0
- package/dist/knowledge/model.d.ts +57 -0
- package/dist/knowledge/model.d.ts.map +1 -1
- package/dist/knowledge/outline.d.ts.map +1 -1
- package/dist/knowledge/outline.js +14 -1
- package/dist/knowledge/outline.js.map +1 -1
- package/dist/knowledge/pack/crc32.d.ts +3 -0
- package/dist/knowledge/pack/crc32.d.ts.map +1 -0
- package/dist/knowledge/pack/crc32.js +18 -0
- package/dist/knowledge/pack/crc32.js.map +1 -0
- package/dist/knowledge/pack/decode.d.ts +14 -0
- package/dist/knowledge/pack/decode.d.ts.map +1 -0
- package/dist/knowledge/pack/decode.js +64 -0
- package/dist/knowledge/pack/decode.js.map +1 -0
- package/dist/knowledge/pack/deflate.d.ts +9 -0
- package/dist/knowledge/pack/deflate.d.ts.map +1 -0
- package/dist/knowledge/pack/deflate.js +45 -0
- package/dist/knowledge/pack/deflate.js.map +1 -0
- package/dist/knowledge/pack/encode.d.ts +17 -0
- package/dist/knowledge/pack/encode.d.ts.map +1 -0
- package/dist/knowledge/pack/encode.js +79 -0
- package/dist/knowledge/pack/encode.js.map +1 -0
- package/dist/knowledge/pack/extrDiff.d.ts +23 -0
- package/dist/knowledge/pack/extrDiff.d.ts.map +1 -0
- package/dist/knowledge/pack/extrDiff.js +0 -0
- package/dist/knowledge/pack/extrDiff.js.map +1 -0
- package/dist/knowledge/pack/frame.d.ts +37 -0
- package/dist/knowledge/pack/frame.d.ts.map +1 -0
- package/dist/knowledge/pack/frame.js +94 -0
- package/dist/knowledge/pack/frame.js.map +1 -0
- package/dist/knowledge/pack/index.d.ts +19 -0
- package/dist/knowledge/pack/index.d.ts.map +1 -0
- package/dist/knowledge/pack/index.js +17 -0
- package/dist/knowledge/pack/index.js.map +1 -0
- package/dist/knowledge/pack/jsonSize.d.ts +5 -0
- package/dist/knowledge/pack/jsonSize.d.ts.map +1 -0
- package/dist/knowledge/pack/jsonSize.js +8 -0
- package/dist/knowledge/pack/jsonSize.js.map +1 -0
- package/dist/knowledge/pack/manifest.d.ts +66 -0
- package/dist/knowledge/pack/manifest.d.ts.map +1 -0
- package/dist/knowledge/pack/manifest.js +104 -0
- package/dist/knowledge/pack/manifest.js.map +1 -0
- package/dist/knowledge/pack/sections/columns.d.ts +32 -0
- package/dist/knowledge/pack/sections/columns.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/columns.js +53 -0
- package/dist/knowledge/pack/sections/columns.js.map +1 -0
- package/dist/knowledge/pack/sections/extr.d.ts +8 -0
- package/dist/knowledge/pack/sections/extr.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/extr.js +99 -0
- package/dist/knowledge/pack/sections/extr.js.map +1 -0
- package/dist/knowledge/pack/sections/extrLists.d.ts +23 -0
- package/dist/knowledge/pack/sections/extrLists.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/extrLists.js +194 -0
- package/dist/knowledge/pack/sections/extrLists.js.map +1 -0
- package/dist/knowledge/pack/sections/facts.d.ts +8 -0
- package/dist/knowledge/pack/sections/facts.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/facts.js +167 -0
- package/dist/knowledge/pack/sections/facts.js.map +1 -0
- package/dist/knowledge/pack/sections/index.d.ts +6 -0
- package/dist/knowledge/pack/sections/index.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/index.js +9 -0
- package/dist/knowledge/pack/sections/index.js.map +1 -0
- package/dist/knowledge/pack/sections/rows.d.ts +31 -0
- package/dist/knowledge/pack/sections/rows.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/rows.js +226 -0
- package/dist/knowledge/pack/sections/rows.js.map +1 -0
- package/dist/knowledge/pack/strings.d.ts +39 -0
- package/dist/knowledge/pack/strings.d.ts.map +1 -0
- package/dist/knowledge/pack/strings.js +115 -0
- package/dist/knowledge/pack/strings.js.map +1 -0
- package/dist/knowledge/pack/types.d.ts +109 -0
- package/dist/knowledge/pack/types.d.ts.map +1 -0
- package/dist/knowledge/pack/types.js +17 -0
- package/dist/knowledge/pack/types.js.map +1 -0
- package/dist/knowledge/pack/varint.d.ts +50 -0
- package/dist/knowledge/pack/varint.d.ts.map +1 -0
- package/dist/knowledge/pack/varint.js +141 -0
- package/dist/knowledge/pack/varint.js.map +1 -0
- package/dist/knowledge/partitions.d.ts +1 -1
- package/dist/knowledge/partitions.d.ts.map +1 -1
- package/dist/knowledge/partitions.js +6 -1
- package/dist/knowledge/partitions.js.map +1 -1
- package/dist/knowledge/review.d.ts.map +1 -1
- package/dist/knowledge/review.js +58 -16
- package/dist/knowledge/review.js.map +1 -1
- package/dist/knowledge/reviewJudge.d.ts +8 -1
- package/dist/knowledge/reviewJudge.d.ts.map +1 -1
- package/dist/knowledge/reviewJudge.js +14 -8
- package/dist/knowledge/reviewJudge.js.map +1 -1
- package/dist/knowledge/reviewModel.d.ts +35 -1
- package/dist/knowledge/reviewModel.d.ts.map +1 -1
- package/dist/knowledge/reviewPrefetch.d.ts +8 -2
- package/dist/knowledge/reviewPrefetch.d.ts.map +1 -1
- package/dist/knowledge/reviewPrefetch.js +50 -25
- package/dist/knowledge/reviewPrefetch.js.map +1 -1
- package/dist/knowledge/reviewRun.d.ts +15 -3
- package/dist/knowledge/reviewRun.d.ts.map +1 -1
- package/dist/knowledge/reviewRun.js +19 -2
- package/dist/knowledge/reviewRun.js.map +1 -1
- package/dist/knowledge/reviewRunModel.d.ts +8 -1
- package/dist/knowledge/reviewRunModel.d.ts.map +1 -1
- package/dist/knowledge/reviewRunModel.js.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.d.ts +72 -6
- package/dist/knowledge/stores/localSnapshotStore.d.ts.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.js +279 -75
- package/dist/knowledge/stores/localSnapshotStore.js.map +1 -1
- package/dist/knowledge/systems.d.ts +18 -0
- package/dist/knowledge/systems.d.ts.map +1 -0
- package/dist/knowledge/systems.js +29 -0
- package/dist/knowledge/systems.js.map +1 -0
- package/package.json +7 -3
- package/src/knowledge/build.ts +31 -3
- package/src/knowledge/extractFile.ts +26 -6
- package/src/knowledge/extractJob.ts +26 -7
- package/src/knowledge/findings.ts +9 -1
- package/src/knowledge/index.ts +7 -3
- package/src/knowledge/jobDeadline.ts +43 -0
- package/src/knowledge/judgeCalls.ts +19 -2
- package/src/knowledge/judgePriority.ts +36 -0
- package/src/knowledge/model.ts +49 -0
- package/src/knowledge/outline.test.ts +50 -0
- package/src/knowledge/outline.ts +15 -1
- package/src/knowledge/pack/crc32.ts +17 -0
- package/src/knowledge/pack/decode.ts +58 -0
- package/src/knowledge/pack/deflate.ts +43 -0
- package/src/knowledge/pack/encode.ts +81 -0
- package/src/knowledge/pack/extrDiff.ts +0 -0
- package/src/knowledge/pack/frame.ts +86 -0
- package/src/knowledge/pack/index.ts +26 -0
- package/src/knowledge/pack/jsonSize.ts +11 -0
- package/src/knowledge/pack/manifest.ts +128 -0
- package/src/knowledge/pack/pack.guards.test.ts +77 -0
- package/src/knowledge/pack/pack.reconcile.test.ts +96 -0
- package/src/knowledge/pack/pack.roundtrip.test.ts +50 -0
- package/src/knowledge/pack/pack.size.test.ts +22 -0
- package/src/knowledge/pack/pack.testkit.test-util.ts +90 -0
- package/src/knowledge/pack/sections/columns.ts +61 -0
- package/src/knowledge/pack/sections/extr.ts +90 -0
- package/src/knowledge/pack/sections/extrLists.ts +147 -0
- package/src/knowledge/pack/sections/facts.ts +123 -0
- package/src/knowledge/pack/sections/index.ts +13 -0
- package/src/knowledge/pack/sections/rows.ts +184 -0
- package/src/knowledge/pack/strings.ts +123 -0
- package/src/knowledge/pack/types.ts +89 -0
- package/src/knowledge/pack/varint.ts +138 -0
- package/src/knowledge/partitions.test.ts +4 -0
- package/src/knowledge/partitions.ts +3 -1
- package/src/knowledge/review.throughput.test.ts +122 -0
- package/src/knowledge/review.ts +54 -16
- package/src/knowledge/review.twoPass.test.ts +224 -0
- package/src/knowledge/reviewJudge.ts +19 -8
- package/src/knowledge/reviewModel.ts +32 -1
- package/src/knowledge/reviewPrefetch.ts +51 -24
- package/src/knowledge/reviewRun.test.ts +20 -3
- package/src/knowledge/reviewRun.ts +33 -6
- package/src/knowledge/reviewRunModel.ts +4 -1
- package/src/knowledge/stores/localSnapshotStore.batched.test.ts +25 -3
- package/src/knowledge/stores/localSnapshotStore.extractedLog.test.ts +280 -0
- package/src/knowledge/stores/localSnapshotStore.ts +283 -76
- package/src/knowledge/systems.test.ts +119 -0
- package/src/knowledge/systems.ts +32 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/sections/rows.ts — the small row sections of BFKP v1 (PRD 41 A11): FILE
|
|
3
|
+
* `(path#, hash#, bytes)`, STALE `(sourceRef#, hash#)`, DEL tombstones, SCOPE, ANALYSIS, and the
|
|
4
|
+
* manifest sections LEAF (a reconcile answer) and BKT (the 4,096 bucket digests).
|
|
5
|
+
* Rows arrive sorted (see `normalizePack`), so index runs are delta-encoded.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { boundedCount, ByteReader, ByteWriter, PackFormatError } from '../varint.js';
|
|
9
|
+
import type { Pack } from '../types.js';
|
|
10
|
+
import type { SectionCodec } from './columns.js';
|
|
11
|
+
import { MANIFEST_BUCKETS } from '../manifest.js';
|
|
12
|
+
|
|
13
|
+
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
|
+
|
|
15
|
+
export const fileSection: SectionCodec = {
|
|
16
|
+
tag: TAG.FILE,
|
|
17
|
+
write(pack, t) {
|
|
18
|
+
const rows = pack.files;
|
|
19
|
+
if (!rows?.length) return undefined;
|
|
20
|
+
const w = new ByteWriter();
|
|
21
|
+
w.varint(rows.length);
|
|
22
|
+
let prev = 0;
|
|
23
|
+
for (const f of rows) { const i = t.str(f.path); w.varint(i - prev); prev = i; }
|
|
24
|
+
for (const f of rows) w.varint(t.hash(f.hash));
|
|
25
|
+
for (const f of rows) w.varint(f.bytes);
|
|
26
|
+
return w.finish();
|
|
27
|
+
},
|
|
28
|
+
read(bytes, t, limits, into) {
|
|
29
|
+
const r = new ByteReader(bytes);
|
|
30
|
+
const n = boundedCount(r, limits.maxRows, 'files');
|
|
31
|
+
const paths: string[] = [];
|
|
32
|
+
let prev = 0;
|
|
33
|
+
for (let i = 0; i < n; i++) { prev += r.varint(); paths.push(t.str(prev)); }
|
|
34
|
+
const hashes = paths.map(() => t.hash(r.varint()));
|
|
35
|
+
into.files = paths.map((path, i) => ({ path, hash: hashes[i]!, bytes: r.varint() }));
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const staleSection: SectionCodec = {
|
|
40
|
+
tag: TAG.STALE,
|
|
41
|
+
write(pack, t) {
|
|
42
|
+
const rows = pack.stale;
|
|
43
|
+
if (!rows?.length) return undefined;
|
|
44
|
+
const w = new ByteWriter();
|
|
45
|
+
w.varint(rows.length);
|
|
46
|
+
let prev = 0;
|
|
47
|
+
for (const s of rows) { const i = t.str(s.sourceRef); w.varint(i - prev); prev = i; }
|
|
48
|
+
for (const s of rows) w.varint(t.hash(s.hash));
|
|
49
|
+
return w.finish();
|
|
50
|
+
},
|
|
51
|
+
read(bytes, t, limits, into) {
|
|
52
|
+
const r = new ByteReader(bytes);
|
|
53
|
+
const n = boundedCount(r, limits.maxRows, 'stale');
|
|
54
|
+
const refs: string[] = [];
|
|
55
|
+
let prev = 0;
|
|
56
|
+
for (let i = 0; i < n; i++) { prev += r.varint(); refs.push(t.str(prev)); }
|
|
57
|
+
into.stale = refs.map((sourceRef) => ({ sourceRef, hash: t.hash(r.varint()) }));
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const deleteSection: SectionCodec = {
|
|
62
|
+
tag: TAG.DEL,
|
|
63
|
+
write(pack, t) {
|
|
64
|
+
const d = pack.deletes;
|
|
65
|
+
if (!d || d.files.length + d.facts.length + d.relations.length === 0) return undefined;
|
|
66
|
+
const w = new ByteWriter();
|
|
67
|
+
for (const list of [d.files, d.facts]) {
|
|
68
|
+
w.varint(list.length);
|
|
69
|
+
let prev = 0;
|
|
70
|
+
for (const s of list) { const i = t.str(s); w.varint(i - prev); prev = i; }
|
|
71
|
+
}
|
|
72
|
+
w.varint(d.relations.length);
|
|
73
|
+
for (const x of d.relations) w.varint(t.str(x.from));
|
|
74
|
+
for (const x of d.relations) w.varint(t.str(x.to));
|
|
75
|
+
for (const x of d.relations) w.varint(t.str(x.kind));
|
|
76
|
+
return w.finish();
|
|
77
|
+
},
|
|
78
|
+
read(bytes, t, limits, into) {
|
|
79
|
+
const r = new ByteReader(bytes);
|
|
80
|
+
const lists: string[][] = [];
|
|
81
|
+
for (let k = 0; k < 2; k++) {
|
|
82
|
+
const n = boundedCount(r, limits.maxRows, 'tombstones');
|
|
83
|
+
const out: string[] = [];
|
|
84
|
+
let prev = 0;
|
|
85
|
+
for (let i = 0; i < n; i++) { prev += r.varint(); out.push(t.str(prev)); }
|
|
86
|
+
lists.push(out);
|
|
87
|
+
}
|
|
88
|
+
const n = boundedCount(r, limits.maxRows, 'relation tombstones');
|
|
89
|
+
const from = Array.from({ length: n }, () => t.str(r.varint()));
|
|
90
|
+
const to = Array.from({ length: n }, () => t.str(r.varint()));
|
|
91
|
+
const kind = Array.from({ length: n }, () => t.str(r.varint()));
|
|
92
|
+
into.deletes = { files: lists[0]!, facts: lists[1]!, relations: from.map((f, i) => ({ from: f, to: to[i]!, kind: kind[i]! })) };
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const scopeSection: SectionCodec = {
|
|
97
|
+
tag: TAG.SCOPE,
|
|
98
|
+
write(pack, t) {
|
|
99
|
+
const s = pack.scope;
|
|
100
|
+
if (!s) return undefined;
|
|
101
|
+
const w = new ByteWriter();
|
|
102
|
+
w.varint(t.str(s.rootId));
|
|
103
|
+
w.varint(t.str(s.title));
|
|
104
|
+
w.varint(s.packs.length);
|
|
105
|
+
for (const p of s.packs) w.varint(t.str(p));
|
|
106
|
+
w.u8((s.lastDigest !== undefined ? 1 : 0) | (s.lastReviewedAt !== undefined ? 2 : 0));
|
|
107
|
+
if (s.lastDigest !== undefined) w.varint(t.str(s.lastDigest));
|
|
108
|
+
if (s.lastReviewedAt !== undefined) w.varint(t.str(s.lastReviewedAt));
|
|
109
|
+
return w.finish();
|
|
110
|
+
},
|
|
111
|
+
read(bytes, t, limits, into) {
|
|
112
|
+
const r = new ByteReader(bytes);
|
|
113
|
+
const scope: NonNullable<Pack['scope']> = { rootId: t.str(r.varint()), title: t.str(r.varint()), packs: [] };
|
|
114
|
+
const n = boundedCount(r, limits.maxRows, 'packs');
|
|
115
|
+
for (let i = 0; i < n; i++) scope.packs.push(t.str(r.varint()));
|
|
116
|
+
const flags = r.u8();
|
|
117
|
+
if (flags & 1) scope.lastDigest = t.str(r.varint());
|
|
118
|
+
if (flags & 2) scope.lastReviewedAt = t.str(r.varint());
|
|
119
|
+
into.scope = scope;
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/** The analysis snapshot is one JSON-compatible document with no row structure; it travels as one string (deflate does the rest). */
|
|
124
|
+
export const analysisSection: SectionCodec = {
|
|
125
|
+
tag: TAG.ANALYSIS,
|
|
126
|
+
write(pack, t) {
|
|
127
|
+
if (pack.analysis === undefined) return undefined;
|
|
128
|
+
const w = new ByteWriter();
|
|
129
|
+
w.varint(t.str(JSON.stringify(pack.analysis)));
|
|
130
|
+
return w.finish();
|
|
131
|
+
},
|
|
132
|
+
read(bytes, t, _limits, into) {
|
|
133
|
+
into.analysis = JSON.parse(t.str(new ByteReader(bytes).varint())) as unknown;
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export const leafSection: SectionCodec = {
|
|
138
|
+
tag: TAG.LEAF,
|
|
139
|
+
write(pack, t) {
|
|
140
|
+
const rows = pack.leaves;
|
|
141
|
+
if (!rows?.length) return undefined;
|
|
142
|
+
const w = new ByteWriter();
|
|
143
|
+
w.varint(rows.length);
|
|
144
|
+
let prev = 0;
|
|
145
|
+
for (const l of rows) { const i = t.str(l.key); w.varint(i - prev); prev = i; }
|
|
146
|
+
for (const l of rows) w.varint(t.hash(l.digest));
|
|
147
|
+
return w.finish();
|
|
148
|
+
},
|
|
149
|
+
read(bytes, t, limits, into) {
|
|
150
|
+
const r = new ByteReader(bytes);
|
|
151
|
+
const n = boundedCount(r, limits.maxRows, 'leaves');
|
|
152
|
+
const keys: string[] = [];
|
|
153
|
+
let prev = 0;
|
|
154
|
+
for (let i = 0; i < n; i++) { prev += r.varint(); keys.push(t.str(prev)); }
|
|
155
|
+
into.leaves = keys.map((key) => ({ key, digest: t.hash(r.varint()) }));
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/** The bucket digests, raw: 4,096 x 32 bytes. */
|
|
160
|
+
export const bucketSection: SectionCodec = {
|
|
161
|
+
tag: TAG.BKT,
|
|
162
|
+
write(pack) {
|
|
163
|
+
const b = pack.buckets;
|
|
164
|
+
if (!b) return undefined;
|
|
165
|
+
if (b.length !== MANIFEST_BUCKETS) throw new PackFormatError(`a bucket list holds ${MANIFEST_BUCKETS} digests`);
|
|
166
|
+
const w = new ByteWriter();
|
|
167
|
+
for (const hex of b) {
|
|
168
|
+
const raw = new Uint8Array(32);
|
|
169
|
+
for (let i = 0; i < 32; i++) raw[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
170
|
+
w.bytes(raw);
|
|
171
|
+
}
|
|
172
|
+
return w.finish();
|
|
173
|
+
},
|
|
174
|
+
read(bytes, _t, _limits, into) {
|
|
175
|
+
if (bytes.length !== MANIFEST_BUCKETS * 32) throw new PackFormatError('bucket section has the wrong size');
|
|
176
|
+
const out: string[] = [];
|
|
177
|
+
for (let i = 0; i < MANIFEST_BUCKETS; i++) {
|
|
178
|
+
let hex = '';
|
|
179
|
+
for (const b of bytes.subarray(i * 32, i * 32 + 32)) hex += (b < 16 ? '0' : '') + b.toString(16);
|
|
180
|
+
out.push(hex);
|
|
181
|
+
}
|
|
182
|
+
into.buckets = out;
|
|
183
|
+
},
|
|
184
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/strings.ts — the STR and HASH sections of BFKP v1 (PRD 41 A11).
|
|
3
|
+
*
|
|
4
|
+
* STR every path, key, ref, symbol name and label once, sorted and front-coded: for each
|
|
5
|
+
* string the length of the byte prefix it shares with the one before, then the suffix.
|
|
6
|
+
* HASH every content hash once, sorted. A 64-character lowercase hex hash is stored as its raw
|
|
7
|
+
* 32 bytes (flag 0); any other id (a root-level `~tag:…` output) as UTF-8 (flag 1).
|
|
8
|
+
*
|
|
9
|
+
* Sections refer to both by index. Encoding runs each section encoder twice (see encode.ts): a
|
|
10
|
+
* first pass with {@link collector} gathers what is used, a second with {@link tablesOf} writes real indexes.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { boundedCount, ByteReader, ByteWriter, PackFormatError, utf8 } from './varint.js';
|
|
14
|
+
|
|
15
|
+
/** What a section encoder needs: the index of a string and of a hash. */
|
|
16
|
+
export interface Tables {
|
|
17
|
+
str(s: string): number;
|
|
18
|
+
hash(h: string): number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** What a section decoder needs: the string and hash at an index. */
|
|
22
|
+
export interface ReadTables {
|
|
23
|
+
str(i: number): string;
|
|
24
|
+
hash(i: number): string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
28
|
+
|
|
29
|
+
/** A pass-1 `Tables` that records every string and hash it is asked for. */
|
|
30
|
+
export function collector(): Tables & { strings: Set<string>; hashes: Set<string> } {
|
|
31
|
+
const strings = new Set<string>(), hashes = new Set<string>();
|
|
32
|
+
return { strings, hashes, str: (s) => { strings.add(s); return 0; }, hash: (h) => { hashes.add(h); return 0; } };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface BuiltTables extends Tables { strings: string[]; hashes: string[] }
|
|
36
|
+
|
|
37
|
+
/** The real tables over what pass 1 collected: sorted, so equal input gives equal bytes. */
|
|
38
|
+
export function tablesOf(strings: Iterable<string>, hashes: Iterable<string>): BuiltTables {
|
|
39
|
+
const s = [...new Set(strings)].sort(), h = [...new Set(hashes)].sort();
|
|
40
|
+
const si = new Map(s.map((v, i) => [v, i])), hi = new Map(h.map((v, i) => [v, i]));
|
|
41
|
+
return { strings: s, hashes: h, str: (v) => si.get(v)!, hash: (v) => hi.get(v)! };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function commonPrefix(a: Uint8Array, b: Uint8Array): number {
|
|
45
|
+
const n = Math.min(a.length, b.length);
|
|
46
|
+
let i = 0;
|
|
47
|
+
while (i < n && a[i] === b[i]) i++;
|
|
48
|
+
return i;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function writeStringTable(strings: readonly string[]): Uint8Array {
|
|
52
|
+
const w = new ByteWriter();
|
|
53
|
+
w.varint(strings.length);
|
|
54
|
+
let prev: Uint8Array = new Uint8Array(0);
|
|
55
|
+
for (const s of strings) {
|
|
56
|
+
const b = utf8(s);
|
|
57
|
+
const shared = commonPrefix(prev, b);
|
|
58
|
+
w.varint(shared);
|
|
59
|
+
w.varint(b.length - shared);
|
|
60
|
+
w.bytes(b.subarray(shared));
|
|
61
|
+
prev = b;
|
|
62
|
+
}
|
|
63
|
+
return w.finish();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function readStringTable(bytes: Uint8Array, maxStrings: number): string[] {
|
|
67
|
+
const r = new ByteReader(bytes);
|
|
68
|
+
const n = boundedCount(r, maxStrings, 'string table');
|
|
69
|
+
const dec = new TextDecoder('utf-8', { fatal: true });
|
|
70
|
+
const out: string[] = [];
|
|
71
|
+
let prev: Uint8Array = new Uint8Array(0);
|
|
72
|
+
for (let i = 0; i < n; i++) {
|
|
73
|
+
const shared = r.varint(), rest = r.varint();
|
|
74
|
+
if (shared > prev.length) throw new PackFormatError('string table: prefix longer than the previous string');
|
|
75
|
+
const b = new Uint8Array(shared + rest);
|
|
76
|
+
b.set(prev.subarray(0, shared));
|
|
77
|
+
b.set(r.bytes(rest), shared);
|
|
78
|
+
try { out.push(dec.decode(b)); } catch { throw new PackFormatError('string table: invalid UTF-8'); }
|
|
79
|
+
prev = b;
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function writeHashTable(hashes: readonly string[]): Uint8Array {
|
|
85
|
+
const w = new ByteWriter();
|
|
86
|
+
w.varint(hashes.length);
|
|
87
|
+
for (const h of hashes) {
|
|
88
|
+
if (HEX64.test(h)) {
|
|
89
|
+
w.u8(0);
|
|
90
|
+
const raw = new Uint8Array(32);
|
|
91
|
+
for (let i = 0; i < 32; i++) raw[i] = parseInt(h.slice(i * 2, i * 2 + 2), 16);
|
|
92
|
+
w.bytes(raw);
|
|
93
|
+
} else {
|
|
94
|
+
w.u8(1);
|
|
95
|
+
w.str(h);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return w.finish();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function readHashTable(bytes: Uint8Array, maxHashes: number): string[] {
|
|
102
|
+
const r = new ByteReader(bytes);
|
|
103
|
+
const n = boundedCount(r, maxHashes, 'hash table');
|
|
104
|
+
const out: string[] = [];
|
|
105
|
+
for (let i = 0; i < n; i++) {
|
|
106
|
+
const flag = r.u8();
|
|
107
|
+
if (flag === 0) {
|
|
108
|
+
let hex = '';
|
|
109
|
+
for (const b of r.bytes(32)) hex += (b < 16 ? '0' : '') + b.toString(16);
|
|
110
|
+
out.push(hex);
|
|
111
|
+
} else if (flag === 1) out.push(r.str());
|
|
112
|
+
else throw new PackFormatError('hash table: unknown entry flag');
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The read-side lookup over decoded tables; an out-of-range index is a malformed pack. */
|
|
118
|
+
export function readTables(strings: readonly string[], hashes: readonly string[]): ReadTables {
|
|
119
|
+
return {
|
|
120
|
+
str: (i) => { const v = strings[i]; if (v === undefined) throw new PackFormatError('string index out of range'); return v; },
|
|
121
|
+
hash: (i) => { const v = hashes[i]; if (v === undefined) throw new PackFormatError('hash index out of range'); return v; },
|
|
122
|
+
};
|
|
123
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/types.ts — the BFKP v1 data model (PRD 41 A11): what `encodePack` takes and
|
|
3
|
+
* `decodePack` returns. Rows reuse the knowledge model's own types (`StoredFact`, `StoredRelation`,
|
|
4
|
+
* `Extracted`, `ScopeRecord`), so a pack carries exactly what the single writer stores.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Extracted, KEdge, KNode, NodeRef, ScopeRecord, StoredFact, StoredOrigin, StoredRelation } from '../model.js';
|
|
8
|
+
|
|
9
|
+
/** A reviewed file: its root-relative path, the hash its extracted output is keyed under, and its size. */
|
|
10
|
+
export interface PackFileRow { path: string; hash: string; bytes: number }
|
|
11
|
+
|
|
12
|
+
/** A source's current hash (what the old `markStale` call recorded). '' = the file was removed. */
|
|
13
|
+
export interface PackStaleRow { sourceRef: string; hash: string }
|
|
14
|
+
|
|
15
|
+
/** A relation's upsert identity. */
|
|
16
|
+
export interface RelationId { from: string; to: string; kind: string }
|
|
17
|
+
|
|
18
|
+
/** Tombstones: keys the sender no longer has. */
|
|
19
|
+
export interface PackDeletes { files: string[]; facts: string[]; relations: RelationId[] }
|
|
20
|
+
|
|
21
|
+
/** One list of a re-extracted entry, as a diff against the prior extraction: items to drop and items to insert at a position. */
|
|
22
|
+
export interface ListDiff<T> { remove: string[]; add: Array<{ at: number; item: T }> }
|
|
23
|
+
|
|
24
|
+
/** What changed in one source's extraction since the acknowledged prior one (same `sourceRef`). */
|
|
25
|
+
export interface ExtrEntryDiff {
|
|
26
|
+
sourceRef: string;
|
|
27
|
+
sourceHash: string;
|
|
28
|
+
extractorVersion?: number;
|
|
29
|
+
nodes: ListDiff<KNode>;
|
|
30
|
+
edges: ListDiff<KEdge>;
|
|
31
|
+
keys: ListDiff<{ ref: string; key: string }>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The Extracted output of one content hash: whole, or as a diff against the prior hash's output. */
|
|
35
|
+
export type PackExtr =
|
|
36
|
+
| { kind: 'full'; hash: string; entries: Extracted[] }
|
|
37
|
+
| { kind: 'diff'; hash: string; prior: string; entries: ExtrEntryDiff[] };
|
|
38
|
+
|
|
39
|
+
/** A leaf of the manifest tree: a stable key and the digest of its content. */
|
|
40
|
+
export interface ManifestLeaf { key: string; digest: string }
|
|
41
|
+
|
|
42
|
+
/** Everything a pack can carry besides its frame fields. Every part is optional (an empty section is not written). */
|
|
43
|
+
export interface PackBody {
|
|
44
|
+
files?: PackFileRow[];
|
|
45
|
+
extracted?: PackExtr[];
|
|
46
|
+
facts?: StoredFact[];
|
|
47
|
+
relations?: StoredRelation[];
|
|
48
|
+
stale?: PackStaleRow[];
|
|
49
|
+
deletes?: PackDeletes;
|
|
50
|
+
scope?: ScopeRecord;
|
|
51
|
+
/** The analysis snapshot (JSON-compatible). */
|
|
52
|
+
analysis?: unknown;
|
|
53
|
+
/** Hashes named for their own sake (a `have` request). Hashes a section already references are not repeated here. */
|
|
54
|
+
hashes?: string[];
|
|
55
|
+
/** Manifest leaves (a reconcile answer). */
|
|
56
|
+
leaves?: ManifestLeaf[];
|
|
57
|
+
/** The 4,096 bucket digests of the manifest tree, hex (a manifest GET answer). */
|
|
58
|
+
buckets?: string[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The frame fields plus the body. */
|
|
62
|
+
export interface Pack extends PackBody {
|
|
63
|
+
rootId: string;
|
|
64
|
+
/** The server generation this pack applies to; 0 = a full pack. */
|
|
65
|
+
baseGeneration: number;
|
|
66
|
+
/** The digest the server must reach after applying (64 hex); `ZERO_DIGEST` when the pack asserts none. */
|
|
67
|
+
targetDigest: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const ZERO_DIGEST = '0'.repeat(64);
|
|
71
|
+
|
|
72
|
+
/** A pack's decoded counts, for guards before any work. */
|
|
73
|
+
export interface PackLimits {
|
|
74
|
+
/** Bytes the body may inflate to (default 64 MB). */
|
|
75
|
+
maxDecodedBytes: number;
|
|
76
|
+
/** Strings in the table (default 2,000,000). */
|
|
77
|
+
maxStrings: number;
|
|
78
|
+
/** Rows in any one section (default 1,000,000). */
|
|
79
|
+
maxRows: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const DEFAULT_PACK_LIMITS: PackLimits = { maxDecodedBytes: 64 * 1024 * 1024, maxStrings: 2_000_000, maxRows: 1_000_000 };
|
|
83
|
+
|
|
84
|
+
/** The decoded body inflated past `maxDecodedBytes`: the server answers 413 `pack_too_large`. */
|
|
85
|
+
export class PackTooLargeError extends Error {
|
|
86
|
+
constructor(readonly limit: number) { super(`pack inflates past ${limit} bytes`); this.name = 'PackTooLargeError'; }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type { Extracted, KEdge, KNode, NodeRef, ScopeRecord, StoredFact, StoredOrigin, StoredRelation };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/varint.ts — the byte-level primitives of BFKP v1 (PRD 41 A11): a growable byte
|
|
3
|
+
* writer and a bounds-checked reader over unsigned LEB128 varints, raw bytes, UTF-8 strings, and
|
|
4
|
+
* a compact number codec. Pure TypeScript — no Buffer, no node: imports.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** A malformed pack: the server answers 422 `bad_pack`. */
|
|
8
|
+
export class PackFormatError extends Error {
|
|
9
|
+
constructor(message: string) { super(message); this.name = 'PackFormatError'; }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const ENCODER = new TextEncoder();
|
|
13
|
+
const DECODER = new TextDecoder('utf-8', { fatal: true });
|
|
14
|
+
|
|
15
|
+
export const utf8 = (s: string): Uint8Array => ENCODER.encode(s);
|
|
16
|
+
|
|
17
|
+
/** Largest integer a varint carries (2^53 - 1, JS's safe integer). */
|
|
18
|
+
export const MAX_VARINT = Number.MAX_SAFE_INTEGER;
|
|
19
|
+
|
|
20
|
+
export class ByteWriter {
|
|
21
|
+
private buf = new Uint8Array(1024);
|
|
22
|
+
private len = 0;
|
|
23
|
+
|
|
24
|
+
private room(n: number): void {
|
|
25
|
+
if (this.len + n <= this.buf.length) return;
|
|
26
|
+
let cap = this.buf.length;
|
|
27
|
+
while (cap < this.len + n) cap *= 2;
|
|
28
|
+
const next = new Uint8Array(cap);
|
|
29
|
+
next.set(this.buf.subarray(0, this.len));
|
|
30
|
+
this.buf = next;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get length(): number { return this.len; }
|
|
34
|
+
|
|
35
|
+
u8(v: number): void { this.room(1); this.buf[this.len++] = v & 0xff; }
|
|
36
|
+
|
|
37
|
+
/** Unsigned LEB128. */
|
|
38
|
+
varint(v: number): void {
|
|
39
|
+
if (!Number.isSafeInteger(v) || v < 0) throw new PackFormatError(`not an unsigned varint: ${v}`);
|
|
40
|
+
this.room(8);
|
|
41
|
+
while (v >= 0x80) { this.buf[this.len++] = (v % 0x80) | 0x80; v = Math.floor(v / 0x80); }
|
|
42
|
+
this.buf[this.len++] = v;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
bytes(b: Uint8Array): void { this.room(b.length); this.buf.set(b, this.len); this.len += b.length; }
|
|
46
|
+
|
|
47
|
+
/** A varint length, then the UTF-8 bytes. */
|
|
48
|
+
str(s: string): void { const b = utf8(s); this.varint(b.length); this.bytes(b); }
|
|
49
|
+
|
|
50
|
+
u32le(v: number): void { this.room(4); for (let i = 0; i < 4; i++) this.buf[this.len++] = (v >>> (8 * i)) & 0xff; }
|
|
51
|
+
|
|
52
|
+
f64(v: number): void {
|
|
53
|
+
this.room(8);
|
|
54
|
+
new DataView(this.buf.buffer, this.buf.byteOffset + this.len, 8).setFloat64(0, v, true);
|
|
55
|
+
this.len += 8;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A finite number, as small as it can be and always exactly recoverable: a non-negative integer
|
|
60
|
+
* (tag 0), a non-negative decimal of at most 6 places (tag 1, scaled), or a raw float64 (tag 2).
|
|
61
|
+
*/
|
|
62
|
+
num(v: number): void {
|
|
63
|
+
if (Number.isSafeInteger(v) && v >= 0 && v < 2 ** 50) { this.varint(v * 4); return; }
|
|
64
|
+
if (v >= 0 && v < 2 ** 30) {
|
|
65
|
+
const scaled = Math.round(v * 1e6);
|
|
66
|
+
if (scaled / 1e6 === v) { this.varint(scaled * 4 + 1); return; }
|
|
67
|
+
}
|
|
68
|
+
this.varint(2);
|
|
69
|
+
this.f64(v);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
finish(): Uint8Array { return this.buf.slice(0, this.len); }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export class ByteReader {
|
|
76
|
+
private pos = 0;
|
|
77
|
+
constructor(private readonly buf: Uint8Array) {}
|
|
78
|
+
|
|
79
|
+
get offset(): number { return this.pos; }
|
|
80
|
+
get done(): boolean { return this.pos >= this.buf.length; }
|
|
81
|
+
get remaining(): number { return this.buf.length - this.pos; }
|
|
82
|
+
|
|
83
|
+
u8(): number {
|
|
84
|
+
if (this.pos >= this.buf.length) throw new PackFormatError('unexpected end of data');
|
|
85
|
+
return this.buf[this.pos++]!;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
varint(): number {
|
|
89
|
+
let result = 0, scale = 1;
|
|
90
|
+
for (let i = 0; i < 8; i++) {
|
|
91
|
+
const b = this.u8();
|
|
92
|
+
result += (b & 0x7f) * scale;
|
|
93
|
+
if ((b & 0x80) === 0) return result;
|
|
94
|
+
scale *= 0x80;
|
|
95
|
+
}
|
|
96
|
+
throw new PackFormatError('varint too long');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
bytes(n: number): Uint8Array {
|
|
100
|
+
if (!Number.isSafeInteger(n) || n < 0 || this.pos + n > this.buf.length) throw new PackFormatError('unexpected end of data');
|
|
101
|
+
const out = this.buf.subarray(this.pos, this.pos + n);
|
|
102
|
+
this.pos += n;
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
str(): string {
|
|
107
|
+
try { return DECODER.decode(this.bytes(this.varint())); } catch (e) {
|
|
108
|
+
if (e instanceof PackFormatError) throw e;
|
|
109
|
+
throw new PackFormatError('invalid UTF-8');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
u32le(): number {
|
|
114
|
+
const b = this.bytes(4);
|
|
115
|
+
return (b[0]! | (b[1]! << 8) | (b[2]! << 16) | (b[3]! << 24)) >>> 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
f64(): number {
|
|
119
|
+
const b = this.bytes(8);
|
|
120
|
+
return new DataView(b.buffer, b.byteOffset, 8).getFloat64(0, true);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
num(): number {
|
|
124
|
+
const v = this.varint();
|
|
125
|
+
const tag = v % 4;
|
|
126
|
+
if (tag === 0) return v / 4;
|
|
127
|
+
if (tag === 1) return (v - 1) / 4 / 1e6;
|
|
128
|
+
if (v === 2) return this.f64();
|
|
129
|
+
throw new PackFormatError('bad number tag');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** A count that is bounded before anything is allocated for it. */
|
|
134
|
+
export function boundedCount(r: ByteReader, max: number, what: string): number {
|
|
135
|
+
const n = r.varint();
|
|
136
|
+
if (n > max) throw new PackFormatError(`${what}: ${n} exceeds the bound ${max}`);
|
|
137
|
+
return n;
|
|
138
|
+
}
|
|
@@ -54,6 +54,10 @@ describe('globs and partitionOf', () => {
|
|
|
54
54
|
expect(matchGlob('b.md', '*')).toBe(true);
|
|
55
55
|
expect(matchGlob('x.ts', '*.{ts,tsx}')).toBe(true);
|
|
56
56
|
expect(globToRegExp('a.b').test('aXb')).toBe(false);
|
|
57
|
+
// A backslash makes the next character literal.
|
|
58
|
+
expect(matchGlob('copy*/x.ts', 'copy\\*/**')).toBe(true);
|
|
59
|
+
expect(matchGlob('copyabc/x.ts', 'copy\\*/**')).toBe(false);
|
|
60
|
+
expect(matchGlob('a[1]', 'a\\[1\\]')).toBe(true);
|
|
57
61
|
});
|
|
58
62
|
|
|
59
63
|
it('takes the first matching rule', () => {
|
|
@@ -19,7 +19,7 @@ const globCache = new Map<string, RegExp>();
|
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* A glob as a RegExp over `/`-separated paths: `**` spans folders (`**\/` may match none),
|
|
22
|
-
* `*` and `?` stay within one segment, `{a,b}` is alternation
|
|
22
|
+
* `*` and `?` stay within one segment, `{a,b}` is alternation, `\x` is the literal `x`.
|
|
23
23
|
*/
|
|
24
24
|
export function globToRegExp(glob: string): RegExp {
|
|
25
25
|
const cached = globCache.get(glob);
|
|
@@ -27,6 +27,8 @@ export function globToRegExp(glob: string): RegExp {
|
|
|
27
27
|
let re = '';
|
|
28
28
|
for (let i = 0; i < glob.length; i++) {
|
|
29
29
|
const ch = glob[i]!;
|
|
30
|
+
// A backslash makes the next character literal (`copy\*` is the folder named `copy*`), as in `.gitignore`.
|
|
31
|
+
if (ch === '\\' && i + 1 < glob.length) { re += escapeRe(glob[++i]!); continue; }
|
|
30
32
|
if (ch === '*') {
|
|
31
33
|
if (glob[i + 1] === '*') {
|
|
32
34
|
const slashAfter = glob[i + 2] === '/';
|