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.

21.7 KB
Raw Blame
"""Saved news subscriptions run with their own provider and model (#7860).

* ``resolve_subscription_model`` turns a saved row into the
  ``(provider, model)`` it runs with, including rows the news page's
  "Create Subscription" saved before #7860 with the whole ``PROVIDER:model``
  selection as the model and its first segment as the provider.
* A scheduled run hands quick_summary one settings snapshot carrying the
  subscription's provider, model, search engine and (for ``openai_endpoint``)
  custom endpoint (quick_summary reads its ``provider`` argument only when no
  snapshot is given), and generates the headline and topics from that same
  snapshot.
* A scheduled run is refused, and researches and generates nothing, when
  its egress context cannot be built, when the two-axis rule refuses it, or
  when it would use an ``openai_endpoint`` URL that is unset or fails the
  custom-endpoint check run-now applies.
* The run-now/overdue payload resolves saved rows the same way.
"""

from contextlib import contextmanager
from datetime import datetime, UTC
from unittest.mock import MagicMock, Mock, patch

import pytest

from local_deep_research.news.subscription_runner import (
    build_subscription_request_data,
    resolve_subscription_model,
)

DB_SESSION = "local_deep_research.database.session_context.get_user_db_session"
SETTINGS_MGR = "local_deep_research.settings.manager.SettingsManager"
SET_CTX = "local_deep_research.config.thread_settings.set_settings_context"
GET_LLM = "local_deep_research.api.research_functions.get_llm"
QUICK_SUMMARY = "local_deep_research.api.research_functions.quick_summary"
HEADLINE_GEN = (
    "local_deep_research.news.utils.headline_generator.generate_headline"
)
TOPIC_GEN = "local_deep_research.news.utils.topic_generator.generate_topics"
REPORT_STORAGE = "local_deep_research.storage.get_report_storage"
CITATION_FMT = (
    "local_deep_research.text_optimization.citation_formatter.CitationFormatter"
)
GET_SETTING_SNAP = (
    "local_deep_research.config.search_config.get_setting_from_snapshot"
)

# The user's own settings in every scheduler test below.
DEFAULT_PROVIDER = "openai"
DEFAULT_MODEL = "gpt-4.1-mini"
PRIMARY_ENGINE = "searxng"
USER_ENDPOINT = "http://10.0.0.5:8080/v1"


@pytest.mark.parametrize(
    "provider,model,expected",
    [
        # Saved from the model dropdown before #7860: the uppercase key is
        # stripped once, whatever case the provider was stored in.
        ("ollama", "OLLAMA:llama3", ("ollama", "llama3")),
        ("OLLAMA", "OLLAMA:llama3", ("ollama", "llama3")),
        ("ollama", "OLLAMA:llama3.1:8b", ("ollama", "llama3.1:8b")),
        ("openai", "OPENAI:gpt-4.1-mini", ("openai", "gpt-4.1-mini")),
        (
            "openai_endpoint",
            "OPENAI_ENDPOINT:org/model",
            ("openai_endpoint", "org/model"),
        ),
        ("ollama", "OLLAMA:OLLAMA:model", ("ollama", "OLLAMA:model")),
        ("ollama", "OLLAMA:", ("ollama", None)),
        # A model tag typed by hand, its first segment saved as the
        # provider: the provider is unset and the model kept whole, even
        # when that segment is a provider key in another case.
        ("qwen", "qwen:7b", (None, "qwen:7b")),
        ("llama3.1", "llama3.1:8b", (None, "llama3.1:8b")),
        ("ollama", "ollama:llama3.1:8b", (None, "ollama:llama3.1:8b")),
        # Any other row is kept as saved; OPENAI is not a prefix of
        # OPENAI_ENDPOINT:, and another provider's prefix is not stripped.
        ("ollama", "llama3.1:8b", ("ollama", "llama3.1:8b")),
        (
            "openai",
            "OPENAI_ENDPOINT:org/model",
            ("openai", "OPENAI_ENDPOINT:org/model"),
        ),
        ("ollama", "OPENAI:model:tag", ("ollama", "OPENAI:model:tag")),
        (
            "openrouter",
            "vendor/model:free",
            ("openrouter", "vendor/model:free"),
        ),
        # Not a built-in provider key: unset.
        ("custom", "my-model", (None, "my-model")),
        # NULL/'' provider: unset, and the model is not touched.
        (None, "OLLAMA:llama3", (None, "OLLAMA:llama3")),
        ("", "OLLAMA:llama3", (None, "OLLAMA:llama3")),
        (None, "llama3.1:8b", (None, "llama3.1:8b")),
        # NULL/'' model: unset.
        ("ollama", None, ("ollama", None)),
        ("ollama", "", ("ollama", None)),
        (None, None, (None, None)),
        ("", "", (None, None)),
    ],
)
def test_resolve_subscription_model(provider, model, expected):
    assert resolve_subscription_model(provider, model) == expected


