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.

44.5 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

"""Tests for the macOS Seatbelt profile generator, asserting on the profile TEXT."""

from __future__ import annotations

import os
import re
import shlex
import subprocess
import sys
import zipfile
from pathlib import Path

import pytest

if sys.platform == "win32":
    pytest.skip("the Seatbelt profile generator is POSIX only", allow_module_level = True)

from core.inference import sandbox_macos as backend
from core.inference.os_sandbox import (
    SandboxUnavailableError,
    ToolLaunchPlan,
    WorkdirUnsafeError,
)

_WORKDIR = "/tmp/unsloth-session-abc123"
_PRIVATE_TMP = "/tmp/us-seatbelt-xyz789"
_READ_PREFIX = '(allow file-read* file-test-existence (literal "/") '
_OPTIONAL_PREFIX = '(allow file-read* file-test-existence (literal "/etc/gitconfig")'
_WRITE_PREFIX = "(allow file-write* (literal "


@pytest.fixture
def profile(monkeypatch) -> str:
    real_exists, real_isdir = os.path.exists, os.path.isdir
    named = {_WORKDIR, _PRIVATE_TMP}
    monkeypatch.setattr(os.path, "exists", lambda path: path in named or real_exists(path))
    monkeypatch.setattr(os.path, "isdir", lambda path: path in named or real_isdir(path))
    return backend.build_profile(workdir = _WORKDIR, private_tmp = _PRIVATE_TMP, runtime_paths = ())


@pytest.fixture
def launchable(monkeypatch):
    """Only the availability gate is stubbed; everything else runs for real."""
    monkeypatch.setattr(backend, "available", lambda: (True, "stubbed for this test"))


def _rule(profile: str, prefix: str) -> str:
    matches = [line for line in profile.splitlines() if line.startswith(prefix)]
    assert len(matches) == 1, f"expected exactly one rule starting {prefix!r}, got {len(matches)}"
    return matches[0]


def _subpaths(rule: str) -> set[str]:
    return set(re.findall(r'\(subpath "([^"]+)"\)', rule))


def _literals(rule: str) -> set[str]:
    return set(re.findall(r'\(literal "([^"]+)"\)', rule))


def test_module_imports_and_exports_the_backend_contract():
    assert backend.BACKEND_NAME == "macos-seatbelt"
    assert isinstance(backend.PROFILE_ID, str) and backend.PROFILE_ID
    assert isinstance(backend.LIMITATIONS, tuple)
    assert all(isinstance(item, str) for item in backend.LIMITATIONS)
    assert "unrestricted_network" in backend.LIMITATIONS
    assert callable(backend.prepare)


def test_profile_is_deny_default(profile):
    lines = profile.splitlines()
    assert lines[0] == "(version 1)"
    assert lines[1] == "(deny default)"
    assert "(allow default)" not in profile


def test_the_fd_directory_is_listable_so_spawning_never_brute_forces_closes(profile):
    assert '(allow file-read* (literal "/dev/fd"))' in profile.splitlines()


def test_every_rule_is_a_balanced_s_expression(profile):
    depth = 0
    in_string = False
    for index, char in enumerate(profile):
        if in_string:
            if char == "\\":
                continue
            if char == '"':
                in_string = False
            continue
        if char == '"':
            in_string = True
        elif char == "(":
            depth += 1
        elif char == ")":
            depth -= 1
            assert depth >= 0, f"unbalanced ) at offset {index}"
    assert depth == 0, f"{depth} unclosed ( in the profile"
    assert not in_string, "the profile ends inside a quoted string"
    for line in profile.splitlines():
        assert line == "" or line.startswith("(") or line.startswith("  ("), line


def test_login_keychain_mach_service_is_absent(profile):
    """com.apple.SecurityServer would make the login Keychain readable through Security.framework."""
    assert "com.apple.SecurityServer" not in profile
    assert "com.apple.SecurityServer" not in backend._MACH_SERVICES
    assert 'com.apple.trustd"' in profile
    assert 'com.apple.ocspd"' in profile
    assert '(literal "/usr/bin/security")' in _rule(profile, "(deny process-exec ")


def test_denied_executables_are_denied_after_the_exec_allowance(profile):
    """Seatbelt is last-match-wins, so the deny has to come after the allow."""
    lines = profile.splitlines()
    deny_index = next(
        index for index, line in enumerate(lines) if line.startswith("(deny process-exec ")
    )
    assert lines.index("(allow process-exec)") < deny_index
    denied = _literals(_rule(profile, "(deny process-exec "))
    for path in backend._DENIED_EXECUTABLES:
        assert path in denied, f"{path} is no longer exec-denied"


def test_both_private_spellings_are_emitted_for_the_workdir(profile):
    for rule in (_rule(profile, _READ_PREFIX), _rule(profile, _WRITE_PREFIX)):
        for path in (_WORKDIR, f"/private{_WORKDIR}"):
            assert f'(literal "{path}")' in rule
            assert f'(subpath "{path}")' in rule


def test_a_non_ascii_workdir_reaches_the_profile_unescaped(monkeypatch):
    workdir = "/tmp/unsloth-session-caf\u00e9"
    private_tmp = "/tmp/us-seatbelt-\u00fcber"
    real_exists, real_isdir = os.path.exists, os.path.isdir
    named = {workdir, private_tmp}
    monkeypatch.setattr(os.path, "exists", lambda path: path in named or real_exists(path))
    monkeypatch.setattr(os.path, "isdir", lambda path: path in named or real_isdir(path))
    text = backend.build_profile(workdir = workdir, private_tmp = private_tmp, runtime_paths = ())
    assert "\\u00" not in text
    for path in (workdir, private_tmp):
        assert f'(subpath "{path}")' in _rule(text, _WRITE_PREFIX)


