From d95002f64e46a2aed4e95e1371bc5be00a3bec51 Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" Date: Tue, 14 Jul 2026 14:08:51 -0400 Subject: [PATCH] fix(pi): stop recording allowed scoped rm as `bash denied` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit evaluateToolCall consulted wizardCanUseTool before applying the scoped-rm rescue (#834), and wizardCanUseTool captures a `bash denied` analytics event on every deny — so every allowed `rm .posthog-events.json` still showed up in telemetry as a denial, indistinguishable from a real block. Decide the rescue first and skip the allowlist entirely for a scoped rm. The gate decision is unchanged (a rescued rm overrode any deny before, and it still falls through to the YARA scan); only the spurious telemetry goes away. Generated-By: PostHog Code Task-Id: 3d7ba1ec-5512-45cb-ac94-2e88f074e7d5 --- .../harness/pi/__tests__/security.test.ts | 17 +++++++++++++++++ src/lib/agent/runner/harness/pi/security.ts | 18 +++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/lib/agent/runner/harness/pi/__tests__/security.test.ts b/src/lib/agent/runner/harness/pi/__tests__/security.test.ts index aaf4f8c97..e090dcf1f 100644 --- a/src/lib/agent/runner/harness/pi/__tests__/security.test.ts +++ b/src/lib/agent/runner/harness/pi/__tests__/security.test.ts @@ -484,6 +484,23 @@ describe('pi-security: plain rm matches the anthropic arm', () => { ); }); + test('an allowed scoped rm never captures a `bash denied` event', async () => { + // wizardCanUseTool records analytics on every deny, so the rescue must be + // decided BEFORE consulting it — otherwise each allowed rm still shows up + // in telemetry as denied. + const { analytics } = await import('@utils/analytics'); + const capture = vi.spyOn(analytics, 'wizardCapture'); + try { + expect(await rmBlocked('rm .posthog-events.json')).toBe(false); + expect(capture).not.toHaveBeenCalledWith( + 'bash denied', + expect.anything(), + ); + } finally { + capture.mockRestore(); + } + }); + test('normalizes a relative workingDirectory', async () => { const relRoot = path.relative(process.cwd(), path.resolve('/project')); expect( diff --git a/src/lib/agent/runner/harness/pi/security.ts b/src/lib/agent/runner/harness/pi/security.ts index edf7241e8..8f208ca9d 100644 --- a/src/lib/agent/runner/harness/pi/security.ts +++ b/src/lib/agent/runner/harness/pi/security.ts @@ -303,20 +303,24 @@ export async function evaluateToolCall( llmProvider?: LLMProvider, ): Promise { try { - const policy = toClaudePolicyCall(toolName, input); - const decision = wizardCanUseTool(policy.name, policy.input, { - disallowedTools: ctx.disallowedTools, - wizardAskPending: ctx.getWizardAskPending?.() ?? false, - }); // The allowlist is a pi-only restriction; the anthropic arm runs bash // unrestricted and leans on the shared YARA scan. Let a plain `rm` of // project files through to that same scan so pi matches that behavior. + // Checked BEFORE wizardCanUseTool: its deny path captures a `bash denied` + // analytics event, which would record every allowed scoped rm as denied. const allowedLikeAnthropic = toolName === 'bash' && isScopedFileRemoval(str(input.command), ctx.workingDirectory); - if (decision.behavior === 'deny' && !allowedLikeAnthropic) { - return { block: true, reason: decision.message }; + if (!allowedLikeAnthropic) { + const policy = toClaudePolicyCall(toolName, input); + const decision = wizardCanUseTool(policy.name, policy.input, { + disallowedTools: ctx.disallowedTools, + wizardAskPending: ctx.getWizardAskPending?.() ?? false, + }); + if (decision.behavior === 'deny') { + return { block: true, reason: decision.message }; + } } const yaraReason = await preExecutionYaraBlock(