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, #stream_kind

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



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
89
90
91
92
93
94
95
96
# File 'lib/rubino/tools/read_tool.rb', line 47

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 = expand_workspace_path(file_path)
  # Reads are BROAD (#406): like Hermes/Claude/Codex, read resolves any
  # NON-secret path with no prompt (clone-and-inspect). A SECRET/credential
  # path (#446) is NOT refused here anymore — it is gated UPSTREAM by
  # Security::ApprovalPolicy#decide (→ :ask), so an APPROVED read returns
  # the real bytes while a denied/headless read never reaches #call.
  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 + content hash BEFORE rendering so a slow render on a huge
  # file doesn't race with a concurrent writer — we want the state the
  # model "saw", not the one at end-of-render. The hash is the single
  # source of truth the edit-gate and dedup both consult.
  mtime  = File.mtime(expanded)
  digest = Digest::SHA256.hexdigest(File.binread(expanded))
  @read_tracker&.register(expanded, mtime, digest)

  # Re-reading the exact same window of UNCHANGED bytes just re-injects
  # content already in context. Skip the work with a nudge — but only when
  # the file still hashes the same, the TTL holds, and no edit-failure
  # recovery is pending (those serve fresh content). See ReadTracker.
  if @read_tracker&.duplicate_read?(expanded, offset, limit, digest)
    return { output: "[DUPLICATE READ] Exact repeat of an earlier read of #{file_path} " \
                     "(lines #{offset}-#{offset + limit - 1}) — 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



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

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



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

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



20
21
22
# File 'lib/rubino/tools/read_tool.rb', line 20

def name
  "read"
end

#risk_levelObject



43
44
45
# File 'lib/rubino/tools/read_tool.rb', line 43

def risk_level
  :low
end