# SPDX-License-Identifier: AGPL-3.0-or-later
"""Verifier mutation tests.

For each verifier, the authoritative input it trusts is mutated — separately
the artifact and the identity/version binding — and the verifier must reject
the mutation. Leaving source unfixed, any failure here is a reported defect.

Verifiers covered:
- the model-output admission verifier (:func:`model_adapter.validate_output`);
- the trial classifier over oracle grades (:func:`oracle.classify_trial`);
- the replay-equivalence comparator (:func:`conformance.state_fingerprint` /
  :func:`conformance.first_difference`), used as the reference pattern.
"""
from __future__ import annotations

import dataclasses

import pytest

from orchestrator import events, model_adapter, oracle, reducer
from orchestrator.conformance import first_difference, state_fingerprint
from orchestrator.model import RunState
from orchestrator.model_adapter import ModelOutputRejected

from test_milestone2_conformance import _event, _validated_node_state

# -- model-output verifier --------------------------------------------------


def _event(event_type, payload, idem="k", source="controller"):
    return events.make_event(event_type, payload, idempotency_key=idem, source=source)


def _model_attempt_state(prompt_hash="sha256:prompt"):
    state = RunState(run_id="r1")
    state, _ = reducer.reduce(
        state,
        _event(
            "NODE_PROPOSED",
            {
                "node_id": "n1",
                "contract": {"budgets": {"MODEL_COST": 100}, "owned_scope": ["src/**"]},
                "cause": "ROOT",
            },
        ),
    )
    state, _ = reducer.reduce(
        state,
        _event(
            "ATTEMPT_PLANNED",
            {"attempt_id": "a1", "node_id": "n1", "cause": "INITIAL", "action_type": "REVIEW"},
        ),
    )
    state, _ = reducer.reduce(
        state, _event("ACTION_RESERVED", {"attempt_id": "a1", "commitment": 5}, source="scheduler")
    )
    state, _ = reducer.reduce(
        state, _event("ATTEMPT_COMMITMENT_OPENED", {"attempt_id": "a1", "commitment": 5})
    )
    state, _ = reducer.reduce(
        state,
        _event(
            "ACTION_DISPATCHED",
            {
                "attempt_id": "a1",
                "executor": "model-adapter",
                "action_type": "REVIEW",
                "model_id": "model-x",
                "prompt_hash": prompt_hash,
            },
        ),
    )
    state, _ = reducer.reduce(
        state, _event("DISPATCH_ACKNOWLEDGED", {"attempt_id": "a1", "token": "t"}, source="executor")
    )
    return state


def _state_with_artefact(artefact_hash="sha256:art-1"):
    state = _model_attempt_state()
    state, _ = reducer.reduce(
        state,
        _event(
            "ARTEFACT_ADMITTED",
            {
                "artefact_id": "art-1",
                "type": "GIT_CANDIDATE",
                "hash": artefact_hash,
                "attempt_id": "a1",
            },
            idem="art-1",
        ),
    )
    return state


def _valid_output(prompt_hash="sha256:prompt", **overrides):
    payload = {
        "output_id": "o1",
        "attempt_id": "a1",
        "output_type": "SEMANTIC_EVALUATION",
        "output": {"subject_reference": "src/app.txt", "verdict": "PASS", "rationale": "ok"},
        "provenance": {
            "model_id": "model-x",
            "model_version": "1",
            "call_id": "a1",
            "prompt_hash": prompt_hash,
            "policy_version": 1,
        },
    }
    payload.update(overrides)
    return payload


def _with_body(**body_overrides):
    body = dict(_valid_output()["output"])
    body.update(body_overrides)
    return _valid_output(output=body)


def _with_provenance(**provenance_overrides):
    provenance = dict(_valid_output()["provenance"])
    provenance.update(provenance_overrides)
    return _valid_output(provenance=provenance)


