Skip to content

Conversation

@ammar-agent
Copy link
Collaborator

Add new file_search tool to help agents find specific code sections, verify content before editing, and confirm changes after editing.

Features

  • Literal string pattern matching (case-sensitive)
  • Configurable context lines (0-10, default: 3)
  • Max results limit (1-1000, default: 100)
  • Returns line numbers, matching content, and surrounding context
  • Works with both local and SSH runtimes

Implementation

Backend

  • Uses runtime.normalizePath for path resolution
  • Validates file size and path permissions
  • Efficient string search with context extraction
  • Consistent error handling with RuntimeError

UI Component

  • Displays match count in header
  • Shows matches with highlighted line numbers
  • Context lines shown with reduced opacity
  • Matching line highlighted with distinct background
  • Handles zero matches gracefully

Types

  • FileSearchToolArgs: file_path, pattern, context_lines?, max_results?
  • FileSearchToolResult: matches array with line numbers and context
  • FileSearchMatch: line_number, line_content, context_before, context_after

Tests

10 comprehensive tests covering:

  • Single and multiple matches
  • Empty results / Max results limit
  • Context line boundaries / File not found errors
  • Directory rejection / Case sensitivity / Substring matching

Use Cases

  1. Pre-edit verification: Search for exact content before file_edit_replace_string
  2. Post-edit confirmation: Verify changes landed correctly
  3. Code exploration: Find function definitions, imports, specific patterns
  4. Error investigation: Locate error messages, stack traces, specific values

Benefits

  • Reduces "old_string not found" errors by letting agents search first
  • Lower cognitive load than reading entire files
  • Complements file_read for targeted inspection
  • Enables iterative refinement of edit operations

Files Changed

  • src/types/tools.ts - Types for file_search tool
  • src/utils/tools/toolDefinitions.ts - Tool definition and schema
  • src/utils/tools/tools.ts - Tool registration
  • src/services/tools/file_search.ts - Backend implementation
  • src/services/tools/file_search.test.ts - 10 unit tests (all pass)
  • src/components/tools/FileSearchToolCall.tsx - UI component
  • src/components/Messages/ToolMessage.tsx - Component routing

Generated with cmux

Add new file_search tool to help agents find specific code sections, verify
content before editing, and confirm changes after editing.

## Features

- Literal string pattern matching (case-sensitive)
- Configurable context lines (0-10, default: 3)
- Max results limit (1-1000, default: 100)
- Returns line numbers, matching content, and surrounding context
- Works with both local and SSH runtimes

## Implementation

**Backend (src/services/tools/file_search.ts):**
- Uses runtime.normalizePath for path resolution
- Validates file size and path permissions
- Efficient string search with context extraction
- Consistent error handling with RuntimeError

**UI (src/components/tools/FileSearchToolCall.tsx):**
- Displays match count in header
- Shows matches with highlighted line numbers
- Context lines shown with reduced opacity
- Matching line highlighted with distinct background
- Handles zero matches gracefully

**Types (src/types/tools.ts):**
- FileSearchToolArgs: file_path, pattern, context_lines?, max_results?
- FileSearchToolResult: matches array with line numbers and context
- FileSearchMatch: line_number, line_content, context_before, context_after

**Tests (src/services/tools/file_search.test.ts):**
- 10 comprehensive tests covering:
  - Single and multiple matches
  - Empty results
  - Max results limit
  - Context line boundaries
  - File not found errors
  - Directory rejection
  - Case sensitivity
  - Substring matching

## Use Cases

1. **Pre-edit verification**: Search for exact content before file_edit_replace_string
2. **Post-edit confirmation**: Verify changes landed correctly
3. **Code exploration**: Find function definitions, imports, specific patterns
4. **Error investigation**: Locate error messages, stack traces, specific values

## Benefits

- Reduces "old_string not found" errors by letting agents search first
- Lower cognitive load than reading entire files
- Complements file_read for targeted inspection
- Enables iterative refinement of edit operations

_Generated with _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant