pi-mega-compact 0.19.1 → 0.19.3
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/config/vector-cortex.js +21 -0
- package/dist/config.js +1 -1
- package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +2 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-query.js +46 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-topology.js +145 -5
- package/dist/extensions/dashboard-server/routes-vector-cortex.js +1 -0
- package/dist/extensions/dashboard-server/routes.js +1 -1
- package/dist/extensions/dashboard-server/server.js +3 -1
- package/dist/src/config/vector-cortex.js +21 -0
- package/dist/src/config.js +1 -1
- package/dist/src/tieredRouter.js +8 -0
- package/dist/src/vector-cortex/migrations/router-generation-v2.js +252 -0
- package/dist/src/vector-cortex/topology/build.js +232 -0
- package/dist/src/vector-cortex/topology/index.js +113 -0
- package/dist/src/vector-cortex/topology/query.js +342 -0
- package/dist/src/vector-cortex/topology/types.js +54 -0
- package/dist/src/vector-cortex/vc3b-support.js +125 -0
- package/dist/vector-cortex/migrations/router-generation-v2.js +252 -0
- package/dist/vector-cortex/topology/build.js +232 -0
- package/dist/vector-cortex/topology/index.js +113 -0
- package/dist/vector-cortex/topology/query.js +342 -0
- package/dist/vector-cortex/topology/types.js +54 -0
- package/dist/vector-cortex/vc3b-support.js +125 -0
- package/extensions/dashboard-client/dist/assets/{AreaChart-B9KYKrUE.js → AreaChart-DqiFSnDx.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AreaChart-B9KYKrUE.js.map → AreaChart-DqiFSnDx.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{BarChart-D1sWkR6j.js → BarChart-Cqg-PQMK.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{BarChart-D1sWkR6j.js.map → BarChart-Cqg-PQMK.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-BR8MYcJq.js → CacheTab-BPNs0xIF.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-BR8MYcJq.js.map → CacheTab-BPNs0xIF.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-BRNpDMvP.js → EventsTab-BQf7OGkf.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-BRNpDMvP.js.map → EventsTab-BQf7OGkf.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-rr5nXpjq.js → HealthTab-B14TaSPN.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-rr5nXpjq.js.map → HealthTab-B14TaSPN.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtjPWnzN.js → MaintenanceTab-D8lkRoa1.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtjPWnzN.js.map → MaintenanceTab-D8lkRoa1.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-5SnFDIYc.js → MemoryMapTab-BOWtGfpx.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-5SnFDIYc.js.map → MemoryMapTab-BOWtGfpx.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-4MZfwRhq.js → MetricsTab-7XI9cWaX.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-4MZfwRhq.js.map → MetricsTab-7XI9cWaX.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-BAKENnbk.js → OverviewTab-C9KaGYvh.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-BAKENnbk.js.map → OverviewTab-C9KaGYvh.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-7-z_OmZo.js → ReposTab-BwESNhox.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-7-z_OmZo.js.map → ReposTab-BwESNhox.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-D_r2ki0y.js → SessionsTab-DKH55Rda.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-D_r2ki0y.js.map → SessionsTab-DKH55Rda.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SetupTab-DPujtYgv.js → SetupTab-zDxf-nJd.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SetupTab-DPujtYgv.js.map → SetupTab-zDxf-nJd.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CLHy30t6.js → TimeSavedCard-C9isog4L.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CLHy30t6.js.map → TimeSavedCard-C9isog4L.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-Coul6CHc.js → TurnsTab-ciI6ydvj.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-Coul6CHc.js.map → TurnsTab-ciI6ydvj.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js +2 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{WikiTab-hryG5oyY.js → WikiTab-DcYHc734.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{WikiTab-hryG5oyY.js.map → WikiTab-DcYHc734.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{button-Dxo95lRI.js → button-DF8lx4bt.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{button-Dxo95lRI.js.map → button-DF8lx4bt.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{card-FXwhLF_u.js → card-DIoDheSZ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{card-FXwhLF_u.js.map → card-DIoDheSZ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-M-GeRM8U.js → generateCategoricalChart-BFRzeYX3.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-M-GeRM8U.js.map → generateCategoricalChart-BFRzeYX3.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{index-BIZZXvoy.js → index-DSZON0Yi.js} +3 -3
- package/extensions/dashboard-client/dist/assets/{index-BIZZXvoy.js.map → index-DSZON0Yi.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{switch-C-d4tkN1.js → switch-l4qo22mU.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{switch-C-d4tkN1.js.map → switch-l4qo22mU.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{toggle-Bfmucdcl.js → toggle-DPn6td1_.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{toggle-Bfmucdcl.js.map → toggle-DPn6td1_.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-CxK_wdmk.js → useSSE-BLmolqTJ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-CxK_wdmk.js.map → useSSE-BLmolqTJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +1 -1
- package/extensions/dashboard-client/src/api/vector-cortex.ts +9 -0
- package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +82 -0
- package/extensions/dashboard-client/src/types/vector-cortex.ts +25 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex.ts +35 -0
- package/extensions/dashboard-server/routes-rag-settings-helpers.ts +12 -0
- package/extensions/dashboard-server/routes-vector-cortex-query.ts +54 -0
- package/extensions/dashboard-server/routes-vector-cortex-topology.ts +166 -5
- package/extensions/dashboard-server/routes-vector-cortex.ts +1 -0
- package/extensions/dashboard-server/routes.ts +1 -0
- package/extensions/dashboard-server/server.ts +2 -0
- package/package.json +1 -1
- package/src/config/vector-cortex.ts +23 -0
- package/src/config.ts +2 -0
- package/src/tieredRouter.ts +9 -0
- package/src/vector-cortex/migrations/router-generation-v2.ts +317 -0
- package/src/vector-cortex/topology/build.ts +259 -0
- package/src/vector-cortex/topology/index.ts +143 -0
- package/src/vector-cortex/topology/query.ts +485 -0
- package/src/vector-cortex/topology/types.ts +156 -0
- package/src/vector-cortex/vc3b-support.ts +142 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-rqBpWc7A.js +0 -2
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-rqBpWc7A.js.map +0 -1
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/topology/build.ts — deterministic cortical topology builder (VC3B).
|
|
3
|
+
*
|
|
4
|
+
* Implements the deterministic graph construction (tasks 2–4):
|
|
5
|
+
* - For each (source, head) retain only candidates whose score is strictly
|
|
6
|
+
* above the calibrated threshold.
|
|
7
|
+
* - Stable-sort winning candidates by score DESCENDING, then by unsigned
|
|
8
|
+
* target-ID bytes ASCENDING (TOP-TIE-002 — equal scores sort by target ID).
|
|
9
|
+
* - Cap each (source, head) at top-k=16; the seventeenth eligible neighbor is
|
|
10
|
+
* excluded (TOP-K-001).
|
|
11
|
+
* - Remove self edges (source === target); reject them as TOP_SELF_EDGE.
|
|
12
|
+
* - Reject non-finite scores as TOP_SCORE_NONFINITE — a single bad head's NaN
|
|
13
|
+
* rejects only its own edge and never poisons other heads.
|
|
14
|
+
* - Encode dependency edges as single directed records; contradiction edges as
|
|
15
|
+
* symmetric PAIRED records (source→target and target→source) (TOP-KIND-003).
|
|
16
|
+
*
|
|
17
|
+
* This is the mode-A producer in the sprint triad: a multi-head topology index.
|
|
18
|
+
* The other two modes (B linear VectorSet scan, C source-seq/keyword) must yield
|
|
19
|
+
* the same threshold/tie/cap invariants and the same graph digest. Pure
|
|
20
|
+
* deterministic function — no I/O, no console, no network (PREVENT-PI-004),
|
|
21
|
+
* no `any` (PREVENT-011).
|
|
22
|
+
*/
|
|
23
|
+
import { TOP_K, } from "./types.js";
|
|
24
|
+
/**
|
|
25
|
+
* Field (`|`) and record (`~`) separators used by the canonical digest framing in
|
|
26
|
+
* index.ts. Id values (source/target/head) carrying either byte would corrupt the
|
|
27
|
+
* framing and make distinct graphs collide on an identical digest, so the build
|
|
28
|
+
* rejects such candidates with TOP_FRAMING_SEP (Q04, mirrors the dashboard's
|
|
29
|
+
* parseCandidatePayload guard) — enforced at this pure seam, not only at one caller.
|
|
30
|
+
*/
|
|
31
|
+
const FRAMING_SEP = /[|~]/;
|
|
32
|
+
/**
|
|
33
|
+
* Build the deterministic topology graph from calibrated-threshold candidates.
|
|
34
|
+
* Order-independent across any permutation of `input.candidates`.
|
|
35
|
+
*/
|
|
36
|
+
export function buildTopology(input) {
|
|
37
|
+
const { sessionId, sourceHighWater, threshold } = input;
|
|
38
|
+
// Group winning candidates by (source, head), stable-sorting each group by
|
|
39
|
+
// score desc then unsigned target-ID bytes, capping at top-k.
|
|
40
|
+
const byGroup = new Map();
|
|
41
|
+
const rejections = [];
|
|
42
|
+
for (const c of input.candidates) {
|
|
43
|
+
// Q04 — framing hardening: the canonical digest serialization in index.ts uses
|
|
44
|
+
// `|` (FIELD_SEP) and `~` (RECORD_SEP) as unambiguous delimiters. Those are
|
|
45
|
+
// collision-free only while id values (source/target/head) contain neither
|
|
46
|
+
// byte; a content-derived id carrying either would make two distinct graphs
|
|
47
|
+
// hash to an identical digest. Enforce the no-separator-in-id constraint at
|
|
48
|
+
// this pure build seam (not only at the dashboard caller) by rejecting such a
|
|
49
|
+
// candidate in isolation with TOP_FRAMING_SEP — the digest stays well-defined
|
|
50
|
+
// for ANY input.
|
|
51
|
+
if (FRAMING_SEP.test(c.source) || FRAMING_SEP.test(c.target) || FRAMING_SEP.test(c.head)) {
|
|
52
|
+
rejections.push(reject(c, "TOP_FRAMING_SEP"));
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
// Non-finite score: reject this candidate in isolation (never poison others).
|
|
56
|
+
if (!Number.isFinite(c.score)) {
|
|
57
|
+
rejections.push(reject(c, "TOP_SCORE_NONFINITE"));
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (c.source === c.target) {
|
|
61
|
+
rejections.push(reject(c, "TOP_SELF_EDGE"));
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (c.score <= threshold)
|
|
65
|
+
continue; // below calibrated threshold
|
|
66
|
+
const key = groupKey(c.source, c.head);
|
|
67
|
+
const group = byGroup.get(key);
|
|
68
|
+
if (group)
|
|
69
|
+
group.push(c);
|
|
70
|
+
else
|
|
71
|
+
byGroup.set(key, [c]);
|
|
72
|
+
}
|
|
73
|
+
// Stable sort each (source, head) group: score desc, then unsigned target-ID
|
|
74
|
+
// bytes asc. Keep exactly the top-k=16 (the 17th eligible neighbor is excluded).
|
|
75
|
+
const selected = [];
|
|
76
|
+
for (const group of byGroup.values()) {
|
|
77
|
+
group.sort(compareCandidates);
|
|
78
|
+
for (let i = 0; i < Math.min(group.length, TOP_K); i++) {
|
|
79
|
+
selected.push(group[i]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Encode dependency edges as directed records; contradiction edges as
|
|
83
|
+
// symmetric PAIRED records. Duplicate (source,target,head) contradictions
|
|
84
|
+
// collapse: the same symmetric pair is emitted once per direction — dedupe to
|
|
85
|
+
// keep the paired graph well-formed and the digest stable.
|
|
86
|
+
//
|
|
87
|
+
// IMPORTANT: `selected` is first ordered by a canonical global comparator so
|
|
88
|
+
// every dedup decision (which head represents a collapsed contradiction pair,
|
|
89
|
+
// which identical directed edge is kept) is deterministic and INDEPENDENT of
|
|
90
|
+
// the incoming candidate order. Without this, reversing the input order could
|
|
91
|
+
// flip which head represents a symmetric pair and change the digest.
|
|
92
|
+
//
|
|
93
|
+
// The comparator sorts by SCORE DESCENDING FIRST, so first-writer-wins dedup
|
|
94
|
+
// keeps the MAXIMUM score per collapsed relation. Two candidates emitting the
|
|
95
|
+
// same directed edge (same source,target,head) with different scores keep the
|
|
96
|
+
// higher one; a contradiction pair a↔b with different scores is claimed by the
|
|
97
|
+
// higher-scoring candidate regardless of which end is "source". A downstream
|
|
98
|
+
// consumer (VC3C receives TopologyV1) therefore gets the strongest score for a
|
|
99
|
+
// collapsed relation, never the weakest, and the winner is decided by score —
|
|
100
|
+
// not by source-ID byte order. (Score ties fall through to bytewise keys so
|
|
101
|
+
// the total order stays deterministic.)
|
|
102
|
+
selected.sort(compareSelected);
|
|
103
|
+
const edges = new Map();
|
|
104
|
+
const nodes = new Map();
|
|
105
|
+
for (const c of selected) {
|
|
106
|
+
nodes.set(c.source, nodeKind(c));
|
|
107
|
+
nodes.set(c.target, nodeKind(c));
|
|
108
|
+
if (c.kind === "contradiction") {
|
|
109
|
+
// Symmetric pair: emit source→target and target→source exactly once, keyed
|
|
110
|
+
// so that a reversed duplicate from another candidate collapses to one pair.
|
|
111
|
+
const fwd = pairKey(c.source, c.target);
|
|
112
|
+
const rev = pairKey(c.target, c.source);
|
|
113
|
+
const hasFwd = edges.has(fwd) || edges.has(rev);
|
|
114
|
+
if (!hasFwd) {
|
|
115
|
+
edges.set(fwd, { source: c.source, target: c.target, head: c.head, score: c.score, direction: "contradiction" });
|
|
116
|
+
edges.set(rev, { source: c.target, target: c.source, head: c.head, score: c.score, direction: "contradiction" });
|
|
117
|
+
}
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
// Directed dependency: distinct per (source, target, head).
|
|
121
|
+
const depKey = dirKey(c.source, c.target, c.head, "dependency");
|
|
122
|
+
if (!edges.has(depKey)) {
|
|
123
|
+
edges.set(depKey, { source: c.source, target: c.target, head: c.head, score: c.score, direction: "dependency" });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const nodeList = [...nodes.entries()]
|
|
127
|
+
.map(([id, kind]) => ({ id, kind }))
|
|
128
|
+
.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
129
|
+
const edgeList = [...edges.values()].sort(compareEdges);
|
|
130
|
+
const graph = {
|
|
131
|
+
schema: "topology-v1",
|
|
132
|
+
sessionId,
|
|
133
|
+
sourceHighWater,
|
|
134
|
+
threshold,
|
|
135
|
+
nodeCount: nodeList.length,
|
|
136
|
+
edgeCount: edgeList.length,
|
|
137
|
+
generationDigest: "",
|
|
138
|
+
nodes: nodeList,
|
|
139
|
+
edges: edgeList,
|
|
140
|
+
};
|
|
141
|
+
return { ok: true, topology: graph, rejected: rejections };
|
|
142
|
+
}
|
|
143
|
+
function reject(c, code) {
|
|
144
|
+
return { source: c.source, target: c.target, head: c.head, score: c.score, code };
|
|
145
|
+
}
|
|
146
|
+
function groupKey(source, head) {
|
|
147
|
+
return JSON.stringify([source, head]);
|
|
148
|
+
}
|
|
149
|
+
function pairKey(a, b) {
|
|
150
|
+
return JSON.stringify([a, b, " "]);
|
|
151
|
+
}
|
|
152
|
+
function dirKey(a, b, head, direction) {
|
|
153
|
+
return JSON.stringify([a, b, head, direction]);
|
|
154
|
+
}
|
|
155
|
+
function compareCandidates(a, b) {
|
|
156
|
+
if (a.score !== b.score)
|
|
157
|
+
return a.score > b.score ? -1 : 1;
|
|
158
|
+
// Equal scores: sort by unsigned target-ID bytes ascending (TOP-TIE-002).
|
|
159
|
+
const byTarget = unsignedBytesCompare(a.target, b.target);
|
|
160
|
+
if (byTarget !== 0)
|
|
161
|
+
return byTarget;
|
|
162
|
+
// Equal score + target: break the tie by kind bytes so the within-group sort is
|
|
163
|
+
// a TOTAL order, independent of input order (Q01). Without this, two tied
|
|
164
|
+
// candidates of differing kind that land exactly at the top-k=16 boundary would
|
|
165
|
+
// fall back to JS stable order = candidate input order, flipping which edge
|
|
166
|
+
// survives and changing the graph digest. Mirrors compareSelected's `kind` key.
|
|
167
|
+
return unsignedBytesCompare(a.kind, b.kind);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Total, deterministic order over selected candidates used to drive dedup before
|
|
171
|
+
* encoding. Independent of the incoming input order so a collapsed contradiction
|
|
172
|
+
* pair always picks the same representing head and identical directed edges keep
|
|
173
|
+
* the same winner.
|
|
174
|
+
*
|
|
175
|
+
* SCORE DESCENDING IS THE PRIMARY KEY: first-writer-wins dedup on this order
|
|
176
|
+
* retains the highest score for every collapsed relation (Q01). Only after
|
|
177
|
+
* scores are equal do we fall through to bytewise source/target/head/kind keys,
|
|
178
|
+
* which keeps a sound deterministic tie-break (a score tie collapses to the
|
|
179
|
+
* same representing head regardless of input order).
|
|
180
|
+
*/
|
|
181
|
+
function compareSelected(a, b) {
|
|
182
|
+
return ((a.score < b.score ? 1 : a.score > b.score ? -1 : 0) ||
|
|
183
|
+
unsignedBytesCompare(a.source, b.source) ||
|
|
184
|
+
unsignedBytesCompare(a.target, b.target) ||
|
|
185
|
+
unsignedBytesCompare(a.head, b.head) ||
|
|
186
|
+
unsignedBytesCompare(a.kind, b.kind));
|
|
187
|
+
}
|
|
188
|
+
function compareEdges(a, b) {
|
|
189
|
+
return (unsignedBytesCompare(a.source, b.source) ||
|
|
190
|
+
unsignedBytesCompare(a.target, b.target) ||
|
|
191
|
+
unsignedBytesCompare(a.head, b.head) ||
|
|
192
|
+
unsignedBytesCompare(a.direction, b.direction) ||
|
|
193
|
+
(a.score < b.score ? -1 : a.score > b.score ? 1 : 0));
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Unsigned bytewise comparison over UTF-8 bytes of two strings. String
|
|
197
|
+
* `<`/`>` in JS is UTF-16 code-unit order, which equals UTF-8 byte order for
|
|
198
|
+
* the ASCII identifiers used (and for our domain); we implement an explicit
|
|
199
|
+
* unsigned-byte comparator so ordering is defined by spec, not JS internals.
|
|
200
|
+
*/
|
|
201
|
+
function unsignedBytesCompare(a, b) {
|
|
202
|
+
const ba = Buffer.from(a, "utf8");
|
|
203
|
+
const bb = Buffer.from(b, "utf8");
|
|
204
|
+
const n = Math.min(ba.length, bb.length);
|
|
205
|
+
for (let i = 0; i < n; i++) {
|
|
206
|
+
if (ba[i] !== bb[i])
|
|
207
|
+
return ba[i] < bb[i] ? -1 : 1;
|
|
208
|
+
}
|
|
209
|
+
return ba.length < bb.length ? -1 : ba.length > bb.length ? 1 : 0;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Node kind for a retained edge: a node is marked by the record kind it is
|
|
213
|
+
* derived from. A contradiction candidate marks both endpoints "contradiction";
|
|
214
|
+
* a dependency candidate marks both endpoints "dependency" (Q02) — the node
|
|
215
|
+
* kind matches the producing record kind, never a synthetic placeholder.
|
|
216
|
+
*
|
|
217
|
+
* NODE-KIND SEMANTICS (documented, Q03): a node participates in the kind of
|
|
218
|
+
* whichever retained edge LABELLED it LAST during the linear pass over `selected`.
|
|
219
|
+
* Because `selected` is sorted SCORE-DESCENDING first, the last label a node
|
|
220
|
+
* receives comes from its LOWEST-scoring retained edge — i.e. when a node is
|
|
221
|
+
* touched by both a dependency and a contradiction edge (of equal or lower
|
|
222
|
+
* score), the WEAKEST relation's kind wins the node's `kind` field, not the
|
|
223
|
+
* strongest. This is deliberate and DETERMINISTIC (the total compareSelected
|
|
224
|
+
* order, with the Q01 `kind` tie-break, fixes the pass order independent of
|
|
225
|
+
* input order), and mode B reproduces it exactly. A downstream VC3C consumer
|
|
226
|
+
* must read node.kind as "the relation kind of the weakest retained edge
|
|
227
|
+
* incident to this node", not "the dominant/strongest relation". Per-edge
|
|
228
|
+
* direction + kind are authoritative on the edges themselves.
|
|
229
|
+
*/
|
|
230
|
+
function nodeKind(c) {
|
|
231
|
+
return c.kind === "contradiction" ? "contradiction" : "dependency";
|
|
232
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/topology/index.ts — topology barrel + stable graph digest (VC3B).
|
|
3
|
+
*
|
|
4
|
+
* Typical role: a thin barrel + a few derived helpers. The build algorithm lives
|
|
5
|
+
* in build.ts; this file:
|
|
6
|
+
* - re-exports the TopologyV1 / EdgeV1 contracts;
|
|
7
|
+
* - computes ONE deterministic graph digest over the canonical, order-
|
|
8
|
+
* independent serialization of nodes + edges (task 4);
|
|
9
|
+
* - compose `buildTopologyGraph` = build + digest + emit on one seam.
|
|
10
|
+
*
|
|
11
|
+
* Ordinal-order independence: the digest is computed over nodes sorted by id
|
|
12
|
+
* bytes and edges sorted by (source, target, head, direction) then score, so
|
|
13
|
+
* any permutation of the build input yields an identical digest (verified by the
|
|
14
|
+
* acceptance consumer across 1,000 distinct input orderings — Q02).
|
|
15
|
+
*
|
|
16
|
+
* Pure deterministic helpers: no I/O, no console, no network (PREVENT-PI-004),
|
|
17
|
+
* no `any` (PREVENT-011).
|
|
18
|
+
*/
|
|
19
|
+
import { createHash } from "node:crypto";
|
|
20
|
+
import { buildTopology } from "./build.js";
|
|
21
|
+
export { TOP_IDS, TOP_NAMED_IDS, TOP_K } from "./types.js";
|
|
22
|
+
export { buildTopology } from "./build.js";
|
|
23
|
+
/**
|
|
24
|
+
* Deterministic topological-construction seam (task 5). Runs the build, fills
|
|
25
|
+
* the stable generationDigest, and optionally emits:
|
|
26
|
+
* - `vector_cortex_topology_built` — once, when a graph is produced;
|
|
27
|
+
* - `vector_cortex_topology_edge_rejected`— once per rejected edge with code.
|
|
28
|
+
* Emission is best-effort and non-fatal (never breaks the agent loop).
|
|
29
|
+
*/
|
|
30
|
+
export function buildTopologyGraph(input, emit) {
|
|
31
|
+
const result = buildTopology(input);
|
|
32
|
+
if (!result.ok)
|
|
33
|
+
return result;
|
|
34
|
+
const topology = {
|
|
35
|
+
...result.topology,
|
|
36
|
+
generationDigest: graphDigest(result.topology),
|
|
37
|
+
};
|
|
38
|
+
const out = { ok: true, topology, rejected: result.rejected };
|
|
39
|
+
if (emit) {
|
|
40
|
+
try {
|
|
41
|
+
emit("vector_cortex_topology_built", {
|
|
42
|
+
sessionId: topology.sessionId,
|
|
43
|
+
sourceHighWater: topology.sourceHighWater.toString(),
|
|
44
|
+
nodeCount: topology.nodeCount,
|
|
45
|
+
edgeCount: topology.edgeCount,
|
|
46
|
+
generationDigest: topology.generationDigest,
|
|
47
|
+
});
|
|
48
|
+
for (const r of result.rejected) {
|
|
49
|
+
emit("vector_cortex_topology_edge_rejected", {
|
|
50
|
+
sessionId: topology.sessionId,
|
|
51
|
+
source: r.source,
|
|
52
|
+
target: r.target,
|
|
53
|
+
head: r.head,
|
|
54
|
+
code: r.code,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
/* non-fatal observability — never break the agent loop */
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* ONE deterministic SHA-256 (hex) over the canonical serialization of the graph.
|
|
66
|
+
* Canonical form is a compact stream: `topology-v1|` + sorted nodes + `|` +
|
|
67
|
+
* sorted edges, where nodes are sorted by id bytes and edges by
|
|
68
|
+
* (source, target, head, direction) then score. Order-independence is achieved
|
|
69
|
+
* by that canonical sort, never by input order.
|
|
70
|
+
*
|
|
71
|
+
* Printable framing (no control bytes): FIELD_SEP (`|`) separates fields and
|
|
72
|
+
* RECORD_SEP (`~`) separates records. The project-controlled field values (ids,
|
|
73
|
+
* kinds, heads, directions, scores) contain neither `|` nor `~`, so the framing
|
|
74
|
+
* never collides, and keeping the file control-byte-free lets git treat this
|
|
75
|
+
* core sprint file as text (prevents a spurious "binary file" classification).
|
|
76
|
+
*/
|
|
77
|
+
// Printable, unambiguous framing delimiters for the canonical serialization.
|
|
78
|
+
const FIELD_SEP = "|";
|
|
79
|
+
const RECORD_SEP = "~";
|
|
80
|
+
export function graphDigest(topology) {
|
|
81
|
+
const nodes = canonicalNodes(topology.nodes);
|
|
82
|
+
const edges = canonicalEdges(topology.edges);
|
|
83
|
+
const hash = createHash("sha256");
|
|
84
|
+
hash.update(`topology-v1${FIELD_SEP}`);
|
|
85
|
+
hash.update(nodes);
|
|
86
|
+
hash.update(FIELD_SEP);
|
|
87
|
+
hash.update(edges);
|
|
88
|
+
return `sha256:${hash.digest("hex")}`;
|
|
89
|
+
}
|
|
90
|
+
function canonicalNodes(nodes) {
|
|
91
|
+
const sorted = [...nodes]
|
|
92
|
+
.map((n) => `${n.id}${FIELD_SEP}${n.kind}`)
|
|
93
|
+
.sort(unsignedBytesCompare)
|
|
94
|
+
.join(RECORD_SEP);
|
|
95
|
+
return Buffer.from(sorted, "utf8");
|
|
96
|
+
}
|
|
97
|
+
function canonicalEdges(edges) {
|
|
98
|
+
const parts = edges
|
|
99
|
+
.map((e) => `${e.source}${FIELD_SEP}${e.target}${FIELD_SEP}${e.head}${FIELD_SEP}${e.direction}${FIELD_SEP}${e.score}`)
|
|
100
|
+
.sort(unsignedBytesCompare)
|
|
101
|
+
.join(RECORD_SEP);
|
|
102
|
+
return Buffer.from(parts, "utf8");
|
|
103
|
+
}
|
|
104
|
+
function unsignedBytesCompare(a, b) {
|
|
105
|
+
const ba = Buffer.from(a, "utf8");
|
|
106
|
+
const bb = Buffer.from(b, "utf8");
|
|
107
|
+
const n = Math.min(ba.length, bb.length);
|
|
108
|
+
for (let i = 0; i < n; i++) {
|
|
109
|
+
if (ba[i] !== bb[i])
|
|
110
|
+
return ba[i] < bb[i] ? -1 : 1;
|
|
111
|
+
}
|
|
112
|
+
return ba.length < bb.length ? -1 : ba.length > bb.length ? 1 : 0;
|
|
113
|
+
}
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/topology/query.ts — topology query + router invalidation (VC3C).
|
|
3
|
+
*
|
|
4
|
+
* Owns `RouterKeyV2` (the structured router key) and `TopologyQueryV1` (the
|
|
5
|
+
* generation query/index surface that serves graphs and invalidates stale
|
|
6
|
+
* generations). This is the degradation seam of the sprint's failure triad:
|
|
7
|
+
*
|
|
8
|
+
* A = topology index at current generation (serve the active graph);
|
|
9
|
+
* B = fresh linear scan forced when a key targets a STALE generation;
|
|
10
|
+
* C = authority sequence scan forced when the derived store is unavailable.
|
|
11
|
+
*
|
|
12
|
+
* Key encoding is length-delimited with unsigned-byte (prefix) ordering and a
|
|
13
|
+
* FIXED five-field arity, so no field value can ambiguously prefix another and
|
|
14
|
+
* two sessions `a` / `aa` can never prefix-collide at the key level
|
|
15
|
+
* (M6-KEY-001). Invalidation matches the exact `(session,generation)` identity
|
|
16
|
+
* bytes — never a string-prefix — and a query whose key carries a generation
|
|
17
|
+
* older than the session's active one is rejected with `TOP_GENERATION_STALE`
|
|
18
|
+
* (M6-STALE-002), which forces mode B.
|
|
19
|
+
*
|
|
20
|
+
* Pure src/ logic over an injected `TopologyQueryHost` (the source of graphs +
|
|
21
|
+
* linear/authority scans) and an optional `TopologyQueryEmit`. Deterministic,
|
|
22
|
+
* pi-agnostic, no console, no network (PREVENT-PI-004), no `any`
|
|
23
|
+
* (PREVENT-011). The caller owns durability; every store write on the caller
|
|
24
|
+
* side is best-effort and never breaks the agent loop.
|
|
25
|
+
*/
|
|
26
|
+
import { createHash } from "node:crypto";
|
|
27
|
+
/** Query rejected: the key's generation is stale relative to the active one. */
|
|
28
|
+
export const TOP_GENERATION_STALE = "TOP_GENERATION_STALE";
|
|
29
|
+
/** Query rejected: a key could not be decoded (malformed length-delimited bytes). */
|
|
30
|
+
export const TOP_KEY_DECODE_FAILED = "TOP_KEY_DECODE_FAILED";
|
|
31
|
+
/** Query rejected: the derived store is unavailable and no authority scan is possible. */
|
|
32
|
+
export const TOP_AUTHORITY_UNAVAILABLE = "TOP_AUTHORITY_UNAVAILABLE";
|
|
33
|
+
/** Frozen router key schema version carried in every encoded key. */
|
|
34
|
+
export const ROUTER_KEY_VERSION = 2;
|
|
35
|
+
/**
|
|
36
|
+
* Create the query surface. `emit` is optional and flag-gated (the caller passes
|
|
37
|
+
* a reporter produced by the same pattern VC3A/VC3B use — zero emissions when
|
|
38
|
+
* `MEGACOMPACT_VC3C=0`, byte-identical to the predecessor). Pure in-memory index
|
|
39
|
+
* over the injected host; durability of invalidation is the caller's job.
|
|
40
|
+
*/
|
|
41
|
+
export function createTopologyQuery(host, emit) {
|
|
42
|
+
// session -> Set of indexed generation strings (exact, byte-identity).
|
|
43
|
+
const generations = new Map();
|
|
44
|
+
const fire = (event, fields) => {
|
|
45
|
+
if (!emit)
|
|
46
|
+
return;
|
|
47
|
+
try {
|
|
48
|
+
emit(event, fields);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
/* non-fatal observability — never break the agent loop */
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const query = (key) => {
|
|
55
|
+
const active = host.activeGeneration(key.session);
|
|
56
|
+
// Mode C: derived store unavailable -> authority sequence scan.
|
|
57
|
+
if (!host.derivedAvailable(key.session)) {
|
|
58
|
+
const cDigest = host.authorityScan(key.session, key.sourceStart, key.sourceEnd);
|
|
59
|
+
if (cDigest === undefined) {
|
|
60
|
+
return { ok: false, code: TOP_AUTHORITY_UNAVAILABLE, mode: "C" };
|
|
61
|
+
}
|
|
62
|
+
fire("vector_cortex_topology_query_demoted", {
|
|
63
|
+
session: key.session,
|
|
64
|
+
generation: key.generation.toString(),
|
|
65
|
+
code: "DERIVED_UNAVAILABLE",
|
|
66
|
+
toMode: "C",
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
ok: true,
|
|
70
|
+
mode: "C",
|
|
71
|
+
session: key.session,
|
|
72
|
+
generation: key.generation,
|
|
73
|
+
algorithm: key.algorithm,
|
|
74
|
+
matchedKey: encodeRouterKeyV2(key),
|
|
75
|
+
digest: cDigest.digest,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
// A key is stale when its generation is below the active one (or the active
|
|
79
|
+
// generation is unset and the requested generation is non-zero).
|
|
80
|
+
const stale = active === undefined
|
|
81
|
+
? key.generation !== 0n
|
|
82
|
+
: key.generation < active;
|
|
83
|
+
if (stale) {
|
|
84
|
+
// Mode B: fresh linear scan forced by a stale A key.
|
|
85
|
+
const bDigest = host.linearScan(key.session, key.sourceStart, key.sourceEnd);
|
|
86
|
+
fire("vector_cortex_topology_query_demoted", {
|
|
87
|
+
session: key.session,
|
|
88
|
+
generation: key.generation.toString(),
|
|
89
|
+
activeGeneration: active === undefined ? null : active.toString(),
|
|
90
|
+
code: "STALE_GENERATION",
|
|
91
|
+
toMode: "B",
|
|
92
|
+
});
|
|
93
|
+
return {
|
|
94
|
+
ok: true,
|
|
95
|
+
mode: "B",
|
|
96
|
+
session: key.session,
|
|
97
|
+
generation: key.generation,
|
|
98
|
+
algorithm: key.algorithm,
|
|
99
|
+
matchedKey: encodeRouterKeyV2(key),
|
|
100
|
+
digest: bDigest.digest,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// Mode A: topology index at the current generation. If the derived store is
|
|
104
|
+
// up but has not indexed this exact (generation, algorithm) yet, that is a
|
|
105
|
+
// mode-A miss (no graph to serve) — force the fresh linear scan rather than
|
|
106
|
+
// fabricate a graph or report a false staleness (Q03: the index cannot be
|
|
107
|
+
// trusted to hold every current generation; B recomputes from source).
|
|
108
|
+
const graph = host.graphAt(key.session, key.generation, key.algorithm);
|
|
109
|
+
if (graph === undefined) {
|
|
110
|
+
const bDigest = host.linearScan(key.session, key.sourceStart, key.sourceEnd);
|
|
111
|
+
fire("vector_cortex_topology_query_demoted", {
|
|
112
|
+
session: key.session,
|
|
113
|
+
generation: key.generation.toString(),
|
|
114
|
+
code: "GENERATION_MISS",
|
|
115
|
+
toMode: "B",
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
ok: true,
|
|
119
|
+
mode: "B",
|
|
120
|
+
session: key.session,
|
|
121
|
+
generation: key.generation,
|
|
122
|
+
algorithm: key.algorithm,
|
|
123
|
+
matchedKey: encodeRouterKeyV2(key),
|
|
124
|
+
digest: bDigest.digest,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
ok: true,
|
|
129
|
+
mode: "A",
|
|
130
|
+
session: key.session,
|
|
131
|
+
generation: key.generation,
|
|
132
|
+
algorithm: key.algorithm,
|
|
133
|
+
matchedKey: encodeRouterKeyV2(key),
|
|
134
|
+
digest: graph.digest,
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
const invalidate = (session, generation) => {
|
|
138
|
+
const invalidationIdentity = invalidationKey(session, generation);
|
|
139
|
+
const set = generations.get(session);
|
|
140
|
+
if (set)
|
|
141
|
+
set.delete(invalidationIdentity);
|
|
142
|
+
fire("vector_cortex_router_generation_invalidated", {
|
|
143
|
+
session,
|
|
144
|
+
generation: generation.toString(),
|
|
145
|
+
key: invalidationIdentity,
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
const generationalCount = (session) => {
|
|
149
|
+
return generations.get(session)?.size ?? 0;
|
|
150
|
+
};
|
|
151
|
+
return { query, invalidate, generationalCount };
|
|
152
|
+
}
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
// Length-delimited, unsigned-byte-ordered key encoding (M6-KEY-001)
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
/**
|
|
157
|
+
* Encode one field: `u32BE(byteLength) || bytes`. Non-negative numbers are
|
|
158
|
+
* encoded as their unsigned big-endian byte form (leading zeros trimmed). This
|
|
159
|
+
* is the standard big-endian length-prefixed encoding: two fields compare in
|
|
160
|
+
* unsigned-byte order exactly as their values do, and no value is an ambiguous
|
|
161
|
+
* prefix of another.
|
|
162
|
+
*/
|
|
163
|
+
function encodeLengthDelimited(bytes) {
|
|
164
|
+
const out = new Uint8Array(4 + bytes.length);
|
|
165
|
+
const view = new DataView(out.buffer);
|
|
166
|
+
view.setUint32(0, bytes.length, false);
|
|
167
|
+
out.set(bytes, 4);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
/** Big-endian unsigned bytes of a non-negative bigint (length-prefixed below). */
|
|
171
|
+
function bigintBytes(n) {
|
|
172
|
+
let v = n;
|
|
173
|
+
const out = [];
|
|
174
|
+
if (v === 0n)
|
|
175
|
+
return new Uint8Array([0]);
|
|
176
|
+
while (v > 0n) {
|
|
177
|
+
out.unshift(Number(v & 0xffn));
|
|
178
|
+
v >>= 8n;
|
|
179
|
+
}
|
|
180
|
+
return Uint8Array.from(out);
|
|
181
|
+
}
|
|
182
|
+
/** UTF-8 bytes of a string. */
|
|
183
|
+
function strBytes(s) {
|
|
184
|
+
return new TextEncoder().encode(s);
|
|
185
|
+
}
|
|
186
|
+
function concat(parts) {
|
|
187
|
+
let len = 0;
|
|
188
|
+
for (const p of parts)
|
|
189
|
+
len += p.length;
|
|
190
|
+
const out = new Uint8Array(len);
|
|
191
|
+
let off = 0;
|
|
192
|
+
for (const p of parts) {
|
|
193
|
+
out.set(p, off);
|
|
194
|
+
off += p.length;
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Encode a RouterKeyV2 into its canonical, length-delimited, unsigned-byte-ordered
|
|
200
|
+
* byte form. Version-prefixed (1 byte) + five length-delimited fields in fixed
|
|
201
|
+
* order (session, sourceStart, sourceEnd, generation, algorithm). Fixed arity +
|
|
202
|
+
* length-delimiting means no key is a strict prefix of another and sessions `a`
|
|
203
|
+
* and `aa` never collide (M6-KEY-001).
|
|
204
|
+
*/
|
|
205
|
+
export function encodeRouterKeyV2(key) {
|
|
206
|
+
const version = Uint8Array.of(ROUTER_KEY_VERSION);
|
|
207
|
+
const session = encodeLengthDelimited(strBytes(key.session));
|
|
208
|
+
const sourceStart = encodeLengthDelimited(bigintBytes(key.sourceStart));
|
|
209
|
+
const sourceEnd = encodeLengthDelimited(bigintBytes(key.sourceEnd));
|
|
210
|
+
const generation = encodeLengthDelimited(bigintBytes(key.generation));
|
|
211
|
+
const algorithm = encodeLengthDelimited(strBytes(key.algorithm));
|
|
212
|
+
return "rk2:" + Buffer.from(concat([version, session, sourceStart, sourceEnd, generation, algorithm])).toString("hex");
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Decode a RouterKeyV2 from its canonical byte form. Returns
|
|
216
|
+
* TOP_KEY_DECODE_FAILED on malformed bytes (bad version, truncated length, or
|
|
217
|
+
* trailing garbage). Never parses by string prefix.
|
|
218
|
+
*/
|
|
219
|
+
export function decodeRouterKeyV2(encoded) {
|
|
220
|
+
if (!encoded.startsWith("rk2:"))
|
|
221
|
+
return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
222
|
+
const hex = encoded.slice(4);
|
|
223
|
+
if (!/^[0-9a-f]*$/.test(hex) || hex.length % 2 !== 0) {
|
|
224
|
+
return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
225
|
+
}
|
|
226
|
+
const buf = Buffer.from(hex, "hex");
|
|
227
|
+
const bytes = new Uint8Array(buf);
|
|
228
|
+
const reader = new Reader(bytes);
|
|
229
|
+
const version = reader.u8();
|
|
230
|
+
if (version !== ROUTER_KEY_VERSION)
|
|
231
|
+
return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
232
|
+
const sessionBytes = reader.lenPrefixed();
|
|
233
|
+
const ssBytes = reader.lenPrefixed();
|
|
234
|
+
const seBytes = reader.lenPrefixed();
|
|
235
|
+
const genBytes = reader.lenPrefixed();
|
|
236
|
+
const algoBytes = reader.lenPrefixed();
|
|
237
|
+
if (reader.remaining() !== 0)
|
|
238
|
+
return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
239
|
+
if (sessionBytes === null || ssBytes === null || seBytes === null || genBytes === null || algoBytes === null) {
|
|
240
|
+
return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
241
|
+
}
|
|
242
|
+
const session = new TextDecoder().decode(sessionBytes);
|
|
243
|
+
const sourceStart = bytesToBigint(ssBytes);
|
|
244
|
+
const sourceEnd = bytesToBigint(seBytes);
|
|
245
|
+
const generation = bytesToBigint(genBytes);
|
|
246
|
+
const algorithm = new TextDecoder().decode(algoBytes);
|
|
247
|
+
return { ok: true, key: { session, sourceStart, sourceEnd, generation, algorithm } };
|
|
248
|
+
}
|
|
249
|
+
/** Sequential length-delimited byte reader. */
|
|
250
|
+
class Reader {
|
|
251
|
+
bytes;
|
|
252
|
+
off = 0;
|
|
253
|
+
constructor(bytes) {
|
|
254
|
+
this.bytes = bytes;
|
|
255
|
+
}
|
|
256
|
+
u8() {
|
|
257
|
+
const v = this.bytes[this.off];
|
|
258
|
+
this.off += 1;
|
|
259
|
+
return v;
|
|
260
|
+
}
|
|
261
|
+
lenPrefixed() {
|
|
262
|
+
if (this.off + 4 > this.bytes.length)
|
|
263
|
+
return null;
|
|
264
|
+
const view = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.off, 4);
|
|
265
|
+
const len = view.getUint32(0, false);
|
|
266
|
+
this.off += 4;
|
|
267
|
+
if (this.off + len > this.bytes.length)
|
|
268
|
+
return null;
|
|
269
|
+
const out = this.bytes.slice(this.off, this.off + len);
|
|
270
|
+
this.off += len;
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
remaining() {
|
|
274
|
+
return this.bytes.length - this.off;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** Non-negative bigint from its unsigned big-endian bytes. */
|
|
278
|
+
function bytesToBigint(bytes) {
|
|
279
|
+
let n = 0n;
|
|
280
|
+
for (const b of bytes)
|
|
281
|
+
n = (n << 8n) | BigInt(b);
|
|
282
|
+
return n;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Canonical invalidation identity for an exact (session,generation) pair, using
|
|
286
|
+
* the SAME length-delimited scheme so two pairs can only match by exact byte
|
|
287
|
+
* equality (never by string prefix — `a` never matches `aa`). Returned as hex
|
|
288
|
+
* for use as a registry key.
|
|
289
|
+
*/
|
|
290
|
+
export function invalidationKey(session, generation) {
|
|
291
|
+
const sessionF = encodeLengthDelimited(strBytes(session));
|
|
292
|
+
const genF = encodeLengthDelimited(bigintBytes(generation));
|
|
293
|
+
return "inv:" + Buffer.from(concat([sessionF, genF])).toString("hex");
|
|
294
|
+
}
|
|
295
|
+
/** One deterministic SHA-256 (hex) digest over the canonical key bytes. */
|
|
296
|
+
export function keyDigest(key) {
|
|
297
|
+
return `sha256:${createHash("sha256").update(Buffer.from(encodeRouterKeyV2(key).slice(4), "hex")).digest("hex")}`;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Shared invalidation-capability seam (narrow delegate, no routing rewrite).
|
|
301
|
+
* Records the EXACT (session,generation) identity and, when `enabled`, best-effort
|
|
302
|
+
* emits `vector_cortex_router_generation_invalidated`. Falsy `enabled` (flag OFF)
|
|
303
|
+
* is a strict no-op so the predecessor keying is byte-identical to pre-sprint.
|
|
304
|
+
* Exactness is enforced by `invalidationKey` (length-delimited, never a string
|
|
305
|
+
* prefix — `a` never matches `aa`), the same identity M6 validates.
|
|
306
|
+
*/
|
|
307
|
+
export function routerGenerationInvalidationSeam(key, enabled, emit) {
|
|
308
|
+
if (!enabled)
|
|
309
|
+
return;
|
|
310
|
+
const identity = invalidationKey(key.session, key.generation);
|
|
311
|
+
if (emit) {
|
|
312
|
+
try {
|
|
313
|
+
emit("vector_cortex_router_generation_invalidated", {
|
|
314
|
+
session: key.session,
|
|
315
|
+
generation: key.generation.toString(),
|
|
316
|
+
key: identity,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
/* non-fatal observability — never break the agent loop */
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/** Registered TOP query conformance ID range (TOP-021..030). */
|
|
325
|
+
export const TOP_QUERY_IDS = [
|
|
326
|
+
"TOP-021",
|
|
327
|
+
"TOP-022",
|
|
328
|
+
"TOP-023",
|
|
329
|
+
"TOP-024",
|
|
330
|
+
"TOP-025",
|
|
331
|
+
"TOP-026",
|
|
332
|
+
"TOP-027",
|
|
333
|
+
"TOP-028",
|
|
334
|
+
"TOP-029",
|
|
335
|
+
"TOP-030",
|
|
336
|
+
];
|
|
337
|
+
/** Registered named TOP-query conformance IDs. */
|
|
338
|
+
export const TOP_QUERY_NAMED_IDS = [
|
|
339
|
+
"TOP-QUERY-003",
|
|
340
|
+
"M6-KEY-001",
|
|
341
|
+
"M6-STALE-002",
|
|
342
|
+
];
|