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

183 lines
5.8 KiB
TypeScript

import { describe, expect, test } from "vitest";
import {
classifyToolKind,
isMcpCheck,
resolveShellInvocation,
} from "#src/access-intent/tool-kind";
import type { ShellToolsConfig } from "#src/config-schema";
describe("classifyToolKind", () => {
test("classifies bash", () => {
expect(classifyToolKind("bash")).toBe("bash");
});
test("classifies mcp", () => {
expect(classifyToolKind("mcp")).toBe("mcp");
});
test("classifies skill", () => {
expect(classifyToolKind("skill")).toBe("skill");
});
test("classifies path-bearing built-in tools as path", () => {
for (const tool of ["read", "write", "edit", "grep", "find", "ls"]) {
expect(classifyToolKind(tool)).toBe("path");
}
});
test("classifies an arbitrary extension tool as extension", () => {
expect(classifyToolKind("task")).toBe("extension");
expect(classifyToolKind("third_party_tool")).toBe("extension");
});
test("classifies the special path surfaces as extension", () => {
// `path` and `external_directory` are not tool names — they reach the
// classifier only as normalized surface names in `deriveSource`, where the
// `SPECIAL_PERMISSION_KEYS` check maps them to `special` before the kind.
expect(classifyToolKind("path")).toBe("extension");
expect(classifyToolKind("external_directory")).toBe("extension");
});
test("trims surrounding whitespace before classifying", () => {
expect(classifyToolKind(" bash ")).toBe("bash");
expect(classifyToolKind("\tmcp\n")).toBe("mcp");
expect(classifyToolKind(" read ")).toBe("path");
});
});
describe("isMcpCheck", () => {
test("is true when the tool itself is mcp", () => {
expect(isMcpCheck({ toolName: "mcp", source: "tool" })).toBe(true);
});
test("is true when the winning rule matched on the mcp surface", () => {
// The `source` disjunct: a server-qualified toolName still classifies as an
// MCP call because `deriveSource` set source to `mcp`.
expect(
isMcpCheck({ toolName: "some-server:some-tool", source: "mcp" }),
).toBe(true);
expect(isMcpCheck({ toolName: "read", source: "mcp" })).toBe(true);
});
test("is false for a bash check", () => {
expect(isMcpCheck({ toolName: "bash", source: "bash" })).toBe(false);
});
test("is false for a plain tool check", () => {
expect(isMcpCheck({ toolName: "read", source: "tool" })).toBe(false);
expect(isMcpCheck({ toolName: "task", source: "default" })).toBe(false);
});
});
describe("resolveShellInvocation", () => {
const execAlias: ShellToolsConfig = {
exec_command: { commandArgument: "cmd", workdirArgument: "workdir" },
};
describe("native bash", () => {
test("extracts the command with no workdir", () => {
expect(
resolveShellInvocation("bash", { command: "git status" }, undefined),
).toEqual({ command: "git status", workdir: undefined });
});
test("trims the command", () => {
expect(
resolveShellInvocation(
"bash",
{ command: " git status " },
undefined,
),
).toEqual({ command: "git status", workdir: undefined });
});
test("yields an empty command when absent or non-string", () => {
expect(resolveShellInvocation("bash", {}, undefined)).toEqual({
command: "",
workdir: undefined,
});
expect(
resolveShellInvocation("bash", { command: 42 }, undefined),
).toEqual({ command: "", workdir: undefined });
});
test("resolves regardless of the shellTools map", () => {
expect(
resolveShellInvocation("bash", { command: "ls" }, execAlias),
).toEqual({ command: "ls", workdir: undefined });
});
});
describe("aliased shell tool", () => {
test("extracts command and workdir from the mapped arguments", () => {
expect(
resolveShellInvocation(
"exec_command",
{ cmd: "npm install", workdir: "/etc" },
execAlias,
),
).toEqual({ command: "npm install", workdir: "/etc" });
});
test("omits workdir when the alias declares no workdirArgument", () => {
const aliases: ShellToolsConfig = {
exec_command: { commandArgument: "cmd" },
};
expect(
resolveShellInvocation(
"exec_command",
{ cmd: "npm install", workdir: "/etc" },
aliases,
),
).toEqual({ command: "npm install", workdir: undefined });
});
test("omits workdir when the mapped workdir argument is absent", () => {
expect(
resolveShellInvocation(
"exec_command",
{ cmd: "npm install" },
execAlias,
),
).toEqual({ command: "npm install", workdir: undefined });
});
test("yields an empty command when the mapped command argument is absent", () => {
expect(
resolveShellInvocation("exec_command", { workdir: "/etc" }, execAlias),
).toEqual({ command: "", workdir: "/etc" });
});
test("trims the extracted command and workdir", () => {
expect(
resolveShellInvocation(
"exec_command",
{ cmd: " npm install ", workdir: " /etc " },
execAlias,
),
).toEqual({ command: "npm install", workdir: "/etc" });
});
});
describe("non-shell tools", () => {
test("returns null for an unaliased extension tool", () => {
expect(
resolveShellInvocation(
"exec_command",
{ cmd: "npm install" },
undefined,
),
).toBeNull();
expect(
resolveShellInvocation("read", { path: "a.txt" }, execAlias),
).toBeNull();
});
test("returns null when the map names a different tool", () => {
expect(
resolveShellInvocation("other_tool", { cmd: "npm install" }, execAlias),
).toBeNull();
});
});
});