Skip to content

The agent that grows with you

fix(config): replace a cyclic SOUL.md symlink instead of failing home init

KeyArgo85620ff30139
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index 6873e225fcd..524292ad0e9 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -11,6 +11,7 @@ drop_stale_root_modules()
import copy
import difflib
+import errno
import json
import logging
import os
@@ -642,6 +643,36 @@ def _ensure_default_soul_md(home: Path) -> None:
"""Seed DEFAULT_SOUL_MD on first run; upgrade a legacy comment-only scaffold in place.
A SOUL.md the user actually customized is never touched."""
soul_path = home / "SOUL.md"
+ if soul_path.is_symlink():
+ try:
+ soul_path.stat()
+ except OSError as exc:
+ # A cyclic link chain is the only failure this branch handles; a dangling link
+ # (missing target) or an unresolvable mount keeps the existing behaviour below.
+ if exc.errno == errno.ELOOP:
+ # ``stat`` refuses a cyclic chain, so the seeding write below would follow the
+ # link and raise ELOOP — an OSError initialize_home turns into
+ # HomeInitializationError, leaving the gateway unable to boot (exit-75 relaunch
+ # loop, #114592). A cyclic link cannot resolve to content: seed the default as a
+ # regular file IN PLACE OF the link, never through it. A unique temp name keeps
+ # concurrent boot attempts off one shared path; a failure here leaves the link
+ # alone instead of killing home initialization.
+ try:
+ fd, tmp_name = tempfile.mkstemp(prefix=".SOUL.md.", suffix=".seed", dir=str(home))
+ except OSError:
+ return
+ try:
+ with os.fdopen(fd, "w", encoding="utf-8") as handle:
+ handle.write(DEFAULT_SOUL_MD)
+ os.replace(tmp_name, soul_path)
+ except OSError:
+ try:
+ os.unlink(tmp_name)
+ except OSError:
+ pass
+ return
+ _secure_file(soul_path)
+ return
if soul_path.exists():
try:
existing = soul_path.read_text(encoding="utf-8")
diff --git a/tests/hermes_cli/test_home_init_soul_symlink.py b/tests/hermes_cli/test_home_init_soul_symlink.py
new file mode 100644
index 00000000000..f8ec5123032
--- /dev/null
+++ b/tests/hermes_cli/test_home_init_soul_symlink.py
@@ -0,0 +1,114 @@
+"""Regression for #114592: a cyclic SOUL.md symlink must not kill home initialization.
+
+A cyclic SOUL.md link makes ``stat`` fail with ELOOP, so the seeding write followed the
+link and raised OSError (errno 40 on Linux, 62 on macOS). ``initialize_home`` turned that
+into ``HomeInitializationError``, so every gateway spawn died before boot (exit-75
+relaunch storm). The seed now replaces a cyclic link instead of writing through it.
+
+Boundary: only a cyclic chain is replaced — a dangling link or a link that resolves is
+left alone, so operator wiring and dangling-link preservation stay out of scope.
+"""
+
+import os
+
+from hermes_cli.config import DEFAULT_SOUL_MD, _ensure_default_soul_md
+from hermes_cli.config_home import initialize_home
+from hermes_cli.default_soul import _LEGACY_TEMPLATE_SOULS
+
+_SUBDIRS = ("cron", "sessions", "logs", "memories")
+
+
+def _seed_temps(home):
+ return [p.name for p in home.iterdir() if p.name.startswith(".SOUL.md.")]
+
+
+def test_initialize_home_survives_self_referential_soul_symlink(tmp_path):
+ """The reported boot-kill path: a home whose SOUL.md link points at itself."""
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ soul = home / "SOUL.md"
+ soul.symlink_to(soul)
+
+ initialize_home(home, _SUBDIRS, set())
+
+ assert not soul.is_symlink()
+ assert soul.read_text(encoding="utf-8") == DEFAULT_SOUL_MD
+ assert not _seed_temps(home)
+
+
+def test_soul_symlink_to_customized_file_is_left_alone(tmp_path):
+ """A resolving link is operator wiring: the link survives, content is untouched."""
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ target = tmp_path / "shared-identity.md"
+ target.write_text("custom identity\n", encoding="utf-8")
+ soul = home / "SOUL.md"
+ soul.symlink_to(target)
+
+ _ensure_default_soul_md(home)
+ initialize_home(home, _SUBDIRS, set())
+
+ assert soul.is_symlink()
+ assert soul.read_text(encoding="utf-8") == "custom identity\n"
+
+
+def test_soul_symlink_to_legacy_scaffold_keeps_link_and_upgrades_target(tmp_path):
+ """A resolving link keeps pointing at its target, which is upgraded in place."""
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ target = tmp_path / "shared-scaffold.md"
+ target.write_text(_LEGACY_TEMPLATE_SOULS[1], encoding="utf-8")
+ soul = home / "SOUL.md"
+ soul.symlink_to(target)
+
+ _ensure_default_soul_md(home)
+
+ assert soul.is_symlink()
+ assert target.read_text(encoding="utf-8") == DEFAULT_SOUL_MD
+
+
+def test_dangling_soul_symlink_is_not_replaced(tmp_path):
+ """Boundary: a dangling link is not a cycle — the replacement branch must not touch it."""
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ soul = home / "SOUL.md"
+ soul.symlink_to(home / "does-not-exist.md")
+
+ _ensure_default_soul_md(home)
+
+ assert soul.is_symlink()
+
+
+def test_unseedable_cyclic_soul_symlink_still_boots(tmp_path, monkeypatch):
+ """A home where the seed cannot be committed: initialization must not raise."""
+ import hermes_cli.config as config_mod
+
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ soul = home / "SOUL.md"
+ soul.symlink_to(soul)
+
+ class _ReadOnlyOS:
+ def __getattr__(self, name):
+ return getattr(os, name)
+
+ def replace(self, *args, **kwargs):
+ raise PermissionError("read-only home")
+
+ monkeypatch.setattr(config_mod, "os", _ReadOnlyOS())
+
+ initialize_home(home, _SUBDIRS, set())
+
+ assert soul.is_symlink()
+ assert not _seed_temps(home)
+
+
+def test_missing_soul_md_still_seeds_default(tmp_path):
+ """Unchanged happy path: first run seeds DEFAULT_SOUL_MD as a regular file."""
+ home = tmp_path / ".hermes"
+ home.mkdir()
+
+ initialize_home(home, _SUBDIRS, set())
+
+ assert not (home / "SOUL.md").is_symlink()
+ assert (home / "SOUL.md").read_text(encoding="utf-8") == DEFAULT_SOUL_MD