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.

16.4 KB
Raw Blame
"""Stage B (ADR-0007): bridge the pure classification core to live engines and
providers, assemble a run's component set, and decide its admissibility.

Resolution reads each element's *declared* two-axis label (the class attributes
added in stage A) and applies the dynamic refinements that depend on
configuration:

  * a self-hosted store's exposure flips to ``EXPOSING`` when its configured
    URL resolves to a public host (reusing the policy's fail-up classifier), so
    a Paperless/Elasticsearch holding private data on a public endpoint becomes
    quadrant 4 (sensitive + exposing);
  * a collection's sensitivity flips to ``NON_SENSITIVE`` when it is marked
    public; the aggregate ``library`` stays sensitive;
  * a collection's exposure flips to ``EXPOSING`` when the vector store its
    index lives in is not a local file and its configured endpoint does not
    resolve local — the same fail-up, applied to the RAG sink.

``audit_run`` computes and LOGS the four-quadrant decision and returns it; the
run-start precheck AND the worker chokepoint ENFORCE on that result (rejecting a
denied run) — except under the ``UNPROTECTED`` escape hatch, where ``audit_run``
evaluates in permissive mode (always allowed, diagnostic preserved).
``audit_run`` never raises: if the decision cannot be computed it **fails
closed**, returning a denying ``Decision`` (reason ``audit_error``) so the run
is refused visibly rather than silently degrading to the scope PEPs.
"""

from __future__ import annotations

from typing import Iterable, List, Optional

from loguru import logger

from .classification import (
    Component,
    Decision,
    Exposure,
    Label,
    Mode,
    Role,
    Sensitivity,
    evaluate_run,
)


def engine_label(engine_name: str, settings_snapshot, ctx) -> Optional[Label]:
    """Resolve a search engine's two-axis label, or ``None`` if unknown.

    Reads the engine class's declared ``egress_sensitivity`` /
    ``egress_exposure`` and applies the configuration-dependent refinements.
    """
    from .policy import (
        _classify_engine_url,
        _get_engine_class,
        _get_setting_value,
        _resolve_collection_is_public,
        vector_store_is_contained,
    )

    # Collections / library: the class is not a static engine, and sensitivity
    # comes from the per-collection is_public flag (public -> non-sensitive).
    if engine_name == "library" or engine_name.startswith("collection_"):
        is_public = _resolve_collection_is_public(engine_name, ctx.username)
        sensitivity = (
            Sensitivity.NON_SENSITIVE if is_public else Sensitivity.SENSITIVE
        )
        # A collection is contained only while its VECTOR STORE is: with a
        # local-file store (FAISS) the documents, embeddings and queries never
        # leave the box, but a server-backed store on a public endpoint is a
        # sink just like Paperless/Elasticsearch on a public ``url_setting`` —
        # so it fails up to EXPOSING (quadrant 4 for a private collection).
        # Shared with the scope PEP's ``classify_engine`` so the two can't
        # diverge; a strict no-op for a local-file store.
        exposure = (
            Exposure.CONTAINED
            if vector_store_is_contained(engine_name, ctx, settings_snapshot)
            else Exposure.EXPOSING
        )
        return Label(sensitivity, exposure)

    cls = _get_engine_class(engine_name)
    if cls is None:
        return None  # unknown engine — the per-engine PEP still guards it

    sensitivity = getattr(cls, "egress_sensitivity", Sensitivity.SENSITIVE)
    exposure = getattr(cls, "egress_exposure", Exposure.EXPOSING)

    # Exposure fail-up: a contained store whose configured URL resolves to a
    # public host leaks the query off the box -> EXPOSING. Only ever tightens,
    # mirroring the policy's asymmetric URL override. COUPLED WITH
    # ``classify_engine``: it reads the endpoint with the same Elasticsearch
    # node handling (a ``hosts`` string split into nodes, host dictionaries)
    # and acts on the same two verdicts, a public endpoint (the engine turns
    # public) and a configured one it cannot classify, such as a fake-IP
    # address (the engine is withheld). Neither is provably on the box.
    url_setting = getattr(cls, "url_setting", None)
    if url_setting and exposure is Exposure.CONTAINED:
        host_classification = _classify_engine_url(
            url_setting,
            settings_snapshot,
            ctx,
            allow_host_dicts=engine_name == "elasticsearch",
        )
        if host_classification is False or (
            host_classification is None
            and _get_setting_value(settings_snapshot, url_setting, None)
        ):
            exposure = Exposure.EXPOSING

    # Per-destination trust (ADR-0007 stage D): the user has explicitly vouched
    # for this engine's endpoint, so treat it as contained (moves a dual-risk
    # store from quadrant 4 to quadrant 2). Only ever applies to a local-nature
    # store that failed up on its URL — NEVER to an inherently-public engine
    # (is_public), so a trusted name can't launder a public search sink.
    if (
        exposure is Exposure.EXPOSING
        and getattr(cls, "is_public", False) is not True
        and engine_name.lower()
        in _trusted_names("policy.trusted_search_engines", settings_snapshot)
    ):
        exposure = Exposure.CONTAINED

    return Label(sensitivity, exposure)


