Skip to content

Commit 465860b

Browse files
fix(sandbox): add opt-in bounded workspace outbox reads (#5128)
Co-authored-by: Justin Beckwith <[email protected]>
1 parent 848ba47 commit 465860b

23 files changed

Lines changed: 1948 additions & 294 deletions

‎src/agents/extensions/sandbox/blaxel/sandbox.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from ....sandbox.manifest import Manifest
4545
from ....sandbox.session import SandboxSession, SandboxSessionState
4646
from ....sandbox.session.base_sandbox_session import BaseSandboxSession
47+
from ....sandbox.session.bounded_read import collect_bounded
4748
from ....sandbox.session.dependencies import Dependencies
4849
from ....sandbox.session.manager import Instrumentation
4950
from ....sandbox.session.pty_output import collect_pty_output
@@ -506,6 +507,25 @@ async def mkdir(
506507
cause=e,
507508
) from e
508509

510+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
511+
workspace_path = await self._validate_path_access(path)
512+
filesystem = self._sandbox.fs
513+
client = filesystem.get_client()
514+
path_arg = filesystem.format_path(sandbox_path_str(workspace_path))
515+
async with client.stream(
516+
"GET",
517+
f"{filesystem.url}/filesystem/{path_arg}",
518+
headers={"Accept": "application/octet-stream"},
519+
) as response:
520+
if response.status_code == 404:
521+
raise WorkspaceReadNotFoundError(path=path)
522+
if response.status_code != 200:
523+
raise WorkspaceArchiveReadError(
524+
path=path,
525+
retryable=True if response.status_code in TRANSIENT_HTTP_STATUS_CODES else None,
526+
)
527+
return await collect_bounded(response.aiter_bytes(chunk_size=65536), max_bytes)
528+
509529
async def read(self, path: Path | str, *, user: str | User | None = None) -> io.IOBase:
510530
error_path = posix_path_as_path(coerce_posix_path(path))
511531
if user is not None:

‎src/agents/extensions/sandbox/cloudflare/sandbox.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from ....sandbox.manifest import Manifest
5454
from ....sandbox.session import SandboxSession, SandboxSessionState
5555
from ....sandbox.session.base_sandbox_session import BaseSandboxSession
56+
from ....sandbox.session.bounded_read import collect_bounded
5657
from ....sandbox.session.dependencies import Dependencies
5758
from ....sandbox.session.manager import Instrumentation
5859
from ....sandbox.session.mount_lifecycle import (
@@ -1265,6 +1266,43 @@ async def pty_terminate_all(self) -> None:
12651266
for entry in entries:
12661267
await self._terminate_pty_entry(entry)
12671268

1269+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
1270+
workspace_path = await self._validate_path_access(path)
1271+
url_path = quote(sandbox_path_str(workspace_path).lstrip("/"), safe="/")
1272+
async with self._session().get(
1273+
self._url(f"file/{url_path}"), timeout=self._request_timeout()
1274+
) as response:
1275+
if response.status == 404:
1276+
raise WorkspaceReadNotFoundError(path=path)
1277+
if response.status != 200:
1278+
raise WorkspaceArchiveReadError(
1279+
path=path,
1280+
retryable=False
1281+
if response.status == 403
1282+
else _cloudflare_retryability_for_status(response.status),
1283+
)
1284+
# Existing Workers return either bytes or an SSE-encoded file. Bound
1285+
# the wire representation too, before the existing decoder allocates.
1286+
try:
1287+
prefix = await response.content.readexactly(7)
1288+
except asyncio.IncompleteReadError as error:
1289+
prefix = error.partial
1290+
if prefix != b"data: {":
1291+
if len(prefix) >= max_bytes:
1292+
return prefix[:max_bytes]
1293+
return prefix + await collect_bounded(
1294+
response.content.iter_chunked(65536), max_bytes - len(prefix)
1295+
)
1296+
wire_limit = 8 * max_bytes + 65536
1297+
body = prefix + await collect_bounded(
1298+
response.content.iter_chunked(65536), wire_limit + 1 - len(prefix)
1299+
)
1300+
if len(body) > wire_limit:
1301+
raise WorkspaceArchiveReadError(
1302+
path=path, context={"reason": "bounded_read_wire_limit"}
1303+
)
1304+
return self._decode_streamed_payload(body)[:max_bytes]
1305+
12681306
async def read(self, path: Path | str, *, user: str | User | None = None) -> io.IOBase:
12691307
if user is not None:
12701308
await self._check_read_with_exec(path, user=user)

‎src/agents/extensions/sandbox/daytona/sandbox.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
ExecTransportError,
3535
ExposedPortUnavailableError,
3636
InvalidManifestPathError as InvalidManifestPathError,
37+
SandboxError,
3738
WorkspaceArchiveReadError,
3839
WorkspaceArchiveWriteError,
3940
WorkspaceReadNotFoundError,
@@ -43,6 +44,7 @@
4344
from ....sandbox.manifest import Manifest
4445
from ....sandbox.session import SandboxSession, SandboxSessionState
4546
from ....sandbox.session.base_sandbox_session import BaseSandboxSession
47+
from ....sandbox.session.bounded_read import collect_bounded
4648
from ....sandbox.session.dependencies import Dependencies
4749
from ....sandbox.session.manager import Instrumentation
4850
from ....sandbox.session.pty_output import collect_pty_output
@@ -940,6 +942,31 @@ async def _terminate_pty_entry(self, entry: _DaytonaPtySessionEntry) -> None:
940942
except asyncio.TimeoutError:
941943
pass
942944

945+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
946+
workspace_path = await self._validate_path_access(path)
947+
# The high-level download buffers the response. The generated toolbox
948+
# client exposes the same endpoint without preloading its body.
949+
try:
950+
response = await self._sandbox.fs._api_client.download_file_without_preload_content(
951+
path=sandbox_path_str(workspace_path),
952+
_request_timeout=float(self.state.timeouts.file_download_s),
953+
)
954+
try:
955+
if response.status == 404:
956+
raise WorkspaceReadNotFoundError(path=path)
957+
if response.status != 200:
958+
raise WorkspaceArchiveReadError(
959+
path=path, retryable=_DAYTONA_HTTP_STATUS_RETRYABLE.get(response.status)
960+
)
961+
return await collect_bounded(response.content.iter_chunked(65536), max_bytes)
962+
finally:
963+
response.close()
964+
except SandboxError:
965+
raise
966+
except Exception as error:
967+
retryable, _ = _daytona_provider_retryability(error)
968+
raise WorkspaceArchiveReadError(path=path, retryable=retryable) from None
969+
943970
async def read(self, path: Path | str, *, user: str | User | None = None) -> io.IOBase:
944971
error_path = posix_path_as_path(coerce_posix_path(path))
945972
if user is not None:

‎src/agents/extensions/sandbox/e2b/sandbox.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from ....sandbox.manifest import Manifest
5252
from ....sandbox.session import SandboxSession, SandboxSessionState
5353
from ....sandbox.session.base_sandbox_session import BaseSandboxSession
54+
from ....sandbox.session.bounded_read import collect_bounded
5455
from ....sandbox.session.dependencies import Dependencies
5556
from ....sandbox.session.manager import Instrumentation
5657
from ....sandbox.session.pty_output import collect_pty_output
@@ -1114,6 +1115,20 @@ async def pty_terminate_all(self) -> None:
11141115
for entry in entries:
11151116
await self._terminate_pty_entry(entry)
11161117

1118+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
1119+
workspace_path = await self._validate_path_access(path)
1120+
try:
1121+
stream = await _sandbox_read_file(
1122+
self._sandbox, sandbox_path_str(workspace_path), format="stream"
1123+
)
1124+
async with cast(Any, stream) as chunks:
1125+
return await collect_bounded(chunks, max_bytes)
1126+
except _e2b_not_found_error_types():
1127+
raise WorkspaceReadNotFoundError(path=path) from None
1128+
except Exception as error:
1129+
retryable, _ = _e2b_provider_retryability(error)
1130+
raise WorkspaceArchiveReadError(path=path, retryable=retryable) from None
1131+
11171132
async def read(self, path: Path, *, user: str | User | None = None) -> io.IOBase:
11181133
if user is not None:
11191134
await self._check_read_with_exec(path, user=user)

‎src/agents/extensions/sandbox/modal/sandbox.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,39 @@ async def _terminate_pty_entry(self, entry: _ModalPtyProcessEntry) -> None:
12481248
return_exceptions=True,
12491249
)
12501250

1251+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
1252+
try:
1253+
workspace_path = await self._validate_path_access(path)
1254+
await self._ensure_sandbox()
1255+
assert self._sandbox is not None
1256+
# Each read starts a remote operation. Use Modal's 100 MiB per-read
1257+
# ceiling while respecting the caller's remaining byte budget.
1258+
stream = await self._sandbox.open.aio(sandbox_path_str(workspace_path), "rb")
1259+
completed = False
1260+
try:
1261+
result = bytearray()
1262+
while len(result) < max_bytes:
1263+
chunk = await stream.read.aio(min(100 * 1024 * 1024, max_bytes - len(result)))
1264+
if not chunk:
1265+
break
1266+
result.extend(chunk)
1267+
payload = bytes(result)
1268+
completed = True
1269+
return payload
1270+
finally:
1271+
try:
1272+
await asyncio.wait_for(stream.close.aio(), timeout=5.0)
1273+
except Exception:
1274+
# Preserve an active read failure or cancellation. A close failure
1275+
# still fails a read that would otherwise have completed.
1276+
if completed:
1277+
raise
1278+
except (FileNotFoundError, SandboxError):
1279+
raise
1280+
except Exception as error:
1281+
retryable, _ = _modal_provider_retryability(error)
1282+
raise WorkspaceArchiveReadError(path=path, retryable=retryable) from None
1283+
12511284
async def read(self, path: Path, *, user: str | User | None = None) -> io.IOBase:
12521285
if user is not None:
12531286
await self._check_read_with_exec(path, user=user)

‎src/agents/extensions/sandbox/runloop/sandbox.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from ....sandbox.manifest import Manifest
4949
from ....sandbox.session import SandboxSession, SandboxSessionState
5050
from ....sandbox.session.base_sandbox_session import BaseSandboxSession
51+
from ....sandbox.session.bounded_read import collect_bounded
5152
from ....sandbox.session.dependencies import Dependencies
5253
from ....sandbox.session.manager import Instrumentation
5354
from ....sandbox.session.runtime_helpers import RESOLVE_WORKSPACE_PATH_HELPER, RuntimeHelperScript
@@ -955,6 +956,24 @@ async def _resolve_exposed_port(self, port: int) -> ExposedPortEndpoint:
955956
cause=e,
956957
) from e
957958

