Class: Rubino::Tools::ReadTool

Inherits:
Base
  • Object
show all
Defined in:
lib/rubino/tools/read_tool.rb

Overview

Reads a file with ‘cat -n` style line numbers, offset/limit windowing, and a hard cap on per-line length. Line numbers let the LLM cite or edit exact lines instead of “the second occurrence of X”; offset/limit let it page through files that would otherwise blow the context.

Constant Summary collapse

DEFAULT_LIMIT =
2000
MAX_LINE_WIDTH =
2000
MAX_OUTPUT_BYTES =

Hard cap on the bytes a single read returns (~25k tokens at 4 bytes/tok, matching Claude Code’s read gate). A window of 2000 lines × 2000 chars could otherwise build multiple MB in memory and blow up prefill/TTFT; past this we stop and tell the model to narrow the range or grep.

100_000

Instance Attribute Summary

Attributes inherited from Base

#cancel_token, #read_tracker, #stream_chunk

Instance Method Summary collapse

Methods inherited from Base

#cancellation_requested?, #config_key, #emit_chunk, #risky?, #to_tool_definition, workspace_root, workspace_roots

Instance Method Details

#call(arguments) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rubino/tools/read_tool.rb', line 45

def call(arguments)
  file_path = arguments["file_path"] || arguments[:file_path]
  offset    = (arguments["offset"]   || arguments[:offset]   || 1).to_i
  limit     = (arguments["limit"]    || arguments[:limit]    || DEFAULT_LIMIT).to_i

  return "Error: file_path is required" if file_path.nil? || file_path.to_s.empty?

  expanded = File.expand_path(file_path)
  return "Error: File not found: #{file_path}" unless File.exist?(expanded)
  return "Error: Not a regular file: #{file_path}" unless File.file?(expanded)

  if binary?(expanded)
    size = File.size(expanded)
    return { output: "Error: #{file_path} appears to be a binary file (#{size} bytes). " \
                     "Reading it as text would corrupt the buffer. " \
                     "Use the shell tool with xxd/file/strings for inspection.",
             error_code: :binary_file }
  end

  offset = 1 if offset < 1
  limit  = DEFAULT_LIMIT if limit <= 0

  # Stash mtime BEFORE rendering so a slow render on a huge file doesn't
  # race with a concurrent writer — we want the mtime the model "saw",
  # not the one at end-of-render.
  mtime = File.mtime(expanded)
  @read_tracker&.register(expanded, mtime)

  # Re-reading the exact same window (same file, offset, limit, unchanged
  # mtime) within a turn just re-injects bytes already in context. Return
  # a short nudge instead so the conversation doesn't carry the same
  # content twice. A real edit bumps mtime, so legitimate re-reads pass.
  dup = @read_tracker&.register_window(expanded, offset, limit, mtime)
  if dup && dup > 1
    return { output: "[DUPLICATE READ] Exact repeat of an earlier read of #{file_path} " \
                     "(lines #{offset}-#{offset + limit - 1}) this turn — reuse that result " \
                     "instead of re-reading.",
             metrics: "duplicate" }
  end

  render(expanded, file_path, offset, limit)
rescue StandardError => e
  "Error reading #{file_path}: #{e.message}"
end

#descriptionObject



22
23
24
25
26
27
# File 'lib/rubino/tools/read_tool.rb', line 22

def description
  "Read a text file from the filesystem with line numbers (cat -n style). " \
    "Supports offset (1-based start line) and limit (max lines returned). " \
    "Long lines are truncated at #{MAX_LINE_WIDTH} chars. " \
    "Default window: first #{DEFAULT_LIMIT} lines."
end

#input_schemaObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubino/tools/read_tool.rb', line 29

def input_schema
  {
    type: "object",
    properties: {
      file_path: { type: "string", description: "Absolute or relative file path" },
      offset: { type: "integer", description: "1-based line to start at (default 1)" },
      limit: { type: "integer", description: "Max lines to return (default #{DEFAULT_LIMIT})" }
    },
    required: %w[file_path]
  }
end

#nameObject



18
19
20
# File 'lib/rubino/tools/read_tool.rb', line 18

def name
  "read"
end

#risk_levelObject



41
42
43
# File 'lib/rubino/tools/read_tool.rb', line 41

def risk_level
  :low
end