feat(tool-search): expand dynamic group activation

This commit is contained in:
云服务部-叶林立
2026-08-28 12:05:11 +08:00
parent bf874455db
commit 2dcec0207c
15 changed files with 897 additions and 119 deletions
@@ -0,0 +1,206 @@
import assert from "node:assert/strict";
import { chmod, copyFile, mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
import { spawnSync } from "node:child_process";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import {
CURRENT_TOOL_SEARCH_DEFAULTS,
LEGACY_TOOL_SEARCH_DEFAULTS,
migrateToolSearchSettings,
} from "../scripts/migrate-tool-search-settings.mjs";
const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
async function fixture(t: test.TestContext): Promise<{ root: string; settingsPath: string }> {
const root = await mkdtemp(join(tmpdir(), "my-pi-tool-search-migration-"));
t.after(() => rm(root, { recursive: true, force: true }));
return { root, settingsPath: join(root, "settings.json") };
}
test("migrates only the complete legacy default snapshot with a private backup", async (t) => {
const { root, settingsPath } = await fixture(t);
const original = {
theme: "dark",
toolSearch: LEGACY_TOOL_SEARCH_DEFAULTS,
unrelated: { keep: true },
};
await writeFile(settingsPath, `${JSON.stringify(original, null, 2)}\n`, "utf8");
await chmod(settingsPath, 0o644);
const result = await migrateToolSearchSettings(settingsPath, { now: new Date("2026-08-28T12:34:56.789Z") });
assert.equal(result.status, "migrated");
assert.equal(result.backupPath, `${settingsPath}.bak.2026-08-28T12-34-56-789Z`);
assert.deepEqual(JSON.parse(await readFile(settingsPath, "utf8")), {
...original,
toolSearch: CURRENT_TOOL_SEARCH_DEFAULTS,
});
assert.deepEqual(JSON.parse(await readFile(result.backupPath!, "utf8")), original);
assert.equal((await stat(settingsPath)).mode & 0o777, 0o600);
assert.equal((await stat(result.backupPath!)).mode & 0o777, 0o600);
assert.deepEqual((await readdir(root)).sort(), [
"settings.json",
"settings.json.bak.2026-08-28T12-34-56-789Z",
]);
});
test("does not remove a pre-existing backup when the timestamp collides", async (t) => {
const { settingsPath } = await fixture(t);
const now = new Date("2026-08-28T12:34:56.789Z");
const backupPath = `${settingsPath}.bak.2026-08-28T12-34-56-789Z`;
const original = JSON.stringify({ toolSearch: LEGACY_TOOL_SEARCH_DEFAULTS });
await writeFile(settingsPath, original, "utf8");
await writeFile(backupPath, "existing backup\n", "utf8");
await assert.rejects(migrateToolSearchSettings(settingsPath, { now }), { code: "EEXIST" });
assert.equal(await readFile(backupPath, "utf8"), "existing backup\n");
assert.equal(await readFile(settingsPath, "utf8"), original);
});
test("preserves custom Tool Search settings even when they retain the old capacities", async (t) => {
const { root, settingsPath } = await fixture(t);
const custom = {
toolSearch: {
...LEGACY_TOOL_SEARCH_DEFAULTS,
alwaysEnabled: [...LEGACY_TOOL_SEARCH_DEFAULTS.alwaysEnabled, "custom_tool"],
},
};
const original = `${JSON.stringify(custom, null, 2)}\n`;
await writeFile(settingsPath, original, "utf8");
assert.deepEqual(await migrateToolSearchSettings(settingsPath), { status: "custom" });
assert.equal(await readFile(settingsPath, "utf8"), original);
assert.deepEqual(await readdir(root), ["settings.json"]);
});
test("recognizes current defaults and skips files without a Tool Search section", async (t) => {
const current = await fixture(t);
await writeFile(current.settingsPath, JSON.stringify({ toolSearch: CURRENT_TOOL_SEARCH_DEFAULTS }), "utf8");
assert.deepEqual(await migrateToolSearchSettings(current.settingsPath), { status: "current" });
const unversioned = await fixture(t);
const { bundleDefaultsVersion: _version, ...runtimeDefaults } = CURRENT_TOOL_SEARCH_DEFAULTS;
await writeFile(unversioned.settingsPath, JSON.stringify({ toolSearch: runtimeDefaults }), "utf8");
assert.deepEqual(await migrateToolSearchSettings(unversioned.settingsPath), { status: "current" });
const absent = await fixture(t);
await writeFile(absent.settingsPath, JSON.stringify({ theme: "dark" }), "utf8");
assert.deepEqual(await migrateToolSearchSettings(absent.settingsPath), { status: "missing-section" });
});
test("fails safely for invalid JSON and reports a missing settings file", async (t) => {
const invalid = await fixture(t);
await writeFile(invalid.settingsPath, "{ invalid\n", "utf8");
await assert.rejects(migrateToolSearchSettings(invalid.settingsPath), /invalid JSON/);
assert.equal(await readFile(invalid.settingsPath, "utf8"), "{ invalid\n");
const missing = await fixture(t);
assert.deepEqual(await migrateToolSearchSettings(missing.settingsPath), { status: "missing" });
});
test("aborts when settings change before the atomic replacement", async (t) => {
const { root, settingsPath } = await fixture(t);
await writeFile(settingsPath, JSON.stringify({ toolSearch: LEGACY_TOOL_SEARCH_DEFAULTS }), "utf8");
const competing = `${JSON.stringify({ toolSearch: { ...LEGACY_TOOL_SEARCH_DEFAULTS, maxActiveGroups: 4 } })}\n`;
await assert.rejects(
migrateToolSearchSettings(settingsPath, {
beforeCommit: () => writeFile(settingsPath, competing, "utf8"),
}),
/changed during migration/,
);
assert.equal(await readFile(settingsPath, "utf8"), competing);
const files = await readdir(root);
assert.equal(files.filter((name) => name.includes(".bak.")).length, 1);
assert.equal(files.some((name) => name.endsWith(".tmp")), false);
});
async function updateFixture(t: test.TestContext, piExitStatus = 0) {
const root = await mkdtemp(join(tmpdir(), "my-pi-tool-search-update-"));
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 settingsPath = join(agentDir, "settings.json");
const log = join(root, "pi.log");
const updatedBundle = join(root, "updated-bundle");
await mkdir(bin, { recursive: true });
await mkdir(agentDir, { recursive: true });
await mkdir(join(updatedBundle, "config"), { recursive: true });
await mkdir(join(updatedBundle, "scripts"), { recursive: true });
await copyFile(join(repositoryRoot, "config", "p10k.zsh"), join(updatedBundle, "config", "p10k.zsh"));
await copyFile(
join(repositoryRoot, "config", "hippo-memory-version"),
join(updatedBundle, "config", "hippo-memory-version"),
);
await copyFile(
join(repositoryRoot, "scripts", "migrate-tool-search-settings.mjs"),
join(updatedBundle, "scripts", "migrate-tool-search-settings.mjs"),
);
await writeFile(
join(bin, "pi"),
`#!/bin/sh\nprintf 'pi %s\\n' "$*" >> "$MOCK_PI_LOG"\nexit ${piExitStatus}\n`,
"utf8",
);
await chmod(join(bin, "pi"), 0o755);
await writeFile(settingsPath, JSON.stringify({ toolSearch: LEGACY_TOOL_SEARCH_DEFAULTS }), "utf8");
return {
settingsPath,
log,
env: {
...process.env,
HOME: home,
XDG_CONFIG_HOME: join(home, ".config"),
PI_CODING_AGENT_DIR: agentDir,
PI_PACKAGE_SOURCE: updatedBundle,
MOCK_PI_LOG: log,
PATH: `${bin}:${dirname(process.execPath)}:/usr/bin:/bin`,
ZSH: "",
ZSH_CUSTOM: "",
ZDOTDIR: home,
},
};
}
test("update.sh migrates the exact legacy snapshot after a successful package update", async (t) => {
const fixture = await updateFixture(t);
const result = spawnSync("sh", [join(repositoryRoot, "update.sh")], {
cwd: repositoryRoot,
env: fixture.env,
encoding: "utf8",
timeout: 30_000,
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(await readFile(fixture.log, "utf8"), /pi update .*updated-bundle/);
assert.match(result.stdout, /旧版默认配置已迁移/);
assert.deepEqual(JSON.parse(await readFile(fixture.settingsPath, "utf8")).toolSearch, CURRENT_TOOL_SEARCH_DEFAULTS);
assert.equal((await stat(fixture.settingsPath)).mode & 0o777, 0o600);
});
test("update.sh does not migrate settings when pi update fails", async (t) => {
const fixture = await updateFixture(t, 1);
const before = await readFile(fixture.settingsPath, "utf8");
const result = spawnSync("sh", [join(repositoryRoot, "update.sh")], {
cwd: repositoryRoot,
env: fixture.env,
encoding: "utf8",
timeout: 30_000,
});
assert.equal(result.status, 1);
assert.equal(await readFile(fixture.settingsPath, "utf8"), before);
assert.doesNotMatch(result.stdout, /旧版默认配置已迁移/);
});
test("update.sh preserves settings when the updated bundle helper cannot be located", async (t) => {
const fixture = await updateFixture(t);
const before = await readFile(fixture.settingsPath, "utf8");
const result = spawnSync("sh", [join(repositoryRoot, "update.sh")], {
cwd: repositoryRoot,
env: { ...fixture.env, PI_PACKAGE_SOURCE: "unsupported:my-pi" },
encoding: "utf8",
timeout: 30_000,
});
assert.equal(result.status, 1);
assert.equal(await readFile(fixture.settingsPath, "utf8"), before);
assert.match(result.stderr, /无法定位升级后组合包中的 Tool Search/);
});