@pytest.mark.parametrize(
    "provider,model,expected",
    [
        ("ollama", "OLLAMA:llama3.1:8b", ("ollama", "llama3.1:8b")),
        ("qwen", "qwen:7b", (None, "qwen:7b")),
        ("llama3.1", "llama3.1:8b", (None, "llama3.1:8b")),
        ("ollama", "", ("ollama", None)),
    ],
)
def test_run_now_payload_resolves_the_saved_row(provider, model, expected):
    """The run-now route and the overdue sweep build their payload here."""
    data = build_subscription_request_data(
        query_template="q",
        current_date="2026-10-24",
        triggered_by="manual",
        subscription_id="s",
        model_provider=provider,
        model=model,
    )
    assert (data["model_provider"], data["model"]) == expected


@pytest.fixture
def scheduler(monkeypatch):
    from local_deep_research.scheduler.background import (
        BackgroundJobScheduler,
    )
    import local_deep_research.scheduler.background as mod

    # The egress scope comes from the test snapshots, not the environment.
    monkeypatch.delenv("LDR_POLICY_EGRESS_SCOPE", raising=False)
    BackgroundJobScheduler._instance = None
    mod._scheduler_instance = None
    with patch(f"{mod.__name__}.BackgroundScheduler") as cls:
        cls.return_value = MagicMock()
        sched = BackgroundJobScheduler()
        sched.user_sessions["u"] = {
            "scheduled_jobs": set(),
            "last_activity": datetime.now(UTC),
        }
        sched._credential_store.store("u", "pw")
        sched.scheduler.get_job.return_value = None
        yield sched
    BackgroundJobScheduler._instance = None
    mod._scheduler_instance = None


def _ctx(session):
    session.__enter__ = Mock(return_value=session)
    session.__exit__ = Mock(return_value=False)
    return session


def _value(snapshot, key):
    entry = snapshot[key]
    return entry["value"] if isinstance(entry, dict) else entry


def _user_snapshot(
    egress_scope=None,
    primary_engine=PRIMARY_ENGINE,
    user_provider=DEFAULT_PROVIDER,
    user_endpoint=USER_ENDPOINT,
):
    snapshot = {
        "llm.provider": {"value": user_provider, "ui_element": "select"},
        "llm.model": {"value": DEFAULT_MODEL, "ui_element": "text"},
    }
    if user_endpoint is not None:
        snapshot["llm.openai_endpoint.url"] = {
            "value": user_endpoint,
            "ui_element": "text",
        }
    if primary_engine:
        snapshot["search.tool"] = {
            "value": primary_engine,
            "ui_element": "select",
        }
    if egress_scope:
        snapshot["policy.egress_scope"] = {
            "value": egress_scope,
            "ui_element": "select",
        }
    return snapshot


@contextmanager
def _saved_subscription(
    provider,
    model,
    search_engine=None,
    egress_scope=None,
    custom_endpoint=None,
    primary_engine=PRIMARY_ENGINE,
    user_provider=DEFAULT_PROVIDER,
    user_endpoint=USER_ENDPOINT,
):
    """Patch the user database to hold one active subscription row."""
    sub = MagicMock()
    sub.status = "active"
    sub.id = 1
    sub.name = "Sub"
    sub.query_or_topic = "fusion news"
    sub.refresh_interval_minutes = 60
    sub.model_provider = provider
    sub.model = model
    sub.search_strategy = "news_aggregation"
    sub.search_engine = search_engine
    sub.custom_endpoint = custom_endpoint
    db = _ctx(MagicMock())
    db.get.return_value = sub
    settings_manager = MagicMock()
    # A fresh copy per call, as SettingsManager builds one.
    settings_manager.get_settings_snapshot.side_effect = (
        lambda *args, **kwargs: _user_snapshot(
            egress_scope, primary_engine, user_provider, user_endpoint
        )
    )
    with (
        patch(DB_SESSION, return_value=db),
        patch(SETTINGS_MGR, return_value=settings_manager),
        patch(SET_CTX),
    ):
        yield db


class _StopAtLLM(Exception):
    """Raised by the patched get_llm to end the run once it is reached."""