MUTATIONS = {
    # artifact mutations
    "unknown_output_type": lambda: _valid_output(output_type="UNKNOWN_TYPE"),
    "body_not_a_mapping": lambda: _valid_output(output=[]),
    "body_missing_schema_field": lambda: _with_body(rationale=None),
    "body_enum_out_of_catalogue": lambda: _with_body(verdict="MAYBE"),
    "body_missing_subject": lambda: _valid_output(
        output={"verdict": "PASS", "rationale": "ok"}
    ),
    "body_wrong_scalar_type": lambda: _with_body(subject_reference=123),
    "scope_path_outside": lambda: _with_body(paths=["outside/evil.txt"]),
    "scope_path_wrong_type": lambda: _with_body(paths=123),
    "scope_path_empty_string": lambda: _with_body(paths=[""]),
    # identity/version binding mutations, mutated separately from the artifact
    "provenance_missing": lambda: _valid_output(provenance=None),
    "provenance_missing_model_id": lambda: _with_provenance(model_id=""),
    "provenance_model_id_mismatch": lambda: _with_provenance(model_id="model-y"),
    "provenance_prompt_hash_mismatch": lambda: _with_provenance(prompt_hash="sha256:other"),
    "provenance_call_id_mismatch": lambda: _with_provenance(call_id="a2"),
    "provenance_policy_version_mismatch": lambda: _with_provenance(policy_version=2),
    "unknown_attempt": lambda: _valid_output(attempt_id="ghost"),
    "input_version_unknown_reference": lambda: _valid_output(input_versions={"ghost": 1}),
    "artefact_hash_unknown_reference": lambda: _valid_output(
        artefact_hashes={"ghost": "sha256:x"}
    ),
    "artefact_output_hash_not_imported": lambda: _valid_output(
        output_type="ARTEFACT",
        output={"reference": "r", "content_hash": "sha256:none", "media_type": "text/plain"},
    ),
}


@pytest.mark.parametrize("name", sorted(MUTATIONS))
def test_model_output_verifier_rejects_mutations(name):
    with pytest.raises(ModelOutputRejected):
        model_adapter.validate_output(_model_attempt_state(), MUTATIONS[name]())


def test_model_output_verifier_accepts_the_unmutated_baseline():
    model_adapter.validate_output(_model_attempt_state(), _valid_output())


def test_model_output_verifier_binds_artefact_hash_to_imported_content():
    state = _state_with_artefact("sha256:art-1")
    model_adapter.validate_output(
        state,
        _valid_output(
            output_type="ARTEFACT",
            output={
                "reference": "art-1",
                "content_hash": "sha256:art-1",
                "media_type": "text/plain",
                "path": "src/app.txt",
            },
            artefact_hashes={"art-1": "sha256:art-1"},
        ),
    )
    with pytest.raises(ModelOutputRejected):
        model_adapter.validate_output(
            state,
            _valid_output(
                output_type="ARTEFACT",
                output={
                    "reference": "art-1",
                    "content_hash": "sha256:art-1",
                    "media_type": "text/plain",
                },
                artefact_hashes={"art-1": "sha256:tampered"},
            ),
        )


# -- oracle grade verifier --------------------------------------------------


def _manifest() -> oracle.OracleManifest:
    criterion = oracle.OracleCriterion("AC-1", mandatory=True, weight=1.0)
    check = oracle.OracleCheck(
        "C-1",
        ("AC-1",),
        weight=1.0,
        expected_outcome=oracle.GRADE_PASS,
        timeout_seconds=1.0,
        mandatory=True,
        probe=lambda tree: oracle.GRADE_PASS,
    )
    manifest = oracle.OracleManifest("oracle-v1", (criterion,), (check,))
    manifest.validate_bidirectional_coverage(["AC-1"])
    return manifest


def _grade(**overrides) -> oracle.OracleGrade:
    values = {"oracle_version": "oracle-v1", "outcomes": {"C-1": oracle.GRADE_PASS}}
    values.update(overrides)
    return oracle.OracleGrade(**values)


def test_oracle_classifier_accepts_a_matching_grade():
    manifest = _manifest()
    assert (
        oracle.classify_trial(manifest, _grade(), oracle.ACCEPTANCE_CLAIMED)
        == oracle.TRIAL_CORRECT_ACCEPTANCE
    )


def test_oracle_classifier_flags_a_claimed_but_failing_grade():
    manifest = _manifest()
    grade = _grade(outcomes={"C-1": oracle.GRADE_FAIL})
    assert oracle.classify_trial(manifest, grade, oracle.ACCEPTANCE_CLAIMED) == oracle.TRIAL_FALSE_SUCCESS


def test_oracle_classifier_treats_unknown_outcomes_as_indeterminate():
    manifest = _manifest()
    grade = _grade(outcomes={"C-1": "NOT_A_GRADE"})
    assert oracle.classify_trial(manifest, grade, oracle.ACCEPTANCE_CLAIMED) == oracle.TRIAL_INDETERMINATE


# -- defects pinned by the verifier mutations -------------------------------


def test_bug_classifier_ignores_oracle_version_binding():
    """BUG: a grade produced under a different oracle version is trusted."""
    manifest = _manifest()
    mismatched = _grade(oracle_version="oracle-v2")
    assert (
        oracle.classify_trial(manifest, mismatched, oracle.ACCEPTANCE_CLAIMED)
        != oracle.TRIAL_CORRECT_ACCEPTANCE
    )


