# SPDX-License-Identifier: AGPL-3.0-or-later
"""Batch 6 residual items: R17 input-binding staleness, R12 model identity,
R11 trial inspection, R18 selection before batching.
"""
from __future__ import annotations

import json
import os

import pytest

from test_milestone4_conformance import (
    _event,
    _model_attempt_state as model_attempt_state,
    _valid_output as valid_output,
)

from orchestrator import experiment, model_adapter, reducer, spend
from orchestrator.model_adapter import ModelClient, ModelCompletion, ModelOutput, ModelOutputRejected


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



class InvalidVerdictClient(ModelClient):
    def max_cost(self, request):
        return 1.0

    def max_tokens(self, request):
        return (10, 10)

    def complete(self, request):
        return ModelCompletion(
            outputs=(
                ModelOutput(
                    "SEMANTIC_EVALUATION",
                    {"subject_reference": "n", "verdict": "NOPE", "rationale": "r"},
                ),
            ),
            cost=1.0,
            units="USD",
            model_id="m",
            model_version="1",
            input_tokens=10,
            output_tokens=10,
        )



class HugeBodyClient(ModelClient):
    def __init__(self, marker="Z" * 1000):
        self.marker = marker

    def max_cost(self, request):
        return 1.0

    def max_tokens(self, request):
        return (10, 10)

    def complete(self, request):
        return ModelCompletion(
            outputs=(ModelOutput("OBSERVATION", {
                "subject": "s",
                "statement": self.marker * (model_adapter.MAX_OUTPUT_BODY_BYTES // 1000 + 2),
                "observed_references": [],
            }),),
            cost=1.0, units="USD", model_id="m", model_version="1",
            input_tokens=10, output_tokens=10,
        )



class HugeTypeClient(ModelClient):
    def max_cost(self, request):
        return 1.0

    def max_tokens(self, request):
        return (10, 10)

    def __init__(self, huge_type="T" * 10_000):
        self.huge_type = huge_type

    def complete(self, request):
        return ModelCompletion(
            outputs=(ModelOutput(self.huge_type, {
                "subject": "s", "statement": "A" * 70_000, "observed_references": []}),),
            cost=1.0, units="USD", model_id="m", model_version="1",
            input_tokens=10, output_tokens=10,
        )


def test_matching_input_bindings_are_accepted():
    """Anti-vacuity: the matching bindings pass before the mutations."""
    state = _state_with_artefact()
    payload = valid_output(
        input_versions={"art-1": 1}, artefact_hashes={"art-1": "sha256:h"}
    )
    model_adapter.validate_output(state, payload)


def test_stale_artefact_hash_is_rejected():
    """R17: an output whose input hash no longer matches the current artefact is
    stale and must be rejected."""
    state = _state_with_artefact()
    state.artefacts["art-1"].hash = "sha256:changed"
    payload = valid_output(
        input_versions={"art-1": 1}, artefact_hashes={"art-1": "sha256:h"}
    )
    with pytest.raises(ModelOutputRejected):
        model_adapter.validate_output(state, payload)


def test_changed_input_version_is_rejected():
    state = _state_with_artefact()
    payload = valid_output(input_versions={"art-1": 2})
    with pytest.raises(ModelOutputRejected):
        model_adapter.validate_output(state, payload)


def test_unknown_input_reference_is_rejected():
    state = _state_with_artefact()
    payload = valid_output(input_versions={"missing": 1})
    with pytest.raises(ModelOutputRejected):
        model_adapter.validate_output(state, payload)


def test_model_identity_mismatch_is_rejected():
    """R12: a completion resolved to a different model identity is not the
    recorded call and is refused; no alias is recorded without an explicit
    permitted-alias policy."""
    state = _state_with_artefact()
    payload = valid_output()
    payload["provenance"] = {**payload["provenance"], "model_id": "other-model"}
    with pytest.raises(ModelOutputRejected):
        model_adapter.validate_output(state, payload)


def test_inspect_trial_reports_records_and_refuses_absent(tmp_path):
    """R11: an existing trial can be inspected without running it; an absent
    trial is refused."""
    workdir = str(tmp_path)
    authorization = spend.ExperimentAuthorization(
        owner="test", limit=100.0, authorized_at="2026-01-01T00:00:00Z", method="test"
    )
    ledger = spend.open_spend_ledger(workdir, authorization)
    ledger.reserve("EXP-DIRECT-1", 10.0, identity={"arm": "DIRECT", "seed": 1})
    trial_root = os.path.join(workdir, "trials", "DIRECT-1")
    os.makedirs(trial_root, exist_ok=True)
    with open(os.path.join(trial_root, "identity.json"), "w") as handle:
        json.dump({"experiment": "X", "arm": "DIRECT", "seed": 1}, handle)
    inspected = experiment.inspect_trial(workdir, "DIRECT", 1)
    assert inspected["identity"]["arm"] == "DIRECT"
    assert inspected["store_exists"] is False
    assert inspected["ledger_trial"]["status"] == "RESERVED"
    with pytest.raises(experiment.ExperimentError):
        experiment.inspect_trial(workdir, "DIRECT", 99)


def test_two_composite_children_select_decomposition_in_both_arms():
    """R18: a contract with two composite children is never treated as a
    directly executable leaf, so concurrent batching cannot merge decomposable
    nodes."""
    composite = {
        "obligations": (
            {"child_id": "a", "scope": "src/a/**", "mandatory": True},
            {"child_id": "b", "scope": "src/b/**", "mandatory": True},
        )
    }
    assert experiment.select_ready_action(composite, direct=False) == "PROPOSE_DECOMPOSITION"
    # DIRECT stays an undecomposed baseline by construction.
    assert experiment.select_ready_action(composite, direct=True) == "EXECUTE"
    leaf = {"obligations": ({"child_id": "a", "scope": "src/a/**", "mandatory": True},)}
    assert experiment.select_ready_action(leaf, direct=False) == "EXECUTE"


def test_model_client_factory_defaults_to_scripted_and_can_be_injected():
    """The smoke runner's injection point: the default stays the deterministic
    scripted client, and a factory replaces it without touching the pipeline."""
    from orchestrator import experiment

    original = experiment.MODEL_CLIENT_FACTORY
    try:
        experiment.set_model_client_factory(None)
        assert isinstance(
            experiment.model_client_for(experiment.MODEL_PROFILE),
            experiment.ExperimentModelClient,
        )
        marker = object()
        experiment.set_model_client_factory(lambda profile, seed: (marker, seed))
        assert experiment.model_client_for(experiment.MODEL_PROFILE) == (marker, None)
        # The trial seed is carried into the client factory.
        assert experiment.model_client_for(experiment.MODEL_PROFILE, 1003) == (marker, 1003)
    finally:
        experiment.set_model_client_factory(original)


def test_secrets_source_injection_can_be_installed():
    from orchestrator import experiment, secrets

    original = experiment.SECRETS_SOURCE
    try:
        source = secrets.SecretsSource()
        experiment.set_secrets_source(source)
        assert experiment.SECRETS_SOURCE is source
    finally:
        experiment.set_secrets_source(original)


def test_model_output_rejection_is_recorded_without_the_untrusted_body():
    """A rejected reply is recorded (trusted reason only) as REJECTED."""
    from test_milestone4_conformance import _event, _model_attempt_state

    state = _model_attempt_state()
    state, _ = reducer.reduce(
        state,
        _event(
            "MODEL_OUTPUT_REJECTED",
            {
                "output_id": "o1",
                "attempt_id": "a1",
                "output_type": "SEMANTIC_EVALUATION",
                "reason": "invalid schema field 'verdict'",
            },
            idem="rej",
        ),
    )
    record = state.model_outputs["o1"]
    assert record.state == "REJECTED"
    assert record.reason == "invalid schema field 'verdict'"
    assert record.body == {}


def test_schema_invalid_provider_reply_is_rejected_not_fatal(tmp_path):
    """An untrusted reply that fails validation is recorded as a rejection and
    does not abort the attempt or the trial."""
    from helpers import build_run_config

    from orchestrator.controller import Controller
    from orchestrator.executor import ScriptedExecutor
    from orchestrator.model_adapter import ModelAdapter, ModelClient, ModelCompletion, ModelOutput
    from orchestrator.store import EventStore

    run_id = "RUN-REJECT"

    store = EventStore(str(tmp_path / "reject.db"))
    adapter = ModelAdapter(InvalidVerdictClient(), model_id="m", model_version="1")
    controller = Controller(
        store, ScriptedExecutor({}), model_executor=adapter,
        executor_environment={"base_commit": "HEAD", "timeout_seconds": 30.0},
    )
    try:
        root = controller.submit_run(
            build_run_config(
                run_id,
                budgets={"MODEL_COST": 10.0},
                limits={
                    "root_model_input_tokens": 100,
                    "root_model_output_tokens": 100,
                    "model_calls": 2,
                },
            )
        )
        _attempt, response = controller.run_model_review(run_id, root, 1.0)
        assert response["self_reported_status"] == "COMPLETED"
        state = store.state(run_id)
        assert all(
            record.state == "REJECTED" for record in state.model_outputs.values()
        )
        event_types = [event.event_type for event in store.history(run_id)]
        assert "MODEL_OUTPUT_REJECTED" in event_types
        assert "MODEL_OUTPUT_ADMITTED" not in event_types
    finally:
        store.close()


def test_oversized_output_body_is_rejected_before_admission():
    """F1: a reply cannot convert volume into durable state; the body bound is
    enforced by validate_output for any client."""
    state = model_attempt_state()
    payload = valid_output()
    payload["output"]["rationale"] = "A" * (model_adapter.MAX_OUTPUT_BODY_BYTES + 1)
    with pytest.raises(ModelOutputRejected):
        model_adapter.validate_output(state, payload)


def test_rejection_reason_is_bounded_and_omits_the_untrusted_value(tmp_path):
    """F3: the persisted rejection reason is a bounded trusted diagnostic, not
    the untrusted reply bytes."""
    from helpers import build_run_config

    from orchestrator.controller import Controller
    from orchestrator.executor import ScriptedExecutor
    from orchestrator.model_adapter import ModelAdapter, ScriptedModelClient
    from orchestrator.store import EventStore

    run_id = "RUN-REASON"
    client = ScriptedModelClient(
        {
            f"{run_id}-root": model_adapter.ModelCompletion(
                outputs=(model_adapter.ModelOutput("OBSERVATION", {
                    "subject": "s", "statement": "x", "observed_references": []}),),
                cost=1.0, units="USD", model_id="model-x", model_version="1",
                input_tokens=1, output_tokens=1,
            )
        }
    )
    store = EventStore(str(tmp_path / "reason.db"))
    adapter = ModelAdapter(client, model_id="model-x", model_version="1")
    controller = Controller(store, ScriptedExecutor({}), model_executor=adapter)
    try:
        root = controller.submit_run(
            build_run_config(
                run_id,
                budgets={"MODEL_COST": 10.0},
                limits={"root_model_input_tokens": 100, "root_model_output_tokens": 100},
            )
        )
        attempt, _ = controller.run_model_review(run_id, root, 1.0)
        huge = "A" * 100_000
        controller.admit_model_outputs(run_id, attempt, [
            {
                "output_type": "OBSERVATION",
                "output": {"subject": "s", "statement": "x", "observed_references": []},
                "provenance": {
                    "model_id": "model-x", "model_version": "1", "call_id": attempt,
                    "prompt_hash": controller._prompt_hashes[root], "policy_version": 1,
                },
            },
            {
                "output_type": "OBSERVATION",
                "output": {"subject": "s", "statement": "x", "observed_references": [],
                           "paths": [f"secrets/{huge}.txt"]},
                "provenance": {
                    "model_id": "model-x", "model_version": "1", "call_id": attempt,
                    "prompt_hash": controller._prompt_hashes[root], "policy_version": 1,
                },
            },
        ])
        rejected = [
            event for event in store.history(run_id)
            if event.event_type == "MODEL_OUTPUT_REJECTED"
        ]
        assert rejected
        reason = rejected[-1].payload["reason"]
        assert len(reason) <= 520
        assert huge[:100] not in reason
    finally:
        store.close()


def test_oversized_reply_is_not_persisted_before_validation(tmp_path):
    """F1: an oversized reply is dropped and recorded as a rejection *before*
    the observation/result events, so the event store cannot grow with it."""
    import json as _json
    import os as _os

    from helpers import build_run_config

    from orchestrator.controller import Controller
    from orchestrator.executor import ScriptedExecutor
    from orchestrator.model_adapter import ModelAdapter, ModelClient, ModelCompletion, ModelOutput
    from orchestrator.store import EventStore

    run_id = "RUN-OVERSIZED"
    marker = "Z" * 1000

    db_path = str(tmp_path / "oversized.db")
    store = EventStore(db_path)
    adapter = ModelAdapter(HugeBodyClient(), model_id="m", model_version="1")
    controller = Controller(
        store, ScriptedExecutor({}), model_executor=adapter,
        executor_environment={"base_commit": "HEAD", "timeout_seconds": 30.0},
    )
    try:
        root = controller.submit_run(
            build_run_config(
                run_id,
                budgets={"MODEL_COST": 10.0},
                limits={"root_model_input_tokens": 100, "root_model_output_tokens": 100},
            )
        )
        controller.run_model_review(run_id, root, 1.0)
        rejected = [
            event for event in store.history(run_id)
            if event.event_type == "MODEL_OUTPUT_REJECTED"
        ]
        assert rejected
        assert "byte bound" in rejected[-1].payload["reason"]
        for event in store.history(run_id):
            assert marker not in _json.dumps(event.payload, default=str)
        assert _os.path.getsize(db_path) < 1_000_000
    finally:
        store.close()


def test_oversized_rejection_bounds_the_untrusted_output_type(tmp_path):
    """Latent P3: a pass-through client can supply an arbitrary output_type
    string; the recorded rejection field is bounded."""
    from helpers import build_run_config

    from orchestrator.controller import Controller
    from orchestrator.executor import ScriptedExecutor
    from orchestrator.model_adapter import ModelAdapter, ModelClient, ModelCompletion, ModelOutput
    from orchestrator.store import EventStore

    run_id = "RUN-TYPEBOUND"
    huge_type = "T" * 10_000

    store = EventStore(str(tmp_path / "typebound.db"))
    adapter = ModelAdapter(HugeTypeClient(), model_id="m", model_version="1")
    controller = Controller(
        store, ScriptedExecutor({}), model_executor=adapter,
        executor_environment={"base_commit": "HEAD", "timeout_seconds": 30.0},
    )
    try:
        root = controller.submit_run(
            build_run_config(
                run_id,
                budgets={"MODEL_COST": 10.0},
                limits={"root_model_input_tokens": 100, "root_model_output_tokens": 100},
            )
        )
        controller.run_model_review(run_id, root, 1.0)
        rejected = [
            event for event in store.history(run_id)
            if event.event_type == "MODEL_OUTPUT_REJECTED"
        ]
        assert rejected
        assert len(rejected[-1].payload["output_type"]) <= 80
    finally:
        store.close()
