This commit is contained in:
Lars committed 2026-08-29 11:04:34 +02:00
1 parent 4f37991ad1
commit be11f82f84
39 files changed
+2490 -239

No files matched your search

+84 -5
View File
@@ -8,7 +8,6 @@ import json
import os
import sys
import tempfile
import threading
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from unittest.mock import patch
@@ -25,6 +24,8 @@ from fastapi.testclient import TestClient
from identity_store import (
STATUS_LEGACY,
confirm_identity,
is_maskable_label,
is_registry_maskable_label,
list_confirmed_identities,
list_mappings,
list_registry,
@@ -37,6 +38,7 @@ from privacy_gateway import (
PrivacyGatewayError,
_demask,
_validate_response,
_visible_response,
complete,
mask_prompt,
reset_debug,
@@ -181,7 +183,10 @@ def main() -> None:
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("full_detection_coverage") is True, "successful detect records technical chunk coverage")
expect(first.trace.get("semantic_identity_guaranteed") is False, "chunk coverage is not semantic certainty")
expect(first.diagnostics.get("semantic_identity_guaranteed") is False, "compact trace does not claim semantic completeness")
expect(first.trace.get("confirmed_registry_applied") is True, "confirmed registry is applied as a safety net")
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)}
@@ -315,7 +320,14 @@ def main() -> None:
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()))
results.append(
(
label,
tuple(out.trace.get("active_tokens") or []),
out.trace.get("egress") or "",
out.trace.get("intern") or "",
)
)
with ThreadPoolExecutor(max_workers=2) as pool:
one = pool.submit(worker, "Anna")
@@ -324,6 +336,10 @@ def main() -> None:
two.result()
labels = {item[0] for item in results}
expect(labels == {"Anna", "Maren"}, "parallel requests keep their own manifests")
by_label = {item[0]: item for item in results}
expect("Maren" not in by_label["Anna"][2] and "Maren" not in by_label["Anna"][3], "Anna request does not carry Maren")
expect("Anna" not in by_label["Maren"][2] and "Anna" not in by_label["Maren"][3], "Maren request does not carry Anna")
expect(by_label["Anna"][1] and by_label["Maren"][1], "each parallel request has its own tokens")
from db import get_db
import uuid
@@ -432,13 +448,14 @@ def main() -> None:
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")
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")
expect(not is_maskable_label("Lia"), "detect gate still skips short labels")
expect(is_registry_maskable_label("Lia"), "confirmed registry may mask short names")
expect(not is_registry_maskable_label("ich"), "registry gate still skips pronouns")
from db import get_db as _get_db
@@ -506,6 +523,10 @@ def main() -> None:
tokenized = _validate_response("Anna war ruhig.", anna_manifest)
expect(tokenized.startswith("[[PERSON:01]]"), "active cleartext is normalized, not discarded")
expect(_demask(tokenized, anna_manifest) == "Anna war ruhig.", "normalized plaintext demasks")
expect(
_visible_response("Anna war ruhig.", anna_manifest) == "Anna war ruhig.",
"visible output keeps model plaintext instead of the check roundtrip",
)
no_user = client.get("/api/admin/identities")
expect(no_user.status_code in {401, 403}, "identity registry requires auth")
@@ -525,6 +546,64 @@ def main() -> None:
expect(admin_calls["n"] == 0, "admin registry actions create no external egress")
expect(created.json()["status"] == "confirmed", "admin create confirms locally")
before_confirm = {item.get("canonical_label") for item in list_confirmed_identities(profile_id)}
confirm_identity(profile_id, "Lia", aliases=["Mara"])
expect(is_registry_maskable_label("Lia"), "short confirmed name remains maskable")
egress_body = (
"WRITING_PROFILE\nLia schreibt knapp.\n"
"STYLE_EXAMPLES\nMara lachte am Markt.\n"
"CURRENT_DAY_SOURCES\nLia und Mara gingen später.\n"
"EXISTING_TEXT\nLia blieb."
)
covered = _run(profile_id, egress_body)
masked_all = covered.trace.get("egress") or ""
intern = covered.trace.get("intern") or ""
expect("Lia" in intern and "Mara" in intern, "clear names remain in the local template")
expect("Lia" not in masked_all, "confirmed short name is masked in every egress section")
expect("Mara" not in masked_all, "confirmed alias is masked in every egress section")
expect(
masked_all.count("[[") >= intern.count("Lia") + intern.count("Mara"),
"canonical and alias mentions are replaced",
)
expect("WRITING_PROFILE" in masked_all and "STYLE_EXAMPLES" in masked_all, "writing profile and style examples stay in egress")
expect("CURRENT_DAY_SOURCES" in masked_all and "EXISTING_TEXT" in masked_all, "sources and existing text stay in egress")
expect(covered.trace.get("confirmed_registry_applied") is True, "registry safety net is marked applied")
expect(covered.trace.get("semantic_identity_guaranteed") is False, "registry coverage is not semantic certainty")
expect(covered.trace.get("pre_egress_validation") == "ok", "confirmed plaintext is not left in egress")
group_text = "Die Kinder verstanden kein Wort. Herkunft: Nordland."
kinder_start = group_text.index("Kinder")
nord_start = group_text.index("Nordland")
install_test_spans(
[
{
"start": kinder_start,
"end": kinder_start + len("Kinder"),
"text": "Kinder",
"entity_type": "PERSON",
},
{
"start": nord_start,
"end": nord_start + len("Nordland"),
"text": "Nordland",
"entity_type": "PLACE",
},
]
)
try:
overmasked = _run(profile_id, group_text)
proposals = list_review_proposals(profile_id)
labels = {(item.get("observed_label") or item.get("canonical_label") or item.get("local_label") or "").casefold() for item in proposals}
expect("kinder" in labels or "nordland" in labels, "semantic overmask stays request-local as a proposal")
confirmed_now = {item.get("canonical_label") for item in list_confirmed_identities(profile_id)}
expect("Kinder" not in confirmed_now, "generic group is not persisted as an identity")
expect("Nordland" not in confirmed_now, "origin designation is not persisted as an identity")
expect(confirmed_now == before_confirm | {"Lia"}, "only the confirmed registry row is durable")
expect(overmasked.trace.get("full_detection_coverage") is True, "injected spans still mark technical coverage")
expect(overmasked.trace.get("semantic_identity_guaranteed") is False, "overmasking is not claimed as certainty")
finally:
reset_detect_test_hooks()
turn = client.post(
f"/api/journal/conversations/{conv.json()['id']}/turn",
headers=headers,