# SPDX-License-Identifier: AGPL-3.0-or-later
"""Batch 3 (EXECUTION): sandbox limit verification, cancellation interruption,
and the single monotonic attempt deadline.

Sandbox-level cases skip when NAMESPACE_V1 is unavailable; cgroup cases skip
when no delegated cgroup v2 subtree exists.
"""
from __future__ import annotations

import ast
import contextlib
import dataclasses
import os
import shutil
import subprocess
import tempfile
import threading
import time

import pytest

from orchestrator import sandbox
from orchestrator.executor import AttemptCancelled, AttemptTimedOut

from test_milestone3_sandbox import TOOLCHAIN_PATHS
from test_milestone3_acceptance import Harness, build_fixture_repo, python_probe


# The surface the teardown pins grade: the plan's own executor invocation, the
# `/usr/bin/python3 /work/probe.py` argv pair every plan drives. The scoped scan
# counts a pid only where its command line names that pair, so an unrelated
# process that merely carries the probe-path literal in argv (a commit hook
# wrapper, a `bash -c` quoting the path) is never counted a survivor, while a
# live workload process and every fork it made still is. It is one source: every
# plan drives it and the scan matches it, so the two cannot drift.
_PROBE_ARGV = ("/usr/bin/python3", "/work/probe.py")


def _caps():
    found = sandbox.capabilities()
    if not found.profile_available:
        pytest.fail(f"NAMESPACE_V1 unavailable (fail-closed gate): {found.unavailable_reason()}")
    return found


def _probe_plan(source: str, *, wall_seconds: float):
    root = tempfile.mkdtemp(prefix="batch3-root-")
    work = tempfile.mkdtemp(prefix="batch3-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=_PROBE_ARGV,
        env={"PATH": "/usr/bin:/bin", "HOME": "/work", "LANG": "C.UTF-8"},
        limits=sandbox.SandboxLimits(wall_seconds=wall_seconds),
    )
    return plan, (root, work)


def test_sandbox_runner_cancellation_is_prompt():
    """R04: a cancellation interrupts a blocking runner instead of waiting for
    the command to finish."""
    _caps()
    plan, directories = _probe_plan("import time\ntime.sleep(30)\n", wall_seconds=30)
    runner = sandbox.SandboxRunner()
    started = time.monotonic()
    try:
        with pytest.raises(sandbox.SandboxCancelled):
            runner.run(plan, should_cancel=lambda: True)
    finally:
        for directory in directories:
            shutil.rmtree(directory, ignore_errors=True)
    assert time.monotonic() - started < 10.0, "cancellation did not interrupt the runner"


def test_runner_falls_back_honestly_when_cgroup_limits_are_unverified(monkeypatch):
    """R15: a denied cgroup limit write must not be reported as enforced."""
    capabilities = _caps()
    if not (capabilities.cgroup_v2 and capabilities.cgroup_parent):
        pytest.fail("no delegated cgroup v2 subtree (fail-closed gate)")

    def boom(_self, _limits):
        raise OSError("cgroup limit write denied")

    monkeypatch.setattr(sandbox._Cgroup, "set_limits", boom)
    plan, directories = _probe_plan("print('ok')\n", wall_seconds=30)
    runner = sandbox.SandboxRunner(capabilities_override=capabilities)
    try:
        observation = runner.run(plan)
    finally:
        for directory in directories:
            shutil.rmtree(directory, ignore_errors=True)
    assert observation.cgroup is None
    assert observation.limits_verified is False
    assert observation.resource_source == "RUSAGE_CHILDREN"


