# SPDX-License-Identifier: AGPL-3.0-or-later
"""The eight acceptance demonstrations of the seventh roadmap milestone.

Each test is named after and documents one bullet listed under
"Seventh milestone: post-experiment decisions and production hardening" in
docs/roadmap.md. The milestone resolves the experiment's deferred design
questions on the recorded evidence and hardens the reference implementation;
nothing here changes committed machinery semantics.
"""

from __future__ import annotations

import json
import os
import time

import pytest

from helpers import open_test_spend_ledger

from orchestrator import (
    conformance,
    decisions,
    events,
    model_adapter,
    model_provider,
    observability,
    reducer,
    sandbox,
    secrets,
)
from orchestrator.controller import Controller, WorkflowError
from orchestrator.experiment import (
    ARM_ORDER,
    TRIAL_SEEDS,
    freeze_manifest,
    run_measured_comparison,
)
from orchestrator.executor import ScriptedExecutor
from orchestrator.model import RunState
from orchestrator.reducer import ReducerError
from orchestrator.store import CrashInjected, EventStore
from orchestrator import tables
from orchestrator.conformance import TOOLCHAIN_PATHS
from helpers import build_run_config


def _completed_script(run_id):
    def script(request):
        return {
            "self_reported_status": "COMPLETED",
            "artefacts": [{"type": "GIT_CANDIDATE", "content_hash": "sha256:abc123"}],
            "observations": [{"type": "COMMAND_RESULT", "command_id": "BUILD", "exit_code": 0}],
            "cost_report": {"committed": 5, "actual": 5, "units": "USD"},
        }

    return script


def _run_config(run_id):
    return {
        "schema_version": 1,
        "run_id": run_id,
        "root_contract": {
            "description": "fixture task",
            "owned_scope": ["src/**"],
            "acceptance_command": "python3 -m fixture",
        },
        "executor_profile": "scripted-fixture-v1",
        "budgets": {"MODEL_COST": 100.0},
        "policy_version": 1,
        "project_kernel": {
            "schema_version": 1,
            "kernel_id": "KERNEL-1",
            "repository_commit": "base",
            "frozen_at": "2026-09-19T00:00:00Z",
        },
        "experiment_manifest": {
            "schema_version": 1,
            "manifest_id": "MANIFEST-1",
            "manifest_hash": "sha256:deadbeef",
        },
    }


# ---------------------------------------------------------------------------
# Shared fixtures: one reduced frozen experiment (single seed per arm) and one
# full conformance suite run. The frozen manifest and the gate are identical in
# kind to the milestone-6 experiment; a single trial per arm keeps the module
# fast while exercising the same frozen pipeline end to end.
# ---------------------------------------------------------------------------


@pytest.fixture(scope="module")
def workdir(tmp_path_factory):
    return str(tmp_path_factory.mktemp("m7-experiment"))


@pytest.fixture(scope="module")
def suite_report():
    return conformance.run_full_suite()


@pytest.fixture(scope="module")
def experiment_result(workdir, experiment_prefix):
    """A frozen-manifest measured comparison with one seed per arm, run behind
    the same conformance gate as the full experiment.

    The immutable prefix (target, oracle, calibration, pilots) is session-shared
    (performance plan A3); this module owns only its own workdir, spend ledger,
    one-seed comparison, and gate report."""
    if not sandbox.capabilities().profile_available:
        pytest.fail("the experiment refuses to run unsandboxed")
    spend_ledger = open_test_spend_ledger(workdir)
    manifest = freeze_manifest(
        target=experiment_prefix.target,
        oracle_manifest=experiment_prefix.oracle,
        limits=experiment_prefix.pilots.calibrated_limits,
        calibration=experiment_prefix.calibration,
        pilot_calibration_method=experiment_prefix.pilots.method,
        trial_seeds=(TRIAL_SEEDS[0],),
        arm_assignment=tuple((arm, (TRIAL_SEEDS[0],)) for arm in ARM_ORDER),
    )
    comparison = run_measured_comparison(
        manifest, experiment_prefix.oracle, workdir, spend_ledger=spend_ledger
    )
    gate_report = conformance.run_full_suite()
    return {
        "manifest": manifest,
        "comparison": comparison,
        "gate_passed": gate_report.passed,
        "gate_failures": gate_report.failures(),
    }


# 1. The measured comparison is interpreted and recorded: per-arm structural
# findings from the frozen manifest runs, the explicit limit of scripted
# evidence, and for each deferred decision the conditions under which a
# real-model run would change it.


