ExAgent.Sandbox (ExAgent v3.0.0)

View Source

Opt-in tools for isolated execution in a host-provided sandbox.

Stability — Candidate.

Bind a sandbox to an authenticated scope when constructing the agent. Generated tools use ordinary permissions, approval, result validation and retention. The model supplies code or an operation reference. The host chooses the backend, mounts, limits and optional background job store. Isolation is the backend's responsibility. Dispatch is single-use while the backend retains the reservation or receipt. Generated tools assign each new operation its own ID.

Summary

Functions

Read one bounded, typed PNG, JPEG or WebP capture in this scope. Uses the same read acknowledgements as read/2.

Release an operation and its retained data as the authorized host. A lost acknowledgement returns {:error, {:result_unconfirmed, id}}.

Execute approved code under a single operation ID. This host entry point assumes the caller has authorized both scope and code. Use new_id/0 for each operation. A continuation supplies its effect ID for the first dispatch.

Bind a {backend_module, config} to a nonempty :scope and a versioned :identity (string-keyed %{"id" => id, "version" => version}). Change the version when the executor behind that identity changes. Construction does no backend IO.

Generate a fresh operation ID for a host call to execute/3.

Read a confirmed execution receipt and its bounded transcript in this scope. The JSON receipt is the returned map with "text" removed. {:error, {:unavailable, id}} means the read could not obtain a valid response. {:error, {:result_unconfirmed, id}} reports a retained operation with an uncertain outcome. Either error or a missing record leaves reconciliation open.

Build sandbox_execute and sandbox_read. Code execution requires approval even with an allow rule. Supply an ExAgent model struct as :model to include sandbox_capture when its profile accepts image tool results. Capture also checks the active profile before fetching an image.

Types

operation_error()

@type operation_error() ::
  :invalid_operation_id
  | :invalid_code
  | :invalid_mount
  | :unavailable
  | :busy
  | :not_found
  | :capture_unavailable
  | {:operation_exists | :unavailable | :result_unconfirmed, operation_id()}

operation_id()

@type operation_id() :: String.t()

t()

@opaque t()

Functions

capture(sandbox, id)

@spec capture(t(), operation_id()) ::
  {:ok, ExAgent.Tool.Content.t()} | {:error, operation_error()}

Read one bounded, typed PNG, JPEG or WebP capture in this scope. Uses the same read acknowledgements as read/2.

destroy(sandbox, id)

@spec destroy(t(), operation_id()) :: :ok | {:error, operation_error()}

Release an operation and its retained data as the authorized host. A lost acknowledgement returns {:error, {:result_unconfirmed, id}}.

execute(sandbox, id, code)

@spec execute(t(), operation_id(), String.t()) ::
  {:ok, ExAgent.Sandbox.Backend.receipt()} | {:error, operation_error()}

Execute approved code under a single operation ID. This host entry point assumes the caller has authorized both scope and code. Use new_id/0 for each operation. A continuation supplies its effect ID for the first dispatch.

{:error, {:operation_exists, id}} reports an existing reservation. {:error, {:result_unconfirmed, id}} means the backend could not confirm a mutation or its receipt. Reconcile both by reading the original ID. Local records can disappear on destruction, eviction, expiry or restart.

new(opts)

@spec new(keyword()) :: t()

Bind a {backend_module, config} to a nonempty :scope and a versioned :identity (string-keyed %{"id" => id, "version" => version}). Change the version when the executor behind that identity changes. Construction does no backend IO.

:mounts defaults to []. Each pair is an absolute host path and a unique destination of the form /inputs/name. All mounts are read-only. :network accepts :none (the default).

:limits is a keyword list. Defaults are 500 CPU millicores, 128 MiB of memory, 10,000 ms, 32 processes, 16 KiB of text, 256 KiB per capture and 16 MiB of scratch space. The keys are :cpu_millicores, :memory_bytes, :time_ms, :processes, :output_bytes, :capture_bytes and :scratch_bytes.

new_id()

@spec new_id() :: operation_id()

Generate a fresh operation ID for a host call to execute/3.

read(sandbox, id)

@spec read(t(), operation_id()) :: {:ok, map()} | {:error, operation_error()}

Read a confirmed execution receipt and its bounded transcript in this scope. The JSON receipt is the returned map with "text" removed. {:error, {:unavailable, id}} means the read could not obtain a valid response. {:error, {:result_unconfirmed, id}} reports a retained operation with an uncertain outcome. Either error or a missing record leaves reconciliation open.

tools(sandbox, opts \\ [])

@spec tools(t(), keyword()) :: [ExAgent.Tool.t()]

Build sandbox_execute and sandbox_read. Code execution requires approval even with an allow rule. Supply an ExAgent model struct as :model to include sandbox_capture when its profile accepts image tool results. Capture also checks the active profile before fetching an image.

The read and capture tools return a bounded :tool_error when the sandbox is unavailable or reports that the queried execution is unconfirmed.

With :jobs, also build sandbox_start using a Jobs process dedicated to this same scope. Its operation ID doubles as the job ID.