Files
my-pi/tests/update-git-clean-recovery.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

161 lines
5.3 KiB
TypeScript

import assert from "node:assert/strict";
import { chmod, copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { spawnSync } from "node:child_process";
import test from "node:test";
import { fileURLToPath } from "node:url";
const repositoryRoot = dirname(dirname(fileURLToPath(import.meta.url)));
async function fixture(
t: test.TestContext,
options: { createMarker?: boolean; origin?: string; secondPiStatus?: number } = {},
) {
const root = await mkdtemp(join(tmpdir(), "my-pi-update-clean-recovery-"));
t.after(() => rm(root, { recursive: true, force: true }));
const home = join(root, "home");
const bin = join(root, "bin");
const agentDir = join(home, ".pi", "agent");
const target = join(agentDir, "git", "bitbucket.org", "siakitem", "my-pi");
const marker = join(agentDir, "git", "bitbucket.org", "siakitem", ".my-pi.pi-update-incomplete");
const piLog = join(root, "pi.log");
const piCount = join(root, "pi.count");
const gitLog = join(root, "git.log");
const cleanCount = join(root, "clean.count");
await mkdir(join(target, ".git"), { recursive: true });
await mkdir(join(target, "config"), { recursive: true });
await copyFile(join(repositoryRoot, "config", "p10k.zsh"), join(target, "config", "p10k.zsh"));
await copyFile(
join(repositoryRoot, "config", "pi-hashline-edit.json"),
join(target, "config", "pi-hashline-edit.json"),
);
await mkdir(bin, { recursive: true });
await writeFile(
join(bin, "pi"),
`#!/bin/sh
count=0
[ ! -f "$MOCK_PI_COUNT" ] || count=$(cat "$MOCK_PI_COUNT")
count=$((count + 1))
printf '%s\n' "$count" > "$MOCK_PI_COUNT"
printf 'pi %s\n' "$*" >> "$MOCK_PI_LOG"
if [ "$count" -eq 1 ]; then
if [ "$MOCK_CREATE_MARKER" = 1 ]; then
: > "$MOCK_MARKER"
fi
exit 1
fi
if [ "$MOCK_SECOND_PI_STATUS" -eq 0 ]; then
rm -f "$MOCK_MARKER"
fi
exit "$MOCK_SECOND_PI_STATUS"
`,
"utf8",
);
await chmod(join(bin, "pi"), 0o755);
await writeFile(
join(bin, "git"),
`#!/bin/sh
printf 'git %s\n' "$*" >> "$MOCK_GIT_LOG"
case "$*" in
*" rev-parse --show-toplevel")
printf '%s\n' "$MOCK_GIT_TARGET"
;;
*" remote get-url origin")
printf '%s\n' "$MOCK_GIT_ORIGIN"
;;
*" clean -fdx")
count=0
[ ! -f "$MOCK_CLEAN_COUNT" ] || count=$(cat "$MOCK_CLEAN_COUNT")
count=$((count + 1))
printf '%s\n' "$count" > "$MOCK_CLEAN_COUNT"
if [ "$count" -eq 1 ]; then
printf '%s\n' 'warning: failed to remove node_modules/: Directory not empty' >&2
exit 1
fi
;;
esac
`,
"utf8",
);
await chmod(join(bin, "git"), 0o755);
await writeFile(join(bin, "uname"), "#!/bin/sh\nprintf '%s\\n' Linux\n", "utf8");
await chmod(join(bin, "uname"), 0o755);
const env = {
...process.env,
HOME: home,
XDG_CONFIG_HOME: join(home, ".config"),
PI_CODING_AGENT_DIR: agentDir,
MOCK_CREATE_MARKER: options.createMarker === false ? "0" : "1",
MOCK_MARKER: marker,
MOCK_PI_COUNT: piCount,
MOCK_PI_LOG: piLog,
MOCK_SECOND_PI_STATUS: String(options.secondPiStatus ?? 0),
MOCK_GIT_LOG: gitLog,
MOCK_CLEAN_COUNT: cleanCount,
MOCK_GIT_TARGET: target,
MOCK_GIT_ORIGIN: options.origin ?? "git@bitbucket.org:siakitem/my-pi.git",
PATH: `${bin}:${dirname(process.execPath)}:/usr/bin:/bin`,
ZSH: "",
ZSH_CUSTOM: "",
ZDOTDIR: home,
};
delete env.PI_PACKAGE_SOURCE;
return { cleanCount, env, gitLog, marker, piCount, piLog };
}
test("update.sh retries an identity-checked interrupted Git cleanup before retrying pi update", async (t) => {
const f = await fixture(t);
const result = spawnSync("sh", [join(repositoryRoot, "update.sh")], {
cwd: repositoryRoot,
env: f.env,
encoding: "utf8",
timeout: 30_000,
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.equal((await readFile(f.piCount, "utf8")).trim(), "2");
assert.equal((await readFile(f.cleanCount, "utf8")).trim(), "2");
assert.match(result.stdout, /检测到默认组合包的 Pi Git 升级中断/);
assert.match(result.stdout, /第 2 次尝试后完成/);
await assert.rejects(readFile(f.marker, "utf8"), { code: "ENOENT" });
});
test("update.sh keeps the original fail-fast behavior when no update marker exists", async (t) => {
const f = await fixture(t, { createMarker: false });
const result = spawnSync("sh", [join(repositoryRoot, "update.sh")], {
cwd: repositoryRoot,
env: f.env,
encoding: "utf8",
timeout: 30_000,
});
assert.equal(result.status, 1);
assert.equal((await readFile(f.piCount, "utf8")).trim(), "1");
await assert.rejects(readFile(f.gitLog, "utf8"), { code: "ENOENT" });
assert.match(result.stderr, /pi update 执行失败/);
});
test("update.sh refuses cleanup when the cached checkout origin does not match", async (t) => {
const f = await fixture(t, { origin: "git@example.invalid:other/package.git" });
const result = spawnSync("sh", [join(repositoryRoot, "update.sh")], {
cwd: repositoryRoot,
env: f.env,
encoding: "utf8",
timeout: 30_000,
});
assert.equal(result.status, 1);
assert.equal((await readFile(f.piCount, "utf8")).trim(), "1");
const gitLog = await readFile(f.gitLog, "utf8");
assert.doesNotMatch(gitLog, /clean -fdx/);
assert.match(result.stderr, /checkout 身份校验失败/);
});