"""Cross-platform process-tree control for ProbHub sandboxes."""

import errno
import os
import platform
import selectors
import signal
import stat
import subprocess
import sys
import tempfile
import time
from dataclasses import dataclass, field
from pathlib import Path


DEFAULT_PROCESS_LIMIT = 32
VALIDATOR_TIMEOUT_SECONDS = 5.0
VALIDATOR_MEMORY_LIMIT_MB = 512
VALIDATOR_OUTPUT_LIMIT_BYTES = 8 * 1024 * 1024
DEFAULT_POLL_INTERVAL = 0.005
CANCEL_FILE_ENV = "PROBHUB_CANCEL_FILE"
WINDOWS_CREATE_SUSPENDED = 0x00000004
PYTHON_OPTIONS_WITH_ARGUMENT = frozenset(("-W", "-X", "--check-hash-based-pycs"))
UNIX_EXEC_START_TIMEOUT_SECONDS = 10.0
UNIX_EXEC_READY = b"PROBHUB_UNIX_EXEC_READY_V1\n"
UNIX_EXEC_STATUS_LIMIT = 64 * 1024
MAX_TRACKED_POSIX_DESCENDANTS = 4096
POSIX_CLEANUP_TIMEOUT_SECONDS = 2.0
PROCESS_CLEANUP_DIAGNOSTIC_LIMIT = 32
PROCESS_CLEANUP_FAILED = "process_cleanup_failed"
MAX_TRACKED_WINDOWS_PROCESSES = 4096
MAX_WINDOWS_SNAPSHOT_BYTES = 64 * 1024 * 1024
UNIX_EXEC_HELPER_CODE = r'''
import os
import signal
import sys

READY = b"PROBHUB_UNIX_EXEC_READY_V1\n"


def report_failure(status_fd, message):
    try:
        os.write(status_fd, ("Unix execution helper failed: " + message).encode("utf-8"))
    except OSError:
        pass


def main():
    argv = sys.argv[1:]
    status_fd = None
    try:
        if len(argv) < 7 or argv[0] != "--memory-limit-bytes" or argv[2] != "--status-fd":
            raise ValueError("invalid helper arguments")
        limit_bytes = int(argv[1])
        status_fd = int(argv[3])
        if argv[4] not in ("--restore-signals", "--keep-signals"):
            raise ValueError("invalid signal restore mode")
        restore_signals = argv[4] == "--restore-signals"
        if argv[5] != "--" or not argv[6:]:
            raise ValueError("target command is missing")
        if limit_bytes <= 0:
            raise ValueError("memory limit must be positive")

        import resource

        os.set_inheritable(status_fd, False)
        resource.setrlimit(resource.RLIMIT_AS, (limit_bytes, limit_bytes))
        if restore_signals:
            for name in ("SIGPIPE", "SIGXFZ", "SIGXFSZ"):
                signum = getattr(signal, name, None)
                if signum is not None:
                    signal.signal(signum, signal.SIG_DFL)
        os.write(status_fd, READY)
        os.execvpe(argv[6], argv[6:], os.environ)
    except BaseException as exc:
        if status_fd is not None:
            report_failure(status_fd, str(exc))
        return 127


raise SystemExit(main())
'''.lstrip()


class ProcessCancelled(Exception):
    """Raised when a supervised ProbHub operation requests cancellation.

    The exception is deliberately shared by the process supervisor and the
    higher-level build/seal orchestration.  Adapters can therefore map every
    pre-publication cancellation to the same public ``cancelled`` code.
    """

    code = "cancelled"

    def __init__(self, message="operation cancelled"):
        super().__init__(str(message))


class OutputBudgetError(OSError):
    """Raised when captured output cannot be measured or bounded safely."""


@dataclass(frozen=True)
class ProcessIdentity:
    """A PID plus its Linux kernel start-time identity.

    ``start_time_ticks`` is unavailable on Windows, where Job Object or
    ``taskkill /T`` containment remains responsible for tree cleanup.
    """

    pid: int
    start_time_ticks: int | None = None


@dataclass(frozen=True)
class _LinuxProcessInfo:
    ppid: int
    rss_pages: int
    start_time_ticks: int
    state: str


def cancellation_requested():
    path = os.environ.get(CANCEL_FILE_ENV)
    if not path:
        return False
    try:
        return Path(path).is_file()
    except OSError:
        return False


def check_cancellation(cancel_check=None):
    """Raise the shared cancellation exception when an operation is stopped.

    ``None`` uses the process-wide ``PROBHUB_CANCEL_FILE`` signal.  Callers
    may provide a test or adapter callback, but the public outcome remains
    the same.  This helper is intended for phase boundaries; managed child
    processes continue to use :func:`run_managed_to_files` for polling and
    process-tree cleanup.
    """

    check = cancellation_requested if cancel_check is None else cancel_check
    if bool(check()):
        raise ProcessCancelled()


def _cancellation_requested(cancel_check):
    check = cancellation_requested if cancel_check is None else cancel_check
    return bool(check())


def _same_executable(left, right):
    try:
        return os.path.samefile(left, right)
    except (OSError, TypeError, ValueError):
        try:
            return os.path.normcase(os.path.abspath(os.fspath(left))) == os.path.normcase(
                os.path.abspath(os.fspath(right))
            )
        except (TypeError, ValueError):
            return False


def _prepare_windows_python_command(command, env):
    """Bypass Windows venv redirectors so Job Object containment reaches Python."""
    if platform.system() != "Windows" or isinstance(command, (str, bytes, os.PathLike)):
        return command, env
    command = list(command)
    if not command or sys.prefix == sys.base_prefix:
        return command, env
    if not _same_executable(command[0], sys.executable):
        return command, env

    base_executable = Path(sys.base_prefix) / Path(sys.executable).name
    helper = Path(__file__).with_name("_windows_python_exec.py")
    if not base_executable.is_file():
        raise OSError(f"Windows virtualenv base interpreter is missing: {base_executable}")
    if not helper.is_file():
        raise OSError(f"Windows Python containment helper is missing: {helper}")

    arguments = command[1:]
    interpreter_options = []
    while arguments:
        option = arguments[0]
        if option == "--":
            arguments = arguments[1:]
            break
        if option in ("-", "-c", "-m") or not str(option).startswith("-"):
            break
        interpreter_options.append(option)
        arguments = arguments[1:]
        if option in PYTHON_OPTIONS_WITH_ARGUMENT:
            if not arguments:
                raise ValueError(f"Python option requires an argument: {option}")
            interpreter_options.append(arguments[0])
            arguments = arguments[1:]

    # A Windows venv python.exe can be a redirector whose Popen handle belongs
    # to a launcher instead of the real interpreter. Assigning only that handle
    # to a Job lets the interpreter and its descendants escape all limits.
    original_executable = str(command[0])
    command = [
        str(base_executable),
        *interpreter_options,
        str(helper),
        str(base_executable),
        *arguments,
    ]
    child_env = dict(os.environ if env is None else env)
    # CPython uses this path to discover pyvenv.cfg before the helper starts.
    # The helper then points descendants at the base executable while the root
    # process keeps its original venv prefix and processed site-packages.
    child_env["__PYVENV_LAUNCHER__"] = original_executable
    return command, child_env


