# SPDX-License-Identifier: AGPL-3.0-or-later
"""The five acceptance demonstrations of the third roadmap milestone, plus the
trusted-repository end-to-end run.

Each is named after and documents one demonstration listed under "Third
milestone: real execution — sandbox first, then the command executor" in
docs/roadmap.md. Real command execution happens inside the ``NAMESPACE_V1``
sandbox; the repository under test is a deterministic frozen fixture, never
arbitrary repository code.
"""

from __future__ import annotations

import hashlib
import json
import os
import shutil
import subprocess
import sys
import tempfile

import pytest

from orchestrator import executor_protocol, sandbox
from orchestrator.command_executor import CommandExecutor, CommandExecutorConfiguration
from orchestrator.controller import Controller
from orchestrator.store import CrashInjected, EventStore
from orchestrator.worktree import WorktreeManager, _git_environment, snapshot

from helpers import build_run_config

TOOLCHAIN_PATHS = (
    "/usr",
    "/lib",
    "/lib64",
    "/bin",
    "/sbin",
    "/etc/ld.so.cache",
    "/etc/ld.so.conf",
    "/etc/ld.so.conf.d",
    "/etc/alternatives",
    "/etc/ssl/certs",
    "/etc/nsswitch.conf",
    "/etc/passwd",
    "/etc/group",
)

GIT_ENVIRONMENT = {
    "GIT_AUTHOR_NAME": "fixture",
    "GIT_AUTHOR_EMAIL": "fixture@localhost",
    "GIT_COMMITTER_NAME": "fixture",
    "GIT_COMMITTER_EMAIL": "fixture@localhost",
    "GIT_AUTHOR_DATE": "2000-01-01T00:00:00+0000",
    "GIT_COMMITTER_DATE": "2000-01-01T00:00:00+0000",
}


def build_fixture_repo(root: str):
    """A deterministic frozen repository (trusted fixtures, not arbitrary code)."""
    repo = os.path.join(root, "repo")
    os.makedirs(os.path.join(repo, "src"))
    with open(os.path.join(repo, "src", "app.txt"), "w") as handle:
        handle.write("base\n")
    with open(os.path.join(repo, "README.md"), "w") as handle:
        handle.write("frozen fixture\n")
    environment = _git_environment(GIT_ENVIRONMENT)
    for args in (["init", "-q"], ["add", "-A"], ["commit", "-q", "-m", "base"]):
        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


def python_probe(source: str):
    return ("/usr/bin/python3", "-c", source)


class Harness:
    """Controller + CommandExecutor over a frozen fixture repository."""

    def __init__(self, root: str, *, run_id="RUN-M3", commands=None, limits=None, repo=None, commit=None):
        self.root = root
        self.run_id = run_id
        self.repo, self.commit = (repo, commit) if repo is not None else build_fixture_repo(root)
        configuration = 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=limits or sandbox.SandboxLimits(wall_seconds=30),
        )
        self.executor = CommandExecutor(configuration)
        self.store = EventStore(os.path.join(root, "events.db"))
        self.controller = Controller(
            self.store,
            self.executor,
            executor_environment={"base_commit": self.commit, "timeout_seconds": 30.0},
        )
        self.artefact_root = configuration.artefact_root
        self.worktree_root = configuration.worktree_root

    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},
            **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()}")
    harness = Harness(str(tmp_path))
    yield harness
    harness.close()


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


@pytest.fixture(scope="module")
def sandbox_runner(capabilities):
    if not capabilities.profile_available:
        pytest.fail(f"NAMESPACE_V1 unavailable (fail-closed gate): {capabilities.unavailable_reason()}")
    return sandbox.SandboxRunner(capabilities_override=capabilities)


def run_probe(sandbox_runner, source: str, limits=None):
    root = tempfile.mkdtemp(prefix="m3-probe-root-")
    work = tempfile.mkdtemp(prefix="m3-probe-work-")
    with open(os.path.join(work, "probe.py"), "w") as handle:
        handle.write(source)
    plan = sandbox.SandboxPlan(
        new_root=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={"PATH": "/usr/bin:/bin", "HOME": "/work", "LANG": "C.UTF-8"},
        limits=limits or sandbox.SandboxLimits(wall_seconds=30),
    )
    try:
        return sandbox_runner.run(plan)
    finally:
        shutil.rmtree(root, ignore_errors=True)
        shutil.rmtree(work, ignore_errors=True)


