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,82 @@
1
+ /**
2
+ * vector-cortex/encoder/emit-vc2b.ts — VC2B observability seam.
3
+ *
4
+ * Owns the two VC2B events (task 5), gated on `MEGACOMPACT_VC2B` so the
5
+ * flag-OFF path emits zero events (mode C parity, byte-identical predecessor):
6
+ *
7
+ * vector_cortex_encoder_heads_emitted — a multi-head VectorSetV1 produced
8
+ * vector_cortex_encoder_fallback_selected — a mode B/C fallback selected
9
+ *
10
+ * No dashboard or API change is necessary for this internal sprint (task 5).
11
+ * Every event is a JSON line with `ts` + `event` (ENGINEERING_PRACTICES §8); the
12
+ * emitters are non-fatal (never break the agent loop). Pi-agnostic, zero network
13
+ * (PREVENT-PI-004), no `any` (PREVENT-011).
14
+ */
15
+
16
+ import { VC2B_ENABLED } from "../../config/vector-cortex.js";
17
+ import { Logger } from "../../log.js";
18
+
19
+ export type EncoderEmit = (event: string, fields: Record<string, unknown>) => void;
20
+
21
+ /** The two-event VC2B reporter surface. */
22
+ export interface EncoderHeadsReporter {
23
+ readonly headsEmitted: (fields: Record<string, unknown>) => void;
24
+ readonly fallbackSelected: (fields: Record<string, unknown>) => void;
25
+ }
26
+
27
+ /** A flag-gated no-op reporter (zero emissions, structural no-op). */
28
+ export const NOOP_VC2B_REPORTER: EncoderHeadsReporter = {
29
+ headsEmitted: () => {},
30
+ fallbackSelected: () => {},
31
+ };
32
+
33
+ /**
34
+ * The default emitter: routes both VC2B events into the append-only structured
35
+ * logger (`src/log.ts`) as JSON lines with `ts` + `event`. Supplying `emit:` to
36
+ * `createEncoderHeadsReporter` replaces this with a caller-provided sink (used
37
+ * by tests and downstream consumers). Making the default a REAL producer means a
38
+ * caller that just invokes the producer seam (`encodeOrFallback`, `encodeVectorSet`,
39
+ * `selectTrigramBFallback`, `selectLexicalC`) without injecting an emitter still
40
+ * yields structured telemetry instead of silently dropping every event (task 5,
41
+ * code-review Q01). Best-effort: the logger swallows all I/O errors.
42
+ */
43
+ function defaultEmitFor(logPath: string | undefined): EncoderEmit {
44
+ const logger = new Logger(logPath === undefined ? {} : { path: logPath });
45
+ return (event, fields) => {
46
+ logger.info(event, fields);
47
+ };
48
+ }
49
+
50
+ /** Options for the default logger-backed sink (used when no `emit` is injected). */
51
+ export interface EncoderHeadsEmitOptions {
52
+ /** Where the default structured sink writes (defaults to the global log path). */
53
+ readonly logPath?: string;
54
+ }
55
+
56
+ /**
57
+ * Flag-gated emit, defaulting to a real logger-backed sink. The returned
58
+ * reporter is itself flag-gated (`VC2B_ENABLED`), so wiring it into a producer
59
+ * seam yields zero emissions when `MEGACOMPACT_VC2B=0` (byte-identical to the
60
+ * predecessor). Pass an explicit `emit` to route elsewhere (tests, downstream
61
+ * consumers); omit it to emit real structured log lines (Q01: the default is a
62
+ * live producer, not a silent no-op). `opts.logPath` only redirects the default
63
+ * sink and is ignored when `emit` is supplied.
64
+ */
65
+ export function createEncoderHeadsReporter(
66
+ emit?: EncoderEmit,
67
+ opts: EncoderHeadsEmitOptions = {},
68
+ ): EncoderHeadsReporter {
69
+ const sink = emit ?? defaultEmitFor(opts.logPath);
70
+ const fire = (event: string, fields: Record<string, unknown>): void => {
71
+ if (!VC2B_ENABLED()) return;
72
+ try {
73
+ sink(event, { ...fields, ts: new Date().toISOString() });
74
+ } catch {
75
+ /* non-fatal observability */
76
+ }
77
+ };
78
+ return {
79
+ headsEmitted: (fields) => fire("vector_cortex_encoder_heads_emitted", fields),
80
+ fallbackSelected: (fields) => fire("vector_cortex_encoder_fallback_selected", fields),
81
+ };
82
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * vector-cortex/encoder/emit.ts — VC2A observability seam.
3
+ *
4
+ * Emits the two VC2A structured events, gated on `MEGACOMPACT_VC2A` (mode C
5
+ * parity: flag OFF => zero emissions). Every event is a JSON line with `ts` +
6
+ * `event` (ENGINEERING_PRACTICES §8); the emitters are never fatal on consumer
7
+ * failure (non-fatal observability, never breaks the agent loop).
8
+ *
9
+ * vector_cortex_encoder_asset_verified — a qualified manifest+digest load
10
+ * vector_cortex_encoder_runtime_demoted — a demotion to mode B or C
11
+ *
12
+ * No network, no side effects beyond the supplied emit callback
13
+ * (PREVENT-PI-004 / PREVENT-011).
14
+ */
15
+
16
+ import { VC2A_ENABLED } from "../../config/vector-cortex.js";
17
+
18
+ export type EncoderEmit = (event: string, fields: Record<string, unknown>) => void;
19
+
20
+ /** The two-event reporter surface consumed by the runtime seams. */
21
+ export interface EncoderReporter {
22
+ readonly assetVerified: (fields: Record<string, unknown>) => void;
23
+ readonly runtimeDemoted: (fields: Record<string, unknown>) => void;
24
+ }
25
+
26
+ /** A flag-gated no-op reporter (zero emissions, default when none injected). */
27
+ export const NOOP_ENCODER_REPORTER: EncoderReporter = {
28
+ assetVerified: () => {},
29
+ runtimeDemoted: () => {},
30
+ };
31
+
32
+ /**
33
+ * Flag-gated emit: no-op when VC2A is off or no emitter is supplied. The
34
+ * returned reporter is itself flag-gated (`VC2A_ENABLED`), so wiring it into a
35
+ * runtime seam yields zero emissions when `MEGACOMPACT_VC2A=0` (byte-identical
36
+ * to the predecessor).
37
+ */
38
+ export function createEncoderReporter(emit?: EncoderEmit): EncoderReporter {
39
+ const fire = (event: string, fields: Record<string, unknown>): void => {
40
+ if (!VC2A_ENABLED()) return;
41
+ try {
42
+ emit?.(event, { ...fields, ts: new Date().toISOString() });
43
+ } catch {
44
+ /* non-fatal observability */
45
+ }
46
+ };
47
+ return {
48
+ assetVerified: (fields) => fire("vector_cortex_encoder_asset_verified", fields),
49
+ runtimeDemoted: (fields) => fire("vector_cortex_encoder_runtime_demoted", fields),
50
+ };
51
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * vector-cortex/encoder/heads.ts — VC2B multi-head encoder (tasks 1–2).
3
+ *
4
+ * Produces a `VectorSetV1`: five independent L2-normalized projection heads in
5
+ * STABLE order — semantic 384, dependency 128, contradiction 128, cacheStability
6
+ * 64, payloadRouting 32 (MODEL_ASSET §decision record). Each head L2-normalizes
7
+ * its raw projection; a zero-norm projection maps to an all-zero vector (task 2).
8
+ *
9
+ * The raw per-head projection is a deterministic seeded compression of the input
10
+ * token sequence (seeded by `ENCODER_SEED` and the head's stable index), which
11
+ * mirrors the VC2A `projectSemantic` placeholder pattern: the contract, shape
12
+ * gating, normalization, zero-norm mapping, ordering and loss/seed constants are
13
+ * all normative here; real trained weights are substituted in VC2C. This keeps
14
+ * the mode-A multi-head path fully testable end-to-end today with zero network.
15
+ *
16
+ * The VC2B emit seam (task 5) is wired: producing a VectorSetV1 emits
17
+ * `vector_cortex_encoder_heads_emitted`; selecting a mode B/C fallback emits
18
+ * `vector_cortex_encoder_fallback_selected` — both gated on MEGACOMPACT_VC2B.
19
+ *
20
+ * Pi-agnostic, zero network (PREVENT-PI-004), no `any` (PREVENT-011).
21
+ */
22
+
23
+ import {
24
+ ENCODER_HEAD_DIMS,
25
+ ENCODER_HEAD_ORDER,
26
+ ENCODER_HEAD_LOSS_WEIGHTS,
27
+ ENCODER_HEAD_LOSS_SUM,
28
+ ENCODER_SEED,
29
+ type EncoderHeadName,
30
+ type HeadVector,
31
+ type VectorSetV1,
32
+ } from "./types.js";
33
+ import {
34
+ createEncoderHeadsReporter,
35
+ NOOP_VC2B_REPORTER,
36
+ type EncoderHeadsReporter,
37
+ } from "./emit-vc2b.js";
38
+
39
+ /** The stable head index of a head name (its position in ENCODER_HEAD_ORDER). */
40
+ const HEAD_INDEX: Readonly<Record<EncoderHeadName, number>> = {
41
+ semantic: 0,
42
+ dependency: 1,
43
+ contradiction: 2,
44
+ cacheStability: 3,
45
+ payloadRouting: 4,
46
+ };
47
+
48
+ /** Deterministic 32-bit LCG step (matches runtime.ts projectSemantic). */
49
+ function nextState(state: number): number {
50
+ return (state * 1664525 + 1013904223) >>> 0;
51
+ }
52
+
53
+ export interface HeadProjectionOptions {
54
+ readonly seed?: number;
55
+ readonly reporter?: EncoderHeadsReporter;
56
+ }
57
+
58
+ /**
59
+ * L2-normalize a float vector in place semantics (returns a new Float32Array).
60
+ * A zero-norm (or empty) input maps to an all-zero vector of the same length
61
+ * (task 2: "mapping zero norm to an all-zero vector"). All finite.
62
+ */
63
+ export function l2Normalize(values: Float32Array): Float32Array {
64
+ const out = new Float32Array(values.length);
65
+ let sum = 0;
66
+ for (const v of values) sum += v * v;
67
+ const norm = Math.sqrt(sum);
68
+ if (!(norm > 0)) return out; // zero norm -> all-zero
69
+ for (let i = 0; i < values.length; i++) out[i] = values[i]! / norm;
70
+ return out;
71
+ }
72
+
73
+ /** L2 norm of a Float32Array (0 for empty/all-zero). */
74
+ export function l2Norm(values: Float32Array): number {
75
+ let sum = 0;
76
+ for (const v of values) sum += v * v;
77
+ return Math.sqrt(sum);
78
+ }
79
+
80
+ /** A deterministic per-head projection over the token sequence, pre-normalization. */
81
+ function projectRaw(head: EncoderHeadName, tokens: readonly number[], seed: number): Float32Array {
82
+ const dim = ENCODER_HEAD_DIMS[head];
83
+ const out = new Float32Array(dim);
84
+ // An EMPTY token sequence has no signal: the raw projection is the zero vector,
85
+ // so after L2 normalization it maps to the all-zero vector (task 2: "mapping
86
+ // zero norm to an all-zero vector"; ENC-ZERO-002). This keeps empty input
87
+ // finite and zero-norm instead of seeding spurious unit-norm noise.
88
+ if (tokens.length === 0) return out;
89
+ // Mix the stable head index + ENCODER_SEED + seed into a per-head state so
90
+ // each head is a distinct independent projection (failure-triad independence).
91
+ let state = (((ENCODER_SEED ^ HEAD_INDEX[head]) >>> 0) ^ (seed >>> 0)) ^ 0x9e3779b9;
92
+ for (const t of tokens) state = nextState(state ^ ((t >>> 0) * 2654435761));
93
+ for (let i = 0; i < dim; i++) {
94
+ state = nextState(state ^ seed);
95
+ out[i] = (state / 4294967296) * 2 - 1;
96
+ }
97
+ return out;
98
+ }
99
+
100
+ /**
101
+ * Compute one head's L2-normalized vector (all-zero on zero norm) for a token
102
+ * sequence. Deterministic for a given seed (repeat drift == 0).
103
+ */
104
+ export function projectHead(
105
+ head: EncoderHeadName,
106
+ tokens: readonly number[],
107
+ seed: number = ENCODER_SEED,
108
+ ): HeadVector {
109
+ const raw = projectRaw(head, tokens, seed);
110
+ return { head, dim: ENCODER_HEAD_DIMS[head], values: l2Normalize(raw) };
111
+ }
112
+
113
+ /**
114
+ * Encode a token sequence into a `VectorSetV1`: the five heads in stable order,
115
+ * each L2-normalized (all-zero on zero norm). Emits `heads_emitted` via the
116
+ * reporter (non-fatal, flag-gated). Deterministic for a given seed.
117
+ */
118
+ export function encodeVectorSet(
119
+ tokens: readonly number[],
120
+ options: HeadProjectionOptions = {},
121
+ ): VectorSetV1 {
122
+ const seed = options.seed ?? ENCODER_SEED;
123
+ const reporter = options.reporter ?? createEncoderHeadsReporter();
124
+ const heads: HeadVector[] = ENCODER_HEAD_ORDER.map((h) => projectHead(h, tokens, seed));
125
+ reporter.headsEmitted({
126
+ heads: heads.length,
127
+ dims: heads.map((h) => h.dim).join("/"),
128
+ normalized: true,
129
+ tokens: tokens.length,
130
+ });
131
+ return { schema: "vector-set-v1", inputTokens: [...tokens], heads, normalized: true };
132
+ }
133
+
134
+ /**
135
+ * The per-head loss weights (must sum to ENCODER_HEAD_LOSS_SUM exactly).
136
+ * Exposed for training/tests to assert the normative .35/.20/.20/.15/.10 split.
137
+ */
138
+ export function headLossWeights(): Readonly<Record<EncoderHeadName, number>> {
139
+ return { ...ENCODER_HEAD_LOSS_WEIGHTS };
140
+ }
141
+
142
+ export { ENCODER_HEAD_ORDER, ENCODER_HEAD_DIMS, ENCODER_HEAD_LOSS_SUM, ENCODER_SEED, NOOP_VC2B_REPORTER };
@@ -0,0 +1,123 @@
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
+
23
+ import { createHash } from "node:crypto";
24
+ import { l2Normalize } from "./heads.js";
25
+ import {
26
+ createEncoderHeadsReporter,
27
+ type EncoderHeadsReporter,
28
+ } from "./emit-vc2b.js";
29
+
30
+ /** Fixed output width of lexical C. */
31
+ export const ENCODER_LEXICAL_WIDTH = 256;
32
+
33
+ /** The documented limitation lexical C reports (continuity, not semantics). */
34
+ export const ENCODER_LEXICAL_LIMITATION =
35
+ "lexical C: token/phrase-level continuity only; old semantic context is omitted";
36
+
37
+ export function tokenizeLexical(text: string): string[] {
38
+ // Split into lowercase token/phrase units on non-alphanumeric boundaries.
39
+ return text.toLowerCase().split(/[^a-z0-9_]+/).filter((t) => t.length > 0);
40
+ }
41
+
42
+ /**
43
+ * Normalize a single token/phrase unit the same way `tokenizeLexical` does for
44
+ * the string form: lowercase and strip leading/trailing non-alphanumeric runs.
45
+ * Applied to array-form tokens so both accepted input forms of `embedLexical`
46
+ * hash identical conceptual content to identical buckets, regardless of how the
47
+ * caller chose to pass it (code-review Q03).
48
+ */
49
+ function normalizeToken(tok: string): string {
50
+ return tok.toLowerCase().split(/[^a-z0-9_]+/).join("");
51
+ }
52
+
53
+ /**
54
+ * Resolve either accepted input form to a normalized token sequence: the string
55
+ * form routes through `tokenizeLexical` (split on non-alphanumeric boundaries,
56
+ * fragmented tokens discarded); the array form applies the same per-token
57
+ * lowercase/strip normalization and drops tokens that normalize to empty. Both
58
+ * paths therefore agree on hash buckets for the same conceptual content.
59
+ */
60
+ function resolveTokens(tokensOrText: readonly string[] | string): string[] {
61
+ if (typeof tokensOrText === "string") return tokenizeLexical(tokensOrText);
62
+ const out: string[] = [];
63
+ for (const raw of tokensOrText) {
64
+ const norm = normalizeToken(raw);
65
+ if (norm.length > 0) out.push(norm);
66
+ }
67
+ return out;
68
+ }
69
+
70
+ /**
71
+ * Encode a token/phrase sequence into a 256-dim L2-normalized lexical vector
72
+ * (all-zero on empty input). Features: exact token count + token id-hash sums +
73
+ * phrase-adjacency hashes. Deterministic (repeat drift == 0). Pure local compute.
74
+ */
75
+ export function embedLexical(tokensOrText: readonly string[] | string): Float32Array {
76
+ const width = ENCODER_LEXICAL_WIDTH;
77
+ const out = new Float32Array(width);
78
+ const tokens = resolveTokens(tokensOrText);
79
+ for (let i = 0; i < tokens.length; i++) {
80
+ const tok = tokens[i]!;
81
+ const h = createHash("sha256").update(`t:${tok}`).digest();
82
+ const bucket = h.readUInt32BE(0) % width;
83
+ const weight = (h.readUInt32BE(4) / 4294967295) * 2 - 1;
84
+ out[bucket] += weight;
85
+ // Phrase adjacency: bigram hash blended in so semantic-free ordering matters.
86
+ if (i > 0) {
87
+ const pair = createHash("sha256").update(`p:${tokens[i - 1]}:${tok}`).digest();
88
+ const pb = pair.readUInt32BE(0) % width;
89
+ const pw = (pair.readUInt32BE(4) / 4294967295) * 2 - 1;
90
+ out[pb] += pw;
91
+ }
92
+ }
93
+ return l2Normalize(out);
94
+ }
95
+
96
+ /** The documented limitation string, surfaced when lexical C is selected. */
97
+ export function selectLexicalC(
98
+ options: { readonly reporter?: EncoderHeadsReporter } = {},
99
+ ): {
100
+ ok: true;
101
+ mode: "C";
102
+ dim: number;
103
+ width: number;
104
+ limitation: string;
105
+ } {
106
+ const reporter = options.reporter ?? createEncoderHeadsReporter();
107
+ const selection = {
108
+ ok: true as const,
109
+ mode: "C" as const,
110
+ dim: ENCODER_LEXICAL_WIDTH,
111
+ width: ENCODER_LEXICAL_WIDTH,
112
+ limitation: ENCODER_LEXICAL_LIMITATION,
113
+ };
114
+ reporter.fallbackSelected({
115
+ mode: selection.mode,
116
+ dim: selection.dim,
117
+ width: selection.width,
118
+ limitation: selection.limitation,
119
+ });
120
+ return selection;
121
+ }
122
+
123
+ export { l2Normalize };
@@ -0,0 +1,163 @@
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
+
28
+ import { createEncoderRuntime } from "./runtime.js";
29
+ import { encodeVectorSet, type HeadProjectionOptions } from "./heads.js";
30
+ import { embedTrigram512, selectTrigramBFallback } from "./trigram.js";
31
+ import { embedLexical, selectLexicalC } from "./lexical.js";
32
+ import { createEncoderHeadsReporter, type EncoderHeadsReporter } from "./emit-vc2b.js";
33
+ import {
34
+ ENC_FAIL,
35
+ type EncoderInput,
36
+ type EncoderLoadResult,
37
+ type EncoderRuntime,
38
+ type VectorSetV1,
39
+ } from "./types.js";
40
+
41
+ /** A resolved encode decision: a qualified mode-A VectorSet, an explicit B/C
42
+ * fallback vector, or a hard failure. */
43
+ export type RouterVerdict =
44
+ | { readonly ok: true; readonly mode: "A"; readonly vectorSet: VectorSetV1; readonly code: null }
45
+ | {
46
+ readonly ok: true;
47
+ readonly mode: "B" | "C";
48
+ readonly vector: Float32Array;
49
+ readonly width: number;
50
+ /** Semantic-context limitation (mode C) or null (mode B). */
51
+ readonly limitation: string | null;
52
+ /** The VC2A failure code that triggered the fallback (e.g.
53
+ * ENC_ASSET_UNREADABLE), or null when the caller forced the mode. */
54
+ readonly code: string | null;
55
+ }
56
+ | { readonly ok: false; readonly mode: "B" | "C"; readonly code: string };
57
+
58
+ export interface RouterOptions extends HeadProjectionOptions {
59
+ /** The VC2A runtime (mode A path). Defaults to a freshly created runtime. */
60
+ readonly runtime?: EncoderRuntime;
61
+ /** Forced fallback: skips the A load and selects the named VC2B fallback
62
+ * (used to exercise C when A and B are both disabled). Optional. */
63
+ readonly forceFallback?: "B" | "C";
64
+ }
65
+
66
+ /** Deterministic text derived from an int token sequence so the asset-free
67
+ * fallback producers operate on the same authority the learned path encoded. */
68
+ function textFromTokens(tokens: readonly number[]): string {
69
+ return tokens.join("-");
70
+ }
71
+
72
+ /**
73
+ * Try to produce an encoding for an input token sequence. Mode A: verify+load
74
+ * the local learned asset via the EncoderRuntime; on a real `load()` failure
75
+ * (removed model, digest mismatch, missing manifest, ...) the router catches it
76
+ * and selects the independently initialized trigram B or lexical C, emitting
77
+ * `vector_cortex_encoder_fallback_selected` (and `heads_emitted` when A wins).
78
+ */
79
+ export function encodeOrFallback(
80
+ input: EncoderInput,
81
+ assetDir: string,
82
+ options: RouterOptions = {},
83
+ ): RouterVerdict {
84
+ const reporter = options.reporter ?? createEncoderHeadsReporter();
85
+ const tokens = Array.isArray(input?.tokens) ? input.tokens : [];
86
+ const runtime = options.runtime ?? createEncoderRuntime();
87
+
88
+ // A caller that explicitly forces a fallback mode wins even over the empty-
89
+ // input degenerate case: the forced-mode contract must hold for ANY input, so
90
+ // handle forceFallback before the empty-input selection below (Q02). A forced
91
+ // B/C is NOT a rollback and NOT a demotion — it is an intentional selection
92
+ // for capacity/testing — so the verdict carries `code: null`, never
93
+ // ENC_FAIL.ROLLBACK, so a consumer that reads `verdict.code` as "what
94
+ // triggered the fallback" won't misread a deliberately forced mode as a
95
+ // rollback and take rollback-specific action (code-review Q04).
96
+ if (options.forceFallback !== undefined) {
97
+ if (options.forceFallback === "C") {
98
+ const sel = selectLexicalC({ reporter });
99
+ const vector = embedLexical(textFromTokens(tokens));
100
+ return { ok: true, mode: "C", vector, width: sel.width, limitation: sel.limitation, code: null };
101
+ }
102
+ const sel = selectTrigramBFallback({ reporter });
103
+ const vector = embedTrigram512(textFromTokens(tokens));
104
+ return { ok: true, mode: "B", vector, width: sel.width, limitation: null, code: null };
105
+ }
106
+
107
+ // Empty input yields the asset-free fallback (finite, deterministic zero
108
+ // vector, ENC-ZERO-002). This is legitimate degenerate behavior, NOT a shape
109
+ // failure, so the verdict carries no failure code (code === null) — a consumer
110
+ // that interprets `code` as "what went wrong" must not misread a valid all-zero
111
+ // B vector as a shape rejection (Q05). Only reached when no mode is forced, so
112
+ // a forced B/C is never subverted by empty tokens.
113
+ if (tokens.length === 0) {
114
+ const sel = selectTrigramBFallback({ reporter });
115
+ const vector = embedTrigram512("");
116
+ return { ok: true, mode: "B", vector, width: sel.width, limitation: null, code: null };
117
+ }
118
+
119
+ // Mode A attempt — reached only when no mode is forced (the forceFallback
120
+ // guard at the top already returned), so `options.forceFallback` is undefined here.
121
+ const loaded = runtime.load(assetDir);
122
+ if (!loaded.ok) {
123
+ return fallbackFromLoad(loaded, reporter, tokens);
124
+ }
125
+ // Q01/Q03: a qualified mode-A load is not enough — the verified per-manifest
126
+ // token capacity (maxTokens, <= global 512) must also be enforced before we
127
+ // produce a VectorSetV1. run inference over the input; an over-cap sequence
128
+ // (e.g. 100 tokens against a verified maxTokens=64 manifest) or an
129
+ // over-budget inference is rejected here and routed to the B/C fallback with
130
+ // the real failure code, rather than silently emitting an ok:true mode-A
131
+ // set whose inputTokens breach the model's declared capacity.
132
+ const inferred = runtime.infer({ tokens });
133
+ if (!inferred.ok) {
134
+ return fallbackFromLoad({ ok: false, mode: "B", code: inferred.code }, reporter, tokens);
135
+ }
136
+ const vectorSet = encodeVectorSet(tokens, { reporter, seed: options.seed });
137
+ return { ok: true, mode: "A", vectorSet, code: null };
138
+ }
139
+
140
+ /**
141
+ * Catch a (real or forced) A load failure (ok === false only) and select the
142
+ * B/C fallback that emits `vector_cortex_encoder_fallback_selected` from the
143
+ * production seam. The parameter is narrowed to the failed-load variant because
144
+ * the router hands off here only on a non-A/failed load — a qualified mode-A
145
+ * success never reaches this function (Q04).
146
+ */
147
+ function fallbackFromLoad(
148
+ loaded: Extract<EncoderLoadResult, { ok: false }>,
149
+ reporter: EncoderHeadsReporter,
150
+ tokens: readonly number[],
151
+ ): RouterVerdict {
152
+ if (loaded.mode === "C") {
153
+ const sel = selectLexicalC({ reporter });
154
+ const vector = embedLexical(textFromTokens(tokens));
155
+ return { ok: true, mode: "C", vector, width: sel.width, limitation: sel.limitation, code: loaded.code };
156
+ }
157
+ const sel = selectTrigramBFallback({ reporter });
158
+ const vector = embedTrigram512(textFromTokens(tokens));
159
+ return { ok: true, mode: "B", vector, width: sel.width, limitation: null, code: loaded.code };
160
+ }
161
+
162
+ export { ENC_FAIL };
163
+ export type { EncoderRuntime };