def _prepare_unix_memory_limited_command(command, memory_limit_mb):
    if isinstance(command, (str, bytes, os.PathLike)):
        raise TypeError("memory-limited Unix commands must be an argument sequence")
    command = list(command)
    if not command:
        raise ValueError("memory-limited Unix target command is missing")
    limit_bytes = int(float(memory_limit_mb) * 1024 * 1024)
    if limit_bytes <= 0:
        raise ValueError("memory limit must be positive")
    return command, limit_bytes


def _unix_memory_limited_command(command, limit_bytes, status_fd, restore_signals):
    return [
        sys.executable,
        "-I",
        "-S",
        "-c",
        UNIX_EXEC_HELPER_CODE,
        "--memory-limit-bytes",
        str(limit_bytes),
        "--status-fd",
        str(status_fd),
        "--restore-signals" if restore_signals else "--keep-signals",
        "--",
        *command,
    ]


def _terminate_failed_unix_start(proc):
    try:
        os.killpg(proc.pid, signal.SIGKILL)
    except OSError:
        try:
            proc.kill()
        except OSError:
            pass
    try:
        proc.wait(timeout=2)
    except (OSError, subprocess.TimeoutExpired):
        pass


def _await_unix_exec(proc, status_fd):
    deadline = time.monotonic() + UNIX_EXEC_START_TIMEOUT_SECONDS
    chunks = []
    total = 0
    with selectors.DefaultSelector() as selector:
        selector.register(status_fd, selectors.EVENT_READ)
        while True:
            remaining = deadline - time.monotonic()
            if remaining <= 0 or not selector.select(remaining):
                _terminate_failed_unix_start(proc)
                raise OSError(
                    "Unix execution helper did not reach exec before the startup deadline"
                )
            detail = os.read(status_fd, 4096)
            if not detail:
                break
            chunks.append(detail)
            total += len(detail)
            if total > UNIX_EXEC_STATUS_LIMIT:
                _terminate_failed_unix_start(proc)
                raise OSError("Unix execution helper returned an oversized startup status")

    detail = b"".join(chunks)
    if detail != UNIX_EXEC_READY:
        _terminate_failed_unix_start(proc)
        message = detail.decode("utf-8", errors="replace").strip()
        raise OSError(message or "Unix execution helper did not report startup readiness")


_WINDOWS_API = None


def _windows_api():
    global _WINDOWS_API
    if _WINDOWS_API is not None:
        return _WINDOWS_API
    if platform.system() != "Windows":
        _WINDOWS_API = {}
        return _WINDOWS_API
    try:
        import ctypes
        from ctypes import wintypes

        size_t = ctypes.c_size_t
        ulong_ptr = ctypes.c_size_t

        class BasicLimitInformation(ctypes.Structure):
            _fields_ = [
                ("PerProcessUserTimeLimit", ctypes.c_int64),
                ("PerJobUserTimeLimit", ctypes.c_int64),
                ("LimitFlags", wintypes.DWORD),
                ("MinimumWorkingSetSize", size_t),
                ("MaximumWorkingSetSize", size_t),
                ("ActiveProcessLimit", wintypes.DWORD),
                ("Affinity", ulong_ptr),
                ("PriorityClass", wintypes.DWORD),
                ("SchedulingClass", wintypes.DWORD),
            ]

        class IoCounters(ctypes.Structure):
            _fields_ = [
                ("ReadOperationCount", ctypes.c_uint64),
                ("WriteOperationCount", ctypes.c_uint64),
                ("OtherOperationCount", ctypes.c_uint64),
                ("ReadTransferCount", ctypes.c_uint64),
                ("WriteTransferCount", ctypes.c_uint64),
                ("OtherTransferCount", ctypes.c_uint64),
            ]

        class ExtendedLimitInformation(ctypes.Structure):
            _fields_ = [
                ("BasicLimitInformation", BasicLimitInformation),
                ("IoInfo", IoCounters),
                ("ProcessMemoryLimit", size_t),
                ("JobMemoryLimit", size_t),
                ("PeakProcessMemoryUsed", size_t),
                ("PeakJobMemoryUsed", size_t),
            ]

        class BasicAccountingInformation(ctypes.Structure):
            _fields_ = [
                ("TotalUserTime", ctypes.c_int64),
                ("TotalKernelTime", ctypes.c_int64),
                ("ThisPeriodTotalUserTime", ctypes.c_int64),
                ("ThisPeriodTotalKernelTime", ctypes.c_int64),
                ("TotalPageFaultCount", wintypes.DWORD),
                ("TotalProcesses", wintypes.DWORD),
                ("ActiveProcesses", wintypes.DWORD),
                ("TotalTerminatedProcesses", wintypes.DWORD),
            ]

        class ThreadEntry32(ctypes.Structure):
            _fields_ = [
                ("dwSize", wintypes.DWORD),
                ("cntUsage", wintypes.DWORD),
                ("th32ThreadID", wintypes.DWORD),
                ("th32OwnerProcessID", wintypes.DWORD),
                ("tpBasePri", wintypes.LONG),
                ("tpDeltaPri", wintypes.LONG),
                ("dwFlags", wintypes.DWORD),
            ]

        class UnicodeString(ctypes.Structure):
            _fields_ = [
                ("Length", wintypes.USHORT),
                ("MaximumLength", wintypes.USHORT),
                ("Buffer", ctypes.c_void_p),
            ]

        class SystemProcessInformation(ctypes.Structure):
            _fields_ = [
                ("NextEntryOffset", wintypes.ULONG),
                ("NumberOfThreads", wintypes.ULONG),
                ("Reserved", ctypes.c_int64 * 3),
                ("CreateTime", ctypes.c_int64),
                ("UserTime", ctypes.c_int64),
                ("KernelTime", ctypes.c_int64),
                ("ImageName", UnicodeString),
                ("BasePriority", wintypes.LONG),
                ("UniqueProcessId", wintypes.HANDLE),
                ("InheritedFromUniqueProcessId", wintypes.HANDLE),
            ]

        kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
        kernel32.CreateJobObjectW.restype = wintypes.HANDLE
        kernel32.SetInformationJobObject.argtypes = [
            wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD
        ]
        kernel32.SetInformationJobObject.restype = wintypes.BOOL
        kernel32.AssignProcessToJobObject.argtypes = [wintypes.HANDLE, wintypes.HANDLE]
        kernel32.AssignProcessToJobObject.restype = wintypes.BOOL
        kernel32.TerminateJobObject.argtypes = [wintypes.HANDLE, wintypes.UINT]
        kernel32.TerminateJobObject.restype = wintypes.BOOL
        kernel32.QueryInformationJobObject.argtypes = [
            wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD, ctypes.c_void_p
        ]
        kernel32.QueryInformationJobObject.restype = wintypes.BOOL
        kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
        kernel32.CloseHandle.restype = wintypes.BOOL
        kernel32.CreateToolhelp32Snapshot.argtypes = [wintypes.DWORD, wintypes.DWORD]
        kernel32.CreateToolhelp32Snapshot.restype = wintypes.HANDLE
        kernel32.Thread32First.argtypes = [wintypes.HANDLE, ctypes.c_void_p]
        kernel32.Thread32First.restype = wintypes.BOOL
        kernel32.Thread32Next.argtypes = [wintypes.HANDLE, ctypes.c_void_p]
        kernel32.Thread32Next.restype = wintypes.BOOL
        kernel32.OpenThread.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
        kernel32.OpenThread.restype = wintypes.HANDLE
        kernel32.ResumeThread.argtypes = [wintypes.HANDLE]
        kernel32.ResumeThread.restype = wintypes.DWORD
        kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
        kernel32.OpenProcess.restype = wintypes.HANDLE
        kernel32.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD]
        kernel32.WaitForSingleObject.restype = wintypes.DWORD
        kernel32.TerminateProcess.argtypes = [wintypes.HANDLE, wintypes.UINT]
        kernel32.TerminateProcess.restype = wintypes.BOOL
        kernel32.GetProcessTimes.argtypes = [wintypes.HANDLE] + [ctypes.c_void_p] * 4
        kernel32.GetProcessTimes.restype = wintypes.BOOL
        kernel32.GetCurrentProcess.restype = wintypes.HANDLE
        kernel32.DuplicateHandle.argtypes = [
            wintypes.HANDLE, wintypes.HANDLE, wintypes.HANDLE, ctypes.c_void_p,
            wintypes.DWORD, wintypes.BOOL, wintypes.DWORD,
        ]
        kernel32.DuplicateHandle.restype = wintypes.BOOL
        ntdll = ctypes.WinDLL("ntdll")
        ntdll.NtQuerySystemInformation.argtypes = [
            wintypes.ULONG, ctypes.c_void_p, wintypes.ULONG, ctypes.POINTER(wintypes.ULONG)
        ]
        ntdll.NtQuerySystemInformation.restype = wintypes.LONG
        _WINDOWS_API = {
            "ctypes": ctypes,
            "kernel32": kernel32,
            "info_type": ExtendedLimitInformation,
            "accounting_type": BasicAccountingInformation,
            "thread_entry_type": ThreadEntry32,
            "process_information_type": SystemProcessInformation,
            "query_processes": ntdll.NtQuerySystemInformation,
        }
    except Exception:
        _WINDOWS_API = {}
    return _WINDOWS_API


