Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

123 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""What the Linux sandbox argv and its seccomp program actually say."""

from __future__ import annotations

import ast
import ctypes
import errno
import inspect
import io
import json
import os
import platform
import shlex
import shutil
import socket
import stat
import struct
import subprocess
import sys
import sysconfig
import tempfile
import textwrap
import threading
import time

import pytest

if sys.platform != "linux":
    pytest.skip("the bubblewrap backend is Linux only", allow_module_level = True)

from core.inference import (  # noqa: E402
    os_sandbox,
    sandbox_landlock,
    sandbox_linux,
    sandbox_seccomp,
)
from core.inference.os_sandbox import (  # noqa: E402
    SandboxUnavailableError,
    ToolLaunchPlan,
    WorkdirUnsafeError,
)  # noqa: E402


def _plan(
    workdir,
    argv = ("/bin/true",),
    **kwargs,
):
    return ToolLaunchPlan(argv = argv, workdir = str(workdir), env = {"PATH": "/usr/bin"}, **kwargs)


@pytest.fixture
def bwrap():
    """Skip without bubblewrap, as ``prepared`` does: the test calls the real ``prepare()``, which resolves bwrap first."""
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")


@pytest.fixture
def prepared(tmp_path):
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    launch = sandbox_linux.prepare(_plan(tmp_path))
    yield launch
    launch.cleanup()


def _pairs(argv, flag):
    return [
        (argv[index + 1], argv[index + 2])
        for index, token in enumerate(argv)
        if token == flag and index + 2 < len(argv)
    ]


def test_the_launch_runs_bwrap_and_ends_with_the_payload_after_a_bare_separator(prepared):
    assert os.path.basename(prepared.argv[0]) == "bwrap"
    separator = prepared.argv.index("--")
    assert prepared.argv[separator + 1 :] == ("/bin/true",)
    assert prepared.argv.count("--") == 1


def test_the_network_namespace_is_deliberately_left_alone(prepared):
    assert "--unshare-net" not in prepared.argv
    assert "--unshare-all" not in prepared.argv


def test_every_namespace_except_the_network_one_is_unshared(prepared):
    for option in (
        "--unshare-user",
        "--unshare-pid",
        "--unshare-ipc",
        "--unshare-uts",
        "--unshare-cgroup",
    ):
        assert option in prepared.argv


def test_the_session_dies_with_studio_and_detaches_from_the_terminal(prepared):
    assert "--die-with-parent" in prepared.argv
    assert "--new-session" in prepared.argv


def test_capabilities_are_dropped_and_the_filter_arrives_as_an_inherited_descriptor(prepared):
    argv = prepared.argv
    assert argv[argv.index("--cap-drop") + 1] == "ALL"
    fd = int(argv[argv.index("--seccomp") + 1])
    assert prepared.pass_fds == (fd,)
    assert [handle.fileno() for handle in prepared.owned_files] == [fd]
    assert os.lseek(fd, 0, os.SEEK_CUR) == 0


def test_identity_is_synthesised_rather_than_bound_from_the_host(prepared):
    binds = {destination: source for source, destination in _pairs(prepared.argv, "--ro-bind")}
    passwd, group = binds["/etc/passwd"], binds["/etc/group"]
    assert passwd != "/etc/passwd" and group != "/etc/group"
    assert prepared.cleanup_paths == [os.path.dirname(passwd)]
    assert os.stat(os.path.dirname(passwd)).st_mode & 0o777 == 0o700
    with open(passwd, encoding = "utf-8") as stream:
        entries = stream.read().splitlines()
    assert len(entries) == 1 and entries[0].split(":")[2] == str(os.getuid())
    assert entries[0].split(":")[5] == prepared.argv[prepared.argv.index("HOME") + 1]


@pytest.mark.parametrize("name", ("a:b", "a\nb"))
def test_a_workdir_that_would_corrupt_the_passwd_entry_is_not_its_home(tmp_path, name):
    identity_dir, passwd, _ = sandbox_linux._identity_files(str(tmp_path / name))
    try:
        with open(passwd, encoding = "utf-8") as stream:
            entries = stream.read().splitlines()
        fields = entries[0].split(":")
        assert len(entries) == 1 and len(fields) == 7 and fields[5] == "/nonexistent"
    finally:
        shutil.rmtree(identity_dir)


def test_a_non_utf8_workdir_keeps_its_bytes_as_the_passwd_home(tmp_path):
    home = os.fsdecode(os.fsencode(str(tmp_path)) + b"/caf\xe9")
    identity_dir, passwd, _ = sandbox_linux._identity_files(home)
    try:
        with open(passwd, "rb") as stream:
            assert stream.read().split(b":")[5] == os.fsencode(home)
    finally:
        shutil.rmtree(identity_dir)


def test_the_writable_workdir_bind_lands_after_the_root_goes_read_only(prepared, tmp_path):
    argv = prepared.argv
    workdir = os.path.realpath(tmp_path)
    remount = argv.index("--remount-ro")
    assert argv[remount + 1] == "/"
    assert argv.index("--dir", 0, remount) < remount
    assert (workdir, workdir) in _pairs(argv[remount:], "--bind")
    assert argv[argv.index("--chdir") + 1] == workdir


def test_both_spellings_of_a_symlinked_workdir_get_a_mount_point(bwrap, tmp_path):
    real = tmp_path / "real"
    real.mkdir()
    link = tmp_path / "link"
    link.symlink_to(real)

    launch = sandbox_linux.prepare(
        ToolLaunchPlan(
            argv = (sys.executable, "-c", "pass"),
            workdir = str(link),
            env = {"PATH": "/usr/bin:/bin"},
        )
    )
    try:
        argv = launch.argv
        remount = argv.index("--remount-ro")
        made = [argv[i + 1] for i, token in enumerate(argv[:remount]) if token == "--dir"]
        assert str(link) in made, "the caller's spelling has no mount point"
        assert os.path.realpath(link) in made, "the canonical spelling has no mount point"
        bound = _pairs(argv[remount:], "--bind")
        assert (os.path.realpath(link), str(link)) in bound
    finally:
        launch.cleanup()


def test_the_private_tmpfs_replaces_the_shared_directories(prepared):
    argv = prepared.argv
    remount = argv.index("--remount-ro")
    tmpfs = [argv[i + 1] for i, token in enumerate(argv) if token == "--tmpfs"]
    assert tmpfs[:2] == ["/dev/shm", "/tmp"]
    assert argv.index("--tmpfs") > remount


def test_no_private_key_directory_enters_the_jail_at_all(prepared):
    sources = [source for source, _ in _pairs(prepared.argv, "--ro-bind-try")]
    assert "/etc/ssl/certs" in sources
    assert "/etc/ssl" not in sources and "/etc/pki" not in sources
    secret = "/etc/ssl/private"
    assert os.path.isdir(secret), "no private-key directory here, so this proves nothing"
    for source, _ in (*_pairs(prepared.argv, "--ro-bind-try"), *_pairs(prepared.argv, "--ro-bind")):
        assert not sandbox_linux._within(secret, source), source


def test_distro_jdk_configuration_is_bound_but_its_credentials_are_not(tmp_path, monkeypatch):
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    etc = tmp_path / "etc"
    fedora = etc / "java" / "java-25-openjdk" / "java-25-openjdk"
    roots = (etc / "java-21-openjdk", fedora / "conf", fedora / "lib", etc / "java21-openjdk")
    secrets = []
    for root in roots:
        (root / "security").mkdir(parents = True)
        marker = "default.policy" if root == fedora / "lib" else "java.security"
        (root / "security" / marker).write_text("", encoding = "utf-8")
        (root / "net.properties").write_text("", encoding = "utf-8")
        (root / "management").mkdir()
        secrets += [root / "management" / "jmxremote.password", root / "keystore.p12"]
    (etc / "java-21-openjdk" / "jvm-amd64.cfg").write_text("", encoding = "utf-8")
    outside = tmp_path / "outside"
    outside.mkdir()
    secrets.append(outside / "private-key")
    service = etc / "java-service"
    (service / "security").mkdir(parents = True)
    secrets += [service / "security" / "token", service / "app.properties"]
    for secret in secrets:
        secret.write_text("SECRET", encoding = "utf-8")
    (outside / "java.security").write_text("", encoding = "utf-8")
    linked = etc / "java-8-openjdk"
    linked.mkdir()
    (linked / "security").symlink_to(outside)
    (linked / "net.properties").symlink_to(outside / "private-key")
    elsewhere = tmp_path / "elsewhere"
    (elsewhere / "security").mkdir(parents = True)
    (elsewhere / "security" / "java.security").write_text("", encoding = "utf-8")
    secrets.append(elsewhere / "security" / "credential")
    secrets[-1].write_text("SECRET", encoding = "utf-8")
    linked_top = etc / "java-17-openjdk"
    linked_top.symlink_to(elsewhere)
    linked_marker = etc / "java-11-openjdk"
    (linked_marker / "security").mkdir(parents = True)
    (linked_marker / "security" / "java.security").symlink_to(outside / "java.security")
    linked_file = etc / "java-21-openjdk" / "linked.properties"
    linked_file.symlink_to(outside / "private-key")
    monkeypatch.setattr(sandbox_linux, "_ETC_JAVA_GLOB", str(etc / "java*"))
    workdir = tmp_path / "session"
    workdir.mkdir()
    launch = sandbox_linux.prepare(_plan(workdir))
    try:
        read_only = _pairs(launch.argv, "--ro-bind-try")
        for path in (
            *(root / name for root in roots for name in ("security", "net.properties")),
            etc / "java-21-openjdk" / "jvm-amd64.cfg",
        ):
            assert (str(path), str(path)) in read_only
        for flag in ("--bind", "--bind-try", "--ro-bind", "--ro-bind-try"):
            for source, _ in _pairs(launch.argv, flag):
                for link in (linked, linked_top, linked_marker, linked_file):
                    assert not sandbox_linux._within(source, str(link)), source
                for secret in secrets:
                    assert not sandbox_linux._within(str(secret), source), (secret, source)
    finally:
        launch.cleanup()


def test_a_host_jvm_initialises_its_security_properties_inside_the_jail(tmp_path):
    java = shutil.which("java")
    if sandbox_linux.shutil.which("bwrap") is None or java is None:
        pytest.skip("needs bubblewrap and a Java runtime")
    payload = (java, "-XshowSettings:security:properties", "-version")
    if subprocess.run(payload, capture_output = True).returncode != 0:
        pytest.skip("this JVM does not support -XshowSettings:security")

    def run(argv):
        launch = sandbox_linux.prepare(_plan(tmp_path, argv = argv))
        try:
            return subprocess.run(
                launch.argv,
                env = launch.env,
                pass_fds = launch.pass_fds,
                preexec_fn = launch.preexec_fn,
                capture_output = True,
                text = True,
                timeout = 60,
            )
        finally:
            launch.cleanup()

    if run(("/bin/true",)).returncode != 0:
        pytest.skip("bubblewrap cannot create a sandbox on this host")
    completed = run(payload)
    assert completed.returncode == 0, completed.stderr
    assert "Error loading java.security" not in completed.stderr
    keytool = os.path.join(os.path.dirname(os.path.realpath(java)), "keytool")
    if os.access(keytool, os.X_OK):
        completed = run((keytool, "-list", "-cacerts", "-storepass", "changeit"))
        assert completed.returncode == 0, completed.stdout + completed.stderr
    # Java reads user.home from getpwuid(), not $HOME.
    properties = run((java, "-XshowSettings:properties", "-version")).stderr
    assert f"user.home = {tmp_path}" in properties, properties


def test_pip_gets_a_writable_prefix_inside_the_workdir(prepared):
    argv = prepared.argv
    packages = os.path.join(prepared.workdir, sandbox_linux.SESSION_PACKAGES_RELPATH)
    assert argv[argv.index("PIP_PREFIX") + 1] == packages
    assert argv[argv.index("PIP_IGNORE_INSTALLED") + 1] == "1"
    assert "PIP_TARGET" not in argv
    pythonpath = argv[argv.index("PYTHONPATH") + 1].split(os.pathsep)
    assert pythonpath[-1] == packages
    assert (
        sysconfig.get_path("purelib", vars = {"base": packages, "platbase": packages}) in pythonpath
    )


def test_prefix_dirs_ignore_scheme_paths_on_another_drive(monkeypatch):
    from core.inference import os_sandbox

    real = os_sandbox.sysconfig.get_paths

    def get_paths(scheme, vars):
        # relpath raises on nt for a path on another drive; None makes it raise here too.
        return {**real(scheme, vars = vars), "stdlib": None, "include": None}

    monkeypatch.setattr(os_sandbox.sysconfig, "get_paths", get_paths)
    assert os_sandbox._pip_prefix_dirs() == (
        os_sandbox._PIP_PREFIX_LIB_DIRS,
        os_sandbox._PIP_PREFIX_SCRIPTS_DIR,
    )


def test_commands_follow_the_prefix_schemes_scripts_dir(monkeypatch):
    from core.inference import os_sandbox

    monkeypatch.setattr(os_sandbox, "_PIP_PREFIX_SCRIPTS_DIR", os.path.join("local", "bin"))
    path = sandbox_linux._path(_plan("/w"), "/w/p").split(os.pathsep)
    assert path == ["/usr/bin", "/w/p/local/bin", "/w/p/bin"]


def _wheel(directory, name, version):
    info = f"{name}-{version}.dist-info"
    files = {
        f"shared/{name}.py": f"VERSION = {version!r}\ndef main():\n    print(VERSION)\n",
        f"{info}/METADATA": f"Metadata-Version: 2.1\nName: {name}\nVersion: {version}\n",
        f"{info}/WHEEL": "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n",
        f"{info}/entry_points.txt": f"[console_scripts]\n{name} = shared.{name}:main\n",
    }
    import zipfile

    with zipfile.ZipFile(directory / f"{name}-{version}-py3-none-any.whl", "w") as wheel:
        for path, text in files.items():
            wheel.writestr(path, text)
        wheel.writestr(f"{info}/RECORD", "".join(f"{p},,\n" for p in [*files, f"{info}/RECORD"]))


def test_successive_pip_installs_in_the_jail_keep_every_command_module_and_version(tmp_path):
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    workdir = tmp_path / "session"
    wheels = workdir / "wheels"
    wheels.mkdir(parents = True)
    for name, version in (("alpha", "1.0"), ("beta", "1.0"), ("beta", "2.0")):
        _wheel(wheels, name, version)
    env = {
        "PATH": "/usr/bin:/bin",
        "PYTHONPATH": os.path.join(os.path.dirname(sandbox_linux.__file__), "sandbox_site"),
        "PIP_DISABLE_PIP_VERSION_CHECK": "1",
    }

    def run(*argv):
        launch = sandbox_linux.prepare(ToolLaunchPlan(argv = argv, workdir = str(workdir), env = env))
        try:
            return subprocess.run(
                launch.argv,
                env = launch.env,
                pass_fds = launch.pass_fds,
                preexec_fn = launch.preexec_fn,
                capture_output = True,
                text = True,
                timeout = 180,
            )
        finally:
            launch.cleanup()

    if run("/bin/true").returncode != 0:
        pytest.skip("bubblewrap cannot create a sandbox on this host")
    if run(sys.executable, "-m", "pip", "--version").returncode != 0:
        pytest.skip(
            reason = "pip is not installed in this interpreter, so there is no install to run"
        )
    for spec in ("alpha", "beta==1.0", "beta==2.0"):
        done = run(
            sys.executable, "-m", "pip", "install", "--no-index", "--find-links", str(wheels), spec
        )
        assert done.returncode == 0, done.stdout + done.stderr
    probe = "import shared.alpha, shared.beta, importlib.metadata as m; print(shared.beta.VERSION, m.version('beta'))"
    seen = run(sys.executable, "-c", probe)
    assert seen.stdout.split() == ["2.0", "2.0"], seen.stdout + seen.stderr
    commands = run("/bin/sh", "-c", "alpha && beta")
    assert commands.stdout.split() == ["1.0", "2.0"], commands.stdout + commands.stderr


def test_system_directories_are_bound_whole_and_never_file_by_file(prepared):
    sources = [source for source, _ in _pairs(prepared.argv, "--ro-bind-try")]
    assert "/usr/lib" in sources
    named = {
        *sandbox_linux._ETC_FILES,
        *sandbox_linux._etc_java_binds(),
        *sandbox_linux._ETC_FILES_IF_TRUSTED,
        *sandbox_linux._NETWORK_FILES,
    }
    identity_dir = prepared.cleanup_paths[0]
    for flag in ("--bind", "--ro-bind", "--ro-bind-try"):
        for source, _ in _pairs(prepared.argv, flag):
            if os.path.isdir(source):
                continue
            if source in named or os.path.dirname(source) == identity_dir:
                continue
            assert os.path.basename(source) == "pyvenv.cfg", source


def test_the_interpreter_contributes_only_a_handful_of_read_only_binds(prepared):
    runtime = [
        source
        for source, destination in _pairs(prepared.argv, "--ro-bind")
        if source == destination
    ]
    assert 0 < len(runtime) < 40, runtime
    system_roots = tuple(path for path in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(path))
    for path in runtime:
        assert not any(sandbox_linux._within(path, root) for root in system_roots)


def test_nothing_binds_or_creates_the_filesystem_root(prepared):
    for flag in ("--bind", "--ro-bind", "--ro-bind-try"):
        for source, destination in _pairs(prepared.argv, flag):
            assert source != "/" and destination != "/"
    directories = [
        prepared.argv[index + 1] for index, token in enumerate(prepared.argv) if token == "--dir"
    ]
    assert "/" not in directories


def test_a_launch_with_no_command_is_refused_rather_than_handed_to_bwrap(bwrap, tmp_path):
    with pytest.raises(SandboxUnavailableError, match = "needs a command"):
        sandbox_linux.prepare(_plan(tmp_path, argv = ()))


def test_a_path_bwrap_from_a_writable_location_is_never_executed(tmp_path, monkeypatch):
    planted = tmp_path / "bwrap"
    planted.write_text("#!/bin/sh\nexit 0\n", encoding = "utf-8")
    planted.chmod(0o777)
    monkeypatch.setattr(sandbox_linux.shutil, "which", lambda _name: str(planted))

    with pytest.raises(SandboxUnavailableError, match = "trusted system installation"):
        sandbox_linux.prepare(_plan(tmp_path))


_NIX_BWRAP = "/nix/store/abc-bubblewrap/bin/bwrap"
_OVERFLOW = 65534


def _fake_tree(
    monkeypatch,
    entries,
    *,
    found = _NIX_BWRAP,
    overflow = None,
):
    """Point the trust check at `found` over `entries` ({path: (mode, uid)}), in a namespace if `overflow`."""
    real_stat, real_access = os.stat, os.access

    def fake_stat(path, *args, **kwargs):
        if os.fspath(path) not in entries:
            return real_stat(path, *args, **kwargs)
        mode, uid = entries[os.fspath(path)]
        return os.stat_result((mode, 1, 1, 1, uid, 0, 0, 0, 0, 0))

    monkeypatch.setattr(sandbox_linux.shutil, "which", lambda _name: found)
    monkeypatch.setattr(sandbox_linux.os.path, "realpath", lambda path, **_kw: path)
    monkeypatch.setattr(sandbox_linux.os, "stat", fake_stat)
    monkeypatch.setattr(
        sandbox_linux.os, "access", lambda path, mode: path in entries or real_access(path, mode)
    )
    monkeypatch.setattr(sandbox_linux, "_unmapped_uid", lambda: overflow)
    monkeypatch.delenv(sandbox_linux.BWRAP_PATH_ENV, raising = False)


def _nix_tree(
    store_mode = stat.S_ISVTX | 0o775,
    owner = 0,
    store_owner = 0,
):
    return {
        "/": (stat.S_IFDIR | 0o755, owner),
        "/nix": (stat.S_IFDIR | 0o755, owner),
        "/nix/store": (stat.S_IFDIR | store_mode, store_owner),
        "/nix/store/abc-bubblewrap": (stat.S_IFDIR | 0o555, owner),
        "/nix/store/abc-bubblewrap/bin": (stat.S_IFDIR | 0o555, owner),
        _NIX_BWRAP: (stat.S_IFREG | 0o555, owner),
    }


def test_a_nix_store_bwrap_is_trusted(monkeypatch):
    # /nix/store is root:nixbld 1775: the sticky bit stops build users from replacing root's entries.
    _fake_tree(monkeypatch, _nix_tree())
    assert sandbox_linux._trusted_bwrap_path() == _NIX_BWRAP


@pytest.mark.parametrize(
    "store_mode, store_owner",
    [(0o775, 0), (0o777, 0), (stat.S_ISVTX | 0o777, 1000)],
    ids = ["group-writable", "world-writable", "sticky-but-user-owned"],
)
def test_a_replaceable_store_is_still_refused(monkeypatch, store_mode, store_owner):
    _fake_tree(monkeypatch, _nix_tree(store_mode, store_owner = store_owner))
    with pytest.raises(sandbox_linux.BwrapUntrustedError, match = "replaceable: /nix/store\\)"):
        sandbox_linux._trusted_bwrap_path()


def _fhs_tree():
    """An FHS env (appimageTools, nixpkgs wrappers): / is the user's tmpfs, host files show as the overflow uid."""
    tree = _nix_tree(owner = _OVERFLOW, store_owner = _OVERFLOW)
    tree["/"] = (stat.S_IFDIR | 0o755, 1000)
    return tree


