Files
my-pi/tests/extension-load.test.ts
T
云服务部-叶林立 73f3117a7e feat(pi-hermes-memory): 接入持久记忆扩展并移除 Hippo
用仓库内 pi-hermes-memory 替换 hippo-memory-pi,清理 Hippo 在组合包中的
全部注入点。

移除 Hippo:
- 删除 hippo-memory-pi/ 与 config/hippo-memory-version。
- extensions/plugin-init.ts 只保留 CodeGraph 初始化目标与预检。
- install.sh / update.sh / uninstall.sh 移除 Hippo CLI 的安装、升级、卸载
  与版本校验逻辑;uninstall.sh 同时删除仅服务于该提示的死代码 ask_yes_no。
- 删除 tests/hippo-extension.test.ts 与 tests/hippo-scripts.test.ts。
- tests/extension-load.test.ts 反转“不得加载 Hermes”的断言为必须加载,
  并断言其排在 pi-compaction 之前。

接入 pi-hermes-memory:
- package.json 增加 file:./pi-hermes-memory 依赖、files 条目与加载入口;
  加载顺序位于 pi-compaction 之前,否则 flushOnCompact 会被 pi-compaction
  的 { cancel: true } 静默跳过。
- 六个记忆工具在 Tool Search 中常驻:memory_search 与 session_search 连同
  context_tree_query 归入 memory-recall,memory_add / memory_replace /
  memory_remove / skill_manage 归入 memory-management。常驻是必需的——注入
  的 memory policy 会在普通工作中指示模型调用它们,schema 不可见会让模型
  静默放弃而不是去 tool_search,同时常驻也保持请求前缀缓存稳定。
- bundleDefaultsVersion 由 2 升到 3;migrate-tool-search-settings.mjs 新增
  上一版快照到当前版本的迁移路径,仍只迁移完整匹配的默认快照并保留自定义
  配置。
- config/pi-permission-policy.jsonc 为六个工具设置 allow:它们只写扩展自己的
  agent 目录数据、不触碰工作区,且 memory_search 每轮都可能被调用,落入
  默认 ask 会产生每轮 AutoReview 往返。

依赖:
- pi-hermes-memory 的 devDependencies 由上游 ^0.80.2 等范围改为与根包一致的
  精确版本。上游范围与根包 0.84.2 不兼容,会让 npm 在本目录嵌套安装第二套
  Pi runtime(实测 229 MB),违反“不得在组合包内再安装一套 Pi runtime”的
  纪律。
- 根 allowScripts 保持只有 better-sqlite3@12.11.1。hermes 的
  better-sqlite3@13 运行时 dlopen 的是包内 N-API 预编译二进制
  prebuilds/<platform>.node,npm 因 binding.gyp 推断出的 node-gyp rebuild
  无需执行;实测删除 build/ 目录后仍正常工作。

文档:AGENTS.md 与 README.md 改写记忆章节,新增 pi-hermes-memory 开发约定,
替换文末的 Hippo 项目记忆块。

验证:根 12 个测试文件、pi-tool-search 的 typecheck/test/build、
pi-permission-system 的 typecheck/2569 测试、pi-hermes-memory 自带的 59 个
测试文件全部通过;隔离 Pi 环境联合加载无错误并生成 bundleDefaultsVersion 3;
npm install 幂等;迁移 helper 对 v2 快照迁移、对自定义配置保留。
2026-10-14 00:56:41 -07:00

236 lines
12 KiB
TypeScript

import assert from "node:assert/strict";
import { mkdir, mkdtemp, readFile, rm, 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;
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.ok(
packageJson.pi.extensions.includes("./pi-extension-codex-fast-mode/index.ts"),
"the bundle must load the locally maintained Codex Fast mode extension",
);
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.equal(
packageJson.dependencies["@firstpick/pi-extension-codex-fast-mode"],
"file:./pi-extension-codex-fast-mode",
);
assert.ok(
packageJson.files.includes("pi-extension-codex-fast-mode"),
"the packed bundle must include the locally maintained Codex Fast mode source",
);
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",
);
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);
const deployedPermissionPolicy = await readFile(
join(home, ".pi-agent", "extensions", "pi-permission-system", "policy.jsonc"),
"utf8",
);
const deployedHardGuards = await readFile(
join(home, ".pi-agent", "extensions", "pi-permission-system", "hard-guards.jsonc"),
"utf8",
);
assert.equal(deployedPermissionPolicy, permissionPolicySource, "the seeded policy must match the bundle source");
assert.equal(deployedHardGuards, hardGuardSource, "the seeded hard guards must match the bundle source");
const deployedConfigDir = join(home, ".pi-agent", "extensions", "pi-permission-system");
const deployedPolicyPath = join(deployedConfigDir, "policy.jsonc");
const deployedHardGuardPath = join(deployedConfigDir, "hard-guards.jsonc");
const customPolicy = `{ "permission": { "*": "deny" } }\n`;
const customHardGuards = `{ "version": 1, "permission": { "*": "allow" } }\n`;
await writeFile(deployedPolicyPath, customPolicy, "utf8");
await writeFile(deployedHardGuardPath, customHardGuards, "utf8");
const preserveResult = runBundle();
assert.equal(preserveResult.status, 0, `${preserveResult.stdout}\n${preserveResult.stderr}`);
assert.equal(await readFile(deployedPolicyPath, "utf8"), customPolicy, "reload must preserve user policy edits");
assert.equal(await readFile(deployedHardGuardPath, "utf8"), customHardGuards, "reload must preserve user hard guards");
await rm(deployedPolicyPath);
const legacyPolicy = `{ "authorizerChain": ["auto-review"], "permission": { "*": "ask" } }\n`;
await writeFile(join(deployedConfigDir, "config.json"), legacyPolicy, "utf8");
const migrationResult = runBundle();
assert.equal(migrationResult.status, 0, `${migrationResult.stdout}\n${migrationResult.stderr}`);
assert.equal(await readFile(deployedPolicyPath, "utf8"), legacyPolicy, "legacy config.json must seed policy.jsonc once");
assert.equal(await readFile(deployedHardGuardPath, "utf8"), customHardGuards, "legacy migration must not replace hard guards");
});