pi-mega-compact 0.20.10 → 0.20.11

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.
@@ -0,0 +1,73 @@
1
+ /**
2
+ * vector-cortex/cache/types.ts — VC7A frozen range crystal contract.
3
+ *
4
+ * A CRYSTAL is a frozen, immutable rendering of a bounded set of source ranges.
5
+ * VC5B proved a render is deterministic; VC7A makes that determinism REUSABLE by
6
+ * freezing the rendered bytes under a key that names exactly what they depend on.
7
+ *
8
+ * THE IDENTITY RULE (the whole sprint in one sentence). A crystal is keyed by
9
+ * the source ranges it COVERS, the digest of those covered bytes, the VALIDATED
10
+ * dependency high-water it was rendered against, and the renderer/profile that
11
+ * produced it — and by NOTHING ELSE. In particular the GLOBAL LEDGER FRONTIER is
12
+ * deliberately excluded from identity. That exclusion is the point: appending an
13
+ * unrelated turn advances the global frontier on every single request, so a key
14
+ * that included it would invalidate every crystal continuously and the cache
15
+ * would never hit. Conversely, the four fields that ARE in the key are precisely
16
+ * the ones whose change would make the frozen bytes WRONG:
17
+ *
18
+ * - covered ranges / `coveredDigest` — different bytes render differently;
19
+ * - `dependencyHighWater` — a dependency that has since advanced
20
+ * may contradict what was frozen;
21
+ * - `rendererVersion` — a renderer change changes the output;
22
+ * - `profileId` + `profileVersion` — a provider profile changes framing.
23
+ *
24
+ * So: unrelated append leaves the key UNCHANGED (CRY-FRONTIER-001); one covered
25
+ * byte, one dependency tick, one renderer bump, or one profile bump invalidates
26
+ * it 100% (CRY-COVERED-002 / CRY-DEP-003).
27
+ *
28
+ * VALIDATED, NOT OBSERVED, HIGH-WATER. `dependencyHighWater` is the DURABLE
29
+ * contiguous authority high-water the render was VALIDATED against, not whatever
30
+ * the writer happened to see. TRIAD_RESILIENCE freezes the derived high-water
31
+ * during an authority outage precisely so a crystal cannot be minted claiming
32
+ * evidence the authority has not durably accepted.
33
+ *
34
+ * DIGEST CONVENTIONS (three fields, two shapes — do not mix them).
35
+ * - `DagSpan.digest` and `CrystalKeyV1.coveredDigest` are `sha256:<hex>`, WITH
36
+ * the prefix (they name COVERED SOURCE BYTES, matching the DAG span
37
+ * convention the ranges themselves carry).
38
+ * - `CrystalKeyV1.requestDigest` is BARE lowercase hex, matching
39
+ * `RenderManifestV1.requestDigest`.
40
+ * - `CrystalV1.contentDigest` (the store's content address) is BARE lowercase
41
+ * hex, matching `ExactShardV1.digest`.
42
+ * Mixing these would either make every lookup miss or — far worse — make a
43
+ * prefix-stripped comparison accidentally succeed across granularities.
44
+ *
45
+ * Pure types + registered conformance IDs: no storage, no console, no network
46
+ * (PREVENT-PI-004 / PREVENT-011).
47
+ */
48
+ /**
49
+ * Maximum covered ranges in one key. A key is caller-shaped input; this bound
50
+ * exists so a single lookup cannot be turned into an unbounded sort/hash of
51
+ * attacker-chosen ranges (CRY_KEY_LIMIT).
52
+ */
53
+ export const CRYSTAL_LIMIT_RANGES = 256;
54
+ /** Maximum aggregate covered bytes across a key's ranges (8 MiB). */
55
+ export const CRYSTAL_LIMIT_BYTES = 8 * 1024 * 1024;
56
+ /**
57
+ * Registered VC7A crystal conformance ID range (CRY-001..015). The acceptance
58
+ * aggregator reads these rows from the v2 manifest and asserts each returns its
59
+ * manifest `ok`/`code`.
60
+ */
61
+ export const CRYSTAL_IDS = Array.from({ length: 15 }, (_v, i) => `CRY-${String(i + 1).padStart(3, "0")}`);
62
+ /**
63
+ * Registered VC7A provider-identity conformance rows (PRO-016..023), continuing
64
+ * VC5B's PRO-001..015. These pin the PROFILE half of crystal identity: a profile
65
+ * or renderer bump must invalidate, and a profile-irrelevant change must not.
66
+ */
67
+ export const CRYSTAL_PROVIDER_IDS = Array.from({ length: 8 }, (_v, i) => `PRO-${String(i + 16).padStart(3, "0")}`);
68
+ /** Named VC7A conformance assertions (the sprint's headline rows). */
69
+ export const CRYSTAL_NAMED_IDS = [
70
+ "CRY-FRONTIER-001",
71
+ "CRY-COVERED-002",
72
+ "CRY-DEP-003",
73
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.20.10",
3
+ "version": "0.20.11",
4
4
  "description": "Layered, local, vector-backed context compressor for pi — supersede/collapse/cluster compaction with deduped inline recall.",
5
5
  "type": "module",
6
6
  "license": "BSD-3-Clause",
@@ -0,0 +1,206 @@
1
+ /**
2
+ * vector-cortex/cache/types.ts — VC7A frozen range crystal contract.
3
+ *
4
+ * A CRYSTAL is a frozen, immutable rendering of a bounded set of source ranges.
5
+ * VC5B proved a render is deterministic; VC7A makes that determinism REUSABLE by
6
+ * freezing the rendered bytes under a key that names exactly what they depend on.
7
+ *
8
+ * THE IDENTITY RULE (the whole sprint in one sentence). A crystal is keyed by
9
+ * the source ranges it COVERS, the digest of those covered bytes, the VALIDATED
10
+ * dependency high-water it was rendered against, and the renderer/profile that
11
+ * produced it — and by NOTHING ELSE. In particular the GLOBAL LEDGER FRONTIER is
12
+ * deliberately excluded from identity. That exclusion is the point: appending an
13
+ * unrelated turn advances the global frontier on every single request, so a key
14
+ * that included it would invalidate every crystal continuously and the cache
15
+ * would never hit. Conversely, the four fields that ARE in the key are precisely
16
+ * the ones whose change would make the frozen bytes WRONG:
17
+ *
18
+ * - covered ranges / `coveredDigest` — different bytes render differently;
19
+ * - `dependencyHighWater` — a dependency that has since advanced
20
+ * may contradict what was frozen;
21
+ * - `rendererVersion` — a renderer change changes the output;
22
+ * - `profileId` + `profileVersion` — a provider profile changes framing.
23
+ *
24
+ * So: unrelated append leaves the key UNCHANGED (CRY-FRONTIER-001); one covered
25
+ * byte, one dependency tick, one renderer bump, or one profile bump invalidates
26
+ * it 100% (CRY-COVERED-002 / CRY-DEP-003).
27
+ *
28
+ * VALIDATED, NOT OBSERVED, HIGH-WATER. `dependencyHighWater` is the DURABLE
29
+ * contiguous authority high-water the render was VALIDATED against, not whatever
30
+ * the writer happened to see. TRIAD_RESILIENCE freezes the derived high-water
31
+ * during an authority outage precisely so a crystal cannot be minted claiming
32
+ * evidence the authority has not durably accepted.
33
+ *
34
+ * DIGEST CONVENTIONS (three fields, two shapes — do not mix them).
35
+ * - `DagSpan.digest` and `CrystalKeyV1.coveredDigest` are `sha256:<hex>`, WITH
36
+ * the prefix (they name COVERED SOURCE BYTES, matching the DAG span
37
+ * convention the ranges themselves carry).
38
+ * - `CrystalKeyV1.requestDigest` is BARE lowercase hex, matching
39
+ * `RenderManifestV1.requestDigest`.
40
+ * - `CrystalV1.contentDigest` (the store's content address) is BARE lowercase
41
+ * hex, matching `ExactShardV1.digest`.
42
+ * Mixing these would either make every lookup miss or — far worse — make a
43
+ * prefix-stripped comparison accidentally succeed across granularities.
44
+ *
45
+ * Pure types + registered conformance IDs: no storage, no console, no network
46
+ * (PREVENT-PI-004 / PREVENT-011).
47
+ */
48
+
49
+ import type { DagSpan } from "../prompt-dag/types.js";
50
+
51
+ /**
52
+ * The immutable identity of a frozen crystal.
53
+ *
54
+ * Every field here is an IDENTITY field: changing any one of them yields a
55
+ * different key, and the key is stable under any change to a field NOT listed
56
+ * here (most importantly the global ledger frontier).
57
+ */
58
+ export interface CrystalKeyV1 {
59
+ /** Provider profile that framed the render (VC5B `ProviderProfile.id`). */
60
+ readonly profileId: string;
61
+ /** Profile version — a profile bump must invalidate frozen bytes. */
62
+ readonly profileVersion: string;
63
+ /** Digest of the canonical request shape, BARE lowercase hex (VC5B). */
64
+ readonly requestDigest: string;
65
+ /** The source ranges this crystal covers. Sorted + disjoint (see `crystal.ts`). */
66
+ readonly sourceRanges: readonly DagSpan[];
67
+ /** Digest over the covered ranges' pinned digests, `sha256:` prefixed. */
68
+ readonly coveredDigest: string;
69
+ /** VALIDATED durable dependency high-water — never the global frontier. */
70
+ readonly dependencyHighWater: bigint;
71
+ /** Renderer version that produced the frozen bytes (VC5B renderer). */
72
+ readonly rendererVersion: string;
73
+ }
74
+
75
+ /**
76
+ * A frozen crystal: immutable bytes plus the manifest that explains them.
77
+ *
78
+ * `bytes` are the rendered output verbatim. `contentDigest` is the SHA-256 of
79
+ * exactly those bytes (bare lowercase hex) and is what makes the store
80
+ * CONTENT-ADDRESSED: two writers who independently render the same key must
81
+ * produce the same digest, and if they do not, one of them is wrong and the
82
+ * store refuses rather than picking a winner (`CRY_KEY_COLLISION`).
83
+ */
84
+ export interface CrystalV1 {
85
+ readonly schema: "crystal-v1";
86
+ /** Canonical encoding of the key (see `encodeCrystalKey`). */
87
+ readonly keyDigest: string;
88
+ /** The frozen rendered bytes, verbatim and immutable. */
89
+ readonly bytes: Uint8Array;
90
+ /** SHA-256 over `bytes`, BARE lowercase hex (the content address). */
91
+ readonly contentDigest: string;
92
+ /** Byte length of `bytes`, carried so readers need not touch the payload. */
93
+ readonly byteCount: number;
94
+ /** The identity this crystal was frozen under. */
95
+ readonly key: CrystalKeyV1;
96
+ }
97
+
98
+ /** VC7A failure codes (registered CRY codes). */
99
+ export type CrystalFailureCode =
100
+ /** Two covered ranges in the same session overlap — identity is ambiguous. */
101
+ | "CRY_RANGE_OVERLAP"
102
+ /** A key already exists with DIFFERENT bytes; the store never overwrites. */
103
+ | "CRY_KEY_COLLISION"
104
+ /** A covered range is malformed (reversed/negative seq or byte bounds). */
105
+ | "CRY_RANGE_INVALID"
106
+ /** The key names no covered ranges — a crystal must cover something. */
107
+ | "CRY_RANGE_EMPTY"
108
+ /** The key exceeds the covered-range or aggregate-byte bound. */
109
+ | "CRY_KEY_LIMIT"
110
+ /** The store is unavailable — triad mode C, nothing is served from cache. */
111
+ | "CRY_STORE_UNAVAILABLE";
112
+
113
+ /** The verdict of canonical key encoding. */
114
+ export type CrystalKeyResult =
115
+ | { readonly ok: true; readonly keyDigest: string; readonly key: CrystalKeyV1 }
116
+ | { readonly ok: false; readonly codes: readonly CrystalFailureCode[] };
117
+
118
+ /**
119
+ * The verdict of a store write.
120
+ *
121
+ * `written` distinguishes a FIRST write from an idempotent re-write of identical
122
+ * bytes: both are `ok`, because re-freezing the same crystal is legitimate (two
123
+ * concurrent renders raced), but only the first actually stored anything. A
124
+ * different-bytes write is never `ok` and never mutates the stored crystal.
125
+ */
126
+ export type CrystalWriteResult =
127
+ | { readonly ok: true; readonly written: boolean; readonly contentDigest: string }
128
+ | { readonly ok: false; readonly code: CrystalFailureCode; readonly contentDigest: string };
129
+
130
+ /**
131
+ * Runtime triad mode for the crystal path (TRIAD_RESILIENCE).
132
+ *
133
+ * A — the crystal store answered the read (the fast, normal path);
134
+ * B — a miss or a collision forced a FRESH deterministic render. This is an
135
+ * INDEPENDENT algorithm: no crystal, no store index, the renderer runs
136
+ * from the plan exactly as it would have before VC7A existed;
137
+ * C — the store is unavailable, so the cache is BYPASSED entirely. Mode C
138
+ * serves nothing from cache and states the loss of old semantic context;
139
+ * it never substitutes a stale or partially-written crystal.
140
+ */
141
+ export type CrystalMode = "A" | "B" | "C";
142
+
143
+ /** Reader-only aggregate for GET /api/vector-cortex/cache-crystals. */
144
+ export interface CrystalStoreStats {
145
+ readonly mode: CrystalMode;
146
+ /** Distinct crystals currently held. */
147
+ readonly crystalCount: number;
148
+ /** Aggregate frozen bytes held (sum of `byteCount`). */
149
+ readonly totalBytes: number;
150
+ /** Reads answered from the store (mode A). */
151
+ readonly hits: number;
152
+ /** Reads with no stored crystal, forcing a fresh render (mode B). */
153
+ readonly misses: number;
154
+ /** Bytes served from hits — the cache's observable benefit. */
155
+ readonly hitBytes: number;
156
+ /** First writes that actually stored a crystal. */
157
+ readonly writes: number;
158
+ /** Idempotent re-writes of byte-identical crystals. */
159
+ readonly duplicateWrites: number;
160
+ /** Same-key, different-bytes writes refused (`CRY_KEY_COLLISION`). */
161
+ readonly collisions: number;
162
+ }
163
+
164
+ /**
165
+ * Maximum covered ranges in one key. A key is caller-shaped input; this bound
166
+ * exists so a single lookup cannot be turned into an unbounded sort/hash of
167
+ * attacker-chosen ranges (CRY_KEY_LIMIT).
168
+ */
169
+ export const CRYSTAL_LIMIT_RANGES = 256;
170
+
171
+ /** Maximum aggregate covered bytes across a key's ranges (8 MiB). */
172
+ export const CRYSTAL_LIMIT_BYTES = 8 * 1024 * 1024;
173
+
174
+ /**
175
+ * Registered VC7A crystal conformance ID range (CRY-001..015). The acceptance
176
+ * aggregator reads these rows from the v2 manifest and asserts each returns its
177
+ * manifest `ok`/`code`.
178
+ */
179
+ export const CRYSTAL_IDS: readonly string[] = Array.from(
180
+ { length: 15 },
181
+ (_v, i) => `CRY-${String(i + 1).padStart(3, "0")}`,
182
+ );
183
+
184
+ /**
185
+ * Registered VC7A provider-identity conformance rows (PRO-016..023), continuing
186
+ * VC5B's PRO-001..015. These pin the PROFILE half of crystal identity: a profile
187
+ * or renderer bump must invalidate, and a profile-irrelevant change must not.
188
+ */
189
+ export const CRYSTAL_PROVIDER_IDS: readonly string[] = Array.from(
190
+ { length: 8 },
191
+ (_v, i) => `PRO-${String(i + 16).padStart(3, "0")}`,
192
+ );
193
+
194
+ /** Named VC7A conformance assertions (the sprint's headline rows). */
195
+ export const CRYSTAL_NAMED_IDS = [
196
+ "CRY-FRONTIER-001",
197
+ "CRY-COVERED-002",
198
+ "CRY-DEP-003",
199
+ ] as const;
200
+
201
+ /** The two structured events the VC7A reporter emits. */
202
+ export type CrystalEventName =
203
+ | "vector_cortex_crystal_written"
204
+ | "vector_cortex_crystal_collision";
205
+
206
+ export type { DagSpan };