@pytest.mark.slow
def test_measured_comparison_is_interpreted_and_recorded_with_structural_findings(
    experiment_result,
):
    manifest = experiment_result["manifest"]
    comparison = experiment_result["comparison"]
    interpretation = decisions.interpret_comparison(manifest, comparison)

    # The interpretation is bound to the frozen manifest it interprets.
    assert interpretation.manifest_hash == comparison.manifest_hash == manifest.content_hash()
    # The scripted-evidence limit is explicit: structure, not model behaviour.
    assert interpretation.evidence_limit == decisions.SCRIPTED_EVIDENCE_LIMIT
    assert "differentiates orchestration structure, not model behaviour" in (
        interpretation.evidence_limit
    )
    # The manifest's own unmeasured-metrics disclosure is frozen and
    # non-empty: the manifest itself declares what the scripted client cannot
    # measure.
    assert manifest.unmeasured_metrics

    findings = {finding.arm: finding for finding in interpretation.arms}
    assert set(findings) == set(ARM_ORDER)
    # DIRECT: no decomposition; everything else decomposes.
    assert findings["DIRECT"].mean_nodes == 1.0
    assert any("undecomposed" in statement for statement in
               findings["DIRECT"].structural_findings)
    assert findings["FIXED_ONE_LEVEL"].mean_nodes > 1.0
    assert any("fixed one-level tree" in statement for statement in
               findings["FIXED_ONE_LEVEL"].structural_findings)
    for recursive in ("RECURSIVE_LAZY", "RECURSIVE_CONCURRENT"):
        assert findings[recursive].mean_nodes > 1.0
        assert any("decomposition recurs" in statement for statement in
                   findings[recursive].structural_findings)
    # Concurrency is a measured structural fact: the concurrent arm schedules
    # independent actions alongside each other; the sequential arms do not.
    assert findings["RECURSIVE_CONCURRENT"].mean_ready_action_concurrency > 0
    assert findings["RECURSIVE_LAZY"].mean_ready_action_concurrency == 0
    assert findings["DIRECT"].mean_ready_action_concurrency == 0
    # Every recorded finding is grounded in the recorded summary numbers.
    for finding in interpretation.arms:
        summary = comparison.arm(finding.arm)
        assert finding.mean_nodes == summary.mean_nodes
        assert finding.mean_depth == summary.mean_depth
        assert (
            finding.mean_ready_action_concurrency == summary.mean_ready_action_concurrency
        )
        assert finding.realised_speedup == summary.mean_realised_speedup

    # Every deferred decision is recorded with its resolution and the
    # conditions under which a real-model run would change it.
    for decision in interpretation.decisions:
        assert decision.question and decision.resolution and decision.basis
        assert decision.real_model_conditions, decision.decision_id
    resolved = {decision.decision_id for decision in interpretation.decisions
                if decision.status == "RESOLVED"}
    assert resolved == {
        "EVENT_STORE",
        "DISPATCH_SUBSTRATE",
        "OBSERVABILITY_BACKEND",
        "HUMAN_APPROVAL_SURFACE",
        "SECRETS_MANAGEMENT",
        "MODEL_PROVIDER_ABSTRACTION",
    }
    # The store and dispatch decisions are resolved in favour of the retained
    # production substrate, on demonstrated requirements.
    assert decisions.deferred_decision("EVENT_STORE").resolution.startswith(
        "SQLite in WAL mode is retained"
    )
    assert decisions.deferred_decision("DISPATCH_SUBSTRATE").resolution.startswith(
        "In-process dispatch is retained"
    )


# 2. The production event-store decision: SQLite in WAL mode is retained; the
# single-writer transactional append with atomic event-plus-state persistence
# is preserved, nothing normative depends on store-specific behaviour, and the
# invariant is proven against the chosen store.


