"""BUG-1802: the high-rigor API must never raise the engine's can_apply=false to true. 原值:``_compute_active_rectification_events`` 用 technique_contract 的 confirmation_allowed 直接覆盖 can_apply; 外部验证全部通过时,引擎自己判 can_apply=false(低置信、近邻 / 去一事件未过、领先只是暂列)也会变成 true。 新值:两者同时为真才可应用;引擎不能应用、置信度 low、有缺层或 leader_status 不是 comparable 时一律 false, contract 同步改为 continue_rectification 并记 local_engine_cannot_apply。 原因:占星师 2026-11-08 验收清单 §9 / §10(收到 winning_segment 不能忽略 can_apply、缺层与低置信)。 """ from __future__ import annotations import sys from pathlib import Path import pytest from tests.conftest import FIXTURE_BIRTH_DATE, FIXTURE_BIRTH_LAT, FIXTURE_BIRTH_LON ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) import jyotish_api_server as api_server # noqa: E402 from jyotish_api_server import JyotishComputeService # noqa: E402 EVENTS = [ {"id": "5cb071d6-6d99-46be-85dc-a9bf59ef6ac5", "domain": "education", "date": "2011-09", "precision": "month"}, {"id": "0790866c-ad5e-4a45-b2b4-a5c73f6be6ea", "domain": "career", "date": "2019-07-01", "precision": "day"}, {"id": "0ef52e51-ab5f-453b-81e5-adb44a929224", "domain": "relationship", "date": "2021", "precision": "year"}, {"id": "300c1c47-c223-4b40-8e27-47b3f6902795", "domain": "finance", "date": "2022-06", "precision": "month"}, ] def _local_result(request: dict, **overrides) -> dict: return { "result_id": "local-result", "confidence": "high", "can_apply": True, "winning_segment": {"start_time": "14:30", "end_time": "14:30", "representative_time": "14:30", "width_minutes": 1}, "event_count": len(request["events"]), "domain_count": len({event["domain"] for event in request["events"]}), "top_score": 30, "second_score": 20, "margin_percent": 33.33, "reasons": [], "evidence": [], "algorithm_version": "fixture", "canonical_input_hash": "canonical-fixture", "calculation_contract": {"events": request["events"]}, "stability_diagnostics": {"neighbor_stability": {"all_required_passed": True}, "leave_one_event_out": {"status": "pass"}}, "missing_layers": [], "leader_status": "comparable", "candidate_ranking_summary": [ {"rank": 1, "time": "14:30", "score": 30, "tied_minute_count": 1}, {"rank": 2, "time": "14:31", "score": 20, "tied_minute_count": 1}, ], **overrides, } class _VedAstro: @staticmethod def run_rectification_minute_snapshot_for_case(case, case_id="user_chart"): minute = case["minute"] layer = lambda name: {"status": "ok", "fingerprint": f"{name}-{minute}", "houses": {"House1": {}}, "planets": {}} # noqa: E731 return { "available": True, "status": "ok", "source": "vedastro_official", "layers": { "ascendant_house_boundaries": {**layer("asc"), "ascendant": {"sign": "Leo", "degree_in_sign": minute / 10}}, "D9": layer("d9"), "D10": layer("d10"), "dasha_boundaries": {"status": "ok", "fingerprint": f"dasha-{minute}", "boundary_count": 3}, "kp_cusp_sub_lord": {"status": "unsupported_by_verified_official_interface", "reason": "not supported"}, }, } @staticmethod def run_range_scan_for_case(case, _domain, _start, _end, case_id="user_chart"): return {"available": True, "status": "ok", "event_count": 1, "top_event": {"event_id": f"event-{case_id}"}, "evidence_ledger": [{"signal_lift": 1}]} def _run(monkeypatch, **overrides) -> dict: original_loader = api_server._load_local_module class LocalScorer: @staticmethod def score_life_events(request): return _local_result(request, **overrides) monkeypatch.setattr(api_server, "_load_local_module", lambda name: ( LocalScorer if name == "active_rectification_events" else _VedAstro if name == "vedastro_service_adapter" else original_loader(name))) monkeypatch.setattr("scripts.rectification_three_engine_packet.build_packet", lambda _case: { "engine_status": {"local": "ok", "pyjhora": "ok", "jyotishganit": "ok"}, "match_count": 3, "mismatch_count": 0}) monkeypatch.setattr(JyotishComputeService, "_compute_vedastro_gateway_run", lambda *_a, **_k: { "status": "ok", "official_closure_state": "official_verified", "official_closure_reason": "official_raw_response_present"}) return JyotishComputeService()._compute_active_rectification_events({ "birth_date": FIXTURE_BIRTH_DATE, "start_time": "14:29", "end_time": "14:31", "lat": FIXTURE_BIRTH_LAT, "lon": FIXTURE_BIRTH_LON, "tz": 8, "high_rigor": True, "events": EVENTS, }) def test_external_pass_keeps_an_applicable_engine_result_applicable(monkeypatch) -> None: result = _run(monkeypatch) assert result["technique_contract"]["confirmation_allowed"] is True assert result["can_apply"] is True @pytest.mark.parametrize("overrides", [ {"can_apply": False, "confidence": "low", "reasons": ["leave_one_event_out_not_passed"], "stability_diagnostics": {"neighbor_stability": {"all_required_passed": True}, "leave_one_event_out": {"status": "fail"}}}, {"can_apply": False, "confidence": "medium", "reasons": ["neighbor_stability_not_passed"]}, {"confidence": "low"}, {"leader_status": "provisional_unequal_coverage"}, {"missing_layers": ["Narayana_MD_AD"]}, ], ids=["engine_false_low", "engine_false_medium", "low_confidence", "provisional_leader", "missing_layers"]) def test_external_pass_cannot_override_an_engine_that_cannot_apply(monkeypatch, overrides) -> None: result = _run(monkeypatch, **overrides) contract = result["technique_contract"] assert result["can_apply"] is False assert contract["confirmation_allowed"] is False assert contract["decision"] == "continue_rectification" assert "local_engine_cannot_apply" in contract["hard_blockers"] assert "local_engine_cannot_apply" in result["reasons"]