ExAgent.Sandbox.Backend behaviour (ExAgent v3.0.0)

View Source

Host implementation of an isolated execution sandbox.

Stability — Candidate.

An operation is keyed by the trusted scope and ID together. Reserve it in create/3 before running any code. execute/3 consumes that reservation at most once. While the backend retains the operation record, a repeated reservation returns {:error, :operation_exists}. Dispatch requires a confirmed, unused reservation. Hosts allocate a fresh ID with ExAgent.Sandbox.new_id/0 and reconcile uncertain outcomes by reading the original ID.

Enforce the policy in the isolated environment. Return a confirmed receipt under the same operation ID only after execution has stopped. Keep bounded text and an optional image for authorized reads. Destruction removes retained data and releases resources. An adapter may refuse destruction while active. An absent or expired record is insufficient evidence to reconcile execution.

A mutating callback that loses its acknowledgement returns {:unknown, :result_unconfirmed}. Use {:error, reason} on execution only for a confirmed refusal before dispatch. Timeout and output exhaustion with confirmed termination are receipts. Read and capture callbacks return {:error, :unavailable} for an unavailable read. Return {:unknown, :result_unconfirmed} when a retained operation has an uncertain outcome. The generated query tools report either response as a :tool_error and leave the execution's reconciliation open.

Keep read/2 and capture/2 free of execution side effects. The generated tools allow these callbacks up to 5,000 ms or half the remaining tool budget, whichever is shorter. When the wait expires the tool stops its query task and reports an unavailable query. Backend work that outlives the calling process needs its own deadline and cleanup, including for direct host reads.

Summary

Types

limits()

@type limits() :: %{
  cpu_millicores: pos_integer(),
  memory_bytes: pos_integer(),
  time_ms: pos_integer(),
  processes: pos_integer(),
  output_bytes: pos_integer(),
  capture_bytes: pos_integer(),
  scratch_bytes: pos_integer()
}

mount()

@type mount() :: {source :: String.t(), destination :: String.t()}

operation()

@type operation() :: %{scope: String.t(), id: String.t()}

policy()

@type policy() :: %{network: :none, mounts: [mount()], limits: limits()}

reason()

@type reason() ::
  :busy
  | :unavailable
  | :not_found
  | :operation_exists
  | :capture_unavailable
  | :invalid_mount

receipt()

@type receipt() :: %{
  required(String.t()) => String.t() | boolean() | non_neg_integer() | nil
}

uncertainty()

@type uncertainty() :: {:unknown, :result_unconfirmed}

Callbacks

capture(config, operation)

@callback capture(config :: term(), operation()) ::
  {:ok, {String.t(), binary()}} | {:error, reason()} | uncertainty()

create(config, operation, policy)

@callback create(config :: term(), operation(), policy()) ::
  :ok | {:error, reason()} | uncertainty()

destroy(config, operation)

@callback destroy(config :: term(), operation()) ::
  :ok | {:error, reason()} | uncertainty()

execute(config, operation, t)

@callback execute(config :: term(), operation(), String.t()) ::
  {:ok, receipt()} | {:error, reason()} | uncertainty()

read(config, operation)

@callback read(config :: term(), operation()) ::
  {:ok, receipt(), String.t()} | {:error, reason()} | uncertainty()