def test_optional_literals_are_allowed_even_though_they_do_not_exist(profile):
    literals = _literals(_rule(profile, _OPTIONAL_PREFIX))
    for path in backend._OPTIONAL_READ_LITERALS:
        assert path in literals, f"{path} lost its unconditional read allowance"
    assert {"/private/etc/gitconfig", "/private/etc/gitattributes"} <= literals
    absent = [path for path in backend._OPTIONAL_READ_LITERALS if not os.path.exists(path)]
    assert absent, "no optional literal is absent here, so this test proved nothing"


def test_optional_read_literals_never_follow_a_symlink(tmp_path):
    """Read literals must not resolve: an /etc/gitconfig symlinked at ~/dotfiles would put a home path in the read set."""
    target = tmp_path / "secret"
    target.write_text("")
    link = tmp_path / "link"
    link.symlink_to(target)
    unresolved = backend._literal_filters((str(link),), resolve = False)
    assert f'(literal "{link}")' in unresolved
    assert not any(str(target) in filter_ for filter_ in unresolved), unresolved
    assert f'(literal "{target}")' in backend._literal_filters((str(link),))


def test_ancestor_metadata_rules_are_emitted(profile):
    metadata_rule = _rule(profile, "(allow file-read-metadata ")
    metadata = _literals(metadata_rule)
    assert "/" in metadata
    for ancestor in (
        "/tmp",
        "/private/tmp",
        "/private",
        "/etc",
        "/private/etc",
        "/usr/local/etc",
        "/var/run",
        "/private/var/run",
    ):
        assert ancestor in metadata, f"{ancestor} has no file-read-metadata rule"
    assert not _subpaths(metadata_rule)


def test_workdir_and_private_tmp_are_the_only_writable_subpaths(profile):
    writable = _subpaths(_rule(profile, _WRITE_PREFIX))
    assert writable == {_WORKDIR, f"/private{_WORKDIR}", _PRIVATE_TMP, f"/private{_PRIVATE_TMP}"}
    for forbidden in ("/", "/usr", "/tmp", "/private/tmp", str(Path.home())):
        assert forbidden not in writable
    assert not _subpaths(_rule(profile, "(allow file-read* file-test-existence file-write-data "))


def test_no_read_root_reaches_the_users_home(profile):
    """Asserted against the tables, not the profile text: the read roots are existence-filtered and mostly vanish on a non-macOS host."""
    home = str(Path.home())
    assert f'(subpath "{home}")' not in profile
    assert "/Users/" not in profile
    tables = (
        backend._READ_ROOTS
        + backend._TLS_TRUST_PATHS
        + backend._OPTIONAL_READ_ROOTS
        + backend._OPTIONAL_READ_LITERALS
    )
    for path in tables:
        assert not path.startswith("/Users/"), path
        assert "~" not in path, path
    assert "/System/Library/Keychains" in backend._TLS_TRUST_PATHS
    assert not any("Library/Keychains" in path and path.startswith(home) for path in tables)


def test_ip_egress_is_unrestricted_but_unix_sockets_are_not(profile):
    lines = profile.splitlines()
    for rule in ("(allow system-socket)", "(allow network-inbound)"):
        assert rule in lines
    assert "(allow network-outbound)" not in lines
    assert "(allow network-bind)" not in lines
    assert '(allow network-outbound (remote ip "*:*"))' in lines
    assert '(allow network-bind (local ip "*:*"))' in lines
    assert "localhost" not in profile
    assert "proxy" not in profile.lower()
    for root in (_PRIVATE_TMP, _WORKDIR):
        for path in (root, f"/private{root}"):
            assert f'(allow network-outbound (remote unix-socket (subpath "{path}")))' in lines
            assert f'(allow network-bind (local unix-socket (subpath "{path}")))' in lines
    assert '(allow network-outbound (literal "/private/var/run/mDNSResponder")' in profile
    assert "(allow network-outbound (remote unix-socket (literal " in profile
    for path in ("/var/run/mDNSResponder", "/private/var/run/mDNSResponder"):
        assert f'(allow network-outbound (remote unix-socket (literal "{path}")))' in lines
    assert '(literal "/var/run/mDNSResponder")' in profile
    assert "docker.sock" not in profile


def test_process_substitution_descriptors_are_readable(profile):
    """bash process substitution hands the child /dev/fd/63, so narrowing the rule to 0, 1 and 2 fails a command that works everywhere else."""
    assert '(allow file-read* (regex #"^/dev/fd/[0-9]+$"))' in profile
    assert '(allow file-write* (regex #"^/dev/fd/[0-9]+$"))' in profile


def test_pip_gets_a_writable_prefix_inside_the_workdir():
    env = backend._sandbox_environment(
        {"PATH": "/usr/bin", "PYTHONPATH": "/shim"}, _WORKDIR, _PRIVATE_TMP
    )
    packages = f"{_WORKDIR}/{backend.SESSION_PACKAGES_RELPATH}"
    assert env["PIP_PREFIX"] == packages
    assert "PIP_TARGET" not in env
    pythonpath = env["PYTHONPATH"].split(os.pathsep)
    assert pythonpath[0] == "/shim"
    assert pythonpath[-1] == packages
    assert all(entry.startswith(packages) for entry in pythonpath[1:])


