fix(retrieval): scope search_decisions by domain — derive from case, block only on undeterminable case (GAP-12, INV-RET1)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-05-30 18:23:41 +00:00
parent bd6edb8937
commit 0c8d415044
2 changed files with 164 additions and 2 deletions

View File

@@ -7,7 +7,7 @@ import logging
import time
from uuid import UUID
from legal_mcp.services import db, embeddings, hybrid_search, telemetry
from legal_mcp.services import db, embeddings, hybrid_search, practice_area as pa, telemetry
logger = logging.getLogger(__name__)
@@ -30,7 +30,9 @@ async def search_decisions(
appeal_subtype: סוג ערר לסינון (building_permit/betterment_levy/compensation_197)
case_number: אם סופק, ה-practice_area/subtype יוסקו אוטומטית מהתיק
"""
# Auto-resolve practice_area from case_number if available
# Auto-resolve practice_area from case_number if available (GAP-12 / INV-RET1):
# explicit practice_area wins; otherwise derive from the case so the search is
# scoped to the case's legal domain. Case-less search stays cross-domain.
resolved_case_id: UUID | None = None
if case_number and not practice_area:
case = await db.get_case_by_number(case_number)
@@ -42,6 +44,22 @@ async def search_decisions(
except (KeyError, ValueError, TypeError):
resolved_case_id = None
# Case row had no practice_area — fall back to deriving from the
# case-number prefix (1xxx/8xxx/9xxx). Returns "" for unknown prefixes.
if not practice_area:
practice_area = pa.derive_domain_practice_area(case_number)
# Still undeterminable: a case is present but we cannot scope the
# search to its domain. This is a data anomaly — BLOCK rather than
# silently running a cross-domain search for a specific case.
if not practice_area:
return (
f"שגיאה: לא ניתן לקבוע את התחום המשפטי (practice_area) של תיק "
f"{case_number}. לתיק אין practice_area מוגדר ולא ניתן להסיק אותו "
f"ממספר התיק. זוהי אנומליית נתונים — נא להגדיר את ה-practice_area "
f"של התיק (למשל דרך case_update) לפני הרצת חיפוש מסונן לתיק זה."
)
if not practice_area:
logger.warning(
"search_decisions called without practice_area filter — "