def test_a_namespace_hidden_owner_names_the_pin_instead_of_an_install(monkeypatch):
    _fake_tree(monkeypatch, _fhs_tree(), found = "/usr/bin/bwrap", overflow = _OVERFLOW)
    monkeypatch.setattr(sandbox_linux.os.path, "realpath", lambda _path, **_kw: _NIX_BWRAP)
    with pytest.raises(sandbox_linux.BwrapUntrustedError) as refused:
        sandbox_linux._trusted_bwrap_path()
    assert refused.value.hidden_root and not refused.value.pinned
    # The stable PATH spelling, not the store path a rebuild would change.
    assert f"{sandbox_linux.BWRAP_PATH_ENV}=/usr/bin/bwrap" in str(refused.value)
    assert "package manager" not in str(refused.value)


def test_a_pin_vouches_for_the_path_and_the_namespace_overflow_owner(monkeypatch):
    _fake_tree(monkeypatch, _fhs_tree(), found = None, overflow = _OVERFLOW)
    monkeypatch.setenv(sandbox_linux.BWRAP_PATH_ENV, _NIX_BWRAP)
    assert sandbox_linux._trusted_bwrap_path() == _NIX_BWRAP


@pytest.mark.parametrize(
    "owner, overflow",
    [(_OVERFLOW, None), (1000, _OVERFLOW)],
    ids = ["real-nobody-outside-a-namespace", "user-owned-in-a-namespace"],
)
def test_a_pin_is_still_held_to_the_ownership_checks(monkeypatch, owner, overflow):
    tree = _nix_tree()
    tree[_NIX_BWRAP] = (stat.S_IFREG | 0o555, owner)
    _fake_tree(monkeypatch, tree, overflow = overflow)
    monkeypatch.setenv(sandbox_linux.BWRAP_PATH_ENV, _NIX_BWRAP)
    with pytest.raises(
        sandbox_linux.BwrapUntrustedError, match = sandbox_linux.BWRAP_PATH_ENV
    ) as refused:
        sandbox_linux._trusted_bwrap_path()
    assert refused.value.pinned


def test_a_relative_pin_is_refused(monkeypatch):
    _fake_tree(monkeypatch, _nix_tree())
    monkeypatch.setenv(sandbox_linux.BWRAP_PATH_ENV, "bin/bwrap")
    with pytest.raises(sandbox_linux.BwrapUntrustedError, match = "must be an absolute path"):
        sandbox_linux._trusted_bwrap_path()


@pytest.mark.parametrize(
    "uid_map, euid, expected",
    [
        ("         0          0 4294967295\n", 1000, None),
        ("      1000       1000          1\n", 1000, _OVERFLOW),
        ("         0       1000          1\n", 0, _OVERFLOW),
        ("", _OVERFLOW, None),
        ("         0       1000          1\n         1     100000      65536\n", 0, None),
        ("         0          0     100000\n", 1000, None),
    ],
    ids = [
        "initial-namespace",
        "current-user-mapped",
        "mapped-to-root",
        "unmapped-itself",
        "rootless-subuid-range-maps-it",
        "partial-identity-map-covers-it",
    ],
)
def test_the_overflow_uid_counts_only_inside_a_namespace(monkeypatch, uid_map, euid, expected):
    files = {"/proc/self/uid_map": uid_map, "/proc/sys/kernel/overflowuid": f"{_OVERFLOW}\n"}
    real_open = open
    monkeypatch.setattr(
        sandbox_linux,
        "open",
        lambda path, *a, **kw: io.StringIO(files[path])
        if path in files
        else real_open(path, *a, **kw),
        raising = False,
    )
    monkeypatch.setattr(sandbox_linux.os, "geteuid", lambda: euid)
    assert sandbox_linux._unmapped_uid() == expected


def test_the_workdir_is_the_only_writable_bind(prepared, tmp_path):
    workdir = os.path.realpath(tmp_path)
    assert _pairs(prepared.argv, "--bind") == [(workdir, workdir)]


def test_the_model_cache_shares_its_data_subdirectories_and_nothing_else(
    bwrap, tmp_path, monkeypatch
):
    cache = tmp_path / "hostcache"
    for name in ("hub", "datasets", "modules", "xet", "assets"):
        (cache / name).mkdir(parents = True)
    (cache / "token").write_text("hf_A_REAL_LOOKING_TOKEN")
    (cache / "stored_tokens").write_text("{}")
    _share_cache(monkeypatch, cache)

    launch = sandbox_linux.prepare(_plan(tmp_path))
    try:
        workdir = os.path.realpath(tmp_path)
        inner = os.path.join(workdir, ".cache", "huggingface")
        shared = _pairs(launch.argv, "--bind-try")
        assert sorted(shared) == sorted(
            (os.path.join(str(cache), name), os.path.join(inner, name))
            for name in ("hub", "datasets", "xet", "assets")
        )
        assert not any("modules" in destination for _, destination in shared)
        assert launch.argv[launch.argv.index("HF_HOME") + 1] == inner
        for credential in ("token", "stored_tokens"):
            host_path = os.path.join(str(cache), credential)
            assert not any(host_path in token for token in launch.argv), credential
    finally:
        launch.cleanup()


def test_the_cache_mount_points_are_made_here_and_left(bwrap, tmp_path, cache):
    workdir = tmp_path / "session"
    workdir.mkdir()

    launch = sandbox_linux.prepare(_plan(workdir))
    launch.cleanup()
    assert (workdir / ".cache" / "huggingface" / "hub").is_dir()
    second = sandbox_linux.prepare(_plan(workdir))
    second.cleanup()
    assert (workdir / ".cache" / "huggingface" / "hub").is_dir()


def test_a_cache_directory_the_tool_call_wrote_is_left_alone(bwrap, tmp_path, cache):
    workdir = tmp_path / "session"
    (workdir / ".cache" / "huggingface").mkdir(parents = True)
    (workdir / ".cache" / "notes.txt").write_text("the user's")

    launch = sandbox_linux.prepare(_plan(workdir))
    launch.cleanup()
    assert (workdir / ".cache" / "notes.txt").read_text() == "the user's"


def test_the_model_cache_bind_is_absent_when_the_host_has_no_cache(bwrap, tmp_path, monkeypatch):
    monkeypatch.setattr(sandbox_linux, "_model_cache_binds", lambda workdir: {})
    launch = sandbox_linux.prepare(_plan(tmp_path))
    try:
        assert "HF_HOME" not in launch.argv
        assert [
            p for p in _pairs(launch.argv, "--bind") if p[0] != os.path.realpath(tmp_path)
        ] == []
    finally:
        launch.cleanup()


def test_the_inner_home_and_tmpdir_are_set_by_bwrap_not_by_the_caller_environment(prepared):
    argv = prepared.argv
    assert argv[argv.index("HOME") + 1] == prepared.workdir
    assert argv[argv.index("TMPDIR") + 1] == "/tmp"
    assert prepared.env == {"PATH": "/usr/bin"}


def test_a_tmpdir_inside_the_workdir_survives_into_the_jail(bwrap, tmp_path):
    scratch = tmp_path / "unsloth-tmp"
    scratch.mkdir()
    plan = ToolLaunchPlan(
        argv = ("/bin/true",),
        workdir = str(tmp_path),
        env = {"PATH": "/usr/bin", "TMPDIR": str(scratch)},
    )
    launch = sandbox_linux.prepare(plan)
    try:
        assert launch.argv[launch.argv.index("TMPDIR") + 1] == str(scratch)
    finally:
        launch.cleanup()


def test_a_tmpdir_outside_the_workdir_is_replaced_by_the_private_tmpfs(bwrap, tmp_path):
    plan = ToolLaunchPlan(
        argv = ("/bin/true",),
        workdir = str(tmp_path),
        env = {"PATH": "/usr/bin", "TMPDIR": "/var/tmp/somewhere-else"},
    )
    launch = sandbox_linux.prepare(plan)
    try:
        assert launch.argv[launch.argv.index("TMPDIR") + 1] == "/tmp"
    finally:
        launch.cleanup()


def test_the_outer_setsid_preexec_is_preserved(bwrap, tmp_path):
    ran = []
    launch = sandbox_linux.prepare(_plan(tmp_path, preexec_fn = lambda: ran.append("plan")))
    try:
        assert launch.preexec_fn is not None
        launch.preexec_fn()
        assert ran == ["plan"]
    finally:
        launch.cleanup()


def test_the_plan_policy_fields_survive_preparation(bwrap, tmp_path):
    launch = sandbox_linux.prepare(_plan(tmp_path, timeout_seconds = 42, terminate_descendants = False))
    try:
        assert launch.timeout_seconds == 42
        assert launch.terminate_descendants is False
        assert launch.backend == sandbox_linux.BACKEND_NAME
    finally:
        launch.cleanup()


def test_cleanup_releases_the_descriptor_and_the_private_identity_directory(bwrap, tmp_path):
    launch = sandbox_linux.prepare(_plan(tmp_path))
    identity_dir = launch.cleanup_paths[0]
    handle = launch.owned_files[0]
    launch.cleanup()
    assert handle.closed
    assert not os.path.exists(identity_dir)
    assert launch.cleanup_diagnostics == []


def test_the_backend_declares_the_two_things_it_does_not_confine():
    assert "unrestricted_network" in sandbox_linux.LIMITATIONS
    assert "model_cache_writable" in sandbox_linux.LIMITATIONS
    assert isinstance(sandbox_linux.LIMITATIONS, tuple)
    assert sandbox_linux.BACKEND_NAME and sandbox_linux.PROFILE_ID


def test_a_unix_socket_under_the_workdir_is_refused():
    workdir = tempfile.mkdtemp(prefix = "sbx-")
    listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    try:
        listener.bind(os.path.join(workdir, "s"))
        with pytest.raises(SandboxUnavailableError, match = "device or IPC node"):
            sandbox_linux._validate_workdir(workdir)
    finally:
        listener.close()
        shutil.rmtree(workdir, ignore_errors = True)


def test_a_fifo_under_the_workdir_is_refused(tmp_path):
    os.mkfifo(str(tmp_path / "pipe"))
    with pytest.raises(SandboxUnavailableError, match = "device or IPC node"):
        sandbox_linux._validate_workdir(str(tmp_path))


def test_a_file_hard_linked_from_outside_the_workdir_is_refused(tmp_path):
    outside, workdir = tmp_path / "outside", tmp_path / "session"
    outside.mkdir()
    workdir.mkdir()
    (outside / "secret").write_text("x")
    os.link(str(outside / "secret"), str(workdir / "innocent"))
    with pytest.raises(SandboxUnavailableError, match = "hard-linked from outside"):
        sandbox_linux._validate_workdir(str(workdir))


def test_a_hard_link_wholly_inside_the_workdir_is_allowed(tmp_path):
    (tmp_path / "a").write_text("x")
    os.link(str(tmp_path / "a"), str(tmp_path / "b"))
    (tmp_path / "nested").mkdir()
    os.link(str(tmp_path / "a"), str(tmp_path / "nested" / "c"))
    assert sandbox_linux._validate_workdir(str(tmp_path)) == (os.path.realpath(tmp_path), ())