def _console_script_wheel(directory: Path, name: str) -> None:
    info = f"{name}-1.0.dist-info"
    files = {
        f"shared/{name}.py": "def main():\n    print('ok')\n",
        f"{info}/METADATA": f"Metadata-Version: 2.1\nName: {name}\nVersion: 1.0\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",
    }
    with zipfile.ZipFile(directory / f"{name}-1.0-py3-none-any.whl", "w") as wheel:
        for path, text in files.items():
            wheel.writestr(path, text)
        wheel.writestr(
            f"{info}/RECORD", "".join(f"{path},,\n" for path in [*files, f"{info}/RECORD"])
        )


def test_a_second_pip_install_keeps_its_commands_and_namespace_modules(tmp_path):
    if subprocess.run([sys.executable, "-m", "pip", "--version"], capture_output = True).returncode:
        pytest.skip(
            reason = "pip is not installed in this interpreter, so there is no install to run"
        )
    wheels = tmp_path / "wheels"
    wheels.mkdir()
    for name in ("alpha", "beta"):
        _console_script_wheel(wheels, name)
    workdir = tmp_path / "session"
    private_tmp = tmp_path / "tmp"
    workdir.mkdir()
    private_tmp.mkdir()
    env = backend._sandbox_environment(
        {"PATH": os.environ["PATH"], "PIP_DISABLE_PIP_VERSION_CHECK": "1"},
        str(workdir),
        str(private_tmp),
    )
    for name in ("alpha", "beta"):
        installed = subprocess.run(
            [
                sys.executable,
                "-m",
                "pip",
                "install",
                "--no-index",
                "--find-links",
                str(wheels),
                name,
            ],
            env = env,
            capture_output = True,
            text = True,
        )
        assert installed.returncode == 0, installed.stdout + installed.stderr

    bin_dir = workdir / backend.SESSION_PACKAGES_RELPATH / "bin"
    assert (bin_dir / "alpha").exists()
    assert (bin_dir / "beta").exists()
    imported = subprocess.run(
        [sys.executable, "-c", "import shared.alpha, shared.beta"],
        env = env,
        capture_output = True,
        text = True,
    )
    assert imported.returncode == 0, imported.stderr


def test_reinstalling_another_version_leaves_one_version_installed(tmp_path):
    if subprocess.run([sys.executable, "-m", "pip", "--version"], capture_output = True).returncode:
        pytest.skip(
            reason = "pip is not installed in this interpreter, so there is no install to run"
        )
    wheels = tmp_path / "wheels"
    wheels.mkdir()
    for version in ("1.0", "2.0"):
        info = f"vpkg-{version}.dist-info"
        files = {
            "vpkg.py": f"VERSION = {version!r}\n",
            f"{info}/METADATA": f"Metadata-Version: 2.1\nName: vpkg\nVersion: {version}\n",
            f"{info}/WHEEL": "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n",
        }
        with zipfile.ZipFile(wheels / f"vpkg-{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"])
            )
    workdir = tmp_path / "session"
    workdir.mkdir()
    shim = Path(backend.__file__).with_name("sandbox_site")
    env = backend._sandbox_environment(
        {
            "PATH": os.environ["PATH"],
            "PYTHONPATH": str(shim),
            "PIP_DISABLE_PIP_VERSION_CHECK": "1",
        },
        str(workdir),
        str(tmp_path),
    )
    probe = "import vpkg, importlib.metadata as m; print(vpkg.VERSION, m.version('vpkg'))"
    for version in ("1.0", "2.0", "1.0"):
        installed = subprocess.run(
            [
                sys.executable,
                "-m",
                "pip",
                "install",
                "--no-index",
                "--find-links",
                str(wheels),
                f"vpkg=={version}",
            ],
            env = env,
            capture_output = True,
            text = True,
        )
        assert installed.returncode == 0, installed.stdout + installed.stderr
        seen = subprocess.run(
            [sys.executable, "-c", probe], env = env, capture_output = True, text = True
        )
        assert seen.stdout.split() == [version, version], seen.stdout + seen.stderr
    assert len(list(workdir.glob(f"{backend.SESSION_PACKAGES_RELPATH}/**/vpkg-*.dist-info"))) == 1


def test_openmp_can_write_its_registration_segment(profile):
    kmp = next(
        block for block in profile.split("(allow ipc-posix-shm") if "__KMP_REGISTERED_LIB_" in block
    )
    assert "ipc-posix-shm-write-data" in kmp, kmp


def test_sysctl_and_shm_rules_survive(profile):
    sysctl = _rule(profile, "(allow sysctl-read ")
    for name in ("hw.ncpu", "hw.memsize", "kern.osproductversion"):
        assert f'(sysctl-name "{name}")' in sysctl
    assert "(allow ipc-posix-sem)" in profile
    assert "^/torch_[0-9]+_[0-9]+_[0-9]+$" in profile


def test_runtime_read_paths_cover_the_interpreter_and_the_site_shim():
    paths = backend.runtime_read_paths()
    shim = os.path.normpath(os.path.join(os.path.dirname(backend.__file__), "sandbox_site"))
    assert shim in paths
    assert "/" not in paths and "/usr" not in paths
    for prefix in (sys.prefix, sys.exec_prefix, sys.base_prefix, sys.base_exec_prefix):
        for name in ("bin", "lib"):
            member = os.path.join(prefix, name)
            if not os.path.isdir(member):
                continue
            assert any(backend._within(member, root) for root in paths), member
    dynload = os.path.join(
        sys.base_exec_prefix,
        "lib",
        f"python{sys.version_info.major}." f"{sys.version_info.minor}",
        "lib-dynload",
    )
    if os.path.isdir(dynload):
        assert any(backend._within(dynload, root) for root in paths), dynload


