pi-mega-compact 0.19.3 → 0.20.1

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.
Files changed (114) hide show
  1. package/dist/config/vector-cortex.js +24 -0
  2. package/dist/config.js +1 -1
  3. package/dist/extensions/dashboard-server/routes-cache.test/_helpers.js +2 -0
  4. package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +2 -0
  5. package/dist/extensions/dashboard-server/routes-vector-cortex-helpers.js +100 -0
  6. package/dist/extensions/dashboard-server/routes-vector-cortex-residual.js +51 -0
  7. package/dist/extensions/dashboard-server/routes-vector-cortex-shards.js +48 -0
  8. package/dist/extensions/dashboard-server/routes-vector-cortex.js +2 -0
  9. package/dist/extensions/dashboard-server/routes.js +1 -1
  10. package/dist/extensions/dashboard-server/server.js +41 -16
  11. package/dist/extensions/dashboard-server/tailscale.js +32 -0
  12. package/dist/src/config/vector-cortex.js +24 -0
  13. package/dist/src/config.js +1 -1
  14. package/dist/src/vector-cortex/residual/codec.js +224 -0
  15. package/dist/src/vector-cortex/residual/dct.js +158 -0
  16. package/dist/src/vector-cortex/residual/fixture-payload.js +77 -0
  17. package/dist/src/vector-cortex/residual/gf256.js +182 -0
  18. package/dist/src/vector-cortex/residual/parity.js +245 -0
  19. package/dist/src/vector-cortex/residual/quantize.js +200 -0
  20. package/dist/src/vector-cortex/residual/stream.js +124 -0
  21. package/dist/src/vector-cortex/residual/types.js +55 -0
  22. package/dist/src/vector-cortex/shards/exact.js +134 -0
  23. package/dist/src/vector-cortex/shards/manifest.js +237 -0
  24. package/dist/src/vector-cortex/shards/semantic.js +137 -0
  25. package/dist/src/vector-cortex/shards/types.js +51 -0
  26. package/dist/vector-cortex/residual/codec.js +224 -0
  27. package/dist/vector-cortex/residual/dct.js +158 -0
  28. package/dist/vector-cortex/residual/fixture-payload.js +77 -0
  29. package/dist/vector-cortex/residual/gf256.js +182 -0
  30. package/dist/vector-cortex/residual/parity.js +245 -0
  31. package/dist/vector-cortex/residual/quantize.js +200 -0
  32. package/dist/vector-cortex/residual/stream.js +124 -0
  33. package/dist/vector-cortex/residual/types.js +55 -0
  34. package/dist/vector-cortex/shards/exact.js +134 -0
  35. package/dist/vector-cortex/shards/manifest.js +237 -0
  36. package/dist/vector-cortex/shards/semantic.js +137 -0
  37. package/dist/vector-cortex/shards/types.js +51 -0
  38. package/extensions/dashboard-client/dist/assets/{AreaChart-DqiFSnDx.js → AreaChart-n4z5w9Ox.js} +2 -2
  39. package/extensions/dashboard-client/dist/assets/{AreaChart-DqiFSnDx.js.map → AreaChart-n4z5w9Ox.js.map} +1 -1
  40. package/extensions/dashboard-client/dist/assets/{BarChart-Cqg-PQMK.js → BarChart-BLGjWWBr.js} +2 -2
  41. package/extensions/dashboard-client/dist/assets/{BarChart-Cqg-PQMK.js.map → BarChart-BLGjWWBr.js.map} +1 -1
  42. package/extensions/dashboard-client/dist/assets/{CacheTab-BPNs0xIF.js → CacheTab-CAnrKKVK.js} +2 -2
  43. package/extensions/dashboard-client/dist/assets/{CacheTab-BPNs0xIF.js.map → CacheTab-CAnrKKVK.js.map} +1 -1
  44. package/extensions/dashboard-client/dist/assets/{EventsTab-BQf7OGkf.js → EventsTab-D-GL7Ngq.js} +2 -2
  45. package/extensions/dashboard-client/dist/assets/{EventsTab-BQf7OGkf.js.map → EventsTab-D-GL7Ngq.js.map} +1 -1
  46. package/extensions/dashboard-client/dist/assets/{HealthTab-B14TaSPN.js → HealthTab-C4x5qQ8s.js} +2 -2
  47. package/extensions/dashboard-client/dist/assets/{HealthTab-B14TaSPN.js.map → HealthTab-C4x5qQ8s.js.map} +1 -1
  48. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-D8lkRoa1.js → MaintenanceTab-BSPs0eq5.js} +2 -2
  49. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-D8lkRoa1.js.map → MaintenanceTab-BSPs0eq5.js.map} +1 -1
  50. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-BOWtGfpx.js → MemoryMapTab-BWEUtTSu.js} +2 -2
  51. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-BOWtGfpx.js.map → MemoryMapTab-BWEUtTSu.js.map} +1 -1
  52. package/extensions/dashboard-client/dist/assets/{MetricsTab-7XI9cWaX.js → MetricsTab-Dhm1c3Cx.js} +2 -2
  53. package/extensions/dashboard-client/dist/assets/{MetricsTab-7XI9cWaX.js.map → MetricsTab-Dhm1c3Cx.js.map} +1 -1
  54. package/extensions/dashboard-client/dist/assets/{OverviewTab-C9KaGYvh.js → OverviewTab-Cm6cySiQ.js} +2 -2
  55. package/extensions/dashboard-client/dist/assets/{OverviewTab-C9KaGYvh.js.map → OverviewTab-Cm6cySiQ.js.map} +1 -1
  56. package/extensions/dashboard-client/dist/assets/{ReposTab-BwESNhox.js → ReposTab-DxLN1N4K.js} +2 -2
  57. package/extensions/dashboard-client/dist/assets/{ReposTab-BwESNhox.js.map → ReposTab-DxLN1N4K.js.map} +1 -1
  58. package/extensions/dashboard-client/dist/assets/{SessionsTab-DKH55Rda.js → SessionsTab-CZuI4ISW.js} +2 -2
  59. package/extensions/dashboard-client/dist/assets/{SessionsTab-DKH55Rda.js.map → SessionsTab-CZuI4ISW.js.map} +1 -1
  60. package/extensions/dashboard-client/dist/assets/{SetupTab-zDxf-nJd.js → SetupTab-C_0VOomm.js} +2 -2
  61. package/extensions/dashboard-client/dist/assets/{SetupTab-zDxf-nJd.js.map → SetupTab-C_0VOomm.js.map} +1 -1
  62. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-C9isog4L.js → TimeSavedCard-BhbaRLmi.js} +2 -2
  63. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-C9isog4L.js.map → TimeSavedCard-BhbaRLmi.js.map} +1 -1
  64. package/extensions/dashboard-client/dist/assets/{TurnsTab-ciI6ydvj.js → TurnsTab-CXJmbjje.js} +2 -2
  65. package/extensions/dashboard-client/dist/assets/{TurnsTab-ciI6ydvj.js.map → TurnsTab-CXJmbjje.js.map} +1 -1
  66. package/extensions/dashboard-client/dist/assets/VectorCortexTab-BGe9hu3r.js +2 -0
  67. package/extensions/dashboard-client/dist/assets/VectorCortexTab-BGe9hu3r.js.map +1 -0
  68. package/extensions/dashboard-client/dist/assets/{WikiTab-DcYHc734.js → WikiTab-C5uvMCzp.js} +2 -2
  69. package/extensions/dashboard-client/dist/assets/{WikiTab-DcYHc734.js.map → WikiTab-C5uvMCzp.js.map} +1 -1
  70. package/extensions/dashboard-client/dist/assets/{button-DF8lx4bt.js → button-Cl_fxJcg.js} +2 -2
  71. package/extensions/dashboard-client/dist/assets/{button-DF8lx4bt.js.map → button-Cl_fxJcg.js.map} +1 -1
  72. package/extensions/dashboard-client/dist/assets/{card-DIoDheSZ.js → card-BH8Le5YM.js} +2 -2
  73. package/extensions/dashboard-client/dist/assets/{card-DIoDheSZ.js.map → card-BH8Le5YM.js.map} +1 -1
  74. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-BFRzeYX3.js → generateCategoricalChart-sZ6ZNtBi.js} +2 -2
  75. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-BFRzeYX3.js.map → generateCategoricalChart-sZ6ZNtBi.js.map} +1 -1
  76. package/extensions/dashboard-client/dist/assets/{index-DSZON0Yi.js → index-CX9zv8cJ.js} +3 -3
  77. package/extensions/dashboard-client/dist/assets/{index-DSZON0Yi.js.map → index-CX9zv8cJ.js.map} +1 -1
  78. package/extensions/dashboard-client/dist/assets/{switch-l4qo22mU.js → switch-CXJhcjhM.js} +2 -2
  79. package/extensions/dashboard-client/dist/assets/{switch-l4qo22mU.js.map → switch-CXJhcjhM.js.map} +1 -1
  80. package/extensions/dashboard-client/dist/assets/{toggle-DPn6td1_.js → toggle-CoLtIc6y.js} +2 -2
  81. package/extensions/dashboard-client/dist/assets/{toggle-DPn6td1_.js.map → toggle-CoLtIc6y.js.map} +1 -1
  82. package/extensions/dashboard-client/dist/assets/{useSSE-BLmolqTJ.js → useSSE-fSXho606.js} +2 -2
  83. package/extensions/dashboard-client/dist/assets/{useSSE-BLmolqTJ.js.map → useSSE-fSXho606.js.map} +1 -1
  84. package/extensions/dashboard-client/dist/index.html +1 -1
  85. package/extensions/dashboard-client/src/api/vector-cortex.ts +9 -0
  86. package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +35 -0
  87. package/extensions/dashboard-client/src/types/vector-cortex.ts +13 -0
  88. package/extensions/dashboard-server/api-contracts/vector-cortex.ts +53 -0
  89. package/extensions/dashboard-server/routes-cache.test/_helpers.ts +2 -0
  90. package/extensions/dashboard-server/routes-rag-settings-helpers.ts +12 -0
  91. package/extensions/dashboard-server/routes-vector-cortex-helpers.ts +110 -0
  92. package/extensions/dashboard-server/routes-vector-cortex-residual.ts +60 -0
  93. package/extensions/dashboard-server/routes-vector-cortex-shards.ts +57 -0
  94. package/extensions/dashboard-server/routes-vector-cortex.ts +2 -0
  95. package/extensions/dashboard-server/routes.ts +2 -0
  96. package/extensions/dashboard-server/server.ts +46 -17
  97. package/extensions/dashboard-server/tailscale.ts +32 -0
  98. package/package.json +1 -1
  99. package/src/config/vector-cortex.ts +26 -0
  100. package/src/config.ts +2 -0
  101. package/src/vector-cortex/residual/codec.ts +292 -0
  102. package/src/vector-cortex/residual/dct.ts +166 -0
  103. package/src/vector-cortex/residual/fixture-payload.ts +90 -0
  104. package/src/vector-cortex/residual/gf256.ts +196 -0
  105. package/src/vector-cortex/residual/parity.ts +283 -0
  106. package/src/vector-cortex/residual/quantize.ts +230 -0
  107. package/src/vector-cortex/residual/stream.ts +135 -0
  108. package/src/vector-cortex/residual/types.ts +236 -0
  109. package/src/vector-cortex/shards/exact.ts +141 -0
  110. package/src/vector-cortex/shards/manifest.ts +276 -0
  111. package/src/vector-cortex/shards/semantic.ts +156 -0
  112. package/src/vector-cortex/shards/types.ts +212 -0
  113. package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js +0 -2
  114. package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js.map +0 -1