def _require_local_probe(ctx):
    """A require-local EgressContext used to ask the enforcing PEPs whether a
    provider's configured endpoint is local. Copies the caller's hostname
    allow-list / username so classification matches the real run."""
    from .policy import EgressContext, EgressScope

    return EgressContext(
        scope=EgressScope.PRIVATE_ONLY,
        primary_engine=getattr(ctx, "primary_engine", "") or "",
        require_local_llm=True,
        require_local_embeddings=True,
        local_hostnames=getattr(ctx, "local_hostnames", ()) or (),
        username=getattr(ctx, "username", None),
    )


def _trusted_names(setting_key: str, settings_snapshot) -> set:
    """Lower-cased set of names the user has explicitly trusted.

    Reads a JSON-list setting (mirroring ``llm.allowed_local_hostnames``);
    returns an empty set on a missing/malformed value.
    """
    if not settings_snapshot:
        return set()
    from .policy import _get_setting_value, coerce_str_list

    _, names = coerce_str_list(
        _get_setting_value(settings_snapshot, setting_key, ())
    )
    return {n.lower() for n in names}


def _inference_label(provider, evaluate_fn, settings_snapshot, ctx) -> Label:
    """Shared exposure resolution for an inference sink (LLM or embeddings).

    Decided by the SAME classification the enforcing PEP uses (``evaluate_fn``
    under a require-local probe): the provider's configured endpoint is
    CONTAINED iff the PEP would accept it as local, else EXPOSING — keeping the
    resolver in exact lock-step with enforcement (so a self-hosted endpoint on a
    local URL is contained, not falsely exposing). Trust then relaxes an
    exposing sink the user has explicitly vouched for.
    """
    p = (provider or "").lower()
    snapshot = settings_snapshot if settings_snapshot is not None else {}
    try:
        allowed = evaluate_fn(
            p, _require_local_probe(ctx), settings_snapshot=snapshot
        ).allowed
    except Exception:  # noqa: silent-exception - fail closed to exposing
        allowed = False
    exposure = Exposure.CONTAINED if allowed else Exposure.EXPOSING
    if exposure is Exposure.EXPOSING and p in _trusted_names(
        "policy.trusted_inference_providers", settings_snapshot
    ):
        exposure = Exposure.CONTAINED
    return Label(Sensitivity.NON_SENSITIVE, exposure)


def llm_label(
    provider: Optional[str], settings_snapshot=None, ctx=None
) -> Label:
    """Resolve an LLM provider's label (an inference sink — exposure only)."""
    from .policy import evaluate_llm_endpoint

    return _inference_label(
        provider, evaluate_llm_endpoint, settings_snapshot, ctx
    )


def embeddings_label(
    provider: Optional[str], settings_snapshot=None, ctx=None
) -> Label:
    """Resolve an embeddings provider's label (an inference sink)."""
    from .policy import evaluate_embeddings

    return _inference_label(
        provider, evaluate_embeddings, settings_snapshot, ctx
    )


def classify_run(
    settings_snapshot,
    ctx,
    *,
    engines: Iterable[str],
    llm_provider: Optional[str] = None,
    embeddings_provider: Optional[str] = None,
) -> List[Component]:
    """Assemble the run's classified component set.

    Each search engine contributes two components sharing its name — a
    ``SOURCE`` (carrying its sensitivity) and a ``SEARCH_SINK`` (carrying its
    exposure) — so the quadrant-4 self-exclusion in :func:`evaluate_run` works.
    The LLM and embeddings providers are ``INFERENCE_SINK`` components.
    """
    components: List[Component] = []
    for name in engines:
        label = engine_label(name, settings_snapshot, ctx)
        if label is None:
            # Unknown engine (not in the registry — e.g. a programmatic
            # retriever): fail CLOSED to the most restrictive quadrant rather
            # than dropping it, so an unclassified source can never silently
            # relax the run's admissibility.
            label = Label(Sensitivity.SENSITIVE, Exposure.EXPOSING)
        components.append(Component(name, Role.SOURCE, label))
        components.append(Component(name, Role.SEARCH_SINK, label))
    if llm_provider:
        components.append(
            Component(
                f"llm:{llm_provider}",
                Role.INFERENCE_SINK,
                llm_label(llm_provider, settings_snapshot, ctx),
            )
        )
    if embeddings_provider:
        components.append(
            Component(
                f"embeddings:{embeddings_provider}",
                Role.INFERENCE_SINK,
                embeddings_label(embeddings_provider, settings_snapshot, ctx),
            )
        )
    return components


def _fail_closed_decision(ctx) -> Decision:
    """The decision to return when the two-axis rule cannot be computed.

    Refuses the run (reason ``audit_error``) so the failure is visible, except
    under the ``UNPROTECTED`` escape hatch, which must never block.

    This is called from the ``except`` blocks that handle failures, so it must
    NOT import ``.policy``: if the triggering failure was itself a ``.policy``
    import error (its imports are lazy precisely to dodge a circular-import
    hazard), re-importing here would re-raise and defeat the fail-closed
    guarantee — ``audit_run`` would raise instead of returning a denial, and
    the callers' outer handlers would then fail OPEN. ``EgressScope`` is a
    ``str`` enum, so compare against the literal value with no import.
    """
    if getattr(ctx, "scope", None) == "unprotected":
        return Decision(True, "unprotected")
    return Decision(False, "audit_error")


