# SPDX-License-Identifier: AGPL-3.0-or-later
"""Regression tests for the milestone-8 readiness review findings.

Each test names the finding it pins down so a future change cannot silently
reintroduce the reviewed defect.
"""
from __future__ import annotations

import json
import os
import subprocess
import sys

import pytest

from helpers import build_run_config

from orchestrator import (
    executor_protocol,
    model_adapter,
    reducer,
    secrets,
    tables,
)
from orchestrator.controller import Controller, RunLimitExhausted, WorkflowError
from orchestrator.executor import ScriptedExecutor
from orchestrator.executor_protocol import ProtocolViolation
from orchestrator.secrets import Credential
from orchestrator.store import EventStore
from orchestrator.worktree import GIT_ENVIRONMENT, OutOfScopeError, WorktreeManager, _git_environment, snapshot

from test_milestone3_acceptance import build_fixture_repo


# ---------------------------------------------------------------------------
# R01 / R07 / R21: trusted import, scope binding, durable candidate object
# ---------------------------------------------------------------------------


def _fixture_repo_with_protected_content(root: str):
    repo, _ = build_fixture_repo(root)
    os.makedirs(os.path.join(repo, "hidden-tests"))
    with open(os.path.join(repo, "hidden-tests", "secret.py"), "w") as handle:
        handle.write("SECRET = 1\n")
    os.makedirs(os.path.join(repo, "src", "real"))
    with open(os.path.join(repo, "src", "real", "x.txt"), "w") as handle:
        handle.write("x\n")
    os.symlink("real", os.path.join(repo, "src", "linkdir"))
    with open(os.path.join(repo, ".gitignore"), "w") as handle:
        handle.write("*.ignored\n")
    environment = _git_environment(GIT_ENVIRONMENT)
    for args in (["add", "-A"], ["commit", "-q", "-m", "protected"]):
        subprocess.run(["git", "-C", repo, *args], env=environment, check=True, capture_output=True)
    commit = subprocess.run(
        ["git", "-C", repo, "rev-parse", "HEAD"],
        env=environment,
        capture_output=True,
        text=True,
        check=True,
    ).stdout.strip()
    return repo, commit



class _FirstBadClient(model_adapter.ModelClient):
    def __init__(self):
        self.calls = 0

    def max_cost(self, request):
        return 1.0

    def complete(self, request):
        self.calls += 1
        if self.calls == 1:
            return model_adapter.ModelCompletion(
                outputs=(), cost=float("nan"), units="USD",
                model_id="model-x", model_version="1", input_tokens=1, output_tokens=1,
            )
        return model_adapter.ModelCompletion(
            outputs=(), cost=1.0, units="USD",
            model_id="model-x", model_version="1", input_tokens=1, output_tokens=1,
        )


def test_candidate_git_metadata_is_rejected_before_import(tmp_path):
    repo, commit = _fixture_repo_with_protected_content(str(tmp_path))
    manager = WorktreeManager(
        repository=repo,
        worktree_root=str(tmp_path / "wt"),
        artefact_root=str(tmp_path / "art"),
    )
    tree = manager.materialise(commit, "WT-R01")
    base = snapshot(tree)
    # A candidate-controlled .git/config must never reach trusted Git.
    os.makedirs(os.path.join(tree, ".git"))
    with open(os.path.join(tree, ".git", "config"), "w") as handle:
        handle.write("[core]\n\thooksPath = .\n")
    with pytest.raises(ProtocolViolation):
        manager.create_candidate(
            base_commit=commit,
            tree_path=tree,
            base_manifest=base,
            worktree_id="WT-R01",
            scope_include=["src/**"],
            scope_exclude=[".git/**", ".orchestrator/**", "hidden-tests/**"],
        )


