PraisonAI icon indicating copy to clipboard operation
PraisonAI copied to clipboard

test isolation: praisonai_code._framework_availability._cache is module-global and nothing invalidates it (a stubbed find_spec leaks into later test files)

Open pm25coder opened this issue 2 months ago • 2 comments

Summary

praisonai_code/_framework_availability.py keeps a module-global availability memo (_cache: dict[str, bool] = {}, L7) plus an invalidate() helper (L129-134). Nothing in the repository calls that helper - a code search for framework_availability.invalidate returns 0 hits, tests/conftest.py and both tests/_pytest_plugins/* modules contain no reference to the module, and the three test modules that import it contain no invalidate call. An availability answer computed while a test has stubbed the probe is therefore memoised for the rest of the pytest process.

This is the same class of problem as #3560 (Tool availability checks cache transient probe failures), and it is the sibling of the cache that #5116 fixed: there the object is FrameworkAdapterRegistry._avail_cache (per-registry, dropped by registry.invalidate_availability()); here it is a process-global dict that no test-facing hook reaches.

Reproduction (stock main, pytest 9.1.1) - two files in one process; the first stubs find_spec, the second asks the real question:

# test_a_probe.py
from unittest.mock import patch
from praisonai._framework_availability import is_available


def test_probe_inside_a_find_spec_stub():
    with patch("importlib.util.find_spec", return_value=object()):
        assert is_available("langgraph") is True
# test_b_real.py
import importlib.util
from praisonai._framework_availability import is_available


def test_later_file_sees_the_real_answer():
    real = importlib.util.find_spec("langgraph") is not None
    assert is_available("langgraph") is real
$ python -m pytest test_a_probe.py test_b_real.py -q
1 failed, 1 passed
E   AssertionError: memoised answer survived the patch: is_available=True but the real answer is False

$ python -m pytest test_b_real.py -q          # control: fresh process
1 passed

langgraph is not installed in that environment; the only reason the second file sees True is the memo written while the patch was active.

Verified details (at main = 3eb82885846de064ee0c4cc923e6edf7cb72a938)

  • src/praisonai-code/praisonai_code/_framework_availability.py - L7 (_cache), L89-114 (_PROBES, 22 packages including "crewai"), L116-127 (is_available, memo write at L126), L129-134 (invalidate).
  • src/praisonai/praisonai/_framework_availability.py assigns sys.modules[__name__] to that module, so both import paths share one global cache.
  • It is on live production paths, not only tests: praisonai/auto.py L158 imports it and uses it at L667/L692/L704/L1024; praisonai/framework_adapters/crewai_adapter.py L33-36 answers is_available() with is_available("crewai").
  • FrameworkAdapterRegistry.invalidate_availability() does not reach it - verified by leaving {"langgraph": True} in the module cache and calling the registry hook: the cache is unchanged.

Suggested minimal fix

Wire the module's own reset into the suite's per-test hygiene, next to the existing autouse=True fixtures in src/praisonai/tests/conftest.py (~L61/L157/L219):

@pytest.fixture(autouse=True)
def _reset_framework_availability_cache():
    from praisonai_code import _framework_availability

    _framework_availability.invalidate()
    yield
    _framework_availability.invalidate()

A session-scoped call would only help the first test; per-test is what keeps a stubbed answer from outliving the patch that produced it. Mentioning invalidate() in the is_available() docstring as the test escape hatch would also make it discoverable - the _avail_cache sibling documents exactly that.

Scope note

I could not find a test in the repository that fails today because of this, so this is latent rather than an active flake. It becomes active as soon as a test stubs importlib.util.find_spec / importlib.metadata.distribution and then calls is_available(...) for any of the 22 probed packages. The stub pattern itself is already used in the suite (tests/unit/mcp/test_mcp_bridge.py, tests/unit/mcp/conftest.py, tests/unit/test_c7_1_boundaries.py, tests/unit/test_langextract_sink.py, tests/unit/cli/test_main_dispatcher.py reference find_spec), and tests/unit/test_auto_lazy_loading.py calls is_available("litellm") / is_available("openai") directly.

pm25coder avatar Sep 16 '26 16:09 pm25coder

👋 Thanks for opening this issue, @pm25coder!

A maintainer will review this shortly. In the meantime:

Bug and enhancement reports are auto-triaged with Claude. Questions and other issues: a maintainer can comment @claude to trigger analysis.

Routing: Agent-callable tools → PraisonAI-Tools; lifecycle plugins (tracing/hooks/guardrails) → PraisonAI-Plugins. TypeScript/JavaScript SDK fixes land in src/praisonai-ts/ in this repo.

Claude finished @pm25coder's task —— View job • claude/issue-5117-20260916-1635 • [Create PR ➔](https://github.com/MervinPraison/PraisonAI/compare/main...claude/issue-5117-20260916-1635?quick_pull=1&title=Issue%20%235117%3A%20Changes%20from%20Claude&body=This%20PR%20addresses%20issue%20%235117%0A%0AGenerated%20with%20%5BClaude%20Code%5D(https%3A%2F%2Fclaude.ai%2Fcode)


Task: Fix test isolation for _framework_availability._cache

  • [x] Read guidelines & verify _framework_availability.py (module-global _cache + invalidate())
  • [x] Locate src/praisonai/tests/conftest.py autouse fixtures
  • [x] Implement minimal fix (autouse reset fixture + is_available() docstring hint)
  • [x] Verify reproduction & fix
  • [x] Commit, push, create PR

Summary

The module-global memo in praisonai_code/_framework_availability.py (_cache, L7) was never invalidated between tests, so an answer computed while a test stubs find_spec/distribution leaked into later test files.

Fix (minimal, lightweight — no new params/modules/exports):

  • Added an autouse=True fixture _reset_framework_availability_cache in src/praisonai/tests/conftest.py that calls the module's existing invalidate() before and after each test (per-test, guarded import).
  • Documented invalidate() as the test escape hatch in the is_available() docstring.

Verification — reproduced the issue's two-file scenario:

  • Without reset fixture: 1 failed, 1 passed (memoised True leaked past the patch).
  • With reset fixture: 2 passed.

PR: https://github.com/MervinPraison/PraisonAI/pull/5118


Job run • Branch