def test_a_venv_at_a_project_root_does_not_put_the_project_in_the_read_set(monkeypatch, tmp_path):
    project = tmp_path / "project"
    (project / "bin").mkdir(parents = True)
    (project / "lib").mkdir()
    (project / ".git").mkdir()
    (project / ".env").write_text("OPENAI_API_KEY=sk-real")
    (project / "pyvenv.cfg").write_text("home = /usr/bin\n")
    for name in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):
        monkeypatch.setattr(sys, name, str(project))

    paths = backend.runtime_read_paths()
    assert str(project) not in paths
    assert not any(backend._within(str(project / ".env"), root) for root in paths)
    assert not any(backend._within(str(project / ".git"), root) for root in paths)
    assert str(project / "bin") in paths
    assert str(project / "lib") in paths


def test_prepare_wraps_argv_and_preserves_the_preexec(tmp_path, launchable):
    """The preexec is tools.py's os.setsid(); losing it strands the process group."""
    workdir = tmp_path / "session"
    workdir.mkdir()

    def preexec() -> None:
        return None

    plan = ToolLaunchPlan(
        argv = ("/bin/echo", "hello"),
        workdir = str(workdir),
        env = {"PATH": "/usr/bin", "DYLD_INSERT_LIBRARIES": "/evil.dylib", "KEEP": "1"},
        preexec_fn = preexec,
        timeout_seconds = 30,
    )
    prepared = backend.prepare(plan)
    private_tmp = prepared.cleanup_paths[0]
    try:
        assert prepared.argv[0] == backend.SANDBOX_EXEC
        assert prepared.argv[1] == "-p"
        assert prepared.argv[2].startswith("(version 1)\n(deny default)")
        assert prepared.argv[3] == "--"
        assert prepared.argv[4:] == ("/bin/echo", "hello")
        assert prepared.preexec_fn is preexec
        assert prepared.backend == backend.BACKEND_NAME
        assert prepared.timeout_seconds == 30
        assert prepared.env["HOME"] == str(workdir)
        assert prepared.env["KEEP"] == "1"
        assert "DYLD_INSERT_LIBRARIES" not in prepared.env
        assert len(prepared.cleanup_paths) == 1
        assert prepared.env["TMPDIR"] == private_tmp
        assert os.path.isdir(private_tmp)
        writable = _subpaths(_rule(prepared.argv[2], _WRITE_PREFIX))
        assert str(workdir) in writable and private_tmp in writable
    finally:
        prepared.cleanup()
    assert not os.path.exists(private_tmp)


def test_prepare_refuses_a_workdir_that_does_not_exist(tmp_path, launchable):
    plan = ToolLaunchPlan(argv = ("/bin/echo",), workdir = str(tmp_path / "missing"), env = {})
    with pytest.raises(SandboxUnavailableError, match = "does not exist"):
        backend.prepare(plan)


def test_prepare_refuses_the_filesystem_root_as_a_workdir(launchable):
    plan = ToolLaunchPlan(argv = ("/bin/echo",), workdir = "/", env = {})
    with pytest.raises(SandboxUnavailableError, match = "filesystem root"):
        backend.prepare(plan)


def test_prepare_refuses_when_the_launcher_is_missing(tmp_path):
    """No stub here: argv[0] must never become a sandbox-exec that is not there."""
    if os.path.exists(backend.SANDBOX_EXEC):
        pytest.skip("this host has a real /usr/bin/sandbox-exec, so the gate cannot be observed")
    workdir = tmp_path / "session"
    workdir.mkdir()
    plan = ToolLaunchPlan(argv = ("/bin/echo",), workdir = str(workdir), env = {})
    with pytest.raises(SandboxUnavailableError, match = "Seatbelt launcher"):
        backend.prepare(plan)


def test_a_rule_that_lost_every_filter_raises_instead_of_granting_everything():
    with pytest.raises(SandboxUnavailableError, match = "unconditionally"):
        backend._rule("allow file-write*", [])
    with pytest.raises(SandboxUnavailableError, match = "unconditionally"):
        backend._rule("deny process-exec", [])
    rendered = backend._rule("allow file-write*", ['(literal "/x")'])
    assert rendered == '(allow file-write* (literal "/x"))'
    with pytest.raises(SandboxUnavailableError, match = "unconditionally"):
        backend.build_profile(
            workdir = "/tmp/does-not-exist-workdir",
            private_tmp = "/tmp/does-not-exist-tmp",
            runtime_paths = (),
        )


def test_paths_must_be_absolute_and_free_of_newlines():
    with pytest.raises(SandboxUnavailableError):
        backend._validated("relative/path")
    with pytest.raises(SandboxUnavailableError):
        backend._validated("/tmp/with\nnewline")
    with pytest.raises(SandboxUnavailableError):
        backend._validated("/tmp/with\0nul")


def test_available_never_raises_on_a_host_without_the_launcher():
    ok, reason = backend.available()
    assert isinstance(ok, bool) and isinstance(reason, str) and reason
    if not os.path.exists(backend.SANDBOX_EXEC):
        assert ok is False
        assert backend.SANDBOX_EXEC in reason


_darwin_only = pytest.mark.skipif(
    sys.platform != "darwin" or not os.path.exists(backend.SANDBOX_EXEC),
    reason = "needs a real macOS host: only a Darwin kernel can compile or enforce an SBPL profile",
)


@_darwin_only
def test_profile_compiles_under_sandbox_exec(tmp_path):
    workdir = tmp_path / "session"
    workdir.mkdir()
    plan = ToolLaunchPlan(argv = ("/usr/bin/true",), workdir = str(workdir), env = {})
    prepared = backend.prepare(plan)
    try:
        result = subprocess.run(
            prepared.argv, capture_output = True, text = True, timeout = 60, check = False
        )
        assert result.returncode == 0, result.stderr
    finally:
        prepared.cleanup()


