Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2U3BH1SxwAGyhhbe1
133 lines
5.7 KiB
Python
133 lines
5.7 KiB
Python
"""BUG-2154: archived yoga rule ids, their canonical entry, and the shared evidence identity.
|
|
|
|
Astrologer ruling 2026-11-22 §3 (D): ``kalatra_dosha_mars_1_4_7_8_12`` is the canonical entry for
|
|
the Mars 1/4/7/8/12 screen; ``kalatra_dosha_mars_7`` (same condition) no longer executes or counts
|
|
on its own and stays in ``references/yoga_rules.json`` as an archived alias (``enabled: false``,
|
|
``duplicate_of``). Old report packets that still carry the archived id resolve to the canonical
|
|
entry here, so one fact gives one page, one table row and one adverse item.
|
|
|
|
Evidence identity (shared by the yoga row and the dosha table row ``mangal_dosha``)::
|
|
|
|
ev:mars_marriage_screen@<chart>:<reference_point> e.g. ev:mars_marriage_screen@D1:lagna
|
|
|
|
The report dedup key is ``(dedup_key, reference_point, chart, source_profile)``, so a later Moon-
|
|
or Venus-based version, another chart, or another source profile is never merged into this one.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
RULES_PATH = Path(__file__).resolve().parents[2] / "references" / "yoga_rules.json"
|
|
|
|
# The dosha table key and the canonical yoga rule that read the same Mars fact.
|
|
MARS_MARRIAGE_RULE_ID = "kalatra_dosha_mars_1_4_7_8_12"
|
|
MARS_MARRIAGE_DOSHA_KEY = "mangal_dosha"
|
|
MARS_MARRIAGE_EVIDENCE = "mars_marriage_screen"
|
|
SHARED_EVIDENCE_GROUPS = {MARS_MARRIAGE_RULE_ID: MARS_MARRIAGE_EVIDENCE}
|
|
DEFAULT_REFERENCE_POINT = "lagna"
|
|
DEFAULT_CHART = "D1"
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def _rules() -> dict[str, dict]:
|
|
data = json.loads(RULES_PATH.read_text(encoding="utf-8"))
|
|
return {str(rule["id"]): rule for rule in data.get("rules") or [] if isinstance(rule, dict) and rule.get("id")}
|
|
|
|
|
|
def canonical_rule_id(rule_id: Any) -> str:
|
|
"""The canonical id for ``rule_id`` (itself when it is not an archived alias)."""
|
|
current = str(rule_id or "").strip()
|
|
seen = set()
|
|
while current and current not in seen:
|
|
seen.add(current)
|
|
target = str((_rules().get(current) or {}).get("duplicate_of") or "").strip()
|
|
if not target or target not in _rules():
|
|
return current
|
|
current = target
|
|
return current
|
|
|
|
|
|
def canonical_rule(rule_id: Any) -> dict | None:
|
|
return _rules().get(canonical_rule_id(rule_id))
|
|
|
|
|
|
def is_archived_alias(rule_id: Any) -> bool:
|
|
return canonical_rule_id(rule_id) != str(rule_id or "").strip()
|
|
|
|
|
|
def evidence_identity(rule_id: Any, chart: Any = None, reference_point: Any = None) -> str | None:
|
|
"""``ev:mars_marriage_screen@D1:lagna`` for the Mars screen; None for rules without a shared identity."""
|
|
canonical = canonical_rule_id(rule_id)
|
|
group = SHARED_EVIDENCE_GROUPS.get(canonical)
|
|
if not group:
|
|
return None
|
|
rule = _rules().get(canonical) or {}
|
|
point = str(reference_point or rule.get("reference_point") or DEFAULT_REFERENCE_POINT)
|
|
return f"ev:{group}@{chart or DEFAULT_CHART}:{point}"
|
|
|
|
|
|
def dosha_evidence_identity(dosha_key: str, chart: Any = None, reference_point: Any = None) -> str | None:
|
|
"""The same identity for the dosha table row that reads the same fact (``mangal_dosha`` only)."""
|
|
if dosha_key != MARS_MARRIAGE_DOSHA_KEY:
|
|
return None
|
|
return evidence_identity(MARS_MARRIAGE_RULE_ID, chart, reference_point)
|
|
|
|
|
|
def report_dedup_key(rule_id: Any, chart: Any = None) -> tuple | None:
|
|
"""``(dedup_key, reference_point, chart, source_profile)`` for a rule with a dedup group, else None."""
|
|
rule = canonical_rule(rule_id) or {}
|
|
group = rule.get("dedup_key")
|
|
if not group:
|
|
return None
|
|
return (
|
|
str(group),
|
|
str(rule.get("reference_point") or DEFAULT_REFERENCE_POINT),
|
|
str(chart or DEFAULT_CHART),
|
|
str(rule.get("source_profile") or ""),
|
|
)
|
|
|
|
|
|
def resolve_row(row: dict) -> dict:
|
|
"""A copy of an engine row with an archived id moved to its canonical entry (names, effects).
|
|
|
|
Rows of a canonical rule are returned unchanged except for the ``evidence_identity`` field.
|
|
The copy keeps ``legacy_rule_id`` so an old report's id stays traceable.
|
|
"""
|
|
if not isinstance(row, dict):
|
|
return row
|
|
rule_id = str(row.get("rule_id") or "").strip()
|
|
canonical = canonical_rule_id(rule_id) if rule_id else ""
|
|
out = dict(row)
|
|
if canonical and canonical != rule_id:
|
|
rule = _rules().get(canonical) or {}
|
|
out["legacy_rule_id"] = rule_id
|
|
out["rule_id"] = canonical
|
|
# BUG-2300: a row whose traditional effect the engine withheld keeps it withheld; the
|
|
# canonical rule's effects / strength are not copied back over the gate.
|
|
try:
|
|
from .yoga_effect_gate import effect_withheld
|
|
except ImportError: # pragma: no cover - scripts/report_render on sys.path
|
|
from yoga_effect_gate import effect_withheld
|
|
keys = ("name", "name_cn", "category") if effect_withheld(row) else (
|
|
"name", "name_cn", "effects", "effects_en", "strength", "strength_en", "category")
|
|
for key in keys:
|
|
if key in rule:
|
|
out[key] = rule[key]
|
|
evidence = out.get("condition_evidence")
|
|
if isinstance(evidence, dict) and evidence.get("rule_id") == rule_id:
|
|
out["condition_evidence"] = {**evidence, "rule_id": canonical}
|
|
identity = evidence_identity(out.get("rule_id"), out.get("chart") or out.get("chart_type"))
|
|
if identity:
|
|
out["evidence_identity"] = identity
|
|
return out
|
|
|
|
|
|
def reader_explanation(rule_id: Any, language: str) -> str:
|
|
"""The rule's own reader explanation (``reader_explanation`` / ``reader_explanation_en``), or ''."""
|
|
rule = canonical_rule(rule_id) or {}
|
|
return str(rule.get("reader_explanation_en" if language == "en" else "reader_explanation") or "").strip()
|