def windows_assign_job(proc, memory_limit_mb=None, process_limit=DEFAULT_PROCESS_LIMIT):
    api = _windows_api()
    if not api:
        return None
    ctypes = api["ctypes"]
    kernel32 = api["kernel32"]
    info_type = api["info_type"]
    try:
        job = kernel32.CreateJobObjectW(None, None)
        if not job:
            return None
        info = info_type()
        info.BasicLimitInformation.LimitFlags = 0x00002000  # KILL_ON_JOB_CLOSE
        if memory_limit_mb is not None:
            info.BasicLimitInformation.LimitFlags |= 0x00000200  # JOB_MEMORY
            info.JobMemoryLimit = int(float(memory_limit_mb) * 1024 * 1024)
        if process_limit is not None and int(process_limit) > 0:
            info.BasicLimitInformation.LimitFlags |= 0x00000008  # ACTIVE_PROCESS
            info.BasicLimitInformation.ActiveProcessLimit = int(process_limit)
        if not kernel32.SetInformationJobObject(job, 9, ctypes.byref(info), ctypes.sizeof(info)):
            kernel32.CloseHandle(job)
            return None
        if not kernel32.AssignProcessToJobObject(job, int(proc._handle)):
            kernel32.CloseHandle(job)
            return None
        return job
    except Exception:
        return None


def windows_resume_process(pid):
    api = _windows_api()
    if not api:
        return False
    ctypes = api["ctypes"]
    kernel32 = api["kernel32"]
    entry_type = api["thread_entry_type"]
    try:
        snapshot = kernel32.CreateToolhelp32Snapshot(0x00000004, 0)  # TH32CS_SNAPTHREAD
        if not snapshot or snapshot == ctypes.c_void_p(-1).value:
            return False
        resumed = False
        try:
            entry = entry_type()
            entry.dwSize = ctypes.sizeof(entry)
            has_entry = kernel32.Thread32First(snapshot, ctypes.byref(entry))
            while has_entry:
                if int(entry.th32OwnerProcessID) == int(pid):
                    thread = kernel32.OpenThread(0x0002, False, entry.th32ThreadID)  # THREAD_SUSPEND_RESUME
                    if thread:
                        try:
                            for _ in range(16):
                                previous = int(kernel32.ResumeThread(thread))
                                if previous == 0xFFFFFFFF:
                                    return False
                                resumed = True
                                if previous <= 1:
                                    break
                        finally:
                            kernel32.CloseHandle(thread)
                entry.dwSize = ctypes.sizeof(entry)
                has_entry = kernel32.Thread32Next(snapshot, ctypes.byref(entry))
        finally:
            kernel32.CloseHandle(snapshot)
        return resumed
    except Exception:
        return False


def windows_query_job_peak_memory(handle):
    api = _windows_api()
    if not handle or not api:
        return None
    ctypes = api["ctypes"]
    kernel32 = api["kernel32"]
    info_type = api["info_type"]
    try:
        info = info_type()
        if not kernel32.QueryInformationJobObject(
            handle, 9, ctypes.byref(info), ctypes.sizeof(info), None
        ):
            return None
        peak_bytes = int(info.PeakJobMemoryUsed or info.PeakProcessMemoryUsed or 0)
        return peak_bytes / (1024 * 1024)
    except Exception:
        return None


def close_windows_handle(handle):
    api = _windows_api()
    if not handle or not api:
        return
    try:
        api["kernel32"].CloseHandle(handle)
    except Exception:
        pass


def terminate_windows_job(handle, timeout=2.0):
    """Terminate a Windows Job and wait until every associated process exits."""
    api = _windows_api()
    result = {"ok": False, "tracked": 0, "remaining_pids": [], "errors": []}
    if not handle or not api:
        close_windows_handle(handle)
        result["errors"].append("Windows Job cleanup is unavailable")
        return result
    ctypes = api["ctypes"]
    kernel32 = api["kernel32"]
    accounting_type = api["accounting_type"]
    try:
        if not kernel32.TerminateJobObject(handle, 1):
            raise OSError("cannot terminate Windows Job")
        deadline = time.perf_counter() + float(timeout)
        while True:
            accounting = accounting_type()
            if not kernel32.QueryInformationJobObject(
                handle, 1, ctypes.byref(accounting), ctypes.sizeof(accounting), None
            ):
                raise OSError("cannot confirm Windows Job process exit")
            if int(accounting.ActiveProcesses) == 0:
                result["ok"] = True
                break
            if time.perf_counter() >= deadline:
                raise OSError("Windows Job processes survived cleanup deadline")
            time.sleep(0.01)
    except Exception as exc:
        result["errors"].append(str(exc))
    finally:
        close_windows_handle(handle)
    return result


class _WindowsProcessSnapshot(dict):
    def __init__(self):
        super().__init__()
        self.created = {}


def _windows_process_parents():
    api = _windows_api()
    if not api:
        raise OSError("Windows process enumeration is unavailable")
    ctypes = api["ctypes"]
    entry_type = api["process_information_type"]
    size = 256 * 1024
    while size <= MAX_WINDOWS_SNAPSHOT_BYTES:
        buffer = ctypes.create_string_buffer(size)
        returned = ctypes.c_uint32()
        status = api["query_processes"](5, buffer, size, ctypes.byref(returned)) & 0xFFFFFFFF
        if status == 0xC0000004:  # STATUS_INFO_LENGTH_MISMATCH
            size = max(size * 2, returned.value)
            continue
        if status != 0 or not 0 < returned.value <= size:
            raise OSError(f"cannot snapshot Windows descendants (NTSTATUS {status:#x})")
        parents = _WindowsProcessSnapshot()
        offset = 0
        while offset + ctypes.sizeof(entry_type) <= returned.value:
            entry = entry_type.from_buffer(buffer, offset)
            pid = int(entry.UniqueProcessId or 0)
            parents[pid] = int(entry.InheritedFromUniqueProcessId or 0)
            parents.created[pid] = int(entry.CreateTime)
            step = int(entry.NextEntryOffset)
            if not step:
                return parents
            if step < ctypes.sizeof(entry_type):
                raise OSError("Windows process snapshot has an invalid entry offset")
            offset += step
        raise OSError("Windows process snapshot is truncated")
    raise OSError("Windows process snapshot exceeds its byte limit")