def test_a_nested_host_mount_under_the_workdir_is_refused(tmp_path, monkeypatch):
    nested = tmp_path / "mounted"
    nested.mkdir()
    identity = (nested.stat().st_dev, nested.stat().st_ino)
    real = os.lstat

    def lstat(path, *args, **kwargs):
        # A mount answers with its own device; path/.. still answers with the host's.
        info = real(path, *args, **kwargs)
        if (info.st_dev, info.st_ino) != identity:
            return info
        fields = list(info)
        fields[2] = info.st_dev + 1
        return os.stat_result(fields, {"st_mtime_ns": info.st_mtime_ns})

    monkeypatch.setattr(os, "lstat", lstat)
    with pytest.raises(SandboxUnavailableError, match = "nested host mount"):
        sandbox_linux._validate_workdir(str(tmp_path))


def test_the_workdir_itself_being_a_mount_point_is_allowed(tmp_path, monkeypatch):
    real = os.path.ismount
    monkeypatch.setattr(
        os.path, "ismount", lambda path: os.path.samefile(path, tmp_path) or real(path)
    )
    assert sandbox_linux._validate_workdir(str(tmp_path)) == (os.path.realpath(tmp_path), ())


def test_a_workdir_too_large_to_check_still_launches_and_says_it_was_not_checked(
    tmp_path, monkeypatch
):
    monkeypatch.setattr(os_sandbox, "WORKDIR_SCAN_ENTRIES", 2)
    for name in ("a", "b", "c", "d"):
        (tmp_path / name).write_text("")
    resolved, limitations = sandbox_linux._validate_workdir(str(tmp_path))
    assert resolved == os.path.realpath(tmp_path)
    assert limitations == ("workdir_scan_incomplete",)


def test_an_overrun_does_not_stop_a_real_finding_from_refusing(tmp_path, monkeypatch):
    os.mkfifo(str(tmp_path / "channel"))
    with pytest.raises(SandboxUnavailableError, match = "device or IPC node"):
        sandbox_linux._validate_workdir(str(tmp_path))


def test_an_incomplete_scan_reaches_the_execution_record(bwrap, tmp_path, monkeypatch):
    monkeypatch.setattr(os_sandbox, "WORKDIR_SCAN_ENTRIES", 2)
    for name in ("a", "b", "c", "d"):
        (tmp_path / name).write_text("")
    prepared = sandbox_linux.prepare(
        os_sandbox.ToolLaunchPlan(
            argv = (sys.executable, "-c", "pass"),
            workdir = str(tmp_path),
            env = {},
            requested_mode = "required",
        )
    )
    try:
        assert "workdir_scan_incomplete" in prepared.launch_limitations
    finally:
        prepared.cleanup()


def test_a_workdir_that_is_not_a_directory_is_refused(tmp_path):
    with pytest.raises(SandboxUnavailableError, match = "not a safe canonical directory"):
        sandbox_linux._validate_workdir(str(tmp_path / "missing"))
    with pytest.raises(SandboxUnavailableError, match = "not a safe canonical directory"):
        sandbox_linux._validate_workdir("/")


def test_a_symlinked_directory_under_the_workdir_is_not_followed(tmp_path):
    (tmp_path / "escape").symlink_to("/dev")
    assert sandbox_linux._validate_workdir(str(tmp_path)) == (os.path.realpath(tmp_path), ())


def test_every_interpreter_path_this_python_imports_from_is_bound(prepared, tmp_path):
    bound = [
        source
        for flag in ("--ro-bind", "--ro-bind-try", "--bind")
        for source, _ in _pairs(prepared.argv, flag)
    ]
    prefixes = (sys.prefix, sys.base_prefix, sys.exec_prefix, sys.base_exec_prefix)
    paths = sysconfig.get_paths()
    needed = {
        paths[key] for key in ("stdlib", "platstdlib", "purelib", "platlib") if paths.get(key)
    }
    needed |= {
        entry
        for entry in sys.path
        if entry and any(sandbox_linux._within(entry, os.path.join(p, "lib")) for p in prefixes)
    }
    for path in sorted(needed):
        if not os.path.exists(path):
            continue
        assert any(sandbox_linux._within(path, root) for root in bound), path


def test_a_venv_at_a_project_root_does_not_expose_the_project(tmp_path, monkeypatch):
    project = tmp_path / "project"
    (project / "lib").mkdir(parents = True)
    (project / "bin").mkdir()
    (project / "src").mkdir()
    (project / ".env").write_text("TOKEN=secret")
    for name in ("prefix", "exec_prefix"):
        monkeypatch.setattr(sys, name, str(project))
    roots = tuple(path for path in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(path))
    paths = sandbox_linux._runtime_read_paths(os.path.realpath(tmp_path / "wd"), roots)
    assert os.path.realpath(project) not in paths
    assert os.path.realpath(project / "lib") in paths
    assert not any(sandbox_linux._within(str(project / "src"), path) for path in paths)


def test_an_interpreter_path_resolving_to_the_filesystem_root_is_refused(tmp_path, monkeypatch):
    (tmp_path / "lib").symlink_to("/")
    monkeypatch.setattr(sys, "prefix", str(tmp_path))
    with pytest.raises(SandboxUnavailableError, match = "filesystem root"):
        sandbox_linux._runtime_read_paths(str(tmp_path / "wd"), ("/usr/lib",))


def test_a_system_interpreter_adds_nothing_the_system_roots_do_not_already_cover(
    tmp_path, monkeypatch
):
    monkeypatch.setattr(sys, "prefix", "/usr")
    monkeypatch.setattr(sys, "base_prefix", "/usr")
    roots = tuple(path for path in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(path))
    for path in sandbox_linux._runtime_read_paths(str(tmp_path), roots):
        assert path not in ("/", "/usr", "/usr/local")


def test_runtime_paths_inside_the_workdir_are_left_to_the_writable_bind(tmp_path, monkeypatch):
    inner = tmp_path / "venv"
    (inner / "lib").mkdir(parents = True)
    monkeypatch.setattr(sys, "prefix", str(inner))
    roots = tuple(path for path in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(path))
    paths = sandbox_linux._runtime_read_paths(os.path.realpath(tmp_path), roots)
    assert os.path.realpath(inner / "lib") not in paths


def test_the_probe_builds_its_launch_through_the_real_argv_builder(tmp_path):
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    launch = sandbox_linux.probe_argv(str(tmp_path), ("/bin/true", "-x"), {"PATH": "/usr/bin"})
    try:
        reference = sandbox_linux.prepare(_plan(tmp_path, argv = ("/bin/true", "-x")))
        try:

            def normalise(argv, launch):
                fd = str(argv[argv.index("--seccomp") + 1])
                identity = launch.cleanup_paths[0]
                return tuple(item.replace(identity, "<identity>") for item in argv if item != fd)

            assert normalise(launch.argv, launch) == normalise(reference.argv, reference)
        finally:
            reference.cleanup()
    finally:
        launch.cleanup()


_AUDIT_ARCH = sandbox_seccomp._ABIS.get(platform.machine().lower(), (0,))[0]
_KILL, _ALLOW = 0x80000000, 0x7FFF0000
_EPERM, _ENOSYS = sandbox_seccomp._EPERM, sandbox_seccomp._ENOSYS

if _AUDIT_ARCH == 0:
    pytest.skip(f"no seccomp ABI for {platform.machine()}", allow_module_level = True)


def _evaluate(
    instructions,
    *,
    nr,
    arch = _AUDIT_ARCH,
    args = (0,) * 6,
):
    data = struct.pack("=IIQ6Q", nr, arch, 0, *args)
    accumulator, counter = 0, 0
    for _ in range(len(instructions) * 4):
        code, jt, jf, k = instructions[counter]
        counter += 1
        if code == 0x20:
            (accumulator,) = struct.unpack_from("=I", data, k)
        elif code == 0x15:
            counter += jt if accumulator == k else jf
        elif code == 0x45:
            counter += jt if accumulator & k else jf
        elif code == 0x06:
            return k
        else:
            raise AssertionError(f"unexpected BPF opcode {code:#x}")
    raise AssertionError("the program ran off the end without returning")


def _share_cache(monkeypatch, cache):
    """Share every subdirectory of *cache* that exists, as the real resolver would."""
    monkeypatch.setattr(
        sandbox_linux,
        "_model_cache_binds",
        lambda workdir: {
            name: str(cache / name)
            for name in sandbox_linux._MODEL_CACHE_SUBDIRS
            if (cache / name).is_dir()
        },
    )


@pytest.fixture
def cache(tmp_path, monkeypatch):
    """A host cache with one populated subdirectory, shared into the jail."""
    directory = tmp_path / "hostcache"
    (directory / "hub").mkdir(parents = True)
    _share_cache(monkeypatch, directory)
    return directory


@pytest.fixture(params = [False, True], ids = ["userns_by_bwrap", "userns_by_seccomp"])
def program(request):
    return sandbox_seccomp.program(platform.machine(), block_userns = request.param)


def _socket_nr():
    return sandbox_seccomp._ABIS[platform.machine().lower()][1]


def test_a_vsock_socket_is_denied_because_no_mount_namespace_can_hide_it(program):
    assert _evaluate(program, nr = _socket_nr(), args = (40, 0, 0, 0, 0, 0)) == _EPERM


def test_ordinary_sockets_stay_allowed_because_the_network_is_not_confined(program):
    for family in (1, 2, 10):
        assert _evaluate(program, nr = _socket_nr(), args = (family, 0, 0, 0, 0, 0)) == _ALLOW


def test_a_vsock_socketpair_is_denied_too(program):
    socketpair_nr = sandbox_seccomp._ABIS[platform.machine().lower()][2]
    assert _evaluate(program, nr = socketpair_nr, args = (40, 0, 0, 0, 0, 0)) == _EPERM
    assert _evaluate(program, nr = socketpair_nr, args = (1, 0, 0, 0, 0, 0)) == _ALLOW


def test_io_uring_is_denied_on_all_three_entry_points(program):
    for number in (425, 426, 427):
        assert _evaluate(program, nr = number) == _EPERM


def test_the_keyring_syscalls_are_denied(program):
    for number in sandbox_seccomp._KEYRING_SYSCALLS[platform.machine().lower()]:
        assert _evaluate(program, nr = number) == _EPERM


def test_an_unrelated_syscall_is_allowed(program):
    assert _evaluate(program, nr = 1) == _ALLOW


def test_a_foreign_abi_is_killed_rather_than_evaluated(program):
    assert _evaluate(program, nr = 1, arch = 0xDEADBEEF) == _KILL


@pytest.mark.skipif(platform.machine().lower() not in ("x86_64", "amd64"), reason = "x32 is x86 only")
def test_the_x32_syscall_table_is_killed(program):
    assert _evaluate(program, nr = 1 | 0x40000000) == _KILL


def test_nested_user_namespaces_are_refused_only_when_bwrap_cannot_do_it():
    clone_nr, unshare_nr, clone3_nr = sandbox_seccomp._USERNS_SYSCALLS[platform.machine().lower()]
    blocking = sandbox_seccomp.program(platform.machine(), block_userns = True)
    assert _evaluate(blocking, nr = unshare_nr, args = (0x10000000, 0, 0, 0, 0, 0)) == _EPERM
    for flags in (0x00000200, 0x00000400, 0x00040000):
        assert _evaluate(blocking, nr = unshare_nr, args = (flags, 0, 0, 0, 0, 0)) == _ALLOW
    assert _evaluate(blocking, nr = clone3_nr) == _ENOSYS
    assert _evaluate(blocking, nr = clone_nr, args = (0x10000000, 0, 0, 0, 0, 0)) == _EPERM
    assert _evaluate(blocking, nr = clone_nr, args = (0x00000100, 0, 0, 0, 0, 0)) == _ALLOW

    permissive = sandbox_seccomp.program(platform.machine(), block_userns = False)
    assert _evaluate(permissive, nr = unshare_nr, args = (0x10000000, 0, 0, 0, 0, 0)) == _ALLOW
    assert _evaluate(permissive, nr = clone3_nr) == _ALLOW


class _SockFilter(ctypes.Structure):
    _fields_ = [
        ("code", ctypes.c_ushort),
        ("jt", ctypes.c_ubyte),
        ("jf", ctypes.c_ubyte),
        ("k", ctypes.c_uint32),
    ]


class _SockFprog(ctypes.Structure):
    _fields_ = [("len", ctypes.c_ushort), ("filter", ctypes.POINTER(_SockFilter))]


def _kernel_verdicts(block_userns):
    """Each probe's errno from a fork child, 0 meaning allowed."""
    clone3_nr, unshare_nr = 435, sandbox_seccomp._USERNS_SYSCALLS[platform.machine().lower()][1]
    keyctl_nr = sandbox_seccomp._KEYRING_SYSCALLS[platform.machine().lower()][2]
    read_fd, write_fd = os.pipe()
    pid = os.fork()
    if pid == 0:  # pragma: no cover - this child never returns into pytest
        os.close(read_fd)
        try:
            instructions = sandbox_seccomp.program(platform.machine(), block_userns = block_userns)
            block = (_SockFilter * len(instructions))(
                *(_SockFilter(*item) for item in instructions)
            )
            libc = ctypes.CDLL(None, use_errno = True)
            libc.syscall.restype = ctypes.c_long
            if libc.prctl(38, 1, 0, 0, 0) != 0:
                os._exit(97)
            fprog = _SockFprog(len(instructions), block)
            if libc.prctl(22, 2, ctypes.byref(fprog), 0, 0) != 0:
                os._exit(98)
            verdicts = {}
            for name, family in (("inet", socket.AF_INET), ("unix", socket.AF_UNIX), ("vsock", 40)):
                try:
                    socket.socket(family, socket.SOCK_STREAM).close()
                    verdicts[name] = 0
                except OSError as error:
                    verdicts[name] = error.errno
            for name, number, argument in (
                ("io_uring", 425, 0),
                ("keyctl", keyctl_nr, 0),
                ("clone3", clone3_nr, None),
                ("unshare_userns", unshare_nr, 0x10000000),
                ("unshare_fs", unshare_nr, 0x00000200),
            ):
                ctypes.set_errno(0)
                result = libc.syscall(number, argument, 0, 0)
                verdicts[name] = 0 if result >= 0 else ctypes.get_errno()
            child = os.fork()
            if child == 0:
                os._exit(0)
            verdicts["fork"] = os.waitpid(child, 0)[1]
            os.write(write_fd, json.dumps(verdicts).encode())
            os._exit(0)
        except BaseException:
            os._exit(99)
    os.close(write_fd)
    with os.fdopen(read_fd, "rb") as stream:
        payload = stream.read()
    status = os.waitpid(pid, 0)[1]
    assert status == 0, f"the seccomp child exited with status {status:#x}"
    return json.loads(payload)


def test_the_kernel_loads_the_filter_and_denies_the_channels_it_names():
    for block_userns in (False, True):
        verdicts = _kernel_verdicts(block_userns)
        assert verdicts["vsock"] == errno.EPERM
        assert verdicts["io_uring"] == errno.EPERM
        assert verdicts["keyctl"] == errno.EPERM
        assert verdicts["inet"] == 0 and verdicts["unix"] == 0
        assert verdicts["fork"] == 0


def test_the_kernel_refuses_nested_user_namespaces_only_in_the_fallback_filter():
    assert _kernel_verdicts(block_userns = True)["clone3"] == errno.ENOSYS
    blocking = _kernel_verdicts(block_userns = True)
    assert blocking["unshare_userns"] == errno.EPERM
    assert blocking["unshare_fs"] == 0
    assert _kernel_verdicts(block_userns = False)["clone3"] != errno.ENOSYS


def test_an_unreviewed_architecture_is_refused_rather_than_left_unfiltered():
    with pytest.raises(RuntimeError, match = "little-endian"):
        sandbox_seccomp.program("riscv64")


def test_the_filter_file_holds_exactly_the_program_and_is_rewound():
    stream = sandbox_seccomp.filter_file(block_userns = True)
    try:
        assert stream.tell() == 0
        payload = stream.read()
        assert payload == sandbox_seccomp.program_bytes(block_userns = True)
        assert len(payload) % 8 == 0
        instructions = sandbox_seccomp.program(platform.machine(), block_userns = True)
        assert len(payload) // 8 == len(instructions)
        assert struct.unpack_from("=HBBI", payload, 0) == instructions[0]
    finally:
        stream.close()


