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.

20.2 KB
Raw Blame
"""Warning calculation for the settings UI.

Thin orchestrator: reads settings from a single DB session,
delegates to pure check functions in hardware.py and context.py.
"""

from typing import List, Optional

from loguru import logger

from ...database.session_context import get_user_db_session
from ...utilities.db_utils import get_settings_manager
from .context import (
    check_context_below_history,
    check_context_truncation_history,
)
from .backup import (
    check_backup_disabled,
    check_backup_healthy,
    check_no_backups_exist,
    check_report_backup_unavailable,
)
from .hardware import (
    LOCAL_PROVIDERS,
    check_high_context,
    check_legacy_server_config,
    check_model_mismatch,
)
from ...security.egress.policy import DEFAULT_EGRESS_SCOPE
from ...security.egress.warnings import (
    check_cloud_embeddings_enabled,
    check_cloud_llm_enabled,
    check_effective_scope,
    check_local_engine_public_url,
    check_private_engine_url_blocked,
    check_public_egress_enabled,
    check_trusted_destinations,
    check_unprotected_egress,
)
from ...constants import DEFAULT_SEARCH_TOOL


def _safe_check(check_fn, *args, **kwargs):
    """Run a single warning check, returning None on failure."""
    try:
        return check_fn(*args, **kwargs)
    except Exception:
        name = getattr(check_fn, "__name__", repr(check_fn))
        logger.exception(f"Warning check {name} failed")
        return None


