Class: Brute::Tools::FSSearch

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/brute/tools/fs_search.rb

Overview

Existing features (ref: opencode grep tool):

  1. Global result cap — limit total matches to 100 across all files.

  2. Per-line truncation — truncate individual match lines longer than 2000 chars via rg –max-columns with preview.

  3. Structured truncation message — when results are capped, append: “(Results truncated: showing 100 of N matches. Consider a more specific path or pattern.)”

  4. Sort results by file mtime — most-recently-modified files first, so the LLM sees the most relevant matches first.

  5. Return a plain string instead of a Hash.

  6. Align output cap with universal truncation (2000 lines / 50 KB).

Constant Summary collapse

MAX_TOTAL_MATCHES =
100

Instance Method Summary collapse

Instance Method Details

#execute(pattern:, path: nil, glob: nil, ignore_case: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/brute/tools/fs_search.rb', line 37

def execute(pattern:, path: nil, glob: nil, ignore_case: false)
  dir = File.expand_path(path || Dir.pwd)
  raise "Directory not found: #{dir}" unless File.directory?(dir)

  cmd = ["rg", "--line-number", "--max-columns=2000", "--max-columns-preview", "--sortr=modified"]
  cmd << "--ignore-case" if ignore_case
  cmd += ["--glob", glob] if glob
  cmd << pattern
  cmd << dir

  stdout, stderr, status = Open3.capture3(*cmd)

  output = stdout.empty? ? stderr : stdout

  # Global cap at MAX_TOTAL_MATCHES lines
  lines = output.lines
  total_matches = lines.size
  if total_matches > MAX_TOTAL_MATCHES
    output = lines.first(MAX_TOTAL_MATCHES).join
    output += "\n(Results truncated: showing 100 of #{total_matches} matches. Consider a more specific path or pattern.)"
  end

  Brute::Truncation.truncate(output)
end

#nameObject



33
# File 'lib/brute/tools/fs_search.rb', line 33

def name; "fs_search"; end