Skip to content

The agent that grows with you

refactor(update): evict through the caller's module table

kshitijb1d38c2e5a9d
diff --git a/hermes_cli/update_cmd_maint.py b/hermes_cli/update_cmd_maint.py
index 096d5262c7f..c76fbd68a9e 100644
--- a/hermes_cli/update_cmd_maint.py
+++ b/hermes_cli/update_cmd_maint.py
@@ -108,19 +108,20 @@ def _stale_purge_prefixes() -> frozenset:
return frozenset(names) - {"tests"}
-def _evict_module(name: str) -> bool:
- """Drop *name* from ``sys.modules`` AND the stale attribute its parent package still holds.
-
- Returns True when a ``sys.modules`` entry was removed. The attribute matters: ``from hermes_cli
- import main_dashboard`` is resolved by ``_handle_fromlist``, which is satisfied by the ATTRIBUTE
- the import system left on the parent package — so a purged submodule keeps being handed to
- call-time imports unless the attribute goes too. The parent (``hermes_cli``) is protected and
- survives the purge, which is how a pre-pull ``main_dashboard`` outlived it and crashed the
- dashboard cleanup (#111689).
+def _evict_module(modules: dict, name: str) -> bool:
+ """Returns True when *name* was cached in *modules*; also unbinds the evicted module from its
+ parent package.
+
+ The attribute matters: ``from hermes_cli import main_dashboard`` is resolved by
+ ``_handle_fromlist``, which is satisfied by the ATTRIBUTE the import system left on the parent
+ package — so a purged submodule keeps being handed to call-time imports unless the attribute
+ goes too. The parent (``hermes_cli``) is protected and survives the purge, which is how a
+ pre-pull ``main_dashboard`` outlived it and crashed the dashboard cleanup on a symbol the pull
+ had just added (#112604).
"""
- dropped = sys.modules.pop(name, None)
+ dropped = modules.pop(name, None)
parent_name, _, child = name.rpartition(".")
- parent = sys.modules.get(parent_name)
+ parent = modules.get(parent_name)
# Identity, not name: a same-named module that something else already rebound on the
# package is newer than the one evicted here and must stay. ``vars()`` keeps a lazy
# package ``__getattr__`` (``providers``) from importing during the purge.
@@ -148,7 +149,7 @@ def _purge_stale_hermes_modules() -> None:
and not name.startswith(_STALE_PURGE_PROTECTED_PREFIX)
# Root-package check: startswith() alone also matches unrelated ``gateway_foo``.
and name.split(".", 1)[0] in prefixes
- and _evict_module(name)
+ and _evict_module(modules, name)
]
if purged:
logger.debug("Purged %d stale Hermes module(s) after checkout update", len(purged))
@@ -360,7 +361,7 @@ def _reload_process_scan_modules() -> None:
refreshed here: ``hermes_cli.main`` imports those eagerly at CLI start, so reloading would
rewrite the module dict the running update still holds bindings into. The
``_purge_stale_hermes_modules`` eviction, which runs earlier in the update, is what makes the
- call-time ``from hermes_cli import main_dashboard`` re-read the pulled source (#111689).
+ call-time ``from hermes_cli import main_dashboard`` re-read the pulled source (#112604).
"""
_reload_modules(
("hermes_cli._subprocess_compat", "hermes_cli.dashboard_procs"),