def test_production_event_store_decision_keeps_sqlite_wal_with_the_atomic_append_invariant(
    tmp_path,
):
    decision = decisions.deferred_decision("EVENT_STORE")
    assert decision.status == "RESOLVED"
    assert "nothing normative depends on store-specific behaviour" in decision.resolution

    db_path = str(tmp_path / "events.db")
    store = EventStore(db_path)
    try:
        # The chosen store runs SQLite in WAL mode.
        mode = store._conn.execute("PRAGMA journal_mode").fetchone()[0]
        assert mode.lower() == "wal"

        run_id = "r1"
        controller = Controller(store, ScriptedExecutor({f"{run_id}-root": _completed_script(run_id)}))
        root = controller.submit_run(_run_config(run_id))
        attempt = controller.plan_attempt(run_id, root, 5.0)

        def assert_log_determines_state():
            """State content is a pure function of the event sequence: folding
            the durable log through the pure reducer reproduces the store's
            state projection exactly. ``graph_version`` is the store-assigned
            state counter persisted atomically with each event row; it replays
            deterministically from the log, so it is asserted separately."""
            folded = RunState(run_id=run_id)
            for event in store.history(run_id):
                folded, _ = reducer.reduce(folded, event)
            replayed = store.replay_fresh(run_id)
            assert replayed.graph_version == store.state(run_id).graph_version
            from orchestrator.conformance import first_difference, state_fingerprint

            expected = state_fingerprint(replayed)
            actual = state_fingerprint(folded)
            expected.pop("graph_version")
            actual.pop("graph_version")
            difference = first_difference(expected, actual)
            assert difference is None

        assert_log_determines_state()
        token = controller.dispatch_attempt(run_id, attempt, root)
        response = controller._dispatch_responses[attempt]
        controller.admit_observation_and_result(run_id, attempt, root, token, response)
        controller.validate_and_accept(run_id, root, {"checks": ["BUILD"]}, f"{attempt}-result")
        assert_log_determines_state()

        # Crash injection at the persistence boundary against the chosen
        # store: a crash before commit leaves nothing persisted; the retry
        # with the same idempotency key succeeds cleanly.
        crash_attempt = "r1-root-attempt-crash"

        def boom(event):
            raise CrashInjected("simulated crash before commit")

        store.crash_before_commit = boom
        with pytest.raises(CrashInjected):
            store.admit(
                run_id,
                events.make_event(
                    "ATTEMPT_PLANNED",
                    {"attempt_id": crash_attempt, "node_id": root, "cause": "INITIAL"},
                    idempotency_key=f"plan-{crash_attempt}",
                ),
            )
        store.crash_before_commit = None
        assert crash_attempt not in store.state(run_id).attempts
        replayed = store.replay_fresh(run_id)
        assert crash_attempt not in replayed.attempts
        assert_log_determines_state()
        store.admit(
            run_id,
            events.make_event(
                "ATTEMPT_PLANNED",
                {"attempt_id": crash_attempt, "node_id": root, "cause": "INITIAL"},
                idempotency_key=f"plan-{crash_attempt}",
            ),
        )
        assert store.state(run_id).attempts[crash_attempt].state == "PLANNED"
        assert_log_determines_state()
    finally:
        store.close()

    # Restart from the durable log alone: everything observed above survives
    # a process boundary because it is a function of the event sequence.
    reopened = EventStore(db_path)
    try:
        replayed = reopened.replay_fresh("r1")
        assert replayed.attempts["r1-root-attempt-crash"].state == "PLANNED"
        assert reopened.state("r1").nodes["r1-root"].state == "ACCEPTED"
    finally:
        reopened.close()


# 3. The dispatch-substrate decision: in-process dispatch is retained; durable
# dispatch intent stays recorded as events; the substrate holds no normative
# state; ACTION_DISPATCHED remains durable before any executor contact, with
# the declared ATTEMPT_FAILED_ON_RECOVERY path intact.


def test_dispatch_substrate_decision_keeps_durable_event_backed_intent_and_a_stateless_substrate(
    tmp_path,
):
    decision = decisions.deferred_decision("DISPATCH_SUBSTRATE")
    assert decision.status == "RESOLVED"
    assert "the substrate holds no normative state" in decision.resolution

    db_path = str(tmp_path / "dispatch.db")
    run_id = "r1"
    store = EventStore(db_path)
    # A substrate that dies on contact: the durable intent must already be
    # recorded when the executor is reached.
    class DyingExecutor(ScriptedExecutor):
        def dispatch(self, request):
            raise RuntimeError("substrate died on contact")

    try:
        controller = Controller(store, DyingExecutor({f"{run_id}-root": _completed_script(run_id)}))
        root = controller.submit_run(_run_config(run_id))
        attempt = controller.plan_attempt(run_id, root, 5.0)
        with pytest.raises(RuntimeError):
            controller.dispatch_attempt(run_id, attempt, root)
        history = [event.event_type for event in store.history(run_id)]
        assert history.count("ACTION_DISPATCHED") == 1
        assert store.state(run_id).attempts[attempt].state == "DISPATCHED"
    finally:
        store.close()

    # Recovery with a freshly constructed in-process substrate: it holds no
    # normative state, so the declared ATTEMPT_FAILED_ON_RECOVERY path runs
    # from the durable log alone.
    restarted_store = EventStore(db_path)
    try:
        restarted_controller = Controller(
            restarted_store, ScriptedExecutor({f"{run_id}-root": _completed_script(run_id)})
        )
        recovered = restarted_controller.recover(run_id)
        assert recovered == [attempt]
        state = restarted_store.state(run_id)
        assert state.attempts[attempt].state == "FAILED"
        assert state.attempts[attempt].accounting_state == "FINALLY_SETTLED"
        assert restarted_controller.get_costs(run_id)["total_charged"] == 5
        recovery_events = [
            event.event_type for event in restarted_store.history(run_id)
        ]
        assert recovery_events.count("ATTEMPT_FAILED_ON_RECOVERY") == 1
    finally:
        restarted_store.close()


