@seanhogg/builderforce-memory 2026.10.63 → 2026.10.67
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/index.d.ts +1 -1
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +1 -1
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/pack/frame.d.ts +12 -3
- package/dist/knowledge/pack/frame.d.ts.map +1 -1
- package/dist/knowledge/pack/frame.js +18 -4
- package/dist/knowledge/pack/frame.js.map +1 -1
- package/dist/knowledge/pack/manifest.d.ts +11 -3
- package/dist/knowledge/pack/manifest.d.ts.map +1 -1
- package/dist/knowledge/pack/manifest.js +26 -12
- package/dist/knowledge/pack/manifest.js.map +1 -1
- package/dist/knowledge/pack/sections/extr.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/extr.js +16 -4
- package/dist/knowledge/pack/sections/extr.js.map +1 -1
- package/dist/knowledge/pack/sections/extrLists.d.ts +0 -1
- package/dist/knowledge/pack/sections/extrLists.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/extrLists.js +5 -6
- package/dist/knowledge/pack/sections/extrLists.js.map +1 -1
- package/dist/knowledge/pack/sections/facts.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/facts.js +3 -2
- package/dist/knowledge/pack/sections/facts.js.map +1 -1
- package/dist/knowledge/pack/sections/rows.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/rows.js +11 -2
- package/dist/knowledge/pack/sections/rows.js.map +1 -1
- package/dist/knowledge/pack/strings.d.ts.map +1 -1
- package/dist/knowledge/pack/strings.js +6 -1
- package/dist/knowledge/pack/strings.js.map +1 -1
- package/dist/knowledge/pack/varint.d.ts +14 -0
- package/dist/knowledge/pack/varint.d.ts.map +1 -1
- package/dist/knowledge/pack/varint.js +32 -1
- package/dist/knowledge/pack/varint.js.map +1 -1
- package/dist/knowledge/refs.d.ts +14 -2
- package/dist/knowledge/refs.d.ts.map +1 -1
- package/dist/knowledge/refs.js +30 -8
- package/dist/knowledge/refs.js.map +1 -1
- package/dist/knowledge/review.d.ts +6 -6
- package/dist/knowledge/review.d.ts.map +1 -1
- package/dist/knowledge/review.js +56 -329
- package/dist/knowledge/review.js.map +1 -1
- package/dist/knowledge/reviewAnalyse.d.ts +17 -0
- package/dist/knowledge/reviewAnalyse.d.ts.map +1 -0
- package/dist/knowledge/reviewAnalyse.js +61 -0
- package/dist/knowledge/reviewAnalyse.js.map +1 -0
- package/dist/knowledge/reviewApply.d.ts +33 -0
- package/dist/knowledge/reviewApply.d.ts.map +1 -0
- package/dist/knowledge/reviewApply.js +121 -0
- package/dist/knowledge/reviewApply.js.map +1 -0
- package/dist/knowledge/reviewContext.d.ts +77 -0
- package/dist/knowledge/reviewContext.d.ts.map +1 -0
- package/dist/knowledge/reviewContext.js +111 -0
- package/dist/knowledge/reviewContext.js.map +1 -0
- package/dist/knowledge/reviewFinish.d.ts +9 -0
- package/dist/knowledge/reviewFinish.d.ts.map +1 -0
- package/dist/knowledge/reviewFinish.js +62 -0
- package/dist/knowledge/reviewFinish.js.map +1 -0
- package/dist/knowledge/reviewRunModel.d.ts +3 -0
- package/dist/knowledge/reviewRunModel.d.ts.map +1 -1
- package/dist/knowledge/reviewRunModel.js.map +1 -1
- package/dist/knowledge/stores/extractLogWriter.d.ts +16 -3
- package/dist/knowledge/stores/extractLogWriter.d.ts.map +1 -1
- package/dist/knowledge/stores/extractLogWriter.js +38 -13
- package/dist/knowledge/stores/extractLogWriter.js.map +1 -1
- package/dist/knowledge/stores/extractRefs.d.ts +68 -0
- package/dist/knowledge/stores/extractRefs.d.ts.map +1 -0
- package/dist/knowledge/stores/extractRefs.js +168 -0
- package/dist/knowledge/stores/extractRefs.js.map +1 -0
- package/dist/knowledge/stores/localSnapshotStore.d.ts +5 -0
- package/dist/knowledge/stores/localSnapshotStore.d.ts.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.js +25 -5
- package/dist/knowledge/stores/localSnapshotStore.js.map +1 -1
- package/dist/knowledge/stores/rowDelta.d.ts +6 -0
- package/dist/knowledge/stores/rowDelta.d.ts.map +1 -1
- package/dist/knowledge/stores/rowDelta.js.map +1 -1
- package/dist/knowledge/stores/rowMemory.d.ts +30 -0
- package/dist/knowledge/stores/rowMemory.d.ts.map +1 -1
- package/dist/knowledge/stores/rowMemory.js +46 -1
- package/dist/knowledge/stores/rowMemory.js.map +1 -1
- package/dist/knowledge/stores/rowSync.d.ts.map +1 -1
- package/dist/knowledge/stores/rowSync.js +2 -1
- package/dist/knowledge/stores/rowSync.js.map +1 -1
- package/dist/knowledge/stores/snapshotFormat.d.ts +13 -2
- package/dist/knowledge/stores/snapshotFormat.d.ts.map +1 -1
- package/dist/knowledge/stores/snapshotFormat.js +22 -9
- package/dist/knowledge/stores/snapshotFormat.js.map +1 -1
- package/dist/working/fold.d.ts +14 -0
- package/dist/working/fold.d.ts.map +1 -1
- package/dist/working/fold.js +14 -4
- package/dist/working/fold.js.map +1 -1
- package/dist/working/index.d.ts +3 -1
- package/dist/working/index.d.ts.map +1 -1
- package/dist/working/index.js +2 -1
- package/dist/working/index.js.map +1 -1
- package/dist/working/textSummary.d.ts +46 -0
- package/dist/working/textSummary.d.ts.map +1 -0
- package/dist/working/textSummary.js +142 -0
- package/dist/working/textSummary.js.map +1 -0
- package/package.json +1 -1
- package/src/contract/exports.snapshot.json +8 -1
- package/src/contract/regression.extractEviction.test.ts +162 -0
- package/src/contract/regression.fixCommits.test.ts +112 -0
- package/src/contract/regression.packWire.test.ts +174 -0
- package/src/contract/regression.refs.test.ts +49 -0
- package/src/contract/types.contract.test.ts +2 -1
- package/src/knowledge/index.ts +1 -1
- package/src/knowledge/pack/frame.ts +21 -5
- package/src/knowledge/pack/manifest.ts +26 -11
- package/src/knowledge/pack/sections/extr.ts +14 -4
- package/src/knowledge/pack/sections/extrLists.ts +4 -6
- package/src/knowledge/pack/sections/facts.ts +3 -2
- package/src/knowledge/pack/sections/rows.ts +11 -2
- package/src/knowledge/pack/strings.ts +5 -1
- package/src/knowledge/pack/varint.ts +33 -1
- package/src/knowledge/refs.ts +26 -8
- package/src/knowledge/review.ts +58 -311
- package/src/knowledge/reviewAnalyse.ts +59 -0
- package/src/knowledge/reviewApply.ts +128 -0
- package/src/knowledge/reviewContext.ts +158 -0
- package/src/knowledge/reviewFinish.ts +63 -0
- package/src/knowledge/reviewRunModel.ts +3 -0
- package/src/knowledge/stores/extractLogWriter.ts +36 -12
- package/src/knowledge/stores/extractRefs.ts +161 -0
- package/src/knowledge/stores/localSnapshotStore.ts +22 -5
- package/src/knowledge/stores/rowDelta.ts +6 -0
- package/src/knowledge/stores/rowMemory.ts +50 -1
- package/src/knowledge/stores/rowSync.ts +2 -1
- package/src/knowledge/stores/snapshotFormat.ts +27 -10
- package/src/working/fold.ts +15 -4
- package/src/working/index.ts +13 -1
- package/src/working/textSummary.test.ts +113 -0
- package/src/working/textSummary.ts +156 -0
|
@@ -13,13 +13,19 @@
|
|
|
13
13
|
|
|
14
14
|
import { sha256Hex } from '../../retrieval/knowledge-index/sha256.js';
|
|
15
15
|
import type { ManifestLeaf, StoredFact, StoredRelation } from './types.js';
|
|
16
|
+
import { wireString } from './varint.js';
|
|
16
17
|
|
|
17
18
|
export const MANIFEST_BUCKETS = 4096;
|
|
18
19
|
|
|
19
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* FNV-1a (32-bit) over the UTF-16 code units of the key's WIRE form ({@link wireString}): stable across runtimes and
|
|
22
|
+
* releases. A key holding a lone surrogate (a legal Windows path) reaches the other side as U+FFFD, so it is hashed as
|
|
23
|
+
* U+FFFD here too, or the two sides would bucket it differently and never agree. A well-formed key hashes as before.
|
|
24
|
+
*/
|
|
20
25
|
export function keyHash(key: string): number {
|
|
26
|
+
const k = wireString(key);
|
|
21
27
|
let h = 0x811c9dc5;
|
|
22
|
-
for (let i = 0; i <
|
|
28
|
+
for (let i = 0; i < k.length; i++) { h ^= k.charCodeAt(i); h = Math.imul(h, 0x01000193) >>> 0; }
|
|
23
29
|
return h >>> 0;
|
|
24
30
|
}
|
|
25
31
|
|
|
@@ -36,6 +42,8 @@ export interface ManifestTree {
|
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
const cmp = (a: ManifestLeaf, b: ManifestLeaf): number => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
45
|
+
/** A leaf as the other side decodes it (see {@link wireString}): equal leaves sort, bucket and digest the same on both. */
|
|
46
|
+
const wireLeaf = (l: ManifestLeaf): ManifestLeaf => { const key = wireString(l.key); return key === l.key ? l : { ...l, key }; };
|
|
39
47
|
|
|
40
48
|
/**
|
|
41
49
|
* The digest of ONE bucket: the hash of its leaves sorted by key (an empty bucket has a fixed digest). The server keeps
|
|
@@ -43,7 +51,7 @@ const cmp = (a: ManifestLeaf, b: ManifestLeaf): number => (a.key < b.key ? -1 :
|
|
|
43
51
|
*/
|
|
44
52
|
export function bucketDigest(leaves: readonly ManifestLeaf[]): string {
|
|
45
53
|
if (leaves.length === 0) return EMPTY_BUCKET;
|
|
46
|
-
return sha256Hex(
|
|
54
|
+
return sha256Hex(leaves.map(wireLeaf).sort(cmp).map((l) => `${l.key}\u0000${l.digest}\n`).join(''));
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
/** The root digest from the `MANIFEST_BUCKETS` bucket digests (hex), in bucket order. */
|
|
@@ -54,7 +62,8 @@ export function rootDigestOfBuckets(digests: readonly string[]): string {
|
|
|
54
62
|
|
|
55
63
|
export function manifestTree(leaves: readonly ManifestLeaf[]): ManifestTree {
|
|
56
64
|
const grouped = new Map<number, ManifestLeaf[]>();
|
|
57
|
-
for (const
|
|
65
|
+
for (const l0 of leaves) {
|
|
66
|
+
const l = wireLeaf(l0);
|
|
58
67
|
const b = bucketOf(l.key);
|
|
59
68
|
const list = grouped.get(b);
|
|
60
69
|
if (list) list.push(l); else grouped.set(b, [l]);
|
|
@@ -75,19 +84,25 @@ export function differingBuckets(a: readonly string[], b: readonly string[]): nu
|
|
|
75
84
|
return out;
|
|
76
85
|
}
|
|
77
86
|
|
|
78
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* A source's leaf key: its path, or `~tag|path` for a root-level output (the blob store's own source identity). Every leaf
|
|
89
|
+
* key and digest input is taken in its wire form ({@link wireString}), so a lone surrogate means the same on both sides.
|
|
90
|
+
*/
|
|
79
91
|
export function fileLeafKey(path: string, hash: string): string {
|
|
80
|
-
|
|
92
|
+
const p = wireString(path);
|
|
93
|
+
if (!hash.startsWith('~')) return `f:${p}`;
|
|
81
94
|
const colon = hash.indexOf(':');
|
|
82
|
-
return `f:${colon > 0 ? hash.slice(0, colon) : hash}|${
|
|
95
|
+
return `f:${colon > 0 ? hash.slice(0, colon) : hash}|${p}`;
|
|
83
96
|
}
|
|
84
97
|
|
|
85
|
-
export const factLeafKey = (key: string): string => `a:${key}`;
|
|
98
|
+
export const factLeafKey = (key: string): string => `a:${wireString(key)}`;
|
|
86
99
|
/**
|
|
87
100
|
* A relation's leaf key: `r:` + the JSON array `[from, to, kind]`. Printable and unambiguous: Postgres `text` and
|
|
88
|
-
* `jsonb` reject NUL, which the earlier `from␀to␀kind` form contained.
|
|
101
|
+
* `jsonb` reject NUL, which the earlier `from␀to␀kind` form contained. (JSON.stringify would escape a lone surrogate as
|
|
102
|
+
* `\udXXX` text, which the decoded U+FFFD on the other side never equals: the parts are taken in wire form first.)
|
|
89
103
|
*/
|
|
90
|
-
export const relationLeafKey = (r: { from: string; to: string; kind: string }): string =>
|
|
104
|
+
export const relationLeafKey = (r: { from: string; to: string; kind: string }): string =>
|
|
105
|
+
`r:${JSON.stringify([wireString(r.from), wireString(r.to), wireString(r.kind)])}`;
|
|
91
106
|
|
|
92
107
|
/** The inverse of {@link relationLeafKey}; null when `key` is not a relation leaf key. The ONE parser. */
|
|
93
108
|
export function parseRelationLeafKey(key: string): { from: string; to: string; kind: string } | null {
|
|
@@ -102,7 +117,7 @@ export function parseRelationLeafKey(key: string): { from: string; to: string; k
|
|
|
102
117
|
}
|
|
103
118
|
|
|
104
119
|
/** The digest of what a fact says: its key, text and provenance kind. Importance and timestamps are the server's to score. */
|
|
105
|
-
export const factDigest = (f: Pick<StoredFact, 'key' | 'content' | 'origin'>): string => sha256Hex(JSON.stringify([f.key, f.content, f.origin]));
|
|
120
|
+
export const factDigest = (f: Pick<StoredFact, 'key' | 'content' | 'origin'>): string => sha256Hex(JSON.stringify([wireString(f.key), wireString(f.content), f.origin]));
|
|
106
121
|
|
|
107
122
|
export const relationDigest = (r: Pick<StoredRelation, 'weight' | 'origin' | 'confidence' | 'sourceHash'>): string =>
|
|
108
123
|
sha256Hex(`${r.weight.toFixed(3)}|${r.origin}|${r.confidence === undefined ? '' : r.confidence.toFixed(3)}|${r.sourceHash ?? ''}`);
|
|
@@ -6,12 +6,14 @@
|
|
|
6
6
|
|
|
7
7
|
import type { Extracted, ExtrEntryDiff, ListDiff, PackExtr } from '../types.js';
|
|
8
8
|
import type { ReadTables, Tables } from '../strings.js';
|
|
9
|
-
import { boundedCount, ByteReader, ByteWriter, type Budget } from '../varint.js';
|
|
9
|
+
import { assertConsumed, assertFlagsKnown, boundedCount, ByteReader, ByteWriter, PackFormatError, type Budget } from '../varint.js';
|
|
10
10
|
import type { SectionCodec } from './columns.js';
|
|
11
11
|
import { readEdges, readKeys, readNodes, writeEdges, writeKeys, writeNodes } from './extrLists.js';
|
|
12
12
|
import { TAG } from './rows.js';
|
|
13
13
|
|
|
14
14
|
const HAS_VERSION = 1;
|
|
15
|
+
/** The kind byte of a hash: whole, or a diff against a prior hash. Any other value is from a writer this reader does not know. */
|
|
16
|
+
const KIND_FULL = 0, KIND_DIFF = 1;
|
|
15
17
|
|
|
16
18
|
function writeDiff<T>(w: ByteWriter, t: Tables, d: ListDiff<T>, items: (w: ByteWriter, t: Tables, xs: T[]) => void): void {
|
|
17
19
|
w.varint(d.remove.length);
|
|
@@ -29,6 +31,9 @@ function readDiff<T>(r: ByteReader, t: ReadTables, budget: Budget, items: (r: By
|
|
|
29
31
|
let prev = 0;
|
|
30
32
|
for (let i = 0; i < n; i++) { prev += r.varint(); ats.push(prev); }
|
|
31
33
|
const list = items(r, t, budget);
|
|
34
|
+
// The positions and the items are counted separately on the wire: a mismatch would store `undefined` (fewer items) or
|
|
35
|
+
// silently drop items (more), so it is a malformed pack.
|
|
36
|
+
if (list.length !== n) throw new PackFormatError(`diff: ${n} add positions but ${list.length} added items`);
|
|
32
37
|
return { remove, add: ats.map((at, i) => ({ at, item: list[i]! })) };
|
|
33
38
|
}
|
|
34
39
|
|
|
@@ -40,7 +45,7 @@ export const extrSection: SectionCodec = {
|
|
|
40
45
|
const w = new ByteWriter();
|
|
41
46
|
w.varint(rows.length);
|
|
42
47
|
for (const x of rows) {
|
|
43
|
-
w.u8(x.kind === 'full' ?
|
|
48
|
+
w.u8(x.kind === 'full' ? KIND_FULL : KIND_DIFF);
|
|
44
49
|
w.varint(t.hash(x.hash));
|
|
45
50
|
if (x.kind === 'diff') w.varint(t.hash(x.prior));
|
|
46
51
|
w.varint(x.entries.length);
|
|
@@ -65,7 +70,9 @@ export const extrSection: SectionCodec = {
|
|
|
65
70
|
const n = boundedCount(r, limits.budget, 'extracted hashes');
|
|
66
71
|
const out: PackExtr[] = [];
|
|
67
72
|
for (let i = 0; i < n; i++) {
|
|
68
|
-
const
|
|
73
|
+
const kind = r.u8();
|
|
74
|
+
if (kind !== KIND_FULL && kind !== KIND_DIFF) throw new PackFormatError(`unknown EXTR entry kind ${kind}`);
|
|
75
|
+
const diff = kind === KIND_DIFF;
|
|
69
76
|
const hash = t.hash(r.varint());
|
|
70
77
|
const prior = diff ? t.hash(r.varint()) : '';
|
|
71
78
|
const count = boundedCount(r, limits.budget, 'extracted entries');
|
|
@@ -73,7 +80,9 @@ export const extrSection: SectionCodec = {
|
|
|
73
80
|
for (let k = 0; k < count; k++) {
|
|
74
81
|
const sourceRef = t.str(r.varint());
|
|
75
82
|
const sourceHash = t.hash(r.varint());
|
|
76
|
-
const
|
|
83
|
+
const vflags = r.u8();
|
|
84
|
+
assertFlagsKnown(vflags, HAS_VERSION);
|
|
85
|
+
const version = vflags & HAS_VERSION ? { extractorVersion: r.varint() } : {};
|
|
77
86
|
if (!diff) {
|
|
78
87
|
entries.push({ sourceRef: sourceRef as Extracted['sourceRef'], sourceHash, ...version, nodes: readNodes(r, t, limits.budget), edges: readEdges(r, t, limits.budget), keys: readKeys(r, t, limits.budget) });
|
|
79
88
|
} else {
|
|
@@ -85,6 +94,7 @@ export const extrSection: SectionCodec = {
|
|
|
85
94
|
}
|
|
86
95
|
out.push(diff ? { kind: 'diff', hash, prior, entries: entries as ExtrEntryDiff[] } : { kind: 'full', hash, entries: entries as Extracted[] });
|
|
87
96
|
}
|
|
97
|
+
assertConsumed(r, 'EXTR section');
|
|
88
98
|
into.extracted = out;
|
|
89
99
|
},
|
|
90
100
|
};
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import type { KEdge, KNode, NodeRef } from '../types.js';
|
|
8
8
|
import type { ReadTables, Tables } from '../strings.js';
|
|
9
|
-
import { boundedCount, ByteReader, ByteWriter, PackFormatError, type Budget } from '../varint.js';
|
|
9
|
+
import { assertFlagsKnown, boundedCount, ByteReader, ByteWriter, PackFormatError, type Budget } from '../varint.js';
|
|
10
10
|
import { EDGE_KINDS, NODE_KINDS, ORIGINS, VERDICTS, readEnum, readWord, unzigzag, writeEnum, writeWord, zigzag } from './columns.js';
|
|
11
11
|
|
|
12
12
|
const N_PARTITION = 1, N_AREA = 2, N_STALE = 4, N_STALE_V = 8, N_BADGES = 16, N_SYSTEM = 32, N_APP = 64, N_COPY = 128, N_HASH = 256;
|
|
@@ -74,7 +74,9 @@ export function readNodes(r: ByteReader, t: ReadTables, budget: Budget): KNode[]
|
|
|
74
74
|
const count = boundedCount(r, budget, 'badges');
|
|
75
75
|
for (let b = 0; b < count; b++) {
|
|
76
76
|
const key = t.str(r.varint());
|
|
77
|
-
|
|
77
|
+
const type = r.u8();
|
|
78
|
+
if (type > 1) throw new PackFormatError('unknown badge value type');
|
|
79
|
+
badges[key] = type === 0 ? r.num() : t.str(r.varint());
|
|
78
80
|
}
|
|
79
81
|
node.badges = badges;
|
|
80
82
|
}
|
|
@@ -145,7 +147,3 @@ export function readKeys(r: ByteReader, t: ReadTables, budget: Budget): Array<{
|
|
|
145
147
|
for (let i = 0; i < n; i++) { prev += unzigzag(r.varint()); refs.push(t.str(prev)); }
|
|
146
148
|
return refs.map((ref) => ({ ref: ref as NodeRef, key: t.str(r.varint()) }));
|
|
147
149
|
}
|
|
148
|
-
|
|
149
|
-
export function assertFlagsKnown(flags: number, mask: number): void {
|
|
150
|
-
if (flags & ~mask) throw new PackFormatError('unknown flag bits');
|
|
151
|
-
}
|
|
@@ -4,9 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { NodeRef, StoredFact, StoredRelation } from '../types.js';
|
|
7
|
-
import { boundedCount, ByteReader, ByteWriter } from '../varint.js';
|
|
7
|
+
import { assertConsumed, assertFlagsKnown, boundedCount, ByteReader, ByteWriter } from '../varint.js';
|
|
8
8
|
import { EDGE_KINDS, STORED_ORIGINS, readEnum, readWord, writeEnum, writeWord, type SectionCodec } from './columns.js';
|
|
9
|
-
import { assertFlagsKnown } from './extrLists.js';
|
|
10
9
|
import { TAG } from './rows.js';
|
|
11
10
|
|
|
12
11
|
const F_UPDATED = 1, F_BY_NULL = 2, F_BY = 4;
|
|
@@ -72,6 +71,7 @@ export const factSection: SectionCodec = {
|
|
|
72
71
|
f.fileAnchors.push(a);
|
|
73
72
|
}
|
|
74
73
|
});
|
|
74
|
+
assertConsumed(r, 'facts section');
|
|
75
75
|
into.facts = facts;
|
|
76
76
|
},
|
|
77
77
|
};
|
|
@@ -122,5 +122,6 @@ export const relationSection: SectionCodec = {
|
|
|
122
122
|
if (f & R_STALE_NULL) x.staleAt = null;
|
|
123
123
|
return x;
|
|
124
124
|
});
|
|
125
|
+
assertConsumed(r, 'relations section');
|
|
125
126
|
},
|
|
126
127
|
};
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Rows arrive sorted (see `normalizePack`), so index runs are delta-encoded.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { boundedCount, ByteReader, ByteWriter, PackFormatError } from '../varint.js';
|
|
8
|
+
import { assertConsumed, assertFlagsKnown, boundedCount, ByteReader, ByteWriter, PackFormatError } from '../varint.js';
|
|
9
9
|
import { fromHex64, toHex } from '../hex.js';
|
|
10
10
|
import type { Pack } from '../types.js';
|
|
11
11
|
import type { SectionCodec } from './columns.js';
|
|
@@ -34,6 +34,7 @@ export const fileSection: SectionCodec = {
|
|
|
34
34
|
for (let i = 0; i < n; i++) { prev += r.varint(); paths.push(t.str(prev)); }
|
|
35
35
|
const hashes = paths.map(() => t.hash(r.varint()));
|
|
36
36
|
into.files = paths.map((path, i) => ({ path, hash: hashes[i]!, bytes: r.varint() }));
|
|
37
|
+
assertConsumed(r, 'FILE section');
|
|
37
38
|
},
|
|
38
39
|
};
|
|
39
40
|
|
|
@@ -56,6 +57,7 @@ export const staleSection: SectionCodec = {
|
|
|
56
57
|
let prev = 0;
|
|
57
58
|
for (let i = 0; i < n; i++) { prev += r.varint(); refs.push(t.str(prev)); }
|
|
58
59
|
into.stale = refs.map((sourceRef) => ({ sourceRef, hash: t.hash(r.varint()) }));
|
|
60
|
+
assertConsumed(r, 'STALE section');
|
|
59
61
|
},
|
|
60
62
|
};
|
|
61
63
|
|
|
@@ -90,6 +92,7 @@ export const deleteSection: SectionCodec = {
|
|
|
90
92
|
const from = Array.from({ length: n }, () => t.str(r.varint()));
|
|
91
93
|
const to = Array.from({ length: n }, () => t.str(r.varint()));
|
|
92
94
|
const kind = Array.from({ length: n }, () => t.str(r.varint()));
|
|
95
|
+
assertConsumed(r, 'DEL section');
|
|
93
96
|
into.deletes = { files: lists[0]!, facts: lists[1]!, relations: from.map((f, i) => ({ from: f, to: to[i]!, kind: kind[i]! })) };
|
|
94
97
|
},
|
|
95
98
|
};
|
|
@@ -115,8 +118,10 @@ export const scopeSection: SectionCodec = {
|
|
|
115
118
|
const n = boundedCount(r, limits.budget, 'packs');
|
|
116
119
|
for (let i = 0; i < n; i++) scope.packs.push(t.str(r.varint()));
|
|
117
120
|
const flags = r.u8();
|
|
121
|
+
assertFlagsKnown(flags, 3);
|
|
118
122
|
if (flags & 1) scope.lastDigest = t.str(r.varint());
|
|
119
123
|
if (flags & 2) scope.lastReviewedAt = t.str(r.varint());
|
|
124
|
+
assertConsumed(r, 'SCOPE section');
|
|
120
125
|
into.scope = scope;
|
|
121
126
|
},
|
|
122
127
|
};
|
|
@@ -138,7 +143,10 @@ export const analysisSection: SectionCodec = {
|
|
|
138
143
|
return w.finish();
|
|
139
144
|
},
|
|
140
145
|
read(bytes, t, _limits, into) {
|
|
141
|
-
|
|
146
|
+
const r = new ByteReader(bytes);
|
|
147
|
+
const text = t.str(r.varint());
|
|
148
|
+
assertConsumed(r, 'ANALYSIS section');
|
|
149
|
+
into.analysis = JSON.parse(text) as unknown;
|
|
142
150
|
},
|
|
143
151
|
};
|
|
144
152
|
|
|
@@ -161,6 +169,7 @@ export const leafSection: SectionCodec = {
|
|
|
161
169
|
let prev = 0;
|
|
162
170
|
for (let i = 0; i < n; i++) { prev += r.varint(); keys.push(t.str(prev)); }
|
|
163
171
|
into.leaves = keys.map((key) => ({ key, digest: t.hash(r.varint()) }));
|
|
172
|
+
assertConsumed(r, 'LEAF section');
|
|
164
173
|
},
|
|
165
174
|
};
|
|
166
175
|
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import { fromHex64, HEX64, toHex } from './hex.js';
|
|
14
14
|
import { PackTooLargeError } from './types.js';
|
|
15
|
-
import { boundedCount, ByteReader, ByteWriter, PackFormatError, utf8 } from './varint.js';
|
|
15
|
+
import { assertConsumed, boundedCount, ByteReader, ByteWriter, PackFormatError, utf8 } from './varint.js';
|
|
16
16
|
|
|
17
17
|
/** What a section encoder needs: the index of a string and of a hash. */
|
|
18
18
|
export interface Tables {
|
|
@@ -78,6 +78,8 @@ export function readStringTable(bytes: Uint8Array, maxStrings: number, maxBytes:
|
|
|
78
78
|
for (let i = 0; i < n; i++) {
|
|
79
79
|
const shared = r.varint(), rest = r.varint();
|
|
80
80
|
if (shared > prev.length) throw new PackFormatError('string table: prefix longer than the previous string');
|
|
81
|
+
// A suffix the section does not hold is a malformed table (422), checked before the size cap (413) so it is never reported as "too large".
|
|
82
|
+
if (rest > r.remaining) throw new PackFormatError('string table: suffix runs past the end of the section');
|
|
81
83
|
total += shared + rest;
|
|
82
84
|
if (total > maxBytes) throw new PackTooLargeError(maxBytes);
|
|
83
85
|
const b = new Uint8Array(shared + rest);
|
|
@@ -86,6 +88,7 @@ export function readStringTable(bytes: Uint8Array, maxStrings: number, maxBytes:
|
|
|
86
88
|
try { out.push(dec.decode(b)); } catch { throw new PackFormatError('string table: invalid UTF-8'); }
|
|
87
89
|
prev = b;
|
|
88
90
|
}
|
|
91
|
+
assertConsumed(r, 'string table');
|
|
89
92
|
return out;
|
|
90
93
|
}
|
|
91
94
|
|
|
@@ -114,6 +117,7 @@ export function readHashTable(bytes: Uint8Array, maxHashes: number): string[] {
|
|
|
114
117
|
else if (flag === 1) out.push(r.str());
|
|
115
118
|
else throw new PackFormatError('hash table: unknown entry flag');
|
|
116
119
|
}
|
|
120
|
+
assertConsumed(r, 'hash table');
|
|
117
121
|
return out;
|
|
118
122
|
}
|
|
119
123
|
|
|
@@ -85,12 +85,21 @@ export class ByteReader {
|
|
|
85
85
|
return this.buf[this.pos++]!;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Unsigned LEB128, canonical only: at most {@link MAX_VARINT}, and no overlong form (a final byte of 0 after a
|
|
90
|
+
* continuation byte names the same value in more bytes). Both are a malformed pack, never a size error.
|
|
91
|
+
*/
|
|
88
92
|
varint(): number {
|
|
89
93
|
let result = 0, scale = 1;
|
|
90
94
|
for (let i = 0; i < 8; i++) {
|
|
91
95
|
const b = this.u8();
|
|
92
96
|
result += (b & 0x7f) * scale;
|
|
93
|
-
if ((b & 0x80) === 0)
|
|
97
|
+
if ((b & 0x80) === 0) {
|
|
98
|
+
if (b === 0 && i > 0) throw new PackFormatError('non-canonical varint (overlong encoding)');
|
|
99
|
+
// Exact below 2^53; any larger value rounds to at least 2^53, so the comparison never lets one through.
|
|
100
|
+
if (result > MAX_VARINT) throw new PackFormatError('varint exceeds the safe integer range');
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
94
103
|
scale *= 0x80;
|
|
95
104
|
}
|
|
96
105
|
throw new PackFormatError('varint too long');
|
|
@@ -130,6 +139,29 @@ export class ByteReader {
|
|
|
130
139
|
}
|
|
131
140
|
}
|
|
132
141
|
|
|
142
|
+
/** A flag word with a bit outside `mask` is from a newer writer this reader cannot honour: a malformed pack, never ignored. */
|
|
143
|
+
export function assertFlagsKnown(flags: number, mask: number): void {
|
|
144
|
+
if (flags & ~mask) throw new PackFormatError('unknown flag bits');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A section (or table) must be read to its last byte: bytes left over mean the writer and this reader disagree. */
|
|
148
|
+
export function assertConsumed(r: ByteReader, what: string): void {
|
|
149
|
+
if (!r.done) throw new PackFormatError(`${what}: ${r.remaining} trailing bytes`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A lone UTF-16 surrogate: a high one not followed by a low one, or a low one not preceded by a high one. */
|
|
153
|
+
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
|
|
154
|
+
const ANY_SURROGATE = /[\uD800-\uDFFF]/;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* `s` as it arrives after a trip through the wire: UTF-8 cannot carry a lone surrogate, so {@link utf8} (TextEncoder)
|
|
158
|
+
* writes U+FFFD in its place. Anything hashed on one side and compared with what the other side decoded must be hashed
|
|
159
|
+
* in THIS form. The identity for every well-formed string, so no existing digest changes.
|
|
160
|
+
*/
|
|
161
|
+
export function wireString(s: string): string {
|
|
162
|
+
return ANY_SURROGATE.test(s) ? s.replace(LONE_SURROGATE, '�') : s;
|
|
163
|
+
}
|
|
164
|
+
|
|
133
165
|
/** What a whole pack may still hold: every list a decode reads draws its count from ONE budget. */
|
|
134
166
|
export interface Budget { left: number }
|
|
135
167
|
|
package/src/knowledge/refs.ts
CHANGED
|
@@ -25,20 +25,38 @@ export function srcRef(rootId: string, path: string): NodeRef {
|
|
|
25
25
|
return `src:${rootId}::${path}`;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
/**
|
|
29
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Splits `src:<rootId>::<path>` into its root and its FULL path (never section-stripped: `src/C#/a.cs` stays whole). A root id
|
|
30
|
+
* may end in ':' (the linked repo's `cloud:<id>:`), so the colons after the first `::` that directly follow it belong to the
|
|
31
|
+
* root: `src:cloud:5:::docs/a.md` is root `cloud:5:`, path `docs/a.md`. (A path that itself starts with ':' is read as part of
|
|
32
|
+
* the root — root ids never hold '::' nor end in ':' when built by `rootIdFor`, so only a foreign id can make it ambiguous.)
|
|
33
|
+
* Pass `knownRootIds` where the root is known: a ref of one of them is split by its exact prefix, so even a path that starts
|
|
34
|
+
* with ':' is exact. Undefined when `ref` is not a source ref. THE grammar: every reader of a `src:` ref goes through this.
|
|
35
|
+
*/
|
|
36
|
+
export function splitSourceRef(ref: string, knownRootIds: readonly string[] = []): { rootId: string; path: string } | undefined {
|
|
30
37
|
if (!ref.startsWith('src:')) return undefined;
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
for (const rootId of knownRootIds) if (ref.startsWith(`src:${rootId}::`)) return { rootId, path: ref.slice(`src:${rootId}::`.length) };
|
|
39
|
+
const at = ref.indexOf('::', 4);
|
|
40
|
+
if (at <= 4) return undefined;
|
|
41
|
+
let end = at;
|
|
42
|
+
while (ref[end + 2] === ':') end += 1;
|
|
43
|
+
return { rootId: ref.slice(4, end), path: ref.slice(end + 2) };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The root-relative path of a `src:` ref (the section suffix dropped), else undefined. Splits by {@link splitSourceRef}. */
|
|
47
|
+
export function pathOfRef(ref: string): string | undefined {
|
|
48
|
+
const rest = splitSourceRef(ref)?.path;
|
|
49
|
+
if (rest === undefined) return undefined;
|
|
34
50
|
const hash = rest.indexOf('#');
|
|
35
51
|
return hash < 0 ? rest : rest.slice(0, hash);
|
|
36
52
|
}
|
|
37
53
|
|
|
38
|
-
/** The ref of the file a `src:` ref belongs to (itself for a file). */
|
|
54
|
+
/** The ref of the file a `src:` ref belongs to (itself for a file): the ref cut at the first '#' of its path ({@link splitSourceRef}). */
|
|
39
55
|
export function fileRefOf(ref: NodeRef): NodeRef {
|
|
40
|
-
const
|
|
41
|
-
|
|
56
|
+
const split = splitSourceRef(ref);
|
|
57
|
+
if (!split) return ref;
|
|
58
|
+
const hash = split.path.indexOf('#');
|
|
59
|
+
return hash < 0 ? ref : (ref.slice(0, ref.length - split.path.length + hash) as NodeRef);
|
|
42
60
|
}
|
|
43
61
|
|
|
44
62
|
/** The node ref of a stored fact key: `ent:` keys are refs already; the rest are `fact:`. */
|