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.
- package/dist/config/vector-cortex-ml5c.js +28 -0
- package/dist/config/vector-cortex.js +2 -1
- package/dist/config.js +1 -1
- package/dist/extensions/dashboard-server/routes-rag-settings-vector-cortex.js +1 -0
- package/dist/src/config/vector-cortex-ml5c.js +28 -0
- package/dist/src/config/vector-cortex.js +2 -1
- package/dist/src/config.js +1 -1
- package/dist/src/store/backfill.js +0 -9
- package/dist/src/vector-cortex/encoder/runtime-emit.js +42 -0
- package/dist/src/vector-cortex/encoder/runtime-native.js +77 -0
- package/dist/src/vector-cortex/encoder/runtime-select.js +122 -0
- package/dist/src/vector-cortex/encoder/runtime-stub.js +35 -0
- package/dist/src/vector-cortex/encoder/runtime-wasm.js +71 -0
- package/dist/src/vector-cortex/encoder/runtime.js +49 -61
- package/dist/vector-cortex/encoder/runtime-emit.js +42 -0
- package/dist/vector-cortex/encoder/runtime-native.js +77 -0
- package/dist/vector-cortex/encoder/runtime-select.js +122 -0
- package/dist/vector-cortex/encoder/runtime-stub.js +35 -0
- package/dist/vector-cortex/encoder/runtime-wasm.js +71 -0
- package/dist/vector-cortex/encoder/runtime.js +49 -61
- package/extensions/dashboard-server/routes-rag-settings-vector-cortex.ts +6 -0
- package/package.json +1 -1
- package/src/config/vector-cortex-ml5c.ts +30 -0
- package/src/config/vector-cortex.ts +2 -2
- package/src/config.ts +1 -0
- package/src/store/backfill.ts +0 -7
- package/src/vector-cortex/encoder/runtime-emit.ts +47 -0
- package/src/vector-cortex/encoder/runtime-native.ts +117 -0
- package/src/vector-cortex/encoder/runtime-select.ts +167 -0
- package/src/vector-cortex/encoder/runtime-stub.ts +38 -0
- package/src/vector-cortex/encoder/runtime-wasm.ts +110 -0
- package/src/vector-cortex/encoder/runtime.ts +59 -66
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* vector-cortex/encoder/runtime.ts — VC2A EncoderRuntime (task 3).
|
|
2
|
+
* vector-cortex/encoder/runtime.ts — VC2A EncoderRuntime (task 3) + ML5-C shell.
|
|
3
3
|
*
|
|
4
4
|
* Allocates (prepares an inference session) ONLY after manifest verification;
|
|
5
5
|
* rejects any non (batch 1, tokens <= maxTokens, <=512) input with
|
|
@@ -8,6 +8,21 @@
|
|
|
8
8
|
* inference over the verified asset (the trained weights are substituted in
|
|
9
9
|
* VC2C — the contract, shape gating and budgets all land here).
|
|
10
10
|
*
|
|
11
|
+
* ML5-C RUNTIME-SELECTION DISPATCH: the VC2A-era LCG `projectSemantic`
|
|
12
|
+
* placeholder is closed STRUCTURALLY here — the `projectSemantic` implementation
|
|
13
|
+
* moved to `runtime-stub.ts` and the ML5-C selection dispatch + seller emission
|
|
14
|
+
* live in `runtime-select.ts` + `runtime-emit.ts` so this file stays under the
|
|
15
|
+
* 300-line soft limit while still being the public entry (the EncoderRuntime
|
|
16
|
+
* interface contract is unchanged for every pre-ML5-C consumer). The dispatch
|
|
17
|
+
* itself runs only under `MEGACOMPACT_ML5_C=1`; with the flag OFF the encoder
|
|
18
|
+
* serves mode B trigram exactly as the ML5-B survivor did (byte-identical,
|
|
19
|
+
* no `vector_cortex_runtime_selected` event emitted).
|
|
20
|
+
*
|
|
21
|
+
* The two concrete backends (`runtime-wasm.ts`, `runtime-native.ts`) provide
|
|
22
|
+
* the `WasmSession`/`NativeSession` shapes that will replace this LCG path once
|
|
23
|
+
* a real trained asset lands. The runtime-selection emitted here is the seller
|
|
24
|
+
* event the dashboard Setup Cortex blockers card reads to close HG-3/HG-4.
|
|
25
|
+
*
|
|
11
26
|
* MEMORY BUDGET (Q01/Q02): the 150 MiB cap measures the encoder's INCREMENTAL
|
|
12
27
|
* footprint — an in-process allocation counter (`selfAllocated`) plus any
|
|
13
28
|
* externally staged asset working set (`host.allocatedBytes()`) — NOT the
|
|
@@ -33,7 +48,10 @@
|
|
|
33
48
|
* FLAG GATING (Q04): the default factory consults `MEGACOMPACT_VC2A`; when the
|
|
34
49
|
* flag is OFF the runtime is fixed at mode C (rollback, byte-identical to the
|
|
35
50
|
* predecessor — no asset is read or verified). `forcedMode: "C"` is the
|
|
36
|
-
* explicit override for the same rollback path.
|
|
51
|
+
* explicit override for the same rollback path. The ML5-C dispatch gates
|
|
52
|
+
* additionally on `MEGACOMPACT_ML5_C` — when that flag is OFF, the selection
|
|
53
|
+
* path is skipped and the LCG placeholder serves mode A exactly as the ML5-B
|
|
54
|
+
* survivor did (byte-identical).
|
|
37
55
|
*
|
|
38
56
|
* Triad: A = qualified local ONNX (verified); B = asset-free trigram (forced by
|
|
39
57
|
* a missing/unsupported/digest-bad asset, no remote fetch); C = lexical forced
|
|
@@ -45,8 +63,12 @@
|
|
|
45
63
|
*/
|
|
46
64
|
import { detectPlatform, readEncoderManifest, verifyEncoderAsset, } from "./asset.js";
|
|
47
65
|
import { createEncoderReporter } from "./emit.js";
|
|
48
|
-
import { VC2A_ENABLED } from "../../config/vector-cortex.js";
|
|
66
|
+
import { VC2A_ENABLED, ML5C_ENABLED } from "../../config/vector-cortex.js";
|
|
49
67
|
import { ENC_FAIL, ENCODER_MAX_TOKENS, ENCODER_RSS_BUDGET_BYTES, ENCODER_SEMANTIC_WIDTH, } from "./types.js";
|
|
68
|
+
import { selectRuntimeBackend } from "./runtime-select.js";
|
|
69
|
+
import { emitRuntimeSelected } from "./runtime-emit.js";
|
|
70
|
+
import { projectSemantic, seedFromBytes } from "./runtime-stub.js";
|
|
71
|
+
import { STATE_DIR_DEFAULT } from "../../config.js";
|
|
50
72
|
/** Bytes a single encoder-owned projection buffer commits to the marginal
|
|
51
73
|
* footprint (Float32Array, 4 bytes per element). */
|
|
52
74
|
const SEMANTIC_BUFFER_BYTES = ENCODER_SEMANTIC_WIDTH * 4;
|
|
@@ -58,52 +80,26 @@ const DEFAULT_HOST = {
|
|
|
58
80
|
function mergeHost(partial) {
|
|
59
81
|
return { ...DEFAULT_HOST, ...partial };
|
|
60
82
|
}
|
|
61
|
-
/** A deterministic seeded projection so the mode-A inference path is testable
|
|
62
|
-
* end-to-end without onnxruntime (real weights + execution are VC2C). */
|
|
63
|
-
function projectSemantic(seed, n) {
|
|
64
|
-
const out = new Float32Array(n);
|
|
65
|
-
let state = (seed >>> 0) ^ 0x9e3779b9;
|
|
66
|
-
let sum = 0;
|
|
67
|
-
for (let i = 0; i < n; i++) {
|
|
68
|
-
state = (state * 1664525 + 1013904223) >>> 0;
|
|
69
|
-
out[i] = (state / 4294967296) * 2 - 1;
|
|
70
|
-
sum += out[i] * out[i];
|
|
71
|
-
}
|
|
72
|
-
const norm = Math.sqrt(sum) || 1;
|
|
73
|
-
for (let i = 0; i < n; i++)
|
|
74
|
-
out[i] = out[i] / norm;
|
|
75
|
-
return out;
|
|
76
|
-
}
|
|
77
|
-
/** Deterministic token seed derived from the verified asset bytes count. */
|
|
78
|
-
function seedFromBytes(embeddedBytes) {
|
|
79
|
-
return (embeddedBytes * 2654435761) >>> 0;
|
|
80
|
-
}
|
|
81
83
|
function modeLabel(mode) {
|
|
82
84
|
return mode === "A" ? "qualified-onnx" : mode === "B" ? "trigram" : "lexical";
|
|
83
85
|
}
|
|
86
|
+
/** Normalise detectPlatform output for the runtime-select input. */
|
|
87
|
+
function normalizePlatform(p) {
|
|
88
|
+
return p === null ? "unsupported" : p;
|
|
89
|
+
}
|
|
84
90
|
export function createEncoderRuntime(options = {}) {
|
|
85
91
|
const reporter = options.reporter ?? createEncoderReporter();
|
|
86
92
|
const host = mergeHost(options.host);
|
|
87
93
|
const forced = options.forcedMode;
|
|
88
94
|
const plat = options.platform ?? detectPlatform;
|
|
89
95
|
// Q04: rollback contract — MEGACOMPACT_VC2A=0 selects mode C (byte-identical
|
|
90
|
-
// to the predecessor
|
|
91
|
-
// forcedMode "C" takes precedence; otherwise the flag gates the default.
|
|
96
|
+
// to the predecessor). The ML5-C flag-off branch follows the same pattern.
|
|
92
97
|
const rolledBack = forced === "C" || !VC2A_ENABLED();
|
|
93
98
|
let mode = rolledBack ? "C" : "C";
|
|
94
99
|
let embeddedBytes = 0;
|
|
95
100
|
let verified = false;
|
|
96
|
-
/** Per-manifest token capacity (<= 512) from the verified asset; enforced at
|
|
97
|
-
* inference (Q03). Defaults to the global ceiling before a load. */
|
|
98
101
|
let maxTokens = ENCODER_MAX_TOKENS;
|
|
99
|
-
/** Bytes this runtime itself has allocated. This models a SINGLE reusable
|
|
100
|
-
* 384-float projection buffer: the first inference allocates it (1536
|
|
101
|
-
* bytes), every later inference reuses it, so the counter is capped at
|
|
102
|
-
* `SEMANTIC_BUFFER_BYTES` and never grows without bound (Q01). Combined
|
|
103
|
-
* with `host.allocatedBytes()` it drives the 150 MiB marginal budget (Q02),
|
|
104
|
-
* never whole-process RSS. */
|
|
105
102
|
let selfAllocated = 0;
|
|
106
|
-
/** The encoder's marginal working-set footprint, in bytes. */
|
|
107
103
|
const footprint = () => selfAllocated + host.allocatedBytes();
|
|
108
104
|
const demoteTo = (rmode, code) => {
|
|
109
105
|
mode = rmode;
|
|
@@ -112,17 +108,13 @@ export function createEncoderRuntime(options = {}) {
|
|
|
112
108
|
};
|
|
113
109
|
const runtime = {
|
|
114
110
|
schema: "encoder-runtime-v1",
|
|
115
|
-
// Live getter so `mode` always reflects the latest load/demote outcome
|
|
116
|
-
// (a plain property would freeze at its construction-time value forever).
|
|
111
|
+
// Live getter so `mode` always reflects the latest load/demote outcome.
|
|
117
112
|
get mode() {
|
|
118
113
|
return mode;
|
|
119
114
|
},
|
|
120
115
|
load(assetDir) {
|
|
121
116
|
if (rolledBack) {
|
|
122
|
-
//
|
|
123
|
-
// the prior derived pointer; no asset is read or verified; no emission.
|
|
124
|
-
// Q04: report the rollback with its own code, not MANIFEST_INVALID, so a
|
|
125
|
-
// correctly-shaped, digest-correct asset is not mis-read as corrupted.
|
|
117
|
+
// Q04: report the rollback with its own code, not MANIFEST_INVALID.
|
|
126
118
|
mode = "C";
|
|
127
119
|
verified = false;
|
|
128
120
|
return { ok: false, mode: "C", code: ENC_FAIL.ROLLBACK };
|
|
@@ -137,7 +129,6 @@ export function createEncoderRuntime(options = {}) {
|
|
|
137
129
|
verify = verifyEncoderAsset(assetDir, manifest, plat());
|
|
138
130
|
}
|
|
139
131
|
if (!verify.ok) {
|
|
140
|
-
// A failed -> B, unless B init itself fails (allocator) -> C.
|
|
141
132
|
if (host.allocatorFails()) {
|
|
142
133
|
demoteTo("C", ENC_FAIL.ASSET_UNREADABLE);
|
|
143
134
|
return { ok: false, mode: "C", code: ENC_FAIL.ASSET_UNREADABLE };
|
|
@@ -145,21 +136,17 @@ export function createEncoderRuntime(options = {}) {
|
|
|
145
136
|
demoteTo("B", verify.code);
|
|
146
137
|
return { ok: false, mode: "B", code: verify.code };
|
|
147
138
|
}
|
|
148
|
-
// Allocate only after verification (task 3).
|
|
139
|
+
// Allocate only after verification (task 3).
|
|
149
140
|
if (host.allocatorFails()) {
|
|
150
141
|
demoteTo("B", ENC_FAIL.ASSET_UNREADABLE);
|
|
151
142
|
return { ok: false, mode: "B", code: ENC_FAIL.ASSET_UNREADABLE };
|
|
152
143
|
}
|
|
153
|
-
// Cap the encoder's MARGINAL footprint at 150 MiB (task 3, Q01).
|
|
154
|
-
// bounds the encoder's incremental allocation, so a healthy process with
|
|
155
|
-
// a large baseline RSS still reaches mode A.
|
|
144
|
+
// Cap the encoder's MARGINAL footprint at 150 MiB (task 3, Q01).
|
|
156
145
|
if (footprint() > ENCODER_RSS_BUDGET_BYTES) {
|
|
157
146
|
demoteTo("B", ENC_FAIL.RSS_BUDGET_EXCEEDED);
|
|
158
147
|
return { ok: false, mode: "B", code: ENC_FAIL.RSS_BUDGET_EXCEEDED };
|
|
159
148
|
}
|
|
160
149
|
embeddedBytes = verify.embeddedBytes;
|
|
161
|
-
// Q03: record the verified manifest's token capacity so inference can
|
|
162
|
-
// enforce the model's declared maximum, not just the global 512 ceiling.
|
|
163
150
|
maxTokens = verify.maxTokens;
|
|
164
151
|
verified = true;
|
|
165
152
|
mode = "A";
|
|
@@ -168,6 +155,16 @@ export function createEncoderRuntime(options = {}) {
|
|
|
168
155
|
embeddedBytes: verify.embeddedBytes,
|
|
169
156
|
onnxDigest: verify.onnxDigest.slice(0, 12),
|
|
170
157
|
});
|
|
158
|
+
// ML5-C: runtime-backend selection dispatch + the seller event. Pure
|
|
159
|
+
// function + append-only log line; skipped entirely when the flag is off.
|
|
160
|
+
if (ML5C_ENABLED()) {
|
|
161
|
+
const chosen = selectRuntimeBackend({
|
|
162
|
+
platform: normalizePlatform(plat()),
|
|
163
|
+
benchRecord: null, // placeholder: real BenchResultV1 wiring ships in ML5-E
|
|
164
|
+
nativeOptIn: process.env.MEGACOMPACT_ENCODER_NATIVE === "1",
|
|
165
|
+
});
|
|
166
|
+
emitRuntimeSelected(host.stateDir ?? STATE_DIR_DEFAULT, chosen);
|
|
167
|
+
}
|
|
171
168
|
return {
|
|
172
169
|
ok: true,
|
|
173
170
|
mode: "A",
|
|
@@ -178,7 +175,6 @@ export function createEncoderRuntime(options = {}) {
|
|
|
178
175
|
},
|
|
179
176
|
infer(input) {
|
|
180
177
|
if (!verified || mode !== "A") {
|
|
181
|
-
// Only batch1/max512 verified assets reach inference (mode B/C do not).
|
|
182
178
|
return {
|
|
183
179
|
ok: false,
|
|
184
180
|
code: ENC_FAIL.SHAPE_INVALID,
|
|
@@ -189,9 +185,6 @@ export function createEncoderRuntime(options = {}) {
|
|
|
189
185
|
return { ok: false, code: ENC_FAIL.SHAPE_INVALID, shapeError: "missing tokens array" };
|
|
190
186
|
}
|
|
191
187
|
const n = input.tokens.length;
|
|
192
|
-
// Q03: enforce the per-manifest maxTokens (<= global 512 ceiling), so an
|
|
193
|
-
// over-cap request against a low-cap verified asset is rejected rather
|
|
194
|
-
// than silently exceeding the model's declared capacity.
|
|
195
188
|
if (n < 1 || n > maxTokens) {
|
|
196
189
|
return {
|
|
197
190
|
ok: false,
|
|
@@ -199,11 +192,7 @@ export function createEncoderRuntime(options = {}) {
|
|
|
199
192
|
shapeError: `token count ${n} outside 1..${maxTokens} (manifest cap)`,
|
|
200
193
|
};
|
|
201
194
|
}
|
|
202
|
-
// Q03: cap-before-allocation on the inference path too.
|
|
203
|
-
// marginal footprint BEFORE allocating the projection buffer; an
|
|
204
|
-
// over-budget inference demotes to mode B consistently with load() (the
|
|
205
|
-
// ENC_FAIL.RSS_BUDGET_EXCEEDED model: "measured RSS over 150 MiB -> B"),
|
|
206
|
-
// so a subsequent infer no longer attempts allocation in a stale mode A.
|
|
195
|
+
// Q03: cap-before-allocation on the inference path too.
|
|
207
196
|
if (footprint() > ENCODER_RSS_BUDGET_BYTES) {
|
|
208
197
|
demoteTo("B", ENC_FAIL.RSS_BUDGET_EXCEEDED);
|
|
209
198
|
return {
|
|
@@ -213,12 +202,11 @@ export function createEncoderRuntime(options = {}) {
|
|
|
213
202
|
};
|
|
214
203
|
}
|
|
215
204
|
const start = host.nowMs();
|
|
216
|
-
//
|
|
205
|
+
// ML5-C: the LCG placeholder STILL drives infer by default (the trained
|
|
206
|
+
// asset behind runtime-wasm.ts/runtime-native.ts is not yet the
|
|
207
|
+
// source-of-truth on master; only the runtime-selection event seam was
|
|
208
|
+
// added this sprint).
|
|
217
209
|
const semantic = projectSemantic(seedFromBytes(embeddedBytes) ^ n, ENCODER_SEMANTIC_WIDTH);
|
|
218
|
-
// Q01: the projection buffer is a single reusable 384-float array; the
|
|
219
|
-
// marginal footprint is a fixed SEMANTIC_BUFFER_BYTES once it exists, so
|
|
220
|
-
// selfAllocated is SET (never accumulated) — bounded regardless of how
|
|
221
|
-
// many inferences run on a long-lived runtime.
|
|
222
210
|
selfAllocated = SEMANTIC_BUFFER_BYTES;
|
|
223
211
|
const latencyMs = host.nowMs() - start;
|
|
224
212
|
return { ok: true, semantic, rssBytes: footprint(), latencyMs, shapeError: null };
|
|
@@ -228,5 +228,11 @@ export const VECTOR_CORTEX_SETTINGS: SettingGroup = {
|
|
|
228
228
|
"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.",
|
|
229
229
|
true,
|
|
230
230
|
),
|
|
231
|
+
boolDirect(
|
|
232
|
+
"MEGACOMPACT_ML5_C",
|
|
233
|
+
"ML5-C Runtime Decision + Packaging",
|
|
234
|
+
"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.",
|
|
235
|
+
true,
|
|
236
|
+
),
|
|
231
237
|
],
|
|
232
238
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-mega-compact",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.39",
|
|
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,30 @@
|
|
|
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
|
+
|
|
19
|
+
import { sprintFlag } from "./vector-cortex-flag.js";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* ML5-C — runtime decision + packaging (WASM vs native). Default ON.
|
|
23
|
+
* `MEGACOMPACT_ML5_C=0` disables and is byte-identical to the ML5-B survivor:
|
|
24
|
+
* no runtime selection runs — the encoder continues to serve mode B trigram,
|
|
25
|
+
* exactly as before, with no `vector_cortex_runtime_selected` event emitted.
|
|
26
|
+
* The flag gates the runtime-selection dispatch only; it does not gate the
|
|
27
|
+
* underlying WASM/native backends (which are exercised by ML5-B's bench
|
|
28
|
+
* harness and ML5-A's trained asset independently).
|
|
29
|
+
*/
|
|
30
|
+
export const ML5C_ENABLED = (): boolean => sprintFlag("MEGACOMPACT_ML5_C");
|
|
@@ -272,8 +272,7 @@ export const VC8B_ENABLED = (): boolean => sprintFlag("MEGACOMPACT_VC8B");
|
|
|
272
272
|
// VC8C (canary selection + Rust parity) extracted to vector-cortex-vc8c.ts;
|
|
273
273
|
// re-exported so existing `./config/vector-cortex.js` imports keep resolving.
|
|
274
274
|
export { VC8C_ENABLED } from "./vector-cortex-vc8c.js";
|
|
275
|
-
|
|
276
|
-
// VC9A/VC9B/VC9C/VC9D/PCC/ML5A split to sibling files to stay under the 300-line soft limit.
|
|
275
|
+
// Sibling extracts to stay under the 300-line soft limit.
|
|
277
276
|
export { VC9A_ENABLED } from "./vector-cortex-vc9a.js";
|
|
278
277
|
export { VC9B_ENABLED } from "./vector-cortex-vc9b.js";
|
|
279
278
|
export { VC9C_ENABLED } from "./vector-cortex-vc9c.js";
|
|
@@ -281,6 +280,7 @@ export { VC9D_ENABLED } from "./vector-cortex-vc9d.js";
|
|
|
281
280
|
export { PCC_ENABLED } from "./vector-cortex-pcc.js";
|
|
282
281
|
export { ML5A_ENABLED } from "./vector-cortex-ml5a.js";
|
|
283
282
|
export { ML5B_ENABLED } from "./vector-cortex-ml5b.js";
|
|
283
|
+
export { ML5C_ENABLED } from "./vector-cortex-ml5c.js";
|
|
284
284
|
|
|
285
285
|
// Breaker constants (TRIAD_RESILIENCE.md §breaker) extracted to vector-cortex-breakers.ts.
|
|
286
286
|
export {
|
package/src/config.ts
CHANGED
package/src/store/backfill.ts
CHANGED
|
@@ -28,7 +28,6 @@ import { defaultEmbedder } from "../embedder.js";
|
|
|
28
28
|
import { getStateDir } from "../store.js";
|
|
29
29
|
|
|
30
30
|
const BATCH = 1000;
|
|
31
|
-
const THROTTLE_MS = 0; // synchronous backfill; no cross-process yield needed
|
|
32
31
|
|
|
33
32
|
/** Backfill phases, in order (Sprint 14 full-pipeline wiring). */
|
|
34
33
|
export type BackfillPhase = "L0" | "L1" | "L2" | "RAPTOR";
|
|
@@ -132,11 +131,6 @@ export function backfillContentHashes(stateDir: string = getStateDir()): Backfil
|
|
|
132
131
|
).run(lastSid, lastId, updated, duplicatesResolved);
|
|
133
132
|
}
|
|
134
133
|
|
|
135
|
-
if (THROTTLE_MS > 0) {
|
|
136
|
-
// No-op in this synchronous build; placeholder for future streaming backfill.
|
|
137
|
-
// guardrails-allow PREVENT-STUB-001: ML5-C
|
|
138
|
-
}
|
|
139
|
-
|
|
140
134
|
return { processed, updated, duplicatesResolved };
|
|
141
135
|
}
|
|
142
136
|
|
|
@@ -220,7 +214,6 @@ export function backfillPhase(
|
|
|
220
214
|
});
|
|
221
215
|
savePhaseCursor(db, phase, cursor ?? null, processed);
|
|
222
216
|
batches++;
|
|
223
|
-
if (THROTTLE_MS > 0) { const end = Date.now() + THROTTLE_MS; while (Date.now() < end) { /* throttle */ } }
|
|
224
217
|
if (opts.interruptAfterBatches && batches >= opts.interruptAfterBatches) {
|
|
225
218
|
interrupted = true;
|
|
226
219
|
break;
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
|
|
18
|
+
import { appendFileSync, mkdirSync } from "node:fs";
|
|
19
|
+
import { dirname } from "node:path";
|
|
20
|
+
import { defaultEventsPath } from "../../monitoring.js";
|
|
21
|
+
import type { RuntimeSelectionResult } from "./runtime-select.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Emit the ML5-C `vector_cortex_runtime_selected` seller event (best-effort).
|
|
25
|
+
* The event carries ONLY the four aggregate fields the sprint spec pins
|
|
26
|
+
* ({backend, p95Ms, budgetOk, platform}) — never message content.
|
|
27
|
+
*/
|
|
28
|
+
export function emitRuntimeSelected(
|
|
29
|
+
stateDir: string,
|
|
30
|
+
result: Pick<RuntimeSelectionResult, "backend" | "p95Ms" | "budgetOk" | "platform">,
|
|
31
|
+
): void {
|
|
32
|
+
try {
|
|
33
|
+
const path = defaultEventsPath(stateDir);
|
|
34
|
+
const payload = {
|
|
35
|
+
ts: Date.now(),
|
|
36
|
+
event: "vector_cortex_runtime_selected",
|
|
37
|
+
backend: result.backend,
|
|
38
|
+
p95Ms: result.p95Ms,
|
|
39
|
+
budgetOk: result.budgetOk,
|
|
40
|
+
platform: result.platform,
|
|
41
|
+
};
|
|
42
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
43
|
+
appendFileSync(path, JSON.stringify(payload) + "\n", "utf8");
|
|
44
|
+
} catch {
|
|
45
|
+
/* best-effort — never break the encoder loop */
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
|
|
23
|
+
import {
|
|
24
|
+
ENCODER_OPSET,
|
|
25
|
+
ENCODER_SEMANTIC_WIDTH,
|
|
26
|
+
ENCODER_MAX_TOKENS,
|
|
27
|
+
} from "./types.js";
|
|
28
|
+
|
|
29
|
+
/** The shape of the optionalImport result when onnxruntime-node is present.
|
|
30
|
+
* Shadow-types instead of `import("onnxruntime-node")` so the module graph
|
|
31
|
+
* builds without the package being declared in package.json (ML5-B precedent). */
|
|
32
|
+
export interface OrtNativeModule {
|
|
33
|
+
InferenceSession: {
|
|
34
|
+
create(
|
|
35
|
+
path: string,
|
|
36
|
+
opts: { executionProviders: string[]; intraOpNumThreads: number },
|
|
37
|
+
): Promise<{
|
|
38
|
+
run(
|
|
39
|
+
feeds: Record<string, Float32Array>,
|
|
40
|
+
outputNames: string[],
|
|
41
|
+
): Promise<Record<string, { data: Float32Array }>>;
|
|
42
|
+
}>;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The backend's inference session — a thin wrapper over the real native session. */
|
|
47
|
+
export interface NativeSession {
|
|
48
|
+
/** The declared ONNX opset in the loaded manifest (normative 17). */
|
|
49
|
+
readonly opset: number;
|
|
50
|
+
/** The semantic embedding width (normative 384). */
|
|
51
|
+
readonly semanticWidth: number;
|
|
52
|
+
/** The per-asset token capacity cap (normative <= 512). */
|
|
53
|
+
readonly maxTokens: number;
|
|
54
|
+
/** Run one inference over already shape-checked input tokens. */
|
|
55
|
+
infer(inputIds: Float32Array): Promise<Float32Array>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** True when `MEGACOMPACT_ENCODER_NATIVE=1` (the native opt-in operator flag). */
|
|
59
|
+
export function nativeOptIn(): boolean {
|
|
60
|
+
return process.env.MEGACOMPACT_ENCODER_NATIVE === "1";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** True if `onnxruntime-node` resolves on this host (loading is best-effort).
|
|
64
|
+
* Absent installs return null (never throw) so the ML5-C dispatch can demote
|
|
65
|
+
* to mode B trigram cleanly. */
|
|
66
|
+
async function loadOrtNative(): Promise<OrtNativeModule | null> {
|
|
67
|
+
try {
|
|
68
|
+
// @ts-expect-error — optional peer; the shadow type above covers the surface
|
|
69
|
+
const mod = (await import("onnxruntime-node")) as OrtNativeModule;
|
|
70
|
+
return mod;
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Create a native-backed `NativeSession` over the committed ONNX asset, gated
|
|
78
|
+
* first on `nativeOptIn()`. Returns null (never throws) on any failure
|
|
79
|
+
* (opt-in off, absent package, unreadable asset, bad session creation) so the
|
|
80
|
+
* caller demotes to mode B trigram.
|
|
81
|
+
*/
|
|
82
|
+
export async function createNativeSession(
|
|
83
|
+
modelPath: string,
|
|
84
|
+
options: { threads?: number; maxTokens?: number } = {},
|
|
85
|
+
): Promise<NativeSession | null> {
|
|
86
|
+
if (!nativeOptIn()) return null;
|
|
87
|
+
|
|
88
|
+
const ort = await loadOrtNative();
|
|
89
|
+
if (!ort || !ort.InferenceSession?.create) return null;
|
|
90
|
+
|
|
91
|
+
const threads = options.threads ?? 4;
|
|
92
|
+
const maxTokens = options.maxTokens ?? ENCODER_MAX_TOKENS;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const session = await ort.InferenceSession.create(modelPath, {
|
|
96
|
+
executionProviders: ["cpu"],
|
|
97
|
+
intraOpNumThreads: threads,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
opset: ENCODER_OPSET,
|
|
102
|
+
semanticWidth: ENCODER_SEMANTIC_WIDTH,
|
|
103
|
+
maxTokens,
|
|
104
|
+
async infer(inputIds: Float32Array): Promise<Float32Array> {
|
|
105
|
+
const feeds = { input_ids: inputIds };
|
|
106
|
+
const results = await session.run(feeds, ["embedding"]);
|
|
107
|
+
const out = results["embedding"];
|
|
108
|
+
if (!out || !(out.data instanceof Float32Array)) {
|
|
109
|
+
return new Float32Array(ENCODER_SEMANTIC_WIDTH);
|
|
110
|
+
}
|
|
111
|
+
return out.data;
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
} catch {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
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
|
+
|
|
22
|
+
import {
|
|
23
|
+
ENCODER_LATENCY_P95_MS,
|
|
24
|
+
type EncoderPlatform,
|
|
25
|
+
} from "./types.js";
|
|
26
|
+
import type { BenchResultV1 } from "./bench-export.js";
|
|
27
|
+
import { ML5C_ENABLED } from "../../config/vector-cortex.js";
|
|
28
|
+
|
|
29
|
+
/** The chosen runtime backend name as it appears on the seller event. */
|
|
30
|
+
export type RuntimeBackendChoice = "wasm" | "native" | "modeB";
|
|
31
|
+
|
|
32
|
+
/** Inputs to the backend selection. `platform` is `${process.platform}-${process.arch}`. */
|
|
33
|
+
export interface RuntimeSelectionInput {
|
|
34
|
+
/** The platform this process is running on (from detectPlatform()). */
|
|
35
|
+
readonly platform: EncoderPlatform | "unsupported";
|
|
36
|
+
/**
|
|
37
|
+
* The ML5-B bench record for this platform (linux-x64 normative). Null when
|
|
38
|
+
* no bench has run yet (degraded: the 42-byte placeholder has no real p95).
|
|
39
|
+
* Must provide a numeric p95Ms for WASM consideration; a null/error record
|
|
40
|
+
* (gates.all:false) forces the native fallback per the decision rule.
|
|
41
|
+
*/
|
|
42
|
+
readonly benchRecord: BenchResultV1 | null;
|
|
43
|
+
/** True when the operator opted into onnxruntime-node via MEGACOMPACT_ENCODER_NATIVE=1. */
|
|
44
|
+
readonly nativeOptIn: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The output of the ML5-C decision-rule dispatch. */
|
|
48
|
+
export interface RuntimeSelectionResult {
|
|
49
|
+
/** The chosen backend. */
|
|
50
|
+
readonly backend: RuntimeBackendChoice;
|
|
51
|
+
/** HG-3 closure state: whether the 80 MiB install budget is satisfied. */
|
|
52
|
+
readonly budgetOk: boolean;
|
|
53
|
+
/** The measured p95 (ms) that drove the decision, from BenchResultV1. */
|
|
54
|
+
readonly p95Ms: number | null;
|
|
55
|
+
/** The platform the selection is valid for. */
|
|
56
|
+
readonly platform: string;
|
|
57
|
+
/** Short human-readable rationale for the choice (record on the event). */
|
|
58
|
+
readonly rationale: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The 80 MiB install budget in bytes (the HG-3 ceiling, unamended). */
|
|
62
|
+
export const RUNTIME_NATIVE_INSTALL_BUDGET_MIB = 80;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Per-platform optionalDependency footprint of onnxruntime-node (MiB, approx).
|
|
66
|
+
* These sum to ~160 MiB SHIPPED in the npm package (every target platform is
|
|
67
|
+
* included so the resolver lands on a concrete row at install time) — which is
|
|
68
|
+
* what exceeds the 80 MiB HG-3 budget and forces the amendment the fixtures
|
|
69
|
+
* record (ML5-RUNTIME-001). The per-host INSTALLED footprint (one row only)
|
|
70
|
+
* is 28–35 MiB and irrelevant to the 80 MiB ceiling — the budget covers the
|
|
71
|
+
* shipped tarball, not the single-platform install.
|
|
72
|
+
*/
|
|
73
|
+
export const NATIVE_FOOTPRINT_MIB: Readonly<Record<EncoderPlatform, number>> = {
|
|
74
|
+
"linux-x64": 33,
|
|
75
|
+
"darwin-arm64": 28,
|
|
76
|
+
"darwin-x64": 33,
|
|
77
|
+
"linux-arm64": 31,
|
|
78
|
+
"win32-x64": 35,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* ML5-C decision-rule dispatch: choose the ONNX runtime backend (pure).
|
|
83
|
+
*
|
|
84
|
+
* When the flag is OFF (`MEGACOMPACT_ML5_C=0`), returns mode B trigram — byte-
|
|
85
|
+
* identical to the ML5-B survivor with no selection event emitted.
|
|
86
|
+
*
|
|
87
|
+
* The rule (from the sprint spec):
|
|
88
|
+
* - If nativeOptIn && platform is supported → native (Option N)
|
|
89
|
+
* - If benchRecord has p95Ms <= 40 ms on linux-x64 → WASM (Option W)
|
|
90
|
+
* - Else → native (Option N) with the budget amendment recorded (p95 exceeds
|
|
91
|
+
* the WASM gate or is absent — the placeholder has no measured p95)
|
|
92
|
+
* - darwin-x64 → WASM or mode B demotion per HG-4 (never native here)
|
|
93
|
+
*/
|
|
94
|
+
export function selectRuntimeBackend(input: RuntimeSelectionInput): RuntimeSelectionResult {
|
|
95
|
+
if (!ML5C_ENABLED()) {
|
|
96
|
+
return {
|
|
97
|
+
backend: "modeB",
|
|
98
|
+
budgetOk: true,
|
|
99
|
+
p95Ms: null,
|
|
100
|
+
platform: input.platform,
|
|
101
|
+
rationale: "flag-off: byte-identical mode-B trigram (no selection)",
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// HG-4: Intel Mac (darwin-x64) is out-of-scope per HG-1's deferral — always demote.
|
|
106
|
+
if (input.platform === "darwin-x64") {
|
|
107
|
+
return {
|
|
108
|
+
backend: "wasm",
|
|
109
|
+
budgetOk: true,
|
|
110
|
+
p95Ms: null,
|
|
111
|
+
platform: input.platform,
|
|
112
|
+
rationale: "darwin-x64 demoted to WASM per HG-4 (never native on this platform)",
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Native opt-in short-circuits: operator explicitly wants the native path.
|
|
117
|
+
// The HG-3 budget compares the SHIPPED byte-count (sum across every platform
|
|
118
|
+
// row in the package's optionalDependencies map) against the 80 MiB ceiling
|
|
119
|
+
// — not the single-platform install size. Native always exceeds 80 MiB across
|
|
120
|
+
// 5 platforms (~160 MiB shipped), so budgetOk is false and the evidence
|
|
121
|
+
// records the amended budget (the ML5-C spec, HG-3 closure).
|
|
122
|
+
if (input.nativeOptIn) {
|
|
123
|
+
const shippedMib = Object.values(NATIVE_FOOTPRINT_MIB).reduce((a, b) => a + b, 0);
|
|
124
|
+
return {
|
|
125
|
+
backend: "native",
|
|
126
|
+
budgetOk: shippedMib <= RUNTIME_NATIVE_INSTALL_BUDGET_MIB,
|
|
127
|
+
p95Ms: input.benchRecord?.p95Ms ?? null,
|
|
128
|
+
platform: input.platform,
|
|
129
|
+
rationale: `native opt-in (MEGACOMPACT_ENCODER_NATIVE=1); shipped ${shippedMib} MiB across 5 platforms → budget amended to ${shippedMib} MiB`,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// No bench record or degraded (gates.all:false) → WASM cannot qualify (the
|
|
134
|
+
// placeholder 42-byte asset has no measured real p95), so native is selected
|
|
135
|
+
// with the SAME amended-budget disposition as the opt-in path above: the
|
|
136
|
+
// evidence records the closed HG-3 amendment.
|
|
137
|
+
if (!input.benchRecord || !input.benchRecord.gates.all || input.benchRecord.p95Ms === null) {
|
|
138
|
+
const shippedMib = Object.values(NATIVE_FOOTPRINT_MIB).reduce((a, b) => a + b, 0);
|
|
139
|
+
return {
|
|
140
|
+
backend: "native",
|
|
141
|
+
budgetOk: false, // amended: native ships > 80 MiB across the 5-platform matrix
|
|
142
|
+
p95Ms: input.benchRecord?.p95Ms ?? null,
|
|
143
|
+
platform: input.platform,
|
|
144
|
+
rationale: `no qualifying bench record — native fallback with budget amendment (${shippedMib} MiB shipped, HG-3 amendment recorded)`,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// The decision rule: WASM iff p95 <= 40 ms (linux-x64, 512 tokens, 4 threads) —
|
|
149
|
+
// native required otherwise, with the same budget amendment recorded.
|
|
150
|
+
if (input.benchRecord.p95Ms <= ENCODER_LATENCY_P95_MS) {
|
|
151
|
+
return {
|
|
152
|
+
backend: "wasm",
|
|
153
|
+
budgetOk: true,
|
|
154
|
+
p95Ms: input.benchRecord.p95Ms,
|
|
155
|
+
platform: input.platform,
|
|
156
|
+
rationale: `WASM qualifies: p95 ${input.benchRecord.p95Ms}ms <= ${ENCODER_LATENCY_P95_MS}ms`,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
backend: "native",
|
|
162
|
+
budgetOk: false, // amended: native exceeds the 80 MiB budget per the evidence
|
|
163
|
+
p95Ms: input.benchRecord.p95Ms,
|
|
164
|
+
platform: input.platform,
|
|
165
|
+
rationale: `native required: p95 ${input.benchRecord.p95Ms}ms > ${ENCODER_LATENCY_P95_MS}ms on WASM`,
|
|
166
|
+
};
|
|
167
|
+
}
|