Class: Brute::Tools::FSSearch

Inherits:
Brute::Tool 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

Methods inherited from Brute::Tool

#call, description, #description, param, param_definitions, params, #params, #params_schema

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
61
62
63
64
65
66
67
68
69
70
# 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)
  unless File.directory?(dir)
    raise "Directory not found: #{dir}"
  end

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

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

  if stdout.empty?
    output = stderr
  else
    output = stdout
  end

  # 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