def test_import_preserves_excluded_content_and_persists_candidate(tmp_path):
    repo, commit = _fixture_repo_with_protected_content(str(tmp_path))
    manager = WorktreeManager(
        repository=repo,
        worktree_root=str(tmp_path / "wt"),
        artefact_root=str(tmp_path / "art"),
    )
    tree = manager.materialise(commit, "WT-R07")
    base = snapshot(tree)
    assert not os.path.exists(os.path.join(tree, "hidden-tests"))
    with open(os.path.join(tree, "src", "app.txt"), "w") as handle:
        handle.write("changed\n")
    with open(os.path.join(tree, "src", "output.ignored"), "w") as handle:
        handle.write("ignored but committed\n")
    record = manager.create_candidate(
        base_commit=commit,
        tree_path=tree,
        base_manifest=base,
        worktree_id="WT-R07",
        scope_include=["src/**"],
        scope_exclude=[".git/**", ".orchestrator/**", "hidden-tests/**"],
    )
    assert record["import_verified"] is True
    assert set(record["changed_paths"]) >= {"src/app.txt", "src/output.ignored"}
    integration = os.path.join(str(tmp_path / "wt"), "WT-R07-integration")
    # Excluded base content and directory symlinks survive the import.
    assert os.path.exists(os.path.join(integration, "hidden-tests", "secret.py"))
    assert os.path.islink(os.path.join(integration, "src", "linkdir"))
    # Ignored candidate content is committed, not silently dropped by git add.
    committed = subprocess.run(
        ["git", "-C", integration, "ls-tree", "-r", "--name-only", "HEAD"],
        capture_output=True, text=True, check=True,
    ).stdout.split()
    assert "src/output.ignored" in committed
    # R21: the verified object graph is bundled before cleanup.
    assert os.path.exists(record["bundle_path"])


def test_import_handles_file_and_directory_kind_swaps(tmp_path):
    """A file replaced by a directory (and vice versa) must import and verify,
    not fail with a filesystem error mid-import."""
    import shutil as _shutil

    repo, _ = build_fixture_repo(str(tmp_path))
    os.makedirs(os.path.join(repo, "src", "pkg"))
    with open(os.path.join(repo, "src", "pkg", "m.py"), "w") as handle:
        handle.write("m\n")
    environment = _git_environment(GIT_ENVIRONMENT)
    for args in (["add", "-A"], ["commit", "-q", "-m", "pkg"]):
        subprocess.run(["git", "-C", repo, *args], env=environment, check=True, capture_output=True)
    commit = subprocess.run(
        ["git", "-C", repo, "rev-parse", "HEAD"],
        env=environment,
        capture_output=True,
        text=True,
        check=True,
    ).stdout.strip()
    manager = WorktreeManager(
        repository=repo,
        worktree_root=str(tmp_path / "wt"),
        artefact_root=str(tmp_path / "art"),
    )
    tree = manager.materialise(commit, "WT-KIND")
    base = snapshot(tree)
    # src/app.txt (file) becomes a directory; src/pkg (directory) becomes a file.
    os.remove(os.path.join(tree, "src", "app.txt"))
    os.makedirs(os.path.join(tree, "src", "app.txt"))
    with open(os.path.join(tree, "src", "app.txt", "x"), "w") as handle:
        handle.write("x\n")
    _shutil.rmtree(os.path.join(tree, "src", "pkg"))
    with open(os.path.join(tree, "src", "pkg"), "w") as handle:
        handle.write("now a file\n")
    record = manager.create_candidate(
        base_commit=commit,
        tree_path=tree,
        base_manifest=base,
        worktree_id="WT-KIND",
        scope_include=["src/**"],
        scope_exclude=[".git/**", ".orchestrator/**", "hidden-tests/**"],
    )
    assert record["import_verified"] is True
    integration = os.path.join(str(tmp_path / "wt"), "WT-KIND-integration")
    assert os.path.isfile(os.path.join(integration, "src", "app.txt", "x"))
    assert os.path.isfile(os.path.join(integration, "src", "pkg"))


def test_out_of_scope_candidate_is_refused(tmp_path):
    repo, commit = _fixture_repo_with_protected_content(str(tmp_path))
    manager = WorktreeManager(
        repository=repo,
        worktree_root=str(tmp_path / "wt"),
        artefact_root=str(tmp_path / "art"),
    )
    tree = manager.materialise(commit, "WT-SCOPE")
    base = snapshot(tree)
    os.makedirs(os.path.join(tree, "hidden-tests"), exist_ok=True)
    with open(os.path.join(tree, "hidden-tests", "leak.py"), "w") as handle:
        handle.write("leak\n")
    with pytest.raises(OutOfScopeError):
        manager.create_candidate(
            base_commit=commit,
            tree_path=tree,
            base_manifest=base,
            worktree_id="WT-SCOPE",
            scope_include=["src/**"],
            scope_exclude=[".git/**", ".orchestrator/**", "hidden-tests/**"],
        )


# ---------------------------------------------------------------------------
# R03: frozen run limits are enforced before the next billable action
# ---------------------------------------------------------------------------


def _scripted_controller(tmp_path, run_id, **overrides):
    store = EventStore(str(tmp_path / f"{run_id}.db"))
    controller = Controller(store, ScriptedExecutor({}))
    config = build_run_config(run_id, **overrides)
    root = controller.submit_run(config)
    return store, controller, root


