MVP 0.93 - stabile, aber Writing Profile ohne erkennbaren Nutzen

This commit is contained in:
Lars committed 2026-08-27 10:03:05 +02:00
1 parent 53b1eff833
commit f17b3083e8
56 files changed
+5366 -784

No files matched your search

+106 -24
View File
@@ -39,6 +39,7 @@ from writing_profile_store import (
get_profile,
import_text,
replace_trait_refs,
set_lifecycle,
upsert_trait,
update_facet,
)
@@ -769,6 +770,7 @@ def test_task_brief_and_dedupe(client: TestClient, headers: dict, profile_id: st
}
],
)
set_lifecycle(profile_id, "confirmed")
before = get_profile(profile_id)
brief = compile_task_brief(profile_id, "journal_generate")
after = get_profile(profile_id)
@@ -777,8 +779,8 @@ def test_task_brief_and_dedupe(client: TestClient, headers: dict, profile_id: st
expect(_ends_on_word_boundary(brief), "task brief does not cut inside a word")
for slug, statement in statements:
expect(slug in brief or statement.split(",")[0] in brief, f"relevant trait {slug} can be included")
expect(brief.count("Repräsentative Exemplare:") <= 1, "at most one exemplar section")
expect(len([line for line in brief.splitlines() if line.startswith("- ") and "Beispiel" not in line]) >= 6, "six traits considered")
expect("Repräsentative Exemplare:" not in brief, "historical exemplars are not mixed into the compact brief")
expect(len([line for line in brief.splitlines() if line.startswith("- ")]) >= 6, "six traits considered")
expect(before["sources"] == after["sources"], "compile_task_brief does not mutate stored sources")
expect(
[(item.get("slug"), item.get("statement")) for item in before["traits"]]
@@ -836,22 +838,43 @@ def test_generate_flow(client: TestClient, headers: dict) -> None:
json={"conversation_ids": [conv.json()["id"]]},
)
expect(gen.status_code == 200, f"generate {gen.status_code}")
expect([item.get("purpose") for item in recorder] == ["journal_reconstruct", "journal_generate"], "both stages ran")
expect([item.get("purpose") for item in recorder] == ["journal_generate"], "runtime generate calls only narration")
run_log = gen.json().get("run_log") or (gen.json().get("trace") or {}).get("log") or []
expect(any(item.get("kind") == "model_call" for item in run_log), "run log records model calls")
expect(
any(item.get("kind") == "stage1_result" and item.get("stage") == "local_source_artifact" for item in run_log),
"run log records local stage 1",
)
expect(sum(1 for item in run_log if item.get("kind") == "detect") <= 1, "at most one detect call")
expect(sum(1 for item in run_log if item.get("kind") == "model_call") == 1, "exactly one generate model call")
expect(
not any(key in (item or {}) for item in run_log for key in ("intern", "egress", "raw", "reply", "local_label")),
"run log omits prompt bodies and labels",
)
stages = {item.get("purpose"): item for item in (gen.json().get("trace") or {}).get("stages") or []}
expect("journal_reconstruct" in stages, "stage 1 ran through the gateway")
expect("local_source_artifact" in stages, "stage 1 is the local source artifact")
expect("journal_generate" in stages, "stage 2 ran through the gateway")
reconstruct = stages["journal_reconstruct"].get("intern") or ""
expect("journal_reconstruct" not in stages, "runtime path does not call journal_reconstruct")
reconstruct = stages["local_source_artifact"].get("intern") or ""
narrate = stages["journal_generate"].get("intern") or ""
expect("[u1]" in reconstruct and "user:" in reconstruct, "stage 1 sees labeled user sources")
expect("assistant:" in reconstruct, "stage 1 may see assistant as context")
expect(stages["local_source_artifact"].get("status") == "local_ok", "local stage 1 succeeded")
expect(stages["local_source_artifact"].get("provider") in (None, ""), "local stage has no provider")
expect(stages["local_source_artifact"].get("model") in (None, ""), "local stage has no model")
expect((stages["local_source_artifact"].get("budget") or {}).get("prompt_tokens") == 0, "local stage has no tokens")
expect("verified_artifact" in reconstruct and "u1" in reconstruct, "local artifact contains source ids")
expect("assistant:" not in reconstruct, "assistant lines are not in the artifact")
expect("Markt" in reconstruct and "Hafen" in reconstruct, "every selected user source is in the artifact")
expect("assistant:" not in narrate, "stage 2 does not resend assistant lines")
expect("user:" not in narrate, "stage 2 does not resend the raw dialogue")
expect("verified_artifact" in narrate and "source_order" in narrate, "stage 2 receives the local verified artifact")
expect("sources" in narrate, "stage 2 artifact contains rehydrated sources")
expect("CURRENT_DAY_SOURCES" in narrate, "stage 2 labels current-day facts")
expect("[u1]" in narrate, "stage 2 lists source ids")
expect("7:30" in reconstruct or "7:30" in narrate, "times survive into generation")
expect("Markt" in narrate and "Hafen" in narrate, "full selected user sources reach stage 2")
expect("Vielleicht bleibe ich kürzer" in narrate, "omitted labels do not drop canonical hedges")
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")
reset_debug()
short_recorder = install_test_recorder()
@@ -886,14 +909,14 @@ def test_generate_flow(client: TestClient, headers: dict) -> None:
)
expect(short_gen.status_code == 200, f"short-claim generate {short_gen.status_code}")
expect(
[item.get("purpose") for item in short_recorder] == ["journal_reconstruct", "journal_generate"],
"short claims still run both stages because sources are rehydrated",
[item.get("purpose") for item in short_recorder] == ["journal_generate"],
"invalid former stage-1 JSON is irrelevant because reconstruct is not called",
)
short_narrate = ""
for item in (short_gen.json().get("trace") or {}).get("stages") or []:
if item.get("purpose") == "journal_generate":
short_narrate = item.get("intern") or ""
expect("Kirschen" in short_narrate and "Hafen" in short_narrate, "short claims cannot drop the rest of the sources")
expect("Kirschen" in short_narrate and "Hafen" in short_narrate, "local artifact cannot drop selected sources")
expect("\"feeling\"" not in short_narrate, "feeling labels do not reach stage 2 as facts")
budget = (gen.json().get("trace") or {}).get("budget") or stages["journal_generate"].get("budget") or {}
expect(budget.get("reserved_output_tokens") == 4096, "trace records reserved output")
@@ -930,22 +953,23 @@ def test_generate_flow(client: TestClient, headers: dict) -> None:
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
expect(denied_recon.status_code == 200, f"incomplete reconstruct falls back locally {denied_recon.text}")
expect(denied_recon.status_code == 200, f"former reconstruct JSON is unused {denied_recon.text}")
fallback_body = denied_recon.json().get("body") or ""
expect("Markt" in fallback_body or "Hafen" in fallback_body, "local artifact still produces a draft")
fallback_stages = {item.get("purpose"): item for item in (denied_recon.json().get("trace") or {}).get("stages") or []}
expect(fallback_stages.get("journal_reconstruct", {}).get("stage1") == "local_fallback", "invalid stage 1 is marked local_fallback")
expect(fallback_stages.get("local_source_artifact", {}).get("stage1") == "local_ok", "stage 1 is local and successful")
expect(
(fallback_stages.get("journal_reconstruct") or {}).get("reason") == "incomplete_coverage",
"fallback records why the model JSON was rejected",
fallback_stages.get("local_source_artifact", {}).get("coverage") == "all_selected_sources"
or "verified_artifact" in ((fallback_stages.get("local_source_artifact") or {}).get("intern") or ""),
"local coverage remains all selected sources",
)
expect("journal_generate" in fallback_stages, "stage 2 still runs on the local artifact")
narrate_fallback = fallback_stages.get("journal_generate", {}).get("intern") or ""
expect("Heute um 7:30 Uhr Markt" in narrate_fallback, "first user source is rehydrated locally")
expect("Später noch der Hafen" in narrate_fallback, "omitted model source is still in the local artifact")
expect("Später noch der Hafen" in narrate_fallback, "second user source is still in the local artifact")
expect(
[item.get("purpose") for item in skip_recorder] == ["journal_reconstruct", "journal_generate"],
"stage 2 is called with local coverage after invalid reconstruction",
[item.get("purpose") for item in skip_recorder] == ["journal_generate"],
"no reconstruct provider call after unused model JSON",
)
reset_catalog()
@@ -1010,17 +1034,74 @@ def test_generate_identity_leak_local_fallback(client: TestClient, headers: dict
)
expect(gen.status_code == 200, f"identity leak still yields a local draft {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",
)
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")
stages = {item.get("purpose"): item for item in (payload.get("trace") or {}).get("stages") or []}
reconstruct = stages.get("journal_reconstruct") or {}
reconstruct = stages.get("local_source_artifact") or {}
narrate = stages.get("journal_generate") or {}
expect(
reconstruct.get("reason") == "identity_leak_blocked" or reconstruct.get("guard") == "identity_leak_blocked",
"stage 1 records the blocked leak",
)
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")
def test_generate_inactive_mapping_keeps_model_text(client: TestClient, headers: dict, profile_id: str) -> None:
from identity_store import remember_mapping
from providers import ChatResult
remember_mapping(profile_id, "Clarissa", "PERSON:99")
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Inaktiv"})
day = client.post(
f"/api/journal/spaces/{space.json()['id']}/days",
headers=headers,
json={"calendar_date": "2026-08-26"},
)
conv = client.post(
f"/api/journal/days/{day.json()['day']['id']}/conversations",
headers=headers,
json={"title": "Tag"},
)
turn = client.post(
f"/api/journal/conversations/{conv.json()['id']}/turn",
headers=headers,
json={"body": "Heute Markt, danach Kirschen am Hafen."},
)
expect(turn.status_code == 200, f"inactive-mapping setup {turn.status_code}")
calls = {"n": 0}
def keep(_messages, _policy):
calls["n"] += 1
return ChatResult(
content="Ein Markttag\n\nHeute Markt, danach Kirschen am Hafen. Clarissa blieb unerwähnt.",
model="fake",
usage={},
context_compression="disabled",
)
with patch("privacy_gateway.complete_model", keep):
gen = client.post(
f"/api/journal/days/{day.json()['day']['id']}/generate",
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(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")
def test_traces_are_request_scoped(client: TestClient, headers: dict) -> None:
@@ -1167,6 +1248,7 @@ def main() -> None:
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_inactive_mapping_keeps_model_text(client, headers, profile_id)
test_traces_are_request_scoped(client, headers)
test_two_conversations_same_day(client, headers)
print("journal budget tests passed.")