# 4. The observability backend over the audit output: run history, ledger,
# evidence, and oracle grades queryable beyond the harness API, with the
# retention and data-classification policy recorded; read-only, with no new
# mutation path.


def test_observability_backend_queries_audit_output_read_only(tmp_path):
    run_id = "OBS-RUN"
    store = EventStore(os.path.join(tmp_path, "obs.db"))
    controller = Controller(store, ScriptedExecutor({f"{run_id}-root": _completed_script(run_id)}))
    source = secrets.SecretsSource()
    credential = source.issue("EXECUTOR_BOUNDARY", credential_id="obs-cred", ttl_seconds=60)
    try:
        root = controller.submit_run(_run_config(run_id))
        attempt = controller.execute_attempt(run_id, root, 5.0)
        controller.validate_and_accept(run_id, root, {"checks": ["BUILD"]}, f"{attempt}-result")
        # A visible and a hidden evidence record: the hidden one is an oracle
        # grade recorded through the standing evidence path.
        controller._emit(run_id, "EVIDENCE_ADMITTED",
                         {"evidence_id": f"{run_id}-ev-visible", "node_id": root,
                          "check_class": "VISIBLE", "checks": [{"name": "BUILD", "passed": True}]},
                         f"evidence-visible-{run_id}")
        controller._emit(run_id, "EVIDENCE_ADMITTED",
                         {"evidence_id": f"{run_id}-ev-hidden", "node_id": root,
                          "check_class": "HIDDEN",
                          "checks": [{"name": "hidden-behaviour", "passed": True}]},
                         f"evidence-hidden-{run_id}")
        controller.issue_credential(run_id, credential)

        backend = observability.ObservabilityBackend(store)
        before_state = conformance.state_fingerprint(store.state(run_id))
        before_history = [event.to_record() for event in store.history(run_id)]

        assert backend.runs() == [run_id]
        history = backend.run_history(run_id)
        assert [row["event_type"] for row in history][:2] == ["NODE_PROPOSED", "NODE_ADMITTED"]
        filtered = backend.run_history(run_id, event_type="CREDENTIAL_ISSUED")
        assert [row["payload"]["credential_id"] for row in filtered] == ["obs-cred"]
        assert backend.run_history(run_id, attempt_id=attempt)[0]["event_type"] != "RUN_CONFIGURED"
        # An accepted root records the ACCEPTANCE_CLAIMED outcome over the
        # ACCEPTED terminal node state (docs/glossary.md#run-outcomes).
        assert backend.run_outcome(run_id)["outcome"] == "ACCEPTANCE_CLAIMED"
        node_rows = backend.nodes(run_id)
        assert node_rows[0]["node_id"] == root
        assert node_rows[0]["state"] == "ACCEPTED"
        assert any(row["attempt_id"] == attempt for row in backend.attempts(run_id))
        # Ledger and evidence are queryable with their classifications.
        ledger = backend.ledger(run_id)
        assert ledger["total_charged"] == controller.get_costs(run_id)["total_charged"]
        evidence = backend.evidence(run_id)
        classes = {row["check_class"]: row for row in evidence}
        assert classes["VISIBLE"]["classification"] == "INTERNAL"
        assert classes["HIDDEN"]["classification"] == "RESTRICTED"
        grades = backend.oracle_grades(run_id)
        assert grades == [
            {
                "evidence_id": f"{run_id}-ev-hidden",
                "node_id": root,
                "outcome": "PASS",
                "classification": "RESTRICTED",
                "trial_classification": None,
                "oracle_version": None,
                "manifest_hash": None,
                "candidate_commit": None,
                "initial_outcomes": {},
                "effective_outcomes": {},
                "rerun_eligible": [],
                "rerun_spent": False,
                "controller_outcome": None,
                "checks": [{"name": "hidden-behaviour", "passed": True}],
            }
        ]
        # Credential records expose the digest only; the raw value is
        # classified SECRET and is never queryable.
        credential_rows = backend.credentials(run_id)
        assert credential_rows[0]["classification"] == "RESTRICTED"
        assert credential_rows[0]["redacted_digest"] == credential.redacted_digest
        assert all(row["redacted_digest"] != credential.secret for row in credential_rows)
        blob = json.dumps(backend.run_history(run_id))
        assert credential.secret not in blob

        # The retention and data-classification policy is recorded.
        policy = backend.retention_policy
        assert policy.authoritative_event_log == "RETAINED_FULLY"
        assert policy.projections == "REBUILDABLE_PROJECTIONS"
        assert policy.credentials == "DIGEST_ONLY"
        assert backend.data_classification["raw_credentials"] == "SECRET"
        assert backend.data_classification["evidence_hidden"] == "RESTRICTED"

        # Read-only: a full query pass leaves state, history, and replay
        # byte-identical, and the backend exposes no mutation path.
        after_state = conformance.state_fingerprint(store.state(run_id))
        after_history = [event.to_record() for event in store.history(run_id)]
        assert before_state == after_state
        assert before_history == after_history
        assert store.replay_fresh(run_id).approvals == {}
        for forbidden in ("admit", "emit", "append", "record", "write", "commit"):
            assert not any(
                name == forbidden or name.startswith(f"{name}_")
                for name in dir(backend)
            ), f"backend must not expose a {name!r} mutation path"
    finally:
        store.close()


