pi-mega-compact 0.20.0 → 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 (42) hide show
  1. package/dist/config/vector-cortex.js +12 -0
  2. package/dist/config.js +1 -1
  3. package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +1 -0
  4. package/dist/extensions/dashboard-server/routes-vector-cortex-residual.js +51 -0
  5. package/dist/extensions/dashboard-server/routes-vector-cortex.js +1 -0
  6. package/dist/extensions/dashboard-server/routes.js +1 -1
  7. package/dist/extensions/dashboard-server/server.js +3 -1
  8. package/dist/src/config/vector-cortex.js +12 -0
  9. package/dist/src/config.js +1 -1
  10. package/dist/src/vector-cortex/residual/codec.js +224 -0
  11. package/dist/src/vector-cortex/residual/dct.js +158 -0
  12. package/dist/src/vector-cortex/residual/fixture-payload.js +77 -0
  13. package/dist/src/vector-cortex/residual/gf256.js +182 -0
  14. package/dist/src/vector-cortex/residual/parity.js +245 -0
  15. package/dist/src/vector-cortex/residual/quantize.js +200 -0
  16. package/dist/src/vector-cortex/residual/stream.js +124 -0
  17. package/dist/src/vector-cortex/residual/types.js +55 -0
  18. package/dist/vector-cortex/residual/codec.js +224 -0
  19. package/dist/vector-cortex/residual/dct.js +158 -0
  20. package/dist/vector-cortex/residual/fixture-payload.js +77 -0
  21. package/dist/vector-cortex/residual/gf256.js +182 -0
  22. package/dist/vector-cortex/residual/parity.js +245 -0
  23. package/dist/vector-cortex/residual/quantize.js +200 -0
  24. package/dist/vector-cortex/residual/stream.js +124 -0
  25. package/dist/vector-cortex/residual/types.js +55 -0
  26. package/extensions/dashboard-server/api-contracts/vector-cortex.ts +29 -0
  27. package/extensions/dashboard-server/routes-rag-settings-helpers.ts +6 -0
  28. package/extensions/dashboard-server/routes-vector-cortex-residual.ts +60 -0
  29. package/extensions/dashboard-server/routes-vector-cortex.ts +1 -0
  30. package/extensions/dashboard-server/routes.ts +1 -0
  31. package/extensions/dashboard-server/server.ts +2 -0
  32. package/package.json +1 -1
  33. package/src/config/vector-cortex.ts +13 -0
  34. package/src/config.ts +1 -0
  35. package/src/vector-cortex/residual/codec.ts +292 -0
  36. package/src/vector-cortex/residual/dct.ts +166 -0
  37. package/src/vector-cortex/residual/fixture-payload.ts +90 -0
  38. package/src/vector-cortex/residual/gf256.ts +196 -0
  39. package/src/vector-cortex/residual/parity.ts +283 -0
  40. package/src/vector-cortex/residual/quantize.ts +230 -0
  41. package/src/vector-cortex/residual/stream.ts +135 -0
  42. package/src/vector-cortex/residual/types.ts +236 -0