@_darwin_only
def test_a_child_spawns_promptly_under_an_unlimited_fd_limit(tmp_path):
    """Before /dev/fd was listable, this spawn spun forever at 100% CPU, unkillable by its parent's timeout."""
    import resource
    import signal

    _soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
    if hard != resource.RLIM_INFINITY:
        pytest.skip("needs an unlimited hard RLIMIT_NOFILE to raise the soft limit to")

    def unlimited():
        resource.setrlimit(resource.RLIMIT_NOFILE, (resource.RLIM_INFINITY, hard))

    workdir = tmp_path / "session"
    workdir.mkdir()
    spawn = (
        "import subprocess, time\n"
        "start = time.monotonic()\n"
        "subprocess.run(['/usr/bin/true'], check = True)\n"
        "print(time.monotonic() - start)\n"
    )
    prepared = backend.prepare(
        ToolLaunchPlan(
            argv = (sys.executable, "-I", "-S", "-c", spawn),
            workdir = str(workdir),
            env = {"PATH": "/usr/bin:/bin"},
            preexec_fn = unlimited,
        )
    )
    proc = subprocess.Popen(
        prepared.argv,
        env = prepared.env,
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE,
        text = True,
        preexec_fn = prepared.preexec_fn,
        start_new_session = True,
    )
    try:
        stdout, stderr = proc.communicate(timeout = 30)
    except subprocess.TimeoutExpired:
        pytest.fail("spawning a child inside the sandbox did not finish within 30s")
    finally:
        if proc.returncode is None:
            try:
                os.killpg(proc.pid, signal.SIGKILL)
            except OSError:
                pass
        proc.wait()
        prepared.cleanup()
    assert proc.returncode == 0, stderr
    assert float(stdout.strip()) < 5.0


@_darwin_only
def test_home_is_unreadable_inside_the_sandbox(tmp_path):
    """The file is created and proven readable on the host first: a read that fails for an unrelated reason proves nothing."""
    workdir = tmp_path / "session"
    workdir.mkdir()
    canary = Path(os.path.expanduser("~")) / ".unsloth-seatbelt-canary"
    canary.write_text("UNSLOTH_CANARY_HOME_READABLE", encoding = "utf-8")
    try:
        argv = ("/bin/sh", "-c", f"cat {shlex.quote(str(canary))}")
        host = subprocess.run(argv, capture_output = True, text = True, timeout = 60, check = False)
        assert host.returncode == 0 and "UNSLOTH_CANARY_HOME_READABLE" in host.stdout, (
            "the canary is not readable even outside the sandbox, so the negative "
            f"control below would prove nothing: {host.stderr}"
        )

        prepared = backend.prepare(
            ToolLaunchPlan(argv = argv, workdir = str(workdir), env = {"PATH": "/usr/bin:/bin"})
        )
        try:
            result = subprocess.run(
                prepared.argv, capture_output = True, text = True, timeout = 60, check = False
            )
            assert result.returncode != 0
            assert "UNSLOTH_CANARY_HOME_READABLE" not in result.stdout
        finally:
            prepared.cleanup()
    finally:
        canary.unlink(missing_ok = True)


def test_a_runtime_path_symlinked_out_of_the_workdir_is_not_readable(monkeypatch, tmp_path):
    workdir = tmp_path / "session"
    workdir.mkdir()
    secret = tmp_path / "secrets"
    secret.mkdir()
    (secret / "id_rsa").write_text("PRIVATE KEY")
    venv = workdir / "venv"
    (venv / "bin").mkdir(parents = True)
    (venv / "lib").symlink_to(secret)
    for name in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):
        monkeypatch.setattr(sys, name, str(venv))

    paths = backend.runtime_read_paths(str(workdir))
    assert str(secret) not in paths
    assert not any(backend._within(str(secret), path) for path in paths)
    assert str(secret) in backend.runtime_read_paths()


def test_pip_console_scripts_are_reachable_and_never_shadow_a_system_command():
    env = backend._sandbox_environment(
        {"PATH": "/usr/bin:/bin", "PYTHONPATH": "/shim"}, _WORKDIR, _PRIVATE_TMP
    )
    packages = f"{_WORKDIR}/{backend.SESSION_PACKAGES_RELPATH}"
    assert env["PATH"].split(os.pathsep) == ["/usr/bin", "/bin", f"{packages}/bin"]


def test_the_semaphore_namespace_is_named_rather_than_narrowed(profile):
    assert "(allow ipc-posix-sem)" in profile
    assert "posix_semaphore_namespace_shared" in backend.LIMITATIONS


def test_host_process_metadata_is_named_rather_than_withheld(profile):
    sysctl = _rule(profile, "(allow sysctl-read ")
    assert '(sysctl-name-prefix "kern.proc.pid.")' in sysctl
    assert "host_process_metadata_readable" in backend.LIMITATIONS
    assert "(allow process-info* (target same-sandbox))" in profile


def test_a_framework_build_gets_its_dyld_image(monkeypatch, tmp_path):
    prefix = tmp_path / "Python.framework" / "Versions" / "3.13"
    (prefix / "bin").mkdir(parents = True)
    (prefix / "lib").mkdir()
    (prefix / "Python").write_bytes(b"\xcf\xfa\xed\xfe")
    for name in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):
        monkeypatch.setattr(sys, name, str(prefix))
    paths = backend.runtime_read_paths()
    assert str(prefix / "Python") in paths
    assert str(prefix) not in paths


