Commit Graph

2 Commits

Author SHA1 Message Date
nesquena-hermes 9655504350 test(mcp_server): restore module identity + fix sys.modules.patch.dict pollution
Root cause: tests/test_mcp_server.py and tests/test_issue1857_usage_overwrite.py
both leaked module state into the full pytest suite, causing 20+ failures in
unrelated test files when they ran together.

Two distinct bugs:

1. test_issue1857_usage_overwrite.py used mock.patch.dict(sys.modules, {...}).
   patch.dict tracks original keys at __enter__ and DELETES any keys added
   during the patch on __exit__. That silently evicted lazily-imported
   pydantic submodules (e.g. pydantic.root_model), producing
   KeyError: 'pydantic.root_model' in test_mcp_server.py downstream.
   Fix: manual save/restore of only the three keys we explicitly inject.

2. test_mcp_server.py mutated module-level constants on api.config / api.models /
   mcp_server (STATE_DIR, SESSION_DIR, PROJECTS_FILE, …) without restoring,
   leaving downstream tests reading deleted tmpdirs. Fix: snapshot original
   values on first _reimport_mcp() call and restore in _cleanup_state_dir.

   Additionally, test_profiles_match_single_source_of_truth re-imported
   api.routes / api.profiles into sys.modules and only restored sys.modules,
   not the parent api package's attributes. `import api.routes as r` resolves
   via sys.modules['api'].routes (parent attribute), NOT directly via
   sys.modules['api.routes']. So fresh modules leaked through despite the
   sys.modules restore. Fix: also restore parent-package attributes.

Result: full pytest suite goes from 20 failures + 36 errors back to all green
(4947 passed, 8 skipped). Up from 4898 in v0.51.27, gain of 49 from
PR #1895 (MCP server tests) + #1866 (goal handler tests).
2026-05-08 19:58:21 +00:00
Frank Song c1a9d7ce79 fix: overwrite session usage per turn 2026-05-08 15:37:09 +00:00