def probe_json(observation):
    for line in observation.stdout.decode(errors="replace").splitlines():
        if line.startswith("PROBE:"):
            return json.loads(line[6:])
    raise AssertionError(f"probe produced no result: {observation}")


# 1. An executable sandbox profile: path, network, syscall, and duration
# isolation around every attempt subprocess, with credential redaction.


def test_executable_sandbox_profile_path_network_syscall_duration_and_credentials(
    sandbox_runner, capabilities
):
    assert capabilities.profile_available
    observation = run_probe(
        sandbox_runner,
        "import json, os, socket\n"
        "result = {\n"
        "  'cwd': os.getcwd(),\n"
        "  'env': sorted(os.environ),\n"
        "  'write_outside': None,\n"
        "  'network': None,\n"
        "}\n"
        "try:\n"
        "    open('/etc/hostname','w').write('x')\n"
        "    result['write_outside'] = 'WROTE'\n"
        "except OSError as error:\n"
        "    result['write_outside'] = error.errno\n"
        "s = socket.socket(); s.settimeout(3)\n"
        "try:\n"
        "    s.connect(('1.1.1.1', 80)); result['network'] = 'CONNECTED'\n"
        "except OSError as error:\n"
        "    result['network'] = error.errno\n"
        "print('PROBE:' + json.dumps(result))\n",
    )
    result = probe_json(observation)
    assert observation.profile == sandbox.PROFILE_NAMESPACE_V1
    assert result["cwd"] == "/work"                              # path isolation
    assert result["write_outside"] in (13, 30)                  # path isolation
    assert result["network"] == 101                             # network isolation
    assert result["env"] == ["HOME", "LANG", "PATH"]            # credential redaction
    assert observation.capabilities["landlock_abi"] >= 1        # profile evidence

    killed = run_probe(
        sandbox_runner,
        "import ctypes\nctypes.CDLL(None, use_errno=True).ptrace(0, 0, 0, 0)\n",
    )
    assert killed.signal == sandbox.signal.SIGSYS               # syscall isolation

    overrun = run_probe(
        sandbox_runner,
        "import time\ntime.sleep(60)\n",
        limits=sandbox.SandboxLimits(wall_seconds=2),
    )
    assert overrun.timed_out is True                            # duration isolation
    assert overrun.signal in (sandbox.signal.SIGKILL, sandbox.signal.SIGTERM)


# 2. Sandbox-isolation violations emit SANDBOX_VIOLATION_RECORDED with
# evidence, terminate the attempt FAILED, reject its outputs, and charge
# actual or conservatively settled cost; a duration overrun fires a
# timer-driven timeout through the attempt machine.


