Skip to content

The agent that grows with you

fix(update): heal stale root modules at every fresh import site

KeyArgo0d5a07c7ac86
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index 12dfd11566e..2bce6b8ddc0 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -1,6 +1,14 @@
"""Configuration management for Hermes Agent: config.yaml / .env loading, saving,
validation, migration, and the ``hermes config`` command."""
+# Stale-module bridge — must run before ANY import below can bind a root-level symbol.
+# A pre-handoff updater purges only package prefixes after the pull, so a root module
+# (``utils``) stays cached from the OLD tree; the first fresh consumer of its new symbols
+# dies with ImportError before any later heal point is reached. See hermes_cli.stale_modules.
+from hermes_cli.stale_modules import drop_stale_root_modules
+
+drop_stale_root_modules()
+
import copy
import difflib
import json
@@ -28,12 +36,8 @@ from hermes_cli.colors import Colors, color
from hermes_cli import managed_scope
from hermes_cli.default_soul import DEFAULT_SOUL_MD, is_legacy_template_soul
from hermes_cli.secret_prompt import masked_secret_prompt
-from hermes_cli.stale_modules import drop_stale_root_modules
# Re-export from hermes_constants — canonical definition lives there.
from hermes_constants import get_hermes_home, get_process_hermes_home # noqa: F401
-# Drop a pre-pull ``utils`` cache (narrow purge left root modules) before the
-# import below — see hermes_cli.stale_modules. Bridge for ≤v2026.9.14 → HEAD.
-drop_stale_root_modules()
from utils import atomic_replace, atomic_yaml_write, fast_safe_load, file_signature
logger = logging.getLogger(__name__)
diff --git a/hermes_cli/managed_scope.py b/hermes_cli/managed_scope.py
index 0f27db37748..888080075d7 100644
--- a/hermes_cli/managed_scope.py
+++ b/hermes_cli/managed_scope.py
@@ -17,6 +17,13 @@ from typing import Dict, Optional
import yaml
+# Stale-module bridge: this module binds ``utils.file_signature`` at import time, so a fresh
+# import in a post-pull updater process (pre-handoff purge keeps root modules cached) dies
+# unless the stale ``utils`` is dropped first. See hermes_cli.stale_modules.
+from hermes_cli.stale_modules import drop_stale_root_modules
+
+drop_stale_root_modules()
+
from utils import file_signature
logger = logging.getLogger(__name__)
diff --git a/tests/hermes_cli/test_stale_root_module_bridge.py b/tests/hermes_cli/test_stale_root_module_bridge.py
index 3065a47c3c0..0eb080b0621 100644
--- a/tests/hermes_cli/test_stale_root_module_bridge.py
+++ b/tests/hermes_cli/test_stale_root_module_bridge.py
@@ -42,6 +42,31 @@ def _import_fresh_consumer(name: str, source: str) -> types.ModuleType:
return mod
+# Mirrors the post-pull purge of a pre-handoff updater (v2026.9.14
+# hermes_cli/update_cmd_maint.py): package prefixes only, root-level modules survive.
+_PRE_HANDOFF_PURGE_PREFIXES = ("hermes_cli", "gateway", "tools", "tui_gateway", "agent")
+_PRE_HANDOFF_PURGE_PROTECTED = {"hermes_cli", "hermes_cli.main", "hermes_cli.hermes_logging"}
+
+
+def pre_handoff_purge():
+ """Evict what a pre-handoff updater evicts after the pull, restoring it afterwards."""
+ saved: dict = {}
+
+ def _purge() -> None:
+ for name in list(sys.modules):
+ if name in _PRE_HANDOFF_PURGE_PROTECTED or name.startswith("hermes_cli.update_"):
+ continue
+ if name.split(".", 1)[0] in _PRE_HANDOFF_PURGE_PREFIXES:
+ module = sys.modules.pop(name, None)
+ if module is not None:
+ saved[name] = module
+
+ yield _purge
+ for name, module in saved.items():
+ sys.modules.setdefault(name, module)
+
+
def test_drop_stale_root_modules_evicts_utils_missing_file_signature(monkeypatch):
import utils
from hermes_cli.stale_modules import drop_stale_root_modules
@@ -77,18 +102,28 @@ def test_naive_consumer_still_dies_on_stale_utils(monkeypatch):
)
-def test_fresh_config_import_heals_stale_utils_missing_file_signature(monkeypatch):
- """Restart-phase shape: hermes_cli.* purged, root utils stale, config re-imported."""
+def test_fresh_config_import_heals_stale_utils_missing_file_signature(monkeypatch, pre_handoff_purge):
+ """Restart-phase shape: the whole hermes_cli.* graph purged, root utils stale, config
+ re-imported — the exact shape of the post-pull gateway-restart import."""
import utils
monkeypatch.delattr(utils, "file_signature")
- for name in list(sys.modules):
- if name == "hermes_cli.config" or name.startswith("hermes_cli.config."):
- sys.modules.pop(name, None)
- if name == "hermes_cli.stale_modules" or name.startswith("hermes_cli.stale_modules."):
- sys.modules.pop(name, None)
+ pre_handoff_purge()
config = importlib.import_module("hermes_cli.config")
assert hasattr(config, "file_signature")
assert callable(config.file_signature)
assert hasattr(sys.modules["utils"], "file_signature")
+
+
+def test_fresh_managed_scope_import_heals_stale_utils(monkeypatch, pre_handoff_purge):
+ """managed_scope binds ``utils.file_signature`` at import time; a fresh import into a
+ purged graph must heal the root cache itself rather than depend on config running first."""
+ import utils
+
+ monkeypatch.delattr(utils, "file_signature")
+ pre_handoff_purge()
+ assert "hermes_cli.managed_scope" not in sys.modules
+
+ managed_scope = importlib.import_module("hermes_cli.managed_scope")
+ assert callable(managed_scope.file_signature)