def test_a_runtime_path_symlinked_out_of_the_workdir_is_not_bound(tmp_path, monkeypatch):
    workdir = tmp_path / "session"
    workdir.mkdir()
    secret = tmp_path / "secrets"
    secret.mkdir()
    (secret / "id_rsa").write_text("PRIVATE KEY")
    venv = workdir / "venv"
    venv.mkdir()
    (venv / "lib").symlink_to(secret)
    monkeypatch.setattr(sys, "prefix", str(venv))

    paths = sandbox_linux._runtime_read_paths(str(workdir), ("/usr/lib",))
    assert str(secret) not in paths
    assert not any(sandbox_linux._within(str(secret), path) for path in paths)


def test_a_symlinked_cache_ancestor_is_refused_rather_than_written_through(bwrap, tmp_path, cache):
    """Refused rather than unlinked: symlinking a cache leaf at another volume is a legitimate layout."""
    workdir = tmp_path / "session"
    workdir.mkdir()
    outside = tmp_path / "outside"
    outside.mkdir()
    (workdir / ".cache").symlink_to(outside)

    with pytest.raises(SandboxUnavailableError, match = "not a plain directory"):
        sandbox_linux.prepare(_plan(workdir))
    assert (workdir / ".cache").is_symlink()
    assert sorted(entry.name for entry in outside.iterdir()) == []


def test_the_probe_launch_sets_no_new_privs_like_a_real_one(tmp_path):
    from core.inference import sandbox_probe

    assert sandbox_probe._PR_SET_NO_NEW_PRIVS == 38
    source = inspect.getsource(sandbox_probe._run_probe)
    assert "preexec_fn = _probe_preexec" in source
    assert "_no_new_privs()" in inspect.getsource(sandbox_probe._probe_preexec)
    for hook in (sandbox_probe._no_new_privs, sandbox_probe._probe_preexec):
        # Runs between fork and exec: an import there can deadlock on the import lock.
        assert not [
            node
            for node in ast.walk(ast.parse(textwrap.dedent(inspect.getsource(hook))))
            if isinstance(node, (ast.Import, ast.ImportFrom))
        ]


def test_the_pip_targets_script_directory_is_last_on_path(prepared):
    argv = prepared.argv
    entries = argv[argv.index("PATH") + 1].split(os.pathsep)
    packages = os.path.join(prepared.workdir, sandbox_linux.SESSION_PACKAGES_RELPATH)
    assert entries[-1] == os.path.join(packages, "bin")
    assert "/usr/bin" in entries[:-1]


def test_the_compiler_headers_a_source_build_needs_are_readable(prepared):
    sources = [source for source, _ in _pairs(prepared.argv, "--ro-bind-try")]
    assert "/usr/include" in sources
    assert "/usr/local/include" in sources
    include = sysconfig.get_paths().get("include")
    if include and os.path.isdir(include):
        bound = [source for source, _ in _pairs(prepared.argv, "--ro-bind")]
        assert any(sandbox_linux._within(include, path) for path in (*sources, *bound)), include


def test_a_runtime_prefix_contributes_its_git_helpers(tmp_path, monkeypatch):
    prefix = tmp_path / "conda"
    for name in ("bin", "libexec", "lib"):
        (prefix / name).mkdir(parents = True)
    monkeypatch.setattr(sys, "prefix", str(prefix))
    paths = sandbox_linux._runtime_read_paths(str(tmp_path / "session"), ("/usr/lib",))
    assert str(prefix / "libexec") in paths
    assert str(prefix) not in paths


def test_the_launch_pre_exec_scopes_abstract_sockets(tmp_path):
    if not sandbox_landlock.abstract_scope_supported():
        pytest.skip("this kernel predates the Landlock abstract-socket scope")
    name = b"\0unsloth-abstract-probe-" + os.urandom(6).hex().encode()
    listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    listener.bind(name)
    listener.listen(1)
    read_fd, write_fd = os.pipe()
    try:
        child = os.fork()
        if child == 0:  # pragma: no cover - runs in the forked child
            try:
                os.close(read_fd)
                sandbox_landlock.with_abstract_scope(None)()
                probe = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                probe.settimeout(2)
                try:
                    probe.connect(name)
                    os.write(write_fd, b"connected")
                except OSError as error:
                    os.write(write_fd, str(error.errno).encode())
            finally:
                os._exit(0)
        os.close(write_fd)
        write_fd = -1
        with os.fdopen(read_fd, "rb") as stream:
            verdict = stream.read()
        read_fd = -1
        os.waitpid(child, 0)
        assert verdict == str(errno.EPERM).encode(), verdict
        control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        control.settimeout(2)
        control.connect(name)
        control.close()
    finally:
        for fd in (read_fd, write_fd):
            if fd >= 0:
                os.close(fd)
        listener.close()


def test_the_backend_says_so_when_the_kernel_cannot_scope_them(monkeypatch):
    supported = sandbox_landlock.abstract_scope_supported()
    assert ("host_abstract_sockets_reachable" in sandbox_linux.LIMITATIONS) is not supported


def test_a_landlock_probe_child_that_never_answers_does_not_hang_the_server(monkeypatch):
    if not sandbox_landlock._abi_reports_scope():
        pytest.skip("no Landlock ABI 6 here, so the fork under test never happens")

    def never_answers():
        time.sleep(30)

    monkeypatch.setattr(sandbox_landlock, "apply_abstract_scope", never_answers)
    monkeypatch.setattr(sandbox_landlock, "_PROBE_TIMEOUT_SECONDS", 1.0)
    sandbox_landlock.abstract_scope_supported.cache_clear()
    try:
        started = time.monotonic()
        assert sandbox_landlock.abstract_scope_supported() is False
        assert time.monotonic() - started < 10, "the deadline did not fire"
    finally:
        sandbox_landlock.abstract_scope_supported.cache_clear()


def test_the_plan_pre_exec_still_runs_before_the_scope():
    ran = []
    composed = sandbox_landlock.with_abstract_scope(lambda: ran.append("plan"))
    composed()
    assert ran == ["plan"]


def test_a_standalone_interpreter_in_the_workdir_is_re_bound_read_only(
    bwrap, tmp_path, monkeypatch
):
    workdir = tmp_path / "session"
    workdir.mkdir()
    executable = workdir / "python"
    executable.write_bytes(b"#!/bin/sh\nexit 0\n")
    executable.chmod(0o755)
    for attribute in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):
        monkeypatch.setattr(sys, attribute, str(workdir))
    monkeypatch.setattr(sys, "executable", str(executable))

    assert sandbox_linux._runtime_paths_under(str(workdir)) == (str(executable),)
    launch = sandbox_linux.prepare(_plan(workdir))
    try:
        argv = list(launch.argv)
        read_only = [argv[i + 1] for i, item in enumerate(argv) if item == "--ro-bind"]
        assert str(executable) in read_only, read_only
        assert argv.index("--ro-bind", argv.index("--bind")) > argv.index("--bind")
    finally:
        launch.cleanup()


def test_an_interpreter_in_a_home_directory_does_not_bind_the_home(tmp_path, monkeypatch):
    home = tmp_path / "alice"
    (home / ".ssh").mkdir(parents = True)
    (home / ".ssh" / "id_rsa").write_text("SECRET", encoding = "utf-8")
    executable = home / "python"
    executable.write_bytes(b"#!/bin/sh\nexit 0\n")
    executable.chmod(0o755)
    monkeypatch.setattr(sys, "executable", str(executable))

    paths = sandbox_linux._runtime_read_paths(str(tmp_path / "session"), ("/usr/lib",))
    assert str(home) not in paths, paths
    assert not any(_within_for_test(p, str(home / ".ssh")) for p in paths), paths
    assert str(executable) in paths, paths


def _within_for_test(path: str, root: str) -> bool:
    return path == root or path.startswith(root.rstrip("/") + "/")


def test_a_runtime_origin_is_excluded_under_the_workdir_alias_too(bwrap, tmp_path, monkeypatch):
    real = tmp_path / "real"
    real.mkdir()
    alias = tmp_path / "alias"
    alias.symlink_to(real)
    secret = tmp_path / "secrets"
    secret.mkdir()
    (secret / "id_rsa").write_text("SECRET", encoding = "utf-8")
    venv = real / "venv"
    (venv / "bin").mkdir(parents = True)
    (venv / "lib").symlink_to(secret)
    for attribute in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):
        monkeypatch.setattr(sys, attribute, str(alias / "venv"))

    paths = sandbox_linux._runtime_read_paths(os.path.realpath(real), ("/usr/lib",), str(alias))
    assert not any(os.path.realpath(p) == str(secret) for p in paths), paths
    launch = sandbox_linux.prepare(_plan(alias))
    try:
        argv = list(launch.argv)
        bound = [
            argv[i + 1] for i, item in enumerate(argv) if item in ("--ro-bind", "--ro-bind-try")
        ]
        assert not any(os.path.realpath(p) == str(secret) for p in bound), bound
    finally:
        launch.cleanup()


def test_an_editable_source_inside_the_workdir_is_re_bound_read_only(tmp_path, monkeypatch):
    workdir = tmp_path / "session"
    package = workdir / "mypkg"
    package.mkdir(parents = True)
    (package / "__init__.py").write_text("", encoding = "utf-8")
    monkeypatch.setattr(sandbox_linux, "editable_source_roots", lambda: (str(package),))

    assert str(package) in sandbox_linux._runtime_paths_under(str(workdir))
    assert str(package) not in sandbox_linux._runtime_read_paths(str(workdir), ("/usr/lib",))


def test_the_interpreter_spelling_the_launch_execs_is_bound(tmp_path, monkeypatch):
    target_bin = tmp_path / "opt" / "py" / "bin"
    target_bin.mkdir(parents = True)
    target = target_bin / "python3.13"
    target.write_bytes(b"#!/bin/sh\nexit 0\n")
    target.chmod(0o755)
    user_bin = tmp_path / "home" / "bin"
    user_bin.mkdir(parents = True)
    link = user_bin / "python"
    link.symlink_to(target)
    monkeypatch.setattr(sys, "executable", str(link))
    for attribute in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):
        monkeypatch.setattr(sys, attribute, str(tmp_path / "opt" / "py"))

    paths = sandbox_linux._runtime_read_paths(str(tmp_path / "session"), ("/usr/lib",))
    assert str(link) in paths, paths
    assert str(target) in paths or str(target_bin) in paths, paths
    assert str(user_bin) not in paths, paths


def test_a_wedged_cache_mount_drops_the_cache_instead_of_hanging_the_launch(tmp_path, monkeypatch):
    cache = tmp_path / "hostcache"
    (cache / "hub").mkdir(parents = True)
    _share_cache_paths(monkeypatch, cache)

    def wedged(path):
        time.sleep(30)
        raise AssertionError("the caller should not have waited for this")

    monkeypatch.setattr(
        sandbox_linux, "_inspect_cache_component", lambda name, path, witness = None: wedged(path)
    )
    monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 1.0)
    started = time.monotonic()
    binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))
    assert time.monotonic() - started < 10, "the deadline did not fire"
    assert binds == {}, binds


def _share_cache_paths(monkeypatch, cache):
    import types

    class _Paths:
        cache_home = str(cache)
        hub_cache = str(cache / "hub")
        xet_cache = str(cache / "xet")

    module = types.ModuleType("utils.hf_cache_settings")
    module.get_hf_cache_paths = lambda: _Paths()
    module.effective_cache_home = lambda: cache
    monkeypatch.setitem(sys.modules, "utils.hf_cache_settings", module)
    # The resolver prefers these over cache_home; a caller's own settings must not leak in.
    monkeypatch.delenv("HF_DATASETS_CACHE", raising = False)
    monkeypatch.delenv("HF_ASSETS_CACHE", raising = False)


def test_a_symlinked_runtime_entry_inside_the_workdir_is_protected_by_name(tmp_path, monkeypatch):
    workdir = tmp_path / "session"
    target = workdir / "real_pkg"
    target.mkdir(parents = True)
    (target / "__init__.py").write_text("", encoding = "utf-8")
    entry = workdir / "mypkg"
    entry.symlink_to(target)
    monkeypatch.setattr(sandbox_linux, "editable_source_roots", lambda: (str(entry),))

    protected = sandbox_linux._runtime_paths_under(str(workdir))
    assert str(entry) in protected, protected
    assert str(target) in protected, protected


def test_a_wedged_cache_path_is_not_re_scanned_by_every_later_launch(tmp_path, monkeypatch):
    cache = tmp_path / "hostcache"
    (cache / "hub").mkdir(parents = True)
    _share_cache_paths(monkeypatch, cache)
    started: list[str] = []

    def wedged(
        name,
        path,
        witness = None,
    ):
        started.append(path)
        time.sleep(30)

    monkeypatch.setattr(sandbox_linux, "_inspect_cache_component", wedged)
    monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.5)
    monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})
    before = set(threading.enumerate())
    session = str(tmp_path / "session")
    assert sandbox_linux._model_cache_binds(session) == {}
    assert sandbox_linux._model_cache_binds(session) == {}
    # Scan workers may run late, so wait for each launched worker to reach the scan before counting.
    workers = [
        t
        for t in threading.enumerate()
        if t not in before and t.name.startswith("unsloth-cache-scan-")
    ]
    deadline = time.monotonic() + 15
    while len(started) < len(workers) and time.monotonic() < deadline:
        time.sleep(0.05)
    assert started, "no cache scan was started"
    assert len(started) == len(
        set(started)
    ), "a second launch started another worker on the same path"
    assert len(workers) == len(started)


def test_a_wedged_cache_scan_is_waited_on_twice_then_skipped_at_once(monkeypatch, tmp_path):
    component = tmp_path / "hub"
    component.mkdir()
    release = threading.Event()
    monkeypatch.setattr(
        sandbox_linux, "_cache_hazard_memoized", lambda name, path: release.wait(30)
    )
    monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.4)
    monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})
    # A fake clock that a join advances by its timeout: the waits are read off the joins, not timed.
    now = [100.0]
    joins = []
    real_thread = threading.Thread

    class Worker(real_thread):
        def join(self, timeout = None):
            joins.append(round(timeout, 6))
            now[0] += timeout
            super().join(0)

    monkeypatch.setattr(sandbox_linux.threading, "Thread", Worker)
    monkeypatch.setattr(sandbox_linux.time, "monotonic", lambda: now[0])
    try:
        for _ in range(4):
            hazard = sandbox_linux._cache_hazard_within_deadline("hub", str(component))
            assert hazard == sandbox_linux.CACHE_STILL_CHECKING
    finally:
        release.set()
    assert joins == [0.4, 0.4, 0.0, 0.0], f"later launches still wait on the wedged scan: {joins}"


def test_revalidating_a_cached_verdict_on_a_wedged_mount_is_bounded_too(monkeypatch, tmp_path):
    """A memo hit re-stats every directory the walk saw, which blocks on a stalled NFS/FUSE cache just like the walk."""
    component = tmp_path / "hub"
    component.mkdir()
    monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})
    sandbox_linux.reset_cache_verdicts()
    assert sandbox_linux._cache_hazard_within_deadline("hub", str(component)) is None

    release = threading.Event()
    monkeypatch.setattr(
        sandbox_linux, "directory_witness_matches", lambda witness: release.wait(30)
    )
    monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.5)
    start = time.monotonic()
    try:
        hazard = sandbox_linux._cache_hazard_within_deadline("hub", str(component))
    finally:
        release.set()

    assert time.monotonic() - start < 10
    assert hazard == sandbox_linux.CACHE_STILL_CHECKING, hazard


def test_a_runtime_entry_whose_target_leaves_the_workdir_gets_no_rule(tmp_path, monkeypatch):
    workdir = tmp_path / "session"
    workdir.mkdir()
    private = tmp_path / "private"
    private.mkdir()
    (private / "id_rsa").write_text("SECRET", encoding = "utf-8")
    (workdir / "venv").mkdir()
    (workdir / "venv" / "lib").symlink_to(private)
    monkeypatch.setattr(sys, "prefix", str(workdir / "venv"))
    monkeypatch.setattr(sys, "exec_prefix", str(workdir / "venv"))

    protected = sandbox_linux._runtime_paths_under(str(workdir))
    assert str(workdir / "venv" / "lib") not in protected, protected
    assert not any(os.path.realpath(p) == str(private) for p in protected), protected


