mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-10-26 18:55:22 +00:00
AutoReview: - 审查器回到 Codex Guardian 风格的对话式审查,默认跟随会话当前主模型 (每次审查实时读取 ctx.model);provider/model 必须同时设置才固定审查 模型,只设其一视为配置错误并转人工。 - 结论只通过 approve / reject 两个工具给出,参数仅 reason;不强制 toolChoice,正文文本(即使是合法 JSON)一律忽略。 - approve 自动放行;reject 转人工并在弹窗显示理由(标注模型生成)。 超时、Provider 错误、截断、未调用工具、多次调用、参数无效等异常同样 转人工,并附固定的系统说明;用户按 Esc 中止时直接拒绝。 - 模型调用改为 modelRegistry.streamSimple(...).result():Pi 1.3.0 面向 扩展的 ModelRegistry 没有 completeSimple,原写法在真实环境中每次都会 转人工。 - 对话记录保留可信度标注;工具结果在有更短的 pi-compaction 摘要时替换 为摘要;token 估算改为非 ASCII 字符按 1 token 计;预算可配置。 - 同一条用户消息内完全相同的请求只缓存 approve 结论。 - 默认 reasoning low、总超时 30 秒、最多 2 次尝试、maxOutputTokens 4000。 删除: - 未发布过的 TypeSafe Jev 分类器实现、DenialCircuitBreaker。 - 整个 annotator 机制(model-explanation、注册表、registerAnnotator、 provider-timeout);annotators 配置键保留为已弃用并忽略,避免旧配置 被拒绝。 测试: - 新增 reviewer / transcript / 弹窗说明 / 默认配置测试;清理重复、恒真 或只测实现细节的旧测试。 - 使用 deepseek-v4.1-flash 实测 5 个场景各 5 次,25 次结论全部符合预期。 Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
292 lines
9.1 KiB
TypeScript
292 lines
9.1 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import { posixPathFlavor } from "#src/path/path-flavor";
|
|
import type { RuleOrigin } from "#src/rule";
|
|
import { evaluate } from "#src/rule";
|
|
import {
|
|
composeRuleset,
|
|
synthesizeBaseline,
|
|
synthesizeDefaults,
|
|
} from "#src/synthesize";
|
|
|
|
// ── synthesizeDefaults ─────────────────────────────────────────────────────
|
|
|
|
describe("synthesizeDefaults", () => {
|
|
test("emits a single universal catch-all rule with layer 'default' and origin 'builtin'", () => {
|
|
const rules = synthesizeDefaults("ask");
|
|
expect(rules).toHaveLength(1);
|
|
expect(rules[0]).toEqual({
|
|
surface: "*",
|
|
pattern: "*",
|
|
action: "ask",
|
|
layer: "default",
|
|
origin: "builtin",
|
|
});
|
|
});
|
|
|
|
test("reflects the supplied PermissionState as the action", () => {
|
|
expect(synthesizeDefaults("allow")[0].action).toBe("allow");
|
|
expect(synthesizeDefaults("deny")[0].action).toBe("deny");
|
|
expect(synthesizeDefaults("ask")[0].action).toBe("ask");
|
|
});
|
|
|
|
test("universal rule catches any surface via wildcardMatch", () => {
|
|
const rules = synthesizeDefaults("ask");
|
|
expect(evaluate("read", "*", rules, posixPathFlavor).action).toBe("ask");
|
|
expect(evaluate("bash", "git status", rules, posixPathFlavor).action).toBe(
|
|
"ask",
|
|
);
|
|
expect(
|
|
evaluate("external_directory", "*", rules, posixPathFlavor).action,
|
|
).toBe("ask");
|
|
expect(evaluate("future_surface", "*", rules, posixPathFlavor).action).toBe(
|
|
"ask",
|
|
);
|
|
});
|
|
|
|
test("universal rule has layer 'default'", () => {
|
|
const rules = synthesizeDefaults("allow");
|
|
expect(evaluate("read", "*", rules, posixPathFlavor).layer).toBe("default");
|
|
});
|
|
|
|
test("defaults to origin 'builtin' when no origin supplied", () => {
|
|
const rules = synthesizeDefaults("ask");
|
|
expect(rules[0].origin).toBe("builtin");
|
|
});
|
|
|
|
test("universal rule carries config scope origin when supplied", () => {
|
|
const origin: RuleOrigin = "global";
|
|
const rules = synthesizeDefaults("ask", origin);
|
|
expect(rules[0].origin).toBe("global");
|
|
});
|
|
|
|
test("origin is preserved through evaluate()", () => {
|
|
const rules = synthesizeDefaults("allow", "project");
|
|
const result = evaluate("read", "*", rules, posixPathFlavor);
|
|
expect(result.origin).toBe("project");
|
|
});
|
|
});
|
|
|
|
// ── synthesizeBaseline ─────────────────────────────────────────────────────
|
|
|
|
describe("synthesizeBaseline", () => {
|
|
test("returns empty ruleset when config has no mcp allow rules", () => {
|
|
const configRules = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "*",
|
|
action: "deny" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
expect(synthesizeBaseline(configRules)).toEqual([]);
|
|
});
|
|
|
|
test("returns empty ruleset for empty config rules", () => {
|
|
expect(synthesizeBaseline([])).toEqual([]);
|
|
});
|
|
|
|
test("synthesizes 5 baseline rules when at least one mcp allow config rule exists", () => {
|
|
const configRules = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "exa:*",
|
|
action: "allow" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const rules = synthesizeBaseline(configRules);
|
|
expect(rules).toHaveLength(5);
|
|
});
|
|
|
|
test("baseline rules all have layer 'baseline', action 'allow', and origin 'baseline'", () => {
|
|
const configRules = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "exa:*",
|
|
action: "allow" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const rules = synthesizeBaseline(configRules);
|
|
for (const rule of rules) {
|
|
expect(rule.layer).toBe("baseline");
|
|
expect(rule.action).toBe("allow");
|
|
expect(rule.surface).toBe("mcp");
|
|
expect(rule.origin).toBe("baseline");
|
|
}
|
|
});
|
|
|
|
test("baseline rules cover the 5 MCP metadata targets", () => {
|
|
const configRules = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "exa:*",
|
|
action: "allow" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const rules = synthesizeBaseline(configRules);
|
|
const patterns = rules.map((r) => r.pattern);
|
|
expect(patterns).toContain("mcp_status");
|
|
expect(patterns).toContain("mcp_list");
|
|
expect(patterns).toContain("mcp_search");
|
|
expect(patterns).toContain("mcp_describe");
|
|
expect(patterns).toContain("mcp_connect");
|
|
});
|
|
|
|
test("baseline is NOT synthesized when allow rule is on a non-mcp surface", () => {
|
|
const configRules = [
|
|
{
|
|
surface: "bash",
|
|
pattern: "git *",
|
|
action: "allow" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
expect(synthesizeBaseline(configRules)).toEqual([]);
|
|
});
|
|
|
|
test("baseline auto-allows mcp_status when an mcp allow rule exists", () => {
|
|
const configRules = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "exa:*",
|
|
action: "allow" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const rules = synthesizeBaseline(configRules);
|
|
const result = evaluate("mcp", "mcp_status", rules, posixPathFlavor);
|
|
expect(result.action).toBe("allow");
|
|
expect(result.layer).toBe("baseline");
|
|
expect(result.origin).toBe("baseline");
|
|
});
|
|
});
|
|
|
|
// ── composeRuleset ─────────────────────────────────────────────────────────
|
|
|
|
describe("composeRuleset", () => {
|
|
test("returns concatenation of all layers in order", () => {
|
|
const defaults = synthesizeDefaults("ask");
|
|
const baseline = synthesizeBaseline([
|
|
{
|
|
surface: "mcp",
|
|
pattern: "exa:*",
|
|
action: "allow",
|
|
layer: "config",
|
|
origin: "global" as const,
|
|
},
|
|
]);
|
|
const config = [
|
|
{
|
|
surface: "bash",
|
|
pattern: "rm -rf *",
|
|
action: "deny" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const composed = composeRuleset(defaults, baseline, config);
|
|
expect(composed.length).toBe(
|
|
defaults.length + baseline.length + config.length,
|
|
);
|
|
});
|
|
|
|
test("defaults come first (lowest priority), config comes last (highest priority)", () => {
|
|
const defaults = synthesizeDefaults("ask");
|
|
const config = [
|
|
{
|
|
surface: "bash",
|
|
pattern: "*",
|
|
action: "deny" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const composed = composeRuleset(defaults, [], config);
|
|
const result = evaluate("bash", "echo hello", composed, posixPathFlavor);
|
|
expect(result.action).toBe("deny");
|
|
expect(result.layer).toBe("config");
|
|
});
|
|
|
|
test("config beats default for matching patterns", () => {
|
|
const defaults = synthesizeDefaults("ask");
|
|
const config = [
|
|
{
|
|
surface: "read",
|
|
pattern: "*",
|
|
action: "allow" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const composed = composeRuleset(defaults, [], config);
|
|
const result = evaluate("read", "*", composed, posixPathFlavor);
|
|
expect(result.action).toBe("allow");
|
|
expect(result.layer).toBe("config");
|
|
});
|
|
|
|
test("baseline beats default but config beats baseline", () => {
|
|
const defaults = synthesizeDefaults("ask");
|
|
const baseline = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "mcp_status",
|
|
action: "allow" as const,
|
|
layer: "baseline" as const,
|
|
origin: "baseline" as const,
|
|
},
|
|
];
|
|
const config = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "mcp_status",
|
|
action: "deny" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const composed = composeRuleset(defaults, baseline, config);
|
|
const result = evaluate("mcp", "mcp_status", composed, posixPathFlavor);
|
|
expect(result.action).toBe("deny");
|
|
expect(result.layer).toBe("config");
|
|
});
|
|
|
|
test("config beats baseline for specific patterns", () => {
|
|
const defaults = synthesizeDefaults("ask");
|
|
const baseline = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "mcp_status",
|
|
action: "allow" as const,
|
|
layer: "baseline" as const,
|
|
origin: "baseline" as const,
|
|
},
|
|
];
|
|
const config = [
|
|
{
|
|
surface: "mcp",
|
|
pattern: "exa_web_search",
|
|
action: "allow" as const,
|
|
layer: "config" as const,
|
|
origin: "global" as const,
|
|
},
|
|
];
|
|
const composed = composeRuleset(defaults, baseline, config);
|
|
const result = evaluate("mcp", "exa_web_search", composed, posixPathFlavor);
|
|
expect(result.action).toBe("allow");
|
|
expect(result.layer).toBe("config");
|
|
});
|
|
|
|
test("handles empty layers gracefully", () => {
|
|
const defaults = synthesizeDefaults("ask");
|
|
const composed = composeRuleset(defaults, [], []);
|
|
expect(composed).toEqual(defaults);
|
|
});
|
|
});
|