959+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
960+
normalized_path = await self._validate_path_access(path)
961+
try:
962+
async with self._sdk.api.devboxes.with_streaming_response.download_file(
963+
self.devbox_id,
964+
path=sandbox_path_str(normalized_path),
965+
timeout=self.state.timeouts.file_download_s,
966+
) as response:
967+
return await collect_bounded(response.iter_bytes(chunk_size=65536), max_bytes)
968+
except Exception as error:
969+
if _is_runloop_not_found(error):
970+
raise WorkspaceReadNotFoundError(path=path) from None
971+
if _is_runloop_provider_error(error):
972+
raise WorkspaceArchiveReadError(
973+
path=path, retryable=_runloop_provider_retryability(error)
974+
) from None
975+
raise
976+
958977
async def read(self, path: Path | str, *, user: str | User | None = None) -> io.IOBase:
959978
"""Read a file via Runloop's binary file API."""
960979
error_path = posix_path_as_path(coerce_posix_path(path))

‎src/agents/extensions/sandbox/vercel/sandbox.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from ....sandbox.materialization import MaterializationResult
5757
from ....sandbox.session import SandboxSession, SandboxSessionState, manifest_ops
5858
from ....sandbox.session.base_sandbox_session import BaseSandboxSession
59+
from ....sandbox.session.bounded_read import collect_bounded
5960
from ....sandbox.session.dependencies import Dependencies
6061
from ....sandbox.session.manager import Instrumentation
6162
from ....sandbox.session.mount_lifecycle import (
@@ -1239,6 +1240,35 @@ async def _resolve_exposed_port(self, port: int) -> ExposedPortEndpoint:
12391240
tls=tls,
12401241
)
12411242

