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.

19.9 KB
Raw Blame
"""Interrupted curated caches must not silently change embedding behavior."""

import json
from unittest.mock import patch

import pytest

from local_deep_research.embeddings.providers.implementations.sentence_transformers import (
    SentenceTransformersProvider as Provider,
)
from local_deep_research.embeddings.sentence_transformer_models import (
    DEFAULT_SENTENCE_TRANSFORMER_MODEL,
    get_sentence_transformer_model_spec,
)
from local_deep_research.security.egress.policy import PolicyDeniedError


LEGACY_MODELS = [
    "all-MiniLM-L6-v2",
    "sentence-transformers/all-MiniLM-L6-v2",
    "all-mpnet-base-v2",
    "multi-qa-MiniLM-L6-cos-v1",
    "paraphrase-multilingual-MiniLM-L12-v2",
]

# Repositories whose tokenizer vocabulary is published only as tokenizer.json.
TOKENIZER_JSON_ONLY_MODELS = {
    DEFAULT_SENTENCE_TRANSFORMER_MODEL,
    "paraphrase-multilingual-MiniLM-L12-v2",
}

POLICIES = [
    pytest.param(None, False, id="snapshotless"),
    pytest.param(
        {"policy.egress_scope": "strict", "search.tool": "arxiv"},
        False,
        id="strict",
    ),
    pytest.param(
        {"policy.egress_scope": "private_only", "search.tool": "library"},
        False,
        id="private",
    ),
    pytest.param(
        {
            "policy.egress_scope": "public_only",
            "search.tool": "arxiv",
            "embeddings.require_local": True,
        },
        False,
        id="public-local-only",
    ),
    pytest.param(
        {"policy.egress_scope": "public_only", "search.tool": "arxiv"},
        True,
        id="public",
    ),
]


def _cached_snapshot(tmp_path, monkeypatch, model, missing_file=None):
    """Use the real Hub cache lookup with metadata files and refs/main.

    ``missing_file`` is one file name, or a tuple of names, left out of the
    snapshot. The tokenizer vocabulary mirrors each repository's layout:
    the legacy WordPiece/MPNet models publish ``tokenizer.json`` and
    ``vocab.txt``; GTE ModernBERT and multilingual MiniLM publish only
    ``tokenizer.json`` of the two.
    """
    missing = (
        set(missing_file) if isinstance(missing_file, tuple) else {missing_file}
    )
    from huggingface_hub import constants

    spec = get_sentence_transformer_model_spec(model)
    revision = spec.revision or "a" * 40
    monkeypatch.delenv("SENTENCE_TRANSFORMERS_HOME", raising=False)
    monkeypatch.setattr(constants, "HF_HUB_CACHE", str(tmp_path))
    repository = tmp_path / ("models--" + spec.repository.replace("/", "--"))
    snapshot = repository / "snapshots" / revision
    (repository / "refs").mkdir(parents=True)
    (repository / "refs" / "main").write_text(revision)
    inferred_limit = (
        8192 if model == DEFAULT_SENTENCE_TRANSFORMER_MODEL else 512
    )
    metadata = {
        "config.json": {"max_position_embeddings": inferred_limit},
        "modules.json": [
            {
                "idx": 0,
                "path": "",
                "type": "sentence_transformers.models.Transformer",
            },
            {
                "idx": 1,
                "path": "1_Pooling",
                "type": "sentence_transformers.models.Pooling",
            },
        ],
        "1_Pooling/config.json": {"word_embedding_dimension": spec.dimensions},
        "tokenizer_config.json": {"model_max_length": inferred_limit},
    }
    # The pinned GTE artifact does not publish a Transformer module config.
    if model != DEFAULT_SENTENCE_TRANSFORMER_MODEL:
        metadata["sentence_bert_config.json"] = {
            "max_seq_length": spec.max_seq_length,
            "do_lower_case": False,
        }
    for filename, contents in metadata.items():
        if filename not in missing:
            path = snapshot / filename
            path.parent.mkdir(parents=True, exist_ok=True)
            path.write_text(json.dumps(contents))
    vocabulary = ["tokenizer.json"]
    if model not in TOKENIZER_JSON_ONLY_MODELS:
        vocabulary.append("vocab.txt")
    for filename in vocabulary:
        if filename not in missing:
            (snapshot / filename).write_text("{}")
    return snapshot, revision


