pi-mega-compact 0.20.38 → 0.20.39

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 (32) hide show
  1. package/dist/config/vector-cortex-ml5c.js +28 -0
  2. package/dist/config/vector-cortex.js +2 -1
  3. package/dist/config.js +1 -1
  4. package/dist/extensions/dashboard-server/routes-rag-settings-vector-cortex.js +1 -0
  5. package/dist/src/config/vector-cortex-ml5c.js +28 -0
  6. package/dist/src/config/vector-cortex.js +2 -1
  7. package/dist/src/config.js +1 -1
  8. package/dist/src/store/backfill.js +0 -9
  9. package/dist/src/vector-cortex/encoder/runtime-emit.js +42 -0
  10. package/dist/src/vector-cortex/encoder/runtime-native.js +77 -0
  11. package/dist/src/vector-cortex/encoder/runtime-select.js +122 -0
  12. package/dist/src/vector-cortex/encoder/runtime-stub.js +35 -0
  13. package/dist/src/vector-cortex/encoder/runtime-wasm.js +71 -0
  14. package/dist/src/vector-cortex/encoder/runtime.js +49 -61
  15. package/dist/vector-cortex/encoder/runtime-emit.js +42 -0
  16. package/dist/vector-cortex/encoder/runtime-native.js +77 -0
  17. package/dist/vector-cortex/encoder/runtime-select.js +122 -0
  18. package/dist/vector-cortex/encoder/runtime-stub.js +35 -0
  19. package/dist/vector-cortex/encoder/runtime-wasm.js +71 -0
  20. package/dist/vector-cortex/encoder/runtime.js +49 -61
  21. package/extensions/dashboard-server/routes-rag-settings-vector-cortex.ts +6 -0
  22. package/package.json +1 -1
  23. package/src/config/vector-cortex-ml5c.ts +30 -0
  24. package/src/config/vector-cortex.ts +2 -2
  25. package/src/config.ts +1 -0
  26. package/src/store/backfill.ts +0 -7
  27. package/src/vector-cortex/encoder/runtime-emit.ts +47 -0
  28. package/src/vector-cortex/encoder/runtime-native.ts +117 -0
  29. package/src/vector-cortex/encoder/runtime-select.ts +167 -0
  30. package/src/vector-cortex/encoder/runtime-stub.ts +38 -0
  31. package/src/vector-cortex/encoder/runtime-wasm.ts +110 -0
  32. package/src/vector-cortex/encoder/runtime.ts +59 -66
@@ -0,0 +1,28 @@
1
+ /**
2
+ * config/vector-cortex-ml5c.ts — ML5-C runtime decision + packaging flag.
3
+ *
4
+ * Sibling extract mirroring vector-cortex-ml5a.ts / vector-cortex-ml5b.ts, so
5
+ * vector-cortex.ts stays under its 300-line soft limit (soft-as-hard gate).
6
+ * This is the ONNX Runtime backend selection + packaging sprint flag.
7
+ * vector-cortex.ts re-exports the ENUM below and root src/config.ts re-exports
8
+ * it, so no consumer import path changes.
9
+ *
10
+ * ML5-C selects the ONNX runtime backend (WASM vs native) based on the ML5-B
11
+ * bench record and platform support. The flag gates the runtime-selection
12
+ * dispatch only; when OFF the encoder serves mode B trigram exactly as before
13
+ * (byte-identical to the ML5-B survivor — no `vector_cortex_runtime_selected`
14
+ * event is emitted, no session-selection dispatch runs).
15
+ *
16
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 / PREVENT-011).
17
+ */
18
+ import { sprintFlag } from "./vector-cortex-flag.js";
19
+ /**
20
+ * ML5-C — runtime decision + packaging (WASM vs native). Default ON.
21
+ * `MEGACOMPACT_ML5_C=0` disables and is byte-identical to the ML5-B survivor:
22
+ * no runtime selection runs — the encoder continues to serve mode B trigram,
23
+ * exactly as before, with no `vector_cortex_runtime_selected` event emitted.
24
+ * The flag gates the runtime-selection dispatch only; it does not gate the
25
+ * underlying WASM/native backends (which are exercised by ML5-B's bench
26
+ * harness and ML5-A's trained asset independently).
27
+ */
28
+ export const ML5C_ENABLED = () => sprintFlag("MEGACOMPACT_ML5_C");
@@ -242,7 +242,7 @@ export const VC8B_ENABLED = () => sprintFlag("MEGACOMPACT_VC8B");
242
242
  // VC8C (canary selection + Rust parity) extracted to vector-cortex-vc8c.ts;
