Class: OllamaAgent::Runtime::KernelToolSeed::PlanningSandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/runtime/kernel_tool_seed.rb

Overview

Read-only planning helpers scoped to workspace_root (no Agent / TTY).

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ PlanningSandbox

Returns a new instance of PlanningSandbox.



101
102
103
104
# File 'lib/ollama_agent/runtime/kernel_tool_seed.rb', line 101

def initialize(root)
  @root = File.expand_path(root.to_s)
  @guard = Security::ResourceGuard.new(root: @root)
end

Instance Method Details

#list_files(directory: ".", max_entries: 100, max_depth: nil, **_ignored) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ollama_agent/runtime/kernel_tool_seed.rb', line 119

def list_files(directory: ".", max_entries: 100, max_depth: nil, **_ignored)
  dir = directory.to_s.empty? ? "." : directory.to_s
  base = File.expand_path(dir, @root)
  return "path not allowed" unless @guard.allow?(base)
  return "Not a directory: #{dir}" unless File.directory?(base)

  cap = clamp_list_limit(max_entries)
  paths = collect_relative_paths(base, cap, max_depth: max_depth)
  return "(no files listed)" if paths.empty?

  body = paths.sort.join("\n")
  return body if paths.size < cap

  "#{body}\n(list truncated at #{cap} entries; pass max_entries or narrow directory)"
end

#read_file(path:, start_line: nil, end_line: nil, **_ignored) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ollama_agent/runtime/kernel_tool_seed.rb', line 106

def read_file(path:, start_line: nil, end_line: nil, **_ignored)
  abs = File.expand_path(path.to_s, @root)
  return "path not allowed" unless @guard.allow?(abs)
  return "Error reading file: not a file" unless File.file?(abs)

  return read_line_range(abs, start_line, end_line) if start_line || end_line
  return "Error reading file: file too large (max #{MAX_READ_BYTES} bytes)" if File.size(abs) > MAX_READ_BYTES

  File.read(abs, encoding: Encoding::UTF_8)
rescue Errno::ENOENT => e
  "Error reading file: #{e.message}"
end

#search_files(pattern:, directory: ".", **_ignored) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ollama_agent/runtime/kernel_tool_seed.rb', line 135

def search_files(pattern:, directory: ".", **_ignored)
  pat = pattern.to_s
  return "Error: search_files requires a non-empty pattern" if pat.strip.empty?

  dir = directory.to_s.empty? ? "." : directory.to_s
  abs = File.expand_path(dir, @root)
  return "path not allowed" unless @guard.allow?(abs)

  rg = SearchBackend.rg_executable
  grep = SearchBackend.grep_executable
  return SearchTextBackend.no_backends_message if rg.nil? && grep.nil?

  SearchTextBackend.search(ripgrep_bin: rg, grep_bin: grep, pattern: pat, target: abs)
end