def test_an_interpreter_symlinked_out_of_the_workdir_fails_the_call(tmp_path, monkeypatch):
    workdir = tmp_path / "session"
    workdir.mkdir()
    outside = tmp_path / "elsewhere"
    outside.mkdir()
    real = outside / "python"
    real.write_bytes(b"#!/bin/sh\nexit 0\n")
    real.chmod(0o755)
    link = workdir / "python"
    link.symlink_to(real)
    monkeypatch.setattr(sys, "executable", str(link))

    with pytest.raises(WorkdirUnsafeError, match = "Python that runs Unsloth"):
        sandbox_linux._runtime_paths_under(str(workdir))


def test_concurrent_launches_start_one_cache_worker_and_never_raise(tmp_path, monkeypatch):
    cache = tmp_path / "hostcache"
    (cache / "hub").mkdir(parents = True)
    _share_cache_paths(monkeypatch, cache)
    started: list[str] = []
    gate = threading.Event()

    def wedged(
        name,
        path,
        witness = None,
    ):
        started.append(path)
        gate.wait(30)

    monkeypatch.setattr(sandbox_linux, "_inspect_cache_component", wedged)
    monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.5)
    monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})
    session = str(tmp_path / "session")
    errors: list[BaseException] = []

    def launch() -> None:
        try:
            sandbox_linux._model_cache_binds(session)
        except BaseException as exc:  # noqa: BLE001 - the point of the test
            errors.append(exc)

    threads = [threading.Thread(target = launch) for _ in range(8)]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join(60)
    gate.set()
    assert not errors, errors
    assert started, "the inspection never ran"
    assert len(started) == len(set(started)), f"a path was scanned twice: {sorted(started)}"


def test_a_runtime_prefix_contributes_its_certificate_store(tmp_path, monkeypatch):
    prefix = tmp_path / "conda"
    for name in ("bin", "ssl", "lib"):
        (prefix / name).mkdir(parents = True)
    monkeypatch.setattr(sys, "prefix", str(prefix))
    paths = sandbox_linux._runtime_read_paths(str(tmp_path / "session"), ("/usr/lib",))
    assert str(prefix / "ssl") in paths


def test_a_workdir_reached_through_a_symlink_is_bound_at_the_spelling_the_caller_used(
    bwrap, tmp_path
):
    real = tmp_path / "real"
    real.mkdir()
    link = tmp_path / "link"
    link.symlink_to(real)
    launch = sandbox_linux.prepare(_plan(link))
    try:
        binds = _pairs(launch.argv, "--bind")
        assert (str(real), str(link)) in binds
        assert (str(real), str(real)) in binds
        assert launch.argv[launch.argv.index("--chdir") + 1] == str(link)
        assert launch.argv[launch.argv.index("HOME") + 1] == str(link)
    finally:
        launch.cleanup()


def test_a_cache_leaf_left_behind_as_a_file_is_refused_at_preparation(bwrap, tmp_path, cache):
    workdir = tmp_path / "session"
    (workdir / ".cache" / "huggingface").mkdir(parents = True)
    (workdir / ".cache" / "huggingface" / "hub").write_text("not a directory")

    with pytest.raises(SandboxUnavailableError, match = "not a plain directory"):
        sandbox_linux.prepare(_plan(workdir))
    assert (workdir / ".cache" / "huggingface" / "hub").read_text() == "not a directory"


@pytest.mark.skipif(
    os.geteuid() == 0,
    reason = "root reads and writes regardless of the mode bits, so the premise is void",
)
def test_an_unreadable_directory_is_refused(tmp_path):
    """A mode-000 directory hides a link out from the scan, and the process that owns it can chmod it back."""
    locked = tmp_path / "locked"
    locked.mkdir()
    (locked / "inside").write_text("x")
    locked.chmod(0o000)
    try:
        with pytest.raises(SandboxUnavailableError, match = "cannot be fully inspected"):
            sandbox_linux._validate_workdir(str(tmp_path))
    finally:
        locked.chmod(0o700)


def test_the_cache_studio_actually_uses_is_the_one_shared(tmp_path, monkeypatch):
    """HF_HUB_CACHE=/mnt/models is NOT /mnt/models/hub, so each component is bound where it actually is."""
    hub = tmp_path / "mnt" / "models"
    xet = tmp_path / "fast" / "xet"
    home = tmp_path / "cachehome"
    for path in (hub, xet, home / "datasets", home / "assets"):
        path.mkdir(parents = True)
    import utils.hf_cache_settings as cache_settings

    # datasets/assets follow the real HF home, which a Studio-selected folder leaves in place.
    monkeypatch.setenv("HF_HOME", str(home))
    monkeypatch.delenv("HF_DATASETS_CACHE", raising = False)
    monkeypatch.delenv("HF_ASSETS_CACHE", raising = False)
    monkeypatch.setattr(
        cache_settings,
        "get_hf_cache_paths",
        lambda: cache_settings.HuggingFaceCachePaths(
            cache_home = hub.parent, hub_cache = hub, xet_cache = xet, source = "studio"
        ),
    )
    binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))
    assert binds["hub"] == str(hub)
    assert binds["xet"] == str(xet)
    assert binds["datasets"] == str(home / "datasets")
    assert "hub" not in sandbox_linux._model_cache_binds(str(hub.parent))


def _real_cache(monkeypatch, home):
    """Point the settings layer at *home* and let the REAL _model_cache_binds run."""
    import types

    paths = types.SimpleNamespace(cache_home = home, hub_cache = home / "hub", xet_cache = home / "xet")
    module = types.ModuleType("utils.hf_cache_settings")
    module.get_hf_cache_paths = lambda: paths
    module.effective_cache_home = lambda: home
    monkeypatch.setitem(sys.modules, "utils.hf_cache_settings", module)
    monkeypatch.delenv("HF_DATASETS_CACHE", raising = False)
    monkeypatch.delenv("HF_ASSETS_CACHE", raising = False)


def test_a_cache_component_holding_an_ipc_node_is_not_shared(tmp_path, monkeypatch):
    host = tmp_path / "hostcache"
    (host / "hub").mkdir(parents = True)
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    monkeypatch.chdir(host / "hub")
    sock.bind("leftover.sock")
    try:
        _real_cache(monkeypatch, host)
        assert "hub" not in sandbox_linux._model_cache_binds(str(tmp_path / "session"))
    finally:
        sock.close()


def test_a_cache_component_holding_an_external_hard_link_is_not_shared(tmp_path, monkeypatch):
    """Same inode under two names, one of them outside the cache."""
    host = tmp_path / "hostcache"
    (host / "hub").mkdir(parents = True)
    outside = tmp_path / "private.txt"
    outside.write_text("secret")
    os.link(outside, host / "hub" / "innocent.bin")
    _real_cache(monkeypatch, host)
    assert "hub" not in sandbox_linux._model_cache_binds(str(tmp_path / "session"))


def test_a_clean_cache_component_is_still_shared(tmp_path, monkeypatch):
    """The negative control: the check above must not simply drop everything."""
    host = tmp_path / "hostcache"
    (host / "hub" / "models--x").mkdir(parents = True)
    (host / "hub" / "models--x" / "weights.bin").write_text("w")
    _real_cache(monkeypatch, host)
    assert sandbox_linux._model_cache_binds(str(tmp_path / "session"))["hub"] == str(host / "hub")


def test_a_hazardous_cache_drops_the_component_rather_than_failing_the_launch(
    bwrap, tmp_path, monkeypatch
):
    host = tmp_path / "hostcache"
    (host / "hub").mkdir(parents = True)
    os.mkfifo(host / "hub" / "pipe")
    _real_cache(monkeypatch, host)
    workdir = tmp_path / "session"
    workdir.mkdir()
    launch = sandbox_linux.prepare(_plan(workdir))
    try:
        assert os.path.realpath(host / "hub") not in launch.argv
    finally:
        launch.cleanup()


def test_a_trusted_system_gitconfig_is_bound_and_an_untrusted_one_is_not(tmp_path, monkeypatch):
    good = tmp_path / "gitconfig"
    good.write_text("[http]\n")
    link = tmp_path / "linked"
    link.symlink_to(good)
    assert sandbox_linux._trusted_system_file(str(link)) is False, "a symlink is followed"
    assert sandbox_linux._trusted_system_file(str(tmp_path / "absent")) is False
    os.chmod(good, 0o666)
    assert sandbox_linux._trusted_system_file(str(good)) is False, "world-writable accepted"


def test_a_runtime_under_the_workdir_is_re_bound_read_only(bwrap, tmp_path, monkeypatch):
    workdir = tmp_path / "session"
    venv = workdir / "venv"
    (venv / "lib").mkdir(parents = True)
    (venv / "bin").mkdir()
    monkeypatch.setattr(sys, "prefix", str(venv))
    monkeypatch.setattr(sys, "exec_prefix", str(venv))

    launch = sandbox_linux.prepare(_plan(workdir))
    try:
        argv = launch.argv
        writable = argv.index("--bind")
        ro_after = [
            source
            for index, source in enumerate(argv)
            if index > writable and argv[index - 1] == "--ro-bind"
        ]
        assert str(venv / "lib") in ro_after, "the runtime stayed writable"
        assert str(venv / "bin") in ro_after
    finally:
        launch.cleanup()


def test_a_runtime_symlinked_out_of_the_workdir_is_not_re_bound(bwrap, tmp_path, monkeypatch):
    """The negative control for the rule above."""
    workdir = tmp_path / "session"
    (workdir / "venv").mkdir(parents = True)
    secret = tmp_path / "home" / ".ssh"
    secret.mkdir(parents = True)
    (workdir / "venv" / "lib").symlink_to(secret)
    monkeypatch.setattr(sys, "prefix", str(workdir / "venv"))

    launch = sandbox_linux.prepare(_plan(workdir))
    try:
        for flag in ("--bind", "--ro-bind", "--ro-bind-try"):
            for source, _ in _pairs(launch.argv, flag):
                assert not sandbox_linux._within(str(secret), source), source
    finally:
        launch.cleanup()


def test_a_nested_bind_mount_in_the_cache_is_caught_by_the_mount_table(tmp_path, monkeypatch):
    host = tmp_path / "hostcache"
    (host / "hub" / "nested").mkdir(parents = True)
    _real_cache(monkeypatch, host)
    monkeypatch.setattr(
        sandbox_linux, "_host_mount_points", lambda: (str(host / "hub" / "nested"),)
    )
    assert "hub" not in sandbox_linux._model_cache_binds(str(tmp_path / "session"))


def test_a_nested_bind_mount_is_caught_through_a_symlinked_cache(tmp_path, monkeypatch):
    """The mount table lists canonical paths, so a cache reached through a symlinked ~/.cache must be compared in its resolved form."""
    real = tmp_path / "volume" / "huggingface"
    (real / "hub" / "nested").mkdir(parents = True)
    (tmp_path / "cache-link").symlink_to(tmp_path / "volume")
    _real_cache(monkeypatch, tmp_path / "cache-link" / "huggingface")
    monkeypatch.setattr(
        sandbox_linux, "_host_mount_points", lambda: (str(real / "hub" / "nested"),)
    )
    assert "hub" not in sandbox_linux._model_cache_binds(str(tmp_path / "session"))


def _fake_editable(
    tmp_path,
    monkeypatch,
    source: str,
    top_level: str | None = None,
):
    """A dist-info recording an editable install, the way an installer writes it."""
    site_dir = tmp_path / "sitepkgs"
    info = site_dir / "demo-1.0.dist-info"
    info.mkdir(parents = True)
    (info / "METADATA").write_text("Name: demo\nVersion: 1.0\n", encoding = "utf-8")
    (info / "RECORD").write_text("", encoding = "utf-8")
    if top_level is not None:
        (info / "top_level.txt").write_text(top_level + "\n", encoding = "utf-8")
    (info / "direct_url.json").write_text(
        json.dumps({"url": f"file://{source}", "dir_info": {"editable": True}}),
        encoding = "utf-8",
    )
    monkeypatch.syspath_prepend(str(site_dir))
    # Only the fake: an editable install in the environment running the suite (pip install -e)
    # would otherwise be granted too, and the expected roots would depend on the host.
    from importlib import metadata

    real_distributions = metadata.distributions
    monkeypatch.setattr(
        metadata, "distributions", lambda **kw: real_distributions(path = [str(site_dir)])
    )
    os_sandbox.editable_source_roots.cache_clear()
    return site_dir


def test_an_editable_installs_source_root_is_readable(tmp_path, monkeypatch):
    source = tmp_path / "checkout"
    package = source / "demo"
    package.mkdir(parents = True)
    (package / "__init__.py").write_text("", encoding = "utf-8")
    (source / ".env").write_text("AWS_SECRET_ACCESS_KEY=real\n", encoding = "utf-8")
    (source / "fixtures").mkdir()
    _fake_editable(tmp_path, monkeypatch, str(source))
    try:
        granted = os_sandbox.editable_source_roots()
        assert str(package) in granted, granted
        assert str(source) not in granted, granted
        assert not any("fixtures" in path or ".env" in path for path in granted), granted
        roots = tuple(p for p in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(p))
        read = sandbox_linux._runtime_read_paths(str(tmp_path / "wd"), roots)
        assert str(package) in read
        assert str(source) not in read
    finally:
        os_sandbox.editable_source_roots.cache_clear()


def test_a_guessed_editable_import_root_gives_up_what_it_cannot_confirm(tmp_path, monkeypatch):
    source = tmp_path / "checkout"
    package = source / "demo"
    package.mkdir(parents = True)
    (package / "__init__.py").write_text("", encoding = "utf-8")
    (source / "deploy.py").write_text("AWS_SECRET_ACCESS_KEY = 'real'\n", encoding = "utf-8")
    (source / "conftest.py").write_text("", encoding = "utf-8")
    tests_pkg = source / "tests"
    tests_pkg.mkdir()
    (tests_pkg / "__init__.py").write_text("", encoding = "utf-8")
    (tests_pkg / "fixtures").mkdir()
    (tests_pkg / "fixtures" / "id_rsa").write_text("-----BEGIN-----", encoding = "utf-8")
    _fake_editable(tmp_path, monkeypatch, str(source), top_level = "demo")
    try:
        granted = os_sandbox.editable_source_roots()
        assert granted == (str(package),), granted
    finally:
        os_sandbox.editable_source_roots.cache_clear()


def test_a_declared_package_symlinked_out_of_the_checkout_is_refused(tmp_path, monkeypatch):
    source = tmp_path / "checkout"
    source.mkdir()
    private = tmp_path / "private"
    private.mkdir()
    (private / "id_rsa").write_text("SECRET", encoding = "utf-8")
    (private / "__init__.py").write_text("", encoding = "utf-8")
    (source / "demo").symlink_to(private)
    _fake_editable(tmp_path, monkeypatch, str(source), top_level = "demo")
    try:
        granted = os_sandbox.editable_source_roots()
        assert granted == (), granted
    finally:
        os_sandbox.editable_source_roots.cache_clear()


def test_an_editable_root_at_the_filesystem_root_is_refused(tmp_path, monkeypatch):
    """The negative control."""
    _fake_editable(tmp_path, monkeypatch, "/usr")
    try:
        assert os_sandbox.editable_source_roots() == ()
    finally:
        os_sandbox.editable_source_roots.cache_clear()


def test_a_runtime_under_a_symlinked_workdir_is_read_only_through_both_spellings(
    bwrap, tmp_path, monkeypatch
):
    real = tmp_path / "real"
    venv = real / "venv"
    (venv / "lib").mkdir(parents = True)
    (venv / "bin").mkdir()
    alias = tmp_path / "alias"
    alias.symlink_to(real)
    monkeypatch.setattr(sys, "prefix", str(venv))
    monkeypatch.setattr(sys, "exec_prefix", str(venv))

    launch = sandbox_linux.prepare(_plan(alias))
    try:
        argv = list(launch.argv)
        binds = [i for i, item in enumerate(argv) if item == "--bind"]
        writable = [argv[i + 2] for i in binds]
        assert str(alias) in writable and str(real) in writable, writable
        last_bind = max(binds)
        for leg in ("lib", "bin"):
            for spelling in (real / "venv" / leg, alias / "venv" / leg):
                landed = [
                    i
                    for i in range(len(argv))
                    if argv[i] == "--ro-bind" and argv[i + 2] == str(spelling)
                ]
                assert landed, f"{spelling} is not re-bound read-only"
                assert min(landed) > last_bind, f"{spelling} is bound before the last --bind"
    finally:
        launch.cleanup()


