662327e8b4
CI / nodejs-build (push) Successful in 2m47s
CI / branch-name (push) Has been skipped
CI / guardrail-integrity (push) Has been skipped
CI / detect-changes (push) Successful in 10s
CI / secret-scan (push) Has been skipped
CI / dep-audit (push) Has been skipped
CI / sbom-scan (push) Has been skipped
CI / validate-canonical-controls (push) Successful in 16s
CI / loc-budget (push) Failing after 17s
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-python-backend (push) Successful in 42s
CI / test-python-document-crawler (push) Has been skipped
CI / test-go (push) Has been skipped
CI / iace-gt-coverage (push) Has been skipped
CI / test-python-dsms-gateway (push) Has been skipped
Massiv-Update auf Basis BMW-Test-Iterationen (v1→v9): Core Compliance-Check - Sonnet check_type Klassifikation: text/process/review fuer alle 1874 MCs in compliance.doc_check_controls (script + Sidecar /data/mc_classification.db). rag_document_checker filtert auf check_type='text' fuer doc_check. Plus fits_doc_type-Audit (v2) + ui_only-Audit fuer DSA/E-Commerce-MCs in falscher doc_type-Schublade. - scope_requires-Filter: biometric/ai_decision/child_targeting MCs werden per business_profile gefiltert (FRT skipped fuer BMW etc.). - Embedding-Match (BGE-M3) als Phase-3 nach Regex-Match: Per-doc_type-Threshold-Override (impressum 0.50, dse/cookie 0.60), Short-Field-Rescue (15-Wort-Chunks) fuer Pflichtfelder im Impressum. Title+check_question als Embedding-Input fuer mehr Kontext. - Cookie-Text-Routing: consent-tester gibt cmp_cookie_text aus dem CMP-Reconstruct zurueck, Backend bevorzugt das gegen DOM-Extraction wenn richer (BMW 1824 vs 600 Worte). Vendor-Redundanz + EU-Alternativen + Cost-Saving - vendor_redundancy.analyze() — funktionale Kategorisierung der CMP-Vendors, Detektion von Mehrfach-Anbietern pro Kategorie, EU-Alternative-Lookup (Matomo, IONOS, HERE, Friendly Captcha, Smart AdServer, ...). - vendor_cost_estimator: Tier-Inferenz aus Cookie-Footprint (Cookie-Anzahl + Premium-Feature-Cookies + Third-Party-Quote → starter/professional/ enterprise/premier). - Self-Service-Werbung (Google/Meta/Pinterest/...) = 0 Lizenz-Kosten (nur Media-Spend, separat). DSP-Plattformen behalten enge Range. - Tier-aware Saving-Range: bei Enterprise/Premier nutzen wir den oberen 40-100%-Band der Listpreise, nicht starter→premier. - Multi-Function-Tools (Matomo Pro, SAP CX, IONOS Cloud, Userlike, Smart AdServer, HERE Maps, Vimeo Pro, LamaPoll) — ein Tool ersetzt mehrere Kategorien gleichzeitig. Cookie-Wissens-DB + Funktionale Klassifikation - cookie_knowledge_db: 50 kuratierte Top-Cookies (Google/Meta/Adobe/MS/...) mit vendor, exact_purpose, data_collected, IAB-TCF-IDs, reid_risk, schrems_ii_status, EuGH-Urteile, EU-Alternative. - cookie_function_classifier: pro Cookie funktionale Rolle (tracking_id, ad_pixel, session_id, ab_test, csrf, ...) + blocking_impact. Country-Inferenz aus Rechtsform - cookie_link_validator: Country-Field wird aus Vendor-Name abgeleitet (A/S=DK, GmbH=DE, Inc=US, B.V.=NL, ...) plus Vendor-Lookup-Table. Reduziert false-positive no_country-Flags bei eindeutig-EU-Vendors (Adform DK, Pinterest IE). Action-Recipes + Doc-Anchor-Locator - finding_action_recipes: pro Finding-Typ (no_cookies_listed, no_country, broken_opt_out, "Auftragsverarbeiter erwaehnen", "Art. 22 Profiling", ...) eine strukturierte Anweisung mit what/why/fix_text/where/example. Zum 1:1-Einfuegen in Kunden-Dokumente. - doc_anchor_locator: Embedding-basiert (BGE-M3 cosine) — sucht den passenden Absatz im existierenden Kundendokument fuer jeden Finding. Per-Run Thread-Local-Cache. Fallback: keyword-Match. - Email-Rendering integriert Recipe + Anchor pro Doc-Pruefungs-Fail + Vendor-Flag-Liste mit aufklappbarer Action-Liste. - Score-Erklaerung pro Vendor-Zeile (3/5-Untertitel + Tooltip). Migration-Pipeline (Compliance-Check -> Customer Banner/Documents) - migration_to_banner.py: Vendor-Liste -> CookieBannerConfig mit 4 Kategorien + Review-Flags. - migration_to_document.py: Vendor-Liste -> Cookie-Policy + VVT-Register + Privacy-Policy-Pre-Fills. - agent_migration_routes: 3 Preview-Endpoints (banner-preview, document-preview, summary). Persistierung der cmp_vendors in /data/compliance_audits.db check_payloads-Tabelle. Borlabs-Parity Cookie-Banner-Features - Consent-Historie im Banner: window.bpShowConsentHistory() + localStorage. - Content-Blocker: cookie-banner-content-blocker.ts — YouTube/Maps/Video Placeholder bis Einwilligung. - Google Consent Mode v2 erweitert: wait_for_update + region=EEA/CH/GB. - Consent-Log Export (CSV/JSON) per einwilligungen_export_routes. Bug-Fixes - canonical_control_routes: _jsonish-Helper fuer string-typed jsonb, similar-controls-Endpoint mit _has_embedding_col()-Cache (kein 500 mehr). - Control-Library Frontend: defensive .map-Coercer in 2 Detail-Views. - Embedding-Service-Batching (32er Batches statt 165 in einem Call). - KeyError 'control_id' in MC-Result-Aggregation (defensive .get). - Master-Controls-Klick-Through von /sdk/master-controls auf /sdk/control-library?control=<id> mit URL-Param-Auto-Open. - Dockerfile: /data pre-chowned auf appuser (Audit-DB-Schreibrecht). - Cookie-Text-Routing-Bug (cmp_reconstructed > DOM-extraction). - doc_type-aware MC-Filter (statt all-text-MCs). - Master-Contract-Dedup (60 BMW-Internal-Eintraege = 1 Adobe-Vertrag). - A3-v2-Audit hat 24 UI-Sprache-MCs als 'process' reklassifiziert. Tests - test_migration_mappers.py (9 Tests) - test_migration_endpoints.py (4 Tests) Skripte (one-shot) - classify_mc_check_type.py (v1) + _v2 (PK=control_id,doc_type) - audit_mc_doctype_fit.py (v1 fits) + _v2 (ui_only + scope_requires) BMW-Run-Bilanz v1 (broken) -> v9 (alle Fixes): DSE 7,5% -> 81-83% Impressum 4% -> 100% (6 echte MCs alle erfuellt) Cookie 0% -> 79-83% (CMP-Text-Routing + Embedding) Plus: 10 Konsolidierungs-Kategorien, geschaetzte Saving 200k-3M / Jahr Plus: Action-Recipes + Doc-Anchors fuer jeden Fail Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
351 lines
12 KiB
Python
351 lines
12 KiB
Python
"""
|
|
Doc-Anchor-Locator — fuer ein Finding den passendsten Einfuege-Ort im
|
|
existierenden Dokument finden.
|
|
|
|
Primary strategy: BGE-M3 Embedding-Match zwischen einer pro-Finding
|
|
Anchor-Query und allen Absaetzen des Docs. Echtes semantisches Matching
|
|
(BMW schreibt "Verarbeiter" statt "Auftragsverarbeiter" → Keyword waere
|
|
out, Embedding catches it).
|
|
|
|
Fallback: Keyword-Match (wenn kein Embedding-Service erreichbar).
|
|
|
|
Output pro Anchor:
|
|
- anchor_phrase : Originaltext-Auszug
|
|
- position_hint : "Nach Absatz X von Y: '...'"
|
|
- confidence : 'high' | 'medium' | 'low'
|
|
- score : float (cosine similarity oder keyword-rank)
|
|
- method : 'embedding' | 'keyword' | 'fallback'
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import math
|
|
import os
|
|
import re
|
|
import threading
|
|
from typing import Iterable
|
|
|
|
import httpx
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
EMBEDDING_URL = os.getenv("EMBEDDING_URL", "http://embedding-service:8087")
|
|
|
|
# Pro Finding-Typ eine semantisch reiche Anchor-Query, die der Embedding-
|
|
# Matcher gegen den Doc-Text wirft. Reicher als die kurze MC-check_question.
|
|
# Sucht NICHT den Pflichttext selbst (der fehlt ja) sondern den Absatz wo
|
|
# der Fix HINEIN-soll — also den thematisch verwandten Kontext.
|
|
_ANCHOR_QUERIES: list[tuple[str, str, str]] = [
|
|
# (finding_label_partial, anchor_query, fallback_hint)
|
|
(
|
|
"Auftragsverarbeiter erwaehnt",
|
|
"Empfaenger der Daten Verarbeiter Dienstleister Cloud Hosting CRM "
|
|
"Auftragsverarbeitung Weitergabe Datenuebermittlung an Dritte",
|
|
"Im Abschnitt 'Empfaenger' oder 'Datenuebermittlung'",
|
|
),
|
|
(
|
|
"Automatisierte Entscheidungen",
|
|
"Betroffenenrechte automatisierte Entscheidung Profiling Logik "
|
|
"Tragweite Auswirkung Art. 22 DSGVO",
|
|
"Am Ende des Abschnitts 'Betroffenenrechte'",
|
|
),
|
|
(
|
|
"Konkrete Aufsichtsbehoerde",
|
|
"Beschwerderecht Datenschutzaufsicht Aufsichtsbehoerde Beschwerde "
|
|
"bei der Behoerde einreichen Recht auf Beschwerde",
|
|
"Im Abschnitt 'Beschwerderecht'",
|
|
),
|
|
(
|
|
"Angemessenheitsbeschluss",
|
|
"Drittlandtransfer USA Standardvertragsklauseln SCC DPF Data Privacy "
|
|
"Framework Angemessenheitsbeschluss internationale Datenuebermittlung",
|
|
"Im Abschnitt 'Drittlandtransfer'",
|
|
),
|
|
(
|
|
"Anschrift des Verantwortlichen",
|
|
"Verantwortlicher Verantwortliche Stelle Datenschutz Betreiber dieser "
|
|
"Website Firma Anschrift Kontakt",
|
|
"Am Anfang der Datenschutzerklaerung / Cookie-Richtlinie",
|
|
),
|
|
(
|
|
"Konkrete Cookie-Namen",
|
|
"Welche Cookies verwenden wir Cookie-Tabelle Liste der Cookies "
|
|
"Cookie-Kategorien Auflistung der Cookies Name Anbieter Zweck",
|
|
"Im Abschnitt 'Welche Cookies verwenden wir?'",
|
|
),
|
|
(
|
|
"Konkrete Anbieter/Dienste",
|
|
"Drittanbieter Dienste Anbieter wir nutzen folgende Dienste "
|
|
"Empfaenger der Cookie-Daten Liste der Dienstleister",
|
|
"In der Drittanbieter-Liste der Cookie-Richtlinie",
|
|
),
|
|
(
|
|
"Analytics-/Statistik-Tools konkret benannt",
|
|
"Statistik Analytics Reichweitenmessung Webanalyse Tracking "
|
|
"Google Analytics Matomo Adobe Analytics",
|
|
"Im Abschnitt 'Statistik / Analyse-Cookies'",
|
|
),
|
|
(
|
|
"Konkrete Speicherdauer",
|
|
"Speicherdauer Lebensdauer wie lange Ablauf Cookie-Tabelle Spalte "
|
|
"Speicherdauer pro Cookie",
|
|
"In der Cookie-Tabelle pro Eintrag",
|
|
),
|
|
(
|
|
"Opt-Out-Links",
|
|
"Widerruf widersprechen deaktivieren Cookie-Einstellungen aendern "
|
|
"Opt-Out Einstellungen anpassen",
|
|
"Im Abschnitt 'Wie kann ich widersprechen?'",
|
|
),
|
|
(
|
|
"Privacy-Policy-Links",
|
|
"Datenschutzerklaerung des Drittanbieters Privacy Policy Link auf "
|
|
"Datenschutzhinweise der Drittanbieter",
|
|
"Im Drittanbieter-Listing der Cookie-Richtlinie",
|
|
),
|
|
(
|
|
"Verbraucherstreitbeilegung",
|
|
"Online-Streitbeilegung OS-Plattform Verbraucherschlichtungsstelle "
|
|
"Streitbeilegung Verbraucher",
|
|
"Am Ende des Impressums (eigener Abschnitt 'Streitbeilegung')",
|
|
),
|
|
(
|
|
"Rechtswidriger Haftungsausschluss",
|
|
"Haftung Disclaimer wir distanzieren uns Links zu externen Webseiten "
|
|
"Haftungsausschluss Drittinhalte",
|
|
"Am Ende des Impressums (Disclaimer-Absatz)",
|
|
),
|
|
(
|
|
"Name der vertretungsberechtigten",
|
|
"Vertreten durch Vorstand Geschaeftsfuehrung Geschaeftsfuehrer "
|
|
"vertretungsberechtigt Repraesentant",
|
|
"Im Impressum nach Firmenname + Anschrift",
|
|
),
|
|
(
|
|
"Zustaendige Kammer",
|
|
"Berufsrecht Kammer Aufsichtsbehoerde berufsrechtliche Angaben "
|
|
"zustaendige Kammer",
|
|
"Im Impressum im Abschnitt 'Berufsrechtliche Angaben'",
|
|
),
|
|
(
|
|
"Drittlaender",
|
|
"Drittland Drittlaender USA Indien China internationale Datenuebermittlung "
|
|
"Datenexport in Nicht-EU-Staaten",
|
|
"Im Abschnitt 'Drittlandtransfer'",
|
|
),
|
|
(
|
|
"Schutzgarantien",
|
|
"Schutzgarantien Schutzvorkehrungen Schutzmassnahmen SCC "
|
|
"Standardvertragsklauseln einsehen Anforderung",
|
|
"Im Abschnitt 'Drittlandtransfer / Schutzgarantien'",
|
|
),
|
|
]
|
|
|
|
|
|
# ─── Thread-local Cache fuer Doc-Chunks + Embeddings ───────────────
|
|
# Pro Compliance-Check-Run werden die Doc-Texte einmal embedded und im
|
|
# Thread-Local-Storage gehalten, damit mehrere Findings im selben Run
|
|
# nicht jeweils neu embedded werden.
|
|
|
|
_tls = threading.local()
|
|
|
|
|
|
def _get_cache() -> dict:
|
|
if not hasattr(_tls, "cache"):
|
|
_tls.cache = {}
|
|
return _tls.cache
|
|
|
|
|
|
def reset_cache() -> None:
|
|
"""Per-request-cache leeren (sollte am Start jedes Doc-Check-Runs aufgerufen
|
|
werden, damit Vorgaenger-Daten kein Leak verursachen)."""
|
|
if hasattr(_tls, "cache"):
|
|
_tls.cache = {}
|
|
|
|
|
|
# ─── Helfer ────────────────────────────────────────────────────────
|
|
|
|
def _normalize(text: str) -> str:
|
|
return (text or "").lower().replace("\xad", "").replace("ß", "ss")
|
|
|
|
|
|
def _split_paragraphs(text: str) -> list[str]:
|
|
"""Split a doc into paragraphs (by double newline, fallback single)."""
|
|
if not text:
|
|
return []
|
|
paras = re.split(r"\n\s*\n", text)
|
|
if len(paras) < 3:
|
|
paras = re.split(r"(?<=[\.\?\!])\s+(?=[A-ZÄÖÜ])", text)
|
|
return [p.strip() for p in paras if p.strip()]
|
|
|
|
|
|
def _embed_sync(texts: list[str], timeout: float = 60.0,
|
|
batch_size: int = 32) -> list[list[float]]:
|
|
"""Synchroner Batch-Embed-Call (Anchor-Lokalisierung laeuft in
|
|
Sync-HTML-Render, nicht in async context)."""
|
|
if not texts:
|
|
return []
|
|
out: list[list[float]] = []
|
|
with httpx.Client(timeout=timeout) as client:
|
|
for i in range(0, len(texts), batch_size):
|
|
batch = texts[i:i + batch_size]
|
|
try:
|
|
r = client.post(f"{EMBEDDING_URL}/embed", json={"texts": batch})
|
|
r.raise_for_status()
|
|
out.extend(r.json().get("embeddings") or [])
|
|
except Exception as e:
|
|
logger.warning("Anchor embed sub-batch [%d-%d] failed: %s",
|
|
i, i + len(batch), e)
|
|
out.extend([[] for _ in batch])
|
|
return out
|
|
|
|
|
|
def _cosine(a: list[float], b: list[float]) -> float:
|
|
if not a or not b or len(a) != len(b):
|
|
return 0.0
|
|
dot = sum(x * y for x, y in zip(a, b))
|
|
na = math.sqrt(sum(x * x for x in a))
|
|
nb = math.sqrt(sum(y * y for y in b))
|
|
if na == 0 or nb == 0:
|
|
return 0.0
|
|
return dot / (na * nb)
|
|
|
|
|
|
def _doc_paragraphs_and_vectors(
|
|
doc_id: str, doc_text: str,
|
|
) -> tuple[list[str], list[list[float]]]:
|
|
"""Lazy-cache: Absaetze + Vektoren pro Doc-ID. Wird genau einmal pro
|
|
Doc und Run berechnet."""
|
|
cache = _get_cache()
|
|
if doc_id in cache:
|
|
return cache[doc_id]
|
|
|
|
paras = _split_paragraphs(doc_text)
|
|
if not paras:
|
|
cache[doc_id] = ([], [])
|
|
return cache[doc_id]
|
|
|
|
vecs = _embed_sync(paras)
|
|
cache[doc_id] = (paras, vecs)
|
|
return cache[doc_id]
|
|
|
|
|
|
def _keyword_fallback(fl: str, doc_text: str) -> dict | None:
|
|
"""Fallback wenn Embedding-Service ausfaellt oder zu wenig Trefferqualitaet."""
|
|
# Use the old _ANCHOR_QUERIES list — extract just the fallback hint
|
|
for label_partial, _query, fallback_hint in _ANCHOR_QUERIES:
|
|
if _normalize(label_partial) in fl:
|
|
return {
|
|
"anchor_phrase": None,
|
|
"position_hint": fallback_hint,
|
|
"confidence": "low",
|
|
"method": "fallback",
|
|
}
|
|
return None
|
|
|
|
|
|
def locate_anchor(
|
|
finding_label: str,
|
|
doc_text: str,
|
|
doc_id: str | None = None,
|
|
) -> dict | None:
|
|
"""Fuer ein Finding den passendsten Anker-Absatz im Doc-Text finden.
|
|
|
|
Primary: Embedding-Match. Sekundaer: Keyword-Hits als Bonus. Fallback:
|
|
rein keyword-basiert wenn Embedding-Service nicht erreichbar ist.
|
|
|
|
`doc_id` ist ein cache-key (z.B. doc_type oder url). Wenn None, wird
|
|
aus dem doc_text-Hash abgeleitet.
|
|
"""
|
|
if not doc_text or not finding_label:
|
|
return None
|
|
|
|
fl = _normalize(finding_label)
|
|
|
|
# Welche Anchor-Query matched dieses Finding?
|
|
query = None
|
|
fallback_hint = None
|
|
matched_label = None
|
|
for label_partial, q, fb in _ANCHOR_QUERIES:
|
|
if _normalize(label_partial) in fl:
|
|
query, fallback_hint, matched_label = q, fb, label_partial
|
|
break
|
|
if not query:
|
|
return None
|
|
|
|
doc_id = doc_id or f"doc-{hash(doc_text) & 0xffffffff:08x}"
|
|
|
|
# 1) Embedding-Match
|
|
paras, doc_vecs = _doc_paragraphs_and_vectors(doc_id, doc_text)
|
|
if not paras:
|
|
return None
|
|
|
|
embeddings_available = any(v for v in doc_vecs)
|
|
if not embeddings_available:
|
|
return _keyword_fallback(fl, doc_text)
|
|
|
|
try:
|
|
q_vec = _embed_sync([query])[0] if query else None
|
|
except Exception:
|
|
q_vec = None
|
|
|
|
if not q_vec:
|
|
return _keyword_fallback(fl, doc_text)
|
|
|
|
# Per-Absatz Score = cosine + Heading-Bonus
|
|
best_idx = -1
|
|
best_score = 0.0
|
|
for i, (p, dv) in enumerate(zip(paras, doc_vecs)):
|
|
if not dv:
|
|
continue
|
|
sim = _cosine(q_vec, dv)
|
|
# Heading-Bonus: kurze Absaetze + Markdown-Heading-Marker
|
|
if len(p.split()) <= 8 or p.strip().startswith("#"):
|
|
sim += 0.05
|
|
if sim > best_score:
|
|
best_score = sim
|
|
best_idx = i
|
|
|
|
# Konfidenz-Schwellen — kalibriert anhand BMW-Run
|
|
if best_idx < 0 or best_score < 0.40:
|
|
# Zu schwacher Match — Fallback verwenden
|
|
return {
|
|
"anchor_phrase": None,
|
|
"position_hint": fallback_hint,
|
|
"confidence": "low",
|
|
"score": round(best_score, 3) if best_idx >= 0 else 0,
|
|
"method": "embedding-no-match",
|
|
}
|
|
|
|
if best_score >= 0.62:
|
|
confidence = "high"
|
|
elif best_score >= 0.50:
|
|
confidence = "medium"
|
|
else:
|
|
confidence = "low"
|
|
|
|
anchor = paras[best_idx]
|
|
words = anchor.split()
|
|
snippet = " ".join(words[:30]) + ("…" if len(words) > 30 else "")
|
|
return {
|
|
"anchor_phrase": snippet,
|
|
"anchor_index": best_idx,
|
|
"total_paragraphs": len(paras),
|
|
"position_hint": f"Nach Absatz {best_idx + 1} von {len(paras)}: '{snippet}'",
|
|
"confidence": confidence,
|
|
"score": round(best_score, 3),
|
|
"method": "embedding",
|
|
}
|
|
|
|
|
|
def annotate_findings_with_anchors(
|
|
findings: Iterable[dict], doc_text: str, doc_id: str | None = None,
|
|
) -> list[dict]:
|
|
"""Pro Finding den Anchor suchen und das Dict um 'anchor' erweitern."""
|
|
out = []
|
|
for f in findings:
|
|
a = locate_anchor(f.get("label") or f.get("title") or "", doc_text, doc_id)
|
|
out.append({**f, "anchor": a})
|
|
return out
|