Skip to content

The agent that grows with you

fix(file_sync): Windows sync_back no longer drops remote edits (#76267)

fangliquanflq1bfbe19f5fec
diff --git a/tools/environments/file_sync.py b/tools/environments/file_sync.py
index c9cb048e582..766edb13b5e 100644
--- a/tools/environments/file_sync.py
+++ b/tools/environments/file_sync.py
@@ -302,12 +302,16 @@ class FileSyncManager:
except Exception:
file_mapping = []
- with tempfile.NamedTemporaryFile(suffix=".tar") as tf:
- self._bulk_download_fn(Path(tf.name))
+ # mkstemp + close: NamedTemporaryFile keeps an exclusive handle on Windows, so the
+ # backend's open(dest, "wb") / write_bytes on the same path raised PermissionError.
+ fd, tar_path = tempfile.mkstemp(suffix=".tar")
+ os.close(fd)
+ try:
+ self._bulk_download_fn(Path(tar_path))
# A misbehaving sandbox could produce an arbitrarily large tar.
try:
- tar_size = os.path.getsize(tf.name)
+ tar_size = os.path.getsize(tar_path)
except OSError:
tar_size = 0
if tar_size > _SYNC_BACK_MAX_BYTES:
@@ -317,7 +321,7 @@ class FileSyncManager:
return
with tempfile.TemporaryDirectory(prefix="hermes-sync-back-") as staging:
- with tarfile.open(tf.name) as tar:
+ with tarfile.open(tar_path) as tar:
tar.extractall(staging, filter="data")
upload_only = self._upload_only_host_paths | _credential_host_paths()
@@ -325,13 +329,19 @@ class FileSyncManager:
for dirpath, _dirnames, filenames in os.walk(staging):
for fname in filenames:
staged_file = os.path.join(dirpath, fname)
- remote_path = "/" + os.path.relpath(staged_file, staging)
+ # Remote keys are POSIX; relpath uses host separators (backslashes on Windows).
+ remote_path = "/" + Path(os.path.relpath(staged_file, staging)).as_posix()
applied += self._apply_staged_file(staged_file, remote_path, file_mapping, upload_only)
if applied:
logger.info("sync_back: applied %d changed file(s)", applied)
else:
logger.debug("sync_back: no remote changes detected")
+ finally:
+ try:
+ os.unlink(tar_path)
+ except OSError:
+ pass
def _apply_staged_file(
self, staged_file: str, remote_path: str, file_mapping: list[tuple[str, str]], upload_only_host_paths: set[str],
@@ -377,9 +387,9 @@ class FileSyncManager:
for host, remote in file_mapping or []:
if self._is_upload_only_host_path(host, upload_only_host_paths):
continue
- remote_dir = str(Path(remote).parent)
+ remote_dir = posixpath.dirname(remote) # remote paths are POSIX even on a Windows host
if remote_path.startswith(remote_dir + "/"):
- return str(Path(host).parent) + remote_path[len(remote_dir):]
+ return str(Path(host).parent / remote_path[len(remote_dir) + 1:])
return None
@staticmethod