@pytest.mark.parametrize("model", LEGACY_MODELS)
@pytest.mark.parametrize("missing_file", [None, "sentence_bert_config.json"])
@pytest.mark.parametrize("settings_snapshot,download_allowed", POLICIES)
def test_transformer_metadata_admission_preserves_policy_and_revision(
    model,
    missing_file,
    settings_snapshot,
    download_allowed,
    tmp_path,
    monkeypatch,
):
    _, revision = _cached_snapshot(tmp_path, monkeypatch, model, missing_file)
    with patch(
        "langchain_community.embeddings.SentenceTransformerEmbeddings"
    ) as constructor:
        if missing_file and not download_allowed:
            with pytest.raises(PolicyDeniedError) as error:
                Provider.create_embeddings(
                    model=model, settings_snapshot=settings_snapshot
                )
            assert error.value.decision.reason == "embeddings_model_not_cached"
            constructor.assert_not_called()
            return
        Provider.create_embeddings(
            model=model, settings_snapshot=settings_snapshot
        )

    constructor.assert_called_once()
    kwargs = constructor.call_args.kwargs["model_kwargs"]
    assert kwargs.get("local_files_only", False) is (missing_file is None)
    assert kwargs["revision"] == revision
    assert kwargs["token"] is False
    assert kwargs["trust_remote_code"] is False
    assert kwargs["model_kwargs"]["use_safetensors"] is True


@pytest.mark.parametrize("missing_file", [None, "tokenizer_config.json"])
@pytest.mark.parametrize(
    "model", [DEFAULT_SENTENCE_TRANSFORMER_MODEL, *LEGACY_MODELS]
)
@pytest.mark.parametrize("download_allowed", [False, True])
def test_tokenizer_metadata_is_required_for_curated_loads(
    model, missing_file, download_allowed, tmp_path, monkeypatch
):
    _, revision = _cached_snapshot(tmp_path, monkeypatch, model, missing_file)
    settings_snapshot = {
        "policy.egress_scope": "public_only"
        if download_allowed
        else "private_only",
        "search.tool": "arxiv",
    }
    with patch(
        "langchain_community.embeddings.SentenceTransformerEmbeddings"
    ) as constructor:
        if missing_file and not download_allowed:
            with pytest.raises(PolicyDeniedError) as error:
                Provider.create_embeddings(
                    model=model, settings_snapshot=settings_snapshot
                )
            assert error.value.decision.reason == "embeddings_model_not_cached"
            constructor.assert_not_called()
            return
        Provider.create_embeddings(
            model=model, settings_snapshot=settings_snapshot
        )

    constructor.assert_called_once()
    kwargs = constructor.call_args.kwargs["model_kwargs"]
    assert kwargs.get("local_files_only", False) is (missing_file is None)
    assert kwargs["revision"] == revision
    assert kwargs["token"] is False
    assert kwargs["trust_remote_code"] is False
    assert kwargs["model_kwargs"]["use_safetensors"] is True


