Files
my-pi/pi-permission-system/test/extension-config.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

218 lines
6.6 KiB
TypeScript

import { chmodSync, mkdirSync, mkdtempSync, rmSync, statSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, test } from "vitest";
import type { PermissionSystemExtensionConfig } from "#src/extension-config";
import {
detectMisplacedPermissionKeys,
ensurePermissionSystemLogsDirectory,
isYoloModeEnabled,
normalizePermissionSystemConfig,
} from "#src/extension-config";
function makeConfig(
yoloMode: boolean | undefined,
): PermissionSystemExtensionConfig {
return { yoloMode } as PermissionSystemExtensionConfig;
}
describe("detectMisplacedPermissionKeys", () => {
it("returns an empty array for a record with only valid extension keys", () => {
const result = detectMisplacedPermissionKeys({
debugLog: true,
permissionReviewLog: true,
yoloMode: false,
});
expect(result).toEqual([]);
});
it("returns an empty array for an empty record", () => {
const result = detectMisplacedPermissionKeys({});
expect(result).toEqual([]);
});
it("returns misplaced key names when legacy permission-rule keys are present", () => {
const result = detectMisplacedPermissionKeys({
debugLog: true,
defaultPolicy: { tools: "ask" },
bash: { "git status": "allow" },
});
expect(result).toEqual(["defaultPolicy", "bash"]);
});
it("detects all known legacy permission-rule keys", () => {
const result = detectMisplacedPermissionKeys({
defaultPolicy: {},
tools: {},
bash: {},
mcp: {},
skills: {},
special: {},
});
expect(result).toEqual([
"defaultPolicy",
"tools",
"bash",
"mcp",
"skills",
"special",
]);
});
it("does not detect doom_loop as a misplaced permission key", () => {
const result = detectMisplacedPermissionKeys({
doom_loop: {},
});
expect(result).toEqual([]);
});
it("does not flag the new flat-format permission key as misplaced", () => {
const result = detectMisplacedPermissionKeys({
debugLog: false,
permission: { "*": "ask" },
});
expect(result).toEqual([]);
});
it("ignores unknown keys that are not permission-rule keys", () => {
const result = detectMisplacedPermissionKeys({
debugLog: true,
someRandomKey: "value",
});
expect(result).toEqual([]);
});
});
describe("normalizePermissionSystemConfig", () => {
it("normalizes a valid config object", () => {
const result = normalizePermissionSystemConfig({
debugLog: true,
permissionReviewLog: false,
yoloMode: true,
});
expect(result).toEqual({
debugLog: true,
permissionReviewLog: false,
yoloMode: true,
doublePressToConfirm: true,
});
});
it("applies the documented defaults for missing runtime knobs", () => {
const result = normalizePermissionSystemConfig({});
expect(result).toMatchObject({
debugLog: false,
permissionReviewLog: true,
yoloMode: false,
doublePressToConfirm: true,
});
});
it("sets doublePressToConfirm false when explicitly disabled", () => {
const result = normalizePermissionSystemConfig({
doublePressToConfirm: false,
});
expect(result.doublePressToConfirm).toBe(false);
});
it("includes the optional scalar knobs when provided", () => {
const result = normalizePermissionSystemConfig({
forwardingTimeoutMs: 120_000,
promptMaxRows: 12,
promptFieldMaxWidth: 80,
reviewLogFieldMaxWidth: 200,
});
expect(result.forwardingTimeoutMs).toBe(120_000);
expect(result.promptMaxRows).toBe(12);
expect(result.promptFieldMaxWidth).toBe(80);
expect(result.reviewLogFieldMaxWidth).toBe(200);
});
it("omits the optional scalar knobs when absent, leaving consumer defaults", () => {
const result = normalizePermissionSystemConfig({});
for (const key of [
"forwardingTimeoutMs",
"promptMaxRows",
"promptFieldMaxWidth",
"reviewLogFieldMaxWidth",
]) {
expect(key in result).toBe(false);
}
});
// Deliberately dropped rather than carried: a declared config field no
// runtime consumer reads is a maintenance trap, so the deprecated caps stop
// at the merge intermediate the detector reads (#745).
it("drops the deprecated preview caps even when the config sets them", () => {
const result = normalizePermissionSystemConfig({
toolInputPreviewMaxLength: 400,
toolTextSummaryMaxLength: 120,
});
expect("toolInputPreviewMaxLength" in result).toBe(false);
expect("toolTextSummaryMaxLength" in result).toBe(false);
});
it("includes shellTools when provided", () => {
const result = normalizePermissionSystemConfig({
shellTools: {
exec_command: { commandArgument: "cmd", workdirArgument: "workdir" },
},
});
expect(result.shellTools).toEqual({
exec_command: { commandArgument: "cmd", workdirArgument: "workdir" },
});
expect("shellTools" in normalizePermissionSystemConfig({})).toBe(false);
});
it("includes authorizerChain when provided", () => {
const result = normalizePermissionSystemConfig({
authorizerChain: ["model-judge", "typo-reviewer"],
});
expect(result.authorizerChain).toEqual(["model-judge", "typo-reviewer"]);
expect("authorizerChain" in normalizePermissionSystemConfig({})).toBe(false);
});
});
describe("ensurePermissionSystemLogsDirectory", () => {
let baseDir: string;
beforeEach(() => {
baseDir = mkdtempSync(join(tmpdir(), "pi-permission-system-logsdir-"));
});
afterEach(() => {
rmSync(baseDir, { recursive: true, force: true });
});
test("creates the logs directory owner-only", () => {
const logsDir = join(baseDir, "extensions", "pi-permission-system", "logs");
expect(ensurePermissionSystemLogsDirectory(logsDir)).toBe(undefined);
expect(statSync(logsDir).mode & 0o777).toBe(0o700);
});
test("tightens a directory inherited from an earlier version", () => {
const logsDir = join(baseDir, "logs");
mkdirSync(logsDir);
chmodSync(logsDir, 0o755);
expect(ensurePermissionSystemLogsDirectory(logsDir)).toBe(undefined);
expect(statSync(logsDir).mode & 0o777).toBe(0o700);
});
});
describe("isYoloModeEnabled", () => {
it("returns true when yoloMode is true", () => {
expect(isYoloModeEnabled(makeConfig(true))).toBe(true);
});
it("returns false when yoloMode is false", () => {
expect(isYoloModeEnabled(makeConfig(false))).toBe(false);
});
it("returns false when yoloMode is undefined", () => {
expect(isYoloModeEnabled(makeConfig(undefined))).toBe(false);
});
});