Skip to content

Commit 61e98ab

Browse files
fix(sandbox): preserve nested paths in workspace snapshots (#5189)
1 parent c467732 commit 61e98ab

9 files changed

Lines changed: 180 additions & 23 deletions

File tree

‎src/agents/sandbox/session/base_sandbox_session.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,10 +1387,14 @@ def _should_compute_snapshot_fingerprint_on_persist(self) -> bool:
13871387

13881388
return True
13891389

1390-
async def _compute_and_cache_snapshot_fingerprint(self) -> dict[str, str]:
1390+
async def _compute_and_cache_snapshot_fingerprint(
1391+
self, *, version: str | None = None
1392+
) -> dict[str, str]:
13911393
"""Compute the current workspace fingerprint in-container and atomically cache it."""
13921394

1393-
return await snapshot_lifecycle.compute_and_cache_snapshot_fingerprint(self)
1395+
return await snapshot_lifecycle.compute_and_cache_snapshot_fingerprint(
1396+
self, version=version
1397+
)
13941398

13951399
async def _read_cached_snapshot_fingerprint(self) -> dict[str, str]:
13961400
"""Read the cached snapshot fingerprint record from the running sandbox."""

‎src/agents/sandbox/session/runtime_helpers.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,14 @@
231231
if [ "$escape_tar_patterns" -eq 1 ]; then
232232
rel=$(printf '%s\\n' "$rel" | sed 's/[][\\\\*?]/\\\\&/g')
233233
fi
234-
quoted_rel=$(quote_sh "$rel")
234+
# Reproduce released fingerprints when comparing a preserved live workspace.
235+
# Switching algorithms before comparison could restore an incomplete old archive.
236+
if [ "$version" = "workspace_tar_sha256_v1" ]; then
237+
quoted_rel=$(quote_sh "$rel")
238+
tar_cmd="$tar_cmd --exclude=$quoted_rel"
239+
fi
235240
quoted_dot_rel=$(quote_sh "./$rel")
236-
tar_cmd="$tar_cmd --exclude=$quoted_rel --exclude=$quoted_dot_rel"
241+
tar_cmd="$tar_cmd --exclude=$quoted_dot_rel"
237242
done
238243
239244
tar_cmd="$tar_cmd -C $(quote_sh "$workspace_root") -cf - ."

‎src/agents/sandbox/session/snapshot_lifecycle.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
if TYPE_CHECKING:
1616
from .base_sandbox_session import BaseSandboxSession
1717

18-
SNAPSHOT_FINGERPRINT_VERSION = "workspace_tar_sha256_v1"
18+
SNAPSHOT_FINGERPRINT_VERSION = "workspace_tar_sha256_v2"
1919

2020

2121
async def persist_snapshot(session: BaseSandboxSession) -> None:
@@ -64,7 +64,10 @@ async def live_workspace_matches_snapshot_on_resume(session: BaseSandboxSession)
6464
return False
6565

6666
try:
67-
cached_record = await session._compute_and_cache_snapshot_fingerprint()
67+
# Compare old records with their original exclusions before deciding to clear
68+
# a live workspace. The next persist upgrades both the archive and fingerprint.
69+
version = stored_version if stored_version == "workspace_tar_sha256_v1" else None
70+
cached_record = await session._compute_and_cache_snapshot_fingerprint(version=version)
6871
except Exception:
6972
return False
7073

@@ -101,12 +104,14 @@ def workspace_fingerprint_skip_relpaths(session: BaseSandboxSession) -> set[Path
101104

102105
async def compute_and_cache_snapshot_fingerprint(
103106
session: BaseSandboxSession,
107+
*,
108+
version: str | None = None,
104109
) -> dict[str, str]:
105110
helper_path = await session._ensure_runtime_helper_installed(WORKSPACE_FINGERPRINT_HELPER)
106111
command = [
107112
str(helper_path),
108113
session._workspace_root_path().as_posix(),
109-
session._snapshot_fingerprint_version(),
114+
version if version is not None else session._snapshot_fingerprint_version(),
110115
session._snapshot_fingerprint_cache_path().as_posix(),
111116
session._resume_manifest_digest(),
112117
]

‎src/agents/sandbox/session/tar_workspace.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ def shell_tar_exclude_args(skip_relpaths: Iterable[Path]) -> list[str]:
1313
rel_posix = rel.as_posix().lstrip("/")
1414
if not rel_posix or rel_posix in {".", "/"}:
1515
continue
16-
excludes.append(f"--exclude={shlex.quote(rel_posix)}")
16+
# Archives are rooted at "."; bare patterns also match unrelated nested paths.
1717
excludes.append(f"--exclude={shlex.quote(f'./{rel_posix}')}")
1818
return excludes

‎tests/extensions/sandbox/test_daytona.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,11 +1195,9 @@ async def test_persist_workspace_uses_nested_mount_targets_and_runtime_skip_path
11951195
f"{daytona_module.DEFAULT_DAYTONA_WORKSPACE_ROOT}/repo/sub",
11961196
}
11971197
tar_command = sandbox.process.exec_calls[0][0]
1198-
assert "--exclude=repo" in tar_command
11991198
assert "--exclude=./repo" in tar_command
1200-
assert "--exclude=repo/sub" in tar_command
12011199
assert "--exclude=./repo/sub" in tar_command
1202-
assert "--exclude=runtime.tmp" in tar_command
1200+
assert "--exclude=./runtime.tmp" in tar_command
12031201

12041202
@pytest.mark.asyncio
12051203
async def test_persist_workspace_remounts_prior_mounts_after_unmount_failure(

‎tests/extensions/sandbox/test_e2b.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,10 +1628,7 @@ async def test_e2b_persist_workspace_excludes_runtime_skip_paths() -> None:
16281628
archive = await session.persist_workspace()
16291629

16301630
assert archive.read() == b"fake-tar-bytes"
1631-
expected_command = (
1632-
"tar --exclude=logs/events.jsonl --exclude=./logs/events.jsonl "
1633-
"-C /workspace -cf - . | base64 -w0"
1634-
)
1631+
expected_command = "tar --exclude=./logs/events.jsonl -C /workspace -cf - . | base64 -w0"
16351632
assert sandbox.commands.calls == [
16361633
{
16371634
"command": expected_command,
@@ -1826,9 +1823,7 @@ async def test_e2b_persist_workspace_uses_nested_mount_targets_and_resolved_excl
18261823
"/workspace/repo/sub",
18271824
]
18281825
tar_command = str(sandbox.commands.calls[-1]["command"])
1829-
assert "--exclude=repo" in tar_command
18301826
assert "--exclude=./repo" in tar_command
1831-
assert "--exclude=repo/sub" in tar_command
18321827
assert "--exclude=./repo/sub" in tar_command
18331828

18341829

‎tests/sandbox/test_runtime_helpers.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,51 @@ def fingerprint() -> str:
8181
assert fingerprint() != first
8282

8383

84+
@pytest.mark.skipif(
85+
sys.platform != "linux",
86+
reason="cloud workspace fingerprints use Linux tar; BSD tar has different exclusion semantics",
87+
)
88+
def test_workspace_fingerprint_detects_nested_paths_with_excluded_name(tmp_path: Path) -> None:
89+
helper_path = _install_fingerprint_helper(tmp_path)
90+
workspace = tmp_path / "workspace"
91+
excluded = workspace / "data/scratch.txt"
92+
durable = workspace / "app/data/users.csv"
93+
excluded.parent.mkdir(parents=True)
94+
durable.parent.mkdir(parents=True)
95+
excluded.write_text("scratch-one", encoding="utf-8")
96+
durable.write_text("id,name", encoding="utf-8")
97+
ordinary = workspace / "app/main.py"
98+
ordinary.write_text("print(1)", encoding="utf-8")
99+
100+
def fingerprint() -> str:
101+
result = subprocess.run(
102+
[
103+
str(helper_path),
104+
str(workspace),
105+
"test-version",
106+
str(tmp_path / "fingerprint.json"),
107+
"manifest-digest",
108+
"data",
109+
],
110+
check=True,
111+
capture_output=True,
112+
text=True,
113+
)
114+
return str(json.loads(result.stdout)["fingerprint"])
115+
116+
first = fingerprint()
117+
assert fingerprint() == first
118+
excluded.write_text("scratch-two", encoding="utf-8")
119+
assert fingerprint() == first
120+
121+
durable.write_text("ID,NAME", encoding="utf-8")
122+
nested_changed = fingerprint()
123+
assert nested_changed != first
124+
125+
ordinary.write_text("print(2)", encoding="utf-8")
126+
assert fingerprint() != nested_changed
127+
128+
84129
@requires_posix_shell
85130
def test_resolve_workspace_path_helper_allows_extra_root_symlink_target(tmp_path: Path) -> None:
86131
helper_path = _install_resolve_helper(tmp_path)

‎tests/sandbox/test_snapshot.py‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import hashlib
45
import io
6+
import subprocess
57
from pathlib import Path
68
from typing import Literal
79

810
import pytest
911
from pydantic import PrivateAttr, ValidationError
1012

1113
from agents.sandbox import Manifest, RemoteSnapshot, RemoteSnapshotSpec, resolve_snapshot
12-
from agents.sandbox.entries import File
14+
from agents.sandbox.entries import Dir, File
1315
from agents.sandbox.errors import SnapshotPersistError
1416
from agents.sandbox.materialization import MaterializationResult
1517
from agents.sandbox.sandboxes.unix_local import UnixLocalSandboxSessionState
@@ -448,7 +450,7 @@ async def test_non_noop_snapshot_stop_records_snapshot_fingerprint(tmp_path: Pat
448450
await session.stop()
449451

450452
assert session.state.snapshot_fingerprint is not None
451-
assert session.state.snapshot_fingerprint_version == "workspace_tar_sha256_v1"
453+
assert session.state.snapshot_fingerprint_version == "workspace_tar_sha256_v2"
452454
cache_payload = session._parse_snapshot_fingerprint_record(
453455
session._snapshot_fingerprint_cache_path().read_text()
454456
)
@@ -473,6 +475,60 @@ async def test_start_skips_snapshot_restore_when_live_workspace_fingerprint_matc
473475
assert session.apply_manifest_calls == [True]
474476

475477

478+
@pytest.mark.asyncio
479+
@pytest.mark.parametrize("workspace_drifted", [False, True])
480+
async def test_start_compares_legacy_fingerprint_before_restoring_workspace(
481+
tmp_path: Path, workspace_drifted: bool
482+
) -> None:
483+
workspace = tmp_path / "workspace"
484+
(workspace / "data").mkdir(parents=True)
485+
(workspace / "app/data").mkdir(parents=True)
486+
durable = workspace / "app/data/users.csv"
487+
durable.write_bytes(b"id,name")
488+
ordinary = workspace / "app/main.py"
489+
ordinary.write_bytes(b"print(1)")
490+
(workspace / "data/scratch.txt").write_bytes(b"scratch")
491+
# Capture the released archive and fingerprint independently of the current helper.
492+
legacy_archive = subprocess.run(
493+
[
494+
"tar",
495+
"--exclude=data",
496+
"--exclude=./data",
497+
"-C",
498+
str(workspace),
499+
"-cf",
500+
"-",
501+
".",
502+
],
503+
check=True,
504+
capture_output=True,
505+
).stdout
506+
session = _ResumeTrackingSession(
507+
workspace_root=workspace,
508+
snapshot=TestRestorableSnapshot(id="legacy", payload=legacy_archive),
509+
)
510+
session.state.manifest = Manifest(
511+
root=workspace.as_posix(), entries={"data": Dir(ephemeral=True)}
512+
)
513+
archive_hash = hashlib.sha256(legacy_archive).hexdigest()
514+
legacy_hash = hashlib.sha256(
515+
f"{archive_hash}\n{session._resume_manifest_digest()}\n".encode()
516+
).hexdigest()
517+
session.state.snapshot_fingerprint = legacy_hash
518+
session.state.snapshot_fingerprint_version = "workspace_tar_sha256_v1"
519+
if workspace_drifted:
520+
ordinary.write_bytes(b"print(2)")
521+
522+
await session.start()
523+
524+
assert session.clear_calls == int(workspace_drifted)
525+
assert session.hydrate_payloads == ([legacy_archive] if workspace_drifted else [])
526+
if not workspace_drifted:
527+
assert durable.read_bytes() == b"id,name"
528+
await session.stop()
529+
assert session.state.snapshot_fingerprint_version == "workspace_tar_sha256_v2"
530+
531+
476532
@pytest.mark.asyncio
477533
async def test_start_closes_restored_workspace_archive(tmp_path: Path) -> None:
478534
snapshot = TestClosingRestoreSnapshot(id="resume-snapshot")
Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,77 @@
1+
import shlex
2+
import shutil
3+
import subprocess
4+
import sys
5+
import tarfile
16
from pathlib import Path
27

8+
import pytest
9+
10+
from agents.sandbox import Manifest
11+
from agents.sandbox.entries import Dir
312
from agents.sandbox.session.tar_workspace import shell_tar_exclude_args
413

514

615
def test_shell_tar_exclude_args_skips_empty_and_dot_paths() -> None:
716
assert shell_tar_exclude_args([Path(""), Path("."), Path("/")]) == []
817

918

10-
def test_shell_tar_exclude_args_sorts_and_adds_plain_and_dot_prefixed_patterns() -> None:
19+
def test_shell_tar_exclude_args_sorts_and_roots_patterns() -> None:
1120
assert shell_tar_exclude_args(
1221
[
1322
Path("logs/events.jsonl"),
1423
Path("cache dir/file.txt"),
1524
]
1625
) == [
17-
"--exclude='cache dir/file.txt'",
1826
"--exclude='./cache dir/file.txt'",
19-
"--exclude=logs/events.jsonl",
2027
"--exclude=./logs/events.jsonl",
2128
]
2229

2330

2431
def test_shell_tar_exclude_args_normalizes_absolute_paths() -> None:
2532
assert shell_tar_exclude_args([Path("/tmp/workspace/cache")]) == [
26-
"--exclude=tmp/workspace/cache",
2733
"--exclude=./tmp/workspace/cache",
2834
]
35+
36+
37+
@pytest.mark.skipif(
38+
sys.platform != "linux" or shutil.which("tar") is None,
39+
reason="cloud workspace archives use Linux tar; BSD tar has different exclusion semantics",
40+
)
41+
@pytest.mark.parametrize("skip_path", ["data", "cache/data", "cache dir"])
42+
def test_workspace_archive_preserves_nested_paths_with_excluded_name(
43+
tmp_path: Path, skip_path: str
44+
) -> None:
45+
workspace = tmp_path / "workspace"
46+
excluded = workspace / skip_path / "scratch.txt"
47+
durable = workspace / "app" / skip_path / "users.csv"
48+
excluded.parent.mkdir(parents=True)
49+
durable.parent.mkdir(parents=True)
50+
excluded.write_text("scratch", encoding="utf-8")
51+
durable.write_text("id,name", encoding="utf-8")
52+
(workspace / "app/main.py").write_text("print(1)", encoding="utf-8")
53+
54+
manifest = Manifest(root=workspace.as_posix(), entries={skip_path: Dir(ephemeral=True)})
55+
excludes = " ".join(shell_tar_exclude_args(manifest.ephemeral_persistence_paths()))
56+
archive_path = tmp_path / "workspace.tar"
57+
subprocess.run(
58+
[
59+
"sh",
60+
"-c",
61+
f"tar {excludes} -C {shlex.quote(workspace.as_posix())} "
62+
f"-cf {shlex.quote(archive_path.as_posix())} .",
63+
],
64+
check=True,
65+
capture_output=True,
66+
)
67+
68+
with tarfile.open(archive_path) as archive:
69+
assert f"./{skip_path}/scratch.txt" not in archive.getnames()
70+
for path, expected in [
71+
(f"./app/{skip_path}/users.csv", b"id,name"),
72+
("./app/main.py", b"print(1)"),
73+
]:
74+
restored = archive.extractfile(path)
75+
assert restored is not None
76+
with restored:
77+
assert restored.read() == expected

0 commit comments

Comments
 (0)