def _windows_process_created(handle):
    api = _windows_api()
    ctypes = api["ctypes"]
    times = [ctypes.c_uint64() for _ in range(4)]
    if not api["kernel32"].GetProcessTimes(handle, *(ctypes.byref(t) for t in times)):
        raise OSError("cannot identify Windows descendant creation time")
    return times[0].value


class _WindowsProcessTree:
    """Pin observed PIDs with handles, including Store redirector breakaways."""

    def __init__(self, proc):
        self.proc = proc
        self.handles = {}
        self.errors = []
        self.pending = {}
        try:
            api = _windows_api()
            ctypes, kernel = api["ctypes"], api["kernel32"]
            current = kernel.GetCurrentProcess()
            owned = ctypes.c_void_p()
            if not kernel.DuplicateHandle(current, int(proc._handle), current, ctypes.byref(owned), 0, False, 2):
                raise OSError("cannot pin managed Windows root handle")
            try:
                self.handles[proc.pid] = (owned.value, _windows_process_created(owned.value))
            except BaseException:
                close_windows_handle(owned.value)
                raise
        except OSError as exc:
            self._error(exc)

    def _error(self, error):
        if len(self.errors) < PROCESS_CLEANUP_DIAGNOSTIC_LIMIT:
            self.errors.append(str(error)[:4096])

    def _open(self, pid):
        kernel = _windows_api()["kernel32"]
        # A held handle pins the process identity even after it exits. Never
        # terminate a descendant by a later, potentially reused raw PID.
        handle = kernel.OpenProcess(0x100000 | 0x1000 | 0x0001, False, pid)
        if not handle:
            error = _windows_api()["ctypes"].get_last_error()
            if error == 87:  # already exited
                return None
            # A Job memory/exit race can revoke termination access before the
            # process object is signalled. Confirm exit with narrower rights;
            # never treat access denied itself as evidence of successful exit.
            observer = kernel.OpenProcess(0x100000 | 0x1000, False, pid)
            if observer:
                try:
                    if kernel.WaitForSingleObject(observer, 100) == 0:
                        return None
                finally:
                    close_windows_handle(observer)
            if pid not in _windows_process_parents():
                return None
            raise OSError(f"cannot open Windows descendant {pid} of root {self.proc.pid} (Windows error {error})")
        try:
            created = _windows_process_created(handle)
        except BaseException:
            close_windows_handle(handle)
            raise
        self.handles[pid] = (handle, created)
        return handle

    def capture(self):
        new = []
        try:
            parents = _windows_process_parents()
            for pid, (parent, _, created) in tuple(self.pending.items()):
                if parents.get(pid) != parent or (
                    created is not None and getattr(parents, "created", {}).get(pid) != created
                ):
                    del self.pending[pid]  # gone or reused; never signal a raw PID
            children = {}
            for pid, parent in parents.items():
                children.setdefault(parent, []).append(pid)
            pending = list(self.handles)
            while pending:
                parent = pending.pop()
                for pid in children.get(parent, ()):
                    if pid in self.handles:
                        continue
                    observed_created = getattr(parents, "created", {}).get(pid)
                    if observed_created is not None and observed_created < self.handles[parent][1]:
                        continue  # old child of a previous occupant of this parent PID
                    if pid not in self.pending and (
                        len(self.handles) + len(self.pending) >= MAX_TRACKED_WINDOWS_PROCESSES
                    ):
                        raise OSError("Windows descendant tracking limit exceeded")
                    try:
                        opened = self._open(pid)
                    except OSError as exc:
                        # Windows fault reporting can temporarily expose a
                        # same-image process clone without termination access.
                        # Retain it as unresolved until exit is confirmed, not
                        # as either a successful cleanup or a permanent error.
                        self.pending[pid] = (parent, str(exc), observed_created)
                        continue
                    self.pending.pop(pid, None)
                    if opened is None:
                        continue
                    if self.handles[pid][1] < self.handles[parent][1]:
                        close_windows_handle(self.handles.pop(pid)[0])
                        continue  # stale parent PID from before this parent existed
                    new.append(pid)
                    pending.append(pid)
            if new:
                # Validate the snapshot after pinning handles: a candidate PID
                # may have been reused between enumeration and OpenProcess.
                current = _windows_process_parents()
                discarded = set()
                for pid in new:
                    if parents[pid] in discarded or (
                        pid in current and current[pid] != parents[pid]
                    ) or (
                        pid in getattr(current, "created", {})
                        and current.created[pid] != self.handles[pid][1]
                    ):
                        close_windows_handle(self.handles.pop(pid)[0])
                        discarded.add(pid)
        except OSError as exc:
            # Never retain a candidate whose post-open identity check failed:
            # it could be an unrelated process that reused a snapshot PID.
            for pid in new:
                if pid in self.handles:
                    close_windows_handle(self.handles.pop(pid)[0])
            self._error(exc)

    def terminate(self, job_handle):
        self.capture()
        kernel = _windows_api()["kernel32"]
        job_result = terminate_windows_job(job_handle) if job_handle else None
        if job_result and not job_result["ok"]:
            for error in job_result["errors"]:
                self._error(error)
        deadline = time.perf_counter() + 2.0
        remaining = []
        try:
            while True:
                for pid, (handle, _) in self.handles.items():
                    state = kernel.WaitForSingleObject(handle, 0)
                    if state == 0x102:  # WAIT_TIMEOUT: still alive
                        # TerminateJobObject/another handle may have initiated
                        # exit already. Only the final bounded wait determines
                        # whether a failed termination request is an error.
                        kernel.TerminateProcess(handle, 1)
                    elif state != 0:
                        self._error(f"cannot confirm Windows descendant {pid} exit")
                # A previously observed redirector can create another process
                # while the root/Job is stopping. Discover it before releasing
                # any identity handles, including when the root already exited.
                self.capture()
                remaining = [
                    pid for pid, (handle, _) in self.handles.items()
                    if kernel.WaitForSingleObject(handle, 0) != 0
                ]
                if (not remaining and not self.pending) or time.perf_counter() >= deadline:
                    break
                time.sleep(0.01)
            if remaining:
                self._error("Windows descendant processes survived cleanup deadline")
            for _, message, _ in self.pending.values():
                self._error(message)
            try:
                self.proc.wait(timeout=2)
            except (OSError, subprocess.TimeoutExpired) as exc:
                self._error(f"cannot reap Windows root process: {exc}")
            return {
                "ok": not self.errors,
                "tracked": max(0, len(self.handles) - 1),
                "remaining_pids": sorted(set(remaining) | set(self.pending))[:PROCESS_CLEANUP_DIAGNOSTIC_LIMIT],
                "errors": list(self.errors),
            }
        finally:
            self.close()

    def close(self):
        for handle, _ in self.handles.values():
            close_windows_handle(handle)
        self.handles.clear()
        self.pending.clear()