def test_action_limit_refuses_the_second_action(tmp_path):
    store, controller, root = _scripted_controller(
        tmp_path, "RUN-LIMIT", limits={"actions": 1}
    )
    try:
        controller.plan_attempt("RUN-LIMIT", root, 1.0)
        with pytest.raises(RunLimitExhausted):
            controller.plan_attempt("RUN-LIMIT", root, 1.0)
    finally:
        store.close()


def test_node_limit_refuses_decomposition_before_creation(tmp_path):
    store, controller, root = _scripted_controller(
        tmp_path, "RUN-NODES", limits={"nodes": 1}
    )
    try:
        children = [
            {
                "node_id": "RUN-NODES-child",
                "mandatory": True,
                "criterion_ids": ["AC-001"],
                "budget_allocation": {"MODEL_COST": 1.0},
                "contract": {
                    "description": "child",
                    "owned_scope": ["src/child/**"],
                    "acceptance_criteria": [{"id": "AC-001", "mandatory": True}],
                },
            }
        ]
        with pytest.raises(WorkflowError):
            controller.admit_derived_decomposition("RUN-NODES", root, "d1", children, [])
    finally:
        store.close()


# ---------------------------------------------------------------------------
# R04: cancellation rejects new work and preserves dispatched liability
# ---------------------------------------------------------------------------


def test_cancelled_run_rejects_new_work(tmp_path):
    store, controller, root = _scripted_controller(tmp_path, "RUN-CANCEL")
    try:
        controller.cancel_run("RUN-CANCEL", root)
        assert controller.store.state("RUN-CANCEL").outcome == "CANCELLED"
        with pytest.raises(WorkflowError):
            controller.plan_attempt("RUN-CANCEL", root, 1.0)
    finally:
        store.close()


# ---------------------------------------------------------------------------
# R05: redaction covers mapping keys
# ---------------------------------------------------------------------------


def test_redaction_covers_mapping_keys():
    credential = Credential(
        credential_id="c1", scope="model", secret="sekret-value", expires_at=0.0
    )
    payload = {"sekret-value": "value", "nested": [{"sekret-value": "x"}]}
    redacted = secrets.redact_deep(payload, (credential,))
    assert "sekret-value" not in json.dumps(redacted)


# ---------------------------------------------------------------------------
# R12: a mismatched submitted policy version is rejected
# ---------------------------------------------------------------------------


def test_policy_version_mismatch_is_rejected(tmp_path):
    from orchestrator import events

    store = EventStore(str(tmp_path / "policy.db"))
    controller = Controller(store, ScriptedExecutor({}))
    config = build_run_config("RUN-POLICY", policy_version=999)
    try:
        with pytest.raises(events.UnsupportedPolicyVersion):
            controller.submit_run(config)
    finally:
        store.close()


# ---------------------------------------------------------------------------
# R17: scalar artefact paths are validated as paths, not characters
# ---------------------------------------------------------------------------


def _model_state_with_artefact():
    import test_milestone4_conformance as conformance

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


def test_scalar_artefact_path_in_scope_is_accepted():
    state = _model_state_with_artefact()
    payload = {
        "output_id": "o1",
        "attempt_id": "a1",
        "output_type": "ARTEFACT",
        "output": {"reference": "art-1", "content_hash": "sha256:h", "media_type": "text/x-python", "path": "src/app.py"},
        "provenance": {
            "model_id": "model-x",
            "model_version": "1",
            "call_id": "a1",
            "prompt_hash": "sha256:prompt",
            "policy_version": 1,
        },
    }
    model_adapter.validate_output(state, payload)


# ---------------------------------------------------------------------------
# R19: mandatory parent criteria cannot be credited to an optional child
# ---------------------------------------------------------------------------


def test_optional_parent_criterion_is_not_required_in_mandatory_coverage(tmp_path):
    """R19: only mandatory parent criteria must appear in mandatory coverage."""
    from orchestrator import decomposition

    store = EventStore(str(tmp_path / "mixed.db"))
    controller = Controller(store, ScriptedExecutor({}))
    try:
        config = build_run_config("RUN-MIXED")
        config["root_contract"] = {
            **config["root_contract"],
            "acceptance_criteria": [
                {"id": "AC-001", "mandatory": True},
                {"id": "AC-OPT", "mandatory": False},
            ],
        }
        root = controller.submit_run(config)
        children = [
            {
                "node_id": "RUN-MIXED-m",
                "mandatory": True,
                "criterion_ids": ["AC-001"],
                "budget_allocation": {"MODEL_COST": 1.0},
                "contract": {
                    "description": "mandatory",
                    "owned_scope": ["src/m/**"],
                    "acceptance_criteria": [{"id": "AC-001", "mandatory": True}],
                    "budget_id": "RUN-MIXED-m-budget",
                },
            }
        ]
        state = controller.store.state("RUN-MIXED")
        proposal = {
            "node_id": root,
            "children": children,
            "criterion_coverage": [
                {"criterion_id": "AC-001", "child_id": "RUN-MIXED-m", "mandatory_cover": True}
            ],
            "dependencies": [],
            "integration_owner": "RUN-MIXED-m",
        }
        # The optional criterion is intentionally absent from mandatory
        # coverage; validation must still succeed.
        decomposition.validate_decomposition(proposal, state)
    finally:
        store.close()


