From 2e5d3482b63eb42a706dc34129b2bec8200b0a1b Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 12 Oct 2026 10:27:24 +0800 Subject: [PATCH] fix: probe the real pi npm copy dir and accept versioned specs in lane presence (#2850) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit inspectLanePresence("pi") probed /agent/npm/node_modules/... — an extra "agent" segment stacked on top of resolvePiHome()'s own ~/.pi/agent default — so a real pi-managed copy never resolved: `bili plugin list` showed "unknown" and doctor kept targets empty (false "materializes on next startup" hint, no stale verdict). Versioned npm specs (npm:billion-context@pinned / @latest) were additionally misrouted into the local-path branch by the exact-string PI_NPM_ENTRY match. - extract piNpmCopyRoot() in pi-channel.ts as the single source of truth for the materialization dir (now shared with isPiNpmCopy) - inspectLanePresence: match any npm:billion-context spec form, probe the real dir - updateLane(pi): route versioned specs to the pi-managed message instead of "entry points at this checkout" - regression tests: decoy copy at the old wrong path, all three spec forms, default/PI_HOME/PI_CODING_AGENT_DIR layouts, missing-copy degradation, corrupt settings probe failure, absolute-path local-path semantics, doctor verdicts --- src/pi-channel.ts | 12 +++- src/plugin-install.ts | 13 +++- tests/doctor.test.ts | 101 ++++++++++++++++++++++++++++++ tests/plugin-list-version.test.ts | 65 +++++++++++++++++++ 4 files changed, 187 insertions(+), 4 deletions(-) diff --git a/src/pi-channel.ts b/src/pi-channel.ts index a9b939b47a..ea704c2f5e 100644 --- a/src/pi-channel.ts +++ b/src/pi-channel.ts @@ -126,13 +126,23 @@ export async function runPiAsync(args: string[], env: NodeJS.ProcessEnv = proces } } +/** On-disk root of the pi-managed npm copy of billion-context — exactly where + * pi materializes the settings entry. resolvePiHome() ALREADY includes the + * host's agent segment (~/.pi/agent by default, or PI_CODING_AGENT_DIR / + * PI_HOME), so callers must never append another "agent" (#2850). Single + * source of truth shared by isPiNpmCopy() and the doctor/plugin-list lane + * presence probe. */ +export function piNpmCopyRoot(env: NodeJS.ProcessEnv = process.env): string { + return path.join(resolvePiHome(env), "npm", "node_modules", PI_PACKAGE); +} + /** True when `installDir` is the pi-managed npm copy of billion-context * (/npm/node_modules/billion-context, as pi materializes the * settings entry), literal or after symlink resolution. Such a copy has no * global bili driving its refresh — the proxy running FROM it is the only * candidate to drive pi's own channel. */ export function isPiNpmCopy(installDir: string, env: NodeJS.ProcessEnv = process.env): boolean { - const root = path.join(resolvePiHome(env), "npm", "node_modules", PI_PACKAGE); + const root = piNpmCopyRoot(env); let real = installDir; try { real = fs.realpathSync(installDir); diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 59d280da3d..675a204c38 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -40,6 +40,7 @@ import { resolveDshHome, resolveHermesHome, resolveKimiHome, resolvePiHome } fro import { resolveClaudeNativePort } from "./config.js"; import { lanePreferredPort, readProxyInstanceFile } from "./instance.js"; import { DSH_PACKAGE, dshBundleInstalled, dshHasLegacyManagedBlock, dshProfileDependsOnBili, dshProfileDepSpec, dshProfileDirs, isRegistryDepSpec, planDshSpawn, refreshDshProfileBundles, runDshPlugin, stripLegacyManagedBlock } from "./dsh-channel.js"; +import { piNpmCopyRoot } from "./pi-channel.js"; import { clearOwnerLaneBackoff, fetchRegistryVersion } from "./update.js"; import { restoreKimiBackup, unrouteKimi } from "./kimi/native.js"; import { inspectZcodeRouting, resolveZcodeDataDir } from "./zcode/json-edit.js"; @@ -1956,8 +1957,12 @@ export function inspectLanePresence(agent: PluginAgent): LanePresence { const list = Array.isArray(packages) ? (packages as unknown[]).map(String) : []; const entries = list.filter((p) => isBiliPiEntry(p, root) || p === piEntryFor(root)); if (entries.length === 0) return laneAbsent(); - if (entries.includes(PI_NPM_ENTRY)) { - const store = path.join(resolvePiHome(process.env), "agent", "npm", "node_modules", PI_NPM_ENTRY.slice("npm:".length)); + // #2850: every npm spec form (plain / @pinned / @latest) resolves to + // the SAME pi-managed copy directory — the spec suffix never changes + // the folder name — so match the form, not just the exact unpinned + // string, and probe the real materialization path (piNpmCopyRoot). + if (entries.some((p) => /^npm:billion-context(@|$)/.test(p))) { + const store = piNpmCopyRoot(process.env); return { installed: true, pointers: entries, @@ -2377,7 +2382,9 @@ async function updateLane(agent: PluginAgent, opts: PluginUpdateOpts, log: (leve const packages = readJson(piSettingsFile()).packages; const list = Array.isArray(packages) ? (packages as unknown[]).map(String) : []; const root = selfPackageRoot(); - if (list.some((p) => p === PI_NPM_ENTRY)) { + // #2850: versioned npm specs (@pinned/@latest) are the same pi-managed + // copy, not a checkout pointer — route them to the same message. + if (list.some((p) => /^npm:billion-context(@|$)/.test(p))) { return ["pi: the plugin copy is pi-managed (npm:billion-context) — `pi update` upgrades it; bili never overwrites it (#991)"]; } if (list.some((p) => isPiEntry(p, root))) { diff --git a/tests/doctor.test.ts b/tests/doctor.test.ts index 20c7163a23..2bdef4fedc 100644 --- a/tests/doctor.test.ts +++ b/tests/doctor.test.ts @@ -153,6 +153,107 @@ test("inspectLanePresence dsh: local pin flags a frozen dev lane, registry pin r } }); +test("#2850: inspectLanePresence pi: npm lane resolves the real copy root for every spec form", () => { + const base = mkdtempSync(path.join(tmpdir(), "bc-doctor-pi-npm-")); + try { + const piHome = path.join(base, "pi"); + const realCopy = path.join(piHome, "npm", "node_modules", "billion-context"); + mkdirSync(realCopy, { recursive: true }); + writeFileSync(path.join(realCopy, "package.json"), JSON.stringify({ name: "billion-context", version: "0.1.143" })); + // Decoy at the OLD buggy probe path (/agent/npm/...) — must never be read. + const decoy = path.join(piHome, "agent", "npm", "node_modules", "billion-context"); + mkdirSync(decoy, { recursive: true }); + writeFileSync(path.join(decoy, "package.json"), JSON.stringify({ name: "billion-context", version: "9.9.9" })); + mkdirSync(piHome, { recursive: true }); + const settings = (packages: string[]): void => writeFileSync(path.join(piHome, "settings.json"), JSON.stringify({ packages })); + for (const spec of ["npm:billion-context", "npm:billion-context@0.1.150", "npm:billion-context@latest"]) { + settings([spec]); + withEnv({ PI_CODING_AGENT_DIR: piHome }, () => { + const p = inspectLanePresence("pi"); + assert.equal(p.installed, true); + assert.equal(p.form, "npm"); + assert.deepEqual(p.targets, [realCopy], `${spec}: target must be the real pi materialization dir`); + assert.equal(p.copyVersion, "0.1.143", `${spec}: the decoy at the old wrong path must not leak`); + }); + } + // Copy not materialized yet → empty targets (the doctor hint applies), still installed/npm. + settings(["npm:billion-context"]); + rmrf(realCopy); + withEnv({ PI_CODING_AGENT_DIR: piHome }, () => { + const p = inspectLanePresence("pi"); + assert.equal(p.installed, true); + assert.equal(p.form, "npm"); + assert.deepEqual(p.targets, []); + assert.equal(p.copyVersion, undefined); + }); + } finally { + rmrf(base); + } +}); + +test("#2850: inspectLanePresence pi: absolute-path entry keeps local-path semantics; corrupt settings stays a probe failure", () => { + const base = mkdtempSync(path.join(tmpdir(), "bc-doctor-pi-abs-")); + try { + const piHome = path.join(base, "pi"); + // A real dev checkout is named billion-context — the recognition regex + // keys off that basename (or a node_modules/billion-context segment). + const absRoot = path.join(base, "billion-context"); + mkdirSync(absRoot, { recursive: true }); + mkdirSync(piHome, { recursive: true }); + writeFileSync(path.join(absRoot, "package.json"), JSON.stringify({ name: "billion-context", version: "0.1.142" })); + writeFileSync(path.join(piHome, "settings.json"), JSON.stringify({ packages: [absRoot] })); + withEnv({ PI_CODING_AGENT_DIR: piHome }, () => { + const p = inspectLanePresence("pi"); + assert.equal(p.installed, true); + assert.equal(p.form, "local-path"); + assert.deepEqual(p.targets, [absRoot]); + assert.equal(p.copyVersion, "0.1.142"); + }); + // Corrupt settings: single-source probe propagates the parse error (no false absent). + writeFileSync(path.join(piHome, "settings.json"), "{ not json"); + withEnv({ PI_CODING_AGENT_DIR: piHome }, () => { + assert.throws(() => inspectLanePresence("pi"), /not valid JSON/); + }); + } finally { + rmrf(base); + } +}); + +test("#2850: runDoctor pi npm lane verdicts off the real copy and drops the false materialize hint", async () => { + const base = mkdtempSync(path.join(tmpdir(), "bc-doctor-pi-run-")); + const piHome = path.join(base, "pi"); + const realCopy = path.join(piHome, "npm", "node_modules", "billion-context"); + mkdirSync(realCopy, { recursive: true }); + writeFileSync(path.join(realCopy, "package.json"), JSON.stringify({ name: "billion-context", version: "0.1.143" })); + writeFileSync(path.join(piHome, "settings.json"), JSON.stringify({ packages: ["npm:billion-context"] })); + const registryDoc = (version: string): Response => new Response(JSON.stringify({ name: "billion-context", version }), { status: 200, headers: { "Content-Type": "application/json" } }); + try { + await mockFetch(() => registryDoc("999.0.0"))(async () => { + await withEnv({ ...doctorSandboxEnv(base), PI_CODING_AGENT_DIR: piHome }, async () => { + const report = await runDoctor({ packageName: "billion-context", runningVersion: "0.1.143" }); + const pi = report.lanes.find((l) => l.agent === "pi"); + assert.equal(pi?.installed, true); + assert.equal(pi?.copyVersion, "0.1.143"); + assert.equal(pi?.verdict, "stale"); + assert.match(renderDoctorReport(report), /stale \(v0\.1\.143 → v999\.0\.0\)/); + assert.ok(!(pi?.detail ?? "").includes("materializes"), `copy exists — no false hint: ${pi?.detail}`); + }); + }); + // Copy genuinely absent → the pre-materialization hint applies again. + rmrf(realCopy); + await mockFetch(() => registryDoc("999.0.0"))(async () => { + await withEnv({ ...doctorSandboxEnv(base), PI_CODING_AGENT_DIR: piHome }, async () => { + const report = await runDoctor({ packageName: "billion-context", runningVersion: "0.1.143" }); + const pi = report.lanes.find((l) => l.agent === "pi"); + assert.equal(pi?.installed, true); + assert.match(pi?.detail ?? "", /pi materializes the npm copy on next startup/); + }); + }); + } finally { + rmrf(base); + } +}); + test("inspectLanePresence hermes: baked plugin.yaml version is the freshness signal", () => { const base = mkdtempSync(path.join(tmpdir(), "bc-doctor-hermes-")); try { diff --git a/tests/plugin-list-version.test.ts b/tests/plugin-list-version.test.ts index 2493aa883d..e6705a6dde 100644 --- a/tests/plugin-list-version.test.ts +++ b/tests/plugin-list-version.test.ts @@ -154,6 +154,71 @@ test("pluginStatusAll resolves each lane's own on-disk copy version (shared vs i } }); +test("#2850: pluginStatusAll pi reads the pi-managed npm copy's own version for every spec form (decoy at the old wrong path ignored)", () => { + const base = mkdtempSync(path.join(tmpdir(), "bc-pluginlist-pi-")); + try { + const piHome = path.join(base, "pi"); + const realCopy = path.join(piHome, "npm", "node_modules", "billion-context"); + // Decoy at the OLD buggy probe path (/agent/npm/...) with a + // different version — proves the fix reads the real materialized copy. + const decoy = path.join(piHome, "agent", "npm", "node_modules", "billion-context"); + mkdirSync(realCopy, { recursive: true }); + mkdirSync(decoy, { recursive: true }); + writeFileSync(path.join(realCopy, "package.json"), JSON.stringify({ name: "billion-context", version: "0.1.143" })); + writeFileSync(path.join(decoy, "package.json"), JSON.stringify({ name: "billion-context", version: "9.9.9" })); + const settings = (packages: string[]): void => { + mkdirSync(piHome, { recursive: true }); + writeFileSync(path.join(piHome, "settings.json"), JSON.stringify({ packages })); + }; + for (const spec of ["npm:billion-context", "npm:billion-context@0.1.150", "npm:billion-context@latest"]) { + settings([spec]); + withEnv({ ...isolatedHome(base), PI_CODING_AGENT_DIR: piHome }, () => { + const row = pluginStatusAll().find((r) => r.agent === "pi")!; + assert.ok(row.status.startsWith("installed"), `${spec}: ${row.status}`); + assert.equal(row.copyVersion, "0.1.143", `${spec}: must read the real copy — never the decoy, the spec string, or the CLI version`); + }); + } + // Copy not materialized yet → installed but unresolvable ("unknown"). + settings(["npm:billion-context"]); + rmrf(realCopy); + withEnv({ ...isolatedHome(base), PI_CODING_AGENT_DIR: piHome }, () => { + const row = pluginStatusAll().find((r) => r.agent === "pi")!; + assert.ok(row.status.startsWith("installed")); + assert.equal(row.copyVersion, undefined, "missing copy must stay unknown, never fall back to the CLI version"); + }); + } finally { + rmrf(base); + } +}); + +test("#2850: pluginStatusAll pi resolves the copy under the default home and PI_HOME overrides", () => { + const base = mkdtempSync(path.join(tmpdir(), "bc-pluginlist-pihome-")); + try { + const place = (piHome: string, version: string): void => { + const copy = path.join(piHome, "npm", "node_modules", "billion-context"); + mkdirSync(copy, { recursive: true }); + writeFileSync(path.join(copy, "package.json"), JSON.stringify({ name: "billion-context", version })); + writeFileSync(path.join(piHome, "settings.json"), JSON.stringify({ packages: ["npm:billion-context"] })); + }; + // Default layout: /.pi/agent + place(path.join(base, "home", ".pi", "agent"), "0.1.140"); + withEnv(isolatedHome(base), () => { + const row = pluginStatusAll().find((r) => r.agent === "pi")!; + assert.ok(row.status.startsWith("installed")); + assert.equal(row.copyVersion, "0.1.140"); + }); + // PI_HOME override (PI_CODING_AGENT_DIR unset) + const ph = path.join(base, "pihome"); + place(ph, "0.1.141"); + withEnv({ ...isolatedHome(base), PI_HOME: ph }, () => { + const row = pluginStatusAll().find((r) => r.agent === "pi")!; + assert.equal(row.copyVersion, "0.1.141"); + }); + } finally { + rmrf(base); + } +}); + test("pluginStatusAll dsh joins distinct per-profile copy versions so drift is visible", () => { const base = mkdtempSync(path.join(tmpdir(), "bc-pluginlist-dsh-")); try {