@@ -0,0 +1,236 @@
1
+ /**
2
+ * vector-cortex/residual/types.ts — reversible residual payload codec (VC4B).
3
+ *
4
+ * Owns `ResidualCodecV1` / `ParityShardV1` — the contract of the sprint failure
5
+ * triad:
6
+ *
7
+ * A = admitted residual (DCT + int16 + exact corrections) + RS(9,6) parity;
8
+ * B = exact compressed bytes (forced when the >95% accounting rejects A);
9
+ * C = ledger bytes (forced when A/B decode fails).
10
+ *
11
+ * Semantic vectors NEVER claim to recover exact text. Exact bytes come only from
12
+ * exact payload shards (VC4A `ExactShardV1`) or this REVERSIBLE codec: a block
13
+ * DCT-II analysis, int16 quantization, and a block-scoped exact correction
14
+ * stream that makes post-decode byte error exactly zero for admitted artifacts.
15
+ * Numeric erasure parity protects the codec bytes; it is not a substitute for
16
+ * the exact payload (RESIDUAL_CODEC.md).
17
+ *
18
+ * Consumes only reviewer-accepted predecessor contracts (VC1A EventV2 bytes via
19
+ * VC4A shard ranges) and the common contracts. Pure types + registered
20
+ * conformance IDs: no storage, no console, no network (PREVENT-PI-004 /
21
+ * PREVENT-011).
22
+ */
23
+
24
+ /** Fixed transform block length (RESIDUAL_CODEC §byte scope: 4096). */
25
+ export const RESIDUAL_BLOCK_SIZE = 4096;
26
+
27
+ /** Reed–Solomon shard geometry: k=6 data shards, m=3 parity shards. */
28
+ export const RS_DATA_SHARDS = 6;
29
+ export const RS_PARITY_SHARDS = 3;
30
+ export const RS_TOTAL_SHARDS = RS_DATA_SHARDS + RS_PARITY_SHARDS;
31
+
32
+ /** GF(2^8) primitive polynomial for the parity field (0x11d). */
33
+ export const GF_PRIMITIVE_POLYNOMIAL = 0x11d;
34
+
35
+ /** Header magic bytes `VCR1`. */
36
+ export const RESIDUAL_MAGIC = "VCR1";
37
+
38
+ /**
39
+ * Admission ratio: residual is admitted only when its FULL encoded size is at
40
+ * most `floor(0.95 * exactCompressedSize)` (RESIDUAL_CODEC §admission). The
41
+ * accounting counts every persisted byte — header, scales, coefficients,
42
+ * corrections, shard metadata, all 9 shards, and digests.
43
+ */
44
+ export const ADMISSION_NUMERATOR = 95;
45
+ export const ADMISSION_DENOMINATOR = 100;
46
+
47
+ /**
48
+ * Canonical header of an encoded residual artifact. Serialized (little-endian)
49
+ * as: magic `VCR1` (4 bytes), u32 original length, 32-byte SHA-256 of the
50
+ * ORIGINAL payload, u16 block size, u16 data shard count `k`, u16 parity count
51
+ * `m` — 46 bytes total.
52
+ */
53
+ export interface ResidualHeaderV1 {
54
+ readonly magic: typeof RESIDUAL_MAGIC;
55
+ /** Original (pre-padding) payload length in bytes. */
56
+ readonly originalLength: number;
57
+ /** SHA-256 of the original payload bytes, lowercase hex (64 chars). */
58
+ readonly payloadDigest: string;
59
+ readonly blockSize: typeof RESIDUAL_BLOCK_SIZE;
60
+ readonly dataShards: typeof RS_DATA_SHARDS;
61
+ readonly parityShards: typeof RS_PARITY_SHARDS;
62
+ }
63
+
64
+ /** Serialized header byte length: 4 + 4 + 32 + 2 + 2 + 2. */
65
+ export const RESIDUAL_HEADER_BYTES = 46;
66
+
67
+ /**
68
+ * One transform block's quantized coefficients. `scale` is the per-block float32
69
+ * scale `max(abs(c))/32767` (exactly 0 for an all-zero block); `coefficients`
70
+ * are the int16 quantized DCT-II coefficients in ascending frequency order.
71
+ */
72
+ export interface QuantizedBlockV1 {
73
+ /** Per-block float32 LE scale. Zero for an all-zero coefficient block. */
74
+ readonly scale: number;
75
+ /** Int16 coefficients, length exactly `RESIDUAL_BLOCK_SIZE`. */
76
+ readonly coefficients: Int16Array;
77
+ }
78
+
79
+ /**
80
+ * One exact byte correction inside a block: the reconstruction differed from
81
+ * the original at `offset`, and `original` is the authoritative byte.
82
+ */
83
+ export interface CorrectionV1 {
84
+ /** Offset within the block, 0..4095. */
85
+ readonly offset: number;
86
+ /** The original (authoritative) byte value, 0..255. */
87
+ readonly original: number;
88
+ }
89
+
90
+ /**
91
+ * The block-scoped exact correction stream. Blocks appear in ascending
92
+ * `blockIndex`; each block's corrections are sorted by ascending offset with no
93
+ * duplicate offset. Omitted blocks have an implicit count of zero.
94
+ */
95
+ export interface BlockCorrectionsV1 {
96
+ readonly blockIndex: number;
97
+ readonly corrections: readonly CorrectionV1[];
98
+ }
99
+
100
+ /**
101
+ * A fully encoded residual artifact before shard splitting: the header plus the
102
+ * per-block quantization and the exact correction stream. Serializing this in
103
+ * canonical order produces the PROTECTED STREAM that the parity layer splits.
104
+ */
105
+ export interface ResidualCodecV1 {
106
+ readonly schema: "residual-codec-v1";
107
+ readonly header: ResidualHeaderV1;
108
+ readonly blocks: readonly QuantizedBlockV1[];
109
+ readonly corrections: readonly BlockCorrectionsV1[];
110
+ }
111
+
112
+ /**
113
+ * One Reed–Solomon shard over the protected stream. Shards 0..5 are the
114
+ * systematic data shards (the protected stream split into `k=6` equal,
115
+ * zero-padded pieces); shards 6..8 are the parity shards, in that order. Every
116
+ * shard carries its own SHA-256 so an unknown corruption is DETECTED (and can
117
+ * then be marked as a known erasure) even though it is never blindly corrected.
118
+ */
119
+ export interface ParityShardV1 {
120
+ readonly schema: "parity-shard-v1";
121
+ /** Shard index 0..8 (0..5 data, 6..8 parity). */
122
+ readonly index: number;
123
+ readonly kind: "data" | "parity";
124
+ /** Shard payload bytes; every shard has the same length. */
125
+ readonly bytes: Uint8Array;
126
+ /** SHA-256 of `bytes`, lowercase hex. */
127
+ readonly digest: string;
128
+ /** Length of the UNPADDED protected stream (needed to truncate on recovery). */
129
+ readonly streamLength: number;
130
+ }
131
+
132
+ /** Byte accounting handed forward to VC4C (spec §next handoff). */
133
+ export interface ResidualAccountingV1 {
134
+ /** Every persisted residual byte: header + scales + coefficients + corrections + shards + digests. */
135
+ readonly encodedSize: number;
136
+ /** Size of the competing exact compressed representation. */
137
+ readonly exactCompressedSize: number;
138
+ /** `floor(0.95 * exactCompressedSize)` — the inclusive admission ceiling. */
139
+ readonly admissionCeiling: number;
140
+ /** Number of correction entries across every block (density numerator). */
141
+ readonly correctionCount: number;
142
+ /** Number of transform blocks. */
143
+ readonly blockCount: number;
144
+ }
145
+
146
+ /** Failure codes the residual codec / parity layer can return. */
147
+ export type ResidualFailureCode =
148
+ | "RES_QUANTIZE_RANGE"
149
+ | "RES_TOO_MANY_ERASURES"
150
+ | "RES_DUPLICATE_SHARD_INDEX"
151
+ | "RES_SHARD_DIGEST_MISMATCH"
152
+ | "RES_SHARD_LENGTH_MISMATCH"
153
+ | "RES_SINGULAR_MATRIX"
154
+ | "RES_PAYLOAD_DIGEST_MISMATCH"
155
+ | "RES_HEADER_INVALID"
156
+ | "RES_CORRECTION_DUPLICATE_OFFSET"
157
+ | "RES_CORRECTION_RANGE"
158
+ | "RES_NOT_ADMITTED";
159
+
160
+ /** Encode result: an admitted artifact, or the exact reason it was not admitted. */
161
+ export type ResidualEncodeResult =
162
+ | {
163
+ ok: true;
164
+ /** Mode A: the residual was admitted. */
165
+ admitted: true;
166
+ codec: ResidualCodecV1;
167
+ shards: readonly ParityShardV1[];
168
+ accounting: ResidualAccountingV1;
169
+ }
170
+ | {
171
+ ok: true;
172
+ /** Mode B: encode succeeded but accounting rejected it; store exact bytes. */
173
+ admitted: false;
174
+ code: "RES_NOT_ADMITTED";
175
+ accounting: ResidualAccountingV1;
176
+ }
177
+ | { ok: false; code: ResidualFailureCode };
178
+
179
+ /** Decode result: the exact original bytes, or the exact failure code. */
180
+ export type ResidualDecodeResult =
181
+ | { ok: true; bytes: Uint8Array }
182
+ | { ok: false; code: ResidualFailureCode };
183
+
184
+ /** Parity reconstruction result over a (possibly partial) shard set. */
185
+ export type ParityRecoveryResult =
186
+ | { ok: true; stream: Uint8Array; recoveredIndices: readonly number[] }
187
+ | { ok: false; code: ResidualFailureCode };
188
+
189
+ /** The two structured events the VC4B reporter emits. */
190
+ export type ResidualEventName =
191
+ | "vector_cortex_residual_admitted"
192
+ | "vector_cortex_parity_recovery_failed";
193
+
194
+ /** Injected emit callback — same (event, fields) shape as the other VC seams. */
195
+ export type ResidualEmitter = (
196
+ event: ResidualEventName,
197
+ fields: Record<string, unknown>,
198
+ ) => void;
199
+
200
+ /** Typed, best-effort reporter bound to the two residual event names. */
201
+ export interface ResidualReporter {
202
+ readonly residualAdmitted: (fields: Record<string, unknown>) => void;
203
+ readonly parityRecoveryFailed: (fields: Record<string, unknown>) => void;
204
+ }
205
+
206
+ /**
207
+ * Aggregate-only residual metrics exposed to the dashboard. NEVER payload:
208
+ * counts, byte totals and ratios only (SECURITY_PRIVACY — the exact ledger is
209
+ * not training data and residual payloads are never rendered).
210
+ */
211
+ export interface ResidualMetricsV1 {
212
+ readonly encodeAttempts: number;
213
+ readonly admittedCount: number;
214
+ readonly rejectedCount: number;
215
+ readonly recoveryFailures: number;
216
+ readonly encodedByteTotal: number;
217
+ readonly exactByteTotal: number;
218
+ }
219
+
220
+ /**
221
+ * Registered RES conformance ID range (RES-001..050). The acceptance test reads
222
+ * these rows from the v2 manifest and asserts each returns its manifest
223
+ * `ok`/`code`. The three named assertions (RES-DCT-001 / RES-RS-002 /
224
+ * RES-ADMIT-003) live alongside them.
225
+ */
226
+ export const RES_IDS: readonly string[] = Array.from(
227
+ { length: 50 },
228
+ (_v, i) => `RES-${String(i + 1).padStart(3, "0")}`,
229
+ );
230
+
231
+ /** Named RES conformance assertions (the sprint's headline rows). */
232
+ export const RES_NAMED_IDS = [
233
+ "RES-DCT-001",
234
+ "RES-RS-002",
235
+ "RES-ADMIT-003",
236
+ ] as const;