def test_mandatory_criterion_cannot_be_covered_by_optional_child(tmp_path):
    from orchestrator import decomposition

    store = EventStore(str(tmp_path / "coverage.db"))
    controller = Controller(store, ScriptedExecutor({}))
    try:
        config = build_run_config("RUN-COV")
        config["root_contract"] = {
            **config["root_contract"],
            "acceptance_criteria": [{"id": "AC-001", "mandatory": True}],
        }
        root = controller.submit_run(config)
        children = [
            {
                "node_id": "RUN-COV-opt",
                "mandatory": False,
                "criterion_ids": ["AC-001"],
                "budget_allocation": {"MODEL_COST": 1.0},
                "contract": {
                    "description": "optional",
                    "owned_scope": ["src/opt/**"],
                    "acceptance_criteria": [{"id": "AC-001", "mandatory": True}],
                    "budget_id": "RUN-COV-opt-budget",
                },
            }
        ]
        state = controller.store.state("RUN-COV")
        proposal = {
            "node_id": root,
            "children": children,
            "criterion_coverage": [
                {"criterion_id": "AC-001", "child_id": "RUN-COV-opt", "mandatory_cover": True}
            ],
            "dependencies": [],
            "integration_owner": "RUN-COV-opt",
        }
        with pytest.raises(decomposition.DecompositionError):
            decomposition.validate_decomposition(proposal, state)
    finally:
        store.close()


# ---------------------------------------------------------------------------
# R22: an acknowledgement timer is disarmed once acknowledged
# ---------------------------------------------------------------------------


def test_acknowledgement_timer_is_disarmed(tmp_path):
    from test_milestone2_acceptance import make_controller, child_script

    store = EventStore(str(tmp_path / "timer.db"))
    controller = make_controller(store, {"RUN-TIMER-root": child_script(1.0, "sha256:r")})
    try:
        root = controller.submit_run(build_run_config("RUN-TIMER"))
        attempt_id = controller.execute_attempt("RUN-TIMER", root, 5.0)
        state = controller.store.state("RUN-TIMER")
        timer = next(iter(state.timers.values()))
        assert timer.disarmed is True
        state_before = state.attempts[attempt_id].state
        # An obsolete firing must not change the acknowledged attempt.
        controller._emit(
            "RUN-TIMER",
            "TIMER_FIRED",
            {"timer_id": timer.timer_id, "attempt_id": attempt_id,
             "configured_deadline": timer.deadline,
             "observed_firing_time": timer.deadline},
            f"timer-fired-{attempt_id}",
            source="timer",
        )
        assert controller.store.state("RUN-TIMER").attempts[attempt_id].state == state_before
    finally:
        store.close()


# ---------------------------------------------------------------------------
# R16: competing proposals cannot both pass revalidation
# ---------------------------------------------------------------------------


def test_competing_proposals_cannot_both_revalidate(tmp_path):
    import threading

    from orchestrator.controller import VersionConflict
    from orchestrator.proposals import Proposal

    store = EventStore(str(tmp_path / "race.db"))
    controller = Controller(store, ScriptedExecutor({}))
    try:
        root = controller.submit_run(build_run_config("RUN-RACE"))
        version = controller.store.state("RUN-RACE").nodes[root].version
        barrier = threading.Barrier(2)
        outcomes = []
        lock = threading.Lock()

        def worker(proposal_id):
            barrier.wait()
            proposal = Proposal(
                proposal_id=proposal_id,
                event_type="NODE_MARKED_INFEASIBLE",
                payload={"node_id": root},
                node_id=root,
                expected_versions={"node": {root: version}},
            )
            try:
                controller.propose("RUN-RACE", proposal)
                result = "ok"
            except VersionConflict:
                result = "conflict"
            except Exception as error:  # noqa: BLE001
                result = type(error).__name__
            with lock:
                outcomes.append(result)

        threads = [threading.Thread(target=worker, args=(f"p{i}",)) for i in range(2)]
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()
        assert sorted(outcomes) == ["conflict", "ok"], outcomes
    finally:
        store.close()