243
243
  // re-exported so existing `./config/vector-cortex.js` imports keep resolving.
244
244
  export { VC8C_ENABLED } from "./vector-cortex-vc8c.js";
245
- // VC9A/VC9B/VC9C/VC9D/PCC/ML5A split to sibling files to stay under the 300-line soft limit.
245
+ // Sibling extracts to stay under the 300-line soft limit.
246
246
  export { VC9A_ENABLED } from "./vector-cortex-vc9a.js";
247
247
  export { VC9B_ENABLED } from "./vector-cortex-vc9b.js";
248
248
  export { VC9C_ENABLED } from "./vector-cortex-vc9c.js";
@@ -250,5 +250,6 @@ export { VC9D_ENABLED } from "./vector-cortex-vc9d.js";
250
250
  export { PCC_ENABLED } from "./vector-cortex-pcc.js";
251
251
  export { ML5A_ENABLED } from "./vector-cortex-ml5a.js";
252
252
  export { ML5B_ENABLED } from "./vector-cortex-ml5b.js";
253
+ export { ML5C_ENABLED } from "./vector-cortex-ml5c.js";
253
254
  // Breaker constants (TRIAD_RESILIENCE.md §breaker) extracted to vector-cortex-breakers.ts.
254
255
  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, 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, 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";
@@ -54,5 +54,6 @@ export const VECTOR_CORTEX_SETTINGS = {
54
54
  boolDirect("MEGACOMPACT_VC9D", "VC9D Embedder Detect Consolidation", "Embedder-detect consolidation + VC9 workstream roll-up: memoizes /api/setup-detect against the mutable input (resolved binary path + mtime) so consecutive requests reuse the result without re-spawning, and unifies the embedder + cortex sub-tabs' 5s poll contract. OFF = byte-identical predecessor (VC9C-era): detect spawns fresh per request and the embedder poll keeps its previous cadence.", true),
55
55
  boolDirect("MEGACOMPACT_PC_C", "PC-C Dashboard Cache Visibility", "Dashboard per-turn prompt-cache visibility: surfaces the per-turn stable-prefix ratio trend (GET /api/prefix-stability) in the CacheTab PrefixStabilityCard. Reads aggregate ratios/counts from the local monitoring events log only — no payload bytes. OFF = byte-identical predecessor (PC-B-era): /api/prefix-stability returns 404 and the CacheTab omits the PrefixStabilityCard.", true),
56
56
  boolDirect("MEGACOMPACT_ML5_A", "ML5-A Five-Head Training Load", "ML5-A real trained-head loading: loadHeadProjections (trained-heads-v1) feeds selectQualifiedEncoder (trainedHeadsPath atomic demotion) + loadCalibrationV1. ON (default) = a pinned trained-heads path must load for mode A. OFF = loaders return null and selection ignores trainedHeadsPath — byte-identical to the placeholder-weighted VC2C path.", true),
57
+ boolDirect("MEGACOMPACT_ML5_C", "ML5-C Runtime Decision + Packaging", "ML5-C runtime backend selection (WASM vs native): selects the ONNX runtime backend based on the ML5-B bench record and platform support. ON (default) = the runtime-selection dispatch runs and emits vector_cortex_runtime_selected. OFF = no selection runs — encoder serves mode B trigram, byte-identical to ML5-B.", true),
57
58
  ],
58
59
  };