@pytest.mark.parametrize(
    ("model", "missing", "admitted"),
    [
        pytest.param(
            DEFAULT_SENTENCE_TRANSFORMER_MODEL,
            ("tokenizer.json",),
            False,
            id="gte-no-tokenizer-json",
        ),
        pytest.param(
            "paraphrase-multilingual-MiniLM-L12-v2",
            ("tokenizer.json",),
            False,
            id="multilingual-no-tokenizer-json",
        ),
        pytest.param(
            "all-MiniLM-L6-v2",
            ("tokenizer.json", "vocab.txt"),
            False,
            id="legacy-no-vocabulary",
        ),
        pytest.param(
            "sentence-transformers/all-mpnet-base-v2",
            ("tokenizer.json", "vocab.txt"),
            False,
            id="legacy-alias-no-vocabulary",
        ),
        pytest.param(
            "all-MiniLM-L6-v2",
            ("tokenizer.json",),
            True,
            id="legacy-vocab-txt-only",
        ),
        pytest.param(
            "multi-qa-MiniLM-L6-cos-v1",
            ("vocab.txt",),
            True,
            id="legacy-tokenizer-json-only",
        ),
    ],
)
@pytest.mark.parametrize("download_allowed", [False, True])
def test_tokenizer_vocabulary_is_required_for_offline_admission(
    model, missing, admitted, download_allowed, tmp_path, monkeypatch
):
    """A cache without any tokenizer vocabulary must never load offline.

    The offline constructor does not raise for it: it builds a placeholder
    tokenizer from the special tokens alone, so every text embeds as the same
    few ids and the missing-weight repair never runs. Either WordPiece file
    alone rebuilds the real tokenizer, so a legacy cache holding just one of
    them is still admitted.
    """
    _, revision = _cached_snapshot(tmp_path, monkeypatch, model, missing)
    settings_snapshot = {
        "policy.egress_scope": "public_only"
        if download_allowed
        else "private_only",
        "search.tool": "arxiv",
    }
    with patch(
        "langchain_community.embeddings.SentenceTransformerEmbeddings"
    ) as constructor:
        if not admitted and not download_allowed:
            with pytest.raises(PolicyDeniedError) as error:
                Provider.create_embeddings(
                    model=model, settings_snapshot=settings_snapshot
                )
            assert error.value.decision.reason == "embeddings_model_not_cached"
            constructor.assert_not_called()
            return
        Provider.create_embeddings(
            model=model, settings_snapshot=settings_snapshot
        )

    constructor.assert_called_once()
    kwargs = constructor.call_args.kwargs["model_kwargs"]
    # Not admitted: the authorized download stays on the cached revision.
    assert kwargs.get("local_files_only", False) is admitted
    assert kwargs["revision"] == revision
    assert kwargs["token"] is False
    assert kwargs["trust_remote_code"] is False
    assert kwargs["model_kwargs"]["use_safetensors"] is True


def test_actual_transformer_loader_accepts_missing_token_limit_metadata(
    tmp_path, monkeypatch
):
    """The missing-weight OSError retry cannot detect this silent fallback."""
    from sentence_transformers.models import Transformer

    snapshot, _ = _cached_snapshot(tmp_path, monkeypatch, "all-MiniLM-L6-v2")
    complete = Transformer.load_config(
        str(snapshot), local_files_only=True, token=False
    )
    assert complete["max_seq_length"] == 256

    (snapshot / "sentence_bert_config.json").unlink()
    incomplete = Transformer.load_config(
        str(snapshot), local_files_only=True, token=False
    )
    assert "max_seq_length" not in incomplete


def test_actual_tokenizer_loader_changes_special_tokens_without_metadata(
    tmp_path,
):
    """Multilingual MiniLM explicitly selects a generic fast tokenizer over BERT.

    Its model config says BERT while its tokenizer config selects the generic
    fast tokenizer with XLM-R-style special tokens. A tiny local vocabulary
    demonstrates the same silent fallback without downloading model weights.
    """
    from tokenizers import Tokenizer
    from tokenizers.models import WordPiece
    from transformers import AutoTokenizer

    vocabulary = {
        "<s>": 0,
        "<pad>": 1,
        "</s>": 2,
        "<unk>": 3,
        "<mask>": 4,
        "hello": 5,
    }
    tokenizer = Tokenizer(WordPiece(vocab=vocabulary, unk_token="<unk>"))
    tokenizer.save(str(tmp_path / "tokenizer.json"))
    (tmp_path / "config.json").write_text(json.dumps({"model_type": "bert"}))
    tokenizer_config = tmp_path / "tokenizer_config.json"
    tokenizer_config.write_text(
        json.dumps(
            {
                "tokenizer_class": "PreTrainedTokenizerFast",
                "cls_token": "<s>",
                "sep_token": "</s>",
                "unk_token": "<unk>",
                "pad_token": "<pad>",
                "mask_token": "<mask>",
                "model_max_length": 512,
            }
        )
    )

    load_kwargs = {
        "local_files_only": True,
        "token": False,
        "trust_remote_code": False,
    }
    complete = AutoTokenizer.from_pretrained(str(tmp_path), **load_kwargs)
    assert complete.cls_token == "<s>"
    assert complete.cls_token_id == 0

    tokenizer_config.unlink()
    incomplete = AutoTokenizer.from_pretrained(str(tmp_path), **load_kwargs)
    assert incomplete.cls_token == "[CLS]"
    assert incomplete.cls_token_id != complete.cls_token_id


