Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/agents/sandbox/sandboxes/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ async def hydrate_workspace(self, data: io.IOBase) -> None:
archive.seek(0)
await self._stream_into_exec(
cmd=["tar", "-x", "-C", root.as_posix()],
stream=archive,
stream=cast(io.IOBase, archive),
error_path=error_root,
)

Expand Down
4 changes: 2 additions & 2 deletions src/agents/sandbox/sandboxes/unix_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ def _preexec() -> None:
else:
with suppress(OSError):
os.close(secondary_fd)
entry = _UnixPtyProcessEntry(process=process, tty=True, primary_fd=primary_fd)
entry.pump_tasks = [asyncio.create_task(self._pump_pty_primary_fd(entry))]
entry = _UnixPtyProcessEntry(process=process, tty=True, primary_fd=primary_fd)
entry.pump_tasks = [asyncio.create_task(self._pump_pty_primary_fd(entry))]
else:
process = await asyncio.create_subprocess_exec(
*exec_command,
Expand Down
3 changes: 2 additions & 1 deletion src/agents/sandbox/util/tar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tempfile
from collections.abc import Iterable
from pathlib import Path, PurePosixPath, PureWindowsPath
from typing import cast


class UnsafeTarMemberError(ValueError):
Expand Down Expand Up @@ -158,7 +159,7 @@ def strip_tar_member_prefix(data: io.IOBase, *, prefix: str | Path) -> io.IOBase
with tarfile.open(fileobj=out, mode="r:*") as tar:
validate_tarfile(tar)
out.seek(0)
return out
return cast(io.IOBase, out)
except Exception:
out.close()
raise
Expand Down
40 changes: 40 additions & 0 deletions tests/sandbox/test_windows_tempfile_iobase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import annotations

import io
import tarfile
import tempfile

from agents.sandbox.util.tar_utils import strip_tar_member_prefix


def _prefixed_workspace_tar() -> io.BytesIO:
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w") as tar:
directory = tarfile.TarInfo("workspace")
directory.type = tarfile.DIRTYPE
tar.addfile(directory)
payload = b"hello"
member = tarfile.TarInfo("workspace/hello.txt")
member.size = len(payload)
tar.addfile(member, io.BytesIO(payload))
buf.seek(0)
return buf


def test_strip_tar_member_prefix_returns_seekable_stream() -> None:
"""Windows TemporaryFile wrappers must remain usable as binary IO streams."""

stream = strip_tar_member_prefix(_prefixed_workspace_tar(), prefix="workspace")
stream.seek(0)
with tarfile.open(fileobj=stream, mode="r:*") as tar:
assert tar.getnames() == [".", "hello.txt"]


def test_temporary_file_supports_hydrate_style_copy() -> None:
"""Docker hydrate copies the archive through TemporaryFile, then seeks it."""

payload = b"ustar-payload"
with tempfile.TemporaryFile() as archive:
archive.write(payload)
archive.seek(0)
assert archive.read() == payload
Loading