diff --git a/code_puppy/agents/_runtime.py b/code_puppy/agents/_runtime.py index 0d5e2b281..70a7dc563 100644 --- a/code_puppy/agents/_runtime.py +++ b/code_puppy/agents/_runtime.py @@ -409,8 +409,8 @@ async def run_agent_task() -> Any: except* Exception as other: unexpected = _collect_exceptions( other, - lambda e: not isinstance( - e, (asyncio.CancelledError, UsageLimitExceeded) + lambda e: ( + not isinstance(e, (asyncio.CancelledError, UsageLimitExceeded)) ), ) for exc in unexpected: diff --git a/tests/plugins/conftest.py b/tests/plugins/conftest.py index 8cb5e6c08..34c48d5e1 100644 --- a/tests/plugins/conftest.py +++ b/tests/plugins/conftest.py @@ -9,11 +9,23 @@ def pytest_configure(config): """Configure pytest with compatibility workarounds. - Pre-patch sys.modules to provide a mock ``mcp.types`` during collection. - This prevents a ValueError in pydantic's RootModel metaclass when MCP - is not installed in the test environment. + Pre-patch sys.modules to provide a mock ``mcp.types`` during collection, + but only when the real ``mcp`` package is not importable. Previously we + checked ``"mcp" not in sys.modules`` which is true on a clean start — + that installed MagicMocks for ``mcp.client`` etc. and then, when other + tests (e.g. ``tests/agents/test_compaction.py``) transitively imported + ``pydantic_ai.mcp``, its ``from mcp.client.sse import sse_client`` blew + up with "'mcp.client' is not a package" because the MagicMock had no + ``__path__``. + + Real ``mcp`` is installed in this project's dev environment, so the + mock path is only needed in bare CI images. Probe with a real import + first; only fall back to mocks if that fails. """ - if "mcp" not in sys.modules: + try: + import mcp # noqa: F401 + import mcp.types # noqa: F401 + except ImportError: mcp_mock = MagicMock() mcp_mock.types = MagicMock() sys.modules["mcp"] = mcp_mock