pi-mega-compact 0.20.45 → 0.20.46

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,39 @@
1
+ /**
2
+ * config/vector-cortex-enc0d.ts — ENC-0d promotion gate over real trained assets.
3
+ *
4
+ * Extracted from vector-cortex.ts so that file stays under the 300-line soft
5
+ * limit (soft-as-hard gate), exactly as vector-cortex-enc0a.ts / enc0b.ts /
6
+ * enc0c.ts and the VC8C/VC9A-D/ML5A-E/DEDUP_ATTR siblings were. vector-cortex.ts
7
+ * re-exports the flag below and root src/config.ts re-exports it, so no
8
+ * consumer import path changes.
9
+ *
10
+ * ENC-0d turns the ML5-E promotion gate into the real-asset promotion path:
11
+ * it accepts a `{color}` real candidate manifest (the ENC-0c trained head
12
+ * weights + the ENC-0b trunk, staged under
13
+ * ~/.pi/mega-compact-encoder/candidates/), digest-verifies every staged byte
14
+ * before any swap, and performs an atomic asset swap with rollback-to-previous
15
+ * on qualification failure. The extension itself never stages candidate assets —
16
+ * only the operator trains them. When the flag is ON and a digest-verified
17
+ * green candidate exists, an atomic swap is performed and the runtime flips into
18
+ * qualified mode A; a red qualification (or any verification failure) keeps the
19
+ * prior asset live and emits a demotion.
20
+ *
21
+ * `MEGACOMPACT_ENC_0D=0` accepts no candidate, swaps nothing, emits nothing, and
22
+ * the shipped manifest stays at the ENC-0c survivor — byte-identical predecessor.
23
+ *
24
+ * The split is purely mechanical: ENC_0D_ENABLED follows ENC_0C_ENABLED in name,
25
+ * semantics, and default, and vector-cortex.ts re-exports it so every existing
26
+ * `from "./config/vector-cortex.js"` import keeps resolving unchanged.
27
+ *
28
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 / PREVENT-011).
29
+ */
30
+ import { sprintFlag } from "./vector-cortex-flag.js";
31
+ /**
32
+ * ENC-0d — atomic real-asset promotion over trained candidates. Default ON.
33
+ * `MEGACOMPACT_ENC_0D=0` disables and is byte-identical to the predecessor
34
+ * (ENC-0c): no candidate is accepted, no swap is performed, no promote/
35
+ * demote/rollback events are emitted, and the shipped manifest stays at the
36
+ * ENC-0c survivor. This flag MUST also be a dashboard SETTINGS toggle (visible
37
+ * in config UI, never in EXCLUDED_SETTINGS), mirroring ENC_0A/ENC_0B/ENC_0C.
38
+ */
39
+ export const ENC_0D_ENABLED = () => sprintFlag("MEGACOMPACT_ENC_0D");
@@ -252,5 +252,6 @@ export { DEDUP_ATTR_ENABLED } from "./vector-cortex-dedup-attr.js";
252
252
  export { ENC_0A_ENABLED } from "./vector-cortex-enc0a.js";
253
253
  export { ENC_0B_ENABLED } from "./vector-cortex-enc0b.js";
254
254
  export { ENC_0C_ENABLED } from "./vector-cortex-enc0c.js";
255
+ export { ENC_0D_ENABLED } from "./vector-cortex-enc0d.js";
255
256
  // Breaker constants (TRIAD_RESILIENCE.md §breaker) extracted to vector-cortex-breakers.ts.
