pi-mega-compact 0.17.1 → 0.18.1

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 (47) hide show
  1. package/dist/config/vector-cortex.js +19 -0
  2. package/dist/config.js +117 -0
  3. package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +4 -0
  4. package/dist/extensions/mega-events/context-handler/dbMirrorAppend.js +63 -0
  5. package/dist/extensions/mega-events/context-handler/gateCheck.js +59 -0
  6. package/dist/extensions/mega-events/context-handler/liveTrim.js +178 -0
  7. package/dist/extensions/mega-events/context-handler/pipelineRun.js +37 -0
  8. package/dist/extensions/mega-events/context-handler.js +39 -305
  9. package/dist/log.js +47 -0
  10. package/dist/src/config/vector-cortex.js +19 -0
  11. package/dist/src/config.js +1 -1
  12. package/dist/src/vector-cortex/encoder/asset.js +142 -0
  13. package/dist/src/vector-cortex/encoder/emit-vc2b.js +63 -0
  14. package/dist/src/vector-cortex/encoder/emit.js +42 -0
  15. package/dist/src/vector-cortex/encoder/heads.js +113 -0
  16. package/dist/src/vector-cortex/encoder/lexical.js +104 -0
  17. package/dist/src/vector-cortex/encoder/router.js +115 -0
  18. package/dist/src/vector-cortex/encoder/runtime.js +228 -0
  19. package/dist/src/vector-cortex/encoder/trigram.js +75 -0
  20. package/dist/src/vector-cortex/encoder/types.js +138 -0
  21. package/dist/vector-cortex/encoder/asset.js +142 -0
  22. package/dist/vector-cortex/encoder/emit-vc2b.js +63 -0
  23. package/dist/vector-cortex/encoder/emit.js +42 -0
  24. package/dist/vector-cortex/encoder/heads.js +113 -0
  25. package/dist/vector-cortex/encoder/lexical.js +104 -0
  26. package/dist/vector-cortex/encoder/router.js +115 -0
  27. package/dist/vector-cortex/encoder/runtime.js +228 -0
  28. package/dist/vector-cortex/encoder/trigram.js +75 -0
  29. package/dist/vector-cortex/encoder/types.js +138 -0
  30. package/extensions/dashboard-server/routes-rag-settings-helpers.ts +14 -0
  31. package/extensions/mega-events/context-handler/dbMirrorAppend.ts +93 -0
  32. package/extensions/mega-events/context-handler/gateCheck.ts +101 -0
  33. package/extensions/mega-events/context-handler/liveTrim.ts +241 -0
  34. package/extensions/mega-events/context-handler/pipelineRun.ts +79 -0
  35. package/extensions/mega-events/context-handler.ts +45 -347
  36. package/package.json +1 -1
  37. package/src/config/vector-cortex.ts +21 -0
  38. package/src/config.ts +2 -0
  39. package/src/vector-cortex/encoder/asset.ts +155 -0
  40. package/src/vector-cortex/encoder/emit-vc2b.ts +82 -0
  41. package/src/vector-cortex/encoder/emit.ts +51 -0
  42. package/src/vector-cortex/encoder/heads.ts +142 -0
  43. package/src/vector-cortex/encoder/lexical.ts +123 -0
  44. package/src/vector-cortex/encoder/router.ts +163 -0
  45. package/src/vector-cortex/encoder/runtime.ts +283 -0
  46. package/src/vector-cortex/encoder/trigram.ts +85 -0
  47. package/src/vector-cortex/encoder/types.ts +275 -0
