Skip to content

The agent that grows with you

fix: stage atomic temp files beside the resolved symlink target

kshitijk4poor9a4c2892b682
diff --git a/hermes_cli/backup_restore.py b/hermes_cli/backup_restore.py
index d6e2a224884..18bd43d9b0f 100644
--- a/hermes_cli/backup_restore.py
+++ b/hermes_cli/backup_restore.py
@@ -344,9 +344,10 @@ def _extract_member_atomically(
mode &= ~(stat.S_ISUID | stat.S_ISGID)
# Truncate the stem: mkstemp adds ~16 characters, and a member already near
- # NAME_MAX would otherwise fail here on a write that used to succeed.
+ # NAME_MAX would otherwise fail here on a write that used to succeed. Stage beside the
+ # RESOLVED target so a symlinked member renames atomically instead of an EXDEV in-place copy.
fd, tmp_name = tempfile.mkstemp(
- dir=str(target.parent), prefix=f".{target.name[:80]}.", suffix=".partial"
+ dir=os.path.dirname(os.path.realpath(target)), prefix=f".{target.name[:80]}.", suffix=".partial"
)
try:
with os.fdopen(fd, "wb") as dst:
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index 2815fbfe1a1..bc3b43d14f0 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -2628,7 +2628,8 @@ def _write_env_lines(env_path: Path, lines: list, *, preserve_mode: bool) -> Non
original_mode = stat.S_IMODE(env_path.stat().st_mode) if preserve_mode else None
except OSError:
pass
- fd, tmp_path = tempfile.mkstemp(dir=str(env_path.parent), suffix=".tmp", prefix=".env_")
+ # Beside the resolved target so a symlinked .env still renames atomically (no EXDEV copy).
+ fd, tmp_path = tempfile.mkstemp(dir=os.path.dirname(os.path.realpath(env_path)), suffix=".tmp", prefix=".env_")
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
f.writelines(lines)
diff --git a/hermes_cli/env_loader.py b/hermes_cli/env_loader.py
index 92b4bcf113b..a9b687cecbb 100644
--- a/hermes_cli/env_loader.py
+++ b/hermes_cli/env_loader.py
@@ -436,7 +436,8 @@ def _sanitize_env_file_if_needed(path: Path) -> None:
sanitized = _sanitize_env_lines(stripped)
if sanitized != original or force_utf8_rewrite:
import tempfile
- fd, tmp = tempfile.mkstemp(dir=str(path.parent), suffix=".tmp", prefix=".env_")
+ # Beside the resolved target so a symlinked .env still renames atomically (no EXDEV copy).
+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(os.path.realpath(path)), suffix=".tmp", prefix=".env_")
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
f.writelines(sanitized)
diff --git a/tests/test_atomic_replace_symlinks.py b/tests/test_atomic_replace_symlinks.py
index 9fcdf9c16c9..0d509b11cbd 100644
--- a/tests/test_atomic_replace_symlinks.py
+++ b/tests/test_atomic_replace_symlinks.py
@@ -108,6 +108,37 @@ def test_atomic_json_write_preserves_symlink(tmp_path: Path) -> None:
assert loaded == {"hello": "world"}
+def test_atomic_json_write_symlink_into_other_dir_renames_not_copies(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """A link into another dir (treated as another filesystem) must still publish by rename:
+ staging the temp beside the link makes the rename EXDEV and the copy fallback tears."""
+ elsewhere = tmp_path / "elsewhere"
+ elsewhere.mkdir()
+ real = elsewhere / "real.json"
+ real.write_text("{}", encoding="utf-8")
+ link = tmp_path / "link.json"
+ link.symlink_to(real)
+ real_replace = os.replace
+
+ def replace_same_dir_only(src, dst):
+ if os.path.dirname(os.path.realpath(src)) != os.path.dirname(os.path.realpath(dst)):
+ raise OSError(errno.EXDEV, os.strerror(errno.EXDEV))
+ return real_replace(src, dst)
+
+ def no_copy(*a, **k):
+ raise AssertionError("in-place copy fallback used")
+
+ monkeypatch.setattr("utils.os.replace", replace_same_dir_only)
+ monkeypatch.setattr("utils._copy_fallback", no_copy)
+
+ atomic_json_write(link, {"hello": "world"})
+
+ assert link.is_symlink()
+ assert json.loads(real.read_text(encoding="utf-8")) == {"hello": "world"}
+
+
@pytest.mark.require_symlinks
def test_atomic_yaml_write_preserves_symlink(tmp_path: Path) -> None:
real = tmp_path / "real.yaml"
diff --git a/utils.py b/utils.py
index 9915c61234d..57177e6193e 100644
--- a/utils.py
+++ b/utils.py
@@ -286,7 +286,9 @@ def _atomic_write(path: Path, write, *, prefix: str, encoding: str = "utf-8", mo
if mode is None and not path.exists():
mode = default_new_file_mode()
original_owner = _preserve_file_owner(path) if preserve_owner else None
- fd, tmp_path = tempfile.mkstemp(dir=str(path.parent), prefix=prefix, suffix=".tmp")
+ # Stage beside the RESOLVED target: a temp next to a symlink into another filesystem turns the
+ # rename into EXDEV and atomic_replace falls back to a tearable in-place copy.
+ fd, tmp_path = tempfile.mkstemp(dir=str(Path(os.path.realpath(path)).parent), prefix=prefix, suffix=".tmp")
try:
with os.fdopen(fd, "wb" if binary else "w", encoding=None if binary else encoding) as f:
if mode is not None and hasattr(os, "fchmod"):