# 5. The human-approval integration surface: approvals admitted as typed
# events through the admission path with source-authority, schema, and policy
# validation — never an out-of-band override — with timeout and escalation
# paths recorded as events.


def test_human_approvals_are_admitted_as_typed_events_through_the_admission_path(tmp_path):
    run_id = "APPROVAL-RUN"
    store = EventStore(os.path.join(tmp_path, "approvals.db"))
    controller = Controller(store, ScriptedExecutor({f"{run_id}-root": _completed_script(run_id)}))
    try:
        root = controller.submit_run(_run_config(run_id))
        # A real result must exist for the deferred acceptance to bind: the
        # authoritative guard now rejects acceptance of a nonexistent result.
        attempt = controller.execute_attempt(run_id, root, 5.0)
        controller._emit(run_id, "VALIDATION_STARTED", {"node_id": root}, f"vs-{root}")
        controller._emit(run_id, "VALIDATION_PASSED",
                         {"node_id": root, "evidence": {"checks": ["BUILD"]}}, f"vp-{root}")

        # Acceptance that requires approval is deferred, not overridden.
        controller.request_acceptance_approval(
            run_id, root, approval_id="ap-1", deadline=99.0,
            evidence={"checks": ["BUILD"]}, result_id=f"{attempt}-result",
        )
        state = store.state(run_id)
        assert state.nodes[root].state == "BLOCKED"
        assert state.approvals["ap-1"].state == "PENDING"

        # An out-of-band override fails: acceptance authorisation is not a
        # legal transition while the node is BLOCKED, so nothing but the
        # admitted-grant path can accept the node.
        with pytest.raises(tables.UnknownTransition):
            store.admit(run_id, events.make_event(
                "ACCEPTANCE_AUTHORISED",
                {"node_id": root, "bindings": {"acceptance_id": "override"}},
                idempotency_key="override",
            ))

        # A controller-sourced decision is rejected before it can persist:
        # the approval authority's decision is the only admitted form.
        with pytest.raises(events.InvalidSourceAuthority):
            store.admit(run_id, events.make_event(
                "APPROVAL_GRANTED",
                {"approval_id": "ap-1", "decision_authority": "controller"},
                idempotency_key="self-grant",
            ))
        assert "APPROVAL_GRANTED" not in [event.event_type for event in store.history(run_id)]

        # Timeout is an event; escalation is an event; only the escalated
        # authority's admitted decision is valid.
        # Timeout is an event, and only after the recorded deadline has
        # actually passed; escalation is an event; only the escalated
        # authority's admitted decision is valid.
        controller.request_approval(run_id, root, approval_id="ap-2-early",
                                    subject="BUDGET_EXCEPTION", deadline=time.time() + 3600.0)
        with pytest.raises(WorkflowError):
            controller.fire_approval_timeout(run_id, "ap-2-early")
        assert store.state(run_id).approvals["ap-2-early"].state == "PENDING"
        controller.fire_approval_timeout(run_id, "ap-1")
        assert store.state(run_id).approvals["ap-1"].state == "TIMED_OUT"
        controller.escalate_approval(run_id, "ap-1", escalated_to="duty-manager")
        assert store.state(run_id).approvals["ap-1"].state == "ESCALATED"
        with pytest.raises(ReducerError):
            controller.admit_approval_decision(run_id, "ap-1", "GRANTED",
                                               decision_authority="human-1")
        controller.admit_approval_decision(run_id, "ap-1", "GRANTED",
                                           decision_authority="duty-manager")
        state = store.state(run_id)
        assert state.approvals["ap-1"].state == "GRANTED"
        # The grant resumes the deferred acceptance on its own legal path:
        # readiness, re-validation of the recorded evidence, authorisation.
        assert state.nodes[root].state == "ACCEPTED"

        # A denial closes the request without granting it: the gated decision
        # stays where it was, and nothing is accepted by the denial.
        controller.request_approval(run_id, root, approval_id="ap-2",
                                    subject="BUDGET_EXCEPTION", deadline=50.0)
        controller.admit_approval_decision(run_id, "ap-2", "DENIED",
                                           decision_authority="human-1")
        assert store.state(run_id).approvals["ap-2"].state == "DENIED"
        assert store.state(run_id).nodes[root].state == "ACCEPTED"

        # Every approval path was a typed admitted event.
        event_types = [event.event_type for event in store.history(run_id)]
        for expected in (
            "ACCEPTANCE_DEFERRED", "APPROVAL_REQUESTED", "APPROVAL_TIMED_OUT",
            "APPROVAL_ESCALATED", "APPROVAL_GRANTED", "APPROVAL_DENIED",
        ):
            assert event_types.count(expected) >= 1
    finally:
        store.close()


