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.

7.2 KB
Raw Blame
"""The egress denial-guidance helper must turn machine reason codes into clear,
actionable user messages (what was blocked + how to allow it)."""

from __future__ import annotations

import pytest

from local_deep_research.security.egress.guidance import denial_guidance


@pytest.mark.parametrize(
    "reason,target,must_contain",
    [
        # Scope blocks name the scope AND the setting to change.
        # The retired `both` scope was replaced by `adaptive` (migration 0019),
        # so the guidance must NOT tell users to switch to "Both".
        # When ``primary_engine`` is supplied and equals ``target``, Adaptive
        # is reliable and gets mentioned. When the primary differs (or is
        # absent), only the explicit compatible scope is named.
        (
            "scope_mismatch_private_only",
            "arxiv",
            ["Private only", "Egress Scope", "Adaptive"],
        ),
        (
            "scope_mismatch_public_only",
            "library",
            ["Public only", "Egress Scope", "Adaptive"],
        ),
        ("strict_not_primary", "wikipedia", ["Strict", "primary"]),
        # Inference blocks name the require-local toggle.
        ("provider_cloud_only", "openai", ["Require local LLM", "openai"]),
        ("provider_cloud", "openai", ["Require local embeddings"]),
        ("provider_remote", "ollama", ["local"]),
        # Hard rule: metadata cannot be overridden.
        ("blocked_metadata_ip", None, ["cannot be overridden", "metadata"]),
        ("elasticsearch_cloud_id_public_egress", None, ["Cloud ID", "hosts"]),
        ("unknown_egress_scope", None, ["unrecognised", "Egress Scope"]),
    ],
)
def test_guidance_is_clear_and_actionable(reason, target, must_contain):
    # Pass primary_engine=target_id so the scope-mismatch messages mention
    # Adaptive (the legacy happy-path test).
    msg = denial_guidance(
        reason, target=target, target_id=target, primary_engine=target
    )
    assert msg and len(msg) > 20
    for needle in must_contain:
        assert needle in msg, f"{reason}: expected '{needle}' in: {msg}"


def test_scope_mismatch_never_recommends_retired_both_scope():
    # Regression: guidance.py used to say "change your Egress Scope to Both or
    # Public only". `both` was deprecated (migration 0019), so the copy must
    # point at Adaptive (default) instead — at least when the primary engine
    # is the blocked target. (When the primary is something else we omit
    # Adaptive entirely; see the test below.)
    for reason in ("scope_mismatch_private_only", "scope_mismatch_public_only"):
        msg = denial_guidance(
            reason,
            target="arxiv",
            target_id="arxiv",
            primary_engine="arxiv",
        )
        assert "Both" not in msg, f"{reason}: guidance still references 'Both'"
        assert "Adaptive" in msg, (
            f"{reason}: guidance should recommend 'Adaptive' when target is primary"
        )


def test_scope_mismatch_omits_adaptive_when_target_is_not_primary():
    # Adaptive (default) follows the saved primary engine, so if the
    # blocked target is NOT the primary, switching to Adaptive may resolve
    # back to the same restrictive scope and the block would persist. In
    # that case the explicit compatible scope is the only reliable fix —
    # the message must NOT mislead the user into trying Adaptive first.
    msg_private = denial_guidance(
        "scope_mismatch_private_only",
        target="arxiv",
        target_id="arxiv",
        primary_engine="library",
    )
    assert "Adaptive" not in msg_private, (
        "Adaptive must NOT be mentioned when the blocked target isn't the primary"
    )
    assert "Public only" in msg_private
    assert (
        "Private only" in msg_private
    )  # the current scope, in the "what" part

    msg_public = denial_guidance(
        "scope_mismatch_public_only",
        target="library",
        target_id="library",
        primary_engine="arxiv",
    )
    assert "Adaptive" not in msg_public
    assert "Private only" in msg_public
    # Collection-specific escape hatch is preserved on this reason.
    assert "mark it Public on the collection page" in msg_public

    # And the symmetric happy-path: target == primary => Adaptive IS named.
    msg_match = denial_guidance(
        "scope_mismatch_public_only",
        target="library",
        target_id="library",
        primary_engine="library",
    )
    assert "Adaptive" in msg_match
    assert "Private only" in msg_match


def test_scope_mismatch_omits_adaptive_when_primary_engine_unknown():
    # No primary_engine supplied (e.g. we couldn't resolve it for this run).
    # We must NOT mention Adaptive as a remedy because we don't know if it
    # would help — fall back to the explicit compatible scope only.
    msg = denial_guidance("scope_mismatch_private_only", target="arxiv")
    assert "Adaptive" not in msg
    assert "Public only" in msg


def test_adaptive_check_uses_raw_target_id_not_display_target():
    # research_routes formats the display ``target`` as
    # ``"Search engine 'library'"`` but passes the raw ``"library"`` via
    # ``target_id`` for the Adaptive comparison. The display string must
    # NOT participate in the comparison (it would always fail) — only
    # ``target_id`` does.
    msg_match = denial_guidance(
        "scope_mismatch_public_only",
        target="Search engine 'library'",
        target_id="library",
        primary_engine="library",
    )
    assert "Adaptive" in msg_match, (
        "When the raw target_id matches primary_engine, Adaptive must be "
        "named — display-string wrapping must not break the check."
    )

    # And the inverse: the raw ids differ, even though the display strings
    # would look the same.
    msg_diff = denial_guidance(
        "scope_mismatch_public_only",
        target="Search engine 'library'",
        target_id="library",
        primary_engine="arxiv",
    )
    assert "Adaptive" not in msg_diff


def test_guidance_inserts_target():
    msg = denial_guidance("scope_mismatch_private_only", target="arxiv")
    assert "arxiv" in msg


def test_non_policy_reasons_get_honest_explanation_not_setting_advice():
    # A malformed URL isn't a policy block the user fixes via settings.
    msg = denial_guidance("url_malformed")
    assert "malformed" in msg
    assert "Egress Scope" not in msg


def test_unknown_reason_is_safe_and_nonempty():
    msg = denial_guidance("brand_new_code_42", target="X")
    assert "X" in msg
    assert "brand_new_code_42" in msg  # surfaced for support
    assert msg  # never empty


def test_target_none_falls_back_to_this_action():
    msg = denial_guidance("scope_mismatch_private_only")  # no target
    assert msg.startswith("This action")
    assert "Egress Scope" in msg


@pytest.mark.parametrize(
    "reason,needle",
    [
        ("no_hostname", "no host"),
        ("unsupported_scheme", "http/https"),
        ("dangerous_scheme", "non-web scheme"),
        ("host_unclassified", "resolved or classified"),
        ("internal_error", "internal error"),
    ],
)
def test_all_non_policy_reasons_explain_without_setting_advice(reason, needle):
    msg = denial_guidance(reason)
    assert needle in msg
    # These are parse/format failures, not a policy the user toggles.
    assert "Egress Scope" not in msg