MVP 0.93 - stabile, aber Writing Profile ohne erkennbaren Nutzen
This commit is contained in:
1 parent
53b1eff833
commit
f17b3083e8
56 files changed
+5366
-784
No files matched your search
@@ -1,10 +1,17 @@
|
||||
"""Detection + mask + demask. Run from backend/: python tests/test_privacy_detect.py"""
|
||||
"""Semantic detection contracts. Fake provider proves schema and fail-closed, not live quality.
|
||||
|
||||
Run from backend/: python tests/test_privacy_detect.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
@@ -15,9 +22,38 @@ os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from identity_store import list_mappings
|
||||
from identity_store import (
|
||||
STATUS_LEGACY,
|
||||
confirm_identity,
|
||||
list_confirmed_identities,
|
||||
list_mappings,
|
||||
list_registry,
|
||||
list_review_proposals,
|
||||
remember_mapping,
|
||||
)
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
from privacy_gateway import (
|
||||
GatewayRequest,
|
||||
PrivacyGatewayError,
|
||||
_demask,
|
||||
_validate_response,
|
||||
complete,
|
||||
mask_prompt,
|
||||
reset_debug,
|
||||
)
|
||||
from entity_detect import (
|
||||
DETECT_CHUNK_CHARS,
|
||||
DetectError,
|
||||
detect_personal_egress,
|
||||
install_test_detect_failure,
|
||||
install_test_spans,
|
||||
install_test_truncated,
|
||||
reset_detect_test_hooks,
|
||||
split_detect_chunks,
|
||||
uses_llm_detect,
|
||||
validate_detected_entity,
|
||||
)
|
||||
from providers import ProviderConfig
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
@@ -26,6 +62,18 @@ def expect(ok: bool, message: str) -> None:
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def _run(profile_id: str, rendered: str, purpose: str = "dialogue_turn"):
|
||||
return complete(
|
||||
GatewayRequest(
|
||||
prompt_id="detect-test",
|
||||
purpose=purpose,
|
||||
data_class="B",
|
||||
profile_id=profile_id,
|
||||
payload={"rendered": rendered, "source_text": rendered},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
@@ -34,6 +82,7 @@ def main() -> None:
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
headers = {"X-Auth-Token": setup.json()["token"]}
|
||||
profile_id = setup.json()["profile_id"]
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Alltag"})
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
@@ -45,111 +94,315 @@ def main() -> None:
|
||||
headers=headers,
|
||||
json={"title": "Gespräch"},
|
||||
)
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Ich war mit Anna in Hamburg und danach allein."},
|
||||
|
||||
balcony = _run(profile_id, "Ich ging auf den Balkon und setzte mich.")
|
||||
expect(balcony.allowed, "balcony sentence is allowed")
|
||||
expect("Balkon" in (balcony.trace.get("egress") or ""), "capitalized common noun stays unmasked")
|
||||
expect("[[PERSON:" not in (balcony.trace.get("egress") or ""), "balcony is not a person placeholder")
|
||||
|
||||
typo = _run(profile_id, "Ich ging auf den Bakon und setzte mich.")
|
||||
expect("Bakon" in (typo.trace.get("egress") or ""), "misspelled common noun is not stored as a person")
|
||||
expect(
|
||||
not any((item.get("canonical_label") or "") == "Bakon" for item in list_confirmed_identities(profile_id)),
|
||||
"misspelled noun is not a confirmed identity",
|
||||
)
|
||||
expect(turn.status_code == 200, f"turn {turn.text}")
|
||||
trace = turn.json().get("trace") or {}
|
||||
expect(trace, "gateway returned request-scoped trace")
|
||||
expect(trace["detect_provider"] == "fake-detect", "detect provider is separate")
|
||||
expect("Anna" not in (trace.get("egress") or ""), "Anna masked before generate")
|
||||
expect("Hamburg" not in (trace.get("egress") or ""), "Hamburg masked before generate")
|
||||
expect("[[PERSON:" in (trace.get("egress") or "") or "[[PLACE:" in (trace.get("egress") or ""), "placeholders in egress")
|
||||
expect("Anna" in turn.json()["user"]["body"], "source keeps cleartext")
|
||||
mappings = list_mappings(setup.json()["profile_id"])
|
||||
labels = {item["local_label"] for item in mappings}
|
||||
expect("Anna" in labels, "Anna stored in local dictionary")
|
||||
expect("Hamburg" in labels, "Hamburg stored in local dictionary")
|
||||
expect(all(not (item.get("token") or "").startswith("[[") or True for item in mappings), "tokens stored")
|
||||
|
||||
reset_debug()
|
||||
again = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Anna war ruhig."},
|
||||
food = _run(profile_id, "Ich aß Sushi auf dem Teller.")
|
||||
expect("Sushi" in (food.trace.get("egress") or ""), "food context keeps the word")
|
||||
person_sushi = _run(profile_id, "Sushi kam ins Wohnzimmer.")
|
||||
expect("[[PERSON:" in (person_sushi.trace.get("egress") or ""), "same word can be a person in another sentence")
|
||||
expect("Sushi" not in (person_sushi.trace.get("egress") or "").replace("[[PERSON:", ""), "person Sushi is masked")
|
||||
|
||||
mixed = _run(
|
||||
profile_id,
|
||||
"Ich traf Anna. Ich arbeitete am privaten Projekt Aurora. "
|
||||
"Die Organisation Nordwerk schickte Unterlagen. Ich war in Hamburg.",
|
||||
)
|
||||
expect(again.status_code == 200, "second turn")
|
||||
last2 = again.json().get("trace") or {}
|
||||
egress = mixed.trace.get("egress") or ""
|
||||
expect("[[PERSON:" in egress, "person is typed as PERSON")
|
||||
expect("[[PROJECT:" in egress, "private project is typed as PROJECT")
|
||||
expect("[[ORG:" in egress, "organisation is typed as ORG")
|
||||
expect("[[PLACE:" in egress, "named city is typed as PLACE")
|
||||
|
||||
expect("nicht" in (last2.get("egress") or "").lower() or "Anna" not in (last2.get("egress") or ""), "dictionary remasks Anna without new invention")
|
||||
expect(last2.get("mapping_count", 0) >= 2, "dictionary reused")
|
||||
sample = "Ich traf Anna."
|
||||
start = sample.index("Anna")
|
||||
span = validate_detected_entity(
|
||||
{"start": start, "end": start + 4, "text": "Anna", "entity_type": "PERSON"},
|
||||
sample,
|
||||
0,
|
||||
)
|
||||
expect(span.text == "Anna" and span.start == start, "valid span matches the local substring")
|
||||
relocated = validate_detected_entity(
|
||||
{"start": 0, "end": 4, "text": "Anna", "entity_type": "PERSON"},
|
||||
sample,
|
||||
0,
|
||||
)
|
||||
expect(relocated is not None and relocated.start == start and relocated.text == "Anna", "wrong offsets are grounded to the local word")
|
||||
expect(
|
||||
validate_detected_entity(
|
||||
{"start": -1, "end": 4, "text": "Anna", "entity_type": "PERSON"},
|
||||
sample,
|
||||
0,
|
||||
).start
|
||||
== start,
|
||||
"negative offsets still ground when the word exists",
|
||||
)
|
||||
expect(
|
||||
validate_detected_entity(
|
||||
{"start": 0, "end": 4, "text": "Nein", "entity_type": "PERSON"},
|
||||
sample,
|
||||
0,
|
||||
)
|
||||
is None,
|
||||
"a word that is not in the chunk is skipped",
|
||||
)
|
||||
expect(
|
||||
validate_detected_entity(
|
||||
{"start": "x", "end": 4, "text": "Anna", "entity_type": "PERSON"},
|
||||
sample,
|
||||
0,
|
||||
).start
|
||||
== start,
|
||||
"non-numeric offsets still ground when the word exists",
|
||||
)
|
||||
rejected = []
|
||||
for payload, label in (
|
||||
({"start": start, "end": start + 4, "text": "Anna", "entity_type": "PET"}, "unknown type"),
|
||||
({"start": start, "end": start + 4, "text": "Anna", "entity_type": "PERSON", "token": "PERSON:01"}, "detector token"),
|
||||
({"start": start, "end": start + 4, "text": "Anna", "entity_type": "PERSON", "score": 1}, "extra field"),
|
||||
):
|
||||
try:
|
||||
validate_detected_entity(payload, sample, 0)
|
||||
except DetectError:
|
||||
rejected.append(label)
|
||||
expect(rejected == ["unknown type", "detector token", "extra field"], "invalid detector output is rejected")
|
||||
|
||||
from entity_detect import _pattern_entities
|
||||
from identity_store import is_maskable_label
|
||||
from privacy_gateway import _mask
|
||||
first = _run(profile_id, "Ich war mit Anna in Hamburg und danach allein.")
|
||||
expect(first.trace.get("detect_provider") == "fake-detect", "detect provider is separate")
|
||||
expect("Anna" not in (first.trace.get("egress") or ""), "Anna masked before generate")
|
||||
expect("Hamburg" not in (first.trace.get("egress") or ""), "Hamburg masked before generate")
|
||||
expect(first.trace.get("full_detection_coverage") is True, "successful detect records full coverage")
|
||||
expect(first.trace.get("generate_called") is True, "generate runs after complete detection")
|
||||
expect(first.diagnostics.get("detect_calls") == 1, "normal short text uses one detect call")
|
||||
labels_after = {item["canonical_label"] for item in list_confirmed_identities(profile_id)}
|
||||
expect("Anna" not in labels_after, "detect hit is not auto-confirmed")
|
||||
expect("Hamburg" not in labels_after, "place detect hit is not auto-confirmed")
|
||||
proposals = [item["observed_label"] for item in list_review_proposals(profile_id)]
|
||||
expect("Anna" in proposals, "unconfirmed proposal may be stored for review")
|
||||
|
||||
patterned = {item["text"] for item in _pattern_entities("Ich war mit Anna in Hamburg.", set())}
|
||||
expect("Anna" in patterned and "Hamburg" in patterned, "pattern finds proper names")
|
||||
expect("Himmel" not in {item["text"] for item in _pattern_entities("Der Himmel war klar.", set())}, "pattern skips weather")
|
||||
second = _run(profile_id, "Anna war ruhig.")
|
||||
expect("Anna" not in (second.trace.get("egress") or ""), "second request still runs full detection")
|
||||
expect(second.trace.get("request_local_hits", 0) >= 1, "known labels are not excluded from the new detection")
|
||||
|
||||
poisoned = remember_mapping(profile_id, "Wohnzimmer", "PERSON:77")
|
||||
expect(poisoned["status"] == "confirmed", "remember_mapping is explicit confirmation")
|
||||
update = client.patch(
|
||||
f"/api/admin/identities/{poisoned['id']}",
|
||||
headers=headers,
|
||||
json={"status": "inactive"},
|
||||
)
|
||||
expect(update.status_code == 200, "admin can deactivate a false mapping")
|
||||
expect(update.json()["status"] == "inactive", "deactivated mapping is inactive")
|
||||
later = _run(profile_id, "Ich saß im Wohnzimmer.")
|
||||
expect("Wohnzimmer" in (later.trace.get("egress") or ""), "inactive mapping does not keep masking a room")
|
||||
expect(
|
||||
not any(item.get("canonical_label") == "Wohnzimmer" and item.get("status") == "confirmed" for item in list_registry(profile_id)),
|
||||
"later request is not bound to the deactivated mapping",
|
||||
)
|
||||
|
||||
confirmed_project = confirm_identity(profile_id, "Kanshō", entity_type="PROJECT", aliases=["Kansho"])
|
||||
canonical = _run(profile_id, "Ich arbeitete an Kansho weiter.")
|
||||
expect("[[PROJECT:" in (canonical.trace.get("egress") or ""), "confirmed alias is masked")
|
||||
expect("Kansho" not in (canonical.trace.get("egress") or ""), "confirmed alias does not leak")
|
||||
project_manifest = mask_prompt(
|
||||
"Ich arbeitete an Kansho weiter.",
|
||||
[
|
||||
{
|
||||
"local_label": "Kansho",
|
||||
"token": confirmed_project["token"],
|
||||
"entity_type": "PROJECT",
|
||||
"demask_label": "Kanshō",
|
||||
}
|
||||
],
|
||||
"journal_generate",
|
||||
)
|
||||
expect(
|
||||
_demask(f"[[{confirmed_project['token']}]] weiter", project_manifest) == "Kanshō weiter",
|
||||
"confirmed alias demasks to the canonical spelling",
|
||||
)
|
||||
|
||||
observed_manifest = mask_prompt(
|
||||
"Ich traf Anna am Abend.",
|
||||
[{"local_label": "Anna", "token": "PERSON:01", "demask_label": "Anna"}],
|
||||
"journal_generate",
|
||||
)
|
||||
expect(_demask("[[PERSON:01]] am Abend.", observed_manifest) == "Anna am Abend.", "request-local hit demasks to the observed spelling")
|
||||
|
||||
leftover = _demask("[[PERSON:01]] und [[PERSON:99]]", mask_prompt("Anna war da.", [{"local_label": "Anna", "token": "PERSON:01", "demask_label": "Anna"}], "journal_generate"))
|
||||
expect("[[PERSON:99]]" in leftover, "unknown placeholder is not materialized")
|
||||
|
||||
long_text = ("Anfang. " + ("x" * 1200) + " Anna in der Mitte. " + ("y" * 1200) + " Ende.")
|
||||
with patch("entity_detect.DETECT_CHUNK_CHARS", 400), patch("entity_detect.DETECT_CHUNK_OVERLAP", 80):
|
||||
chunks = split_detect_chunks(long_text, chunk_chars=400, overlap=80)
|
||||
expect(len(chunks) >= 3, "long egress is chunked")
|
||||
covered = 0
|
||||
prev_end = 0
|
||||
for offset, chunk in chunks:
|
||||
expect(offset <= prev_end, "chunks overlap or abut")
|
||||
covered = max(covered, offset + len(chunk))
|
||||
prev_end = offset + len(chunk)
|
||||
expect(covered == len(long_text), "chunk union covers the full text")
|
||||
boundary = _run(profile_id, long_text)
|
||||
expect(boundary.trace.get("chunk_count") >= 2, "gateway chunks a long prompt")
|
||||
expect(boundary.trace.get("chunks_ok") == boundary.trace.get("chunk_count"), "every chunk is marked ok")
|
||||
expect("Anna" not in (boundary.trace.get("egress") or ""), "entity at a chunk boundary is still masked")
|
||||
expect(boundary.trace.get("full_detection_coverage") is True, "coverage is true only after every chunk")
|
||||
|
||||
generate_calls = {"n": 0}
|
||||
|
||||
def boom(_messages, _policy):
|
||||
generate_calls["n"] += 1
|
||||
raise AssertionError("generate must not run")
|
||||
|
||||
install_test_detect_failure(DetectError("detect_chunk_failed", "chunk failed"))
|
||||
blocked = False
|
||||
with patch("privacy_gateway.complete_model", boom):
|
||||
try:
|
||||
_run(profile_id, "Ich traf Anna.")
|
||||
except PrivacyGatewayError as exc:
|
||||
blocked = exc.code == "detect_chunk_failed"
|
||||
expect(exc.diagnostics.get("generate_called") is False, "failed chunk does not call generate")
|
||||
expect(exc.diagnostics.get("full_detection_coverage") is False, "failed chunk is not full coverage")
|
||||
expect("Substring" not in (exc.message or ""), "detect errors omit the internal substring wording")
|
||||
expect("Angaben" in (exc.message or ""), "detect errors use a user-facing message")
|
||||
reset_detect_test_hooks()
|
||||
expect(blocked, "failed chunk fails closed")
|
||||
expect(generate_calls["n"] == 0, "generate is not called when a chunk fails")
|
||||
|
||||
install_test_truncated(True)
|
||||
truncated = False
|
||||
generate_calls["n"] = 0
|
||||
with patch("privacy_gateway.complete_model", boom):
|
||||
try:
|
||||
_run(profile_id, "Ich traf Anna.")
|
||||
except PrivacyGatewayError as exc:
|
||||
truncated = exc.code == "detect_truncated"
|
||||
expect(exc.diagnostics.get("generate_called") is False, "truncated detect does not call generate")
|
||||
reset_detect_test_hooks()
|
||||
expect(truncated, "truncated detect output fails closed")
|
||||
|
||||
start = "Ich traf Anna.".index("Anna")
|
||||
install_test_spans([{"start": start, "end": start + 4, "text": "Anna", "entity_type": "PERSON", "token": "PERSON:09"}])
|
||||
extra = False
|
||||
with patch("privacy_gateway.complete_model", boom):
|
||||
try:
|
||||
_run(profile_id, "Ich traf Anna.")
|
||||
except PrivacyGatewayError as exc:
|
||||
extra = exc.code == "detect_invalid_output"
|
||||
reset_detect_test_hooks()
|
||||
expect(extra, "detector-assigned tokens fail closed")
|
||||
|
||||
install_test_spans([{"start": 0, "end": 4, "text": "Anna", "entity_type": "PERSON"}])
|
||||
try:
|
||||
grounded_run = _run(profile_id, "Ich traf Anna am Abend.")
|
||||
expect("Anna" not in (grounded_run.trace.get("egress") or ""), "grounded name is still masked")
|
||||
expect(grounded_run.trace.get("generate_called") is True, "wrong offsets do not block generate")
|
||||
finally:
|
||||
reset_detect_test_hooks()
|
||||
|
||||
results = []
|
||||
|
||||
def worker(label: str):
|
||||
reset_debug()
|
||||
out = _run(profile_id, f"Ich traf {label}.")
|
||||
results.append((label, out.trace.get("active_tokens"), threading.get_ident()))
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
one = pool.submit(worker, "Anna")
|
||||
two = pool.submit(worker, "Maren")
|
||||
one.result()
|
||||
two.result()
|
||||
labels = {item[0] for item in results}
|
||||
expect(labels == {"Anna", "Maren"}, "parallel requests keep their own manifests")
|
||||
|
||||
expect(not is_maskable_label("ich"), "pronoun ich is not a mapping")
|
||||
expect(not is_maskable_label("user"), "role user is not a mapping")
|
||||
expect(not is_maskable_label("Himmel"), "weather is not identity")
|
||||
expect(not is_maskable_label("Der Himmel"), "determiner plus weather is not identity")
|
||||
expect(not is_maskable_label("6:00 Uhr"), "clock time is not identity")
|
||||
expect(not is_maskable_label("meinen Tee"), "tea is not identity")
|
||||
expect(not is_maskable_label("Relativ"), "sentence adverb is not identity")
|
||||
expect(not is_maskable_label("Allerdings"), "conjunction is not identity")
|
||||
expect(not is_maskable_label("Tages"), "common noun is not identity")
|
||||
expect(not is_maskable_label("Brote"), "food is not identity")
|
||||
expect(not is_maskable_label("Überlegen"), "common noun is not identity")
|
||||
expect(not is_maskable_label("Ortes"), "place word is not identity")
|
||||
expect(not is_maskable_label("Zwiebeln"), "food is not identity")
|
||||
expect(not is_maskable_label("letzten"), "function word letzten is not identity")
|
||||
expect(not is_maskable_label("Getöse"), "collective noun is not identity")
|
||||
expect(not is_maskable_label("Schiff"), "vessel is not identity")
|
||||
expect(not is_maskable_label("Faden"), "prompt word is not identity")
|
||||
expect("Getöse" not in {item["text"] for item in _pattern_entities("Zweimaster mit Getöse im Hafen.", set())}, "mit plus common noun is not a name")
|
||||
expect("Anna" in {item["text"] for item in _pattern_entities("Ich war mit Anna im Hafen.", set())}, "mit plus given name stays")
|
||||
expect(is_maskable_label("Sushi"), "given name stays maskable")
|
||||
expect(is_maskable_label("meine Frau Sushi"), "kinship stripped, name remains")
|
||||
from identity_store import coalesce_mappings, normalize_token, remember_mapping
|
||||
from db import get_db
|
||||
import uuid
|
||||
|
||||
legacy_id = str(uuid.uuid4())
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO identity_mappings
|
||||
(id, profile_id, token, local_label, canonical_label, entity_type, status, origin)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(legacy_id, profile_id, "PERSON:55", "Wohnblock", "Wohnblock", "PERSON", STATUS_LEGACY, "legacy_auto"),
|
||||
)
|
||||
expect(
|
||||
not any(item.get("id") == legacy_id for item in list_mappings(profile_id)),
|
||||
"legacy mappings are not treated as confirmed",
|
||||
)
|
||||
legacy_run = _run(profile_id, "Der Wohnblock war ruhig.")
|
||||
expect("Wohnblock" in (legacy_run.trace.get("egress") or ""), "legacy auto-mapping does not skip detection or force masking")
|
||||
|
||||
listed = client.get("/api/admin/identities", headers=headers)
|
||||
expect(listed.status_code == 200, "admin identities list is authenticated")
|
||||
expect("registry" in listed.json() and "proposals" in listed.json(), "admin list returns registry and proposals")
|
||||
expect("local_label" not in json.dumps(first.diagnostics), "compact diagnostics omit labels")
|
||||
expect("Anna" not in json.dumps(first.diagnostics), "compact diagnostics omit clear names")
|
||||
expect("Anna" not in json.dumps(first.trace.get("log") or []), "run log omits labels")
|
||||
|
||||
provider_seen = {"n": 0}
|
||||
|
||||
def mark_provider(messages, policy):
|
||||
provider_seen["n"] += 1
|
||||
from privacy_gateway import ChatResult as _unused # noqa: F401
|
||||
from providers import ChatResult
|
||||
|
||||
return ChatResult(content="[[PERSON:01]] war ruhig.", model="fake", usage={})
|
||||
|
||||
with patch("privacy_gateway.complete_model", mark_provider):
|
||||
_run(profile_id, "Ich traf Anna.")
|
||||
expect(provider_seen["n"] == 1, "successful detection still uses exactly one generate call")
|
||||
|
||||
from identity_store import coalesce_mappings, normalize_token
|
||||
from privacy_gateway import _mask
|
||||
from pronoun_bind import bind_user_lines, bind_pronouns
|
||||
from privacy_gateway import mask_for_egress
|
||||
|
||||
expect(normalize_token("PERSON:KURZ") is None, "template token KURZ rejected")
|
||||
first = remember_mapping(setup.json()["profile_id"], "meine Frau Sushi", "PERSON:08")
|
||||
second = remember_mapping(setup.json()["profile_id"], "Sushi", "PERSON:KURZ")
|
||||
expect(first["token"] == second["token"], "alias reuses token")
|
||||
expect(second["local_label"] == "Sushi", "canonical label is the name")
|
||||
first_map = remember_mapping(profile_id, "meine Frau Sushi", "PERSON:08")
|
||||
second_map = remember_mapping(profile_id, "Sushi", "PERSON:KURZ")
|
||||
expect(first_map["token"] == second_map["token"], "alias reuses token")
|
||||
expect(second_map["canonical_label"] == "Sushi", "canonical label is the name")
|
||||
extra_id = str(uuid.uuid4())
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO identity_mappings (id, profile_id, token, local_label)
|
||||
VALUES (?, ?, ?, ?)
|
||||
INSERT INTO identity_mappings (id, profile_id, token, local_label, canonical_label, entity_type, status, origin)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(extra_id, setup.json()["profile_id"], "PERSON:KURZ", "meine Frau Sushi"),
|
||||
(extra_id, profile_id, "PERSON:KURZ", "meine Frau Sushi", "meine Frau Sushi", "PERSON", "confirmed", "local_authoritative"),
|
||||
)
|
||||
coalesce_mappings(setup.json()["profile_id"])
|
||||
coalesce_mappings(profile_id)
|
||||
sushi_rows = [
|
||||
item
|
||||
for item in list_mappings(setup.json()["profile_id"])
|
||||
if (item.get("local_label") or "").lower() == "sushi"
|
||||
for item in list_registry(profile_id)
|
||||
if (item.get("canonical_label") or "").lower() == "sushi"
|
||||
]
|
||||
expect(len(sushi_rows) == 1, "coalesce keeps one Sushi row")
|
||||
expect(sushi_rows[0]["token"].upper() != "PERSON:KURZ", "stable token wins over KURZ")
|
||||
joined = _mask(
|
||||
"meine Frau Sushi kam. Sushi stellte fest.",
|
||||
sushi_rows,
|
||||
)
|
||||
joined = _mask("meine Frau Sushi kam. Sushi stellte fest.", [{"local_label": "Sushi", "token": sushi_rows[0]["token"]}])
|
||||
expect(joined.count(f"[[{sushi_rows[0]['token']}]]") == 2, "both mentions share one placeholder")
|
||||
expect("Sushi" not in joined, "name fully masked")
|
||||
expect("meine Frau " in joined, "kinship stays visible")
|
||||
homonym = _mask(
|
||||
"Ich esse gerne Sushi mit meiner Frau Sushi.",
|
||||
sushi_rows,
|
||||
[{"local_label": "Sushi", "token": sushi_rows[0]["token"]}],
|
||||
)
|
||||
expect("esse gerne Sushi" in homonym, "food homonym stays")
|
||||
expect("Frau [[" in homonym or f"Frau [[{sushi_rows[0]['token']}]]" in homonym, "kinship name is masked")
|
||||
expect(homonym.count("[[") == 1, "only the person mention is masked")
|
||||
eating = _mask("Heute nur von Sushi essen erzählt.", sushi_rows)
|
||||
eating = _mask("Heute nur von Sushi essen erzählt.", [{"local_label": "Sushi", "token": sushi_rows[0]["token"]}])
|
||||
expect("Sushi essen" in eating, "noun before eat stays food")
|
||||
expect("[[" not in eating, "food-only sentence is not the person")
|
||||
from pronoun_bind import bind_user_lines, bind_pronouns
|
||||
from privacy_gateway import mask_for_egress
|
||||
|
||||
anna = [{"local_label": "Anna", "token": "PERSON:01"}]
|
||||
bound = bind_user_lines(
|
||||
@@ -159,32 +412,18 @@ def main() -> None:
|
||||
)
|
||||
)
|
||||
expect(bound.count("[[PERSON:01]]") == 3, "name and both pronouns share one token")
|
||||
expect("Sie" not in bound and "ihr" not in bound, "pronouns rebound to person")
|
||||
journal_masked = mask_for_egress(
|
||||
"user: Ich war mit Anna.\nuser: Sie setzte sich zu mir, und ich gab ihr Tee.",
|
||||
anna,
|
||||
"journal_generate",
|
||||
)
|
||||
expect(journal_masked.count("[[PERSON:01]]") == 1, "journal masks the name only")
|
||||
expect("Sie setzte" in journal_masked and "ihr Tee" in journal_masked, "journal keeps anaphora")
|
||||
dialogue_masked = mask_for_egress(
|
||||
"user: Ich war mit Anna.\nuser: Sie setzte sich zu mir, und ich gab ihr Tee.",
|
||||
anna,
|
||||
"dialogue_turn",
|
||||
)
|
||||
expect(dialogue_masked.count("[[PERSON:01]]") == 3, "dialogue still binds pronouns")
|
||||
instruction = bind_user_lines(
|
||||
"user: Anna kam.\n"
|
||||
"Der Impuls darf eine Frage sein. Er darf keine neue Station setzen.\n"
|
||||
)
|
||||
expect("Er darf keine neue Station" in instruction, "instruction pronoun stays")
|
||||
instruction_name = _mask(
|
||||
"Nenne Berg nicht in der Anweisung.\nuser: Berg kam vorbei.",
|
||||
[{"local_label": "Berg", "token": "PERSON:02"}],
|
||||
personal_lines_only=True,
|
||||
)
|
||||
expect("Nenne Berg nicht" in instruction_name, "instruction line is not remasked")
|
||||
expect("[[PERSON:02]]" in instruction_name, "user line still remasked")
|
||||
herr = [{"local_label": "Berg", "token": "PERSON:02"}]
|
||||
male, _ = bind_pronouns(_mask("Herr Berg kam. Er setzte sich.", herr))
|
||||
expect(male.count("[[PERSON:02]]") == 2, "masculine pronoun follows last man")
|
||||
@@ -192,18 +431,41 @@ def main() -> None:
|
||||
expect("ihr" in address, "address ihr is not a person")
|
||||
plural, _ = bind_pronouns(_mask("Anna kam. Sie waren zu dritt.", anna))
|
||||
expect("Sie waren" in plural or "sie waren" in plural.lower(), "plural sie stays")
|
||||
croatia = (
|
||||
"Heute bin ich ca. gegen 6:00 Uhr aufgestanden. Relativ früh gesellte sich "
|
||||
"auch meine Frau Sushi zu mir. Allerdings sollte die Bootstour erst um 11:00 Uhr starten."
|
||||
)
|
||||
patterned_live = {item["text"] for item in _pattern_entities(croatia, set())}
|
||||
expect("Sushi" in patterned_live, "pattern keeps given name")
|
||||
expect(
|
||||
patterned_live <= {"Sushi"},
|
||||
f"pattern does not treat German function words as names: {patterned_live}",
|
||||
)
|
||||
from entity_detect import uses_llm_detect
|
||||
from providers import ProviderConfig
|
||||
|
||||
from identity_store import is_maskable_label
|
||||
|
||||
expect(not is_maskable_label("ich"), "pronoun ich is not a mapping")
|
||||
expect(not is_maskable_label("user"), "role user is not a mapping")
|
||||
expect(not is_maskable_label("6:00 Uhr"), "clock time is not identity")
|
||||
expect(is_maskable_label("Sushi"), "name-shaped label stays structurally maskable")
|
||||
expect(is_maskable_label("Himmel"), "weather word is not banned by a stopword list")
|
||||
|
||||
from db import get_db as _get_db
|
||||
|
||||
with _get_db() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT seed_revision, template, default_template FROM ai_prompts WHERE slug = ?",
|
||||
("mvp.entity_detect",),
|
||||
).fetchone()
|
||||
expect(row["seed_revision"] == "2026-08-27-detect-ground-v1", "detect prompt revision is stored")
|
||||
expect("{{known_labels}}" not in (row["template"] or ""), "new detect prompt has no known_labels skip list")
|
||||
expect("Zwiebeln" not in (row["template"] or ""), "detect prompt has no food word list")
|
||||
|
||||
with _get_db() as conn:
|
||||
conn.execute(
|
||||
"UPDATE ai_prompts SET template = ? WHERE slug = ?",
|
||||
("CUSTOM DETECT {{source_text}}", "mvp.entity_detect"),
|
||||
)
|
||||
from db import init_db
|
||||
|
||||
init_db()
|
||||
with _get_db() as conn:
|
||||
custom = conn.execute(
|
||||
"SELECT template, seed_revision FROM ai_prompts WHERE slug = ?",
|
||||
("mvp.entity_detect",),
|
||||
).fetchone()
|
||||
expect(custom["template"] == "CUSTOM DETECT {{source_text}}", "user-edited detect prompt is not overwritten")
|
||||
expect(custom["seed_revision"] == "2026-08-27-detect-ground-v1", "revision still updates the default")
|
||||
|
||||
remote = ProviderConfig(
|
||||
role="detect",
|
||||
@@ -232,42 +494,60 @@ def main() -> None:
|
||||
)
|
||||
expect(uses_llm_detect(local), "production still allows local detect")
|
||||
os.environ.pop("KANSHO_ENV", None)
|
||||
poisoned = _mask(
|
||||
"Nicht erwähnt ist nicht geschehen. Ich kochte Tee.",
|
||||
[{"local_label": "ich", "token": "PERSON:05"}],
|
||||
)
|
||||
expect("N[[PERSON:05]]t" not in poisoned, "ich does not split nicht")
|
||||
expect("Nicht erwähnt" in poisoned, "instruction word bleibt")
|
||||
from privacy_gateway import PrivacyGatewayError, _demask, _validate_response
|
||||
|
||||
mapped = [{"local_label": "Anna", "token": "PERSON:01"}]
|
||||
kept = _validate_response("[[PERSON:01]] war ruhig.", mapped)
|
||||
expect(kept.startswith("[[PERSON:01]]"), "placeholders pass validation")
|
||||
expect(_demask(kept, mapped) == "Anna war ruhig.", "demask after validation")
|
||||
blocked = False
|
||||
anna_manifest = mask_prompt("Anna war am Markt.", mapped, "journal_generate")
|
||||
kept = _validate_response("[[PERSON:01]] war ruhig.", anna_manifest)
|
||||
expect(_demask(kept, anna_manifest) == "Anna war ruhig.", "demask after validation")
|
||||
expect(
|
||||
_demask("[[ person:01 ]] war ruhig.", anna_manifest) == "Anna war ruhig.",
|
||||
"demask is case- and space-insensitive",
|
||||
)
|
||||
blocked_plain = False
|
||||
try:
|
||||
_validate_response("Anna war ruhig.", mapped)
|
||||
_validate_response("Anna war ruhig.", anna_manifest)
|
||||
except PrivacyGatewayError as exc:
|
||||
blocked = exc.code == "response_validation_failed"
|
||||
expect(blocked, "plaintext identity is blocked before demask")
|
||||
sushi_map = [{"local_label": "Sushi", "token": "PERSON:01"}]
|
||||
food_reply = _validate_response("Heute nur von Sushi essen erzählt.", sushi_map)
|
||||
expect("Sushi essen" in food_reply, "food homonym in a reply is not treated as a leak")
|
||||
mixed = mask_for_egress(
|
||||
"Belegte Vorhaben (nur user-Quellen):\n- Morgen wollen wir mit Anna fahren.\nuser: Heute Markt.",
|
||||
mapped,
|
||||
"dialogue_turn",
|
||||
blocked_plain = exc.code == "response_validation_failed"
|
||||
expect(blocked_plain, "plaintext identity is blocked before demask")
|
||||
|
||||
no_user = client.get("/api/admin/identities")
|
||||
expect(no_user.status_code in {401, 403}, "identity registry requires auth")
|
||||
admin_calls = {"n": 0}
|
||||
|
||||
def no_egress(*_args, **_kwargs):
|
||||
admin_calls["n"] += 1
|
||||
raise AssertionError("admin identity actions must not call detect")
|
||||
|
||||
with patch("entity_detect.complete_chat", no_egress), patch("privacy_gateway.complete_model", no_egress):
|
||||
created = client.post(
|
||||
"/api/admin/identities",
|
||||
headers=headers,
|
||||
json={"canonical_label": "Nordlicht", "entity_type": "PROJECT", "aliases": ["Nordlicht-Projekt"]},
|
||||
)
|
||||
expect(created.status_code == 200, f"admin create stays local {created.text}")
|
||||
expect(admin_calls["n"] == 0, "admin registry actions create no external egress")
|
||||
expect(created.json()["status"] == "confirmed", "admin create confirms locally")
|
||||
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Ich war mit Anna in Hamburg und danach allein."},
|
||||
)
|
||||
expect("Anna" not in mixed, "opening context names are masked for dialogue egress")
|
||||
expect("[[PERSON:01]]" in mixed, "opening context uses the person token")
|
||||
expect("Heute Markt" in mixed, "current user wording remains")
|
||||
excerpt = mask_for_egress(
|
||||
"Frühere Einträge im Space (nur Hinweise, keine Gewissheit):\n2026-08-01: Anna war am Hafen.\nuser: Heute windstill.",
|
||||
mapped,
|
||||
"dialogue_turn",
|
||||
)
|
||||
expect("Anna" not in excerpt, "space excerpts are masked for dialogue egress")
|
||||
expect("[[PERSON:01]]" in excerpt, "space excerpt uses the person token")
|
||||
expect(turn.status_code == 200, f"turn {turn.status_code}")
|
||||
expect((turn.json().get("trace") or {}).get("generate_called") is True, "dialogue generate still runs once after detect")
|
||||
|
||||
install_test_detect_failure(DetectError("detect_invalid_output", "Detect-text entspricht nicht dem lokalen Substring."))
|
||||
try:
|
||||
held = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Heute nur der Markt."},
|
||||
)
|
||||
expect(held.status_code == 200, f"detect fallback turn {held.status_code} {held.text}")
|
||||
expect((held.json().get("assistant") or {}).get("body"), "detect failure still leaves a local impulse")
|
||||
expect((held.json().get("decision") or {}).get("guard") == "detect_blocked", "detect fallback is marked locally")
|
||||
finally:
|
||||
reset_detect_test_hooks()
|
||||
|
||||
print("All privacy detect tests passed.")
|
||||
|
||||
|
||||
Reference in new issue
Block a user