feat(cli): tell users when pi2.nvim cannot host a spawned instance

`checkPlugin` only looked for `lua/pi/web/bridge.lua`, so a checkout that can
drive an existing nvim but cannot be spawned reported `ok` and the failure
surfaced later as an unexplained "did not say hello".

The probe now also requires `lua/pi/host.lua`: both present is `ok`, bridge only
is `warn` with the update hint, neither is `fail`. `pi2 daemon --help` lists the
instance knobs (`PI2_NVIM_BIN`, `PI2_NVIM_INIT`, `PI2_PLUGIN_DIR`,
`PI2_ENSURE_TIMEOUT_MS`) it previously kept to itself.
This commit is contained in:
yuez committed 2026-10-28 17:32:41 +08:00
1 parent 85faacb0ba
commit b95ff6055b
3 files changed
+80 -6

No files matched your search

+5
View File
@@ -46,6 +46,11 @@ Environment:
PI2_PORT, PI2_HOST (${DEFAULT_HOST} (all interfaces)), PI2_STATIC_DIR, PI2_PORT, PI2_HOST (${DEFAULT_HOST} (all interfaces)), PI2_STATIC_DIR,
PI2_ORIGINS, PI2_RUNTIME_DIR, PI2_DATA_DIR, PI2_STATE_DIR, PI2_ORIGINS, PI2_RUNTIME_DIR, PI2_DATA_DIR, PI2_STATE_DIR,
PI2_LOG_LEVEL, PI2_WORKSPACE_ROOTS, PI2_PUBLIC_URL PI2_LOG_LEVEL, PI2_WORKSPACE_ROOTS, PI2_PUBLIC_URL
Managed instances:
PI2_NVIM_BIN=<path> nvim binary on the daemon's PATH (default nvim)
PI2_NVIM_INIT=<file> -u <file>; "user" loads your own config; unset = generated init
PI2_PLUGIN_DIR=<dir> pi2.nvim checkout the generated init prepends
PI2_ENSURE_TIMEOUT_MS=<n> spawn hello timeout in ms (default 10000)
Runtime files ($PI2_RUNTIME_DIR): Runtime files ($PI2_RUNTIME_DIR):
d.sock plugin unix socket (nvim → daemon) d.sock plugin unix socket (nvim → daemon)
+22 -5
View File
@@ -13,7 +13,7 @@ import { promisify } from "node:util";
import { DEFAULT_HOST, DEFAULT_PORT, describeHost, loadConfig } from "../config.js"; import { DEFAULT_HOST, DEFAULT_PORT, describeHost, loadConfig } from "../config.js";
import { defaultStaticDir, executablePath, isBundledRuntime, runtimeDescription } from "../core/install.js"; import { defaultStaticDir, executablePath, isBundledRuntime, runtimeDescription } from "../core/install.js";
import { isLoopbackHost } from "../core/network.js"; import { isLoopbackHost } from "../core/network.js";
import { daemonSocketPath, runtimeDir } from "../core/paths.js"; import { daemonSocketPath, expandTilde, runtimeDir } from "../core/paths.js";
import { PROTOCOL_VERSION } from "../protocol/types.js"; import { PROTOCOL_VERSION } from "../protocol/types.js";
import { import {
defaultServicePaths, defaultServicePaths,
@@ -71,7 +71,7 @@ export interface DoctorContext {
nvim: BinaryProbe; nvim: BinaryProbe;
pi: BinaryProbe; pi: BinaryProbe;
curl: BinaryProbe; curl: BinaryProbe;
plugin: { dir: string | undefined; bridgeFound: boolean }; plugin: { dir: string | undefined; bridgeFound: boolean; hostFound?: boolean };
runtime: { path: string; exists: boolean; mode: number | undefined }; runtime: { path: string; exists: boolean; mode: number | undefined };
cliToken: { path: string; exists: boolean; mode: number | undefined }; cliToken: { path: string; exists: boolean; mode: number | undefined };
socket: { path: string; exists: boolean }; socket: { path: string; exists: boolean };
@@ -182,7 +182,16 @@ export function checkCurl(probe: BinaryProbe): DoctorCheck {
export function checkPlugin(probe: DoctorContext["plugin"]): DoctorCheck { export function checkPlugin(probe: DoctorContext["plugin"]): DoctorCheck {
if (probe.bridgeFound) { if (probe.bridgeFound) {
return { name: "plugin", status: "ok", detail: `${probe.dir ?? "pi2.nvim"} (lua/pi/web/bridge.lua)` }; // The daemon spawns headless hosts with `lua require("pi.host").start()`,
// so a plugin without `lua/pi/host.lua` cannot serve the web console.
if (probe.hostFound === true) {
return { name: "plugin", status: "ok", detail: `${probe.dir ?? "pi2.nvim"} (pi2.nvim host-capable)` };
}
return {
name: "plugin",
status: "warn",
detail: `pi2.nvim at ${probe.dir ?? "~/.local/share/nvim/lazy/pi2.nvim"} has no lua/pi/host.lua — update the plugin; the daemon cannot start a workspace from the web console`,
};
} }
return { return {
name: "plugin", name: "plugin",
@@ -586,6 +595,13 @@ async function binaryProbe(command: string, args: readonly string[], options: Do
return { found: true, path, version }; return { found: true, path, version };
} }
/** `PI2_PLUGIN_DIR` → plugin root; `~`/`~/…` expand against the resolved home. */
function pluginHome(raw: string | undefined, home: string): string {
const configured = raw?.trim();
if (configured === undefined || configured === "") return join(home, ".local", "share", "nvim", "lazy", "pi2.nvim");
return expandTilde(configured, home) ?? configured;
}
export async function collectContext(options: DoctorOptions = {}): Promise<DoctorContext> { export async function collectContext(options: DoctorOptions = {}): Promise<DoctorContext> {
const env = options.env ?? process.env; const env = options.env ?? process.env;
const platform = options.platform ?? osPlatform(); const platform = options.platform ?? osPlatform();
@@ -602,8 +618,9 @@ export async function collectContext(options: DoctorOptions = {}): Promise<Docto
const pi = await binaryProbe("pi", ["--version"], options, env); const pi = await binaryProbe("pi", ["--version"], options, env);
const curl = await binaryProbe("curl", ["--version"], options, env); const curl = await binaryProbe("curl", ["--version"], options, env);
const pluginDir = env.PI2_PLUGIN_DIR?.trim() || join(home, ".local", "share", "nvim", "lazy", "pi2.nvim"); const pluginDir = pluginHome(env.PI2_PLUGIN_DIR, home);
const bridgeFound = existsSync(join(pluginDir, "lua", "pi", "web", "bridge.lua")); const bridgeFound = existsSync(join(pluginDir, "lua", "pi", "web", "bridge.lua"));
const hostFound = existsSync(join(pluginDir, "lua", "pi", "host.lua"));
const runtimeStat = statOrUndefined(runtime); const runtimeStat = statOrUndefined(runtime);
const cliTokenPath = join(runtime, "cli.token"); const cliTokenPath = join(runtime, "cli.token");
@@ -674,7 +691,7 @@ export async function collectContext(options: DoctorOptions = {}): Promise<Docto
nvim, nvim,
pi, pi,
curl, curl,
plugin: { dir: pluginDir, bridgeFound }, plugin: { dir: pluginDir, bridgeFound, hostFound },
runtime: { path: runtime, exists: runtimeStat.exists, mode: runtimeStat.mode }, runtime: { path: runtime, exists: runtimeStat.exists, mode: runtimeStat.mode },
cliToken: { path: cliTokenPath, exists: tokenStat.exists, mode: tokenStat.mode }, cliToken: { path: cliTokenPath, exists: tokenStat.exists, mode: tokenStat.mode },
socket: { path: socketPath, exists: existsSync(socketPath) }, socket: { path: socketPath, exists: existsSync(socketPath) },
+53 -1
View File
@@ -8,6 +8,7 @@ import {
checkLinger, checkLinger,
checkNvim, checkNvim,
checkNode, checkNode,
checkPlugin,
checkProtocol, checkProtocol,
checkUnit, checkUnit,
collectContext, collectContext,
@@ -34,7 +35,7 @@ function context(overrides: Partial<DoctorContext> = {}): DoctorContext {
path: "/usr/bin/curl", path: "/usr/bin/curl",
version: "curl 8.7.1 (x86_64-pc-linux-gnu) libcurl/8.7.1 OpenSSL/3.3.1", version: "curl 8.7.1 (x86_64-pc-linux-gnu) libcurl/8.7.1 OpenSSL/3.3.1",
}, },
plugin: { dir: "/home/u/.local/share/nvim/lazy/pi2.nvim", bridgeFound: true }, plugin: { dir: "/home/u/.local/share/nvim/lazy/pi2.nvim", bridgeFound: true, hostFound: true },
runtime: { path: "/run/user/1000/pi2", exists: true, mode: 0o700 }, runtime: { path: "/run/user/1000/pi2", exists: true, mode: 0o700 },
cliToken: { path: "/run/user/1000/pi2/cli.token", exists: true, mode: 0o600 }, cliToken: { path: "/run/user/1000/pi2/cli.token", exists: true, mode: 0o600 },
socket: { path: "/run/user/1000/pi2/d.sock", exists: true }, socket: { path: "/run/user/1000/pi2/d.sock", exists: true },
@@ -99,6 +100,24 @@ describe("doctor checks (all passing)", () => {
}); });
}); });
describe("doctor plugin check", () => {
it("passes only when bridge.lua and lua/pi/host.lua both exist", () => {
expect(checkPlugin({ dir: "/p", bridgeFound: true, hostFound: true })).toEqual({
name: "plugin",
status: "ok",
detail: "/p (pi2.nvim host-capable)",
});
const stale = checkPlugin({ dir: "/p", bridgeFound: true, hostFound: false });
expect(stale.status).toBe("warn");
expect(stale.detail).toContain("/p has no lua/pi/host.lua");
expect(checkPlugin({ dir: "/p", bridgeFound: true }).status).toBe("warn");
expect(checkPlugin({ dir: "/p", bridgeFound: false })).toMatchObject({ status: "fail" });
expect(checkPlugin({ dir: undefined, bridgeFound: false }).detail).toContain("set PI2_PLUGIN_DIR or install");
});
});
describe("doctor bind check", () => { describe("doctor bind check", () => {
it("is ok for a loopback bind", () => { it("is ok for a loopback bind", () => {
const check = checkBind({ host: "127.0.0.1", port: 7676, publicUrl: null }); const check = checkBind({ host: "127.0.0.1", port: 7676, publicUrl: null });
@@ -436,6 +455,15 @@ describe("doctor failure cases", () => {
expect(report.ok).toBe(true); expect(report.ok).toBe(true);
}); });
it("warns when the plugin lacks lua/pi/host.lua", async () => {
const report = await checks({
plugin: { dir: "/home/u/nvim/pi2.nvim", bridgeFound: true, hostFound: false },
});
expect(statusOf(report, "plugin")).toBe("warn");
expect(report.ok).toBe(true);
expect(report.checks.find((check) => check.name === "plugin")?.detail).toContain("lua/pi/host.lua");
});
it("fails when the plugin is not installed", async () => { it("fails when the plugin is not installed", async () => {
const report = await checks({ plugin: { dir: "/home/u/nvim/pi2.nvim", bridgeFound: false } }); const report = await checks({ plugin: { dir: "/home/u/nvim/pi2.nvim", bridgeFound: false } });
expect(statusOf(report, "plugin")).toBe("fail"); expect(statusOf(report, "plugin")).toBe("fail");
@@ -611,6 +639,30 @@ describe("collectContext", () => {
expect(report.checks.find((check) => check.name === "web-dist")?.status).toBe("warn"); expect(report.checks.find((check) => check.name === "web-dist")?.status).toBe("warn");
}); });
it("expands a leading ~/ in PI2_PLUGIN_DIR", async () => {
const home = join(dir, "home");
const pluginDir = join(home, "plugins", "pi2.nvim");
mkdirSync(join(pluginDir, "lua", "pi", "web"), { recursive: true });
writeFileSync(join(pluginDir, "lua", "pi", "web", "bridge.lua"), "");
writeFileSync(join(pluginDir, "lua", "pi", "host.lua"), "");
const ctx = await collectContext({
platform: "linux",
home,
uid: 1000,
env: {
PATH: "/usr/bin",
PI2_PLUGIN_DIR: "~/plugins/pi2.nvim",
PI2_RUNTIME_DIR: join(dir, "run"),
PI2_STATIC_DIR: join(dir, "web"),
},
which: () => undefined,
run: async () => ({ ok: false, stdout: "", stderr: "not found" }),
});
expect(ctx.plugin).toEqual({ dir: pluginDir, bridgeFound: true, hostFound: true });
});
it("records a missing daemon as unreachable", async () => { it("records a missing daemon as unreachable", async () => {
const ctx = await collectContext({ const ctx = await collectContext({
platform: "darwin", platform: "darwin",