@seanhogg/builderforce-memory 2026.10.45 → 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 +13 -7
- 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/index.d.ts +5 -3
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +2 -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/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/package.json +7 -3
- package/src/knowledge/build.ts +11 -5
- package/src/knowledge/extractFile.ts +26 -6
- package/src/knowledge/extractJob.ts +26 -7
- package/src/knowledge/index.ts +5 -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/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 +12 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/sections/extrLists.ts — the three lists an Extracted entry holds (nodes, edges,
|
|
3
|
+
* keys), written column by column (PRD 41 A11): kinds as small codes, names as STR indexes, the
|
|
4
|
+
* ref index run zigzag-delta coded, optional fields behind a flag word.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { KEdge, KNode, NodeRef } from '../types.js';
|
|
8
|
+
import type { ReadTables, Tables } from '../strings.js';
|
|
9
|
+
import { boundedCount, ByteReader, ByteWriter, PackFormatError } from '../varint.js';
|
|
10
|
+
import { EDGE_KINDS, NODE_KINDS, ORIGINS, VERDICTS, readEnum, readWord, unzigzag, writeEnum, writeWord, zigzag } from './columns.js';
|
|
11
|
+
|
|
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
|
+
|
|
14
|
+
export function writeNodes(w: ByteWriter, t: Tables, nodes: readonly KNode[]): void {
|
|
15
|
+
w.varint(nodes.length);
|
|
16
|
+
let prev = 0;
|
|
17
|
+
for (const n of nodes) { const i = t.str(n.ref); w.varint(zigzag(i - prev)); prev = i; }
|
|
18
|
+
for (const n of nodes) writeWord(w, t, NODE_KINDS, n.kind);
|
|
19
|
+
for (const n of nodes) w.varint(t.str(n.label));
|
|
20
|
+
for (const n of nodes) w.varint(t.str(n.pack));
|
|
21
|
+
for (const n of nodes) w.num(n.importance);
|
|
22
|
+
for (const n of nodes) writeEnum(w, ORIGINS, n.origin);
|
|
23
|
+
const flags = nodes.map((n) =>
|
|
24
|
+
(n.partition !== undefined ? N_PARTITION : 0) | (n.area !== undefined ? N_AREA : 0)
|
|
25
|
+
| (n.stale !== undefined ? N_STALE : 0) | (n.stale ? N_STALE_V : 0) | (n.badges !== undefined ? N_BADGES : 0)
|
|
26
|
+
| (n.system !== undefined ? N_SYSTEM : 0) | (n.app !== undefined ? N_APP : 0) | (n.copyOf !== undefined ? N_COPY : 0)
|
|
27
|
+
| (n.sourceHash !== undefined ? N_HASH : 0));
|
|
28
|
+
for (const f of flags) w.varint(f);
|
|
29
|
+
nodes.forEach((n, i) => {
|
|
30
|
+
const f = flags[i]!;
|
|
31
|
+
if (f & N_PARTITION) w.varint(t.str(n.partition!));
|
|
32
|
+
if (f & N_AREA) w.varint(t.str(n.area!));
|
|
33
|
+
if (f & N_SYSTEM) w.varint(t.str(n.system!));
|
|
34
|
+
if (f & N_APP) w.varint(t.str(n.app!));
|
|
35
|
+
if (f & N_COPY) w.varint(t.str(n.copyOf!));
|
|
36
|
+
if (f & N_HASH) w.varint(t.hash(n.sourceHash!));
|
|
37
|
+
if (f & N_BADGES) {
|
|
38
|
+
const keys = Object.keys(n.badges!);
|
|
39
|
+
w.varint(keys.length);
|
|
40
|
+
for (const k of keys) {
|
|
41
|
+
w.varint(t.str(k));
|
|
42
|
+
const v = n.badges![k]!;
|
|
43
|
+
if (typeof v === 'number') { w.u8(0); w.num(v); } else { w.u8(1); w.varint(t.str(v)); }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function readNodes(r: ByteReader, t: ReadTables, max: number): KNode[] {
|
|
50
|
+
const n = boundedCount(r, max, 'nodes');
|
|
51
|
+
const refs: string[] = [];
|
|
52
|
+
let prev = 0;
|
|
53
|
+
for (let i = 0; i < n; i++) { prev += unzigzag(r.varint()); refs.push(t.str(prev)); }
|
|
54
|
+
const kinds = Array.from({ length: n }, () => readWord(r, t, NODE_KINDS));
|
|
55
|
+
const labels = Array.from({ length: n }, () => t.str(r.varint()));
|
|
56
|
+
const packs = Array.from({ length: n }, () => t.str(r.varint()));
|
|
57
|
+
const importance = Array.from({ length: n }, () => r.num());
|
|
58
|
+
const origins = Array.from({ length: n }, () => readEnum(r, ORIGINS));
|
|
59
|
+
const flags = Array.from({ length: n }, () => r.varint());
|
|
60
|
+
return refs.map((ref, i): KNode => {
|
|
61
|
+
const f = flags[i]!;
|
|
62
|
+
const node: KNode = { ref: ref as NodeRef, kind: kinds[i]!, label: labels[i]!, pack: packs[i]!, importance: importance[i]!, origin: origins[i]! };
|
|
63
|
+
if (f & N_PARTITION) node.partition = t.str(r.varint());
|
|
64
|
+
if (f & N_AREA) node.area = t.str(r.varint()) as NodeRef;
|
|
65
|
+
if (f & N_SYSTEM) node.system = t.str(r.varint());
|
|
66
|
+
if (f & N_APP) node.app = t.str(r.varint());
|
|
67
|
+
if (f & N_COPY) node.copyOf = t.str(r.varint());
|
|
68
|
+
if (f & N_HASH) node.sourceHash = t.hash(r.varint());
|
|
69
|
+
if (f & N_STALE) node.stale = (f & N_STALE_V) !== 0;
|
|
70
|
+
if (f & N_BADGES) {
|
|
71
|
+
const badges: Record<string, number | string> = {};
|
|
72
|
+
const count = boundedCount(r, max, 'badges');
|
|
73
|
+
for (let b = 0; b < count; b++) {
|
|
74
|
+
const key = t.str(r.varint());
|
|
75
|
+
badges[key] = r.u8() === 0 ? r.num() : t.str(r.varint());
|
|
76
|
+
}
|
|
77
|
+
node.badges = badges;
|
|
78
|
+
}
|
|
79
|
+
return node;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const E_CONF = 1, E_VERDICT = 2, E_CYCLE = 4, E_CYCLE_V = 8, E_STALE = 16, E_STALE_V = 32, E_BYNAME = 64;
|
|
84
|
+
|
|
85
|
+
export function writeEdges(w: ByteWriter, t: Tables, edges: readonly KEdge[]): void {
|
|
86
|
+
w.varint(edges.length);
|
|
87
|
+
let prev = 0;
|
|
88
|
+
for (const e of edges) { const i = t.str(e.from); w.varint(zigzag(i - prev)); prev = i; }
|
|
89
|
+
for (const e of edges) w.varint(t.str(e.to));
|
|
90
|
+
for (const e of edges) writeWord(w, t, EDGE_KINDS, e.kind);
|
|
91
|
+
for (const e of edges) w.num(e.weight);
|
|
92
|
+
for (const e of edges) writeEnum(w, ORIGINS, e.origin);
|
|
93
|
+
const flags = edges.map((e) =>
|
|
94
|
+
(e.confidence !== undefined ? E_CONF : 0) | (e.verdict !== undefined ? E_VERDICT : 0)
|
|
95
|
+
| (e.cycle !== undefined ? E_CYCLE : 0) | (e.cycle ? E_CYCLE_V : 0)
|
|
96
|
+
| (e.stale !== undefined ? E_STALE : 0) | (e.stale ? E_STALE_V : 0) | (e.byName !== undefined ? E_BYNAME : 0));
|
|
97
|
+
for (const f of flags) w.varint(f);
|
|
98
|
+
edges.forEach((e, i) => {
|
|
99
|
+
const f = flags[i]!;
|
|
100
|
+
if (f & E_CONF) w.num(e.confidence!);
|
|
101
|
+
if (f & E_VERDICT) writeEnum(w, VERDICTS, e.verdict!);
|
|
102
|
+
if (f & E_BYNAME) w.varint(t.str(e.byName!));
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function readEdges(r: ByteReader, t: ReadTables, max: number): KEdge[] {
|
|
107
|
+
const n = boundedCount(r, max, 'edges');
|
|
108
|
+
const from: string[] = [];
|
|
109
|
+
let prev = 0;
|
|
110
|
+
for (let i = 0; i < n; i++) { prev += unzigzag(r.varint()); from.push(t.str(prev)); }
|
|
111
|
+
const to = Array.from({ length: n }, () => t.str(r.varint()));
|
|
112
|
+
const kinds = Array.from({ length: n }, () => readWord(r, t, EDGE_KINDS));
|
|
113
|
+
const weights = Array.from({ length: n }, () => r.num());
|
|
114
|
+
const origins = Array.from({ length: n }, () => readEnum(r, ORIGINS));
|
|
115
|
+
const flags = Array.from({ length: n }, () => r.varint());
|
|
116
|
+
return from.map((f0, i): KEdge => {
|
|
117
|
+
const f = flags[i]!;
|
|
118
|
+
const edge: KEdge = { from: f0 as NodeRef, to: to[i]! as NodeRef, kind: kinds[i]!, weight: weights[i]!, origin: origins[i]! };
|
|
119
|
+
if (f & E_CONF) edge.confidence = r.num();
|
|
120
|
+
if (f & E_VERDICT) edge.verdict = readEnum(r, VERDICTS);
|
|
121
|
+
if (f & E_CYCLE) edge.cycle = (f & E_CYCLE_V) !== 0;
|
|
122
|
+
if (f & E_STALE) edge.stale = (f & E_STALE_V) !== 0;
|
|
123
|
+
if (f & E_BYNAME) edge.byName = t.str(r.varint());
|
|
124
|
+
return edge;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type KeyRow = { ref: string; key: string };
|
|
129
|
+
|
|
130
|
+
export function writeKeys(w: ByteWriter, t: Tables, keys: readonly KeyRow[]): void {
|
|
131
|
+
w.varint(keys.length);
|
|
132
|
+
let prev = 0;
|
|
133
|
+
for (const k of keys) { const i = t.str(k.ref); w.varint(zigzag(i - prev)); prev = i; }
|
|
134
|
+
for (const k of keys) w.varint(t.str(k.key));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function readKeys(r: ByteReader, t: ReadTables, max: number): Array<{ ref: NodeRef; key: string }> {
|
|
138
|
+
const n = boundedCount(r, max, 'keys');
|
|
139
|
+
const refs: string[] = [];
|
|
140
|
+
let prev = 0;
|
|
141
|
+
for (let i = 0; i < n; i++) { prev += unzigzag(r.varint()); refs.push(t.str(prev)); }
|
|
142
|
+
return refs.map((ref) => ({ ref: ref as NodeRef, key: t.str(r.varint()) }));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function assertFlagsKnown(flags: number, mask: number): void {
|
|
146
|
+
if (flags & ~mask) throw new PackFormatError('unknown flag bits');
|
|
147
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/sections/facts.ts — the FACT and REL sections of BFKP v1 (PRD 41 A11): the stored
|
|
3
|
+
* rows, columnar, keys as STR indexes (sorted, so delta-coded), relation kind as an enum.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { NodeRef, StoredFact, StoredRelation } from '../types.js';
|
|
7
|
+
import { boundedCount, ByteReader, ByteWriter, PackFormatError } from '../varint.js';
|
|
8
|
+
import { EDGE_KINDS, STORED_ORIGINS, readEnum, readWord, writeEnum, writeWord, type SectionCodec } from './columns.js';
|
|
9
|
+
import { TAG } from './rows.js';
|
|
10
|
+
|
|
11
|
+
const F_UPDATED = 1, F_BY_NULL = 2, F_BY = 4;
|
|
12
|
+
const A_LINE = 1, A_HASH = 2;
|
|
13
|
+
|
|
14
|
+
export const factSection: SectionCodec = {
|
|
15
|
+
tag: TAG.FACT,
|
|
16
|
+
write(pack, t) {
|
|
17
|
+
const rows = pack.facts;
|
|
18
|
+
if (!rows?.length) return undefined;
|
|
19
|
+
const w = new ByteWriter();
|
|
20
|
+
w.varint(rows.length);
|
|
21
|
+
let prev = 0;
|
|
22
|
+
for (const f of rows) { const i = t.str(f.key); w.varint(i - prev); prev = i; }
|
|
23
|
+
for (const f of rows) w.varint(t.str(f.content));
|
|
24
|
+
for (const f of rows) w.varint(t.str(f.source));
|
|
25
|
+
for (const f of rows) w.num(f.importance);
|
|
26
|
+
for (const f of rows) writeEnum(w, STORED_ORIGINS, f.origin);
|
|
27
|
+
const flags = rows.map((f) => (f.updatedAt !== undefined ? F_UPDATED : 0) | (f.createdBy === null ? F_BY_NULL : 0) | (typeof f.createdBy === 'string' ? F_BY : 0));
|
|
28
|
+
for (const f of flags) w.u8(f);
|
|
29
|
+
rows.forEach((f, i) => {
|
|
30
|
+
if (flags[i]! & F_UPDATED) w.varint(t.str(f.updatedAt!));
|
|
31
|
+
if (flags[i]! & F_BY) w.varint(t.str(f.createdBy!));
|
|
32
|
+
});
|
|
33
|
+
for (const f of rows) w.varint(f.fileAnchors.length);
|
|
34
|
+
for (const f of rows) for (const a of f.fileAnchors) w.varint(t.str(a.path));
|
|
35
|
+
for (const f of rows) for (const a of f.fileAnchors) w.u8((a.line !== undefined ? A_LINE : 0) | (a.contentHash !== undefined ? A_HASH : 0));
|
|
36
|
+
for (const f of rows) for (const a of f.fileAnchors) {
|
|
37
|
+
if (a.line !== undefined) w.varint(a.line);
|
|
38
|
+
if (a.contentHash !== undefined) w.varint(t.str(a.contentHash));
|
|
39
|
+
}
|
|
40
|
+
return w.finish();
|
|
41
|
+
},
|
|
42
|
+
read(bytes, t, limits, into) {
|
|
43
|
+
const r = new ByteReader(bytes);
|
|
44
|
+
const n = boundedCount(r, limits.maxRows, 'facts');
|
|
45
|
+
const keys: string[] = [];
|
|
46
|
+
let prev = 0;
|
|
47
|
+
for (let i = 0; i < n; i++) { prev += r.varint(); keys.push(t.str(prev)); }
|
|
48
|
+
const content = keys.map(() => t.str(r.varint()));
|
|
49
|
+
const source = keys.map(() => t.str(r.varint()));
|
|
50
|
+
const importance = keys.map(() => r.num());
|
|
51
|
+
const origin = keys.map(() => readEnum(r, STORED_ORIGINS));
|
|
52
|
+
const flags = keys.map(() => r.u8());
|
|
53
|
+
const facts: StoredFact[] = keys.map((key, i) => ({ key, content: content[i]!, source: source[i]!, importance: importance[i]!, origin: origin[i]!, fileAnchors: [] }));
|
|
54
|
+
facts.forEach((f, i) => {
|
|
55
|
+
if (flags[i]! & F_UPDATED) f.updatedAt = t.str(r.varint());
|
|
56
|
+
if (flags[i]! & F_BY_NULL) f.createdBy = null;
|
|
57
|
+
if (flags[i]! & F_BY) f.createdBy = t.str(r.varint());
|
|
58
|
+
});
|
|
59
|
+
const counts = facts.map(() => boundedCount(r, limits.maxRows, 'anchors'));
|
|
60
|
+
const total = counts.reduce((a, b) => a + b, 0);
|
|
61
|
+
if (total > limits.maxRows) throw new PackFormatError('anchors: over the row bound');
|
|
62
|
+
const paths = Array.from({ length: total }, () => t.str(r.varint()));
|
|
63
|
+
const aflags = Array.from({ length: total }, () => r.u8());
|
|
64
|
+
let at = 0;
|
|
65
|
+
facts.forEach((f, i) => {
|
|
66
|
+
for (let k = 0; k < counts[i]!; k++, at++) {
|
|
67
|
+
const a: StoredFact['fileAnchors'][number] = { path: paths[at]! };
|
|
68
|
+
if (aflags[at]! & A_LINE) a.line = r.varint();
|
|
69
|
+
if (aflags[at]! & A_HASH) a.contentHash = t.str(r.varint());
|
|
70
|
+
f.fileAnchors.push(a);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
into.facts = facts;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const R_CONF = 1, R_HASH = 2, R_STALE = 4, R_STALE_NULL = 8;
|
|
78
|
+
|
|
79
|
+
export const relationSection: SectionCodec = {
|
|
80
|
+
tag: TAG.REL,
|
|
81
|
+
write(pack, t) {
|
|
82
|
+
const rows = pack.relations;
|
|
83
|
+
if (!rows?.length) return undefined;
|
|
84
|
+
const w = new ByteWriter();
|
|
85
|
+
w.varint(rows.length);
|
|
86
|
+
let prev = 0;
|
|
87
|
+
for (const x of rows) { const i = t.str(x.from); w.varint(i - prev); prev = i; }
|
|
88
|
+
for (const x of rows) w.varint(t.str(x.to));
|
|
89
|
+
for (const x of rows) writeWord(w, t, EDGE_KINDS, x.kind);
|
|
90
|
+
for (const x of rows) w.num(x.weight);
|
|
91
|
+
for (const x of rows) writeEnum(w, STORED_ORIGINS, x.origin);
|
|
92
|
+
const flags = rows.map((x) => (x.confidence !== undefined ? R_CONF : 0) | (x.sourceHash !== undefined ? R_HASH : 0)
|
|
93
|
+
| (typeof x.staleAt === 'string' ? R_STALE : 0) | (x.staleAt === null ? R_STALE_NULL : 0));
|
|
94
|
+
for (const f of flags) w.u8(f);
|
|
95
|
+
rows.forEach((x, i) => {
|
|
96
|
+
if (flags[i]! & R_CONF) w.num(x.confidence!);
|
|
97
|
+
if (flags[i]! & R_HASH) w.varint(t.hash(x.sourceHash!));
|
|
98
|
+
if (flags[i]! & R_STALE) w.varint(t.str(x.staleAt!));
|
|
99
|
+
});
|
|
100
|
+
return w.finish();
|
|
101
|
+
},
|
|
102
|
+
read(bytes, t, limits, into) {
|
|
103
|
+
const r = new ByteReader(bytes);
|
|
104
|
+
const n = boundedCount(r, limits.maxRows, 'relations');
|
|
105
|
+
const from: string[] = [];
|
|
106
|
+
let prev = 0;
|
|
107
|
+
for (let i = 0; i < n; i++) { prev += r.varint(); from.push(t.str(prev)); }
|
|
108
|
+
const to = from.map(() => t.str(r.varint()));
|
|
109
|
+
const kind = from.map(() => readWord(r, t, EDGE_KINDS));
|
|
110
|
+
const weight = from.map(() => r.num());
|
|
111
|
+
const origin = from.map(() => readEnum(r, STORED_ORIGINS));
|
|
112
|
+
const flags = from.map(() => r.u8());
|
|
113
|
+
into.relations = from.map((f0, i): StoredRelation => {
|
|
114
|
+
const x: StoredRelation = { from: f0 as NodeRef, to: to[i]! as NodeRef, kind: kind[i]!, weight: weight[i]!, origin: origin[i]! };
|
|
115
|
+
const f = flags[i]!;
|
|
116
|
+
if (f & R_CONF) x.confidence = r.num();
|
|
117
|
+
if (f & R_HASH) x.sourceHash = t.hash(r.varint());
|
|
118
|
+
if (f & R_STALE) x.staleAt = t.str(r.varint());
|
|
119
|
+
if (f & R_STALE_NULL) x.staleAt = null;
|
|
120
|
+
return x;
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** knowledge/pack/sections/index.ts — every row section of BFKP v1, in the order they are written (STR and HASH are written first by encode.ts). */
|
|
2
|
+
|
|
3
|
+
import { deleteSection, analysisSection, bucketSection, fileSection, leafSection, scopeSection, staleSection } from './rows.js';
|
|
4
|
+
import { extrSection } from './extr.js';
|
|
5
|
+
import { factSection, relationSection } from './facts.js';
|
|
6
|
+
import type { SectionCodec } from './columns.js';
|
|
7
|
+
|
|
8
|
+
export const ROW_SECTIONS: readonly SectionCodec[] = [
|
|
9
|
+
fileSection, extrSection, factSection, relationSection, staleSection, deleteSection, scopeSection, analysisSection, leafSection, bucketSection,
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export { TAG } from './rows.js';
|
|
13
|
+
export type { SectionCodec } from './columns.js';
|
|
@@ -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 };
|