openai-agents-python/tests/sandbox/test_manifest_application.py
Steve Coffey 2d665c9a67
Sandbox Agents (#2889)
### Sandbox Agents

This release adds **Sandbox Agents**, a beta SDK surface for running
agents with a persistent, isolated workspace. Sandbox agents keep the
normal `Agent` and `Runner` flow, but add workspace manifests,
sandbox-native capabilities, sandbox clients, snapshots, and resume
support so agents can work over real files, run commands, edit
repositories, generate artifacts, and continue work across runs.

Key pieces:

- `SandboxAgent`: an `Agent` with sandbox defaults such as
`default_manifest`, sandbox instructions, capabilities, and `run_as`.
- `Manifest`: a fresh-workspace contract for files, directories, local
files, local directories, Git repos, environment, users, groups, and
mounts.
- `SandboxRunConfig`: per-run sandbox wiring for client creation, live
session injection, serialized session resume, manifest overrides,
snapshots, and materialization concurrency limits.
- Built-in capabilities for shell access, filesystem editing and image
inspection, skills, memory, and compaction.
- Workspace snapshots and serialized sandbox session state for
reconnecting to existing work or seeding a fresh sandbox from saved
contents.

### Sandbox clients and hosted providers

Sandbox agents now support local, containerized, and hosted execution
backends:

- `UnixLocalSandboxClient` for fast local development.
- `DockerSandboxClient` for container isolation and image parity.
- Hosted sandbox clients for Blaxel, Cloudflare, Daytona, E2B, Modal,
Runloop, and Vercel through optional extras.

The release also adds provider-specific examples and mount strategies
for common storage backends, including S3, Cloudflare R2, Google Cloud
Storage, Azure Blob Storage, and S3 Files where supported by the
selected backend.

### Sandbox memory

Adds a sandbox memory capability that lets future sandbox-agent runs
learn from prior runs. Memory stores extracted lessons in the sandbox
workspace, injects a concise summary into later runs, and uses
progressive disclosure so agents can search deeper rollout summaries
only when useful.

Memory supports:

- Read-only or generate-only modes.
- Live updates when the agent discovers stale memory.
- Multi-turn grouping through `conversation_id`, SDK `Session`,
`RunConfig.group_id`, or generated run IDs.
- Separate memory layouts for isolating memory across agents or
workflows.
- S3-backed examples for persisted memory across runs.

### Workspace mounts, snapshots, and resume

This release adds a full workspace entry and mount model for sandbox
sessions:

- Local files and directories.
- Synthetic files and directories.
- Git repository entries.
- Remote storage mounts for S3, R2, GCS, Azure Blob Storage, and S3
Files.
- Provider-specific mount strategies across Docker, Modal, Cloudflare,
Blaxel, Daytona, E2B, and Runloop.
- Portable snapshots with path normalization, symlink preservation,
mount-safe snapshotting, and remote snapshot support.
- Resume paths through runner-managed `RunState`, explicit
`SandboxSessionState`, or saved snapshots.

### Examples and tutorials

Adds a large `examples/sandbox/` suite covering:

- Local Unix and Docker sandbox runners.
- Docker mount smoke tests for S3, GCS, Azure Blob Storage, and S3
Files.
- Sandbox coding tasks with skills.
- Sandbox agents as tools and handoff patterns.
- Memory examples, including multi-agent/multi-turn memory and S3-backed
memory.
- Tax-prep and healthcare-support workflows.
- Dataroom QA and metric extraction tutorials.
- Repository code review tutorial.
- Vision website clone tutorial.
- Provider examples for Blaxel, Cloudflare, Daytona, E2B, Modal,
Runloop, Temporal, and Vercel.

### Runtime, tracing, and model plumbing

The release includes the runtime plumbing needed to make sandbox agents
work naturally inside the existing SDK:

- Runner-managed sandbox preparation, capability binding, session
lifecycle, state serialization, and resume behavior.
- Sandbox-aware `RunState` serialization.
- Unified sandbox tracing with SDK spans.
- Token usage on tracing spans.
- Runner-managed prompt cache key defaults.
- OpenAI agent registration and harness ID configuration.
- Safer redaction of sensitive MCP tool outputs when sensitive tracing
is disabled.
- Additional OpenAI client/model utilities and Chat Completions
coverage.


## Documentation & Other Changes

- docs: add Asqav to external tracing processors list.
- docs: update translated document pages.

Co-authored-by: Abdulrahman Alfozan <alfozan@openai.com>
Co-authored-by: Aditya Singh <60082699+adityasingh2400@users.noreply.github.com>
Co-authored-by: Andi Liu <andi@openai.com>
Co-authored-by: Aron <263346377+aron-cf@users.noreply.github.com>
Co-authored-by: ashwinnathan-openai <ashwinnathan@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: cploujoux <cploujoux@blaxel.ai>
Co-authored-by: elainegan-openai <168589666+elainegan-openai@users.noreply.github.com>
Co-authored-by: Elias Freider <freider@users.noreply.github.com>
Co-authored-by: Erik Dunteman <erik@erikds-macbook-air.local>
Co-authored-by: Jason Liu <jasonliu@openai.com>
Co-authored-by: Jason Steving <32336750+jasonsteving99@users.noreply.github.com>
Co-authored-by: Kazuhiro Sera <seratch@openai.com>
Co-authored-by: Lovre Pešut <lovre.pesut@gmail.com>
Co-authored-by: Lucas Wang <lucas_wang@lucas-futures.com>
Co-authored-by: Matt Brockman <matt.brockman@e2b.dev>
Co-authored-by: Mish Ushakov <mishushakov@users.noreply.github.com>
Co-authored-by: Naresh <ghostwriternr@gmail.com>
Co-authored-by: nicholasclark-openai <nicholasclark@openai.com>
Co-authored-by: qiyaoq-oai <qiyaoq@openai.com>
Co-authored-by: Scott Trinh <scott@scotttrinh.com>
Co-authored-by: tode-rl <tony@runloop.ai>
Co-authored-by: Wendy Jiao <wendyjiao@openai.com>
2026-04-15 10:00:40 -07:00

453 lines
15 KiB
Python

from __future__ import annotations
import asyncio
from collections.abc import Awaitable, Callable, Sequence
from pathlib import Path
import pytest
import agents.sandbox.session.manifest_application as manifest_application_module
from agents.sandbox.entries import (
Dir,
File,
GCSMount,
InContainerMountStrategy,
MountpointMountPattern,
)
from agents.sandbox.errors import ExecNonZeroError
from agents.sandbox.manifest import Manifest
from agents.sandbox.materialization import MaterializedFile
from agents.sandbox.session.manifest_application import ManifestApplier
from agents.sandbox.types import ExecResult, Group, User
def _materialized(dest: Path) -> list[MaterializedFile]:
return [MaterializedFile(path=dest, sha256=dest.as_posix())]
@pytest.mark.asyncio
async def test_manifest_applier_only_applies_ephemeral_entries_without_account_provisioning() -> (
None
):
mkdir_calls: list[Path] = []
exec_calls: list[tuple[str, ...]] = []
apply_calls: list[tuple[str, Path, Path]] = []
async def mkdir(path: Path) -> None:
mkdir_calls.append(path)
async def exec_checked_nonzero(*command: str) -> ExecResult:
exec_calls.append(command)
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(entry: object, dest: Path, base_dir: Path) -> list[MaterializedFile]:
apply_calls.append((type(entry).__name__, dest, base_dir))
return _materialized(dest)
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
manifest = Manifest(
root="/workspace",
entries={
"keep.txt": File(content=b"keep"),
"tmp.txt": File(content=b"tmp", ephemeral=True),
},
users=[User(name="alice")],
groups=[Group(name="dev", users=[User(name="alice")])],
)
result = await applier.apply_manifest(manifest, only_ephemeral=True)
assert mkdir_calls == [Path("/workspace")]
assert exec_calls == []
assert apply_calls == [("File", Path("/workspace/tmp.txt"), Path("/"))]
assert result.files == _materialized(Path("/workspace/tmp.txt"))
@pytest.mark.asyncio
async def test_manifest_applier_only_ephemeral_reapplies_nested_ephemeral_children() -> None:
apply_calls: list[tuple[str, Path, Path]] = []
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*_command: str) -> ExecResult:
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(entry: object, dest: Path, base_dir: Path) -> list[MaterializedFile]:
apply_calls.append((type(entry).__name__, dest, base_dir))
return _materialized(dest)
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
manifest = Manifest(
root="/workspace",
entries={
"dir": Dir(
children={
"keep.txt": File(content=b"keep"),
"tmp.txt": File(content=b"tmp", ephemeral=True),
}
)
},
)
result = await applier.apply_manifest(manifest, only_ephemeral=True)
assert apply_calls == [("File", Path("/workspace/dir/tmp.txt"), Path("/"))]
assert result.files == _materialized(Path("/workspace/dir/tmp.txt"))
@pytest.mark.asyncio
async def test_manifest_applier_only_ephemeral_reapplies_full_ephemeral_directories() -> None:
applied_entries: list[tuple[object, Path, Path]] = []
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*_command: str) -> ExecResult:
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(entry: object, dest: Path, base_dir: Path) -> list[MaterializedFile]:
applied_entries.append((entry, dest, base_dir))
return _materialized(dest)
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
manifest = Manifest(
root="/workspace",
entries={
"tmp": Dir(
ephemeral=True,
children={
"keep.txt": File(content=b"keep"),
"nested": Dir(children={"child.txt": File(content=b"child")}),
"tmp.txt": File(content=b"tmp", ephemeral=True),
},
)
},
)
result = await applier.apply_manifest(manifest, only_ephemeral=True)
assert len(applied_entries) == 1
entry, dest, base_dir = applied_entries[0]
assert isinstance(entry, Dir)
assert dest == Path("/workspace/tmp")
assert base_dir == Path("/")
assert set(entry.children) == {"keep.txt", "nested", "tmp.txt"}
assert result.files == _materialized(Path("/workspace/tmp"))
@pytest.mark.asyncio
async def test_manifest_applier_respects_explicit_base_dir() -> None:
apply_calls: list[tuple[str, Path, Path]] = []
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*_command: str) -> ExecResult:
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(entry: object, dest: Path, base_dir: Path) -> list[MaterializedFile]:
apply_calls.append((type(entry).__name__, dest, base_dir))
return _materialized(dest)
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
manifest = Manifest(entries={"file.txt": File(content=b"hello")})
result = await applier.apply_manifest(manifest, base_dir=Path("/tmp/project"))
assert apply_calls == [("File", Path("/workspace/file.txt"), Path("/tmp/project"))]
assert result.files == _materialized(Path("/workspace/file.txt"))
@pytest.mark.asyncio
async def test_manifest_applier_caps_parallel_entry_batch(
monkeypatch: pytest.MonkeyPatch,
) -> None:
observed_limits: list[int | None] = []
async def gather_with_limit_recording(
task_factories: Sequence[Callable[[], Awaitable[list[MaterializedFile]]]],
*,
max_concurrency: int | None = None,
) -> list[list[MaterializedFile]]:
observed_limits.append(max_concurrency)
return [await factory() for factory in task_factories]
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*_command: str) -> ExecResult:
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(_entry: object, dest: Path, _base_dir: Path) -> list[MaterializedFile]:
return _materialized(dest)
monkeypatch.setattr(
manifest_application_module,
"gather_in_order",
gather_with_limit_recording,
)
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
max_entry_concurrency=2,
)
result = await applier.apply_manifest(
Manifest(entries={"a.txt": File(content=b"a"), "b.txt": File(content=b"b")})
)
assert observed_limits == [2]
assert result.files == [
MaterializedFile(path=Path("/workspace/a.txt"), sha256="/workspace/a.txt"),
MaterializedFile(path=Path("/workspace/b.txt"), sha256="/workspace/b.txt"),
]
@pytest.mark.asyncio
async def test_manifest_applier_provisions_groups_and_unique_users_before_entries() -> None:
exec_calls: list[tuple[str, ...]] = []
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*command: str) -> ExecResult:
exec_calls.append(command)
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(_entry: object, _dest: Path, _base_dir: Path) -> list[MaterializedFile]:
return []
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
manifest = Manifest(
users=[User(name="alice")],
groups=[Group(name="dev", users=[User(name="alice"), User(name="bob")])],
)
result = await applier.apply_manifest(manifest)
assert result.files == []
assert exec_calls[0] == ("groupadd", "dev")
assert exec_calls.count(("groupadd", "alice")) == 0
assert exec_calls.count(("groupadd", "bob")) == 0
assert ("useradd", "-U", "-M", "-s", "/usr/sbin/nologin", "alice") in exec_calls
assert ("useradd", "-U", "-M", "-s", "/usr/sbin/nologin", "bob") in exec_calls
assert ("usermod", "-aG", "dev", "alice") in exec_calls
assert ("usermod", "-aG", "dev", "bob") in exec_calls
@pytest.mark.asyncio
async def test_manifest_applier_can_apply_full_manifest_without_account_provisioning() -> None:
exec_calls: list[tuple[str, ...]] = []
apply_calls: list[tuple[str, Path, Path]] = []
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*command: str) -> ExecResult:
exec_calls.append(command)
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(entry: object, dest: Path, base_dir: Path) -> list[MaterializedFile]:
apply_calls.append((type(entry).__name__, dest, base_dir))
return _materialized(dest)
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
manifest = Manifest(
entries={"file.txt": File(content=b"hello")},
users=[User(name="alice")],
groups=[Group(name="dev", users=[User(name="alice")])],
)
result = await applier.apply_manifest(manifest, provision_accounts=False)
assert exec_calls == []
assert apply_calls == [("File", Path("/workspace/file.txt"), Path("/"))]
assert result.files == _materialized(Path("/workspace/file.txt"))
@pytest.mark.asyncio
async def test_manifest_applier_raises_with_command_stdout_and_stderr_on_provision_failure() -> (
None
):
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*command: str) -> ExecResult:
raise ExecNonZeroError(
ExecResult(stdout=b"groupadd output", stderr=b"groupadd failed", exit_code=9),
command=command,
)
async def apply_entry(_entry: object, _dest: Path, _base_dir: Path) -> list[MaterializedFile]:
return []
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
manifest = Manifest(groups=[Group(name="dev", users=[])])
with pytest.raises(ExecNonZeroError) as exc_info:
await applier.apply_manifest(manifest)
assert exc_info.value.context["command"] == ("groupadd", "dev")
assert exc_info.value.context["command_str"] == "groupadd dev"
assert exc_info.value.context["stdout"] == "groupadd output"
assert exc_info.value.context["stderr"] == "groupadd failed"
assert exc_info.value.message == "stdout: groupadd output\nstderr: groupadd failed"
@pytest.mark.asyncio
async def test_manifest_applier_raises_without_stream_labels_when_only_stdout_is_present() -> None:
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*command: str) -> ExecResult:
raise ExecNonZeroError(
ExecResult(stdout=b"useradd unavailable", stderr=b"", exit_code=127),
command=command,
)
async def apply_entry(_entry: object, _dest: Path, _base_dir: Path) -> list[MaterializedFile]:
return []
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
manifest = Manifest(users=[User(name="sandbox-user")])
with pytest.raises(ExecNonZeroError) as exc_info:
await applier.apply_manifest(manifest)
assert exc_info.value.context["command_str"] == (
"useradd -U -M -s /usr/sbin/nologin sandbox-user"
)
assert exc_info.value.context["stdout"] == "useradd unavailable"
assert exc_info.value.context["stderr"] == ""
assert exc_info.value.message == "useradd unavailable"
@pytest.mark.asyncio
async def test_apply_entry_batch_flushes_parallel_work_before_overlapping_paths() -> None:
events: list[tuple[str, Path]] = []
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*_command: str) -> ExecResult:
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(_entry: object, dest: Path, _base_dir: Path) -> list[MaterializedFile]:
events.append(("start", dest))
await asyncio.sleep(0)
events.append(("end", dest))
return _materialized(dest)
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
destinations = [
Path("/workspace/alpha.txt"),
Path("/workspace/beta.txt"),
Path("/workspace/nested"),
Path("/workspace/nested/child.txt"),
]
files = await applier._apply_entry_batch(
[
(destinations[0], File(content=b"a")),
(destinations[1], File(content=b"b")),
(destinations[2], Dir()),
(destinations[3], File(content=b"c")),
],
base_dir=Path("/"),
)
assert [file.path for file in files] == destinations
child_start = events.index(("start", destinations[3]))
assert events.index(("end", destinations[0])) < child_start
assert events.index(("end", destinations[1])) < child_start
assert events.index(("end", destinations[2])) < child_start
@pytest.mark.asyncio
async def test_apply_entry_batch_flushes_before_and_after_mount_entries() -> None:
events: list[tuple[str, Path]] = []
async def mkdir(_path: Path) -> None:
return None
async def exec_checked_nonzero(*_command: str) -> ExecResult:
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
async def apply_entry(_entry: object, dest: Path, _base_dir: Path) -> list[MaterializedFile]:
events.append(("start", dest))
await asyncio.sleep(0)
events.append(("end", dest))
return _materialized(dest)
applier = ManifestApplier(
mkdir=mkdir,
exec_checked_nonzero=exec_checked_nonzero,
apply_entry=apply_entry,
)
destinations = [
Path("/workspace/alpha.txt"),
Path("/workspace/beta.txt"),
Path("/workspace/mount"),
Path("/workspace/gamma.txt"),
]
files = await applier._apply_entry_batch(
[
(destinations[0], File(content=b"a")),
(destinations[1], File(content=b"b")),
(
destinations[2],
GCSMount(
bucket="sandbox-bucket",
mount_strategy=InContainerMountStrategy(pattern=MountpointMountPattern()),
),
),
(destinations[3], File(content=b"c")),
],
base_dir=Path("/"),
)
assert [file.path for file in files] == destinations
mount_start = events.index(("start", destinations[2]))
gamma_start = events.index(("start", destinations[3]))
assert events.index(("end", destinations[0])) < mount_start
assert events.index(("end", destinations[1])) < mount_start
assert events.index(("end", destinations[2])) < gamma_start