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

+309
View File
@@ -0,0 +1,309 @@
"""Opt-in debug persist for the admin test spur. Run from backend/: python tests/test_debug_persist.py"""
from __future__ import annotations
import json
import os
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
REPO = ROOT.parent
sys.path.insert(0, str(ROOT))
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-debug-persist-test.sqlite")
os.environ["KANSHO_MEDIA_ROOT"] = str(Path(tempfile.gettempdir()) / "kansho-debug-persist-media")
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
os.environ["KANSHO_FAKE_DETECT"] = "1"
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
Path(os.environ["KANSHO_MEDIA_ROOT"]).mkdir(parents=True, exist_ok=True)
from fastapi.testclient import TestClient
from identity_store import remember_mapping
from main import app
from privacy_gateway import reset_debug
def expect(ok: bool, message: str) -> None:
if not ok:
raise SystemExit(f"FAIL: {message}")
print(f"OK {message}")
def header(token: str) -> dict:
return {"X-Auth-Token": token}
def open_space_day(client, headers, title="Alltag", date="2026-08-28"):
space = client.post("/api/journal/spaces", headers=headers, json={"title": title})
expect(space.status_code == 200, f"create space {space.status_code}")
day = client.post(
f"/api/journal/spaces/{space.json()['id']}/days",
headers=headers,
json={"calendar_date": date},
)
expect(day.status_code == 200, f"open day {day.status_code}")
return space.json(), day.json()
def blob(data) -> str:
return json.dumps(data, ensure_ascii=False)
def main() -> None:
reset_debug()
ui = (REPO / "frontend" / "src" / "pages" / "AdminDebugPage.jsx").read_text(encoding="utf-8")
expect("Debug persistieren" in ui, "admin page has the persist toggle")
expect("Space / Tag / Gespräche" in ui, "admin page uses space/day/conversation structure")
expect("Gespräch-JSON" in ui, "admin page can download one conversation")
expect("JSON herunterladen" not in ui, "admin page no longer uses a flat download label as primary")
nav = (REPO / "frontend" / "src" / "config" / "adminNav.js").read_text(encoding="utf-8")
expect("/admin/debug" in nav, "admin nav lists debug")
dialogue_ui = (REPO / "frontend" / "src" / "pages" / "DialoguePage.jsx").read_text(encoding="utf-8")
expect("CallTrace" not in dialogue_ui, "dialogue page no longer shows the live test spur")
expect("Debug herunterladen" in dialogue_ui, "dialogue page can download the conversation debug log")
day_ui = (REPO / "frontend" / "src" / "pages" / "JournalDayPage.jsx").read_text(encoding="utf-8")
expect("import CallTrace" not in day_ui, "journal day does not render CallTrace under the thread")
expect("Debug herunterladen" in day_ui, "journal day can download the conversation debug log")
editor_ui = (REPO / "frontend" / "src" / "pages" / "JournalEditorPage.jsx").read_text(encoding="utf-8")
expect("import CallTrace" in editor_ui, "journal editor still shows the generate trace")
expect("<h2>Verlauf</h2>" in editor_ui, "journal editor names the generate trace")
expect("Debug herunterladen" in editor_ui, "journal editor can download the generate debug log")
expect("purpose=journal_generate" in editor_ui, "journal editor downloads the generate scope")
with TestClient(app) as client:
setup = client.post(
"/api/auth/setup",
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
)
expect(setup.status_code == 200, f"setup {setup.status_code}")
headers = header(setup.json()["token"])
profile_id = setup.json()["profile_id"]
other = client.post(
"/api/users",
headers=headers,
json={"email": "ute@example.test", "name": "Ute", "password": "user-pass", "role": "user"},
)
expect(other.status_code == 200, "second profile")
other_headers = header(
client.post("/api/auth/login", json={"email": "ute@example.test", "password": "user-pass"}).json()["token"]
)
settings = client.get("/api/admin/debug", headers=headers)
expect(settings.status_code == 200, "admin can read debug settings")
expect(settings.json()["persist_enabled"] is False, "persist is off by default")
expect(settings.json()["run_count"] == 0, "no runs at start")
denied = client.get("/api/admin/debug", headers=other_headers)
expect(denied.status_code == 403, "non-admin cannot read debug settings")
space, day = open_space_day(client, headers)
day_id = day["day"]["id"]
conv = client.post(
f"/api/journal/days/{day_id}/conversations",
headers=headers,
json={"title": "Gespräch"},
)
expect(conv.status_code == 200, f"start conversation {conv.status_code}")
conv_id = conv.json()["id"]
reset_debug()
turn = client.post(
f"/api/journal/conversations/{conv_id}/turn",
headers=headers,
json={"body": "Heute war der Markt voll. Ich habe Kirschen gekauft."},
)
expect(turn.status_code == 200, f"turn while persist off {turn.status_code}")
listed = client.get("/api/admin/debug/runs", headers=headers)
expect(listed.json()["runs"] == [], "off means no persisted steps")
enabled = client.put("/api/admin/debug", headers=headers, json={"persist_enabled": True})
expect(enabled.status_code == 200, "enable persist")
expect(enabled.json()["persist_enabled"] is True, "persist flag stored")
remember_mapping(profile_id, "Anna")
reset_debug()
first = client.post(
f"/api/journal/conversations/{conv_id}/turn",
headers=headers,
json={"body": "Anna war am Stand. Die Kirschen waren reif."},
)
expect(first.status_code == 200, f"first persisted turn {first.status_code}")
reset_debug()
second = client.post(
f"/api/journal/conversations/{conv_id}/turn",
headers=headers,
json={"body": "Später noch der Hafen, ungefähr 18:00 Uhr."},
)
expect(second.status_code == 200, f"second persisted turn {second.status_code}")
listed = client.get("/api/admin/debug/runs", headers=headers)
expect(listed.status_code == 200, "list runs")
runs = listed.json()["runs"]
expect(len(runs) >= 2, "each turn is stored")
expect(all(item.get("purpose") == "dialogue_turn" for item in runs[:2]), "listed steps are dialogue turns")
newest = client.get(f"/api/admin/debug/runs/{runs[0]['id']}", headers=headers)
expect(newest.status_code == 200, "read run detail")
payload = newest.json().get("payload") or {}
stored = blob(payload)
expect("local_label" not in stored, "mapping labels are not persisted")
expect("mapping_table" not in stored, "mapping table is not persisted")
expect("replacements" not in stored, "replacement objects are not persisted")
details = [
client.get(f"/api/admin/debug/runs/{item['id']}", headers=headers).json()
for item in runs[:2]
]
expect(
any("Hafen" in blob(item.get("payload") or {}) for item in details),
"intern keeps the step text",
)
hafen = next(item.get("payload") or {} for item in details if "Hafen" in blob(item.get("payload") or {}))
expect((hafen.get("trace") or {}).get("egress") or (hafen.get("call_traces")), "egress of the step is stored")
expect((hafen.get("user") or {}).get("body"), "user line of the step is stored")
expect((hafen.get("assistant") or {}).get("body"), "assistant line of the step is stored")
expect(hafen.get("call_traces") or (hafen.get("trace") or {}).get("egress"), "gateway calls of the step are stored")
user_space = client.post("/api/journal/spaces", headers=other_headers, json={"title": "Utes Tag"})
user_day = client.post(
f"/api/journal/spaces/{user_space.json()['id']}/days",
headers=other_headers,
json={"calendar_date": "2026-08-28"},
)
user_conv = client.post(
f"/api/journal/days/{user_day.json()['day']['id']}/conversations",
headers=other_headers,
json={"title": "Gespräch"},
)
user_turn = client.post(
f"/api/journal/conversations/{user_conv.json()['id']}/turn",
headers=other_headers,
json={"body": "Utes privater Satz bleibt ohne Debug-Speicher."},
)
expect(user_turn.status_code == 200, "non-admin can still talk")
after_user = client.get("/api/admin/debug/runs", headers=headers).json()["runs"]
expect(len(after_user) == len(runs), "non-admin turns are not persisted")
expect(
not any("Utes privater Satz" in blob(item) for item in after_user),
"admin list does not contain the other profile",
)
reset_debug()
gen = client.post(
f"/api/journal/days/{day_id}/generate",
headers=headers,
json={"conversation_ids": [conv_id]},
)
expect(gen.status_code == 200, f"generate {gen.status_code}")
after_gen = client.get("/api/admin/debug/runs", headers=headers).json()["runs"]
expect(any(item.get("purpose") == "journal_generate" for item in after_gen), "journal generate is a stored step")
exported = client.get("/api/admin/debug/export?format=json", headers=headers)
expect(exported.status_code == 200, "json export")
expect("attachment" in (exported.headers.get("content-disposition") or ""), "json is a download")
document = exported.json()
expect(document.get("kind") == "kansho.debug_export", "export names the contract")
expect(document.get("version") == 2, "export version is nested tree")
expect(document.get("scope") == "all", "full export is unscoped")
expect(document.get("run_count") == len(document.get("runs") or []), "run_count matches runs")
expect(len(document.get("runs") or []) >= 3, "export contains the dialogue and generate steps")
expect((document.get("spaces") or []), "full export has space tree")
space = (document.get("spaces") or [{}])[0]
expect(space.get("title") == "Alltag", "tree names the space")
day_node = (space.get("days") or [{}])[0]
expect(day_node.get("calendar_date") == "2026-08-28", "tree names the day")
conv_node = (day_node.get("conversations") or [{}])[0]
expect(len(conv_node.get("steps") or []) >= 2, "tree keeps dialogue steps on the conversation")
expect(day_node.get("generates"), "journal generate sits on the day")
expect("local_label" not in blob(document), "export has no mapping labels")
created = [item.get("created") for item in document["runs"]]
expect(created == sorted(created), "export is chronological")
tree = client.get("/api/admin/debug/tree", headers=headers)
expect(tree.status_code == 200, "tree endpoint")
tree_space = (tree.json().get("spaces") or [{}])[0]
expect(tree_space.get("title") == "Alltag", "live tree names the space")
tree_conv = ((tree_space.get("days") or [{}])[0].get("conversations") or [{}])[0]
expect(tree_conv.get("id") == conv_id, "live tree points at the conversation")
expect(tree_conv.get("step_count") >= 2, "live tree counts steps")
conv_export = client.get(
f"/api/admin/debug/export?format=json&conversation_id={conv_id}",
headers=headers,
)
expect(conv_export.status_code == 200, "conversation export")
conv_doc = conv_export.json()
expect(conv_doc.get("scope") == "conversation", "conversation export is scoped")
expect(conv_doc.get("conversation", {}).get("id") == conv_id, "conversation export names the dialogue")
expect(
all(item.get("purpose") == "dialogue_turn" for item in conv_doc.get("runs") or []),
"conversation export is the dialogue steps, not the day generate",
)
expect(len(conv_doc.get("runs") or []) >= 2, "conversation export has both turns")
expect("conversation" in (conv_export.headers.get("content-disposition") or ""), "conversation filename is distinct")
latest = client.get(
f"/api/admin/debug/latest?journal_day_id={day_id}&purpose=journal_generate",
headers=headers,
)
expect(latest.status_code == 200, "latest generate endpoint")
latest_run = latest.json().get("run") or {}
expect(latest_run.get("purpose") == "journal_generate", "latest run is the journal generate")
expect(latest_run.get("journal_day_id") == day_id, "latest generate belongs to the day")
expect((latest_run.get("trace") or {}).get("purpose") == "journal_generate" or (latest_run.get("trace") or {}).get("stages"), "latest generate keeps the trace")
gen_export = client.get(
f"/api/admin/debug/export?format=json&journal_day_id={day_id}&purpose=journal_generate",
headers=headers,
)
expect(gen_export.status_code == 200, "generate export")
gen_doc = gen_export.json()
expect(gen_doc.get("scope") == "journal_generate", "generate export is scoped")
expect(
all(item.get("purpose") == "journal_generate" for item in gen_doc.get("runs") or []),
"generate export is only the journal draft steps",
)
expect(len(gen_doc.get("runs") or []) >= 1, "generate export has the draft run")
expect("journal" in (gen_export.headers.get("content-disposition") or ""), "generate filename is distinct")
markdown = client.get("/api/admin/debug/export?format=markdown", headers=headers)
expect(markdown.status_code == 200, "markdown export")
text = markdown.text
expect("Kanshō Debug-Export" in text, "markdown has a title")
expect("Intern (Klartext-Vorlage, nicht gesendet)" in text, "markdown has intern section")
expect("Egress (maskiert, gesendet)" in text, "markdown has egress section")
expect("dialogue_turn" in text, "markdown lists dialogue steps")
expect("journal_generate" in text, "markdown lists generate steps")
bad = client.get("/api/admin/debug/export?format=csv", headers=headers)
expect(bad.status_code == 400, "unknown export format is rejected")
disabled = client.put("/api/admin/debug", headers=headers, json={"persist_enabled": False})
expect(disabled.json()["persist_enabled"] is False, "persist can be turned off")
count_before = len(client.get("/api/admin/debug/runs", headers=headers).json()["runs"])
reset_debug()
later = client.post(
f"/api/journal/conversations/{conv_id}/turn",
headers=headers,
json={"body": "Noch ein Satz ohne Persistenz."},
)
expect(later.status_code == 200, "turn after disable")
count_after = len(client.get("/api/admin/debug/runs", headers=headers).json()["runs"])
expect(count_after == count_before, "disable stops new writes and keeps history")
run_id = client.get("/api/admin/debug/runs", headers=headers).json()["runs"][0]["id"]
removed = client.delete(f"/api/admin/debug/runs/{run_id}", headers=headers)
expect(removed.status_code == 200, "delete one run")
cleared = client.post("/api/admin/debug/runs/clear", headers=headers)
expect(cleared.status_code == 200, "clear remaining runs")
expect(client.get("/api/admin/debug/runs", headers=headers).json()["runs"] == [], "history empty after clear")
health = client.get("/api/admin/health", headers=headers)
expect(health.json()["inventory"]["debug_persist_enabled"] is False, "health shows persist off")
expect(health.json()["inventory"]["debug_runs"] == 0, "health shows empty debug count")
print("All debug persist tests passed.")
if __name__ == "__main__":
main()
+5
View File
@@ -79,6 +79,11 @@ def main() -> None:
admin = client.get("/api/admin/health", headers=headers)
expect(admin.status_code == 200, "admin health")
expect(admin.json()["inventory"]["prompts"] == 5, "five seeded prompts")
expect(admin.json()["inventory"]["debug_persist_enabled"] is False, "debug persist off by default")
expect(admin.json()["inventory"]["debug_runs"] == 0, "no debug runs at start")
debug = client.get("/api/admin/debug", headers=headers)
expect(debug.status_code == 200 and debug.json()["persist_enabled"] is False, "debug settings default off")
providers = client.get("/api/admin/providers", headers=headers)
expect(providers.status_code == 200, "admin providers")
+58
View File
@@ -483,6 +483,64 @@ def main() -> None:
expect((third.json().get("trace") or {}).get("writing_profile", {}).get("present") is True, "confirmed profile is marked present")
expect(sum(1 for item in (third.json().get("run_log") or []) if item.get("kind") == "model_call") == 1, "profile A/B still uses one generate call")
kept_body = (
"Die Kinder verstanden kein Wort. Später fragte ich, "
"ob die Kinder überhaupt Deutsch verstehen. Die Kinder lachten."
)
def keep_groups(_messages, _policy):
return ChatResult(
content=f"Markttag\n\n{kept_body}",
model="fake",
usage={},
context_compression="disabled",
)
with patch("privacy_gateway.complete_model", keep_groups):
kept = client.post(
f"/api/journal/days/{day.json()['day']['id']}/generate",
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
expect(kept.status_code == 200, f"accepted model text {kept.text}")
expect(kept.json().get("title") == "Markttag", "title split stays technical")
expect(kept.json().get("body") == kept_body, "stored body equals demasked model body")
expect("Er" not in (kept.json().get("body") or "").split(), "generic group is not rewritten to a pronoun")
stored = f"{kept.json().get('title')}\n\n{kept.json().get('body')}".strip()
expect((kept.json().get("trace") or {}).get("reply") == stored, "trace reply is the stored draft")
expect((kept.json().get("trace") or {}).get("stored_body") == kept_body, "trace stores the same body")
expect((kept.json().get("trace") or {}).get("generate_calls") == 1, "accepted text still uses one generate call")
def mixed_surface(_messages, _policy):
body = (
"Die Gruppe verstand kein Wort. "
"Später die Gruppe wieder. "
"die Gruppen standen abseits."
)
return ChatResult(
content=f"Markttag\n\n{body}",
model="fake",
usage={},
context_compression="disabled",
)
with patch("privacy_gateway.complete_model", mixed_surface):
mixed = client.post(
f"/api/journal/days/{day.json()['day']['id']}/generate",
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
expect(mixed.status_code == 200, f"mixed surface generate {mixed.text}")
mixed_body = mixed.json().get("body") or ""
expect(mixed_body.startswith("Die Gruppe verstand"), "journal stores sentence-initial model casing")
expect("Später die Gruppe wieder" in mixed_body, "journal keeps mid-sentence model casing")
expect("die Gruppen standen" in mixed_body, "journal keeps unmatched plural surface form")
mixed_stored = f"{mixed.json().get('title')}\n\n{mixed_body}".strip()
expect((mixed.json().get("trace") or {}).get("reply") == mixed_stored, "journal trace reply is the stored draft")
expect((mixed.json().get("trace") or {}).get("stored_body") == mixed_body, "journal stored_body matches the draft")
expect("Die Gruppe verstand" in ((mixed.json().get("trace") or {}).get("raw") or ""), "journal trace raw keeps provider casing")
expect((mixed.json().get("trace") or {}).get("generate_calls") == 1, "mixed surface still uses one generate call")
def invent_title(_messages, _policy):
return ChatResult(
content="Hanna am Hafen\n\nIch ging zum Markt, auf dem es ziemlich voll war.",
@@ -42,6 +42,7 @@ from journal_generation_policy import (
publish_guideline,
save_selection,
seed_generation_instructions,
set_default_guideline,
snapshot_summary,
update_guideline,
validate_selection,
@@ -214,6 +215,9 @@ def test_runtime_has_no_numeric_policy() -> None:
expect(day.count("slot:") >= 4, "journal page has four independent slots")
expect("<select" in day, "journal page uses select fields")
expect("Diese Auswahl als Standard merken" in day, "journal page can remember explicitly")
expect("if (filled) return current" in day, "settings load does not overwrite a filled selection")
expect("Auswahl nicht mehr aktiv" in day, "stale guideline ids stay visible as invalid")
expect("Rev. ${option.revision}" in day, "options show the guideline revision")
expect("Faktenregeln und Datenschutz bleiben unabhängig" in day, "facts/privacy note remains")
editor = (FRONTEND / "src" / "pages" / "JournalEditorPage.jsx").read_text(encoding="utf-8")
expect("generation_summary" in editor, "editor can show the generation snapshot")
@@ -232,6 +236,11 @@ def test_runtime_has_no_numeric_policy() -> None:
trace_ui = (FRONTEND / "src" / "components" / "CallTrace.jsx").read_text(encoding="utf-8")
expect("Editorial Mode" not in trace_ui, "trace UI does not present source mode as a run decision")
expect("source_mode" not in trace_ui, "trace UI has no source_mode label")
expect("Chunks technisch verarbeitet" in trace_ui, "trace separates technical chunk coverage")
expect("Semantische Identitätserkennung" in trace_ui, "trace names semantic uncertainty")
expect("nicht garantiert" in trace_ui, "trace does not claim semantic completeness")
expect("Gespeicherter Entwurf" in trace_ui, "trace shows the stored draft")
expect("Technisch verarbeitete Chunks bedeuten keine vollständige" in trace_ui, "trace explains coverage is not identity certainty")
def test_seed_and_compiler() -> None:
@@ -493,6 +502,27 @@ def main() -> None:
expect((rejected.json().get("detail") or {}).get("code") == "invalid_generation_selection", "rejection uses the selection code")
after_reject = client.get("/api/journal/generation-settings", headers=headers).json()
expect(after_reject["selection"] == default_ids(), "rejected values are not stored")
save_selection(profile_id, low_ids())
set_default_guideline(seed_id("transformation", "substantial"))
expect(load_selection(profile_id)["transformation_id"] == seed_id("transformation", "correction"), "setting a catalog default does not rewrite a stored selection")
save_selection(profile_id, default_ids())
blocked_calls = {"n": 0}
def no_provider(*_a, **_k):
blocked_calls["n"] += 1
raise AssertionError("invalid selection must not call a provider")
with patch("privacy_gateway.complete_model", no_provider), patch("entity_detect.complete_chat", no_provider):
before_provider = client.post(
f"/api/journal/days/{day.json()['day']['id']}/generate",
headers=headers,
json={
"conversation_ids": [conv.json()["id"]],
"generation_selection": {**default_ids(), "transformation_id": "missing"},
},
)
expect(before_provider.status_code == 400, "invalid selection still fails closed")
expect(blocked_calls["n"] == 0, "invalid selection aborts before detect and generate")
draft = client.post(
"/api/admin/generation-instructions/journal_generate",
@@ -575,6 +605,53 @@ def main() -> None:
for fact in HARD_FACTS:
expect(fact in intern, f"hard fact remains at low policy: {fact}")
copyedit_ids = selection_of(
transformation="copyedit",
detail="complete",
voice="clear",
narrative="weighted",
)
reset_debug()
copyedit_recorder = install_test_recorder()
copyedit_run = client.post(
f"/api/journal/days/{day.json()['day']['id']}/generate",
headers=headers,
json={
"conversation_ids": [conv.json()["id"]],
"generation_selection": copyedit_ids,
"remember_generation_selection": False,
},
)
expect(copyedit_run.status_code == 200, f"copyedit generate {copyedit_run.text}")
copyedit_meta = selection_meta(copyedit_run.json())
copyedit_intern = intern_of(copyedit_run.json())
copyedit_snap = copyedit_run.json().get("generation_snapshot") or {}
expect(copyedit_meta.get("ids") == {
"transformation": seed_id("transformation", "copyedit"),
"detail": seed_id("detail", "complete"),
"voice": seed_id("voice", "clear"),
"narrative": seed_id("narrative", "weighted"),
}, "copyedit request ids reach the trace")
expect(copyedit_meta.get("keys", {}).get("transformation") == "copyedit", "copyedit key reaches the trace")
expect(copyedit_meta.get("revisions", {}).get("transformation") == 1, "copyedit revision reaches the trace")
expect(copyedit_meta.get("revisions", {}).get("narrative") == 1, "weighted revision of the selected id reaches the trace")
expect(copyedit_snap.get("transformation", {}).get("id") == seed_id("transformation", "copyedit"), "snapshot stores the selected copyedit id")
expect(copyedit_snap.get("transformation", {}).get("revision") == 1, "snapshot stores the selected copyedit revision")
expect(copyedit_snap.get("narrative", {}).get("id") == seed_id("narrative", "weighted"), "snapshot stores the selected weighted id")
expect(seed_instruction("transformation", "copyedit") in copyedit_intern, "selected copyedit instruction reaches the prompt")
expect(seed_instruction("narrative", "weighted") in copyedit_intern, "selected weighted instruction reaches the prompt")
expect(seed_instruction("transformation", "substantial") not in copyedit_intern, "unselected substantial instruction is absent")
expect([item.get("purpose") for item in copyedit_recorder] == ["journal_generate"], "copyedit still uses one generate call")
preview = client.post(
"/api/admin/generation-instructions/journal_generate/preview",
headers=headers,
json={"generation_selection": copyedit_ids},
)
expect(preview.status_code == 200, f"copyedit preview {preview.text}")
expect(preview.json()["transformation_instructions"] == seed_instruction("transformation", "copyedit"), "preview compiles the same copyedit instruction")
expect(preview.json().get("selection", {}).get("ids", {}).get("transformation") == seed_id("transformation", "copyedit"), "preview reports the same copyedit id")
expect(preview.json().get("selection", {}).get("revisions", {}).get("narrative") == 1, "preview reports the same weighted revision")
notes_day = client.post(
f"/api/journal/spaces/{space.json()['id']}/days",
headers=headers,
@@ -789,6 +866,7 @@ def main() -> None:
headers=headers,
)
expect(published.status_code == 200, f"publish {published.text}")
expect(load_selection(profile_id)["transformation_id"] == seed_id("transformation", "substantial"), "publishing a clone does not rewrite the stored selection")
immutable = client.put(
f"/api/admin/generation-instructions/journal_generate/{seed_id('transformation', 'substantial')}",
headers=headers,
+20 -25
View File
@@ -60,33 +60,28 @@ def main() -> None:
kept = "Am Morgen der Markt.\n\nSpäter das Boot, noch gehetzt."
expect(shape_journal("Tag", kept, turns)[1] == kept, "already shaped text is left alone")
source = [
"Gegen 8:30 Uhr war Sushi immer noch nicht auf dem Balkon. "
"Ich fand sie unter der Dusche. Später saß sie auf dem Bett und telefonierte. "
"Auf die Frage, ob sie mit mir frühstücken wolle, antwortete sie, dass sie erst Brot holen müsse."
]
stuffed = (
"Gegen 8:30 Uhr war Sushi immer noch nicht bei mir auf dem Balkon. "
"Ich machte mich auf die Suche und fand Sushi schließlich unter der Dusche im Badezimmer. "
"Als Sushi dann immer noch nicht auftauchte, sah ich nochmals nach und musste feststellen, "
"dass Sushi auf dem Bett saß und telefonierte. Als ich gerade mit den Vorbereitungen fertig war, "
"kam Sushi aus dem Schlafzimmer. Auf meine Frage, ob Sushi mit mir frühstücken wolle, "
"antwortete sie, dass Sushi erst Brot vom Laden holen müsse."
repeated = (
"Die Kinder verstanden kein Wort. Später fragte ich, "
"ob die Kinder überhaupt Deutsch verstehen. Die Kinder lachten."
)
from journal_shape import infer_person_gender, naturalize_person_mentions
_, unchanged = shape_journal(
"Tag",
repeated,
["Die Kinder verstanden kein Wort."],
person_labels=["Die Kinder", "Kinder"],
)
expect(unchanged == repeated, "repeated generic person terms stay unchanged")
expect("Er" not in unchanged.split(), "generic group is not turned into a pronoun")
expect(unchanged.count("Kinder") == 3, "every generic mention remains")
named = (
"Maren kam zum Markt. Später rief Maren an. "
"Ob Maren noch käme, wusste ich nicht. Maren blieb weg."
)
_, named_out = shape_journal("Tag", named, named.split(". "), person_labels=["Maren"])
expect(named_out == named, "accepted person names are not replaced by pronouns")
expect("Er " not in named_out and "Sie " not in named_out, "no guessed pronoun rewrite")
expect(infer_person_gender("Sushi", source) == "f", "source pronouns set feminine")
cleaned = naturalize_person_mentions(stuffed, ["Sushi"], source)
expect(cleaned.count("Sushi") == 2, "name kept twice then pronouns")
expect("fand Sushi" in cleaned, "second mention may stay a name")
expect("dass sie erst Brot" in cleaned, "reported speech keeps sie not the name")
expect("ob sie mit mir" in cleaned, "ob-clause is sie")
expect("Als sie dann" in cleaned, "later subject becomes pronoun")
expect("esse gerne Sushi" in naturalize_person_mentions(
"Ich esse gerne Sushi mit meiner Frau Sushi. Danach aß ich Sushi.",
["Sushi"],
source,
), "food homonym is not turned into a pronoun")
print("journal shape ok")
+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,
+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):