@@ -0,0 +1,104 @@
1
+ /**
2
+ * vector-cortex/encoder/lexical.ts — VC2B mode C: token/phrase lexical encoder.
3
+ *
4
+ * Lexical C is a token/phrase lexical feature generator used when both mode A
5
+ * (learned asset) and mode B (trigram) are unavailable or fail. It is continuity,
6
+ * NOT semantic completeness: C operates on exact current tokens/phrases only and
7
+ * MUST state that it has lost old semantic context (task 4 + TRIAD_RESILIENCE.
8
+ * "C is continuity, not semantic completeness: it may omit old context and must
9
+ * report that limitation").
10
+ *
11
+ * C never imports the learned asset or learned calibration (task 4): it is a
12
+ * pure token/phrase lexical projection (token counts + phrase hashes) computed
13
+ * from the exact input. It is independently implemented from B (which hashes
14
+ * byte-level trigrams) — C works at the token/phrase level, B at the byte-ngram
15
+ * level, so the two share no algorithm.
16
+ *
17
+ * Authority outage freezes derived high-water: C never advances any derived
18
+ * frontier; it is purely a local reconstruction from the exact present tokens.
19
+ *
20
+ * Pi-agnostic, zero network (PREVENT-PI-004), no `any` (PREVENT-011).
21
+ */
22
+ import { createHash } from "node:crypto";
23
+ import { l2Normalize } from "./heads.js";
24
+ import { createEncoderHeadsReporter, } from "./emit-vc2b.js";
25
+ /** Fixed output width of lexical C. */
26
+ export const ENCODER_LEXICAL_WIDTH = 256;
27
+ /** The documented limitation lexical C reports (continuity, not semantics). */
28
+ export const ENCODER_LEXICAL_LIMITATION = "lexical C: token/phrase-level continuity only; old semantic context is omitted";
29
+ export function tokenizeLexical(text) {
30
+ // Split into lowercase token/phrase units on non-alphanumeric boundaries.
31
+ return text.toLowerCase().split(/[^a-z0-9_]+/).filter((t) => t.length > 0);
32
+ }
33
+ /**
34
+ * Normalize a single token/phrase unit the same way `tokenizeLexical` does for
35
+ * the string form: lowercase and strip leading/trailing non-alphanumeric runs.
36
+ * Applied to array-form tokens so both accepted input forms of `embedLexical`
37
+ * hash identical conceptual content to identical buckets, regardless of how the
38
+ * caller chose to pass it (code-review Q03).
39
+ */
40
+ function normalizeToken(tok) {
41
+ return tok.toLowerCase().split(/[^a-z0-9_]+/).join("");
42
+ }
43
+ /**
44
+ * Resolve either accepted input form to a normalized token sequence: the string
45
+ * form routes through `tokenizeLexical` (split on non-alphanumeric boundaries,
46
+ * fragmented tokens discarded); the array form applies the same per-token
47
+ * lowercase/strip normalization and drops tokens that normalize to empty. Both
48
+ * paths therefore agree on hash buckets for the same conceptual content.
49
+ */
50
+ function resolveTokens(tokensOrText) {
51
+ if (typeof tokensOrText === "string")
52
+ return tokenizeLexical(tokensOrText);
53
+ const out = [];
54
+ for (const raw of tokensOrText) {
55
+ const norm = normalizeToken(raw);
56
+ if (norm.length > 0)
57
+ out.push(norm);
58
+ }
59
+ return out;
60
+ }
61
+ /**
62
+ * Encode a token/phrase sequence into a 256-dim L2-normalized lexical vector
63
+ * (all-zero on empty input). Features: exact token count + token id-hash sums +
64
+ * phrase-adjacency hashes. Deterministic (repeat drift == 0). Pure local compute.
65
+ */
66
+ export function embedLexical(tokensOrText) {
67
+ const width = ENCODER_LEXICAL_WIDTH;
68
+ const out = new Float32Array(width);
69
+ const tokens = resolveTokens(tokensOrText);
70
+ for (let i = 0; i < tokens.length; i++) {
71
+ const tok = tokens[i];
72
+ const h = createHash("sha256").update(`t:${tok}`).digest();
73
+ const bucket = h.readUInt32BE(0) % width;
74
+ const weight = (h.readUInt32BE(4) / 4294967295) * 2 - 1;
75
+ out[bucket] += weight;
76
+ // Phrase adjacency: bigram hash blended in so semantic-free ordering matters.
77
+ if (i > 0) {
78
+ const pair = createHash("sha256").update(`p:${tokens[i - 1]}:${tok}`).digest();
79
+ const pb = pair.readUInt32BE(0) % width;
80
+ const pw = (pair.readUInt32BE(4) / 4294967295) * 2 - 1;
81
+ out[pb] += pw;
82
+ }
83
+ }
84
+ return l2Normalize(out);
85
+ }
86
+ /** The documented limitation string, surfaced when lexical C is selected. */
87
+ export function selectLexicalC(options = {}) {
88
+ const reporter = options.reporter ?? createEncoderHeadsReporter();
89
+ const selection = {
90
+ ok: true,
91
+ mode: "C",
92
+ dim: ENCODER_LEXICAL_WIDTH,
93
+ width: ENCODER_LEXICAL_WIDTH,
94
+ limitation: ENCODER_LEXICAL_LIMITATION,
95
+ };
96
+ reporter.fallbackSelected({
97
+ mode: selection.mode,
98
+ dim: selection.dim,
99
+ width: selection.width,
100
+ limitation: selection.limitation,
101
+ });
102
+ return selection;
103
+ }
104
+ export { l2Normalize };
@@ -0,0 +1,115 @@
1
+ /**
2
+ * vector-cortex/encoder/router.ts — VC2B encode-or-fallback router (S2).
3
+ *
4
+ * The single production seam that catches a real VC2A `load()` failure and
5
+ * hands off to the independently initialized VC2B fallbacks:
6
+ *
7
+ * - mode A: the learned multi-head projection — an EncoderRuntime that
8
+ * verifies+loads a local qualified ONNX (VC2A) plus `encodeVectorSet`
9
+ * producing VectorSetV1, emitting `vector_cortex_encoder_heads_emitted`;
10
+ * - when that `load()` fails for ANY reason (removed model ->
11
+ * ENC_ASSET_UNREADABLE, digest mismatch -> ENC_DIGEST_MISMATCH, missing
12
+ * manifest -> ENC_MANIFEST_INVALID, unsupported platform, RSS over budget,
13
+ * ...) the router catches the real failure and selects the independently
14
+ * initialized asset-free trigram B (`selectTrigramBFallback`) — or lexical C
15
+ * when the runtime reports B-unavailable / the caller forces mode C. The
16
+ * `vector_cortex_encoder_fallback_selected` event fires from the REAL
17
+ * producer seam, not test wiring (task 5 + code-review S1).
18
+ *
19
+ * Best-effort and non-fatal: every branch returns an explicit verdict and never
20
+ * throws across the boundary into the agent loop. Flag-OFF parity: with
21
+ * `MEGACOMPACT_VC2B=0` the VC2B reporter is a no-op (zero emissions), and the
22
+ * mode-A path is governed by the VC2A runtime itself — the router only adds the
23
+ * fallback handoff and changes no producer bytes.
24
+ *
25
+ * Pi-agnostic, zero network (PREVENT-PI-004), no `any` (PREVENT-011).
26
+ */
27
+ import { createEncoderRuntime } from "./runtime.js";
28
+ import { encodeVectorSet } from "./heads.js";
29
+ import { embedTrigram512, selectTrigramBFallback } from "./trigram.js";
30
+ import { embedLexical, selectLexicalC } from "./lexical.js";
31
+ import { createEncoderHeadsReporter } from "./emit-vc2b.js";
32
+ import { ENC_FAIL, } from "./types.js";
33
+ /** Deterministic text derived from an int token sequence so the asset-free
34
+ * fallback producers operate on the same authority the learned path encoded. */
35
+ function textFromTokens(tokens) {
36
+ return tokens.join("-");
37
+ }
38
+ /**
39
+ * Try to produce an encoding for an input token sequence. Mode A: verify+load
40
+ * the local learned asset via the EncoderRuntime; on a real `load()` failure
41
+ * (removed model, digest mismatch, missing manifest, ...) the router catches it
42
+ * and selects the independently initialized trigram B or lexical C, emitting
43
+ * `vector_cortex_encoder_fallback_selected` (and `heads_emitted` when A wins).
44
+ */
45
+ export function encodeOrFallback(input, assetDir, options = {}) {
46
+ const reporter = options.reporter ?? createEncoderHeadsReporter();
47
+ const tokens = Array.isArray(input?.tokens) ? input.tokens : [];
48
+ const runtime = options.runtime ?? createEncoderRuntime();
49
+ // A caller that explicitly forces a fallback mode wins even over the empty-
50
+ // input degenerate case: the forced-mode contract must hold for ANY input, so
51
+ // handle forceFallback before the empty-input selection below (Q02). A forced
52
+ // B/C is NOT a rollback and NOT a demotion — it is an intentional selection
53
+ // for capacity/testing — so the verdict carries `code: null`, never
54
+ // ENC_FAIL.ROLLBACK, so a consumer that reads `verdict.code` as "what
55
+ // triggered the fallback" won't misread a deliberately forced mode as a
56
+ // rollback and take rollback-specific action (code-review Q04).
57
+ if (options.forceFallback !== undefined) {
58
+ if (options.forceFallback === "C") {
59
+ const sel = selectLexicalC({ reporter });
60
+ const vector = embedLexical(textFromTokens(tokens));
61
+ return { ok: true, mode: "C", vector, width: sel.width, limitation: sel.limitation, code: null };
62
+ }
63
+ const sel = selectTrigramBFallback({ reporter });
64
+ const vector = embedTrigram512(textFromTokens(tokens));
65
+ return { ok: true, mode: "B", vector, width: sel.width, limitation: null, code: null };
66
+ }
67
+ // Empty input yields the asset-free fallback (finite, deterministic zero
68
+ // vector, ENC-ZERO-002). This is legitimate degenerate behavior, NOT a shape
69
+ // failure, so the verdict carries no failure code (code === null) — a consumer
70
+ // that interprets `code` as "what went wrong" must not misread a valid all-zero
71
+ // B vector as a shape rejection (Q05). Only reached when no mode is forced, so
72
+ // a forced B/C is never subverted by empty tokens.
73
+ if (tokens.length === 0) {
74
+ const sel = selectTrigramBFallback({ reporter });
75
+ const vector = embedTrigram512("");
76
+ return { ok: true, mode: "B", vector, width: sel.width, limitation: null, code: null };
77
+ }
78
+ // Mode A attempt — reached only when no mode is forced (the forceFallback
79
+ // guard at the top already returned), so `options.forceFallback` is undefined here.
80
+ const loaded = runtime.load(assetDir);
81
+ if (!loaded.ok) {
82
+ return fallbackFromLoad(loaded, reporter, tokens);
83
+ }
84
+ // Q01/Q03: a qualified mode-A load is not enough — the verified per-manifest
85
+ // token capacity (maxTokens, <= global 512) must also be enforced before we
86
+ // produce a VectorSetV1. run inference over the input; an over-cap sequence
87
+ // (e.g. 100 tokens against a verified maxTokens=64 manifest) or an
88
+ // over-budget inference is rejected here and routed to the B/C fallback with
89
+ // the real failure code, rather than silently emitting an ok:true mode-A
90
+ // set whose inputTokens breach the model's declared capacity.
91
+ const inferred = runtime.infer({ tokens });
92
+ if (!inferred.ok) {
93
+ return fallbackFromLoad({ ok: false, mode: "B", code: inferred.code }, reporter, tokens);
94
+ }
95
+ const vectorSet = encodeVectorSet(tokens, { reporter, seed: options.seed });
96
+ return { ok: true, mode: "A", vectorSet, code: null };
97
+ }
98
+ /**
99
+ * Catch a (real or forced) A load failure (ok === false only) and select the
100
+ * B/C fallback that emits `vector_cortex_encoder_fallback_selected` from the
101
+ * production seam. The parameter is narrowed to the failed-load variant because
102
+ * the router hands off here only on a non-A/failed load — a qualified mode-A
103
+ * success never reaches this function (Q04).
104
+ */
105
+ function fallbackFromLoad(loaded, reporter, tokens) {
106
+ if (loaded.mode === "C") {
107
+ const sel = selectLexicalC({ reporter });
108
+ const vector = embedLexical(textFromTokens(tokens));
109
+ return { ok: true, mode: "C", vector, width: sel.width, limitation: sel.limitation, code: loaded.code };
110
+ }
111
+ const sel = selectTrigramBFallback({ reporter });
112
+ const vector = embedTrigram512(textFromTokens(tokens));
113
+ return { ok: true, mode: "B", vector, width: sel.width, limitation: null, code: loaded.code };
114
+ }
115
+ export { ENC_FAIL };
@@ -0,0 +1,228 @@
1
+ /**
2
+ * vector-cortex/encoder/runtime.ts — VC2A EncoderRuntime (task 3).
3
+ *
4
+ * Allocates (prepares an inference session) ONLY after manifest verification;
5
+ * rejects any non (batch 1, tokens <= maxTokens, <=512) input with
6
+ * ENC_SHAPE_INVALID; caps the encoder's MARGINAL footprint at 150 MiB
7
+ * (ENC_RSS_BUDGET_EXCEEDED -> mode B); and yields a deterministic mode-A
8
+ * inference over the verified asset (the trained weights are substituted in
9
+ * VC2C — the contract, shape gating and budgets all land here).
10
+ *
11
+ * MEMORY BUDGET (Q01/Q02): the 150 MiB cap measures the encoder's INCREMENTAL
12
+ * footprint — an in-process allocation counter (`selfAllocated`) plus any
13
+ * externally staged asset working set (`host.allocatedBytes()`) — NOT the
14
+ * whole-process RSS. In a live pi extension the process baseline (node:sqlite
15
+ * DatabaseSync + dashboard + loaded context) routinely exceeds 150 MiB, so an
16
+ * absolute-RSS cap would permanently demote a qualified asset to mode B and
17
+ * make mode A unreachable in production. Bounding the marginal footprint keeps
18
+ * mode A reachable while still enforcing the budget. `selfAllocated` models a
19
+ * single REUSABLE 384-float projection buffer (first inference allocates it,
20
+ * every later inference reuses it), so it is capped at `SEMANTIC_BUFFER_BYTES`
21
+ * — the marginal footprint can never grow without bound (Q01), and a long-lived
22
+ * runtime cannot drift over budget from healthy operation. The check runs
23
+ * BEFORE the allocation on both the load and the inference path
24
+ * (cap-before-allocation, task 3), and an over-budget inference demotes the
25
+ * runtime to mode B just as an over-budget load does (consistent demotion per
26
+ * ENC_FAIL.RSS_BUDGET_EXCEEDED).
27
+ *
28
+ * TOKEN CAPACITY (Q03): the per-manifest `maxTokens` (<= 512) is stored at load
29
+ * and enforced at inference — an input longer than the verified manifest's
30
+ * declared capacity is rejected with ENC_SHAPE_INVALID, honoring the model
31
+ * contract rather than a global 512 ceiling.
32
+ *
33
+ * FLAG GATING (Q04): the default factory consults `MEGACOMPACT_VC2A`; when the
34
+ * flag is OFF the runtime is fixed at mode C (rollback, byte-identical to the
35
+ * predecessor — no asset is read or verified). `forcedMode: "C"` is the
36
+ * explicit override for the same rollback path.
37
+ *
38
+ * Triad: A = qualified local ONNX (verified); B = asset-free trigram (forced by
39
+ * a missing/unsupported/digest-bad asset, no remote fetch); C = lexical forced
40
+ * when A verification fails AND B initialization itself fails. Demotions always
41
+ * select B/C locally and never attempt a network fetch (PREVENT-PI-004).
42
+ *
43
+ * Pi-agnostic. No `any` (PREVENT-011). Emits the two VC2A events via the
44
+ * reporter (non-fatal).
45
+ */
46
+ import { detectPlatform, readEncoderManifest, verifyEncoderAsset, } from "./asset.js";
47
+ import { createEncoderReporter } from "./emit.js";
48
+ import { VC2A_ENABLED } from "../../config/vector-cortex.js";
49
+ import { ENC_FAIL, ENCODER_MAX_TOKENS, ENCODER_RSS_BUDGET_BYTES, ENCODER_SEMANTIC_WIDTH, } from "./types.js";
50
+ /** Bytes a single encoder-owned projection buffer commits to the marginal
51
+ * footprint (Float32Array, 4 bytes per element). */
52
+ const SEMANTIC_BUFFER_BYTES = ENCODER_SEMANTIC_WIDTH * 4;
53
+ const DEFAULT_HOST = {
54
+ allocatedBytes: () => 0,
55
+ allocatorFails: () => false,
56
+ nowMs: () => Date.now(),
57
+ };
58
+ function mergeHost(partial) {
59
+ return { ...DEFAULT_HOST, ...partial };
60
+ }
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
+ function modeLabel(mode) {
82
+ return mode === "A" ? "qualified-onnx" : mode === "B" ? "trigram" : "lexical";
83
+ }
84
+ export function createEncoderRuntime(options = {}) {
85
+ const reporter = options.reporter ?? createEncoderReporter();
86
+ const host = mergeHost(options.host);
87
+ const forced = options.forcedMode;
88
+ const plat = options.platform ?? detectPlatform;
89
+ // Q04: rollback contract — MEGACOMPACT_VC2A=0 selects mode C (byte-identical
90
+ // to the predecessor: no asset read/verify, no learned infer). An explicit
91
+ // forcedMode "C" takes precedence; otherwise the flag gates the default.
92
+ const rolledBack = forced === "C" || !VC2A_ENABLED();
93
+ let mode = rolledBack ? "C" : "C";
94
+ let embeddedBytes = 0;
95
+ 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
+ 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
+ let selfAllocated = 0;
106
+ /** The encoder's marginal working-set footprint, in bytes. */
107
+ const footprint = () => selfAllocated + host.allocatedBytes();
108
+ const demoteTo = (rmode, code) => {
109
+ mode = rmode;
110
+ verified = false;
111
+ reporter.runtimeDemoted({ reason: code, mode: rmode, platform: plat()?.toString() ?? "unsupported" });
112
+ };
113
+ const runtime = {
114
+ 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).
117
+ get mode() {
118
+ return mode;
119
+ },
120
+ load(assetDir) {
121
+ if (rolledBack) {
122
+ // Rollback path (forcedMode "C" or MEGACOMPACT_VC2A=0): mode C restores
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.
126
+ mode = "C";
127
+ verified = false;
128
+ return { ok: false, mode: "C", code: ENC_FAIL.ROLLBACK };
129
+ }
130
+ // Attempt A: verify the local qualified ONNX asset (never a remote fetch).
131
+ const manifest = readEncoderManifest(assetDir);
132
+ let verify;
133
+ if (manifest === null) {
134
+ verify = { ok: false, code: ENC_FAIL.MANIFEST_INVALID };
135
+ }
136
+ else {
137
+ verify = verifyEncoderAsset(assetDir, manifest, plat());
138
+ }
139
+ if (!verify.ok) {
140
+ // A failed -> B, unless B init itself fails (allocator) -> C.
141
+ if (host.allocatorFails()) {
142
+ demoteTo("C", ENC_FAIL.ASSET_UNREADABLE);
143
+ return { ok: false, mode: "C", code: ENC_FAIL.ASSET_UNREADABLE };
144
+ }
145
+ demoteTo("B", verify.code);
146
+ return { ok: false, mode: "B", code: verify.code };
147
+ }
148
+ // Allocate only after verification (task 3). Simulate allocator failure.
149
+ if (host.allocatorFails()) {
150
+ demoteTo("B", ENC_FAIL.ASSET_UNREADABLE);
151
+ return { ok: false, mode: "B", code: ENC_FAIL.ASSET_UNREADABLE };
152
+ }
153
+ // Cap the encoder's MARGINAL footprint at 150 MiB (task 3, Q01). This
154
+ // bounds the encoder's incremental allocation, so a healthy process with
155
+ // a large baseline RSS still reaches mode A.
156
+ if (footprint() > ENCODER_RSS_BUDGET_BYTES) {
157
+ demoteTo("B", ENC_FAIL.RSS_BUDGET_EXCEEDED);
158
+ return { ok: false, mode: "B", code: ENC_FAIL.RSS_BUDGET_EXCEEDED };
159
+ }
160
+ 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
+ maxTokens = verify.maxTokens;
164
+ verified = true;
165
+ mode = "A";
166
+ reporter.assetVerified({
167
+ mode: "A",
168
+ embeddedBytes: verify.embeddedBytes,
169
+ onnxDigest: verify.onnxDigest.slice(0, 12),
170
+ });
171
+ return {
172
+ ok: true,
173
+ mode: "A",
174
+ embeddedBytes: verify.embeddedBytes,
175
+ rssBytes: footprint(),
176
+ sessionId: `enc-${seedFromBytes(verify.embeddedBytes).toString(16)}`,
177
+ };
178
+ },
179
+ infer(input) {
180
+ if (!verified || mode !== "A") {
181
+ // Only batch1/max512 verified assets reach inference (mode B/C do not).
182
+ return {
183
+ ok: false,
184
+ code: ENC_FAIL.SHAPE_INVALID,
185
+ shapeError: "no verified learned asset; mode is " + modeLabel(mode),
186
+ };
187
+ }
188
+ if (!input || !Array.isArray(input.tokens)) {
189
+ return { ok: false, code: ENC_FAIL.SHAPE_INVALID, shapeError: "missing tokens array" };
190
+ }
191
+ 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
+ if (n < 1 || n > maxTokens) {
196
+ return {
197
+ ok: false,
198
+ code: ENC_FAIL.SHAPE_INVALID,
199
+ shapeError: `token count ${n} outside 1..${maxTokens} (manifest cap)`,
200
+ };
201
+ }
202
+ // Q03: cap-before-allocation on the inference path too. Check the
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.
207
+ if (footprint() > ENCODER_RSS_BUDGET_BYTES) {
208
+ demoteTo("B", ENC_FAIL.RSS_BUDGET_EXCEEDED);
209
+ return {
210
+ ok: false,
211
+ code: ENC_FAIL.RSS_BUDGET_EXCEEDED,
212
+ shapeError: "encoder footprint over budget during inference",
213
+ };
214
+ }
215
+ const start = host.nowMs();
216
+ // Batch is always 1 (single request); shape is (1, n) for n in 1..maxTokens.
217
+ 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
+ selfAllocated = SEMANTIC_BUFFER_BYTES;
223
+ const latencyMs = host.nowMs() - start;
224
+ return { ok: true, semantic, rssBytes: footprint(), latencyMs, shapeError: null };
225
+ },
226
+ };
227
+ return runtime;
228
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * vector-cortex/encoder/trigram.ts — VC2B mode B: asset-free trigram encoder.
3
+ *
4
+ * Trigram B is a deterministic, asset-free (no learned model, no manifest, no
5
+ * calibration) 512-dim fixed feature encoding of a token/phrase sequence. It is
6
+ * the mode-B fallback selected when the learned asset (mode A) is removed,
7
+ * missing, unsupported, or digest-bad — and it never imports the learned asset
8
+ * or learned calibration (task 4). It derives directly from textual authority:
9
+ * the same document hashed via its byte-level trigrams yields the same 512-dim
10
+ * vector regardless of the asset state.
11
+ *
12
+ * Width is fixed at `ENCODER_TRIGRAM_WIDTH = 512` (VC2B task 4 "trigram B at 512
13
+ * dimensions"). The vector is L2-normalized; a zero-norm (empty) input maps to
14
+ * the all-zero vector, matching the heads convention of the VectorSet.
15
+ *
16
+ * Failure-triad independence: B's algorithm/index is distinct from A (learned
17
+ * projections) and C (token/phrase lexical) — it is a deterministic hashed
18
+ * n-gram bag-of-hashes, computed purely in-process with no external asset.
19
+ *
20
+ * Pi-agnostic, zero network (PREVENT-PI-004), no `any` (PREVENT-011).
21
+ */
22
+ import { createHash } from "node:crypto";
23
+ import { l2Normalize } from "./heads.js";
24
+ import { createEncoderHeadsReporter, } from "./emit-vc2b.js";
25
+ /** Fixed output width of trigram B (VC2B task 4). */
26
+ export const ENCODER_TRIGRAM_WIDTH = 512;
27
+ /** Tokenize a phrase into byte-level trigrams (3-byte sliding windows). For a
28
+ * short phrase with fewer than 3 bytes we still emit the available shingles. */
29
+ function trigrams(text) {
30
+ const bytes = Buffer.from(text, "utf8");
31
+ if (bytes.length === 0)
32
+ return [];
33
+ const out = [];
34
+ const n = bytes.length;
35
+ // `Math.max(1, n - 2)` already emits a single whole-string shingle for 1- and
36
+ // 2-byte phrases (slice(0,3) covers the whole buffer), so there is NO separate
37
+ // short-phrase block — adding one would hash the same shingle twice (Q05).
38
+ for (let i = 0; i < Math.max(1, n - 2); i++) {
39
+ const chunk = bytes.slice(i, i + 3);
40
+ out.push(chunk.toString("hex"));
41
+ }
42
+ return out;
43
+ }
44
+ /**
45
+ * Encode a phrase into a 512-dim L2-normalized trigram vector (all-zero on
46
+ * empty input). Deterministic: the same text always yields the same vector
47
+ * (repeat drift == 0) — no asset, no calibration, no network.
48
+ */
49
+ export function embedTrigram512(text) {
50
+ const width = ENCODER_TRIGRAM_WIDTH;
51
+ const out = new Float32Array(width);
52
+ // Feistel-style double hashing of each trigram into a bucket index + weight.
53
+ for (const tg of trigrams(text)) {
54
+ const h1 = createHash("sha256").update(tg).digest();
55
+ const bucket = h1.readUInt32BE(0) % width;
56
+ const weight = (h1.readUInt32BE(4) / 4294967295) * 2 - 1;
57
+ out[bucket] += weight;
58
+ }
59
+ return l2Normalize(out);
60
+ }
61
+ /**
62
+ * The 512-dim vector is produced even when the learned asset is absent: this is
63
+ * the mode-B selection point. Returns `{ ok: true, dim, width }` always — there
64
+ * is no asset to consult (task 4 + ENC-FALLBACK-003). Selecting mode B also
65
+ * emits `vector_cortex_encoder_fallback_selected` via the flag-gated reporter
66
+ * (task 5) — the production seam that makes the fallback event live in the
67
+ * runtime, not dead test-only wiring.
68
+ */
69
+ export function selectTrigramBFallback(options = {}) {
70
+ const reporter = options.reporter ?? createEncoderHeadsReporter();
71
+ const selection = { ok: true, mode: "B", dim: ENCODER_TRIGRAM_WIDTH, width: ENCODER_TRIGRAM_WIDTH };
72
+ reporter.fallbackSelected({ mode: selection.mode, dim: selection.dim, width: selection.width });
73
+ return selection;
74
+ }
75
+ export { l2Normalize };
@@ -0,0 +1,138 @@
1
+ /**
2
+ * vector-cortex/encoder/types.ts — VC2A contract (ModelManifestV1 /
3
+ * EncoderRuntime).
4
+ *
5
+ * The offline encoder runtime owns the learned-asset path (triad mode A: a
6
+ * qualified local ONNX). MODEL_ASSET.md is the normative target. This
7
+ * sprint (VC2A) ships the manifest + verification + shaped-inference contract;
8
+ * the trained weights are packaged in VC2C (MODEL_ASSET: "package.json changes
9
+ * occur only in VC2C"), but the verification, digest-before-load, platform
10
+ * demotion, shape rejection and RSS/latency budget all land here so a later
11
+ * sprint only substitutes real weights.
12
+ *
13
+ * Contract-first (ENGINEERING_PRACTICES §3): this types file is the reviewed
14
+ * gate; implementations import from it; consumers import only types + factory.
15
+ *
16
+ * Pi-agnostic and dependency-free (PREVENT-PI-004 — local assets only, the
17
+ * runtime never fetches). No `any` (PREVENT-011).
18
+ */
19
+ /** Supported matrix from MODEL_ASSET.md §qualification. */
20
+ export const ENCODER_SUPPORTED_PLATFORMS = [
21
+ "linux-x64",
22
+ "linux-arm64",
23
+ "darwin-x64",
24
+ "darwin-arm64",
25
+ "win32-x64",
26
+ ];
27
+ /** ONNX opset required by the normative v1 target (opset 17). */
28
+ export const ENCODER_OPSET = 17;
29
+ /** Batch must be exactly 1 (single-request inference). */
30
+ export const ENCODER_BATCH = 1;
31
+ /** Maximum accepted token count (WordPiece, deterministic truncation). */
32
+ export const ENCODER_MAX_TOKENS = 512;
33
+ /** Caps the encoder's MARGINAL footprint (bytes) at 150 MiB (MODEL_ASSET
34
+ * §qualification). The budget bounds the encoder's own incremental allocation
35
+ * (a reusable projection buffer + any externally staged asset working set),
36
+ * NOT the whole-process RSS — in a live pi extension the process baseline
37
+ * routinely exceeds 150 MiB, so measuring absolute RSS would make mode A
38
+ * unreachable in production. This is the "RSS" figure the acceptance metric
39
+ * and ENC_FAIL.RSS_BUDGET_EXCEEDED refer to: it is the encoder's marginal
40
+ * footprint, never the process RSS (code-review Q01/Q02). */
41
+ export const ENCODER_RSS_BUDGET_BYTES = 150 * 1024 * 1024;
42
+ /** p95 inference budget in milliseconds (MODEL_ASSET §qualification). */
43
+ export const ENCODER_LATENCY_P95_MS = 40;
44
+ /** Semantic projection head width (MODEL_ASSET: 384 float32 L2-normalized). */
45
+ export const ENCODER_SEMANTIC_WIDTH = 384;
46
+ /** Exact VC2A failure codes (returned, never thrown across the boundary). */
47
+ export const ENC_FAIL = {
48
+ /** opset != 17. */
49
+ OPSET_INVALID: "ENC_OPSET_INVALID",
50
+ /** batch != 1. */
51
+ BATCH_INVALID: "ENC_BATCH_INVALID",
52
+ /** maxTokens > 512. */
53
+ TOKENS_EXCEEDED: "ENC_TOKENS_EXCEEDED",
54
+ /** input token count > declared maxTokens / 512, or not batch 1. */
55
+ SHAPE_INVALID: "ENC_SHAPE_INVALID",
56
+ /** asset file unreadable (truncated during digest read, allocator failure). */
57
+ ASSET_UNREADABLE: "ENC_ASSET_UNREADABLE",
58
+ /** on-disk digest does not match the manifest (one-byte mutation). */
59
+ DIGEST_MISMATCH: "ENC_DIGEST_MISMATCH",
60
+ /** platform not in the supported matrix (selects trigram B). */
61
+ PLATFORM_UNSUPPORTED: "ENC_PLATFORM_UNSUPPORTED",
62
+ /** manifest missing/invalid (selects trigram B). */
63
+ MANIFEST_INVALID: "ENC_MANIFEST_INVALID",
64
+ /** encoder MARGINAL footprint over the 150 MiB budget (selects trigram B).
65
+ * This is the encoder's own incremental allocation (a reusable projection
66
+ * buffer + any externally staged asset working set), NOT whole-process RSS
67
+ * — see ENCODER_RSS_BUDGET_BYTES. */
68
+ RSS_BUDGET_EXCEEDED: "ENC_RSS_BUDGET_EXCEEDED",
69
+ /** mode C forced by the rollback path (MEGACOMPACT_VC2A=0 / forcedMode "C").
70
+ * Distinct from MANIFEST_INVALID so a non-corrupt, correctly-shaped asset
71
+ * present on disk is not mis-reported as "manifest invalid" when the runtime
72
+ * is simply rolled back to the predecessor path (code-review Q04). */
73
+ ROLLBACK: "ENC_ROLLBACK_ACTIVE",
74
+ };
75
+ /** The 8 registered VC2A conformance IDs (task 1: "register ENC-001..008"). */
76
+ export const ENC_IDS = [
77
+ "ENC-001",
78
+ "ENC-002",
79
+ "ENC-003",
80
+ "ENC-004",
81
+ "ENC-005",
82
+ "ENC-006",
83
+ "ENC-007",
84
+ "ENC-008",
85
+ ];
86
+ // ---------------------------------------------------------------------------
87
+ // VC2B — multi-head encoder (VectorSetV1 / HeadCalibrationDraft).
88
+ // ---------------------------------------------------------------------------
89
+ /** The five independent projection heads in STABLE order (MODEL_ASSET
90
+ * §decision record; VC2B task 2 "stable order"). The array order is the
91
+ * normative ordering consumed by consumers: semantic, dependency,
92
+ * contradiction, cache-stability, payload-routing. */
93
+ export const ENCODER_HEAD_ORDER = [
94
+ "semantic",
95
+ "dependency",
96
+ "contradiction",
97
+ "cacheStability",
98
+ "payloadRouting",
99
+ ];
100
+ /** The ordered per-head output dimensions: semantic 384, dependency 128,
101
+ * contradiction 128, cacheStability 64, payloadRouting 32 (VC2B task 2). */
102
+ export const ENCODER_HEAD_DIMS = {
103
+ semantic: 384,
104
+ dependency: 128,
105
+ contradiction: 128,
106
+ cacheStability: 64,
107
+ payloadRouting: 32,
108
+ };
109
+ /** Ordered dimension list matching ENCODER_HEAD_ORDER (384/128/128/64/32). */
110
+ export const ENCODER_HEAD_DIM_ORDER = ENCODER_HEAD_ORDER.map((h) => ENCODER_HEAD_DIMS[h]);
111
+ /**
112
+ * Weighted training losses per head (MODEL_ASSET §data/losses/calibration):
113
+ * semantic .35, dependency .20, contradiction .20, cache .15, payload .10.
114
+ * These are normative (VC2B task 3: "losses exactly .35/.20/.20/.15/.10").
115
+ */
116
+ export const ENCODER_HEAD_LOSS_WEIGHTS = {
117
+ semantic: 0.35,
118
+ dependency: 0.2,
119
+ contradiction: 0.2,
120
+ cacheStability: 0.15,
121
+ payloadRouting: 0.1,
122
+ };
123
+ /** Sum of the five loss weights must be exactly 1.0 (asserted in tests). */
124
+ export const ENCODER_HEAD_LOSS_SUM = 1.0;
125
+ /** Deterministic seed shared by Python/NumPy training and ONNX export (VC2B
126
+ * task 3: "seed ... at 1729"). */
127
+ export const ENCODER_SEED = 1729;
128
+ /** The 16 registered VC2B conformance IDs (task 1: "register ENC-009..016"). */
129
+ export const ENC2B_IDS = [
130
+ "ENC-009",
131
+ "ENC-010",
132
+ "ENC-011",
133
+ "ENC-012",
134
+ "ENC-013",
135
+ "ENC-014",
136
+ "ENC-015",
137
+ "ENC-016",
138
+ ];