Files
my-pi/pi-permission-system/test/access-intent/input-normalizer.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

310 lines
10 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
const mockHomedir = vi.hoisted(() => vi.fn(() => "/mock/home"));
vi.mock("node:os", () => ({
homedir: mockHomedir,
default: { homedir: mockHomedir },
}));
// Mock node:fs so realpathSync (used by the canonical alias) is controllable.
// Default implementation is identity — lexical tests are unaffected.
const realpathSync = vi.hoisted(() =>
vi.fn<(path: string) => string>((p) => p),
);
vi.mock("node:fs", () => ({
realpathSync,
default: { realpathSync },
}));
import {
buildAccessIntentForSurface,
buildResolvedIntentFromMatchValues,
normalizeInput,
} from "#src/access-intent/input-normalizer";
import { createMcpPermissionTargets } from "#src/access-intent/mcp-targets";
import { posixPathFlavor } from "#src/path/path-flavor";
import { PathNormalizer } from "#src/path-normalizer";
afterEach(() => {
mockHomedir.mockClear();
realpathSync.mockReset();
realpathSync.mockImplementation((p: string) => p);
});
describe("normalizeInput — non-MCP surfaces", () => {
// Path-bearing and special surfaces no longer derive path lookup values
// through normalizeInput — that is now done by the access-path gate (#502)
// and the service/RPC builder (#503). normalizeInput's tool branch collapses
// every path-bearing or special surface to the catch-all ["*"] exactly as it
// does for any unrecognised extension tool.
describe("path-bearing and special surfaces collapse to '*'", () => {
it("path-bearing surfaces ignore input.path and return ['*']", () => {
// After #504 removal: path-bearing surfaces no longer have a special
// branch in normalizeInput — the access-path gate handles them (#502).
for (const surface of ["path", "read"]) {
const result = normalizeInput(surface, { path: ".env" }, []);
expect(result.surface).toBe(surface);
expect(result.values).toEqual(["*"]);
expect(result.resultExtras).toEqual({});
}
});
it("missing path also returns ['*'] (unchanged fallback)", () => {
for (const surface of [
"path",
"read",
"write",
"edit",
"grep",
"find",
"ls",
]) {
const result = normalizeInput(surface, {}, []);
expect(result.values).toEqual(["*"]);
}
});
});
describe("skill", () => {
it("uses skill name from input.name", () => {
const result = normalizeInput("skill", { name: "librarian" }, []);
expect(result.surface).toBe("skill");
expect(result.values).toEqual(["librarian"]);
expect(result.resultExtras).toEqual({});
});
it("falls back to '*' when name is missing or not a string", () => {
expect(normalizeInput("skill", {}, []).values).toEqual(["*"]);
expect(normalizeInput("skill", { name: 99 }, []).values).toEqual(["*"]);
});
});
describe("bash", () => {
it("uses command from input.command", () => {
const result = normalizeInput("bash", { command: "git status" }, []);
expect(result.surface).toBe("bash");
expect(result.values).toEqual(["git status"]);
expect(result.resultExtras).toEqual({ command: "git status" });
});
it("uses empty string when command is missing or not a string", () => {
for (const input of [{}, { command: 42 }]) {
const result = normalizeInput("bash", input, []);
expect(result.values).toEqual([""]);
expect(result.resultExtras).toEqual({ command: "" });
}
});
it("strips leading comment lines from values but keeps original in resultExtras", () => {
const cmd = "# Check debug logs\nfind /home -path '*debug*' -type f";
const result = normalizeInput("bash", { command: cmd }, []);
expect(result.values).toEqual(["find /home -path '*debug*' -type f"]);
expect(result.resultExtras).toEqual({ command: cmd });
});
it("strips multiple comment lines", () => {
const cmd = "# Step 1\n# Step 2\ngit status --short";
const result = normalizeInput("bash", { command: cmd }, []);
expect(result.values).toEqual(["git status --short"]);
});
it("preserves command when no comment lines present", () => {
const result = normalizeInput(
"bash",
{ command: "grep -rn foo src/" },
[],
);
expect(result.values).toEqual(["grep -rn foo src/"]);
});
it("falls back to original when all lines are comments", () => {
const cmd = "# just a comment";
const result = normalizeInput("bash", { command: cmd }, []);
expect(result.values).toEqual(["# just a comment"]);
});
});
describe("extension tools (non-path-bearing)", () => {
it("uses '*' as the lookup value for extension tools", () => {
const result = normalizeInput("my_extension_tool", { some: "input" }, []);
expect(result.surface).toBe("my_extension_tool");
expect(result.values).toEqual(["*"]);
expect(result.resultExtras).toEqual({});
});
it("uses '*' even when extension tool has a path field", () => {
const result = normalizeInput(
"my_extension_tool",
{ path: "/some/path" },
[],
);
expect(result.values).toEqual(["*"]);
});
});
});
describe("normalizeInput — MCP surface", () => {
it("values include specific targets before the catch-all for a qualified tool call", () => {
const result = normalizeInput("mcp", { tool: "exa:search" }, []);
expect(result.surface).toBe("mcp");
expect(result.values).toContain("exa_search");
expect(result.values).toContain("exa:search");
expect(result.values).toContain("exa");
expect(result.values).toContain("mcp_call");
// 'mcp' is always last
expect(result.values.at(-1)).toBe("mcp");
});
it("matches createMcpPermissionTargets output + 'mcp' appended", () => {
const rawTargets = createMcpPermissionTargets({ tool: "exa:search" }, [
"exa",
]);
const result = normalizeInput("mcp", { tool: "exa:search" }, ["exa"]);
expect(result.values).toEqual([...rawTargets, "mcp"]);
});
it("resultExtras.target is the first specific target (most-specific)", () => {
const result = normalizeInput("mcp", { tool: "exa:search" }, []);
expect(result.resultExtras.target).toBe(result.values[0]);
});
it("resultExtras.target is 'mcp' when no specific targets are derived", () => {
// Empty input → only mcp_status then mcp appended
const result = normalizeInput("mcp", {}, []);
expect(result.resultExtras.target).toBe("mcp_status");
});
it("values contain no duplicates", () => {
const result = normalizeInput("mcp", { tool: "exa:search" }, ["exa"]);
const unique = [...new Set(result.values)];
expect(result.values).toEqual(unique);
});
it("produces mcp_status + mcp for status input", () => {
const result = normalizeInput("mcp", {}, []);
expect(result.values).toEqual(["mcp_status", "mcp"]);
});
it("produces connect targets + mcp for connect input", () => {
const result = normalizeInput("mcp", { connect: "exa" }, []);
expect(result.values).toContain("mcp_connect_exa");
expect(result.values).toContain("mcp_connect");
expect(result.values.at(-1)).toBe("mcp");
});
});
describe("buildAccessIntentForSurface", () => {
const normalizer = new PathNormalizer(posixPathFlavor, "/test/project");
it("emits an access-path intent carrying the canonical alias for the path surface", () => {
realpathSync.mockImplementation((p: string) =>
p === "/test/project/link" ? "/test/project/real" : p,
);
const intent = buildAccessIntentForSurface(
"path",
"link",
normalizer,
undefined,
);
expect(intent.kind).toBe("access-path");
if (intent.kind === "access-path") {
expect(intent.surface).toBe("path");
expect(intent.path.matchValues()).toContain("/test/project/real");
expect(intent.path.value()).toBe("/test/project/link");
}
});
it("emits a tool intent for a non-path surface (bash)", () => {
const intent = buildAccessIntentForSurface(
"bash",
"echo hi",
normalizer,
"my-agent",
);
expect(intent).toEqual({
kind: "tool",
surface: "bash",
input: { command: "echo hi" },
agentName: "my-agent",
});
});
it("passes agentName through on the access-path branch", () => {
const intent = buildAccessIntentForSurface(
"path",
"/some/file",
normalizer,
"Explore",
);
expect(intent.agentName).toBe("Explore");
});
it("falls back to a tool intent for a value-less path surface", () => {
const intent = buildAccessIntentForSurface(
"path",
undefined,
normalizer,
undefined,
);
expect(intent.kind).toBe("tool");
if (intent.kind === "tool") {
expect(intent.surface).toBe("path");
}
});
});
describe("buildResolvedIntentFromMatchValues", () => {
// The forwarded-serving wire's sole producer of a pre-fixed ResolvedAccessIntent
// (#597): match values arrive already fixed at the child (matchValues()), so
// this never touches a PathNormalizer or rebuilds an AccessPath.
it("emits a path-values intent carrying the given match values as-is for the path surface", () => {
const intent = buildResolvedIntentFromMatchValues(
"path",
["/worktree/issue-42/src/foo.ts", "src/foo.ts", "/main/src/foo.ts"],
"Explore",
);
expect(intent).toEqual({
kind: "path-values",
surface: "path",
values: [
"/worktree/issue-42/src/foo.ts",
"src/foo.ts",
"/main/src/foo.ts",
],
agentName: "Explore",
});
});
it("emits a tool intent from the single portable value for a non-path surface (bash)", () => {
const intent = buildResolvedIntentFromMatchValues(
"bash",
["git status"],
"Explore",
);
expect(intent).toEqual({
kind: "tool",
surface: "bash",
input: { command: "git status" },
agentName: "Explore",
});
});
it("emits a tool intent from the single portable value for a skill surface", () => {
const intent = buildResolvedIntentFromMatchValues(
"skill",
["librarian"],
"Explore",
);
expect(intent).toEqual({
kind: "tool",
surface: "skill",
input: { name: "librarian" },
agentName: "Explore",
});
});
});