From 8f83eb2c25e600dde38f7c58ddacd0bdc5ce4ebd Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 16:26:25 +0800 Subject: [PATCH] fix(report): Kalachakra uses the classical sign periods from the Moon longitude (BUG-1656) The full reading and the dasha registry called the Kalachakra module without the Moon longitude, so BUG-1487's PVR ch.24 sign periods never ran in the product. The report printed the legacy approximation (planet lords, 7..18 years) under a "simplified model" label. Both callers now pass the Moon longitude. The dasha family carries method_profile. Classical results keep their own AD/PD rows instead of the rotated simplified table, and the reader and research tables list signs. The PyJHora comparison was rerun: majors now agree, sub-period division still differs (documented). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DbBXoyeZrYn6ryNcSe5Y2Y --- docs/BUG_HISTORY.md | 16 +++ .../research-timing-pyjhora-20261023.json | 6 +- scripts/annual_native/dasha_children.py | 24 +++++ scripts/dasha_registry.py | 5 +- scripts/jyotish_engine.py | 9 +- scripts/pl9_reader_export.py | 26 ++++- .../research_timing_pyjhora_compare.py | 7 +- scripts/research_timing/render.py | 16 +++ .../test_kalachakra_report_classical_1656.py | 97 +++++++++++++++++++ 9 files changed, 197 insertions(+), 9 deletions(-) create mode 100644 tests/test_kalachakra_report_classical_1656.py diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index 852d3a77e..431406356 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -22711,3 +22711,19 @@ - 相关记录:BUG-1652、BUG-1650。 - 复发自:BUG-1652(同一移植的后续修正) - 修复版本:分支 `fix/tajika-sync-1655`。 + +## BUG-1656 | 完整数据版报告的 Kala Chakra 大运一直是旧近似表(行星主星、7、8、9…年递增),不是按月亮经度起算的星座期 + +- 状态:fixed-pending-verify(代码 + 回归测试;staging 导出待复核) +- 首次发现 / 最近更新:2026-11-06(产品负责人对比上游导出的报告与本站导出:Kala Chakra 表的主星是行星、年数逐段加 1,上游是星座期) +- 影响面:`scripts/jyotish_engine.py`(完整解读调用 Kalachakra 时传月亮经度;大运家族带上 `method_profile`)、`scripts/dasha_registry.py`(同上)、`scripts/annual_native/dasha_children.py`(古典结果保留自带的小运与细运,不再用旋转简化表覆盖)、`scripts/pl9_reader_export.py`(标题与说明按 `method_profile`,表里写星座)、`scripts/research_timing/render.py`(次小运读古典结果)、`scripts/research/research_timing_pyjhora_compare.py` 与 `docs/tasks/research-timing-pyjhora-20261023.json`(对照本机一侧改走古典路径后重跑)。 +- 现象:「标准大运表格页」的 Kala Chakra 标题带「(简化模型)」,主星为南交点、金星、太阳……年数 7 至 18 逐段加 1;附表「Kalachakra 大运」与研究附表的次小运同样来自这张表;研究附表注明「与 PyJHora 不一致:差在 起点」。 +- 触发条件:所有完整数据版导出与咨询里读 `modules.kalachakra_dasha` 的地方。 +- 根因:BUG-1487 只在调用方传入 `moon_longitude` 时才走古典九星座期,否则保留旧近似表。完整解读(`cmd_full_reading`)和大运登记表都没传月亮经度,所以产品一直走旧表;报告渲染又对任何结果都套旋转简化子运,并无条件加「简化模型」标题。 +- 修复:两个调用方传月亮经度;家族带 `method_profile`;古典结果保留 PVR 第 24 章的小运和递推细运,并给子行补父星座;报告表格按星座列出,说明改为「按本命月亮实际经度起算星座期(PVR 第 24 章)……对参数敏感」。没有月亮经度的旧路径仍有标注为简化的子运。 +- 验证:新增 `tests/test_kalachakra_report_classical_1656.py` 5 条(完整解读走古典路径、九段星座与年数等于 PyJHora 4.8.7 同盘 Raman 结果、家族保留古典小运、报告表无「简化模型」且列星座、研究次小运读古典行;旧路径仍有简化子运)。撤掉修复后其中 4 条失败。含 Kalachakra 的 12 个测试文件与 `test_research_timing.py`、`test_full_data_parity_chapters.py`、`test_report_full_data_fix.py` 通过;`test_report_english_dictionary.py::test_every_han_string_has_an_english_sibling` 在未改动的基线上同样失败(瑜伽规则 `gajakesari` 缺英文),与本单无关。PyJHora 对照重跑:Kalachakra 由「起点」不一致变为「子运划分」不一致,其余体系结果不变。 +- 已知差异:主运与 PyJHora 星座顺序、年数一致,日期差约 2 天(年长约定不同:本仓 365.25 天)。小运划分与 PyJHora 不同:PyJHora 从主运本星座起排,本仓按 BUG-1487 核对过书例的 PVR 第 24 章比例;细运是同一比例的递推,未经书例单独核对,报告里注明对参数敏感。 +- 防复发:回归测试锁住完整解读的 `method_profile` 与星座序列;以后新增 Kalachakra 调用方必须传月亮经度。 +- 关联记录:BUG-1487(古典星座期只在传月亮经度时生效,本单补上调用方)。 +- 复发自:BUG-1487 的遗留:当时测试只直接调用模块,没有覆盖完整解读与报告路径。 +- 修复版本:分支 `codex/report-parity-gaps-20261106`。 diff --git a/docs/tasks/research-timing-pyjhora-20261023.json b/docs/tasks/research-timing-pyjhora-20261023.json index 7a97074fe..d6c746b19 100644 --- a/docs/tasks/research-timing-pyjhora-20261023.json +++ b/docs/tasks/research-timing-pyjhora-20261023.json @@ -198,9 +198,9 @@ "heading_en": "Kalachakra", "heading_zh": "Kalachakra", "match": "exact", - "category": "起点", - "category_en": "start", - "example": "chart 0 period 1 native Taurus vs PyJHora Gemini" + "category": "子运划分", + "category_en": "sub-period division", + "example": "chart 0 third level under Gemini does not line up" } } } diff --git a/scripts/annual_native/dasha_children.py b/scripts/annual_native/dasha_children.py index 091ba913a..85d25ab2c 100644 --- a/scripts/annual_native/dasha_children.py +++ b/scripts/annual_native/dasha_children.py @@ -271,6 +271,26 @@ def _yogini_periods(module: dict, rows: list[dict]) -> list[dict]: return attached +CLASSICAL_KALACHAKRA_PROFILE = "pvr_classical_nine_sign_md_ad" + + +def _with_classical_parents(row: dict) -> dict: + """Name each classical child's parent sign so tables can show the chain.""" + if not isinstance(row, dict): + return row + for child in row.get("antardasha") or []: + if isinstance(child, dict): + child.setdefault("parent_lord", row.get("lord")) + child.setdefault("parent_rashi", row.get("rashi")) + child.setdefault("parent_sign", row.get("rashi")) + for grand in child.get("pratyantardasha") or []: + if isinstance(grand, dict): + grand.setdefault("parent_lord", child.get("lord")) + grand.setdefault("parent_rashi", child.get("rashi")) + grand.setdefault("parent_sign", child.get("rashi")) + return row + + def enrich_master_family(key: str, module: dict | None, periods: list | None) -> list[dict]: """Copy family rows and attach children that already have a sourced rule. @@ -288,6 +308,10 @@ def enrich_master_family(key: str, module: dict | None, periods: list | None) -> if key == "kala_chakra": if not rows: return rows + if module.get("method_profile") == CLASSICAL_KALACHAKRA_PROFILE: + # BUG-1656: the classical module already carries PVR ch.24 AD/PD. + # The rotated simplified table would overwrite them. + return [_with_classical_parents(row) for row in rows] try: enriched, _ad, _pd = attach_kalachakra_simplified_children( rows, diff --git a/scripts/dasha_registry.py b/scripts/dasha_registry.py index d9267f813..c6f7ca2f6 100644 --- a/scripts/dasha_registry.py +++ b/scripts/dasha_registry.py @@ -311,7 +311,10 @@ def compute_dasha( }) return {"periods": _major_rows(payload)} if key == "kalachakra": - payload = _load("kalachakra_dasha").calculate_kalachakra_dasha(alt) + payload = _load("kalachakra_dasha").calculate_kalachakra_dasha({ + **alt, + "moon_longitude": conditional["moon_longitude"], + }) return {"periods": _major_rows(payload)} if key == "shasti_hayani": birth_info = dict(conditional) diff --git a/scripts/jyotish_engine.py b/scripts/jyotish_engine.py index 17e1dc8d4..403655b7e 100644 --- a/scripts/jyotish_engine.py +++ b/scripts/jyotish_engine.py @@ -3034,6 +3034,8 @@ def _native_dasha_master_families(modules: dict) -> dict: ) if periods: family['periods'] = periods + if isinstance(module, dict) and module.get('method_profile'): + family['method_profile'] = module['method_profile'] families[key] = family return families @@ -16902,7 +16904,12 @@ def cmd_full_reading(args): try: from kalachakra_dasha import calculate_kalachakra_dasha - kalachakra_data = calculate_kalachakra_dasha(birth_info_for_alt_dasha) + # BUG-1656: without the Moon longitude the module falls back to the + # legacy approximation (planet lords, 7..18 years), not Kalachakra. + kalachakra_data = calculate_kalachakra_dasha({ + **birth_info_for_alt_dasha, + 'moon_longitude': moon_lon, + }) report['modules']['kalachakra_dasha'] = kalachakra_data except Exception as alt_e: kalachakra_data = {'error': str(alt_e)} diff --git a/scripts/pl9_reader_export.py b/scripts/pl9_reader_export.py index a6e860958..4b428530c 100644 --- a/scripts/pl9_reader_export.py +++ b/scripts/pl9_reader_export.py @@ -1955,10 +1955,22 @@ def render_pl9_parity_markdown(packet: dict) -> str: if not periods: continue heading = f'### p{page} {title_text}' - if 'Kala Chakra' in title_text: + kalachakra_classical = ( + 'Kala Chakra' in title_text + and family.get('method_profile') == 'pvr_classical_nine_sign_md_ad' + ) + if 'Kala Chakra' in title_text and not kalachakra_classical: heading += ' (Simplified model)' if _lang == 'en' else '(简化模型)' lines.extend([heading, '']) - if 'Kala Chakra' in title_text: + if kalachakra_classical: + # BUG-1656: sign periods from the natal Moon longitude. + lines.extend([ + '- Sign periods start from the natal Moon longitude (PVR ch. 24). Sub-periods follow the published proportions; the third level repeats the same rule and is parameter-sensitive.' + if _lang == 'en' + else '- 按本命月亮实际经度起算星座期(PVR 第 24 章)。小运按书中比例划分,细运用同一比例再分一层,结果对参数敏感。', + '', + ]) + elif 'Kala Chakra' in title_text: lines.extend([ '- Sub-periods use a simplified model. The major periods are unchanged.' if _lang == 'en' @@ -1980,6 +1992,9 @@ def render_pl9_parity_markdown(packet: dict) -> str: continue raw = _dict(period.get('raw_period')) lord = period.get('lord') or period.get('planet') or period.get('sign') or period.get('rashi') or raw.get('lord') or raw.get('planet') or raw.get('sign') or raw.get('rashi') + if kalachakra_classical and period.get('rashi'): + # A Kalachakra period belongs to a sign; its lord follows from it. + lord = period.get('rashi') lines.append( f"| {index} | {_cell(_token(lord))} | {_period_start(period) or _cell(raw.get('start_date') or raw.get('start'))} | " f"{_period_end(period) or _cell(raw.get('end_date') or raw.get('end'))} | {_period_years(period)} |" @@ -2036,6 +2051,9 @@ def render_pl9_parity_markdown(packet: dict) -> str: or child_raw.get('planet') or child_raw.get('sign') ) + if kalachakra_classical: + main_lord = child.get('parent_sign') or main_lord + sub_lord = child.get('rashi') or sub_lord lines.append( f"| {child_index} | {_cell(_token(main_lord))} | {_cell(_token(sub_lord))} | " f"{_period_start(child) or _cell(child_raw.get('start_date') or child_raw.get('start'))} | " @@ -2054,9 +2072,11 @@ def render_pl9_parity_markdown(packet: dict) -> str: ]) for pd_index, (child, grand) in enumerate(pd_rows, start=1): grand_lord = grand.get('lord') or grand.get('planet') or grand.get('rashi') or grand.get('sign') + if kalachakra_classical: + grand_lord = grand.get('rashi') or grand_lord lines.append( f"| {pd_index} | {_cell(_token(child.get('parent_sign') or child.get('parent_lord') or child.get('main')))} | " - f"{_cell(_token(child.get('lord') or child.get('rashi') or child.get('sign')))} | " + f"{_cell(_token((child.get('rashi') if kalachakra_classical else None) or child.get('lord') or child.get('rashi') or child.get('sign')))} | " f"{_cell(_token(grand_lord))} | {_period_start(grand)} | {_period_end(grand)} |" ) else: diff --git a/scripts/research/research_timing_pyjhora_compare.py b/scripts/research/research_timing_pyjhora_compare.py index c23ca5130..4058a7046 100644 --- a/scripts/research/research_timing_pyjhora_compare.py +++ b/scripts/research/research_timing_pyjhora_compare.py @@ -257,12 +257,17 @@ def collect_native(indexes: tuple[int, ...] = CHARTS) -> dict: # This calculator subtracts datetime.now(), which is naive. Keep the # clock time and drop the zone so the call can finish. birth_dt = epoch.replace(tzinfo=None) if isinstance(epoch, datetime) and epoch.tzinfo is not None else epoch + # BUG-1656: compare the classical path the product now uses. calculated = calculate_kalachakra_dasha({ "moon_nakshatra_index": nak, "moon_pada": pada, + "moon_longitude": moon_lon, "birth_datetime": birth_dt, }) - enriched, _children, _grands = attach_kalachakra_simplified_children(calculated.get("major") or []) + if calculated.get("method_profile") == "pvr_classical_nine_sign_md_ad": + enriched = calculated.get("major") or [] + else: + enriched, _children, _grands = attach_kalachakra_simplified_children(calculated.get("major") or []) systems["kalachakra"] = fingerprint_periods(enriched, kind="sign") except (ValueError, RuntimeError, OverflowError, KeyError, TypeError, OSError) as exc: systems["kalachakra"] = {"error": type(exc).__name__} diff --git a/scripts/research_timing/render.py b/scripts/research_timing/render.py index 9acc07298..95ca3bc72 100644 --- a/scripts/research_timing/render.py +++ b/scripts/research_timing/render.py @@ -260,6 +260,22 @@ def _kalachakra(packet: dict, language: str) -> str: else "次小运按简化模型,从现有主运按比例分出。主运和子运与原来的表相同。" ) module = _first(packet, ("timing_and_predictive_systems", "kalachakra_dasha")) + if isinstance(module, dict) and module.get("method_profile") == "pvr_classical_nine_sign_md_ad": + # BUG-1656: the classical module already has PVR ch.24 third-level rows. + classical_note = ( + "Sign periods start from the natal Moon longitude (PVR ch. 24). The third level divides each sub-period by the same published proportions." + if language == "en" + else "按本命月亮实际经度起算(PVR 第 24 章)。次小运把每个小运按书中同一比例再分一层。" + ) + shaped = [{ + "level": "PD", + # A Kalachakra period is named by its sign; the lord follows from it. + "sign": item.get("rashi") or item.get("sign"), + "start": item.get("start_date") or item.get("start"), + "end": item.get("end_date") or item.get("end"), + } for item in (module.get("pratyantardasha") or []) if isinstance(item, dict)] + if shaped: + return classical_note + "\n\n" + _table(shaped, language) rows = [item for item in _major_list(module) if isinstance(item, dict) and "antardasha" not in item and "subperiods" not in item] if not rows: rows = [item for item in _major_list(module) if isinstance(item, dict)] diff --git a/tests/test_kalachakra_report_classical_1656.py b/tests/test_kalachakra_report_classical_1656.py new file mode 100644 index 000000000..f1f4c725a --- /dev/null +++ b/tests/test_kalachakra_report_classical_1656.py @@ -0,0 +1,97 @@ +"""BUG-1656: the report must use the classical Kalachakra sign periods. + +BUG-1487 added the PVR ch.24 sign periods, but only when the caller passes the +Moon longitude. The full reading and the dasha registry did not pass it, so the +report printed the legacy approximation (planet lords, 7, 8, 9 ... years). +""" + +from __future__ import annotations + +from types import SimpleNamespace + +import pytest + +from scripts.annual_native.dasha_children import enrich_master_family +from scripts.jyotish_engine import _native_dasha_master_families, cmd_full_reading +from scripts.pl9_reader_export import render_pl9_parity_markdown +from scripts.research_timing.render import _kalachakra +from tests.test_report_longform_parity import _producer_packet + +# Fictional chart (Delhi, 1990-01-01 12:00, Raman, mean node). +CHART = { + "year": 1990, "month": 1, "day": 1, "hour": 12, "minute": 0, "second": 0, + "lat": 28.6139, "lon": 77.2090, "tz": 5.5, + "ayanamsa": "raman", "node_mode": "mean", + "today": "2026-11-06", "transit_date": "2026-11-06", "target_year": 2026, "age": 36, + "birth_time_accuracy": "confirmed", +} + +# PyJHora 4.8.7 `raasi.kalachakra.kalachakra_dhasa(moon, jd, dhasa_level_index=1)` +# for the same chart with RAMAN ayanamsa: (sign, years). The first row is the +# birth balance. Run on 2026-11-06. +PYJHORA_MAJORS = ( + ("Scorpio", 2.88), ("Libra", 16.0), ("Virgo", 9.0), ("Leo", 5.0), ("Cancer", 21.0), + ("Gemini", 9.0), ("Taurus", 16.0), ("Aries", 7.0), ("Sagittarius", 10.0), +) +CLASSICAL = "pvr_classical_nine_sign_md_ad" + + +@pytest.fixture(scope="module") +def modules() -> dict: + report = cmd_full_reading(SimpleNamespace(**CHART)) + return report["modules"] + + +def test_full_reading_uses_the_classical_sign_periods(modules: dict) -> None: + payload = modules["kalachakra_dasha"] + assert payload["method_profile"] == CLASSICAL + majors = payload["major"] + assert [row["rashi"] for row in majors[:9]] == [sign for sign, _ in PYJHORA_MAJORS] + assert round(float(majors[0]["years"]), 1) == pytest.approx(PYJHORA_MAJORS[0][1], abs=0.05) + assert [float(row["years"]) for row in majors[1:9]] == [years for _, years in PYJHORA_MAJORS[1:]] + + +def test_report_family_keeps_the_classical_children(modules: dict) -> None: + family = _native_dasha_master_families(modules)["kala_chakra"] + assert family["method_profile"] == CLASSICAL + first = family["periods"][0] + children = first["antardasha"] + assert children, "classical AD rows were dropped" + assert {child["derivation"] for child in children} == {"pvr_ch24_proportional_ad_birth_clipped"} + assert all(child.get("model") != "simplified" for child in children) + assert children[0]["parent_sign"] == first["rashi"] + + +def test_legacy_rows_still_get_the_labelled_simplified_children() -> None: + rows = [{"lord": "Ketu", "rashi": "Capricorn", "years": 7, + "start_date": "2000-01-01T00:00:00", "end_date": "2007-01-01T00:00:00"}] + enriched = enrich_master_family("kala_chakra", {"method_profile": "legacy_approximation_without_moon_longitude"}, rows) + assert enriched[0]["antardasha"][0]["model"] == "simplified" + + +def test_reader_table_names_signs_and_drops_the_simplified_label(modules: dict) -> None: + markdown = render_pl9_parity_markdown(_producer_packet({ + "timing_and_predictive_systems": { + "dasha": modules.get("dasha"), + "kalachakra_dasha": modules.get("kalachakra_dasha"), + "dasha_master_pack": { + "schema": "dasha_master_report_pack_v1", + "families": _native_dasha_master_families(modules), + }, + }, + })) + start = markdown.index("### Kala Chakra 大运") + section = markdown[start:start + 3000] + heading = markdown[start:markdown.index("\n", start)] + assert "简化模型" not in heading + assert "简化模型" not in section + assert "PVR 第 24 章" in section + first_row = next(line for line in section.splitlines() if line.startswith("| 1 |")) + assert "天蝎座" in first_row or "Scorpio" in first_row + + +def test_research_third_level_reads_the_classical_rows(modules: dict) -> None: + text = _kalachakra({"timing_and_predictive_systems": {"kalachakra_dasha": modules["kalachakra_dasha"]}}, "zh") + assert "简化模型" not in text + assert "PVR 第 24 章" in text + assert "| 次小运 |" in text