Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

8.6 KB
Raw Blame
"""Property-based generalization of ``test_no_plaintext_production_path.py``.

That test proves the core invariant — chunk TEXT lives only in the (per-user,
SQLCipher-encrypted) DB, while the on-disk FAISS index holds only vectors +
int ``DocumentChunk.id``s — for one fixed secret string. Here we fuzz the
chunk TEXT itself with Hypothesis over arbitrary unicode: empty strings,
non-BMP codepoints, NUL/other control characters, and large bodies. For every
generated example we drive the REAL ``VectorIndex`` facade over a REAL
on-disk ``FaissVectorStore`` (a real ``faiss.IndexIDMap2``, real
``faiss.write_index`` bytes on disk) with a deterministic, network-free
``_FakeEmbeddings``, then recursively grep every file under the index
directory (the persisted ``.faiss`` plus any transient temp-file bytes) for
the exact text. It must appear NOWHERE on disk.

The text is embedded behind a distinctive per-test marker (rather than
grepped for on its own) so the check stays sound even for tiny/degenerate
examples (e.g. a single space or digit) that would otherwise be near-certain
to occur *by coincidence* inside arbitrary binary index bytes.
"""

import shutil
import tempfile
import threading
import uuid
from contextlib import contextmanager
from pathlib import Path
from unittest import mock

import numpy as np
from hypothesis import HealthCheck, given, settings
from hypothesis import strategies as st
from langchain_core.embeddings import Embeddings
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
    DocumentChunk,
    EmbeddingProvider,
)
from local_deep_research.vector_stores.facade import ChunkInput, VectorIndex

# Distinctive marker prefix. Random-looking and long enough (30+ chars) that
# it cannot plausibly occur by chance inside the binary bytes of a faiss
# index file, so its absence is a meaningful signal rather than luck.
MARKER = "ZZZ_PROPERTY_ON_DISK_MARKER_7c19be4a"


class _FakeEmbeddings(Embeddings):
    """Deterministic, network-free embeddings. Vectors are numeric, so the
    text marker can never survive into them as raw bytes -- the grep stays
    sound regardless of what text is drawn."""

    def __init__(self, dim: int = 8):
        self.dim = dim

    def _vec(self, text: str):
        rng = np.random.default_rng(abs(hash(text)) % (2**32))
        return rng.random(self.dim).tolist()

    def embed_documents(self, texts):
        return [self._vec(t) for t in texts]

    def embed_query(self, text):
        return self._vec(text)


def _grep_all(root: Path, needle: bytes):
    """Every on-disk file under ``root`` that contains ``needle`` (raw bytes)."""
    return [
        str(f)
        for f in root.rglob("*")
        if f.is_file() and needle in f.read_bytes()
    ]


def _noop(_p):
    return None


def _ok(_p):
    return (True, None)


# ---------------------------------------------------------------------------
# Text strategies: unicode incl. non-BMP, control/NUL characters, empty, and
# large bodies. Plain ``st.text()`` already draws from the full codepoint
# range (hypothesis excludes lone surrogates so ``str.encode()`` never
# raises), but we bias the mix toward the specific edges the migration must
# survive so a modest example budget reliably hits them.
_control_chars = st.characters(min_codepoint=0x00, max_codepoint=0x1F)
_non_bmp_chars = st.characters(min_codepoint=0x10000, max_codepoint=0x10FFFF)

_arbitrary_chunk_text = st.one_of(
    st.just(""),
    st.text(min_size=0, max_size=50),
    st.text(alphabet=_control_chars, min_size=1, max_size=20),
    st.text(alphabet=_non_bmp_chars, min_size=1, max_size=10),
    st.text(min_size=2000, max_size=4000),
)


