# SPDX-License-Identifier: AGPL-3.0-or-later
"""The six acceptance demonstrations of the fourth roadmap milestone, plus the
model-plans-then-executes end-to-end run.

The model adapter is a deterministic mockable fixture; real sandboxed commands
still run through the command executor. Model outputs are untrusted typed
assertions validated at admission; the recorded executor result is the only
command evidence.
"""

from __future__ import annotations

import hashlib
import json
import os

import pytest

from orchestrator import acceptance_checks, executor_protocol, model_adapter, sandbox
from orchestrator.command_executor import CommandExecutor, CommandExecutorConfiguration
from orchestrator.controller import AcceptanceRefused, BudgetExhausted, Controller
from orchestrator.model_adapter import ModelAdapter, ModelCompletion, ModelOutput, ScriptedModelClient
from orchestrator.store import EventStore

from helpers import build_run_config

# Reuse the frozen-fixture repository and toolchain helpers from milestone 3.
from test_milestone3_acceptance import (
    GIT_ENVIRONMENT,
    TOOLCHAIN_PATHS,
    build_fixture_repo,
    python_probe,
)


class Harness:
    def __init__(self, root, *, run_id="RUN-M4", commands=None, completion=None, max_cost=None,
                 outputs=None):
        self.root = root
        self.run_id = run_id
        self.repo, self.commit = build_fixture_repo(root)
        self.command_executor = CommandExecutor(
            CommandExecutorConfiguration(
                repository=self.repo,
                base_commit=self.commit,
                worktree_root=os.path.join(root, "worktrees"),
                artefact_root=os.path.join(root, "artefacts"),
                toolchain_paths=TOOLCHAIN_PATHS,
                commands=commands
                or {
                    f"{run_id}-root": (
                        (
                            "BUILD",
                            python_probe(
                                "import os\n"
                                "os.makedirs('/work/src/health/api', exist_ok=True)\n"
                                "open('/work/src/health/api/routes.py','w').write('built\\n')\n"
                                "print('build ok')\n"
                            ),
                        ),
                        ("TEST", python_probe("print('tests pass')")),
                    )
                },
                limits=sandbox.SandboxLimits(wall_seconds=30),
            )
        )
        if outputs is None:
            outputs = [ModelOutput("SEMANTIC_EVALUATION", {
                "subject_reference": f"{run_id}-root",
                "verdict": "PASS",
                "rationale": "looks reasonable",
            })]
        self.completion = completion or ModelCompletion(
            outputs=tuple(outputs), cost=3.0, units="COST_UNITS",
            model_id="model-x", model_version="1",
        )
        self.output_count = len(outputs)
        self.client = ScriptedModelClient(
            {f"{run_id}-root": self.completion},
            max_costs={f"{run_id}-root": max_cost if max_cost is not None else self.completion.cost},
        )
        self.adapter = ModelAdapter(self.client, model_id="model-x", model_version="1")
        self.store = EventStore(os.path.join(root, "events.db"))
        self.controller = Controller(
            self.store,
            self.command_executor,
            model_executor=self.adapter,
            executor_environment={"base_commit": self.commit, "timeout_seconds": 30.0},
        )

    def submit(self, **overrides):
        config = build_run_config(
            self.run_id,
            root_contract={
                "description": "fixture task",
                "owned_scope": ["src/**"],
                "acceptance_command": "python3 -m fixture",
            },
            budgets={"MODEL_COST": 10.0},
            limits={
                "root_model_input_tokens": 1000,
                "root_model_output_tokens": 1000,
                "model_calls": 20,
            },
            **overrides,
        )
        self.root_node = self.controller.submit_run(config)
        return self.root_node

    def close(self):
        self.store.close()


@pytest.fixture
def harness(tmp_path, capabilities):
    if not capabilities.profile_available:
        pytest.fail(f"NAMESPACE_V1 unavailable (fail-closed gate): {capabilities.unavailable_reason()}")
    model = Harness(str(tmp_path))
    yield model
    model.close()


@pytest.fixture(scope="module")
def capabilities():
    return sandbox.capabilities(refresh=True)


# 1. One reasoning-model adapter whose every output is typed and rejected at
# admission when it violates its type schema, provenance, authority, scope, or
# policy compatibility — including the adversarial executor-record-governs case.


