Files
my-pi/tests/update-git-clean-recovery.test.ts
T
云服务部-叶林立 dec48dab6d feat: 新增系统提示词封面,并修正工具结果投影与常驻工具
系统提示词封面(新增能力):

- extensions/system-prompt-cover.ts:读取 ~/.pi/agent/SYSTEM_COVER.md,
  在 before_agent_start 返回 { systemPrompt },用文本拼接替换 Pi 默认
  系统提示词的开头,同时保留 tools / rules / docs / skills /
  project_context / cwd 全部分区及其原始顺序。
- 必须走 forceSystemPrompt 投影路径。customPrompt 选项会连带抑制
  tools / rules / docs 三个分区,而回填只能追加到末尾,无法恢复原顺序;
  且 forceSystemPrompt 优先级更高,二者同时存在时 customPrompt 会被
  静默忽略,因此 handler 不得写 customPrompt。
- 分割边界要求独占一行的裸标签:<project_instructions path="..."> 嵌套在
  <project_context> 内,只有裸标签才是分区边界。
- 注册在 tool-routing 之前,否则提示词已被压平,改 systemPromptOptions
  不再影响本轮请求。
- scripts/deploy-system-prompt-cover.mjs:同目录临时文件 + rename 原子
  覆盖,不保留备份;install.sh 从脚本目录、update.sh 从升级后组合包
  定位 helper,禁止回退到可能陈旧的脚本目录。
- 封面缺失、不可读或空白时静默 no-op,Pi 行为保持不变;封面每轮重新
  读取,修改后在下一轮生效,不影响已结束的历史对话。

pi-compaction 投影修正:

- 仅在「摘要 + Source 行」严格短于原文时才替换工具结果。此前命中即
  无条件替换,小结果反而被包壳放大;未替换的结果完整保留原始 content
  parts(含非文本部分)。
- deterministic.maxStubTokens 由 256 提升到 1024。

pi-tool-search 常驻工具:

- alwaysEnabled 增加 context_tree_query:恢复被投影的工具证据不应依赖
  一次 tool_search 激活,常驻同时保持请求前缀稳定。
- BUNDLE_TOOL_SEARCH_DEFAULTS_VERSION 由 3 提升到 4;
  scripts/migrate-tool-search-settings.mjs 新增 version-3 快照常量,
  使其与 version-2 一样可被完整精确匹配并迁移。
- 权限策略为 context_tree_query 显式 allow:它只读扩展自己的 segment
  索引、不触碰工作区,常驻后不应每轮触发 AutoReview 往返。该种子仅在
  全新安装时部署,已存在的用户 policy.jsonc 不会被覆盖。

文档与测试同步:

- AGENTS.md:新增 system-prompt-cover 开发约定,修正加载顺序、
  bundleDefaultsVersion、可迁移旧快照集合与常驻工具范围的描述。
- pi-tool-search 的 README / CHANGELOG 同步 version 4 与新增常驻工具。
- tests/system-prompt-cover.test.ts:覆盖顺序保持、尾部逐字节不变、
  嵌套 project_instructions 不误判、部署幂等与覆盖、缺源时非零退出。
- 修正 pi-tool-search/test/config.test.ts 与
  tests/tool-search-settings-migration.test.ts 中写死的 version 3 与
  工具列表断言。
- update-git-clean-recovery / tool-search-settings-migration 的 mock
  组合包补拷 scripts/,与真实发布包结构一致,避免 helper 定位失败。

测试:根 61/61、pi-compaction 36/36 + tsc、pi-tool-search 16/16 + tsc、
pi-permission-system 2577/2577;sh -n 与 git diff --check 通过。
2026-10-15 18:02:03 -07:00

172 lines
5.8 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"),
);
// A real released bundle also ships `scripts/`; the update flow locates its helpers
// there rather than falling back to a possibly stale script directory.
await mkdir(join(target, "scripts"), { recursive: true });
await copyFile(
join(repositoryRoot, "config", "system-prompt-cover.md"),
join(target, "config", "system-prompt-cover.md"),
);
await copyFile(
join(repositoryRoot, "scripts", "deploy-system-prompt-cover.mjs"),
join(target, "scripts", "deploy-system-prompt-cover.mjs"),
);
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 身份校验失败/);
});