Class: AgentSandbox::Sandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_sandbox/sandbox.rb

Instance Method Summary collapse

Constructor Details

#initialize(backend) ⇒ Sandbox

Returns a new instance of Sandbox.



14
15
16
17
# File 'lib/agent_sandbox/sandbox.rb', line 14

def initialize(backend)
  @backend = backend
  @started = false
end

Instance Method Details

#exec(command, check: false) ⇒ Object

Raises:



26
27
28
29
30
31
# File 'lib/agent_sandbox/sandbox.rb', line 26

def exec(command, check: false)
  start
  result = @backend.exec(command)
  raise ExecError.new(status: result.status, stdout: result.stdout, stderr: result.stderr) if check && !result.success?
  result
end

#openObject Also known as: with

Auto-start, yield, auto-stop. Cleanup runs on normal return, on exceptions, and on non-local exits (return/break/throw).

Dual-failure strategy: preserve the original block exception’s class and ivars at the top (so ‘rescue ExecError` / `rescue HttpError` keeps working) and splice the cleanup failure into the cause chain ABOVE the block error’s pre-existing cause — so full_message / APMs / anything that traverses ‘cause` sees both failures AND the block error’s own cause chain survives untouched. The cleanup failure is also exposed via #cleanup_error for callers that want it directly.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/agent_sandbox/sandbox.rb', line 77

def open
  start
  block_error = nil
  begin
    yield self
  rescue => e
    block_error = e
    raise
  ensure
    begin
      stop
    rescue => cleanup_error
      if block_error
        copy = block_error.exception(block_error.message)
        copy.set_backtrace(block_error.backtrace) if block_error.backtrace
        copy.define_singleton_method(:cleanup_error) { cleanup_error }
        raise copy, cause: chain_cleanup(cleanup_error, block_error.cause)
      else
        raise
      end
    end
  end
end

#port_url(port) ⇒ Object



54
55
56
# File 'lib/agent_sandbox/sandbox.rb', line 54

def port_url(port)
  @backend.port_url(port)
end

#read_file(path) ⇒ Object



49
50
51
52
# File 'lib/agent_sandbox/sandbox.rb', line 49

def read_file(path)
  start
  @backend.read_file(path)
end

#spawn(command) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/agent_sandbox/sandbox.rb', line 33

def spawn(command)
  # Capability check before `start` so unsupported backends don't leave an
  # orphaned remote sandbox running until its timeout.
  unless backend_supports?(:spawn)
    raise UnsupportedOperation,
          "#{@backend.class.name} does not support spawn (use exec, or switch backend)"
  end
  start
  @backend.spawn(command)
end

#startObject



19
20
21
22
23
24
# File 'lib/agent_sandbox/sandbox.rb', line 19

def start
  return self if @started
  @backend.start
  @started = true
  self
end

#stopObject



58
59
60
61
62
63
64
65
# File 'lib/agent_sandbox/sandbox.rb', line 58

def stop
  # Always invalidate wrapper start-state and delegate, even on retry.
  # Backends are expected to be idempotent (Docker rm always runs;
  # E2B short-circuits when @sandbox_id is nil). Without this, a failed
  # backend cleanup would leave the wrapper unable to retry stop.
  @started = false
  @backend.stop
end

#write_file(path, content) ⇒ Object



44
45
46
47
# File 'lib/agent_sandbox/sandbox.rb', line 44

def write_file(path, content)
  start
  @backend.write_file(path, content)
end