def test_model_adapter_typed_outputs_validated_at_admission(harness):
    run_id = harness.run_id
    root = harness.submit()
    attempt, response = harness.controller.run_model_review(run_id, root, 5.0)
    state = harness.store.state(run_id)
    outputs = [o for o in state.model_outputs.values() if o.attempt_id == attempt]
    assert len(outputs) == 1
    assert outputs[0].output_type == "SEMANTIC_EVALUATION"
    assert outputs[0].state == "ADMITTED"
    # Real usage flowed into the ledger.
    assert response["cost_report"]["actual"] == 3.0
    assert harness.controller.get_costs(run_id)["total_charged"] == 3.0

    # Admission rejections: each violates exactly one rule and persists nothing.
    state = harness.store.state(run_id)
    base = {
        "output_type": "SEMANTIC_EVALUATION",
        "output": {"subject_reference": "x", "verdict": "PASS", "rationale": "ok"},
        "provenance": {
            "model_id": "model-x",
            "model_version": "1",
            "call_id": attempt,
            "prompt_hash": harness.controller._prompt_hashes[root],
            "policy_version": 1,
        },
    }
    from orchestrator.model_adapter import ModelOutputRejected

    def rejected(mutate):
        payload = json.loads(json.dumps(base))
        mutate(payload)
        with pytest.raises(ModelOutputRejected):
            model_adapter.validate_output(state, payload)

    rejected(lambda p: p["output"].pop("rationale"))                      # schema
    rejected(lambda p: p["provenance"].__setitem__("model_id", "other"))  # producer authority
    rejected(lambda p: p["provenance"].__setitem__("prompt_hash", "sha256:other"))  # provenance
    rejected(lambda p: p["provenance"].__setitem__("policy_version", 99))  # policy compatibility
    rejected(lambda p: p.__setitem__("input_versions", {"missing": 1}))    # input versions
    rejected(lambda p: p.__setitem__("artefact_hashes", {"missing": "sha256:x"}))  # hashes
    rejected(lambda p: p["output"].__setitem__("verdict", "MAYBE"))        # unknown enum value


def test_model_adapter_delegated_scope_is_enforced(harness):
    run_id = harness.run_id
    root = harness.submit()
    attempt, _ = harness.controller.run_model_review(run_id, root, 5.0)

    out_of_scope = {
        "output_type": "OBSERVATION",
        "output": {"subject": "s", "statement": "x", "observed_references": [],
                   "paths": ["secrets/leak.txt"]},
        "provenance": {
            "model_id": "model-x", "model_version": "1", "call_id": attempt,
            "prompt_hash": harness.controller._prompt_hashes[root], "policy_version": 1,
        },
    }
    # The out-of-scope output is refused: it is recorded as a rejection (the
    # trusted reason only) and never admitted, without aborting the trial.
    admitted = harness.controller.admit_model_outputs(run_id, attempt, [out_of_scope])
    assert admitted == []
    assert "MODEL_OUTPUT_REJECTED" in [
        event.event_type for event in harness.store.history(run_id)
    ]


def test_adversarial_model_pass_cannot_override_a_failing_executor_record(harness):
    run_id = harness.run_id
    # A command attempt whose build fails: the executor record is authoritative.
    failing = Harness(
        os.path.join(harness.root, "failing"),
        run_id=run_id,
        commands={f"{run_id}-root": (("BUILD", python_probe("import sys; sys.exit(1)")),)},
    )
    try:
        root = failing.submit()
        attempt = failing.controller.execute_attempt(run_id, root, 5.0)
        state = failing.store.state(run_id)
        assert state.attempts[attempt].state == "FAILED"
        response = failing.controller._dispatch_responses[attempt]
        visible = acceptance_checks.VisibleAcceptanceChecker().run(response["observations"])
        assert visible.all_passed() is False
        evidence_id = failing.controller.admit_evidence(run_id, root, visible)

        # The model asserts a passing command; the assertion is admitted but is
        # not evidence.
        model_attempt, _ = failing.controller.run_model_review(run_id, root, 5.0)
        model_outputs = [
            o.output_id for o in failing.store.state(run_id).model_outputs.values()
        ]
        assert model_outputs
        with pytest.raises(AcceptanceRefused):
            failing.controller.authorise_acceptance(
                run_id, root, evidence_ids=[evidence_id], model_output_ids=model_outputs
            )
        result = failing.store.state(run_id)
        assert result.nodes[root].state != "ACCEPTED"
        assert result.outcome is None
    finally:
        failing.close()


# 2. Enforceable cost caps: real usage into the ledger with reclamation; a
# commitment that cannot cover the declared maximum is refused before dispatch;
# exhaustion mid-call settles conservatively.


