Add pre-ruling interim draft (טיוטת ביניים) for appeals committee
All checks were successful
Build & Deploy / build-and-deploy (push) Successful in 1m26s

Lets the chair generate a partial decision DOCX before the discussion-and-
ruling block is decided. Same template, skill and DOCX styling as the final
decision (David, RTL, bookmarks) — only the block selection and order differ:
רקע (ו) → תכניות+היתרים (ט) → טענות (ז) → הליכים (ח). The opening (ה),
ruling (י), summary (יא), and signatures (יב) are omitted.

- New appraiser_facts table + CRUD + conflict detection in db.py (V5 schema).
  Conflict = same plan/permit identifier reported differently by 2+ appraisers.
- New appraiser_facts_extractor service: per-appraisal Claude extraction of
  plans + permits with raw quotes and page numbers.
- block-tet prompt extended with a permits sub-section sourced from the
  extracted facts, plus an explicit instruction to flag inter-appraiser
  conflicts in neutral wording without resolving them (deferred to block-yod).
- block-chet prompt extended with a post-hearing materials context sourced
  from documents.metadata.is_post_hearing.
- docx_exporter.export_decision now accepts mode='interim' which reorders
  the blocks per the chair's mental model and writes
  טיוטת-ביניים-v{N}.docx (versioned independently of regular drafts).
- 3 new MCP tools: extract_appraiser_facts, write_interim_draft,
  export_interim_draft. write_interim_draft auto-runs extraction if the
  appraiser_facts table is empty for the case.
This commit is contained in:
2026-04-18 13:28:04 +00:00
parent 2b40e02a65
commit c619c22a51
7 changed files with 731 additions and 17 deletions

View File

@@ -168,16 +168,46 @@ def _add_image_placeholder(doc, description: str) -> None:
# ── Main export ───────────────────────────────────────────────────
async def export_decision(case_id: UUID, output_path: str | None = None) -> str:
# Order in which blocks are emitted for each export mode.
# 'final' = standard 12-block decision in canonical order (block_index).
# 'interim' = pre-ruling draft requested by the chair before ratio decidendi
# is set: רקע → תכניות+היתרים → טענות → הליכים, omitting opening (ה),
# ruling (י), summary (יא), and signatures (יב).
_INTERIM_BLOCK_ORDER = [
"block-alef", # institutional header (skipped if empty — first page optional)
"block-bet", # panel (skipped if empty)
"block-gimel", # parties (skipped if empty)
"block-dalet", # "החלטה" title (skipped if empty)
"block-vav", # רקע עובדתי
"block-tet", # תכניות + היתרים (extended)
"block-zayin", # טענות הצדדים
"block-chet", # הליכים (incl. post-hearing)
]
def _draft_filename_prefix(mode: str) -> str:
return "טיוטת-ביניים" if mode == "interim" else "טיוטה"
async def export_decision(
case_id: UUID,
output_path: str | None = None,
mode: str = "final",
) -> str:
"""ייצוא החלטה ל-DOCX.
Args:
case_id: מזהה התיק
output_path: נתיב לשמירה (אופציונלי)
mode: 'final' (ברירת מחדל) או 'interim' (טיוטת ביניים — ללא
דיון/סיכום/חתימות, סדר חדש: רקע → תכניות+היתרים → טענות → הליכים)
Returns:
נתיב הקובץ שנוצר
"""
if mode not in ("final", "interim"):
raise ValueError(f"Unknown export mode: {mode}")
case = await db.get_case(case_id)
if not case:
raise ValueError(f"Case {case_id} not found")
@@ -189,7 +219,7 @@ async def export_decision(case_id: UUID, output_path: str | None = None) -> str:
# Get blocks
pool = await db.get_pool()
async with pool.acquire() as conn:
blocks = await conn.fetch(
rows = await conn.fetch(
"""SELECT block_id, block_index, title, content, word_count
FROM decision_blocks
WHERE decision_id = $1
@@ -197,9 +227,20 @@ async def export_decision(case_id: UUID, output_path: str | None = None) -> str:
UUID(decision["id"]),
)
if not blocks:
if not rows:
raise ValueError("No blocks in decision")
by_id = {r["block_id"]: r for r in rows}
if mode == "interim":
ordered_blocks = [by_id[bid] for bid in _INTERIM_BLOCK_ORDER if bid in by_id]
if not ordered_blocks:
raise ValueError(
"אין בלוקים מתאימים לטיוטת ביניים. הרץ write_interim_draft קודם."
)
else:
ordered_blocks = list(rows)
# Create document
doc = Document()
@@ -213,7 +254,7 @@ async def export_decision(case_id: UUID, output_path: str | None = None) -> str:
# Write blocks with bookmarks wrapping each block (anchors for revisions)
bm_counter = [_BOOKMARK_ID_START]
for block in blocks:
for block in ordered_blocks:
block_id = block["block_id"]
content = block["content"] or ""
if not content.strip():
@@ -232,8 +273,8 @@ async def export_decision(case_id: UUID, output_path: str | None = None) -> str:
if not output_path:
export_dir = config.find_case_dir(case["case_number"]) / "exports"
export_dir.mkdir(parents=True, exist_ok=True)
# Find next version number
existing = sorted(export_dir.glob("טיוטה-v*.docx"))
prefix = _draft_filename_prefix(mode)
existing = sorted(export_dir.glob(f"{prefix}-v*.docx"))
next_ver = 1
for p in existing:
try:
@@ -241,11 +282,11 @@ async def export_decision(case_id: UUID, output_path: str | None = None) -> str:
next_ver = max(next_ver, ver + 1)
except (IndexError, ValueError):
pass
output_path = str(export_dir / f"טיוטה-v{next_ver}.docx")
output_path = str(export_dir / f"{prefix}-v{next_ver}.docx")
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
doc.save(output_path)
logger.info("DOCX exported: %s", output_path)
logger.info("DOCX exported (mode=%s): %s", mode, output_path)
return output_path