From 00305aec615d239b9e124e88893fdb733fc8d6d1 Mon Sep 17 00:00:00 2001 From: alt-glitch Date: Sun, 23 Aug 2026 14:04:17 +0530 Subject: [PATCH] test(tool-search): kill the shared-stemmer mutant with cache-missing input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The parallel determinism test warms _stem's lru_cache after ~11 distinct stems, so almost no iterations reach the underlying stemmer and a shared (non-thread-local) instance survives it. New test bypasses the cache with per-iteration unique tokens via _stem.__wrapped__, so thousands of stems run concurrently: a shared stemmer's mutable parse state fails it within 2,000 calls (verified — the mutant dies 8/8 runs; healthy runs stay green). --- tests/tools/test_tool_search_multiquery.py | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/tools/test_tool_search_multiquery.py b/tests/tools/test_tool_search_multiquery.py index 68769e309c..a6ffef8787 100644 --- a/tests/tools/test_tool_search_multiquery.py +++ b/tests/tools/test_tool_search_multiquery.py @@ -163,6 +163,39 @@ class TestStemming: assert dispatched == [expected_json] * 64 + def test_stemmer_is_safe_under_concurrent_cache_misses(self): + """Hammer the raw stemmer from 8 threads with cache-missing input. + + ``_stem``'s lru_cache means a small corpus warms after a handful of + misses and later iterations never reach the stemmer, so a shared + (non-thread-local) stemmer instance can survive a threaded test over + repeated tokens. This test bypasses the cache: every call stems a + unique token via ``_stem.__wrapped__``, so thousands of stems execute + concurrently on the underlying per-thread instances. A shared + stemmer's mutable parse state produces wrong stems or raises here. + """ + from tools.tool_search import _stem + + words = ["issues", "creating", "meetings", "categories", "searching"] + + def serial_baseline(salt): + return [ + _stem.__wrapped__(f"{word}x{salt}n{i}") + for i, word in enumerate(words) + ] + + expected = {salt: serial_baseline(salt) for salt in range(400)} + + def worker(salt): + return salt, [ + _stem.__wrapped__(f"{word}x{salt}n{i}") + for i, word in enumerate(words) + ] + + with ThreadPoolExecutor(max_workers=8) as pool: + for salt, stems in pool.map(worker, range(400)): + assert stems == expected[salt] + # --------------------------------------------------------------------------- # Exact-name ranking and shared corpus statistics