Skip to content

The agent that grows with you

fix(update): protect the whole hermes_cli.update_* family from the stale-module purge

Tekniume17dd8b4281d
diff --git a/hermes_cli/update_cmd_maint.py b/hermes_cli/update_cmd_maint.py
index 3e23f39d467..a2579f28a9f 100644
--- a/hermes_cli/update_cmd_maint.py
+++ b/hermes_cli/update_cmd_maint.py
@@ -31,19 +31,14 @@ _STALE_PURGE_PREFIXES = "hermes_cli", "gateway", "tools", "tui_gateway", "agent"
#: Modules EXECUTING the update survive the purge: evicting them buys nothing (running frames
#: keep them alive) and reloading them mid-flight is the one genuinely unsafe move.
-_STALE_PURGE_PROTECTED = frozenset(
- {
- "hermes_cli",
- "hermes_cli.main",
- "hermes_cli.update_cmd",
- "hermes_cli.update_receipt",
- "hermes_cli.hermes_logging",
- }
- # The updater's own split modules are executing too.
- | {f"hermes_cli.update_cmd_{c}" for c in (
- "config", "deps", "fleet", "git", "maint", "stash", "windows", "zip",
- )}
-)
+_STALE_PURGE_PROTECTED = frozenset({"hermes_cli", "hermes_cli.main", "hermes_cli.hermes_logging"})
+
+#: The updater's own module family (``update_cmd*``, ``update_receipt``, ``update_inventory``,
+#: ``update_lock``, ...) is protected as a prefix: these hold per-run state — the open receipt
+#: singleton, the pre-update plan's ``RuntimeRecord`` class identity, the lock — and evicting
+#: one swaps in a fresh module whose ``_current`` is None (receipt silently never written) or
+#: whose dataclass fails every ``isinstance`` against the plan built before the purge.
+_STALE_PURGE_PROTECTED_PREFIX = "hermes_cli.update_"
_PRE_UPDATE_SNAPSHOT_KEEP = 1
@@ -90,6 +85,7 @@ def _purge_stale_hermes_modules() -> None:
purged = [
name for name in list(modules)
if name not in _STALE_PURGE_PROTECTED
+ and not name.startswith(_STALE_PURGE_PROTECTED_PREFIX)
# Root-package check: startswith() alone also matches unrelated ``gateway_foo``.
and name.split(".", 1)[0] in _STALE_PURGE_PREFIXES
and modules.pop(name, None) is not None
diff --git a/tests/hermes_cli/test_update_stale_module_purge.py b/tests/hermes_cli/test_update_stale_module_purge.py
index 11b4f102c84..8084c0db08e 100644
--- a/tests/hermes_cli/test_update_stale_module_purge.py
+++ b/tests/hermes_cli/test_update_stale_module_purge.py
@@ -161,3 +161,14 @@ def test_stale_symbol_scenario_end_to_end():
sys.modules.pop(name, None)
if real is not None:
sys.modules[name] = real
+
+
+def test_purge_keeps_plan_record_class_identity():
+ # The pre-update plan is built BEFORE the purge; reconciliation after it filters with
+ # ``isinstance(r, RuntimeRecord)``. An evicted ``update_inventory`` yields a fresh class,
+ # every record fails the check, and the plan-vs-execution report goes silently empty.
+ from hermes_cli.update_inventory import RuntimeRecord as before
+
+ cli_main._purge_stale_hermes_modules()
+ from hermes_cli.update_inventory import RuntimeRecord as after
+ assert after is before