@@ -0,0 +1,28 @@
1
+ /**
2
+ * config/vector-cortex-ml5c.ts — ML5-C runtime decision + packaging flag.
3
+ *
4
+ * Sibling extract mirroring vector-cortex-ml5a.ts / vector-cortex-ml5b.ts, so
5
+ * vector-cortex.ts stays under its 300-line soft limit (soft-as-hard gate).
6
+ * This is the ONNX Runtime backend selection + packaging sprint flag.
7
+ * vector-cortex.ts re-exports the ENUM below and root src/config.ts re-exports
8
+ * it, so no consumer import path changes.
9
+ *
10
+ * ML5-C selects the ONNX runtime backend (WASM vs native) based on the ML5-B
11
+ * bench record and platform support. The flag gates the runtime-selection
12
+ * dispatch only; when OFF the encoder serves mode B trigram exactly as before
13
+ * (byte-identical to the ML5-B survivor — no `vector_cortex_runtime_selected`
14
+ * event is emitted, no session-selection dispatch runs).
15
+ *
16
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 / PREVENT-011).
17
+ */
18
+ import { sprintFlag } from "./vector-cortex-flag.js";
19
+ /**
20
+ * ML5-C — runtime decision + packaging (WASM vs native). Default ON.
21
+ * `MEGACOMPACT_ML5_C=0` disables and is byte-identical to the ML5-B survivor:
22
+ * no runtime selection runs — the encoder continues to serve mode B trigram,
23
+ * exactly as before, with no `vector_cortex_runtime_selected` event emitted.
24
+ * The flag gates the runtime-selection dispatch only; it does not gate the
25
+ * underlying WASM/native backends (which are exercised by ML5-B's bench
26
+ * harness and ML5-A's trained asset independently).
27
+ */
28
+ export const ML5C_ENABLED = () => sprintFlag("MEGACOMPACT_ML5_C");
@@ -242,7 +242,7 @@ export const VC8B_ENABLED = () => sprintFlag("MEGACOMPACT_VC8B");
242
242
  // VC8C (canary selection + Rust parity) extracted to vector-cortex-vc8c.ts;
243
243
  // re-exported so existing `./config/vector-cortex.js` imports keep resolving.
244
244
  export { VC8C_ENABLED } from "./vector-cortex-vc8c.js";
245
- // VC9A/VC9B/VC9C/VC9D/PCC/ML5A split to sibling files to stay under the 300-line soft limit.
245
+ // Sibling extracts to stay under the 300-line soft limit.
246
246
  export { VC9A_ENABLED } from "./vector-cortex-vc9a.js";
247
247
  export { VC9B_ENABLED } from "./vector-cortex-vc9b.js";
248
248
  export { VC9C_ENABLED } from "./vector-cortex-vc9c.js";
@@ -250,5 +250,6 @@ export { VC9D_ENABLED } from "./vector-cortex-vc9d.js";
250
250
  export { PCC_ENABLED } from "./vector-cortex-pcc.js";
251
251
  export { ML5A_ENABLED } from "./vector-cortex-ml5a.js";
252
252
  export { ML5B_ENABLED } from "./vector-cortex-ml5b.js";
253
+ export { ML5C_ENABLED } from "./vector-cortex-ml5c.js";
253
254
  // Breaker constants (TRIAD_RESILIENCE.md §breaker) extracted to vector-cortex-breakers.ts.
254
255
  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, 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, 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";
@@ -24,7 +24,6 @@ import { buildRaptorTree } from "../dedup/raptor/tree.js";
24
24
  import { defaultEmbedder } from "../embedder.js";
25
25
  import { getStateDir } from "../store.js";
26
26
  const BATCH = 1000;