def test_bug_classifier_does_not_reject_an_incomplete_grade():
    """BUG: a grade missing a manifest check raises KeyError instead of a
    verification error."""
    manifest = _manifest()
    incomplete = _grade(outcomes={})
    with pytest.raises(oracle.OracleManifestInvalid):
        oracle.classify_trial(manifest, incomplete, oracle.ACCEPTANCE_CLAIMED)


# -- acceptance guard verifier ----------------------------------------------


def _validated_state() -> RunState:
    state = _validated_node_state()
    state, _ = reducer.reduce(state, _event("VALIDATION_STARTED", {"node_id": "n1"}, idem="vs"))
    state, _ = reducer.reduce(
        state, _event("VALIDATION_PASSED", {"node_id": "n1", "evidence": {}}, idem="vp")
    )
    return state


ACCEPTANCE_MUTATIONS = {
    # content mutation: fabricated inline checks are a declaration, not evidence
    "fabricated_inline_content": {
        "acceptance_id": "acc-content",
        "evidence": {"checks": [{"name": "FAKE", "passed": True}]},
    },
    # identity mutation: evidence owned by no admitted record
    "foreign_identity": {
        "acceptance_id": "acc-identity",
        "evidence_ids": ["ev-foreign"],
    },
}


@pytest.mark.parametrize("name", sorted(ACCEPTANCE_MUTATIONS))
def test_acceptance_guard_rejects_mutations(name):
    event = _event(
        "ACCEPTANCE_AUTHORISED",
        {"node_id": "n1", "bindings": ACCEPTANCE_MUTATIONS[name]},
        idem=ACCEPTANCE_MUTATIONS[name]["acceptance_id"],
    )
    with pytest.raises(reducer.ReducerError):
        reducer.reduce(_validated_state(), event)


# -- replay-equivalence comparator ------------------------------------------


def test_comparator_reports_no_difference_for_identical_state():
    state = _model_attempt_state()
    assert first_difference(state_fingerprint(state), state_fingerprint(state)) is None


@pytest.mark.parametrize(
    "mutate",
    [
        lambda state: state.nodes["n1"].__setattr__("state", "ACCEPTED"),
        lambda state: state.attempts["a1"].__setattr__("state", "FAILED"),
        lambda state: state.ledger.__setattr__("total_charged", 99.0),
        lambda state: state.__setattr__("graph_version", 7),
        lambda state: state.ledger.charged.__setitem__("a1", 42.0),
    ],
)
def test_comparator_detects_a_mutated_state(mutate):
    expected = state_fingerprint(_model_attempt_state())
    mutated = _model_attempt_state()
    mutate(mutated)
    difference = first_difference(expected, state_fingerprint(mutated))
    assert difference is not None


# --------------------------------------------------------------------------
# Verifier-mutation registry completion (report-recurring-failures.md #4):
# the binding classes — committed-tree binding, path_in_scope normalisation,
# and the R12 label-echo freeze binding.
# --------------------------------------------------------------------------


def test_delegated_scope_refuses_normalisation_variants():
    """Content mutation: a path smuggled with traversal or absolute variants
    that would normalise into the owned scope is refused — the matcher never
    normalises an untrusted path into scope (EXECUTION review F7)."""
    from orchestrator.executor_protocol import path_in_scope

    include = ["src/**"]
    smuggled = [
        "src/health/../../../hidden/expected.json",
        "/absolute/src/health/parse.py",
        "src/health/parse.py/..",
        "",
    ]
    for path in smuggled:
        assert not path_in_scope(path, include, []), (
            f"the normalisation variant {path!r} must never be in scope"
        )
    assert path_in_scope("src/health/parse.py", include, [])


def test_delegated_scope_honours_the_exclude_binding():
    """Identity mutation: the exclude binding is part of the authorisation —
    a path that matches the include pattern but the exclude pattern is
    refused, so relabelling the scope cannot widen it."""
    from orchestrator.executor_protocol import path_in_scope

    include = ["src/**"]
    exclude = ["src/health/**"]
    assert path_in_scope("src/render/render.py", include, exclude)
    assert not path_in_scope("src/health/parse.py", include, exclude)


