1616import pytest
1717
1818from agents .editor import ApplyPatchOperation
19- from agents .sandbox import SandboxPathGrant
19+ from agents .sandbox import LocalSnapshotSpec , SandboxPathGrant
2020from agents .sandbox .errors import (
2121 ApplyPatchDiffError ,
2222 PtySessionNotFoundError ,
3030 UnixLocalSandboxSessionState ,
3131 _UnixPtyProcessEntry ,
3232)
33- from agents .sandbox .snapshot import NoopSnapshot
33+ from agents .sandbox .snapshot import LocalSnapshot , NoopSnapshot
3434from agents .sandbox .types import ExecResult , User
3535from 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
59208async def test_unix_local_inherits_host_environment_by_default (
60209 tmp_path : Path ,
0 commit comments