# SPDX-License-Identifier: AGPL-3.0-or-later
"""A1 guard-disable mutation tests.

An enforcement parameter must not be disablable by supplying its sentinel or
default value (``0``, ``None``, ``{}``, ``[]``, ``""``, or a non-finite
number). For each enforcement parameter this suite first proves the valid case
works — so a rejection is attributable to the mutation — then substitutes the
sentinels and asserts the guard still holds or the call is refused before any
attempt is admitted.

Enforcement parameters covered: the per-run commitment, the declared token
ceiling, the run ``limits`` block, and the experiment spend ledger.
"""
from __future__ import annotations

import inspect

import pytest

from orchestrator import experiment, model_adapter, schemas
from orchestrator.controller import BudgetExhausted, Controller, WorkflowError
from orchestrator.executor import ScriptedExecutor
from orchestrator.model import LedgerStateError, OvercommitmentError
from orchestrator.model_adapter import ModelAdapter
from orchestrator.schemas import SchemaValidationError
from orchestrator.store import EventStore

from helpers import build_run_config
from test_milestone8_readiness import _CountingModelClient, _token_controller

REFUSALS = (
    WorkflowError,
    LedgerStateError,
    OvercommitmentError,
    SchemaValidationError,
    TypeError,
    ValueError,
)


def _completion(input_tokens: int = 1, output_tokens: int = 1):
    return model_adapter.ModelCompletion(
        outputs=(),
        cost=1.0,
        units="COST_UNITS",
        model_id="model-x",
        model_version="1",
        input_tokens=input_tokens,
        output_tokens=output_tokens,
    )


def test_valid_commitment_and_model_review_are_accepted_first(tmp_path):
    """Anti-vacuity: the unmutated cases must succeed."""
    client = _CountingModelClient(_completion(), max_cost=1.0, max_tokens=(5, 5))
    store, controller, root = _token_controller(
        tmp_path,
        "RUN-A1-VALID",
        client,
        limits={
            "root_model_input_tokens": 100,
            "root_model_output_tokens": 100,
            "model_calls": 10,
        },
    )
    try:
        attempt = controller.plan_attempt("RUN-A1-VALID", root, 5.0, action_type="REVIEW")
        assert attempt in controller.store.state("RUN-A1-VALID").attempts
        controller.run_model_review("RUN-A1-VALID", root, 5.0)
        assert len(client.requests) == 1
    finally:
        store.close()


@pytest.mark.parametrize("sentinel", [None, {}, [], "", float("nan"), -1.0])
def test_commitment_sentinel_cannot_disable_the_ledger_guard(tmp_path, sentinel):
    run_id = "RUN-A1-SENT"
    client = _CountingModelClient(_completion(), max_cost=1.0, max_tokens=(1, 1))
    store, controller, root = _token_controller(tmp_path, run_id, client)
    try:
        with pytest.raises(REFUSALS):
            controller.plan_attempt(run_id, root, sentinel, action_type="REVIEW")
        assert controller.store.state(run_id).attempts == {}
    finally:
        store.close()


def test_zero_declared_token_ceiling_sentinel_still_bounds_spend(tmp_path):
    """A client declaring ``(0, 0)`` declares no per-call bound; the run ceiling
    must still be enforced (bounded) rather than disabled."""
    client = _CountingModelClient(
        _completion(100, 20), max_cost=1.0, max_tokens=(0, 0)
    )
    store, controller, root = _token_controller(
        tmp_path,
        "RUN-A1-ZERO",
        client,
        limits={"root_model_input_tokens": 100, "root_model_output_tokens": 100},
    )
    try:
        refused = False
        for _ in range(4):
            try:
                controller.run_model_review("RUN-A1-ZERO", root, 1.0)
            except BudgetExhausted:
                refused = True
                break
        assert refused, "the token ceiling must remain enforced"
        assert len(client.requests) <= 3, "the ceiling cannot be disabled entirely"
    finally:
        store.close()


