Skip to content

The agent that grows with you

fix(cli): preserve symlinked config.yaml in the migration script's atomic write

briandevanse75336d5977f
diff --git a/optional-skills/migration/openclaw-migration/scripts/openclaw_to_hermes.py b/optional-skills/migration/openclaw-migration/scripts/openclaw_to_hermes.py
index 5c1144b2314..8d5a5a3af00 100644
--- a/optional-skills/migration/openclaw-migration/scripts/openclaw_to_hermes.py
+++ b/optional-skills/migration/openclaw-migration/scripts/openclaw_to_hermes.py
@@ -9,6 +9,7 @@ reports exactly what was skipped and why.
from __future__ import annotations
import argparse
+import errno
import hashlib
import json
import os
@@ -413,20 +414,32 @@ def dump_yaml_file(path: Path, data: Dict[str, Any]) -> None:
an interrupted migration cannot leave a truncated config.yaml behind.
Inlined rather than importing ``utils.atomic_write_text``: this script is
- standalone and runs with only the stdlib on its path.
+ standalone and runs with only the stdlib on its path. The symlink and
+ cross-device handling mirrors ``utils.atomic_replace``: a plain
+ ``os.replace`` onto a symlinked config.yaml would replace the *link* with a
+ regular file, silently detaching managed deployments that symlink
+ ``~/.hermes/config.yaml`` into a dotfiles repo or profile package.
"""
if yaml is None:
raise RuntimeError("PyYAML is required to update Hermes config.yaml")
ensure_parent(path)
+ target = os.path.realpath(str(path)) if os.path.islink(str(path)) else str(path)
fd, tmp_path = tempfile.mkstemp(
- dir=str(path.parent), prefix=".tmp_", suffix=".yaml"
+ dir=os.path.dirname(target) or ".", prefix=".tmp_", suffix=".yaml"
)
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
handle.write(yaml.safe_dump(data, sort_keys=False, allow_unicode=False))
handle.flush()
os.fsync(handle.fileno())
- os.replace(tmp_path, path)
+ try:
+ os.replace(tmp_path, target)
+ except OSError as exc:
+ # Cross-device or bind-mount deployments cannot rename into place.
+ if exc.errno not in (errno.EXDEV, errno.EBUSY):
+ raise
+ shutil.copyfile(tmp_path, target)
+ os.unlink(tmp_path)
except BaseException:
try:
os.unlink(tmp_path)
diff --git a/tests/hermes_cli/test_agent_import.py b/tests/hermes_cli/test_agent_import.py
index c758ad6b721..1f01d24af18 100644
--- a/tests/hermes_cli/test_agent_import.py
+++ b/tests/hermes_cli/test_agent_import.py
@@ -548,7 +548,9 @@ class TestExistingConfigPreserved:
"""A permission problem is the other half of the same root cause."""
import os
- if os.geteuid() == 0:
+ if os.name != "posix":
+ pytest.skip("chmod-based permission denial is POSIX-only")
+ if getattr(os, "geteuid", lambda: 1)() == 0:
pytest.skip("root bypasses file permissions")
config_path.write_text(EXISTING_CONFIG, encoding="utf-8")
before = config_path.read_bytes()
diff --git a/tests/skills/test_openclaw_migration.py b/tests/skills/test_openclaw_migration.py
index f24e117085d..37a7a3bec1b 100644
--- a/tests/skills/test_openclaw_migration.py
+++ b/tests/skills/test_openclaw_migration.py
@@ -247,6 +247,29 @@ def test_absent_config_is_still_created(tmp_path: Path):
assert "anthropic/claude-sonnet-4" in config_path.read_text(encoding="utf-8")
+def test_symlinked_config_stays_a_symlink(tmp_path: Path):
+ """Managed deployments symlink ~/.hermes/config.yaml into a dotfiles repo.
+
+ A plain ``os.replace`` onto the link would detach it into a regular file;
+ ``dump_yaml_file`` resolves the link first, as ``utils.atomic_replace`` does.
+ """
+ mod = load_module()
+ real = tmp_path / "dotfiles" / "config.yaml"
+ real.parent.mkdir(parents=True)
+ real.write_text("model: hermes-4-405b\ncommand_allowlist:\n - /usr/bin/*\n",
+ encoding="utf-8")
+ migrator, config_path = _allowlist_migrator(mod, tmp_path, "placeholder: true\n")
+ config_path.unlink()
+ config_path.symlink_to(real)
+
+ migrator.migrate()
+
+ assert config_path.is_symlink()
+ assert config_path.resolve() == real.resolve()
+ assert "/home/test/**" in real.read_text(encoding="utf-8")
+ assert "hermes-4-405b" in real.read_text(encoding="utf-8")
+
+
def test_unreadable_config_refused_by_model_config_too(tmp_path: Path):
"""The refusal is at the shared helper, so every config step inherits it."""
mod = load_module()