This commit is contained in:
Lars committed 2026-08-29 20:28:59 +02:00
1 parent be11f82f84
commit e42f751fdc
47 files changed
+3359 -301

No files matched your search

+505
View File
@@ -0,0 +1,505 @@
"""Bounded detect-contract retry, acyclic diagnostics, persist must not hide EngineError.
Run from backend/: python tests/test_detect_contract_retry.py
Fake detect/provider only. No live calls.
"""
from __future__ import annotations
import json
import os
import sys
import tempfile
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-detect-contract-retry-test.sqlite")
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
os.environ["KANSHO_FAKE_DETECT"] = "1"
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
from fastapi.testclient import TestClient
from debug_store import persist_engine_error, sanitize
from engine import EngineError
from entity_detect import (
DetectError,
detect_personal_egress,
install_test_detect_failure,
install_test_detect_script,
reset_detect_test_hooks,
split_detect_chunks,
validate_detected_entity,
)
from journal_generate import _compose_journal_trace, _stage_trace
from main import app
from privacy_gateway import GatewayRequest, PrivacyGatewayError, complete, reset_debug
from providers import ChatResult
USAGE_A = {"prompt_tokens": 10, "completion_tokens": 3, "total_tokens": 13, "cost": 0.001}
USAGE_B = {"prompt_tokens": 8, "completion_tokens": 2, "total_tokens": 10, "cost": 0.0005}
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 _ids(obj, stack=None) -> bool:
"""True if obj is acyclic."""
if not isinstance(obj, (dict, list, tuple)):
return True
stack = stack or set()
ident = id(obj)
if ident in stack:
return False
stack.add(ident)
try:
items = obj.values() if isinstance(obj, dict) else obj
return all(_ids(item, stack) for item in items)
finally:
stack.discard(ident)
def _budget_is_not_diagnostics(payload: dict) -> bool:
diag = payload.get("diagnostics") if isinstance(payload.get("diagnostics"), dict) else payload
trace = diag.get("trace") if isinstance(diag.get("trace"), dict) else {}
budget = trace.get("budget")
if budget is diag:
return False
for stage in trace.get("stages") or []:
if isinstance(stage, dict) and stage.get("budget") is diag:
return False
return True
def _run(profile_id: str, rendered: str, purpose: str = "dialogue_turn"):
return complete(
GatewayRequest(
prompt_id="detect-retry-test",
purpose=purpose,
data_class="B",
profile_id=profile_id,
payload={"rendered": rendered, "source_text": rendered},
)
)
def _chat(entities: list[dict], usage: dict) -> ChatResult:
return ChatResult(
content=json.dumps({"entities": entities}),
model="fake-detect",
usage=usage,
finish_reason="stop",
)
def _invalid_type():
return [{"start": 0, "end": 1, "text": "x", "entity_type": "FOOD"}]
def _extra_field():
return [{"start": 0, "end": 1, "text": "x", "entity_type": "PERSON", "score": 0.9}]
def _missing_field():
return [{"start": 0, "text": "x", "entity_type": "PERSON"}]
def _bad_offset(chunk_text: str):
return [{"start": 0, "end": 5, "text": "ZZZXQ", "entity_type": "PERSON"}]
def open_space_day(client, headers, title="Alltag", date="2026-08-29"):
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 main() -> None:
reset_debug()
reset_detect_test_hooks()
sample = "Ich traf Anna."
try:
validate_detected_entity(
{"start": 0, "end": 4, "text": "Anna", "entity_type": "FOOD"},
sample,
0,
)
typed = False
except DetectError as exc:
typed = exc.code == "detect_invalid_output"
expect((exc.diagnostics or {}).get("invalid_entity_type") == "FOOD", "unknown type is recorded without the span text")
expect((exc.diagnostics or {}).get("contract_violation") == "unknown_entity_type", "unknown type has a violation category")
expect("Anna" not in json.dumps(exc.diagnostics or {}), "span plaintext is not stored on the type violation")
expect(typed, "unknown entity_type is a contract error")
cyclic = {}
nested = {"budget": cyclic, "stages": [cyclic]}
cyclic["trace"] = nested
cyclic["local_label"] = "secret-name"
cleaned = sanitize(cyclic)
expect(cleaned is not None, "cyclic sanitize returns a value")
expect(_ids(cleaned), "sanitized cyclic input is acyclic")
expect("local_label" not in json.dumps(cleaned), "sanitize still drops mapping labels")
expect(json.dumps(cleaned, default=str), "sanitized cyclic input is JSON-serializable")
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, "setup")
token = setup.json()["token"]
headers = header(token)
profile_id = setup.json()["profile_id"]
calls = []
def retry_then_ok(attempt, chunk_index, chunk_text, offset, schema_retry=False):
usage = USAGE_A if attempt == 1 else USAGE_B
calls.append((attempt, chunk_index, schema_retry))
if attempt == 1:
return _chat(_invalid_type(), usage)
return _chat([], usage)
long_body = (
"WRITING_PROFILE\nkurze trockene Saetze. " * 12
+ "STYLE_EXAMPLES\nDer Abend blieb ruhig. " * 12
+ "CURRENT_DAY_SOURCES\nHeute der Markt, danach der Hafen. "
)
with patch("entity_detect.DETECT_CHUNK_CHARS", 90), patch("entity_detect.DETECT_CHUNK_OVERLAP", 12):
chunks = split_detect_chunks(long_body, chunk_chars=90, overlap=12)
expect(len(chunks) >= 2, "long profile+examples text is chunked")
install_test_detect_script(retry_then_ok)
try:
outcome = detect_personal_egress(profile_id, long_body)
finally:
reset_detect_test_hooks()
attempts = {item[0] for item in calls}
expect(attempts == {1, 2}, "invalid first pass starts a second full pass")
first_chunks = [item[1] for item in calls if item[0] == 1]
second_chunks = [item[1] for item in calls if item[0] == 2]
expect(second_chunks == list(range(len(chunks))), "second pass re-checks every chunk")
expect(first_chunks == [0], "failed first pass stops at the invalid chunk and discards partials")
expect(any(item[2] for item in calls if item[0] == 2), "second pass is marked as schema retry")
expect(not any(item[2] for item in calls if item[0] == 1), "first pass has no schema retry hint")
expect(outcome.stats.detect_partial_discarded is True, "partial first-pass spans are discarded")
expect(outcome.stats.detect_passes == 2, "successful retry records two passes")
expect(outcome.stats.detect_calls == len(calls), "detect calls sum both passes")
expect(outcome.stats.cost_known is True, "injected usage keeps known detect cost")
expected_cost = USAGE_A["cost"] * len(first_chunks) + USAGE_B["cost"] * len(second_chunks)
expect(abs(outcome.stats.cost - expected_cost) < 1e-9, "detect costs of both passes are aggregated")
expect(
abs(
outcome.stats.prompt_tokens
- (USAGE_A["prompt_tokens"] * len(first_chunks) + USAGE_B["prompt_tokens"] * len(second_chunks))
)
< 1e-9,
"detect tokens of both passes are aggregated",
)
expect(outcome.stats.full_detection_coverage is True, "valid second pass has full coverage")
expect(outcome.stats.detect_ms >= 0, "retry records duration")
generate_n = {"n": 0}
def count_generate(messages, policy):
generate_n["n"] += 1
from privacy_gateway import _fake_complete
return ChatResult(content=_fake_complete(policy.get("purpose") or "", messages[0].get("content") or ""), model="fake")
install_test_detect_script(retry_then_ok)
generate_n["n"] = 0
with patch("privacy_gateway.complete_model", count_generate):
try:
ran = _run(profile_id, "Heute der Markt und danach der Hafen.")
finally:
reset_detect_test_hooks()
expect(ran.trace.get("generate_called") is True, "valid retry allows exactly one narration call")
expect(generate_n["n"] == 1, "narration runs once after a recovered detect pass")
expect(_ids(ran.diagnostics or {}), "successful diagnostics are acyclic")
expect(_ids(ran.trace or {}), "successful trace is acyclic")
def always_invalid(attempt, chunk_index, chunk_text, offset, schema_retry=False):
return _chat(_invalid_type(), USAGE_A)
generate_n["n"] = 0
install_test_detect_script(always_invalid)
blocked = None
with patch("privacy_gateway.complete_model", count_generate):
try:
_run(profile_id, "Heute der Markt und danach der Hafen.")
except PrivacyGatewayError as exc:
blocked = exc
finally:
reset_detect_test_hooks()
expect(blocked is not None and blocked.code == "detect_invalid_output", "two invalid passes fail closed")
expect(generate_n["n"] == 0, "invalid detect never calls narration")
expect(blocked.diagnostics.get("generate_called") is False, "final abort keeps generate_called false")
expect(blocked.diagnostics.get("detect_passes") == 2, "both invalid passes are counted")
expect(blocked.diagnostics.get("contract_violation") == "unknown_entity_type", "final abort keeps the violation category")
expect(blocked.diagnostics.get("invalid_entity_type") == "FOOD", "delivered type is stored without span text")
expect(blocked.diagnostics.get("detect_partial_discarded") is True, "failed retry discarded the first pass")
expect(_ids(blocked.diagnostics), "failed diagnostics are acyclic")
expect("FOOD" in json.dumps(blocked.diagnostics), "invalid type remains visible")
expect("secret" not in json.dumps(blocked.diagnostics).lower(), "no extra identity payload")
expect(blocked.status_code != 500, "detect abort is not a generic 500")
def extra_then_fail(attempt, chunk_index, chunk_text, offset, schema_retry=False):
return _chat(_extra_field(), USAGE_A)
install_test_detect_script(extra_then_fail)
extra_err = None
try:
detect_personal_egress(profile_id, "Heute der Markt.")
except DetectError as exc:
extra_err = exc
finally:
reset_detect_test_hooks()
expect(extra_err is not None and extra_err.diagnostics.get("contract_violation") == "extra_fields", "extra fields fail closed after retry")
def missing_then_fail(attempt, chunk_index, chunk_text, offset, schema_retry=False):
return _chat(_missing_field(), USAGE_A)
install_test_detect_script(missing_then_fail)
missing_err = None
try:
detect_personal_egress(profile_id, "Heute der Markt.")
except DetectError as exc:
missing_err = exc
finally:
reset_detect_test_hooks()
expect(missing_err is not None and missing_err.diagnostics.get("contract_violation") == "missing_fields", "missing fields fail closed after retry")
def offset_then_fail(attempt, chunk_index, chunk_text, offset, schema_retry=False):
return _chat(_bad_offset(chunk_text), USAGE_A)
install_test_detect_script(offset_then_fail)
offset_err = None
try:
detect_personal_egress(profile_id, "Heute der Markt.")
except DetectError as exc:
offset_err = exc
finally:
reset_detect_test_hooks()
expect(offset_err is not None and offset_err.diagnostics.get("contract_violation") == "unusable_offsets", "unusable offsets fail closed after retry")
net_calls = []
def network_fail(attempt, chunk_index, chunk_text, offset, schema_retry=False):
net_calls.append(attempt)
raise DetectError("detect_chunk_failed", "chunk failed")
install_test_detect_script(network_fail)
net_err = None
try:
detect_personal_egress(profile_id, "Heute der Markt.")
except DetectError as exc:
net_err = exc
finally:
reset_detect_test_hooks()
expect(net_err is not None and net_err.code == "detect_chunk_failed", "provider/chunk failure stays distinct")
expect(net_calls == [1], "network-like chunk failure is not doubled by the contract retry")
install_test_detect_failure(DetectError("detect_chunk_failed", "chunk failed"))
immediate = None
try:
detect_personal_egress(profile_id, "Heute der Markt.")
except DetectError as exc:
immediate = exc
finally:
reset_detect_test_hooks()
expect(immediate is not None and immediate.diagnostics.get("detect_passes") == 0, "injected provider failure does not start a contract retry")
mixed = []
def worker(kind: str):
reset_debug()
seen = []
def script(attempt, chunk_index, chunk_text, offset, schema_retry=False):
seen.append((kind, attempt))
if attempt == 1:
entity_type = "FOOD" if kind == "alpha" else "ANIMAL"
return _chat([{"start": 0, "end": 1, "text": "x", "entity_type": entity_type}], USAGE_A)
return _chat([], USAGE_B)
install_test_detect_script(script)
try:
out = detect_personal_egress(profile_id, f"Heute der Markt {kind}.")
mixed.append((kind, tuple(seen), out.stats.invalid_entity_type, out.stats.detect_calls, id(out.stats)))
finally:
reset_detect_test_hooks()
with ThreadPoolExecutor(max_workers=2) as pool:
one = pool.submit(worker, "alpha")
two = pool.submit(worker, "beta")
one.result()
two.result()
kinds = {item[0] for item in mixed}
expect(kinds == {"alpha", "beta"}, "parallel detect retries both complete")
by_kind = {item[0]: item for item in mixed}
expect(
all(kind == row[0] for row in by_kind.values() for kind, _attempt in row[1]),
"parallel scripts stay on their request",
)
expect(by_kind["alpha"][2] in (None, "FOOD"), "alpha does not keep beta's type")
expect(by_kind["beta"][2] in (None, "ANIMAL"), "beta does not keep alpha's type")
expect(by_kind["alpha"][4] != by_kind["beta"][4], "parallel stats objects are distinct")
failed = {
"diagnostics": {"generate_called": False, "detect_cost": 0.001, "cost": None, "purpose": "journal_generate"},
"trace": {"purpose": "journal_generate", "generate_called": False},
"content": "",
}
staged = _stage_trace(failed, "journal_generate")
expect(staged.get("budget") is not failed["diagnostics"], "stage budget is a snapshot, not the live diagnostics object")
failed["diagnostics"]["trace"] = {"budget": staged.get("budget")}
expect(_ids(failed), "attaching the snapshot does not create a diagnostics cycle")
composed = _compose_journal_trace(
{"trace": {"purpose": "local_source_artifact"}, "diagnostics": {"prompt_tokens": 0}},
failed,
run_log=[{"kind": "detect", "status": "error"}],
narrate_prompt={"slug": "mvp.journal_generate", "seed_revision": "test"},
profile_meta={"present": False},
style_meta={"count": 0},
dropped=[],
extra={"generation_selection": {"voice_id": "journal-generate-voice-clear"}, "style_application": {"include_core": False}},
)
failed["diagnostics"]["trace"] = composed
expect(_ids(failed["diagnostics"]), "composed journal diagnostics stay acyclic")
expect(_budget_is_not_diagnostics(failed), "composed budget is not a back-reference")
expect(json.dumps(sanitize(failed), default=str), "composed failure is serializable")
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"},
)
turn = client.post(
f"/api/journal/conversations/{conv.json()['id']}/turn",
headers=headers,
json={"body": "Heute war der Markt voll und danach der Hafen ruhig."},
)
expect(turn.status_code == 200, f"turn {turn.status_code}")
enabled = client.put("/api/admin/debug", headers=headers, json={"persist_enabled": True})
expect(enabled.status_code == 200 and enabled.json()["persist_enabled"] is True, "persist enabled")
before = client.get("/api/admin/debug/runs", headers=headers).json()["runs"]
install_test_detect_script(always_invalid)
try:
gen = client.post(
f"/api/journal/days/{day_id}/generate",
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
finally:
reset_detect_test_hooks()
expect(gen.status_code == 503, f"journal detect abort status {gen.status_code}")
detail = gen.json()["detail"]
expect(detail["code"] == "detect_invalid_output", "API keeps the detect contract code")
expect(detail["code"] != "error" and gen.status_code != 500, "detect abort is not HTTP 500")
expect("Angaben" in (detail.get("message") or ""), "API uses the user-facing detect message")
expect(detail.get("cost_report"), "API includes a cost report")
expect(detail["cost_report"].get("generate_called") is False, "cost report says generate was not called")
expect(
(detail.get("diagnostics") or {}).get("generate_called") is False,
"diagnostics keep generate_called false",
)
expect(_ids(detail), "API error payload is acyclic")
expect(_budget_is_not_diagnostics(detail), "API error budget is not a diagnostics alias")
after = client.get("/api/admin/debug/runs", headers=headers).json()["runs"]
error_runs = [
item
for item in after
if item.get("purpose") == "journal_generate" and item.get("status") == "error"
]
expect(len(error_runs) == 1, "failed journal request is stored exactly once")
run = error_runs[0]
expect(run.get("journal_day_id") == day_id, "error run is attached to the journal day")
detail_run = client.get(f"/api/admin/debug/runs/{run['id']}", headers=headers)
expect(detail_run.status_code == 200, f"error run detail {detail_run.status_code}")
payload = detail_run.json().get("payload") or {}
expect(payload.get("code") == "detect_invalid_output", "stored run keeps the detect code")
expect(payload.get("generate_called") is False, "stored run records generate_called false")
expect(payload.get("detect_passes") == 2, "stored run records both detect passes")
expect(payload.get("detect_partial_discarded") is True, "stored run records discarded partials")
expect(payload.get("contract_violation") == "unknown_entity_type", "stored run records the violation")
expect(payload.get("invalid_entity_type") == "FOOD", "stored run records the delivered type")
expect(payload.get("generation_selection") or (payload.get("trace") or {}).get("generation_selection"), "stored run keeps generation selection")
expect("local_label" not in json.dumps(payload), "stored run has no mapping labels")
exported = client.get(
f"/api/admin/debug/export?format=json&journal_day_id={day_id}&purpose=journal_generate",
headers=headers,
)
expect(exported.status_code == 200, f"error run export {exported.status_code}")
document = exported.json()
encoded = json.dumps(document)
expect(encoded, "exported error run is JSON-serializable")
expect("detect_invalid_output" in encoded, "export contains the detect code")
hidden = EngineError("detect_invalid_output", "Persönliche Angaben konnten nicht sicher zugeordnet werden.", 503)
with patch("debug_store.persist_step", side_effect=RuntimeError("persist boom")):
persist_engine_error(profile_id, purpose="journal_generate", exc=hidden, journal_day_id=day_id)
install_test_detect_script(always_invalid)
try:
with patch("debug_store.persist_step", side_effect=RuntimeError("persist boom")):
gen_hidden = client.post(
f"/api/journal/days/{day_id}/generate",
headers=headers,
json={"conversation_ids": [conv.json()["id"]]},
)
finally:
reset_detect_test_hooks()
expect(gen_hidden.status_code == 503, f"persist failure keeps detect status {gen_hidden.status_code}")
expect(gen_hidden.json()["detail"]["code"] == "detect_invalid_output", "persist failure does not replace EngineError")
expect(gen_hidden.status_code != 500, "persist failure is not HTTP 500")
reset_detect_test_hooks()
control_space, control_day = open_space_day(client, headers, "Kontrolle", "2026-08-28")
control_conv = client.post(
f"/api/journal/days/{control_day['day']['id']}/conversations",
headers=headers,
json={"title": "Gespräch"},
)
control_turn = client.post(
f"/api/journal/conversations/{control_conv.json()['id']}/turn",
headers=headers,
json={"body": "Heute Tee auf dem Balkon, danach nur der Wind."},
)
expect(control_turn.status_code == 200, "control turn")
control = client.post(
f"/api/journal/days/{control_day['day']['id']}/generate",
headers=headers,
json={"conversation_ids": [control_conv.json()["id"]]},
)
expect(control.status_code == 200, f"control generate without profile {control.status_code}")
expect((control.json().get("trace") or {}).get("generate_called") is True, "control run still narrates once")
expect(_ids(control.json().get("trace") or {}), "control trace is acyclic")
client.put("/api/admin/debug", headers=headers, json={"persist_enabled": False})
print("ALL TESTS PASSED")
if __name__ == "__main__":
main()
+1 -1
View File
@@ -873,7 +873,7 @@ def test_generate_flow(client: TestClient, headers: dict) -> None:
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("STYLE_EXAMPLES" not in narrate, "default voice does not label a style-examples block")
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()
+3 -3
View File
@@ -71,7 +71,6 @@ def test_prompt_contract() -> None:
text = prompt.get("template") or ""
expect("INHALTSTREUE" in text, "prompt keeps hard content rules")
expect("CURRENT_DAY_SOURCES" in text, "prompt labels current-day facts")
expect("STYLE_EXAMPLES" in text, "prompt labels style examples")
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")
@@ -253,8 +252,9 @@ def main() -> None:
intern = intern_of(gen.json())
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("\nSTYLE_EXAMPLES\n" not in intern, "default voice does not attach a style-example section")
expect("STYLE_EXAMPLES" not in intern, "default voice has no orphaned STYLE_EXAMPLES reference")
expect("WRITING_PROFILE" in intern, "writing profile heading or instruction remains")
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")
+1 -1
View File
@@ -49,7 +49,7 @@ def main() -> None:
expect("{{source_mode_instructions}}" not in templates[VARIANT_CURRENT], "current variant has no source-mode placeholder")
expect("Zwinge nicht den gesamten Tag in einen einzigen Quellenmodus." in templates[VARIANT_CURRENT], "current variant has mixed-source instruction")
expect("ich gieng zum laden" not in templates[VARIANT_CURRENT], "current variant has no synthetic examples")
expect(GENERATE_SEED_REVISION == "2026-08-27-journal-mixed-sources-v1", "eval tracks the seeded revision constant")
expect(GENERATE_SEED_REVISION == "2026-08-29-voice-legacy-immutable-v1", "eval tracks the seeded revision constant")
required_classes = {
"already_narrative_with_errors",
@@ -60,7 +60,6 @@ HARD_FACTS = (
"Plan und Vollzug",
"Verneinungen",
"Unsicherheiten",
"STYLE_EXAMPLES dienen ausschließlich als Stilreferenz",
"[[PERSON:01]]",
)
BANNED_CODE_PHRASES = (
@@ -101,6 +100,11 @@ REQUIRED_KEYS = {
("voice", "light"),
("voice", "noticeable"),
("voice", "clear"),
("voice", "with_examples"),
("voice", "legacy_neutral"),
("voice", "legacy_light"),
("voice", "legacy_noticeable"),
("voice", "legacy_clear"),
("narrative", "chronicle"),
("narrative", "structured"),
("narrative", "weighted"),
@@ -209,7 +213,9 @@ def test_runtime_has_no_numeric_policy() -> None:
continue
expect(not has_token(text, token), f"{path.name} has no {token}")
day = (FRONTEND / "src" / "pages" / "JournalDayPage.jsx").read_text(encoding="utf-8")
expect('type="range"' not in day, "journal page has no sliders")
expect("Stilquellen" in day, "voice select can show which style sources apply")
expect("type=\"range\"" not in day, "journal page has no extra sliders for style context")
expect(day.count("<select") == 1, "journal page maps four slots onto one select, not extra style-context fields")
expect("policy-slider" not in day, "journal page has no slider class")
expect("SLOT_SELECTS" in day, "journal page names the four selects")
expect(day.count("slot:") >= 4, "journal page has four independent slots")
@@ -224,7 +230,10 @@ def test_runtime_has_no_numeric_policy() -> None:
admin = (FRONTEND / "src" / "pages" / "AdminGenerationPage.jsx").read_text(encoding="utf-8")
expect("<textarea" not in admin.split("generation-admin-editor")[0], "admin overview has no textareas")
expect(admin.count("<textarea") == 1, "admin opens one instruction editor at a time")
expect("Öffnen" in admin, "admin overview has open action")
expect("Stilquellen für diese Ausprägung" in admin, "admin edits style context with the instruction")
expect("Vorgänger" in admin, "admin shows predecessor or clone origin")
expect("include_core" in admin, "admin names core inclusion")
expect("Writing-Profile-Core" in admin, "admin labels core in German product terms")
expect("backend/config/" not in admin, "admin copy does not advertise seed file paths")
expect("Quellenmodus" not in admin, "admin has no source-mode management")
expect("source_mode" not in admin, "admin source has no source_mode slot")
@@ -239,6 +248,8 @@ def test_runtime_has_no_numeric_policy() -> None:
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("Stilanwendung" in trace_ui, "trace shows requested versus effective style application")
expect("cloned_from" in trace_ui, "trace can show the predecessor id")
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")
@@ -275,8 +286,22 @@ def test_seed_and_compiler() -> None:
"default narrative instruction matches the seed",
)
expect(defaults.seed_revision == SEED["seed_revision"], "compiled policy names the seed revision")
expect(defaults.style_context["include_core"] is True, "default voice includes core")
expect(defaults.style_context["include_facet"] is True, "default voice includes facet")
expect(defaults.style_context["include_traits"] is True, "default voice includes traits")
expect(defaults.style_context["include_style_examples"] is False, "default voice omits style examples")
expect("source_mode_instructions" not in defaults.instructions, "compiler has no source-mode instruction")
free = compile_selection({**default_selection_ids(), "voice_id": seed_id("voice", "neutral")})
expect(free.style_context == {
"include_core": False,
"include_facet": False,
"include_traits": False,
"include_style_examples": False,
}, "profile-free voice selects no style sources")
examples = compile_selection({**default_selection_ids(), "voice_id": seed_id("voice", "with_examples")})
expect(all(examples.style_context.values()), "with-examples voice selects all style sources")
low = compile_selection(low_ids())
high = compile_selection(high_ids())
expect(low.keys["transformation"] == "correction", "low transformation is named")
@@ -389,18 +414,19 @@ def test_prompt_contract() -> None:
expect("{{editorial_mode}}" not in text, "prompt has no editorial_mode placeholder")
expect("{{editorial_instructions}}" not in text, "prompt has no editorial_instructions placeholder")
expect(MIXED_SOURCES_INSTRUCTION in text, "prompt contains the mixed-source instruction")
expect("STYLE_EXAMPLES dienen ausschließlich" not in text, "standing prompt has no orphaned STYLE_EXAMPLES sentence")
for fact in HARD_FACTS:
expect(fact in text, f"hard fact rule remains: {fact}")
expect("ich gieng zum laden" not in text, "no synthetic prose example")
expect("nachbarhund im garten" not in text, "no synthetic notes example")
expect("EDITORIAL_MODE" not in text, "source mode is not a user-facing prompt heading")
expect(GENERATE_SEED_REVISION == "2026-08-27-journal-mixed-sources-v1", "seed revision constant tracks mixed sources")
expect(GENERATE_SEED_REVISION == "2026-08-29-voice-legacy-immutable-v1", "seed revision constant tracks immutable voice catalog")
with get_db() as conn:
row = conn.execute(
"SELECT seed_revision, template, default_template FROM ai_prompts WHERE slug = ?",
("mvp.journal_generate",),
).fetchone()
expect(row["seed_revision"] == "2026-08-27-journal-mixed-sources-v1", "untouched default is updated to mixed sources")
expect(row["seed_revision"] == "2026-08-29-voice-legacy-immutable-v1", "untouched default is updated to immutable voice catalog")
expect(row["template"] == row["default_template"], "untouched template matches the seed")
expect("source_mode" not in (SEED.get("slots") or {}), "source modes stay out of the seed")
@@ -1019,7 +1045,7 @@ def main() -> None:
).fetchone()
expect("{{transformation_instructions}}" in (row["default_template"] or ""), "default template still tracks mixed sources")
expect(MIXED_SOURCES_INSTRUCTION in (row["default_template"] or ""), "default template has mixed-source instruction")
expect(row["seed_revision"] == "2026-08-27-journal-mixed-sources-v1", "revision updates even when template is custom")
expect(row["seed_revision"] == "2026-08-29-voice-legacy-immutable-v1", "revision updates even when template is custom")
readable = snapshot_summary(
{
+3 -3
View File
@@ -51,7 +51,7 @@ def test_active_prompt_contract() -> None:
text = prompt.get("template") or ""
expect("INHALTSTREUE" in text, "active prompt keeps hard content rules")
expect("CURRENT_DAY_SOURCES" in text, "active prompt labels current-day facts")
expect("STYLE_EXAMPLES" in text, "active prompt labels style examples")
expect("{{style_examples}}" in text, "active prompt still has the style-examples slot")
expect("Rechtschreibung" in text or "korrigieren" in text, "active prompt allows spelling and grammar fixes")
expect("Verneinungen" in text and "Unsicherheiten" in text, "active prompt keeps semantic uncertainty")
expect("keine neuen tatsachen" in text.lower(), "active prompt still forbids new facts")
@@ -67,7 +67,7 @@ def test_active_prompt_contract() -> None:
"SELECT seed_revision, template, default_template FROM ai_prompts WHERE slug = ?",
("mvp.journal_generate",),
).fetchone()
expect(row["seed_revision"] == "2026-08-27-journal-mixed-sources-v1", "system prompt revision is stored")
expect(row["seed_revision"] == "2026-08-29-voice-legacy-immutable-v1", "system prompt revision is stored")
expect(row["template"] == row["default_template"], "untouched install uses the seeded template")
@@ -313,7 +313,7 @@ def main() -> None:
("mvp.journal_generate",),
).fetchone()
expect("INHALTSTREUE" in (row["default_template"] or ""), "default template still tracks the seed")
expect(row["seed_revision"] == "2026-08-27-journal-mixed-sources-v1", "revision updates even when template is custom")
expect(row["seed_revision"] == "2026-08-29-voice-legacy-immutable-v1", "revision updates even when template is custom")
print("journal narration tests passed.")
+374
View File
@@ -0,0 +1,374 @@
"""Style-application variants control which writing-profile parts reach generate."""
from __future__ import annotations
import os
import sys
import tempfile
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-journal-style-context-test.sqlite")
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
os.environ["KANSHO_FAKE_DETECT"] = "1"
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
from fastapi.testclient import TestClient
from db import get_db, init_db
from journal_editorial import (
compile_journal_style_application,
style_application_trace,
)
from journal_generation_policy import (
clone_guideline,
compile_selection,
default_selection_ids,
get_guideline,
save_selection,
seed_generation_instructions,
)
from journal_generate import pack_narration_context
from main import app
from privacy_gateway import install_test_recorder, reset_debug
from prompt_budget import JournalBudget
from writing_profile_store import (
compile_task_brief,
import_text,
set_lifecycle,
update_facet,
upsert_trait,
)
CORE_MARK = "ZXCORE_MARKER kurze trockene Saetze"
FACET_MARK = "ZXFACET_MARKER ruhige Abendreflexion"
TRAIT_MARK = "ZXTRAIT_MARKER keine rhythmischen Aufzaehlungen"
EXAMPLE_MARK = "ZXEXAMPLE_MARKER Der Hafen blieb hinter der Faehre"
TODAY_MARK = "ZXTODAY_MARKER am heutigen Markt"
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 intern_of(payload: dict) -> str:
for stage in (payload.get("trace") or {}).get("stages") or []:
if stage.get("purpose") == "journal_generate":
return stage.get("intern") or ""
return (payload.get("trace") or {}).get("intern") or ""
def style_region(intern: str) -> str:
return (intern or "").split("\nCURRENT_DAY_SOURCES\n")[0]
def has_heading(intern: str, title: str) -> bool:
body = intern or ""
return f"\n{title}\n" in body or body.startswith(f"{title}\n")
def seed_id(slot: str, key: str) -> str:
from journal_generation_policy import load_seed_document
seed = load_seed_document()
for item in (seed.get("slots") or {}).get(slot, {}).get("variants") or []:
if item.get("guideline_key") == key:
return item["id"]
raise SystemExit(f"FAIL: missing seed {slot}/{key}")
def selection_of(voice_key: str) -> dict[str, str]:
ids = default_selection_ids()
ids["voice_id"] = seed_id("voice", voice_key)
return ids
def ensure_local_profile(profile_id: str = "style-context-local") -> str:
with get_db() as conn:
conn.execute(
"""
INSERT OR IGNORE INTO profiles (id, email, name, password_hash, role)
VALUES (?, ?, ?, ?, 'user')
""",
(profile_id, f"{profile_id}@example.test", "Style", "x"),
)
return profile_id
def install_profile(profile_id: str) -> None:
update_facet(profile_id, "core", value=CORE_MARK)
update_facet(profile_id, "autobiographical_journal", value=FACET_MARK)
upsert_trait(
profile_id,
slug="journal-rhythm",
facet_key="autobiographical_journal",
label="Rhythmus",
statement=TRAIT_MARK,
origin="manual",
)
set_lifecycle(profile_id, "confirmed")
import_text(
profile_id,
EXAMPLE_MARK + " in knappen ruhigen Saetzen ohne Pathoswolken.",
occurred_at="2026-07-01",
)
def test_compile_flags() -> None:
init_db()
profile_id = ensure_local_profile()
install_profile(profile_id)
full = compile_task_brief(profile_id)
expect(CORE_MARK in full and FACET_MARK in full and TRAIT_MARK in full, "full brief keeps selected parts")
core_only = compile_task_brief(
profile_id,
include_core=True,
include_facet=False,
include_traits=False,
)
expect(CORE_MARK in core_only, "core-only brief keeps core")
expect(FACET_MARK not in core_only, "core-only brief omits facet")
expect(TRAIT_MARK not in core_only, "core-only brief omits traits")
none = compile_task_brief(
profile_id,
include_core=False,
include_facet=False,
include_traits=False,
)
expect(none == "", "no selected profile parts yield an empty brief")
bundle = compile_journal_style_application(profile_id, compile_selection(selection_of("neutral")).style_context)
expect(bundle["profile_block"] == "", "profile-free omits the wrapped profile block")
expect(bundle["examples_block"] == "", "profile-free omits the wrapped examples block")
def test_budget_omission_in_effective_trace() -> None:
init_db()
prompt = {
"id": "style-pack",
"slug": "mvp.journal_generate",
"prompt_type": "base",
"template": "{{writing_profile}}{{style_examples}}DAY\n{{reconstruction}}\nOLD\n{{existing_text}}\n",
}
assembled = {
"writing_profile": "WRITING_PROFILE\nCore: kurze Sätze.\n",
"reconstruction": "Heute Markt.",
"style_examples": "",
"existing_text": "",
}
huge = "Stil " + ("Beispielwort " * 400)
from engine import preview_prompt
from prompt_budget import estimate_tokens
rendered = preview_prompt(prompt, assembled)["rendered"]
budget = JournalBudget(
model="fake",
purpose="journal_generate",
effective_context_window=32_768,
reserved_output_tokens=256,
safety_margin=0.15,
available_input_tokens=estimate_tokens(rendered) + 80,
chars_per_token=2.0,
)
packed, dropped = pack_narration_context(
prompt,
budget,
assembled,
style_examples=huge,
existing_text="Bestehende Fassung",
include_existing=True,
)
expect("style_examples" in dropped, "budget drops style examples first")
expect("Beispielwort" not in (packed.get("style_examples") or ""), "dropped examples leave the packed prompt")
compiled = compile_selection(selection_of("with_examples"))
bundle = {
"requested": dict(compiled.style_context),
"brief": "Core: kurze Sätze.",
"profile_block": assembled["writing_profile"],
"examples_block": huge,
"example_rows": [{"kind": "imported_text", "excerpt": huge}],
"omitted": [],
"effective": {"neutral_fallback": False, "trait_count": 0},
}
trace = style_application_trace(compiled, bundle, dropped=dropped)
expect(trace["effective"]["style_example_count"] == 0, "effective trace has no examples after budget drop")
expect(
any(item.get("part") == "style_examples" and item.get("reason") == "budget" for item in trace["omitted"]),
"budget drop is recorded on the effective trace",
)
expect(trace["id"] == seed_id("voice", "with_examples"), "trace keeps the requested voice id")
expect(trace["revision"] == 1, "trace keeps the requested revision")
def main() -> None:
test_compile_flags()
test_budget_omission_in_effective_trace()
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
init_db()
reset_debug()
with TestClient(app) as client:
setup = client.post(
"/api/auth/setup",
json={"email": "voice@example.test", "name": "Ada", "password": "test-pass"},
)
headers = header(setup.json()["token"])
profile_id = setup.json()["profile_id"]
install_profile(profile_id)
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Stil"})
day = client.post(
f"/api/journal/spaces/{space.json()['id']}/days",
headers=headers,
json={"calendar_date": "2026-08-29"},
)
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": f"Ich war am Markt. {TODAY_MARK}. Vielleicht bleibe ich kürzer."},
)
expect(turn.status_code == 200, f"turn {turn.status_code}")
day_id = day.json()["day"]["id"]
conv_id = conv.json()["id"]
def generate(voice_key: str) -> dict:
reset_debug()
recorder = install_test_recorder()
result = client.post(
f"/api/journal/days/{day_id}/generate",
headers=headers,
json={
"conversation_ids": [conv_id],
"generation_selection": selection_of(voice_key),
"remember_generation_selection": False,
},
)
expect(result.status_code == 200, f"{voice_key} generate {result.status_code}")
payload = result.json()
intern = intern_of(payload)
style = style_region(intern)
app_trace = (payload.get("trace") or {}).get("style_application") or {}
calls = [item.get("purpose") for item in recorder]
expect(calls == ["journal_generate"], f"{voice_key} still uses exactly one narration call")
expect(app_trace.get("id") == seed_id("voice", voice_key), f"{voice_key} id reaches the trace")
expect(app_trace.get("key") == voice_key, f"{voice_key} key reaches the trace")
expect(app_trace.get("revision") == 1, f"{voice_key} revision reaches the trace")
expect(
(payload.get("trace") or {}).get("generation_selection", {}).get("ids", {}).get("voice")
== seed_id("voice", voice_key),
f"{voice_key} selection id matches prompt/trace",
)
expect(TODAY_MARK not in style, f"{voice_key} does not use today as a style example")
expect(TODAY_MARK in intern.split("\nCURRENT_DAY_SOURCES\n")[-1], f"{voice_key} keeps today as content")
expect("keine neuen Tatsachen" in intern, f"{voice_key} keeps fact fidelity")
expect("[[PERSON:01]]" in intern, f"{voice_key} keeps privacy placeholders")
return {"payload": payload, "intern": intern, "style": style, "trace": app_trace}
free = generate("neutral")
expect(not has_heading(free["intern"], "WRITING_PROFILE"), "profile-free omits the profile heading")
expect(not has_heading(free["intern"], "STYLE_EXAMPLES"), "profile-free omits the examples heading")
expect("STYLE_EXAMPLES" not in free["intern"], "profile-free has no orphaned STYLE_EXAMPLES reference")
expect(CORE_MARK not in free["intern"], "profile-free sends no core")
expect(FACET_MARK not in free["intern"], "profile-free sends no facet")
expect(TRAIT_MARK not in free["intern"], "profile-free sends no traits")
expect(EXAMPLE_MARK not in free["intern"], "profile-free sends no style examples")
expect(free["trace"]["effective"]["include_core"] is False, "profile-free effective core is no")
expect(free["trace"]["effective"]["style_example_count"] == 0, "profile-free effective examples are zero")
light = generate("light")
expect(CORE_MARK in light["style"], "light sends core")
expect(FACET_MARK not in light["intern"], "light does not send facet even indirectly")
expect(TRAIT_MARK not in light["intern"], "light does not send traits even indirectly")
expect(EXAMPLE_MARK not in light["intern"], "light does not send examples even indirectly")
expect(has_heading(light["intern"], "WRITING_PROFILE"), "light keeps a profile heading")
expect(not has_heading(light["intern"], "STYLE_EXAMPLES"), "light omits the examples heading")
expect("STYLE_EXAMPLES" not in light["intern"], "light has no orphaned STYLE_EXAMPLES reference")
expect(light["trace"]["effective"]["include_core"] is True, "light effective core is yes")
expect(light["trace"]["effective"]["include_facet"] is False, "light effective facet is no")
expect(light["trace"]["effective"]["trait_count"] == 0, "light effective traits are zero")
clear = generate("clear")
expect(CORE_MARK in clear["style"], "clear sends core")
expect(FACET_MARK in clear["style"], "clear sends the relevant facet")
expect(TRAIT_MARK in clear["style"], "clear sends traits")
expect(EXAMPLE_MARK not in clear["intern"], "clear does not send examples")
expect(not has_heading(clear["intern"], "STYLE_EXAMPLES"), "clear omits the examples heading")
expect("STYLE_EXAMPLES" not in clear["intern"], "clear has no orphaned STYLE_EXAMPLES reference")
expect(clear["trace"]["effective"]["include_core"] is True, "clear effective core is yes")
expect(clear["trace"]["effective"]["include_facet"] is True, "clear effective facet is yes")
expect(clear["trace"]["effective"]["trait_count"] >= 1, "clear effective traits are counted")
expect(clear["trace"]["effective"]["style_example_count"] == 0, "clear effective examples are zero")
examples = generate("with_examples")
expect(CORE_MARK in examples["style"], "with-examples sends core")
expect(FACET_MARK in examples["style"], "with-examples sends facet")
expect(TRAIT_MARK in examples["style"], "with-examples sends traits")
expect(EXAMPLE_MARK in examples["style"], "with-examples sends style examples")
expect(has_heading(examples["intern"], "STYLE_EXAMPLES"), "with-examples labels the examples block")
expect("ausschließlich als Stilreferenz" in examples["intern"] or "sprachliche" in examples["style"], "examples stay style-only")
expect("keine Tatsachen des heutigen" in examples["style"] or "nicht übernommen" in examples["style"], "example facts are forbidden")
expect(examples["trace"]["effective"]["style_example_count"] >= 1, "with-examples effective count is used")
expect(EXAMPLE_MARK not in (examples["payload"].get("body") or ""), "example facts are not copied into today's draft")
settings = client.get("/api/journal/generation-settings", headers=headers).json()
voice_options = settings["options"]["voice"]
free_opt = next(item for item in voice_options if item["id"] == seed_id("voice", "neutral"))
expect(free_opt["style_context"]["include_core"] is False, "generation-settings expose style context without prompt text")
expect("instruction" not in free_opt, "generation-settings still hide prompt wording")
save_selection(profile_id, selection_of("neutral"))
remembered = generate("noticeable")
expect(CORE_MARK in remembered["style"], "legacy noticeable remains selectable")
profile_run = client.post(
f"/api/journal/days/{day_id}/generate",
headers=headers,
json={"conversation_ids": [conv_id]},
)
expect(profile_run.status_code == 200, f"stored selection generate {profile_run.status_code}")
stored_intern = intern_of(profile_run.json())
expect(not has_heading(stored_intern, "WRITING_PROFILE"), "stored profile-free selection stays profile-free")
expect(CORE_MARK not in stored_intern, "stored profile-free selection still omits core")
cloned = clone_guideline(seed_id("voice", "clear"))
original_instruction = get_guideline(seed_id("voice", "clear"), include_instruction=True)["instruction"]
with get_db() as conn:
seed_generation_instructions(conn)
expect(
get_guideline(seed_id("voice", "clear"), include_instruction=True)["instruction"] == original_instruction,
"seed refresh does not replace the published original with a clone",
)
expect(
get_guideline(cloned["id"], include_instruction=True)["style_context"]["include_core"] is True,
"cloned voice keeps the structured style context",
)
def compile_one(key: str) -> str:
bundle = compile_journal_style_application(profile_id, compile_selection(selection_of(key)).style_context)
return (bundle.get("brief") or "") + "\n" + (bundle.get("style_examples") or "")
with ThreadPoolExecutor(max_workers=2) as pool:
first = pool.submit(compile_one, "neutral")
second = pool.submit(compile_one, "with_examples")
free_text = first.result()
example_text = second.result()
expect(CORE_MARK not in free_text, "parallel profile-free compile stays empty")
expect(CORE_MARK in example_text and EXAMPLE_MARK in example_text, "parallel with-examples compile keeps all parts")
expect(EXAMPLE_MARK not in free_text, "parallel requests do not mix style configurations")
print("journal style context tests passed.")
if __name__ == "__main__":
main()
@@ -0,0 +1,356 @@
"""Legacy comparison voices and immutable seed IDs after style-context control."""
from __future__ import annotations
import copy
import os
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-journal-legacy-immutable-test.sqlite")
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
os.environ["KANSHO_FAKE_DETECT"] = "1"
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
from fastapi.testclient import TestClient
from db import get_db, init_db
from journal_generation_policy import (
PURPOSE_JOURNAL,
archive_guideline,
clone_guideline,
compile_selection,
default_selection_ids,
get_guideline,
list_guidelines,
load_seed_document,
load_selection,
publish_guideline,
save_selection,
seed_generation_instructions,
update_guideline,
)
from journal_editorial import format_style_examples
from main import app
from privacy_gateway import install_test_recorder, reset_debug
# Reconstructed from git HEAD `backend/config/generation_instructions.seed.json`
# (commit 4f37991, seed_revision 2026-08-27-generation-guidelines-v1).
HEAD_VOICE = {
"neutral": {
"id": "journal-generate-voice-neutral",
"instruction": "Schreibe in einem neutralen Journalstil. Das Writing Profile höchstens als leise Tendenz.",
"label": "neutral",
"summary": "Neutraler Journalstil, Writing Profile nur als leise Tendenz.",
},
"light": {
"id": "journal-generate-voice-light",
"instruction": "Nimm Rhythmus und Wortwahl des Writing Profiles zurückhaltend auf.",
"label": "dezent",
"summary": "Rhythmus und Wortwahl des Writing Profiles zurückhaltend.",
},
"noticeable": {
"id": "journal-generate-voice-noticeable",
"instruction": "Wende das Writing Profile spürbar an, ohne es in den Vordergrund zu stellen.",
"label": "spürbar",
"summary": "Writing Profile spürbar, ohne in den Vordergrund zu treten.",
},
"clear": {
"id": "journal-generate-voice-clear",
"instruction": "Wende Rhythmus, Wortwahl und Reflexionsdichte des Writing Profiles deutlich an, ohne den Text künstlich zu literarisieren.",
"label": "deutlich",
"summary": "Rhythmus, Wortwahl und Reflexionsdichte des Writing Profiles deutlich.",
},
}
FULL_STYLE_CONTEXT = {
"include_core": True,
"include_facet": True,
"include_traits": True,
"include_style_examples": True,
}
CURRENT_VOICE_IDS = {
"neutral": "journal-generate-voice-neutral",
"light": "journal-generate-voice-light",
"noticeable": "journal-generate-voice-noticeable",
"clear": "journal-generate-voice-clear",
"with_examples": "journal-generate-voice-with-examples",
}
LEGACY_KEYS = ("legacy_neutral", "legacy_light", "legacy_noticeable", "legacy_clear")
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 intern_of(payload: dict) -> str:
for stage in (payload.get("trace") or {}).get("stages") or []:
if stage.get("purpose") == "journal_generate":
return stage.get("intern") or ""
return (payload.get("trace") or {}).get("intern") or ""
def seed_id(slot: str, key: str) -> str:
seed = load_seed_document()
for item in (seed.get("slots") or {}).get(slot, {}).get("variants") or []:
if item.get("guideline_key") == key:
return item["id"]
raise SystemExit(f"FAIL: missing seed {slot}/{key}")
def voice_rows() -> list[dict]:
return list_guidelines(PURPOSE_JOURNAL, slot="voice", include_instruction=True)
def snapshot_current_voices() -> dict:
return {
item["id"]: {
"instruction": item["instruction"],
"label": item["label"],
"summary": item["summary"],
"style_context": dict(item["style_context"]),
"is_default": item["is_default"],
"revision": item["revision"],
"status": item["status"],
}
for item in voice_rows()
if item["id"] in CURRENT_VOICE_IDS.values()
}
def test_current_variants_and_legacy_seed() -> None:
init_db()
before = snapshot_current_voices()
expect(CURRENT_VOICE_IDS["clear"] in before, "current default voice id remains")
expect(before[CURRENT_VOICE_IDS["clear"]]["is_default"] is True, "current default stays on Persönliche Stimme deutlich")
expect(
before[CURRENT_VOICE_IDS["neutral"]]["style_context"]["include_core"] is False,
"current profile-free voice keeps an empty style context",
)
expect(
default_selection_ids()["voice_id"] == CURRENT_VOICE_IDS["clear"],
"catalog default still points at the current clear id",
)
with get_db() as conn:
seed_generation_instructions(conn)
seed_generation_instructions(conn)
after = snapshot_current_voices()
expect(before == after, "reseeding does not change current voice ids or semantics")
legacy = [item for item in voice_rows() if item["guideline_key"] in LEGACY_KEYS]
expect(len(legacy) == 4, "legacy comparison voices are inserted once")
expect(len({item["id"] for item in legacy}) == 4, "legacy voices have distinct ids")
expect(all(not item["is_default"] for item in legacy), "legacy voices are not default")
expect(all(item["status"] == "active" for item in legacy), "legacy voices are selectable")
expect(
all(item["style_context"] == FULL_STYLE_CONTEXT for item in legacy),
"legacy voices reconstruct the former full style context",
)
by_key = {item["guideline_key"]: item for item in legacy}
for old_key, spec in HEAD_VOICE.items():
row = by_key[f"legacy_{old_key}"]
expect(row["id"] != spec["id"], f"legacy {old_key} uses a new stable id")
expect(row["instruction"] == spec["instruction"], f"legacy {old_key} keeps the reconstructed instruction")
expect("Legacy – vor Stilkontextsteuerung" in row["label"], f"legacy {old_key} is labeled as pre-style-context")
expect(row["id"].startswith("journal-generate-voice-legacy-"), f"legacy {old_key} id is namespaced")
def test_stored_selection_and_clones_and_archives() -> None:
init_db()
profile_id = "legacy-immutable-user"
with get_db() as conn:
conn.execute(
"""
INSERT OR IGNORE INTO profiles (id, email, name, password_hash, role)
VALUES (?, ?, ?, ?, 'user')
""",
(profile_id, "legacy@example.test", "Legacy", "x"),
)
stored = default_selection_ids()
stored["voice_id"] = CURRENT_VOICE_IDS["neutral"]
save_selection(profile_id, stored)
cloned = clone_guideline(CURRENT_VOICE_IDS["clear"])
original_clone_instruction = "CLONE MUST STAY"
update_guideline(
cloned["id"],
{
"guideline_key": "clone_clear",
"label": "Klon",
"summary": "Admin-Klon",
"instruction": original_clone_instruction,
"style_context": {
"include_core": True,
"include_facet": False,
"include_traits": False,
"include_style_examples": False,
},
},
)
published_clone = publish_guideline(clone_guideline(CURRENT_VOICE_IDS["light"])["id"])
archived = archive_guideline(published_clone["id"])
archived_instruction = get_guideline(archived["id"], include_instruction=True)["instruction"]
archived_status = archived["status"]
with get_db() as conn:
seed_generation_instructions(conn)
expect(load_selection(profile_id)["voice_id"] == CURRENT_VOICE_IDS["neutral"], "stored selection is not rewritten")
expect(
get_guideline(cloned["id"], include_instruction=True)["instruction"] == original_clone_instruction,
"admin clone is not overwritten by seed",
)
expect(get_guideline(archived["id"], include_instruction=True)["status"] == archived_status, "archived variant stays archived")
expect(
get_guideline(archived["id"], include_instruction=True)["instruction"] == archived_instruction,
"archived variant is not rewritten",
)
expect(
get_guideline(CURRENT_VOICE_IDS["clear"], include_instruction=True)["is_default"] is True,
"current default is not reset by a legacy seed pass",
)
def test_later_semantic_seed_creates_successor() -> None:
init_db()
profile_id = "legacy-successor-user"
with get_db() as conn:
conn.execute(
"""
INSERT OR IGNORE INTO profiles (id, email, name, password_hash, role)
VALUES (?, ?, ?, ?, 'user')
""",
(profile_id, "succ@example.test", "Succ", "x"),
)
stored = default_selection_ids()
save_selection(profile_id, stored)
original = get_guideline(CURRENT_VOICE_IDS["clear"], include_instruction=True)
mutated = copy.deepcopy(load_seed_document())
for item in mutated["slots"]["voice"]["variants"]:
if item.get("id") == CURRENT_VOICE_IDS["clear"]:
item["instruction"] = "CHANGED FUTURE SEMANTICS FOR CLEAR"
item["label"] = "Persönliche Stimme deutlich (neu)"
break
mutated["seed_revision"] = "2026-08-30-future-voice-v1"
with get_db() as conn:
seed_generation_instructions(conn, mutated)
seed_generation_instructions(conn, mutated)
kept = get_guideline(CURRENT_VOICE_IDS["clear"], include_instruction=True)
expect(kept["instruction"] == original["instruction"], "future seed does not overwrite the existing variant id")
expect(kept["label"] == original["label"], "future seed does not change the existing label")
successors = [
item
for item in voice_rows()
if item.get("cloned_from") == CURRENT_VOICE_IDS["clear"]
and item["instruction"] == "CHANGED FUTURE SEMANTICS FOR CLEAR"
]
expect(len(successors) == 1, "changed semantics are inserted once as a successor")
successor = successors[0]
expect(successor["id"] != CURRENT_VOICE_IDS["clear"], "successor uses a new id")
expect(successor["revision"] == int(original["revision"]) + 1, "successor raises the revision")
expect(successor["cloned_from"] == CURRENT_VOICE_IDS["clear"], "successor records the predecessor")
expect(successor["is_default"] is True, "new default may point at the successor")
expect(kept["is_default"] is False, "previous default is released when the seed names a new default")
expect(
load_selection(profile_id)["voice_id"] == CURRENT_VOICE_IDS["clear"],
"stored selection keeps the previous id after a semantic successor is added",
)
compiled = compile_selection({**default_selection_ids(), "voice_id": CURRENT_VOICE_IDS["clear"]})
expect(compiled.ids["voice"] == CURRENT_VOICE_IDS["clear"], "previous id remains selectable with its old meaning")
expect(compiled.cloned_from["voice"] == "", "original current variant has no predecessor")
compiled_new = compile_selection({**default_selection_ids(), "voice_id": successor["id"]})
expect(compiled_new.cloned_from["voice"] == CURRENT_VOICE_IDS["clear"], "compiled policy exposes the predecessor")
def test_legacy_generate_trace_and_prompt() -> None:
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
init_db()
client = TestClient(app)
setup = client.post(
"/api/auth/setup",
json={"email": "legacy-gen@example.test", "password": "pass-pass", "name": "Legacy"},
)
expect(setup.status_code == 200, f"setup {setup.status_code}")
token = setup.json()["token"]
headers = header(token)
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Urlaub"})
expect(space.status_code == 200, f"space {space.status_code}")
day = client.post(
f"/api/journal/spaces/{space.json()['id']}/days",
headers=headers,
json={"calendar_date": "2026-08-29"},
)
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": "Ich war am Markt. Vielleicht bleibe ich kürzer."},
)
expect(turn.status_code == 200, f"turn {turn.status_code}")
day_id = day.json()["day"]["id"]
conv_id = conv.json()["id"]
selection = default_selection_ids()
reset_debug()
recorder = install_test_recorder()
current = client.post(
f"/api/journal/days/{day_id}/generate",
headers=headers,
json={"conversation_ids": [conv_id], "generation_selection": selection, "remember_generation_selection": False},
)
expect(current.status_code == 200, f"current generate {current.status_code}")
current_payload = current.json()
current_intern = intern_of(current_payload)
current_trace = (current_payload.get("trace") or {}).get("style_application") or {}
expect([item.get("purpose") for item in recorder] == ["journal_generate"], "current path still uses one narration call")
expect(current_trace.get("id") == CURRENT_VOICE_IDS["clear"], "trace keeps the current voice id")
expect(current_trace.get("revision") == 1, "trace keeps the current revision")
expect("STYLE_EXAMPLES" not in current_intern, "current default prompt has no STYLE_EXAMPLES reference")
expect("\nSTYLE_EXAMPLES\n" not in current_intern, "current default prompt has no example block")
formatted = format_style_examples(
[{"kind": "journal_entry", "occurred_at": "2026-08-01", "excerpt": "Nur ein Stilbeispiel."}]
)
expect("keine Tatsachen des heutigen" in formatted or "nicht übernommen" in formatted, "examples stay style-only when present")
reset_debug()
recorder = install_test_recorder()
selection["voice_id"] = seed_id("voice", "legacy_neutral")
legacy = client.post(
f"/api/journal/days/{day_id}/generate",
headers=headers,
json={"conversation_ids": [conv_id], "generation_selection": selection, "remember_generation_selection": False},
)
expect(legacy.status_code == 200, f"legacy generate {legacy.status_code}")
payload = legacy.json()
intern = intern_of(payload)
app_trace = (payload.get("trace") or {}).get("style_application") or {}
expect([item.get("purpose") for item in recorder] == ["journal_generate"], "legacy path still uses one narration call")
expect(app_trace.get("id") == seed_id("voice", "legacy_neutral"), "legacy trace uses the reconstructed id")
expect(app_trace.get("revision") == 1, "legacy trace uses the reconstructed revision")
expect(app_trace.get("key") == "legacy_neutral", "legacy trace names the reconstructed key")
expect("höchstens als leise Tendenz" in intern, "legacy instruction reaches the provider prompt")
expect(app_trace.get("requested", {}).get("include_style_examples") is True, "legacy requests the former full style context")
def main() -> None:
test_current_variants_and_legacy_seed()
test_stored_selection_and_clones_and_archives()
test_later_semantic_seed_creates_successor()
test_legacy_generate_trace_and_prompt()
print("journal style legacy immutable tests passed.")
if __name__ == "__main__":
main()
+2 -2
View File
@@ -327,7 +327,7 @@ def main() -> None:
"WRITING_PROFILE" in narrate_intern or "Neutraler Journalstil" in narrate_intern or "Core:" in narrate_intern,
"A style brief reaches generate",
)
expect("STYLE_EXAMPLES" in narrate_intern, "A style examples block is labeled")
expect("STYLE_EXAMPLES" not in narrate_intern, "A default voice does not attach a style-examples block")
style_part = narrate_intern.split("\nCURRENT_DAY_SOURCES\n")[0]
expect("Erzählmerkmale" not in style_part, "A current day dialogue is not appended as style signals")
expect("Neutraler Journalstil" in style_part or "Core:" in style_part, "A empty profile uses the neutral journal voice")
@@ -516,7 +516,7 @@ def main() -> None:
if stage.get("purpose") == "journal_generate":
intern_i = stage.get("intern") or ""
intern_i = intern_i or ((gen_i.json().get("trace") or {}).get("intern") or "")
expect("STYLE_EXAMPLES" in intern_i, "I style examples reach generate")
expect("STYLE_EXAMPLES" not in intern_i, "I default voice does not send imported texts as style examples")
expect("WRITING_PROFILE" in intern_i or "Neutraler Journalstil" in intern_i, "I writing profile reaches generate")
expect("knapp" in imported.json()["compiled_brief"], "I stored brief still lists the import")
profile = client.get("/api/journal/writing-profile", headers=headers).json()
+17 -9
View File
@@ -134,12 +134,15 @@ def main() -> None:
0,
)
expect(span.text == "Anna" and span.start == start, "valid span matches the local substring")
relocated = validate_detected_entity(
{"start": 0, "end": 4, "text": "Anna", "entity_type": "PERSON"},
sample,
0,
expect(
validate_detected_entity(
{"start": 0, "end": 4, "text": "Anna", "entity_type": "PERSON"},
sample,
0,
).start
== start,
"wrong offsets are grounded to the local word",
)
expect(relocated is not None and relocated.start == start and relocated.text == "Anna", "wrong offsets are grounded to the local word")
expect(
validate_detected_entity(
{"start": -1, "end": 4, "text": "Anna", "entity_type": "PERSON"},
@@ -149,15 +152,20 @@ def main() -> None:
== start,
"negative offsets still ground when the word exists",
)
expect(
try:
validate_detected_entity(
{"start": 0, "end": 4, "text": "Nein", "entity_type": "PERSON"},
sample,
0,
)
is None,
"a word that is not in the chunk is skipped",
)
missing_word = False
except DetectError as exc:
missing_word = exc.code == "detect_invalid_output"
expect(
(exc.diagnostics or {}).get("contract_violation") == "unusable_offsets",
"a word that is not in the chunk is a contract violation",
)
expect(missing_word, "a word that is not in the chunk fails closed")
expect(
validate_detected_entity(
{"start": "x", "end": 4, "text": "Anna", "entity_type": "PERSON"},
+181
View File
@@ -0,0 +1,181 @@
"""Profile-review failures expose reason and whether the provider was billed.
Run from backend/: python tests/test_profile_review_errors.py
No live provider calls.
"""
from __future__ import annotations
import os
import sys
import tempfile
from pathlib import Path
from unittest.mock import patch
import httpx
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-profile-review-errors-test.sqlite")
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
os.environ["KANSHO_FAKE_DETECT"] = "1"
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
from fastapi.testclient import TestClient
from engine import EngineError
from main import app
from privacy_gateway import public_cost_report
from providers import ProviderConfig, complete_chat, provider_error_diagnostics
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 _openrouter() -> ProviderConfig:
return ProviderConfig(
role="generate",
name="openrouter",
mode="http",
url="https://openrouter.ai/api/v1/chat/completions",
model="openai/gpt-4o",
key="sk-test",
local=False,
zdr=True,
no_train=True,
)
def main() -> None:
none = public_cost_report({})
expect(none["billed"] == "no", "empty diagnostics are unbilled")
expect("keine Kosten entstanden" in none["note"], "empty diagnostics say no provider call")
detect_only = public_cost_report(
{"detect_calls": 2, "detect_cost": 0.0012, "generate_called": False, "generate_requested": False}
)
expect(detect_only["billed"] == "yes", "detect cost counts as billed")
expect("Detect" in detect_only["note"] and "Generate nicht" in detect_only["note"], "detect-only note names both stages")
sent = public_cost_report(
{"detect_calls": 1, "detect_cost": 0.0004, "generate_requested": True, "generate_called": False}
)
expect(sent["billed"] == "yes", "known detect cost stays billed if generate is unknown")
expect("Generate-Aufruf gesendet" in sent["note"], "unknown generate request is explicit")
unknown = public_cost_report({"generate_requested": True, "generate_called": False})
expect(unknown["billed"] == "unknown", "generate request without any usage is unknown")
known = public_cost_report(
{"detect_calls": 1, "detect_cost": 0.0004, "generate_called": True, "cost": 0.021}
)
expect(known["billed"] == "yes", "generate usage is billed")
expect(abs((known["total_cost"] or 0) - 0.0214) < 1e-9, "total adds detect and generate")
parsed = provider_error_diagnostics(
402,
'{"error":{"message":"Insufficient credits","code":402},"usage":{"cost":0.0}}',
)
expect(parsed["http_status"] == 402, "provider diagnostics keep HTTP status")
expect(parsed["provider_message"] == "Insufficient credits", "provider message is copied")
expect(parsed.get("cost") == 0.0, "error usage cost is kept")
class Reject:
status_code = 402
text = '{"error":{"message":"Insufficient credits","code":402}}'
def json(self):
return {"error": {"message": "Insufficient credits", "code": 402}}
try:
with patch("providers.httpx.post", lambda *args, **kwargs: Reject()):
complete_chat(_openrouter(), [{"role": "user", "content": "x"}], timeout=5)
raise SystemExit("FAIL: rejected complete_chat should raise")
except Exception as exc:
expect(getattr(exc, "code", "") == "provider_rejected", "credit reject is provider_rejected")
expect("HTTP 402" in str(exc), "reject message includes HTTP status")
expect("Insufficient credits" in str(exc), "reject message includes provider reason")
expect(exc.diagnostics.get("generate_requested") is True, "reject marks generate as requested")
def boom(*args, **kwargs):
raise httpx.TimeoutException("timed out")
try:
with patch("providers.httpx.post", boom):
complete_chat(_openrouter(), [{"role": "user", "content": "x"}], timeout=12)
raise SystemExit("FAIL: timeout should raise")
except Exception as exc:
expect(getattr(exc, "code", "") == "provider_timeout", "timeout is distinct from unreachable")
expect("12 Sekunden" in str(exc), "timeout names the limit")
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.text}")
token = setup.json()["token"]
headers = header(token)
imported = client.post(
"/api/journal/writing-profile/import",
headers=headers,
json={"body": "Heute Kirschen am Markt, später der Hafen, und abends nur zwei knappe Zeilen."},
)
expect(imported.status_code == 200, f"import {imported.text}")
rejected = EngineError(
"provider_rejected",
"generate-Provider hat die Anfrage abgelehnt (HTTP 402): Insufficient credits",
502,
diagnostics={
"http_status": 402,
"provider_message": "Insufficient credits",
"generate_requested": True,
"generate_called": False,
"detect_calls": 1,
"detect_cost": 0.0015,
},
)
with patch("engine.execute_prompt", side_effect=rejected):
response = client.post("/api/journal/writing-profile/initial-build/api", headers=headers)
expect(response.status_code == 502, f"rejected review status {response.text}")
detail = response.json()["detail"]
expect(detail["code"] == "provider_rejected", "API keeps provider code")
expect("Insufficient credits" in detail["message"], "API message keeps provider reason")
expect(detail["cost_report"]["billed"] == "yes", "detect cost is reported as billed")
expect("Detect" in detail["cost_report"]["note"], "cost note mentions detect")
expect(detail["cost_report"]["generate_requested"] is True, "generate was requested")
expect(detail["cost_report"]["generate_called"] is False, "generate did not complete")
invalid = {
"content": "kein json",
"diagnostics": {
"generate_called": True,
"generate_requested": True,
"cost": 0.033,
"detect_calls": 1,
"detect_cost": 0.002,
},
"trace": {"generate_called": True, "purpose": "profile_review"},
}
with patch("engine.execute_prompt", return_value=invalid):
response = client.post("/api/journal/writing-profile/review/api", headers=headers)
expect(response.status_code == 400, f"invalid result status {response.text}")
detail = response.json()["detail"]
expect(detail["code"] == "invalid_review_result", "parse failure keeps store code")
expect("JSON" in detail["message"] or "json" in detail["message"].lower(), "parse failure explains JSON")
expect(detail["cost_report"]["billed"] == "yes", "successful generate before parse failure is billed")
expect(detail["cost_report"]["generate_called"] is True, "parse failure still records generate")
expect(abs((detail["cost_report"]["cost"] or 0) - 0.033) < 1e-9, "generate cost survives parse failure")
print("ALL TESTS PASSED")
if __name__ == "__main__":
main()