From 9be2862e4ee2bfb601e3891893dcd9b4d35ea664 Mon Sep 17 00:00:00 2001 From: afonsopc Date: Fri, 5 Jun 2026 00:04:15 +0100 Subject: [PATCH] Stub llm_core via monkeypatch.setitem so the cross-tenant test does not leak its fake into later test modules --- tests/test_memory_extractor_vector_cross_tenant.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/test_memory_extractor_vector_cross_tenant.py b/tests/test_memory_extractor_vector_cross_tenant.py index 6b1d243..49702c1 100644 --- a/tests/test_memory_extractor_vector_cross_tenant.py +++ b/tests/test_memory_extractor_vector_cross_tenant.py @@ -32,16 +32,20 @@ def _load_extractor(): return mod -def _install_llm_stub(facts_json): +def _install_llm_stub(monkeypatch, facts_json): mod = types.ModuleType("src.llm_core") async def llm_call_async(*a, **k): return facts_json mod.llm_call_async = llm_call_async + # Use monkeypatch.setitem so sys.modules is restored at teardown. A raw + # assignment here permanently replaced the real src.llm_core with this + # stripped stub, leaking "My home is in Lisbon" (and hiding _detect_provider) + # into every later-collected test that imports the real module. src_pkg = sys.modules.get("src") or types.ModuleType("src") - sys.modules["src"] = src_pkg - sys.modules["src.llm_core"] = mod + monkeypatch.setitem(sys.modules, "src", src_pkg) + monkeypatch.setitem(sys.modules, "src.llm_core", mod) class FakeSession: @@ -95,7 +99,7 @@ def test_vector_match_from_other_tenant_does_not_drop_users_fact(monkeypatch): ]) # The vector store reports user B's new fact as a near-duplicate of a1. vec = FakeVector(match_id="a1") - _install_llm_stub('["My home is in Lisbon"]') + _install_llm_stub(monkeypatch, '["My home is in Lisbon"]') memory_extractor = _load_extractor()