def test_model_cost_reclamation_refusal_and_exhaustion(harness):
    run_id = harness.run_id
    root = harness.submit()
    harness.controller.run_model_review(run_id, root, 5.0)
    state = harness.store.state(run_id)
    # Cost 3 of a 5 commitment: actual 3, reclamation 2 recorded.
    assert state.ledger.total_charged == 3.0
    reclamations = [e.amount for e in state.ledger.entries.values() if e.kind == "RECLAMATION"]
    assert 2.0 in reclamations
    assert state.ledger.available(f"{root}-budget") == 7.0

    # A declared maximum above the commitment is refused before dispatch.
    tight = Harness(
        os.path.join(harness.root, "tight"),
        run_id=run_id,
        completion=ModelCompletion(
            outputs=(ModelOutput("OBSERVATION", {"subject": "s", "statement": "x",
                                                 "observed_references": []}),),
            cost=1.0, units="COST_UNITS", model_id="model-x", model_version="1",
        ),
        max_cost=10.0,
    )
    try:
        tight_root = tight.submit()
        with pytest.raises(BudgetExhausted):
            tight.controller.run_model_review(run_id, tight_root, 5.0)
        assert tight.store.state(run_id).attempts == {}
        assert any(
            e.event_type == "BUDGET_EXHAUSTION_RECORDED" for e in tight.store.history(run_id)
        )
    finally:
        tight.close()

    # Usage above the commitment is reported in full, no outputs are admitted,
    # the attempt is FAILED, and the excess is explicit overrun liability.
    over = Harness(
        os.path.join(harness.root, "over"),
        run_id=run_id,
        completion=ModelCompletion(
            outputs=(ModelOutput("OBSERVATION", {"subject": "s", "statement": "x",
                                                 "observed_references": []}),),
            cost=9.0, units="COST_UNITS", model_id="model-x", model_version="1",
        ),
        max_cost=5.0,  # declared max within the commitment; usage overran it
    )
    try:
        over_root = over.submit()
        attempt, _ = over.controller.run_model_review(run_id, over_root, 5.0)
        state = over.store.state(run_id)
        assert state.ledger.total_charged == 9.0  # reported actual, not clipped
        assert state.ledger.total_overrun == 4.0
        assert state.attempts[attempt].state == "FAILED"
        assert not [o for o in state.model_outputs.values() if o.attempt_id == attempt]
    finally:
        over.close()


# 3. Independent visible and hidden acceptance-check interfaces sharing one
# evidence admission path; hidden content never reaches a workload or a prompt.


def test_visible_and_hidden_acceptance_checks_share_the_evidence_path(harness):
    run_id = harness.run_id
    root = harness.submit()
    attempt = harness.controller.execute_attempt(run_id, root, 5.0)
    response = harness.controller._dispatch_responses[attempt]

    visible = acceptance_checks.VisibleAcceptanceChecker().run(response["observations"])
    assert visible.all_passed()
    visible_id = harness.controller.admit_evidence(run_id, root, visible)

    def hidden_check(tree_path):
        return os.path.exists(os.path.join(tree_path, "src", "app.txt"))

    hidden = acceptance_checks.HiddenAcceptanceChecker({"fixture_hidden": hidden_check}).run(
        os.path.join(harness.root, "repo")
    )
    assert hidden.check_class == "HIDDEN" and hidden.all_passed()
    hidden_id = harness.controller.admit_evidence(run_id, root, hidden)

    state = harness.store.state(run_id)
    assert state.evidence[visible_id].check_class == "VISIBLE"
    assert state.evidence[hidden_id].check_class == "HIDDEN"

    # Hidden context is stripped before prompt construction.
    context = [
        model_adapter.context_item("src/app.txt", 1, "visible content"),
        model_adapter.context_item("hidden-tests/secret.py", 1, "SECRET ORACLE"),
    ]
    filtered = model_adapter.filter_context(context)
    assert [item.reference for item in filtered] == ["src/app.txt"]
    prompt, prompt_hash = model_adapter.build_prompt({"description": "d"}, filtered)
    assert "SECRET ORACLE" not in prompt
    assert prompt_hash.startswith("sha256:")


# 4. Repeated-failure detection feeding REPLAN_REQUIRED or infeasibility, crash
# recovery with a model executor, and model-cost accounting settlement.