def test_a_runtime_under_the_workdir_is_denied_write_after_the_allowance(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))

    profile = backend.build_profile(workdir = str(workdir), private_tmp = "/tmp/pt", runtime_paths = ())
    lines = profile.splitlines()
    allow = next(i for i, line in enumerate(lines) if line.startswith("(allow file-write* "))
    deny = next(i for i, line in enumerate(lines) if line.startswith("(deny file-write* "))
    assert deny > allow, "a deny before the allowance is overridden by it"
    denied = _subpaths(lines[deny]) | _literals(lines[deny])
    assert str(venv / "lib") in denied
    assert str(venv / "bin") in denied


def test_no_write_denial_is_emitted_when_the_runtime_is_outside_the_workdir(tmp_path, monkeypatch):
    """The negative control: the rule above must not fire for an ordinary layout, where denying anything under the workdir would take away the one writable place."""
    workdir = tmp_path / "session"
    workdir.mkdir()
    monkeypatch.setattr(sys, "prefix", "/usr")
    monkeypatch.setattr(sys, "exec_prefix", "/usr")
    profile = backend.build_profile(workdir = str(workdir), private_tmp = "/tmp/pt", runtime_paths = ())
    assert not any(line.startswith("(deny file-write* ") for line in profile.splitlines())


def test_the_openssl_directory_is_granted_by_component_not_whole(profile):
    for spelling in ("/etc/ssl", "/private/etc/ssl"):
        for line in profile.splitlines():
            if line.startswith("(allow file-read-metadata"):
                continue
            assert f'(subpath "{spelling}")' not in line, line
            assert f'(literal "{spelling}")' not in line, line
    assert "/private/etc/ssl" not in backend._TLS_TRUST_PATHS
    for component in ("cert.pem", "certs", "openssl.cnf"):
        assert f"/private/etc/ssl/{component}" in backend._TLS_TRUST_PATHS


@pytest.mark.skipif(
    os.geteuid() == 0,
    reason = "root reads and writes regardless of the mode bits, so the premise is void",
)
def test_a_toolchain_directory_the_user_can_write_is_not_trusted(tmp_path):
    mine = tmp_path / "FakeXcode.app" / "Contents" / "Developer"
    mine.mkdir(parents = True)
    assert backend._trusted_system_dir(str(mine)) is False
    assert backend._trusted_system_dir(str(tmp_path / "absent")) is False
    missing_file = tmp_path / "Developer"
    missing_file.write_text("", encoding = "utf-8")
    assert backend._trusted_system_dir(str(missing_file)) is False
    assert backend._trusted_system_dir("/usr") is True


@pytest.mark.skipif(
    os.geteuid() == 0,
    reason = "root reads and writes regardless of the mode bits, so the premise is void",
)
def test_the_developer_dir_variable_never_reaches_xcode_select(monkeypatch, tmp_path):
    mine = tmp_path / "Developer"
    mine.mkdir()
    monkeypatch.setenv("DEVELOPER_DIR", str(mine))
    monkeypatch.setattr(backend.sys, "platform", "darwin")
    monkeypatch.setattr(backend.os.path, "exists", lambda path: True)
    seen: dict = {}

    def fake_run(argv, **kwargs):
        seen.update(kwargs)
        return subprocess.CompletedProcess(argv, 0, stdout = str(mine), stderr = "")

    monkeypatch.setattr(backend.subprocess, "run", fake_run)
    monkeypatch.setattr(backend, "_developer_paths_cache", None)
    try:
        assert backend._developer_paths() == ()
    finally:
        backend._developer_paths_cache = None
    assert "DEVELOPER_DIR" not in seen["env"]


def _profile_for(workdir, monkeypatch, prefix):
    monkeypatch.setattr(sys, "prefix", str(prefix))
    monkeypatch.setattr(sys, "exec_prefix", str(prefix))
    return backend.build_profile(workdir = str(workdir), private_tmp = _PRIVATE_TMP, runtime_paths = ())


def test_a_runtime_under_a_symlinked_workdir_is_denied_through_both_spellings(
    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)

    profile = _profile_for(alias, monkeypatch, venv)
    under = backend.runtime_paths_under(str(alias))
    for spelling in (real / "venv" / "lib", alias / "venv" / "lib"):
        assert str(spelling) in under, under
    deny = _rule(profile, "(deny file-write* ")
    for spelling in (real / "venv" / "lib", alias / "venv" / "lib"):
        assert f'(subpath "{spelling}")' in deny, deny
    lines = profile.splitlines()
    assert lines.index(_rule(profile, _WRITE_PREFIX)) < lines.index(deny)


def test_the_framework_python_image_is_denied_when_the_prefix_is_under_the_workdir(
    tmp_path, monkeypatch
):
    workdir = tmp_path / "session"
    prefix = workdir / "Python.framework" / "Versions" / "3.12"
    (prefix / "lib").mkdir(parents = True)
    image = prefix / "Python"
    image.write_bytes(b"\xcf\xfa\xed\xfe")

    deny = _rule(_profile_for(workdir, monkeypatch, prefix), "(deny file-write* ")
    assert str(image) in backend.runtime_paths_under(str(workdir))
    assert f'(literal "{image}")' in deny, deny


