11from __future__ import annotations
22
33import asyncio
4+ import hashlib
45import io
6+ import subprocess
57from pathlib import Path
68from typing import Literal
79
810import pytest
911from pydantic import PrivateAttr , ValidationError
1012
1113from agents .sandbox import Manifest , RemoteSnapshot , RemoteSnapshotSpec , resolve_snapshot
12- from agents .sandbox .entries import File
14+ from agents .sandbox .entries import Dir , File
1315from agents .sandbox .errors import SnapshotPersistError
1416from agents .sandbox .materialization import MaterializationResult
1517from 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
477533async def test_start_closes_restored_workspace_archive (tmp_path : Path ) -> None :
478534 snapshot = TestClosingRestoreSnapshot (id = "resume-snapshot" )
0 commit comments