1243+
@redact_mount_error_data
1244+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
1245+
async with self._s3_mount_operation():
1246+
normalized_path = await self._validate_path_access(path)
1247+
sandbox = await self._ensure_sandbox()
1248+
try:
1249+
chunks = await sandbox.iter_file(
1250+
sandbox_path_str(normalized_path), chunk_size=65536
1251+
)
1252+
completed = False
1253+
try:
1254+
payload = await collect_bounded(chunks, max_bytes)
1255+
completed = True
1256+
return payload
1257+
finally:
1258+
try:
1259+
await chunks.aclose()
1260+
except Exception:
1261+
# Preserve the primary read failure or cancellation.
1262+
# A close failure is primary only after a successful read.
1263+
if completed:
1264+
raise
1265+
except vercel_sandbox.SandboxNotFoundError:
1266+
raise WorkspaceReadNotFoundError(path=path) from None
1267+
except Exception as error:
1268+
raise WorkspaceArchiveReadError(
1269+
path=path, retryable=_vercel_provider_retryability(error)
1270+
) from None
1271+
12421272
@redact_mount_error_data
12431273
async def read(self, path: Path, *, user: str | User | None = None) -> io.IOBase:
12441274
async with self._s3_mount_operation():

‎src/agents/sandbox/sandboxes/_unix_local_file_ops.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ def read(self, path: Path) -> io.IOBase:
9595
os.close(fd)
9696
raise
9797