def _research_llm_call(scheduler, provider, model, **kwargs):
    """Run the saved row through the scheduler and the real quick_summary up
    to get_llm; return the provider, model and snapshot get_llm resolves."""
    with (
        _saved_subscription(provider, model, **kwargs),
        patch(GET_LLM, side_effect=_StopAtLLM) as get_llm,
        patch.object(scheduler, "_store_research_result") as store,
    ):
        scheduler._check_subscription("u", 1)

    get_llm.assert_called_once()
    store.assert_not_called()
    call = get_llm.call_args.kwargs
    snapshot = call["settings_snapshot"]
    # get_llm uses an explicit argument and otherwise the snapshot.
    used_provider = call["provider"]
    if used_provider is None:
        used_provider = _value(snapshot, "llm.provider")
    used_model = call["model_name"]
    if used_model is None:
        used_model = _value(snapshot, "llm.model")
    return used_provider, used_model, snapshot


SCHEDULED_ROWS = [
    # The subscription's own provider and model.
    ("ollama", "llama3.1:8b", ("ollama", "llama3.1:8b")),
    ("anthropic", "claude-model", ("anthropic", "claude-model")),
    # Saved from the dropdown before #7860.
    ("ollama", "OLLAMA:llama3.1:8b", ("ollama", "llama3.1:8b")),
    # A typed model tag: the user's provider with the whole tag.
    ("qwen", "qwen:7b", (DEFAULT_PROVIDER, "qwen:7b")),
    ("llama3.1", "llama3.1:8b", (DEFAULT_PROVIDER, "llama3.1:8b")),
    # Unset: the user's provider and model.
    (None, None, (DEFAULT_PROVIDER, DEFAULT_MODEL)),
    ("", "", (DEFAULT_PROVIDER, DEFAULT_MODEL)),
    ("ollama", "", ("ollama", DEFAULT_MODEL)),
]


@pytest.mark.parametrize("provider,model,expected", SCHEDULED_ROWS)
def test_scheduled_research_runs_on_the_subscription_provider_and_model(
    scheduler, provider, model, expected
):
    used_provider, used_model, _ = _research_llm_call(
        scheduler, provider, model
    )
    assert (used_provider, used_model) == expected


@pytest.mark.parametrize(
    "egress_scope,expected_engine",
    [
        (None, "wikipedia"),
        # Under STRICT the shared overlay keeps the saved primary engine.
        ("strict", PRIMARY_ENGINE),
    ],
)
def test_scheduled_search_engine_goes_through_the_runtime_overlay(
    scheduler, egress_scope, expected_engine
):
    used_provider, used_model, snapshot = _research_llm_call(
        scheduler,
        "ollama",
        "llama3.1:8b",
        search_engine="wikipedia",
        egress_scope=egress_scope,
    )
    assert _value(snapshot, "search.tool") == expected_engine
    assert (used_provider, used_model) == ("ollama", "llama3.1:8b")


@pytest.mark.parametrize("provider,model,expected", SCHEDULED_ROWS)
def test_headline_and_topics_use_the_research_provider_and_model(
    scheduler, provider, model, expected
):
    formatter = MagicMock()
    formatter.return_value.format_document.return_value = "R"
    with (
        _saved_subscription(provider, model) as db,
        patch(
            QUICK_SUMMARY,
            return_value={"report": "R", "query": "q", "sources": []},
        ) as quick_summary,
        patch(HEADLINE_GEN, return_value="H") as headline,
        patch(TOPIC_GEN, return_value=["t"]) as topics,
        patch(REPORT_STORAGE),
        patch(CITATION_FMT, formatter),
        patch(GET_SETTING_SNAP, return_value="no_hyperlinks"),
    ):
        scheduler._check_subscription("u", 1)

    quick_summary.assert_called_once()
    headline.assert_called_once()
    topics.assert_called_once()
    db.add.assert_called_once()
    research_snapshot = quick_summary.call_args.kwargs["settings_snapshot"]
    for snapshot in (
        research_snapshot,
        headline.call_args.kwargs["settings_snapshot"],
        topics.call_args.kwargs["settings_snapshot"],
    ):
        used = (
            _value(snapshot, "llm.provider"),
            _value(snapshot, "llm.model"),
        )
        assert used == expected
        assert snapshot["_username"] == "u"


BACKGROUND_LOGGER = "local_deep_research.scheduler.background.logger"
# A self-hosted store, classified sensitive by the two-axis rule (ADR-0007).
SENSITIVE_PRIMARY = "elasticsearch"


