Skip to content

Commit 7f83445

Browse files
committed
Merge main and preserve UnixLocal snapshot test imports
2 parents 5479144 + 99f8d77 commit 7f83445

2 files changed

Lines changed: 189 additions & 15 deletions

File tree

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
UnsafeTarMemberError,
7171
safe_extract_tarfile,
7272
should_skip_tar_member,
73+
validate_tarfile,
7374
)
7475
from ..workspace_paths import _raise_if_filesystem_root
7576
from . import _unix_local_file_ops
@@ -1221,19 +1222,17 @@ async def persist_workspace(self) -> io.IOBase:
12211222

12221223
def _archive_workspace() -> None:
12231224
with tarfile.open(fileobj=buf, mode="w") as tar:
1224-
tar.add(
1225-
root,
1226-
arcname=".",
1227-
filter=lambda ti: (
1228-
None
1229-
if should_skip_tar_member(
1230-
ti.name,
1231-
skip_rel_paths=skip,
1232-
root_name=None,
1233-
)
1234-
else ti
1235-
),
1236-
)
1225+
1226+
def filter_member(member: tarfile.TarInfo) -> tarfile.TarInfo | None:
1227+
# tarfile records inodes before filtering. Clear even excluded entries so
1228+
# every retained hardlink has its own payload. Unlike dereference=True,
1229+
# this preserves symlinks instead of reading their targets on the host.
1230+
getattr(tar, "inodes").clear() # noqa: B009 - Not exposed by typeshed.
1231+
if should_skip_tar_member(member.name, skip_rel_paths=skip, root_name=None):
1232+
return None
1233+
return member
1234+
1235+
tar.add(root, arcname=".", filter=filter_member)
12371236

12381237
try:
12391238
await run_blocking_workspace_io(_archive_workspace)
@@ -1243,6 +1242,32 @@ def _archive_workspace() -> None:
12431242
buf.seek(0)
12441243
return buf
12451244

1245+
async def _restore_snapshot_into_workspace_on_resume(self) -> None:
1246+
root = Path(self.state.manifest.root)
1247+
archive = await self.state.snapshot.restore(dependencies=self.dependencies)
1248+
1249+
def validate_archive() -> None:
1250+
try:
1251+
with tarfile.open(fileobj=archive, mode="r:*") as tar:
1252+
validate_tarfile(tar, allow_external_symlink_targets=False)
1253+
archive.seek(0)
1254+
except UnsafeTarMemberError as e:
1255+
raise WorkspaceArchiveWriteError(
1256+
path=root, context={"reason": e.reason, "member": e.member}, cause=e
1257+
) from e
1258+
except (tarfile.TarError, OSError) as e:
1259+
raise WorkspaceArchiveWriteError(path=root, cause=e) from e
1260+
1261+
try:
1262+
# Older snapshots may contain unsupported members. Reject them before discarding
1263+
# the live files; keep hydrate_workspace's own validation for direct callers too.
1264+
await run_blocking_workspace_io(validate_archive)
1265+
await self._clear_workspace_root_on_resume()
1266+
await self.hydrate_workspace(archive)
1267+
finally:
1268+
with suppress(Exception):
1269+
archive.close()
1270+
12461271
async def hydrate_workspace(self, data: io.IOBase) -> None:
12471272
root = Path(self.state.manifest.root)
12481273

‎tests/sandbox/test_unix_local.py‎

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pytest
1717

1818
from agents.editor import ApplyPatchOperation
19-
from agents.sandbox import SandboxPathGrant
19+
from agents.sandbox import LocalSnapshotSpec, SandboxPathGrant
2020
from agents.sandbox.errors import (
2121
ApplyPatchDiffError,
2222
PtySessionNotFoundError,
@@ -30,7 +30,7 @@
3030
UnixLocalSandboxSessionState,
3131
_UnixPtyProcessEntry,
3232
)
33-
from agents.sandbox.snapshot import NoopSnapshot
33+
from agents.sandbox.snapshot import LocalSnapshot, NoopSnapshot
3434
from agents.sandbox.types import ExecResult, User
3535
from tests.sandbox._filesystem_test_session import FilesystemTestSandboxSession
3636