def test_cgroup_set_limits_fails_closed_on_readback_mismatch(monkeypatch):
    """R15: a limit that does not read back as written is an error, not a
    success."""
    capabilities = _caps()
    if not (capabilities.cgroup_v2 and capabilities.cgroup_parent):
        pytest.fail("no delegated cgroup v2 subtree (fail-closed gate)")
    cgroup = sandbox._Cgroup(
        capabilities.cgroup_parent,
        f"batch3-{os.getpid()}-limits",
        capabilities.cgroup_controllers,
    )
    try:
        monkeypatch.setattr(sandbox._Cgroup, "_read", lambda self, name: "mismatch")
        with pytest.raises(OSError):
            cgroup.set_limits(sandbox.SandboxLimits())
    finally:
        monkeypatch.undo()
        cgroup.remove()


def test_attempt_deadline_is_shared_across_commands():
    """R15: one monotonic attempt deadline covers every command; the second
    command does not receive the full budget afresh."""
    _caps()
    root = tempfile.mkdtemp(prefix="batch3-deadline-")
    commands = {
        "RUN-B3-DEADLINE-root": (
            ("FIRST", python_probe("import time\ntime.sleep(0.3)\n")),
            ("SECOND", python_probe("import time\ntime.sleep(0.3)\n")),
        )
    }
    harness = Harness(
        root, run_id="RUN-B3-DEADLINE", commands=commands,
        limits=sandbox.SandboxLimits(wall_seconds=0.5),
    )
    try:
        node = harness.submit()
        with pytest.raises(AttemptTimedOut):
            harness.controller.execute_attempt("RUN-B3-DEADLINE", node, 1.0)
    finally:
        harness.close()
        shutil.rmtree(root, ignore_errors=True)


def test_cancellation_during_dispatch_does_not_produce_a_result():
    """R04: cancelling a dispatched attempt interrupts the sandbox and yields no
    result, while the attempt ends CANCELLED."""
    _caps()
    root = tempfile.mkdtemp(prefix="batch3-cancel-")
    commands = {
        "RUN-B3-CANCEL-root": (("LONG", python_probe("import time\ntime.sleep(30)\n")),)
    }
    harness = Harness(root, run_id="RUN-B3-CANCEL", commands=commands)
    failures = []
    try:
        node = harness.submit()
        attempt = harness.controller.plan_attempt("RUN-B3-CANCEL", node, 1.0)

        def dispatch():
            try:
                harness.controller.dispatch_attempt("RUN-B3-CANCEL", attempt, node)
            except Exception as error:  # noqa: BLE001 - reported to the parent thread
                failures.append(error)

        thread = threading.Thread(target=dispatch)
        thread.start()
        time.sleep(0.8)
        harness.controller.cancel_run("RUN-B3-CANCEL", node)
        thread.join(timeout=30)
        assert not thread.is_alive(), "dispatch did not stop after cancellation"
        assert any(isinstance(error, AttemptCancelled) for error in failures), failures
        state = harness.store.state("RUN-B3-CANCEL")
        assert state.attempts[attempt].state == "CANCELLED"
        assert f"{attempt}-result" not in state.freshness
    finally:
        harness.close()
        shutil.rmtree(root, ignore_errors=True)


# ---------------------------------------------------------------------------
# EXECUTION-gate adversarial review regressions (F1-F7)
# ---------------------------------------------------------------------------


def _probe_pids():
    found = []
    for entry in os.listdir("/proc"):
        if not entry.isdigit():
            continue
        try:
            with open(f"/proc/{entry}/cmdline", "rb") as handle:
                argv = [part.decode(errors="replace") for part in handle.read().split(b"\0") if part]
        except OSError:
            continue
        for index in range(len(argv) - 1):
            if (argv[index], argv[index + 1]) == _PROBE_ARGV:
                found.append(int(entry))
                break
    return found


def _wait_cmdline(pid: int, needle: str) -> str:
    last = ""
    for _ in range(20):
        try:
            with open(f"/proc/{pid}/cmdline", "rb") as handle:
                last = handle.read().replace(b"\0", b" ").decode(errors="replace")
        except OSError:
            last = ""
        if needle in last:
            return last
        time.sleep(0.1)
    return last


