PraisonAI icon indicating copy to clipboard operation
PraisonAI copied to clipboard

tests/conftest.py: the #5117 availability guard resets one of the two memos on the probe path -- FrameworkAdapterRegistry._avail_cache still carries a stubbed answer into later tests

Open pm25coder opened this issue 2 months ago • 2 comments

Context

#5118 wired _framework_availability.invalidate() into a per-test autouse fixture (src/praisonai/tests/conftest.py:61-78). That closes the memo #5117 reported. There is a second memo on the same code path that the fixture never touches, so the leak class it was written to close is still open one layer up.

Two memos, one guard

memo owner written by dropped by
probe memo _cache praisonai_code/_framework_availability.py:7 is_available() :116 invalidate() :135 — called by the new fixture
registry memo _avail_cache praisonai/framework_adapters/registry.py:63 is_available() :251 invalidate_availability() :254 — not called by the new fixture

get_default_registry() resolves to PluginRegistry.default() (praisonai_code/_registry.py:342, _default_instance at :350), so _avail_cache lives for the whole pytest session — the same lifetime as the probe memo, just not on the same object.

It is a one-hop chain

registry.is_available(name) → create(name) (:238) → adapter.is_available() (:243) → _framework_availability.is_available(name) (framework_adapters/crewai_adapter.py, same shape in the autogen/agno/adk/pydantic adapters). A stubbed importlib.util.find_spec therefore lands in both memos, and the fixture evicts only one of them: the second memo keeps a copy of the answer the patch produced.

Repro (staged lab package, CPython 3.13.9 / pytest 9.1.1, one fresh process per arm)

Harness: praisonai_code/_framework_availability.py and the fixture copied verbatim from the merge commit; an adapter registered under the name crewai whose is_available() returns _framework_availability.is_available("crewai") (the shipped adapter shape); crewai not installed, so the real answer is False. Two tests — one probe under patch.object(importlib.util, "find_spec", return_value=object()), then one that asserts the real answer with no patch in scope:

arm harness merged fixture registry reset result
A chain above (global find_spec stub + delegating adapter) yes no fail (assert True is False)
B chain above yes yes 2 passed, real answer False
C probe seam patched (patch.object(registry, "create", ...), the seam test_framework_validators.py uses) yes no fail, same value
D probe seam patched no yes fail on the probe memo — the leaked value stays in _cache
E probe seam patched yes yes 4 passed

Raw output for arm A:

[chain] under stub -> True ; registry memo={'crewai': True}
[chain] real answer later -> True ; registry memo={'crewai': True} ; probe memo={}
E   AssertionError: the registry memo still reports the stubbed answer
E   assert True is False

The second test sees an empty probe memo (the fixture did its job) and still gets the stubbed answer back from the registry copy.

Suggested fix

Extend the same fixture, symmetrically around the yield:

try:
    from praisonai.framework_adapters.registry import get_default_registry
except ImportError:
    pass
else:
    get_default_registry().invalidate_availability()

Both calls are cheap — dict.clear() under a lock (registry.py:264-265), same shape as the invalidate() the fixture already calls — and the import sits behind the same wrapper gate as the existing sibling import. Resetting at the source instead (dropping the memo) is also possible, but the fixture route is what #5118 chose for the first memo.

What I could not reproduce

I did not find a test in src/praisonai/tests that currently triggers this ordering. The two places that drive the registry memo today reset it by hand — tests/unit/test_framework_validators.py:175 and :190, the first carrying a comment that names the hazard ("cannot leak an order-dependent crewai: False into later tests"). So this is latent, not something I observed in CI. It is worth closing centrally for the same reason #5117 was: the next test that stubs the probe and reaches the registry through an adapter gets an answer that outlives its patch, and the new fixture's docstring ("a stubbed answer never outlives the patch that produced it") states that this cannot happen.

One more memo of the same shape, for completeness — not exercised in the arms above: BaseCLIIntegration._availability_cache (praisonai/integrations/base.py:73), written from shutil.which at :115, dropped by invalidate_availability (:119), and cleared by hand in tests/unit/integrations/test_wrapper_gaps_fixes.py. If the intent is "no process-global availability memo survives a test", that one belongs in the fixture too, or behind a shared helper the fixture calls.

Environment / scope: staged lab package (the candidate module plus its wrapper shim and the delegating adapter), not a full checkout; the module and fixture contents are the ones at the merge commit; the base.py detail comes from a source read of main (4e5b83fa), not from a run.

pm25coder avatar Sep 17 '26 13: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-5133-20260917-1313 • [Create PR ➔](https://github.com/MervinPraison/PraisonAI/compare/main...claude/issue-5133-20260917-1313?quick_pull=1&title=Issue%20%235133%3A%20Changes%20from%20Claude&body=This%20PR%20addresses%20issue%20%235133%0A%0AGenerated%20with%20%5BClaude%20Code%5D(https%3A%2F%2Fclaude.ai%2Fcode)


Task: Fix conftest.py availability guard (issue #5133)

  • [x] Read AGENTS.md guidelines / verify scope (wrapper test fixture — no core surface change)
  • [x] Read conftest.py fixture and registry code
  • [x] Extend fixture to invalidate registry memo (_avail_cache)
  • [x] Verify both invalidation paths import & run
  • [x] Commit, push, create PR

Summary

The autouse _reset_framework_availability_cache fixture only invalidated the probe memo in praisonai_code._framework_availability. The registry memo FrameworkAdapterRegistry._avail_cache — fed from the same probe via adapter.is_available() — was never reset, so a stubbed find_spec answer could outlive its patch (the arm A/C failure in the report).

Fix: extend the same fixture symmetrically around the yield to also call get_default_registry().invalidate_availability(), behind the identical ImportError-guarded import pattern the fixture already uses. Both calls are cheap dict.clear() under a lock — no new surface area, backward compatible.

Kept minimal per the "lightweight and powerful" guidance: I did not fold in the optional third memo (BaseCLIIntegration._availability_cache) since it is already reset by hand in its own test and closing it would broaden scope beyond the reported leak.

Note on testing: test_framework_validators.py skips locally because the praisonaiagents optional deps aren't installed in this environment (pre-existing). I verified conftest.py parses/collects cleanly and that both invalidation methods import and execute against the checkout.

PR: https://github.com/MervinPraison/PraisonAI/pull/5134 • Branch: claude/issue-5133-20260917-1313