feat: add privacy router and zero-cost safeguards
This commit is contained in:
@@ -13,6 +13,35 @@ class ConfigurationError(ValueError):
|
||||
"""Raised when runtime configuration is unsafe or invalid."""
|
||||
|
||||
|
||||
def _boolean(values: Mapping[str, str], name: str, default: bool) -> bool:
|
||||
raw = values.get(name)
|
||||
if raw is None:
|
||||
return default
|
||||
normalized = raw.strip().lower()
|
||||
if normalized in {"1", "true", "yes", "on"}:
|
||||
return True
|
||||
if normalized in {"0", "false", "no", "off"}:
|
||||
return False
|
||||
raise ConfigurationError(f"{name} muss true oder false sein.")
|
||||
|
||||
|
||||
def _positive_integer(
|
||||
values: Mapping[str, str],
|
||||
name: str,
|
||||
default: int,
|
||||
*,
|
||||
maximum: int,
|
||||
) -> int:
|
||||
raw = values.get(name, str(default))
|
||||
try:
|
||||
result = int(raw)
|
||||
except ValueError as exc:
|
||||
raise ConfigurationError(f"{name} muss eine ganze Zahl sein.") from exc
|
||||
if result <= 0 or result > maximum:
|
||||
raise ConfigurationError(f"{name} muss zwischen 1 und {maximum} liegen.")
|
||||
return result
|
||||
|
||||
|
||||
def default_data_dir(
|
||||
environ: Mapping[str, str] | None = None,
|
||||
*,
|
||||
@@ -42,6 +71,18 @@ class Settings:
|
||||
model: str = "qwen3:8b"
|
||||
ollama_base_url: str = "http://127.0.0.1:11434"
|
||||
timeout_seconds: float = 180.0
|
||||
provider_mode: str = "auto"
|
||||
gemini_enabled: bool = False
|
||||
gemini_model: str = "gemini-3.6-flash"
|
||||
gemini_optional_model: str = "gemini-3.5-flash-lite"
|
||||
free_only: bool = True
|
||||
billing_confirmed_disabled: bool = False
|
||||
max_cloud_requests_per_day: int = 25
|
||||
max_cloud_input_chars: int = 12_000
|
||||
max_cloud_output_tokens: int = 1_024
|
||||
gemini_timeout_seconds: float = 30.0
|
||||
gemini_max_retries: int = 1
|
||||
max_cloud_context_messages: int = 6
|
||||
|
||||
@property
|
||||
def database_path(self) -> Path:
|
||||
@@ -59,11 +100,20 @@ class Settings:
|
||||
model = values.get("JAVIS_MODEL", "qwen3:8b").strip()
|
||||
base_url = values.get("JAVIS_OLLAMA_URL", "http://127.0.0.1:11434").strip()
|
||||
timeout_raw = values.get("JAVIS_MODEL_TIMEOUT", "180")
|
||||
provider_mode = values.get("JAVIS_PROVIDER_MODE", "auto").strip().lower()
|
||||
gemini_enabled = _boolean(values, "JAVIS_GEMINI_ENABLED", False)
|
||||
free_only = _boolean(values, "JAVIS_FREE_ONLY", True)
|
||||
billing_disabled = _boolean(values, "JAVIS_GEMINI_BILLING_CONFIRMED_DISABLED", False)
|
||||
gemini_timeout_raw = values.get("JAVIS_GEMINI_TIMEOUT", "30")
|
||||
|
||||
try:
|
||||
timeout = float(timeout_raw)
|
||||
except ValueError as exc:
|
||||
raise ConfigurationError("JAVIS_MODEL_TIMEOUT muss eine Zahl sein.") from exc
|
||||
try:
|
||||
gemini_timeout = float(gemini_timeout_raw)
|
||||
except ValueError as exc:
|
||||
raise ConfigurationError("JAVIS_GEMINI_TIMEOUT muss eine Zahl sein.") from exc
|
||||
|
||||
settings = cls(
|
||||
data_dir=data_dir.resolve(),
|
||||
@@ -71,6 +121,29 @@ class Settings:
|
||||
model=model,
|
||||
ollama_base_url=base_url,
|
||||
timeout_seconds=timeout,
|
||||
provider_mode=provider_mode,
|
||||
gemini_enabled=gemini_enabled,
|
||||
gemini_model=values.get("JAVIS_GEMINI_MODEL", "gemini-3.6-flash").strip(),
|
||||
gemini_optional_model=values.get(
|
||||
"JAVIS_GEMINI_OPTIONAL_MODEL", "gemini-3.5-flash-lite"
|
||||
).strip(),
|
||||
free_only=free_only,
|
||||
billing_confirmed_disabled=billing_disabled,
|
||||
max_cloud_requests_per_day=_positive_integer(
|
||||
values, "JAVIS_MAX_CLOUD_REQUESTS_PER_DAY", 25, maximum=1_000
|
||||
),
|
||||
max_cloud_input_chars=_positive_integer(
|
||||
values, "JAVIS_MAX_CLOUD_INPUT_CHARS", 12_000, maximum=1_000_000
|
||||
),
|
||||
max_cloud_output_tokens=_positive_integer(
|
||||
values, "JAVIS_MAX_CLOUD_OUTPUT_TOKENS", 1_024, maximum=65_536
|
||||
),
|
||||
gemini_timeout_seconds=gemini_timeout,
|
||||
gemini_max_retries=_positive_integer(values, "JAVIS_GEMINI_MAX_ATTEMPTS", 2, maximum=2)
|
||||
- 1,
|
||||
max_cloud_context_messages=_positive_integer(
|
||||
values, "JAVIS_MAX_CLOUD_CONTEXT_MESSAGES", 6, maximum=50
|
||||
),
|
||||
)
|
||||
settings.validate()
|
||||
return settings
|
||||
@@ -84,6 +157,22 @@ class Settings:
|
||||
raise ConfigurationError("JAVIS_MODEL darf nicht leer sein.")
|
||||
if self.timeout_seconds <= 0:
|
||||
raise ConfigurationError("JAVIS_MODEL_TIMEOUT muss größer als 0 sein.")
|
||||
if self.provider_mode not in {"auto", "local", "gemini"}:
|
||||
raise ConfigurationError("JAVIS_PROVIDER_MODE muss auto, local oder gemini sein.")
|
||||
if not self.free_only:
|
||||
raise ConfigurationError(
|
||||
"Kostenpflichtiger Betrieb wird nicht unterstützt; JAVIS_FREE_ONLY muss true sein."
|
||||
)
|
||||
if self.gemini_model != "gemini-3.6-flash":
|
||||
raise ConfigurationError(
|
||||
"In diesem Paket ist nur das geprüfte Standardmodell gemini-3.6-flash aktiv."
|
||||
)
|
||||
if not self.gemini_optional_model:
|
||||
raise ConfigurationError("JAVIS_GEMINI_OPTIONAL_MODEL darf nicht leer sein.")
|
||||
if self.gemini_timeout_seconds <= 0 or self.gemini_timeout_seconds > 60:
|
||||
raise ConfigurationError(
|
||||
"JAVIS_GEMINI_TIMEOUT muss größer als 0 und höchstens 60 sein."
|
||||
)
|
||||
|
||||
parsed = urlparse(self.ollama_base_url)
|
||||
if parsed.scheme != "http" or parsed.hostname not in {
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
"""Metadata-only provider audit and local usage limits."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
from contextlib import closing
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, date, datetime
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class UsageStoreError(RuntimeError):
|
||||
"""Provider metadata could not be stored or read."""
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ProviderEvent:
|
||||
provider: str
|
||||
model: str
|
||||
success: bool
|
||||
error_category: str | None
|
||||
fallback: bool
|
||||
privacy_policy: str
|
||||
input_tokens: int | None = None
|
||||
output_tokens: int | None = None
|
||||
|
||||
|
||||
class SQLiteUsageStore:
|
||||
def __init__(self, database_path: Path) -> None:
|
||||
self.database_path = database_path
|
||||
try:
|
||||
self.database_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with closing(self._connect()) as connection:
|
||||
connection.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS provider_events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
occurred_at TEXT NOT NULL,
|
||||
provider TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
success INTEGER NOT NULL,
|
||||
error_category TEXT,
|
||||
fallback INTEGER NOT NULL,
|
||||
privacy_policy TEXT NOT NULL,
|
||||
input_tokens INTEGER,
|
||||
output_tokens INTEGER
|
||||
)
|
||||
"""
|
||||
)
|
||||
connection.execute(
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_provider_events_time_provider
|
||||
ON provider_events(occurred_at, provider)
|
||||
"""
|
||||
)
|
||||
connection.commit()
|
||||
except (OSError, sqlite3.Error) as exc:
|
||||
raise UsageStoreError(
|
||||
f"Die Nutzungsmetadatenbank kann nicht geöffnet werden: {database_path}"
|
||||
) from exc
|
||||
|
||||
def _connect(self) -> sqlite3.Connection:
|
||||
return sqlite3.connect(self.database_path, timeout=10)
|
||||
|
||||
def record(self, event: ProviderEvent) -> None:
|
||||
try:
|
||||
with closing(self._connect()) as connection:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO provider_events (
|
||||
occurred_at, provider, model, success, error_category,
|
||||
fallback, privacy_policy, input_tokens, output_tokens
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
datetime.now(UTC).isoformat(timespec="seconds"),
|
||||
event.provider,
|
||||
event.model,
|
||||
int(event.success),
|
||||
event.error_category,
|
||||
int(event.fallback),
|
||||
event.privacy_policy,
|
||||
event.input_tokens,
|
||||
event.output_tokens,
|
||||
),
|
||||
)
|
||||
connection.commit()
|
||||
except sqlite3.Error as exc:
|
||||
raise UsageStoreError("Nutzungsmetadaten konnten nicht gespeichert werden.") from exc
|
||||
|
||||
def cloud_requests_on(self, day: date | None = None) -> int:
|
||||
return self._count(day or datetime.now(UTC).date(), "provider = 'gemini'")
|
||||
|
||||
def local_fallbacks_on(self, day: date | None = None) -> int:
|
||||
return self._count(
|
||||
day or datetime.now(UTC).date(),
|
||||
"provider = 'ollama' AND fallback = 1",
|
||||
)
|
||||
|
||||
def _count(self, day: date, condition: str) -> int:
|
||||
start = f"{day.isoformat()}T00:00:00+00:00"
|
||||
end = f"{day.isoformat()}T23:59:59+00:00"
|
||||
try:
|
||||
with closing(self._connect()) as connection:
|
||||
row = connection.execute(
|
||||
f"""
|
||||
SELECT COUNT(*) FROM provider_events
|
||||
WHERE occurred_at BETWEEN ? AND ? AND {condition}
|
||||
""",
|
||||
(start, end),
|
||||
).fetchone()
|
||||
except sqlite3.Error as exc:
|
||||
raise UsageStoreError("Nutzungsmetadaten konnten nicht gelesen werden.") from exc
|
||||
return int(row[0])
|
||||
@@ -0,0 +1,151 @@
|
||||
"""Conservative local privacy classification for cloud routing."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from enum import StrEnum
|
||||
|
||||
from javis.providers.base import ChatMessage
|
||||
|
||||
|
||||
class CloudPolicy(StrEnum):
|
||||
ALLOWED = "allowed"
|
||||
ASK = "ask"
|
||||
NEVER = "never"
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PrivacyDecision:
|
||||
policy: CloudPolicy
|
||||
reason: str
|
||||
|
||||
|
||||
_NEVER_PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = (
|
||||
(
|
||||
re.compile(
|
||||
r"(?i)\b(api[-_ ]?key|passwort|password|access[-_ ]?token|refresh[-_ ]?token|"
|
||||
r"session[-_ ]?cookie|private[rn]? ssh[-_ ]?schl[üu]ssel)\b"
|
||||
),
|
||||
"mögliches Secret",
|
||||
),
|
||||
(
|
||||
re.compile(r"(?i)(AIza[\w-]{20,}|sk-[\w-]{16,}|BEGIN [A-Z ]*PRIVATE KEY)"),
|
||||
"Schlüsselmuster",
|
||||
),
|
||||
(
|
||||
re.compile(
|
||||
r"(?i)\b(iban|bic|kreditkart|bankkonto|bankdaten|zahlungsdaten|"
|
||||
r"kontonummer|pin[- ]?code)\b"
|
||||
),
|
||||
"Finanz- oder Zahlungsdaten",
|
||||
),
|
||||
(
|
||||
re.compile(
|
||||
r"(?i)\b(diagnose|gesundheit|krankheit|medikament|patient|arztbericht|"
|
||||
r"psychotherapie|blutwert)\b"
|
||||
),
|
||||
"Gesundheitsinformation",
|
||||
),
|
||||
(
|
||||
re.compile(
|
||||
r"(?i)\b(genaue anschrift|wohnanschrift|private[rs]? dokument|"
|
||||
r"vollständige[rs]? personenprofil|rohe? obsidian|nur lokal|"
|
||||
r"ausschließlich lokal)\b"
|
||||
),
|
||||
"ausdrücklich lokal oder besonders sensibel",
|
||||
),
|
||||
)
|
||||
|
||||
_ASK_PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = (
|
||||
(
|
||||
re.compile(
|
||||
r"(?i)\b(meine frau|mein mann|meine familie|mein kind|meine mutter|"
|
||||
r"mein vater|meine beziehung|über mich|über pascal)\b"
|
||||
),
|
||||
"persönliche oder familiäre Information",
|
||||
),
|
||||
(
|
||||
re.compile(r"(?i)\b[\w.+-]+@[\w.-]+\.[a-z]{2,}\b"),
|
||||
"E-Mail-Adresse",
|
||||
),
|
||||
(
|
||||
re.compile(r"(?<!\w)(?:\+?\d[\d ()/-]{7,}\d)(?!\w)"),
|
||||
"mögliche Telefonnummer",
|
||||
),
|
||||
(
|
||||
re.compile(
|
||||
r"(?i)\b(intern(?:e[rsn]?|er)? hostname|interne? domain|rootserver|"
|
||||
r"privates? projekt|nicht öffentlich|systeminformation|"
|
||||
r"private[rs]? termin)\b"
|
||||
),
|
||||
"nicht öffentliche Infrastruktur oder Planung",
|
||||
),
|
||||
(
|
||||
re.compile(r"(?i)\b(ich|mein(?:e[rsn]?|em)?|mir|mich)\b"),
|
||||
"möglicher persönlicher Bezug",
|
||||
),
|
||||
)
|
||||
|
||||
_ALLOWED_PATTERNS: tuple[re.Pattern[str], ...] = (
|
||||
re.compile(
|
||||
r"(?i)\b(python|programmier|quellcode|algorithmus|sqlite|linux|windows|"
|
||||
r"öffentliche dokumentation|allgemeine wissensfrage|was ist|wie funktioniert)\b"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class PrivacyRouter:
|
||||
"""Rule-based, entirely local and intentionally conservative."""
|
||||
|
||||
def classify(self, text: str, *, cloud_requested: bool = False) -> PrivacyDecision:
|
||||
normalized = text.strip()
|
||||
for pattern, reason in _NEVER_PATTERNS:
|
||||
if pattern.search(normalized):
|
||||
return PrivacyDecision(CloudPolicy.NEVER, reason)
|
||||
for pattern, reason in _ASK_PATTERNS:
|
||||
if pattern.search(normalized):
|
||||
return PrivacyDecision(CloudPolicy.ASK, reason)
|
||||
if any(pattern.search(normalized) for pattern in _ALLOWED_PATTERNS):
|
||||
return PrivacyDecision(CloudPolicy.ALLOWED, "allgemeiner oder technischer Inhalt")
|
||||
if cloud_requested:
|
||||
return PrivacyDecision(
|
||||
CloudPolicy.ASK,
|
||||
"expliziter Cloudwunsch bei uneindeutigem Inhalt",
|
||||
)
|
||||
return PrivacyDecision(CloudPolicy.ASK, "Inhalt nicht eindeutig klassifizierbar")
|
||||
|
||||
def minimal_context(
|
||||
self,
|
||||
messages: list[ChatMessage],
|
||||
*,
|
||||
current_policy: CloudPolicy,
|
||||
approved: bool,
|
||||
max_chars: int,
|
||||
max_messages: int,
|
||||
) -> list[ChatMessage]:
|
||||
if current_policy is CloudPolicy.NEVER:
|
||||
return []
|
||||
if current_policy is CloudPolicy.ASK and not approved:
|
||||
return []
|
||||
|
||||
selected: list[ChatMessage] = []
|
||||
remaining = max_chars
|
||||
for index in range(len(messages) - 1, max(-1, len(messages) - max_messages - 1), -1):
|
||||
message = messages[index]
|
||||
decision = self.classify(message.content)
|
||||
is_current = index == len(messages) - 1
|
||||
may_include = decision.policy is CloudPolicy.ALLOWED or (
|
||||
is_current and current_policy is CloudPolicy.ASK and approved
|
||||
)
|
||||
if not may_include:
|
||||
continue
|
||||
content = message.content[:remaining]
|
||||
if not content:
|
||||
break
|
||||
selected.append(ChatMessage(message.role, content))
|
||||
remaining -= len(content)
|
||||
if remaining <= 0:
|
||||
break
|
||||
selected.reverse()
|
||||
return selected
|
||||
Reference in New Issue
Block a user