feat(learning): feed corpus-measured ratios to the writer (P2 #7, structural half) #345

Merged
chaim merged 1 commits from worktree-corpus-ratios-writer into main 2026-06-28 22:11:13 +00:00
Showing only changes of commit 638dbf9b9f - Show all commits

View File

@@ -297,6 +297,32 @@ STRUCTURE_GUIDANCE = {
"partial_acceptance": "קבלה חלקית — מיפוי מתחים: מה מתקבל ולמה, מה נדחה ולמה, איזון.",
}
_RATIO_SECTION_HE = {"background": "רקע", "claims": "טענות", "discussion": "דיון", "summary": "סיכום"}
async def _structure_guidance_with_corpus(outcome: str, practice_area: str) -> str:
"""Outcome structure guidance + the CORPUS-MEASURED section ratios (T10). The
measured ratios come from style_distance.measure_corpus_ratios over style_corpus,
which grows with every enrolled final — so the writer's structural target reflects
Dafna's ACTUAL current distribution, not a frozen constant (this is the structural
half of the voice-profile that learns from each decision). Deterministic (no LLM);
best-effort. Shared by both writer entry points (G2)."""
sg = STRUCTURE_GUIDANCE.get(outcome, "")
if practice_area == "betterment_levy":
sg = (sg + " | היטל השבחה: "
+ " ".join(PRACTICE_AREA_OVERRIDES["betterment_levy"]["discussion_rules"])).strip()
try:
from legal_mcp.services.style_distance import measure_corpus_ratios
entry = (await measure_corpus_ratios() or {}).get(outcome) or {}
secs = entry.get("sections") or {}
if secs:
parts = [f"{_RATIO_SECTION_HE.get(s, s)} ~{round(p)}%" for s, p in secs.items()]
sg = (sg + f" | מבנה-יעד מדוד מהקורפוס ({entry.get('n', 0)} החלטות {outcome}): "
+ " · ".join(parts)).strip()
except Exception as e:
logger.warning("corpus ratios not loaded for structure guidance: %s", e)
return sg
async def write_block(
case_id: UUID,
@@ -352,12 +378,8 @@ async def write_block(
post_hearing_context = await _build_post_hearing_context(case_id)
outcome = canonical_outcome((decision or {}).get("outcome", "rejection"))
structure_guidance = STRUCTURE_GUIDANCE.get(outcome, "")
if case.get("practice_area") == "betterment_levy":
structure_guidance = (
structure_guidance + " | היטל השבחה: "
+ " ".join(PRACTICE_AREA_OVERRIDES["betterment_levy"]["discussion_rules"])
).strip()
structure_guidance = await _structure_guidance_with_corpus(
outcome, case.get("practice_area", ""))
# Content checklist — tells block-yod WHAT topics to cover
content_checklist = ""
@@ -1094,12 +1116,8 @@ async def get_block_context(case_id: UUID, block_id: str, instructions: str = ""
post_hearing_context = await _build_post_hearing_context(case_id)
outcome = canonical_outcome((decision or {}).get("outcome", "rejection"))
structure_guidance = STRUCTURE_GUIDANCE.get(outcome, "")
if case.get("practice_area") == "betterment_levy":
structure_guidance = (
structure_guidance + " | היטל השבחה: "
+ " ".join(PRACTICE_AREA_OVERRIDES["betterment_levy"]["discussion_rules"])
).strip()
structure_guidance = await _structure_guidance_with_corpus(
outcome, case.get("practice_area", ""))
# Content checklist + methodology for block-yod
content_checklist = ""