def test_a_runtime_is_protected_when_sys_prefix_carries_the_workdir_alias(
    bwrap, tmp_path, monkeypatch
):
    real = tmp_path / "real"
    (real / "venv" / "lib").mkdir(parents = True)
    (real / "venv" / "bin").mkdir()
    alias = tmp_path / "alias"
    alias.symlink_to(real)
    monkeypatch.setattr(sys, "prefix", str(alias / "venv"))
    monkeypatch.setattr(sys, "exec_prefix", str(alias / "venv"))

    under = sandbox_linux._runtime_paths_under(str(real))
    assert str(real / "venv" / "lib") in under, under
    assert str(real / "venv" / "bin") in under, under

    launch = sandbox_linux.prepare(_plan(alias))
    try:
        argv = list(launch.argv)
        last_bind = max(i for i, item in enumerate(argv) if item == "--bind")
        for leg in ("lib", "bin"):
            for spelling in (real / "venv" / leg, alias / "venv" / leg):
                landed = [
                    i
                    for i in range(len(argv))
                    if argv[i] == "--ro-bind" and argv[i + 2] == str(spelling)
                ]
                assert landed and max(landed) > last_bind, f"{spelling} unprotected"
    finally:
        launch.cleanup()


def test_an_editable_namespace_package_is_granted_without_an_init(tmp_path, monkeypatch):
    source = tmp_path / "checkout"
    namespace = source / "acme"
    (namespace / "widget").mkdir(parents = True)
    (namespace / "widget" / "__init__.py").write_text("", encoding = "utf-8")
    (source / ".env").write_text("AWS_SECRET_ACCESS_KEY=real\n", encoding = "utf-8")
    (source / "fixtures").mkdir()
    _fake_editable(tmp_path, monkeypatch, str(source), top_level = "acme")
    try:
        granted = os_sandbox.editable_source_roots()
        assert str(namespace) in granted, granted
        assert not any("fixtures" in path or ".env" in path for path in granted), granted
        assert str(source) not in granted, granted
    finally:
        os_sandbox.editable_source_roots.cache_clear()


def test_the_cache_verdict_is_memoized_between_launches(tmp_path, monkeypatch):
    """The walk is O(entries) and was re-paid on every launch."""
    calls = []
    real = sandbox_linux._inspect_cache_component
    monkeypatch.setattr(
        sandbox_linux,
        "_inspect_cache_component",
        lambda name, path, witness = None: (calls.append(path), real(name, path, witness))[1],
    )
    sandbox_linux.reset_cache_verdicts()
    component = tmp_path / "hub"
    component.mkdir()
    (component / "a.bin").write_text("")

    first = sandbox_linux._cache_hazard_within_deadline("hub", str(component))
    walked_once = len(calls)
    second = sandbox_linux._cache_hazard_within_deadline("hub", str(component))

    assert second == first
    assert len(calls) == walked_once, "the second launch re-walked the cache"


def test_a_changed_cache_is_re_inspected_rather_than_trusted(tmp_path, monkeypatch):
    """The memo key is the component root's identity and mtime, so a change made through the root must invalidate it."""
    calls = []
    real = sandbox_linux._inspect_cache_component
    monkeypatch.setattr(
        sandbox_linux,
        "_inspect_cache_component",
        lambda name, path, witness = None: (calls.append(path), real(name, path, witness))[1],
    )
    sandbox_linux.reset_cache_verdicts()
    component = tmp_path / "hub"
    component.mkdir()
    sandbox_linux._cache_hazard_within_deadline("hub", str(component))
    before = len(calls)

    os.utime(component, (0, 0))
    (component / "models--org--new").mkdir()
    sandbox_linux._cache_hazard_within_deadline("hub", str(component))

    assert len(calls) > before, "a changed cache reused its old verdict"


def test_a_failed_launch_drops_every_cache_verdict(tmp_path, monkeypatch):
    """tools.py calls this when a launch dies, for the same reason it drops the capability probe: a failed launch is the one signal that something the planner believed about this host has changed."""
    calls = []
    real = sandbox_linux._inspect_cache_component
    monkeypatch.setattr(
        sandbox_linux,
        "_inspect_cache_component",
        lambda name, path, witness = None: (calls.append(path), real(name, path, witness))[1],
    )
    sandbox_linux.reset_cache_verdicts()
    component = tmp_path / "hub"
    component.mkdir()
    sandbox_linux._cache_hazard_within_deadline("hub", str(component))
    before = len(calls)

    sandbox_linux.reset_cache_verdicts()
    sandbox_linux._cache_hazard_within_deadline("hub", str(component))

    assert len(calls) > before, "reset_cache_verdicts did not invalidate the memo"


def test_the_studio_state_directory_is_never_a_system_bind(monkeypatch, tmp_path):
    """/opt is a system root and the Docker layout puts Studio's state in it."""
    from core.inference import sandbox_linux

    opt = tmp_path / "opt"
    state = opt / "unsloth-studio"
    (state / "auth").mkdir(parents = True)
    (state / "auth" / "auth.db").write_text("secret")
    toolchain = opt / "some-toolchain"
    toolchain.mkdir()

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(state))

    kept = sandbox_linux._without_studio_state((str(opt),))

    assert str(state) not in kept
    assert not any(
        sandbox_linux._within(str(state), path) for path in kept
    ), "a bind source still contains the Studio auth database"
    assert str(toolchain) in kept, "unrelated /opt software stopped being readable"


def test_a_system_root_without_studio_state_is_still_bound_whole(monkeypatch, tmp_path):
    """The descent must only happen where it is needed, or every launch pays a listdir of /usr and binds hundreds of paths."""
    from core.inference import sandbox_linux

    opt = tmp_path / "opt"
    (opt / "some-toolchain").mkdir(parents = True)
    elsewhere = tmp_path / "home" / "studio"
    elsewhere.mkdir(parents = True)

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(elsewhere))

    assert sandbox_linux._without_studio_state((str(opt),)) == (str(opt),)


def test_a_deeply_nested_studio_state_root_is_refused_not_restored(monkeypatch, tmp_path):
    """The descent is bounded, and the bound must fail CLOSED."""
    from core.inference import sandbox_linux

    opt = tmp_path / "opt"
    state = opt / "a" / "b" / "c" / "d" / "studio"
    (state / "auth").mkdir(parents = True)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(state))

    kept = sandbox_linux._without_studio_state((str(opt),), depth = 0)

    assert kept == (), "an ancestor of the Studio auth database was bound anyway"


def test_a_symlinked_sibling_is_not_promoted_to_a_bind_source(monkeypatch, tmp_path):
    """Splitting a system root must not mount what the whole-root bind did not."""
    from core.inference import sandbox_linux

    opt = tmp_path / "opt"
    state = opt / "unsloth-studio"
    (state / "auth").mkdir(parents = True)
    private = tmp_path / "home" / "operator" / "private"
    private.mkdir(parents = True)
    (opt / "private").symlink_to(private)
    (opt / "toolchain").mkdir()
    (opt / "inside-link").symlink_to(opt / "toolchain")

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(state))

    kept = sandbox_linux._without_studio_state((str(opt),))

    assert str(opt / "private") not in kept, "a symlink out of the root became a bind source"
    assert str(opt / "toolchain") in kept
    assert str(opt / "inside-link") in kept


def test_a_cache_holding_studio_state_is_not_shared_writable(monkeypatch, tmp_path):
    """HF_HUB_CACHE at or above the Studio root would bind auth/auth.db in WRITABLE, and the hazard scan would not object: an ordinary file is not a host channel."""
    from core.inference import sandbox_linux

    state = tmp_path / "studio"
    (state / "auth").mkdir(parents = True)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(state))

    assert sandbox_linux._holds_studio_state(str(state)) is True
    assert (
        sandbox_linux._holds_studio_state(str(tmp_path)) is True
    ), "an ancestor of the Studio root was not recognised"
    assert sandbox_linux._holds_studio_state(str(tmp_path / "elsewhere")) is False


def test_the_cache_bind_itself_drops_a_component_holding_studio_state(monkeypatch, tmp_path):
    """Through _model_cache_binds, not just the predicate: the guard is only worth anything if the bind list is what changes."""
    import types

    from core.inference import sandbox_linux

    state = tmp_path / "studio"
    (state / "auth").mkdir(parents = True)
    elsewhere = tmp_path / "models"
    for name in ("xet", "datasets", "assets"):
        (elsewhere / name).mkdir(parents = True, exist_ok = True)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(state))

    settings = types.ModuleType("utils.hf_cache_settings")
    settings.get_hf_cache_paths = lambda: types.SimpleNamespace(
        cache_home = str(elsewhere),
        hub_cache = str(state),
        xet_cache = str(elsewhere / "xet"),
    )
    settings.effective_cache_home = lambda: elsewhere
    monkeypatch.setitem(sys.modules, "utils.hf_cache_settings", settings)
    monkeypatch.setattr(sandbox_linux, "_cache_hazard_within_deadline", lambda name, path: None)

    binds = sandbox_linux._model_cache_binds(str(tmp_path / "work"))

    assert "hub" not in binds, "the Studio root was shared into the sandbox as the hub cache"
    assert binds.get("xet") == str(elsewhere / "xet")


def test_a_cache_configured_as_a_home_directory_is_not_shared(monkeypatch, tmp_path):
    """HF_HUB_CACHE pointed at a home is a misconfiguration; the consequence is not."""
    import types

    from core.inference import sandbox_linux

    home = tmp_path / "home" / "alice"
    (home / ".ssh").mkdir(parents = True)
    (home / ".ssh" / "id_ed25519").write_text("private")
    models = tmp_path / "models"
    for name in ("xet", "datasets", "assets"):
        (models / name).mkdir(parents = True, exist_ok = True)

    monkeypatch.setenv("HOME", str(home))
    settings = types.ModuleType("utils.hf_cache_settings")
    settings.get_hf_cache_paths = lambda: types.SimpleNamespace(
        cache_home = str(models),
        hub_cache = str(home),
        xet_cache = str(models / "xet"),
    )
    settings.effective_cache_home = lambda: models
    monkeypatch.setitem(sys.modules, "utils.hf_cache_settings", settings)
    monkeypatch.setattr(sandbox_linux, "_cache_hazard_within_deadline", lambda name, path: None)

    binds = sandbox_linux._model_cache_binds(str(tmp_path / "work"))

    assert "hub" not in binds, "the user's home was shared into the sandbox writable"
    assert binds.get("xet") == str(models / "xet"), "an ordinary cache stopped working"


def test_a_filesystem_root_is_never_a_cache(monkeypatch, tmp_path):
    """Cheap, and the worst case of the same mistake."""
    from core.inference import sandbox_linux

    monkeypatch.setenv("HOME", str(tmp_path / "somewhere-else"))

    assert sandbox_linux._too_broad_for_a_cache("/") is True
    assert sandbox_linux._too_broad_for_a_cache("/home") is True
    assert sandbox_linux._too_broad_for_a_cache(str(tmp_path / "models" / "hub")) is False


def _stub_capable_backend(monkeypatch, limitations):
    """An available capability and a backend that builds, so the test is about prepare_tool_launch's decision rather than about this host's bwrap."""
    from core.inference import os_sandbox, sandbox_linux

    capability = os_sandbox.SandboxCapability(
        backend = "stub",
        available = True,
        reason = "stub",
        environment = "linux",
        protection_state = "qualified",
        profile_id = "stub",
        limitations = (),
    )
    monkeypatch.setattr(os_sandbox, "capability_snapshot", lambda *a, **k: capability)

    def build(plan):
        return os_sandbox.PreparedSandboxLaunch(
            argv = plan.argv,
            workdir = plan.workdir,
            env = dict(plan.env),
            preexec_fn = None,
            backend = "stub",
            launch_limitations = limitations,
        )

    monkeypatch.setattr(sandbox_linux, "prepare", build)


def test_required_refuses_a_workdir_it_could_not_finish_checking(monkeypatch, tmp_path):
    """`auto` degrades here on purpose, and `required` must not."""
    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    workdir.mkdir()
    for i in range(8):
        (workdir / f"f{i}").write_text("")

    monkeypatch.setattr(os_sandbox, "WORKDIR_SCAN_ENTRIES", 3)

    limitations = os_sandbox.scan_workdir_for_host_channels(str(workdir))
    assert limitations == (
        os_sandbox.WORKDIR_SCAN_INCOMPLETE,
    ), "the budget did not trip, so this test proves nothing"
    _stub_capable_backend(monkeypatch, limitations)

    plan = os_sandbox.ToolLaunchPlan(
        argv = ("/bin/true",),
        workdir = str(workdir),
        env = {},
        requested_mode = "required",
        timeout_seconds = 10,
    )

    with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "too large to check"):
        os_sandbox.prepare_tool_launch(plan)


def test_auto_still_launches_on_the_same_workdir(monkeypatch, tmp_path):
    """The other half of the same decision: the brick fix must survive."""
    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    workdir.mkdir()
    for i in range(8):
        (workdir / f"f{i}").write_text("")

    monkeypatch.setattr(os_sandbox, "WORKDIR_SCAN_ENTRIES", 3)
    _stub_capable_backend(monkeypatch, (os_sandbox.WORKDIR_SCAN_INCOMPLETE,))

    plan = os_sandbox.ToolLaunchPlan(
        argv = ("/bin/true",),
        workdir = str(workdir),
        env = {},
        requested_mode = "auto",
        timeout_seconds = 10,
    )

    prepared = os_sandbox.prepare_tool_launch(plan)
    try:
        assert prepared.execution_record is not None
        assert os_sandbox.WORKDIR_SCAN_INCOMPLETE in prepared.execution_record.limitations
    finally:
        prepared.cleanup()


def test_a_registered_model_folder_is_bound_read_only(monkeypatch, tmp_path):
    """The same disagreement on Linux: silent at the approval gate, absent from the binds, so the read fails inside the jail."""
    from core.inference import os_sandbox

    library = tmp_path / "library" / "models"
    library.mkdir(parents = True)
    workdir = tmp_path / "work"
    workdir.mkdir()
    monkeypatch.setattr(os_sandbox, "model_library_roots", lambda: (str(library),))

    from core.inference import sandbox_linux

    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    monkeypatch.setattr(sandbox_linux, "model_library_roots", lambda: (str(library),))

    launch = sandbox_linux.prepare(_plan(workdir))
    try:
        pairs = _pairs(launch.argv, "--ro-bind-try")
    finally:
        launch.cleanup()

    assert (str(library), str(library)) in pairs, "the registered model folder was not bound"


def test_a_model_folder_that_is_a_system_directory_is_refused(monkeypatch):
    """A registered folder that is really /etc or a home is a misconfiguration, and binding it would undo the rest of the profile."""
    from core.inference import os_sandbox, tool_path_approval

    monkeypatch.setattr(tool_path_approval, "_scan_folder_roots", lambda: ("/etc", "/"))
    monkeypatch.setattr(
        "utils.paths.storage_roots.well_known_model_dirs",
        lambda: (os.path.expanduser("~"),),
        raising = False,
    )

    assert os_sandbox.model_library_roots() == ()


def test_a_model_folder_that_is_a_credential_directory_is_refused(monkeypatch, tmp_path):
    """OLLAMA_MODELS=~/.ssh would otherwise grant the keys the approval gate still asks about."""
    from core.inference import os_sandbox, tool_path_approval

    keys = tmp_path / ".ssh"
    models = tmp_path / "models"
    keys.mkdir()
    models.mkdir()
    monkeypatch.setattr(tool_path_approval, "_scan_folder_roots", lambda: (str(keys),))
    monkeypatch.setattr(
        "utils.paths.storage_roots.well_known_model_dirs",
        lambda: (str(models),),
        raising = False,
    )

    assert os_sandbox.model_library_roots() == (os.path.realpath(models),)