# ---------------------------------------------------------------------------
# Batch 1 (SPEND): enforceable token ceilings and prompt binding
# ---------------------------------------------------------------------------


class _CountingModelClient(model_adapter.ModelClient):
    """Records every completion request so a test can assert zero billable
    invocations, and declares an explicit token ceiling."""

    def __init__(self, completion, *, max_cost, max_tokens):
        self.completion = completion
        self._max_cost = max_cost
        self._max_tokens = max_tokens
        self.requests = []

    def max_cost(self, request):
        return self._max_cost

    def max_tokens(self, request):
        return self._max_tokens

    def complete(self, request):
        self.requests.append(dict(request))
        return self.completion


def _token_controller(tmp_path, run_id, client, **config_overrides):
    from orchestrator.model_adapter import ModelAdapter

    store = EventStore(str(tmp_path / f"{run_id}.db"))
    adapter = ModelAdapter(client, model_id="model-x", model_version="1")
    controller = Controller(
        store,
        ScriptedExecutor({}),
        model_executor=adapter,
        executor_environment={"base_commit": "HEAD", "timeout_seconds": 30.0},
    )
    config = build_run_config(run_id, **config_overrides)
    root = controller.submit_run(config)
    return store, controller, root


def test_token_ceiling_refuses_before_any_billable_call(tmp_path):
    completion = model_adapter.ModelCompletion(
        outputs=(model_adapter.ModelOutput("SEMANTIC_EVALUATION", {
            "subject_reference": "x", "verdict": "PASS", "rationale": "ok"}),),
        cost=1.0, units="COST_UNITS", model_id="model-x", model_version="1",
        input_tokens=50, output_tokens=10,
    )
    client = _CountingModelClient(completion, max_cost=1.0, max_tokens=(50, 10))
    store, controller, root = _token_controller(
        tmp_path, "RUN-TOK",
        client,
        limits={"root_model_input_tokens": 40, "root_model_output_tokens": 5},
    )
    try:
        with pytest.raises(RunLimitExhausted):
            controller.plan_attempt("RUN-TOK", root, 1.0, action_type="REVIEW")
        # The ceiling is enforced before the priced client is invoked at all.
        assert client.requests == []
    finally:
        store.close()


def test_reported_token_overrun_fails_without_admitting_outputs(tmp_path):
    completion = model_adapter.ModelCompletion(
        outputs=(model_adapter.ModelOutput("SEMANTIC_EVALUATION", {
            "subject_reference": "x", "verdict": "PASS", "rationale": "ok"}),),
        cost=1.0, units="COST_UNITS", model_id="model-x", model_version="1",
        input_tokens=50, output_tokens=45,
    )
    client = _CountingModelClient(completion, max_cost=1.0, max_tokens=(10, 10))
    store, controller, root = _token_controller(
        tmp_path, "RUN-OVR",
        client,
        limits={"root_model_input_tokens": 20, "root_model_output_tokens": 20},
    )
    try:
        _attempt, response = controller.run_model_review("RUN-OVR", root, 1.0)
        assert response["self_reported_status"] == "FAILED"
        assert response["model_outputs"] == []
        assert response["cost_report"]["budget_exhausted"] is True
        assert response["cost_report"]["token_overrun"] == {"input": 30, "output": 25}
        # The overrun is preserved in the durable observation, not clipped.
        state = controller.store.state("RUN-OVR")
        assert [o for o in state.model_outputs.values()] == []
    finally:
        store.close()


def test_prompt_reaches_client_and_hidden_context_is_filtered(tmp_path):
    completion = model_adapter.ModelCompletion(
        outputs=(model_adapter.ModelOutput("SEMANTIC_EVALUATION", {
            "subject_reference": "x", "verdict": "PASS", "rationale": "ok"}),),
        cost=1.0, units="COST_UNITS", model_id="model-x", model_version="1",
        input_tokens=1, output_tokens=1,
    )
    client = _CountingModelClient(completion, max_cost=1.0, max_tokens=(1, 1))
    store, controller, root = _token_controller(
        tmp_path, "RUN-PROMPT", client,
        limits={"root_model_input_tokens": 1000, "root_model_output_tokens": 1000},
    )
    try:
        context = (
            model_adapter.context_item("src/visible.py", 1, "VISIBLE-BODY"),
            model_adapter.context_item("src/secret.py", 1, "SECRET-BODY", visibility="HIDDEN"),
        )
        controller.run_model_review("RUN-PROMPT", root, 1.0, context=context)
        assert len(client.requests) == 1
        sent = client.requests[0]
        assert "VISIBLE-BODY" in sent["prompt"]
        assert "SECRET-BODY" not in sent["prompt"]
        import hashlib as _hashlib

        expected = "sha256:" + _hashlib.sha256(sent["prompt"].encode()).hexdigest()
        assert sent["prompt_hash"] == expected
        assert sent["prompt_hash"] == controller._prompt_hashes[root]
    finally:
        store.close()