def test_isolation_violations_are_recorded_and_fail_the_attempt(tmp_path, capabilities):
    if not capabilities.profile_available:
        pytest.fail(f"NAMESPACE_V1 unavailable (fail-closed gate): {capabilities.unavailable_reason()}")
    run_id = "RUN-M3-2"
    # Out-of-scope write detected by the trusted post-attempt diff check.
    harness = Harness(
        str(tmp_path),
        run_id=run_id,
        commands={
            f"{run_id}-root": (("BUILD", python_probe("open('/work/escape.txt','w').write('x')")),)
        },
    )
    try:
        root = harness.submit()
        attempt = harness.controller.plan_attempt(run_id, root, 5.0)
        with pytest.raises(sandbox.SandboxViolation) as excinfo:
            harness.controller.dispatch_attempt(run_id, attempt, root)
        assert excinfo.value.evidence["kind"] == sandbox.VIOLATION_OUT_OF_SCOPE_WRITE
        state = harness.store.state(run_id)
        assert state.attempts[attempt].state == "FAILED"
        violations = [e for e in harness.store.history(run_id) if e.event_type == "SANDBOX_VIOLATION_RECORDED"]
        assert len(violations) == 1
        assert violations[0].payload["evidence"]["kind"] == sandbox.VIOLATION_OUT_OF_SCOPE_WRITE
        # Outputs rejected: no candidate artefact was admitted.
        assert state.artefacts == {}
        # Conservatively settled cost.
        assert state.attempts[attempt].accounting_state == "FINALLY_SETTLED"
        assert harness.controller.get_costs(run_id)["total_charged"] == 5.0
    finally:
        harness.close()

    # Forbidden syscall detected by the kernel SIGSYS signal.
    harness = Harness(
        str(tmp_path / "syscall"),
        run_id=run_id,
        commands={
            f"{run_id}-root": (
                ("BUILD", python_probe("import ctypes; ctypes.CDLL(None, use_errno=True).ptrace(0,0,0,0)")),
            )
        },
    )
    try:
        root = harness.submit()
        attempt = harness.controller.plan_attempt(run_id, root, 5.0)
        with pytest.raises(sandbox.SandboxViolation) as excinfo:
            harness.controller.dispatch_attempt(run_id, attempt, root)
        assert excinfo.value.evidence["kind"] == sandbox.VIOLATION_FORBIDDEN_SYSCALL
        assert harness.store.state(run_id).attempts[attempt].state == "FAILED"
    finally:
        harness.close()

    # A nested out-of-scope write is still caught by the publication gate.
    harness = Harness(
        str(tmp_path / "nested"),
        run_id=run_id,
        commands={
            f"{run_id}-root": (
                (
                    "BUILD",
                    python_probe(
                        "import os\n"
                        "os.makedirs('/work/nested/deep', exist_ok=True)\n"
                        "open('/work/nested/deep/escape.txt','w').write('x')\n"
                    ),
                ),
            )
        },
    )
    try:
        root = harness.submit()
        attempt = harness.controller.plan_attempt(run_id, root, 5.0)
        with pytest.raises(sandbox.SandboxViolation) as excinfo:
            harness.controller.dispatch_attempt(run_id, attempt, root)
        assert excinfo.value.evidence["kind"] == sandbox.VIOLATION_OUT_OF_SCOPE_WRITE
    finally:
        harness.close()

    # A recursive exclude is enforced even when the include is broad: with
    # include "**", a nested hidden-tests change must still be out of scope.
    harness = Harness(
        str(tmp_path / "exclude"),
        run_id=run_id,
        commands={
            f"{run_id}-root": (
                (
                    "BUILD",
                    python_probe(
                        "import os\n"
                        "os.makedirs('/work/hidden-tests/sub', exist_ok=True)\n"
                        "open('/work/hidden-tests/sub/t.py','w').write('x')\n"
                    ),
                ),
            )
        },
    )
    try:
        harness.controller.submit_run(
            build_run_config(
                run_id,
                root_contract={
                    "description": "broad scope",
                    "owned_scope": ["**"],
                    "owned_scope_exclude": ["hidden-tests/**"],
                },
                budgets={"MODEL_COST": 10.0},
            )
        )
        root = f"{run_id}-root"
        attempt = harness.controller.plan_attempt(run_id, root, 5.0)
        with pytest.raises(sandbox.SandboxViolation) as excinfo:
            harness.controller.dispatch_attempt(run_id, attempt, root)
        assert excinfo.value.evidence["kind"] == sandbox.VIOLATION_OUT_OF_SCOPE_WRITE
        assert "hidden-tests/sub/t.py" in excinfo.value.evidence["paths"]
    finally:
        harness.close()

    # Duration overrun fires a timer-driven timeout through the attempt machine.
    harness = Harness(
        str(tmp_path / "timeout"),
        run_id=run_id,
        commands={f"{run_id}-root": (("BUILD", python_probe("import time; time.sleep(60)")),)},
        limits=sandbox.SandboxLimits(wall_seconds=2),
    )
    try:
        root = harness.submit()
        attempt = harness.controller.plan_attempt(run_id, root, 5.0)
        from orchestrator.executor import AttemptTimedOut

        with pytest.raises(AttemptTimedOut):
            harness.controller.dispatch_attempt(run_id, attempt, root)
        state = harness.store.state(run_id)
        assert state.attempts[attempt].state == "TIMED_OUT"
        assert any(e.event_type == "TIMER_FIRED" for e in harness.store.history(run_id))
    finally:
        harness.close()


# 3. The deterministic command executor speaking the Executor protocol inside
# the sandbox: schema-first I/O, candidate-patch capture, and artefact hashing.


