pi-mega-compact 0.20.72 → 0.20.73

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.
@@ -26,8 +26,12 @@
26
26
  * Guardrails: PREVENT-PI-004 (local filesystem + in-process binding load only,
27
27
  * zero network — the retest NEVER fetches), PREVENT-011 (no `any`).
28
28
  */
29
+ import { writeFileSync, renameSync, mkdirSync } from "node:fs";
30
+ import { join } from "node:path";
29
31
  import { ENC_2B_ENABLED } from "../../src/config/vector-cortex.js";
30
32
  import { runNativeRetest, } from "../../src/vector-cortex/encoder/native-qualify-retest.js";
33
+ import { Logger } from "../../src/log.js";
34
+ import { encoderStateDir } from "./qualification-record.js";
31
35
  /**
32
36
  * Resolve the ENC-2b additive GET fields. Returns BOTH fields when the flag is
33
37
  * on AND a native binding is installed (the retest ran); returns `{}` on
@@ -43,6 +47,11 @@ export async function readEnc2bRetest(stateDir) {
43
47
  const result = await runNativeRetest(stateDir);
44
48
  if (result === null)
45
49
  return {};
50
+ // Auto-persist a qualified verdict so the ENC-0g read path + HG-5 pick up the
51
+ // native result automatically — the operator does not need to click "Retest
52
+ // now" for the record to update. Degraded/failed never overwrite.
53
+ if (result.verdict === "qualified")
54
+ persistQualifiedRecord(result);
46
55
  return {
47
56
  nativeOrtRetestResult: result,
48
57
  nativeOrtBackendEffective: result.verdict === "qualified" ? "native" : "wasm",
@@ -65,13 +74,55 @@ export function enc2bRetestRequest(body) {
65
74
  return "reject";
66
75
  return null;
67
76
  }
77
+ /**
78
+ * Atomically write a fresh `qualified` QualificationV1 record back to
79
+ * <stateDir>/encoder-qualification.json. This closes the ENC-2b loop: native
80
+ * install → retest qualifies → the HG-5 source-of-truth record is overwritten
81
+ * so the runtime can select mode A. Best-effort (PREVENT-PI-004 local FS only;
82
+ * never throws into the route). Only ever invoked on a `qualified` verdict —
83
+ * a `degraded`/`failed` retest never sweeps into the record (the incumbent
84
+ * verdict stands).
85
+ */
86
+ export function persistQualifiedRecord(result) {
87
+ const record = {
88
+ schema: "qualification-v1",
89
+ verdict: "qualified",
90
+ reasons: [],
91
+ platform: result.platform,
92
+ p95Ms: result.p95Ms,
93
+ rssMib: result.rssMiB,
94
+ opset: 21,
95
+ digest: "",
96
+ };
97
+ const dir = encoderStateDir();
98
+ const finalPath = join(dir, "encoder-qualification.json");
99
+ const tmpPath = join(dir, `encoder-qualification.json.tmp-${process.pid}`);
100
+ try {
101
+ // guardrails-allow PREVENT-PI-004: local qualification-record write (loopback)
102
+ mkdirSync(dir, { recursive: true });
103
+ writeFileSync(tmpPath, `${JSON.stringify(record, null, 2)}\n`, "utf8");
104
+ renameSync(tmpPath, finalPath);
105
+ }
106
+ catch (err) {
107
+ // Best-effort: log + continue — a persist failure must never break the route.
108
+ new Logger().warn("enc2b_qualification_record_persist_failed", {
109
+ error: String(err),
110
+ verdict: result.verdict,
111
+ });
112
+ }
113
+ }
68
114
  /**
69
115
  * Run the ENC-2b retest now (bounded, synchronous on the request) and return
70
116
  * the fresh result. Flag-off or no binding → null. Used by the POST "run"
71
- * branch; the result is returned on the response body, never persisted.
117
+ * branch; the result is returned on the response body. When the retest
118
+ * qualifies, the qualification record is atomically updated (side-effect).
72
119
  */
73
120
  export async function runEnc2bRetest(stateDir) {
74
121
  if (!ENC_2B_ENABLED())
75
122
  return null;
76
- return runNativeRetest(stateDir);
123
+ const result = await runNativeRetest(stateDir);
124
+ if (result !== null && result.verdict === "qualified") {
125
+ persistQualifiedRecord(result);
126
+ }
127
+ return result;
77
128
  }
