Sandbox
SandboxScriptError
InvalidSandboxStep
Bases: SandboxScriptError
Raised when a sandbox step is invalid at factory construction time.
Source code in src/agents/testing/sandbox.py
UnexpectedSandboxCall
Bases: SandboxScriptError
Raised when a call does not match the next configured sandbox step.
Source code in src/agents/testing/sandbox.py
SandboxCallMatcherError
Bases: SandboxScriptError
Raised when a sandbox step matcher rejects its call.
Source code in src/agents/testing/sandbox.py
UnconsumedSandboxSteps
Bases: SandboxScriptError
Raised when configured sandbox steps remain unconsumed.
Source code in src/agents/testing/sandbox.py
SandboxCall
dataclass
A detached invocation-time sandbox call snapshot.
Source code in src/agents/testing/sandbox.py
SandboxStepSpec
Bases: TypedDict
Dictionary form of one FIFO scripted sandbox call.
Source code in src/agents/testing/sandbox.py
ScriptedSandboxSession
Bases: BaseSandboxSession, ABC
The typed result interface for scripted_sandbox_session.
Source code in src/agents/testing/sandbox.py
calls
abstractmethod
property
calls: tuple[SandboxCall, ...]
Return detached call-history snapshots in invocation order.
remaining_steps
abstractmethod
property
Return the number of configured calls that remain.
assert_complete
abstractmethod
stop
async
Persist/snapshot the workspace.
Note: stop() is intentionally persistence-only. Sandboxes that need to tear down
sandbox resources (Docker containers, remote sessions, etc.) should implement
shutdown() instead.
Source code in src/agents/sandbox/session/base_sandbox_session.py
supports_docker_volume_mounts
Return whether this backend attaches Docker volume mounts before manifest apply.
shutdown
async
Tear down sandbox resources (best-effort).
Default is a no-op. Sandbox-specific sessions (e.g. Docker) should override.
Source code in src/agents/sandbox/session/base_sandbox_session.py
aclose
async
Run the session cleanup lifecycle outside of async with.
This performs the same session-owned cleanup as __aexit__(): persist/snapshot the
workspace via stop(), tear down session resources via shutdown(), and close
session-scoped dependencies. If the session came from a sandbox client, call the client's
delete() separately for backend-specific deletion such as removing a Docker container
or deleting a temporary host workspace.
Source code in src/agents/sandbox/session/base_sandbox_session.py
register_pre_stop_hook
Register an async hook to run once before the session workspace is persisted.
Source code in src/agents/sandbox/session/base_sandbox_session.py
run_pre_stop_hooks
async
Run registered pre-stop hooks once before workspace persistence.
Source code in src/agents/sandbox/session/base_sandbox_session.py
register_persist_workspace_skip_path
Exclude a runtime-created workspace path from future workspace snapshots.
Use this for session side effects that are not part of durable workspace state, such as generated mount config or ephemeral sink output.
Source code in src/agents/sandbox/session/base_sandbox_session.py
exec
async
exec(
*command: str | Path,
timeout: float | None = None,
shell: bool | list[str] = True,
user: str | User | None = None,
) -> ExecResult
Execute a command inside the session.
:param command: Command and args (will be stringified).
:param timeout: Optional wall-clock timeout in seconds.
:param shell: Whether to run this command in a shell. If True is provided,
the command will be run prefixed by sh -lc. A custom shell prefix may be used
by providing a list.
:returns: An ExecResult containing stdout/stderr and exit code.
:raises TimeoutError: If the sandbox cannot complete within timeout.
Source code in src/agents/sandbox/session/base_sandbox_session.py
read
abstractmethod
async
read(
path: Path, *, user: str | User | None = None
) -> IOBase
Read a file from the session's workspace.
:param path: Absolute path in the container or path relative to the workspace root. :param user: Optional sandbox user to perform the read as. :returns: A readable file-like object. :raises: FileNotFoundError: If the path does not exist.
Source code in src/agents/sandbox/session/base_sandbox_session.py
write
abstractmethod
async
write(
path: Path,
data: IOBase,
*,
user: str | User | None = None,
) -> None
Write a file into the session's workspace.
:param path: Absolute path in the container or path relative to the workspace root. :param data: A file-like object positioned at the start of the payload. :param user: Optional sandbox user to perform the write as.
Source code in src/agents/sandbox/session/base_sandbox_session.py
running
abstractmethod
async
persist_workspace
abstractmethod
async
Serialize the session's workspace into a byte stream.
:returns: A readable byte stream representing the workspace contents. Portable tar streams must use workspace-relative member paths rather than embedding the source backend's workspace root directory.
Source code in src/agents/sandbox/session/base_sandbox_session.py
hydrate_workspace
abstractmethod
async
Populate the session's workspace from a serialized byte stream.
:param data: A readable byte stream as produced by persist_workspace.
Portable tar streams are extracted underneath this session's workspace root.
Source code in src/agents/sandbox/session/base_sandbox_session.py
ls
async
ls(
path: Path | str, *, user: str | User | None = None
) -> list[FileEntry]
List directory contents.
:param path: Path to list.
:param user: Optional sandbox user to list as.
:returns: A list of FileEntry objects.
Source code in src/agents/sandbox/session/base_sandbox_session.py
rm
async
rm(
path: Path | str,
*,
recursive: bool = False,
user: str | User | None = None,
) -> None
Remove a file or directory.
:param path: Path to remove. :param recursive: If true, remove directories recursively. :param user: Optional sandbox user to remove as.
Source code in src/agents/sandbox/session/base_sandbox_session.py
mkdir
async
mkdir(
path: Path | str,
*,
parents: bool = False,
user: str | User | None = None,
) -> None
Create a directory.
:param path: Directory to create on the remote. :param parents: If true, create missing parents. :param user: Optional sandbox user to create the directory as.
Source code in src/agents/sandbox/session/base_sandbox_session.py
extract
async
extract(
path: Path | str,
data: IOBase,
*,
compression_scheme: Literal["tar", "zip"] | None = None,
archive_limits: SandboxArchiveLimits | None = None,
) -> None
Write a compressed archive to a destination on the remote. Optionally extract the archive once written.
:param path: Path on the host machine to extract to :param data: a file-like io stream. :param compression_scheme: either "tar" or "zip". If not provided, it will try to infer from the path. :param archive_limits: optional per-call archive resource limits. If omitted, the session default is used.
Source code in src/agents/sandbox/session/base_sandbox_session.py
should_provision_manifest_accounts_on_resume
Return whether resume should reprovision manifest-managed users and groups.
scripted_sandbox_session
scripted_sandbox_session(
steps: Iterable[
SandboxStepSpec | Mapping[str, Any]
] = (),
*,
manifest: Manifest | None = None,
) -> ScriptedSandboxSession
Create a deterministic provider-free sandbox session for agent workflow tests.
Each FIFO step defines method plus exactly one of result, responder, or error.
An optional match callable receives a detached SandboxCall. The returned object is the
session itself, so pass it directly to SandboxRunConfig(session=session). Only configured
model-facing methods are visible. The two PTY methods are exposed together when either one is
configured because they form one advertised session capability. Use a custom
BaseSandboxSession or a real provider for lifecycle, persistence, mount, or broader
filesystem behavior.