def process_alive(pid):
    if not pid:
        return False
    if platform.system() == "Windows":
        try:
            import ctypes
            from ctypes import wintypes

            ERROR_ACCESS_DENIED = 5
            STILL_ACTIVE = 259
            PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
            SYNCHRONIZE = 0x00100000
            WAIT_OBJECT_0 = 0
            kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
            kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
            kernel32.OpenProcess.restype = wintypes.HANDLE
            kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
            kernel32.GetExitCodeProcess.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD)]
            kernel32.GetExitCodeProcess.restype = wintypes.BOOL
            kernel32.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD]
            kernel32.WaitForSingleObject.restype = wintypes.DWORD
            handle = kernel32.OpenProcess(
                PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE, False, int(pid)
            )
            if not handle:
                handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, int(pid))
            if not handle:
                # A live process owned by another user opens with ACCESS_DENIED;
                # report it alive instead of pretending it exited.
                return ctypes.get_last_error() == ERROR_ACCESS_DENIED
            try:
                exit_code = wintypes.DWORD()
                if not kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)):
                    # Cannot query: fall back to the signal probe alone.
                    return kernel32.WaitForSingleObject(handle, 0) != WAIT_OBJECT_0
                # An exited process stays openable while any handle is held
                # (including our own Popen object); a real exit code means dead.
                # This must come first: a just-terminated (e.g. suspended) process
                # reports its exit code before the process object is signalled,
                # and Popen.poll() follows the same exit-code semantics.
                if exit_code.value != STILL_ACTIVE:
                    return False
                # STILL_ACTIVE (259) is ambiguous — a process that exited with
                # code 259 reports it forever; the signalled handle disambiguates.
                return kernel32.WaitForSingleObject(handle, 0) != WAIT_OBJECT_0
            finally:
                kernel32.CloseHandle(handle)
        except Exception:
            return False
    try:
        os.kill(int(pid), 0)
        return True
    except PermissionError:
        return True
    except (OSError, ProcessLookupError):
        return False


def _parse_linux_process_stat(pid, payload):
    close = payload.rfind(")")
    if close < 0:
        raise ValueError("malformed /proc stat")
    fields = payload[close + 2 :].split()
    return _LinuxProcessInfo(
        ppid=int(fields[1]),
        rss_pages=int(fields[21]),
        start_time_ticks=int(fields[19]),
        state=str(fields[0]),
    )


def _read_linux_process_info(pid):
    if platform.system() == "Windows" or not Path("/proc").is_dir():
        return "unavailable", None
    try:
        payload = (Path("/proc") / str(int(pid)) / "stat").read_text(encoding="utf-8")
        return "present", _parse_linux_process_stat(pid, payload)
    except FileNotFoundError:
        return "missing", None
    except OSError as exc:
        if exc.errno in {errno.ENOENT, errno.ESRCH}:
            return "missing", None
        return "unavailable", None
    except (ValueError, IndexError):
        return "unavailable", None


def _linux_process_table():
    if platform.system() == "Windows" or not Path("/proc").is_dir():
        return {}
    table = {}
    try:
        entries = tuple(Path("/proc").iterdir())
    except OSError:
        return {}
    for entry in entries:
        if not entry.name.isdigit():
            continue
        try:
            info = _parse_linux_process_stat(
                int(entry.name), (entry / "stat").read_text(encoding="utf-8")
            )
            table[int(entry.name)] = info
        except (OSError, ValueError, IndexError):
            continue
    return table


def _process_tree_pids_from_table(root_pid, table):
    root_pid = int(root_pid)
    children = {}
    for pid, info in table.items():
        children.setdefault(info.ppid, []).append(pid)
    pending = [root_pid]
    seen = set()
    while pending:
        pid = pending.pop()
        if pid in seen:
            continue
        seen.add(pid)
        pending.extend(children.get(pid, ()))
    return seen


def snapshot_process_tree(root_pid):
    """Return the known root/descendant PIDs where the platform permits it."""
    root_pid = int(root_pid)
    if platform.system() == "Windows":
        return {root_pid}
    table = _linux_process_table()
    if not table:
        return {root_pid}
    return _process_tree_pids_from_table(root_pid, table)


def snapshot_process_tree_identities(root_pid):
    """Return current root/descendant identities where the platform permits it."""
    root_pid = int(root_pid)
    if platform.system() == "Windows":
        return {ProcessIdentity(root_pid)}
    table = _linux_process_table()
    if not table:
        state, info = _read_linux_process_info(root_pid)
        if state != "present":
            return set()
        return {ProcessIdentity(root_pid, info.start_time_ticks)}
    return {
        ProcessIdentity(pid, table[pid].start_time_ticks)
        for pid in _process_tree_pids_from_table(root_pid, table)
        if pid in table
    }


def _identity_status(identity):
    if identity.start_time_ticks is None:
        return "unavailable"
    state, info = _read_linux_process_info(identity.pid)
    if state != "present":
        return "gone" if state == "missing" else "unavailable"
    if info.start_time_ticks != identity.start_time_ticks:
        return "reused"
    if info.state in {"Z", "X", "x"}:
        return "gone"
    return "matching"


def _bounded_identity_map(identities, limit=MAX_TRACKED_POSIX_DESCENDANTS):
    result = {}
    overflow = False
    for identity in sorted(
        identities,
        key=lambda item: (int(item.pid), item.start_time_ticks or -1),
    ):
        if identity.start_time_ticks is None:
            continue
        if identity.pid in result:
            if result[identity.pid] != identity:
                # Keep the first observed identity. Replacing it with a later
                # start-time value could authorize signalling an unrelated
                # process after PID reuse.
                overflow = True
        elif len(result) < int(limit):
            result[identity.pid] = identity
        else:
            overflow = True
    return result, overflow


def _cleanup_identity_snapshot(proc, root_identity, known_identities):
    known, overflow = _bounded_identity_map(known_identities)
    table = _linux_process_table()
    if not table:
        return known, overflow

    roots = []
    root_info = table.get(int(proc.pid))
    root_is_current = (
        root_identity is not None
        and root_info is not None
        and root_identity.start_time_ticks == root_info.start_time_ticks
    )
    # Without an identity captured while the root was alive, a dead Popen PID
    # must not be used to discover a newly-created, unrelated process tree.
    if root_is_current or (root_identity is None and proc.poll() is None):
        roots.append(int(proc.pid))
    for identity in tuple(known.values()):
        info = table.get(identity.pid)
        if info is not None and info.start_time_ticks == identity.start_time_ticks:
            roots.append(identity.pid)

    discovered = []
    for root_pid in roots:
        for pid in _process_tree_pids_from_table(root_pid, table):
            info = table.get(pid)
            if info is not None and pid != int(proc.pid):
                discovered.append(ProcessIdentity(pid, info.start_time_ticks))
    merged, extra_overflow = _bounded_identity_map((*known.values(), *discovered))
    return merged, overflow or extra_overflow


