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