def test_an_optional_search_root_that_resolves_out_of_its_prefix_is_dropped(monkeypatch):
    home = os.path.expanduser("~")
    real = os.path.realpath

    def resolves_home(path):
        return home if path == "/usr/local/bin" else real(path)

    monkeypatch.setattr(os.path, "realpath", resolves_home)
    kept = backend._contained_optional_roots()
    assert "/usr/local/bin" not in kept
    assert "/opt/homebrew/bin" in kept and "/usr/local/lib" in kept
    named = {"/usr/local/bin", _WORKDIR, _PRIVATE_TMP}
    real_isdir, real_exists = os.path.isdir, os.path.exists
    monkeypatch.setattr(os.path, "isdir", lambda path: path in named or real_isdir(path))
    monkeypatch.setattr(os.path, "exists", lambda path: path in named or real_exists(path))
    profile = backend.build_profile(workdir = _WORKDIR, private_tmp = _PRIVATE_TMP, runtime_paths = ())
    assert f'(subpath "{home}")' not in profile


def test_an_editable_checkout_is_listable_but_not_readable(tmp_path, monkeypatch):
    checkout = tmp_path / "checkout"
    package = checkout / "demo"
    package.mkdir(parents = True)
    (package / "__init__.py").write_text("", encoding = "utf-8")
    (checkout / ".env").write_text("AWS_SECRET_ACCESS_KEY=real\n", encoding = "utf-8")
    monkeypatch.setattr(backend, "editable_source_roots", lambda: (str(package),))
    monkeypatch.setattr(backend, "editable_import_roots", lambda: (str(checkout),))

    named = {_WORKDIR, _PRIVATE_TMP}
    real_isdir, real_exists = os.path.isdir, os.path.exists
    monkeypatch.setattr(os.path, "isdir", lambda path: path in named or real_isdir(path))
    monkeypatch.setattr(os.path, "exists", lambda path: path in named or real_exists(path))
    profile = backend.build_profile(
        workdir = _WORKDIR,
        private_tmp = _PRIVATE_TMP,
        runtime_paths = (str(package),),
    )
    assert f'(literal "{checkout}")' in profile
    assert f'(subpath "{checkout}")' not in profile
    assert f'(subpath "{package}")' in profile


def test_a_standalone_interpreter_in_the_workdir_is_denied_writes(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))

    paths = backend.runtime_paths_under(str(workdir))
    assert str(executable) in paths
    assert all(os.path.samefile(path, executable) for path in paths)
    profile = backend.build_profile(
        workdir = str(workdir), private_tmp = str(tmp_path / "tmp"), runtime_paths = ()
    )
    deny = _rule(profile, "(deny file-write* ")
    assert f'(literal "{executable}")' in deny, deny


def test_a_workdir_this_backend_cannot_name_fails_the_call_rather_than_de_isolating_it(
    tmp_path, launchable
):
    with pytest.raises(WorkdirUnsafeError):
        backend.prepare(
            ToolLaunchPlan(argv = ("/usr/bin/true",), workdir = "/tmp/session-\udcff", env = {})
        )


def test_an_interpreter_in_a_home_directory_does_not_grant_the_home(tmp_path, monkeypatch):
    home = tmp_path / "alice"
    (home / ".ssh").mkdir(parents = True)
    executable = home / "python"
    executable.write_bytes(b"#!/bin/sh\nexit 0\n")
    executable.chmod(0o755)
    monkeypatch.setattr(sys, "executable", str(executable))
    paths = backend.runtime_read_paths(str(tmp_path / "session"))
    assert str(home) not in paths, paths
    assert str(executable) in paths, paths


def test_a_runtime_origin_is_excluded_under_either_workdir_spelling(tmp_path, monkeypatch):
    real = tmp_path / "real"
    real.mkdir()
    alias = tmp_path / "alias"
    alias.symlink_to(real)
    secret = tmp_path / "secret"
    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(real / "venv"))

    paths = backend.runtime_read_paths(str(alias))
    assert not any(os.path.realpath(p) == str(secret) for p in paths), paths


def test_an_interpreter_symlinked_out_of_the_workdir_fails_the_call(tmp_path, monkeypatch):
    """The Linux twin's guard, on this backend."""
    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"):
        backend.runtime_paths_under(str(workdir))


def test_a_path_with_a_control_byte_is_refused(tmp_path):
    for control in ("\x0c", "\x01", "\x7f"):
        with pytest.raises(SandboxUnavailableError, match = "control characters"):
            backend._validated(f"/tmp/session{control}x")
    assert backend._validated("/tmp/session-café") == "/tmp/session-café"


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


def test_a_runtime_is_denied_when_sys_prefix_carries_the_workdir_alias(tmp_path, monkeypatch):
    real = tmp_path / "real"
    (real / "venv" / "lib").mkdir(parents = True)
    alias = tmp_path / "alias"
    alias.symlink_to(real)
    profile = _profile_for(alias, monkeypatch, alias / "venv")
    deny = _rule(profile, "(deny file-write* ")
    for spelling in (real / "venv" / "lib", alias / "venv" / "lib"):
        assert f'(subpath "{spelling}")' in deny, deny


def test_a_path_that_cannot_be_encoded_is_refused_rather_than_carried():
    with pytest.raises(SandboxUnavailableError, match = "encodable as UTF-8"):
        backend._validated("/tmp/session-\udcff")
    assert backend._validated("/tmp/session-café") == "/tmp/session-café"
    assert "\\u00" not in backend._sbpl_string("/tmp/session-café")


def test_studio_state_under_an_optional_read_root_is_denied(monkeypatch, tmp_path):
    """A Homebrew-prefixed Studio home is inside a recursive read root."""
    from core.inference import sandbox_macos

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

    rules = sandbox_macos._studio_state_rules((), (), str(tmp_path / "work"), str(tmp_path / "tmp"))

    assert rules, "no rule was emitted for a Studio home inside a read root"
    assert rules[0].startswith("(deny file-read-data")
    assert str(state) in rules[0]


