Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(auto-review): PowerShell 不再被静默拒绝,未知工具改为交审阅器裁决

Chris5bc43955e157
diff --git a/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts b/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
index 5ce92c9dce..3f657bd527 100644
--- a/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
+++ b/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
@@ -262,3 +262,51 @@ describe('classifyBuiltinToolForAutoReview — 外发与未知', () => {
expect(verdict('mcp__srv__tool', {})).toBe('prompt'); // 理论上不会传 MCP 进来,兜底也 fail-closed
});
});
+
+describe('工具映射漏项不得变成静默拒绝', () => {
+ it('PowerShell 归入 exec,且补解释器前缀让红线判据真正生效', () => {
+ // 漏掉它的后果不是「少审一个工具」而是落到兜底 other → 证据不足 → 直接 block,
+ // Windows 用户在 Auto 档下用 PowerShell 是坏的。
+ expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: 'Get-ChildItem' }))
+ .toEqual({ kind: 'exec', command: 'pwsh -Command Get-ChildItem' });
+
+ // core 的 powerShellNeedsConsent 要求命令以 pwsh/powershell 开头才认 —— 不补前缀
+ // 的话 POWERSHELL_DANGER_PATTERNS 一条都匹配不上,红线形同虚设。
+ expect(verdict('PowerShell', { command: 'Remove-Item -Recurse -Force C:\\' }))
+ .toBe('prompt-each-time');
+ expect(verdict('PowerShell', { command: 'Invoke-Expression $payload' }))
+ .toBe('prompt-each-time');
+
+ // 与「Bash 里调 powershell」两种入口给出一致结论。
+ expect(verdict('Bash', { command: 'pwsh -Command Remove-Item -Recurse -Force C:\\' }))
+ .toBe('prompt-each-time');
+
+ // 模型自带前缀时不重复包装。
+ expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: 'pwsh -c Get-Date' }))
+ .toEqual({ kind: 'exec', command: 'pwsh -c Get-Date' });
+
+ // 空命令仍按证据不足处理,不拼出一个只有前缀的假命令。
+ expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: ' ' }))
+ .toEqual({ kind: 'exec', command: '' });
+ });
+
+ it('兜底 other 必须带 description,否则会在调模型前被判证据不足', () => {
+ const action = normalizeBuiltinToolForAutoReview('SomeFutureTool', { anything: 1 });
+ expect(action.kind).toBe('other');
+ // 有 description = 能进审阅器裁决;没有 = missingReviewEvidence 直接 block。
+ expect(action.kind === 'other' && action.description?.trim()).toBeTruthy();
+ // 描述里带工具名,便于审阅器判断这类动作。
+ expect(action.kind === 'other' && action.description).toContain('SomeFutureTool');
+ });
+
+ it('兜底 description 不得泄漏入参内容', () => {
+ // description 会进 reviewer prompt;入参可能含文件内容、凭证或用户数据。
+ const action = normalizeBuiltinToolForAutoReview('SomeFutureTool', {
+ secret: 'sk-live-abcdef123456',
+ path: '/Users/me/.ssh/id_ed25519',
+ });
+ const description = action.kind === 'other' ? action.description ?? '' : '';
+ expect(description).not.toContain('sk-live-abcdef123456');
+ expect(description).not.toContain('id_ed25519');
+ });
+});
diff --git a/packages/maker-core/src/agents/claude-code/auto-review-policy.ts b/packages/maker-core/src/agents/claude-code/auto-review-policy.ts
index 491f65057c..ac6e49eab2 100644
--- a/packages/maker-core/src/agents/claude-code/auto-review-policy.ts
+++ b/packages/maker-core/src/agents/claude-code/auto-review-policy.ts
@@ -45,6 +45,24 @@ const FILE_WRITE_TOOLS: ReadonlySet<string> = new Set([
'Write', 'Edit', 'MultiEdit', 'NotebookEdit',
]);
+/**
+ * PowerShell 工具的 `command` 是**裸** PowerShell 语句(如 `Remove-Item -Recurse x`),
+ * 而 core 的 `powerShellNeedsConsent` 判据要求命令以 `pwsh` / `powershell` 开头才认
+ * (它服务的是 Bash 里写 `powershell -c "…"` 那种形态)。直接把裸语句当 exec 传下去,
+ * POWERSHELL_DANGER_PATTERNS 一条都匹配不上 —— 红线形同虚设。
+ *
+ * 补上解释器前缀,让同一份判据对「PowerShell 工具」和「Bash 调 powershell」两种
+ * 入口给出一致结论。用 `-Command` 是 pwsh 的规范长写法,判据按前缀名识别、不依赖
+ * 具体参数拼写。
+ */
+function powerShellExecCommand(command: string): string {
+ const trimmed = command.trim();
+ if (!trimmed) return '';
+ // 已经自带解释器前缀的(模型偶尔会写全)不重复包装,避免 `pwsh -c pwsh -c …`。
+ if (/^(?:pwsh|powershell)(?:\.exe)?\b/i.test(trimmed)) return trimmed;
+ return `pwsh -Command ${trimmed}`;
+}
+
function extractFilePath(toolName: string, input: unknown): string | undefined {
const obj = input as Record<string, unknown> | null;
if (!obj) return undefined;
@@ -114,9 +132,17 @@ export function normalizeBuiltinToolForAutoReview(
if (FILE_WRITE_TOOLS.has(toolName)) {
return { kind: 'file-write', path: extractFilePath(toolName, input) };
}
+ // Bash / PowerShell 都是「跑一段命令文本」,判据完全相同 —— 归一到 exec 让
+ // classifyShellCommand 的红线(含 POWERSHELL_DANGER_PATTERNS)真正生效。
+ // 漏掉 PowerShell 的后果不是「少审一个工具」而是**静默拒绝**:它会落到下面
+ // 的兜底 other,而无 description 的 other 在 missingReviewEvidence 处直接
+ // block、连模型都不问 —— Windows 用户在 Auto 档下用 PowerShell 是坏的。
if (toolName === 'Bash') {
return { kind: 'exec', command: extractCommand(input) };
}
+ if (toolName === 'PowerShell') {
+ return { kind: 'exec', command: powerShellExecCommand(extractCommand(input)) };
+ }
// WebFetch/WebSearch:把 URL/搜索词送往外部(exfil 面)→ 升级。
if (toolName === 'WebFetch' || toolName === 'WebSearch') {
return {
@@ -125,6 +151,17 @@ export function normalizeBuiltinToolForAutoReview(
target: extractNetworkTarget(toolName, input),
};
}
- // 未知 / 其它一切工具 → fail-closed 升级。
- return { kind: 'other' };
+ // 未知 / 其它一切工具 → 升级给审阅器裁决。
+ //
+ // **必须带 description**:裸 `{ kind: 'other' }` 会在 missingReviewEvidence
+ // (shared/auto-review-decision.ts)被判为「证据不足」→ 在调模型**之前**直接
+ // block。那不是「fail-closed 升级」而是静默拒绝:用户既看不到卡也没有理由,
+ // 而 SDK 每加一个内置工具就会复发一次(实测 PowerShell 已中)。
+ //
+ // 只带工具名,不带入参 —— 入参可能含文件内容、凭证或用户数据,而 description
+ // 会进 reviewer prompt。工具名足以让审阅器判断这类动作该不该放行。
+ return {
+ kind: 'other',
+ description: `Claude Code built-in tool "${toolName}" (not individually classified by Cindy)`,
+ };
}