@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,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/pack.roundtrip.test.ts — BFKP v1 is lossless and deterministic (PRD 41 A11 Accept):
|
|
3
|
+
* a property-style round trip over seeded generated graphs, and equal input gives equal bytes.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { decodePack, encodePack, normalizePack, ZERO_DIGEST, type Pack } from './index.js';
|
|
7
|
+
import { genPack } from './pack.testkit.test-util.js';
|
|
8
|
+
|
|
9
|
+
describe('BFKP round trip', () => {
|
|
10
|
+
it('decode(encode(x)) deep-equals the normalized input over 40 generated graphs', async () => {
|
|
11
|
+
for (let seed = 1; seed <= 40; seed++) {
|
|
12
|
+
const pack = genPack(seed, { files: seed % 11 === 0 ? 0 : (seed * 7) % 45, rich: seed % 2 === 0, hashes: seed % 5 === 0 });
|
|
13
|
+
const decoded = await decodePack(await encodePack(pack));
|
|
14
|
+
expect(decoded).toEqual(normalizePack(pack));
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('round-trips an uncompressed frame the same as a deflated one', async () => {
|
|
19
|
+
const pack = genPack(3, { files: 20 });
|
|
20
|
+
expect(await decodePack(await encodePack(pack, { deflate: false }))).toEqual(await decodePack(await encodePack(pack)));
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('is deterministic: equal input, in any row order, gives equal bytes', async () => {
|
|
24
|
+
const pack = genPack(7, { files: 30 });
|
|
25
|
+
const shuffled: Pack = { ...pack, files: [...pack.files!].reverse(), facts: [...(pack.facts ?? [])].reverse(), relations: [...(pack.relations ?? [])].reverse() };
|
|
26
|
+
const a = await encodePack(pack), b = await encodePack(pack), c = await encodePack(shuffled);
|
|
27
|
+
expect(Buffer.from(a).equals(Buffer.from(b))).toBe(true);
|
|
28
|
+
expect(Buffer.from(a).equals(Buffer.from(c))).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('carries an empty pack (frame only)', async () => {
|
|
32
|
+
const empty: Pack = { rootId: 'r', baseGeneration: 0, targetDigest: ZERO_DIGEST };
|
|
33
|
+
expect(await decodePack(await encodePack(empty))).toEqual(empty);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('keeps explicit hashes (a have request) apart from referenced ones', async () => {
|
|
37
|
+
const have: Pack = { rootId: 'r', baseGeneration: 0, targetDigest: ZERO_DIGEST, hashes: ['a'.repeat(64), '~tag:x', 'b'.repeat(64)] };
|
|
38
|
+
const back = await decodePack(await encodePack(have));
|
|
39
|
+
expect(back.hashes).toEqual(['a'.repeat(64), 'b'.repeat(64), '~tag:x']);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('round-trips awkward numbers exactly', async () => {
|
|
43
|
+
const g = genPack(1, { files: 1 });
|
|
44
|
+
g.facts = [0, 1, 0.5, 1e-9, 0.1234567, 2 ** 40, 1e15 + 0.5, 123456789.123456789].map((importance, i) => ({
|
|
45
|
+
key: `k${i}`, content: 'c', source: 's', importance, origin: 'model' as const, fileAnchors: [],
|
|
46
|
+
}));
|
|
47
|
+
const back = await decodePack(await encodePack(g));
|
|
48
|
+
expect(back.facts!.map((f) => f.importance).sort()).toEqual(g.facts.map((f) => f.importance).sort());
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/pack.size.test.ts — the size promise (PRD 41 A11 Accept): a first sync of a 20,000-file
|
|
3
|
+
* fixture is at most 10% of the same rows as JSON, and a re-review of 10 files is under 50 KB.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { encodePack, jsonBytesOf, MAX_PART_BYTES, splitParts } from './index.js';
|
|
7
|
+
import { genPack } from './pack.testkit.test-util.js';
|
|
8
|
+
|
|
9
|
+
describe('BFKP size', () => {
|
|
10
|
+
it('packs 20,000 files into at most 10% of the same rows as JSON, in 1 + ceil(size / 8 MB) requests at most', async () => {
|
|
11
|
+
const pack = genPack(11, { files: 20_000, symbols: 4, edgesPerFile: 3, facts: 4_000, rich: false });
|
|
12
|
+
const json = jsonBytesOf(pack);
|
|
13
|
+
const bytes = await encodePack(pack);
|
|
14
|
+
expect(bytes.length / json).toBeLessThanOrEqual(0.1);
|
|
15
|
+
expect(splitParts(bytes).length).toBeLessThanOrEqual(1 + Math.ceil(bytes.length / MAX_PART_BYTES));
|
|
16
|
+
}, 120_000);
|
|
17
|
+
|
|
18
|
+
it('a re-review that changes 10 files is one small pack', async () => {
|
|
19
|
+
const full = genPack(12, { files: 10, symbols: 4 });
|
|
20
|
+
expect((await encodePack(full)).length).toBeLessThan(50 * 1024);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/** knowledge/pack/pack.testkit.test-util.ts — a seeded generator of knowledge packs (no new dependency). */
|
|
2
|
+
|
|
3
|
+
import type { Extracted, KEdge, KNode, NodeRef, StoredFact, StoredRelation } from '../model.js';
|
|
4
|
+
import type { Pack } from './index.js';
|
|
5
|
+
import { ZERO_DIGEST } from './index.js';
|
|
6
|
+
|
|
7
|
+
/** mulberry32: a small seeded PRNG. */
|
|
8
|
+
export function rng(seed: number): () => number {
|
|
9
|
+
let a = seed >>> 0;
|
|
10
|
+
return () => {
|
|
11
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
12
|
+
let t = a;
|
|
13
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
14
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
15
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const ROOT = 'ws:gen';
|
|
20
|
+
const WORDS = ['alpha', 'béta', '日本語', 'delta', 'emoji😀', 'zeta', 'parse', 'render', 'store', 'sync'];
|
|
21
|
+
const KINDS = ['references', 'links', 'contains', 'mentions', 'custom-kind'];
|
|
22
|
+
|
|
23
|
+
export interface Gen { next(): number; int(n: number): number; pick<T>(xs: readonly T[]): T; chance(p: number): boolean; hex64(): string; word(): string }
|
|
24
|
+
|
|
25
|
+
export function gen(seed: number): Gen {
|
|
26
|
+
const next = rng(seed);
|
|
27
|
+
const int = (n: number): number => Math.floor(next() * n);
|
|
28
|
+
return {
|
|
29
|
+
next, int, pick: (xs) => xs[int(xs.length)]!, chance: (p) => next() < p, word: () => WORDS[int(WORDS.length)]! + int(50),
|
|
30
|
+
hex64: () => Array.from({ length: 64 }, () => '0123456789abcdef'[int(16)]).join(''),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ref = (path: string, frag = ''): NodeRef => `src:${ROOT}::${path}${frag}` as NodeRef;
|
|
35
|
+
|
|
36
|
+
/** One file's extraction: a file node, `symbols` symbol nodes, edges to other files, keys. */
|
|
37
|
+
export function extractionOf(g: Gen, path: string, hash: string, targets: readonly string[], symbols: number, rich: boolean): Extracted {
|
|
38
|
+
const nodes: KNode[] = [{ ref: ref(path), kind: 'file', label: path, pack: 'code', importance: Math.round(g.next() * 1e4) / 1e4, origin: 'extracted', sourceHash: hash, system: '', app: 'apps/web' }];
|
|
39
|
+
for (let s = 0; s < symbols; s++) {
|
|
40
|
+
const n: KNode = { ref: ref(path, `#sym${s}`), kind: 'symbol', label: `${g.word()}`, pack: 'code', importance: Math.round(g.next() * 1e4) / 1e4, origin: 'extracted', sourceHash: hash, badges: { line: 1 + s * 7 } };
|
|
41
|
+
if (rich && g.chance(0.3)) { n.stale = g.chance(0.5); n.partition = 'ui'; n.badges = { line: s, kind: 'fn', n: g.next() }; }
|
|
42
|
+
nodes.push(n);
|
|
43
|
+
}
|
|
44
|
+
const edges: KEdge[] = targets.map((t) => {
|
|
45
|
+
const e: KEdge = { from: ref(path), to: ref(t), kind: g.pick(KINDS), weight: g.chance(0.5) ? 1 : g.next(), origin: 'extracted' };
|
|
46
|
+
if (rich && g.chance(0.3)) { e.confidence = g.next(); e.verdict = g.pick(['allowed', 'same', 'skips', 'upward'] as const); e.cycle = g.chance(0.5); e.stale = g.chance(0.5); e.byName = g.word(); }
|
|
47
|
+
return e;
|
|
48
|
+
});
|
|
49
|
+
return { sourceRef: ref(path), sourceHash: hash, ...(rich && g.chance(0.5) ? { extractorVersion: g.int(9) } : {}), nodes, edges, keys: [{ ref: ref(path), key: path.toLowerCase() }] };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface GenOptions { files: number; symbols?: number; edgesPerFile?: number; facts?: number; rich?: boolean; hashes?: boolean }
|
|
53
|
+
|
|
54
|
+
/** A generated pack: files, a full extraction per file, facts, relations, stale marks, tombstones, scope. */
|
|
55
|
+
export function genPack(seed: number, o: GenOptions): Pack {
|
|
56
|
+
const g = gen(seed);
|
|
57
|
+
const rich = o.rich ?? true;
|
|
58
|
+
const paths = Array.from({ length: o.files }, (_, i) => `src/d${i % 37}/${g.word()}-${i}.ts`);
|
|
59
|
+
const hashes = paths.map(() => g.hex64());
|
|
60
|
+
const pack: Pack = { rootId: ROOT, baseGeneration: g.int(5), targetDigest: g.hex64() };
|
|
61
|
+
if (o.files > 0) {
|
|
62
|
+
pack.files = paths.map((path, i) => ({ path, hash: hashes[i]!, bytes: g.int(90_000) }));
|
|
63
|
+
pack.extracted = paths.map((path, i) => ({
|
|
64
|
+
kind: 'full' as const, hash: hashes[i]!,
|
|
65
|
+
entries: [extractionOf(g, path, hashes[i]!, Array.from({ length: o.edgesPerFile ?? 3 }, () => paths[g.int(paths.length)]!), o.symbols ?? 4, rich)],
|
|
66
|
+
}));
|
|
67
|
+
pack.stale = paths.slice(0, Math.ceil(o.files / 4)).map((p, i) => ({ sourceRef: ref(p), hash: hashes[i]! }));
|
|
68
|
+
pack.deletes = { files: [`gone/${g.word()}.ts`], facts: [`claim:${g.hex64().slice(0, 12)}`], relations: [{ from: ref('a.ts'), to: ref('b.ts'), kind: 'references' }] };
|
|
69
|
+
}
|
|
70
|
+
const facts: StoredFact[] = Array.from({ length: o.facts ?? Math.floor(o.files / 4) }, (_, i) => {
|
|
71
|
+
const f: StoredFact = { key: `claim:${i}-${g.word()}`, content: `The ${g.word()} module ${g.word()} the ${g.word()}.`, source: 'review', importance: Math.round(g.next() * 1e3) / 1e3, origin: g.pick(['model', 'human', 'agent'] as const), fileAnchors: [] };
|
|
72
|
+
if (paths.length > 0) f.fileAnchors = Array.from({ length: g.int(3) }, () => ({ path: paths[g.int(paths.length)]!, ...(g.chance(0.5) ? { line: g.int(500) } : {}), ...(g.chance(0.5) ? { contentHash: g.hex64() } : {}) }));
|
|
73
|
+
if (rich && g.chance(0.3)) { f.updatedAt = '2026-11-07T10:00:00.000Z'; f.createdBy = g.chance(0.5) ? null : 'user-1'; }
|
|
74
|
+
return f;
|
|
75
|
+
});
|
|
76
|
+
if (facts.length > 0) pack.facts = facts;
|
|
77
|
+
if (paths.length > 1) {
|
|
78
|
+
pack.relations = Array.from({ length: Math.floor(o.files / 3) }, (_, i): StoredRelation => {
|
|
79
|
+
const r: StoredRelation = { from: ref(paths[g.int(paths.length)]!), to: `fact:claim:${i}` as NodeRef, kind: g.pick(KINDS), weight: g.chance(0.5) ? 1 : g.next(), origin: g.pick(['model', 'human', 'agent'] as const) };
|
|
80
|
+
if (rich && g.chance(0.4)) { r.confidence = g.next(); r.sourceHash = hashes[g.int(hashes.length)]!; r.staleAt = g.chance(0.5) ? null : '2026-11-07T00:00:00.000Z'; }
|
|
81
|
+
return r;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (rich && g.chance(0.5)) pack.scope = { rootId: ROOT, title: 'Gen', packs: ['code', 'docs'], lastDigest: g.hex64().slice(0, 32), lastReviewedAt: '2026-11-07T00:00:00.000Z' };
|
|
85
|
+
if (rich && g.chance(0.5)) pack.analysis = { version: 1, nodes: ['a'], edges: [] };
|
|
86
|
+
if (o.hashes) pack.hashes = [g.hex64(), '~tag:odd-id', g.hex64()];
|
|
87
|
+
return pack;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export { ZERO_DIGEST };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/sections/columns.ts — helpers the BFKP sections share: enums that turn the model's
|
|
3
|
+
* small closed vocabularies into one byte, zigzag deltas for index runs, and the section
|
|
4
|
+
* descriptor type (PRD 41 A11).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Pack, PackLimits } from '../types.js';
|
|
8
|
+
import type { ReadTables, Tables } from '../strings.js';
|
|
9
|
+
import { PackFormatError, type ByteReader, type ByteWriter } from '../varint.js';
|
|
10
|
+
|
|
11
|
+
/** One section: its tag, how it is written from a pack (undefined = nothing to write) and read back into one. */
|
|
12
|
+
export interface SectionCodec {
|
|
13
|
+
tag: number;
|
|
14
|
+
write(pack: Pack, t: Tables): Uint8Array | undefined;
|
|
15
|
+
read(bytes: Uint8Array, t: ReadTables, limits: PackLimits, into: Pack): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const ORIGINS = ['extracted', 'model', 'human', 'agent'] as const;
|
|
19
|
+
export const STORED_ORIGINS = ['model', 'human', 'agent'] as const;
|
|
20
|
+
export const VERDICTS = ['allowed', 'same', 'skips', 'upward'] as const;
|
|
21
|
+
/** The relation/edge kinds the model names; anything else is stored as 0 + a string. */
|
|
22
|
+
export const EDGE_KINDS = [
|
|
23
|
+
'references', 'links', 'contains', 'mentions', 'duplicate', 'near-duplicate', 'contradicts', 'agrees',
|
|
24
|
+
'supersedes', 'about', 'co-changed',
|
|
25
|
+
] as const;
|
|
26
|
+
/** The node kinds the packs name. */
|
|
27
|
+
export const NODE_KINDS = ['file', 'section', 'symbol', 'area', 'entity', 'claim', 'fact'] as const;
|
|
28
|
+
|
|
29
|
+
export const zigzag = (n: number): number => (n >= 0 ? n * 2 : -n * 2 - 1);
|
|
30
|
+
export const unzigzag = (z: number): number => (z % 2 === 0 ? z / 2 : -(z + 1) / 2);
|
|
31
|
+
|
|
32
|
+
/** A closed vocabulary: the index + 1 of a known word, else 0 followed by the word's string index. */
|
|
33
|
+
export function writeWord(w: ByteWriter, t: Tables, vocab: readonly string[], word: string): void {
|
|
34
|
+
const at = vocab.indexOf(word);
|
|
35
|
+
if (at >= 0) w.varint(at + 1);
|
|
36
|
+
else { w.varint(0); w.varint(t.str(word)); }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function readWord(r: ByteReader, t: ReadTables, vocab: readonly string[]): string {
|
|
40
|
+
const code = r.varint();
|
|
41
|
+
if (code === 0) return t.str(r.varint());
|
|
42
|
+
const word = vocab[code - 1];
|
|
43
|
+
if (word === undefined) throw new PackFormatError('unknown vocabulary code');
|
|
44
|
+
return word;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A closed enum that must be one of `vocab`. */
|
|
48
|
+
export function writeEnum(w: ByteWriter, vocab: readonly string[], value: string): void {
|
|
49
|
+
const at = vocab.indexOf(value);
|
|
50
|
+
if (at < 0) throw new PackFormatError(`value '${value}' is not one of ${vocab.join('|')}`);
|
|
51
|
+
w.u8(at);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function readEnum<T extends string>(r: ByteReader, vocab: readonly T[]): T {
|
|
55
|
+
const v = vocab[r.u8()];
|
|
56
|
+
if (v === undefined) throw new PackFormatError('unknown enum code');
|
|
57
|
+
return v;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Strings sort the way the string table does, so a sorted run of rows has non-decreasing indexes. */
|
|
61
|
+
export const compare = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/pack/sections/extr.ts — the EXTR section of BFKP v1 (PRD 41 A11): the Extracted output per
|
|
3
|
+
* content hash. A hash is either whole (`full`: nodes, edges and keys columnar) or a `diff` against the
|
|
4
|
+
* prior hash's extraction of the same source (`prior hash#` plus the added and removed items).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Extracted, ExtrEntryDiff, ListDiff, PackExtr } from '../types.js';
|
|
8
|
+
import type { ReadTables, Tables } from '../strings.js';
|
|
9
|
+
import { boundedCount, ByteReader, ByteWriter } from '../varint.js';
|
|
10
|
+
import type { SectionCodec } from './columns.js';
|
|
11
|
+
import { readEdges, readKeys, readNodes, writeEdges, writeKeys, writeNodes } from './extrLists.js';
|
|
12
|
+
import { TAG } from './rows.js';
|
|
13
|
+
|
|
14
|
+
const HAS_VERSION = 1;
|
|
15
|
+
|
|
16
|
+
function writeDiff<T>(w: ByteWriter, t: Tables, d: ListDiff<T>, items: (w: ByteWriter, t: Tables, xs: T[]) => void): void {
|
|
17
|
+
w.varint(d.remove.length);
|
|
18
|
+
for (const id of d.remove) w.varint(t.str(id));
|
|
19
|
+
let prev = 0;
|
|
20
|
+
w.varint(d.add.length);
|
|
21
|
+
for (const a of d.add) { w.varint(a.at - prev); prev = a.at; }
|
|
22
|
+
items(w, t, d.add.map((a) => a.item));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function readDiff<T>(r: ByteReader, t: ReadTables, max: number, items: (r: ByteReader, t: ReadTables, max: number) => T[]): ListDiff<T> {
|
|
26
|
+
const remove = Array.from({ length: boundedCount(r, max, 'diff removals') }, () => t.str(r.varint()));
|
|
27
|
+
const n = boundedCount(r, max, 'diff additions');
|
|
28
|
+
const ats: number[] = [];
|
|
29
|
+
let prev = 0;
|
|
30
|
+
for (let i = 0; i < n; i++) { prev += r.varint(); ats.push(prev); }
|
|
31
|
+
const list = items(r, t, max);
|
|
32
|
+
return { remove, add: ats.map((at, i) => ({ at, item: list[i]! })) };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const extrSection: SectionCodec = {
|
|
36
|
+
tag: TAG.EXTR,
|
|
37
|
+
write(pack, t) {
|
|
38
|
+
const rows = pack.extracted;
|
|
39
|
+
if (!rows?.length) return undefined;
|
|
40
|
+
const w = new ByteWriter();
|
|
41
|
+
w.varint(rows.length);
|
|
42
|
+
for (const x of rows) {
|
|
43
|
+
w.u8(x.kind === 'full' ? 0 : 1);
|
|
44
|
+
w.varint(t.hash(x.hash));
|
|
45
|
+
if (x.kind === 'diff') w.varint(t.hash(x.prior));
|
|
46
|
+
w.varint(x.entries.length);
|
|
47
|
+
for (const e of x.entries) {
|
|
48
|
+
w.varint(t.str(e.sourceRef));
|
|
49
|
+
w.varint(t.hash(e.sourceHash));
|
|
50
|
+
w.u8(e.extractorVersion !== undefined ? HAS_VERSION : 0);
|
|
51
|
+
if (e.extractorVersion !== undefined) w.varint(e.extractorVersion);
|
|
52
|
+
if (x.kind === 'full') {
|
|
53
|
+
const f = e as Extracted;
|
|
54
|
+
writeNodes(w, t, f.nodes); writeEdges(w, t, f.edges); writeKeys(w, t, f.keys);
|
|
55
|
+
} else {
|
|
56
|
+
const d = e as ExtrEntryDiff;
|
|
57
|
+
writeDiff(w, t, d.nodes, writeNodes); writeDiff(w, t, d.edges, writeEdges); writeDiff(w, t, d.keys, writeKeys);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return w.finish();
|
|
62
|
+
},
|
|
63
|
+
read(bytes, t, limits, into) {
|
|
64
|
+
const r = new ByteReader(bytes);
|
|
65
|
+
const n = boundedCount(r, limits.maxRows, 'extracted hashes');
|
|
66
|
+
const out: PackExtr[] = [];
|
|
67
|
+
for (let i = 0; i < n; i++) {
|
|
68
|
+
const diff = r.u8() === 1;
|
|
69
|
+
const hash = t.hash(r.varint());
|
|
70
|
+
const prior = diff ? t.hash(r.varint()) : '';
|
|
71
|
+
const count = boundedCount(r, limits.maxRows, 'extracted entries');
|
|
72
|
+
const entries: Array<Extracted | ExtrEntryDiff> = [];
|
|
73
|
+
for (let k = 0; k < count; k++) {
|
|
74
|
+
const sourceRef = t.str(r.varint());
|
|
75
|
+
const sourceHash = t.hash(r.varint());
|
|
76
|
+
const version = r.u8() & HAS_VERSION ? { extractorVersion: r.varint() } : {};
|
|
77
|
+
if (!diff) {
|
|
78
|
+
entries.push({ sourceRef: sourceRef as Extracted['sourceRef'], sourceHash, ...version, nodes: readNodes(r, t, limits.maxRows), edges: readEdges(r, t, limits.maxRows), keys: readKeys(r, t, limits.maxRows) });
|
|
79
|
+
} else {
|
|
80
|
+
entries.push({
|
|
81
|
+
sourceRef, sourceHash, ...version,
|
|
82
|
+
nodes: readDiff(r, t, limits.maxRows, readNodes), edges: readDiff(r, t, limits.maxRows, readEdges), keys: readDiff(r, t, limits.maxRows, readKeys),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
out.push(diff ? { kind: 'diff', hash, prior, entries: entries as ExtrEntryDiff[] } : { kind: 'full', hash, entries: entries as Extracted[] });
|
|
87
|
+
}
|
|
88
|
+
into.extracted = out;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
@@ -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';
|