@contextmanager
def _scheduled_news_generation():
    """Patch quick_summary and the headline/topic generation after it."""
    formatter = MagicMock()
    formatter.return_value.format_document.return_value = "R"
    with (
        patch(
            QUICK_SUMMARY,
            return_value={"report": "R", "query": "q", "sources": []},
        ) as quick_summary,
        patch(HEADLINE_GEN, return_value="H") as headline,
        patch(TOPIC_GEN, return_value=["t"]) as topics,
        patch(REPORT_STORAGE),
        patch(CITATION_FMT, formatter),
        patch(GET_SETTING_SNAP, return_value="no_hyperlinks"),
    ):
        yield quick_summary, headline, topics


@pytest.mark.parametrize(
    "primary_engine,search_engine,provider,admitted,reason,offending",
    [
        # Configurations the two-axis rule refuses.
        (
            SENSITIVE_PRIMARY,
            None,
            "anthropic",
            False,
            "sensitive_to_exposing_inference",
            ("llm:anthropic",),
        ),
        (
            SENSITIVE_PRIMARY,
            None,
            "openai",
            False,
            "sensitive_to_exposing_inference",
            ("llm:openai",),
        ),
        # The subscription's own search engine is the one audited, not the
        # user's saved primary.
        (
            PRIMARY_ENGINE,
            SENSITIVE_PRIMARY,
            "anthropic",
            False,
            "sensitive_to_exposing_inference",
            ("llm:anthropic",),
        ),
        # Configurations it allows.
        (SENSITIVE_PRIMARY, None, "ollama", True, None, ()),
        (PRIMARY_ENGINE, None, "anthropic", True, None, ()),
        (SENSITIVE_PRIMARY, PRIMARY_ENGINE, "anthropic", True, None, ()),
    ],
)
def test_scheduled_run_applies_the_two_axis_egress_check(
    scheduler,
    primary_engine,
    search_engine,
    provider,
    admitted,
    reason,
    offending,
):
    with (
        _saved_subscription(
            provider,
            "some-model",
            search_engine=search_engine,
            primary_engine=primary_engine,
            user_provider="ollama",
        ) as db,
        _scheduled_news_generation() as (quick_summary, headline, topics),
        patch(BACKGROUND_LOGGER) as logger,
    ):
        scheduler._check_subscription("u", 1)

    for generated in (quick_summary, headline, topics, db.add):
        assert generated.called is admitted
    refusals = [
        call.args[0] for call in logger.bind.return_value.warning.call_args_list
    ]
    if admitted:
        snapshot = quick_summary.call_args.kwargs["settings_snapshot"]
        assert _value(snapshot, "llm.provider") == provider
        assert _value(snapshot, "search.tool") == (
            search_engine or primary_engine
        )
        assert not any(
            message.startswith("scheduled subscription run refused")
            for message in refusals
        ), refusals
    else:
        # Pin the refusal line AND its stable fields. The call-suppression
        # asserts above pass whether the run was refused by the rule or
        # blew up on the log path (the error is swallowed by
        # _check_subscription's catch-all and the run generates nothing
        # either way), so without this the line could silently vanish.
        logger.bind.assert_any_call(policy_audit=True)
        logger.bind.return_value.warning.assert_called_once_with(
            "scheduled subscription run refused: two-axis egress",
            subscription_id=1,
            reason=reason,
            offending=offending,
        )


@pytest.mark.parametrize(
    "primary_engine,egress_scope,exc_name",
    [
        # No primary engine: the run's egress scope cannot be resolved
        # (ValueError).
        (None, None, "ValueError"),
        # An unknown saved scope (PolicyDeniedError).
        (PRIMARY_ENGINE, "not-a-scope", "PolicyDeniedError"),
    ],
)
def test_scheduled_run_is_refused_when_its_egress_context_cannot_be_built(
    scheduler, primary_engine, egress_scope, exc_name
):
    with (
        _saved_subscription(
            "anthropic",
            "some-model",
            primary_engine=primary_engine,
            egress_scope=egress_scope,
        ) as db,
        _scheduled_news_generation() as (quick_summary, headline, topics),
        patch(BACKGROUND_LOGGER) as logger,
    ):
        scheduler._check_subscription("u", 1)

    for generated in (quick_summary, headline, topics, db.add):
        generated.assert_not_called()
    logger.bind.assert_any_call(policy_audit=True)
    # Pin the refusal line AND its stable fields. ``reason`` is the
    # exception CLASS NAME only: the exception text can carry the refused
    # target (PolicyDeniedError holds ``target=str(raw)``) and must never
    # reach the log (the security suites enforce this repo-wide).
    logger.bind.return_value.warning.assert_called_once_with(
        "scheduled subscription run refused: egress policy unevaluable",
        subscription_id=1,
        reason=exc_name,
    )


SUBSCRIPTION_ENDPOINT = "http://192.168.1.20:8000/v1"