def test_command_executor_protocol_candidate_capture_and_artefact_hashing(harness):
    run_id = harness.run_id
    root = harness.submit()
    attempt = harness.controller.execute_attempt(run_id, root, 5.0)

    request = harness.controller._dispatch_requests[attempt]
    response = harness.controller._dispatch_responses[attempt]
    # Schema-first I/O: the response validates against the immutable request.
    executor_protocol.validate_response(
        request, response, expected_token=request["attempt_token"], producer_authority="command-executor"
    )
    assert response["self_reported_status"] == "COMPLETED"
    observes = {entry["command_id"]: entry for entry in response["observations"]}
    assert observes["BUILD"]["exit_code"] == 0 and observes["TEST"]["exit_code"] == 0
    for entry in observes.values():
        assert entry["stdout_hash"].startswith("sha256:")
        assert entry["stderr_hash"].startswith("sha256:")

    # Candidate-patch capture and artefact hashing. The nested in-scope file
    # exercises recursive `src/**` matching (and its directory entries).
    assert "src/health/api/routes.py" in response["changed_paths"]
    assert all(path.startswith("src") for path in response["changed_paths"])
    artefact = response["artefacts"][0]
    assert artefact["type"] == "GIT_CANDIDATE"
    record = json.load(open(os.path.join(harness.artefact_root, f"WT-{attempt}.candidate.json")))
    assert record["import_verified"] is True
    assert record["tree_digest"] == artefact["content_hash"]
    assert "src/health/api/routes.py" in record["changed_paths"]

    state = harness.store.state(run_id)
    assert state.attempts[attempt].state == "RESULT_PRODUCED"
    assert state.artefacts[f"{attempt}-artefact"].state == "INTEGRATED"


# 4. Disposable Git worktrees with post-attempt write-scope checks, deterministic
# integration, single-use tokens, and duplicate-delivery idempotency.


def test_worktrees_scope_gate_integration_tokens_and_idempotency(harness):
    run_id = harness.run_id
    # A disposable worktree has no writable .git and imports deterministically.
    manager = WorktreeManager(
        repository=harness.repo,
        worktree_root=os.path.join(harness.root, "wt-check"),
        artefact_root=os.path.join(harness.root, "artefacts-check"),
    )
    tree = manager.materialise(harness.commit, "WT-CHECK")
    assert not os.path.exists(os.path.join(tree, ".git"))
    base = snapshot(tree)
    with open(os.path.join(tree, "src", "app.txt"), "w") as handle:
        handle.write("changed\n")
    candidate = manager.create_candidate(
        base_commit=harness.commit,
        tree_path=tree,
        base_manifest=base,
        worktree_id="WT-CHECK",
        scope_include=["src/**"],
        scope_exclude=[".git/**", ".orchestrator/**", "hidden-tests/**"],
    )
    assert candidate["changed_paths"] == ["src/app.txt"]
    parent = subprocess.run(
        ["git", "-C", os.path.join(harness.root, "wt-check", "WT-CHECK-integration"),
         "rev-parse", "HEAD~1"],
        capture_output=True, text=True, check=True,
    ).stdout.strip()
    assert parent == harness.commit  # candidate commit descends from the frozen base
    manager.cleanup("WT-CHECK")

    root = harness.submit()
    attempt = harness.controller.execute_attempt(run_id, root, 5.0)
    state = harness.store.state(run_id)
    assert state.artefacts[f"{attempt}-artefact"].state == "INTEGRATED"

    # Single-use attempt token.
    with pytest.raises(ValueError):
        harness.executor.issue_token(attempt)
    request = harness.controller._dispatch_requests[attempt]
    with pytest.raises(Exception):
        harness.executor.dispatch({**request, "attempt_token": "forged"})

    # Duplicate delivery is idempotent: identical response, no extra charge.
    charged = harness.controller.get_costs(run_id)["total_charged"]
    assert harness.executor.dispatch(request) == harness.controller._dispatch_responses[attempt]
    assert harness.controller.get_costs(run_id)["total_charged"] == charged


# Hidden tests are absent from the executor-visible mounts, not merely excluded
# from publication.