# 6. Secrets management replacing environment-only credentials: scoped
# short-lived credentials at the sandbox and executor boundaries; both
# boundaries audited against the new source; credential redaction at every
# boundary preserved and tested.


def test_secrets_management_replaces_environment_only_credentials_with_scoped_credentials(
    tmp_path,
):
    run_id = "SECRETS-RUN"
    store = EventStore(os.path.join(tmp_path, "secrets.db"))
    controller = Controller(store, ScriptedExecutor({f"{run_id}-root": _completed_script(run_id)}))
    try:
        root = controller.submit_run(_run_config(run_id))
        source = secrets.SecretsSource()
        sandbox_credential = source.issue("SANDBOX_BOUNDARY", credential_id="sb-1",
                                          ttl_seconds=120)
        executor_credential = source.issue("EXECUTOR_BOUNDARY", credential_id="eb-1",
                                           ttl_seconds=120)
        # Issuance is audited through the admission path; only digests are
        # recorded.
        controller.issue_credential(run_id, sandbox_credential)
        controller.issue_credential(run_id, executor_credential)
        blob = json.dumps([event.to_record() for event in store.history(run_id)])
        assert sandbox_credential.secret not in blob
        assert executor_credential.secret not in blob
        assert sandbox_credential.redacted_digest in blob

        # The sandbox boundary draws the credential from the source, not the
        # ambient environment: exactly the allowlist plus one scoped
        # credential, and the boundary audit attests it.
        base = {"PATH": "/usr/bin:/bin", "HOME": "/work"}
        sandbox_env = secrets.sandbox_environment_with(sandbox_credential, base)
        assert set(sandbox_env) == set(base) | {secrets.SANDBOX_CREDENTIAL_ENV_KEY}
        audit = secrets.audit_sandbox_boundary(sandbox_credential, sandbox_env,
                                               base_allowlist=base)
        assert audit["boundary"] == "SANDBOX_BOUNDARY"
        assert audit["secret_recorded"] is False
        with pytest.raises(secrets.CredentialRefused):
            secrets.audit_sandbox_boundary(executor_credential, sandbox_env,
                                           base_allowlist=base)
        with pytest.raises(secrets.CredentialRefused):
            secrets.audit_sandbox_boundary(
                sandbox_credential, {**sandbox_env, "HF_TOKEN": "ambient"},
                base_allowlist=base,
            )

        # The executor boundary: the credential travels only in redacted form,
        # and a raw secret anywhere in the payload is refused.
        request = {"attempt_token": "t", "credential_digest": executor_credential.redacted_digest}
        assert secrets.audit_executor_boundary(executor_credential, request)["boundary"] == \
            "EXECUTOR_BOUNDARY"
        with pytest.raises(secrets.CredentialRefused):
            secrets.audit_executor_boundary(
                executor_credential, {"stdout": f"key={executor_credential.secret}"}
            )

        # Redaction is wired into executor-observation admission: a response
        # that echoes the credential back is redacted before it can be
        # admitted or persisted, at the boundary itself.
        echo_run = "SECRETS-ECHO"
        echo_store = EventStore(os.path.join(tmp_path, "echo.db"))
        try:
            echo_script_result = {
                "self_reported_status": "COMPLETED",
                "artefacts": [],
                "observations": [
                    {"type": "COMMAND_RESULT", "command_id": "BUILD", "exit_code": 0,
                     "stdout": f"credential was {executor_credential.secret}"},
                ],
                "cost_report": {"committed": 5, "actual": 5, "units": "USD"},
            }

            def echo_script(request):
                return echo_script_result

            echo_controller = Controller(echo_store, ScriptedExecutor({f"{echo_run}-root": echo_script}))
            echo_root = echo_controller.submit_run(_run_config(echo_run))
            echo_controller.issue_credential(echo_run, executor_credential)
            echo_attempt = echo_controller.execute_attempt(echo_run, echo_root, 5.0)
            admitted = echo_store.history(echo_run)
            observation_events = [
                event for event in admitted if event.event_type == "EXECUTOR_OBSERVATION_ADMITTED"
            ]
            result_events = [event for event in admitted if event.event_type == "RESULT_ADMITTED"]
            blob = json.dumps(
                [event.to_record() for event in observation_events + result_events]
            )
            assert executor_credential.secret not in blob
            assert executor_credential.redacted_digest in blob
            # Replay of the same durable log is unchanged by the redaction:
            # the admitted payloads were redacted before persistence.
            replayed = echo_store.replay_fresh(echo_run)
            assert replayed.nodes[echo_root].state == "READY"
            assert replayed.credentials["eb-1"].state == "ACTIVE"
        finally:
            echo_store.close()

        # Short-lived: an expired credential is refused at its boundary.
        expired = source.issue("SANDBOX_BOUNDARY", credential_id="sb-0", ttl_seconds=1)
        assert not source.active(expired, at=expired.expires_at + 1)
        with pytest.raises(secrets.CredentialRefused):
            source.require_active(expired, at=expired.expires_at + 1)

        # Redaction at every boundary, preserved: anything a workload printed
        # is redacted against the issued credentials before it could be
        # admitted or retained.
        captured = f"probe ran with {sandbox_credential.secret} and {executor_credential.secret}"
        redacted = secrets.redact_text(captured, [sandbox_credential, executor_credential])
        assert sandbox_credential.secret not in redacted
        assert executor_credential.secret not in redacted
        assert sandbox_credential.redacted_digest in redacted

        # Rotation: a fresh credential replaces the retired one, and the
        # retirement is an event.
        replacement = source.issue("SANDBOX_BOUNDARY", credential_id="sb-2", ttl_seconds=120)
        controller.issue_credential(run_id, replacement)
        controller.retire_credential(run_id, "sb-1", reason="rotation")
        state = store.state(run_id)
        assert state.credentials["sb-1"].state == "RETIRED"
        assert state.credentials["sb-2"].state == "ACTIVE"
        event_types = [event.event_type for event in store.history(run_id)]
        assert event_types.count("CREDENTIAL_ISSUED") == 3
        assert event_types.count("CREDENTIAL_RETIRED") == 1

        # With the real sandbox available: the scoped credential crosses the
        # sandbox boundary and its value is redacted out of captured output,
        # while the allowlist still carries no ambient credentials.
        if sandbox.capabilities().profile_available:
            runner = sandbox.SandboxRunner()
            work = str(tmp_path / "work")
            new_root = str(tmp_path / "root")
            os.makedirs(work, exist_ok=True)
            program = "import os; print('CRED:' + os.environ['ORCHESTRATOR_CREDENTIAL'])"
            with open(os.path.join(work, "probe.py"), "w") as handle:
                handle.write(program)
            plan = sandbox.SandboxPlan(
                new_root=new_root,
                rw_mounts=(sandbox.MountSpec(host=work, guest="/work", readonly=False),),
                ro_mounts=tuple(
                    sandbox.MountSpec(host=path, guest=path, readonly=True)
                    for path in TOOLCHAIN_PATHS
                    if os.path.exists(path)
                ),
                tmpfs_mounts=("/tmp",),
                workdir="/work",
                command=("/usr/bin/python3", "/work/probe.py"),
                env=sandbox_env,
                limits=sandbox.SandboxLimits(wall_seconds=30),
            )
            observation = runner.run(plan)
            printed = observation.stdout.decode(errors="replace")
            assert sandbox_credential.secret in printed
            recorded = secrets.redact_text(printed, [sandbox_credential])
            assert sandbox_credential.secret not in recorded
            assert sandbox_credential.redacted_digest in recorded
    finally:
        store.close()