def test_escaped_process_is_killed_without_a_cgroup(monkeypatch):
    """F1: a workload that clears PDEATHSIG and setsid()s is still killed on the
    no-cgroup path, not left running after teardown."""
    capabilities = _caps()
    reduced = dataclasses.replace(capabilities, cgroup_v2=False, cgroup_parent=None)
    root = tempfile.mkdtemp(prefix="batch3-escape-root-")
    work = tempfile.mkdtemp(prefix="batch3-escape-work-")
    escape = (
        "import ctypes, os, time\n"
        "libc = ctypes.CDLL(None, use_errno=True)\n"
        "libc.prctl(1, 0, 0, 0, 0)\n"
        "os.setsid()\n"
        "open('/work/escaped','w').write('x')\n"
        "time.sleep(120)\n"
    )
    with open(os.path.join(work, "probe.py"), "w") as handle:
        handle.write(escape)
    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=_PROBE_ARGV,
        env={"PATH": "/usr/bin:/bin", "HOME": "/work", "LANG": "C.UTF-8"},
        limits=sandbox.SandboxLimits(wall_seconds=30),
    )
    marker = os.path.join(work, "escaped")
    runner = sandbox.SandboxRunner(capabilities_override=reduced)
    try:
        with pytest.raises(sandbox.SandboxCancelled):
            runner.run(plan, should_cancel=lambda: os.path.exists(marker))
        time.sleep(0.3)
        survivors = _probe_pids()
        assert survivors == [], f"escaped workload survived teardown: {survivors}"
    finally:
        for pid in _probe_pids():
            try:
                os.kill(pid, 9)
            except OSError:
                pass
        shutil.rmtree(root, ignore_errors=True)
        shutil.rmtree(work, ignore_errors=True)


@pytest.mark.timeout(120)
def test_racing_forker_and_double_forked_sleeper_die_at_teardown():
    """F20: a racing forker that keeps forking session-escaped sleepers across
    teardown, plus a double-forked sleeper whose middleman exits before the
    kill, must leave no /work/probe.py descendant alive after the runner's kill
    (the nested-grading survivor class)."""
    capabilities = _caps()
    reduced = dataclasses.replace(capabilities, cgroup_v2=False, cgroup_parent=None)
    root = tempfile.mkdtemp(prefix="batch3-race-root-")
    work = tempfile.mkdtemp(prefix="batch3-race-work-")
    racing = (
        "import ctypes, os, time\n"
        "libc = ctypes.CDLL(None, use_errno=True)\n"
        "libc.prctl(1, 0, 0, 0, 0)\n"
        "os.setsid()\n"
        "def sleeper():\n"
        "    os.setsid()\n"
        "    time.sleep(120)\n"
        "def forker():\n"
        "    os.setsid()\n"
        "    while True:\n"
        "        pid = os.fork()\n"
        "        if pid == 0:\n"
        "            sleeper()\n"
        "            os._exit(0)\n"
        "        time.sleep(0.005)\n"
        "for _ in range(4):\n"
        "    pid = os.fork()\n"
        "    if pid == 0:\n"
        "        forker()\n"
        "pid = os.fork()\n"
        "if pid == 0:\n"
        "    gpid = os.fork()\n"
        "    if gpid == 0:\n"
        "        sleeper()\n"
        "        os._exit(0)\n"
        "    os._exit(0)\n"
        "time.sleep(0.5)\n"
        "open('/work/racing','w').write('x')\n"
        "time.sleep(120)\n"
    )
    with open(os.path.join(work, "probe.py"), "w") as handle:
        handle.write(racing)
    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=_PROBE_ARGV,
        env={"PATH": "/usr/bin:/bin", "HOME": "/work", "LANG": "C.UTF-8"},
        limits=sandbox.SandboxLimits(wall_seconds=30),
    )
    marker = os.path.join(work, "racing")
    runner = sandbox.SandboxRunner(capabilities_override=reduced)
    try:
        with pytest.raises(sandbox.SandboxCancelled):
            runner.run(plan, should_cancel=lambda: os.path.exists(marker))
        time.sleep(0.3)
        survivors = _probe_pids()
        assert survivors == [], f"racing forkers survived teardown: {survivors}"
    finally:
        for pid in _probe_pids():
            with contextlib.suppress(OSError):
                os.kill(pid, 9)
        shutil.rmtree(root, ignore_errors=True)
        shutil.rmtree(work, ignore_errors=True)


