Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/pi-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
* (<piHome>/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);
Expand Down
13 changes: 10 additions & 3 deletions src/plugin-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))) {
Expand Down
101 changes: 101 additions & 0 deletions tests/doctor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<piHome>/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 {
Expand Down
65 changes: 65 additions & 0 deletions tests/plugin-list-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<piHome>/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: <HOME>/.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 {
Expand Down
Loading