@pytest.mark.parametrize("reference", ["valid", "missing", "malformed"])
@pytest.mark.parametrize("download_allowed", [False, True])
def test_missing_revision_anchors_preserve_existing_cache_identity(
    reference, download_allowed, tmp_path, monkeypatch
):
    """Missing config/modules must never turn cache repair into a fresh model.

    The remaining cache files belong to revision A. Floating upstream main
    could point at different model bytes, invalidating existing index vectors.
    """
    model = "all-MiniLM-L6-v2"
    snapshot, revision = _cached_snapshot(tmp_path, monkeypatch, model)
    (snapshot / "config.json").unlink()
    (snapshot / "modules.json").unlink()
    ref = snapshot.parent.parent / "refs" / "main"
    if reference == "missing":
        ref.unlink()
    elif reference == "malformed":
        ref.write_text("unresolved-main")
    settings = {
        "policy.egress_scope": "public_only"
        if download_allowed
        else "private_only",
        "search.tool": "arxiv",
    }
    with patch(
        "langchain_community.embeddings.SentenceTransformerEmbeddings"
    ) as constructor:
        if not download_allowed or reference != "valid":
            with pytest.raises(PolicyDeniedError) as error:
                Provider.create_embeddings(
                    model=model, settings_snapshot=settings
                )
            expected = (
                "embeddings_cache_revision_unknown"
                if download_allowed
                else "embeddings_model_not_cached"
            )
            assert error.value.decision.reason == expected
            constructor.assert_not_called()
            return
        Provider.create_embeddings(model=model, settings_snapshot=settings)
    constructor.assert_called_once()
    kwargs = constructor.call_args.kwargs["model_kwargs"]
    assert kwargs["revision"] == revision
    assert "local_files_only" not in kwargs
    assert kwargs["token"] is False
    assert kwargs["trust_remote_code"] is False
    assert kwargs["model_kwargs"]["use_safetensors"] is True


@pytest.mark.parametrize("cache_state", ["absent", "empty", "unresolved"])
def test_only_fresh_cache_allows_a_floating_first_install(
    cache_state, tmp_path, monkeypatch
):
    """Unknown existing state must not inherit the first-install permission."""
    from huggingface_hub import constants

    monkeypatch.delenv("SENTENCE_TRANSFORMERS_HOME", raising=False)
    monkeypatch.setattr(constants, "HF_HUB_CACHE", str(tmp_path))
    repository = "sentence-transformers/all-MiniLM-L6-v2"
    cache = tmp_path / ("models--" + repository.replace("/", "--"))
    if cache_state != "absent":
        cache.mkdir()
    if cache_state == "unresolved":
        (cache / "snapshots").mkdir()
    with patch(
        "langchain_community.embeddings.SentenceTransformerEmbeddings"
    ) as constructor:
        settings = {
            "policy.egress_scope": "public_only",
            "search.tool": "arxiv",
        }
        if cache_state == "unresolved":
            with pytest.raises(PolicyDeniedError) as error:
                Provider.create_embeddings(
                    model="all-MiniLM-L6-v2", settings_snapshot=settings
                )
            assert (
                error.value.decision.reason
                == "embeddings_cache_revision_unknown"
            )
            constructor.assert_not_called()
            return
        Provider.create_embeddings(
            model="all-MiniLM-L6-v2", settings_snapshot=settings
        )
    constructor.assert_called_once()
    kwargs = constructor.call_args.kwargs["model_kwargs"]
    assert "revision" not in kwargs
    assert "local_files_only" not in kwargs
    assert kwargs["model_kwargs"]["use_safetensors"] is True


