Skip to content

Commit d609164

Browse files
committed
feat(prompts): enforce codebase_search as primary code understanding tool
- Add conditional codebase_search enforcement in tool use guidelines - Modify objective section to prioritize codebase_search when available - Update rules section with critical codebase_search-first rule - Pass CodeIndexManager to prompt sections for availability checks - Ensure graceful degradation when codebase_search is unavailable
1 parent dd295d9 commit d609164

File tree

8 files changed

+323
-27
lines changed

8 files changed

+323
-27
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.

src/core/prompts/__tests__/__snapshots__/system.test.ts.snap

Lines changed: 27 additions & 14 deletions
Large diffs are not rendered by default.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { getObjectiveSection } from "../objective"
2+
import { CodeIndexManager } from "../../../../services/code-index/manager"
3+
4+
describe("getObjectiveSection", () => {
5+
// Mock CodeIndexManager with codebase search available
6+
const mockCodeIndexManagerEnabled = {
7+
isFeatureEnabled: true,
8+
isFeatureConfigured: true,
9+
isInitialized: true,
10+
} as CodeIndexManager
11+
12+
// Mock CodeIndexManager with codebase search unavailable
13+
const mockCodeIndexManagerDisabled = {
14+
isFeatureEnabled: false,
15+
isFeatureConfigured: false,
16+
isInitialized: false,
17+
} as CodeIndexManager
18+
19+
describe("when codebase_search is available", () => {
20+
it("should include codebase_search first enforcement in thinking process", () => {
21+
const objective = getObjectiveSection(mockCodeIndexManagerEnabled)
22+
23+
// Check that the objective includes the codebase_search enforcement
24+
expect(objective).toContain("if the task involves understanding existing code or functionality, you MUST use the `codebase_search` tool")
25+
expect(objective).toContain("BEFORE using any other search or file exploration tools")
26+
})
27+
})
28+
29+
describe("when codebase_search is not available", () => {
30+
it("should not include codebase_search enforcement", () => {
31+
const objective = getObjectiveSection(mockCodeIndexManagerDisabled)
32+
33+
// Check that the objective does not include the codebase_search enforcement
34+
expect(objective).not.toContain("you MUST use the `codebase_search` tool")
35+
expect(objective).not.toContain("BEFORE using any other search or file exploration tools")
36+
})
37+
})
38+
39+
it("should maintain proper structure regardless of codebase_search availability", () => {
40+
const objectiveEnabled = getObjectiveSection(mockCodeIndexManagerEnabled)
41+
const objectiveDisabled = getObjectiveSection(mockCodeIndexManagerDisabled)
42+
43+
// Check that all numbered items are present in both cases
44+
for (const objective of [objectiveEnabled, objectiveDisabled]) {
45+
expect(objective).toContain("1. Analyze the user's task")
46+
expect(objective).toContain("2. Work through these goals sequentially")
47+
expect(objective).toContain("3. Remember, you have extensive capabilities")
48+
expect(objective).toContain("4. Once you've completed the user's task")
49+
expect(objective).toContain("5. The user may provide feedback")
50+
}
51+
})
52+
53+
it("should include thinking tags guidance regardless of codebase_search availability", () => {
54+
const objectiveEnabled = getObjectiveSection(mockCodeIndexManagerEnabled)
55+
const objectiveDisabled = getObjectiveSection(mockCodeIndexManagerDisabled)
56+
57+
// Check that thinking tags guidance is included in both cases
58+
for (const objective of [objectiveEnabled, objectiveDisabled]) {
59+
expect(objective).toContain("<thinking></thinking> tags")
60+
expect(objective).toContain("analyze the file structure provided in environment_details")
61+
expect(objective).toContain("think about which of the provided tools is the most relevant")
62+
}
63+
})
64+
65+
it("should include parameter inference guidance regardless of codebase_search availability", () => {
66+
const objectiveEnabled = getObjectiveSection(mockCodeIndexManagerEnabled)
67+
const objectiveDisabled = getObjectiveSection(mockCodeIndexManagerDisabled)
68+
69+
// Check parameter inference guidance in both cases
70+
for (const objective of [objectiveEnabled, objectiveDisabled]) {
71+
expect(objective).toContain("Go through each of the required parameters")
72+
expect(objective).toContain("determine if the user has directly provided or given enough information to infer a value")
73+
expect(objective).toContain("DO NOT invoke the tool (not even with fillers for the missing params)")
74+
expect(objective).toContain("ask_followup_question tool")
75+
}
76+
})
77+
})
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { getToolUseGuidelinesSection } from "../tool-use-guidelines"
2+
import { CodeIndexManager } from "../../../../services/code-index/manager"
3+
4+
describe("getToolUseGuidelinesSection", () => {
5+
// Mock CodeIndexManager with codebase search available
6+
const mockCodeIndexManagerEnabled = {
7+
isFeatureEnabled: true,
8+
isFeatureConfigured: true,
9+
isInitialized: true,
10+
} as CodeIndexManager
11+
12+
// Mock CodeIndexManager with codebase search unavailable
13+
const mockCodeIndexManagerDisabled = {
14+
isFeatureEnabled: false,
15+
isFeatureConfigured: false,
16+
isInitialized: false,
17+
} as CodeIndexManager
18+
19+
describe("when codebase_search is available", () => {
20+
it("should include codebase_search first enforcement", () => {
21+
const guidelines = getToolUseGuidelinesSection(mockCodeIndexManagerEnabled)
22+
23+
// Check that the guidelines include the codebase_search enforcement
24+
expect(guidelines).toContain("IMPORTANT: When starting a new task or when you need to understand existing code/functionality, you MUST use the `codebase_search` tool FIRST")
25+
expect(guidelines).toContain("before any other search tools")
26+
expect(guidelines).toContain("semantic search tool helps you find relevant code based on meaning rather than just keywords")
27+
})
28+
29+
it("should maintain proper numbering with codebase_search", () => {
30+
const guidelines = getToolUseGuidelinesSection(mockCodeIndexManagerEnabled)
31+
32+
// Check that all numbered items are present
33+
expect(guidelines).toContain("1. In <thinking> tags")
34+
expect(guidelines).toContain("2. **IMPORTANT:")
35+
expect(guidelines).toContain("3. Choose the most appropriate tool")
36+
expect(guidelines).toContain("3. If multiple actions are needed")
37+
expect(guidelines).toContain("4. Formulate your tool use")
38+
expect(guidelines).toContain("5. After each tool use")
39+
expect(guidelines).toContain("6. ALWAYS wait for user confirmation")
40+
})
41+
})
42+
43+
describe("when codebase_search is not available", () => {
44+
it("should not include codebase_search enforcement", () => {
45+
const guidelines = getToolUseGuidelinesSection(mockCodeIndexManagerDisabled)
46+
47+
// Check that the guidelines do not include the codebase_search enforcement
48+
expect(guidelines).not.toContain("IMPORTANT: When starting a new task or when you need to understand existing code/functionality, you MUST use the `codebase_search` tool FIRST")
49+
expect(guidelines).not.toContain("semantic search tool helps you find relevant code based on meaning")
50+
})
51+
52+
it("should maintain proper numbering without codebase_search", () => {
53+
const guidelines = getToolUseGuidelinesSection(mockCodeIndexManagerDisabled)
54+
55+
// Check that all numbered items are present with correct numbering
56+
expect(guidelines).toContain("1. In <thinking> tags")
57+
expect(guidelines).toContain("2. Choose the most appropriate tool")
58+
expect(guidelines).toContain("3. If multiple actions are needed")
59+
expect(guidelines).toContain("4. Formulate your tool use")
60+
expect(guidelines).toContain("5. After each tool use")
61+
expect(guidelines).toContain("6. ALWAYS wait for user confirmation")
62+
})
63+
})
64+
65+
it("should include iterative process guidelines regardless of codebase_search availability", () => {
66+
const guidelinesEnabled = getToolUseGuidelinesSection(mockCodeIndexManagerEnabled)
67+
const guidelinesDisabled = getToolUseGuidelinesSection(mockCodeIndexManagerDisabled)
68+
69+
// Check that the iterative process section is included in both cases
70+
for (const guidelines of [guidelinesEnabled, guidelinesDisabled]) {
71+
expect(guidelines).toContain("It is crucial to proceed step-by-step")
72+
expect(guidelines).toContain("1. Confirm the success of each step before proceeding")
73+
expect(guidelines).toContain("2. Address any issues or errors that arise immediately")
74+
expect(guidelines).toContain("3. Adapt your approach based on new information")
75+
expect(guidelines).toContain("4. Ensure that each action builds correctly")
76+
}
77+
})
78+
})
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
export function getObjectiveSection(): string {
1+
import { CodeIndexManager } from "../../../services/code-index/manager"
2+
3+
export function getObjectiveSection(codeIndexManager?: CodeIndexManager): string {
4+
const isCodebaseSearchAvailable = codeIndexManager &&
5+
codeIndexManager.isFeatureEnabled &&
6+
codeIndexManager.isFeatureConfigured &&
7+
codeIndexManager.isInitialized
8+
9+
const codebaseSearchInstruction = isCodebaseSearchAvailable
10+
? "First, if the task involves understanding existing code or functionality, you MUST use the `codebase_search` tool to search for relevant code based on the task's intent BEFORE using any other search or file exploration tools. Then, "
11+
: "First, "
12+
213
return `====
314
415
OBJECTIVE
@@ -7,7 +18,7 @@ You accomplish a given task iteratively, breaking it down into clear steps and w
718
819
1. Analyze the user's task and set clear, achievable goals to accomplish it. Prioritize these goals in a logical order.
920
2. Work through these goals sequentially, utilizing available tools one at a time as necessary. Each goal should correspond to a distinct step in your problem-solving process. You will be informed on the work completed and what's remaining as you go.
10-
3. Remember, you have extensive capabilities with access to a wide range of tools that can be used in powerful and clever ways as necessary to accomplish each goal. Before calling a tool, do some analysis within <thinking></thinking> tags. First, analyze the file structure provided in environment_details to gain context and insights for proceeding effectively. Then, think about which of the provided tools is the most relevant tool to accomplish the user's task. Next, go through each of the required parameters of the relevant tool and determine if the user has directly provided or given enough information to infer a value. When deciding if the parameter can be inferred, carefully consider all the context to see if it supports a specific value. If all of the required parameters are present or can be reasonably inferred, close the thinking tag and proceed with the tool use. BUT, if one of the values for a required parameter is missing, DO NOT invoke the tool (not even with fillers for the missing params) and instead, ask the user to provide the missing parameters using the ask_followup_question tool. DO NOT ask for more information on optional parameters if it is not provided.
21+
3. Remember, you have extensive capabilities with access to a wide range of tools that can be used in powerful and clever ways as necessary to accomplish each goal. Before calling a tool, do some analysis within <thinking></thinking> tags. ${codebaseSearchInstruction}analyze the file structure provided in environment_details to gain context and insights for proceeding effectively. Next, think about which of the provided tools is the most relevant tool to accomplish the user's task. Go through each of the required parameters of the relevant tool and determine if the user has directly provided or given enough information to infer a value. When deciding if the parameter can be inferred, carefully consider all the context to see if it supports a specific value. If all of the required parameters are present or can be reasonably inferred, close the thinking tag and proceed with the tool use. BUT, if one of the values for a required parameter is missing, DO NOT invoke the tool (not even with fillers for the missing params) and instead, ask the user to provide the missing parameters using the ask_followup_question tool. DO NOT ask for more information on optional parameters if it is not provided.
1122
4. Once you've completed the user's task, you must use the attempt_completion tool to present the result of the task to the user. You may also provide a CLI command to showcase the result of your task; this can be particularly useful for web development tasks, where you can run e.g. \`open index.html\` to show the website you've built.
1223
5. The user may provide feedback, which you can use to make improvements and try again. But DO NOT continue in pointless back and forth conversations, i.e. don't end your responses with questions or offers for further assistance.`
1324
}

0 commit comments

Comments
 (0)