256
257
  export { BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./vector-cortex-breakers.js";
package/dist/config.js CHANGED
@@ -114,4 +114,4 @@ export const NEW_UI = () => ragEnabled("MEGACOMPACT_NEW_UI");
114
114
  // default ON, `=0`/`_DISABLED` off. Re-exported from src/config/vector-cortex.ts
115
115
  // so root consumers share one source of truth.
116
116
  // ---------------------------------------------------------------------------
117
- export { VC0A_ENABLED, VC0B_ENABLED, VC1A_ENABLED, VC0C_ENABLED, VC1B_ENABLED, VC1C_ENABLED, VC2A_ENABLED, VC2B_ENABLED, VC2C_ENABLED, VC3A_ENABLED, VC3B_ENABLED, VC3C_ENABLED, VC4A_ENABLED, VC4B_ENABLED, VC4C_ENABLED, VC5A_ENABLED, VC5B_ENABLED, VC5C_ENABLED, VC6A_ENABLED, VC6B_ENABLED, VC6C_ENABLED, VC7A_ENABLED, VC7B_ENABLED, VC7C_ENABLED, VC8A_ENABLED, VC8B_ENABLED, VC8C_ENABLED, VC9A_ENABLED, VC9B_ENABLED, VC9C_ENABLED, VC9D_ENABLED, PCC_ENABLED, ML5A_ENABLED, ML5B_ENABLED, ML5C_ENABLED, ML5D_ENABLED, ML5E_ENABLED, DEDUP_ATTR_ENABLED, ENC_0A_ENABLED, ENC_0B_ENABLED, ENC_0C_ENABLED, BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./config/vector-cortex.js";
117
+ export { VC0A_ENABLED, VC0B_ENABLED, VC1A_ENABLED, VC0C_ENABLED, VC1B_ENABLED, VC1C_ENABLED, VC2A_ENABLED, VC2B_ENABLED, VC2C_ENABLED, VC3A_ENABLED, VC3B_ENABLED, VC3C_ENABLED, VC4A_ENABLED, VC4B_ENABLED, VC4C_ENABLED, VC5A_ENABLED, VC5B_ENABLED, VC5C_ENABLED, VC6A_ENABLED, VC6B_ENABLED, VC6C_ENABLED, VC7A_ENABLED, VC7B_ENABLED, VC7C_ENABLED, VC8A_ENABLED, VC8B_ENABLED, VC8C_ENABLED, VC9A_ENABLED, VC9B_ENABLED, VC9C_ENABLED, VC9D_ENABLED, PCC_ENABLED, ML5A_ENABLED, ML5B_ENABLED, ML5C_ENABLED, ML5D_ENABLED, ML5E_ENABLED, DEDUP_ATTR_ENABLED, ENC_0A_ENABLED, ENC_0B_ENABLED, ENC_0C_ENABLED, ENC_0D_ENABLED, BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./config/vector-cortex.js";
@@ -61,5 +61,6 @@ export const VECTOR_CORTEX_SETTINGS = {
61
61
  boolDirect("MEGACOMPACT_ENC_0A", "ENC-0a Encoder Backend Decision", "ENC-0a learned-encoder backend-decision lock: records the transformers.js/WASM vs onnxruntime-node choice, per-platform install matrix, opset-21 baseline and pinned digests in docs/vector-cortex/encoder-backend-decision.md. OFF = no decision record written / no resolver runs, mode B trigram byte-identical predecessor.", true),
62
62
  boolDirect("MEGACOMPACT_ENC_0B", "ENC-0b Real Trunk Fetch & Gated Path", "ENC-0b real ONNX trunk fetch + gated inference: replaces the LCG placeholder with the real bge-small int8 model through an ONNX InferenceSession (onnxruntime-web WASM). OFF = LCG placeholder serves byte-identical predecessor output, no ONNX session built.", true),
63
63
  boolDirect("MEGACOMPACT_ENC_0C", "ENC-0c Five-Head Supervision Transfer", "ENC-0c five-head supervision transfer on the frozen bge-small trunk: when a developer-trained head candidate is staged under ~/.pi/mega-compact-encoder/candidates/, the five heads serve the trained weights. OFF = no candidate is loaded, the heads serve byte-identical ENC-0b survivor defaults.", true),
64
+ boolDirect("MEGACOMPACT_ENC_0D", "ENC-0d Real-Asset Promotion Gate", "ENC-0d promotion gate over real trained assets: accepts a {color} real candidate manifest (the ENC-0c trained head weights + the ENC-0b trunk), digest-verifies every staged byte, and atomically swaps the shipped manifest to a green candidate with rollback-to-previous on qualification failure. OFF = accepts no candidate, swaps nothing, emits nothing, shipped manifest stays at the ENC-0c survivor.", true),
64
65
  ],
65
66
  };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * config/vector-cortex-enc0d.ts — ENC-0d promotion gate over real trained assets.
3
+ *
4
+ * Extracted from vector-cortex.ts so that file stays under the 300-line soft
5
+ * limit (soft-as-hard gate), exactly as vector-cortex-enc0a.ts / enc0b.ts /
6
+ * enc0c.ts and the VC8C/VC9A-D/ML5A-E/DEDUP_ATTR siblings were. vector-cortex.ts
7
+ * re-exports the flag below and root src/config.ts re-exports it, so no
8
+ * consumer import path changes.
9
+ *
10
+ * ENC-0d turns the ML5-E promotion gate into the real-asset promotion path:
11
+ * it accepts a `{color}` real candidate manifest (the ENC-0c trained head
12
+ * weights + the ENC-0b trunk, staged under
13
+ * ~/.pi/mega-compact-encoder/candidates/), digest-verifies every staged byte
14
+ * before any swap, and performs an atomic asset swap with rollback-to-previous
15
+ * on qualification failure. The extension itself never stages candidate assets —
16
+ * only the operator trains them. When the flag is ON and a digest-verified
17
+ * green candidate exists, an atomic swap is performed and the runtime flips into
18
+ * qualified mode A; a red qualification (or any verification failure) keeps the
19
+ * prior asset live and emits a demotion.
20
+ *
21
+ * `MEGACOMPACT_ENC_0D=0` accepts no candidate, swaps nothing, emits nothing, and
22
+ * the shipped manifest stays at the ENC-0c survivor — byte-identical predecessor.
23
+ *
24
+ * The split is purely mechanical: ENC_0D_ENABLED follows ENC_0C_ENABLED in name,
25
+ * semantics, and default, and vector-cortex.ts re-exports it so every existing
26
+ * `from "./config/vector-cortex.js"` import keeps resolving unchanged.
27
+ *
28
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 / PREVENT-011).
29
+ */
30
+ import { sprintFlag } from "./vector-cortex-flag.js";
31
+ /**
32
+ * ENC-0d — atomic real-asset promotion over trained candidates. Default ON.
33
+ * `MEGACOMPACT_ENC_0D=0` disables and is byte-identical to the predecessor
34
+ * (ENC-0c): no candidate is accepted, no swap is performed, no promote/
35
+ * demote/rollback events are emitted, and the shipped manifest stays at the
36
+ * ENC-0c survivor. This flag MUST also be a dashboard SETTINGS toggle (visible
37
+ * in config UI, never in EXCLUDED_SETTINGS), mirroring ENC_0A/ENC_0B/ENC_0C.
38
+ */
39
+ export const ENC_0D_ENABLED = () => sprintFlag("MEGACOMPACT_ENC_0D");
@@ -252,5 +252,6 @@ export { DEDUP_ATTR_ENABLED } from "./vector-cortex-dedup-attr.js";
252
252
  export { ENC_0A_ENABLED } from "./vector-cortex-enc0a.js";
253
253
  export { ENC_0B_ENABLED } from "./vector-cortex-enc0b.js";
254
254
  export { ENC_0C_ENABLED } from "./vector-cortex-enc0c.js";
255
+ export { ENC_0D_ENABLED } from "./vector-cortex-enc0d.js";
255
256
  // Breaker constants (TRIAD_RESILIENCE.md §breaker) extracted to vector-cortex-breakers.ts.
256
257
  export { BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./vector-cortex-breakers.js";
@@ -114,4 +114,4 @@ export const NEW_UI = () => ragEnabled("MEGACOMPACT_NEW_UI");
114
114
  // default ON, `=0`/`_DISABLED` off. Re-exported from src/config/vector-cortex.ts
115
115
  // so root consumers share one source of truth.
116
116
  // ---------------------------------------------------------------------------
117
- export { VC0A_ENABLED, VC0B_ENABLED, VC1A_ENABLED, VC0C_ENABLED, VC1B_ENABLED, VC1C_ENABLED, VC2A_ENABLED, VC2B_ENABLED, VC2C_ENABLED, VC3A_ENABLED, VC3B_ENABLED, VC3C_ENABLED, VC4A_ENABLED, VC4B_ENABLED, VC4C_ENABLED, VC5A_ENABLED, VC5B_ENABLED, VC5C_ENABLED, VC6A_ENABLED, VC6B_ENABLED, VC6C_ENABLED, VC7A_ENABLED, VC7B_ENABLED, VC7C_ENABLED, VC8A_ENABLED, VC8B_ENABLED, VC8C_ENABLED, VC9A_ENABLED, VC9B_ENABLED, VC9C_ENABLED, VC9D_ENABLED, PCC_ENABLED, ML5A_ENABLED, ML5B_ENABLED, ML5C_ENABLED, ML5D_ENABLED, ML5E_ENABLED, DEDUP_ATTR_ENABLED, ENC_0A_ENABLED, ENC_0B_ENABLED, ENC_0C_ENABLED, BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./config/vector-cortex.js";
117
+ export { VC0A_ENABLED, VC0B_ENABLED, VC1A_ENABLED, VC0C_ENABLED, VC1B_ENABLED, VC1C_ENABLED, VC2A_ENABLED, VC2B_ENABLED, VC2C_ENABLED, VC3A_ENABLED, VC3B_ENABLED, VC3C_ENABLED, VC4A_ENABLED, VC4B_ENABLED, VC4C_ENABLED, VC5A_ENABLED, VC5B_ENABLED, VC5C_ENABLED, VC6A_ENABLED, VC6B_ENABLED, VC6C_ENABLED, VC7A_ENABLED, VC7B_ENABLED, VC7C_ENABLED, VC8A_ENABLED, VC8B_ENABLED, VC8C_ENABLED, VC9A_ENABLED, VC9B_ENABLED, VC9C_ENABLED, VC9D_ENABLED, PCC_ENABLED, ML5A_ENABLED, ML5B_ENABLED, ML5C_ENABLED, ML5D_ENABLED, ML5E_ENABLED, DEDUP_ATTR_ENABLED, ENC_0A_ENABLED, ENC_0B_ENABLED, ENC_0C_ENABLED, ENC_0D_ENABLED, BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./config/vector-cortex.js";
@@ -0,0 +1,40 @@
1
+ /**
2
+ * vector-cortex/encoder/promotion-emit.ts — ENC-0d promotion event writers.
3
+ *
4
+ * Writes the three real-asset promotion events to the monitoring `events.log`
5
+ * as structured append-only JSON lines via the existing `logBenchEvent` seam
6
+ * (`src/monitoring.ts`):
7
+ *
8
+ * - `vector_cortex_asset_promoted` (green digest-verified swap)
9
+ * - `vector_cortex_asset_demoted` (red qualification / digest failure)
10
+ * - `vector_cortex_asset_rollback_back` (restore of a prior stack digest)
11
+ *
12
+ * Non-fatal and log-and-swallow: a write failure never breaks the agent loop
13
+ * (best-effort store/write contract). Each line is `{ts, event, ...fields}`
14
+ * shaped so the dashboard live-stream tail and evidence tooling parse it
15
+ * identically to every other monitoring event. Events carry digests, colors
16
+ * and verdicts ONLY — never message content (EVAL-REDACT-002).
17
+ *
18
+ * The path defaults to the standard state-dir events.log (mirrors
19
+ * defaultEventsPath). Zero network, no `any` (PREVENT-PI-004 / PREVENT-011).
20
+ */
21
+ import { defaultEventsPath, logBenchEvent } from "../../monitoring.js";
22
+ import { getStateDir } from "../../store.js";
23
+ /** Default events.log beside the state dir (mirrors bench.ts). */
24
+ export function promotionEventsPath(stateDir = getStateDir()) {
25
+ return defaultEventsPath(stateDir);
26
+ }
27
+ const EVENT_NAME = {
28
+ promoted: "vector_cortex_asset_promoted",
29
+ demoted: "vector_cortex_asset_demoted",
30
+ rollback_back: "vector_cortex_asset_rollback_back",
31
+ };
32
+ /**
33
+ * Append one promotion event to events.log (best-effort, non-fatal). The line
34
+ * is `{ts, event, ...fields}` shaped, written append-only via the monitoring
35
+ * logBenchEvent seam. On any write failure the event is swallowed — the agent
36
+ * loop is never broken.
37
+ */
38
+ export function appendPromotionEvent(path, kind, fields) {
39
+ logBenchEvent(path, EVENT_NAME[kind], { ...fields });
40
+ }
@@ -14,6 +14,15 @@
14
14
  * an atomic digest swap (flip the committed pointer to the prior entry's
15
15
  * digest in one step) — no partial state is possible because the swap is a
16
16
  * single value assignment.
17
+ *
18
+ * ENC-0d extends the ledger to the real-asset promotion path: `PromotionV1`
19
+ * carries `{color, assetDigestStack}` — a promotion color from gate
20
+ * qualification ("green" atomically swaps to the trained asset, "red" keeps
21
+ * the prior asset live) and a LIFO of prior shipped digest for O(1)-by-sha256
22
+ * rollback. The on-disk manifest byte-swap itself (write-temp-then-rename,
23
+ * never in-place partial) is the promotion gate script's job; these helpers
24
+ * stay pure and return the restorable digest + the updated manifest/stack so
25
+ * the script can perform the swap in one atomic step.
17
26
  */
18
27
  // ---------------------------------------------------------------------------
19
28
  // PromotionV1 — the ledger row recording one nightly training + promotion run.
@@ -42,6 +51,61 @@ export function rollbackTo(manifest, digest) {
42
51
  };
43
52
  }
44
53
  // ---------------------------------------------------------------------------
54
+ // ENC-0d LIFO rollback stack + atomic swap. Pure pointer arithmetic; the
55
+ // on-disk write-temp-then-rename of the swapped manifest bytes is the gate
56
+ // script's job so no partial state can ever be observed (PREVENT-PI-004).
57
+ // ---------------------------------------------------------------------------
58
+ /** Push a freshly-shipped digest onto the LIFO rollback stack (top = latest
59
+ * prior shipped digest). Non-mutating: returns a new array. */
60
+ export function pushAssetDigest(stack, digest) {
61
+ return [...stack, digest];
62
+ }
63
+ /** Pop the top (most recent prior) digest off the LIFO rollback stack without
64
+ * mutating it. Returns the prior digest (null when the stack is empty) plus
65
+ * the remaining stack for the caller to commit (atomic card-swap fallback:
66
+ * a green swap pushes the incumbent digest; a rollback pops it). */
67
+ export function popAssetDigest(stack) {
68
+ if (stack.length === 0)
69
+ return { prior: null, rest: stack };
70
+ const rest = stack.slice(0, -1);
71
+ return { prior: stack[stack.length - 1], rest };
72
+ }
73
+ /** Atomically swap the committed manifest to a real trained asset's digest.
74
+ * Green: append the new entry (append-only — the incumbent stays in the
75
+ * ledger, never overwritten), commit to it, and record the INCUMBENT digest
76
+ * onto the LIFO stack for O(1) rollback (rollback restores the incumbent, so
77
+ * the stack must hold the pre-swap committed digest — pushing the swapped-in
78
+ * digest would point rollback back at the very asset being rolled back). Red
79
+ * (or no candidate digest): return the manifest and stack untouched — no
80
+ * swap, prior asset stays live. */
81
+ export function atomicSwap(manifest, entry, color, stack) {
82
+ if (color !== "green" || !entry.assetDigest) {
83
+ return { manifest, stack, swapped: false };
84
+ }
85
+ const incumbent = manifest.committed;
86
+ const next = appendAsset(manifest, entry);
87
+ return {
88
+ manifest: next,
89
+ stack: incumbent === null ? stack : pushAssetDigest(stack, incumbent),
90
+ swapped: true,
91
+ };
92
+ }
93
+ /** Roll back to the previous shipped asset by SHA-256 (O(1) via the LIFO
94
+ * stack): restores the most recent prior digest in the manifest and pops that
95
+ * entry off the stack. Returns null when no prior digest exists or the prior
96
+ * digest is no longer in the append-only manifest (defensive: append-only
97
+ * means it should always be findable). Never leaves a partial state — the
98
+ * committed pointer flip and the stack pop happen together or not at all. */
99
+ export function assetRollback(manifest, stack) {
100
+ const { prior, rest } = popAssetDigest(stack);
101
+ if (prior === null)
102
+ return null;
103
+ const restored = rollbackTo(manifest, prior);
104
+ if (!restored)
105
+ return null;
106
+ return { manifest: restored, stack: rest, swapped: true };
107
+ }
108
+ // ---------------------------------------------------------------------------
45
109
  // Pure decision rules.
46
110
  // ---------------------------------------------------------------------------
47
111
  /** Evaluate the promotion gate: promote only when all five heads pass AND the
@@ -0,0 +1,40 @@
1
+ /**
2
+ * vector-cortex/encoder/promotion-emit.ts — ENC-0d promotion event writers.
3
+ *
4
+ * Writes the three real-asset promotion events to the monitoring `events.log`
5
+ * as structured append-only JSON lines via the existing `logBenchEvent` seam
6
+ * (`src/monitoring.ts`):
7
+ *
8
+ * - `vector_cortex_asset_promoted` (green digest-verified swap)
9
+ * - `vector_cortex_asset_demoted` (red qualification / digest failure)
10
+ * - `vector_cortex_asset_rollback_back` (restore of a prior stack digest)
11
+ *
12
+ * Non-fatal and log-and-swallow: a write failure never breaks the agent loop
13
+ * (best-effort store/write contract). Each line is `{ts, event, ...fields}`
14
+ * shaped so the dashboard live-stream tail and evidence tooling parse it
15
+ * identically to every other monitoring event. Events carry digests, colors
16
+ * and verdicts ONLY — never message content (EVAL-REDACT-002).
17
+ *
18
+ * The path defaults to the standard state-dir events.log (mirrors
19
+ * defaultEventsPath). Zero network, no `any` (PREVENT-PI-004 / PREVENT-011).
20
+ */
21
+ import { defaultEventsPath, logBenchEvent } from "../../monitoring.js";
22
+ import { getStateDir } from "../../store.js";
23
+ /** Default events.log beside the state dir (mirrors bench.ts). */
24
+ export function promotionEventsPath(stateDir = getStateDir()) {
25
+ return defaultEventsPath(stateDir);
26
+ }
27
+ const EVENT_NAME = {
28
+ promoted: "vector_cortex_asset_promoted",
29
+ demoted: "vector_cortex_asset_demoted",
30
+ rollback_back: "vector_cortex_asset_rollback_back",
31
+ };
32
+ /**
33
+ * Append one promotion event to events.log (best-effort, non-fatal). The line
34
+ * is `{ts, event, ...fields}` shaped, written append-only via the monitoring
35
+ * logBenchEvent seam. On any write failure the event is swallowed — the agent
36
+ * loop is never broken.
37
+ */
38
+ export function appendPromotionEvent(path, kind, fields) {
39
+ logBenchEvent(path, EVENT_NAME[kind], { ...fields });
40
+ }
@@ -14,6 +14,15 @@
14
14
  * an atomic digest swap (flip the committed pointer to the prior entry's
15
15
  * digest in one step) — no partial state is possible because the swap is a
16
16
  * single value assignment.
17
+ *
18
+ * ENC-0d extends the ledger to the real-asset promotion path: `PromotionV1`
19
+ * carries `{color, assetDigestStack}` — a promotion color from gate
20
+ * qualification ("green" atomically swaps to the trained asset, "red" keeps
21
+ * the prior asset live) and a LIFO of prior shipped digest for O(1)-by-sha256
22
+ * rollback. The on-disk manifest byte-swap itself (write-temp-then-rename,
23
+ * never in-place partial) is the promotion gate script's job; these helpers
24
+ * stay pure and return the restorable digest + the updated manifest/stack so
25
+ * the script can perform the swap in one atomic step.
17
26
  */
18
27
  // ---------------------------------------------------------------------------
19
28
  // PromotionV1 — the ledger row recording one nightly training + promotion run.
@@ -42,6 +51,61 @@ export function rollbackTo(manifest, digest) {
42
51
  };
43
52
  }
44
53
  // ---------------------------------------------------------------------------
54
+ // ENC-0d LIFO rollback stack + atomic swap. Pure pointer arithmetic; the
55
+ // on-disk write-temp-then-rename of the swapped manifest bytes is the gate
56
+ // script's job so no partial state can ever be observed (PREVENT-PI-004).
57
+ // ---------------------------------------------------------------------------
58
+ /** Push a freshly-shipped digest onto the LIFO rollback stack (top = latest
59
+ * prior shipped digest). Non-mutating: returns a new array. */
60
+ export function pushAssetDigest(stack, digest) {
61
+ return [...stack, digest];
62
+ }
63
+ /** Pop the top (most recent prior) digest off the LIFO rollback stack without
64
+ * mutating it. Returns the prior digest (null when the stack is empty) plus
65
+ * the remaining stack for the caller to commit (atomic card-swap fallback:
66
+ * a green swap pushes the incumbent digest; a rollback pops it). */
67
+ export function popAssetDigest(stack) {
68
+ if (stack.length === 0)
69
+ return { prior: null, rest: stack };
70
+ const rest = stack.slice(0, -1);
71
+ return { prior: stack[stack.length - 1], rest };
72
+ }
73
+ /** Atomically swap the committed manifest to a real trained asset's digest.
74
+ * Green: append the new entry (append-only — the incumbent stays in the
75
+ * ledger, never overwritten), commit to it, and record the INCUMBENT digest
76
+ * onto the LIFO stack for O(1) rollback (rollback restores the incumbent, so
77
+ * the stack must hold the pre-swap committed digest — pushing the swapped-in
78
+ * digest would point rollback back at the very asset being rolled back). Red
79
+ * (or no candidate digest): return the manifest and stack untouched — no
80
+ * swap, prior asset stays live. */
81
+ export function atomicSwap(manifest, entry, color, stack) {
82
+ if (color !== "green" || !entry.assetDigest) {
83
+ return { manifest, stack, swapped: false };
84
+ }
85
+ const incumbent = manifest.committed;
86
+ const next = appendAsset(manifest, entry);
87
+ return {
88
+ manifest: next,
89
+ stack: incumbent === null ? stack : pushAssetDigest(stack, incumbent),
90
+ swapped: true,
91
+ };
92
+ }
93
+ /** Roll back to the previous shipped asset by SHA-256 (O(1) via the LIFO
94
+ * stack): restores the most recent prior digest in the manifest and pops that
95
+ * entry off the stack. Returns null when no prior digest exists or the prior
96
+ * digest is no longer in the append-only manifest (defensive: append-only
97
+ * means it should always be findable). Never leaves a partial state — the
98
+ * committed pointer flip and the stack pop happen together or not at all. */
99
+ export function assetRollback(manifest, stack) {
100
+ const { prior, rest } = popAssetDigest(stack);
101
+ if (prior === null)
102
+ return null;
103
+ const restored = rollbackTo(manifest, prior);
104
+ if (!restored)
105
+ return null;
106
+ return { manifest: restored, stack: rest, swapped: true };
107
+ }
108
+ // ---------------------------------------------------------------------------
45
109
  // Pure decision rules.
46
110
  // ---------------------------------------------------------------------------
47
111
  /** Evaluate the promotion gate: promote only when all five heads pass AND the
@@ -270,5 +270,11 @@ export const VECTOR_CORTEX_SETTINGS: SettingGroup = {
270
270
  "ENC-0c five-head supervision transfer on the frozen bge-small trunk: when a developer-trained head candidate is staged under ~/.pi/mega-compact-encoder/candidates/, the five heads serve the trained weights. OFF = no candidate is loaded, the heads serve byte-identical ENC-0b survivor defaults.",
271
271
  true,
272
272
  ),
273
+ boolDirect(
274
+ "MEGACOMPACT_ENC_0D",
275
+ "ENC-0d Real-Asset Promotion Gate",
276
+ "ENC-0d promotion gate over real trained assets: accepts a {color} real candidate manifest (the ENC-0c trained head weights + the ENC-0b trunk), digest-verifies every staged byte, and atomically swaps the shipped manifest to a green candidate with rollback-to-previous on qualification failure. OFF = accepts no candidate, swaps nothing, emits nothing, shipped manifest stays at the ENC-0c survivor.",
277
+ true,
278
+ ),
273
279
  ],
274
280
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.20.45",
3
+ "version": "0.20.46",
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,41 @@
1
+ /**
2
+ * config/vector-cortex-enc0d.ts — ENC-0d promotion gate over real trained assets.
3
+ *
4
+ * Extracted from vector-cortex.ts so that file stays under the 300-line soft
5
+ * limit (soft-as-hard gate), exactly as vector-cortex-enc0a.ts / enc0b.ts /
6
+ * enc0c.ts and the VC8C/VC9A-D/ML5A-E/DEDUP_ATTR siblings were. vector-cortex.ts
7
+ * re-exports the flag below and root src/config.ts re-exports it, so no
8
+ * consumer import path changes.
9
+ *
10
+ * ENC-0d turns the ML5-E promotion gate into the real-asset promotion path:
11
+ * it accepts a `{color}` real candidate manifest (the ENC-0c trained head
12
+ * weights + the ENC-0b trunk, staged under
13
+ * ~/.pi/mega-compact-encoder/candidates/), digest-verifies every staged byte
14
+ * before any swap, and performs an atomic asset swap with rollback-to-previous
15
+ * on qualification failure. The extension itself never stages candidate assets —
16
+ * only the operator trains them. When the flag is ON and a digest-verified
17
+ * green candidate exists, an atomic swap is performed and the runtime flips into
18
+ * qualified mode A; a red qualification (or any verification failure) keeps the
19
+ * prior asset live and emits a demotion.
20
+ *
21
+ * `MEGACOMPACT_ENC_0D=0` accepts no candidate, swaps nothing, emits nothing, and
22
+ * the shipped manifest stays at the ENC-0c survivor — byte-identical predecessor.
23
+ *
24
+ * The split is purely mechanical: ENC_0D_ENABLED follows ENC_0C_ENABLED in name,
25
+ * semantics, and default, and vector-cortex.ts re-exports it so every existing
26
+ * `from "./config/vector-cortex.js"` import keeps resolving unchanged.
27
+ *
28
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 / PREVENT-011).
29
+ */
30
+
31
+ import { sprintFlag } from "./vector-cortex-flag.js";
32
+
33
+ /**
34
+ * ENC-0d — atomic real-asset promotion over trained candidates. Default ON.
35
+ * `MEGACOMPACT_ENC_0D=0` disables and is byte-identical to the predecessor
36
+ * (ENC-0c): no candidate is accepted, no swap is performed, no promote/
37
+ * demote/rollback events are emitted, and the shipped manifest stays at the
38
+ * ENC-0c survivor. This flag MUST also be a dashboard SETTINGS toggle (visible
39
+ * in config UI, never in EXCLUDED_SETTINGS), mirroring ENC_0A/ENC_0B/ENC_0C.
40
+ */
41
+ export const ENC_0D_ENABLED = (): boolean => sprintFlag("MEGACOMPACT_ENC_0D");
@@ -7,7 +7,6 @@
7
7
  *
8
8
  * Breaker/triad constants (TRIAD_RESILIENCE.md) live here; pi-agnostic, dep-free.
9
9
  */
10
-
11
10
  import { sprintFlag } from "./vector-cortex-flag.js";
12
11
 
13
12
  // VC0/VC1/VC2 foundation-phase flags in vector-cortex-early.ts; re-exported
@@ -282,6 +281,7 @@ export { DEDUP_ATTR_ENABLED } from "./vector-cortex-dedup-attr.js";
282
281
  export { ENC_0A_ENABLED } from "./vector-cortex-enc0a.js";
283
282
  export { ENC_0B_ENABLED } from "./vector-cortex-enc0b.js";
284
283
  export { ENC_0C_ENABLED } from "./vector-cortex-enc0c.js";
284
+ export { ENC_0D_ENABLED } from "./vector-cortex-enc0d.js";
285
285
  // Breaker constants (TRIAD_RESILIENCE.md §breaker) extracted to vector-cortex-breakers.ts.
286
286
  export {
287
287
  BREAKER_WINDOW_MS,
package/src/config.ts CHANGED
@@ -192,6 +192,7 @@ export {
192
192
  ENC_0A_ENABLED,
193
193
  ENC_0B_ENABLED,
194
194
  ENC_0C_ENABLED,
195
+ ENC_0D_ENABLED,
195
196
  BREAKER_WINDOW_MS,
196
197
  BREAKER_MIN_ATTEMPTS,
197
198
  BREAKER_PERF_FAILURES,
@@ -0,0 +1,63 @@
1
+ /**
2
+ * vector-cortex/encoder/promotion-emit.ts — ENC-0d promotion event writers.
3
+ *
4
+ * Writes the three real-asset promotion events to the monitoring `events.log`
5
+ * as structured append-only JSON lines via the existing `logBenchEvent` seam
6
+ * (`src/monitoring.ts`):
7
+ *
8
+ * - `vector_cortex_asset_promoted` (green digest-verified swap)
9
+ * - `vector_cortex_asset_demoted` (red qualification / digest failure)
10
+ * - `vector_cortex_asset_rollback_back` (restore of a prior stack digest)
11
+ *
12
+ * Non-fatal and log-and-swallow: a write failure never breaks the agent loop
13
+ * (best-effort store/write contract). Each line is `{ts, event, ...fields}`
14
+ * shaped so the dashboard live-stream tail and evidence tooling parse it
15
+ * identically to every other monitoring event. Events carry digests, colors
16
+ * and verdicts ONLY — never message content (EVAL-REDACT-002).
17
+ *
18
+ * The path defaults to the standard state-dir events.log (mirrors
19
+ * defaultEventsPath). Zero network, no `any` (PREVENT-PI-004 / PREVENT-011).
20
+ */
21
+
22
+ import { defaultEventsPath, logBenchEvent } from "../../monitoring.js";
23
+ import { getStateDir } from "../../store.js";
24
+
25
+ /** Default events.log beside the state dir (mirrors bench.ts). */
26
+ export function promotionEventsPath(stateDir: string = getStateDir()): string {
27
+ return defaultEventsPath(stateDir);
28
+ }
29
+
30
+ /** The promotion event kinds this emitter can write. */
31
+ export type PromotionEventKind =
32
+ | "promoted"
33
+ | "demoted"
34
+ | "rollback_back";
35
+
36
+ const EVENT_NAME: Record<PromotionEventKind, string> = {
37
+ promoted: "vector_cortex_asset_promoted",
38
+ demoted: "vector_cortex_asset_demoted",
39
+ rollback_back: "vector_cortex_asset_rollback_back",
40
+ };
41
+
42
+ /** Fields carried by a promotion event: digests, colors and verdicts only.
43
+ * Never any payload or message content (EVAL-REDACT-002). */
44
+ export interface PromotionEventFields {
45
+ readonly color: "green" | "red";
46
+ readonly assetDigest: string | null;
47
+ readonly priorAssetDigest: string | null;
48
+ readonly verdict: string;
49
+ }
50
+
51
+ /**
52
+ * Append one promotion event to events.log (best-effort, non-fatal). The line
53
+ * is `{ts, event, ...fields}` shaped, written append-only via the monitoring
54
+ * logBenchEvent seam. On any write failure the event is swallowed — the agent
55
+ * loop is never broken.
56
+ */
57
+ export function appendPromotionEvent(
58
+ path: string,
59
+ kind: PromotionEventKind,
60
+ fields: PromotionEventFields,
61
+ ): void {
62
+ logBenchEvent(path, EVENT_NAME[kind], { ...fields });
63
+ }
@@ -14,6 +14,15 @@
14
14
  * an atomic digest swap (flip the committed pointer to the prior entry's
15
15
  * digest in one step) — no partial state is possible because the swap is a
16
16
  * single value assignment.
17
+ *
18
+ * ENC-0d extends the ledger to the real-asset promotion path: `PromotionV1`
19
+ * carries `{color, assetDigestStack}` — a promotion color from gate
20
+ * qualification ("green" atomically swaps to the trained asset, "red" keeps
21
+ * the prior asset live) and a LIFO of prior shipped digest for O(1)-by-sha256
22
+ * rollback. The on-disk manifest byte-swap itself (write-temp-then-rename,
23
+ * never in-place partial) is the promotion gate script's job; these helpers
24
+ * stay pure and return the restorable digest + the updated manifest/stack so
25
+ * the script can perform the swap in one atomic step.
17
26
  */
18
27
 
19
28
  // ---------------------------------------------------------------------------
@@ -54,6 +63,13 @@ export interface PromotionV1 {
54
63
  readonly verdict: "promoted" | "demoted" | "noop";
55
64
  /** Present only when verdict is "demoted": the demotion event name. */
56
65
  readonly demotedEvent: "demoted_new_asset" | null;
66
+ /** ENC-0d promotion color from gate qualification. "green" atomically swaps
67
+ * the shipped manifest to the trained asset; "red" keeps the prior asset
68
+ * live and emits a demotion. */
69
+ readonly color: "green" | "red";
70
+ /** ENC-0d LIFO of prior shipped asset digests for O(1)-by-sha256 rollback.
71
+ * The most recent prior digest (top of the stack) is the rollback target. */
72
+ readonly assetDigestStack: readonly string[];
57
73
  }
58
74
 
59
75
  // ---------------------------------------------------------------------------
@@ -104,6 +120,85 @@ export function rollbackTo(
104
120
  };
105
121
  }
106
122
 
123
+ // ---------------------------------------------------------------------------
124
+ // ENC-0d LIFO rollback stack + atomic swap. Pure pointer arithmetic; the
125
+ // on-disk write-temp-then-rename of the swapped manifest bytes is the gate
126
+ // script's job so no partial state can ever be observed (PREVENT-PI-004).
127
+ // ---------------------------------------------------------------------------
128
+
129
+ /** Push a freshly-shipped digest onto the LIFO rollback stack (top = latest
130
+ * prior shipped digest). Non-mutating: returns a new array. */
131
+ export function pushAssetDigest(
132
+ stack: readonly string[],
133
+ digest: string,
134
+ ): readonly string[] {
135
+ return [...stack, digest];
136
+ }
137
+
138
+ /** Pop the top (most recent prior) digest off the LIFO rollback stack without
139
+ * mutating it. Returns the prior digest (null when the stack is empty) plus
140
+ * the remaining stack for the caller to commit (atomic card-swap fallback:
141
+ * a green swap pushes the incumbent digest; a rollback pops it). */
142
+ export function popAssetDigest(
143
+ stack: readonly string[],
144
+ ): { prior: string | null; rest: readonly string[] } {
145
+ if (stack.length === 0) return { prior: null, rest: stack };
146
+ const rest = stack.slice(0, -1);
147
+ return { prior: stack[stack.length - 1]!, rest };
148
+ }
149
+
150
+ /** Outcome of an atomic promotion swap (pure): the (possibly restored)
151
+ * manifest, the updated rollback stack, and whether a digest swap occurred. */
152
+ export interface AtomicSwapOutcome {
153
+ readonly manifest: AssetManifest;
154
+ readonly stack: readonly string[];
155
+ /** True when the committed pointer changed (green swap or rollback). */
156
+ readonly swapped: boolean;
157
+ }
158
+
159
+ /** Atomically swap the committed manifest to a real trained asset's digest.
160
+ * Green: append the new entry (append-only — the incumbent stays in the
161
+ * ledger, never overwritten), commit to it, and record the INCUMBENT digest
162
+ * onto the LIFO stack for O(1) rollback (rollback restores the incumbent, so
163
+ * the stack must hold the pre-swap committed digest — pushing the swapped-in
164
+ * digest would point rollback back at the very asset being rolled back). Red
165
+ * (or no candidate digest): return the manifest and stack untouched — no
166
+ * swap, prior asset stays live. */
167
+ export function atomicSwap(
168
+ manifest: AssetManifest,
169
+ entry: AssetManifestEntry,
170
+ color: "green" | "red",
171
+ stack: readonly string[],
172
+ ): AtomicSwapOutcome {
173
+ if (color !== "green" || !entry.assetDigest) {
174
+ return { manifest, stack, swapped: false };
175
+ }
176
+ const incumbent = manifest.committed;
177
+ const next = appendAsset(manifest, entry);
178
+ return {
179
+ manifest: next,
180
+ stack: incumbent === null ? stack : pushAssetDigest(stack, incumbent),
181
+ swapped: true,
182
+ };
183
+ }
184
+
185
+ /** Roll back to the previous shipped asset by SHA-256 (O(1) via the LIFO
186
+ * stack): restores the most recent prior digest in the manifest and pops that
187
+ * entry off the stack. Returns null when no prior digest exists or the prior
188
+ * digest is no longer in the append-only manifest (defensive: append-only
189
+ * means it should always be findable). Never leaves a partial state — the
190
+ * committed pointer flip and the stack pop happen together or not at all. */
191
+ export function assetRollback(
192
+ manifest: AssetManifest,
193
+ stack: readonly string[],
194
+ ): AtomicSwapOutcome | null {
195
+ const { prior, rest } = popAssetDigest(stack);
196
+ if (prior === null) return null;
197
+ const restored = rollbackTo(manifest, prior);
198
+ if (!restored) return null;
199
+ return { manifest: restored, stack: rest, swapped: true };
200
+ }
201
+
107
202
  // ---------------------------------------------------------------------------
108
203
  // Pure decision rules.
109
204
  // ---------------------------------------------------------------------------