def test_bug_missing_limits_leave_model_spend_unbounded(tmp_path):
    """FIXED (F6 second half): with no ``limits`` configured, a model action is
    refused before any billable call, rather than spending unbounded."""
    client = _CountingModelClient(_completion(), max_cost=1.0, max_tokens=(1, 1))
    store, controller, root = _token_controller(tmp_path, "RUN-A1-NOLIM", client)
    try:
        with pytest.raises(BudgetExhausted) as raised:
            controller.run_model_review("RUN-A1-NOLIM", root, 1.0)
        # The refusal is attributable to the missing token ceiling (not a broken
        # fixture), and it happens before the client is ever invoked.
        assert raised.value.limit_name in (
            "root_model_input_tokens",
            "root_model_output_tokens",
        )
        assert client.requests == [], (
            "a model action without a configured ceiling must be refused before any "
            "billable call"
        )
    finally:
        store.close()


def test_run_trial_requires_a_spend_ledger():
    """F5: no trial entry point may start without an experiment reservation."""
    parameter = inspect.signature(experiment.run_trial).parameters["spend_ledger"]
    assert parameter.default is inspect.Parameter.empty


# --------------------------------------------------------------------------
# Class C completion: every enforcement parameter gets its sentinel matrix
# (report-recurring-failures.md Tier-1 #2). The rows below are registered in
# tests/guard_mutation_matrix.yaml, whose meta-test derives the run-limit keys
# from schemas.RUN_LIMIT_KEYS so a new frozen limit without a row fails.
# --------------------------------------------------------------------------


@pytest.mark.parametrize("key", schemas.RUN_LIMIT_KEYS)
@pytest.mark.parametrize("sentinel", [None, "", -1, float("nan")])
def test_limit_value_sentinel_is_refused_at_admission(tmp_path, key, sentinel):
    """A non-finite, negative, or non-numeric limit value is not a disabled
    guard: the schema refuses the run at admission — before any work, call, or
    reservation exists (R03)."""
    client = _CountingModelClient(_completion(), max_cost=1.0, max_tokens=(1, 1))
    limits = {name: 100 for name in schemas.RUN_LIMIT_KEYS}
    limits[key] = sentinel
    store = EventStore(str(tmp_path / "events.db"))
    controller = Controller(
        store,
        ScriptedExecutor({}),
        model_executor=ModelAdapter(client, model_id="model-x", model_version="1"),
    )
    try:
        with pytest.raises(REFUSALS):
            controller.submit_run(
                build_run_config("RUN-A1-LIMIT", limits=limits)
            )
        assert controller.store.state("RUN-A1-LIMIT").nodes == {}
        assert client.requests == []
    finally:
        store.close()


def test_zero_limits_clamp_fail_closed_not_open(tmp_path):
    """A zero limit is a legal clamp-to-nothing: the first model action is
    refused before any billable call — never a disabled guard."""
    client = _CountingModelClient(_completion(), max_cost=1.0, max_tokens=(1, 1))
    limits = {name: 0 for name in schemas.RUN_LIMIT_KEYS}
    store, controller, root = _token_controller(
        tmp_path, "RUN-A1-ZEROALL", client, limits=limits
    )
    try:
        with pytest.raises(REFUSALS):
            controller.run_model_review("RUN-A1-ZEROALL", root, 1.0)
        assert client.requests == []
    finally:
        store.close()


def test_zero_declared_charge_still_bounded_by_the_commitment(tmp_path):
    """A client declaring a zero maximum charge sharpens nothing: the call may
    proceed, but the admitted actual is still bounded by the controller's
    commitment — the declaration never replaces the guard."""
    client = _CountingModelClient(_completion(), max_cost=0.0, max_tokens=(5, 5))
    store, controller, root = _token_controller(
        tmp_path,
        "RUN-A1-COST0",
        client,
        limits={
            "root_model_input_tokens": 100,
            "root_model_output_tokens": 100,
            "model_calls": 10,
        },
    )
    try:
        controller.run_model_review("RUN-A1-COST0", root, 1.0)
        attempt = next(iter(controller.store.state("RUN-A1-COST0").attempts.values()))
        assert attempt.accounting_state == "FINALLY_SETTLED"
        assert (attempt.actual_cost or 0.0) <= 1.0, (
            "the committed amount is the bound, not the client's declaration"
        )
    finally:
        store.close()


