This commit is contained in:
Lars committed 2026-08-28 08:40:28 +02:00
1 parent f17b3083e8
commit 4f37991ad1
48 files changed
+4919 -540

No files matched your search

+31 -21
View File
@@ -874,7 +874,7 @@ def test_generate_flow(client: TestClient, headers: dict) -> None:
expect("assistant:" not in (gen.json().get("body") or ""), "draft body has no assistant lines")
expect("WRITING_PROFILE" in narrate or "Neutraler Journalstil" in narrate or "Core:" in narrate, "writing profile reaches generate")
expect("STYLE_EXAMPLES" in narrate, "style examples are labeled separately from day facts")
expect("Erzählmerkmale" not in (narrate.split("CURRENT_DAY_SOURCES")[0] if "CURRENT_DAY_SOURCES" in narrate else narrate), "day dialogue is not a style brief")
expect("Erzählmerkmale" not in (narrate.split("\nCURRENT_DAY_SOURCES\n")[0] if "\nCURRENT_DAY_SOURCES\n" in narrate else narrate), "day dialogue is not a style brief")
reset_debug()
short_recorder = install_test_recorder()
@@ -995,7 +995,7 @@ def test_generate_flow(client: TestClient, headers: dict) -> None:
reset_catalog()
def test_generate_identity_leak_local_fallback(client: TestClient, headers: dict, profile_id: str) -> None:
def test_generate_attested_cleartext_is_normalized(client: TestClient, headers: dict, profile_id: str) -> None:
from identity_store import remember_mapping
from providers import ChatResult
@@ -1016,13 +1016,15 @@ def test_generate_identity_leak_local_fallback(client: TestClient, headers: dict
headers=headers,
json={"body": "Heute war ich mit Anna am Markt, danach Kirschen."},
)
expect(turn.status_code == 200, f"identity-leak setup turn {turn.status_code}")
expect(turn.status_code == 200, f"identity-cleartext setup turn {turn.status_code}")
calls = {"n": 0}
def leak(_messages, _policy):
calls["n"] += 1
return ChatResult(
content="Anna stand den ganzen Nachmittag am Markt.",
model="fake",
usage={},
usage={"prompt_tokens": 11, "completion_tokens": 9, "total_tokens": 20, "cost": 0.004},
context_compression="disabled",
)
@@ -1032,24 +1034,20 @@ def test_generate_identity_leak_local_fallback(client: TestClient, headers: dict
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
expect(gen.status_code == 200, f"identity leak still yields a local draft {gen.text}")
expect(gen.status_code == 200, f"attested cleartext is accepted locally {gen.text}")
payload = gen.json()
leak_log = payload.get("run_log") or (payload.get("trace") or {}).get("log") or []
expect(any(item.get("kind") == "retry" for item in leak_log), "identity leak records a retry")
expect(
any(item.get("reason") == "identity_leak_blocked" for item in leak_log),
"identity leak records local fallback",
)
expect(calls["n"] == 1, "attested cleartext does not retry")
expect(not any(item.get("kind") == "retry" for item in leak_log), "no privacy retry event")
body = payload.get("body") or ""
expect("Markt" in body and "Kirschen" in body, "local draft keeps attested user wording")
expect("Anna" in body, "local draft may keep names that the user actually wrote")
expect("Anna" in body, "source-attested name is demasked")
expect("Markt" in body, "model wording is kept after normalization")
stages = {item.get("purpose"): item for item in (payload.get("trace") or {}).get("stages") or []}
reconstruct = stages.get("local_source_artifact") or {}
narrate = stages.get("journal_generate") or {}
expect(reconstruct.get("status") == "local_ok", "stage 1 stays local and does not call reconstruct")
expect(narrate.get("guard") == "identity_leak_blocked", "stage 2 does not use the leaking reply")
expect(sum(1 for item in leak_log if item.get("kind") == "retry") == 1, "active leak retries at most once")
expect(sum(1 for item in leak_log if item.get("kind") == "model_call") <= 2, "retry is the only extra model call")
expect(narrate.get("model_text_accepted") is True, "stage 2 accepts the normalized model text")
expect((payload.get("trace") or {}).get("generate_calls") == 1 or (narrate.get("generate_calls") == 1), "exactly one generate call")
def test_generate_inactive_mapping_keeps_model_text(client: TestClient, headers: dict, profile_id: str) -> None:
@@ -1091,17 +1089,29 @@ def test_generate_inactive_mapping_keeps_model_text(client: TestClient, headers:
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
expect(gen.status_code == 200, f"inactive mapping generate {gen.text}")
payload = gen.json()
run_log = payload.get("run_log") or (payload.get("trace") or {}).get("log") or []
expect(gen.status_code == 409, f"inactive mapping generate {gen.text}")
detail = gen.json().get("detail") or {}
expect(detail.get("code") == "journal_generation_not_accepted", "historical-only name is not stored as a draft")
expect(detail.get("message") == "Generierung nicht übernommen.", "API names the rejection")
diag = detail.get("diagnostics") or {}
run_log = diag.get("log") or (diag.get("trace") or {}).get("log") or []
expect(calls["n"] == 1, "inactive mapping does not trigger a retry")
expect(not any(item.get("kind") == "retry" for item in run_log), "inactive mapping does not retry")
expect(
any(item.get("reason") == "unattested_identity" for item in run_log),
"invented historical name is unattested journal content",
)
expect("Clarissa" not in (payload.get("body") or ""), "unattested name is not accepted as narration")
expect("Markt" in (payload.get("body") or "") and "Kirschen" in (payload.get("body") or ""), "fallback keeps user sources")
expect(diag.get("model_text_accepted") is False, "unattested model text is not accepted")
day_after = client.get(f"/api/journal/days/{day.json()['day']['id']}", headers=headers)
expect(not (day_after.json().get("current_draft")), "rejected generate does not insert a draft")
trace = diag.get("trace") or {}
expect(trace.get("abort_reason") == "unattested_identity", "abort reason remains on the error trace")
expect(
(trace.get("generate_calls") == 1)
or ((trace.get("budget") or {}).get("generate_calls") == 1)
or diag.get("generate_calls") == 1,
"error trace keeps the generate-call count",
)
def test_traces_are_request_scoped(client: TestClient, headers: dict) -> None:
@@ -1247,7 +1257,7 @@ def main() -> None:
profile_id = setup.json()["profile_id"]
test_task_brief_and_dedupe(client, headers, profile_id)
test_generate_flow(client, headers)
test_generate_identity_leak_local_fallback(client, headers, profile_id)
test_generate_attested_cleartext_is_normalized(client, headers, profile_id)
test_generate_inactive_mapping_keeps_model_text(client, headers, profile_id)
test_traces_are_request_scoped(client, headers)
test_two_conversations_same_day(client, headers)