pi-mega-compact 0.18.0 → 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.
@@ -270,6 +270,8 @@ export const SETTINGS: ReadonlyArray<{
270
270
  str("MEGACOMPACT_RAPTOR_MODEL", "RAPTOR Summary Model", "Ollama model for cluster summarization (empty = extractive)", ""),
271
271
  str("MEGACOMPACT_RAPTOR_URL", "RAPTOR Ollama URL", "Ollama endpoint for RAPTOR summarization", "http://127.0.0.1:11434"),
272
272
  num("MEGACOMPACT_EMBED_CACHE", "Embed Cache Size", "Embedding cache entries (0 = disabled)", 256, 0, 10000),
273
+ num("MEGACOMPACT_EMBEDDING_BATCH_TOKENS", "Embedding Batch Tokens", "Oversized-prompt chunking limit (tokens) for the BYO localhost embedder; text above this is chunked + mean-pooled", 2048, 64, 8192),
274
+ num("MEGACOMPACT_EMBEDDING_CHARS_PER_TOKEN", "Embedding Chars per Token", "Estimated characters per token used for embedder chunking size", 4, 1, 32),
273
275
  ],
274
276
  },
275
277
  {
@@ -317,6 +319,12 @@ export const SETTINGS: ReadonlyArray<{
317
319
  "ModelManifestV1 digest-before-load ONNX runtime (opset17/batch1/max512) + asset-free trigram demotion. Asset path assets/vector-cortex/encoder-v1 is immutable/digest-pinned. OFF = mode C, byte-identical to predecessor.",
318
320
  true,
319
321
  ),
322
+ boolDirect(
323
+ "MEGACOMPACT_VC2B",
324
+ "VC2B Multi-Head Encoder",
325
+ "VectorSetV1 five L2-normalized heads (384/128/128/64/32) with head-calibration draft + asset-free trigram B (512d) and lexical C fallbacks, plus the per-head emit seam. OFF = mode C, no per-head vectors emitted, byte-identical predecessor.",
326
+ true,
327
+ ),
320
328
  ],
321
329
  },
322
330
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.18.0",
3
+ "version": "0.18.1",
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",
@@ -80,6 +80,16 @@ export const VC1C_ENABLED = (): boolean => sprintFlag("MEGACOMPACT_VC1C");
80
80
  */
81
81
  export const VC2A_ENABLED = (): boolean => sprintFlag("MEGACOMPACT_VC2A");
82
82
 
83
+ /**
84
+ * VC2B — multi-head encoder (VectorSetV1 / HeadCalibrationDraft).
85
+ * Default ON. `MEGACOMPACT_VC2B=0` disables and is byte-identical to the
86
+ * predecessor (the encoder emits no per-head vectors and no fallback-selected
87
+ * event; the trigram/lexical paths themselves are unchanged and are the
88
+ * predecessor's mode-B/C producers). The real consumers are the encoder-heads
89
+ * emit seam and the multi-head encoder producers (heads/trigram/lexical).
90
+ */
91
+ export const VC2B_ENABLED = (): boolean => sprintFlag("MEGACOMPACT_VC2B");
92
+
83
93
  // ---------------------------------------------------------------------------
84
94
  // Breaker state machine constants (TRIAD_RESILIENCE.md §breaker).
85
95
  // Rolled numbers for one 60s window; VC0C consumes these at its breaker seam.
package/src/config.ts CHANGED
@@ -158,6 +158,7 @@ export {
158
158
  VC1B_ENABLED,
159
159
  VC1C_ENABLED,
160
160
  VC2A_ENABLED,
161
+ VC2B_ENABLED,
161
162
  BREAKER_WINDOW_MS,
162
163
  BREAKER_MIN_ATTEMPTS,
163
164
  BREAKER_PERF_FAILURES,
@@ -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,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 };
@@ -0,0 +1,85 @@
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
+
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 trigram B (VC2B task 4). */
31
+ export const ENCODER_TRIGRAM_WIDTH = 512;
32
+
33
+ /** Tokenize a phrase into byte-level trigrams (3-byte sliding windows). For a
34
+ * short phrase with fewer than 3 bytes we still emit the available shingles. */
35
+ function trigrams(text: string): string[] {
36
+ const bytes = Buffer.from(text, "utf8");
37
+ if (bytes.length === 0) return [];
38
+ const out: string[] = [];
39
+ const n = bytes.length;
40
+ // `Math.max(1, n - 2)` already emits a single whole-string shingle for 1- and
41
+ // 2-byte phrases (slice(0,3) covers the whole buffer), so there is NO separate
42
+ // short-phrase block — adding one would hash the same shingle twice (Q05).
43
+ for (let i = 0; i < Math.max(1, n - 2); i++) {
44
+ const chunk = bytes.slice(i, i + 3);
45
+ out.push(chunk.toString("hex"));
46
+ }
47
+ return out;
48
+ }
49
+
50
+ /**
51
+ * Encode a phrase into a 512-dim L2-normalized trigram vector (all-zero on
52
+ * empty input). Deterministic: the same text always yields the same vector
53
+ * (repeat drift == 0) — no asset, no calibration, no network.
54
+ */
55
+ export function embedTrigram512(text: string): Float32Array {
56
+ const width = ENCODER_TRIGRAM_WIDTH;
57
+ const out = new Float32Array(width);
58
+ // Feistel-style double hashing of each trigram into a bucket index + weight.
59
+ for (const tg of trigrams(text)) {
60
+ const h1 = createHash("sha256").update(tg).digest();
61
+ const bucket = h1.readUInt32BE(0) % width;
62
+ const weight = (h1.readUInt32BE(4) / 4294967295) * 2 - 1;
63
+ out[bucket] += weight;
64
+ }
65
+ return l2Normalize(out);
66
+ }
67
+
68
+ /**
69
+ * The 512-dim vector is produced even when the learned asset is absent: this is
70
+ * the mode-B selection point. Returns `{ ok: true, dim, width }` always — there
71
+ * is no asset to consult (task 4 + ENC-FALLBACK-003). Selecting mode B also
72
+ * emits `vector_cortex_encoder_fallback_selected` via the flag-gated reporter
73
+ * (task 5) — the production seam that makes the fallback event live in the
74
+ * runtime, not dead test-only wiring.
75
+ */
76
+ export function selectTrigramBFallback(
77
+ options: { readonly reporter?: EncoderHeadsReporter } = {},
78
+ ): { ok: true; dim: number; width: number; mode: "B" } {
79
+ const reporter = options.reporter ?? createEncoderHeadsReporter();
80
+ const selection = { ok: true as const, mode: "B" as const, dim: ENCODER_TRIGRAM_WIDTH, width: ENCODER_TRIGRAM_WIDTH };
81
+ reporter.fallbackSelected({ mode: selection.mode, dim: selection.dim, width: selection.width });
82
+ return selection;
83
+ }
84
+
85
+ export { l2Normalize };