|
| 1 | +# Codebase Search Enforcement Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the mechanisms implemented to ensure that the `codebase_search` tool is properly utilized as the first line of understanding when working with codebase context in Roo Code. |
| 6 | + |
| 7 | +## Implementation Details |
| 8 | + |
| 9 | +### 1. Tool Use Guidelines Section |
| 10 | + |
| 11 | +**File**: `src/core/prompts/sections/tool-use-guidelines.ts` |
| 12 | + |
| 13 | +The function now accepts a `CodeIndexManager` parameter to conditionally include codebase_search enforcement: |
| 14 | +- When codebase_search is available: **IMPORTANT: When starting a new task or when you need to understand existing code/functionality, you MUST use the `codebase_search` tool FIRST before any other search tools.** |
| 15 | +- When unavailable: Standard tool selection guidance without codebase_search enforcement |
| 16 | +- Maintains proper numbering regardless of availability |
| 17 | + |
| 18 | +### 2. Objective Section |
| 19 | + |
| 20 | +**File**: `src/core/prompts/sections/objective.ts` |
| 21 | + |
| 22 | +Enhanced the thinking process in step 3 with conditional enforcement: |
| 23 | +- When codebase_search is available: Requires using `codebase_search` tool first if the task involves understanding existing code or functionality |
| 24 | +- When unavailable: Proceeds directly to analyzing file structure |
| 25 | +- Integrated into the <thinking> tags analysis workflow |
| 26 | + |
| 27 | +### 3. Rules Section |
| 28 | + |
| 29 | +**File**: `src/core/prompts/sections/rules.ts` |
| 30 | + |
| 31 | +Added a conditional critical rule: |
| 32 | +- When codebase_search is available: **CRITICAL: When you need to understand existing code or functionality, ALWAYS use the `codebase_search` tool FIRST before using search_files or other file exploration tools.** |
| 33 | +- When unavailable: No codebase_search rule is included |
| 34 | +- search_files guidance adjusts accordingly (mentions "after codebase_search" only when available) |
| 35 | + |
| 36 | +### 4. Mode-Specific Instructions |
| 37 | + |
| 38 | +**File**: `src/shared/modes.ts` |
| 39 | + |
| 40 | +Updated the architect mode's custom instructions: |
| 41 | +- Modified step 1 to explicitly state: **ALWAYS start with the `codebase_search` tool (if available) to understand existing functionality and code structure before using other tools like read_file or search_files.** |
| 42 | +- This ensures that even in architect mode, which is focused on planning, the codebase_search tool is prioritized when available |
| 43 | + |
| 44 | +### 5. System Prompt Integration |
| 45 | + |
| 46 | +**File**: `src/core/prompts/system.ts` |
| 47 | + |
| 48 | +Updated to pass `CodeIndexManager` to the relevant sections: |
| 49 | +- `getToolUseGuidelinesSection(codeIndexManager)` |
| 50 | +- `getObjectiveSection(codeIndexManager)` |
| 51 | +- `getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, codeIndexManager)` |
| 52 | + |
| 53 | +## How It Works |
| 54 | + |
| 55 | +The enforcement works through multiple layers: |
| 56 | + |
| 57 | +1. **Conditional Availability Check**: The system checks if `CodeIndexManager` is enabled, configured, and initialized before including codebase_search guidance. |
| 58 | + |
| 59 | +2. **System Prompt Level**: The tool use guidelines and objective sections conditionally include codebase_search enforcement based on availability. |
| 60 | + |
| 61 | +3. **Mode-Specific Level**: Individual modes (like architect) have their custom instructions updated to reinforce the codebase_search-first approach when available. |
| 62 | + |
| 63 | +4. **User Custom Instructions**: The user's global instruction "Always use codebase_search before any other search first" provides an additional layer of enforcement. |
| 64 | + |
| 65 | +## Benefits |
| 66 | + |
| 67 | +1. **Better Context Understanding**: By using semantic search first, the AI can find functionally relevant code even without knowing exact keywords or file names. |
| 68 | + |
| 69 | +2. **Efficiency**: Reduces the need for multiple regex searches or file explorations by finding the most relevant code upfront. |
| 70 | + |
| 71 | +3. **Consistency**: Ensures a standardized approach to understanding codebase context across all modes and tasks. |
| 72 | + |
| 73 | +4. **Graceful Degradation**: The system only mentions codebase_search when it's actually available, preventing confusion when the feature is disabled. |
| 74 | + |
| 75 | +## Testing |
| 76 | + |
| 77 | +Test files have been created to verify the implementation: |
| 78 | +- `src/core/prompts/sections/__tests__/tool-use-guidelines.test.ts` |
| 79 | +- `src/core/prompts/sections/__tests__/objective.test.ts` |
| 80 | + |
| 81 | +These tests verify both scenarios: |
| 82 | +- When codebase_search is available (enforcement is included) |
| 83 | +- When codebase_search is not available (enforcement is excluded) |
| 84 | + |
| 85 | +## Future Considerations |
| 86 | + |
| 87 | +1. The enforcement only applies when the `codebase_search` tool is available (i.e., when the code index feature is enabled, configured, and initialized). |
| 88 | + |
| 89 | +2. The system gracefully falls back to other search methods if `codebase_search` is not available, without mentioning it in the prompts. |
| 90 | + |
| 91 | +3. The enforcement is designed to guide behavior without being overly restrictive - it emphasizes "MUST" for new tasks and understanding existing code, but allows flexibility for other scenarios. |
0 commit comments