def test_negative_declared_token_ceiling_self_refuses(tmp_path):
    """A client declaring a negative per-call ceiling is refused at plan time
    (the adapter validates its own declaration) — before any call, output, or
    charge exists."""
    client = _CountingModelClient(
        _completion(100, 20), max_cost=1.0, max_tokens=(-1, -1)
    )
    store, controller, root = _token_controller(
        tmp_path,
        "RUN-A1-NEG",
        client,
        limits={
            "root_model_input_tokens": 100000,
            "root_model_output_tokens": 100000,
            "model_calls": 5,
        },
    )
    try:
        with pytest.raises(REFUSALS):
            controller.run_model_review("RUN-A1-NEG", root, 1.0)
        assert client.requests == [], (
            "the malformed declaration is refused before any call"
        )
        exhaustion = [
            e
            for e in store.history("RUN-A1-NEG")
            if e.event_type == "BUDGET_EXHAUSTION_RECORDED"
        ]
        assert exhaustion, "the refusal is recorded as budget exhaustion"
    finally:
        store.close()


@pytest.mark.parametrize("sentinel", [-1, None])
def test_rate_limit_retry_cap_sentinel_stays_bounded(sentinel):
    """The bounded 429 backoff cap cannot be disabled by a sentinel: a
    negative count clamps to zero retries (bounded), and a non-numeric one is
    refused at construction."""
    smoke = _load_smoke_run()
    credential = smoke.secrets.Credential(
        credential_id="cap-sentinel", scope="MODEL_ENDPOINT",
        secret="axis2-cap-sentinel", expires_at=1e18,
    )
    kwargs = dict(
        base_url="http://127.0.0.1:9", path="/s/valid", credential=credential,
        units="COST_UNITS", price_input_per_mtok=0.3, price_output_per_mtok=1.2,
        max_input_tokens=100, timeout=1.0,
    )
    if sentinel is None:
        with pytest.raises(TypeError):
            smoke.OpenAICompatibleClient(
                smoke.experiment.ModelProfile(
                    model_id="m", model_version="1", temperature=0.0, top_p=1.0,
                    max_output_tokens=8, input_tokens_per_call=10,
                    output_tokens_per_call=8, cost_per_call=1.0,
                ),
                max_rate_limit_retries=sentinel,
                **kwargs,
            )
    else:
        client = smoke.OpenAICompatibleClient(
            smoke.experiment.ModelProfile(
                model_id="m", model_version="1", temperature=0.0, top_p=1.0,
                max_output_tokens=8, input_tokens_per_call=10,
                output_tokens_per_call=8, cost_per_call=1.0,
            ),
            max_rate_limit_retries=sentinel,
            **kwargs,
        )
        assert client.max_rate_limit_retries >= 0, "the cap clamps, never disables"


@pytest.mark.parametrize("timeout_sentinel", [0, -1, float("nan"), ""])
@pytest.mark.timeout(30)
def test_per_call_deadline_sentinels_stay_bounded(tmp_path, timeout_sentinel):
    """A zero, negative, non-finite, or empty per-call deadline is clamped to
    the minimum bounded budget — a stalled provider can never run unbounded
    (Axis 2 acceptance: deadlines bound every call)."""
    import provider_stub
    import smoke_run

    credential = smoke_run.secrets.Credential(
        credential_id="deadline-sentinel", scope="MODEL_ENDPOINT",
        secret="axis2-deadline-sentinel", expires_at=1e18,
    )
    with provider_stub.StubServer() as stub:
        client = smoke_run.OpenAICompatibleClient(
            smoke_run.experiment.ModelProfile(
                model_id="stub-model", model_version="1", temperature=0.0,
                top_p=1.0, max_output_tokens=64, input_tokens_per_call=100,
                output_tokens_per_call=64, cost_per_call=1.0,
            ),
            base_url=stub.base_url, path="/s/stall", credential=credential,
            units="COST_UNITS", price_input_per_mtok=0.3,
            price_output_per_mtok=1.2, max_input_tokens=100, timeout=1.0,
        )
        import time as _time

        started = _time.monotonic()
        with pytest.raises(RuntimeError):
            client.complete({"prompt": "p", "timeout_seconds": timeout_sentinel})
        assert _time.monotonic() - started < 3.0, "the call ran unbounded"


def _load_smoke_run():
    import importlib.util
    import os
    import sys

    path = os.path.join(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
        "scripts", "smoke_run.py",
    )
    spec = importlib.util.spec_from_file_location("guard_smoke_run", path)
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module