def calculate_warnings(username: Optional[str] = None) -> List[dict]:
    """Calculate current warning conditions based on settings.

    Uses a single DB session for all setting reads and history queries.

    Args:
        username: User whose settings/history to inspect. Callers in
            FastAPI routes should pass this explicitly (from Depends(
            require_auth)). When omitted, returns [] since we no longer
            have a Flask session to read it from.
    """
    warnings: List[dict] = []

    try:
        if not username:
            return []
        with get_user_db_session(username) as db_session:
            if not db_session:
                return []

            settings_manager = get_settings_manager(db_session, username)

            # Read all needed settings in one session
            provider = settings_manager.get_setting(
                "llm.provider", "ollama"
            ).lower()
            local_context = settings_manager.get_setting(
                "llm.local_context_window_size", 8192
            )
            current_model = settings_manager.get_setting("llm.model", "")
            dismiss_high_context = settings_manager.get_setting(
                "app.warnings.dismiss_high_context", False
            )
            dismiss_model_mismatch = settings_manager.get_setting(
                "app.warnings.dismiss_model_mismatch", False
            )
            dismiss_context_below_history = settings_manager.get_setting(
                "app.warnings.dismiss_context_below_history", False
            )
            dismiss_context_truncation_history = settings_manager.get_setting(
                "app.warnings.dismiss_context_truncation_history", False
            )
            dismiss_legacy_config = settings_manager.get_setting(
                "app.warnings.dismiss_legacy_config", False
            )
            backup_enabled = settings_manager.get_setting(
                "backup.enabled", True
            )
            dismiss_backup_disabled = settings_manager.get_setting(
                "app.warnings.dismiss_backup_disabled", False
            )
            dismiss_no_backups = settings_manager.get_setting(
                "app.warnings.dismiss_no_backups", False
            )

            logger.debug(f"Starting warning calculation - provider={provider}")

            is_local = provider in LOCAL_PROVIDERS

            # --- Hardware / settings checks (pure functions) ---
            w = _safe_check(
                check_high_context,
                provider,
                local_context,
                dismiss_high_context,
            )
            if w:
                warnings.append(w)

            w = _safe_check(
                check_model_mismatch,
                provider,
                current_model,
                local_context,
                dismiss_model_mismatch,
            )
            if w:
                warnings.append(w)

            w = _safe_check(check_legacy_server_config, dismiss_legacy_config)
            if w:
                warnings.append(w)

            # --- Egress policy checks ---
            egress_scope = settings_manager.get_setting(
                "policy.egress_scope", DEFAULT_EGRESS_SCOPE
            )
            require_local_endpoint = bool(
                settings_manager.get_setting(
                    "llm.require_local_endpoint", False
                )
            )
            embeddings_provider = settings_manager.get_setting(
                "embeddings.provider", ""
            )
            embeddings_base_url = settings_manager.get_setting(
                "embeddings.openai.base_url", ""
            )
            require_local_embeddings = bool(
                settings_manager.get_setting("embeddings.require_local", False)
            )
            primary_engine = settings_manager.get_setting(
                "search.tool", DEFAULT_SEARCH_TOOL
            )
            trusted_inference = settings_manager.get_setting(
                "policy.trusted_inference_providers", []
            )
            trusted_search = settings_manager.get_setting(
                "policy.trusted_search_engines", []
            )

            # Resolve the EFFECTIVE posture so the banners are accurate. For
            # `adaptive`, this turns the opaque "follows the primary" into a
            # concrete scope; it also applies the PRIVATE_ONLY -> force-local
            # coupling, so a private-resolving run doesn't falsely show the
            # "cloud LLM enabled" banner. Best-effort: any failure falls back
            # to the raw values (the page must never break on this).
            effective_scope = str(egress_scope).lower()
            effective_require_local_endpoint = require_local_endpoint
            effective_require_local_embeddings = require_local_embeddings
            try:
                from ...security.egress.policy import (
                    context_from_snapshot,
                    resolve_run_primary_engine,
                )

                _snap = settings_manager.get_settings_snapshot()
                if isinstance(_snap, dict):
                    # allow_dns=False: this runs on the /api/warnings page-
                    # render hot path; skip the synchronous getaddrinfo that
                    # ADAPTIVE resolution would otherwise do for a URL-engine
                    # primary (could block the render up to _DNS_TIMEOUT_SEC).
                    # The banner is advisory and falls back to static
                    # classification — accuracy here is best-effort by design.
                    _eff_ctx = context_from_snapshot(
                        _snap,
                        resolve_run_primary_engine(_snap),
                        username=username,
                        allow_dns=False,
                    )
                    effective_scope = _eff_ctx.scope.value
                    effective_require_local_endpoint = (
                        _eff_ctx.require_local_llm
                    )
                    effective_require_local_embeddings = (
                        _eff_ctx.require_local_embeddings
                    )
            except Exception:
                logger.debug(
                    "could not resolve effective egress scope for warnings",
                    exc_info=True,
                )

            adaptive_info_dismissed = bool(
                settings_manager.get_setting(
                    "app.warnings.dismiss_adaptive_scope_info", False
                )
            )

            # Each egress banner has its OWN dismiss flag. Previously all
            # three shared app.warnings.dismiss_egress_policy, so dismissing
            # the fresh-install "public egress" notice ALSO permanently hid
            # the critical cloud-LLM / cloud-embeddings warnings — a
            # false-safety trap (switch to OpenAI later, never warned).
            public_egress_dismissed = bool(
                settings_manager.get_setting(
                    "app.warnings.dismiss_egress_policy", False
                )
            )
            cloud_llm_dismissed = bool(
                settings_manager.get_setting(
                    "app.warnings.dismiss_cloud_llm", False
                )
            )
            cloud_embeddings_dismissed = bool(
                settings_manager.get_setting(
                    "app.warnings.dismiss_cloud_embeddings", False
                )
            )

            # Informational: state what ADAPTIVE actually resolves to.
            w = _safe_check(
                check_effective_scope,
                egress_scope,
                effective_scope,
                primary_engine,
                adaptive_info_dismissed,
            )
            if w:
                warnings.append(w)

            w = _safe_check(
                check_public_egress_enabled,
                effective_scope,
                public_egress_dismissed,
            )
            if w:
                warnings.append(w)

            # Loud, non-dismissible banner when protection is turned off.
            w = _safe_check(check_unprotected_egress, effective_scope)
            if w:
                warnings.append(w)

            # Trusted off-machine destinations (stage D) relax classification.
            w = _safe_check(
                check_trusted_destinations, trusted_inference, trusted_search
            )
            if w:
                warnings.append(w)

            w = _safe_check(
                check_cloud_llm_enabled,
                provider,
                effective_require_local_endpoint,
                cloud_llm_dismissed,
            )
            if w:
                warnings.append(w)

            w = _safe_check(
                check_cloud_embeddings_enabled,
                embeddings_provider,
                embeddings_base_url,
                effective_require_local_embeddings,
                cloud_embeddings_dismissed,
            )
            if w:
                warnings.append(w)

            # Engine-URL misconfiguration banners, driven entirely by the
            # engine declarations (url_setting + is_public/is_local) that
            # guarded_engine_url_descriptors() collects from the registry:
            #   - a PUBLIC engine (SearXNG) whose private instance URL lacks
            #     operator approval silently self-disables at run time;
            #   - a LOCAL document engine (Paperless, Elasticsearch) whose
            #     URL looks public would send document queries off-box and
            #     is excluded under a private-only scope (fail-up).
            # Display names, activation flags, and dismiss keys follow the
            # per-engine settings conventions, so a future engine is covered
            # by declaring url_setting — not by editing this file.
            # A failure in this loop (settings read, descriptor sweep) must
            # not swallow the later backup/history banners, so it gets its
            # own guard rather than relying on the function-wide except.
            try:
                primary_lower = str(primary_engine or "").lower()
                from ...security.egress.validators import (
                    guarded_engine_url_descriptors,
                )

                for desc in guarded_engine_url_descriptors():
                    name = desc.engine_name
                    base = f"search.engine.web.{name}"
                    display = (
                        settings_manager.get_setting(f"{base}.display_name", "")
                        or name.replace("_", " ").title()
                    )
                    url_value = settings_manager.get_setting(
                        desc.url_setting, ""
                    )
                    engine_active = primary_lower == name or any(
                        bool(
                            settings_manager.get_setting(
                                f"{base}.{flag}", False
                            )
                        )
                        for flag in (
                            "enabled",
                            "use_in_auto_search",
                            "agent_enabled",
                        )
                    )
                    if desc.is_public:
                        w = _safe_check(
                            check_private_engine_url_blocked,
                            display_name=display,
                            engine_name=name,
                            url_setting=desc.url_setting,
                            instance_url=url_value,
                            active=engine_active,
                            acknowledged=bool(
                                settings_manager.get_setting(
                                    f"app.warnings.dismiss_{name}_private_url",
                                    False,
                                )
                            ),
                        )
                    else:
                        # Engine-specific public-endpoint indicator: an
                        # Elasticsearch cloud_id always targets Elastic
                        # Cloud, regardless of what the hosts list says.
                        extra = ""
                        if name == "elasticsearch":
                            cloud_id = settings_manager.get_setting(
                                f"{base}.default_params.cloud_id", ""
                            )
                            if str(cloud_id or "").strip():
                                extra = "Elastic Cloud (cloud_id)"
                        w = _safe_check(
                            check_local_engine_public_url,
                            display_name=display,
                            engine_name=name,
                            urls=url_value,
                            active=engine_active,
                            acknowledged=bool(
                                settings_manager.get_setting(
                                    f"app.warnings.dismiss_{name}_public_url",
                                    False,
                                )
                            ),
                            extra=extra,
                            url_setting=desc.url_setting,
                        )
                    if w:
                        warnings.append(w)
            except Exception:
                # logger.exception, not debug: if the descriptor sweep
                # breaks (e.g. an engine-import regression) the whole
                # banner feature goes dark — that must be visible in
                # production logs, matching _safe_check's severity bar.
                logger.exception("engine-URL warning banners skipped")

            # --- Backup checks ---
            w = _safe_check(
                check_backup_disabled, backup_enabled, dismiss_backup_disabled
            )
            if w:
                warnings.append(w)

            # Check backup file status (lightweight directory listing)
            dismiss_backup_info = settings_manager.get_setting(
                "app.warnings.dismiss_backup_info", False
            )
            try:
                from ...config.paths import get_user_backup_directory
                from ...utilities.formatting import human_size
                from ...database.backup.backup_service import (
                    is_safe_glob_result,
                    list_backup_files,
                )

                if username:
                    backup_dir = get_user_backup_directory(username)
                    total_size = 0
                    backup_count = 0
                    # Not Path.glob: it returns nothing for a directory it
                    # cannot read, which would raise a false "no backups"
                    # banner. A listing error skips both backup banners (see
                    # the OSError handler below).
                    for f in list_backup_files(backup_dir):
                        # Skip symlinks / entries resolving outside backup_dir
                        # so a planted symlink can't inflate the count/size
                        # shown in warnings — same hardening as BackupService.
                        if not is_safe_glob_result(f, backup_dir):
                            continue
                        try:
                            total_size += f.stat().st_size
                            backup_count += 1
                        except FileNotFoundError:
                            continue

                    w = _safe_check(
                        check_no_backups_exist,
                        backup_enabled,
                        backup_count,
                        dismiss_no_backups,
                    )
                    if w:
                        warnings.append(w)

                    w = _safe_check(
                        check_backup_healthy,
                        backup_enabled,
                        backup_count,
                        human_size(total_size),
                        dismiss_backup_info,
                    )
                    if w:
                        warnings.append(w)
            except OSError as exc:
                # The backup directory, or an entry in it, could not be
                # accessed, so there is no count to base either banner on.
                # Skip both; the settings panel names the problem. Debug
                # level: this runs on every warnings poll. Exception type
                # only: no path and no secret.
                logger.debug(
                    "Backup banners skipped: backup directory could not be "
                    "accessed ({})",
                    type(exc).__name__,
                )
            except Exception:
                logger.debug("Backup status check skipped")

            # --- Report file backup check ---
            # Runs the same owner and mode check that FileReportStorage runs
            # before writing, so the banner shows while that check would
            # refuse the default directory. Own guard: a failure here must
            # not hide the history banners below.
            try:
                report_backup_enabled = bool(
                    settings_manager.get_setting(
                        "report.enable_file_backup", False
                    )
                )
                report_backup_problem = None
                if report_backup_enabled:
                    from ...config.paths import (
                        DirectoryNotPrivateError,
                        get_research_outputs_directory,
                        restrict_directory_to_owner,
                    )

                    try:
                        restrict_directory_to_owner(
                            get_research_outputs_directory()
                        )
                    except DirectoryNotPrivateError as exc:
                        report_backup_problem = exc.reason
                    except OSError:
                        report_backup_problem = "unavailable"
                w = _safe_check(
                    check_report_backup_unavailable,
                    report_backup_enabled,
                    report_backup_problem,
                )
                if w:
                    warnings.append(w)
            except Exception:
                logger.exception("Report file backup warning check skipped")

            # --- History-based checks (need DB queries) ---
            if is_local and not dismiss_context_below_history:
                w = _safe_check(
                    check_context_below_history, db_session, local_context
                )
                if w:
                    warnings.append(w)

            if is_local and not dismiss_context_truncation_history:
                w = _safe_check(
                    check_context_truncation_history, db_session, local_context
                )
                if w:
                    warnings.append(w)

    except Exception:
        logger.exception("Error calculating warnings")

    return warnings