pi-mega-compact 0.20.70 → 0.20.71

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 (36) hide show
  1. package/dist/extensions/dashboard-server/routes-vector-cortex-crystals.js +45 -30
  2. package/dist/extensions/dashboard-server/routes-vector-cortex-diagnostics.js +50 -29
  3. package/dist/extensions/dashboard-server/routes-vector-cortex-economics.js +33 -20
  4. package/dist/extensions/dashboard-server/routes-vector-cortex-helpers.js +54 -0
  5. package/dist/extensions/dashboard-server/routes-vector-cortex-policy.js +33 -16
  6. package/dist/src/vector-cortex/cache/store.js +25 -2
  7. package/dist/src/vector-cortex/encoder/asset.js +34 -2
  8. package/dist/src/vector-cortex/encoder/router.js +31 -3
  9. package/dist/src/vector-cortex/livewire/livewire-live.js +163 -0
  10. package/dist/src/vector-cortex/livewire/livewire-registry.js +73 -0
  11. package/dist/src/vector-cortex/livewire/livewire-runtime.js +107 -0
  12. package/dist/src/vector-cortex/livewire/livewire-snapshot.js +101 -0
  13. package/dist/src/vector-cortex/livewire/livewire-types.js +19 -0
  14. package/dist/src/vector-cortex/provider/registry.js +44 -12
  15. package/dist/vector-cortex/cache/store.js +25 -2
  16. package/dist/vector-cortex/encoder/asset.js +34 -2
  17. package/dist/vector-cortex/encoder/router.js +31 -3
  18. package/dist/vector-cortex/provider/registry.js +44 -12
  19. package/extensions/dashboard-server/routes-vector-cortex-crystals.ts +46 -30
  20. package/extensions/dashboard-server/routes-vector-cortex-diagnostics.ts +52 -29
  21. package/extensions/dashboard-server/routes-vector-cortex-economics.ts +35 -20
  22. package/extensions/dashboard-server/routes-vector-cortex-helpers.ts +84 -0
  23. package/extensions/dashboard-server/routes-vector-cortex-policy.ts +35 -16
  24. package/package.json +1 -1
  25. package/src/vector-cortex/cache/store.ts +26 -2
  26. package/src/vector-cortex/encoder/asset.ts +46 -3
  27. package/src/vector-cortex/encoder/router.ts +56 -2
  28. package/src/vector-cortex/encoder/types.ts +3 -0
  29. package/src/vector-cortex/livewire/livewire-live.ts +223 -0
  30. package/src/vector-cortex/livewire/livewire-registry.ts +91 -0
  31. package/src/vector-cortex/livewire/livewire-runtime.ts +117 -0
  32. package/src/vector-cortex/livewire/livewire-snapshot.ts +108 -0
  33. package/src/vector-cortex/livewire/livewire-types.ts +84 -0
  34. package/src/vector-cortex/provider/economics.ts +9 -31
  35. package/src/vector-cortex/provider/registry.ts +82 -11
  36. package/src/vector-cortex/provider/types.ts +37 -0
@@ -25,11 +25,12 @@
25
25
  * Pi-agnostic, zero network (PREVENT-PI-004), no `any` (PREVENT-011).
26
26
  */
27
27
  import { createEncoderRuntime } from "./runtime.js";
28
- import { encodeVectorSet } from "./heads.js";
28
+ import { encodeVectorSet, loadHeadProjections, projectHeadFromTrunk, } from "./heads.js";
29
29
  import { embedTrigram512, selectTrigramBFallback } from "./trigram.js";
30
30
  import { embedLexical, selectLexicalC } from "./lexical.js";
31
31
  import { createEncoderHeadsReporter } from "./emit-vc2b.js";
32
- import { ENC_FAIL, } from "./types.js";
32
+ import { ML5A_ENABLED } from "../../config/vector-cortex.js";
33
+ import { ENC_FAIL, ENCODER_HEAD_ORDER, } from "./types.js";
33
34
  /** Deterministic text derived from an int token sequence so the asset-free
34
35
  * fallback producers operate on the same authority the learned path encoded. */
