mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-10-26 10:45: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>
363 lines
12 KiB
TypeScript
363 lines
12 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
// Mock node:fs so realpathSync (used by canonicalizePath) 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 { AccessPath } from "#src/access-intent/access-path";
|
|
import type { GateDescriptor } from "#src/handlers/gates/descriptor";
|
|
import { isGateDescriptor } from "#src/handlers/gates/descriptor";
|
|
import { describePathGate } from "#src/handlers/gates/path";
|
|
import type { ToolCallContext } from "#src/handlers/gates/types";
|
|
import { pathFlavorForPlatform, posixPathFlavor } from "#src/path/path-flavor";
|
|
import { PathNormalizer } from "#src/path-normalizer";
|
|
|
|
import {
|
|
makeGateCheckResult as makeCheckResult,
|
|
makeResolver,
|
|
} from "#test/helpers/gate-fixtures";
|
|
|
|
// ── helpers ────────────────────────────────────────────────────────────────
|
|
|
|
// path.test.ts uses read-tool defaults; the shared makeTcc uses bash defaults.
|
|
function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
|
|
return {
|
|
toolName: "read",
|
|
agentName: null,
|
|
input: { path: ".env" },
|
|
toolCallId: "tc-1",
|
|
cwd: "/test/project",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
// The gate reads the path normalizer (platform + cwd baked in) from the
|
|
// session; here it is bound to the makeTcc default cwd.
|
|
const normalizer = new PathNormalizer(
|
|
pathFlavorForPlatform(process.platform),
|
|
"/test/project",
|
|
);
|
|
|
|
// ── tests ──────────────────────────────────────────────────────────────────
|
|
|
|
describe("describePathGate", () => {
|
|
beforeEach(() => {
|
|
realpathSync.mockReset();
|
|
realpathSync.mockImplementation((p: string) => p);
|
|
});
|
|
|
|
it("returns null for non-path-bearing tools", () => {
|
|
const resolver = makeResolver();
|
|
const result = describePathGate(
|
|
makeTcc({ toolName: "bash", input: { command: "ls" } }),
|
|
resolver,
|
|
normalizer,
|
|
);
|
|
expect(result).toBeNull();
|
|
expect(resolver.resolve).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns null when tool has no extractable path", () => {
|
|
const resolver = makeResolver();
|
|
const result = describePathGate(
|
|
makeTcc({ toolName: "read", input: {} }),
|
|
resolver,
|
|
normalizer,
|
|
);
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("returns null when path check result is allow", () => {
|
|
const resolver = makeResolver(makeCheckResult({ state: "allow" }));
|
|
const result = describePathGate(makeTcc(), resolver, normalizer);
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("returns null when matchedPattern is undefined (universal default)", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({
|
|
state: "ask",
|
|
matchedPattern: undefined,
|
|
source: "special",
|
|
origin: "builtin",
|
|
}),
|
|
);
|
|
const result = describePathGate(makeTcc(), resolver, normalizer);
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("returns GateDescriptor when matchedPattern is defined (explicit path rule)", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({
|
|
state: "ask",
|
|
matchedPattern: "*.env",
|
|
source: "special",
|
|
origin: "global",
|
|
}),
|
|
);
|
|
const result = describePathGate(makeTcc(), resolver, normalizer);
|
|
expect(result).not.toBeNull();
|
|
expect(isGateDescriptor(result)).toBe(true);
|
|
});
|
|
|
|
it("returns GateDescriptor when path check result is deny", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "deny", matchedPattern: "*.env" }),
|
|
);
|
|
const result = describePathGate(makeTcc(), resolver, normalizer);
|
|
expect(result).not.toBeNull();
|
|
expect(isGateDescriptor(result)).toBe(true);
|
|
const desc = result as GateDescriptor;
|
|
expect(desc.surface).toBe("path");
|
|
expect(desc.preCheck?.state).toBe("deny");
|
|
});
|
|
|
|
it("binds a current-directory file's session approval to the cwd subtree", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "ask", matchedPattern: "*" }),
|
|
);
|
|
const result = describePathGate(
|
|
makeTcc({ input: { path: "index.html" }, cwd: "/test/project" }),
|
|
resolver,
|
|
normalizer,
|
|
) as GateDescriptor;
|
|
expect(result.sessionApproval?.surface).toBe("path");
|
|
expect(result.sessionApproval?.representativePattern).toBe(
|
|
"/test/project/*",
|
|
);
|
|
});
|
|
|
|
it("descriptor denialContext references the file path and tool name", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "deny", matchedPattern: "*.env" }),
|
|
);
|
|
const result = describePathGate(
|
|
makeTcc(),
|
|
resolver,
|
|
normalizer,
|
|
) as GateDescriptor;
|
|
expect(result.payload.kind).toBe("path");
|
|
expect(result.payload.request.toolName).toBe("read");
|
|
expect(result.payload.request.value).toBe(".env");
|
|
expect(result.payload.request.matchedPattern).toBe("*.env");
|
|
expect(result.payload.request.requester.agentName).toBeNull();
|
|
});
|
|
|
|
it("carries the child-fixed access facts on promptDetails (path surface)", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "ask", matchedPattern: "*.env" }),
|
|
);
|
|
const result = describePathGate(
|
|
makeTcc(),
|
|
resolver,
|
|
normalizer,
|
|
) as GateDescriptor;
|
|
const accessPath = AccessPath.forPath(".env", {
|
|
cwd: "/test/project",
|
|
flavor: posixPathFlavor,
|
|
});
|
|
expect(result.promptDetails.accessIntent).toEqual({
|
|
surface: "path",
|
|
matchValues: accessPath.matchValues(),
|
|
boundaryValue: accessPath.boundaryValue(),
|
|
});
|
|
});
|
|
|
|
it("descriptor decision uses surface 'path' and the file path as value", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "deny", matchedPattern: "*.env" }),
|
|
);
|
|
const result = describePathGate(
|
|
makeTcc(),
|
|
resolver,
|
|
normalizer,
|
|
) as GateDescriptor;
|
|
expect(result.decision.surface).toBe("path");
|
|
expect(result.decision.value).toBe(".env");
|
|
});
|
|
|
|
it("resolves the path surface with an access-path intent and agent name", () => {
|
|
const resolver = makeResolver(makeCheckResult({ state: "allow" }));
|
|
describePathGate(makeTcc({ agentName: "my-agent" }), resolver, normalizer);
|
|
expect(resolver.resolve).toHaveBeenCalledWith({
|
|
kind: "access-path",
|
|
surface: "path",
|
|
path: AccessPath.forPath(".env", {
|
|
cwd: "/test/project",
|
|
flavor: posixPathFlavor,
|
|
}),
|
|
agentName: "my-agent",
|
|
});
|
|
});
|
|
|
|
it("emits an access-path whose matchValues include the symlink-resolved form (#486)", () => {
|
|
// /test/project/.env is a symlink to /vault/secret.env.
|
|
realpathSync.mockImplementation((p: string) =>
|
|
p === "/test/project/.env" ? "/vault/secret.env" : p,
|
|
);
|
|
const resolver = makeResolver(makeCheckResult({ state: "allow" }));
|
|
describePathGate(makeTcc(), resolver, normalizer);
|
|
|
|
const intent = resolver.resolve.mock.lastCall?.[0];
|
|
expect(intent?.kind).toBe("access-path");
|
|
expect(intent?.kind === "access-path" && intent.path.matchValues()).toEqual(
|
|
["/test/project/.env", ".env", "/vault/secret.env"],
|
|
);
|
|
});
|
|
});
|
|
|
|
// Home-relative path characterization (#350) ──────────────────────────────
|
|
//
|
|
// The gate passes the raw path to the resolver; home expansion is handled
|
|
// downstream by normalizeInput. These tests lock in that the gate works
|
|
// correctly when the tool input contains a ~/... or $HOME/... path.
|
|
|
|
describe("describePathGate — home-relative paths", () => {
|
|
it("passes raw ~/... path to resolver and builds descriptor on deny", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "deny", matchedPattern: "~/.ssh/*" }),
|
|
);
|
|
const result = describePathGate(
|
|
makeTcc({ input: { path: "~/.ssh/config" } }),
|
|
resolver,
|
|
normalizer,
|
|
) as GateDescriptor;
|
|
|
|
expect(isGateDescriptor(result)).toBe(true);
|
|
expect(result.preCheck?.state).toBe("deny");
|
|
// Raw path preserved on the payload for display.
|
|
expect(result.payload.kind).toBe("path");
|
|
expect(result.payload.request.toolName).toBe("read");
|
|
expect(result.payload.request.value).toBe("~/.ssh/config");
|
|
expect(resolver.resolve).toHaveBeenCalledWith({
|
|
kind: "access-path",
|
|
surface: "path",
|
|
path: AccessPath.forPath("~/.ssh/config", {
|
|
cwd: "/test/project",
|
|
flavor: posixPathFlavor,
|
|
}),
|
|
agentName: undefined,
|
|
});
|
|
});
|
|
|
|
it("passes raw $HOME/... path to resolver and builds descriptor on deny", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "deny", matchedPattern: "$HOME/.ssh/*" }),
|
|
);
|
|
const result = describePathGate(
|
|
makeTcc({ input: { path: "$HOME/.ssh/config" } }),
|
|
resolver,
|
|
normalizer,
|
|
) as GateDescriptor;
|
|
|
|
expect(isGateDescriptor(result)).toBe(true);
|
|
expect(result.preCheck?.state).toBe("deny");
|
|
expect(result.payload.kind).toBe("path");
|
|
expect(result.payload.request.value).toBe("$HOME/.ssh/config");
|
|
});
|
|
|
|
it("returns null when home-relative path resolves to allow", () => {
|
|
const resolver = makeResolver(makeCheckResult({ state: "allow" }));
|
|
const result = describePathGate(
|
|
makeTcc({ input: { path: "~/.ssh/config" } }),
|
|
resolver,
|
|
normalizer,
|
|
);
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
// Extension and MCP tools are now path-gated (#352) ──────────────────────────
|
|
|
|
describe("describePathGate — extension and MCP tools (#352)", () => {
|
|
function extractorLookup(toolName: string, key: string) {
|
|
return {
|
|
get: (name: string) =>
|
|
name === toolName
|
|
? (input: Record<string, unknown>) =>
|
|
typeof input[key] === "string" ? input[key] : undefined
|
|
: undefined,
|
|
};
|
|
}
|
|
|
|
it("gates an extension tool that exposes input.path", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "deny", matchedPattern: "*.env" }),
|
|
);
|
|
const result = describePathGate(
|
|
makeTcc({ toolName: "my-ext", input: { path: ".env" } }),
|
|
resolver,
|
|
normalizer,
|
|
);
|
|
expect(isGateDescriptor(result)).toBe(true);
|
|
expect(resolver.resolve).toHaveBeenCalledWith({
|
|
kind: "access-path",
|
|
surface: "path",
|
|
path: AccessPath.forPath(".env", {
|
|
cwd: "/test/project",
|
|
flavor: posixPathFlavor,
|
|
}),
|
|
agentName: undefined,
|
|
});
|
|
});
|
|
|
|
it("gates an MCP tool via arguments.path", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "deny", matchedPattern: "*.env" }),
|
|
);
|
|
const result = describePathGate(
|
|
makeTcc({ toolName: "mcp", input: { arguments: { path: ".env" } } }),
|
|
resolver,
|
|
normalizer,
|
|
);
|
|
expect(isGateDescriptor(result)).toBe(true);
|
|
expect(resolver.resolve).toHaveBeenCalledWith({
|
|
kind: "access-path",
|
|
surface: "path",
|
|
path: AccessPath.forPath(".env", {
|
|
cwd: "/test/project",
|
|
flavor: posixPathFlavor,
|
|
}),
|
|
agentName: undefined,
|
|
});
|
|
});
|
|
|
|
it("uses a registered extractor's path for a custom-shaped tool", () => {
|
|
const resolver = makeResolver(
|
|
makeCheckResult({ state: "deny", matchedPattern: "*" }),
|
|
);
|
|
describePathGate(
|
|
makeTcc({ toolName: "ffgrep", input: { target: "/etc/passwd" } }),
|
|
resolver,
|
|
normalizer,
|
|
extractorLookup("ffgrep", "target"),
|
|
);
|
|
expect(resolver.resolve).toHaveBeenCalledWith({
|
|
kind: "access-path",
|
|
surface: "path",
|
|
path: AccessPath.forPath("/etc/passwd", {
|
|
cwd: "/test/project",
|
|
flavor: posixPathFlavor,
|
|
}),
|
|
agentName: undefined,
|
|
});
|
|
});
|
|
|
|
it("returns null for an extension tool without a path", () => {
|
|
const resolver = makeResolver();
|
|
const result = describePathGate(
|
|
makeTcc({ toolName: "my-ext", input: { other: true } }),
|
|
resolver,
|
|
normalizer,
|
|
);
|
|
expect(result).toBeNull();
|
|
expect(resolver.resolve).not.toHaveBeenCalled();
|
|
});
|
|
});
|