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

+94 -1
View File
@@ -3,6 +3,7 @@ from __future__ import annotations
import json
import os
import re
import sys
import tempfile
from concurrent.futures import ThreadPoolExecutor
@@ -18,6 +19,7 @@ os.environ["KANSHO_FAKE_DETECT"] = "1"
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
from fastapi.testclient import TestClient
from entity_detect import install_test_spans, reset_detect_test_hooks
from identity_store import remember_mapping
from main import app
from privacy_gateway import (
@@ -29,6 +31,7 @@ from privacy_gateway import (
_demask,
_process_response,
_validate_response,
_visible_response,
complete,
last_trace,
mask_prompt,
@@ -137,6 +140,55 @@ def main() -> None:
expect(_demask("[[PERSON:07]] kam.", aliased) == "Clarissa kam.", "confirmed alias demasks to the canonical spelling")
canonical_clear, _meta = _process_response("Clarissa kam vorbei.", aliased)
expect(canonical_clear.startswith("[[PERSON:07]]"), "canonical spelling of an active alias token is remapped")
expect(
_visible_response("Sushi kam vorbei.", aliased) == "Sushi kam vorbei.",
"alias plaintext keeps the model surface form",
)
expect(
_visible_response("Clarissa kam vorbei.", aliased) == "Clarissa kam vorbei.",
"canonical plaintext keeps the model surface form",
)
expect(
_visible_response("[[PERSON:07]] kam vorbei.", aliased) == "Clarissa kam vorbei.",
"emitted placeholder still rehydrates to the canonical spelling",
)
group_prompt = "Heute die Gruppe am Markt."
group_start = group_prompt.index("die Gruppe")
group_rows = [
{
"local_label": "die Gruppe",
"token": "PERSON:04",
"canonical_label": "die Gruppe",
"demask_label": "die Gruppe",
"entity_type": "PERSON",
"start": group_start,
"end": group_start + len("die Gruppe"),
"source": "request_local",
}
]
group = mask_prompt(group_prompt, group_rows, "journal_generate")
expect(len(group.replacements) == 1, "observed multi-word label is active")
mixed_raw = (
"Die Gruppe verstand kein Wort. "
"Später fragte ich, ob die Gruppe noch da sei. "
"[[PERSON:04]] blieb. "
"die Gruppen standen abseits."
)
tokenized, meta = _process_response(mixed_raw, group)
expect(meta.get("response_normalization") == "active_cleartext_normalized", "check copy still remaps active cleartext")
expect(tokenized.startswith("[[PERSON:04]]"), "check copy tokenizes sentence-initial cleartext")
expect("die Gruppe verstand" in _demask(tokenized, group), "check roundtrip uses the stored label")
visible = _visible_response(mixed_raw, group)
expect(visible.startswith("Die Gruppe verstand"), "stored text keeps sentence-initial capitalization")
expect("ob die Gruppe noch da sei" in visible, "stored text keeps mid-sentence form")
expect("die Gruppe blieb" in visible, "emitted placeholder is rehydrated locally")
expect("die Gruppen standen" in visible, "unmatched plural surface form is left unchanged")
expect(visible != _demask(tokenized, group), "visible output is not the rematerialized check copy")
leftover_unknown = _visible_response("[[PERSON:04]] und [[PERSON:99]] und [[…]]", group)
expect(leftover_unknown.startswith("die Gruppe"), "known placeholder is rehydrated")
expect("[[PERSON:99]]" in leftover_unknown, "unknown placeholder is not materialized")
expect("[[…]]" in leftover_unknown, "generic placeholder is not materialized")
overlap = mask_prompt(
"Anna-Lena kam vorbei.",
@@ -296,7 +348,7 @@ def main() -> None:
repaired = _run_gateway(profile_id, "Ich war mit Anna am Markt.")
expect(len(seen) == 1, "active cleartext does not start a second generate call")
expect("[[PERSON:01]]" in seen[0] and "Anna" not in seen[0].replace("[[PERSON:01]]", ""), "egress stays masked")
expect("Anna" in (repaired.content or ""), "normalized cleartext is demasked locally")
expect("Anna" in (repaired.content or ""), "model plaintext identity spelling is kept")
expect(repaired.diagnostics.get("generate_calls") == 1, "exactly one generate call")
expect(
repaired.diagnostics.get("response_normalization") == "active_cleartext_normalized",
@@ -324,6 +376,47 @@ def main() -> None:
expect(repaired.diagnostics.get("total_tokens") == 18, "single call keeps total tokens")
expect(abs(float(repaired.diagnostics.get("cost") or 0) - 0.01) < 1e-9, "single call keeps cost")
prompt_group = "Ich traf die Gruppe."
group_at = prompt_group.index("die Gruppe")
install_test_spans(
[
{
"start": group_at,
"end": group_at + len("die Gruppe"),
"text": "die Gruppe",
"entity_type": "PERSON",
}
]
)
def mixed_surface(messages, _policy):
egress = messages[0].get("content") or ""
match = re.search(r"\[\[PERSON:\d+\]\]", egress)
token = match.group(0) if match else "[[PERSON:01]]"
return ChatResult(
content=f"Die Gruppe verstand kein Wort. {token} blieb. Später die Gruppe wieder.",
model="fake",
usage={},
context_compression="not_applicable",
)
try:
with patch("privacy_gateway.complete_model", mixed_surface):
surfaced = _run_gateway(profile_id, prompt_group)
finally:
reset_detect_test_hooks()
expect(surfaced.diagnostics.get("generate_calls") == 1, "mixed surface still uses one generate call")
expect("Die Gruppe verstand" in ((surfaced.trace or {}).get("raw") or ""), "trace raw keeps model casing")
expect("[[" in ((surfaced.trace or {}).get("raw") or ""), "trace raw keeps the emitted placeholder")
expect(surfaced.content == (surfaced.trace or {}).get("reply"), "visible content matches trace reply")
expect((surfaced.content or "").startswith("Die Gruppe verstand"), "gateway keeps sentence-initial model casing")
expect("die Gruppe blieb" in (surfaced.content or ""), "gateway rehydrates the emitted placeholder")
expect("Später die Gruppe wieder" in (surfaced.content or ""), "gateway keeps later plaintext unchanged")
expect(
surfaced.diagnostics.get("response_normalization") == "active_cleartext_normalized",
"check copy still records cleartext normalization",
)
inactive_calls = {"n": 0}
def keep_inactive(messages, _policy):