From 0028c567112a473f21a0694f7a7c6d5f8fe3cab7 Mon Sep 17 00:00:00 2001 From: Hannes Rudolph Date: Tue, 8 Jul 2025 21:12:53 -0600 Subject: [PATCH] fix: strengthen codebase_search enforcement throughout conversations (#5486) --- .../prompts/sections/__tests__/objective.spec.ts | 3 ++- .../sections/__tests__/tool-use-guidelines.spec.ts | 12 ++++++------ src/core/prompts/sections/objective.ts | 2 +- src/core/prompts/sections/rules.ts | 2 +- src/core/prompts/sections/tool-use-guidelines.ts | 4 ++-- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/core/prompts/sections/__tests__/objective.spec.ts b/src/core/prompts/sections/__tests__/objective.spec.ts index 6c5517e5f4..e129392925 100644 --- a/src/core/prompts/sections/__tests__/objective.spec.ts +++ b/src/core/prompts/sections/__tests__/objective.spec.ts @@ -22,9 +22,10 @@ describe("getObjectiveSection", () => { // Check that the objective includes the codebase_search enforcement expect(objective).toContain( - "if the task involves understanding existing code or functionality, you MUST use the `codebase_search` tool", + "for ANY exploration of code you haven't examined yet in this conversation, you MUST use the `codebase_search` tool", ) expect(objective).toContain("BEFORE using any other search or file exploration tools") + expect(objective).toContain("This applies throughout the entire task, not just at the beginning") }) }) diff --git a/src/core/prompts/sections/__tests__/tool-use-guidelines.spec.ts b/src/core/prompts/sections/__tests__/tool-use-guidelines.spec.ts index f08bd475d8..98e4da3a73 100644 --- a/src/core/prompts/sections/__tests__/tool-use-guidelines.spec.ts +++ b/src/core/prompts/sections/__tests__/tool-use-guidelines.spec.ts @@ -22,11 +22,11 @@ describe("getToolUseGuidelinesSection", () => { // Check that the guidelines include the codebase_search enforcement 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", + "CRITICAL: For ANY exploration of code you haven't examined yet in this conversation, you MUST use the `codebase_search` tool FIRST", ) - expect(guidelines).toContain("before any other search tools") + expect(guidelines).toContain("before any other search or file exploration tools") expect(guidelines).toContain( - "semantic search tool helps you find relevant code based on meaning rather than just keywords", + "semantic search to find relevant code based on meaning rather than just keywords", ) }) @@ -35,7 +35,7 @@ describe("getToolUseGuidelinesSection", () => { // Check that all numbered items are present expect(guidelines).toContain("1. In tags") - expect(guidelines).toContain("2. **IMPORTANT:") + expect(guidelines).toContain("2. **CRITICAL:") expect(guidelines).toContain("3. Choose the most appropriate tool") expect(guidelines).toContain("4. If multiple actions are needed") expect(guidelines).toContain("5. Formulate your tool use") @@ -50,9 +50,9 @@ describe("getToolUseGuidelinesSection", () => { // Check that the guidelines do not include the codebase_search enforcement 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", + "CRITICAL: For ANY exploration of code you haven't examined yet in this conversation, you MUST use the `codebase_search` tool FIRST", ) - expect(guidelines).not.toContain("semantic search tool helps you find relevant code based on meaning") + expect(guidelines).not.toContain("semantic search to find relevant code based on meaning") }) it("should maintain proper numbering without codebase_search", () => { diff --git a/src/core/prompts/sections/objective.ts b/src/core/prompts/sections/objective.ts index 845db046a0..3d18a7e209 100644 --- a/src/core/prompts/sections/objective.ts +++ b/src/core/prompts/sections/objective.ts @@ -11,7 +11,7 @@ export function getObjectiveSection( codeIndexManager.isInitialized const codebaseSearchInstruction = isCodebaseSearchAvailable - ? "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, " + ? "First, for ANY exploration of code you haven't examined yet in this conversation, 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. This applies throughout the entire task, not just at the beginning - whenever you need to explore a new area of code, codebase_search must come first. Then, " : "First, " return `==== diff --git a/src/core/prompts/sections/rules.ts b/src/core/prompts/sections/rules.ts index 3b2e992085..b94bc893cf 100644 --- a/src/core/prompts/sections/rules.ts +++ b/src/core/prompts/sections/rules.ts @@ -58,7 +58,7 @@ export function getRulesSection( codeIndexManager.isInitialized const codebaseSearchRule = isCodebaseSearchAvailable - ? "- **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.** The codebase_search tool uses semantic search to find relevant code based on meaning, not just keywords, making it much more effective for understanding how features are implemented.\n" + ? "- **CRITICAL: For ANY exploration of code you haven't examined yet in this conversation, you MUST use the `codebase_search` tool FIRST before using search_files or other file exploration tools.** This requirement applies throughout the entire conversation, not just when starting a task. The codebase_search tool uses semantic search to find relevant code based on meaning, not just keywords, making it much more effective for understanding how features are implemented. Even if you've already explored some parts of the codebase, any new area or functionality you need to understand requires using codebase_search first.\n" : "" return `==== diff --git a/src/core/prompts/sections/tool-use-guidelines.ts b/src/core/prompts/sections/tool-use-guidelines.ts index 6f19b7e235..a526bbc711 100644 --- a/src/core/prompts/sections/tool-use-guidelines.ts +++ b/src/core/prompts/sections/tool-use-guidelines.ts @@ -19,10 +19,10 @@ export function getToolUseGuidelinesSection(codeIndexManager?: CodeIndexManager) // Conditional codebase search guideline if (isCodebaseSearchAvailable) { guidelinesList.push( - `${itemNumber++}. **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.** This semantic search tool helps you find relevant code based on meaning rather than just keywords. Only after using codebase_search should you use other tools like search_files, list_files, or read_file for more specific exploration.`, + `${itemNumber++}. **CRITICAL: For ANY exploration of code you haven't examined yet in this conversation, you MUST use the \`codebase_search\` tool FIRST before any other search or file exploration tools.** This applies throughout the entire conversation, not just at the beginning. The codebase_search tool uses semantic search to find relevant code based on meaning rather than just keywords, making it far more effective than regex-based search_files for understanding implementations. Even if you've already explored some code, any new area of exploration requires codebase_search first.`, ) guidelinesList.push( - `${itemNumber++}. Choose the most appropriate tool based on the task and the tool descriptions provided. Assess if you need additional information to proceed, and which of the available tools would be most effective for gathering this information. For example using the list_files tool is more effective than running a command like \`ls\` in the terminal. It's critical that you think about each available tool and use the one that best fits the current step in the task.`, + `${itemNumber++}. Choose the most appropriate tool based on the task and the tool descriptions provided. After using codebase_search for initial exploration of any new code area, you may then use more specific tools like search_files (for regex patterns), list_files, or read_file for detailed examination. For example, using the list_files tool is more effective than running a command like \`ls\` in the terminal. It's critical that you think about each available tool and use the one that best fits the current step in the task.`, ) } else { guidelinesList.push(