def test_every_probe_plan_command_still_names_the_scanned_pair():
    """F27: the seam's one surface rule stays anchored to the surface the pins
    grade — every plan ``command`` in the batch3 module resolves to the argv
    pair the scoped scan matches, the one source ``_PROBE_ARGV``. A plan-shape
    change that does not move the scan with it fails here."""
    with open(__file__) as handle:
        tree = ast.parse(handle.read())
    seen = [
        node.value
        for node in ast.walk(tree)
        if isinstance(node, ast.keyword) and node.arg == "command"
    ]
    assert seen, "no plan command found in the batch3 module"
    for node in seen:
        command = globals().get(node.id) if isinstance(node, ast.Name) else ast.literal_eval(node)
        assert command == _PROBE_ARGV, (
            f"plan command {command!r} drifted from the scanned pair {_PROBE_ARGV!r}"
        )


def test_the_scoped_scan_counts_a_live_pair_holder_and_not_an_argv_carrier():
    """F27: one scan, both halves of the F24 shape — an unrelated argv-carrier
    is not counted a survivor while a live pair-holder carrying the plan's own
    executor invocation still is."""
    if not os.path.exists("/sys/fs/cgroup/cgroup.controllers"):
        pytest.skip("no cgroup2 visible here (an R08-type fresh root): the whole-table measure is host-side only")
    plan, directories = _probe_plan("print('ok')\n", wall_seconds=1.0)
    carrier_cmd = "sleep 20; true # teardown sweep quoting the module's path /work/probe" + ".py"
    holder_cmd = "import time; time.sleep(20)"
    carrier: subprocess.Popen | None = None
    holder: subprocess.Popen | None = None
    try:
        carrier = subprocess.Popen(["bash", "-c", carrier_cmd], start_new_session=True)
        holder = subprocess.Popen(
            ["/usr/bin/python3", "-c", holder_cmd, *plan.command],
            start_new_session=True,
        )
        carrier_last = _wait_cmdline(carrier.pid, "/work/probe" + ".py")
        holder_last = _wait_cmdline(holder.pid, plan.command[-1])
        assert "/work/probe" + ".py" in carrier_last, (
            f"the carrier never retained the probe path literal in this context: {carrier_last!r}"
        )
        assert plan.command[-1] in holder_last, (
            f"the holder never retained the plan command in this context: {holder_last!r}"
        )
        assert carrier.poll() is None and holder.poll() is None, (
            "the carrier or the holder died before the scan measured it"
        )
        found = _probe_pids()
        assert carrier.pid not in found, (
            f"the scoped scan counted an unrelated argv carrier: {found}"
        )
        assert holder.pid in found, (
            f"the scoped scan missed a live workload-surface holder: {found}"
        )
        assert found == [holder.pid], (
            f"the scoped scan is not exactly the workload surface: {found}"
        )
    finally:
        for process in (carrier, holder):
            if process is not None:
                if process.poll() is None:
                    with contextlib.suppress(OSError):
                        process.kill()
                process.wait()
        for directory in directories:
            shutil.rmtree(directory, ignore_errors=True)