def _bind_unix_socket(path):
    """Create a bound AF_UNIX socket at *path*."""
    import socket

    here = os.getcwd()
    os.chdir(os.path.dirname(path))
    try:
        listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        try:
            listener.bind(os.path.basename(path))
        finally:
            listener.close()
    finally:
        os.chdir(here)


def test_a_stale_tool_socket_does_not_brick_the_session(tmp_path):
    """A crashed tool's leftover listener must not end the chat."""
    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    scratch = workdir / os_sandbox.TOOL_TEMP_DIRNAME / "pymp-abc"
    scratch.mkdir(parents = True)
    os.chmod(scratch.parent, 0o700)
    _bind_unix_socket(str(scratch / "listener-0"))
    assert (scratch / "listener-0").exists(), "the stale socket was not created"

    assert os_sandbox.scan_workdir_for_host_channels(str(workdir)) == ()
    assert not (scratch / "listener-0").exists(), "the stale socket was left in place"


def test_a_socket_outside_the_scratch_directory_still_fails_the_call(tmp_path):
    """The sweep is an exemption for one Studio-owned directory, not a way past the check: a socket anywhere else in the workdir is still fatal."""
    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    workdir.mkdir()
    _bind_unix_socket(str(workdir / "planted"))

    with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "device or IPC node"):
        os_sandbox.scan_workdir_for_host_channels(str(workdir))


def test_a_scratch_entry_that_cannot_be_removed_still_fails_the_call(tmp_path):
    """Fails closed: what the sweep could not unlink is still a finding."""
    from core.inference import os_sandbox

    if os.geteuid() == 0:
        pytest.skip("root ignores the directory permissions this test relies on")

    workdir = tmp_path / "work"
    scratch = workdir / os_sandbox.TOOL_TEMP_DIRNAME
    held = scratch / "held"
    held.mkdir(parents = True)
    os.chmod(scratch, 0o700)
    _bind_unix_socket(str(held / "listener-0"))
    os.chmod(held, 0o500)
    try:
        with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "device or IPC node"):
            os_sandbox.scan_workdir_for_host_channels(str(workdir))
    finally:
        os.chmod(held, 0o700)


def test_concurrent_launches_share_one_workdir_scan(monkeypatch, tmp_path):
    """A second launch used to see the first one's walk in flight and report the workdir unchecked, which required refuses."""
    import threading
    import time

    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    workdir.mkdir()
    walks = []

    def slow(
        root,
        max_entries,
        seconds,
        witness = None,
    ):
        walks.append(root)
        time.sleep(0.5)
        return None

    monkeypatch.setattr(os_sandbox, "_host_channel_hazard", slow)
    results = []
    callers = [
        threading.Thread(
            target = lambda: results.append(os_sandbox.scan_workdir_for_host_channels(str(workdir)))
        )
        for _ in range(3)
    ]
    for caller in callers:
        caller.start()
    for caller in callers:
        caller.join(20)

    assert results == [(), (), ()], results
    assert len(walks) == 1, "each launch walked the workdir again"


def test_a_workdir_scan_that_blocks_is_given_up_on_rather_than_waited_out(monkeypatch, tmp_path):
    """The budget is checked between entries, which is only a deadline while the walk is running."""
    import threading
    import time

    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    workdir.mkdir()
    released = threading.Event()

    def wedged(root, max_entries, seconds):
        released.wait(8)
        return None

    monkeypatch.setattr(os_sandbox, "_host_channel_hazard", wedged)
    monkeypatch.setattr(os_sandbox, "WORKDIR_SCAN_SECONDS", 0.3)

    started = time.monotonic()
    try:
        limitations = os_sandbox.scan_workdir_for_host_channels(str(workdir))
        waited = time.monotonic() - started
    finally:
        released.set()

    assert limitations == (os_sandbox.WORKDIR_SCAN_INCOMPLETE,)
    assert waited < 5, f"the caller waited {waited:.1f}s on a wedged scan"


def test_a_windows_drive_root_is_not_a_model_library():
    """`C:\\` is not os.sep, so the filesystem-root check missed it and a folder registered as the whole system drive was granted read access."""
    import ntpath
    import posixpath

    from core.inference import os_sandbox

    assert os_sandbox._is_filesystem_root("C:\\", ntpath)
    assert os_sandbox._is_filesystem_root("\\\\server\\share\\", ntpath)
    assert not os_sandbox._is_filesystem_root("C:\\Models", ntpath)
    assert os_sandbox._is_filesystem_root("/", posixpath)
    assert not os_sandbox._is_filesystem_root("/models", posixpath)


def test_a_windows_system_directory_is_not_a_model_library(monkeypatch, tmp_path):
    """The Windows system directories are not fixed paths, so they are read from the environment rather than spelled in a POSIX-only table."""
    from core.inference import os_sandbox, tool_path_approval

    windows = tmp_path / "Windows"
    windows.mkdir()
    monkeypatch.setenv("SystemRoot", str(windows))
    monkeypatch.setattr(tool_path_approval, "_scan_folder_roots", lambda: (str(windows),))
    monkeypatch.setattr(
        "utils.paths.storage_roots.well_known_model_dirs",
        lambda: (),
        raising = False,
    )

    assert os_sandbox.model_library_roots() == ()


def test_the_directory_holding_every_home_is_not_a_model_library(monkeypatch, tmp_path):
    """/home and /Users were listed literally; the Windows equivalent is C:\\Users under whichever drive Windows was installed on."""
    from core.inference import os_sandbox, tool_path_approval

    homes = tmp_path / "homes"
    (homes / "someone").mkdir(parents = True)
    monkeypatch.setenv("HOME", str(homes / "someone"))
    monkeypatch.setattr(tool_path_approval, "_scan_folder_roots", lambda: (str(homes),))
    monkeypatch.setattr(
        "utils.paths.storage_roots.well_known_model_dirs",
        lambda: (),
        raising = False,
    )

    assert os_sandbox.model_library_roots() == ()


def test_a_live_listener_in_the_scratch_directory_is_not_removed(tmp_path):
    """Two tool calls can share the scratch directory, so "left behind" has to be proved: a listener the other call is still using must survive."""
    import socket

    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    scratch = workdir / os_sandbox.TOOL_TEMP_DIRNAME
    scratch.mkdir(parents = True)
    os.chmod(scratch, 0o700)

    here = os.getcwd()
    os.chdir(scratch)
    listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    try:
        listener.bind("listener-0")
        listener.listen(1)
        os.chdir(here)
        assert os_sandbox.clear_stale_tool_ipc(str(workdir)) == ()
        assert (scratch / "listener-0").exists(), "a live listener was unlinked"
    finally:
        os.chdir(here)
        listener.close()


def test_a_scratch_directory_studio_did_not_create_is_left_alone(tmp_path):
    """_sandbox_temp_dir adopts an existing unsloth-tmp rather than failing, so a project that already had one must not have its own endpoints swept."""
    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    scratch = workdir / os_sandbox.TOOL_TEMP_DIRNAME
    scratch.mkdir(parents = True)
    os.chmod(scratch, 0o755)
    _bind_unix_socket(str(scratch / "theirs"))

    assert os_sandbox.clear_stale_tool_ipc(str(workdir)) == ()
    assert (scratch / "theirs").exists(), "a directory Studio did not create was swept"


def test_a_dormant_fifo_is_never_swept(tmp_path):
    """A FIFO with no reader is at rest, not abandoned: it is meant to outlive the processes at its ends, and 0700 plus ownership is evidence of who made the directory rather than proof."""
    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    scratch = workdir / os_sandbox.TOOL_TEMP_DIRNAME
    scratch.mkdir(parents = True)
    os.chmod(scratch, 0o700)
    os.mkfifo(scratch / "theirs", 0o600)

    assert os_sandbox.clear_stale_tool_ipc(str(workdir)) == ()
    assert (scratch / "theirs").exists(), "an idle named pipe was deleted"
    with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "device or IPC node"):
        os_sandbox.scan_workdir_for_host_channels(str(workdir))


def test_the_stale_ipc_sweep_runs_inside_the_scan_budget(monkeypatch, tmp_path):
    """The sweep walks the same workdir, so it can block on the same wedged mount."""
    import threading
    import time

    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    workdir.mkdir()
    released = threading.Event()

    def wedged_sweep(_workdir, _deadline = None):
        released.wait(8)
        return ()

    monkeypatch.setattr(os_sandbox, "clear_stale_tool_ipc", wedged_sweep)
    monkeypatch.setattr(os_sandbox, "WORKDIR_SCAN_SECONDS", 0.3)

    started = time.monotonic()
    try:
        limitations = os_sandbox.scan_workdir_for_host_channels(str(workdir))
        waited = time.monotonic() - started
    finally:
        released.set()

    assert limitations == (os_sandbox.WORKDIR_SCAN_INCOMPLETE,)
    assert waited < 5, f"the caller waited {waited:.1f}s on a wedged sweep"


def test_the_sweep_stops_at_its_deadline(tmp_path):
    """Past the deadline it leaves the rest for the walk to refuse rather than spending the call's time on a directory it may never finish."""
    import time

    from core.inference import os_sandbox

    workdir = tmp_path / "work"
    scratch = workdir / os_sandbox.TOOL_TEMP_DIRNAME
    scratch.mkdir(parents = True)
    os.chmod(scratch, 0o700)
    _bind_unix_socket(str(scratch / "listener-0"))

    assert os_sandbox.clear_stale_tool_ipc(str(workdir), time.monotonic() - 1) == ()
    assert (scratch / "listener-0").exists()
    assert os_sandbox.clear_stale_tool_ipc(str(workdir)) == (str(scratch / "listener-0"),)


def test_a_channel_planted_deep_inside_a_cached_component_invalidates_its_verdict(
    monkeypatch, tmp_path
):
    """The memo made the walk cheap and made invalidation wrong: a socket created inside an existing nested directory leaves the component root's mtime alone, so a clean verdict could be reused over exactly the channel the scan exists to reject."""
    from core.inference import sandbox_linux

    cache = tmp_path / "hostcache"
    nested = cache / "hub" / "models--org--name" / "snapshots" / "abc"
    nested.mkdir(parents = True)
    (nested / "config.json").write_text("{}")
    _share_cache_paths(monkeypatch, cache)
    sandbox_linux.reset_cache_verdicts()

    session = str(tmp_path / "session")
    assert "hub" in sandbox_linux._model_cache_binds(session), "the clean cache was not shared"

    _bind_unix_socket(str(nested / "planted"))
    binds = sandbox_linux._model_cache_binds(session)

    assert "hub" not in binds, "a cached verdict was reused over a newly planted socket"


def test_an_unchanged_cache_is_not_walked_again(monkeypatch, tmp_path):
    """The other half: revalidation has to stay cheaper than the walk it replaces, or the memo is pointless."""
    from core.inference import sandbox_linux

    cache = tmp_path / "hostcache"
    (cache / "hub" / "models--org--name").mkdir(parents = True)
    (cache / "hub" / "models--org--name" / "config.json").write_text("{}")
    _share_cache_paths(monkeypatch, cache)
    sandbox_linux.reset_cache_verdicts()

    session = str(tmp_path / "session")
    assert "hub" in sandbox_linux._model_cache_binds(session)

    walks: list[str] = []
    real = sandbox_linux._inspect_cache_component

    def counted(
        name,
        path,
        witness = None,
    ):
        walks.append(path)
        return real(name, path, witness)

    monkeypatch.setattr(sandbox_linux, "_inspect_cache_component", counted)
    assert "hub" in sandbox_linux._model_cache_binds(session)
    assert walks == [], f"the unchanged cache was walked again: {walks}"


def test_a_bwrap_planted_on_path_is_refused_before_it_runs(monkeypatch, tmp_path):
    """bwrap runs on the host before any isolation exists, so a user-writable one must never be executed."""
    planted = tmp_path / "bin"
    planted.mkdir()
    marker = tmp_path / "ran"
    fake = planted / "bwrap"
    fake.write_text(f"#!/bin/sh\ntouch {marker}\n", encoding = "utf-8")
    fake.chmod(0o755)
    monkeypatch.setenv("PATH", f"{planted}{os.pathsep}{os.environ.get('PATH', '')}")
    workdir = tmp_path / "session"
    workdir.mkdir()
    plan = os_sandbox.ToolLaunchPlan(
        argv = (sys.executable, "-c", "pass"),
        workdir = str(workdir),
        env = {"PATH": os.environ["PATH"]},
        execution_kind = "python",
        timeout_seconds = 5,
    )

    with pytest.raises(os_sandbox.SandboxUnavailableError, match = "trusted system installation"):
        sandbox_linux.prepare(plan)
    assert not marker.exists(), "the planted bwrap was executed"


def test_studio_state_inside_a_runtime_path_is_carved_out(monkeypatch, tmp_path):
    """A runtime root bound whole would hand over a Studio home that lives inside it, auth.db included."""
    lib = tmp_path / "venv" / "lib"
    state = lib / "studio-state"
    (state / "auth").mkdir(parents = True)
    (lib / "python3.12").mkdir()
    managed = tmp_path / "home" / "studio" / "venv"
    managed.mkdir(parents = True)
    monkeypatch.setattr(
        sandbox_linux, "studio_state_roots", lambda: (str(state), str(tmp_path / "home" / "studio"))
    )

    kept = sandbox_linux._without_state_inside((str(lib), str(managed)))

    assert str(lib) not in kept
    assert str(state) not in kept
    assert str(lib / "python3.12") in kept
    assert str(managed) in kept, "the managed venv inside the Studio home must stay readable"


@pytest.mark.parametrize(
    "stderr,refused",
    [
        ("bwrap: Can't mount proc on /newroot/proc: Permission denied", True),  # bwrap 0.9 (Colab)
        ("bwrap: Can't mount proc on /proc: Operation not permitted", True),  # bwrap 0.12
        ("bwrap: Can't mount proc on /newroot/proc: Invalid argument", True),
        ("bwrap: setting up uid map: Permission denied", False),
        ("bwrap: Can't mount tmpfs on /newroot/tmp: Permission denied", False),
        ("bwrap: Can't mount proc on /newroot/proc: No such file or directory", False),
        ("", False),
    ],
)
def test_only_a_refused_proc_mount_reads_as_a_masked_container(stderr, refused):
    assert sandbox_linux.proc_mount_refused(stderr) is refused


def _fake_bwrap(
    tmp_path,
    *,
    fresh_proc,
    without_proc = 0,
):
    """A bwrap whose fresh /proc fails with `fresh_proc` (stderr) and whose other runs exit `without_proc`."""
    record = tmp_path / "bwrap_calls"
    fake = tmp_path / "bwrap"
    fake.write_text(
        "#!/bin/sh\n"
        f'echo "$*" >> "{record}"\n'
        'for a in "$@"; do if [ "$a" = "--proc" ]; then\n'
        + (f"  echo {shlex.quote(fresh_proc)} >&2; exit 1\n" if fresh_proc else "  exit 0\n")
        + "fi; done\n"
        + f"exit {without_proc}\n"
    )
    fake.chmod(0o755)
    sandbox_linux.forget_proc_layout()
    return str(fake), record


@pytest.fixture
def _no_proc_layout_left_behind():
    sandbox_linux.forget_proc_layout()
    yield
    sandbox_linux.forget_proc_layout()


def test_a_container_refusing_a_fresh_proc_gets_an_empty_one(tmp_path, _no_proc_layout_left_behind):
    fake, record = _fake_bwrap(
        tmp_path, fresh_proc = "bwrap: Can't mount proc on /newroot/proc: Permission denied"
    )
    assert sandbox_linux.empty_proc_layout(fake) is True
    assert sandbox_linux.empty_proc_layout(fake) is True
    runs = record.read_text().splitlines()
    assert len(runs) == 2, "the preflight is cached per bwrap"
    assert "--proc /proc" in runs[0] and "--proc" not in runs[1]


def test_the_preflight_reads_bwrap_errors_in_the_c_locale(
    tmp_path, monkeypatch, _no_proc_layout_left_behind
):
    # strerror follows the locale; the detector matches the English text.
    monkeypatch.setenv("LC_ALL", "de_DE.UTF-8")
    monkeypatch.setenv("LANG", "de_DE.UTF-8")
    fake = tmp_path / "bwrap"
    fake.write_text(
        "#!/bin/sh\n"
        'for a in "$@"; do if [ "$a" = "--proc" ]; then\n'
        '  if [ "$LC_ALL" = C ]; then echo "bwrap: Can\'t mount proc on /newroot/proc: Permission denied" >&2;\n'
        '  else echo "bwrap: Can\'t mount proc on /newroot/proc: Keine Berechtigung" >&2; fi; exit 1\n'
        "fi; done\nexit 0\n"
    )
    fake.chmod(0o755)
    assert sandbox_linux.empty_proc_layout(str(fake)) is True