# ---------------------------------------------------------------------------
# Batch 2 (SPEND): experiment-wide authorized cap, durable accounting, binding
# ---------------------------------------------------------------------------


def _authorization(limit=1000.0, owner="owner-1", at="2026-01-01T00:00:00Z"):
    from orchestrator import spend

    return spend.ExperimentAuthorization(
        owner=owner, limit=limit, authorized_at=at, method="owner-decision"
    )


def test_experiment_wide_cap_refuses_before_a_trial_starts(tmp_path):
    from orchestrator import experiment, spend

    workdir = str(tmp_path)
    target = experiment.build_fixed_target(workdir)
    oracle_manifest = experiment.build_hidden_oracle(target)
    ledger = spend.open_spend_ledger(workdir, _authorization(limit=1.0))
    with pytest.raises(spend.SpendCapExceeded):
        experiment.run_pilots(target, oracle_manifest, workdir, spend_ledger=ledger)
    # The over-cap trial never created its durable state.
    assert not os.path.exists(
        os.path.join(workdir, "trials", f"{experiment.ARM_DIRECT}-{experiment.PILOT_SEED}")
    )


def test_spend_ledger_is_durable_and_refuses_a_changed_authorization(tmp_path):
    from orchestrator import spend

    workdir = str(tmp_path)
    ledger = spend.open_spend_ledger(workdir, _authorization(limit=100.0))
    ledger.reserve("EXP-A-1", 100.0, identity={"arm": "A", "seed": 1})
    ledger.settle("EXP-A-1", 40.0, status="CORRECT_ACCEPTANCE")
    # A second reservation that is never settled (an interrupted trial) remains
    # outstanding liability.
    ledger.reserve("EXP-A-2", 30.0, identity={"arm": "A", "seed": 2})
    assert ledger.committed == 40.0
    assert ledger.reserved() == 30.0
    assert ledger.available() == 30.0

    # Reopening preserves consumed capacity and the outstanding reservation.
    reopened = spend.open_spend_ledger(workdir, _authorization(limit=100.0))
    assert reopened.committed == 40.0
    assert reopened.reserved() == 30.0
    assert reopened.available() == 30.0
    # A different authorization cannot widen or rename the ceiling.
    with pytest.raises(spend.SpendLedgerError):
        spend.open_spend_ledger(workdir, _authorization(limit=1000.0))
    with pytest.raises(spend.SpendLedgerError):
        spend.open_spend_ledger(workdir, _authorization(limit=100.0, owner="someone-else"))


def test_spend_gate_requires_authorization_and_ledger(tmp_path):
    from orchestrator import experiment

    workdir = str(tmp_path)
    target = experiment.build_fixed_target(workdir)
    oracle_manifest = experiment.build_hidden_oracle(target)
    # The paid entry points require an explicit authorization/ledger.
    with pytest.raises(TypeError):
        experiment.run_pilots(target, oracle_manifest, workdir)
    with pytest.raises(TypeError):
        experiment.run_experiment(workdir)


def test_frozen_manifest_mappings_are_immutable_and_hash_stable():
    from orchestrator import experiment

    source = {"sampling": {"temperature": 0.2, "top_p": [0.9, 0.95]}}
    frozen = experiment._deep_freeze(source)
    # The frozen copy is read-only and cannot drift the manifest hash.
    with pytest.raises(TypeError):
        frozen["sampling"]["temperature"] = 0.9
    with pytest.raises((TypeError, AttributeError)):
        frozen["sampling"]["top_p"].append(0.5)
    assert experiment._content_hash(frozen) == experiment._content_hash(source)
    # The caller's original mapping is unaffected.
    assert source["sampling"]["temperature"] == 0.2


# ---------------------------------------------------------------------------
# SPEND-gate adversarial review regressions (F1-F10)
# ---------------------------------------------------------------------------