def test_cancellation_between_late_read_and_artefact_emit_publishes_nothing():
    """F2: a cancellation landing after the `late` read must not admit or
    integrate the cancelled attempt's artefact."""
    from orchestrator.controller import Controller
    from orchestrator.executor import ScriptedExecutor
    from orchestrator.store import EventStore
    from helpers import build_run_config

    run_id = "RUN-F2"
    script = lambda request: {  # noqa: E731
        "self_reported_status": "COMPLETED",
        "artefacts": [{"type": "GIT_CANDIDATE", "content_hash": "sha256:f2", "reference": "c"}],
        "observations": [
            {"type": "COMMAND_RESULT", "command_id": "BUILD", "exit_code": 0, "signal": None}
        ],
        "cost_report": {"committed": 5, "actual": 5, "units": "USD"},
    }
    store = EventStore(os.path.join(tempfile.mkdtemp(), "f2.db"))
    controller = Controller(store, ScriptedExecutor({f"{run_id}-root": script}))
    try:
        root = controller.submit_run(build_run_config(run_id, budgets={"MODEL_COST": 10.0}))
        original_emit = controller._emit
        state = {"cancelled": False}

        def emit_hooked(run, event_type, payload, semantic_key, **kwargs):
            if event_type == "RESULT_ADMITTED" and not state["cancelled"]:
                state["cancelled"] = True
                controller.cancel_run(run, root)
            return original_emit(run, event_type, payload, semantic_key, **kwargs)

        controller._emit = emit_hooked
        controller.execute_attempt(run_id, root, 5.0)
        current = store.state(run_id)
        assert current.artefacts == {}
        assert all(
            event.event_type not in ("ARTEFACT_ADMITTED", "ARTEFACT_INTEGRATED")
            for event in store.history(run_id)
        )
        assert current.freshness == {}
    finally:
        store.close()


def test_cancel_racing_dispatch_never_launders_the_charge():
    """F3: whenever the executor actually runs, the dispatched charge is
    retained (never reconciled to zero)."""
    import threading
    from orchestrator.controller import Controller
    from orchestrator.executor import AttemptCancelled, ScriptedExecutor
    from orchestrator.store import EventStore
    from helpers import build_run_config

    for iteration in range(3):
        run_id = f"RUN-F3-{iteration}"
        executor = ScriptedExecutor(
            {f"{run_id}-root": lambda request: {"self_reported_status": "COMPLETED"}}
        )
        started = threading.Event()
        release = threading.Event()
        runs = {"count": 0}
        original_dispatch = executor.dispatch

        def blocking_dispatch(request):
            if request["attempt_id"] in executor._cancelled:
                raise AttemptCancelled(request["attempt_id"])
            runs["count"] += 1
            started.set()
            release.wait(timeout=5)
            return original_dispatch(request)

        executor.dispatch = blocking_dispatch
        store = EventStore(str(tempfile.mkdtemp()) + f"/{run_id}.db")
        controller = Controller(store, executor)
        try:
            root = controller.submit_run(build_run_config(run_id, budgets={"MODEL_COST": 10.0}))
            attempt = controller.plan_attempt(run_id, root, 5.0)
            failures = []

            def dispatch():
                try:
                    controller.dispatch_attempt(run_id, attempt, root)
                except Exception as error:  # noqa: BLE001
                    failures.append(error)

            thread = threading.Thread(target=dispatch)
            thread.start()
            started.wait(timeout=5)
            controller.cancel_run(run_id, root)
            release.set()
            thread.join(timeout=10)
            state = store.state(run_id)
            if runs["count"] > 0:
                assert state.ledger.charged.get(attempt, 0.0) > 0.0, (
                    "a dispatched charge was laundered to zero"
                )
        finally:
            store.close()