def test_graded_tree_mutation_grades_fail(tmp_path, monkeypatch):
    """Content mutation: a hidden check that mutates the graded tree is graded
    FAIL by the per-check digest comparison — the oracle grades the committed
    tree, not a claim about it (R07)."""
    from orchestrator import sandbox

    caps = sandbox.capabilities()
    if not caps.profile_available:
        pytest.fail(f"NAMESPACE_V1 unavailable (fail-closed gate): {caps.unavailable_reason()}")
    from helpers import build_run_config  # noqa: F401 - scaffold parity

    experiment_module = _experiment_module()
    target = experiment_module.build_fixed_target(str(tmp_path))
    driver = (
        "import sys\n"
        "sys.path.insert(0, '/work')\n"
        "from src.parser.parse import parse\n"
        "assert parse('a b') == ['a', 'b']\n"
        "open('/work/mutated-by-check.txt', 'w').write('candidate tampering')\n"
        "print('DRIVER:' + '{\"a\": [\"a\", \"b\"]}')\n"
    )
    monkeypatch.setitem(experiment_module._HIDDEN_DRIVER_MAP, "hidden-parse", driver)
    from orchestrator import oracle

    check = oracle.OracleCheck(
        check_id="hidden-parse",
        criterion_ids=("AC-PARSE",),
        weight=1.0,
        expected_outcome=oracle.GRADE_PASS,
        timeout_seconds=10.0,
        mandatory=True,
    )
    outcome = experiment_module.hidden_runner(check, target.repository)
    assert outcome == oracle.GRADE_FAIL, (
        f"a tree-mutating check must grade FAIL, got {outcome!r}"
    )


def _experiment_module():
    import os
    import sys

    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    if str(os.path.join(root, "src")) not in sys.path:
        sys.path.insert(0, os.path.join(root, "src"))
    from orchestrator import experiment

    return experiment


def test_frozen_binding_refuses_mismatched_oracle_and_profile():
    """Identity mutation (R12 label-echo): the freeze binding must REJECT a
    mismatched pair — an oracle under a different version, or a model profile
    that does not match the frozen identifier. A binding without
    mismatch-rejection is decorative."""
    experiment_module, manifest, oracle_manifest = _binding_fixtures()
    experiment_module._require_frozen_binding(manifest, oracle_manifest)
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module._require_frozen_binding(
            manifest,
            dataclasses.replace(oracle_manifest, oracle_version="oracle-v9"),
        )
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module._require_frozen_binding(
            dataclasses.replace(manifest, model_identifier="other-model"),
            oracle_manifest,
        )


def test_frozen_binding_refuses_a_mutated_suite_hash():
    """Content mutation: a supplied oracle/suite configuration whose frozen
    hidden-suite hash does not match the manifest is rejected."""
    experiment_module, manifest, oracle_manifest = _binding_fixtures()
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module._require_frozen_binding(
            dataclasses.replace(
                manifest, hidden_suite_manifest_hash="sha256:mutated"
            ),
            oracle_manifest,
        )


class _BindingTarget:
    def snapshot_hash(self):
        return "sha256:target"


@dataclasses.dataclass(frozen=True)
class _BindingManifest:
    oracle_version: str
    target: _BindingTarget
    hidden_suite_manifest_hash: str
    model_identifier: str
    model_version: str
    sampling_parameters: dict
    component_versions: tuple
    environment_image_digest: str


def _binding_fixtures():
    experiment_module = _experiment_module()
    oracle_manifest = _binding_oracle()
    target = _BindingTarget()
    suite_hash = experiment_module.hidden_suite_manifest_hash(target, oracle_manifest)
    profile = experiment_module.MODEL_PROFILE
    manifest = _BindingManifest(
        oracle_version="oracle-v1",
        target=target,
        hidden_suite_manifest_hash=suite_hash,
        model_identifier=profile.model_id,
        model_version=profile.model_version,
        sampling_parameters=profile.sampling_parameters(),
        component_versions=experiment_module.component_versions(),
        environment_image_digest=experiment_module._environment_image_digest(),
    )
    return experiment_module, manifest, oracle_manifest


def _binding_oracle():
    from orchestrator import oracle as oracle_module

    return oracle_module.OracleManifest(
        oracle_version="oracle-v1",
        criteria=(oracle_module.OracleCriterion("AC-PARSE"),),
        checks=(
            oracle_module.OracleCheck(
                check_id="hidden-parse",
                criterion_ids=("AC-PARSE",),
                weight=1.0,
                expected_outcome=oracle_module.GRADE_PASS,
                timeout_seconds=5.0,
            ),
        ),
    )


def _fidelity_ledger(workdir: str):
    from orchestrator import spend as spend_module
    from helpers import experiment_authorization

    return spend_module.open_spend_ledger(workdir, experiment_authorization())