def audit_run(
    settings_snapshot,
    ctx,
    *,
    engines: Iterable[str],
    llm_provider: Optional[str] = None,
    embeddings_provider: Optional[str] = None,
) -> Decision:
    """Compute the four-quadrant decision for a run and LOG it.

    Never raises. On success returns the :class:`Decision`. If the decision
    cannot be computed it **fails closed** — returns a denying ``Decision``
    (reason ``audit_error``) so the run is refused and the failure is visible,
    rather than silently degrading to the scope PEPs. Under the ``UNPROTECTED``
    escape-hatch scope the decision is evaluated in :attr:`Mode.PERMISSIVE`
    (always allowed, diagnostic preserved), and an ``audit_error`` under that
    scope is likewise never blocking — the escape hatch must never refuse.
    """
    try:
        from .policy import EgressScope

        mode = (
            Mode.PERMISSIVE
            if getattr(ctx, "scope", None) == EgressScope.UNPROTECTED
            else Mode.ENFORCING
        )
        components = classify_run(
            settings_snapshot,
            ctx,
            engines=engines,
            llm_provider=llm_provider,
            embeddings_provider=embeddings_provider,
        )
        decision = evaluate_run(components, mode=mode)
        logger.bind(policy_audit=True).info(
            "egress two-axis audit",
            allowed=decision.allowed,
            reason=decision.reason,
            offending=decision.offending,
        )
        return decision
    except Exception:  # noqa: silent-exception - audit must never break a run
        logger.bind(policy_audit=True).warning(
            "egress two-axis audit could not be computed — failing closed",
            exc_info=True,
        )
        return _fail_closed_decision(ctx)


def audit_run_from_snapshot(
    settings_snapshot, ctx, primary_engine, llm_provider=None
):
    """Two-axis decision for a run described by its settings snapshot.

    Shared by the ``/api/start_research`` precheck and the worker chokepoint in
    ``run_research_process`` so every entry point — API, follow-up, chat, queue
    — evaluates the identical rule, then delegates to :func:`audit_run` (which
    fails closed to a denying decision when the rule cannot be computed).

    ``llm_provider`` is the run's per-request provider override (the
    ``model_provider`` kwarg / request field). It **wins** over the snapshot,
    mirroring ``get_llm`` (``config/llm_config.py``: an explicit provider is
    used, else the snapshot default). Passing only the snapshot would audit the
    stored default while the run builds the overridden provider — e.g. a saved
    local ``ollama`` audited as CONTAINED while the run actually calls a cloud
    provider chosen for that one run — admitting a leak. A **falsy** override
    (``None`` or ``""`` — chat passes ``""`` when no provider is set) means "no
    override": the provider is resolved from the snapshot, the SAME key/default
    the run uses. Using ``is None`` here would let ``""`` through and
    ``classify_run``'s truthy guard would then silently drop the LLM sink.

    The LLM and (RAG-only) embeddings providers are read with
    :func:`get_setting_from_snapshot` using the SAME keys/defaults the run uses,
    so the check never resolves to a different (safer) value than the run.
    Embeddings for a library / collection run come from
    ``local_search_embedding_provider`` (``search_engine_library.py``), NOT the
    unrelated ``embeddings.provider`` key.

    Resolution runs inside the same fail-closed guard as the decision itself.
    """
    try:
        from ...config.thread_settings import get_setting_from_snapshot

        if not llm_provider:
            # get_setting_from_snapshot only substitutes the default when the
            # key is ABSENT; a key present-but-empty returns "" again. Fall back
            # to the system default so the LLM sink is always classified and
            # never silently dropped by classify_run's truthy guard.
            llm_provider = (
                get_setting_from_snapshot(
                    "llm.provider",
                    "ollama",
                    settings_snapshot=settings_snapshot,
                )
                or "ollama"
            )
        # Embeddings only leave the box when the run actually embeds — RAG over
        # a collection / library. A lexical store or a web primary never embeds,
        # so a configured cloud embedder must not falsely trip the
        # sensitive->exposing check for those runs. Read the SAME key the RAG
        # engine uses (search_engine_library.py: local_search_embedding_provider);
        # embeddings.provider is a different, unused-here setting.
        embeddings_provider = None
        if primary_engine == "library" or (
            isinstance(primary_engine, str)
            and primary_engine.startswith("collection_")
        ):
            embeddings_provider = get_setting_from_snapshot(
                "local_search_embedding_provider",
                default="sentence_transformers",
                settings_snapshot=settings_snapshot,
            )
    except Exception:  # noqa: silent-exception - resolution must fail closed
        logger.bind(policy_audit=True).warning(
            "egress provider resolution failed — failing closed", exc_info=True
        )
        return _fail_closed_decision(ctx)
    return audit_run(
        settings_snapshot,
        ctx,
        engines=[primary_engine],
        llm_provider=llm_provider,
        embeddings_provider=embeddings_provider,
    )