def test_single_flight_holds_when_completion_is_malformed(tmp_path):
    """F1: one (attempt, token) invokes the priced client once even when the
    completion fails post-call validation."""
    import threading

    from orchestrator.model_adapter import ModelAdapter, ModelBudgetRefused, ModelCompletion

    client = _FirstBadClient()
    adapter = ModelAdapter(client, model_id="model-x", model_version="1")
    token = adapter.issue_token("a1")
    request = {
        "run_id": "R", "action_id": "A", "attempt_id": "a1", "attempt_token": token,
        "node_id": "n1", "action_type": "REVIEW", "resolved_inputs": [],
        "budget_commitment": {"dimension": "MODEL_COST", "hard_limit": 5},
        "policy_version": 1, "prompt_hash": "sha256:p",
    }
    barrier = threading.Barrier(2)
    outcomes = []

    def worker():
        barrier.wait()
        try:
            adapter.dispatch(request)
            outcomes.append("ok")
        except ModelBudgetRefused:
            outcomes.append("refused")

    threads = [threading.Thread(target=worker) for _ in range(2)]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()
    assert client.calls == 1, "the priced client was invoked more than once"
    assert outcomes == ["refused", "refused"]


def test_cancellation_does_not_launder_a_reported_actual(tmp_path):
    """F2: cancelling an attempt with a durable reported actual keeps the
    liability instead of resetting it to zero."""
    store = EventStore(str(tmp_path / "cancel.db"))
    controller = Controller(
        store, ScriptedExecutor({"R-root": lambda request: {"self_reported_status": "COMPLETED"}})
    )
    try:
        node_id = controller.submit_run(build_run_config("R"))
        attempt = controller.plan_attempt("R", node_id, 5.0)
        controller.dispatch_attempt("R", attempt, node_id)
        controller._emit(
            "R",
            "EXECUTOR_OBSERVATION_ADMITTED",
            {"attempt_id": attempt, "observation": {"cost_report": {"committed": 5, "actual": 9.0, "units": "USD"}}},
            f"observe-{attempt}",
            source="executor",
        )
        controller._emit(
            "R", "ATTEMPT_EXECUTION_ENDED_UNRECONCILED", {"attempt_id": attempt}, f"end-{attempt}"
        )
        controller.cancel_run("R", node_id)
        state = store.state("R")
        assert state.ledger.total_charged == 9.0
        assert state.ledger.total_overrun == 4.0
        assert state.attempts[attempt].accounting_state == "FINALLY_SETTLED"
    finally:
        store.close()


def test_root_replacement_transfer_cannot_move_spent_budget():
    """F3: only unspent capacity may move between budgets."""
    from orchestrator.model import BudgetRecord, Ledger, OvercommitmentError

    ledger = Ledger()
    ledger.register_budget(BudgetRecord("src", "n1", {"MODEL_COST": 10.0}))
    ledger.register_budget(BudgetRecord("dst", "n2", {"MODEL_COST": 0.0}))
    ledger.reserve("e1", "src", 8.0, "MODEL_COST", "a1", "n1")
    ledger.commit_reservation("a1", 8.0, "MODEL_COST")
    ledger.settle("a1", 8.0, 0.0)
    with pytest.raises(OvercommitmentError):
        ledger.transfer("src", "dst", {"MODEL_COST": 10.0})
    ledger.transfer("src", "dst", {"MODEL_COST": 2.0})
    assert ledger.available("dst", "MODEL_COST") == 2.0


def test_run_trial_requires_a_spend_ledger(tmp_path):
    """F5: the shared trial primitive cannot start without a reservation."""
    import inspect

    from orchestrator import experiment

    parameter = inspect.signature(experiment.run_trial).parameters["spend_ledger"]
    assert parameter.default is inspect.Parameter.empty


def test_zero_declared_token_ceiling_is_bounded_but_may_overshoot_one_call(tmp_path):
    """F6 residual (deferred): a client declaring (0, 0) declares no bound, so
    the run ceiling is still enforced but can be overshot by one call before the
    next request is refused.

    Closing this fully requires either the client to declare its per-call
    ceiling or a calibration that leaves the aggregate token ceiling headroom;
    both change the frozen pilot-calibration contract, so the residual is pinned
    here rather than silently dropped."""
    from orchestrator.controller import BudgetExhausted

    completion = model_adapter.ModelCompletion(
        outputs=(),
        cost=1.0, units="COST_UNITS", model_id="model-x", model_version="1",
        input_tokens=100, output_tokens=20,
    )
    client = _CountingModelClient(completion, max_cost=1.0, max_tokens=(0, 0))
    store, controller, root = _token_controller(
        tmp_path, "RUN-ZERO", client,
        limits={"root_model_input_tokens": 100, "root_model_output_tokens": 100},
    )
    try:
        controller.run_model_review("RUN-ZERO", root, 1.0)
        raised = False
        for _ in range(4):
            try:
                controller.run_model_review("RUN-ZERO", root, 1.0)
            except BudgetExhausted:
                raised = True
                break
        assert raised, "the aggregate token ceiling must eventually refuse"
        # Bounded: the ceiling can be overshot by one call, then refuses.
        assert len(client.requests) <= 3
        consumed = controller._consumed_model_tokens("RUN-ZERO")
        assert consumed["input_tokens"] <= 300
    finally:
        store.close()


