"""Regression tests for the server-owned purpose/candidate binding.""" from copy import deepcopy from datetime import datetime, timedelta, timezone import pytest from scripts.rectification_purpose_gate import BINDING_KEYS from scripts.rectification_purpose_gate.scope import build_scope_request, collect_scope_outputs EVALUATED = "2026-11-16T00:00:00Z" QUESTION = { "question": "Synthetic timing scope", "domains": ["事业"], "granularity": "month", "precision_policy_version": "purpose_policy_v1", "forecast_start": "2026-09-01T00:00:00+08:00", "forecast_end": "2026-12-01T00:00:00+08:00", } PROFILE = {"lat": 57.0, "lon": 0, "tz": 0, "ayanamsa": "raman", "node_mode": "mean"} RANGE = {"start": "2000-01-01T03:00:00Z", "end": "2000-01-01T03:20:00Z", "source": "synthetic_declaration"} def build(question=None, original=None, ranges=None, profiles=None, evaluated=EVALUATED): return build_scope_request( QUESTION if question is None else question, {"input_revision": "synthetic-v1"} if original is None else original, [RANGE] if ranges is None else ranges, {"engine_version": "fixture-native-v1", "branches": {"s": PROFILE}} if profiles is None else profiles, evaluated, ) @pytest.mark.parametrize(("key", "overrides"), [ ("purpose_fingerprint", {"question": {**QUESTION, "domains": ["marriage"]}}), ("forecast_window_fingerprint", {"question": {**QUESTION, "forecast_end": "2026-11-01T00:00:00+08:00"}}), ("forecast_window_fingerprint", {"evaluated": "2026-11-17T00:00:00Z"}), ("input_revision", {"original": {"input_revision": "synthetic-v2"}}), ("profile_fingerprint", {"profiles": {"engine_version": "fixture-native-v1", "branches": {"s": {**PROFILE, "node_mode": "true"}}}}), ("engine_version", {"profiles": {"engine_version": "fixture-native-v2", "branches": {"s": PROFILE}}}), ("precision_policy_version", {"question": {**QUESTION, "precision_policy_version": "purpose_policy_future"}}), ("candidate_set_fingerprint", {"ranges": [{**RANGE, "end": "2000-01-01T03:10:00Z"}]}), ]) def test_binding_changes_when_a_bound_input_changes(key, overrides): first = build() assert tuple(first["binding"]) == BINDING_KEYS assert first == build() candidate = build(**deepcopy(overrides)) assert first["binding"][key] != candidate["binding"][key] def _row(scope, offset, branch="s", **outputs): anchor = datetime(2000, 1, 1, 3, tzinfo=timezone.utc) return {"candidate_at": (anchor + timedelta(seconds=offset)).isoformat(), "branch_id": branch, "profile": scope["profiles"][branch], "outputs": outputs} def test_native_receipts_keep_missing_layer_blocked_and_never_complete_partial_range(): scope = build() result = collect_scope_outputs(scope, [ _row(scope, at, D10={"status": "executed", "value": 0, "method_id": "synthetic", "result_ref": "synthetic-calc", "dependencies": ["ascendant"]}) for at in (0, 60) ]) assert result["domain_complete"] is False assert result["historical_evidence_state"] == "not_evaluated" assert result["layer_coverage"]["D10"] == "partial" assert result["layer_coverage"]["Narayana"] == "blocked" receipt = result["candidates"][0] assert receipt["branch_id"] == "s" and receipt["profile"] == PROFILE assert receipt["outputs"]["D10"]["value"] == 0 assert receipt["outputs"]["D10"]["dependencies"] == ["ascendant"] assert receipt["outputs"]["Narayana"]["value"] is None def test_native_candidates_are_bound_to_known_branch_profile_and_remaining_set(): scope = build() outside = _row(scope, 1201) with pytest.raises(ValueError, match="outside_scope"): collect_scope_outputs(scope, [outside]) wrong_profile = {**_row(scope, 0), "profile": {**PROFILE, "node_mode": "true"}} with pytest.raises(ValueError, match="profile_binding_mismatch"): collect_scope_outputs(scope, [wrong_profile]) with pytest.raises(ValueError, match="duplicate"): collect_scope_outputs(scope, [_row(scope, 0), _row(scope, 0)]) def test_all_points_across_two_noncontiguous_ranges_can_complete_without_history(): scope = build(ranges=[ {**RANGE, "end": "2000-01-01T03:00:01Z"}, {**RANGE, "start": "2000-01-01T03:01:00Z", "end": "2000-01-01T03:01:00Z"}, ]) assert len(scope["plausible_ranges"]) == 2 assert scope["plausible_ranges"][0]["end"] < scope["plausible_ranges"][1]["start"] assert collect_scope_outputs(scope, [_row(scope, 0), _row(scope, 1)])["domain_complete"] is False result = collect_scope_outputs(scope, [_row(scope, 0), _row(scope, 1), _row(scope, 60)]) assert result["domain_complete"] is True assert result["historical_evidence_state"] == "not_evaluated" assert collect_scope_outputs(scope, [])["layer_coverage"]["D1"] == "blocked" def test_unknown_domain_or_policy_exposes_explicit_blocker(): unknown = build(question={**QUESTION, "domains": ["unsupported"]}) assert unknown["policy"]["required_layers"] == ["purpose_policy"] assert unknown["policy"]["missing_domains"] == ["unsupported"] assert collect_scope_outputs(unknown, [_row(unknown, 0)])["layer_coverage"] == {"purpose_policy": "blocked"} def test_required_layers_reuse_machine_readable_route_and_preserve_untimed_boundary(): timed = build(question={**QUESTION, "domains": ["wealth"]}) assert {"D1", "D2", "D11", "Vimshottari", "Narayana", "Ashtakavarga"} <= set(timed["policy"]["required_layers"]) structure = build(question={**QUESTION, "granularity": "structure"}) assert set(structure["policy"]["required_layers"]) >= {"D1", "D10", "A10"} assert "Narayana" not in structure["policy"]["required_layers"] assert structure["purpose_scope"]["forecast_window"] is None