# 7. Model-provider abstraction groundwork for a second adapter: the
# interface, not a second implementation; no multi-model routing, no
# learning-based routing, no recursive planning on model outputs.


def test_model_provider_abstraction_groundwork_admits_a_second_adapter_interface(tmp_path):
    from orchestrator.model_adapter import ModelAdapter, ScriptedModelClient

    def first_adapter():
        return ModelAdapter(
            ScriptedModelClient({}), model_id="model-a", model_version="1"
        )

    registry = model_provider.ModelProviderRegistry()
    registry.register(model_provider.ModelProvider(
        provider_id="first", model_id="model-a", model_version="1",
        adapter_factory=first_adapter,
    ))
    # The groundwork: a second provider may be registered as an interface
    # before any implementation exists.
    registry.register(model_provider.ModelProvider(
        provider_id="second", model_id="model-b", model_version="1",
        capabilities=("IMPLEMENTATION",),
    ))
    assert [item.provider_id for item in registry.descriptors()] == ["first", "second"]

    # Selection is explicit by id; the bound adapter is THE model executor.
    adapter = registry.bind("first")
    assert isinstance(adapter, ModelAdapter)
    assert registry.bind("first") is adapter
    with pytest.raises(model_provider.UnknownProvider):
        registry.select("unregistered")
    with pytest.raises(model_provider.UnknownProvider):
        registry.bind("second")

    # No routing capability is exposed: registering a second descriptor does
    # not create multi-model routing, learning-based routing, or per-call
    # selection.
    stance = registry.routing_check()
    assert stance["routing_policy"] == model_provider.ROUTING_POLICY
    assert stance["per_call_routing"] is False
    assert stance["learning_based_routing"] is False
    assert stance["recursive_planning_on_outputs"] is False
    for forbidden in ("route", "select_for", "score", "rank"):
        assert not hasattr(registry, forbidden)

    # The controller keeps exactly one bound adapter: reasoning actions go to
    # it, everything else to the command (or scripted) executor.
    run_id = "PROVIDER-RUN"
    store = EventStore(os.path.join(tmp_path, "provider.db"))
    client = ScriptedModelClient({
        f"{run_id}-root": model_adapter.ModelCompletion(
            outputs=(model_adapter.ModelOutput(
                "SEMANTIC_EVALUATION",
                {"subject_reference": "src/app.txt", "verdict": "PASS", "rationale": "ok"},
            ),),
            cost=2.0, units="COST_UNITS", model_id="model-a", model_version="1",
        )
    })
    bound_adapter = ModelAdapter(client, model_id="model-a", model_version="1")
    controller = Controller(store, ScriptedExecutor({}), model_executor=bound_adapter)
    try:
        root = controller.submit_run(
            build_run_config(
                run_id,
                limits={
                    "root_model_input_tokens": 1000,
                    "root_model_output_tokens": 1000,
                    "model_calls": 20,
                },
            )
        )
        controller.run_model_review(run_id, root, 5.0)
        state = store.state(run_id)
        # Outputs reached state only as typed, validated MODEL_OUTPUT_ADMITTED
        # events bound to the dispatched prompt; consumption is deterministic
        # policy, never planning on outputs.
        assert len(state.model_outputs) == 1
        output = next(iter(state.model_outputs.values()))
        assert output.state == "ADMITTED"
        assert state.attempts[output.attempt_id].model_id == "model-a"
        assert controller._executor_for("REVIEW") is bound_adapter
        assert controller._executor_for("EXECUTE") is not bound_adapter
    finally:
        store.close()