@pytest.mark.parametrize(
    "provider,custom_endpoint,expected_endpoint",
    [
        # An openai_endpoint row runs against the endpoint saved with it.
        ("openai_endpoint", SUBSCRIPTION_ENDPOINT, SUBSCRIPTION_ENDPOINT),
        # Without one, the user's endpoint setting.
        ("openai_endpoint", None, USER_ENDPOINT),
        ("openai_endpoint", "", USER_ENDPOINT),
        # Another provider ignores the saved endpoint, as run-now does.
        ("ollama", SUBSCRIPTION_ENDPOINT, USER_ENDPOINT),
    ],
)
def test_scheduled_openai_endpoint_run_uses_the_subscription_endpoint(
    scheduler, provider, custom_endpoint, expected_endpoint
):
    used_provider, used_model, snapshot = _research_llm_call(
        scheduler, provider, "org/model", custom_endpoint=custom_endpoint
    )
    assert (used_provider, used_model) == (provider, "org/model")
    assert _value(snapshot, "llm.openai_endpoint.url") == expected_endpoint


# Link-local endpoints the openai_endpoint provider's own base-URL check
# admits but the custom-endpoint check run-now applies refuses.
LINK_LOCAL_ENDPOINTS = [
    "http://169.254.1.2:11434/v1",
    "169.254.1.2:11434",
    "http://[fe80::1]:8000/v1",
]
LINK_LOCAL_ENDPOINT = LINK_LOCAL_ENDPOINTS[0]


# run_endpoint: the URL in the snapshot the run gets, or None when the run is
# refused.
@pytest.mark.parametrize(
    "provider,custom_endpoint,user_provider,user_endpoint,run_endpoint",
    [
        # The subscription's own endpoint fails the check.
        *[
            ("openai_endpoint", endpoint, "ollama", USER_ENDPOINT, None)
            for endpoint in LINK_LOCAL_ENDPOINTS
        ],
        (
            "openai_endpoint",
            "http://169.254.169.254/latest/meta-data/",
            "ollama",
            USER_ENDPOINT,
            None,
        ),
        # No endpoint on the subscription: the user's saved URL is the one
        # checked, also when openai_endpoint is the user's own provider.
        ("openai_endpoint", None, "ollama", LINK_LOCAL_ENDPOINT, None),
        ("openai_endpoint", "", "ollama", LINK_LOCAL_ENDPOINT, None),
        (None, None, "openai_endpoint", LINK_LOCAL_ENDPOINT, None),
        # No endpoint anywhere (user URL blank or not saved).
        ("openai_endpoint", None, "ollama", "", None),
        ("openai_endpoint", "  ", "ollama", None, None),
        (None, None, "openai_endpoint", "", None),
        # A safe endpoint runs, and the subscription's endpoint replaces an
        # unsafe saved URL. Another provider does not use the endpoint, so
        # neither an unsafe nor a blank one is checked.
        (
            "openai_endpoint",
            SUBSCRIPTION_ENDPOINT,
            "ollama",
            LINK_LOCAL_ENDPOINT,
            SUBSCRIPTION_ENDPOINT,
        ),
        ("openai_endpoint", None, "ollama", USER_ENDPOINT, USER_ENDPOINT),
        ("ollama", LINK_LOCAL_ENDPOINT, "ollama", "", ""),
    ],
)
def test_scheduled_run_checks_the_endpoint_it_would_use(
    scheduler,
    provider,
    custom_endpoint,
    user_provider,
    user_endpoint,
    run_endpoint,
):
    """An openai_endpoint run needs a URL that passes the check run-now
    applies, whether it comes from the subscription or the user's settings;
    otherwise nothing runs and a policy-audit line is logged."""
    with (
        _saved_subscription(
            provider,
            "org/model",
            custom_endpoint=custom_endpoint,
            user_provider=user_provider,
            user_endpoint=user_endpoint,
        ) as db,
        _scheduled_news_generation() as (quick_summary, headline, topics),
        patch(BACKGROUND_LOGGER) as logger,
    ):
        scheduler._check_subscription("u", 1)

    admitted = run_endpoint is not None
    for generated in (quick_summary, headline, topics, db.add):
        assert generated.called is admitted
    refusals = [
        call.args[0] for call in logger.bind.return_value.warning.call_args_list
    ]
    if admitted:
        snapshot = quick_summary.call_args.kwargs["settings_snapshot"]
        assert _value(snapshot, "llm.openai_endpoint.url") == run_endpoint
        assert "scheduled subscription run refused: custom endpoint" not in (
            refusals
        )
    else:
        logger.bind.assert_any_call(policy_audit=True)
        assert refusals == [
            "scheduled subscription run refused: custom endpoint"
        ], refusals