def _signal_matching_identity(identity):
    status = _identity_status(identity)
    if status != "matching":
        return status, None
    if identity.pid == os.getpid():
        return "unsafe", "refused to signal the ProbHub supervisor process"

    # A tracked child may share the supervisor's process group (notably a
    # multiprocessing worker).  Whole-group signalling is guarded below, but
    # an individually identity-checked child is safe to terminate here.  The
    # supervisor PID itself is the only process we must refuse explicitly.

    pidfd_open = getattr(os, "pidfd_open", None)
    pidfd_signal = getattr(signal, "pidfd_send_signal", None)
    if pidfd_open is not None and pidfd_signal is not None:
        descriptor = None
        try:
            descriptor = pidfd_open(identity.pid, 0)
            status = _identity_status(identity)
            if status != "matching":
                return status, None
            pidfd_signal(descriptor, signal.SIGKILL, None, 0)
            return "signalled", None
        except ProcessLookupError:
            return "gone", None
        except OSError as exc:
            return "error", f"cannot terminate pid {identity.pid}: {exc}"
        finally:
            if descriptor is not None:
                try:
                    os.close(descriptor)
                except OSError:
                    pass

    # Python/Linux combinations without pidfd support still revalidate the
    # kernel start-time immediately before signalling. Supported CI targets
    # use pidfds, which close the residual check-to-signal PID reuse race.
    if _identity_status(identity) != "matching":
        return "gone", None
    try:
        os.kill(identity.pid, signal.SIGKILL)
        return "signalled", None
    except ProcessLookupError:
        return "gone", None
    except OSError as exc:
        return "error", f"cannot terminate pid {identity.pid}: {exc}"


def _terminate_posix_process_tree(
    proc,
    *,
    root_identity=None,
    known_identities=(),
    tracking_overflow=False,
):
    identities, snapshot_overflow = _cleanup_identity_snapshot(
        proc, root_identity, known_identities
    )
    errors = []
    root_status = _identity_status(root_identity) if root_identity is not None else None
    root_alive = proc.poll() is None
    # A PID whose Popen has already reaped may have been reused. Only an
    # identity still matching the original root can authorize a late group
    # kill; otherwise cleanup is limited to descendants observed earlier.
    should_kill_group = root_alive or root_status == "matching"
    if should_kill_group:
        try:
            root_group = os.getpgid(int(proc.pid))
            if root_group == os.getpgrp():
                # The root may be a multiprocessing worker in the
                # supervisor's group.  Do not kill the whole group; the root
                # identity and observed descendants are handled individually.
                pass
            else:
                os.killpg(root_group, signal.SIGKILL)
        except ProcessLookupError:
            pass
        except OSError as exc:
            errors.append(f"cannot terminate original process group {proc.pid}: {exc}")

    # A root that shares the supervisor's process group cannot be handled by
    # killpg.  Signal its captured identity individually; if a private group
    # was killed above, this is an idempotent gone/already-signalled check.
    if root_identity is not None:
        status, detail = _signal_matching_identity(root_identity)
        if status in {"error", "unsafe", "unavailable"}:
            errors.append(detail or f"cannot confirm pid {root_identity.pid} identity")

    try:
        proc.wait(timeout=POSIX_CLEANUP_TIMEOUT_SECONDS)
    except (subprocess.TimeoutExpired, OSError):
        # The identity signal above is the normal path for multiprocessing
        # workers.  Keep a final adapter-level fallback for callers whose
        # root identity was unavailable or whose process ignored the signal.
        try:
            proc.kill()
            proc.wait(timeout=1)
        except (OSError, subprocess.TimeoutExpired) as exc:
            errors.append(f"cannot reap managed process {proc.pid}: {exc}")

    for identity in tuple(identities.values()):
        status, detail = _signal_matching_identity(identity)
        if status in {"error", "unsafe", "unavailable"}:
            errors.append(detail or f"cannot confirm pid {identity.pid} identity")

    deadline = time.perf_counter() + POSIX_CLEANUP_TIMEOUT_SECONDS
    remaining = []
    unavailable = []
    while True:
        remaining = []
        unavailable = []
        for identity in identities.values():
            status = _identity_status(identity)
            if status == "matching":
                remaining.append(identity.pid)
            elif status == "unavailable":
                unavailable.append(identity.pid)
        if (not remaining and not unavailable) or time.perf_counter() >= deadline:
            break
        time.sleep(0.01)

    if tracking_overflow or snapshot_overflow:
        errors.append(
            f"observed descendant identity limit exceeded ({MAX_TRACKED_POSIX_DESCENDANTS})"
        )
    if unavailable:
        errors.append(
            "cannot verify descendant identity for pid(s): "
            + ", ".join(str(pid) for pid in unavailable[:PROCESS_CLEANUP_DIAGNOSTIC_LIMIT])
        )
    if remaining:
        errors.append(
            "descendant process(es) survived cleanup: "
            + ", ".join(str(pid) for pid in remaining[:PROCESS_CLEANUP_DIAGNOSTIC_LIMIT])
        )
    bounded_errors = errors[:PROCESS_CLEANUP_DIAGNOSTIC_LIMIT]
    return {
        "ok": not bounded_errors,
        "tracked": len(identities),
        "remaining_pids": remaining[:PROCESS_CLEANUP_DIAGNOSTIC_LIMIT],
        "errors": bounded_errors,
    }


def terminate_external_process_tree(proc, known_identities=(), *, known_pids=None):
    """Force-stop a supervisor process and detached descendant groups."""
    if proc is None:
        return {"ok": True, "tracked": 0, "remaining_pids": [], "errors": []}
    root_pid = int(proc.pid)
    if known_pids is not None and not known_identities:
        # Compatibility for older callers. POSIX treats raw PIDs as stale
        # hints and will only act on identities observed again at termination.
        known_identities = known_pids
    values = tuple(known_identities or ())
    if platform.system() == "Windows":
        pids = {
            int(item.pid) if isinstance(item, ProcessIdentity) else int(item)
            for item in values
        } | snapshot_process_tree(root_pid)
        targets = [root_pid] + sorted((pid for pid in pids if pid != root_pid), reverse=True)
        for pid in targets:
            try:
                subprocess.run(
                    ["taskkill", "/PID", str(pid), "/T", "/F"],
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
                    timeout=5,
                    check=False,
                )
            except (OSError, subprocess.TimeoutExpired):
                pass
        try:
            proc.wait(timeout=2)
        except (OSError, subprocess.TimeoutExpired):
            try:
                proc.kill()
                proc.wait(timeout=1)
            except (OSError, subprocess.TimeoutExpired):
                pass
        return {"ok": True, "tracked": len(pids), "remaining_pids": [], "errors": []}

    identities = [item for item in values if isinstance(item, ProcessIdentity)]
    root_identity = next((item for item in identities if item.pid == root_pid), None)
    # A late snapshot is safe only while the original root is alive, or when a
    # previously captured start-time identity still proves the PID is the same
    # process. Never adopt a reused PID as a new cleanup root.
    root_alive = proc.poll() is None
    if root_alive or (
        root_identity is not None and _identity_status(root_identity) == "matching"
    ):
        identities.extend(snapshot_process_tree_identities(root_pid))
        root_identity = next((item for item in identities if item.pid == root_pid), root_identity)
    return _terminate_posix_process_tree(
        proc,
        root_identity=root_identity,
        known_identities=(item for item in identities if item.pid != root_pid),
    )


def _process_tree_observation(root_pid):
    table = _linux_process_table()
    if not table:
        return None, None, set()
    seen = _process_tree_pids_from_table(root_pid, table)
    rss_pages = sum(table[pid].rss_pages for pid in seen if pid in table)
    identities = {
        ProcessIdentity(pid, table[pid].start_time_ticks)
        for pid in seen
        if pid in table and pid != int(root_pid)
    }
    try:
        page_size = os.sysconf("SC_PAGE_SIZE")
    except (AttributeError, ValueError):
        page_size = 4096
    return len(seen), rss_pages * page_size / (1024 * 1024), identities


