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

+147 -45
View File
@@ -20,11 +20,9 @@ from db import get_db, init_db
from engine import load_active_prompt
from identity_store import remember_mapping
from journal_editorial import (
NOTES_TO_JOURNAL,
PROSE_EDIT,
choose_editorial_mode,
editorial_instructions,
GENERATE_SEED_REVISION,
format_style_examples,
incomplete_syntax_markers,
lexical_similarity,
narration_sources_text,
select_journal_style_examples,
@@ -43,7 +41,13 @@ from writing_profile_store import (
)
SEED_REVISION = "2026-08-27-journal-placeholders-v1"
SEED_REVISION = GENERATE_SEED_REVISION
MIXED_SOURCES_INSTRUCTION = (
"Die Quellen können aus Fließtext, Stichpunkten und Satzfragmenten bestehen. "
"Behandle jede Passage entsprechend ihrer Form: Redigiere vorhandenen Fließtext, "
"verwandle Stichpunkte und Fragmente in vollständige Prosa und verbinde beides zu einem einheitlichen Eintrag. "
"Zwinge nicht den gesamten Tag in einen einzigen Quellenmodus."
)
def expect(ok: bool, message: str) -> None:
@@ -56,43 +60,36 @@ def header(token: str) -> dict:
return {"X-Auth-Token": token}
def test_mode_choice() -> None:
expect(choose_editorial_mode(["Ich ging zum Markt. Es war voll."]) == PROSE_EDIT, "narrative sentences select prose_edit")
expect(choose_editorial_mode(["markt", "kirschen", "hafen"]) == NOTES_TO_JOURNAL, "fragments select notes_to_journal")
expect(
choose_editorial_mode(["Ich war am Markt.", "Kirschen gekauft.", "hafen später"]) == PROSE_EDIT,
"mixed default is prose_edit when at least half the blocks have sentence punctuation",
)
expect(
choose_editorial_mode(["markt", "kirschen", "Später der Hafen."]) == NOTES_TO_JOURNAL,
"mixed default is notes_to_journal when the majority lacks sentence punctuation",
)
prose = editorial_instructions(PROSE_EDIT)
notes = editorial_instructions(NOTES_TO_JOURNAL)
expect("Gute Formulierungen bewahren" in prose, "prose_edit keeps good wording")
expect("zusammenhängende Journalprosa" in notes, "notes_to_journal asks for connected prose")
expect(prose != notes, "modes produce different instructions")
expect("nicht inklusive ihrer Fehler" in notes or "Fehler hintereinanderkopieren" in notes, "notes must not be concatenated with errors")
def test_syntax_diagnostics() -> None:
expect(incomplete_syntax_markers("Danach sprach ich mit dem.") > 0, "dangling determiner is diagnostic")
expect(incomplete_syntax_markers("Danach sprach ich und kam zurück.") == 0, "complete sentence is not flagged")
def test_prompt_contract() -> None:
init_db()
prompt = load_active_prompt("mvp.journal_generate")
text = prompt.get("template") or ""
expect("Faktentreue ist nicht Wortlauttreue" in text, "prompt separates fact fidelity from wording")
expect("INHALTSTREUE" in text, "prompt keeps hard content rules")
expect("CURRENT_DAY_SOURCES" in text, "prompt labels current-day facts")
expect("WRITING_PROFILE" in text, "prompt labels the writing profile")
expect("STYLE_EXAMPLES" in text, "prompt labels style examples")
expect("EDITORIAL_MODE" in text, "prompt exposes editorial mode")
expect("keine geschützten Fakten" in text, "prompt says typos are not protected facts")
expect("{{editorial_instructions}}" in text, "prompt injects mode-specific instructions")
expect("WRITING_PROFILE" in text, "prompt labels the writing profile")
expect("{{transformation_instructions}}" in text, "prompt injects compiled transformation instructions")
expect("{{detail_instructions}}" in text, "prompt injects compiled detail instructions")
expect("{{voice_instructions}}" in text, "prompt injects compiled voice instructions")
expect("{{narrative_instructions}}" in text, "prompt injects compiled narrative instructions")
expect("{{source_mode_instructions}}" not in text, "prompt has no source-mode placeholder")
expect(MIXED_SOURCES_INSTRUCTION in text, "prompt contains the mixed-source instruction")
expect("{{writing_profile}}" in text, "prompt injects writing profile")
expect("{{style_examples}}" in text, "prompt injects style examples")
expect("{{reconstruction}}" in text, "prompt injects current-day sources")
expect("{{existing_text}}" in text, "prompt injects existing text")
expect("[[…" not in text and "[[..." not in text, "prompt must not teach ellipsis placeholders")
expect("zeichengetreu" in text, "prompt asks to copy existing placeholders unchanged")
expect("ich gieng zum laden" in text, "prompt includes a synthetic prose_edit example")
expect("Im Laden holte ich Brot" in text, "prompt includes a synthetic notes_to_journal example")
expect("ich gieng zum laden" not in text, "synthetic prose_edit example is gone")
expect("nachbarhund im garten" not in text, "synthetic notes_to_journal example is gone")
expect("Nur den verifizierten Nutzerwortlaut" not in text, "old wording-as-output rule is gone")
expect("Unsicherheiten im Wortlaut" not in text, "old keep-uncertainty-in-wording rule is gone")
expect("EDITORIAL_MODE" not in text, "source mode is no longer a labeled user setting")
expect("Faktentreue ist nicht Wortlauttreue" not in text, "old redundant fidelity lecture is gone")
with get_db() as conn:
row = conn.execute(
"SELECT seed_revision, template, default_template FROM ai_prompts WHERE slug = ?",
@@ -163,8 +160,6 @@ def test_budget_pack_drops_examples_first() -> None:
assembled = {
"writing_profile": "Core: kurze Sätze.",
"reconstruction": "Heute Markt.",
"editorial_mode": PROSE_EDIT,
"editorial_instructions": "x",
"style_examples": "",
"existing_text": "",
}
@@ -209,7 +204,7 @@ def intern_of(payload: dict) -> str:
def main() -> None:
test_mode_choice()
test_syntax_diagnostics()
test_prompt_contract()
test_sources_and_examples_are_separated()
test_unattested_covers_title_and_body()
@@ -256,16 +251,22 @@ def main() -> None:
)
expect(gen.status_code == 200, f"generate {gen.status_code}")
intern = intern_of(gen.json())
expect("EDITORIAL_MODE: prose_edit" in intern, "narrative source selects prose_edit")
expect("Gute Formulierungen bewahren" in intern, "prose_edit instructions reach the model")
expect(MIXED_SOURCES_INSTRUCTION in intern, "unified mixed-source instruction reaches the model")
expect("CURRENT_DAY_SOURCES" in intern, "day facts are labeled")
expect("STYLE_EXAMPLES" in intern, "style examples are labeled")
expect("WRITING_PROFILE" in intern, "writing profile is labeled")
expect("Überarbeite den Text substanziell" in intern, "default transformation policy reaches the prompt")
expect("Erhalte sämtliche belegten Ereignisse" in intern, "default detail policy reaches the prompt")
expect("Neutraler Journalstil" in intern, "without a confirmed profile the neutral fallback is used")
expect("zimlich" in intern.split("CURRENT_DAY_SOURCES")[-1], "today remains content, including typos")
expect("zimlich" not in intern.split("CURRENT_DAY_SOURCES")[0], "today is not a style authority")
expect("zimlich" in intern.split("\nCURRENT_DAY_SOURCES\n")[-1], "today remains content, including typos")
expect("zimlich" not in intern.split("\nCURRENT_DAY_SOURCES\n")[0], "today is not a style authority")
expect(sum(1 for item in (gen.json().get("run_log") or []) if item.get("kind") == "model_call") == 1, "normal path is one generate call")
expect((gen.json().get("trace") or {}).get("editorial_mode") == PROSE_EDIT, "editorial mode is in the admin trace")
expect("editorial_mode" not in (gen.json().get("trace") or {}), "trace has no abandoned editorial mode")
expect((gen.json().get("trace") or {}).get("narration_source") == "model", "accepted model path is recorded")
expect((gen.json().get("trace") or {}).get("prompt_revision") == SEED_REVISION, "prompt revision is in the admin trace")
expect((gen.json().get("trace") or {}).get("writing_profile", {}).get("neutral_fallback") is True, "unconfirmed profile is visible as fallback")
expect((gen.json().get("trace") or {}).get("style_examples", {}).get("count") == 0, "no historical examples yet")
expect((gen.json().get("trace") or {}).get("dropped_optional_blocks") == [], "nothing dropped on a small day")
notes_day = client.post(
f"/api/journal/spaces/{space.json()['id']}/days",
@@ -289,9 +290,8 @@ def main() -> None:
)
expect(notes_gen.status_code == 200, f"notes generate {notes_gen.text}")
notes_intern = intern_of(notes_gen.json())
expect("EDITORIAL_MODE: notes_to_journal" in notes_intern, "fragments select notes_to_journal")
expect("zusammenhängende Journalprosa" in notes_intern, "notes mode reaches the model")
expect("Gute Formulierungen bewahren" not in notes_intern, "prose_edit instructions are not used for notes")
expect(MIXED_SOURCES_INSTRUCTION in notes_intern, "notes use the same mixed-source instruction")
expect("markt" in notes_intern.lower() and "kirschen" in notes_intern.lower(), "notes sources remain complete")
import_text(
profile_id,
@@ -360,7 +360,7 @@ def main() -> None:
expect(body.lower().count("markt") <= 2, "repetition is allowed to be reduced")
expect("traurig" not in body.lower() and "weil" not in body.lower(), "no invented feeling or cause in the patched rewrite")
intern2 = intern_of(second.json())
expect("trockener Schnitt" in intern2.split("CURRENT_DAY_SOURCES")[0], "confirmed writing profile reaches generate")
expect("trockener Schnitt" in intern2.split("\nCURRENT_DAY_SOURCES\n")[0], "confirmed writing profile reaches generate")
expect("Hafen blieb hinter der Fähre" not in body, "historical style facts are not copied into today")
expect(
sum(1 for item in (second.json().get("run_log") or []) if item.get("kind") == "model_call") == 1,
@@ -386,6 +386,103 @@ def main() -> None:
expect("Am Markt holte ich Kirschen" in notes_body, "notes become connected prose")
expect("kirschen\nspäter" not in notes_body.lower(), "notes are not concatenated as fragments")
broken_day = client.post(
f"/api/journal/spaces/{space.json()['id']}/days",
headers=headers,
json={"calendar_date": "2026-08-24"},
)
broken_conv = client.post(
f"/api/journal/days/{broken_day.json()['day']['id']}/conversations",
headers=headers,
json={"title": "Bruch"},
)
client.post(
f"/api/journal/conversations/{broken_conv.json()['id']}/turn",
headers=headers,
json={"body": "ich gieng zum laden. danach sprach ich mit dem und kam zurück. es war kald."},
)
def rebuilt(_messages, _policy):
return ChatResult(
content="Ladengang\n\nIch ging zum Laden. Danach sprach ich und kam zurück. Es war kalt.",
model="fake",
usage={},
context_compression="disabled",
)
with patch("privacy_gateway.complete_model", rebuilt):
broken_out = client.post(
f"/api/journal/days/{broken_day.json()['day']['id']}/generate",
headers=headers,
json={"conversation_ids": [broken_conv.json()["id"]]},
)
broken_body = broken_out.json().get("body") or ""
expect("gieng" not in broken_body.lower(), "general spelling error is not kept")
expect("kald" not in broken_body.lower(), "second general spelling error is not kept")
expect("mit dem" not in broken_body.lower(), "dangling determiner is not kept")
expect("kam zurück" in broken_body, "attested continuation stays")
expect(incomplete_syntax_markers(broken_body) == 0, "patched rewrite has no incomplete syntax")
expect((broken_out.json().get("trace") or {}).get("incomplete_syntax") == 0, "incomplete syntax is a diagnostic")
weight_day = client.post(
f"/api/journal/spaces/{space.json()['id']}/days",
headers=headers,
json={"calendar_date": "2026-08-23"},
)
weight_conv = client.post(
f"/api/journal/days/{weight_day.json()['day']['id']}/conversations",
headers=headers,
json={"title": "Notizen"},
)
client.post(
f"/api/journal/conversations/{weight_conv.json()['id']}/turn",
headers=headers,
json={"body": "morgens tee\nspäter markt\ngegen abend unerwartet der nachbarhund im garten"},
)
def weighted(_messages, _policy):
return ChatResult(
content=(
"Nachbarhund\n\n"
"Morgens trank ich Tee, später war ich am Markt. "
"Es war ein gewöhnlicher Tag – bis gegen Abend unerwartet der Nachbarhund im Garten war."
),
model="fake",
usage={},
context_compression="disabled",
)
with patch("privacy_gateway.complete_model", weighted):
weight_out = client.post(
f"/api/journal/days/{weight_day.json()['day']['id']}/generate",
headers=headers,
json={"conversation_ids": [weight_conv.json()["id"]]},
)
weight_intern = intern_of(weight_out.json())
expect(MIXED_SOURCES_INSTRUCTION in weight_intern, "fragment notes still use the unified instruction")
weight_body = weight_out.json().get("body") or ""
expect("bis gegen Abend" in weight_body or "unerwartet" in weight_body, "attested standout may be weighted")
expect("morgens tee\nspäter markt" not in weight_body.lower(), "notes are not a concatenated list")
expect("glücklich" not in weight_body.lower() and "weil" not in weight_body.lower(), "weighting does not invent feeling or cause")
update_facet(profile_id, "core", value="Lange, ruhig fließende Sätze, behutsame Wortwahl, leise Reflexion.")
set_lifecycle(profile_id, "confirmed")
brief_b = compile_task_brief(profile_id)
expect("fließende Sätze" in brief_b, "second confirmed core compiles")
with patch("privacy_gateway.complete_model", rewritten):
third = client.post(
f"/api/journal/days/{day.json()['day']['id']}/generate",
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
intern3 = intern_of(third.json())
style3 = intern3.split("\nCURRENT_DAY_SOURCES\n")[0]
expect("fließende Sätze" in style3, "second writing profile reaches the rendered prompt")
expect("trockener Schnitt" not in style3, "replaced core is not still the style authority")
expect((third.json().get("trace") or {}).get("writing_profile", {}).get("has_core") is True, "core presence is in the trace")
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")
def invent_title(_messages, _policy):
return ChatResult(
content="Hanna am Hafen\n\nIch ging zum Markt, auf dem es ziemlich voll war.",
@@ -400,8 +497,13 @@ def main() -> None:
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
expect(any(item.get("reason") == "unattested_identity" for item in (blocked.json().get("run_log") or [])), "title identity uses local fallback")
expect("Hanna" not in (blocked.json().get("title") or "") and "Hanna" not in (blocked.json().get("body") or ""), "unattested title identity is not kept")
expect(blocked.status_code == 409, f"unattested title {blocked.text}")
detail = blocked.json().get("detail") or {}
expect(detail.get("code") == "journal_generation_not_accepted", "unattested title is not stored as a draft")
expect(detail.get("message") == "Generierung nicht übernommen.", "API names the rejection")
log = (detail.get("diagnostics") or {}).get("log") or []
expect(any(item.get("reason") == "unattested_identity" for item in log), "title identity is a provenance reject")
expect((detail.get("diagnostics") or {}).get("trace", {}).get("model_text_accepted") is False, "model text is not accepted")
with get_db() as conn:
conn.execute(
@@ -416,7 +518,7 @@ def main() -> None:
"SELECT default_template, seed_revision FROM ai_prompts WHERE slug = ?",
("mvp.journal_generate",),
).fetchone()
expect("Faktentreue ist nicht Wortlauttreue" in (row["default_template"] or ""), "default template still tracks the seed")
expect("INHALTSTREUE" in (row["default_template"] or ""), "default template still tracks the seed")
expect(row["seed_revision"] == SEED_REVISION, "revision updates even when template is custom")
print("journal editorial tests passed.")