def test_token_shaped_completion_units_are_refused(tmp_path):
    """F7: a token-shaped unit cannot be billed against a monetary commitment."""
    from orchestrator.controller import BudgetExhausted

    completion = model_adapter.ModelCompletion(
        outputs=(), cost=1.0, units="MODEL_TOKENS",
        model_id="model-x", model_version="1", input_tokens=1, output_tokens=1,
    )
    client = _CountingModelClient(completion, max_cost=1.0, max_tokens=(1, 1))
    store, controller, root = _token_controller(
        tmp_path, "RUN-UNITS", client,
        limits={"root_model_input_tokens": 100, "root_model_output_tokens": 100},
    )
    try:
        # The token-shaped unit fails the call closed; the controller records the
        # dispatch failure and the attempt does not complete.
        with pytest.raises(BudgetExhausted):
            controller.run_model_review("RUN-UNITS", root, 1.0)
        assert len(client.requests) == 1
        state = controller.store.state("RUN-UNITS")
        assert [o for o in state.model_outputs.values()] == []
    finally:
        store.close()


def test_settle_accepts_actual_within_tolerance_of_commitment():
    """F9: a rounded settle at the commitment boundary is not a failure."""
    from orchestrator.model import BudgetRecord, Ledger, TOLERANCE

    for delta in (0.0, TOLERANCE * 0.5, TOLERANCE):
        ledger = Ledger()
        ledger.register_budget(BudgetRecord("B", "n1", {"MODEL_COST": 5.0}))
        ledger.reserve("e", "B", 5.0, "MODEL_COST", "a1", "n1")
        ledger.commit_reservation("a1", 5.0, "MODEL_COST")
        ledger.settle("a1", 5.0 + delta, 0.0)
    ledger = Ledger()
    ledger.register_budget(BudgetRecord("B", "n1", {"MODEL_COST": 5.0}))
    ledger.reserve("e", "B", 5.0, "MODEL_COST", "a2", "n1")
    ledger.commit_reservation("a2", 5.0, "MODEL_COST")
    ledger.settle("a2", 5.0 + TOLERANCE * 100, 0.0)
    assert ledger.total_overrun > 0.0


def test_frozen_hash_is_equal_for_sets_and_bytearrays():
    """F10: freeze normalisation preserves the content hash and removes
    remaining mutability."""
    from orchestrator import experiment

    source = {"s": {1, 2, 3}, "b": bytearray(b"xy")}
    frozen = experiment._deep_freeze(source)
    assert experiment._content_hash(frozen) == experiment._content_hash(source)
    with pytest.raises((TypeError, AttributeError)):
        frozen["b"].append(0x7A)
    assert isinstance(frozen["b"], bytes)


def _concurrent_reserve_worker(workdir, trial_id, barrier, results):
    """Module-level worker so the spawn multiprocessing context can import it."""
    from orchestrator import spend

    try:
        ledger = spend.open_spend_ledger(workdir, _authorization(limit=10.0))
        barrier.wait(timeout=10)
        ledger.reserve(trial_id, 8.0)
        results.put((trial_id, "reserved"))
    except spend.SpendCapExceeded:
        results.put((trial_id, "refused"))
    except Exception as error:  # noqa: BLE001 - report the failure to the parent
        results.put((trial_id, f"error:{type(error).__name__}"))


def test_spend_ledger_survives_concurrent_reservations(tmp_path):
    """F8: two processes reserving against one workdir cannot lose a
    reservation or exceed the ceiling."""
    import multiprocessing
    from orchestrator import spend

    workdir = str(tmp_path / "experiment")
    context = multiprocessing.get_context("spawn")
    barrier = context.Barrier(2)
    results = context.Queue()

    processes = [
        context.Process(target=_concurrent_reserve_worker, args=(workdir, f"t{i}", barrier, results))
        for i in range(2)
    ]
    for process in processes:
        process.start()
    for process in processes:
        process.join(timeout=20)
        assert process.exitcode == 0
    outcomes = [results.get(timeout=5) for _ in range(2)]
    accepted = [trial for trial, status in outcomes if status == "reserved"]
    assert len(accepted) == 1, outcomes
    reopened = spend.open_spend_ledger(workdir, _authorization(limit=10.0))
    assert reopened.reserved() == 8.0
    assert reopened.reserved() <= reopened.authorization.limit