def test_oracle_manifest_binding_refuses_a_foreign_target(tmp_path):
    """R12 binding with mismatch-rejection (Phase 1.4): the oracle manifest is
    frozen for one target snapshot; the trial driver refuses a hand-built or
    foreign manifest before any work or spend exists."""
    experiment_module = _experiment_module()
    import dataclasses as _dataclasses

    target = experiment_module.build_fixed_target(str(tmp_path))
    oracle_manifest = experiment_module.build_hidden_oracle(target)
    assert oracle_manifest.target_binding == experiment_module.oracle_target_binding(
        target, oracle_manifest
    )
    # A hand-built (unbound) manifest is not runnable either.
    unbound = _dataclasses.replace(oracle_manifest, target_binding="")
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module.run_trial(
            target,
            unbound,
            experiment_module.placeholder_limits(),
            experiment_module.MODEL_PROFILE,
            experiment_module.ARM_DIRECT,
            1001,
            str(tmp_path),
            spend_ledger=_fidelity_ledger(str(tmp_path)),
        )
    # An altered check set with a stale binding is refused (P2-4: the binding
    # describes the manifest's own content).
    tampered = _dataclasses.replace(
        oracle_manifest,
        checks=oracle_manifest.checks + (
            _dataclasses.replace(oracle_manifest.checks[0], check_id="smuggled"),
        ),
    )
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module.run_trial(
            target,
            tampered,
            experiment_module.placeholder_limits(),
            experiment_module.MODEL_PROFILE,
            experiment_module.ARM_DIRECT,
            1001,
            str(tmp_path),
            spend_ledger=_fidelity_ledger(str(tmp_path)),
        )
    # calibrate_oracle refuses the same shapes before any case runs.
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module.calibrate_oracle(target, tampered, str(tmp_path / "cal"))
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module.calibrate_oracle(target, unbound, str(tmp_path / "cal"))

    # freeze_manifest refuses an unbound oracle (no provenance to the target).
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module.freeze_manifest(
            target=target,
            oracle_manifest=unbound,
            limits=experiment_module.placeholder_limits(),
            calibration=experiment_module.CalibrationReport(cases=()),
        )

    # A foreign target (a different snapshot) is refused just as early.
    foreign = _dataclasses.replace(target, commit="0" * 40)
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module.run_trial(
            foreign,
            oracle_manifest,
            experiment_module.placeholder_limits(),
            experiment_module.MODEL_PROFILE,
            experiment_module.ARM_DIRECT,
            1001,
            str(tmp_path),
            spend_ledger=_fidelity_ledger(str(tmp_path)),
        )
    assert foreign.commit != target.commit


def test_freeze_refuses_a_tampered_or_foreign_manifest_review_f13(tmp_path):
    """Review F13: freeze_manifest recomputes the binding — a manifest whose
    check set was altered after binding, or whose binding belongs to another
    target, is refused at freeze (it used to freeze, and to the same content
    hash as the valid one)."""
    experiment_module = _experiment_module()
    target = experiment_module.build_fixed_target(str(tmp_path))
    oracle_manifest = experiment_module.build_hidden_oracle(target)
    limits = experiment_module.placeholder_limits()

    def _case(case_id):
        return experiment_module.CalibrationOutcome(
            case_id=case_id,
            description=case_id,
            expected_classification="CORRECT_ACCEPTANCE",
            outcomes=(("hidden-parse", "PASS"),),
            effective_outcomes=(("hidden-parse", "PASS"),),
            verified_progress=1.0,
            criterion_verified_progress=1.0,
            classification="CORRECT_ACCEPTANCE",
            rerun_eligible=(),
            rerun_spent=False,
        )

    calibration = experiment_module.CalibrationReport(
        cases=tuple(_case(name) for name in (
            "known-good", "unchanged", "known-broken", "known-timeout", "infrastructure",
        ))
    )

    valid = experiment_module.freeze_manifest(
        target=target,
        oracle_manifest=oracle_manifest,
        limits=limits,
        calibration=calibration,
    )
    # The frozen manifest carries the binding in its content hash.
    assert valid.oracle_binding == experiment_module.oracle_target_binding(target, oracle_manifest)

    # A tampered check set keeps the stale binding: refused.
    tampered = dataclasses.replace(
        oracle_manifest,
        checks=oracle_manifest.checks
        + (dataclasses.replace(oracle_manifest.checks[0], check_id="smuggled"),),
    )
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module.freeze_manifest(
            target=target,
            oracle_manifest=tampered,
            limits=limits,
            calibration=calibration,
        )

    # A foreign target with the same manifest: refused.
    foreign = dataclasses.replace(target, commit="0" * 40)
    with pytest.raises(experiment_module.ExperimentError):
        experiment_module.freeze_manifest(
            target=foreign,
            oracle_manifest=oracle_manifest,
            limits=limits,
            calibration=calibration,
        )
