Skip to content

The agent that grows with you

test(update): one stale-main_dashboard helper; fixture restores bindings only

kshitij16bf9a1c494d
diff --git a/tests/hermes_cli/test_update_stale_module_purge.py b/tests/hermes_cli/test_update_stale_module_purge.py
index 9051dba147e..80bad47415d 100644
--- a/tests/hermes_cli/test_update_stale_module_purge.py
+++ b/tests/hermes_cli/test_update_stale_module_purge.py
@@ -41,18 +41,19 @@ def _restore_sys_modules():
package (see `_evict_module`), which `sys.modules` alone does not cover: after the test,
`hermes_cli.X` could hold a freshly imported copy while the cache holds the original, and a
later `patch("hermes_cli.X.fn")` would miss the module the code under test resolves. Restore
- the checkout-owned package namespaces too.
+ the submodule bindings of the checkout-owned packages too — only those, so any other package
+ global a test leaks still shows up as pollution.
"""
from hermes_cli.update_cmd_maint import _stale_purge_prefixes
snapshot = dict(sys.modules)
owned = _stale_purge_prefixes()
- namespaces = {
- name: (mod, dict(vars(mod)))
+ bindings = {
+ mod: {k: v for k, v in vars(mod).items() if isinstance(v, types.ModuleType)}
for name, mod in snapshot.items()
if mod is not None
and name.split(".", 1)[0] in owned
- and getattr(mod, "__path__", None) is not None # packages only: they carry submodules
+ and "__path__" in vars(mod) # packages only: they carry submodules
}
yield
for name, mod in snapshot.items():
@@ -60,11 +61,9 @@ def _restore_sys_modules():
for name in list(sys.modules):
if name not in snapshot:
del sys.modules[name]
- for name, (mod, attrs) in namespaces.items():
- if sys.modules.get(name) is not mod:
- continue
+ for mod, attrs in bindings.items():
current = vars(mod)
- for key in [key for key in current if key not in attrs]:
+ for key in [k for k, v in current.items() if isinstance(v, types.ModuleType) and k not in attrs]:
del current[key]
current.update(attrs)
@@ -75,6 +74,18 @@ def _fake_module(name: str) -> types.ModuleType:
return mod
+def _install_stale_main_dashboard(**attrs) -> types.ModuleType:
+ """A pre-pull ``main_dashboard`` stand-in, bound the way ``hermes_cli.main``'s eager import
+ leaves it: in ``sys.modules`` AND as an attribute of the protected ``hermes_cli`` package."""
+ import hermes_cli
+
+ stale = _fake_module("hermes_cli.main_dashboard")
+ vars(stale).update(attrs)
+ sys.modules["hermes_cli.main_dashboard"] = stale
+ hermes_cli.main_dashboard = stale
+ return stale
+
+
def test_purge_evicts_hermes_prefixed_modules():
victims = [
"hermes_cli.cli_output",
@@ -239,70 +250,44 @@ def test_purge_protects_hermes_logging():
def test_purge_drops_stale_package_attribute_so_from_import_rereads_source():
- """2026-09-16 field failure (#111689): `hermes_cli.main` imports `main_dashboard` at CLI
- start, so the updater process holds it as an ATTRIBUTE of the (protected) `hermes_cli`
- package. Evicting only the sys.modules entry left `from hermes_cli import main_dashboard`
- handing the PRE-pull module to the dashboard cleanup, which then died on a symbol the pull
- had added (`AttributeError ... has no attribute '_loaded_launchd_backend_jobs'`).
+ """Field failure #112604: `hermes_cli.main` imports `main_dashboard` at CLI start, so the
+ updater process holds it as an ATTRIBUTE of the (protected) `hermes_cli` package. Evicting
+ only the sys.modules entry left `from hermes_cli import main_dashboard` handing the PRE-pull
+ module to the dashboard cleanup, which then died on a symbol the pull had added
+ (`AttributeError ... has no attribute '_loaded_launchd_backend_jobs'`).
"""
- import hermes_cli
+ stale = _install_stale_main_dashboard()
- name = "hermes_cli.main_dashboard"
- real = sys.modules.get(name)
- stale = types.ModuleType(name) # pre-pull stand-in: lacks the symbol the cleanup needs
- stale.__file__ = getattr(real, "__file__", "<stale>")
- stale.__stale_sentinel__ = True
- sys.modules[name] = stale
- setattr(hermes_cli, "main_dashboard", stale)
- try:
- cli_main._purge_stale_hermes_modules()
+ cli_main._purge_stale_hermes_modules()
- assert not hasattr(stale, "_loaded_launchd_backend_jobs")
- from hermes_cli import main_dashboard as pulled
+ assert not hasattr(stale, "_loaded_launchd_backend_jobs")
+ from hermes_cli import main_dashboard as pulled
- assert pulled is not stale, "call-time import was handed the pre-pull module"
- assert getattr(pulled, "__stale_sentinel__", False) is False
- assert hasattr(pulled, "_loaded_launchd_backend_jobs")
- finally:
- sys.modules.pop(name, None)
- if real is not None:
- sys.modules[name] = real
- setattr(hermes_cli, "main_dashboard", real)
+ assert pulled is not stale, "call-time import was handed the pre-pull module"
+ assert getattr(pulled, "__stale_sentinel__", False) is False
+ assert hasattr(pulled, "_loaded_launchd_backend_jobs")
def test_dashboard_cleanup_survives_a_pre_pull_main_dashboard():
- """End-to-end shape of the 2026-09-16 field failure: the post-update dashboard cleanup runs
- after the purge, and `_kill_stale_dashboard_processes` resolves its helpers then. With the
- pre-pull `main_dashboard` still reachable, the cleanup aborted on
+ """End-to-end shape of #112604: the post-update dashboard cleanup runs after the purge, and
+ `_kill_stale_dashboard_processes` resolves its helpers then. With the pre-pull
+ `main_dashboard` still reachable, the cleanup aborted on
`AttributeError: module 'hermes_cli.main_dashboard' has no attribute
'_loaded_launchd_backend_jobs'` — after the code update had already succeeded.
"""
- import hermes_cli
- from hermes_cli import dashboard_procs
-
- name = "hermes_cli.main_dashboard"
- real = sys.modules.get(name)
- stale = types.ModuleType(name)
- stale.__file__ = getattr(real, "__file__", "<stale>")
- stale.__stale_sentinel__ = True
# The pre-pull module DOES scan processes; it only lacks the launchd symbol the pull added,
- # so the cleanup reaches the launchd snapshot line and dies there (the reported traceback).
- stale._find_stale_dashboard_pids = lambda **_kw: [999999]
- sys.modules[name] = stale
- setattr(hermes_cli, "main_dashboard", stale)
- try:
- cli_main._purge_stale_hermes_modules()
+ # so a cleanup handed this module reaches the launchd snapshot line and dies there.
+ _install_stale_main_dashboard(_find_stale_dashboard_pids=lambda **_kw: [999999])
- # The scan/kill pass must not touch real processes: the fresh module's pid scan is host
- # dependent (a dashboard may or may not be running), so only the kill step is stubbed.
- with patch.object(dashboard_procs, "_kill_pids_posix", side_effect=lambda pids, k, f: None):
- result = dashboard_procs._kill_stale_dashboard_processes("regression")
+ cli_main._purge_stale_hermes_modules()
- assert isinstance(result, dict), "cleanup aborted instead of returning its result"
- assert hasattr(sys.modules["hermes_cli.main_dashboard"], "_loaded_launchd_backend_jobs"), \
- "cleanup resolved the pre-pull module"
- finally:
- sys.modules.pop(name, None)
- if real is not None:
- sys.modules[name] = real
- setattr(hermes_cli, "main_dashboard", real)
+ # The pulled scanner reads the host's process table through `dashboard_procs`; the stale
+ # stand-in never does. Stubbing the table keeps the test off real processes AND records
+ # which module the cleanup resolved.
+ from hermes_cli import dashboard_procs
+ table_reads = []
+ with patch.object(dashboard_procs, "_iter_process_table", lambda: table_reads.append(1) or []):
+ result = dashboard_procs._kill_stale_dashboard_processes("regression")
+
+ assert isinstance(result, dict), "cleanup aborted instead of returning its result"
+ assert table_reads, "cleanup was handed the pre-pull module instead of the pulled one"