@@ -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
+ }
@@ -0,0 +1,158 @@
1
+ /**
2
+ * vector-cortex/residual/dct.ts — orthonormal DCT-II basis V1 (VC4B).
3
+ *
4
+ * Basis V1 is the orthonormal DCT-II matrix generated ANALYTICALLY and never
5
+ * learned or stored (RESIDUAL_CODEC §byte scope):
6
+ *
7
+ * alpha(0) = sqrt(1/n), alpha(k>0) = sqrt(2/n)
8
+ * C[k][i] = alpha(k) * cos(pi * (2i + 1) * k / (2n))
9
+ *
10
+ * Forward coefficients are the dot product with each basis row (the orthonormal
11
+ * least-squares solve); the inverse is the transpose applied to the coefficient
12
+ * vector. Byte mapping is `x = (byte - 127.5) / 127.5`; the inverse maps back
13
+ * with round-to-nearest-even and clamps to 0..255.
14
+ *
15
+ * The full 4096x4096 matrix is ~134 MB of float64, so it is NEVER materialized.
16
+ * Instead the cosine argument `pi*(2i+1)*k/(2n)` is reduced modulo `2n` against
17
+ * a precomputed half-period table of length `2n` — this is EXACTLY the same set
18
+ * of cosine values the matrix would hold (cos is sampled only at the `2n`
19
+ * distinct arguments `pi*j/(2n)`, j = 0..2n-1), so the transform is bit-stable
20
+ * and table-generation is O(n) rather than O(n^2).
21
+ *
22
+ * Pure numeric transform: no storage, no console, no network (PREVENT-PI-004 /
23
+ * PREVENT-011).
24
+ */
25
+ import { RESIDUAL_BLOCK_SIZE } from "./types.js";
26
+ /** Byte-to-signal mapping midpoint / half-range (RESIDUAL_CODEC §transform). */
27
+ const BYTE_MIDPOINT = 127.5;
28
+ /**
29
+ * Cosine table cache keyed by block length. `table[j] = cos(pi * j / (2n))` for
30
+ * j = 0..2n-1; every DCT argument `pi*(2i+1)*k/(2n)` reduces to one of these
31
+ * entries (with a sign) because cos has period `2*pi` = index period `4n`.
32
+ */
33
+ const cosTables = new Map();
34
+ /** Build (or fetch) the `4n`-entry cosine table for block length `n`. */
35
+ function cosTable(n) {
36
+ const cached = cosTables.get(n);
37
+ if (cached)
38
+ return cached;
39
+ const period = 4 * n;
40
+ const table = new Float64Array(period);
41
+ for (let j = 0; j < period; j++) {
42
+ table[j] = Math.cos((Math.PI * j) / (2 * n));
43
+ }
44
+ cosTables.set(n, table);
45
+ return table;
46
+ }
47
+ /** Orthonormal DCT-II row scale: alpha(0)=sqrt(1/n), else sqrt(2/n). */
48
+ export function alpha(k, n) {
49
+ return k === 0 ? Math.sqrt(1 / n) : Math.sqrt(2 / n);
50
+ }
51
+ /**
52
+ * Map a padded byte block to the signal domain: `x = (byte - 127.5) / 127.5`.
53
+ * The input must be exactly `n` bytes (the caller zero-pads the final block).
54
+ */
55
+ export function bytesToSignal(block) {
56
+ const out = new Float64Array(block.length);
57
+ for (let i = 0; i < block.length; i++) {
58
+ out[i] = (block[i] - BYTE_MIDPOINT) / BYTE_MIDPOINT;
59
+ }
60
+ return out;
61
+ }
62
+ /**
63
+ * Map a reconstructed signal back to bytes: invert the affine map, round to
64
+ * nearest with ties-to-even, and clamp to 0..255 (RESIDUAL_CODEC §transform).
65
+ */
66
+ export function signalToBytes(signal) {
67
+ const out = new Uint8Array(signal.length);
68
+ for (let i = 0; i < signal.length; i++) {
69
+ const v = signal[i] * BYTE_MIDPOINT + BYTE_MIDPOINT;
70
+ const r = roundHalfToEven(v);
71
+ out[i] = r < 0 ? 0 : r > 255 ? 255 : r;
72
+ }
73
+ return out;
74
+ }
75
+ /**
76
+ * Round to nearest, ties to even (banker's rounding). `Math.round` rounds ties
77
+ * toward +Infinity, which is NOT the rule RESIDUAL_CODEC mandates.
78
+ */
79
+ export function roundHalfToEven(v) {
80
+ const floor = Math.floor(v);
81
+ const diff = v - floor;
82
+ if (diff > 0.5)
83
+ return floor + 1;
84
+ if (diff < 0.5)
85
+ return floor;
86
+ // Exact tie: pick the even neighbour.
87
+ return floor % 2 === 0 ? floor : floor + 1;
88
+ }
89
+ /**
90
+ * Forward orthonormal DCT-II over one block of length `n` (default 4096).
91
+ * `coefficients[k] = alpha(k) * sum_i x[i] * cos(pi*(2i+1)*k/(2n))`, emitted in
92
+ * ascending frequency order `k = 0..n-1` (the exact RESIDUAL_CODEC coefficient
93
+ * order).
94
+ */
95
+ export function forwardDct(signal) {
96
+ const n = signal.length;
97
+ const table = cosTable(n);
98
+ const period = 4 * n;
99
+ const out = new Float64Array(n);
100
+ for (let k = 0; k < n; k++) {
101
+ let sum = 0;
102
+ // Argument index advances by 2k each step, starting at k (i=0 gives
103
+ // (2*0+1)*k = k). Reduce modulo the 4n-entry period.
104
+ let idx = k % period;
105
+ const step = (2 * k) % period;
106
+ for (let i = 0; i < n; i++) {
107
+ sum += signal[i] * table[idx];
108
+ idx += step;
109
+ if (idx >= period)
110
+ idx -= period;
111
+ }
112
+ out[k] = alpha(k, n) * sum;
113
+ }
114
+ return out;
115
+ }
116
+ /**
117
+ * Inverse orthonormal DCT (the transpose of the forward matrix):
118
+ * `x[i] = sum_k alpha(k) * c[k] * cos(pi*(2i+1)*k/(2n))`.
119
+ */
120
+ export function inverseDct(coefficients) {
121
+ const n = coefficients.length;
122
+ const table = cosTable(n);
123
+ const period = 4 * n;
124
+ // Pre-scale each coefficient by its row alpha so the inner loop is a plain
125
+ // dot product (identical arithmetic to scaling inside the loop for k, and it
126
+ // keeps the per-i accumulation order stable).
127
+ const scaled = new Float64Array(n);
128
+ for (let k = 0; k < n; k++)
129
+ scaled[k] = alpha(k, n) * coefficients[k];
130
+ const out = new Float64Array(n);
131
+ for (let i = 0; i < n; i++) {
132
+ let sum = 0;
133
+ const step = (2 * i + 1) % period;
134
+ let idx = 0;
135
+ for (let k = 0; k < n; k++) {
136
+ sum += scaled[k] * table[idx];
137
+ idx += step;
138
+ if (idx >= period)
139
+ idx -= period;
140
+ }
141
+ out[i] = sum;
142
+ }
143
+ return out;
144
+ }
145
+ /**
146
+ * Split a payload into fixed 4096-byte blocks, zero-padding ONLY the final
147
+ * block. The original length is retained by the caller (the header) so the
148
+ * padding is dropped on decode.
149
+ */
150
+ export function splitBlocks(payload, blockSize = RESIDUAL_BLOCK_SIZE) {
151
+ const blocks = [];
152
+ for (let off = 0; off < payload.length; off += blockSize) {
153
+ const block = new Uint8Array(blockSize);
154
+ block.set(payload.subarray(off, Math.min(off + blockSize, payload.length)));
155
+ blocks.push(block);
156
+ }
157
+ return blocks;
158
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * vector-cortex/residual/fixture-payload.ts — deterministic payload
3
+ * materialization for the VC4B conformance corpus.
4
+ *
5
+ * The residual fixtures describe their payloads GENERATIVELY (`kind` + `length`
6
+ * + `seed`) rather than embedding megabytes of base64, so this module is the
7
+ * single normative generator both the fixture producer and the acceptance test
8
+ * agree on. Every generator is pure and deterministic — the same descriptor
9
+ * always yields byte-identical output, which is what makes the committed
10
+ * fixtures meaningful.
11
+ *
12
+ * Pure byte generation: no storage, no console, no network (PREVENT-PI-004 /
13
+ * PREVENT-011).
14
+ */
15
+ /** Linear congruential generator (numerical-recipes constants), deterministic. */
16
+ function lcgBytes(length, seed) {
17
+ const out = new Uint8Array(length);
18
+ let state = seed >>> 0;
19
+ for (let i = 0; i < length; i++) {
20
+ state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
21
+ out[i] = (state >>> 24) & 0xff;
22
+ }
23
+ return out;
24
+ }
25
+ /**
26
+ * Materialize a fixture payload descriptor into exact bytes.
27
+ *
28
+ * empty zero bytes
29
+ * zeros `length` 0x00 bytes
30
+ * constant `length` copies of `value`
31
+ * sequence `i % 256`
32
+ * lcg deterministic pseudorandom stream from `seed`
33
+ * text repeating printable ASCII (valid UTF-8)
34
+ * invalid-utf8 a stream containing lone continuation/overlong bytes
35
+ * dc-outlier all 0xff except one 0x00 at `outlierOffset` (forces the exact
36
+ * correction stream: a DC-dominant block whose coarse scale
37
+ * cannot reproduce the outlier's neighbourhood)
38
+ * alternating 0x00/0xff alternation (maximum Nyquist energy)
39
+ */
40
+ export function materializePayload(d) {
41
+ const length = d.length ?? 0;
42
+ switch (d.kind) {
43
+ case "empty":
44
+ return new Uint8Array(0);
45
+ case "zeros":
46
+ return new Uint8Array(length);
47
+ case "constant":
48
+ return new Uint8Array(length).fill((d.value ?? 0) & 0xff);
49
+ case "sequence":
50
+ return Uint8Array.from({ length }, (_v, i) => i % 256);
51
+ case "lcg":
52
+ return lcgBytes(length, d.seed ?? 1);
53
+ case "text": {
54
+ const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 ";
55
+ return Uint8Array.from({ length }, (_v, i) => alphabet.charCodeAt(i % alphabet.length));
56
+ }
57
+ case "invalid-utf8": {
58
+ // Deliberately invalid: lone 0x80 continuation, 0xff (never valid), and
59
+ // the overlong 0xc0 0xaf encoding of "/". Never normalized by the codec.
60
+ const pattern = [0xff, 0x80, 0xc0, 0xaf, 0x00, 0xfe];
61
+ return Uint8Array.from({ length }, (_v, i) => pattern[i % pattern.length]);
62
+ }
63
+ case "dc-outlier": {
64
+ const out = new Uint8Array(length).fill(255);
65
+ const at = d.outlierOffset ?? 0;
66
+ if (at < length)
67
+ out[at] = 0;
68
+ return out;
69
+ }
70
+ case "alternating":
71
+ return Uint8Array.from({ length }, (_v, i) => (i % 2 === 0 ? 0 : 255));
72
+ case "literal":
73
+ return new Uint8Array(Buffer.from(d.bytesBase64 ?? "", "base64"));
74
+ default:
75
+ throw new Error(`unknown residual payload kind: ${d.kind}`);
76
+ }
77
+ }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * vector-cortex/residual/gf256.ts — GF(2^8) field arithmetic and matrix algebra
3
+ * for the VC4B Reed–Solomon erasure parity.
4
+ *
5
+ * Field: GF(2^8) with primitive polynomial `0x11d`, elements represented as
6
+ * polynomial-basis bytes (RESIDUAL_CODEC §erasure parity). Log/antilog tables
7
+ * are built once from generator 2 and drive constant-time multiply/divide.
8
+ *
9
+ * Matrix inversion and recovery use DETERMINISTIC left-to-right pivot search and
10
+ * GF Gaussian elimination — no randomness, no iteration-order dependence, so
11
+ * every implementation reaches byte-identical results.
12
+ *
13
+ * Pure arithmetic: no storage, no console, no network (PREVENT-PI-004 /
14
+ * PREVENT-011).
15
+ */
16
+ import { GF_PRIMITIVE_POLYNOMIAL } from "./types.js";
17
+ const FIELD_SIZE = 256;
18
+ /** `EXP[i] = 2^i` in GF(2^8) (doubled length so multiply needs no modulo). */
19
+ const EXP = new Uint8Array(FIELD_SIZE * 2);
20
+ /** `LOG[x] = i` such that `2^i = x`; `LOG[0]` is unused (0 has no logarithm). */
21
+ const LOG = new Uint8Array(FIELD_SIZE);
22
+ (function buildTables() {
23
+ let x = 1;
24
+ for (let i = 0; i < FIELD_SIZE - 1; i++) {
25
+ EXP[i] = x;
26
+ LOG[x] = i;
27
+ x <<= 1;
28
+ if (x & 0x100)
29
+ x ^= GF_PRIMITIVE_POLYNOMIAL;
30
+ }
31
+ // Mirror the cycle so `EXP[a + b]` is valid for a,b <= 254 without a modulo.
32
+ for (let i = FIELD_SIZE - 1; i < EXP.length; i++) {
33
+ EXP[i] = EXP[i - (FIELD_SIZE - 1)];
34
+ }
35
+ })();
36
+ /** GF(2^8) addition (and subtraction) is XOR. */
37
+ export function gfAdd(a, b) {
38
+ return (a ^ b) & 0xff;
39
+ }
40
+ /** GF(2^8) multiplication via log/antilog tables. */
41
+ export function gfMul(a, b) {
42
+ if (a === 0 || b === 0)
43
+ return 0;
44
+ return EXP[LOG[a] + LOG[b]];
45
+ }
46
+ /** GF(2^8) division; dividing by zero is a programming error and throws. */
47
+ export function gfDiv(a, b) {
48
+ if (b === 0)
49
+ throw new RangeError("gf256: division by zero");
50
+ if (a === 0)
51
+ return 0;
52
+ return EXP[LOG[a] + (FIELD_SIZE - 1) - LOG[b]];
53
+ }
54
+ /** Multiplicative inverse of a non-zero field element. */
55
+ export function gfInv(a) {
56
+ if (a === 0)
57
+ throw new RangeError("gf256: zero has no inverse");
58
+ return EXP[FIELD_SIZE - 1 - LOG[a]];
59
+ }
60
+ /** `base^exponent` in GF(2^8) (exponent is a non-negative integer). */
61
+ export function gfPow(base, exponent) {
62
+ if (exponent === 0)
63
+ return 1;
64
+ if (base === 0)
65
+ return 0;
66
+ return EXP[(LOG[base] * exponent) % (FIELD_SIZE - 1)];
67
+ }
68
+ /** Allocate a zero matrix. */
69
+ export function gfMatrix(rows, cols) {
70
+ return { rows, cols, data: new Uint8Array(rows * cols) };
71
+ }
72
+ /** Read `m[r][c]`. */
73
+ export function gfAt(m, r, c) {
74
+ return m.data[r * m.cols + c];
75
+ }
76
+ /** Write `m[r][c] = v`. */
77
+ export function gfSet(m, r, c, v) {
78
+ m.data[r * m.cols + c] = v;
79
+ }
80
+ /**
81
+ * Build the `rows x cols` Vandermonde matrix `V[r][c] = alpha_r^c` with the
82
+ * distinct evaluation points `alpha_r = r + 1` (RESIDUAL_CODEC: rows r=0..8,
83
+ * columns c=0..5).
84
+ */
85
+ export function vandermonde(rows, cols) {
86
+ const m = gfMatrix(rows, cols);
87
+ for (let r = 0; r < rows; r++) {
88
+ for (let c = 0; c < cols; c++) {
89
+ gfSet(m, r, c, gfPow((r + 1) & 0xff, c));
90
+ }
91
+ }
92
+ return m;
93
+ }
94
+ /** Matrix product `a x b` over GF(2^8). */
95
+ export function gfMatMul(a, b) {
96
+ const out = gfMatrix(a.rows, b.cols);
97
+ for (let r = 0; r < a.rows; r++) {
98
+ for (let c = 0; c < b.cols; c++) {
99
+ let acc = 0;
100
+ for (let i = 0; i < a.cols; i++) {
101
+ acc ^= gfMul(gfAt(a, r, i), gfAt(b, i, c));
102
+ }
103
+ gfSet(out, r, c, acc);
104
+ }
105
+ }
106
+ return out;
107
+ }
108
+ /** Extract the contiguous row range `[start, start+count)` of a matrix. */
109
+ export function gfSubRows(m, start, count) {
110
+ const out = gfMatrix(count, m.cols);
111
+ out.data.set(m.data.subarray(start * m.cols, (start + count) * m.cols));
112
+ return out;
113
+ }
114
+ /** Gather the given row indices (in order) into a new matrix. */
115
+ export function gfPickRows(m, indices) {
116
+ const out = gfMatrix(indices.length, m.cols);
117
+ indices.forEach((src, dest) => {
118
+ out.data.set(m.data.subarray(src * m.cols, (src + 1) * m.cols), dest * m.cols);
119
+ });
120
+ return out;
121
+ }
122
+ /**
123
+ * Invert a square GF(2^8) matrix by Gauss–Jordan elimination with a
124
+ * DETERMINISTIC left-to-right pivot search (the first row at or below the
125
+ * current column with a non-zero entry). Returns null when the matrix is
126
+ * singular.
127
+ */
128
+ export function gfInvert(m) {
129
+ if (m.rows !== m.cols)
130
+ return null;
131
+ const n = m.rows;
132
+ const work = gfMatrix(n, n);
133
+ work.data.set(m.data);
134
+ const inv = gfMatrix(n, n);
135
+ for (let i = 0; i < n; i++)
136
+ gfSet(inv, i, i, 1);
137
+ const swapRows = (mat, a, b) => {
138
+ if (a === b)
139
+ return;
140
+ for (let c = 0; c < mat.cols; c++) {
141
+ const t = gfAt(mat, a, c);
142
+ gfSet(mat, a, c, gfAt(mat, b, c));
143
+ gfSet(mat, b, c, t);
144
+ }
145
+ };
146
+ for (let col = 0; col < n; col++) {
147
+ // Deterministic pivot: the lowest-index row >= col with a non-zero entry.
148
+ let pivot = -1;
149
+ for (let r = col; r < n; r++) {
150
+ if (gfAt(work, r, col) !== 0) {
151
+ pivot = r;
152
+ break;
153
+ }
154
+ }
155
+ if (pivot === -1)
156
+ return null; // singular
157
+ swapRows(work, col, pivot);
158
+ swapRows(inv, col, pivot);
159
+ // Normalize the pivot row.
160
+ const pivotValue = gfAt(work, col, col);
161
+ if (pivotValue !== 1) {
162
+ const scale = gfInv(pivotValue);
163
+ for (let c = 0; c < n; c++) {
164
+ gfSet(work, col, c, gfMul(gfAt(work, col, c), scale));
165
+ gfSet(inv, col, c, gfMul(gfAt(inv, col, c), scale));
166
+ }
167
+ }
168
+ // Eliminate the column from every other row.
169
+ for (let r = 0; r < n; r++) {
170
+ if (r === col)
171
+ continue;
172
+ const factor = gfAt(work, r, col);
173
+ if (factor === 0)
174
+ continue;
175
+ for (let c = 0; c < n; c++) {
176
+ gfSet(work, r, c, gfAdd(gfAt(work, r, c), gfMul(factor, gfAt(work, col, c))));
177
+ gfSet(inv, r, c, gfAdd(gfAt(inv, r, c), gfMul(factor, gfAt(inv, col, c))));
178
+ }
179
+ }
180
+ }
181
+ return inv;
182
+ }