Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.

Commit aff3f01

Browse files
fix: release-audit batch — node:sqlite CI coverage, embedding-runtime doctor parity
Cleared the source-verified findings from the two blind release councils (0 P0 either; these are the P1/P2 hardening items). cortexkit#2 — bind-style CI guard scope: sqlite-bind-style.test.ts scanned only plugin/src, so an array-form bind (cortexkit#151 class: works on bun:sqlite, throws "Unknown named parameter '0'" on node:sqlite) written in pi-plugin/src or cli/src would ship undetected. Now scans all three package srcs (zero existing violations). pi-plugin and cli have no sqlite.ts of their own — they run on the shared chokepoint — so a bind there is just as dangerous. cortexkit#3 — node:sqlite smoke in CI: bun test only exercises the bun:sqlite branch, so the transaction() shim, readonly→readOnly mapping, and the cortexkit#151 array-bind normalization shipped unverified under real node:sqlite (Pi / Desktop). Wired scripts/smoke-node-sqlite.ts into ci.yml (check-plugin) and release.yml (test) behind a Node 24 setup; it exits non-zero on failure so it gates. cortexkit#4 — Pi doctor embedding-runtime parity (cortexkit#128): doctor-opencode detects a broken local onnxruntime-node, doctor-pi didn't. Added a LAYOUT-AGNOSTIC checkLocalEmbeddingRuntimeByResolution that asks Node's resolver from the installed plugin dir (direct, then through @huggingface/transformers for pnpm strict) instead of guessing a path — verified empirically that Pi's real layout varies (local dev-path bun workspace vs npm-hoisted user/project install). Stays "unknown"/silent when it can't introspect, never false-alarms. Caught a Bun-vs-Node divergence under test: Bun's resolver throws a ResolveMessage that is NOT instanceof Error, so reading .code defensively (not gated on instanceof) is required — the doctor runs under Node (npx) but tests under Bun, so both paths are now covered. cortexkit#6 — OpenCode doctor no-config path: local is the DEFAULT provider, so a missing config still means local embeddings; it now runs the runtime check there too via a shared checkLocalEmbeddingRuntimeForDoctor helper (was an early return). cortexkit#7 — ERR_DLOPEN_FAILED latch: onnxruntime-node installed but its native binary fails to load (Windows missing VC++ runtime) is environmental+permanent, same class as missing-package — isNativeRuntimeMissingError now latches it (scoped to onnxruntime so unrelated native-module failures aren't masked). cortexkit#1 (subagent Channel-2) reviewed and intentionally left as-is: the one-shot lease is consumed at a mid-work tool-calls boundary (promptAsync coalesces into the live loop); worst case is a ~100-token revival in the tiny pre-stop window. Gate: plugin 2086/0, Pi 469/0, CLI 165/0; tsc + biome clean all three. Empirically dry-run the live Pi doctor (openai-compatible config → local branch correctly silent; resolution chain proven to find the binary on disk). Co-authored-by: Alfonso [Magic Context] <[email protected]>
1 parent 1722c09 commit aff3f01

9 files changed

Lines changed: 370 additions & 45 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ jobs:
3838
with:
3939
bun-version: latest
4040

41+
# node:sqlite (the Pi / OpenCode-Desktop SQLite backend) is only reachable
42+
# under Node, not Bun. Node 24 also strips the smoke script's inline TS.
43+
- uses: actions/setup-node@v4
44+
with:
45+
node-version: "24"
46+
4147
- name: Install dependencies
4248
run: bun install
4349

@@ -53,6 +59,13 @@ jobs:
5359
- name: Test
5460
run: bun run test
5561

62+
# Exercise the node:sqlite branch of shared/sqlite.ts under REAL Node —
63+
# bun test only covers the bun:sqlite branch, so the transaction() shim,
64+
# readonly→readOnly mapping, and the array-bind normalization (#151) would
65+
# otherwise ship unverified on Pi/Desktop.
66+
- name: Smoke (node:sqlite backend)
67+
run: node packages/plugin/scripts/smoke-node-sqlite.ts
68+
5669
check-pi-plugin:
5770
name: Check (pi-plugin)
5871
runs-on: ubuntu-latest

‎.github/workflows/release.yml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ jobs:
5757
with:
5858
bun-version: latest
5959

60+
# node:sqlite (Pi / OpenCode-Desktop backend) is only reachable under
61+
# Node; Node 24 also strips the smoke script's inline TS.
62+
- uses: actions/setup-node@v4
63+
with:
64+
node-version: "24"
65+
6066
- name: Install dependencies
6167
run: bun install
6268

@@ -72,6 +78,11 @@ jobs:
7278
- name: Test
7379
run: bun run test
7480

