fix(compat-fallout): sys.modules-wiping agent tests restore the original module objects afterwards (their wipe of hermes_*/tools.* detached sibling files' monkeypatches now that facades no longer re-import the defining modules)

This commit is contained in:
Teknium
2026-09-03 16:11:41 -07:00
parent 830b82bd03
commit 1bc3af0c05
2 changed files with 18 additions and 0 deletions
@@ -136,6 +136,7 @@ def agent_env():
# Import fresh so the patched conversation_loop is exercised even when the
# module was imported earlier in the same worker.
saved_modules = dict(sys.modules) # restored in finally — see below
for mod in list(sys.modules):
if mod == "run_agent" or mod.startswith("agent.") or mod.startswith("tools.") or mod.startswith("hermes_"):
del sys.modules[mod]
@@ -155,6 +156,11 @@ def agent_env():
finally:
srv.shutdown()
shutil.rmtree(test_home, ignore_errors=True)
# Put the original module objects back: sibling test files hold module-level
# references into hermes_cli.*/tools.* and their monkeypatches would otherwise
# land on modules the app no longer imports.
sys.modules.clear()
sys.modules.update(saved_modules)
if prev_home is None:
os.environ.pop("HERMES_HOME", None)
else:
@@ -19,6 +19,18 @@ from unittest.mock import MagicMock
import pytest
@pytest.fixture(autouse=True)
def _restore_sys_modules():
"""``_fresh_run_agent`` wipes the agent stack out of ``sys.modules``. Put the original
module objects back afterwards: sibling test files hold module-level references into
``hermes_cli.*`` / ``tools.*`` and their monkeypatches would otherwise land on modules
the app no longer imports."""
saved = dict(sys.modules)
yield
sys.modules.clear()
sys.modules.update(saved)
def _fresh_run_agent(hermes_home):
for mod in list(sys.modules):
if mod == "run_agent" or mod.startswith("agent.") or mod.startswith("tools.") or mod.startswith("hermes_"):