@pytest.mark.parametrize(
    "fresh_proc,without_proc",
    [
        ("", 0),  # a fresh procfs works: the normal layout
        ("bwrap: setting up uid map: Permission denied", 1),  # bwrap is broken for another reason
        ("bwrap: Can't mount proc on /newroot/proc: No such file or directory", 0),
        (
            "bwrap: Can't mount proc on /proc: Permission denied",
            1,
        ),  # nothing works without /proc either
    ],
)
def test_any_other_outcome_keeps_the_fresh_proc(
    tmp_path, _no_proc_layout_left_behind, fresh_proc, without_proc
):
    fake, _record = _fake_bwrap(tmp_path, fresh_proc = fresh_proc, without_proc = without_proc)
    assert sandbox_linux.empty_proc_layout(fake) is False


@pytest.mark.parametrize("empty", [True, False])
def test_the_launch_mounts_an_empty_private_proc_never_the_hosts(tmp_path, monkeypatch, empty):
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a: empty)
    launch = sandbox_linux.prepare(_plan(tmp_path))
    try:
        argv = launch.argv
        assert ("/proc", "/proc") not in _pairs(argv, "--ro-bind")
        assert ("/proc", "/proc") not in _pairs(argv, "--bind")
        assert ("/proc", "/proc") not in _pairs(argv, "--ro-bind-try")
        tmpfs = [argv[i + 1] for i, token in enumerate(argv) if token == "--tmpfs"]
        procs = [argv[i + 1] for i, token in enumerate(argv) if token == "--proc"]
        assert ("/proc" in tmpfs) is empty
        assert procs == ([] if empty else ["/proc"])
    finally:
        launch.cleanup()


@pytest.mark.parametrize("empty", [True, False])
def test_an_empty_proc_blocks_nested_user_namespaces_with_seccomp(tmp_path, monkeypatch, empty):
    # Colab: --disable-userns fails with "cannot open /proc/sys/user/max_user_namespaces".
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a: empty)
    monkeypatch.setattr(sandbox_linux, "_bwrap_supports", lambda _bwrap, _option: True)
    built = []
    real_filter = sandbox_linux.sandbox_seccomp.filter_file

    def filter_file(**kwargs):
        built.append(kwargs["block_userns"])
        return real_filter(**kwargs)

    monkeypatch.setattr(sandbox_linux.sandbox_seccomp, "filter_file", filter_file)
    launch = sandbox_linux.prepare(_plan(tmp_path))
    try:
        assert ("--disable-userns" in launch.argv) is not empty
        assert built == [empty]
    finally:
        launch.cleanup()


def test_the_two_proc_layouts_never_share_a_profile_or_a_cached_verdict(monkeypatch):
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    seen = {}
    for empty in (False, True):
        monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a, e = empty: e)
        seen[empty] = (
            sandbox_linux.profile_id(),
            sandbox_linux.limitations(),
            sandbox_linux.bwrap_identity(),
        )
    assert seen[False][0] == sandbox_linux.PROFILE_ID != seen[True][0]
    assert {"no_process_filesystem", "no_dev_fd_links"} <= set(seen[True][1])
    assert not {"no_process_filesystem", "no_dev_fd_links"} & set(seen[False][1])
    assert seen[False][2] != seen[True][2]


@pytest.mark.parametrize("linked", [True, False], ids = ["fhs-or-nixos", "plain-host"])
def test_a_launch_binds_the_store_when_system_files_link_into_it(tmp_path, monkeypatch, linked):
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    # The interpreter is not a store path, as with Studio's own Python inside an FHS env.
    nix_true = "/nix/store/abc-coreutils/bin/true" if linked else "/usr/bin/true"
    real_isdir = os.path.isdir
    monkeypatch.setattr(sandbox_linux, "_system_true", lambda: nix_true)
    monkeypatch.setattr(os.path, "isdir", lambda p: p == sandbox_linux._NIX_STORE or real_isdir(p))
    monkeypatch.setattr(
        sandbox_linux, "_nixos_current_system", lambda: "/nix/store/xyz-nixos-system"
    )
    # A NixOS interpreter's spelling: binding it would turn /run/current-system into a directory.
    nixos_python = "/run/current-system/sw/bin/python3"
    real_runtime = sandbox_linux._runtime_read_paths
    monkeypatch.setattr(
        sandbox_linux,
        "_runtime_read_paths",
        lambda *a, **kw: (*real_runtime(*a, **kw), nixos_python),
    )
    launch = sandbox_linux.prepare(_plan(tmp_path))
    try:
        assert (("/nix/store", "/nix/store") in _pairs(launch.argv, "--ro-bind-try")) is linked
        symlinks = _pairs(launch.argv, "--symlink")
        assert (("/nix/store/xyz-nixos-system", "/run/current-system") in symlinks) is linked
        assert ((nixos_python, nixos_python) in _pairs(launch.argv, "--ro-bind")) is not linked
    finally:
        launch.cleanup()


def test_the_preflight_runs_a_nix_store_true_with_the_store_bound(monkeypatch):
    nix_true = "/nix/store/abc-coreutils/bin/true"
    real_isfile, real_isdir, real_realpath = os.path.isfile, os.path.isdir, os.path.realpath
    monkeypatch.setattr(
        os.path,
        "isfile",
        lambda p: p == "/run/current-system/sw/bin/true"
        or (real_isfile(p) and p not in ("/usr/bin/true", "/bin/true")),
    )
    monkeypatch.setattr(os.path, "isdir", lambda p: p == sandbox_linux._NIX_STORE or real_isdir(p))
    monkeypatch.setattr(
        os.path,
        "realpath",
        lambda p, **kw: nix_true
        if p == "/run/current-system/sw/bin/true"
        else real_realpath(p, **kw),
    )
    seen = []
    monkeypatch.setattr(
        sandbox_linux.subprocess,
        "run",
        lambda argv, **kw: seen.append(argv) or subprocess.CompletedProcess(argv, 0, "", ""),
    )
    assert sandbox_linux._preflight("/usr/bin/bwrap", ("--proc", "/proc")) is not None
    argv = seen[0]
    assert argv[-1] == nix_true
    assert ("--ro-bind-try", "/nix/store", "/nix/store") in zip(argv, argv[1:], argv[2:])


def test_an_inconclusive_preflight_backs_off_then_retries(monkeypatch, _no_proc_layout_left_behind):
    refused = subprocess.CompletedProcess(
        [], 1, "", "bwrap: Can't mount proc on /newroot/proc: Permission denied"
    )
    answers = iter([None, refused, subprocess.CompletedProcess([], 0, "", "")])
    runs = []
    monkeypatch.setattr(
        sandbox_linux, "_preflight", lambda bwrap, proc: runs.append(proc) or next(answers)
    )
    clock = [1000.0]
    monkeypatch.setattr(sandbox_linux.time, "monotonic", lambda: clock[0])
    identity = ("/usr/bin/bwrap", 1, 1)
    assert sandbox_linux._fresh_proc_refused(identity) is False  # timed out: no verdict kept
    assert sandbox_linux._fresh_proc_refused(identity) is False
    assert len(runs) == 1, "a wedged bwrap is not rerun on every read"
    clock[0] += sandbox_linux._INCONCLUSIVE_RETRY_SECONDS + 1
    assert sandbox_linux._fresh_proc_refused(identity) is True


def test_a_failure_before_proc_is_retried_once_the_host_is_fixed(
    monkeypatch, _no_proc_layout_left_behind
):
    """An AppArmor uid-map denial says nothing about /proc; repaired by hand, a masked /proc must still be found."""
    uid_map = subprocess.CompletedProcess([], 1, "", "bwrap: setting up uid map: Permission denied")
    refused = subprocess.CompletedProcess(
        [], 1, "", "bwrap: Can't mount proc on /newroot/proc: Permission denied"
    )
    answers = iter([uid_map, refused, subprocess.CompletedProcess([], 0, "", "")])
    monkeypatch.setattr(sandbox_linux, "_preflight", lambda bwrap, proc: next(answers))
    clock = [1000.0]
    monkeypatch.setattr(sandbox_linux.time, "monotonic", lambda: clock[0])
    identity = ("/usr/bin/bwrap", 1, 1)
    assert sandbox_linux._fresh_proc_refused(identity) is False
    clock[0] += sandbox_linux._INCONCLUSIVE_RETRY_SECONDS + 1
    assert sandbox_linux._fresh_proc_refused(identity) is True


def test_a_reset_retries_an_inconclusive_preflight_at_once(
    monkeypatch, _no_proc_layout_left_behind
):
    answers = iter([None, subprocess.CompletedProcess([], 0, "", "")])
    monkeypatch.setattr(sandbox_linux, "_preflight", lambda bwrap, proc: next(answers))
    identity = ("/usr/bin/bwrap", 1, 1)
    assert sandbox_linux._fresh_proc_refused(identity) is False
    sandbox_linux.forget_proc_layout()
    assert sandbox_linux._fresh_proc_refused(identity) is False
    with pytest.raises(StopIteration):
        next(answers)


def test_a_forced_capability_check_re_decides_the_proc_layout(monkeypatch):
    if sys.platform != "linux":
        pytest.skip("Linux layout only")
    from core.inference import os_sandbox

    class Forgot(Exception):
        pass

    def forget():
        raise Forgot

    monkeypatch.setattr(sandbox_linux, "forget_proc_layout", forget)
    with pytest.raises(Forgot):
        os_sandbox.capability_snapshot(force = True, execution_kind = "python")


def test_one_launch_reads_the_proc_layout_once(tmp_path, monkeypatch):
    if sandbox_linux.shutil.which("bwrap") is None:
        pytest.skip("bubblewrap is not installed on this host")
    answers = iter([True, False, False, False])
    monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda *_a: next(answers))
    monkeypatch.setattr(sandbox_linux, "_bwrap_supports", lambda _bwrap, _option: True)
    launch = sandbox_linux.prepare(_plan(tmp_path))
    try:
        argv = launch.argv
        tmpfs = [argv[i + 1] for i, token in enumerate(argv) if token == "--tmpfs"]
        assert "--disable-userns" not in argv and "/proc" in tmpfs and "--proc" not in argv
    finally:
        launch.cleanup()


def test_resetting_the_probe_forgets_the_proc_layout(tmp_path, _no_proc_layout_left_behind):
    from core.inference import sandbox_probe

    fake, record = _fake_bwrap(
        tmp_path, fresh_proc = "bwrap: Can't mount proc on /newroot/proc: Permission denied"
    )
    assert sandbox_linux.empty_proc_layout(fake) is True
    sandbox_probe.reset_probe_cache()
    assert sandbox_linux.empty_proc_layout(fake) is True
    assert len(record.read_text().splitlines()) == 4, "the layout was not checked again"


class _Warnings:
    def __init__(self):
        self.warnings = []

    def warning(self, msg, *args, **_kw):
        self.warnings.append(msg % args if args else msg)

    def __getattr__(self, _name):
        return lambda *_a, **_k: None


@pytest.fixture
def cache_log(monkeypatch):
    log = _Warnings()
    monkeypatch.setattr(sandbox_linux, "logger", log)
    monkeypatch.setattr(sandbox_linux, "_cache_warned", {})
    monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})
    sandbox_linux.reset_cache_verdicts()
    yield log
    sandbox_linux.reset_cache_verdicts()


def test_a_cache_folder_a_fresh_hf_home_lacks_is_created_and_shared(
    tmp_path, monkeypatch, cache_log
):
    cache = tmp_path / "hostcache"
    (cache / "hub").mkdir(parents = True)
    _share_cache_paths(monkeypatch, cache)
    binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))
    for name in ("hub", "datasets", "assets", "xet"):
        assert name in binds and os.path.isdir(cache / name), name
    assert cache_log.warnings == []


def test_a_cache_folder_that_cannot_be_created_is_skipped_quietly(tmp_path, monkeypatch, cache_log):
    cache = tmp_path / "hostcache"
    (cache / "hub").mkdir(parents = True)
    _share_cache_paths(monkeypatch, cache)
    real = os.makedirs

    def refuse(path, *a, **k):
        if os.path.basename(path) == "datasets":
            raise PermissionError(13, "Permission denied", path)
        return real(path, *a, **k)

    monkeypatch.setattr(sandbox_linux.os, "makedirs", refuse)
    binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))
    assert "datasets" not in binds and "hub" in binds
    assert cache_log.warnings == []


def test_a_cache_entry_that_is_not_a_folder_still_warns(tmp_path, monkeypatch, cache_log):
    cache = tmp_path / "hostcache"
    (cache / "hub").mkdir(parents = True)
    (cache / "datasets").write_text("not a folder")
    _share_cache_paths(monkeypatch, cache)
    binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))
    assert "datasets" not in binds
    assert cache_log.warnings == [
        "Not sharing the datasets cache into the sandbox: it is not a directory"
    ]


def test_the_same_cache_warning_is_logged_once_a_minute(tmp_path, monkeypatch, cache_log):
    cache = tmp_path / "hostcache"
    (cache / "hub").mkdir(parents = True)
    (cache / "datasets").write_text("not a folder")
    _share_cache_paths(monkeypatch, cache)
    session = str(tmp_path / "session")
    clock = [1000.0]
    monkeypatch.setattr(sandbox_linux.time, "monotonic", lambda: clock[0])
    sandbox_linux._model_cache_binds(session)
    clock[0] += 30
    sandbox_linux._model_cache_binds(session)
    assert len(cache_log.warnings) == 1
    clock[0] += sandbox_linux._CACHE_WARNING_SECONDS
    sandbox_linux._model_cache_binds(session)
    assert len(cache_log.warnings) == 2


def test_a_launch_finding_a_check_in_progress_waits_for_its_answer(
    tmp_path, monkeypatch, cache_log
):
    component = tmp_path / "hub"
    component.mkdir()
    started = threading.Event()
    release = threading.Event()
    walks = []

    def slow(
        name,
        path,
        witness = None,
    ):
        walks.append(path)
        started.set()
        release.wait(30)
        return None

    monkeypatch.setattr(sandbox_linux, "_inspect_cache_component", slow)
    monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.2)
    assert sandbox_linux._cache_hazard_within_deadline("hub", str(component)) == (
        sandbox_linux.CACHE_STILL_CHECKING
    )
    assert started.wait(5)
    # Finishes inside the second launch's wait (the walk's budget is two waits).
    threading.Timer(0.05, release.set).start()
    assert sandbox_linux._cache_hazard_within_deadline("hub", str(component)) is None
    assert walks == [str(component)]


def test_every_real_prepare_call_skips_without_bubblewrap():
    """A test that runs the real prepare() errors on a host without bwrap unless it asks for the skip or fakes which()."""
    import ast

    tree = ast.parse(open(__file__, encoding = "utf-8").read())
    unguarded = []
    for node in tree.body:
        if not (isinstance(node, ast.FunctionDef) and node.name.startswith("test_")):
            continue
        if {"bwrap", "prepared"} & {a.arg for a in node.args.args}:
            continue
        calls = [
            c
            for c in ast.walk(node)
            if isinstance(c, ast.Call)
            and isinstance(c.func, ast.Attribute)
            and c.func.attr == "prepare"
            and isinstance(c.func.value, ast.Name)
            and c.func.value.id == "sandbox_linux"
        ]
        # Guarded too: an inline skip, a faked which(), or a PATH the test sets to its own bwrap.
        guarded = any(
            isinstance(c, ast.Call)
            and isinstance(c.func, ast.Attribute)
            and (
                (
                    c.func.attr == "skip"
                    and isinstance(c.func.value, ast.Name)
                    and c.func.value.id == "pytest"
                )
                or (
                    c.func.attr in ("setattr", "setenv")
                    and any(
                        isinstance(a, ast.Constant) and a.value in ("which", "PATH") for a in c.args
                    )
                )
            )
            for c in ast.walk(node)
        )
        if calls and not guarded:
            unguarded.append(node.name)
    assert not unguarded, unguarded