def test_hidden_tests_are_absent_from_the_sandbox_view(tmp_path, capabilities):
    if not capabilities.profile_available:
        pytest.fail(f"NAMESPACE_V1 unavailable (fail-closed gate): {capabilities.unavailable_reason()}")
    root = str(tmp_path)
    repo, commit = 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 = 'hidden oracle'\n")
    os.makedirs(os.path.join(repo, "hidden-tests", "sub"))
    with open(os.path.join(repo, "hidden-tests", "sub", "nested.py"), "w") as handle:
        handle.write("NESTED = True\n")
    environment = _git_environment(GIT_ENVIRONMENT)
    subprocess.run(["git", "-C", repo, "add", "-A"], env=environment, check=True, capture_output=True)
    subprocess.run(["git", "-C", repo, "commit", "-q", "-m", "hidden"], 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=os.path.join(root, "wt-hidden"),
        artefact_root=os.path.join(root, "artefacts-hidden"),
    )
    tree = manager.materialise(commit, "WT-HIDDEN")
    assert not os.path.exists(os.path.join(tree, "hidden-tests"))
    assert os.path.exists(os.path.join(tree, "src", "app.txt"))
    manager.cleanup("WT-HIDDEN")

    # And inside the sandbox the path is absent for the whole nested tree: the
    # probe command exits non-zero if any hidden-test path is visible.
    run_id = "RUN-M3-HIDDEN"
    harness = Harness(
        root,
        run_id=run_id,
        repo=repo,
        commit=commit,
        commands={
            f"{run_id}-root": (
                (
                    "BUILD",
                    python_probe(
                        "import os, sys\n"
                        "paths = ['/work/hidden-tests', '/work/hidden-tests/sub/nested.py']\n"
                        "sys.exit(1 if any(os.path.exists(p) for p in paths) else 0)\n"
                    ),
                ),
            )
        },
    )
    try:
        harness.submit()
        attempt = harness.controller.plan_attempt(run_id, f"{run_id}-root", 5.0)
        harness.controller.dispatch_attempt(run_id, attempt, f"{run_id}-root")
        response = harness.controller._dispatch_responses[attempt]
        assert response["observations"][0]["exit_code"] == 0
        assert all("hidden-tests" not in path for path in response["changed_paths"])
    finally:
        harness.close()


# 5. Crash injection at the dispatch boundary: ACTION_DISPATCHED is durable
# before the executor is contacted, and recovery fails the attempt with
# conservative settlement.


def test_crash_at_dispatch_boundary_recovers_as_declared(tmp_path, capabilities):
    if not capabilities.profile_available:
        pytest.fail(f"NAMESPACE_V1 unavailable (fail-closed gate): {capabilities.unavailable_reason()}")
    run_id = "RUN-M3-5"
    harness = Harness(str(tmp_path), run_id=run_id)
    db_path = os.path.join(harness.root, "events.db")
    root = harness.submit()
    attempt = harness.controller.plan_attempt(run_id, root, 5.0)

    def crash(event):
        if event.event_type == "ACTION_DISPATCHED":
            raise CrashInjected("simulated crash at the dispatch boundary")

    harness.store.crash_after_commit = crash
    with pytest.raises(CrashInjected):
        harness.controller.dispatch_attempt(run_id, attempt, root)
    # ACTION_DISPATCHED is durable; the executor was never contacted. The
    # in-memory cache is not advanced (the process "crashed"), so the durable
    # state is read by replaying the log.
    assert harness.store.replay_fresh(run_id).attempts[attempt].state == "DISPATCHED"
    assert harness.executor._responses == {}
    harness.close()

    restarted_store = EventStore(db_path)
    try:
        restarted = Controller(
            restarted_store,
            CommandExecutor(harness.executor.configuration),
            executor_environment={"base_commit": harness.commit, "timeout_seconds": 30.0},
        )
        recovered = restarted.recover(run_id)
        assert recovered == [attempt]
        state = restarted_store.state(run_id)
        assert state.attempts[attempt].state == "FAILED"                 # ATTEMPT_FAILED_ON_RECOVERY
        assert state.attempts[attempt].accounting_state == "FINALLY_SETTLED"
        assert restarted.get_costs(run_id)["total_charged"] == 5.0       # conservative settlement
    finally:
        restarted_store.close()


# Trusted-repository end-to-end: deterministic build/test commands execute for
# real inside the sandbox and the run reaches an evidence-bound acceptance.


def test_trusted_repository_end_to_end_run(harness):
    run_id = harness.run_id
    root = harness.submit()
    attempt = harness.controller.execute_attempt(run_id, root, 5.0)
    harness.controller.validate_and_accept(
        run_id, root, {"checks": ["BUILD", "TEST", "SCOPE"]}, f"{attempt}-result"
    )
    state = harness.store.state(run_id)
    assert state.attempts[attempt].state == "RESULT_PRODUCED"
    assert state.nodes[root].state == "ACCEPTED"
    assert state.outcome == "ACCEPTANCE_CLAIMED"
    assert state.artefacts[f"{attempt}-artefact"].state == "INTEGRATED"
    # Replay reproduces state, ledger, and outcome without re-executing.
    responses_before = len(harness.executor._responses)
    replayed = harness.store.replay_fresh(run_id)
    assert replayed.outcome == "ACCEPTANCE_CLAIMED"
    assert replayed.ledger.total_charged == state.ledger.total_charged
    assert replayed.artefacts[f"{attempt}-artefact"].state == "INTEGRATED"
    assert len(harness.executor._responses) == responses_before