# 8. Replay, conformance, and the experiment entry gate stay green throughout.


@pytest.mark.slow
def test_replay_conformance_and_the_experiment_entry_gate_stay_green(
    suite_report, experiment_result,
):
    assert experiment_result["gate_passed"] is True, experiment_result["gate_passed"]
    assert suite_report.passed, "; ".join(suite_report.failures())
    names = {check.name for check in suite_report.checks}
    # The gate covers replay equivalence, transition coverage, guarded
    # alternatives, and the crash boundaries — now including the
    # seventh-milestone corpus runs.
    for required in (
        "replay-equivalence:approvals",
        "replay-equivalence:credentials",
        "transition-coverage",
        "guarded-alternatives",
        "crash-boundaries",
        "root-outcome-mapping",
    ):
        assert required in names, f"the suite is missing {required}"
    replay_failures = [
        check for check in suite_report.checks
        if check.name.startswith("replay-equivalence:") and not check.passed
    ]
    assert not replay_failures, "; ".join(check.detail for check in replay_failures)


# 9. Oracle grades are durable: reopening a completed trial's store recovers the
# grading record and classification (R13).


@pytest.mark.slow
def test_oracle_grades_are_durable_after_reopening(workdir, experiment_result):
    arm, seed = ARM_ORDER[0], TRIAL_SEEDS[0]
    db_path = os.path.join(workdir, "trials", f"{arm}-{seed}", "events.db")
    trial = next(
        item
        for item in experiment_result["comparison"].trials
        if item.arm == arm and item.seed == seed
    )
    store = EventStore(db_path)
    try:
        backend = observability.ObservabilityBackend(store)
        run_id = f"EXP-{arm}-{seed}"
        grades = backend.oracle_grades(run_id)
        assert grades, "a reopened trial must expose its persisted oracle grades"
        assert all(grade["classification"] == "RESTRICTED" for grade in grades)
        # Raw per-check categories (not just pass booleans) survive a reopen.
        hidden = backend.evidence(run_id, check_class="HIDDEN")
        assert hidden and hidden[0]["versions"].get("initial_outcomes")
        assert "classification" in hidden[0]["versions"]
        # The exact grade and classification are reconstructed from the reopened
        # store, and they match the trial that graded this store (R13).
        record = grades[-1]
        grade = backend.reconstruct_grade(run_id)
        assert grade is not None
        assert dict(grade.outcomes) == record["initial_outcomes"]
        assert grade.oracle_version == record["oracle_version"]
        assert record["manifest_hash"] == trial.manifest_hash
        assert backend.reconstruct_classification(run_id) == trial.classification
        assert record["trial_classification"] == trial.classification
    finally:
        store.close()