def test_repeated_failure_detection_and_model_recovery(harness):
    run_id = harness.run_id
    root = harness.submit()
    # Two failures with the same normalised fingerprint: no new evidence.
    harness.controller.record_failure(
        run_id, root, action_type="EXECUTE", diagnostic_category="BUILD_FAILED"
    )
    harness.controller.record_failure(
        run_id, root, action_type="EXECUTE", diagnostic_category="BUILD_FAILED"
    )
    assert (
        harness.controller.evaluate_repeated_failure(run_id, root, replan_material_change=True)
        == "REPLAN_REQUIRED"
    )
    assert harness.store.state(run_id).nodes[root].state == "REPLAN_REQUIRED"
    assert (
        harness.controller.evaluate_repeated_failure(run_id, root, replan_material_change=False)
        == "INFEASIBLE"
    )
    assert harness.store.state(run_id).nodes[root].state == "INFEASIBLE"

    # Crash recovery with a model executor: an interrupted call recovers as
    # ATTEMPT_FAILED_ON_RECOVERY with conservative settlement, and the model is
    # never re-invoked.
    from orchestrator.store import CrashInjected

    recovery = Harness(os.path.join(harness.root, "recovery"), run_id="RUN-M4-R")
    try:
        rec_root = recovery.submit()
        attempt = recovery.controller.plan_attempt(
            "RUN-M4-R", rec_root, 5.0, action_type="REVIEW"
        )
        recovery.store.crash_after_commit = lambda event: (
            (_ for _ in ()).throw(CrashInjected("crash"))
            if event.event_type == "ACTION_DISPATCHED"
            else None
        )
        with pytest.raises(CrashInjected):
            recovery.controller.dispatch_attempt("RUN-M4-R", attempt, rec_root)
        invocations = recovery.client
        recovery.store.close()

        restarted_store = EventStore(os.path.join(recovery.root, "events.db"))
        try:
            restarted = Controller(
                restarted_store,
                recovery.command_executor,
                model_executor=recovery.adapter,
                executor_environment={"base_commit": recovery.commit, "timeout_seconds": 30.0},
            )
            assert restarted.recover("RUN-M4-R") == [attempt]
            state = restarted_store.state("RUN-M4-R")
            assert state.attempts[attempt].state == "FAILED"
            assert state.attempts[attempt].accounting_state == "FINALLY_SETTLED"
            # The model was never invoked for the interrupted call.
            assert (
                restarted_store.replay_fresh("RUN-M4-R").attempts[attempt].state == "FAILED"
            )
        finally:
            restarted_store.close()
    finally:
        pass


# 5. Transition-table and policy-completeness checks extended over the new
# model-output events.


def test_transition_table_and_policy_completeness_over_model_output_events():
    import sys

    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "scripts"))
    import tables as scripts_tables

    assert "model-output-lifecycle.yaml" in scripts_tables.MACHINES
    data = scripts_tables.load_yaml("model-output-lifecycle.yaml")
    assert scripts_tables.find_unreachable_states(data) == []
    assert scripts_tables.check_transition_ambiguity("model-output-lifecycle.yaml", data) == []
    from orchestrator import policy

    assert policy.validate_policy_completeness_rows() == ()
    assert policy.validate_policy_registry() is None


# 6. Replay reproduces state, ledger, and outcome without re-invoking the model.


def test_replay_does_not_reinvoke_the_model(harness):
    run_id = harness.run_id
    root = harness.submit()
    attempt, _ = harness.controller.run_model_review(run_id, root, 5.0)
    state = harness.store.state(run_id)
    invocations = len(harness.adapter._responses)
    replayed = harness.store.replay_fresh(run_id)
    assert replayed.ledger.total_charged == state.ledger.total_charged
    assert set(replayed.model_outputs) == set(state.model_outputs)
    assert all(o.state == "ADMITTED" for o in replayed.model_outputs.values())
    assert len(harness.adapter._responses) == invocations


# Retry eligibility is derived by the retry policy and recorded with its
# version; the guarded alternative's retry-prohibited branch is reachable from
# the acceptance path.


