Skip to content

The agent that grows with you

fix(tests): restore original module identities after vision-routing reloads (#61597)

Teknium67435c9a020c
diff --git a/tests/agent/test_vision_routing_31179.py b/tests/agent/test_vision_routing_31179.py
index 935c6d6b048..f510bbe6abe 100644
--- a/tests/agent/test_vision_routing_31179.py
+++ b/tests/agent/test_vision_routing_31179.py
@@ -60,15 +60,38 @@ def _write_config(home: str, text: str) -> None:
fp.write(text)
-def _fresh_modules():
- """Drop cached hermes modules so each test reloads against current env."""
+_RELOAD_PREFIXES = ("agent.auxiliary_client", "agent.image_routing",
+ "tools.vision_tools", "tools.browser_tool",
+ "hermes_cli.config")
+
+
+def _drop_reload_targets():
for mod in list(sys.modules.keys()):
- if mod.startswith(("agent.auxiliary_client", "agent.image_routing",
- "tools.vision_tools", "tools.browser_tool",
- "hermes_cli.config")):
+ if mod.startswith(_RELOAD_PREFIXES):
del sys.modules[mod]
[email protected](autouse=True)
+def _module_isolation():
+ """Save/restore sys.modules entries this file reloads.
+
+ Without this, reloaded copies of agent.image_routing & friends leak
+ into sys.modules after the test, splitting module identity for any
+ later test that patches ``agent.image_routing.*`` while holding
+ function refs from the original module (issue #61597).
+ """
+ saved = {name: mod for name, mod in sys.modules.items()
+ if name.startswith(_RELOAD_PREFIXES)}
+ yield
+ _drop_reload_targets()
+ sys.modules.update(saved)
+
+
+def _fresh_modules():
+ """Drop cached hermes modules so each test reloads against current env."""
+ _drop_reload_targets()
+
+
# ---------------------------------------------------------------------------
# Fix 1: provider=openai → custom + api.openai.com/v1
# ---------------------------------------------------------------------------