Spaces:
Sleeping
Fuzz retriever query handling (fix missing input guards)
Browse filesAdds tests/test_retriever_fuzz.py covering MathLibRetriever.retrieve and
__init__ across degenerate inputs (empty, whitespace, 5000-char, special
chars, Lean unicode, None, non-string) and index-dir failure modes
(missing, garbage). Heavy deps (HuggingFaceEmbeddings, FAISS,
HuggingFaceCrossEncoder) are mocked before the module is imported so the
suite is hermetic.
Bug found and fixed:
- retrieve(None) (and any non-string) was silently passed to the LangChain
retriever where it surfaced as an opaque error far from the call site.
Now raises TypeError up front.
- Empty/whitespace queries fell through to the embedding model which
returns noisy near-neighbours; they now short-circuit to [].
- Unbounded query length could pin the embedder; capped at 2000 chars.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Files changed (2)
- src/retriever.py+24
- tests/test_retriever_fuzz.pyAdded+230
59 unmodified lines6061626364654 unmodified lines7071727374757659 unmodified linesself._retriever = self._build_retriever(faiss_store, docs)def retrieve(self, query: str, k: Optional[int] = None) -> List[Document]:"""Retrieve and rerank the most relevant Mathlib lemmas for a query.4 unmodified linesReturns:List of Documents ranked by relevance."""if self._retriever is None:self._load()results = self._retriever.invoke(query)59 unmodified lines60616263646566676869704 unmodified lines7576777879808182838485868788899091929394959697989910059 unmodified linesself._retriever = self._build_retriever(faiss_store, docs)# Hard cap on query length passed to downstream retrievers. Long queries# waste embedding work and can blow past tokenizer limits; truncating here# gives callers a predictable upper bound._MAX_QUERY_CHARS = 2000def retrieve(self, query: str, k: Optional[int] = None) -> List[Document]:"""Retrieve and rerank the most relevant Mathlib lemmas for a query.4 unmodified linesReturns:List of Documents ranked by relevance.Raises:TypeError: If `query` is not a string."""if not isinstance(query, str):raise TypeError(f"query must be a str, got {type(query).__name__}")# Empty/whitespace-only queries are valid input but degenerate; bail# out early with no results rather than asking the embedding model to# vectorise an empty string (which produces noisy nearest neighbours).if not query.strip():return []# Truncate absurdly long inputs so a runaway caller can't pin the# embedding model.if len(query) > self._MAX_QUERY_CHARS:query = query[: self._MAX_QUERY_CHARS]if self._retriever is None:self._load()results = self._retriever.invoke(query)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230"""Fuzz tests for `MathLibRetriever.retrieve()` and `MathLibRetriever.__init__`.
Heavy/expensive deps (HuggingFaceEmbeddings, FAISS, HuggingFaceCrossEncoder) aremocked BEFORE `retriever` is imported so the module never touches the realmodels or disk-resident indices."""
import osimport sysimport tempfileimport unittestfrom unittest import mock
# Make `src/` importable.sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))# Spec requires literal `sys.path.insert(0, 'src')` too.sys.path.insert(0, "src")
# ---------------------------------------------------------------------------# Patch heavy dependencies BEFORE importing retriever.# ---------------------------------------------------------------------------
from langchain_core.documents import Document # safe import (lightweight)
# Fake documents the mocked retriever pipeline returns._FAKE_DOCS = [ Document(page_content=f"lemma_{i}", metadata={"name": f"lemma_{i}"}) for i in range(5)]
class _FakeEmbeddings: def __init__(self, *a, **kw): pass
def embed_documents(self, texts): return [[0.0] * 8 for _ in texts]
def embed_query(self, text): return [0.0] * 8
class _FakeDocstore: def __init__(self, docs): self._dict = {str(i): d for i, d in enumerate(docs)}
class _FakeFAISS: """Mock for `langchain_community.vectorstores.FAISS`."""
def __init__(self, docs=None): self.docstore = _FakeDocstore(docs or _FAKE_DOCS)
@classmethod def from_documents(cls, docs, embeddings): # noqa: ARG003 return cls(docs)
@classmethod def load_local(cls, path, embeddings, allow_dangerous_deserialization=False): # noqa: ARG003 # Mimic real FAISS: choke on garbage files. index_path = os.path.join(path, "index.faiss") if os.path.exists(index_path): with open(index_path, "rb") as fh: head = fh.read(64) # Real FAISS index has a magic header; garbage will fail to parse. if b"garbage" in head or len(head) < 8: raise RuntimeError(f"FAISS index at {path} is corrupt or unreadable.") return cls(_FAKE_DOCS)
def save_local(self, path): os.makedirs(path, exist_ok=True) with open(os.path.join(path, "index.faiss"), "wb") as fh: fh.write(b"\x00" * 128)
def as_retriever(self, search_kwargs=None): # noqa: ARG002 return _FakeBaseRetriever()
class _FakeBaseRetriever: def invoke(self, query): # noqa: ARG002 return list(_FAKE_DOCS)
class _FakeCrossEncoder: def __init__(self, *a, **kw): pass
def score(self, pairs): return [0.5 for _ in pairs]
# Build the module-level mocks. These need to be patched in *both* the# original module *and* the `retriever` module namespace, since `retriever.py`# does `from X import Y` (binds Y locally)._patchers = [ mock.patch("langchain_huggingface.HuggingFaceEmbeddings", _FakeEmbeddings), mock.patch("langchain_community.vectorstores.FAISS", _FakeFAISS), mock.patch( "langchain_community.cross_encoders.HuggingFaceCrossEncoder", _FakeCrossEncoder, ),]for p in _patchers: p.start()
# Now safe to import the module under test.import retriever # noqa: E402
# Re-bind the locally imported names inside `retriever` to our fakes, because# `from … import …` already captured the originals at import time.retriever.HuggingFaceEmbeddings = _FakeEmbeddingsretriever.FAISS = _FakeFAISSretriever.HuggingFaceCrossEncoder = _FakeCrossEncoder
# ---------------------------------------------------------------------------# Helper: build a retriever with `_retriever` pre-populated, sidestepping the# real BM25/EnsembleRetriever/ContextualCompressionRetriever pipeline that we# don't need for fuzzing `retrieve()`'s query handling.# ---------------------------------------------------------------------------
def _make_retriever_with_fake_pipeline(index_dir=None): r = retriever.MathLibRetriever(index_dir=index_dir) r._retriever = _FakeBaseRetriever() return r
# ---------------------------------------------------------------------------# Tests# ---------------------------------------------------------------------------
class RetrieveQueryFuzzTests(unittest.TestCase): """Fuzz the `retrieve(query)` method."""
def setUp(self): self.r = _make_retriever_with_fake_pipeline()
def test_empty_string_does_not_crash(self): results = self.r.retrieve("") self.assertIsInstance(results, list)
def test_whitespace_only_does_not_crash(self): results = self.r.retrieve(" \n ") self.assertIsInstance(results, list)
def test_long_query_does_not_crash(self): q = "foo " * 1250 # 5000 chars self.assertEqual(len(q), 5000) results = self.r.retrieve(q) self.assertIsInstance(results, list)
def test_only_special_chars_does_not_crash(self): q = "!@#$%^&*()_+-=[]{}|;:',.<>?/`" results = self.r.retrieve(q) self.assertIsInstance(results, list)
def test_lean_unicode_does_not_crash(self): q = "∀ n : ℕ, ∃ m, n + m = m + n" # ∀ n : ℕ, ∃ m, n + m = m + n results = self.r.retrieve(q) self.assertIsInstance(results, list)
def test_none_query_raises_typeerror(self): """None as a query must raise TypeError, not return garbage.""" with self.assertRaises(TypeError): self.r.retrieve(None)
def test_non_string_int_raises_typeerror(self): with self.assertRaises(TypeError): self.r.retrieve(12345) # type: ignore[arg-type]
def test_respects_k_argument(self): results = self.r.retrieve("anything", k=2) self.assertLessEqual(len(results), 2)
def test_default_k_truncates_to_rerank_top_k(self): # _FAKE_DOCS has 5 entries, default rerank_top_k=5 so we expect all 5 results = self.r.retrieve("anything") self.assertLessEqual(len(results), self.r.rerank_top_k)
class RetrieveLazyLoadTests(unittest.TestCase): """`retrieve()` should lazily call `_load()` when no retriever exists."""
def test_retrieve_without_built_index_raises_runtime_error(self): with tempfile.TemporaryDirectory() as tmp: r = retriever.MathLibRetriever(index_dir=tmp) with self.assertRaises(RuntimeError): r.retrieve("query")
class InitFuzzTests(unittest.TestCase): """Fuzz `MathLibRetriever.__init__` and related path handling."""
def test_index_dir_none_uses_default(self): r = retriever.MathLibRetriever(index_dir=None) # The default lives at <repo>/data/mathlib_index. self.assertTrue(str(r.index_dir).endswith(os.path.join("data", "mathlib_index")))
def test_index_dir_nonexistent_path_is_index_built_false(self): r = retriever.MathLibRetriever(index_dir="/nonexistent/path/definitely-not-here") self.assertFalse(r.is_index_built())
def test_index_dir_garbage_raises_clear_error_on_load(self): with tempfile.TemporaryDirectory() as tmp: # Drop a "looks like an index but is garbage" file. garbage = os.path.join(tmp, "index.faiss") with open(garbage, "wb") as fh: fh.write(b"garbage" * 4)
r = retriever.MathLibRetriever(index_dir=tmp) self.assertTrue(r.is_index_built()) # file exists, so passes the cheap check with self.assertRaises(Exception) as ctx: r._load() # Must surface a meaningful error, not silently succeed. msg = str(ctx.exception).lower() self.assertTrue( any(k in msg for k in ("faiss", "index", "corrupt", "unread", "load")), f"Error message should mention the index/FAISS, got: {ctx.exception!r}", )
def test_empty_index_dir_is_index_built_false(self): with tempfile.TemporaryDirectory() as tmp: r = retriever.MathLibRetriever(index_dir=tmp) self.assertFalse(r.is_index_built())
if __name__ == "__main__": unittest.main()