|
|
|
|
@@ -360,13 +360,9 @@ async def write_block(
|
|
|
|
|
post_hearing_context=post_hearing_context,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Restructure: sources first, then instructions
|
|
|
|
|
prompt = (
|
|
|
|
|
f"## חומרי מקור (מסמכים מלאים — צטט מהם מילה במילה כשאפשר):\n\n"
|
|
|
|
|
f"{source_context}\n\n"
|
|
|
|
|
f"---\n\n"
|
|
|
|
|
f"{formatted_prompt}"
|
|
|
|
|
)
|
|
|
|
|
# source_context is already embedded inside formatted_prompt via {source_context} in the
|
|
|
|
|
# template. Do NOT prepend it again — doing so doubles the prompt size (was 465K chars).
|
|
|
|
|
prompt = formatted_prompt
|
|
|
|
|
|
|
|
|
|
if instructions:
|
|
|
|
|
prompt += f"\n\n## הנחיות נוספות:\n{instructions}"
|
|
|
|
|
@@ -377,6 +373,19 @@ async def write_block(
|
|
|
|
|
if not dir_doc.get("approved"):
|
|
|
|
|
raise ValueError("לא ניתן לכתוב בלוק דיון ללא כיוון מאושר. הפעל brainstorm → approve_direction קודם.")
|
|
|
|
|
|
|
|
|
|
# Guard against context overflow before calling claude -p.
|
|
|
|
|
# Sonnet: 200K context → ~800K chars max; Opus: 200K context → same.
|
|
|
|
|
# In practice the CLI has crashed on prompts above ~400K chars, so use
|
|
|
|
|
# that as a conservative ceiling (well below the token limit).
|
|
|
|
|
_MAX_PROMPT_CHARS = 400_000
|
|
|
|
|
if len(prompt) > _MAX_PROMPT_CHARS:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"Prompt too large for {block_id}: {len(prompt):,} chars "
|
|
|
|
|
f"(limit {_MAX_PROMPT_CHARS:,}). "
|
|
|
|
|
f"source_context: {len(source_context):,} chars. "
|
|
|
|
|
f"Reduce documents or call extract_appraiser_facts first."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Call Claude via Claude Code session (no API)
|
|
|
|
|
model_key = block_cfg["model"]
|
|
|
|
|
timeout = claude_session.LONG_TIMEOUT if model_key == "opus" else claude_session.DEFAULT_TIMEOUT
|
|
|
|
|
@@ -414,16 +423,35 @@ def _build_case_context(case: dict, decision: dict | None) -> str:
|
|
|
|
|
- תוצאה: {outcome_heb}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Which doc_types are relevant per block.
|
|
|
|
|
# None → skip source docs entirely (block uses other context, e.g. claims_context)
|
|
|
|
|
# [] → include all doc types (default for unspecified blocks)
|
|
|
|
|
# [..] → include only the listed doc_type values
|
|
|
|
|
_BLOCK_DOC_TYPES: dict[str, list[str] | None] = {
|
|
|
|
|
"block-he": None, # only case_context needed; no full docs
|
|
|
|
|
"block-vav": ["appeal", "protocol"], # כתב ערר + פרוטוקול ועדה
|
|
|
|
|
"block-zayin": None, # claims_context is sufficient
|
|
|
|
|
"block-chet": ["protocol"], # פרוטוקול + השלמות טיעון
|
|
|
|
|
"block-tet": ["appraisal"], # שומות בלבד
|
|
|
|
|
# block-yod, block-yod-alef, block-he etc. default → all docs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _build_source_context(case_id: UUID, block_id: str) -> str:
|
|
|
|
|
"""Get full document texts for the block.
|
|
|
|
|
"""Get document texts for the block, filtered by relevance.
|
|
|
|
|
|
|
|
|
|
Per Anthropic best practices: send full source documents, not truncated excerpts.
|
|
|
|
|
Place documents at the TOP of the prompt (before instructions) for 30% better recall.
|
|
|
|
|
For grounding: instruct Claude to cite word-for-word from these documents.
|
|
|
|
|
Per-block filtering prevents context overflow on large cases (9+ docs).
|
|
|
|
|
"""
|
|
|
|
|
allowed = _BLOCK_DOC_TYPES.get(block_id, []) # [] sentinel = not in map → all docs
|
|
|
|
|
if allowed is None:
|
|
|
|
|
return "" # this block doesn't need raw source docs
|
|
|
|
|
|
|
|
|
|
docs = await db.list_documents(case_id)
|
|
|
|
|
context_parts = []
|
|
|
|
|
for doc in docs:
|
|
|
|
|
if allowed and doc["doc_type"] not in allowed:
|
|
|
|
|
continue
|
|
|
|
|
text = await db.get_document_text(UUID(doc["id"]))
|
|
|
|
|
if text:
|
|
|
|
|
context_parts.append(f"--- מסמך: {doc['title']} ({doc['doc_type']}) ---\n{text}")
|
|
|
|
|
|