def process_tree_metrics(root_pid):
    """Return descendant count and aggregate RSS in MB where /proc is available."""
    count, rss_mb, _ = _process_tree_observation(root_pid)
    return count, rss_mb


@dataclass
class ManagedProcess:
    proc: subprocess.Popen
    job_handle: object = None
    memory_limit_mb: float | None = None
    process_limit: int | None = DEFAULT_PROCESS_LIMIT
    memory_enforced: bool = False
    process_limit_enforced: bool = False
    peak_memory_mb: float | None = None
    root_identity: ProcessIdentity | None = field(default=None, init=False, repr=False)
    known_descendants: dict = field(default_factory=dict, init=False, repr=False)
    identity_tracking_overflow: bool = field(default=False, init=False, repr=False)
    windows_tree: object = field(default=None, init=False, repr=False)

    def __post_init__(self):
        if platform.system() == "Windows":
            self.windows_tree = _WindowsProcessTree(self.proc)
            return
        state, info = _read_linux_process_info(self.proc.pid)
        if state == "present":
            self.root_identity = ProcessIdentity(self.proc.pid, info.start_time_ticks)

    def _remember_descendants(self, identities):
        for identity in sorted(identities, key=lambda item: item.pid):
            if identity.pid in self.known_descendants:
                if self.known_descendants[identity.pid] != identity:
                    self.identity_tracking_overflow = True
            elif len(self.known_descendants) < MAX_TRACKED_POSIX_DESCENDANTS:
                self.known_descendants[identity.pid] = identity
            else:
                self.identity_tracking_overflow = True

    def sample(self):
        if platform.system() == "Windows":
            self.windows_tree.capture()
            current = windows_query_job_peak_memory(self.job_handle)
            count = None
        else:
            count, current, identities = _process_tree_observation(self.proc.pid)
            self._remember_descendants(identities)
        if current is not None:
            self.peak_memory_mb = max(self.peak_memory_mb or 0.0, current)
        return count, current

    def terminate(self):
        if platform.system() == "Windows":
            result = self.windows_tree.terminate(self.job_handle)
        else:
            result = _terminate_posix_process_tree(
                self.proc,
                root_identity=self.root_identity,
                known_identities=self.known_descendants.values(),
                tracking_overflow=self.identity_tracking_overflow,
            )
        self.job_handle = None
        return result

    def close(self):
        close_windows_handle(self.job_handle)
        if self.windows_tree is not None:
            self.windows_tree.close()
        self.job_handle = None


def spawn_managed(
    command,
    *,
    memory_limit_mb=None,
    process_limit=DEFAULT_PROCESS_LIMIT,
    containment_required=True,
    **kwargs,
):
    kwargs = dict(kwargs)
    if "preexec_fn" in kwargs:
        raise ValueError("preexec_fn is not supported; use managed process limits")
    unix_status_fds = None
    if platform.system() == "Windows":
        command, kwargs["env"] = _prepare_windows_python_command(
            command, kwargs.get("env")
        )
        kwargs["creationflags"] = int(kwargs.get("creationflags", 0)) | WINDOWS_CREATE_SUSPENDED
    else:
        kwargs.setdefault("start_new_session", True)
        if memory_limit_mb is not None:
            if kwargs.get("shell"):
                raise ValueError("memory-limited Unix commands do not support shell=True")
            if kwargs.get("executable") is not None:
                raise ValueError("memory-limited Unix commands do not support executable=")
            pass_fds = tuple(kwargs.get("pass_fds") or ())
            command, limit_bytes = _prepare_unix_memory_limited_command(
                command, memory_limit_mb
            )
            read_fd, write_fd = os.pipe()
            unix_status_fds = (read_fd, write_fd)
            try:
                kwargs["pass_fds"] = (*pass_fds, write_fd)
                kwargs["close_fds"] = True
                command = _unix_memory_limited_command(
                    command,
                    limit_bytes,
                    write_fd,
                    kwargs.get("restore_signals", True),
                )
            except BaseException:
                os.close(read_fd)
                os.close(write_fd)
                raise
    try:
        proc = subprocess.Popen(command, **kwargs)
    except BaseException:
        if unix_status_fds is not None:
            os.close(unix_status_fds[0])
            os.close(unix_status_fds[1])
        raise
    if unix_status_fds is not None:
        read_fd, write_fd = unix_status_fds
        os.close(write_fd)
        try:
            _await_unix_exec(proc, read_fd)
        except BaseException:
            _terminate_failed_unix_start(proc)
            raise
        finally:
            os.close(read_fd)
    job_handle = None
    if platform.system() == "Windows":
        started = False
        try:
            job_handle = windows_assign_job(proc, memory_limit_mb, process_limit)
            if containment_required and job_handle is None:
                raise OSError("Windows Job Object containment is unavailable")
            if not windows_resume_process(proc.pid):
                raise OSError("failed to resume suspended Windows process")
            started = True
        finally:
            if not started:
                if job_handle is not None:
                    terminate_windows_job(job_handle)
                    job_handle = None
                try:
                    proc.kill()
                    proc.wait(timeout=2)
                except (OSError, subprocess.TimeoutExpired):
                    pass
    return ManagedProcess(
        proc=proc,
        job_handle=job_handle,
        memory_limit_mb=memory_limit_mb,
        process_limit=process_limit,
        memory_enforced=(job_handle is not None if platform.system() == "Windows" else memory_limit_mb is not None),
        process_limit_enforced=(job_handle is not None if platform.system() == "Windows" else Path("/proc").is_dir()),
    )


def output_path_size(path, *, required=False):
    try:
        info = os.lstat(path)
    except FileNotFoundError:
        if required:
            raise OutputBudgetError(f"required captured output is missing: {path}")
        return 0, False
    except OSError as exc:
        raise OutputBudgetError(f"cannot inspect captured output {path}: {exc}") from exc
    reparse_flag = getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0)
    if stat.S_ISLNK(info.st_mode) or (
        reparse_flag and getattr(info, "st_file_attributes", 0) & reparse_flag
    ):
        raise OutputBudgetError(f"captured output path is a link or reparse point: {path}")
    if not stat.S_ISREG(info.st_mode):
        raise OutputBudgetError(f"captured output path is not a regular file: {path}")
    return int(info.st_size), True


def _files_size(required_paths, optional_paths=()):
    total = 0
    for path in required_paths or ():
        size, _ = output_path_size(path, required=True)
        total += size
    for path in optional_paths or ():
        size, _ = output_path_size(path)
        total += size
    return total


def allocate_shared_prefix_bytes(sizes, limit_bytes):
    """Allocate a deterministic shared prefix budget across byte streams.

    Each stream receives a prefix, with unused quota from short streams
    redistributed before the remaining streams are split evenly. The input
    order breaks odd-byte ties, so stdout/stderr retention is reproducible.
    """
    normalized = [max(int(size), 0) for size in sizes]
    if limit_bytes is None:
        return normalized
    remaining = max(int(limit_bytes), 0)
    allocations = [0] * len(normalized)
    active = [index for index, size in enumerate(normalized) if size]
    while active and remaining:
        share = remaining // len(active)
        if share <= 0:
            break
        short = [index for index in active if normalized[index] <= share]
        if not short:
            break
        for index in short:
            allocations[index] = normalized[index]
            remaining -= normalized[index]
            active.remove(index)
    if active and remaining:
        base, extra = divmod(remaining, len(active))
        for position, index in enumerate(active):
            allocations[index] = min(
                normalized[index], base + (1 if position < extra else 0)
            )
    return allocations