@@ -55,6 +55,155 @@ async def _exec_internal(
5555
return ExecResult(stdout=b"", stderr=b"", exit_code=0)
5656

5757

58+
@pytest.mark.asyncio
59+
@pytest.mark.parametrize("exclude_first", [False, True])
60+
async def test_unix_local_snapshot_round_trips_hardlinks(
61+
tmp_path: Path, exclude_first: bool
62+
) -> None:
63+
workspace = tmp_path / "workspace"
64+
client = UnixLocalSandboxClient(inherit_host_environment=False)
65+
session = await client.create(
66+
manifest=Manifest(root=str(workspace)),
67+
snapshot=LocalSnapshotSpec(base_path=tmp_path / "snapshots"),
68+
)
69+
await session.start()
70+
first = workspace / "a.py"
71+
second = workspace / "b.py"
72+
first.write_bytes(b"VALUE = 1\n")
73+
first.chmod(0o755)
74+
os.link(first, second)
75+
(workspace / "link.py").symlink_to("b.py")
76+
(workspace / "copy.py").write_bytes(b"independent\n")
77+
if exclude_first:
78+
session.register_persist_workspace_skip_path("a.py")
79+
await session.stop()
80+
archive = await session.state.snapshot.restore()
81+
try:
82+
with tarfile.open(fileobj=archive) as tar:
83+
members = {member.name: member for member in tar.getmembers()}
84+
assert members["./b.py"].isreg()
85+
assert members["./link.py"].issym()
86+
if exclude_first:
87+
assert "./a.py" not in members
88+
else:
89+
assert members["./a.py"].isreg()
90+
finally:
91+
archive.close()
92+
93+
# Prove that resume actually restores the snapshot, not the surviving workspace.
94+
second.write_bytes(b"changed after snapshot\n")
95+
(workspace / "stale.txt").write_bytes(b"remove on resume")
96+
resumed = await client.resume(session.state)
97+
try:
98+
await resumed.start()
99+
assert second.read_bytes() == b"VALUE = 1\n"
100+
assert second.stat().st_mode & 0o777 == 0o755
101+
assert (workspace / "copy.py").read_bytes() == b"independent\n"
102+
assert (workspace / "link.py").is_symlink()
103+
assert (workspace / "link.py").read_bytes() == b"VALUE = 1\n"
104+
assert not (workspace / "stale.txt").exists()
105+
if exclude_first:
106+
assert not first.exists()
107+
else:
108+
assert first.read_bytes() == b"VALUE = 1\n"
109+
assert first.stat().st_ino != second.stat().st_ino
110+
finally:
111+
await resumed.shutdown()
112+
await session.shutdown()
113+
114+
115+
@pytest.mark.asyncio
116+
@pytest.mark.parametrize("invalid_kind", ["hardlink", "external_symlink", "invalid_tar"])
117+
async def test_unix_local_resume_rejects_invalid_snapshot_before_clearing_workspace(
118+
tmp_path: Path, invalid_kind: str
119+
) -> None:
120+
workspace = tmp_path / "workspace"
121+
client = UnixLocalSandboxClient(inherit_host_environment=False)
122+
session = await client.create(
123+
manifest=Manifest(root=str(workspace)),
124+
snapshot=LocalSnapshotSpec(base_path=tmp_path / "snapshots"),
125+
)
126+
await session.start()
127+
(workspace / "keep.txt").write_bytes(b"live workspace")
128+
archive = io.BytesIO()
129+
if invalid_kind == "invalid_tar":
130+
archive.write(b"not a tar archive")
131+
else:
132+
with tarfile.open(fileobj=archive, mode="w") as tar:
133+
member = tarfile.TarInfo("link")
134+
member.type = tarfile.LNKTYPE if invalid_kind == "hardlink" else tarfile.SYMTYPE
135+
member.linkname = "keep.txt" if invalid_kind == "hardlink" else "../outside"
136+
tar.addfile(member)
137+
archive.seek(0)
138+
await session.state.snapshot.persist(archive)
139+
archive.close()
140+
141+
resumed = await client.resume(session.state)
142+
try:
143+
with pytest.raises(WorkspaceArchiveWriteError):
144+
await resumed.start()
145+
assert (workspace / "keep.txt").read_bytes() == b"live workspace"
146+
assert sorted(path.name for path in workspace.iterdir()) == ["keep.txt"]
147+
assert not await resumed.running()
148+
finally:
149+
await resumed.shutdown()
150+
await session.shutdown()
151+
152+
153+
@pytest.mark.asyncio
154+
async def test_unix_local_resume_cancellation_waits_for_archive_validation(
155+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
156+
) -> None:
157+
workspace = tmp_path / "workspace"
158+
client = UnixLocalSandboxClient(inherit_host_environment=False)
159+
session = await client.create(
160+
manifest=Manifest(root=str(workspace)),
161+
snapshot=LocalSnapshotSpec(base_path=tmp_path / "snapshots"),
162+
)
163+
await session.start()
164+
(workspace / "keep.txt").write_bytes(b"live workspace")
165+
await session.stop()
166+
archive = await session.state.snapshot.restore()
167+
started = threading.Event()
168+
release = threading.Event()
169+
events: list[str] = []
170+
validate = unix_local_module.validate_tarfile
171+
172+
async def restore(self: LocalSnapshot, **kwargs: object) -> io.IOBase:
173+
return archive
174+
175+
def slow_validate(tar: tarfile.TarFile, **kwargs: object) -> None:
176+
started.set()
177+
assert release.wait(timeout=5)
178+
validate(tar, allow_external_symlink_targets=False)
179+
events.append("validated")
180+
181+
monkeypatch.setattr(LocalSnapshot, "restore", restore)
182+
monkeypatch.setattr(unix_local_module, "validate_tarfile", slow_validate)
183+
resumed = await client.resume(session.state)
184+
task = asyncio.create_task(resumed.start())
185+
try:
186+
while not started.is_set():
187+
if task.done():
188+
await task
189+
pytest.fail("resume did not validate the archive")
190+
await asyncio.sleep(0.005)
191+
task.cancel()
192+
await asyncio.sleep(0)
193+
assert not task.done()
194+
assert not archive.closed
195+
release.set()
196+
with pytest.raises(asyncio.CancelledError):
197+
await task
198+
assert events == ["validated"]
199+
assert archive.closed
200+
assert (workspace / "keep.txt").read_bytes() == b"live workspace"
201+
finally:
202+
release.set()
203+
await resumed.shutdown()
204+
await session.shutdown()
205+
206+
58207
@pytest.mark.asyncio
59208
async def test_unix_local_inherits_host_environment_by_default(
60209
tmp_path: Path,

0 commit comments

Comments
 (0)