Add local project indexing and search guidance

This commit is contained in:
2026-08-30 22:00:27 -05:00
parent 88c1ef9716
commit 1c7d3c175d
39 changed files with 2094 additions and 306 deletions
+15 -13
View File
@@ -34,10 +34,11 @@ const CODE_INSTRUCTIONS: &str = r#"## Instructions
### Doing Tasks
The user will primarily request software engineering tasks: solving bugs, adding features, refactoring, explaining code, and more. For these tasks:
1. Use search tools to understand the codebase and the user's query. Search extensively — both in parallel and sequentially.
2. Implement the solution using all tools available to you.
3. Verify the solution if possible with tests. NEVER assume a specific test framework — check the project first.
4. After making changes, run lint/typecheck/build commands if you know them, to ensure correctness.
1. For source-code discovery, semantic questions, or finding an unfamiliar implementation, use `search_codebase` first. Never use `grep` to discover or search source code when `search_codebase` is available; reserve it for exact known text in non-code files. Use `file_glob` to locate filenames and `read_files` for focused follow-up context.
2. Search extensively — both in parallel and sequentially.
3. Implement the solution using all tools available to you.
4. Verify the solution if possible with tests. NEVER assume a specific test framework — check the project first.
5. After making changes, run lint/typecheck/build commands if you know them, to ensure correctness.
NEVER commit changes unless the user explicitly asks you to.
@@ -62,7 +63,7 @@ CRITICAL: You are in READ-ONLY planning mode. You MUST NOT:
You MAY:
- Read files
- Search the codebase (grep, glob)
- Search the codebase with `search_codebase`; use `file_glob` for filenames and `grep` only for exact known text in non-code files
- Run read-only shell commands (ls, cat, git log, git status)
- Ask the user clarifying questions
@@ -116,7 +117,8 @@ const CODE_TOOL_GUIDELINES: &str = r#"## Tool Usage
You have been given every tool you need to complete your tasks. Use them to achieve results with as few calls and as little back-and-forth as possible.
**How to choose tools:**
- For reading, writing, searching, and navigating files on the local filesystem, use your filesystem tools (`read_files`, `file_glob`, `grep`, `apply_file_diffs`).
- For source-code discovery, use `search_codebase` first. Use `file_glob` to locate filenames and `read_files` to inspect focused files. Never use `grep` to discover or search source code when `search_codebase` is available; reserve it for exact known text in non-code files.
- For writing and editing files, use `apply_file_diffs`.
- For running commands, installing packages, building, testing, and any shell operation, use `run_shell_command`.
- For tasks that require interacting with external services, web UIs, or capabilities not covered by your filesystem and shell tools, use your MCP tools.
- For complex multi-step tasks where a single script would replace many tool calls, write code (Python, Node, bash) via `run_shell_command` to reduce round-trips. But never use scripts for simple operations that a single command handles.
@@ -140,10 +142,10 @@ If you intend to call multiple tools and there are no dependencies between the c
const PLAN_TOOL_GUIDELINES: &str = r#"## Tool Usage
You have access to read-only tools for exploring the codebase:
- `read_files` — Read file contents
- `grep` — Search for patterns in files
- `search_codebase` — Primary tool for source-code discovery and semantic or structural questions
- `file_glob` — Find files by glob pattern
- `search_codebase` — Semantic code search
- `read_files` — Read focused file contents after discovery
- `grep` — Search exact known text in non-code files only
- `run_shell_command` — ONLY for read-only commands (ls, git log, git status, etc.)
- `ask_user_question` — Ask the user for clarification
@@ -151,10 +153,10 @@ You MUST NOT use `apply_file_diffs`, `create_documents`, `edit_documents`, or an
const REVIEW_TOOL_GUIDELINES: &str = r#"## Tool Usage
You have access to tools for examining the code under review:
- `read_files` — Read file contents to understand context
- `grep` — Search for patterns to find related code
- `file_glob` — Find related files
- `search_codebase` — Semantic search for related implementations
- `search_codebase` — Primary tool for discovering relevant source code and understanding implementations semantically
- `file_glob` — Find related filenames
- `read_files` — Read focused file contents after discovery
- `grep` — Search exact known text in non-code files, or use only as a fallback when the semantic index is unavailable
- `run_shell_command` — For read-only commands (git diff, git log, etc.)
Use these tools to gather context needed for a thorough review. You should read the files being changed and their surrounding context before providing feedback."#;
+28
View File
@@ -16,6 +16,7 @@ mod prompt_builder_tests {
assert!(prompt.system_prompt.contains("Galaxy"));
assert!(prompt.system_prompt.contains("coding mode"));
assert!(prompt.system_prompt.contains("/home/user/project"));
assert!(prompt.system_prompt.contains("search_codebase` first"));
assert!(!prompt.tools.is_empty());
// Code mode should have apply_file_diffs
assert!(prompt.tools.iter().any(|t| t.name == "apply_file_diffs"));
@@ -31,6 +32,33 @@ mod prompt_builder_tests {
assert!(!prompt.tools.iter().any(|t| t.name == "apply_file_diffs"));
// But should have read_files
assert!(prompt.tools.iter().any(|t| t.name == "read_files"));
assert!(prompt.system_prompt.contains("search_codebase` first"));
}
#[test]
fn test_semantic_search_is_advertised_before_regex_search() {
let prompt = PromptBuilder::new(Mode::Code, Provider::Anthropic).build();
let search_index = prompt
.tools
.iter()
.position(|tool| tool.name == "search_codebase")
.expect("search_codebase tool");
let grep_index = prompt
.tools
.iter()
.position(|tool| tool.name == "grep")
.expect("grep tool");
assert!(search_index < grep_index);
assert!(prompt
.tools
.iter()
.find(|tool| tool.name == "search_codebase")
.is_some_and(|tool| tool.description.contains("all source-code discovery")));
assert!(prompt
.tools
.iter()
.find(|tool| tool.name == "grep")
.is_some_and(|tool| tool.description.contains("non-code files")));
}
#[test]
+9 -9
View File
@@ -23,9 +23,9 @@ fn code_tools() -> Vec<ToolDefinition> {
run_shell_command(),
read_files(),
apply_file_diffs(),
grep(),
file_glob(),
search_codebase(),
file_glob(),
grep(),
write_to_long_running_shell_command(),
interrupt_shell_command(),
read_shell_command_output(),
@@ -48,9 +48,9 @@ fn code_tools() -> Vec<ToolDefinition> {
fn plan_tools() -> Vec<ToolDefinition> {
vec![
read_files(),
grep(),
file_glob(),
search_codebase(),
file_glob(),
grep(),
run_shell_command_readonly(),
ask_user_question(),
start_agent(),
@@ -64,9 +64,9 @@ fn plan_tools() -> Vec<ToolDefinition> {
fn review_tools() -> Vec<ToolDefinition> {
vec![
read_files(),
grep(),
file_glob(),
search_codebase(),
file_glob(),
grep(),
run_shell_command_readonly(),
]
}
@@ -136,7 +136,7 @@ fn apply_file_diffs() -> ToolDefinition {
fn grep() -> ToolDefinition {
ToolDefinition {
name: "grep".to_string(),
description: "Search for up to 3 focused regex patterns per call. Uses git grep in git repos (respects .gitignore) or ripgrep otherwise. Returns up to 1,000 matching line locations and reports when additional matches were omitted. Use read_files afterward to see context around matches.".to_string(),
description: "Search for up to 3 focused regex patterns per call. Use only for exact known text in non-code files; never use this to discover or search source code when search_codebase is available. Uses git grep in git repos (respects .gitignore) or ripgrep otherwise. Returns up to 1,000 matching line locations and reports when additional matches were omitted. Use read_files afterward to see context around matches.".to_string(),
input_schema: serde_json::json!({
"type": "object",
"properties": {
@@ -151,7 +151,7 @@ fn grep() -> ToolDefinition {
fn file_glob() -> ToolDefinition {
ToolDefinition {
name: "file_glob".to_string(),
description: "Find files matching up to 3 focused glob patterns per call. Uses git ls-files in git repos. Returns absolute file paths of matches. Make another call for more patterns.".to_string(),
description: "Find filenames matching up to 3 focused glob patterns per call. Use this to locate files, not to search their contents. Uses git ls-files in git repos. Returns absolute file paths of matches. Make another call for more patterns.".to_string(),
input_schema: serde_json::json!({
"type": "object",
"properties": {
@@ -166,7 +166,7 @@ fn file_glob() -> ToolDefinition {
fn search_codebase() -> ToolDefinition {
ToolDefinition {
name: "search_codebase".to_string(),
description: "Semantic code search across the indexed codebase. Use for finding relevant code by meaning rather than exact text match. Better than grep for conceptual queries like 'authentication logic' or 'error handling for database connections'.".to_string(),
description: "Primary tool for discovering relevant source code in the local project index. Use this first for all source-code discovery and conceptual, semantic, or structural searches before reading files. Never use grep to search source code when this tool is available.".to_string(),
input_schema: serde_json::json!({
"type": "object",
"properties": {