def _truncate_paths(paths, limit_bytes, *, required_count=None):
    if limit_bytes is None:
        return []
    paths = tuple(paths or ())
    if required_count is None:
        required_count = len(paths)
    required_count = max(0, min(int(required_count), len(paths)))
    sizes = []
    existed = []
    for index, path in enumerate(paths):
        size, present = output_path_size(path, required=index < required_count)
        sizes.append(size)
        existed.append(present)
    allocations = allocate_shared_prefix_bytes(sizes, limit_bytes)
    failures = []
    for path, size, allocation in zip(paths, sizes, allocations):
        if size <= allocation:
            continue
        try:
            with open(path, "r+b") as stream:
                stream.truncate(allocation)
        except OSError as exc:
            failures.append(f"{path}: {exc}")
    retained = []
    for index, (path, was_present) in enumerate(zip(paths, existed)):
        try:
            size, _ = output_path_size(
                path, required=was_present or index < required_count
            )
            retained.append(size)
        except OutputBudgetError as exc:
            failures.append(str(exc))
            retained.append(0)
    if failures or sum(retained) > int(limit_bytes):
        detail = "; ".join(failures) or "shared output budget could not be enforced"
        raise OutputBudgetError(detail)
    return retained


def wait_managed(
    managed,
    timeout,
    *,
    output_paths=(),
    optional_output_paths=(),
    output_limit_bytes=None,
    poll_interval=DEFAULT_POLL_INTERVAL,
    cancel_check=None,
):
    """Wait for a managed process and always clean the complete process tree."""
    started = time.perf_counter()
    deadline = started + float(timeout)
    reason = "completed"
    message = ""
    last_resource_sample = started
    returncode = None
    elapsed = 0.0
    output_bytes = 0
    retained_output_bytes = 0
    retained_paths = []
    cleanup = {"ok": True, "tracked": 0, "remaining_pids": [], "errors": []}
    pre_cleanup_reason = None
    output_paths = tuple(output_paths or ())
    optional_output_paths = tuple(optional_output_paths or ())
    all_output_paths = (*output_paths, *optional_output_paths)
    try:
        while managed.proc.poll() is None:
            now = time.perf_counter()
            if _cancellation_requested(cancel_check):
                reason, message = "cancelled", "execution cancelled"
                break
            count = current_memory = None
            if now - last_resource_sample >= 0.05:
                count, current_memory = managed.sample()
                last_resource_sample = now
            if (
                managed.memory_limit_mb is not None
                and current_memory is not None
                and current_memory >= float(managed.memory_limit_mb)
            ):
                reason, message = "memory_limit", "memory limit exceeded"
                break
            if (
                managed.process_limit is not None
                and count is not None
                and count > int(managed.process_limit)
            ):
                reason, message = "process_limit", "process limit exceeded"
                break
            if output_limit_bytes is not None and _files_size(
                output_paths, optional_output_paths
            ) > int(output_limit_bytes):
                reason, message = "output_limit", "output limit exceeded"
                break
            if now >= deadline:
                reason, message = "time_limit", "time limit exceeded"
                break
            time.sleep(min(float(poll_interval), max(deadline - now, 0.0)))

        elapsed = time.perf_counter() - started
        # A short process can exit before the first polling iteration. Recheck
        # file size and deadline after exit so fast output floods cannot bypass OLE.
        observed_output_bytes = _files_size(output_paths, optional_output_paths)
        if _cancellation_requested(cancel_check):
            reason, message = "cancelled", "execution cancelled"
        elif output_limit_bytes is not None and observed_output_bytes > int(output_limit_bytes):
            reason, message = "output_limit", "output limit exceeded"
        elif elapsed > float(timeout) and reason == "completed":
            reason, message = "time_limit", "time limit exceeded"
        returncode = managed.proc.poll()
    finally:
        try:
            observed_cleanup = managed.terminate()
            if isinstance(observed_cleanup, dict):
                cleanup = observed_cleanup
        except Exception as exc:
            cleanup = {
                "ok": False,
                "tracked": 0,
                "remaining_pids": [],
                "errors": [f"process tree cleanup raised an exception: {exc}"],
            }
        if not cleanup.get("ok", False):
            pre_cleanup_reason = reason
            reason = PROCESS_CLEANUP_FAILED
            errors = tuple(str(item) for item in (cleanup.get("errors") or ()))
            message = (errors[0] if errors else "process tree cleanup could not be confirmed")[
                :4096
            ]
        if returncode is None:
            returncode = managed.proc.returncode
        output_bytes = _files_size(output_paths, optional_output_paths)
        if output_limit_bytes is not None:
            retained_paths = _truncate_paths(
                all_output_paths,
                output_limit_bytes,
                required_count=len(output_paths),
            )
            retained_output_bytes = sum(retained_paths)
        else:
            retained_paths = [
                output_path_size(path, required=index < len(output_paths))[0]
                for index, path in enumerate(all_output_paths)
            ]
            retained_output_bytes = sum(retained_paths)

    return {
        "reason": reason,
        "message": message,
        "returncode": returncode,
        "time": elapsed,
        "memory": managed.peak_memory_mb,
        "memory_enforced": managed.memory_enforced,
        "process_limit_enforced": managed.process_limit_enforced,
        "output_bytes": output_bytes,
        "retained_output_bytes": retained_output_bytes,
        "stdout_retained_bytes": retained_paths[0] if retained_paths else 0,
        "stderr_retained_bytes": retained_paths[1] if len(retained_paths) > 1 else 0,
        "output_truncated": retained_output_bytes < output_bytes,
        "cleanup": cleanup,
        "pre_cleanup_reason": pre_cleanup_reason,
    }


def run_managed_to_files(
    command,
    *,
    input_path=None,
    input_data=None,
    stdout_path,
    stderr_path,
    additional_output_paths=(),
    timeout,
    memory_limit_mb=None,
    output_limit_bytes=None,
    process_limit=DEFAULT_PROCESS_LIMIT,
    cwd=None,
    env=None,
    cancel_check=None,
):
    input_stream = None
    temporary_input = None
    try:
        if _cancellation_requested(cancel_check):
            raise ProcessCancelled("execution cancelled")
        if input_path is not None:
            input_stream = open(input_path, "rb")
        elif input_data is not None:
            temporary_input = tempfile.TemporaryFile()
            temporary_input.write(input_data)
            temporary_input.seek(0)
            input_stream = temporary_input
        with open(stdout_path, "wb") as stdout_stream, open(stderr_path, "wb") as stderr_stream:
            managed = spawn_managed(
                command,
                stdin=input_stream or subprocess.DEVNULL,
                stdout=stdout_stream,
                stderr=stderr_stream,
                cwd=cwd,
                env=env,
                memory_limit_mb=memory_limit_mb,
                process_limit=process_limit,
            )
            result = wait_managed(
                managed,
                timeout,
                output_paths=(stdout_path, stderr_path),
                optional_output_paths=tuple(additional_output_paths or ()),
                output_limit_bytes=output_limit_bytes,
                cancel_check=cancel_check,
            )
            if result.get("reason") == "cancelled":
                raise ProcessCancelled(result.get("message") or "execution cancelled")
            return result
    finally:
        if input_stream is not None and input_stream is not temporary_input:
            input_stream.close()
        if temporary_input is not None:
            temporary_input.close()
