pi-mega-compact 0.20.0 → 0.20.2
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 +25 -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-reconstruct.js +33 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-residual.js +51 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex.js +2 -0
- package/dist/extensions/dashboard-server/routes.js +1 -1
- package/dist/extensions/dashboard-server/server.js +5 -1
- package/dist/src/config/vector-cortex.js +25 -0
- package/dist/src/config.js +1 -1
- package/dist/src/vector-cortex/reconstruct/_acceptance-helpers.js +365 -0
- package/dist/src/vector-cortex/reconstruct/assemble.js +137 -0
- package/dist/src/vector-cortex/reconstruct/closure.js +268 -0
- package/dist/src/vector-cortex/reconstruct/types.js +46 -0
- package/dist/src/vector-cortex/reconstruct/validate.js +152 -0
- package/dist/src/vector-cortex/residual/codec.js +224 -0
- package/dist/src/vector-cortex/residual/dct.js +158 -0
- package/dist/src/vector-cortex/residual/fixture-payload.js +77 -0
- package/dist/src/vector-cortex/residual/gf256.js +182 -0
- package/dist/src/vector-cortex/residual/parity.js +245 -0
- package/dist/src/vector-cortex/residual/quantize.js +200 -0
- package/dist/src/vector-cortex/residual/stream.js +124 -0
- package/dist/src/vector-cortex/residual/types.js +55 -0
- package/dist/vector-cortex/reconstruct/_acceptance-helpers.js +365 -0
- package/dist/vector-cortex/reconstruct/assemble.js +137 -0
- package/dist/vector-cortex/reconstruct/closure.js +268 -0
- package/dist/vector-cortex/reconstruct/types.js +46 -0
- package/dist/vector-cortex/reconstruct/validate.js +152 -0
- package/dist/vector-cortex/residual/codec.js +224 -0
- package/dist/vector-cortex/residual/dct.js +158 -0
- package/dist/vector-cortex/residual/fixture-payload.js +77 -0
- package/dist/vector-cortex/residual/gf256.js +182 -0
- package/dist/vector-cortex/residual/parity.js +245 -0
- package/dist/vector-cortex/residual/quantize.js +200 -0
- package/dist/vector-cortex/residual/stream.js +124 -0
- package/dist/vector-cortex/residual/types.js +55 -0
- package/extensions/dashboard-client/dist/assets/{AreaChart-n4z5w9Ox.js → AreaChart-XF3jPpvn.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AreaChart-n4z5w9Ox.js.map → AreaChart-XF3jPpvn.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{BarChart-BLGjWWBr.js → BarChart-DnYYn0GF.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{BarChart-BLGjWWBr.js.map → BarChart-DnYYn0GF.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-CAnrKKVK.js → CacheTab-D3bVskCL.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-CAnrKKVK.js.map → CacheTab-D3bVskCL.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-D-GL7Ngq.js → EventsTab-CX_8myfc.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-D-GL7Ngq.js.map → EventsTab-CX_8myfc.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-C4x5qQ8s.js → HealthTab-CnVzBQvN.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-C4x5qQ8s.js.map → HealthTab-CnVzBQvN.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-BSPs0eq5.js → MaintenanceTab-Cao--dtV.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-BSPs0eq5.js.map → MaintenanceTab-Cao--dtV.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-BWEUtTSu.js → MemoryMapTab-DZdI-9ig.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-BWEUtTSu.js.map → MemoryMapTab-DZdI-9ig.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-Dhm1c3Cx.js → MetricsTab-SlpSmNU4.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-Dhm1c3Cx.js.map → MetricsTab-SlpSmNU4.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-Cm6cySiQ.js → OverviewTab-C_-_4au9.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-Cm6cySiQ.js.map → OverviewTab-C_-_4au9.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-DxLN1N4K.js → ReposTab-CyWedWZC.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-DxLN1N4K.js.map → ReposTab-CyWedWZC.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-CZuI4ISW.js → SessionsTab-wqPBMIJZ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-CZuI4ISW.js.map → SessionsTab-wqPBMIJZ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SetupTab-C_0VOomm.js → SetupTab-MojXmuUf.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SetupTab-C_0VOomm.js.map → SetupTab-MojXmuUf.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-BhbaRLmi.js → TimeSavedCard-BxcplEOJ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-BhbaRLmi.js.map → TimeSavedCard-BxcplEOJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-CXJmbjje.js → TurnsTab-D8_qWbjY.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-CXJmbjje.js.map → TurnsTab-D8_qWbjY.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-B3yisAyB.js +2 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-B3yisAyB.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{WikiTab-C5uvMCzp.js → WikiTab-Cfd-QZYz.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{WikiTab-C5uvMCzp.js.map → WikiTab-Cfd-QZYz.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{button-Cl_fxJcg.js → button-tX5ht0bb.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{button-Cl_fxJcg.js.map → button-tX5ht0bb.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{card-BH8Le5YM.js → card-BT3Z5dDg.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{card-BH8Le5YM.js.map → card-BT3Z5dDg.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-sZ6ZNtBi.js → generateCategoricalChart-B6p2WnkY.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-sZ6ZNtBi.js.map → generateCategoricalChart-B6p2WnkY.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{index-CX9zv8cJ.js → index-CFLekP12.js} +3 -3
- package/extensions/dashboard-client/dist/assets/{index-CX9zv8cJ.js.map → index-CFLekP12.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{switch-CXJhcjhM.js → switch-B52oAMSw.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{switch-CXJhcjhM.js.map → switch-B52oAMSw.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{toggle-CoLtIc6y.js → toggle-VxxVc5Tp.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{toggle-CoLtIc6y.js.map → toggle-VxxVc5Tp.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-fSXho606.js → useSSE-DAVdZnG7.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-fSXho606.js.map → useSSE-DAVdZnG7.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 +37 -0
- package/extensions/dashboard-client/src/types/vector-cortex.ts +16 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex.ts +57 -0
- package/extensions/dashboard-server/routes-rag-settings-helpers.ts +12 -0
- package/extensions/dashboard-server/routes-vector-cortex-reconstruct.ts +41 -0
- package/extensions/dashboard-server/routes-vector-cortex-residual.ts +60 -0
- package/extensions/dashboard-server/routes-vector-cortex.ts +2 -0
- package/extensions/dashboard-server/routes.ts +2 -0
- package/extensions/dashboard-server/server.ts +4 -0
- package/package.json +1 -1
- package/src/config/vector-cortex.ts +27 -0
- package/src/config.ts +2 -0
- package/src/vector-cortex/reconstruct/_acceptance-helpers.ts +455 -0
- package/src/vector-cortex/reconstruct/assemble.ts +170 -0
- package/src/vector-cortex/reconstruct/closure.ts +299 -0
- package/src/vector-cortex/reconstruct/types.ts +330 -0
- package/src/vector-cortex/reconstruct/validate.ts +184 -0
- package/src/vector-cortex/residual/codec.ts +292 -0
- package/src/vector-cortex/residual/dct.ts +166 -0
- package/src/vector-cortex/residual/fixture-payload.ts +90 -0
- package/src/vector-cortex/residual/gf256.ts +196 -0
- package/src/vector-cortex/residual/parity.ts +283 -0
- package/src/vector-cortex/residual/quantize.ts +230 -0
- package/src/vector-cortex/residual/stream.ts +135 -0
- package/src/vector-cortex/residual/types.ts +236 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-BGe9hu3r.js +0 -2
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-BGe9hu3r.js.map +0 -1
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/reconstruct/closure.ts — mandatory conservative closure (VC4C).
|
|
3
|
+
*
|
|
4
|
+
* Task 2/3: a WORKLIST recursion that repeatedly adds every `depends`
|
|
5
|
+
* predecessor and every whole tool pair of an already-selected node until the
|
|
6
|
+
* selection stops growing (a FIXED POINT), tracking visited ids so a cyclic
|
|
7
|
+
* graph terminates instead of recursing forever. Task 3: contradictions are
|
|
8
|
+
* resolved by retaining the LATER exact source resolution; an explicit
|
|
9
|
+
* resolution event may name the loser directly; equal/unordered resolutions are
|
|
10
|
+
* an unresolved tie and return `CLO_CONTRADICTION_UNRESOLVED` so the candidate
|
|
11
|
+
* never goes live (CONTRACTS §plan and closure).
|
|
12
|
+
*
|
|
13
|
+
* Determinism: the worklist is drained in sorted id order and every returned
|
|
14
|
+
* array is sorted, so the closed set is identical regardless of the order seeds
|
|
15
|
+
* or edges were supplied in. Time is never read here — contradiction ordering
|
|
16
|
+
* uses the SOURCE `resolvedAtMs` fact carried on the node.
|
|
17
|
+
*
|
|
18
|
+
* OWNERSHIP: `mandatoryTokenEstimate` is CONTENT ONLY (no prompt framing) and is
|
|
19
|
+
* handed unchanged to VC5A, which owns framing + budget admission and returns
|
|
20
|
+
* `MANDATORY_CLOSURE_OVER_BUDGET` on overflow. Closure NEVER truncates a
|
|
21
|
+
* mandatory node.
|
|
22
|
+
*
|
|
23
|
+
* Pure/deterministic: no storage, no console, no network (PREVENT-PI-004 /
|
|
24
|
+
* PREVENT-011).
|
|
25
|
+
*/
|
|
26
|
+
/** Sorted copy (bytewise id order) — every closure output is order-stable. */
|
|
27
|
+
function sortedIds(ids) {
|
|
28
|
+
return [...ids].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
29
|
+
}
|
|
30
|
+
/** Index the graph's nodes by id (last wins is impossible — ids are unique). */
|
|
31
|
+
function indexNodes(graph) {
|
|
32
|
+
const byId = new Map();
|
|
33
|
+
for (const n of graph.nodes)
|
|
34
|
+
byId.set(n.id, n);
|
|
35
|
+
return byId;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Build the "requirement" adjacency: for a selected node `x`, which nodes must
|
|
39
|
+
* ALSO be selected. Edges point prerequisite `from` → dependent `to`, so:
|
|
40
|
+
*
|
|
41
|
+
* - `depends` — selecting `to` requires `from` (walk BACKWARD to the
|
|
42
|
+
* prerequisite; this is the transitive dependency closure);
|
|
43
|
+
* - `tool-pair` — the pair is atomic, so selecting EITHER endpoint requires
|
|
44
|
+
* the other (walk BOTH directions — a pair is never split,
|
|
45
|
+
* PREVENT-PI-002).
|
|
46
|
+
*
|
|
47
|
+
* `contradicts` is deliberately absent: a contradiction never PULLS a node in,
|
|
48
|
+
* it removes one (resolved separately below).
|
|
49
|
+
*/
|
|
50
|
+
function buildRequirements(edges) {
|
|
51
|
+
const req = new Map();
|
|
52
|
+
const add = (key, id, rule) => {
|
|
53
|
+
const list = req.get(key);
|
|
54
|
+
if (list === undefined)
|
|
55
|
+
req.set(key, [{ id, rule }]);
|
|
56
|
+
else
|
|
57
|
+
list.push({ id, rule });
|
|
58
|
+
};
|
|
59
|
+
for (const e of edges) {
|
|
60
|
+
if (e.kind === "depends") {
|
|
61
|
+
add(e.to, e.from, "depends");
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (e.kind === "tool-pair") {
|
|
65
|
+
add(e.to, e.from, "tool-pair");
|
|
66
|
+
add(e.from, e.to, "tool-pair");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return req;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Resolve one contradiction pair. Returns the loser id, or `null` when the pair
|
|
73
|
+
* is an unresolved tie.
|
|
74
|
+
*
|
|
75
|
+
* Precedence (CONTRACTS §plan and closure):
|
|
76
|
+
* 1. An explicit resolution event naming the loser wins outright.
|
|
77
|
+
* 2. Otherwise the LATER exact source resolution supersedes the earlier claim.
|
|
78
|
+
* "Exact" is required: a semantic node never supersedes an exact one, since
|
|
79
|
+
* semantic vectors never claim to recover exact text (RESIDUAL_CODEC).
|
|
80
|
+
* 3. Equal timestamps, missing timestamps, or two non-exact claims are
|
|
81
|
+
* UNORDERED — keep both and reject live use.
|
|
82
|
+
*/
|
|
83
|
+
function resolveContradiction(a, b, explicit) {
|
|
84
|
+
// 1. Explicit resolution event names the loser directly.
|
|
85
|
+
const aLoses = explicit.get(a.id);
|
|
86
|
+
if (aLoses === b.id)
|
|
87
|
+
return a.id;
|
|
88
|
+
const bLoses = explicit.get(b.id);
|
|
89
|
+
if (bLoses === a.id)
|
|
90
|
+
return b.id;
|
|
91
|
+
// 2. Later EXACT source resolution supersedes the earlier claim. Both sides
|
|
92
|
+
// must be exact and both must carry a source resolution time.
|
|
93
|
+
const aExact = a.kind === "exact";
|
|
94
|
+
const bExact = b.kind === "exact";
|
|
95
|
+
const aAt = a.resolvedAtMs;
|
|
96
|
+
const bAt = b.resolvedAtMs;
|
|
97
|
+
if (aExact && bExact && aAt !== undefined && bAt !== undefined && aAt !== bAt) {
|
|
98
|
+
return aAt < bAt ? a.id : b.id;
|
|
99
|
+
}
|
|
100
|
+
// 3. Equal / unordered / non-exact — unresolved tie.
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Close a selection conservatively to a fixed point (task 2 + 3).
|
|
105
|
+
*
|
|
106
|
+
* Algorithm: seed the worklist with the requested ids (plus the anchor floor,
|
|
107
|
+
* which is always mandatory), then repeatedly pop the smallest id and add every
|
|
108
|
+
* node it requires. A `visited` set makes each node expand exactly once, so a
|
|
109
|
+
* cycle (`a depends b`, `b depends a`) terminates at the fixed point rather than
|
|
110
|
+
* recursing forever. Contradictions among the CLOSED set are then resolved; an
|
|
111
|
+
* unresolved tie fails the closure.
|
|
112
|
+
*/
|
|
113
|
+
export function closeSelection(input) {
|
|
114
|
+
const { graph, seeds } = input;
|
|
115
|
+
const byId = indexNodes(graph);
|
|
116
|
+
const failures = [];
|
|
117
|
+
// Structural validation: every edge endpoint and every seed must exist.
|
|
118
|
+
for (const e of graph.edges) {
|
|
119
|
+
if (!byId.has(e.from) || !byId.has(e.to)) {
|
|
120
|
+
if (!failures.includes("CLO_MISSING_NODE"))
|
|
121
|
+
failures.push("CLO_MISSING_NODE");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
for (const s of seeds) {
|
|
125
|
+
if (!byId.has(s)) {
|
|
126
|
+
if (!failures.includes("CLO_UNKNOWN_SEED"))
|
|
127
|
+
failures.push("CLO_UNKNOWN_SEED");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (failures.length > 0) {
|
|
131
|
+
return {
|
|
132
|
+
ok: false,
|
|
133
|
+
selected: [],
|
|
134
|
+
addedDependencies: [],
|
|
135
|
+
removedContradictions: [],
|
|
136
|
+
unresolved: [],
|
|
137
|
+
proof: [],
|
|
138
|
+
failures,
|
|
139
|
+
mandatoryTokenEstimate: 0,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
const requirements = buildRequirements(graph.edges);
|
|
143
|
+
const selected = new Set();
|
|
144
|
+
const proof = [];
|
|
145
|
+
const seedSet = new Set(seeds);
|
|
146
|
+
// The anchor floor is ALWAYS mandatory: an anchor is never dropped by closure
|
|
147
|
+
// (PREVENT-PI-001's anchor-floor discipline restated for the closed prompt).
|
|
148
|
+
const anchorIds = graph.nodes.filter((n) => n.anchor === true).map((n) => n.id);
|
|
149
|
+
// Seed the worklist deterministically (sorted), anchors after explicit seeds
|
|
150
|
+
// so the proof reads "what was asked for, then what the floor forced".
|
|
151
|
+
const worklist = [];
|
|
152
|
+
for (const id of sortedIds(seedSet)) {
|
|
153
|
+
if (selected.has(id))
|
|
154
|
+
continue;
|
|
155
|
+
selected.add(id);
|
|
156
|
+
proof.push({ added: id, rule: "seed" });
|
|
157
|
+
worklist.push(id);
|
|
158
|
+
}
|
|
159
|
+
for (const id of sortedIds(anchorIds)) {
|
|
160
|
+
if (selected.has(id))
|
|
161
|
+
continue;
|
|
162
|
+
selected.add(id);
|
|
163
|
+
proof.push({ added: id, rule: "anchor-floor" });
|
|
164
|
+
worklist.push(id);
|
|
165
|
+
}
|
|
166
|
+
// ── Worklist recursion to a FIXED POINT (visited = `selected`) ─────────────
|
|
167
|
+
// Each id is expanded at most once because it can only enter the worklist at
|
|
168
|
+
// the moment it is inserted into `selected`. A cyclic graph therefore
|
|
169
|
+
// terminates: the second traversal of the cycle finds every node already
|
|
170
|
+
// selected and adds nothing.
|
|
171
|
+
while (worklist.length > 0) {
|
|
172
|
+
// Drain in sorted order so the proof is deterministic across input orders.
|
|
173
|
+
worklist.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
174
|
+
const current = worklist.shift();
|
|
175
|
+
if (current === undefined)
|
|
176
|
+
break;
|
|
177
|
+
const required = requirements.get(current) ?? [];
|
|
178
|
+
// Sort the requirements of one node so sibling additions are ordered too.
|
|
179
|
+
const ordered = [...required].sort((x, y) => x.id < y.id ? -1 : x.id > y.id ? 1 : x.rule < y.rule ? -1 : x.rule > y.rule ? 1 : 0);
|
|
180
|
+
for (const { id, rule } of ordered) {
|
|
181
|
+
if (selected.has(id))
|
|
182
|
+
continue; // already at the fixed point for this node
|
|
183
|
+
selected.add(id);
|
|
184
|
+
proof.push({ added: id, requiredBy: current, rule });
|
|
185
|
+
worklist.push(id);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// ── Contradiction resolution over the CLOSED set (task 3) ─────────────────
|
|
189
|
+
const explicit = new Map();
|
|
190
|
+
for (const r of graph.resolutions ?? [])
|
|
191
|
+
explicit.set(r.loserId, r.winnerId);
|
|
192
|
+
const removed = new Set();
|
|
193
|
+
const unresolved = new Set();
|
|
194
|
+
for (const e of graph.edges) {
|
|
195
|
+
if (e.kind !== "contradicts")
|
|
196
|
+
continue;
|
|
197
|
+
// Only contradictions BETWEEN TWO SELECTED nodes matter: an unselected claim
|
|
198
|
+
// cannot contradict the prompt that is actually being built.
|
|
199
|
+
if (!selected.has(e.from) || !selected.has(e.to))
|
|
200
|
+
continue;
|
|
201
|
+
const a = byId.get(e.from);
|
|
202
|
+
const b = byId.get(e.to);
|
|
203
|
+
if (a === undefined || b === undefined)
|
|
204
|
+
continue;
|
|
205
|
+
// A pair already decided by an earlier edge (duplicate contradiction) is
|
|
206
|
+
// skipped so one resolution is not re-litigated into a false tie.
|
|
207
|
+
if (removed.has(a.id) || removed.has(b.id))
|
|
208
|
+
continue;
|
|
209
|
+
const loser = resolveContradiction(a, b, explicit);
|
|
210
|
+
if (loser === null) {
|
|
211
|
+
unresolved.add(a.id);
|
|
212
|
+
unresolved.add(b.id);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
removed.add(loser);
|
|
216
|
+
}
|
|
217
|
+
// A node that lost a contradiction leaves the selection. An unresolved tie
|
|
218
|
+
// keeps BOTH (CONTRACTS: "ties keep both and reject live use") and fails.
|
|
219
|
+
for (const id of removed)
|
|
220
|
+
selected.delete(id);
|
|
221
|
+
const selectedIds = sortedIds(selected);
|
|
222
|
+
const addedDependencies = selectedIds.filter((id) => !seedSet.has(id));
|
|
223
|
+
const mandatoryTokenEstimate = selectedIds.reduce((sum, id) => sum + (byId.get(id)?.tokenEstimate ?? 0), 0);
|
|
224
|
+
const ok = unresolved.size === 0;
|
|
225
|
+
return {
|
|
226
|
+
ok,
|
|
227
|
+
selected: selectedIds,
|
|
228
|
+
addedDependencies,
|
|
229
|
+
removedContradictions: sortedIds(removed),
|
|
230
|
+
unresolved: sortedIds(unresolved),
|
|
231
|
+
proof,
|
|
232
|
+
failures: ok ? [] : ["CLO_CONTRADICTION_UNRESOLVED"],
|
|
233
|
+
mandatoryTokenEstimate,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Whether a closure result is a genuine fixed point: re-running the closure over
|
|
238
|
+
* its own output adds nothing. Used by the acceptance invariant ("closure reaches
|
|
239
|
+
* a fixed point") — an independent CHECK rather than a restatement of the loop.
|
|
240
|
+
*/
|
|
241
|
+
export function isFixedPoint(graph, result) {
|
|
242
|
+
if (!result.ok)
|
|
243
|
+
return true; // a rejected closure is not required to be closed
|
|
244
|
+
const again = closeSelection({ graph, seeds: result.selected });
|
|
245
|
+
if (!again.ok)
|
|
246
|
+
return false;
|
|
247
|
+
return (again.selected.length === result.selected.length &&
|
|
248
|
+
again.selected.every((id, i) => id === result.selected[i]));
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Mode B: a GREEDY EXACT-ONLY closure, forced when semantic validation fails.
|
|
252
|
+
* Independent of mode A by construction — it consults no semantic node and no
|
|
253
|
+
* semantic index, deriving purely from the exact/event source tiers
|
|
254
|
+
* (TRIAD_RESILIENCE: A/B must not share the same algorithm or index).
|
|
255
|
+
*/
|
|
256
|
+
export function closeExactOnly(input) {
|
|
257
|
+
const exactGraph = {
|
|
258
|
+
sessionId: input.graph.sessionId,
|
|
259
|
+
nodes: input.graph.nodes.filter((n) => n.kind !== "semantic"),
|
|
260
|
+
edges: input.graph.edges.filter((e) => {
|
|
261
|
+
const kinds = new Map(input.graph.nodes.map((n) => [n.id, n.kind]));
|
|
262
|
+
return kinds.get(e.from) !== "semantic" && kinds.get(e.to) !== "semantic";
|
|
263
|
+
}),
|
|
264
|
+
resolutions: input.graph.resolutions,
|
|
265
|
+
};
|
|
266
|
+
const seeds = input.seeds.filter((id) => exactGraph.nodes.some((n) => n.id === id));
|
|
267
|
+
return closeSelection({ graph: exactGraph, seeds });
|
|
268
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/reconstruct/types.ts — conservative closure + reconstruction
|
|
3
|
+
* fidelity contract (VC4C).
|
|
4
|
+
*
|
|
5
|
+
* Owns `ClosureResult` / `ReconstructionV1` — the contract of the sprint failure
|
|
6
|
+
* triad:
|
|
7
|
+
*
|
|
8
|
+
* A = closed semantic + exact/residual reconstruction (the normal path);
|
|
9
|
+
* B = greedy EXACT-only closure, forced when semantic validation fails;
|
|
10
|
+
* C = legacy prompt, forced by an unresolved contradiction (states its loss of
|
|
11
|
+
* old semantic context — TRIAD_RESILIENCE: C is continuity, NOT semantic
|
|
12
|
+
* completeness).
|
|
13
|
+
*
|
|
14
|
+
* Closure is MANDATORY before VC5 (CONTRACTS §plan and closure): recursively add
|
|
15
|
+
* every `depends`/tool-pair predecessor until a fixed point; for contradictions
|
|
16
|
+
* keep the LATER exact source resolution unless an explicit resolution event
|
|
17
|
+
* names the loser; ties keep both and reject live use
|
|
18
|
+
* (`CLO_CONTRADICTION_UNRESOLVED`). The anchor floor is preserved, and the closed
|
|
19
|
+
* mandatory node set is returned with its deterministic token estimate.
|
|
20
|
+
*
|
|
21
|
+
* OWNERSHIP BOUNDARY (CONTRACTS §plan and closure): `mandatoryTokenEstimate`
|
|
22
|
+
* counts CONTENT ONLY — no prompt framing, no role tags, no separators, no
|
|
23
|
+
* budget admission. VC5A exclusively owns framing/budget: it adds framing cost
|
|
24
|
+
* to this estimate and returns `MANDATORY_CLOSURE_OVER_BUDGET` if the mandatory
|
|
25
|
+
* cost exceeds its `tokenBudget`. VC4C therefore NEVER truncates a mandatory
|
|
26
|
+
* node and never reasons about a budget.
|
|
27
|
+
*
|
|
28
|
+
* Consumes only reviewer-accepted predecessor contracts (VC1A `EventV2` byte
|
|
29
|
+
* authority, VC4A `ShardRange`/`ExactShardV1`, VC4B `ParityShardV1` residual
|
|
30
|
+
* decode) and the common contracts. Pure types + registered conformance IDs: no
|
|
31
|
+
* storage, no console, no network (PREVENT-PI-004 / PREVENT-011).
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* Registered CLO conformance ID range (CLO-001..030). The acceptance test reads
|
|
35
|
+
* these rows from the v2 manifest and asserts each returns its manifest
|
|
36
|
+
* `ok`/`code`.
|
|
37
|
+
*/
|
|
38
|
+
export const CLO_IDS = Array.from({ length: 30 }, (_v, i) => `CLO-${String(i + 1).padStart(3, "0")}`);
|
|
39
|
+
/** Registered REC conformance ID range (REC-001..030). */
|
|
40
|
+
export const REC_IDS = Array.from({ length: 30 }, (_v, i) => `REC-${String(i + 1).padStart(3, "0")}`);
|
|
41
|
+
/** Named VC4C conformance assertions (the sprint's headline rows). */
|
|
42
|
+
export const RECONSTRUCT_NAMED_IDS = [
|
|
43
|
+
"CLO-TRANSITIVE-001",
|
|
44
|
+
"CLO-CONTRA-002",
|
|
45
|
+
"REC-ORDER-003",
|
|
46
|
+
];
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/reconstruct/validate.ts — reconstruction validation (VC4C).
|
|
3
|
+
*
|
|
4
|
+
* Task 5: a validator that rejects a closed selection that would corrupt a live
|
|
5
|
+
* prompt and emits exactly two events:
|
|
6
|
+
* - `vector_cortex_reconstruction_validated` (the good outcome)
|
|
7
|
+
* - `vector_cortex_closure_rejected` (a rejected/cancelled closure)
|
|
8
|
+
*
|
|
9
|
+
* Rejection reasons (the failure codes are the ONLY externally-visible detail):
|
|
10
|
+
* - REC_ANCHOR_MISSING an anchor-floor node is absent from the closure
|
|
11
|
+
* - REC_TOOL_PAIR_SPLIT a tool-pair is not contiguous (PREVENT-PI-002)
|
|
12
|
+
* - REC_DIGEST_MISMATCH a decoded shard's bytes disagree with its digest
|
|
13
|
+
* - REC_CONTRADICTION_UNRESOLVED an unresolved tie (closure already failed)
|
|
14
|
+
* - REC_SOURCE_UNAVAILABLE a required shard is missing/erased (failure inj.)
|
|
15
|
+
* - REC_SPAN_OVERLAP two spans intersect in source byte space
|
|
16
|
+
*
|
|
17
|
+
* The validator NEVER emits content; it exposes only a `ReconstructionSummary`
|
|
18
|
+
* (counts + digest + token estimate) or the failure `codes`. Assembly/closure
|
|
19
|
+
* errors are surfaced but the validator is the authoritative last gate.
|
|
20
|
+
*
|
|
21
|
+
* OWNERSHIP: this is a PURE function over its inputs — no storage, no network,
|
|
22
|
+
* no console. The dashboard/seams that consume it live in extensions/, which may
|
|
23
|
+
* emit observability; this module stays silent (structured logging contract).
|
|
24
|
+
*/
|
|
25
|
+
import { createHash } from "node:crypto";
|
|
26
|
+
import { assembleSourceOrder } from "./assemble.js";
|
|
27
|
+
/** Flag-gated reporter — mirrors the VC4B residual reporter pattern. */
|
|
28
|
+
export function createReconstructReporter(emit) {
|
|
29
|
+
const fire = (event, fields) => {
|
|
30
|
+
if (!emit)
|
|
31
|
+
return;
|
|
32
|
+
try {
|
|
33
|
+
emit(event, fields);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
/* non-fatal observability — never break the agent loop */
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
reconstructionValidated: (fields) => fire("vector_cortex_reconstruction_validated", fields),
|
|
41
|
+
closureRejected: (fields) => fire("vector_cortex_closure_rejected", fields),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** Sync SHA-256 hex (validator is sync except for assembly's concat digest). */
|
|
45
|
+
function sha256HexSync(bytes) {
|
|
46
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
47
|
+
}
|
|
48
|
+
/** Verify each decoded shard's bytes hash to its declared digest (REC_DIGEST_MISMATCH). */
|
|
49
|
+
function findDigestMismatch(shards) {
|
|
50
|
+
for (const s of shards) {
|
|
51
|
+
// A pinned digest of "0" is a placeholder: the source tier did not compute a
|
|
52
|
+
// per-shard digest, so the only guarantee is the post-assembly concatenation
|
|
53
|
+
// digest. Any other digest is a real pin that must match exactly.
|
|
54
|
+
if (s.digest === "0")
|
|
55
|
+
continue;
|
|
56
|
+
const computed = sha256HexSync(s.bytes);
|
|
57
|
+
if (computed !== s.digest)
|
|
58
|
+
return "REC_DIGEST_MISMATCH";
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Validate a closed selection and, on success, assemble it into source order.
|
|
64
|
+
* Returns the discriminated `ReconstructionValidation`: ok -> summary only;
|
|
65
|
+
* fail -> failure codes only. Emits exactly one of the two events.
|
|
66
|
+
*/
|
|
67
|
+
export async function validateAndAssemble(input) {
|
|
68
|
+
const { graph, closure, nodes, edges, shards, emit } = input;
|
|
69
|
+
const reporter = createReconstructReporter(emit);
|
|
70
|
+
// 1. A closure that failed its own contradiction resolution cannot go live.
|
|
71
|
+
if (!closure.ok) {
|
|
72
|
+
const codes = ["REC_CONTRADICTION_UNRESOLVED"];
|
|
73
|
+
reporter.closureRejected({ sessionId: graph.sessionId, codes, reason: "closure-not-ok" });
|
|
74
|
+
return {
|
|
75
|
+
validation: { ok: false, codes },
|
|
76
|
+
reconstruction: null,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// 2. Anchor-floor discipline (PREVENT-PI-001 restated): every anchor node must
|
|
80
|
+
// be present in the closed set, or the closure silently dropped a floor
|
|
81
|
+
// item and must be rejected.
|
|
82
|
+
const anchorMissing = nodes.some((n) => n.anchor === true && !closure.selected.includes(n.id));
|
|
83
|
+
if (anchorMissing) {
|
|
84
|
+
reporter.closureRejected({ sessionId: graph.sessionId, codes: ["REC_ANCHOR_MISSING"], reason: "anchor-missing" });
|
|
85
|
+
return {
|
|
86
|
+
validation: { ok: false, codes: ["REC_ANCHOR_MISSING"] },
|
|
87
|
+
reconstruction: null,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// 3. Unresolved contradiction present in the closed set.
|
|
91
|
+
if (closure.unresolved.length > 0) {
|
|
92
|
+
const codes = ["REC_CONTRADICTION_UNRESOLVED"];
|
|
93
|
+
reporter.closureRejected({ sessionId: graph.sessionId, codes, reason: "unresolved-contradiction" });
|
|
94
|
+
return {
|
|
95
|
+
validation: { ok: false, codes },
|
|
96
|
+
reconstruction: null,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// 4. Per-shard digest pre-check hook (REC_DIGEST_MISMATCH path preserved).
|
|
100
|
+
const digestCode = findDigestMismatch(shards);
|
|
101
|
+
if (digestCode !== null) {
|
|
102
|
+
reporter.closureRejected({ sessionId: graph.sessionId, codes: [digestCode], reason: "digest-mismatch" });
|
|
103
|
+
return {
|
|
104
|
+
validation: { ok: false, codes: [digestCode] },
|
|
105
|
+
reconstruction: null,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
// 5. Assembly: missing source (REC_SOURCE_UNAVAILABLE) / split pair
|
|
109
|
+
// (REC_TOOL_PAIR_SPLIT) / overlap (REC_SPAN_OVERLAP) all surface here.
|
|
110
|
+
const assembled = await assembleSourceOrder({
|
|
111
|
+
sessionId: graph.sessionId,
|
|
112
|
+
selected: closure.selected,
|
|
113
|
+
nodes,
|
|
114
|
+
edges,
|
|
115
|
+
shards,
|
|
116
|
+
mandatoryTokenEstimate: closure.mandatoryTokenEstimate,
|
|
117
|
+
});
|
|
118
|
+
if (assembled.code !== null || assembled.reconstruction === null) {
|
|
119
|
+
const code = assembled.code ?? "REC_DIGEST_MISMATCH";
|
|
120
|
+
reporter.closureRejected({ sessionId: graph.sessionId, codes: [code], reason: "assembly-failed" });
|
|
121
|
+
return {
|
|
122
|
+
validation: { ok: false, codes: [code] },
|
|
123
|
+
reconstruction: null,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const rec = assembled.reconstruction;
|
|
127
|
+
const summary = buildSummary(rec);
|
|
128
|
+
reporter.reconstructionValidated({
|
|
129
|
+
sessionId: rec.sessionId,
|
|
130
|
+
spanCount: summary.spanCount,
|
|
131
|
+
protectedSpanCount: summary.protectedSpanCount,
|
|
132
|
+
byteTotal: summary.byteTotal,
|
|
133
|
+
mandatoryTokenEstimate: summary.mandatoryTokenEstimate,
|
|
134
|
+
digest: summary.digest,
|
|
135
|
+
bySource: summary.bySource,
|
|
136
|
+
});
|
|
137
|
+
return { validation: { ok: true, summary }, reconstruction: rec };
|
|
138
|
+
}
|
|
139
|
+
function buildSummary(rec) {
|
|
140
|
+
const exact = rec.spans.filter((s) => s.source === "exact").length;
|
|
141
|
+
const residual = rec.spans.filter((s) => s.source === "residual").length;
|
|
142
|
+
const semantic = rec.spans.filter((s) => s.source === "semantic").length;
|
|
143
|
+
return {
|
|
144
|
+
sessionId: rec.sessionId,
|
|
145
|
+
spanCount: rec.spans.length,
|
|
146
|
+
protectedSpanCount: rec.spans.filter((s) => s.protectedSpan).length,
|
|
147
|
+
byteTotal: rec.byteTotal,
|
|
148
|
+
mandatoryTokenEstimate: rec.mandatoryTokenEstimate,
|
|
149
|
+
digest: rec.digest,
|
|
150
|
+
bySource: { exact, residual, semantic },
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/residual/codec.ts — reversible residual payload codec (VC4B).
|
|
3
|
+
*
|
|
4
|
+
* Encode: split `EventV2.originalBytes` into 4096-byte blocks (zero-padding only
|
|
5
|
+
* the final block), map bytes to `x=(byte-127.5)/127.5`, take the orthonormal
|
|
6
|
+
* DCT-II, quantize to int16 with a per-block float32 scale, reconstruct, and
|
|
7
|
+
* append a block-scoped EXACT correction stream wherever the reconstruction
|
|
8
|
+
* differs. Post-decode byte error for an admitted artifact is therefore exactly
|
|
9
|
+
* ZERO — the digest is verified before admission, never assumed.
|
|
10
|
+
*
|
|
11
|
+
* Admission (RESIDUAL_CODEC §admission): residual is admitted only when the FULL
|
|
12
|
+
* encoded size — header, scales, coefficients, corrections, shard index/length
|
|
13
|
+
* metadata, all 9 shards and their digests — is `<= floor(0.95 *
|
|
14
|
+
* exactCompressedSize)` AND the complete decode + digest check succeeds.
|
|
15
|
+
* Otherwise the caller stores the exact compressed payload (mode B). Coefficient
|
|
16
|
+
* bytes alone are never compared.
|
|
17
|
+
*
|
|
18
|
+
* Emits `vector_cortex_residual_admitted` / `vector_cortex_parity_recovery_failed`
|
|
19
|
+
* through the flag-gated reporter and exposes AGGREGATE-ONLY residual metrics
|
|
20
|
+
* (counts/byte totals — never payload).
|
|
21
|
+
*
|
|
22
|
+
* Guardrails: local hashing only, no storage, no console, no network
|
|
23
|
+
* (PREVENT-PI-004 / PREVENT-011).
|
|
24
|
+
*/
|
|
25
|
+
import { createHash } from "node:crypto";
|
|
26
|
+
import { VC4B_ENABLED } from "../../config/vector-cortex.js";
|
|
27
|
+
import { bytesToSignal, forwardDct, inverseDct, signalToBytes, splitBlocks, } from "./dct.js";
|
|
28
|
+
import { applyCorrections, dequantizeBlock, diffBlock, quantizeBlock, } from "./quantize.js";
|
|
29
|
+
import { encodeShards, recoverStream, sha256Hex } from "./parity.js";
|
|
30
|
+
import { parseStream, serializeStream } from "./stream.js";
|
|
31
|
+
import { ADMISSION_DENOMINATOR, ADMISSION_NUMERATOR, RESIDUAL_BLOCK_SIZE, RESIDUAL_MAGIC, RS_DATA_SHARDS, RS_PARITY_SHARDS, } from "./types.js";
|
|
32
|
+
/** Per-shard persisted metadata: u8 index + u32 LE length + 32-byte digest. */
|
|
33
|
+
const SHARD_METADATA_BYTES = 1 + 4 + 32;
|
|
34
|
+
/**
|
|
35
|
+
* The inclusive admission ceiling `floor(0.95 * exactCompressedSize)`, computed
|
|
36
|
+
* in integer arithmetic so the boundary is exact (a fractional 0.95 multiply
|
|
37
|
+
* would make the "one byte above rejects" case depend on float rounding).
|
|
38
|
+
*/
|
|
39
|
+
export function admissionCeiling(exactCompressedSize) {
|
|
40
|
+
return Math.floor((exactCompressedSize * ADMISSION_NUMERATOR) / ADMISSION_DENOMINATOR);
|
|
41
|
+
}
|
|
42
|
+
/** Total persisted bytes of the shard set (payload + per-shard metadata). */
|
|
43
|
+
export function shardSetBytes(shards) {
|
|
44
|
+
return shards.reduce((n, s) => n + s.bytes.length + SHARD_METADATA_BYTES, 0);
|
|
45
|
+
}
|
|
46
|
+
/** Build the codec artifact (transform + quantize + exact corrections). */
|
|
47
|
+
export function buildArtifact(payload) {
|
|
48
|
+
const digest = sha256Hex(payload);
|
|
49
|
+
const rawBlocks = splitBlocks(payload, RESIDUAL_BLOCK_SIZE);
|
|
50
|
+
const blocks = [];
|
|
51
|
+
const corrections = [];
|
|
52
|
+
for (let b = 0; b < rawBlocks.length; b++) {
|
|
53
|
+
const original = rawBlocks[b];
|
|
54
|
+
const quantized = quantizeBlock(forwardDct(bytesToSignal(original)));
|
|
55
|
+
if (!quantized.ok)
|
|
56
|
+
return { ok: false, code: "RES_QUANTIZE_RANGE" };
|
|
57
|
+
blocks.push(quantized.block);
|
|
58
|
+
// Reconstruct and diff: any residual byte error becomes an exact correction.
|
|
59
|
+
const reconstructed = signalToBytes(inverseDct(dequantizeBlock(quantized.block)));
|
|
60
|
+
const diff = diffBlock(original, reconstructed);
|
|
61
|
+
if (diff.length > 0)
|
|
62
|
+
corrections.push({ blockIndex: b, corrections: diff });
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
ok: true,
|
|
66
|
+
codec: {
|
|
67
|
+
schema: "residual-codec-v1",
|
|
68
|
+
header: {
|
|
69
|
+
magic: RESIDUAL_MAGIC,
|
|
70
|
+
originalLength: payload.length,
|
|
71
|
+
payloadDigest: digest,
|
|
72
|
+
blockSize: RESIDUAL_BLOCK_SIZE,
|
|
73
|
+
dataShards: RS_DATA_SHARDS,
|
|
74
|
+
parityShards: RS_PARITY_SHARDS,
|
|
75
|
+
},
|
|
76
|
+
blocks,
|
|
77
|
+
corrections,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Encode a payload and decide admission against the competing exact compressed
|
|
83
|
+
* size. Admission requires BOTH the <=95% byte accounting AND a full decode whose
|
|
84
|
+
* digest matches the original payload.
|
|
85
|
+
*/
|
|
86
|
+
export function encodeResidual(payload, exactCompressedSize, emit) {
|
|
87
|
+
const built = buildArtifact(payload);
|
|
88
|
+
if (!built.ok)
|
|
89
|
+
return { ok: false, code: built.code };
|
|
90
|
+
const codec = built.codec;
|
|
91
|
+
const stream = serializeStream(codec);
|
|
92
|
+
const shards = encodeShards(stream);
|
|
93
|
+
const encodedSize = stream.length + shardSetBytes(shards);
|
|
94
|
+
const correctionCount = codec.corrections.reduce((n, b) => n + b.corrections.length, 0);
|
|
95
|
+
const accounting = {
|
|
96
|
+
encodedSize,
|
|
97
|
+
exactCompressedSize,
|
|
98
|
+
admissionCeiling: admissionCeiling(exactCompressedSize),
|
|
99
|
+
correctionCount,
|
|
100
|
+
blockCount: codec.blocks.length,
|
|
101
|
+
};
|
|
102
|
+
const reporter = createResidualReporter(emit);
|
|
103
|
+
if (encodedSize > accounting.admissionCeiling) {
|
|
104
|
+
return { ok: true, admitted: false, code: "RES_NOT_ADMITTED", accounting };
|
|
105
|
+
}
|
|
106
|
+
// Never admit without proving the full decode round-trips to the exact bytes.
|
|
107
|
+
const verified = decodeResidual(shards, emit);
|
|
108
|
+
if (!verified.ok)
|
|
109
|
+
return { ok: false, code: verified.code };
|
|
110
|
+
if (sha256Hex(verified.bytes) !== codec.header.payloadDigest) {
|
|
111
|
+
return { ok: false, code: "RES_PAYLOAD_DIGEST_MISMATCH" };
|
|
112
|
+
}
|
|
113
|
+
reporter.residualAdmitted({
|
|
114
|
+
encodedSize,
|
|
115
|
+
exactCompressedSize,
|
|
116
|
+
admissionCeiling: accounting.admissionCeiling,
|
|
117
|
+
blockCount: accounting.blockCount,
|
|
118
|
+
correctionCount,
|
|
119
|
+
});
|
|
120
|
+
return { ok: true, admitted: true, codec, shards, accounting };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Decode from a (possibly partial / partially corrupt) shard set: recover the
|
|
124
|
+
* protected stream, invert the transform, apply the exact corrections, truncate
|
|
125
|
+
* to the original length, and verify the payload digest.
|
|
126
|
+
*/
|
|
127
|
+
export function decodeResidual(shards, emit) {
|
|
128
|
+
const reporter = createResidualReporter(emit);
|
|
129
|
+
const recovered = recoverStream(shards);
|
|
130
|
+
if (!recovered.ok) {
|
|
131
|
+
reporter.parityRecoveryFailed({
|
|
132
|
+
code: recovered.code,
|
|
133
|
+
shardCount: shards.length,
|
|
134
|
+
});
|
|
135
|
+
return { ok: false, code: recovered.code };
|
|
136
|
+
}
|
|
137
|
+
const codec = parseStream(recovered.stream);
|
|
138
|
+
if (!codec) {
|
|
139
|
+
reporter.parityRecoveryFailed({
|
|
140
|
+
code: "RES_HEADER_INVALID",
|
|
141
|
+
shardCount: shards.length,
|
|
142
|
+
});
|
|
143
|
+
return { ok: false, code: "RES_HEADER_INVALID" };
|
|
144
|
+
}
|
|
145
|
+
return decodeArtifact(codec);
|
|
146
|
+
}
|
|
147
|
+
/** Decode a parsed artifact directly (no parity layer). */
|
|
148
|
+
export function decodeArtifact(codec) {
|
|
149
|
+
const blockSize = codec.header.blockSize;
|
|
150
|
+
const out = new Uint8Array(codec.blocks.length * blockSize);
|
|
151
|
+
const byIndex = new Map();
|
|
152
|
+
for (const b of codec.corrections)
|
|
153
|
+
byIndex.set(b.blockIndex, b.corrections);
|
|
154
|
+
for (let b = 0; b < codec.blocks.length; b++) {
|
|
155
|
+
const reconstructed = signalToBytes(inverseDct(dequantizeBlock(codec.blocks[b])));
|
|
156
|
+
const applied = applyCorrections(reconstructed, byIndex.get(b) ?? []);
|
|
157
|
+
if (!applied.ok)
|
|
158
|
+
return { ok: false, code: applied.code };
|
|
159
|
+
out.set(reconstructed, b * blockSize);
|
|
160
|
+
}
|
|
161
|
+
const bytes = out.subarray(0, codec.header.originalLength);
|
|
162
|
+
if (sha256Hex(bytes) !== codec.header.payloadDigest) {
|
|
163
|
+
return { ok: false, code: "RES_PAYLOAD_DIGEST_MISMATCH" };
|
|
164
|
+
}
|
|
165
|
+
// Return an independent copy so the caller cannot alias the working buffer.
|
|
166
|
+
return { ok: true, bytes: Uint8Array.from(bytes) };
|
|
167
|
+
}
|
|
168
|
+
/** SHA-256 of arbitrary bytes (re-exported so callers need one import). */
|
|
169
|
+
export function payloadDigest(bytes) {
|
|
170
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
171
|
+
}
|
|
172
|
+
// ── aggregate-only metrics + reporter ───────────────────────────────────────
|
|
173
|
+
/**
|
|
174
|
+
* Accumulate AGGREGATE-ONLY residual metrics (counts/byte totals). Never
|
|
175
|
+
* payload, never prompt text: the dashboard reads this shape and nothing else.
|
|
176
|
+
*/
|
|
177
|
+
export function accumulateMetrics(previous, result) {
|
|
178
|
+
if (!result.ok) {
|
|
179
|
+
return { ...previous, encodeAttempts: previous.encodeAttempts + 1 };
|
|
180
|
+
}
|
|
181
|
+
const base = {
|
|
182
|
+
...previous,
|
|
183
|
+
encodeAttempts: previous.encodeAttempts + 1,
|
|
184
|
+
encodedByteTotal: previous.encodedByteTotal + result.accounting.encodedSize,
|
|
185
|
+
exactByteTotal: previous.exactByteTotal + result.accounting.exactCompressedSize,
|
|
186
|
+
};
|
|
187
|
+
return result.admitted
|
|
188
|
+
? { ...base, admittedCount: base.admittedCount + 1 }
|
|
189
|
+
: { ...base, rejectedCount: base.rejectedCount + 1 };
|
|
190
|
+
}
|
|
191
|
+
/** A zeroed metrics accumulator. */
|
|
192
|
+
export function emptyMetrics() {
|
|
193
|
+
return {
|
|
194
|
+
encodeAttempts: 0,
|
|
195
|
+
admittedCount: 0,
|
|
196
|
+
rejectedCount: 0,
|
|
197
|
+
recoveryFailures: 0,
|
|
198
|
+
encodedByteTotal: 0,
|
|
199
|
+
exactByteTotal: 0,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
/** Build the flag-gated typed reporter (mirrors the VC4A shard reporter). */
|
|
203
|
+
export function createResidualReporter(emit) {
|
|
204
|
+
const fire = (event, fields) => {
|
|
205
|
+
if (!VC4B_ENABLED())
|
|
206
|
+
return;
|
|
207
|
+
if (!emit)
|
|
208
|
+
return;
|
|
209
|
+
try {
|
|
210
|
+
emit(event, fields);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
/* non-fatal observability — never break the agent loop */
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
return {
|
|
217
|
+
residualAdmitted(fields) {
|
|
218
|
+
fire("vector_cortex_residual_admitted", fields);
|
|
219
|
+
},
|
|
220
|
+
parityRecoveryFailed(fields) {
|
|
221
|
+
fire("vector_cortex_parity_recovery_failed", fields);
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|