Sandbox
SandboxScriptError
InvalidSandboxStep
Bases: SandboxScriptError
Raised when a sandbox step is invalid at factory construction time.
ソースコード位置: src/agents/testing/sandbox.py
UnexpectedSandboxCall
Bases: SandboxScriptError
Raised when a call does not match the next configured sandbox step.
ソースコード位置: src/agents/testing/sandbox.py
SandboxCallMatcherError
Bases: SandboxScriptError
Raised when a sandbox step matcher rejects its call.
ソースコード位置: src/agents/testing/sandbox.py
UnconsumedSandboxSteps
Bases: SandboxScriptError
Raised when configured sandbox steps remain unconsumed.
ソースコード位置: src/agents/testing/sandbox.py
SandboxCall
dataclass
SandboxStepSpec
Bases: TypedDict
Dictionary form of one FIFO scripted sandbox call.
ソースコード位置: src/agents/testing/sandbox.py
ScriptedSandboxSession
Bases: BaseSandboxSession, ABC
The typed result interface for scripted_sandbox_session.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: src/agents/sandbox/session/base_sandbox_session.py
run_pre_stop_hooks
async
Run registered pre-stop hooks once before workspace persistence.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: src/agents/sandbox/session/base_sandbox_session.py
read_bounded
async
Read a prefix of at most max_bytes bytes.
Uses the session identity and the same path scope as read(path).
A result of exactly max_bytes bytes does not indicate EOF. Backends
provided by the SDK bound acquisition from the source. For compatibility,
custom backends inherit a fallback that consumes and closes the stream
returned by read(). That fallback cannot bound any data acquired inside
read() before it returns. Override _read_bounded to provide that
guarantee. Encoded transports may reject responses exceeding their bounded
framing budget. Failures use payload-free WorkspaceArchiveReadError
diagnostics.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: src/agents/sandbox/session/base_sandbox_session.py
mv
async
mv(
source: Path | str,
destination: Path | str,
*,
user: str | User | None = None,
) -> None
Rename a regular file or symlink, replacing the destination entry if it exists.
Directory and special-file sources are unsupported and rejected before the move. As with other workspace file APIs, callers must serialize conflicting mutations.
The exec fallback requires mv -T (GNU or compatible). A tool without that option
fails without moving the source. Do not replace it with a destination precheck: a
concurrently created directory could otherwise receive the source as a child.
This is the shell fallback for backends that only offer exec. A backend with
direct filesystem access, such as UnixLocal, overrides it with a descriptor-relative
os.rename, so the path it validated is the entry it renames.
:param source: Regular file or symlink to move. :param destination: Path to move it to. :param user: Optional sandbox user to move as. :raises ExecNonZeroError: If the destination is an existing directory, or the move fails.
ソースコード位置: src/agents/sandbox/session/base_sandbox_session.py
same_file
async
same_file(
left: Path | str,
right: Path | str,
*,
follow_symlinks: bool = True,
user: str | User | None = None,
) -> bool
Return whether two paths name the same file on the sandbox filesystem.
This asks the filesystem, through test -ef, which compares device and inode. Two
paths that differ as strings can be one file: a filesystem that folds case stores
notes.txt and Notes.txt as a single entry, and APFS folds Unicode normalization
as well, so the NFC and NFD spellings of one accented name are also a single entry.
No string comparison can answer this, and neither can the host that is driving the
session, which may not be the kind of system the sandbox is running on.
test -ef resolves symlinks. With follow_symlinks=False, return false if either
leaf is a symlink, including two paths naming the same symlink. This mode compares
only non-symlink entries and is used before removing an apply-patch source.
This is the shell fallback for backends that only offer exec. UnixLocal overrides
it with a descriptor-relative stat on both entries.
:param left: First path to compare. :param right: Second path to compare. :param follow_symlinks: If false, return false when either leaf is a symlink. :param user: Optional sandbox user to compare as. :returns: True when both paths resolve to the same file.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: 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.