Files
my-pi/pi-permission-system/test/handlers/gates/helpers.test.ts
T
云服务部-叶林立andDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com> 7e6a8faae1 feat(pi-permission-system): AutoReview 改为跟随主模型的对话式工具调用审查
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>
2026-10-19 01:54:17 -07:00

217 lines
6.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { AccessPath } from "#src/access-intent/access-path";
import {
accessFactsFromPath,
accessFactsFromValue,
buildDecisionEvent,
deriveDecisionValue,
deriveResolution,
resolveYoloGrant,
} from "#src/handlers/gates/helpers";
import type { PermissionCheckResult } from "#src/types";
import { makeCheckResult } from "#test/helpers/handler-fixtures";
describe("deriveDecisionValue", () => {
it("returns command for bash", () => {
expect(deriveDecisionValue("bash", { command: "git status" })).toBe(
"git status",
);
});
it("falls back to toolName when bash has no command", () => {
expect(deriveDecisionValue("bash", {})).toBe("bash");
});
it("returns target for mcp", () => {
expect(deriveDecisionValue("mcp", { target: "exa:search" })).toBe(
"exa:search",
);
});
it("falls back to toolName when mcp has no target", () => {
expect(deriveDecisionValue("mcp", {})).toBe("mcp");
});
it("returns toolName for non-path-bearing tools", () => {
expect(deriveDecisionValue("my_extension_tool", {})).toBe(
"my_extension_tool",
);
});
it("returns path for path-bearing tools when path is provided", () => {
expect(deriveDecisionValue("read", {}, "/project/src/main.ts")).toBe(
"/project/src/main.ts",
);
expect(deriveDecisionValue("write", {}, "src/.env")).toBe("src/.env");
});
it("falls back to toolName for path-bearing tools when path is missing", () => {
expect(deriveDecisionValue("read", {})).toBe("read");
expect(deriveDecisionValue("write", {}, undefined)).toBe("write");
});
});
describe("deriveResolution", () => {
it("returns policy_allow for allow state", () => {
expect(deriveResolution("allow", "allow", false, true)).toBe(
"policy_allow",
);
});
it("returns policy_deny for deny state", () => {
expect(deriveResolution("deny", "block", false, true)).toBe("policy_deny");
});
it("returns user_approved for ask + allow without session", () => {
expect(deriveResolution("ask", "allow", false, true)).toBe("user_approved");
});
it("returns user_approved_for_session for ask + allow with session", () => {
expect(deriveResolution("ask", "allow", true, true)).toBe(
"user_approved_for_session",
);
});
it("returns auto_approved when autoApproved flag is set", () => {
expect(deriveResolution("ask", "allow", false, true, true)).toBe(
"auto_approved",
);
});
it("returns auto_approved for allow + autoApproved (yolo-origin allow)", () => {
expect(deriveResolution("allow", "allow", false, false, true)).toBe(
"auto_approved",
);
});
it("returns user_denied for ask + block when confirmation was available", () => {
expect(deriveResolution("ask", "block", false, false)).toBe("user_denied");
});
it("returns confirmation_unavailable for ask + block when confirmation was unavailable", () => {
expect(deriveResolution("ask", "block", false, true)).toBe(
"confirmation_unavailable",
);
});
});
describe("buildDecisionEvent", () => {
function makeCheck(
overrides: Partial<PermissionCheckResult> = {},
): PermissionCheckResult {
return {
state: "allow",
toolName: "read",
source: "tool",
origin: "builtin",
matchedPattern: "*",
...overrides,
};
}
it("builds a decision event with all fields populated", () => {
const event = buildDecisionEvent(
{ surface: "read", value: "read" },
makeCheck({ origin: "global", matchedPattern: "read" }),
"test-agent",
"allow",
"policy_allow",
);
expect(event).toEqual({
surface: "read",
value: "read",
result: "allow",
resolution: "policy_allow",
origin: "global",
agentName: "test-agent",
matchedPattern: "read",
});
});
it("normalises absent optional fields to null", () => {
const event = buildDecisionEvent(
{ surface: "read", value: "read" },
makeCheck({ origin: undefined, matchedPattern: undefined }),
null,
"deny",
"policy_deny",
);
expect(event.origin).toBeNull();
expect(event.agentName).toBeNull();
expect(event.matchedPattern).toBeNull();
});
});
describe("resolveYoloGrant", () => {
it("returns the check unchanged for a ruleset-granted yolo allow", () => {
const check = makeCheckResult({ origin: "yolo", matchedPattern: "*" });
expect(resolveYoloGrant(check, false)).toBe(check);
});
it("grants a residual ask under yolo, preserving the matched pattern", () => {
const check = makeCheckResult({
state: "ask",
source: "bash",
toolName: "bash",
matchedPattern: "<indirection-bash-wrapper>",
});
expect(resolveYoloGrant(check, true)).toEqual({
...check,
state: "allow",
origin: "yolo",
});
});
it("returns null for a residual ask with yolo disabled", () => {
expect(
resolveYoloGrant(
makeCheckResult({ state: "ask", matchedPattern: "*" }),
false,
),
).toBeNull();
});
it("returns null for an allow granted by an ordinary rule", () => {
expect(
resolveYoloGrant(
makeCheckResult({ origin: "global", matchedPattern: "*" }),
true,
),
).toBeNull();
});
it("returns null for a deny, so an explicit deny survives yolo", () => {
expect(
resolveYoloGrant(
makeCheckResult({ state: "deny", matchedPattern: "rm *" }),
true,
),
).toBeNull();
});
});
describe("accessFactsFromPath", () => {
it("collapses an empty boundary (literal-only path) to null", () => {
const path = AccessPath.forLiteral("relative-token");
expect(path.boundaryValue()).toBe("");
expect(accessFactsFromPath("path", path)).toEqual({
surface: "path",
matchValues: ["relative-token"],
boundaryValue: null,
});
});
});
describe("accessFactsFromValue", () => {
it("wraps a single portable value with a null boundary", () => {
expect(accessFactsFromValue("skill", "deep-research")).toEqual({
surface: "skill",
matchValues: ["deep-research"],
boundaryValue: null,
});
});
});