mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-10-26 10:45:22 +00:00
feat: 移除 Codex Fast mode 与 AutoReview 后台分析,模型改为配置驱动
移除 pi-extension-codex-fast-mode:
- 删除整个扩展目录;同步清理根 package.json 的 files / pi.extensions /
dependencies 条目、package-lock.json、tests/extension-load.test.ts 断言
与文档章节。
- pi-minimal-footer 的 Fast on 渲染是该扩展的唯一消费者。扩展移除后 accent
分支不再有生产者,collectFooterStatusItems 简化为直接返回非空状态字符串,
去掉 FooterStatusItem 与 accent 标志,避免留下死代码。
移除 500 次裁决后的后台分析体系:
- 删除 analysis-agent.ts(192 行)与 ledger.ts(296 行),以及对应的
auto-review-runtime / auto-review-ledger 测试。
- runtime.ts 由 177 行收敛到 85 行:安装选项缩为 { service, config };删除
handleAgentSettled 与 agent_settled 注册;保留 session_start / turn_start
(denial circuit breaker 属安全语义)/ session_shutdown。
- 删除 analysis 配置键:config、extension-config、config-schema、生成的
permissions.schema.json 与种子配置。
- 删除 reviewer 的 onDecision 回调(唯一消费者是 ledger)。
- 删除迁移机制 scripts/migrate-permission-policy.mjs、其测试与 update.sh 中
46 行 migrate_permission_policy 接线。种子部署保持“目标缺失才写入”,已存在
的 policy.jsonc 永不被覆盖。
AutoReview 模型改为纯配置驱动:
- 删除 DEFAULT_PROVIDER / DEFAULT_MODEL 常量与 findCodexTemplate 合成逻辑;
model.ts 由 65 行收敛到 48 行。
- provider / model 任一缺失时返回新增失败类别 model-unconfigured 并 defer 到
人工审批;provider 未注册或模型未发布同样 defer。
- reasoning 不再有默认常量,完全由配置决定。
- session_start 在 auto-review 已列入 authorizerChain 且 provider/model 未配齐
时 ui.notify 提示一次,避免不使用 AutoReview 的用户被打扰。
- 种子 autoReview 只保留 enabled,provider/model/reasoning 由用户按本机实际
可用的模型填写。
破坏性变更:analysis 键已从 strictObject schema 删除,任何仍带该键的
policy.jsonc 会使整份配置被拒(fail-closed 到全 ask)。这是刻意的变更,已由
config-loader 测试固定,防止将来无声放宽。
测试:
- 新增 auto-review-model.test.ts(7 例),覆盖此前完全缺失的模型解析:真实
命中、未配置、provider 未知、模型未发布,以及不再合成替代模型。
- 重写 auto-review-runtime.test.ts(6 例):authorizer 注册、未配置时提示
一次、已配置 / disabled / 无 UI / 不在 chain 时静默。
- config-loader.test.ts 新增 2 例:带 analysis 的旧配置被拒且 fail-closed、
autoReview 逐字段合并。
- composition-root.test.ts 的 EXPECTED_HANDLERS 移除 agent_settled。
文档:AGENTS.md、README.md、pi-permission-system 的 README / CHANGELOG /
configuration.md / architecture.md / auto-review UPSTREAM.md。
验证:pi-permission-system 的 typecheck、2577 个测试与 build 全部通过;根 50
个测试通过;schema 重新生成无漂移;新种子通过 schema 校验;5 个脚本 sh -n
通过;git diff --check 无空白问题。未安装 shellcheck;未执行真实 pi install /
update(属外部写操作)。
This commit is contained in:
@@ -1,236 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { createHash } from "node:crypto";
|
||||
import { constants } from "node:fs";
|
||||
import {
|
||||
chmod,
|
||||
open,
|
||||
readFile,
|
||||
rename,
|
||||
unlink,
|
||||
} from "node:fs/promises";
|
||||
import { basename, dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
/** Semantic hash of the previously installed personal policy snapshot. */
|
||||
export const LEGACY_PERMISSION_POLICY_HASHES = new Set([
|
||||
"9abf020e80fceae8c52de3e8b758822dfc86cb90e9083381e2569dde1e29400a",
|
||||
]);
|
||||
|
||||
function stripJsonComments(text) {
|
||||
let output = "";
|
||||
let inString = false;
|
||||
let escaped = false;
|
||||
let lineComment = false;
|
||||
let blockComment = false;
|
||||
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
const character = text[index];
|
||||
const next = text[index + 1];
|
||||
if (lineComment) {
|
||||
if (character === "\n") {
|
||||
lineComment = false;
|
||||
output += character;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (blockComment) {
|
||||
if (character === "*" && next === "/") {
|
||||
blockComment = false;
|
||||
index += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (inString) {
|
||||
output += character;
|
||||
if (escaped) escaped = false;
|
||||
else if (character === "\\") escaped = true;
|
||||
else if (character === '"') inString = false;
|
||||
continue;
|
||||
}
|
||||
if (character === '"') {
|
||||
inString = true;
|
||||
output += character;
|
||||
} else if (character === "/" && next === "/") {
|
||||
lineComment = true;
|
||||
index += 1;
|
||||
} else if (character === "/" && next === "*") {
|
||||
blockComment = true;
|
||||
index += 1;
|
||||
} else {
|
||||
output += character;
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
function stripTrailingCommas(text) {
|
||||
let output = "";
|
||||
let inString = false;
|
||||
let escaped = false;
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
const character = text[index];
|
||||
if (inString) {
|
||||
output += character;
|
||||
if (escaped) escaped = false;
|
||||
else if (character === "\\") escaped = true;
|
||||
else if (character === '"') inString = false;
|
||||
continue;
|
||||
}
|
||||
if (character === '"') {
|
||||
inString = true;
|
||||
output += character;
|
||||
continue;
|
||||
}
|
||||
if (character === ",") {
|
||||
let lookahead = index + 1;
|
||||
while (/\s/.test(text[lookahead] ?? "")) lookahead += 1;
|
||||
if (text[lookahead] === "}" || text[lookahead] === "]") continue;
|
||||
}
|
||||
output += character;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
export function parseJsonc(text, label = "policy.jsonc") {
|
||||
try {
|
||||
return JSON.parse(stripTrailingCommas(stripJsonComments(text)));
|
||||
} catch (error) {
|
||||
throw new Error(`${label} is invalid JSONC: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function sortJson(value) {
|
||||
if (Array.isArray(value)) return value.map(sortJson);
|
||||
if (value !== null && typeof value === "object") {
|
||||
return Object.fromEntries(
|
||||
Object.keys(value)
|
||||
.sort()
|
||||
.map((key) => [key, sortJson(value[key])]),
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function permissionPolicySemanticHash(text) {
|
||||
const parsed = parseJsonc(text);
|
||||
const canonical = JSON.stringify(sortJson(parsed));
|
||||
return createHash("sha256").update(canonical).digest("hex");
|
||||
}
|
||||
|
||||
function backupTimestamp(now) {
|
||||
return now.toISOString().replace(/[:.]/g, "-");
|
||||
}
|
||||
|
||||
async function readFileSnapshot(path) {
|
||||
const noFollow = constants.O_NOFOLLOW ?? 0;
|
||||
const handle = await open(path, constants.O_RDONLY | noFollow);
|
||||
try {
|
||||
const info = await handle.stat();
|
||||
if (!info.isFile()) throw new Error(`policy path is not a regular file: ${path}`);
|
||||
return { content: await handle.readFile("utf8"), dev: info.dev, ino: info.ino };
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function writeExclusive(path, content) {
|
||||
const handle = await open(
|
||||
path,
|
||||
constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL,
|
||||
0o600,
|
||||
);
|
||||
try {
|
||||
await handle.writeFile(content, "utf8");
|
||||
await handle.sync();
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
await chmod(path, 0o600);
|
||||
}
|
||||
|
||||
export async function migratePermissionPolicy(
|
||||
policyPath,
|
||||
seedPath,
|
||||
options = {},
|
||||
) {
|
||||
const now = options.now ?? new Date();
|
||||
const legacyHashes = new Set(
|
||||
options.legacyHashes ?? LEGACY_PERMISSION_POLICY_HASHES,
|
||||
);
|
||||
let original;
|
||||
try {
|
||||
original = await readFileSnapshot(policyPath);
|
||||
} catch (error) {
|
||||
if (error?.code === "ENOENT") return { status: "missing" };
|
||||
throw error;
|
||||
}
|
||||
|
||||
const seedContent = await readFile(seedPath, "utf8");
|
||||
parseJsonc(seedContent, "bundled permission policy seed");
|
||||
const currentHash = permissionPolicySemanticHash(original.content);
|
||||
const seedHash = permissionPolicySemanticHash(seedContent);
|
||||
if (currentHash === seedHash) {
|
||||
await chmod(policyPath, 0o600);
|
||||
return { status: "current" };
|
||||
}
|
||||
if (!legacyHashes.has(currentHash)) return { status: "custom" };
|
||||
|
||||
const backupPath = `${policyPath}.bak.${backupTimestamp(now)}`;
|
||||
const temporaryPath = join(
|
||||
dirname(policyPath),
|
||||
`.${basename(policyPath)}.tmp.${process.pid}.${backupTimestamp(now)}`,
|
||||
);
|
||||
await writeExclusive(backupPath, original.content);
|
||||
|
||||
let temporaryCreated = false;
|
||||
try {
|
||||
await writeExclusive(temporaryPath, seedContent);
|
||||
temporaryCreated = true;
|
||||
if (options.beforeCommit) await options.beforeCommit();
|
||||
const current = await readFileSnapshot(policyPath);
|
||||
if (
|
||||
current.dev !== original.dev ||
|
||||
current.ino !== original.ino ||
|
||||
current.content !== original.content
|
||||
) {
|
||||
throw new Error(
|
||||
"permission policy changed during migration; original file was preserved",
|
||||
);
|
||||
}
|
||||
await rename(temporaryPath, policyPath);
|
||||
temporaryCreated = false;
|
||||
await chmod(policyPath, 0o600);
|
||||
return { status: "migrated", backupPath };
|
||||
} finally {
|
||||
if (temporaryCreated) await unlink(temporaryPath).catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const [policyPath, seedPath] = process.argv.slice(2);
|
||||
if (!policyPath || !seedPath) {
|
||||
console.error(
|
||||
"usage: migrate-permission-policy.mjs <policy.jsonc> <bundled-seed.jsonc>",
|
||||
);
|
||||
process.exitCode = 2;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = await migratePermissionPolicy(policyPath, seedPath);
|
||||
console.log(
|
||||
result.status === "migrated"
|
||||
? `migrated:${result.backupPath}`
|
||||
: result.status,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error instanceof Error ? error.message : String(error));
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
function isMainModule() {
|
||||
return process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
|
||||
}
|
||||
|
||||
if (isMainModule()) await main();
|
||||
Reference in New Issue
Block a user