mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-10-26 18:55: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>
167 lines
5.6 KiB
TypeScript
167 lines
5.6 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import type { AccessIntent } from "#src/access-intent/access-intent";
|
|
import { AuthorizerRegistry } from "#src/authority/authorizer-registry";
|
|
import { posixPathFlavor } from "#src/path/path-flavor";
|
|
import { PathNormalizer } from "#src/path-normalizer";
|
|
import { LocalPermissionsService } from "#src/permissions-service";
|
|
import type { PermissionsService } from "#src/service";
|
|
import {
|
|
getPermissionsService,
|
|
publishPermissionsService,
|
|
unpublishPermissionsService,
|
|
} from "#src/service";
|
|
import { ToolAccessExtractorRegistry } from "#src/tool-access-extractor-registry";
|
|
import { ToolInputFormatterRegistry } from "#src/tool-input-formatter-registry";
|
|
import type { PermissionCheckResult, PermissionState } from "#src/types";
|
|
|
|
// ── helpers ────────────────────────────────────────────────────────────────
|
|
|
|
function makeService(
|
|
overrides: Partial<PermissionsService> = {},
|
|
): PermissionsService {
|
|
return {
|
|
checkPermission: vi.fn(),
|
|
getToolPermission: vi.fn(),
|
|
registerToolInputFormatter: vi.fn(),
|
|
registerToolAccessExtractor: vi.fn(),
|
|
registerAuthorizer: vi.fn(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
// ── globalThis accessor ────────────────────────────────────────────────────
|
|
|
|
describe("globalThis accessor", () => {
|
|
afterEach(() => {
|
|
const current = getPermissionsService();
|
|
if (current) {
|
|
unpublishPermissionsService(current);
|
|
}
|
|
});
|
|
|
|
it("returns undefined when nothing has been published", () => {
|
|
expect(getPermissionsService()).toBeUndefined();
|
|
});
|
|
|
|
it("returns the published service", () => {
|
|
const service = makeService();
|
|
publishPermissionsService(service);
|
|
expect(getPermissionsService()).toBe(service);
|
|
});
|
|
|
|
it("overwrites a previously published service", () => {
|
|
const first = makeService();
|
|
const second = makeService();
|
|
publishPermissionsService(first);
|
|
publishPermissionsService(second);
|
|
expect(getPermissionsService()).toBe(second);
|
|
});
|
|
|
|
it("removes the slot when it still holds the given service", () => {
|
|
const service = makeService();
|
|
publishPermissionsService(service);
|
|
unpublishPermissionsService(service);
|
|
expect(getPermissionsService()).toBeUndefined();
|
|
});
|
|
|
|
it("does not remove the slot when a different service occupies it", () => {
|
|
const parent = makeService();
|
|
const child = makeService();
|
|
publishPermissionsService(parent);
|
|
// A child instance never published `parent`; unpublishing its own service
|
|
// must be a no-op that leaves the parent's slot intact.
|
|
unpublishPermissionsService(child);
|
|
expect(getPermissionsService()).toBe(parent);
|
|
});
|
|
|
|
it("unpublish is safe to call when nothing was published", () => {
|
|
expect(() => unpublishPermissionsService(makeService())).not.toThrow();
|
|
expect(getPermissionsService()).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
// ── service adapter delegation ─────────────────────────────────────────────
|
|
|
|
describe("service round-trip through the global slot", () => {
|
|
afterEach(() => {
|
|
const current = getPermissionsService();
|
|
if (current) {
|
|
unpublishPermissionsService(current);
|
|
}
|
|
});
|
|
|
|
const fakeResult: PermissionCheckResult = {
|
|
toolName: "bash",
|
|
state: "allow",
|
|
matchedPattern: "git *",
|
|
source: "bash",
|
|
origin: "global",
|
|
};
|
|
|
|
function makeResolver() {
|
|
return {
|
|
resolve: vi
|
|
.fn<(intent: AccessIntent) => PermissionCheckResult>()
|
|
.mockReturnValue(fakeResult),
|
|
getToolPermission: vi
|
|
.fn<(toolName: string, agentName?: string) => PermissionState>()
|
|
.mockReturnValue("ask"),
|
|
};
|
|
}
|
|
|
|
function publishLocalService(resolver: ReturnType<typeof makeResolver>) {
|
|
publishPermissionsService(
|
|
new LocalPermissionsService(
|
|
resolver,
|
|
{
|
|
getPathNormalizer: () =>
|
|
new PathNormalizer(posixPathFlavor, "/test/project"),
|
|
},
|
|
new ToolInputFormatterRegistry(),
|
|
new ToolAccessExtractorRegistry(),
|
|
new AuthorizerRegistry(),
|
|
),
|
|
);
|
|
}
|
|
|
|
it("resolves a non-path query via a tool intent", () => {
|
|
const resolver = makeResolver();
|
|
publishLocalService(resolver);
|
|
const result = getPermissionsService()!.checkPermission(
|
|
"bash",
|
|
"git push",
|
|
"Explore",
|
|
);
|
|
expect(result).toBe(fakeResult);
|
|
expect(resolver.resolve).toHaveBeenCalledWith({
|
|
kind: "tool",
|
|
surface: "bash",
|
|
input: { command: "git push" },
|
|
agentName: "Explore",
|
|
});
|
|
});
|
|
|
|
it("resolves a path-surface query via an access-path intent", () => {
|
|
const resolver = makeResolver();
|
|
publishLocalService(resolver);
|
|
getPermissionsService()!.checkPermission("read", "/test/project/.env");
|
|
const intent = resolver.resolve.mock.calls[0][0];
|
|
expect(intent.kind).toBe("access-path");
|
|
if (intent.kind === "access-path") {
|
|
expect(intent.surface).toBe("read");
|
|
}
|
|
});
|
|
|
|
it("delegates getToolPermission through the resolver", () => {
|
|
const resolver = makeResolver();
|
|
resolver.getToolPermission.mockReturnValue("deny");
|
|
publishLocalService(resolver);
|
|
const result = getPermissionsService()!.getToolPermission(
|
|
"write",
|
|
"Explore",
|
|
);
|
|
expect(result).toBe("deny");
|
|
expect(resolver.getToolPermission).toHaveBeenCalledWith("write", "Explore");
|
|
});
|
|
});
|