"""Chronology-aware amendment resolver isolating targeted rule changes from surviving rules.""" from typing import Dict, List from src.schemas import PassageRecord, RetrievedPassage def resolve_amendments(top_passages: List[RetrievedPassage]) -> List[str]: """Produce explicit amendment resolution instructions from retrieved passage metadata.""" by_doc: Dict[str, List[RetrievedPassage]] = {} for rp in top_passages: by_doc.setdefault(rp.passage.doc_id, []).append(rp) notes: List[str] = [] for rp in sorted(top_passages, key=lambda x: x.passage.date): p = rp.passage if p.amends_doc_id: target_present = p.amends_doc_id in by_doc target_note = ( f" Notice {p.amends_doc_id} is also present in the retrieved evidence." if target_present else "" ) notes.append( f"DATED AMENDMENT ACTIVE: {p.passage_id} ({p.doc_id}, dated {p.date}, v{p.version}) " f"amends {p.amends_doc_id}. Scope: {p.amends_scope} " f"Apply {p.passage_id} ONLY to the changed rule; all unamended rules in {p.amends_doc_id} stand.{target_note}" ) return notes def extract_supersession_map(passages: List[PassageRecord]) -> Dict[str, List[Dict[str, str]]]: """Return mapping from base doc_id -> list of amending passage metadata sorted by date.""" mapping: Dict[str, List[Dict[str, str]]] = {} for p in sorted(passages, key=lambda x: x.date): if p.amends_doc_id: mapping.setdefault(p.amends_doc_id, []).append( { "amending_passage_id": p.passage_id, "amending_doc_id": p.doc_id, "date": p.date, "version": p.version, "scope": p.amends_scope or "", } ) return mapping