"""Per-claim evidence-grade caps for the report reading (`report_evidence_grade_v1`). Astrologer 2026-11-12 (her repo 8aaf6099, handoff/2026-11-12/preset_q3 §1.3–1.4 and §5.2): the backend fixes each conclusion's required dependencies by route and claim type, computes a grade ceiling from a six-axis evidence ledger, and the model may only write at or below it. Grades are not probabilities. | Grade | Meaning in this contract | |---|---| | A | strongly and independently validated on a defined case set (never reached here) | | B | matching cases plus several independent methods agree (never reached here) | | C | sourced and computable, but not calibrated on matching cases | | D | a dependency the conclusion needs is missing, undecidable or in open conflict | No conclusion in a report has matching external cases (event calibration is `not_done` everywhere), so the ceiling is C at best. A conclusion falls to D when a *required* layer of its route is missing, failed, withheld, or sits within the birth-time tolerance of an ascendant change in a varga it needs. A missing layer that only the route's checklist names (``checked``) is recorded in the ledger and must be disclosed; it does not by itself make the conclusion D, because an unrelated missing technique must not drag valid conclusions down (§5.2). Dependencies follow her `references/strict-workflow-router.md` §2–§7 mandatory modules. The split into ``required`` and ``checked`` is the product's v1 reading of that list and is listed for her review. """ from __future__ import annotations import re from typing import Any from .birth_precision_window import CONFIRMED_TOLERANCE_MINUTES, WINDOW_POLICY, crosses_birth_window, report_birth_window try: from scripts.consult_card_domain_facts import PRECISION_VARGA except ModuleNotFoundError: # pragma: no cover - direct scripts/ execution path from consult_card_domain_facts import PRECISION_VARGA GRADE_CONTRACT = "report_evidence_grade_v1" GRADE_ORDER = ("A", "B", "C", "D") # Keep the minute-confirmed policy and ledger shape for existing reports. PRECISION_POLICY = "birth_record_minute_v1" MISSING_STATES = {"blocked", "withheld", "missing", "calculation_failed", "missing_input", "not_computed"} # Dependency kinds: # ("p", domain, id) domain participation row # ("s", id) shared technique row # ("v", code) varga ascendant boundary relative to the birth-time tolerance # ("t", key) timing / transit / annual block present # Each claim type: route (her router section), section ids it may appear in, # required deps (missing -> D) and checked deps (missing -> disclosed only). CLAIM_TYPES: dict[str, dict[str, Any]] = { "character_structure": { "route": "full-reading-strict §2", "sections": ("overview", "character"), "required": [("v", "D1"), ("s", "functional")], "checked": [("s", "d9"), ("v", "D9"), ("s", "shadbala"), ("s", "avastha"), ("s", "dispositor_chain")], }, "career_promise": { "route": "career-timing-strict §3", "sections": ("overview", "career"), "required": [("v", "D1"), ("s", "functional"), ("p", "career", "D10"), ("v", "D10")], "checked": [("s", "d9"), ("p", "career", "AmK"), ("p", "career", "AK"), ("p", "career", "Karakamsha"), ("p", "career", "A10"), ("p", "career", "AL"), ("s", "shadbala"), ("s", "ashtakavarga"), ("s", "dispositor_chain"), ("p", "career", "D1_D9_D10"), ("s", "argala")], }, "wealth_earning": { "route": "finance-timing-strict §5", "sections": ("overview", "wealth"), "required": [("v", "D1"), ("s", "functional"), ("p", "wealth", "D2"), ("v", "D2")], "checked": [("p", "wealth", "D11"), ("p", "wealth", "A2"), ("p", "wealth", "A11"), ("p", "wealth", "D10"), ("p", "wealth", "A10"), ("s", "shadbala"), ("s", "ashtakavarga"), ("s", "argala")], }, "wealth_retention": { "route": "finance-timing-strict §5", "sections": ("overview", "wealth"), "required": [("v", "D1"), ("s", "functional"), ("p", "wealth", "D2"), ("v", "D2")], "checked": [("p", "wealth", "A2"), ("s", "shadbala"), ("s", "ashtakavarga"), ("s", "argala")], }, "relationship_structure": { "route": "relationship-timing-strict §4", "sections": ("overview", "love"), "required": [("v", "D1"), ("s", "functional"), ("p", "marriage", "D9_7"), ("v", "D9")], "checked": [("p", "marriage", "UL"), ("p", "marriage", "A7"), ("p", "marriage", "DK"), ("p", "marriage", "Vivah"), ("s", "shadbala")], }, "health_pressure": { "route": "health-timing-strict §7", "sections": ("overview", "health"), "required": [("v", "D1"), ("s", "functional"), ("p", "health", "D6"), ("v", "D6")], "checked": [("p", "health", "D8"), ("v", "D8"), ("p", "health", "D30"), ("v", "D30"), ("p", "health", "drekkana22"), ("s", "shadbala")], }, "health_obstruction": { "route": "health-timing-strict §7 + badhaka-obstacle-planet-guide.md §二", "sections": ("health",), "required": [("v", "D1"), ("p", "health", "Badhaka")], "checked": [], }, "timing_window": { "route": "event-timing-strict §6", "sections": ("overview", "career", "wealth", "love", "health", "now"), "required": [("s", "vimshottari"), ("s", "narayana"), ("t", "transits")], "checked": [("t", "annual"), ("t", "double_transit"), ("s", "yogini"), ("s", "kp")], }, } GRADED_SECTIONS = ("character", "career", "wealth", "love", "health", "now") DEPENDENCY_ALIASES = { "s:functional": ("功能吉凶",), "s:d9": ("九分盘尊贵度", "九分盘", "Navamsha", "D9"), "s:shadbala": ("Shadbala", "六力"), "s:avastha": ("Avastha", "星曜状态"), "s:dispositor_chain": ("定位星链",), "s:ashtakavarga": ("Ashtakavarga", "八分法", "SAV"), "s:argala": ("Argala",), "s:vimshottari": ("Vimshottari", "大运", "小运", "次小运"), "s:narayana": ("Narayana", "纳拉亚纳"), "s:yogini": ("Yogini",), "s:kp": ("KP", "KP 判宫"), "t:transits": ("过境", "行运"), "t:annual": ("年盘",), "t:double_transit": ("Double Transit", "双行运", "双重行运", "双星过境"), "p:career:AmK": ("AmK",), "p:career:AK": ("AK",), "p:career:Karakamsha": ("Karakamsha",), "p:career:A10": ("A10",), "p:career:AL": ("AL",), "p:wealth:A2": ("A2",), "p:wealth:A11": ("A11",), "p:wealth:A10": ("A10",), "p:marriage:D9_7": ("九分盘第七宫", "九分盘", "Navamsha", "D9"), "p:marriage:UL": ("UL",), "p:marriage:A7": ("A7",), "p:marriage:DK": ("DK",), "p:marriage:Vivah": ("Vivah",), "p:health:drekkana22": ("第22个Drekkana", "第22个三分盘"), "p:health:Badhaka": ("Badhaka", "阻碍宫", "阻碍宫主"), "v:D1": ("D1", "本命盘"), "v:D2": ("D2", "财富分盘"), "v:D6": ("D6", "健康分盘"), "v:D9": ("D9", "九分盘", "Navamsha"), "v:D10": ("D10", "事业分盘"), } _VARGA_CODE = re.compile(r"D(?:[1-9]|[1-5]\d|60)\Z") def _dep_id(dep: tuple) -> str: return ":".join(str(part) for part in dep) def _participation(facts: dict, domain: str, item_id: str) -> str: rows = ((facts.get("domains") or {}).get(domain) or {}).get("participation") or [] for row in rows: if isinstance(row, dict) and row.get("id") == item_id: return str(row.get("state") or "missing") return "missing" def _shared(facts: dict, item_id: str) -> str: for row in facts.get("shared_techniques") or []: if isinstance(row, dict) and row.get("id") == item_id: return str(row.get("state") or "missing") return "missing" def _varga_boundary(facts: dict, code: str) -> tuple[str, dict[str, Any]]: precision = facts.get("birth_precision") or {} margin = (precision.get("ascendant_margins") or {}).get(code) if not facts.get("minute_confirmed"): window = report_birth_window(precision.get("window"), minute_confirmed=False) detail = {"policy": WINDOW_POLICY, **window} if code in PRECISION_VARGA: return "not_evaluated", {**detail, "reason": "birth time not confirmed to the minute"} if not isinstance(margin, dict) or any(margin.get(side) is None for side in ("before_minutes", "after_minutes")): return "missing", {**detail, "reason": "no ascendant margin for this varga"} before = float(margin["before_minutes"]) after = float(margin["after_minutes"]) detail.update(before_change_minutes=before, after_change_minutes=after, nearest_change_minutes=min(before, after)) crosses = crosses_birth_window(before, after, window) return ("boundary_sensitive" if crosses else "clear_at_tolerance"), detail if not isinstance(margin, dict): return "missing", {"policy": PRECISION_POLICY, "reason": "no ascendant margin for this varga"} nearest = min(float(margin.get("before_minutes", 0)), float(margin.get("after_minutes", 0))) detail = {"policy": PRECISION_POLICY, "nearest_change_minutes": nearest, "tolerance_minutes": CONFIRMED_TOLERANCE_MINUTES} return ("clear_at_tolerance" if nearest > CONFIRMED_TOLERANCE_MINUTES else "boundary_sensitive"), detail def _block_state(facts: dict, key: str) -> str: if key == "transits": return "executed" if (facts.get("transits") or {}).get("planets") else "missing" if key == "annual": annual = facts.get("annual") or {} return "missing" if annual.get("status") == "blocked" or not annual else "executed" if key == "double_transit": double = (facts.get("transits") or {}).get("double_transit") or {} return "executed" if double.get("checked_houses") else "missing" return "missing" def _parameter_sensitive(facts: dict) -> set[str]: listed = (facts.get("blocked_states") or {}).get("parameter_sensitive") or [] out = {str(item) for item in listed} narayana_status = ((facts.get("timing") or {}).get("narayana") or {}).get("status") if narayana_status in {"partial", "partial_verified", "parameter_sensitive", "conditional"}: out.add("narayana") return out def _evaluate(facts: dict, dep: tuple, sensitive: set[str]) -> dict[str, Any]: kind = dep[0] detail: dict[str, Any] = {} if kind == "p": state = _participation(facts, dep[1], dep[2]) elif kind == "s": state = _shared(facts, dep[1]) if dep[1] in sensitive and state == "executed": state = "parameter_sensitive" elif kind == "v": state, detail = _varga_boundary(facts, dep[1]) else: state = _block_state(facts, dep[1]) return {"id": _dep_id(dep), "state": state, **({"detail": detail} if detail else {})} def _is_missing(row: dict) -> bool: return row["state"] in MISSING_STATES or row["state"] == "boundary_sensitive" def _contradictions(facts: dict) -> list[dict[str, Any]]: functional = facts.get("functional") or {} return [row for row in functional.get("natural_vs_functional_conflicts") or [] if isinstance(row, dict)] def claim_grade(facts: dict, claim_type: str) -> dict[str, Any]: spec = CLAIM_TYPES[claim_type] sensitive = _parameter_sensitive(facts) required = [_evaluate(facts, dep, sensitive) for dep in spec["required"]] checked = [_evaluate(facts, dep, sensitive) for dep in spec["checked"]] missing_required = [row["id"] for row in required if _is_missing(row)] must_disclose = [row["id"] for row in checked if _is_missing(row)] sensitive_rows = [row["id"] for row in required + checked if row["state"] == "parameter_sensitive"] boundary_rows = [] for row in required + checked: if row["id"].startswith("v:"): details = {key: value for key, value in row["detail"].items() if key != "policy"} state = "clear" if row["state"] == "clear_at_tolerance" else row["state"] boundary_rows.append({"id": row["id"], "state": state, **details}) precision = facts.get("birth_precision") or {} purpose = precision.get("purpose_stability") or "not_evaluated" if claim_type == "timing_window" and purpose == "stable": purpose = "not_evaluated" if purpose != "stable": must_disclose.append("purpose_stability_not_evaluated") cap = "D" if missing_required else "C" reasons = ( [f"required dependency unavailable: {item}" for item in missing_required] or ["no matching external cases: event calibration not done"] ) return { "route": spec["route"], "sections": list(spec["sections"]), "cap": cap, "reasons": reasons, "required": required, "checked": checked, "must_disclose": must_disclose, "parameter_sensitive": sensitive_rows, # Six axes (§5.2) plus the local boundary check. "ledger": { "case_fit": "none", "method_maturity": "source_bounded" if not missing_required else "dependency_missing", "independent_agreement": "not_assessed", "purpose_stability": purpose, "boundary_check": {"policy": _precision_policy(facts), "rows": boundary_rows}, "external_calculation_check": "not_done", "contradiction": [f"{row.get('planet')}: natural {row.get('natural')} / functional {row.get('functional')}" for row in _contradictions(facts)], }, } def _precision_policy(facts: dict) -> str: return PRECISION_POLICY if facts.get("minute_confirmed") else WINDOW_POLICY def _dependency_catalog(facts: dict) -> dict[str, dict[str, Any]]: deps = {dep for spec in CLAIM_TYPES.values() for field in ("required", "checked") for dep in spec[field]} for domain, row in (facts.get("domains") or {}).items(): if not isinstance(row, dict) or not isinstance(charts := row.get("varga_charts"), dict): continue for code, chart in charts.items(): if isinstance(code, str) and _VARGA_CODE.fullmatch(code) and isinstance(chart, dict): deps.add(("v", code)) if _participation(facts, domain, code) != "missing": deps.add(("p", domain, code)) sensitive = _parameter_sensitive(facts) catalog = {} for dep in sorted(deps): row = _evaluate(facts, dep, sensitive) code = dep[-1] varga_alias = (code,) if dep[0] == "v" or (dep[0] == "p" and code != "D1" and _VARGA_CODE.fullmatch(code)) else () aliases = DEPENDENCY_ALIASES.get(row["id"], varga_alias) catalog[row["id"]] = {"state": row["state"], "aliases": list(aliases)} return catalog def evidence_grades(facts: dict) -> dict[str, Any]: """The per-claim ceilings the reading must respect.""" return { "contract": GRADE_CONTRACT, "precision_policy": _precision_policy(facts), "grade_order": list(GRADE_ORDER), "graded_sections": list(GRADED_SECTIONS), "claims": {name: claim_grade(facts, name) for name in CLAIM_TYPES}, "dependency_catalog": _dependency_catalog(facts), } def grade_allowed(grade: str, cap: str) -> bool: """A grade may equal its ceiling or be weaker (later in GRADE_ORDER).""" return GRADE_ORDER.index(grade) >= GRADE_ORDER.index(cap)