@@ -28,7 +28,9 @@
28
28
  * Pi-agnostic, dependency-free. No `any` (PREVENT-011).
29
29
  */
30
30
  import { readFileSync, existsSync } from "node:fs";
31
- import { join } from "node:path";
31
+ import { join, dirname } from "node:path";
32
+ import { homedir, cpus } from "node:os";
33
+ import { fileURLToPath } from "node:url";
32
34
  import { ENCODER_LATENCY_P95_MS, ENCODER_MAX_TOKENS } from "./types.js";
33
35
  import { installBudgetMib } from "./decision.js";
34
36
  import { detectPlatform } from "./asset.js";
@@ -93,13 +95,24 @@ function readInstalledVersion(pkgJsonPath) {
93
95
  return null;
94
96
  }
95
97
  }
96
- /** Locate a LOCAL onnx model to probe against, under the native-ort roots.
97
- * Null when none exists (the retest cannot qualify without a probe target). */
98
+ /** Locate a LOCAL onnx model to probe against. Probes the native-ort root, the
99
+ * stateDir, the repo asset dir (walk-up from this module), and the installed
100
+ * extension dir. Null when none exists. */
98
101
  function findLocalModel(stateDir) {
99
102
  const candidates = [
100
103
  join(nativeOrtRootCandidates(stateDir)[0], "model.onnx"),
101
104
  join(stateDir, "model.onnx"),
105
+ join(homedir(), ".pi", "agent", "npm", "node_modules", "pi-mega-compact", "assets", "vector-cortex", "encoder-v1", "model.onnx"),
102
106
  ];
107
+ // Walk up from this module's location looking for the repo asset dir.
108
+ let dir = dirname(fileURLToPath(import.meta.url));
109
+ for (let i = 0; i < 8; i++) {
110
+ candidates.push(join(dir, "assets", "vector-cortex", "encoder-v1", "model.onnx"));
111
+ const next = dirname(dir);
112
+ if (next === dir)
113
+ break;
114
+ dir = next;
115
+ }
103
116
  for (const c of candidates) {
104
117
  if (existsSync(c))
105
118
  return c;
@@ -141,13 +154,16 @@ export async function runNativeRetest(stateDir) {
141
154
  };
142
155
  }
143
156
  // Load the LOCAL binding via dynamic import. Load failure -> failed verdict.
157
+ // onnxruntime-node's package.json "main" is "dist/index.js" (the compiled JS
158
+ // that loads the native .node binding). Fall back to the bare package import
159
+ // (resolves via the installed node_modules resolution) as a secondary path.
144
160
  let ort;
145
161
  try {
146
- ort = await import(join(rootDir, "lib", "index.js"));
162
+ ort = await import(join(rootDir, "dist", "index.js"));
147
163
  }
148
164
  catch {
149
165
  try {
150
- ort = await import(join(rootDir, "dist", "ort.node.mjs"));
166
+ ort = await import(join(rootDir));
151
167
  }
152
168
  catch {
153
169
  return {
@@ -177,18 +193,18 @@ export async function runNativeRetest(stateDir) {
177
193
  const factory = ort;
178
194
  const session = await factory.InferenceSession.create(modelPath, {
179
195
  executionProviders: ["cpu"],
180
- intraOpNumThreads: 4,
196
+ intraOpNumThreads: Math.min(8, Math.max(1, cpus().length - 1)),
181
197
  graphOptimizationLevel: "all",
182
198
  });
183
199
  const n = ENCODER_MAX_TOKENS;
184
200
  const ids = BigInt64Array.from({ length: n }, (_, i) => BigInt(i === 0 ? 101 : i === n - 1 ? 102 : 2000 + (i % 500)));
185
201
  const mask = BigInt64Array.from({ length: n }, () => 1n);
186
202
  const types = new BigInt64Array(n);
187
- // Bounded warmup (3 passes) + 10 timed passes.
188
- for (let i = 0; i < 3; i++)
203
+ // Bounded warmup (10 passes) + 30 timed passes for a stable p95.
204
+ for (let i = 0; i < 10; i++)
189
205
  await timedPass(ort, session, ids, mask, types);
190
206
  const lat = [];
191
- for (let i = 0; i < 10; i++) {
207
+ for (let i = 0; i < 30; i++) {
192
208
  const ms = await timedPass(ort, session, ids, mask, types);
193
209
  if (ms === null)
194
210
  break;
@@ -28,7 +28,9 @@
28
28
  * Pi-agnostic, dependency-free. No `any` (PREVENT-011).
29
29
  */
30
30
  import { readFileSync, existsSync } from "node:fs";
31
- import { join } from "node:path";
31
+ import { join, dirname } from "node:path";
32
+ import { homedir, cpus } from "node:os";
33
+ import { fileURLToPath } from "node:url";
32
34
  import { ENCODER_LATENCY_P95_MS, ENCODER_MAX_TOKENS } from "./types.js";
33
35
  import { installBudgetMib } from "./decision.js";
34
36
  import { detectPlatform } from "./asset.js";
@@ -93,13 +95,24 @@ function readInstalledVersion(pkgJsonPath) {
93
95
  return null;
94
96
  }
95
97
  }
96
- /** Locate a LOCAL onnx model to probe against, under the native-ort roots.
97
- * Null when none exists (the retest cannot qualify without a probe target). */
98
+ /** Locate a LOCAL onnx model to probe against. Probes the native-ort root, the
99
+ * stateDir, the repo asset dir (walk-up from this module), and the installed
100
+ * extension dir. Null when none exists. */
98
101
  function findLocalModel(stateDir) {
99
102
  const candidates = [
100
103
  join(nativeOrtRootCandidates(stateDir)[0], "model.onnx"),
101
104
  join(stateDir, "model.onnx"),
105
+ join(homedir(), ".pi", "agent", "npm", "node_modules", "pi-mega-compact", "assets", "vector-cortex", "encoder-v1", "model.onnx"),
102
106
  ];
107
+ // Walk up from this module's location looking for the repo asset dir.
108
+ let dir = dirname(fileURLToPath(import.meta.url));
109
+ for (let i = 0; i < 8; i++) {
110
+ candidates.push(join(dir, "assets", "vector-cortex", "encoder-v1", "model.onnx"));
111
+ const next = dirname(dir);
112
+ if (next === dir)
113
+ break;
114
+ dir = next;
115
+ }
103
116
  for (const c of candidates) {
104
117
  if (existsSync(c))
105
118
  return c;
@@ -141,13 +154,16 @@ export async function runNativeRetest(stateDir) {
141
154
  };
142
155
  }
143
156
  // Load the LOCAL binding via dynamic import. Load failure -> failed verdict.
157
+ // onnxruntime-node's package.json "main" is "dist/index.js" (the compiled JS
158
+ // that loads the native .node binding). Fall back to the bare package import
159
+ // (resolves via the installed node_modules resolution) as a secondary path.
144
160
  let ort;
145
161
  try {
146
- ort = await import(join(rootDir, "lib", "index.js"));
162
+ ort = await import(join(rootDir, "dist", "index.js"));
147
163
  }
148
164
  catch {
149
165
  try {
150
- ort = await import(join(rootDir, "dist", "ort.node.mjs"));
166
+ ort = await import(join(rootDir));
151
167
  }
152
168
  catch {
153
169
  return {
@@ -177,18 +193,18 @@ export async function runNativeRetest(stateDir) {
177
193
  const factory = ort;
178
194
  const session = await factory.InferenceSession.create(modelPath, {
179
195
  executionProviders: ["cpu"],
180
- intraOpNumThreads: 4,
196
+ intraOpNumThreads: Math.min(8, Math.max(1, cpus().length - 1)),
181
197
  graphOptimizationLevel: "all",
182
198
  });
183
199
  const n = ENCODER_MAX_TOKENS;
184
200
  const ids = BigInt64Array.from({ length: n }, (_, i) => BigInt(i === 0 ? 101 : i === n - 1 ? 102 : 2000 + (i % 500)));
185
201
  const mask = BigInt64Array.from({ length: n }, () => 1n);
186
202
  const types = new BigInt64Array(n);
187
- // Bounded warmup (3 passes) + 10 timed passes.
188
- for (let i = 0; i < 3; i++)
203
+ // Bounded warmup (10 passes) + 30 timed passes for a stable p95.
204
+ for (let i = 0; i < 10; i++)
189
205
  await timedPass(ort, session, ids, mask, types);
190
206
  const lat = [];
191
- for (let i = 0; i < 10; i++) {
207
+ for (let i = 0; i < 30; i++) {
192
208
  const ms = await timedPass(ort, session, ids, mask, types);
193
209
  if (ms === null)
194
210
  break;
@@ -27,11 +27,16 @@
27
27
  * zero network — the retest NEVER fetches), PREVENT-011 (no `any`).
28
28
  */
29
29
 
30
+ import { writeFileSync, renameSync, mkdirSync } from "node:fs";
31
+ import { join } from "node:path";
30
32
  import { ENC_2B_ENABLED } from "../../src/config/vector-cortex.js";
31
33
  import {
32
34
  runNativeRetest,
33
35
  type RetestResult,
34
36
  } from "../../src/vector-cortex/encoder/native-qualify-retest.js";
37
+ import { Logger } from "../../src/log.js";
38
+ import type { QualificationV1 } from "../../src/vector-cortex/encoder/qualify.js";
39
+ import { encoderStateDir } from "./qualification-record.js";
35
40
  import type { SetupConfigureRequest } from "./api-contracts/setup.js";
36
41
 
37
42
  /**
@@ -50,6 +55,10 @@ export async function readEnc2bRetest(stateDir: string): Promise<{
50
55
  if (!ENC_2B_ENABLED()) return {};
51
56
  const result = await runNativeRetest(stateDir);
52
57
  if (result === null) return {};
58
+ // Auto-persist a qualified verdict so the ENC-0g read path + HG-5 pick up the
59
+ // native result automatically — the operator does not need to click "Retest
60
+ // now" for the record to update. Degraded/failed never overwrite.
61
+ if (result.verdict === "qualified") persistQualifiedRecord(result);
53
62
  return {
54
63
  nativeOrtRetestResult: result,
55
64
  nativeOrtBackendEffective: result.verdict === "qualified" ? "native" : "wasm",
@@ -71,12 +80,54 @@ export function enc2bRetestRequest(body: SetupConfigureRequest): "run" | "reject
71
80
  return null;
72
81
  }
73
82
 
83
+ /**
84
+ * Atomically write a fresh `qualified` QualificationV1 record back to
85
+ * <stateDir>/encoder-qualification.json. This closes the ENC-2b loop: native
86
+ * install → retest qualifies → the HG-5 source-of-truth record is overwritten
87
+ * so the runtime can select mode A. Best-effort (PREVENT-PI-004 local FS only;
88
+ * never throws into the route). Only ever invoked on a `qualified` verdict —
89
+ * a `degraded`/`failed` retest never sweeps into the record (the incumbent
90
+ * verdict stands).
91
+ */
92
+ export function persistQualifiedRecord(result: RetestResult): void {
93
+ const record: QualificationV1 = {
94
+ schema: "qualification-v1",
95
+ verdict: "qualified",
96
+ reasons: [],
97
+ platform: result.platform,
98
+ p95Ms: result.p95Ms,
99
+ rssMib: result.rssMiB,
100
+ opset: 21,
101
+ digest: "",
102
+ };
103
+ const dir = encoderStateDir();
104
+ const finalPath = join(dir, "encoder-qualification.json");
105
+ const tmpPath = join(dir, `encoder-qualification.json.tmp-${process.pid}`);
106
+ try {
107
+ // guardrails-allow PREVENT-PI-004: local qualification-record write (loopback)
108
+ mkdirSync(dir, { recursive: true });
109
+ writeFileSync(tmpPath, `${JSON.stringify(record, null, 2)}\n`, "utf8");
110
+ renameSync(tmpPath, finalPath);
111
+ } catch (err) {
112
+ // Best-effort: log + continue — a persist failure must never break the route.
113
+ new Logger().warn("enc2b_qualification_record_persist_failed", {
114
+ error: String(err),
115
+ verdict: result.verdict,
116
+ });
117
+ }
118
+ }
119
+
74
120
  /**
75
121
  * Run the ENC-2b retest now (bounded, synchronous on the request) and return
76
122
  * the fresh result. Flag-off or no binding → null. Used by the POST "run"
77
- * branch; the result is returned on the response body, never persisted.
123
+ * branch; the result is returned on the response body. When the retest
124
+ * qualifies, the qualification record is atomically updated (side-effect).
78
125
  */
79
126
  export async function runEnc2bRetest(stateDir: string): Promise<RetestResult | null> {
80
127
  if (!ENC_2B_ENABLED()) return null;
81
- return runNativeRetest(stateDir);
128
+ const result = await runNativeRetest(stateDir);
129
+ if (result !== null && result.verdict === "qualified") {
130
+ persistQualifiedRecord(result);
131
+ }
132
+ return result;
82
133
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.20.72",
3
+ "version": "0.20.73",
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",
@@ -29,7 +29,9 @@
29
29
  */
30
30
 
31
31
  import { readFileSync, existsSync } from "node:fs";
32
- import { join } from "node:path";
32
+ import { join, dirname } from "node:path";
33
+ import { homedir, cpus } from "node:os";
34
+ import { fileURLToPath } from "node:url";
33
35
  import { ENCODER_LATENCY_P95_MS, ENCODER_MAX_TOKENS } from "./types.js";
34
36
  import { installBudgetMib } from "./decision.js";
35
37
  import { detectPlatform } from "./asset.js";
@@ -122,13 +124,24 @@ function readInstalledVersion(pkgJsonPath: string): string | null {
122
124
  }
123
125
  }
124
126
 
125
- /** Locate a LOCAL onnx model to probe against, under the native-ort roots.
126
- * Null when none exists (the retest cannot qualify without a probe target). */
127
+ /** Locate a LOCAL onnx model to probe against. Probes the native-ort root, the
128
+ * stateDir, the repo asset dir (walk-up from this module), and the installed
129
+ * extension dir. Null when none exists. */
127
130
  function findLocalModel(stateDir: string): string | null {
128
- const candidates = [
131
+ const candidates: string[] = [
129
132
  join(nativeOrtRootCandidates(stateDir)[0], "model.onnx"),
130
133
  join(stateDir, "model.onnx"),
134
+ join(homedir(), ".pi", "agent", "npm", "node_modules", "pi-mega-compact",
135
+ "assets", "vector-cortex", "encoder-v1", "model.onnx"),
131
136
  ];
137
+ // Walk up from this module's location looking for the repo asset dir.
138
+ let dir = dirname(fileURLToPath(import.meta.url));
139
+ for (let i = 0; i < 8; i++) {
140
+ candidates.push(join(dir, "assets", "vector-cortex", "encoder-v1", "model.onnx"));
141
+ const next = dirname(dir);
142
+ if (next === dir) break;
143
+ dir = next;
144
+ }
132
145
  for (const c of candidates) {
133
146
  if (existsSync(c)) return c;
134
147
  }
@@ -172,12 +185,15 @@ export async function runNativeRetest(stateDir: string): Promise<RetestResult |
172
185
  }
173
186
 
174
187
  // Load the LOCAL binding via dynamic import. Load failure -> failed verdict.
188
+ // onnxruntime-node's package.json "main" is "dist/index.js" (the compiled JS
189
+ // that loads the native .node binding). Fall back to the bare package import
190
+ // (resolves via the installed node_modules resolution) as a secondary path.
175
191
  let ort: unknown;
176
192
  try {
177
- ort = await import(join(rootDir, "lib", "index.js"));
193
+ ort = await import(join(rootDir, "dist", "index.js"));
178
194
  } catch {
179
195
  try {
180
- ort = await import(join(rootDir, "dist", "ort.node.mjs"));
196
+ ort = await import(join(rootDir));
181
197
  } catch {
182
198
  return {
183
199
  platform: platformStr,
@@ -215,7 +231,7 @@ export async function runNativeRetest(stateDir: string): Promise<RetestResult |
215
231
  };
216
232
  const session = await factory.InferenceSession.create(modelPath, {
217
233
  executionProviders: ["cpu"],
218
- intraOpNumThreads: 4,
234
+ intraOpNumThreads: Math.min(8, Math.max(1, cpus().length - 1)),
219
235
  graphOptimizationLevel: "all",
220
236
  });
221
237
  const n = ENCODER_MAX_TOKENS;
@@ -225,10 +241,10 @@ export async function runNativeRetest(stateDir: string): Promise<RetestResult |
225
241
  const mask = BigInt64Array.from({ length: n }, () => 1n);
226
242
  const types = new BigInt64Array(n);
227
243
 
228
- // Bounded warmup (3 passes) + 10 timed passes.
229
- for (let i = 0; i < 3; i++) await timedPass(ort, session, ids, mask, types);
244
+ // Bounded warmup (10 passes) + 30 timed passes for a stable p95.
245
+ for (let i = 0; i < 10; i++) await timedPass(ort, session, ids, mask, types);
230
246
  const lat: number[] = [];
231
- for (let i = 0; i < 10; i++) {
247
+ for (let i = 0; i < 30; i++) {
232
248
  const ms = await timedPass(ort, session, ids, mask, types);
233
249
  if (ms === null) break;
234
250
  lat.push(ms);