81+
# Verify the node:sqlite branch (transaction shim, readonly mapping,
82+
# #151 array-bind normalization) under real Node before publishing.
83+
- name: Smoke (node:sqlite backend)
84+
run: node packages/plugin/scripts/smoke-node-sqlite.ts
85+
7586
test-pi:
7687
name: Test (pi-plugin)
7788
runs-on: ubuntu-latest

‎packages/cli/src/commands/doctor-opencode.ts‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,40 @@ async function runIssueFlow(): Promise<number> {
358358
* - Provider that accepts the URL shape but doesn't implement embeddings
359359
* (OpenRouter's /v1 for example) — same detection path.
360360
*/
361+
// Local embeddings need the native ONNX runtime (onnxruntime-node). On Windows
362+
// it sometimes fails to install (its native binary download is interrupted), and
363+
// the plugin's static `import "onnxruntime-node"` then throws on every embedding
364+
// (#128). Surface it here with the fix instead of leaving users with the cryptic
365+
// resolver error in the log. Shared by the explicit-`local` branch AND the
366+
// no-config / default-provider path (local is the default, so a missing config
367+
// still means local embeddings).
368+
function checkLocalEmbeddingRuntimeForDoctor(): { issues: number; localRuntimeBroken?: boolean } {
369+
const runtime = checkLocalEmbeddingRuntime([
370+
join(getOpenCodeCacheDir(), "packages", PLUGIN_ENTRY_WITH_VERSION),
371+
join(getOpenCodeCacheDir(), "packages", PLUGIN_NAME),
372+
]);
373+
if (runtime.state === "package-missing" || runtime.state === "binary-missing") {
374+
log.warn(
375+
"Embedding provider: local — but the native runtime (onnxruntime-node) " +
376+
`is ${runtime.state === "package-missing" ? "not installed" : "missing its platform binary"}. ` +
377+
"Local embeddings won't work. Fix: re-run with `doctor --force` to reinstall " +
378+
"the plugin, or set `embedding.provider` to `openai-compatible` (LM Studio / " +
379+
"Ollama) or `off`. Existing memories are unaffected.",
380+
);
381+
return { issues: 1, localRuntimeBroken: true };
382+
}
383+
log.success("Embedding provider: local (Xenova/all-MiniLM-L6-v2 bundled)");
384+
return { issues: 0 };
385+
}
386+
361387
async function checkEmbeddingConfig(
362388
magicContextConfigPath: string,
363389
): Promise<{ issues: number; localRuntimeBroken?: boolean }> {
364390
if (!existsSync(magicContextConfigPath)) {
365-
// No config → local provider defaults apply, nothing to check.
366-
return { issues: 0 };
391+
// No config → local provider defaults apply. Still verify the local
392+
// runtime: local is the DEFAULT, so "no config" means local embeddings,
393+
// and a broken onnxruntime-node would silently fail (#128/#6).
394+
return checkLocalEmbeddingRuntimeForDoctor();
367395
}
368396

369397
let rawText: string;
@@ -401,27 +429,7 @@ async function checkEmbeddingConfig(
401429
}
402430

403431
if (provider === undefined || provider === "local") {
404-
// Local embeddings need the native ONNX runtime (onnxruntime-node). On
405-
// Windows it sometimes fails to install (its native binary download is
406-
// interrupted), and the plugin's static `import "onnxruntime-node"` then
407-
// throws on every embedding (#128). Surface it here with the fix instead
408-
// of leaving users with the cryptic resolver error in the log.
409-
const runtime = checkLocalEmbeddingRuntime([
410-
join(getOpenCodeCacheDir(), "packages", PLUGIN_ENTRY_WITH_VERSION),
411-
join(getOpenCodeCacheDir(), "packages", PLUGIN_NAME),
412-
]);
413-
if (runtime.state === "package-missing" || runtime.state === "binary-missing") {
414-
log.warn(
415-
"Embedding provider: local — but the native runtime (onnxruntime-node) " +
416-
`is ${runtime.state === "package-missing" ? "not installed" : "missing its platform binary"}. ` +
417-
"Local embeddings won't work. Fix: re-run with `doctor --force` to reinstall " +
418-
"the plugin, or set `embedding.provider` to `openai-compatible` (LM Studio / " +
419-
"Ollama) or `off`. Existing memories are unaffected.",
420-
);
421-
return { issues: 1, localRuntimeBroken: true };
422-
}
423-
log.success("Embedding provider: local (Xenova/all-MiniLM-L6-v2 bundled)");
424-
return { issues: 0 };
432+
return checkLocalEmbeddingRuntimeForDoctor();
425433
}
426434

427435
if (provider !== "openai-compatible") {

‎packages/cli/src/commands/doctor-pi.ts‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "node:fs";
1111
import { createRequire } from "node:module";
1212
import { homedir } from "node:os";
13-
import { dirname, join } from "node:path";
13+
import { dirname, isAbsolute, join } from "node:path";
1414

1515
import { MagicContextConfigSchema } from "@magic-context/core/config/schema/magic-context";
1616
import { substituteConfigVariables } from "@magic-context/core/config/variable";
@@ -28,6 +28,7 @@ import { getMagicContextStorageDir } from "@magic-context/core/shared/data-path"
2828
import { loadPiConfig } from "@magic-context/pi-core/config";
2929
import { parse as parseJsonc, stringify as stringifyJsonc } from "comment-json";
3030
import { collectDiagnostics } from "../lib/diagnostics-pi";
31+
import { checkLocalEmbeddingRuntimeByResolution } from "../lib/embedding-runtime";
3132
import { bundleIssueReport } from "../lib/logs-pi";
3233
import {
3334
getMagicContextLogPath,
@@ -230,6 +231,35 @@ function packagesFrom(settings: Record<string, unknown>): unknown[] {
230231
return Array.isArray(settings.packages) ? settings.packages : [];
231232
}
232233

234+
/**
235+
* Candidate installed-plugin directories to point the local-embedding-runtime
236+
* resolver at. Pi's layout varies (verified on disk): a LOCAL DEV-PATH entry in
237+
* packages[] (e.g. "../../repo/packages/pi-plugin", relative to the agent dir),
238+
* OR a managed npm install at ~/.pi/agent/npm/node_modules/<pkg> (user) /
239+
* <cwd>/.pi/npm/node_modules/<pkg> (project). We collect every plausible dir
240+
* with a package.json; the resolver stays SILENT for any that don't exist.
241+
*/
242+
function piPluginDirCandidates(packages: unknown[], cwd: string): string[] {
243+
const dirs: string[] = [];
244+
const agentDir = getPiAgentConfigDir();
245+
246+
// Local dev-path entries: a string spec that is NOT an npm: specifier and
247+
// resolves to a directory on disk. Relative entries are resolved against the
248+
// Pi agent dir (Pi's settings.packages base).
249+
for (const entry of packages) {
250+
const spec = typeof entry === "string" ? entry.trim() : "";
251+
if (!spec || spec.startsWith("npm:")) continue;
252+
const resolved = isAbsolute(spec) ? spec : join(agentDir, spec);
253+
dirs.push(resolved);
254+
}
255+
256+
// Managed npm install roots (hoisted): <root>/node_modules/<pkg>.
257+
dirs.push(join(agentDir, "npm", "node_modules", PACKAGE_NAME));
258+
dirs.push(join(cwd, ".pi", "npm", "node_modules", PACKAGE_NAME));
259+
260+
return dirs.filter((dir) => existsSync(join(dir, "package.json")));
261+
}
262+
233263
function projectConfigPath(cwd: string): string {
234264
return join(cwd, ".pi", "magic-context.jsonc");
235265
}
@@ -602,7 +632,42 @@ async function runHealthChecks(options: {
602632
} else if (loadedConfig.config.embedding.provider === "off") {
603633
add(results, "info", "Embedding provider disabled");
604634
} else {
605-
add(results, "pass", `Embedding provider: ${loadedConfig.config.embedding.provider}`);
635+
// Local (default) provider: verify the native ONNX runtime
636+
// (onnxruntime-node) actually resolves + has its platform binary. On
637+
// Windows it sometimes fails to install and the plugin's static import
638+
// throws on every embedding (#128). Layout-agnostic resolution from the
639+
// installed plugin dir; stays silent if no plugin dir can be inspected.
640+
let runtimeReported = false;
641+
for (const pluginDir of piPluginDirCandidates(packages, options.cwd)) {
642+
const runtime = checkLocalEmbeddingRuntimeByResolution(pluginDir);
643+
if (runtime.state === "ok") {
644+
add(
645+
results,
646+
"pass",
647+
`Embedding provider: ${loadedConfig.config.embedding.provider} (native runtime present)`,
648+
);
649+
runtimeReported = true;
650+
break;
651+
}
652+
if (runtime.state === "package-missing" || runtime.state === "binary-missing") {
653+
add(
654+
results,
655+
"warn",
656+
`Embedding provider: local — native runtime (onnxruntime-node) is ${
657+
runtime.state === "package-missing"
658+
? "not installed"
659+
: "missing its platform binary"
660+
}. Local embeddings won't work. Fix: reinstall the plugin, or set ` +
661+
"`embedding.provider` to `openai-compatible` (LM Studio / Ollama) or `off`. " +
662+
"Existing memories are unaffected.",
663+
);
664+
runtimeReported = true;
665+
break;
666+
}
667+
}
668+
if (!runtimeReported) {
669+
add(results, "pass", `Embedding provider: ${loadedConfig.config.embedding.provider}`);
670+
}
606671
}
607672

608673
// Conflict detection — Pi doesn't have known competing context-management

‎packages/cli/src/lib/embedding-runtime.test.ts‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { describe, expect, test } from "bun:test";
22
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
5-
import { checkLocalEmbeddingRuntime, checkLocalEmbeddingRuntimeAt } from "./embedding-runtime";
5+
import {
6+
checkLocalEmbeddingRuntime,
7+
checkLocalEmbeddingRuntimeAt,
8+
checkLocalEmbeddingRuntimeByResolution,
9+
} from "./embedding-runtime";
610

711
function makeRoot(): string {
812
return mkdtempSync(join(tmpdir(), "mc-embruntime-"));
@@ -101,3 +105,85 @@ describe("checkLocalEmbeddingRuntime (multi-root)", () => {
101105
}
102106
});
103107
});
108+
109+
// Build a plugin tree where `require.resolve("onnxruntime-node")` actually
110+
// succeeds from the plugin dir — mirrors the real on-disk dev-path / hoisted
111+
// layout (a nested node_modules the package manager populated), which a
112+
// hardcoded path check would get wrong across layouts.
113+
function installResolvablePlugin(withPackage: boolean, withBinary: boolean): string {
114+
const pluginDir = mkdtempSync(join(tmpdir(), "mc-pi-plugin-"));
115+
writeFileSync(
116+
join(pluginDir, "package.json"),
117+
JSON.stringify({ name: "@cortexkit/pi-magic-context", version: "0.0.0" }),
118+
);
119+
if (withPackage) {
120+
const pkgDir = join(pluginDir, "node_modules", "onnxruntime-node");
121+
mkdirSync(pkgDir, { recursive: true });
122+
writeFileSync(
123+
join(pkgDir, "package.json"),
124+
JSON.stringify({ name: "onnxruntime-node", main: "index.js" }),
125+
);
126+
writeFileSync(join(pkgDir, "index.js"), "module.exports = {};");
127+
if (withBinary) {
128+
const binDir = join(pkgDir, "bin", "napi-v6", "win32", "x64");
129+
mkdirSync(binDir, { recursive: true });
130+
writeFileSync(join(binDir, "onnxruntime_binding.node"), "stub");
131+
}
132+
}
133+
return pluginDir;
134+
}
135+
136+
describe("checkLocalEmbeddingRuntimeByResolution", () => {
137+
test("resolvable package + matching binary → ok (dev-path/hoisted layout)", () => {
138+
const dir = installResolvablePlugin(true, true);
139+
try {
140+
expect(checkLocalEmbeddingRuntimeByResolution(dir, "win32", "x64").state).toBe("ok");
141+
} finally {
142+
rmSync(dir, { recursive: true, force: true });
143+
}
144+
});
145+
146+
test("resolvable package but missing platform binary → binary-missing", () => {
147+
const dir = installResolvablePlugin(true, false);
148+
try {
149+
expect(checkLocalEmbeddingRuntimeByResolution(dir, "win32", "x64").state).toBe(
150+
"binary-missing",
151+
);
152+
} finally {
153+
rmSync(dir, { recursive: true, force: true });
154+
}
155+
});
156+
157+
test("plugin exists but onnxruntime-node not resolvable → package-missing (#128)", () => {
158+
const dir = installResolvablePlugin(false, false);
159+
try {
160+
expect(checkLocalEmbeddingRuntimeByResolution(dir, "win32", "x64").state).toBe(
161+
"package-missing",
162+
);
163+
} finally {
164+
rmSync(dir, { recursive: true, force: true });
165+
}
166+
});
167+
168+
test("plugin dir does not exist → unknown (stay silent, never false-alarm)", () => {
169+
expect(
170+
checkLocalEmbeddingRuntimeByResolution(
171+
join(tmpdir(), "mc-pi-plugin-nonexistent-xyz"),
172+
"win32",
173+
"x64",
174+
).state,
175+
).toBe("unknown");
176+
});
177+
178+
test("unknown platform/arch with package resolvable → ok (don't guess a binary)", () => {
179+
const dir = installResolvablePlugin(true, false);
180+
try {
181+
expect(
182+
checkLocalEmbeddingRuntimeByResolution(dir, "freebsd" as NodeJS.Platform, "ppc64")
183+
.state,
184+
).toBe("ok");
185+
} finally {
186+
rmSync(dir, { recursive: true, force: true });
187+
}
188+
});
189+
});

0 commit comments

Comments
 (0)