~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Two-axis egress data classification — ADR-0007, rollout stage A.
Pure decision logic. Each model a run touches (search engine, collection /
store, LLM, embeddings) is classified on two **orthogonal** axes:
* **Sensitivity** — of the data the component *sources*: must it never leave the
machine? (``SENSITIVE`` / ``NON_SENSITIVE``)
* **Exposure** — of the *sink* the component represents: does sending data to it
put that data off-machine? (``EXPOSING`` / ``CONTAINED``)
:func:`evaluate_run` decides whether a *set* of classified components may run
together, per the four-quadrant rule in ADR-0007. The core invariant: **a run
may not let a SENSITIVE source reach an EXPOSING sink.** Because an agentic run
can turn one engine's results into another engine's query (the LangGraph
silent-expansion bug class already in our threat model), the rule is evaluated
over the whole component set, not per individual call.
This module holds the pure decision logic. It is consumed via
``run_classification``, which resolves live engines/providers, assembles a run,
and — at the run-start precheck — **enforces** the decision (rejecting a run
that would let a sensitive source reach an exposing sink), except under the
``UNPROTECTED`` escape hatch where it evaluates permissive. See
``docs/decisions/0007-egress-two-axis-data-classification.md``.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from enum import Enum
from typing import Iterable
class Sensitivity(Enum):
"""Whether the data a component *sources* may leave the machine."""
NON_SENSITIVE = "non_sensitive"
SENSITIVE = "sensitive"
class Exposure(Enum):
"""Whether sending data to a component (as a sink) puts it off-machine."""
CONTAINED = "contained"
EXPOSING = "exposing"
class Role(Enum):
"""How a component participates in a run's data flow.
A single real component may take more than one role — a search engine is a
``SEARCH_SINK`` for the query *and* a ``SOURCE`` of results; Elasticsearch
is a ``SOURCE`` of data *and* a ``SEARCH_SINK`` for the query you send it.
Such a component is passed to :func:`evaluate_run` once per role it plays,
**using the same** :attr:`Component.name` each time — the self-exclusion in
quadrant 4 (a dual-risk store used solo) relies on that shared identity.
"""
SOURCE = "source" # contributes data into the run
SEARCH_SINK = "search_sink" # receives the query (+ maybe expanded results)
INFERENCE_SINK = "inference_sink" # LLM / embeddings — sees all source data
class Mode(Enum):
"""Enforcement mode — SELinux vocabulary (ADR-0007)."""
ENFORCING = "enforcing" # the invariant is enforced; violations blocked
PERMISSIVE = "permissive" # evaluated for warnings, never blocks
@dataclass(frozen=True)
class Label:
"""A component's position on the two axes."""
sensitivity: Sensitivity
exposure: Exposure
@dataclass(frozen=True)
class Component:
"""A classified participant in a run.
``name`` is a stable identifier (engine name, ``collection_<uuid>``,
``llm:<provider>``, ``embeddings:<provider>``) used for self-exclusion and
reporting.
"""
name: str
role: Role
label: Label
@dataclass(frozen=True)
class Decision:
"""Outcome of :func:`evaluate_run`."""
allowed: bool
reason: str
offending: tuple[str, ...] = field(default_factory=tuple)
def evaluate_run(
components: Iterable[Component], *, mode: Mode = Mode.ENFORCING
) -> Decision:
"""Decide whether a set of classified components may run together.
Implements the four-quadrant rule of ADR-0007:
1. ``non-sensitive, contained`` → combines with anything.
2. ``sensitive, contained`` → only with other contained sources.
3. ``non-sensitive, exposing`` → only with non-sensitive sources.
4. ``sensitive + exposing`` → solo, with contained inference.
The rule assumes the caller passes each real engine as BOTH a ``SOURCE``
(carrying its sensitivity) and a ``SEARCH_SINK`` (carrying its exposure)
under one shared ``name`` — the quadrant-4 self-exclusion depends on that
pairing (see ``run_classification.classify_run``).
Under :attr:`Mode.PERMISSIVE` the run is always allowed, but the underlying
would-be-enforced ``reason`` / ``offending`` are preserved (prefixed
``permissive:``) so the caller can still surface a warning banner.
"""
components = tuple(components)
decision = _decide_enforcing(components)
if mode is Mode.PERMISSIVE and not decision.allowed:
return Decision(
True, f"permissive:{decision.reason}", offending=decision.offending
)
return decision
def _decide_enforcing(components: tuple[Component, ...]) -> Decision:
"""The enforcing four-quadrant decision (mode-agnostic)."""
sensitive_names = {
c.name
for c in components
if c.role is Role.SOURCE
and c.label.sensitivity is Sensitivity.SENSITIVE
}
# No sensitive data in the run → nothing to protect; any sink is fine.
if not sensitive_names:
return Decision(True, "no_sensitive_source")
# (1) Sensitive content always reaches the inference sink (the LLM/embeddings
# see every source's data), so an exposing inference sink leaks it.
exposing_inference = tuple(
sorted(
c.name
for c in components
if c.role is Role.INFERENCE_SINK
and c.label.exposure is Exposure.EXPOSING
)
)
if exposing_inference:
return Decision(
False,
"sensitive_to_exposing_inference",
offending=exposing_inference,
)
# (2) An exposing search sink may coexist with sensitive data ONLY if it is
# itself the sole sensitive source (quadrant 4, solo): returning its own
# data to itself is not a new leak. Any *other* sensitive source could be
# agentically expanded into a query to that sink → leak. The sink must
# ALSO be sensitive itself, so a mere name collision between a
# non-sensitive exposing sink and an unrelated sensitive source cannot
# be mistaken for the solo dual-risk case.
offending_search = tuple(
sorted(
{
sink.name
for sink in components
if sink.role is Role.SEARCH_SINK
and sink.label.exposure is Exposure.EXPOSING
and (
(sensitive_names - {sink.name})
or sink.label.sensitivity is not Sensitivity.SENSITIVE
)
}
)
)
if offending_search:
return Decision(
False, "sensitive_to_exposing_search", offending=offending_search
)
return Decision(True, "sensitive_contained")