Skip to content

fix: atomic_replace() fails with EXDEV when HERMES_HOME is a cross-filesystem symlink #34252

Closed as not planned

Description

@ccwssy

title: "fix: atomic_replace() fails with EXDEV when HERMES_HOME is a cross-filesystem symlink"
labels: ["bug", "gateway"]

Description

atomic_replace() in utils.py uses os.replace() to atomically move a temp file onto the target path. However, when ~/.hermes/ is a symlink pointing to a different filesystem (e.g., /mnt/data/hermes-data/), os.replace() raises OSError: [Errno 18] Invalid cross-device link because the temp file (created on rootfs) and the target (on a different mount) are on different filesystems.

Impact

  • hermes model and hermes config commands crash with OSError: [Errno 18]
  • Gateway fails to persist dedup state (feishu_seen_message_ids.json) silently
  • Channel directory state is lost on gateway restart
  • Users with ~/.hermes -> /mnt/data/... symlinks cannot use the agent at all

Reproduction

# Setup: HERMES_HOME symlink to a different filesystem
ln -sf /mnt/data/hermes-data ~/.hermes
hermes config set model.default test  # Fails with EXDEV

Or in ~/.hermes/hermes-agent:

python3 -c "
import tempfile, os
tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.tmp')
tmp.write(b'test')
tmp.close()
os.replace(tmp.name, os.path.expanduser('~/.hermes/.env'))
# OSError: [Errno 18] Invalid cross-device link
"

Root Cause

https://github.com/NousResearch/hermes-agent/blob/main/utils.py#L77-L86

def atomic_replace(tmp_path, target):
    real_path = os.path.realpath(target_str) if os.path.islink(target_str) else target_str
    os.replace(str(tmp_path), real_path)  # ← fails with EXDEV on cross-fs
    return real_path

os.replace() (backed by rename() syscall) cannot move files across mount points. The fix should fall back to shutil.move() when errno == 18 (EXDEV).

Proposed Fix

def atomic_replace(tmp_path, target):
    real_path = os.path.realpath(target_str) if os.path.islink(target_str) else target_str
    try:
        os.replace(str(tmp_path), real_path)
    except OSError as e:
        if e.errno == 18:  # EXDEV: cross-device link
            import shutil
            shutil.move(str(tmp_path), real_path)
        else:
            raise
    return real_path

Related

The same cross-filesystem issue affects save_env_value() / remove_env_value() in hermes_cli/config.py, where tempfile.NamedTemporaryFile creates temp files in the default tmpdir (rootfs) while .resolve() isn't called on get_env_path(). Adding .resolve() to env_path = get_env_path() calls covers this case.

Environment

  • Hermes Agent v0.14.0+ (also present in v2026.5.28)
  • Linux with ~/.hermes symlinked to a separate mount point

Activity

  1. added
    type/bugSomething isn't working
    P2Medium — degraded but workaround exists
    area/configConfig system, migrations, profiles
    on May 29, 2026
  2. alt-glitch commented on May 29, 2026

    @alt-glitch

    Duplicate of #17313 — same root cause: atomic_replace() uses os.replace() which fails with EXDEV on cross-filesystem symlinks. Multiple fix PRs have been attempted: #17322, #17357, #33508, and now #34262. None merged yet.

  3. teknium1 commented on Jul 13, 2026

    @teknium1
    Collaborator

    Thanks for the clear reproduction. This is already fixed on current main by a later implementation.

    • Automated hermes-sweeper review verified utils.py:114-135: atomic_replace() catches EXDEV and falls back to copy/fsync/unlink against the resolved symlink target.
    • Commit bf8effa (fix(utils): copy fallback for atomic replace across devices, fix(utils): EXDEV/EBUSY copy fallback in atomic_replace (port from gemini-cli#21541) #43852) is contained in current main.
    • tests/test_atomic_replace_symlinks.py:324-347 exercises an actual cross-filesystem /dev/shm target through a symlink; :245-280 additionally covers simulated EXDEV and symlink preservation.
    • This supersedes the earlier duplicate/fix-PR discussion referenced by @alt-glitch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existsarea/configConfig system, migrations, profilessweeper:implemented-on-mainSweeper: behavior already present on current maintype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions