Module: RubyLLM::Toolbox::Tools::SandboxRun

Included in:
RunPython, RunRuby, RunRust
Defined in:
lib/ruby_llm/toolbox/tools/sandbox_run.rb

Overview

Shared behavior for the sandboxed code-execution tools (run_ruby, run_python, run_rust): pick the active sandbox backend for this host (Sandbox.build), run the command with the code piped on stdin, and format stdout/stderr/exit uniformly. The Docker backend uses ‘image`; the host-process backends ignore it and run the host’s interpreters.

Instance Method Summary collapse

Instance Method Details

#format_sandbox(out, err, status) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/toolbox/tools/sandbox_run.rb', line 25

def format_sandbox(out, err, status)
  body = +""
  body << if status == :timeout
            "result: timed out after #{config.command_timeout}s (sandbox killed)\n"
          else
            "exit: #{status.exitstatus}\n"
          end
  body << "\n--- stdout ---\n#{out}" unless out.empty?
  body << "\n--- stderr ---\n#{err}" unless err.empty?
  body << "(no output)" if out.empty? && err.empty?
  body
end

#run_in_sandbox(argv, code, image:) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/ruby_llm/toolbox/tools/sandbox_run.rb', line 17

def run_in_sandbox(argv, code, image:)
  backend = Sandbox.build(config)
  out, err, status = backend.run(argv, stdin: code.to_s, image: image)
  truncate(format_sandbox(out, err, status))
rescue Sandbox::Unavailable => e
  error(e.message, code: :sandbox_unavailable)
end