Merge pull request #153 from continuedev/fix/plan-mode-tool-filtering
fix: prevent Bash tool from being presented in plan mode
This commit is contained in:
commit
5aeb47fcb6
2 changed files with 170 additions and 4 deletions
165
src/streamChatResponse.getAllTools.test.ts
Normal file
165
src/streamChatResponse.getAllTools.test.ts
Normal file
|
|
@ -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<ToolPermissionServiceState>(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<ToolPermissionServiceState>(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<ToolPermissionServiceState>(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');
|
||||
});
|
||||
});
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue