Merge branch 'my-pi-worktree1'

# Conflicts:
#	AGENTS.md
#	pi-tool-search/CHANGELOG.md
#	pi-tool-search/docs/dynamic-tool-loading.md
This commit is contained in:
云服务部-叶林立
2026-08-25 15:18:58 +08:00
23 changed files with 1333 additions and 9 deletions
+12
View File
@@ -15,6 +15,10 @@ test("all package extensions load together without global registration conflicts
files: string[];
pi: { extensions: string[] };
};
assert.ok(
packageJson.pi.extensions.includes("./extensions/plugin-init.ts"),
"the bundle must expose the project plugin initializer command",
);
assert.ok(
packageJson.pi.extensions.includes("./hippo-memory-pi/index.ts"),
"the bundle must load the vendored official Hippo Memory extension",
@@ -33,6 +37,9 @@ test("all package extensions load together without global registration conflicts
assert.equal(packageJson.dependencies.jiti, "2.7.0", "the packed helper must load local TypeScript outside Pi");
assert.ok(packageJson.files.includes("pi-ssh"), "the packed bundle must include the locally maintained SSH source");
assert.ok(packageJson.files.includes("ssh_config.sh"), "the packed bundle must include the SSH host import helper");
assert.ok(packageJson.pi.extensions.includes("./pi-ask-user/index.ts"), "the bundle must load pi-ask-user");
assert.equal(packageJson.dependencies["pi-ask-user"], "file:./pi-ask-user");
assert.ok(packageJson.files.includes("pi-ask-user"), "the packed bundle must include pi-ask-user source");
const permissionConfig = JSON.parse(
await readFile(join(repositoryRoot, "config", "pi-permission-system.json"), "utf8"),
) as { authorizerChain: string[]; permission: Record<string, unknown> };
@@ -49,6 +56,11 @@ test("all package extensions load together without global registration conflicts
packageJson.pi.extensions.indexOf("./pi-tool-search/extensions/index.ts"),
"pi-ssh tools must register before Tool Search builds its catalog",
);
assert.ok(
packageJson.pi.extensions.indexOf("./pi-ask-user/index.ts") <
packageJson.pi.extensions.indexOf("./pi-tool-search/extensions/index.ts"),
"ask_user_question must register before Tool Search builds its catalog",
);
assert.equal(
packageJson.dependencies["@firstpick/pi-extension-codex-fast-mode"],
"file:./pi-extension-codex-fast-mode",
+210
View File
@@ -0,0 +1,210 @@
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import assert from "node:assert/strict";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { homedir, tmpdir } from "node:os";
import { join, parse } from "node:path";
import test from "node:test";
import pluginInitExtension, {
findMissingPluginInitializers,
initializeMissingPlugins,
validatePluginInitCwd,
} from "../extensions/plugin-init.ts";
test("initializes missing CodeGraph and Hippo state after preflighting both CLIs", async () => {
const cwd = await mkdtemp(join(tmpdir(), "my-pi-plugin-init-"));
const calls: Array<{ command: string; args: string[]; cwd: string }> = [];
try {
const targets = await findMissingPluginInitializers(cwd);
assert.deepEqual(targets.map(({ name }) => name), ["CodeGraph", "Hippo"]);
await initializeMissingPlugins(cwd, targets, async (command, args, options) => {
calls.push({ command, args, cwd: options.cwd });
return { code: 0, stdout: "ok", stderr: "" };
});
assert.deepEqual(calls, [
{ command: "codegraph", args: ["--version"], cwd },
{ command: "hippo", args: ["--version"], cwd },
{ command: "codegraph", args: ["init", cwd], cwd },
{ command: "hippo", args: ["init"], cwd },
]);
} finally {
await rm(cwd, { recursive: true, force: true });
}
});
test("uses the CLIs' database files as authoritative initialization markers", async () => {
const cwd = await mkdtemp(join(tmpdir(), "my-pi-plugin-init-"));
const calls: string[] = [];
try {
await mkdir(join(cwd, ".codegraph"));
await mkdir(join(cwd, ".hippo"));
assert.deepEqual(
(await findMissingPluginInitializers(cwd)).map(({ name }) => name),
["CodeGraph", "Hippo"],
"bare state directories must not count as initialized",
);
await writeFile(join(cwd, ".codegraph", "codegraph.db"), "");
assert.deepEqual((await findMissingPluginInitializers(cwd)).map(({ name }) => name), ["Hippo"]);
await writeFile(join(cwd, ".hippo", "hippo.db"), "");
const targets = await findMissingPluginInitializers(cwd);
assert.deepEqual(targets, []);
await initializeMissingPlugins(cwd, targets, async (command, args) => {
calls.push(`${command} ${args.join(" ")}`);
return { code: 0, stdout: "ok", stderr: "" };
});
assert.deepEqual(calls, ["codegraph --version", "hippo --version"]);
} finally {
await rm(cwd, { recursive: true, force: true });
}
});
test("rejects unsafe project roots", () => {
assert.throws(() => validatePluginInitCwd(parse(homedir()).root), /文件系统根目录/);
assert.throws(() => validatePluginInitCwd(homedir()), /用户主目录/);
});
test("preflights both CLIs before creating project state", async () => {
const cwd = await mkdtemp(join(tmpdir(), "my-pi-plugin-init-"));
const calls: string[] = [];
try {
const targets = await findMissingPluginInitializers(cwd);
await assert.rejects(
initializeMissingPlugins(cwd, targets, async (command, args) => {
calls.push(`${command} ${args.join(" ")}`);
return command === "hippo"
? { code: 1, stdout: "", stderr: "not installed" }
: { code: 0, stdout: "ok", stderr: "" };
}),
/Hippo 检测失败:not installed/,
);
assert.deepEqual(calls, ["codegraph --version", "hippo --version"]);
} finally {
await rm(cwd, { recursive: true, force: true });
}
});
test("plugin_init confirms the target and hot-reloads after initialization", async () => {
let command: { handler: (args: string, ctx: any) => Promise<void> } | undefined;
const calls: string[] = [];
const pi = {
registerCommand(name: string, definition: typeof command) {
assert.equal(name, "plugin_init");
command = definition;
},
exec: async (program: string, args: string[]) => {
calls.push(`${program} ${args.join(" ")}`);
return { code: 0, stdout: "ok", stderr: "" };
},
} as unknown as ExtensionAPI;
pluginInitExtension(pi);
assert.ok(command);
const registeredCommand = command!;
const cwd = await mkdtemp(join(tmpdir(), "my-pi-plugin-init-"));
let reloaded = false;
let confirmation = "";
try {
await registeredCommand.handler("", {
cwd,
hasUI: true,
ui: {
confirm: async (_title: string, message: string) => {
confirmation = message;
return true;
},
notify() {},
},
reload: async () => {
reloaded = true;
},
});
assert.match(confirmation, new RegExp(cwd.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")));
assert.deepEqual(calls, [
"codegraph --version",
"hippo --version",
`codegraph init ${cwd}`,
"hippo init",
]);
assert.equal(reloaded, true);
} finally {
await rm(cwd, { recursive: true, force: true });
}
});
test("plugin_init reports retained partial state and does not reload when a later initializer fails", async () => {
let command: { handler: (args: string, ctx: any) => Promise<void> } | undefined;
const pi = {
registerCommand(_name: string, definition: typeof command) {
command = definition;
},
exec: async (program: string, args: string[]) => ({
code: program === "hippo" && args[0] === "init" ? 1 : 0,
stdout: "",
stderr: "mock failure",
}),
} as unknown as ExtensionAPI;
pluginInitExtension(pi);
assert.ok(command);
const registeredCommand = command!;
const cwd = await mkdtemp(join(tmpdir(), "my-pi-plugin-init-"));
let reloaded = false;
let error = "";
try {
await registeredCommand.handler("", {
cwd,
hasUI: true,
ui: {
confirm: async () => true,
notify(message: string, level: string) {
if (level === "error") error = message;
},
},
reload: async () => {
reloaded = true;
},
});
assert.match(error, /Hippo 初始化失败:mock failure/);
assert.match(error, /已完成的 CodeGraph 初始化会保留,不会自动回滚/);
assert.equal(reloaded, false);
} finally {
await rm(cwd, { recursive: true, force: true });
}
});
test("plugin_init stops without executing or reloading when confirmation is cancelled", async () => {
let command: { handler: (args: string, ctx: any) => Promise<void> } | undefined;
let executed = false;
const pi = {
registerCommand(_name: string, definition: typeof command) {
command = definition;
},
exec: async () => {
executed = true;
return { code: 0, stdout: "", stderr: "" };
},
} as unknown as ExtensionAPI;
pluginInitExtension(pi);
const registeredCommand = command!;
const cwd = await mkdtemp(join(tmpdir(), "my-pi-plugin-init-"));
let reloaded = false;
try {
await registeredCommand.handler("", {
cwd,
hasUI: true,
ui: { confirm: async () => false, notify() {} },
reload: async () => {
reloaded = true;
},
});
assert.equal(executed, false);
assert.equal(reloaded, false);
} finally {
await rm(cwd, { recursive: true, force: true });
}
});