Files
my-pi/tests/extension-load.test.ts
T
云服务部-叶林立andDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com> 2682bd7fa7 feat: 组合包配置集中到 my-pi/ 目录并改为整体重建
- 组合包拥有、可随时重建的配置、日志与缓存统一放在
  <agentDir>/my-pi/:permission-system/(policy.jsonc、hard-guards.jsonc、
  logs/)、rtk-optimizer/config.json、tool-search/(config.json、
  catalog-v1.json)与 system-prompt-cover.md。hashline.json、lsp.json
  由外部扩展读取固定路径,是唯一例外。
- 新增 config/bundle-manifest.json 与 scripts/rebuild-bundle-config.mjs:
  先校验全部来源与目标,在临时目录构建完整树,再整体删除旧 my-pi/
  (符号链接只删链接)并原子替换,不备份。install.sh / update.sh 每次
  运行都调用它;扩展自身不再播种或迁移配置。
- update.sh 删除 pi update 失败后的 git clean 恢复逻辑:Pi 1.3.0 会在
  下一次 pi update 时依据中断标记自行恢复。同时删除 Tool Search
  settings.json 快照迁移与封面部署脚本。
- Tool Search 改为读取 my-pi/tool-search/config.json(源为
  config/tool-search.json,须与 BUNDLE_TOOL_SEARCH_DEFAULTS 一致),
  不再读写 settings.json 的 toolSearch 段,移除 bundleDefaultsVersion。
- 默认权限策略开启 debugLog。
- 旧位置的残留文件不迁移、不清理;AGENTS.md / README 同步新约束。

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

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-19 02:26:57 -07:00

205 lines
10 KiB
TypeScript

import assert from "node:assert/strict";
import { mkdir, mkdtemp, readFile } 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;
test("subagents source is retained but disabled in the root bundle", async () => {
const packageJson = JSON.parse(await readFile(join(repositoryRoot, "package.json"), "utf8")) as {
dependencies: Record<string, string>;
files: string[];
pi: { extensions: string[] };
};
assert.ok(packageJson.files.includes("pi-subagents"), "the bundle must retain the maintained subagents source");
assert.equal(packageJson.dependencies["@tintinweb/pi-subagents"], undefined);
assert.equal(
packageJson.pi.extensions.includes("./extensions/subagents.ts"),
false,
"the bundle must not default-load the disabled subagents wrapper",
);
});
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[] };
allowScripts: Record<string, boolean>;
};
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("./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(packageJson.files.includes("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.equal(packageJson.dependencies.ssh2, "1.17.0", "the packed bundle must install pi-ssh's runtime transport");
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 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(packageJson.files.includes("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(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(
packageJson.files.includes("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"], undefined);
assert.equal(packageJson.pi.extensions.includes("./extensions/subagents.ts"), false);
assert.ok(
packageJson.pi.extensions.includes("./pi-hermes-memory/src/index.ts"),
"the bundle must load the locally maintained Hermes memory extension",
);
// The cover must rewrite the prompt before tool-routing flattens it into a forced prompt;
// once flattened, later handlers cannot restore the section structure.
assert.ok(
packageJson.pi.extensions.includes("./extensions/system-prompt-cover.ts"),
"the bundle must load the system prompt cover extension",
);
assert.ok(
packageJson.pi.extensions.indexOf("./extensions/system-prompt-cover.ts") <
packageJson.pi.extensions.indexOf("./extensions/tool-routing.ts"),
"the system prompt cover must run before tool-routing forces the prompt",
);
assert.equal(packageJson.dependencies["pi-hermes-memory"], "file:./pi-hermes-memory");
assert.ok(
packageJson.files.includes("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);
assert.equal(packageJson.allowScripts["better-sqlite3@12.11.1"], true);
assert.equal(
packageJson.allowScripts["better-sqlite3@13.0.3"],
undefined,
"Hermes uses better-sqlite3 13 N-API prebuilds, so no extra install-script approval is required",
);
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 });
const args = ["--no-extensions"];
for (const entry of packageJson.pi.extensions) args.push("-e", resolve(repositoryRoot, entry));
args.push("--mode", "rpc");
const loadEnvironment = {
...process.env,
HOME: home,
XDG_CONFIG_HOME: join(home, ".config"),
PI_CODING_AGENT_DIR: join(home, ".pi-agent"),
MY_PI_SEARCH_CONFIG: join(home, "missing-search.env"),
PATH: `${mockBin}:${process.env.PATH ?? ""}`,
PI_OFFLINE: "1",
TAVILY_API_KEY: "",
EXA_API_KEY: "",
KEENABLE_API_KEY: "",
};
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);
});