def test_retry_eligibility_is_derived_and_recorded(harness):
    run_id = harness.run_id
    root = harness.submit()
    eligibility = harness.controller.retry_eligibility(run_id, root)
    assert eligibility["retry_eligible"] is True
    assert eligibility["retry_policy_version"] == 1

    # Two terminal attempts reach retries_per_action_lineage (2).
    failing = Harness(
        os.path.join(harness.root, "derived"),
        run_id=run_id,
        commands={f"{run_id}-root": (("BUILD", python_probe("import sys; sys.exit(1)")),)},
    )
    try:
        derived_root = failing.submit()
        failing.controller.execute_attempt(run_id, derived_root, 2.0)
        failing.controller.execute_attempt(run_id, derived_root, 2.0)
        state = failing.store.state(run_id)
        assert sum(1 for a in state.attempts.values() if a.state == "FAILED") == 2

        # A model asserts PASS while the visible record fails; the emitted
        # VALIDATION_FAILED must carry the *derived* ineligible decision and
        # select the REPLAN_REQUIRED branch of the guarded alternative.
        model_attempt, _ = failing.controller.run_model_review(run_id, derived_root, 5.0)
        outputs = [
            o.output_id for o in failing.store.state(run_id).model_outputs.values()
        ]
        response = failing.controller._dispatch_responses[
            [a for a in state.attempts.values() if a.action_type == "EXECUTE"][0].attempt_id
        ]
        visible = acceptance_checks.VisibleAcceptanceChecker().run(response["observations"])
        evidence_id = failing.controller.admit_evidence(run_id, derived_root, visible)
        with pytest.raises(AcceptanceRefused):
            failing.controller.authorise_acceptance(
                run_id, derived_root, evidence_ids=[evidence_id], model_output_ids=outputs
            )
        failed_events = [
            e for e in failing.store.history(run_id) if e.event_type == "VALIDATION_FAILED"
        ]
        assert failed_events[-1].payload["retry_eligible"] is False
        assert failed_events[-1].payload["retry_policy_version"] == 1
        assert failing.store.state(run_id).nodes[derived_root].state == "REPLAN_REQUIRED"
    finally:
        failing.close()


def test_build_prompt_filters_hidden_content_itself():
    context = [
        model_adapter.context_item("src/app.txt", 1, "visible content"),
        model_adapter.context_item("hidden-tests/secret.py", 1, "SECRET ORACLE"),
        # A hidden result re-packaged under a non-hidden reference is still
        # dropped by its visibility class.
        model_adapter.context_item("notes/summary.txt", 1, "hidden oracle body",
                                   visibility="HIDDEN"),
    ]
    prompt, prompt_hash = model_adapter.build_prompt({"description": "d"}, context)
    assert "visible content" in prompt
    assert "SECRET ORACLE" not in prompt
    assert "hidden oracle body" not in prompt
    assert prompt_hash.startswith("sha256:")


# End-to-end: the model plans, real sandboxed commands execute, and acceptance
# is decided by the executor record, not by the model assertion.


def test_model_plans_then_sandboxed_execution_reaches_evidence_bound_acceptance(harness):
    run_id = harness.run_id
    root = harness.submit()
    # A model PROPOSAL output (recorded, not recursively planned).
    proposal = Harness(
        os.path.join(harness.root, "proposal"),
        run_id=run_id,
        outputs=[ModelOutput("PROPOSAL", {
            "proposal_kind": "PLAN",
            "summary": "add the health endpoint",
            "based_on": [],
        })],
    )
    try:
        proposal_root = proposal.submit()
        proposal.controller.run_model_review(run_id, proposal_root, 5.0)
        proposed = proposal.store.state(run_id)
        assert all(o.state == "ADMITTED" for o in proposed.model_outputs.values())

        # Real sandboxed execution through the command executor.
        attempt = proposal.controller.execute_attempt(run_id, proposal_root, 5.0)
        response = proposal.controller._dispatch_responses[attempt]
        visible = acceptance_checks.VisibleAcceptanceChecker().run(response["observations"])
        evidence_id = proposal.controller.admit_evidence(run_id, proposal_root, visible)

        # A model semantic evaluation is consumed but not authoritative.
        model_attempt, _ = proposal.controller.run_model_review(run_id, proposal_root, 5.0)
        outputs = [
            o.output_id
            for o in proposal.store.state(run_id).model_outputs.values()
            if o.attempt_id == model_attempt
        ]
        proposal.controller.authorise_acceptance(
            run_id,
            proposal_root,
            evidence_ids=[evidence_id],
            result_id=f"{attempt}-result",
            model_output_ids=outputs,
        )
        final = proposal.store.state(run_id)
        assert final.attempts[attempt].state == "RESULT_PRODUCED"
        assert final.nodes[proposal_root].state == "ACCEPTED"
        assert final.outcome == "ACCEPTANCE_CLAIMED"
        # Consumption is recorded; authority came from the visible evidence.
        assert any(o.state == "CONSUMED" for o in final.model_outputs.values())
    finally:
        proposal.close()
