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.

2.8 KB
Raw Blame
"""Tests for the MCP direct-search egress audit net.

Direct MCP searches call ``engine.run()`` without going through
``AdvancedSearchSystem`` (which arms the audit-hook net itself), so
``_execute_search`` arms the net via ``_egress_audit_net``.
"""

import pytest

from local_deep_research.security.egress.audit_hook import (
    active_egress_context,
    get_active_context,
)
from local_deep_research.security.egress.policy import (
    EgressScope,
    context_from_snapshot,
)

from tests.mcp_sdk import is_mcp_installed

MCP_AVAILABLE = is_mcp_installed()

pytestmark = pytest.mark.skipif(
    not MCP_AVAILABLE, reason="MCP package not installed"
)

if MCP_AVAILABLE:
    from local_deep_research.mcp.server import _egress_audit_net


def test_arms_context_for_private_only():
    settings = {
        "policy.egress_scope": {"value": "private_only"},
        "search.tool": {"value": "library"},
        "_username": "tester",
    }
    assert get_active_context() is None
    with _egress_audit_net(settings):
        ctx = get_active_context()
        assert ctx is not None
        assert ctx.scope == EgressScope.PRIVATE_ONLY
        assert ctx.username == "tester"
    assert get_active_context() is None


def test_adaptive_padded_private_primary_arms_private_context():
    settings = {
        "policy.egress_scope": "adaptive",
        "search.tool": " library ",
        "_username": "tester",
    }
    with _egress_audit_net(settings):
        context = get_active_context()
        assert context is not None
        assert context.scope == EgressScope.PRIVATE_ONLY
        assert context.primary_engine == "library"
    assert get_active_context() is None


def test_blank_primary_does_not_arm_an_invented_public_context():
    with _egress_audit_net(
        {"policy.egress_scope": "adaptive", "search.tool": "   "}
    ):
        assert get_active_context() is None


def test_empty_settings_is_noop():
    with _egress_audit_net({}):
        assert get_active_context() is None


def test_already_armed_context_is_kept():
    """When a caller already armed a context (web research worker), the
    MCP net must not replace it."""
    outer = context_from_snapshot(
        {"policy.egress_scope": {"value": "both"}}, "searxng"
    )
    with active_egress_context(outer):
        with _egress_audit_net(
            {
                "policy.egress_scope": {"value": "private_only"},
                "search.tool": {"value": "library"},
            }
        ):
            assert get_active_context() is outer


def test_unevaluable_policy_is_noop():
    """A corrupted scope must not break MCP search — the net simply stays
    unarmed (the factory PEP remains the primary enforcement)."""
    settings = {"policy.egress_scope": {"value": "garbage_scope"}}
    with _egress_audit_net(settings):
        assert get_active_context() is None