def test_teardown_failure_is_reported_after_a_setup_failure(monkeypatch):
    """F4: a teardown failure on the exceptional path must be reported, not
    dropped.

    The simulated teardown failure is forced with a patched ``remove``, so the
    cgroup the run creates is removed here in ``finally`` (F8): a test must not
    leak the host state its own monkeypatch prevented cleaning up."""
    capabilities = _caps()
    if not (capabilities.cgroup_v2 and capabilities.cgroup_parent):
        pytest.fail("no delegated cgroup v2 subtree (fail-closed gate)")
    created = []
    original_init = sandbox._Cgroup.__init__

    def recording_init(self, parent, name, controllers):
        original_init(self, parent, name, controllers)
        created.append(self.path)

    monkeypatch.setattr(sandbox._Cgroup, "__init__", recording_init)
    monkeypatch.setattr(sandbox._Cgroup, "set_limits", lambda self, limits: (_ for _ in ()).throw(OSError("denied")))
    monkeypatch.setattr(sandbox._Cgroup, "remove", lambda self, timeout=5.0: False)
    plan, directories = _probe_plan("print('ok')\n", wall_seconds=30)
    runner = sandbox.SandboxRunner(capabilities_override=capabilities)
    try:
        with pytest.raises(sandbox.SandboxSetupError) as raised:
            runner.run(plan)
        assert "could not be removed" in str(raised.value)
    finally:
        monkeypatch.undo()
        for path in created:
            try:
                os.rmdir(path)
            except OSError:
                pass
        for directory in directories:
            shutil.rmtree(directory, ignore_errors=True)


def test_limits_verified_is_persisted_in_the_command_result():
    """F5: the durable observation carries the limit verification, so the R15
    claim is auditable from the log."""
    _caps()
    root = tempfile.mkdtemp(prefix="batch3-evidence-")
    harness = Harness(root, run_id="RUN-F5")
    try:
        node = harness.submit()
        harness.controller.execute_attempt("RUN-F5", node, 1.0)
        observations = [
            event.payload["observation"]
            for event in harness.store.history("RUN-F5")
            if event.event_type == "EXECUTOR_OBSERVATION_ADMITTED"
        ]
        assert observations
        command_results = [
            item
            for observation in observations
            for item in (observation.get("observations") or [])
        ]
        assert command_results
        assert "limits_verified" in command_results[0]
        assert "cgroup" in command_results[0]
    finally:
        harness.close()
        shutil.rmtree(root, ignore_errors=True)


def test_changed_paths_equals_the_committed_diff_for_mode_only_changes(tmp_path):
    """F6: a non-Git-representable mode change is not reported as a change."""
    import subprocess
    from orchestrator.worktree import GIT_ENVIRONMENT, WorktreeManager, snapshot

    repo, _ = build_fixture_repo(str(tmp_path))
    manager = WorktreeManager(
        repository=repo,
        worktree_root=str(tmp_path / "wt"),
        artefact_root=str(tmp_path / "art"),
    )
    commit = subprocess.run(
        ["git", "-C", repo, "rev-parse", "HEAD"], capture_output=True, text=True, check=True
    ).stdout.strip()
    tree = manager.materialise(commit, "WT-MODE")
    base = snapshot(tree)
    os.chmod(os.path.join(tree, "src", "app.txt"), 0o444)
    record = manager.create_candidate(
        base_commit=commit,
        tree_path=tree,
        base_manifest=base,
        worktree_id="WT-MODE",
        scope_include=["src/**"],
        scope_exclude=[".git/**", ".orchestrator/**", "hidden-tests/**"],
    )
    integration = os.path.join(str(tmp_path / "wt"), "WT-MODE-integration")
    committed = subprocess.run(
        ["git", "-C", integration, "diff", "--name-only", commit, "HEAD"],
        capture_output=True, text=True, check=True,
    ).stdout.split()
    assert sorted(record["changed_paths"]) == sorted(committed)


def test_dotdot_and_absolute_paths_are_out_of_scope():
    """F7: a path that escapes the delegated scope is rejected."""
    from orchestrator import executor_protocol

    assert executor_protocol.path_in_scope("src/../../etc/passwd", ["src/**"], []) is False
    assert executor_protocol.path_in_scope("/etc/passwd", ["**"], []) is False
    assert executor_protocol.path_in_scope("src/app.txt", ["src/**"], []) is True