@pytest.mark.parametrize("download_allowed", [False, True])
@pytest.mark.parametrize(
    "custom_state", ["complete", "partial", "unresolved", "absent"]
)
def test_custom_cache_controls_revision_admission_and_loading(
    download_allowed, custom_state, tmp_path, monkeypatch
):
    """A default-cache revision must never replace the active custom revision."""
    from huggingface_hub import constants

    model = "all-MiniLM-L6-v2"
    default_root = tmp_path / "default"
    custom_root = tmp_path / "custom"
    _cached_snapshot(default_root, monkeypatch, model)
    custom_snapshot, _ = _cached_snapshot(custom_root, monkeypatch, model)
    custom_revision = "b" * 40
    renamed = custom_snapshot.with_name(custom_revision)
    custom_snapshot.rename(renamed)
    reference = renamed.parent.parent / "refs/main"
    reference.write_text(custom_revision)
    if custom_state in ("partial", "unresolved"):
        (renamed / "config.json").unlink()
        (renamed / "modules.json").unlink()
    if custom_state == "unresolved":
        reference.unlink()
    if custom_state == "absent":
        custom_root = tmp_path / "absent"
    monkeypatch.setattr(constants, "HF_HUB_CACHE", str(default_root))
    monkeypatch.setenv("SENTENCE_TRANSFORMERS_HOME", str(custom_root))
    settings = {
        "policy.egress_scope": "public_only"
        if download_allowed
        else "private_only",
        "search.tool": "arxiv",
    }
    with patch(
        "langchain_community.embeddings.SentenceTransformerEmbeddings"
    ) as constructor:
        if custom_state != "complete" and (
            not download_allowed or custom_state == "unresolved"
        ):
            with pytest.raises(PolicyDeniedError) as error:
                Provider.create_embeddings(
                    model=model, settings_snapshot=settings
                )
            assert error.value.decision.reason == (
                "embeddings_cache_revision_unknown"
                if download_allowed
                else "embeddings_model_not_cached"
            )
            constructor.assert_not_called()
            return
        Provider.create_embeddings(model=model, settings_snapshot=settings)
    constructor.assert_called_once()
    assert constructor.call_args.kwargs["cache_folder"] == str(custom_root)
    kwargs = constructor.call_args.kwargs["model_kwargs"]
    if custom_state == "absent":
        assert "revision" not in kwargs
    else:
        assert kwargs["revision"] == custom_revision
    assert kwargs.get("local_files_only", False) is (custom_state == "complete")
    assert kwargs["token"] is False
    assert kwargs["trust_remote_code"] is False


def test_custom_cache_missing_weights_repair_keeps_directory_and_revision(
    tmp_path, monkeypatch
):
    """An offline miss may repair only the same custom-cache snapshot."""
    from huggingface_hub import constants

    root = tmp_path / "custom"
    _, revision = _cached_snapshot(root, monkeypatch, "all-MiniLM-L6-v2")
    monkeypatch.setenv("SENTENCE_TRANSFORMERS_HOME", str(root))
    monkeypatch.setattr(constants, "HF_HUB_CACHE", str(tmp_path / "unused"))
    with patch(
        "langchain_community.embeddings.SentenceTransformerEmbeddings",
        side_effect=[OSError("missing weights"), object()],
    ) as constructor:
        Provider.create_embeddings(
            model="all-MiniLM-L6-v2",
            settings_snapshot={
                "policy.egress_scope": "public_only",
                "search.tool": "arxiv",
            },
        )
    assert constructor.call_count == 2
    first, second = [call.kwargs for call in constructor.call_args_list]
    assert first["cache_folder"] == second["cache_folder"] == str(root)
    assert (
        first["model_kwargs"]["revision"]
        == second["model_kwargs"]["revision"]
        == revision
    )
    assert first["model_kwargs"]["local_files_only"] is True
    assert "local_files_only" not in second["model_kwargs"]