@contextmanager
def _isolated_store(text_for_chunk: str):
    """Build a throwaway (tmp dir + in-memory DB + VectorIndex) sandbox for
    one Hypothesis example and index a single chunk containing
    ``text_for_chunk``. Yields ``(tmp_root, session, source_id, combined)``.

    Fully self-contained per call (own tempdir/engine/session) rather than
    reusing pytest's ``tmp_path``/``mocker`` fixtures, since those are
    function-scoped and Hypothesis re-invokes the test body many times per
    test function call -- sharing them across examples would let state and
    on-disk files pile up between examples.
    """
    tmp_root = Path(tempfile.mkdtemp(prefix="ldr_prop_no_plaintext_"))
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    try:

        @contextmanager
        def _mock_session(*_a, **_k):
            yield session

        with mock.patch(
            "local_deep_research.vector_stores.facade.get_user_db_session",
            _mock_session,
        ):
            vi = VectorIndex(
                username="testuser",
                db_password="pw",
                embeddings=_FakeEmbeddings(dim=8),
                embedding_model="fake-model",
                embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
                collection_name="collection_prop",
                dimension=8,
                path=tmp_root / "collection_prop.faiss",
                lock=threading.Lock(),
                integrity_record=_noop,
                integrity_verify=_ok,
                index_type="flat",
                metric="cosine",
                normalize=True,
            )

            # Embed the drawn text behind the distinctive marker so an exact
            # on-disk match is never a coincidence, even for trivial/short
            # drawn text (empty string, a single space, "0", ...).
            combined = f"{MARKER}::{text_for_chunk}::{MARKER}"
            source_id = f"doc-{uuid.uuid4().hex}"

            stats = vi.index(
                source_type="document",
                source_id=source_id,
                chunks=[ChunkInput(text=combined, metadata={"chunk_index": 0})],
            )
            assert stats.added == 1

            yield tmp_root, session, source_id, combined
    finally:
        session.close()
        engine.dispose()
        shutil.rmtree(tmp_root, ignore_errors=True)


@settings(
    max_examples=40, deadline=None, suppress_health_check=[HealthCheck.too_slow]
)
@given(text=_arbitrary_chunk_text)
def test_arbitrary_chunk_text_never_leaks_to_disk(text):
    with _isolated_store(text) as (tmp_root, session, source_id, combined):
        # The .faiss file was actually written.
        faiss_file = tmp_root / "collection_prop.faiss"
        assert faiss_file.is_file()
        assert faiss_file.stat().st_size > 0

        # CORE INVARIANT: neither the raw marker nor the full marker-wrapped
        # chunk text (which contains the arbitrary drawn text verbatim)
        # appears anywhere on disk under the index directory -- this also
        # sweeps up any transient temp-file bytes left behind by a persist().
        leaked_marker = _grep_all(tmp_root, MARKER.encode())
        assert leaked_marker == [], f"marker leaked to disk: {leaked_marker}"

        leaked_combined = _grep_all(tmp_root, combined.encode())
        assert leaked_combined == [], (
            f"chunk text leaked to disk: {leaked_combined}"
        )

        # No companion docstore/sidecar file of any kind -- the store is
        # pure faiss binary (write_index/read_index), no pickle/json/idmap.
        assert list(tmp_root.rglob("*.pkl")) == []
        assert list(tmp_root.rglob("*.json")) == []
        assert list(tmp_root.rglob("*.tmp")) == []

        # Proof the text really WAS indexed (not silently dropped/truncated):
        # its only copy lives in the DocumentChunk row (the DB -- encrypted
        # in production).
        rows = session.query(DocumentChunk).filter_by(source_id=source_id).all()
        assert len(rows) == 1
        assert rows[0].chunk_text == combined


def test_grep_helper_actually_bites():
    """Sanity check that ``_grep_all`` isn't vacuously passing: plant the
    marker in a file under a directory and confirm it IS detected. Without
    this, a bug that made the main property test's grep silently a no-op
    (e.g. an empty root, a typo'd needle) would let every example pass for
    the wrong reason.
    """
    tmp_root = Path(tempfile.mkdtemp(prefix="ldr_prop_grep_check_"))
    try:
        planted = tmp_root / "planted.bin"
        planted.write_bytes(f"prefix {MARKER} suffix".encode())

        hits = _grep_all(tmp_root, MARKER.encode())
        assert hits == [str(planted)]

        # And a needle that truly isn't present is correctly reported absent.
        assert _grep_all(tmp_root, b"NOT_PRESENT_AT_ALL_xyz") == []
    finally:
        shutil.rmtree(tmp_root, ignore_errors=True)