Files
my-pi/tests/extension-load.test.ts
T
云服务部-叶林立andDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com> 87940da51a feat(prompt): 重新注册 bash,把"不要用 bash 搜索"写进工具描述
真实会话里模型仍习惯性先调 bash grep,被拒后才改用 grep 工具;系统提示词里已有 4 处
提醒,但模型下调用决定时看的是工具 schema 描述,而 Pi 内置 bash 描述对此只字未提。

- 新增 extensions/bash-override.ts:用 Pi 的 createBashToolDefinition 重新注册 bash,
  执行、输出结构与 codemode 说明完全沿用上游;shellPath/commandPrefix 按 ctx.cwd
  经 SettingsManager 惰性解析,与内置一致
- tools.jsonc 新增 descriptionAppend 字段,由 prompt-guard 注册时拼接到上游描述后
- bash 增加命令→工具对照规则(grep -n / grep -rl / find -name),并提示
  文件清单用 $(git ls-files) 传参、不要接 xargs(会触发权限审核)

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-22 01:20:52 -07:00

424 lines
19 KiB
TypeScript

import assert from "node:assert/strict";
import { existsSync } from "node:fs";
import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { spawnSync } from "node:child_process";
import test from "node:test";
const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const hasPi = spawnSync("pi", ["--version"], { encoding: "utf8" }).status === 0;
const filesIncludePackageDir = (files: string[], packageDir: string): boolean =>
files.some((entry) => entry === packageDir || entry.startsWith(`${packageDir}/`));
test("enabled subagents wrapper loads with its bundle-owned mandatory permission entry", { skip: !hasPi }, async () => {
const packageJson = JSON.parse(await readFile(join(repositoryRoot, "package.json"), "utf8")) as {
dependencies: Record<string, string>;
files: string[];
pi: { extensions: string[] };
};
assert.ok(filesIncludePackageDir(packageJson.files, "pi-subagents"));
assert.equal(packageJson.dependencies["@tintinweb/pi-subagents"], "file:./pi-subagents");
assert.ok(
packageJson.pi.extensions.includes("./extensions/subagents.ts"),
"the bundle must default-load the locally maintained subagents wrapper",
);
const home = await mkdtemp(join(tmpdir(), "my-pi-subagents-wrapper-load-"));
const result = spawnSync(
"pi",
["--no-extensions", "-e", join(repositoryRoot, "extensions", "subagents.ts"), "--mode", "rpc"],
{
encoding: "utf8",
timeout: 60_000,
env: {
...process.env,
HOME: home,
XDG_CONFIG_HOME: join(home, ".config"),
PI_CODING_AGENT_DIR: join(home, ".pi-agent"),
PI_OFFLINE: "1",
},
},
);
assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`);
assert.doesNotMatch(`${result.stdout}\n${result.stderr}`, /Failed to load extension/u);
});
test("all package extensions load together without global registration conflicts", { skip: !hasPi }, async () => {
const packageJson = JSON.parse(await readFile(join(repositoryRoot, "package.json"), "utf8")) as {
dependencies: Record<string, string>;
files: string[];
pi: { extensions: string[]; skills?: string[] };
allowScripts?: Record<string, boolean>;
};
assert.ok(
packageJson.pi.extensions.includes("./extensions/codegraph.ts"),
"the bundle must expose the native codegraph_explore tool",
);
assert.equal(
packageJson.pi.extensions.indexOf("./extensions/bash-override.ts"),
packageJson.pi.extensions.indexOf("./extensions/fff-override.ts") + 1,
"the bash description override must register right after fff-override",
);
assert.ok(
packageJson.pi.extensions.includes("./pi-web-search/index.ts"),
"the bundle must load the unified pi-web-search tools",
);
assert.equal(packageJson.dependencies["pi-web-search"], "file:./pi-web-search");
assert.equal(
packageJson.pi.extensions.includes("./extensions/plugin-init.ts"),
false,
"the retired plugin initializer command must not be loaded",
);
assert.ok(
packageJson.pi.extensions.includes("./pi-hermes-memory/src/index.ts"),
"the bundle must load the vendored Hermes memory extension",
);
assert.ok(
packageJson.pi.extensions.includes("./pi-minimal-footer/index.ts"),
"the bundle must load the locally maintained minimal footer",
);
assert.equal(packageJson.dependencies["pi-compaction"], "file:./pi-compaction");
assert.ok(filesIncludePackageDir(packageJson.files, "pi-compaction"), "the packed bundle must include maintained pi-compaction source");
assert.ok(
packageJson.pi.extensions.includes("./pi-compaction/index.ts"),
"the bundle must load the unified pi-compaction authority",
);
assert.equal(packageJson.dependencies["pi-condense"], undefined);
assert.equal(packageJson.dependencies["@ogulcancelik/pi-codex-compaction"], undefined);
assert.equal(packageJson.dependencies["pi-context-view"], undefined);
assert.ok(packageJson.pi.extensions.includes("./extensions/context-view.ts"));
assert.ok(
packageJson.pi.extensions.indexOf("./pi-compaction/index.ts") <
packageJson.pi.extensions.indexOf("./extensions/context-view.ts"),
"the context observer must load after the compaction authority",
);
assert.ok(
packageJson.pi.extensions.indexOf("./pi-compaction/index.ts") <
packageJson.pi.extensions.indexOf("./pi-minimal-footer/index.ts"),
"the footer must load after the compaction metrics service",
);
assert.ok(packageJson.pi.extensions.includes("./pi-ssh/index.ts"), "the bundle must load the locally maintained SSH extension");
assert.equal(packageJson.dependencies["pi-ssh"], "file:./pi-ssh");
assert.match(packageJson.dependencies.ssh2 ?? "", /^\d+\.\d+\.\d+$/, "the packed bundle must install pi-ssh's runtime transport at an exact version");
assert.match(packageJson.dependencies.jiti ?? "", /^\d+\.\d+\.\d+$/, "the packed helper must load local TypeScript outside Pi at an exact version");
assert.ok(filesIncludePackageDir(packageJson.files, "pi-ssh"), "the packed bundle must include the locally maintained SSH source");
assert.ok(packageJson.files.includes("configure.sh"), "the packed bundle must include the search/SSH config 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(filesIncludePackageDir(packageJson.files, "pi-ask-user"), "the packed bundle must include pi-ask-user source");
assert.ok(
packageJson.pi.extensions.includes("./pi-worktree/index.ts"),
"the bundle must load the locally maintained worktree commands",
);
assert.equal(packageJson.dependencies["pi-worktree"], "file:./pi-worktree");
assert.ok(filesIncludePackageDir(packageJson.files, "pi-worktree"), "the packed bundle must include pi-worktree source");
const chromeExtension = "./pi-chrome/extensions/chrome-profile-bridge/index.ts";
assert.equal(packageJson.dependencies["pi-chrome"], "file:./pi-chrome", "the browser bridge must use the locally maintained source");
assert.ok(filesIncludePackageDir(packageJson.files, "pi-chrome"), "the packed bundle must include the locally maintained pi-chrome source");
assert.ok(packageJson.pi.extensions.includes(chromeExtension), "the bundle must load the local Chrome profile bridge directly");
assert.ok(
packageJson.pi.extensions.indexOf(chromeExtension) <
packageJson.pi.extensions.indexOf("./pi-tool-search/extensions/index.ts"),
"pi-chrome must register before Tool Search catalogs its authorized tools",
);
const permissionPolicySource = await readFile(
join(repositoryRoot, "config", "pi-permission-policy.jsonc"),
"utf8",
);
const hardGuardSource = await readFile(
join(repositoryRoot, "config", "pi-permission-hard-guards.jsonc"),
"utf8",
);
const stripLineComments = (source: string): string => source.replace(/^\s*\/\/.*$/gmu, "");
const permissionConfig = JSON.parse(stripLineComments(permissionPolicySource)) as {
authorizerChain: string[];
permission: Record<string, unknown>;
};
const hardGuardConfig = JSON.parse(stripLineComments(hardGuardSource)) as {
permission: { path?: Record<string, string>; bash: Record<string, string> };
};
assert.equal(permissionConfig.permission.ssh_connect, "ask", "SSH connection must enter the permission gate");
assert.equal(permissionConfig.permission.ssh_cd, "ask", "remote workspace changes must enter the permission gate");
assert.deepEqual(permissionConfig.authorizerChain, ["auto-review"], "SSH connection asks must reach AutoReview");
assert.equal(permissionConfig.permission.Agent, "allow", "launching a subagent is side-effect free; child actions stay gated");
assert.equal(permissionConfig.permission.get_subagent_result, "allow");
assert.equal(permissionConfig.permission.steer_subagent, "allow");
assert.equal(hardGuardConfig.permission.path, undefined, "sensitive paths must not be hard-denied by default");
assert.equal(hardGuardConfig.permission.bash["rm -rf /"], "deny");
assert.equal(
packageJson.pi.extensions.some((entry) => entry.includes("pi-permission-auto-review")),
false,
"AutoReview must be integrated instead of loaded as a second extension",
);
assert.ok(
packageJson.pi.extensions.indexOf("./extensions/permission-system.ts") <
packageJson.pi.extensions.indexOf("./pi-ssh/index.ts"),
"the permission service must load before pi-ssh registers its permission bridge",
);
assert.ok(
packageJson.pi.extensions.indexOf("./pi-ssh/index.ts") <
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.ok(
packageJson.pi.extensions.includes("./pi-notify/src/index.ts"),
"the bundle must load the locally maintained Kitty notification extension",
);
assert.equal(packageJson.dependencies["@smoose/pi-notify"], "file:./pi-notify");
assert.ok(
filesIncludePackageDir(packageJson.files, "pi-notify"),
"the packed bundle must include the locally maintained notification source",
);
assert.equal(packageJson.dependencies["@ogulcancelik/pi-minimal-footer"], "file:./pi-minimal-footer");
assert.equal(packageJson.dependencies["@tintinweb/pi-subagents"], "file:./pi-subagents");
assert.ok(packageJson.pi.extensions.includes("./extensions/subagents.ts"));
assert.ok(
packageJson.pi.extensions.indexOf("./extensions/permission-system.ts") <
packageJson.pi.extensions.indexOf("./extensions/subagents.ts"),
"the permission service must load before the subagents wrapper injects it as a mandatory child extension",
);
assert.ok(
packageJson.pi.extensions.indexOf("./extensions/subagents.ts") <
packageJson.pi.extensions.indexOf("./pi-tool-search/extensions/index.ts"),
"the subagent orchestration tools must register before Tool Search builds its catalog",
);
assert.ok(
packageJson.pi.extensions.includes("./pi-hermes-memory/src/index.ts"),
"the bundle must load the locally maintained Hermes memory extension",
);
// extensions/system-prompt.ts is the bundle's single system-prompt owner and
// must run after every tool-registration extension (including Tool Search)
// so it sees the complete tool set and any contributed sections.
assert.equal(
packageJson.pi.extensions.at(-1),
"./extensions/system-prompt.ts",
"the single system-prompt owner must be the last loaded extension",
);
// pi-overflow has two entry points with strict ordering: capture.ts must
// run first so it sees results before RTK rewrites them, and index.ts must
// run last (before system-prompt) so it bounds the final visible text.
assert.equal(
packageJson.pi.extensions.at(0),
"./pi-overflow/capture.ts",
"pi-overflow capture must be the first loaded extension",
);
assert.equal(packageJson.dependencies["pi-overflow"], "file:./pi-overflow");
assert.ok(filesIncludePackageDir(packageJson.files, "pi-overflow"), "the packed bundle must include pi-overflow source");
assert.equal(
packageJson.pi.extensions.at(-2),
"./pi-overflow/index.ts",
"pi-overflow bound stage must load after all other content extensions and before system-prompt",
);
assert.equal(
packageJson.dependencies["context-mode"],
undefined,
"context-mode was retired; new tool-result bounding is owned by pi-overflow",
);
assert.equal(
packageJson.pi.extensions.some((entry) => entry.includes("context-mode")),
false,
"no context-mode entry point may be loaded",
);
assert.ok(
!Array.isArray(packageJson.pi.skills) || packageJson.pi.skills.length === 0,
"pi.skills must be absent or empty now that the context-mode skills are gone",
);
assert.equal(
packageJson.pi.extensions.includes("./extensions/system-prompt-cover.ts"),
false,
"the retired system-prompt cover must not be loaded",
);
assert.equal(
packageJson.pi.extensions.includes("./extensions/tool-routing.ts"),
false,
"the retired tool-routing extension must not be loaded",
);
for (const retired of [
"extensions/system-prompt-cover.ts",
"extensions/system-prompt-cover-config.ts",
"extensions/tool-routing.ts",
]) {
assert.equal(
existsSync(join(repositoryRoot, retired)),
false,
`${retired} was superseded by extensions/system-prompt.ts and must be deleted`,
);
}
assert.equal(packageJson.dependencies["pi-hermes-memory"], "file:./pi-hermes-memory");
assert.ok(
filesIncludePackageDir(packageJson.files, "pi-hermes-memory"),
"the packed bundle must include the locally maintained Hermes memory source",
);
assert.ok(
packageJson.pi.extensions.indexOf("./pi-hermes-memory/src/index.ts") <
packageJson.pi.extensions.indexOf("./pi-compaction/index.ts"),
"the memory extension must register its session_before_compact flush before the compaction authority can cancel the event",
);
assert.equal(
packageJson.pi.extensions.some((entry) => entry.includes("hippo-memory-pi") || entry.includes("the-forge-flow")),
false,
"retired Hippo and third-party memory extensions must not be loaded",
);
assert.equal(packageJson.dependencies["@the-forge-flow/hippo-memory-pi"], undefined);
assert.equal(packageJson.dependencies["better-sqlite3"], undefined);
// better-sqlite3 was only present as context-mode's transitive build
// dependency; with context-mode retired the root lockfile must not carry a
// root-level better-sqlite3 and allowScripts must not approve one.
// pi-hermes-memory's own lockfile carries a nested major that ships N-API
// prebuilds and needs no approval entry.
const rootLock = JSON.parse(await readFile(join(repositoryRoot, "package-lock.json"), "utf8")) as {
packages: Record<string, { version?: string }>;
};
const hermesLock = JSON.parse(await readFile(join(repositoryRoot, "pi-hermes-memory", "package-lock.json"), "utf8")) as {
packages: Record<string, { version?: string }>;
};
const hermesSqliteVersion = hermesLock.packages["node_modules/better-sqlite3"]?.version;
assert.equal(
rootLock.packages["node_modules/better-sqlite3"],
undefined,
"the root lockfile must not pin a root-level better-sqlite3 now that context-mode is gone",
);
assert.match(hermesSqliteVersion ?? "", /^\d+\.\d+\.\d+$/, "the Hermes lockfile must pin its own better-sqlite3");
assert.deepEqual(
Object.keys(packageJson.allowScripts ?? {}).filter((key) => key.startsWith("better-sqlite3@")),
[],
"allowScripts must not approve a better-sqlite3 build without a root-level package",
);
assert.equal(packageJson.dependencies["@sinclair/typebox"], undefined);
const home = await mkdtemp(join(tmpdir(), "my-pi-extension-load-"));
const mockBin = join(home, "bin");
await mkdir(mockBin, { recursive: true });
// Probe loaded LAST: records every registered tool (name, exposure,
// description, source) and the final active set so the test can assert the
// pinned built-in codemode. Its async setup also runs the bash override's
// registered definition end to end — executing `echo hi` through the same
// delegation path a model call would use.
const probeOutput = join(home, "probe-tools.json");
const probePath = join(home, "probe.ts");
await writeFile(
probePath,
[
'import { writeFileSync } from "node:fs";',
`import bashOverrideExtension from ${JSON.stringify(join(repositoryRoot, "extensions", "bash-override.ts"))};`,
"export default async function (pi: any): Promise<void> {",
"\tlet executed: unknown;",
"\ttry {",
"\t\tlet registered: any;",
"\t\tbashOverrideExtension({ registerTool: (tool: any) => { registered = tool; } } as any);",
"\t\tconst result = await registered.execute(",
'\t\t\t"probe-1",',
'\t\t\t{ command: "echo hi" },',
"\t\t\tundefined,",
"\t\t\tundefined,",
`\t\t\t{ cwd: ${JSON.stringify(home)}, sessionManager: { getSessionId: () => "probe-session", getSessionFile: () => undefined } } as any,`,
"\t\t);",
"\t\texecuted = {",
"\t\t\ttext: result?.content?.[0]?.text,",
"\t\t\texitCode: result?.structuredContent?.exit_code,",
"\t\t\thasOutputSchema: result?.structuredContent !== undefined,",
"\t\t\tregisteredDescription: registered?.description,",
"\t\t};",
"\t} catch (error) {",
"\t\texecuted = { error: String(error) };",
"\t}",
'\tpi.on("session_start", () => {',
"\t\twriteFileSync(",
`\t\t\t${JSON.stringify(probeOutput)},`,
"\t\t\tJSON.stringify({",
"\t\t\t\tall: pi.getAllTools().map((tool: any) => ({",
"\t\t\t\t\tname: tool.name,",
"\t\t\t\t\texposure: tool.exposure,",
"\t\t\t\t\tdescription: tool.description,",
"\t\t\t\t\tsourcePath: tool.sourceInfo?.path,",
"\t\t\t\t})),",
"\t\t\t\tactive: pi.getActiveTools(),",
"\t\t\t\texecuted,",
"\t\t\t}),",
"\t\t);",
"\t});",
"}",
"",
].join("\n"),
);
const args = ["--no-extensions", "-e", "builtin:codemode"];
for (const entry of packageJson.pi.extensions) args.push("-e", resolve(repositoryRoot, entry));
args.push("-e", probePath, "--mode", "rpc");
const loadEnvironment = {
...process.env,
HOME: home,
XDG_CONFIG_HOME: join(home, ".config"),
PI_CODING_AGENT_DIR: join(home, ".pi-agent"),
PATH: `${mockBin}:${process.env.PATH ?? ""}`,
PI_OFFLINE: "1",
};
const runBundle = () => spawnSync("pi", args, {
encoding: "utf8",
timeout: 60_000,
env: loadEnvironment,
});
const result = runBundle();
assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`);
assert.doesNotMatch(`${result.stdout}\n${result.stderr}`, /Failed to load extension|conflicts with/u);
const probe = JSON.parse(await readFile(probeOutput, "utf8")) as {
all: { name: string; exposure?: string; description?: string; sourcePath?: string }[];
active: string[];
executed?: {
text?: string;
exitCode?: number;
hasOutputSchema?: boolean;
registeredDescription?: string;
error?: string;
};
};
const codemode = probe.all.find((tool) => tool.name === "codemode");
assert.equal(codemode?.exposure, "model-only", "Pi's built-in codemode must load with model-only exposure");
assert.ok(probe.active.includes("codemode"), "Tool Search must pin codemode into the active set");
assert.ok(
probe.all.every((tool) => tool.name !== "multi_grep"),
"FFF's upstream-flagged multi_grep must stay unregistered (fff-override deletes PI_FFF_MULTIGREP)",
);
assert.ok(
!probe.active.includes("multi_grep"),
"multi_grep must not appear in the active tool set",
);
const bash = probe.all.find((tool) => tool.name === "bash");
assert.ok(bash, "the bundle must expose the bash tool");
assert.ok(
bash.description?.startsWith("Execute a bash command in the current working directory."),
"the bash override must keep Pi's upstream description prefix",
);
assert.ok(
bash.description?.endsWith("which triggers a permission review."),
"the registered bash description must end with the tools.jsonc descriptionAppend",
);
assert.ok(
(bash.sourcePath ?? "").endsWith("extensions/bash-override.ts"),
`bash must be registered by extensions/bash-override.ts, got ${bash.sourcePath}`,
);
const executed = probe.executed ?? {};
assert.equal(executed.error, undefined, `bash override execute must not throw: ${executed.error}`);
assert.equal(executed.exitCode, 0, "echo hi must exit 0 through the delegated definition");
assert.match(executed.text ?? "", /hi/);
assert.equal(
executed.hasOutputSchema,
true,
"the bash override must keep the upstream outputSchema (structuredContent for codemode)",
);
assert.ok(
executed.registeredDescription?.includes("Not for searching files"),
"the catalog descriptionAppend must reach the registered bash description",
);
});