~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""STRICT runtime checks must agree with the factory on saved primaries."""
from copy import deepcopy
from unittest.mock import patch
import pytest
from local_deep_research.security.egress.audit_hook import clear_active_context
from local_deep_research.security.egress.policy import (
EgressScope,
PolicyDeniedError,
)
from local_deep_research.web_search_engines.search_engine_factory import (
create_search_engine,
)
MISSING = object()
INVALID_PRIMARIES = [
pytest.param(MISSING, id="missing"),
pytest.param(None, id="null"),
pytest.param("", id="empty"),
pytest.param(" ", id="spaces"),
pytest.param("\t\n", id="tabs-newline"),
pytest.param(5, id="integer"),
pytest.param(["wikipedia"], id="list"),
pytest.param({"other": "wikipedia"}, id="dictionary"),
]
INVALID_SNAPSHOTS = [
pytest.param({}, id="empty-dictionary"),
pytest.param(None, id="null"),
pytest.param([], id="empty-list"),
pytest.param(5, id="integer"),
pytest.param("invalid-snapshot", id="string"),
]
@pytest.fixture
def wikipedia_engine(monkeypatch):
monkeypatch.delenv("LDR_POLICY_EGRESS_SCOPE", raising=False)
clear_active_context()
engine = create_search_engine(
"wikipedia",
settings_snapshot={
"policy.egress_scope": "strict",
"search.tool": "wikipedia",
"search.engine.web.wikipedia.display_name": "Wikipedia",
},
programmatic_mode=True,
use_full_search=False,
)
assert engine is not None
assert engine._engine_name == "wikipedia"
try:
yield engine
finally:
engine.close()
clear_active_context()
def _set_strict_source(engine, strict_source, monkeypatch):
if strict_source == "operator":
engine.settings_snapshot["policy.egress_scope"] = {"value": "adaptive"}
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", " STRICT ")
else:
engine.settings_snapshot["policy.egress_scope"] = {"value": " strict "}
def _update_primary(engine, primary, wrapped, refresh):
snapshot = (
deepcopy(engine.settings_snapshot)
if refresh
else engine.settings_snapshot
)
if primary is MISSING:
snapshot.pop("search.tool")
elif wrapped:
snapshot["search.tool"]["value"] = primary
else:
snapshot["search.tool"] = primary
engine.settings_snapshot = snapshot
@pytest.mark.parametrize("primary", INVALID_PRIMARIES)
@pytest.mark.parametrize("wrapped", [False, True], ids=["raw", "wrapped"])
@pytest.mark.parametrize(
"refresh", [False, True], ids=["in-place", "refreshed"]
)
@pytest.mark.parametrize("strict_source", ["saved", "operator"])
def test_invalid_strict_primary_stops_search_and_full_content(
wikipedia_engine, monkeypatch, primary, wrapped, refresh, strict_source
):
engine = wikipedia_engine
_set_strict_source(engine, strict_source, monkeypatch)
if wrapped:
engine.settings_snapshot["search.tool"] = {"value": "wikipedia"}
engine._verify_egress_scope() # Warm a legitimate runtime memo.
_update_primary(engine, primary, wrapped, refresh)
with patch.object(engine, "_get_previews", return_value=[]) as provider:
# An invalid policy must not become a successful memoized decision.
for _ in range(2):
with pytest.raises(PolicyDeniedError) as exc_info:
engine.run("synthetic review query")
assert exc_info.value.decision.reason == "invalid_policy_config"
provider.assert_not_called()
with pytest.raises(PolicyDeniedError) as factory_error:
create_search_engine(
"wikipedia",
settings_snapshot=engine.settings_snapshot,
programmatic_mode=True,
use_full_search=False,
)
assert factory_error.value.decision.reason == "invalid_policy_config"
engine.include_full_content = True
assert engine._build_full_search_egress_context() is None
assert not engine.include_full_content
@pytest.mark.parametrize("primary", [" wikipedia ", " arxiv "])
@pytest.mark.parametrize("wrapped", [False, True], ids=["raw", "wrapped"])
@pytest.mark.parametrize(
"refresh", [False, True], ids=["in-place", "refreshed"]
)
@pytest.mark.parametrize("strict_source", ["saved", "operator"])
def test_valid_strict_primary_remains_authoritative(
wikipedia_engine, monkeypatch, primary, wrapped, refresh, strict_source
):
engine = wikipedia_engine
_set_strict_source(engine, strict_source, monkeypatch)
if wrapped:
engine.settings_snapshot["search.tool"] = {"value": "wikipedia"}
engine._verify_egress_scope()
_update_primary(engine, primary, wrapped, refresh)
with patch.object(engine, "_get_previews", return_value=[]) as provider:
if primary.strip() == "wikipedia":
assert engine.run("synthetic review query") == []
provider.assert_called_once()
else:
with pytest.raises(PolicyDeniedError) as exc_info:
engine.run("synthetic review query")
assert exc_info.value.decision.reason == "strict_not_primary"
provider.assert_not_called()
context = engine._build_full_search_egress_context()
assert context.scope == EgressScope.STRICT
assert context.primary_engine == primary.strip()
@pytest.mark.parametrize("scope", ["adaptive", "public_only", "private_only"])
@pytest.mark.parametrize("primary", INVALID_PRIMARIES)
def test_non_strict_engine_fallback_preserves_scope(
wikipedia_engine, scope, primary
):
engine = wikipedia_engine
engine.settings_snapshot["policy.egress_scope"] = scope
_update_primary(engine, primary, wrapped=False, refresh=False)
with patch.object(engine, "_get_previews", return_value=[]) as provider:
if scope == "private_only":
with pytest.raises(PolicyDeniedError) as exc_info:
engine.run("synthetic review query")
assert (
exc_info.value.decision.reason == "scope_mismatch_private_only"
)
provider.assert_not_called()
else:
assert engine.run("synthetic review query") == []
provider.assert_called_once()
context = engine._build_full_search_egress_context()
assert context.primary_engine == "wikipedia"
expected = "public_only" if scope == "adaptive" else scope
assert context.scope.value == expected
@pytest.mark.parametrize("snapshot", INVALID_SNAPSHOTS)
@pytest.mark.parametrize("warm_memo", [False, True], ids=["cold", "warm"])
def test_operator_strict_rejects_missing_or_malformed_snapshot(
wikipedia_engine, monkeypatch, snapshot, warm_memo
):
engine = wikipedia_engine
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", " STRICT ")
if warm_memo:
engine._verify_egress_scope()
engine.settings_snapshot = snapshot
with patch.object(engine, "_get_previews", return_value=[]) as provider:
for _ in range(2):
with pytest.raises(PolicyDeniedError) as exc_info:
engine.run("synthetic review query")
assert exc_info.value.decision.reason == "invalid_policy_config"
provider.assert_not_called()
engine.include_full_content = True
assert engine._build_full_search_egress_context() is None
assert not engine.include_full_content
@pytest.mark.parametrize("snapshot", [None, {}], ids=["missing", "empty"])
def test_missing_snapshot_preserves_programmatic_behavior_without_strict(
wikipedia_engine, snapshot
):
engine = wikipedia_engine
engine.settings_snapshot = snapshot
with patch.object(engine, "_get_previews", return_value=[]) as provider:
assert engine.run("synthetic review query") == []
provider.assert_called_once()
engine.include_full_content = True
assert engine._build_full_search_egress_context() is None
assert engine.include_full_content