27
- const THROTTLE_MS = 0; // synchronous backfill; no cross-process yield needed
28
27
  function ensureProgressTable(db) {
29
28
  db.exec(`
30
29
  CREATE TABLE IF NOT EXISTS backfill_progress (
@@ -92,10 +91,6 @@ export function backfillContentHashes(stateDir = getStateDir()) {
92
91
  withTx(db, () => applyRows(pending));
93
92
  db.prepare("INSERT INTO backfill_progress(name, last_session_id, last_id, updated, duplicates_resolved) VALUES('content_hashes',?,?,?,?) ON CONFLICT(name) DO UPDATE SET last_session_id=excluded.last_session_id, last_id=excluded.last_id, updated=excluded.updated, duplicates_resolved=excluded.duplicates_resolved").run(lastSid, lastId, updated, duplicatesResolved);
94
93
  }
95
- if (THROTTLE_MS > 0) {
96
- // No-op in this synchronous build; placeholder for future streaming backfill.
97
- // guardrails-allow PREVENT-STUB-001: ML5-C
98
- }
99
94
  return { processed, updated, duplicatesResolved };
100
95
  }
101
96
  /** True when no rows remain pending (backfill complete for this state dir). */
@@ -151,10 +146,6 @@ export function backfillPhase(phase, sessionId, stateDir, opts = {}) {
151
146
  });
152
147
  savePhaseCursor(db, phase, cursor ?? null, processed);
153
148
  batches++;
154
- if (THROTTLE_MS > 0) {
155
- const end = Date.now() + THROTTLE_MS;
156
- while (Date.now() < end) { /* throttle */ }
157
- }
158
149
  if (opts.interruptAfterBatches && batches >= opts.interruptAfterBatches) {
159
150
  interrupted = true;
160
151
  break;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * vector-cortex/encoder/runtime-emit.ts — ML5-C seller event emitter.
3
+ *
4
+ * Emits the `vector_cortex_runtime_selected` seller event to the local
5
+ * events.log so the dashboard Setup Cortex blockers card can surface the HG-3
6
+ * (install budget) / HG-4 (darwin-x64 demotion) closure state. Aggregate
7
+ * fields only — never payload bytes (EVAL-REDACT-002).
8
+ *
9
+ * Extracted from runtime.ts so the runtime delegate-shell stays under the
10
+ * 300-line soft limit after the ML5-C dispatch was added. All writes are
11
+ * best-effort / non-fatal; a disk-full or missing state dir never breaks the
12
+ * encoder loop.
13
+ *
14
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 — local filesystem append only;
15
+ * no network). No `any` (PREVENT-011).
16
+ */
17
+ import { appendFileSync, mkdirSync } from "node:fs";
18
+ import { dirname } from "node:path";
19
+ import { defaultEventsPath } from "../../monitoring.js";
20
+ /**
21
+ * Emit the ML5-C `vector_cortex_runtime_selected` seller event (best-effort).
22
+ * The event carries ONLY the four aggregate fields the sprint spec pins
23
+ * ({backend, p95Ms, budgetOk, platform}) — never message content.
24
+ */
25
+ export function emitRuntimeSelected(stateDir, result) {
26
+ try {
27
+ const path = defaultEventsPath(stateDir);
28
+ const payload = {
29
+ ts: Date.now(),
30
+ event: "vector_cortex_runtime_selected",
31
+ backend: result.backend,
32
+ p95Ms: result.p95Ms,
33
+ budgetOk: result.budgetOk,
34
+ platform: result.platform,
35
+ };
36
+ mkdirSync(dirname(path), { recursive: true });
37
+ appendFileSync(path, JSON.stringify(payload) + "\n", "utf8");
38
+ }
39
+ catch {
40
+ /* best-effort — never break the encoder loop */
41
+ }
42
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * vector-cortex/encoder/runtime-native.ts — ML5-C native backend (Option N).
3
+ *
4
+ * Loads an `InferenceSession` from the `onnxruntime-node` native binding for
5
+ * the committed encoder-v1 ONNX asset. This is the CHOSEN selection when
6
+ * `MEGACOMPACT_ENCODER_NATIVE=1` (the native opt-in marker) is set AND the
7
+ * package is present — it uses the platform-specific prebuilt binary (no
8
+ * postinstall compilation needed; per vc2-model-prep §1 the allowScripts
9
+ * removal is safe because only CUDA/TensorRT downloads use it, and pi blocks
10
+ * all scripts anyway).
11
+ *
12
+ * The package is NOT declared in package.json dependencies — it is a lazily-
13
+ * resolved peer that the runtime loads ONLY when the native opt-in is set AND
14
+ * selected. Loading uses dynamic `import()` so the module graph compiles
15
+ * cleanly on hosts without the package (absent installs return null, never
16
+ * throw), so the ML5-C dispatch demotes to mode B trigram rather than
17
+ * breaking.
18
+ *
19
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 — the native binary + model are
20
+ * committed local files). No `any` (PREVENT-011).
21
+ */
22
+ import { ENCODER_OPSET, ENCODER_SEMANTIC_WIDTH, ENCODER_MAX_TOKENS, } from "./types.js";
23
+ /** True when `MEGACOMPACT_ENCODER_NATIVE=1` (the native opt-in operator flag). */
24
+ export function nativeOptIn() {
25
+ return process.env.MEGACOMPACT_ENCODER_NATIVE === "1";
26
+ }
27
+ /** True if `onnxruntime-node` resolves on this host (loading is best-effort).
28
+ * Absent installs return null (never throw) so the ML5-C dispatch can demote
29
+ * to mode B trigram cleanly. */
30
+ async function loadOrtNative() {
31
+ try {
32
+ // @ts-expect-error — optional peer; the shadow type above covers the surface
33
+ const mod = (await import("onnxruntime-node"));
34
+ return mod;
35
+ }
36
+ catch {
37
+ return null;
38
+ }
39
+ }
40
+ /**
41
+ * Create a native-backed `NativeSession` over the committed ONNX asset, gated
42
+ * first on `nativeOptIn()`. Returns null (never throws) on any failure
43
+ * (opt-in off, absent package, unreadable asset, bad session creation) so the
44
+ * caller demotes to mode B trigram.
45
+ */
46
+ export async function createNativeSession(modelPath, options = {}) {
47
+ if (!nativeOptIn())
48
+ return null;
49
+ const ort = await loadOrtNative();
50
+ if (!ort || !ort.InferenceSession?.create)
51
+ return null;
52
+ const threads = options.threads ?? 4;
53
+ const maxTokens = options.maxTokens ?? ENCODER_MAX_TOKENS;
54
+ try {
55
+ const session = await ort.InferenceSession.create(modelPath, {
56
+ executionProviders: ["cpu"],
57
+ intraOpNumThreads: threads,
58
+ });
59
+ return {
60
+ opset: ENCODER_OPSET,
61
+ semanticWidth: ENCODER_SEMANTIC_WIDTH,
62
+ maxTokens,
63
+ async infer(inputIds) {
64
+ const feeds = { input_ids: inputIds };
65
+ const results = await session.run(feeds, ["embedding"]);
66
+ const out = results["embedding"];
67
+ if (!out || !(out.data instanceof Float32Array)) {
68
+ return new Float32Array(ENCODER_SEMANTIC_WIDTH);
69
+ }
70
+ return out.data;
71
+ },
72
+ };
73
+ }
74
+ catch {
75
+ return null;
76
+ }
77
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * vector-cortex/encoder/runtime-select.ts — ML5-C decision-rule dispatch.
3
+ *
4
+ * Pure function of {platform, benchRecord, nativeOptIn} → the chosen ONNX
5
+ * runtime backend. This is the deterministic selection that closes HG-3
6
+ * (install budget) and HG-4 (darwin-x64 disposition) per the ML5-C spec:
7
+ *
8
+ * - Measured p95 at 512 tokens on 4 threads (linux-x64) <= 40 ms → Option W (WASM)
9
+ * - Measured p95 > 40 ms or absent (degraded) → Option N (native)
10
+ * - Platform is darwin-x64 (Intel Mac, HG-1 deferral) → WASM demotion or mode B
11
+ *
12
+ * The `platform` comes from `detectPlatform()` (already in `asset.ts`); the
13
+ * `benchRecord` is the latest `BenchResultV1` (from ML5-B / JSONL); the
14
+ * `nativeOptIn` helper reads `MEGACOMPACT_ENCODER_NATIVE=1`. The selection is
15
+ * PURE — no side effects, no I/O, no network. The caller stamps the result on
16
+ * the `vector_cortex_runtime_selected` event carried to the dashboard.
17
+ *
18
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 — selection is computed+in memory;
19
+ * no fetch/HTTP). No `any` (PREVENT-011).
20
+ */
21
+ import { ENCODER_LATENCY_P95_MS, } from "./types.js";
22
+ import { ML5C_ENABLED } from "../../config/vector-cortex.js";
23
+ /** The 80 MiB install budget in bytes (the HG-3 ceiling, unamended). */
24
+ export const RUNTIME_NATIVE_INSTALL_BUDGET_MIB = 80;
25
+ /**
26
+ * Per-platform optionalDependency footprint of onnxruntime-node (MiB, approx).
27
+ * These sum to ~160 MiB SHIPPED in the npm package (every target platform is
28
+ * included so the resolver lands on a concrete row at install time) — which is
29
+ * what exceeds the 80 MiB HG-3 budget and forces the amendment the fixtures
30
+ * record (ML5-RUNTIME-001). The per-host INSTALLED footprint (one row only)
31
+ * is 28–35 MiB and irrelevant to the 80 MiB ceiling — the budget covers the
32
+ * shipped tarball, not the single-platform install.
33
+ */
34
+ export const NATIVE_FOOTPRINT_MIB = {
35
+ "linux-x64": 33,
36
+ "darwin-arm64": 28,
37
+ "darwin-x64": 33,
38
+ "linux-arm64": 31,
39
+ "win32-x64": 35,
40
+ };
41
+ /**
42
+ * ML5-C decision-rule dispatch: choose the ONNX runtime backend (pure).
43
+ *
44
+ * When the flag is OFF (`MEGACOMPACT_ML5_C=0`), returns mode B trigram — byte-
45
+ * identical to the ML5-B survivor with no selection event emitted.
46
+ *
47
+ * The rule (from the sprint spec):
48
+ * - If nativeOptIn && platform is supported → native (Option N)
49
+ * - If benchRecord has p95Ms <= 40 ms on linux-x64 → WASM (Option W)
50
+ * - Else → native (Option N) with the budget amendment recorded (p95 exceeds
51
+ * the WASM gate or is absent — the placeholder has no measured p95)
52
+ * - darwin-x64 → WASM or mode B demotion per HG-4 (never native here)
53
+ */
54
+ export function selectRuntimeBackend(input) {
55
+ if (!ML5C_ENABLED()) {
56
+ return {
57
+ backend: "modeB",
58
+ budgetOk: true,
59
+ p95Ms: null,
60
+ platform: input.platform,
61
+ rationale: "flag-off: byte-identical mode-B trigram (no selection)",
62
+ };
63
+ }
64
+ // HG-4: Intel Mac (darwin-x64) is out-of-scope per HG-1's deferral — always demote.
65
+ if (input.platform === "darwin-x64") {
66
+ return {
67
+ backend: "wasm",
68
+ budgetOk: true,
69
+ p95Ms: null,
70
+ platform: input.platform,
71
+ rationale: "darwin-x64 demoted to WASM per HG-4 (never native on this platform)",
72
+ };
73
+ }
74
+ // Native opt-in short-circuits: operator explicitly wants the native path.
75
+ // The HG-3 budget compares the SHIPPED byte-count (sum across every platform
76
+ // row in the package's optionalDependencies map) against the 80 MiB ceiling
77
+ // — not the single-platform install size. Native always exceeds 80 MiB across
78
+ // 5 platforms (~160 MiB shipped), so budgetOk is false and the evidence
79
+ // records the amended budget (the ML5-C spec, HG-3 closure).
80
+ if (input.nativeOptIn) {
81
+ const shippedMib = Object.values(NATIVE_FOOTPRINT_MIB).reduce((a, b) => a + b, 0);
82
+ return {
83
+ backend: "native",
84
+ budgetOk: shippedMib <= RUNTIME_NATIVE_INSTALL_BUDGET_MIB,
85
+ p95Ms: input.benchRecord?.p95Ms ?? null,
86
+ platform: input.platform,
87
+ rationale: `native opt-in (MEGACOMPACT_ENCODER_NATIVE=1); shipped ${shippedMib} MiB across 5 platforms → budget amended to ${shippedMib} MiB`,
88
+ };
89
+ }
90
+ // No bench record or degraded (gates.all:false) → WASM cannot qualify (the
91
+ // placeholder 42-byte asset has no measured real p95), so native is selected
92
+ // with the SAME amended-budget disposition as the opt-in path above: the
93
+ // evidence records the closed HG-3 amendment.
94
+ if (!input.benchRecord || !input.benchRecord.gates.all || input.benchRecord.p95Ms === null) {
95
+ const shippedMib = Object.values(NATIVE_FOOTPRINT_MIB).reduce((a, b) => a + b, 0);
96
+ return {
97
+ backend: "native",
98
+ budgetOk: false, // amended: native ships > 80 MiB across the 5-platform matrix
99
+ p95Ms: input.benchRecord?.p95Ms ?? null,
100
+ platform: input.platform,
101
+ rationale: `no qualifying bench record — native fallback with budget amendment (${shippedMib} MiB shipped, HG-3 amendment recorded)`,
102
+ };
103
+ }
104
+ // The decision rule: WASM iff p95 <= 40 ms (linux-x64, 512 tokens, 4 threads) —
105
+ // native required otherwise, with the same budget amendment recorded.
106
+ if (input.benchRecord.p95Ms <= ENCODER_LATENCY_P95_MS) {
107
+ return {
108
+ backend: "wasm",
109
+ budgetOk: true,
110
+ p95Ms: input.benchRecord.p95Ms,
111
+ platform: input.platform,
112
+ rationale: `WASM qualifies: p95 ${input.benchRecord.p95Ms}ms <= ${ENCODER_LATENCY_P95_MS}ms`,
113
+ };
114
+ }
115
+ return {
116
+ backend: "native",
117
+ budgetOk: false, // amended: native exceeds the 80 MiB budget per the evidence
118
+ p95Ms: input.benchRecord.p95Ms,
119
+ platform: input.platform,
120
+ rationale: `native required: p95 ${input.benchRecord.p95Ms}ms > ${ENCODER_LATENCY_P95_MS}ms on WASM`,
121
+ };
122
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * vector-cortex/encoder/runtime-stub.ts — the VC2A-era deterministic LCG
3
+ * placeholder (`projectSemantic`) + token-seed helper, split out of runtime.ts
4
+ * so `runtime.ts` stays under its 300-line soft limit as the delegate-shell.
5
+ *
6
+ * This placeholder is the VC2A-era stand-in for a real ONNX EncoderRuntime
7
+ * inference result (the actual weights were VC2C and the runtime-selection
8
+ * dispatch is ML5-C). It remains exported so tests driving end-to-end shape
9
+ * keep working even when no backend session is active.
10
+ *
11
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 / PREVENT-011).
12
+ */
13
+ import { ENCODER_SEMANTIC_WIDTH } from "./types.js";
14
+ /** A deterministic seeded projection so the mode-A inference path is testable
15
+ * end-to-end without onnxruntime (real weights + execution are VC2C). */
16
+ export function projectSemantic(seed, n) {
17
+ const out = new Float32Array(n);
18
+ let state = (seed >>> 0) ^ 0x9e3779b9;
19
+ let sum = 0;
20
+ for (let i = 0; i < n; i++) {
21
+ state = (state * 1664525 + 1013904223) >>> 0;
22
+ out[i] = (state / 4294967296) * 2 - 1;
23
+ sum += out[i] * out[i];
24
+ }
25
+ const norm = Math.sqrt(sum) || 1;
26
+ for (let i = 0; i < n; i++)
27
+ out[i] = out[i] / norm;
28
+ return out;
29
+ }
30
+ /** Deterministic token seed derived from the verified asset bytes count. */
31
+ export function seedFromBytes(embeddedBytes) {
32
+ return (embeddedBytes * 2654435761) >>> 0;
33
+ }
34
+ /** The semantic embedding width from the normative types barrel. */
35
+ export { ENCODER_SEMANTIC_WIDTH };
@@ -0,0 +1,71 @@
1
+ /**
2
+ * vector-cortex/encoder/runtime-wasm.ts — ML5-C WASM backend (Option W).
3
+ *
4
+ * Loads an `InferenceSession` from the `onnxruntime-web` WASM execution
5
+ * provider for the committed encoder-v1 ONNX asset. This is the default
6
+ * backend when the WASM path is selected by `select.ts` — it covers all Node
7
+ * platforms (no per-platform optionalDependencies), is pure JS + WASM (~9 MiB),
8
+ * and never fetches from the network (PREVENT-PI-004).
9
+ *
10
+ * The package is NOT declared in package.json dependencies — it is a lazily-
11
+ * resolved peer that the runtime loads ONLY when the WASM backend is actually
12
+ * selected. Loading uses dynamic `import()` so the module graph compiles
13
+ * cleanly on hosts without the package; absent installs return null (never
14
+ * throw), so the ML5-C dispatch demotes to mode B trigram rather than
15
+ * breaking (ML5-B-bench precedent: the fixtures declare the shape even when
16
+ * the package is absent).
17
+ *
18
+ * Pi-agnostic, dependency-free (PREVENT-PI-004 — the WASM artifact is loaded
19
+ * from the committed local path; no fetch/HTTP anywhere). No `any`
20
+ * (PREVENT-011).
21
+ */
22
+ import { ENCODER_OPSET, ENCODER_SEMANTIC_WIDTH, ENCODER_MAX_TOKENS, } from "./types.js";
23
+ /** True if `onnxruntime-web` resolves on this host (loading is best-effort).
24
+ * Absent installs return null (never throw) so the ML5-C dispatch can demote
25
+ * to mode B trigram cleanly. */
26
+ async function loadOrtWasm() {
27
+ try {
28
+ // @ts-expect-error — optional peer; the shadow type above covers the surface
29
+ const mod = (await import("onnxruntime-web"));
30
+ return mod;
31
+ }
32
+ catch {
33
+ return null;
34
+ }
35
+ }
36
+ /**
37
+ * Create a WASM-backed `WasmSession` over the committed ONNX asset. Returns
38
+ * null (never throws) on any failure (absent package, unreadable asset, bad
39
+ * session creation) so the caller demotes to mode B trigram per HG-4 mode-B
40
+ * disposition when the WASM path is unavailable on a darwin-x64 host.
41
+ */
42
+ export async function createWasmSession(modelPath, options = {}) {
43
+ const ort = await loadOrtWasm();
44
+ if (!ort || !ort.InferenceSession?.create)
45
+ return null;
46
+ const threads = options.threads ?? 4;
47
+ const maxTokens = options.maxTokens ?? ENCODER_MAX_TOKENS;
48
+ try {
49
+ const session = await ort.InferenceSession.create(modelPath, {
50
+ executionProviders: ["wasm"],
51
+ intraOpNumThreads: threads,
52
+ });
53
+ return {
54
+ opset: ENCODER_OPSET,
55
+ semanticWidth: ENCODER_SEMANTIC_WIDTH,
56
+ maxTokens,
57
+ async infer(inputIds) {
58
+ const feeds = { input_ids: inputIds };
59
+ const results = await session.run(feeds, ["embedding"]);
60
+ const out = results["embedding"];
61
+ if (!out || !(out.data instanceof Float32Array)) {
62
+ return new Float32Array(ENCODER_SEMANTIC_WIDTH);
63
+ }
64
+ return out.data;
65
+ },
66
+ };
67
+ }
68
+ catch {
69
+ return null;
70
+ }
71
+ }