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

+48 -11
View File
@@ -11,11 +11,11 @@ from journal_policy import require_explicit_generate, source_conversation_ids
from identity_store import is_maskable_label, list_mappings, mapping_spellings
from journal_body import clean_title
from journal_editorial import (
format_style_examples,
compile_journal_style_application,
incomplete_syntax_markers,
lexical_similarity,
narration_sources_text,
select_journal_style_examples,
style_application_trace,
style_example_diagnostics,
writing_profile_trace,
)
@@ -26,6 +26,7 @@ from journal_generation_policy import (
draft_snapshot,
mark_guidelines_used,
policy_trace,
prompt_without_orphaned_style_example_rule,
resolve_run_selection,
)
from journal_reconstruct import (
@@ -45,6 +46,7 @@ from prompt_budget import (
from privacy_gateway import (
GENERIC_PLACEHOLDER_INNER,
canonical_token,
compact_diagnostics,
identity_label_pattern,
identity_occurrence_count,
is_identity_mention,
@@ -201,6 +203,8 @@ def _compose_journal_trace(
narrate_trace["writing_profile"] = profile_meta
narrate_trace["style_examples"] = {**style_meta, "dropped": "style_examples" in dropped}
narrate_trace["dropped_optional_blocks"] = dropped
if extra and extra.get("style_application"):
narrate_trace["style_application"] = extra["style_application"]
diag = narrate_result.get("diagnostics") or {}
if diag.get("generate_ms") is not None:
narrate_trace["generate_ms"] = diag.get("generate_ms")
@@ -221,8 +225,9 @@ def _compose_journal_trace(
"prompt_revision": narrate_trace.get("prompt_revision"),
"writing_profile": profile_meta,
"style_examples": narrate_trace.get("style_examples"),
"style_application": narrate_trace.get("style_application") or (extra or {}).get("style_application"),
"dropped_optional_blocks": dropped,
"log": run_log,
"log": [dict(item) if isinstance(item, dict) else item for item in run_log],
"stages": [reconstruct_trace, narrate_trace],
}
@@ -264,11 +269,17 @@ def _raise_budget(exc: JournalBudgetError) -> None:
def _stage_trace(result: dict, fallback_purpose: str) -> dict:
trace = dict(result.get("trace") or {})
if result.get("diagnostics"):
trace["budget"] = result.get("diagnostics")
elif not trace.get("budget"):
source = result.get("trace")
trace = dict(source) if isinstance(source, dict) else {}
budget_src = result.get("diagnostics")
if not isinstance(budget_src, dict):
budget_src = trace.get("budget") if isinstance(trace.get("budget"), dict) else None
if isinstance(budget_src, dict):
trace["budget"] = compact_diagnostics(budget_src)
else:
trace["budget"] = None
if "log" in trace and isinstance(trace["log"], list):
trace["log"] = [dict(item) if isinstance(item, dict) else item for item in trace["log"]]
trace["purpose"] = trace.get("purpose") or fallback_purpose
return trace
@@ -333,7 +344,11 @@ def pack_narration_context(
candidate = dict(assembled)
candidate["style_examples"] = examples
candidate["existing_text"] = existing
rendered = preview_prompt(prompt, candidate)["rendered"]
fit_prompt = prompt_without_orphaned_style_example_rule(
prompt,
include_style_examples=bool((examples or "").strip()),
)
rendered = preview_prompt(fit_prompt, candidate)["rendered"]
try:
assert_input_fits(budget, rendered)
if not examples and style_examples:
@@ -464,12 +479,20 @@ def _generate_draft(
source=persist_meta["source"],
remembered=persist_meta["remembered"],
)
profile_meta = writing_profile_trace(profile_id)
style_example_rows = select_journal_style_examples(
style_bundle = compile_journal_style_application(
profile_id,
compiled_policy.style_context,
exclude_dates=[day.get("calendar_date") or ""],
)
style_examples = format_style_examples(style_example_rows)
profile_meta = writing_profile_trace(
profile_id,
style_context=compiled_policy.style_context,
brief=style_bundle.get("brief") or "",
trait_count=(style_bundle.get("effective") or {}).get("trait_count"),
)
style_example_rows = list(style_bundle.get("example_rows") or [])
style_examples = style_bundle.get("examples_block") or ""
writing_profile = style_bundle.get("profile_block") or ""
style_meta = style_example_diagnostics(style_example_rows)
run_log: list[dict] = []
_event(
@@ -503,6 +526,7 @@ def _generate_draft(
existing_text=existing_text,
conversation_id=selected[0] if selected else None,
reconstruction=narration_sources_text(reconstruction),
writing_profile=writing_profile,
style_examples=style_examples,
transformation_instructions=compiled_policy.instructions["transformation_instructions"],
detail_instructions=compiled_policy.instructions["detail_instructions"],
@@ -523,11 +547,21 @@ def _generate_draft(
_raise_budget(exc)
if dropped:
_event(run_log, "journal_generate", "budget_pack", dropped=",".join(dropped))
style_app = style_application_trace(compiled_policy, style_bundle, dropped=dropped)
if "style_examples" in dropped:
style_meta = {**style_example_diagnostics([]), "dropped": True, "dropped_reason": "budget"}
else:
style_meta = {**style_meta, "dropped": False}
profile_meta["brief_chars"] = len((assembled.get("writing_profile") or "").strip())
mappings = list_mappings(profile_id)
preview_title, preview_body = _local_narration(reconstruction)
source_preview = f"{preview_title}\n\n{preview_body}".strip()
narrate_result: dict = {}
try:
narrate_prompt = prompt_without_orphaned_style_example_rule(
narrate_prompt,
include_style_examples=bool((assembled.get("style_examples") or "").strip()),
)
rendered_preview = preview_prompt(narrate_prompt, assembled)
assert_template_resolved(rendered_preview.get("rendered") or "")
narrate_result = execute_prompt(
@@ -575,6 +609,7 @@ def _generate_draft(
"abort_reason": unattested,
"source_preview": source_preview,
"generation_selection": policy_meta,
"style_application": style_app,
},
)
_reject_generation(
@@ -622,6 +657,7 @@ def _generate_draft(
"abort_reason": exc.code,
"source_preview": source_preview,
"generation_selection": policy_meta,
"style_application": style_app,
},
)
diag["log"] = run_log
@@ -700,6 +736,7 @@ def _generate_draft(
"lexical_similarity": lexical_similarity("\n".join(source_user), body),
"incomplete_syntax": incomplete_syntax_markers(body),
"generation_selection": policy_meta,
"style_application": style_app,
"stored_title": title,
"stored_body": body,
"reply": stored,