From 311d5ee8b2ad4a18391b455b34cd8967d7f2adc9 Mon Sep 17 00:00:00 2001 From: Chad Metcalf Date: Mon, 4 Aug 2025 10:15:16 -0700 Subject: [PATCH] fix: prevent Bash tool from being presented in plan mode Fix service initialization race condition where getAllTools() would return all tools when ToolPermissionService wasn't ready, causing plan mode to incorrectly include excluded tools like Bash. The issue occurred in GitHub Actions where `cn -p` (plan mode) should exclude Bash tool, but due to timing issues, Bash was being presented to the LLM and then failing during execution with permission errors. Changes: - Fix getAllTools() to properly pass permissions to filterExcludedTools() - Throw descriptive error when service not initialized instead of fallback - Add comprehensive regression tests covering all modes and edge cases Fixes automated PR review failures in GitHub Actions workflows. --- src/streamChatResponse.getAllTools.test.ts | 165 +++++++++++++++++++++ src/streamChatResponse.ts | 9 +- 2 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 src/streamChatResponse.getAllTools.test.ts diff --git a/src/streamChatResponse.getAllTools.test.ts b/src/streamChatResponse.getAllTools.test.ts new file mode 100644 index 000000000..7cb3fc1d6 --- /dev/null +++ b/src/streamChatResponse.getAllTools.test.ts @@ -0,0 +1,165 @@ +import { describe, test, expect, beforeEach } from 'vitest'; +import { initializeServices, getServiceSync, SERVICE_NAMES } from './services/index.js'; +import { serviceContainer } from './services/ServiceContainer.js'; +import { getAllTools } from './streamChatResponse.js'; +import type { ToolPermissionServiceState } from './services/ToolPermissionService.js'; + +describe('getAllTools - Tool Filtering', () => { + beforeEach(() => { + // Clean up service container state before each test + const services = [ + SERVICE_NAMES.TOOL_PERMISSIONS, + SERVICE_NAMES.AUTH, + SERVICE_NAMES.API_CLIENT, + SERVICE_NAMES.CONFIG, + SERVICE_NAMES.MODEL, + SERVICE_NAMES.MCP + ]; + + services.forEach(service => { + // Reset service state + (serviceContainer as any).services.delete(service); + (serviceContainer as any).factories.delete(service); + (serviceContainer as any).dependencies.delete(service); + }); + }); + + test('should exclude Bash tool in plan mode after service initialization', async () => { + // Initialize services in plan mode (simulating `cn -p`) + await initializeServices({ + headless: true, + toolPermissionOverrides: { + mode: 'plan' + } + }); + + // Verify service is ready + const serviceResult = getServiceSync(SERVICE_NAMES.TOOL_PERMISSIONS); + expect(serviceResult.state).toBe('ready'); + expect(serviceResult.value?.currentMode).toBe('plan'); + + // Get available tools - this should exclude Bash in plan mode + const tools = getAllTools(); + const toolNames = tools.map(t => t.function.name); + + // Bash should be excluded in plan mode + expect(toolNames).not.toContain('Bash'); + + // Read-only tools should still be available + expect(toolNames).toContain('Read'); + expect(toolNames).toContain('List'); + expect(toolNames).toContain('Search'); + expect(toolNames).toContain('Fetch'); + expect(toolNames).toContain('Checklist'); + + // Write tools should be excluded + expect(toolNames).not.toContain('Write'); + expect(toolNames).not.toContain('Edit'); + }); + + test('should include Bash tool in normal mode', async () => { + // Initialize services in normal mode + await initializeServices({ + headless: true, + toolPermissionOverrides: { + mode: 'normal' + } + }); + + // Verify service is ready + const serviceResult = getServiceSync(SERVICE_NAMES.TOOL_PERMISSIONS); + expect(serviceResult.state).toBe('ready'); + expect(serviceResult.value?.currentMode).toBe('normal'); + + // Get available tools - Bash should be available in normal mode + const tools = getAllTools(); + const toolNames = tools.map(t => t.function.name); + + // All tools should be available in normal mode + expect(toolNames).toContain('Bash'); + expect(toolNames).toContain('Read'); + expect(toolNames).toContain('Write'); + expect(toolNames).toContain('Edit'); + }); + + test('should include all tools in auto mode', async () => { + // Initialize services in auto mode + await initializeServices({ + headless: true, + toolPermissionOverrides: { + mode: 'auto' + } + }); + + // Verify service is ready + const serviceResult = getServiceSync(SERVICE_NAMES.TOOL_PERMISSIONS); + expect(serviceResult.state).toBe('ready'); + expect(serviceResult.value?.currentMode).toBe('auto'); + + // Get available tools - all tools should be available in auto mode + const tools = getAllTools(); + const toolNames = tools.map(t => t.function.name); + + // All tools should be available in auto mode + expect(toolNames).toContain('Bash'); + expect(toolNames).toContain('Read'); + expect(toolNames).toContain('Write'); + expect(toolNames).toContain('Edit'); + }); + + test('should throw error when ToolPermissionService is not initialized', () => { + // Don't initialize services - this should cause getAllTools to throw + expect(() => { + getAllTools(); + }).toThrow('ToolPermissionService not initialized. Services must be initialized before requesting tools.'); + }); + + test('should respect explicit exclude in normal mode', async () => { + // Initialize services in normal mode with Read tool explicitly excluded + await initializeServices({ + headless: true, + toolPermissionOverrides: { + mode: 'normal', + exclude: ['Read'] + } + }); + + const tools = getAllTools(); + const toolNames = tools.map(t => t.function.name); + + // Read should be excluded due to explicit exclude + expect(toolNames).not.toContain('Read'); + + // Other tools should still be available in normal mode + expect(toolNames).toContain('Bash'); + expect(toolNames).toContain('Write'); + expect(toolNames).toContain('List'); + expect(toolNames).toContain('Search'); + }); + + test('plan mode should override allow flags (regression test for GitHub Actions issue)', async () => { + // This test specifically addresses the original issue where plan mode + // wasn't properly excluding tools despite being in plan mode + await initializeServices({ + headless: true, + toolPermissionOverrides: { + mode: 'plan', + allow: ['Write', 'Bash'] // These should be ignored in plan mode + } + }); + + const tools = getAllTools(); + const toolNames = tools.map(t => t.function.name); + + // Plan mode should still exclude write tools despite --allow flags + // This tests that plan mode policies have absolute precedence + expect(toolNames).not.toContain('Write'); + expect(toolNames).not.toContain('Bash'); + expect(toolNames).not.toContain('Edit'); + + // Read-only tools should be available + expect(toolNames).toContain('Read'); + expect(toolNames).toContain('List'); + expect(toolNames).toContain('Search'); + }); +}); \ No newline at end of file diff --git a/src/streamChatResponse.ts b/src/streamChatResponse.ts index fdde012ef..6a9f42ad9 100644 --- a/src/streamChatResponse.ts +++ b/src/streamChatResponse.ts @@ -56,11 +56,12 @@ export function getAllTools() { let allowedToolNames: string[]; if (serviceResult.state === 'ready' && serviceResult.value) { // Filter out excluded tools based on permissions - allowedToolNames = filterExcludedTools(allToolNames); + allowedToolNames = filterExcludedTools(allToolNames, serviceResult.value.permissions); } else { - // Service not ready - return all tools but log a warning - logger.warn("ToolPermissionService not ready in getAllTools, returning all tools"); - allowedToolNames = allToolNames; + // Service not ready - this is a critical error since tools should only be + // requested after services are properly initialized + logger.error("ToolPermissionService not ready in getAllTools - this indicates a service initialization timing issue"); + throw new Error("ToolPermissionService not initialized. Services must be initialized before requesting tools."); } const allowedToolNamesSet = new Set(allowedToolNames);