35
36
  function textFromTokens(tokens) {
@@ -92,9 +93,36 @@ export function encodeOrFallback(input, assetDir, options = {}) {
92
93
  if (!inferred.ok) {
93
94
  return fallbackFromLoad({ ok: false, mode: "B", code: inferred.code }, reporter, tokens);
94
95
  }
95
- const vectorSet = encodeVectorSet(tokens, { reporter, seed: options.seed });
96
+ const vectorSet = produceVectorSet(inferred.semantic, tokens, options, reporter);
96
97
  return { ok: true, mode: "A", vectorSet, code: null };
97
98
  }
99
+ /**
100
+ * Produce a mode-A `VectorSetV1` from the [1,384] trunk embedding (the
101
+ * `runtime.infer` result). VC2B-2 ML5-A: when real trained heads are loaded
102
+ * (`MEGACOMPACT_ML5_A` on + a `trainedHeadsPath` that loads), the multi-head
103
+ * output is projected through the real trained projection matrices via
104
+ * `projectHeadFromTrunk`. Otherwise (flag-off / absent / unloadable artifact)
105
+ * the deterministic LCG placeholder `encodeVectorSet` serves mode A —
106
+ * byte-identical to the VC2B predecessor. Non-fatal: an unloadable artifact
107
+ * degrades to the placeholder, never a throw.
108
+ */
109
+ function produceVectorSet(trunkEmbedding, tokens, options, reporter) {
110
+ const table = ML5A_ENABLED() && options.trainedHeadsPath !== undefined
111
+ ? loadHeadProjections(options.trainedHeadsPath)
112
+ : null;
113
+ if (table !== null) {
114
+ const heads = ENCODER_HEAD_ORDER.map((h) => projectHeadFromTrunk(h, trunkEmbedding, table));
115
+ reporter.headsEmitted({
116
+ heads: heads.length,
117
+ dims: heads.map((h) => h.dim).join("/"),
118
+ normalized: true,
119
+ tokens: tokens.length,
120
+ });
121
+ return { schema: "vector-set-v1", inputTokens: [...tokens], heads, normalized: true };
122
+ }
123
+ // ML5-A placeholder fallback (byte-identical predecessor).
124
+ return encodeVectorSet(tokens, { reporter, seed: options.seed });
125
+ }
98
126
  /**
99
127
  * Catch a (real or forced) A load failure (ok === false only) and select the
100
128
  * B/C fallback that emits `vector_cortex_encoder_fallback_selected` from the
@@ -45,48 +45,80 @@ function baseCache(stableFields) {
45
45
  };
46
46
  }
47
47
  /** Build a contract-profile shape pinned as a conformance fixture too. */
48
- function profile(id, version, excludedJsonPointers) {
48
+ function profile(id, version, excludedJsonPointers, economics) {
49
49
  return {
50
50
  schema: "provider-profile-v1",
51
51
  id,
52
52
  version,
53
53
  hashMode: "entire-canonical-request",
54
54
  excludedJsonPointers,
55
+ economics,
55
56
  };
56
57
  }
58
+ /**
59
+ * Build integer micro-unit economics for a base profile (VC7B). A cache WRITE
60
+ * costs more than an uncached token, a cache READ costs less — the standard
61
+ * provider-prompt-cache shape. `exclusionFixtureId` mirrors the profile's own
62
+ * exclusion fixture (or null when the profile has none to prove).
63
+ */
64
+ function econ(id, version, exclusionFixtureId, values) {
65
+ return {
66
+ schema: "provider-economics-v1",
67
+ profileId: id,
68
+ profileVersion: version,
69
+ basePrice: values.basePrice,
70
+ readPrice: values.readPrice,
71
+ writePrice: values.writePrice,
72
+ ttlMs: values.ttlMs,
73
+ minPrefix: values.minPrefix,
74
+ exclusionFixtureId,
75
+ };
76
+ }
77
+ /** Representative integer micro-unit economics for the Anthropic opus base tier. */
78
+ const OPUS_ECON = econ("anthropic-claude-opus", "v1", null, { basePrice: 15, readPrice: 2, writePrice: 19, ttlMs: 300_000, minPrefix: 1024 });
79
+ /** Representative integer micro-unit economics for the Anthropic sonnet base tier. */
80
+ const SONNET_ECON = econ("anthropic-claude-sonnet", "v1", null, { basePrice: 3, readPrice: 0, writePrice: 4, ttlMs: 300_000, minPrefix: 1024 });
81
+ /** Representative integer micro-unit economics for the OpenAI gpt base tier. */
82
+ const GPT_ECON = econ("openai-gpt", "v1", null, { basePrice: 5, readPrice: 1, writePrice: 6, ttlMs: 300_000, minPrefix: 1024 });
83
+ /** The gemini profile's versioned, fixture-proven exclusion. */
84
+ const GEMINI_EXCLUSIONS = [
85
+ {
86
+ pointer: "/requestId",
87
+ fixtureId: "PRO-EXCLUDE-010",
88
+ proofDigest: "sha256:excluded-request-id-proof",
89
+ },
90
+ ];
91
+ /** Representative integer micro-unit economics for the gemini base tier. */
92
+ const GEMINI_ECON = econ("google-gemini", "v1", "PRO-EXCLUDE-010", { basePrice: 3, readPrice: 1, writePrice: 4, ttlMs: 300_000, minPrefix: 1024 });
57
93
  /**
58
94
  * The fixture-backed base profiles. Each entry is the REAL bundle the renderer
59
95
  * resolves; the parallel conformance fixtures prove the cache-identity behavior.
60
96
  */
61
97
  const BASE_PROFILES = [
62
98
  {
63
- profile: profile("anthropic-claude-opus", "v1", []),
99
+ profile: profile("anthropic-claude-opus", "v1", [], OPUS_ECON),
64
100
  role: BASE_ROLE,
65
101
  tool: BASE_TOOL,
66
102
  cache: baseCache(["systemPromptPrepend", "tools", "nodes"]),
67
103
  },
68
104
  {
69
- profile: profile("anthropic-claude-sonnet", "v1", []),
105
+ profile: profile("anthropic-claude-sonnet", "v1", [], SONNET_ECON),
70
106
  role: BASE_ROLE,
71
107
  tool: BASE_TOOL,
72
108
  cache: baseCache(["systemPromptPrepend", "tools", "nodes"]),
73
109
  },
74
110
  {
75
- profile: profile("openai-gpt", "v1", []),
111
+ profile: profile("openai-gpt", "v1", [], GPT_ECON),
76
112
  role: BASE_ROLE,
77
113
  tool: BASE_TOOL,
78
114
  cache: baseCache(["systemPromptPrepend", "tools", "nodes"]),
79
115
  },
80
116
  {
81
117
  // A profile that proves a fixture-excluded pointer: a provider whose
82
- // request-id header cannot affect cache identity. Excluded + versioned.
83
- profile: profile("google-gemini", "v1", [
84
- {
85
- pointer: "/requestId",
86
- fixtureId: "PRO-EXCLUDE-010",
87
- proofDigest: "sha256:excluded-request-id-proof",
88
- },
89
- ]),
118
+ // request-id header cannot affect cache identity. Excluded + versioned, and
119
+ // the exclusion fixture id is carried into economics so the proof is
120
+ // honored (an unproven exclusion would fail economics validation).
121
+ profile: profile("google-gemini", "v1", GEMINI_EXCLUSIONS, GEMINI_ECON),
90
122
  role: BASE_ROLE,
91
123
  tool: BASE_TOOL,
92
124
  cache: baseCache(["systemPromptPrepend", "tools", "nodes"]),
@@ -28,21 +28,21 @@ import type { IncomingMessage, ServerResponse } from "node:http";
28
28
  import type { RouteContext } from "./routes-core.js";
29
29
  import { VC7A_ENABLED } from "../../src/config.js";
30
30
  import { sendJson } from "./routes-vector-cortex-shared.js";
31
- import { countVcEvents, vcCount } from "./vc-event-counts.js";
32
31
  import { deriveVcStatus } from "./vc-status.js";
32
+ import { readLivewireSnapshot } from "../../src/vector-cortex/livewire/livewire-registry.js";
33
33
  import type { VectorCortexCrystalsView } from "./api-contracts/vector-cortex-cache.js";
34
34
 
35
- // Actual events emitted by src/vector-cortex/cache/crystal-emit.ts.
36
- const CRYSTAL_EVENTS = [
37
- "vector_cortex_crystal_written",
38
- "vector_cortex_crystal_collision",
39
- ] as const;
35
+ // The flag-off deferred reason, kept byte-identical to the pre-LIVEWIRE
36
+ // predecessor (VC7A: `crystal_store_not_instantiated_v0_20_23`).
37
+ const DEFERRED_REASON = "crystal_store_not_instantiated_v0_20_23";
40
38
 
41
39
  /**
42
40
  * Reader-only GET /api/vector-cortex/cache-crystals (VC7A).
43
41
  *
44
- * Counts, byte volumes, and CRY_* codes only a static reader-only aggregate
45
- * seam with the same shape as the VC6A/VC6B/VC6C handlers.
42
+ * Counts, byte volumes, and CRY_* codes only. With the flag ON it surfaces the
43
+ * LIVEWIRE `CrystalStore` aggregate (live reads/writes/collisions accumulated at
44
+ * runtime); with the flag OFF it returns the byte-identical legacy deferred view
45
+ * (mode C, deferredReason present) so flag-off parity holds.
46
46
  */
47
47
  export function handleVectorCortexCrystals(
48
48
  req: IncomingMessage,
@@ -58,31 +58,47 @@ export function handleVectorCortexCrystals(
58
58
  }
59
59
 
60
60
  const enabled = VC7A_ENABLED();
61
- const counts = countVcEvents(ctx.stateDir, CRYSTAL_EVENTS);
62
- // Flag-off routes to mode C: with VC7A off nothing is served from the crystal
63
- // cache, which is exactly the spec's "cache bypass" outcome. Reporting A (hit)
64
- // or B (fresh render forced by a miss) would imply a cache path that is not
65
- // wired at all. Mirrors how VC6C's OFF view reports the mode it actually takes.
66
- const mode: "A" | "B" | "C" = enabled ? "A" : "C";
61
+ if (!enabled) {
62
+ // Flag-off parity: byte-identical to the predecessor (mode C + deferred).
63
+ const body: VectorCortexCrystalsView = {
64
+ enabled: false,
65
+ mode: "C",
66
+ crystalCount: 0,
67
+ totalBytes: 0,
68
+ hits: 0,
69
+ misses: 0,
70
+ hitBytes: 0,
71
+ writes: 0,
72
+ duplicateWrites: 0,
73
+ collisions: 0,
74
+ lastFailure: null,
75
+ updatedAt: new Date().toISOString(),
76
+ deferredReason: DEFERRED_REASON,
77
+ status: deriveVcStatus({ enabled: false, hasData: false }),
78
+ };
79
+ sendJson(res, 200, body);
80
+ return true;
81
+ }
82
+
83
+ const snap = readLivewireSnapshot(ctx.stateDir);
84
+ const crystal = snap.crystals;
85
+ const hasData = crystal.crystalCount > 0;
67
86
  const body: VectorCortexCrystalsView = {
68
- enabled,
69
- mode,
70
- crystalCount: vcCount(counts, "vector_cortex_crystal_written"),
71
- totalBytes: 0,
72
- hits: 0,
73
- misses: 0,
74
- hitBytes: 0,
75
- writes: 0,
76
- duplicateWrites: 0,
77
- collisions: vcCount(counts, "vector_cortex_crystal_collision"),
87
+ enabled: true,
88
+ // Surface the store's honest triad mode (B until a hit, A after, C when the
89
+ // store was set unavailable) rather than a hardcoded A.
90
+ mode: crystal.mode,
91
+ crystalCount: crystal.crystalCount,
92
+ totalBytes: crystal.totalBytes,
93
+ hits: crystal.hits,
94
+ misses: crystal.misses,
95
+ hitBytes: crystal.hitBytes,
96
+ writes: crystal.writes,
97
+ duplicateWrites: crystal.duplicateWrites,
98
+ collisions: crystal.collisions,
78
99
  lastFailure: null,
79
100
  updatedAt: new Date().toISOString(),
80
- deferredReason: "crystal_store_not_instantiated_v0_20_23",
81
- status: deriveVcStatus({
82
- enabled,
83
- deferredReason: "crystal_store_not_instantiated_v0_20_23",
84
- hasData: vcCount(counts, "vector_cortex_crystal_written") > 0,
85
- }),
101
+ status: deriveVcStatus({ enabled: true, hasData }),
86
102
  };
87
103
  sendJson(res, 200, body);
88
104
  return true;
@@ -30,18 +30,21 @@ import type { IncomingMessage, ServerResponse } from "node:http";
30
30
  import type { RouteContext } from "./routes-core.js";
31
31
  import { VC7C_ENABLED } from "../../src/config.js";
32
32
  import { sendJson } from "./routes-vector-cortex-shared.js";
33
- import { countVcEvents, vcCount } from "./vc-event-counts.js";
34
33
  import { deriveVcStatus } from "./vc-status.js";
34
+ import { readLivewireSnapshot } from "../../src/vector-cortex/livewire/livewire-registry.js";
35
35
  import type { VectorCortexDiagnosticsView } from "./api-contracts/vector-cortex-diagnostics.js";
36
36
 
37
- // Actual events emitted by src/vector-cortex/cache/diagnostics-emit.ts.
38
- const DIAGNOSTICS_EVENTS = ["vector_cortex_cache_serve_blocked"] as const;
37
+ // The flag-off deferred reason, kept byte-identical to the pre-LIVEWIRE
38
+ // predecessor (VC7C: `cache_classifier_not_wired_v0_20_23`).
39
+ const DEFERRED_REASON = "cache_classifier_not_wired_v0_20_23";
39
40
 
40
41
  /**
41
42
  * Reader-only GET /api/vector-cortex/cache-diagnostics (VC7C).
42
43
  *
43
- * Per-miss-class counts, breaker state, and CACHE and M5 codes only — a static
44
- * reader-only aggregate seam with the same shape as the VC7B economics handler.
44
+ * Per-miss-class counts, serveBlocked, breaker state, and CACHE/M5 codes only.
45
+ * With the flag ON it surfaces the LIVEWIRE classifier/breaker tallies
46
+ * accumulated at runtime; with the flag OFF it returns the byte-identical legacy
47
+ * deferred view (mode C, deferredReason present).
45
48
  */
46
49
  export function handleVectorCortexDiagnostics(
47
50
  req: IncomingMessage,
@@ -57,32 +60,52 @@ export function handleVectorCortexDiagnostics(
57
60
  }
58
61
 
59
62
  const enabled = VC7C_ENABLED();
60
- const counts = countVcEvents(ctx.stateDir, DIAGNOSTICS_EVENTS);
61
- // Flag-off routes to mode C: with VC7C off the diagnostics/breaker reporter is
62
- // suppressed, so no cache serve is attested here and the surface reports the
63
- // all-cache bypass outcome. Reporting A (crystal served) or B (fresh render
64
- // forced by a breaker) would attest a cache decision this seam is not wired to
65
- // observe. Mirrors how the VC7A/VC7B OFF views report the mode they take.
66
- const mode: "A" | "B" | "C" = enabled ? "A" : "C";
63
+ if (!enabled) {
64
+ // Flag-off parity: byte-identical to the predecessor (mode C + deferred).
65
+ const body: VectorCortexDiagnosticsView = {
66
+ enabled: false,
67
+ mode: "C",
68
+ profileMisses: 0,
69
+ rangeMisses: 0,
70
+ dependencyMisses: 0,
71
+ requestMisses: 0,
72
+ generationMisses: 0,
73
+ unknownMisses: 0,
74
+ serveBlocked: 0,
75
+ breakerState: "closed",
76
+ lastFailure: null,
77
+ updatedAt: new Date().toISOString(),
78
+ deferredReason: DEFERRED_REASON,
79
+ status: deriveVcStatus({ enabled: false, hasData: false }),
80
+ };
81
+ sendJson(res, 200, body);
82
+ return true;
83
+ }
84
+
85
+ const diag = readLivewireSnapshot(ctx.stateDir).diagnostics;
86
+ const hasData =
87
+ diag.profileMisses +
88
+ diag.rangeMisses +
89
+ diag.dependencyMisses +
90
+ diag.requestMisses +
91
+ diag.generationMisses +
92
+ diag.unknownMisses +
93
+ diag.serveBlocked >
94
+ 0;
67
95
  const body: VectorCortexDiagnosticsView = {
68
- enabled,
69
- mode,
70
- profileMisses: 0,
71
- rangeMisses: 0,
72
- dependencyMisses: 0,
73
- requestMisses: 0,
74
- generationMisses: 0,
75
- unknownMisses: 0,
76
- serveBlocked: vcCount(counts, "vector_cortex_cache_serve_blocked"),
77
- breakerState: "closed",
78
- lastFailure: null,
96
+ enabled: true,
97
+ mode: "A",
98
+ profileMisses: diag.profileMisses,
99
+ rangeMisses: diag.rangeMisses,
100
+ dependencyMisses: diag.dependencyMisses,
101
+ requestMisses: diag.requestMisses,
102
+ generationMisses: diag.generationMisses,
103
+ unknownMisses: diag.unknownMisses,
104
+ serveBlocked: diag.serveBlocked,
105
+ breakerState: diag.breakerState,
106
+ lastFailure: diag.lastFailure,
79
107
  updatedAt: new Date().toISOString(),
80
- deferredReason: "cache_classifier_not_wired_v0_20_23",
81
- status: deriveVcStatus({
82
- enabled,
83
- deferredReason: "cache_classifier_not_wired_v0_20_23",
84
- hasData: vcCount(counts, "vector_cortex_cache_serve_blocked") > 0,
85
- }),
108
+ status: deriveVcStatus({ enabled: true, hasData }),
86
109
  };
87
110
  sendJson(res, 200, body);
88
111
  return true;
@@ -28,18 +28,25 @@ import type { RouteContext } from "./routes-core.js";
28
28
  import { VC7B_ENABLED } from "../../src/config.js";
29
29
  import { sendJson } from "./routes-vector-cortex-shared.js";
30
30
  import { deriveVcStatus } from "./vc-status.js";
31
+ import { readLivewireSnapshot } from "../../src/vector-cortex/livewire/livewire-registry.js";
31
32
  import type { VectorCortexEconomicsView } from "./api-contracts/vector-cortex-economics.js";
32
33
 
34
+ // The flag-off deferred reason, kept byte-identical to the pre-LIVEWIRE
35
+ // predecessor (VC7B: `economics_not_computed_v0_20_23`).
36
+ const DEFERRED_REASON = "economics_not_computed_v0_20_23";
37
+
33
38
  /**
34
39
  * Reader-only GET /api/vector-cortex/cache-economics (VC7B).
35
40
  *
36
- * Counts, profile tallies, and ECON_* codes only a static reader-only
37
- * aggregate seam with the same shape as the VC7A crystals handler.
41
+ * Counts, profile tallies, and ECON_* codes only. With the flag ON it surfaces
42
+ * the LIVEWIRE-provided profile economics tallies + the runtime `computed` bit;
43
+ * with the flag OFF it returns the byte-identical legacy deferred view (mode C,
44
+ * deferredReason present).
38
45
  */
39
46
  export function handleVectorCortexEconomics(
40
47
  req: IncomingMessage,
41
48
  res: ServerResponse,
42
- _ctx: RouteContext,
49
+ ctx: RouteContext,
43
50
  ): boolean {
44
51
  const url = req.url ?? "";
45
52
  const path = url.split("?")[0] ?? url;
@@ -50,25 +57,33 @@ export function handleVectorCortexEconomics(
50
57
  }
51
58
 
52
59
  const enabled = VC7B_ENABLED();
53
- // Flag-off routes to mode C: with VC7B off no cache economics are served, which
54
- // is exactly the spec's "economics bypass" outcome. Reporting A (cached render
55
- // priced) or B (fresh render priced) would imply an economics path that is not
56
- // wired at all. Mirrors how VC6C/VC7A OFF views report the mode they take.
57
- const mode: "A" | "B" | "C" = enabled ? "A" : "C";
60
+ if (!enabled) {
61
+ // Flag-off parity: byte-identical to the predecessor (mode C + deferred).
62
+ const body: VectorCortexEconomicsView = {
63
+ enabled: false,
64
+ mode: "C",
65
+ profileCount: 0,
66
+ provenExclusions: 0,
67
+ unprovenExclusions: 0,
68
+ lastFailure: null,
69
+ updatedAt: new Date().toISOString(),
70
+ deferredReason: DEFERRED_REASON,
71
+ status: deriveVcStatus({ enabled: false, hasData: false }),
72
+ };
73
+ sendJson(res, 200, body);
74
+ return true;
75
+ }
76
+
77
+ const econ = readLivewireSnapshot(ctx.stateDir).economics;
58
78
  const body: VectorCortexEconomicsView = {
59
- enabled,
60
- mode,
61
- profileCount: 0,
62
- provenExclusions: 0,
63
- unprovenExclusions: 0,
64
- lastFailure: null,
79
+ enabled: true,
80
+ mode: "A",
81
+ profileCount: econ.profileCount,
82
+ provenExclusions: econ.provenExclusions,
83
+ unprovenExclusions: econ.unprovenExclusions,
84
+ lastFailure: econ.lastFailure,
65
85
  updatedAt: new Date().toISOString(),
66
- deferredReason: "economics_not_computed_v0_20_23",
67
- status: deriveVcStatus({
68
- enabled,
69
- deferredReason: "economics_not_computed_v0_20_23",
70
- hasData: false,
71
- }),
86
+ status: deriveVcStatus({ enabled: true, hasData: econ.computed }),
72
87
  };
73
88
  sendJson(res, 200, body);
74
89
  return true;
@@ -108,3 +108,87 @@ export async function seedEval(dir: string): Promise<void> {
108
108
  { session: "s1", seq: 4, event: "encode", value: 12, unit: "ms", mode: "A" },
109
109
  ]);
110
110
  }
111
+
112
+ /**
113
+ * Seed a LIVEWIRE aggregate snapshot directly on disk so the spawned dashboard
114
+ * server (a separate process) rehydrates real subsystem counts and its reader-only
115
+ * routes report LIVE (non-deferred) data. This is how the LIVEWIRE route tests
116
+ * prove the wiring: the runtimes' persisted counts are exactly what a reader
117
+ * process reconstructs. Counts + codes + triad mode only (SECURITY_PRIVACY).
118
+ */
119
+ export async function seedLivewireSnapshot(
120
+ dir: string,
121
+ overrides?: {
122
+ crystals?: Partial<Record<
123
+ "crystalCount" | "totalBytes" | "hits" | "misses" | "hitBytes" |
124
+ "writes" | "duplicateWrites" | "collisions",
125
+ number
126
+ >>;
127
+ diagnostics?: Partial<Record<
128
+ "profileMisses" | "rangeMisses" | "dependencyMisses" | "requestMisses" |
129
+ "generationMisses" | "unknownMisses" | "serveBlocked",
130
+ number
131
+ >> & { breakerState?: string };
132
+ economics?: {
133
+ computed?: boolean;
134
+ profileCount?: number;
135
+ provenExclusions?: number;
136
+ unprovenExclusions?: number;
137
+ };
138
+ policy?: {
139
+ shadowDecisions?: number;
140
+ clampedDecisions?: number;
141
+ rejectedInputs?: number;
142
+ pressureVersion?: number;
143
+ liveMutations?: number;
144
+ };
145
+ },
146
+ ): Promise<void> {
147
+ const { saveLivewireSnapshot } = await import(
148
+ "../../src/vector-cortex/livewire/livewire-snapshot.js"
149
+ );
150
+ const c = overrides?.crystals ?? {};
151
+ const d = overrides?.diagnostics ?? {};
152
+ const e = overrides?.economics ?? {};
153
+ const p = overrides?.policy ?? {};
154
+ saveLivewireSnapshot(dir, {
155
+ schema: "vector-cortex-livewire-v1",
156
+ crystals: {
157
+ mode: "A",
158
+ crystalCount: c.crystalCount ?? 3,
159
+ totalBytes: c.totalBytes ?? 1024,
160
+ hits: c.hits ?? 0,
161
+ misses: c.misses ?? 0,
162
+ hitBytes: c.hitBytes ?? 0,
163
+ writes: c.writes ?? 0,
164
+ duplicateWrites: c.duplicateWrites ?? 0,
165
+ collisions: c.collisions ?? 0,
166
+ },
167
+ diagnostics: {
168
+ profileMisses: d.profileMisses ?? 0,
169
+ rangeMisses: d.rangeMisses ?? 0,
170
+ dependencyMisses: d.dependencyMisses ?? 0,
171
+ requestMisses: d.requestMisses ?? 0,
172
+ generationMisses: d.generationMisses ?? 0,
173
+ unknownMisses: d.unknownMisses ?? 0,
174
+ serveBlocked: d.serveBlocked ?? 0,
175
+ breakerState: d.breakerState ?? "CLOSED_A",
176
+ lastFailure: null,
177
+ },
178
+ economics: {
179
+ profileCount: e.profileCount ?? 4,
180
+ provenExclusions: e.provenExclusions ?? 1,
181
+ unprovenExclusions: e.unprovenExclusions ?? 0,
182
+ computed: e.computed ?? true,
183
+ lastFailure: null,
184
+ },
185
+ policy: {
186
+ shadowDecisions: p.shadowDecisions ?? 0,
187
+ clampedDecisions: p.clampedDecisions ?? 0,
188
+ rejectedInputs: p.rejectedInputs ?? 0,
189
+ liveMutations: p.liveMutations ?? 0,
190
+ pressureVersion: (p.pressureVersion ?? 1) as 1 | 2,
191
+ lastFailure: null,
192
+ },
193
+ });
194
+ }
@@ -21,13 +21,18 @@ import type { RouteContext } from "./routes-core.js";
21
21
  import { VC8B_ENABLED } from "../../src/config.js";
22
22
  import { sendJson } from "./routes-vector-cortex-shared.js";
23
23
  import { deriveVcStatus } from "./vc-status.js";
24
+ import { readLivewireSnapshot } from "../../src/vector-cortex/livewire/livewire-registry.js";
24
25
  import type { VectorCortexPolicyView } from "./api-contracts/vector-cortex-policy.js";
25
26
 
27
+ // The flag-off deferred reason, kept byte-identical to the pre-LIVEWIRE
28
+ // predecessor (VC8B: `shadow_controller_not_instantiated_v0_20_23`).
29
+ const DEFERRED_REASON = "shadow_controller_not_instantiated_v0_20_23";
30
+
26
31
  /** GET /api/vector-cortex/policy — reader-only policy + shadow aggregate (VC8B). */
27
32
  export function handleVectorCortexPolicy(
28
33
  req: IncomingMessage,
29
34
  res: ServerResponse,
30
- _ctx: RouteContext,
35
+ ctx: RouteContext,
31
36
  ): boolean {
32
37
  const url = req.url ?? "";
33
38
  const path = url.split("?")[0] ?? url;
@@ -39,23 +44,37 @@ export function handleVectorCortexPolicy(
39
44
  }
40
45
 
41
46
  const enabled = VC8B_ENABLED();
42
- const mode: VectorCortexPolicyView["mode"] = enabled ? "A" : "C";
47
+ if (!enabled) {
48
+ // Flag-off parity: byte-identical to the predecessor (mode C + deferred).
49
+ const body: VectorCortexPolicyView = {
50
+ enabled: false,
51
+ mode: "C",
52
+ shadowDecisions: 0,
53
+ clampedDecisions: 0,
54
+ rejectedInputs: 0,
55
+ liveMutations: 0,
56
+ pressureVersion: 1,
57
+ lastFailure: null,
58
+ updatedAt: new Date().toISOString(),
59
+ deferredReason: DEFERRED_REASON,
60
+ status: deriveVcStatus({ enabled: false, hasData: false }),
61
+ };
62
+ sendJson(res, 200, body);
63
+ return true;
64
+ }
65
+
66
+ const policy = readLivewireSnapshot(ctx.stateDir).policy;
43
67
  const body: VectorCortexPolicyView = {
44
- enabled,
45
- mode,
46
- shadowDecisions: 0,
47
- clampedDecisions: 0,
48
- rejectedInputs: 0,
49
- liveMutations: 0,
50
- pressureVersion: 1,
51
- lastFailure: null,
68
+ enabled: true,
69
+ mode: "A",
70
+ shadowDecisions: policy.shadowDecisions,
71
+ clampedDecisions: policy.clampedDecisions,
72
+ rejectedInputs: policy.rejectedInputs,
73
+ liveMutations: policy.liveMutations,
74
+ pressureVersion: policy.pressureVersion,
75
+ lastFailure: policy.lastFailure,
52
76
  updatedAt: new Date().toISOString(),
53
- deferredReason: "shadow_controller_not_instantiated_v0_20_23",
54
- status: deriveVcStatus({
55
- enabled,
56
- deferredReason: "shadow_controller_not_instantiated_v0_20_23",
57
- hasData: false,
58
- }),
77
+ status: deriveVcStatus({ enabled: true, hasData: policy.shadowDecisions > 0 }),
59
78
  };
60
79
  sendJson(res, 200, body);
61
80
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.20.70",
3
+ "version": "0.20.71",
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",
@@ -78,6 +78,11 @@ export class CrystalStore {
78
78
  private writes = 0;
79
79
  private duplicateWrites = 0;
80
80
  private collisions = 0;
81
+ /** Restart-survival crystal count/bytes (LIVEWIRE rehydrate). The `committed`
82
+ * map is empty in a fresh process; these offsets carry the persisted totals
83
+ * so the dashboard does not reset to zero on restart. */
84
+ private rehydratedCrystalCount = 0;
85
+ private rehydratedTotalBytes = 0;
81
86
 
82
87
  /** Freeze a crystal object for a key/bytes pair (digest computed here). */
83
88
  static freeze(keyDigest: string, bytes: Uint8Array, key: CrystalV1["key"]): CrystalV1 {
@@ -196,11 +201,11 @@ export class CrystalStore {
196
201
 
197
202
  /** Reader-only aggregate for the dashboard seam — counts and bytes only. */
198
203
  stats(): CrystalStoreStats {
199
- let totalBytes = 0;
204
+ let totalBytes = this.rehydratedTotalBytes;
200
205
  for (const c of this.committed.values()) totalBytes += c.byteCount;
201
206
  return {
202
207
  mode: this.mode(),
203
- crystalCount: this.committed.size,
208
+ crystalCount: this.rehydratedCrystalCount + this.committed.size,
204
209
  totalBytes,
205
210
  hits: this.hits,
206
211
  misses: this.misses,
@@ -210,4 +215,23 @@ export class CrystalStore {
210
215
  collisions: this.collisions,
211
216
  };
212
217
  }
218
+
219
+ /**
220
+ * Restart survival: seed the CUMULATIVE counters from a previously-persisted
221
+ * aggregate (LIVEWIRE). The in-memory `committed` map is naturally empty in a
222
+ * fresh process — this only restores the running totals the dashboard reports,
223
+ * so a process restart does not reset the dashboard to zero. It deliberately
224
+ * does NOT reconstruct crystals: frozen bytes are never persisted to the
225
+ * reader aggregate, only the counts.
226
+ */
227
+ rehydrate(stats: CrystalStoreStats): void {
228
+ this.rehydratedCrystalCount = stats.crystalCount;
229
+ this.rehydratedTotalBytes = stats.totalBytes;
230
+ this.hits = stats.hits;
231
+ this.misses = stats.misses;
232
+ this.hitBytes = stats.hitBytes;
233
+ this.writes = stats.writes;
234
+ this.duplicateWrites = stats.duplicateWrites;
235
+ this.collisions = stats.collisions;
236
+ }
213
237
  }