98+
def read_bounded(self, path: Path, max_bytes: int) -> bytes:
99+
with self.parent(path) as (parent_fd, name):
100+
fd = os.open(name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=parent_fd)
101+
try:
102+
if not stat.S_ISREG(os.fstat(fd).st_mode):
103+
raise OSError("Bounded reads require a regular file")
104+
stream = os.fdopen(fd, "rb")
105+
except BaseException:
106+
os.close(fd)
107+
raise
108+
with stream:
109+
return stream.read(max_bytes)
110+
98111
def write(self, path: Path, stream: io.IOBase) -> None:
99112
fd = self._open_regular_file(path, for_write=True)
100113
try:

‎src/agents/sandbox/sandboxes/docker.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
MountConfigError,
5454
WorkspaceArchiveReadError,
5555
WorkspaceArchiveWriteError,
56+
WorkspaceReadNotFoundError,
5657
)
5758
from ..manifest import Manifest
5859
from ..session import SandboxSession, SandboxSessionState
@@ -893,6 +894,27 @@ async def _prepare_user_pty_pid_path(self, *, path: Path, user: str | None) -> N
893894
error_path=path,
894895
)
895896

897+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
898+
workspace_path = await self._validate_path_access(path)
899+
# Docker writes already require POSIX sh and head -c. Restrict this
900+
# internal read to image-owned utilities, independent of manifest PATH.
901+
result = await self.exec(
902+
"/bin/sh",
903+
"-c",
904+
"PATH=/usr/bin:/bin; export PATH; "
905+
'[ -e "$1" ] || exit 44; [ -f "$1" ] || exit 45; head -c "$2" < "$1"',
906+
"sh",
907+
sandbox_path_str(workspace_path),
908+
str(max_bytes),
909+
shell=False,
910+
timeout=30.0,
911+
)
912+
if result.exit_code == 44:
913+
raise WorkspaceReadNotFoundError(path=path)
914+
if not result.ok():
915+
raise WorkspaceArchiveReadError(path=path)
916+
return result.stdout
917+
896918
async def read(self, path: Path, *, user: str | User | None = None) -> io.IOBase:
897919
workspace_path = await self._validate_path_access(path)
898920

‎src/agents/sandbox/sandboxes/unix_local.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,9 @@ async def rm(
10301030
except OSError as e:
10311031
raise WorkspaceArchiveWriteError(path=normalized, cause=e) from e
10321032

1033+
async def _read_bounded(self, path: Path, *, max_bytes: int) -> bytes:
1034+
return self._files.read_bounded(self.normalize_path(path), max_bytes)
1035+
10331036
async def read(self, path: Path, *, user: str | User | None = None) -> io.IOBase:
10341037
if user is not None:
10351038
await self._check_read_with_exec(path, user=user)

0 commit comments

Comments
 (0)