def test_a_studio_home_that_does_not_exist_yet_is_still_denied(monkeypatch, tmp_path):
    """Measured on macos-15/26: with no Studio home the probe raised 'no paths survived' and never isolated."""
    from core.inference import sandbox_macos

    state = tmp_path / "not-created-yet" / "studio"
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(state))

    rules = sandbox_macos._studio_state_rules((), (), str(tmp_path / "work"), str(tmp_path / "tmp"))

    assert rules and rules[0].startswith("(deny file-read-data")
    assert f'(subpath "{state}")' in rules[0]


def test_the_runtime_inside_a_custom_studio_home_is_restored(monkeypatch, tmp_path):
    """A blanket deny would break a custom-home install: the managed venv lives under the Studio root, so the interpreter itself would stop being readable."""
    from core.inference import sandbox_macos

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

    rules = sandbox_macos._studio_state_rules(
        (str(venv),), (), str(tmp_path / "work"), str(tmp_path / "tmp")
    )

    assert len(rules) == 2, "the runtime under the Studio home was not restored"
    assert rules[1].startswith("(allow file-read*")
    assert str(venv) in rules[1]


def test_the_studio_state_deny_keeps_path_traversal_working(monkeypatch, tmp_path):
    """The workdir lives under the Studio root on a default install."""
    from core.inference import sandbox_macos

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

    rules = sandbox_macos._studio_state_rules((), (), str(workdir), str(tmp_path / "tmp"))

    assert rules[0].startswith("(deny file-read-data"), rules[0][:60]
    assert "file-read-metadata" not in rules[0]
    assert "file-test-existence" not in rules[0]
    assert "file-read-data" in rules[0]
    assert str(state) in rules[0]


def test_the_workdir_under_the_studio_home_is_restored_for_the_operation_the_deny_names(
    monkeypatch, tmp_path
):
    """Measured on macos-15: a later (allow file-read* ...) loses to (deny file-read-data ...), so every Python call in the default workdir failed with Operation not permitted."""
    from core.inference import sandbox_macos

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

    deny, restore = sandbox_macos._studio_state_rules((), (), str(workdir), str(tmp_path / "tmp"))

    denied = deny[1:].split(" (", 1)[0].split()[1:]
    restored = restore[1:].split(" (", 1)[0].split()[1:]
    assert restore.startswith("(allow ")
    assert set(denied) <= set(restored), (denied, restored)
    assert str(workdir) in restore


def test_a_registered_model_folder_is_readable(monkeypatch, tmp_path):
    """The approval gate treats registered model folders as read-silent, so a read from one never prompts."""
    from core.inference import os_sandbox, sandbox_macos

    library = tmp_path / "models"
    library.mkdir()
    monkeypatch.setattr(sandbox_macos, "model_library_roots", lambda: (str(library),))
    monkeypatch.setattr(os_sandbox, "model_library_roots", lambda: (str(library),))

    workdir = tmp_path / "work"
    workdir.mkdir()
    private_tmp = tmp_path / "tmp"
    private_tmp.mkdir()

    profile = sandbox_macos.build_profile(
        workdir = str(workdir),
        private_tmp = str(private_tmp),
        runtime_paths = (),
    )

    assert str(library) in profile


@_darwin_only
def test_a_hard_link_to_a_readable_file_cannot_be_created_in_the_workdir(tmp_path, monkeypatch):
    """The workdir is writable and the model folders are readable, both on one volume."""
    workdir = tmp_path / "session"
    workdir.mkdir()
    library = tmp_path / "library"
    library.mkdir()
    external = library / "weights.bin"
    external.write_text("UNSLOTH_EXTERNAL_INODE", encoding = "utf-8")
    monkeypatch.setattr(backend, "model_library_roots", lambda: (str(library),))

    control = workdir / "host-alias"
    os.link(external, control)
    assert control.stat().st_ino == external.stat().st_ino, (
        "the host could not hard-link across these two paths, so the negative "
        "control below would prove nothing"
    )
    control.unlink()

    argv = ("/bin/ln", str(external), str(workdir / "alias"))
    prepared = backend.prepare(
        ToolLaunchPlan(argv = argv, workdir = str(workdir), env = {"PATH": "/usr/bin:/bin"})
    )
    try:
        result = subprocess.run(
            prepared.argv, capture_output = True, text = True, timeout = 60, check = False
        )
    finally:
        prepared.cleanup()

    assert result.returncode != 0, (
        "a hard link to a readable file outside the workdir was created inside it, "
        "so a tool can write to that file through the alias"
    )
    assert not (workdir / "alias").exists()


def test_a_workdir_spelled_differently_from_the_studio_home_is_still_readable(
    monkeypatch, tmp_path
):
    """The deny rules are emitted in every spelling a path has, so the restore list has to be built the same way."""
    real_home = tmp_path / "private" / "studio-home"
    workdir = real_home / "sandbox" / "_default"
    workdir.mkdir(parents = True)
    (tmp_path / "alias").symlink_to(tmp_path / "private")
    aliased_home = tmp_path / "alias" / "studio-home"

    monkeypatch.setattr(backend, "studio_state_roots", lambda: (str(aliased_home),))

    rules = backend._studio_state_rules((), (), str(workdir.resolve()), "")

    assert rules, "the state deny was not emitted, so this test proves nothing"
    restored = [rule for rule in rules if rule.startswith("(allow")]
    assert restored, "the workdir under the Studio home was never restored"
    assert any(str(workdir.resolve()) in rule for rule in restored)