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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Loads the sitecustomize path-remap shim into a throwaway module and restores patched globals after."""
from __future__ import annotations
import builtins
import importlib.util
import io
import os
import pathlib
from pathlib import Path
import pytest
_SHIM = (
Path(__file__).resolve().parent.parent
/ "core"
/ "inference"
/ "sandbox_site"
/ "sitecustomize.py"
)
def _save_patch_targets():
"""Snapshot the globals the shim patches; the pathlib accessor is included only before Python 3.11."""
accessor = getattr(pathlib, "_NormalAccessor", None)
return (
(builtins.open, io.open, os.open, os.makedirs, os.mkdir, pathlib.Path.mkdir),
accessor,
accessor.open if accessor is not None else None,
)
def _restore_patch_targets(saved):
"""Undo _save_patch_targets so the test process stays clean."""
globals_tuple, accessor, accessor_open = saved
(builtins.open, io.open, os.open, os.makedirs, os.mkdir, pathlib.Path.mkdir) = globals_tuple
if accessor is not None:
accessor.open = accessor_open
def _load_shim():
"""Import the shim without leaving its open()/mkdir patches installed."""
saved = _save_patch_targets()
spec = importlib.util.spec_from_file_location("_sandbox_sitecustomize_under_test", _SHIM)
mod = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(mod)
finally:
_restore_patch_targets(saved)
mod._notified = True
return mod
def test_always_remap_prefixes_map_into_cwd(monkeypatch, tmp_path):
mod = _load_shim()
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
assert mod._remap("/mnt/data/out.txt") == os.path.join(cwd, "out.txt")
assert mod._remap("/mnt/data") == cwd
assert mod._remap("/etc/passwd") == "/etc/passwd"
assert mod._remap("relative.txt") == "relative.txt"
def test_windows_backslash_spelling_of_a_prefix_maps_into_cwd(monkeypatch):
"""On Windows str(Path("/mnt/data/x")) is "\\mnt\\data\\x"; the forward-slash prefix match alone missed it, so a
write fell back to the basename heal and the matching read failed (staging Windows leg, #16210)."""
import ntpath
import types
mod = _load_shim()
cwd = "C:\\sandbox\\work"
fake_os = types.SimpleNamespace(
**{name: getattr(os, name) for name in dir(os) if not name.startswith("__")}
)
fake_os.sep = "\\"
fake_os.path = types.SimpleNamespace(
**{name: getattr(ntpath, name) for name in dir(ntpath) if not name.startswith("__")}
)
fake_os.path.exists = lambda p: False
fake_os.getcwd = lambda: cwd
monkeypatch.setattr(mod, "os", fake_os)
assert mod._remap("\\mnt\\data\\out.txt") == ntpath.join(cwd, "out.txt")
assert mod._remap("/mnt/data/sub\\out.txt") == ntpath.join(cwd, "sub", "out.txt")
assert mod._remap("\\mnt\\data") == cwd
assert mod._remap("\\mnt\\data\\..\\..\\escape.txt") == ntpath.join(cwd, "escape.txt")
# A drive-qualified path is a real host location, never a code-interpreter convention.
assert mod._remap("D:\\mnt\\data\\out.txt") == "D:\\mnt\\data\\out.txt"
assert mod._remap("\\\\server\\share\\mnt\\data\\x") == "\\\\server\\share\\mnt\\data\\x"
assert mod._remap("data\\out.txt") == "data\\out.txt"
@pytest.mark.skipif(
os.path.exists("/mnt/data"),
reason = "the host has a real /mnt/data, which the shim never shadows",
)
def test_a_pathlib_write_then_read_of_mnt_data_round_trips_in_a_child(tmp_path):
"""End to end in a real interpreter with the shim on PYTHONPATH, on whatever OS runs the suite. Windows spells the
path with backslashes, which the prefix remap must still catch on the read as well as the write."""
import subprocess
import sys
code = (
"from pathlib import Path\n"
"p = Path('/mnt/data/round_trip.txt')\n"
"p.write_text('round trip')\n"
"print(p.read_text())\n"
)
env = {**os.environ, "PYTHONPATH": str(_SHIM.parent)}
proc = subprocess.run(
[sys.executable, "-c", code],
cwd = tmp_path,
env = env,
capture_output = True,
text = True,
timeout = 60,
)
assert proc.returncode == 0, proc.stderr
assert proc.stdout.strip() == "round trip"
assert (tmp_path / "round_trip.txt").read_text(encoding = "utf-8") == "round trip"
def test_prefix_remap_contains_parent_traversal_inside_cwd(monkeypatch, tmp_path):
# '..' components are dropped so the remap never escapes the per-conversation CWD.
mod = _load_shim()
workdir = tmp_path / "session_current" / "work"
workdir.mkdir(parents = True)
monkeypatch.chdir(workdir)
cwd = os.getcwd()
for escaping in (
"/mnt/data/../other_session/file",
"/mnt/data/../../secrets.txt",
"/mnt/data/a/../../b/c.txt",
"/mnt/data/./sub/./x.txt",
):
mapped = mod._remap(escaping)
assert mapped == cwd or mapped.startswith(cwd + os.sep), (escaping, mapped)
assert os.path.realpath(mapped).startswith(os.path.realpath(cwd))
assert mod._remap("/mnt/data/../other_session/file") == os.path.join(
cwd, "other_session", "file"
)
assert mod._remap("/mnt/data/..") == cwd
def test_write_fallback_refuses_dotdot_basename(monkeypatch, tmp_path):
# basename '..' would target the CWD's parent, so the fallback must refuse it.
mod = _load_shim()
workdir = tmp_path / "work"
workdir.mkdir()
monkeypatch.chdir(workdir)
for escaping in ("/no/such/tree/..", "/no/such/tree/.", "/no/such/tree/"):
assert mod._remap_open(escaping, "w") == escaping
def test_write_fallback_remaps_hallucinated_absolute_path(monkeypatch, tmp_path):
# Write-mode opens on an absolute path with a missing parent go to the CWD basename.
mod = _load_shim()
workdir = tmp_path / "workdir"
workdir.mkdir()
monkeypatch.chdir(workdir)
cwd = os.getcwd()
hallucinated = "/home/ubuntu/Sandbox/flappy_bird.html"
for mode in ("w", "a", "x", "w+"):
assert mod._remap_open(hallucinated, mode) == os.path.join(cwd, "flappy_bird.html")
assert mod._remap_open("/no/such/tree/report.txt", "w") == os.path.join(cwd, "report.txt")
def test_write_fallback_never_touches_read_modes(monkeypatch, tmp_path):
mod = _load_shim()
monkeypatch.chdir(tmp_path)
for mode in ("r", "rb", "r+"):
assert mod._remap_open("/etc/definitely_missing_xyz.conf", mode) == (
"/etc/definitely_missing_xyz.conf"
)
def test_write_fallback_passes_through_existing_external_dir(monkeypatch, tmp_path):
mod = _load_shim()
external = tmp_path / "external"
external.mkdir()
workdir = tmp_path / "workdir"
workdir.mkdir()
monkeypatch.chdir(workdir)
target = str(external / "out.txt")
assert mod._remap_open(target, "w") is target
def test_write_fallback_never_clobbers_same_basename(monkeypatch, tmp_path):
# A same-named CWD file is unrelated data: refuse on collision so the real open() raises.
mod = _load_shim()
monkeypatch.chdir(tmp_path)
existing = tmp_path / "report.txt"
existing.write_text("KEEP-ME")
requested = "/definitely_missing_parent_7083/report.txt"
for mode in ("w", "a", "x", "w+", "a+"):
assert mod._remap_open(requested, mode) == requested
with pytest.raises(FileNotFoundError):
open(mod._remap_open(requested, "w"), "w")
assert existing.read_text() == "KEEP-ME"
fresh = "/definitely_missing_parent_7083/brand_new.txt"
assert mod._remap_open(fresh, "w") == os.path.join(os.getcwd(), "brand_new.txt")
def test_write_fallback_reserves_same_target_on_repeated_writes(monkeypatch, tmp_path):
# The fallback must recognise its own prior remap, or every regenerate after the first raises.
mod = _load_shim()
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
invented = "/home/ubuntu/Sandbox/app.html"
target = os.path.join(cwd, "app.html")
assert mod._remap_open(invented, "w") == target
with open(mod._remap_open(invented, "w"), "w") as fh:
fh.write("v1")
for _ in range(3):
assert mod._remap_open(invented, "w") == target
with open(mod._remap_open(invented, "w"), "w") as fh:
fh.write("v2")
assert Path(target).read_text() == "v2"
other = "/opt/other/app.html"
assert mod._remap_open(other, "w") == other
def test_write_fallback_reserves_healed_target_across_separate_runs(monkeypatch, tmp_path):
# Each tool call is a fresh subprocess; the on-disk sidecar carries the remap across runs.
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
invented = "/home/ubuntu/Sandbox/app.html"
target = os.path.join(cwd, "app.html")
run1 = _load_shim()
assert run1._remap_open(invented, "w") == target
with open(run1._remap_open(invented, "w"), "w") as fh:
fh.write("v1")
run2 = _load_shim()
assert run2._remapped_writes == {}
assert run2._remap_open(invented, "w") == target
with open(run2._remap_open(invented, "w"), "w") as fh:
fh.write("v2")
assert Path(target).read_text() == "v2"
other = "/opt/other/app.html"
assert run2._remap_open(other, "w") == other
(tmp_path / "notes.txt").write_text("KEEP-ME")
run3 = _load_shim()
assert run3._remap_open("/some/missing/notes.txt", "w") == "/some/missing/notes.txt"
with pytest.raises(FileNotFoundError):
open(run3._remap_open("/some/missing/notes.txt", "w"), "w")
assert (tmp_path / "notes.txt").read_text() == "KEEP-ME"
@pytest.mark.parametrize("mode", ["r+", "rb+"])
def test_read_update_modes_never_redirected_even_with_missing_parent(monkeypatch, tmp_path, mode):
# r+ / rb+ never create, so "+" must not count as a create mode.
mod = _load_shim()
monkeypatch.chdir(tmp_path)
victim = tmp_path / "victim.txt"
victim.write_text("original")
requested = "/definitely_missing_parent_xyz/victim.txt"
assert mod._remap_open(requested, mode) == requested
with pytest.raises(FileNotFoundError):
open(mod._remap_open(requested, mode), mode)
assert victim.read_text() == "original"
def test_existing_convention_prefix_is_not_shadowed(monkeypatch, tmp_path):
# A convention prefix is remapped only while absent; a real host dir passes through.
mod = _load_shim()
external = tmp_path / "real_prefix"
external.mkdir()
(external / "data.txt").write_text("real external content")
workdir = tmp_path / "conversation"
workdir.mkdir()
monkeypatch.chdir(workdir)
monkeypatch.setattr(mod, "_PREFIXES", (str(external),))
monkeypatch.setattr(mod, "_CONDITIONAL_PREFIXES", ())
target = str(external / "data.txt")
assert mod._remap(target) == target
assert mod._remap_open(target, "r") == target
assert mod._remap_open(target, "w") == target
missing = str(external / "new.txt")
assert mod._remap_open(missing, "w") == missing
(external / "data.txt").unlink()
external.rmdir()
assert mod._remap(target) == os.path.join(os.getcwd(), "data.txt")
def test_os_open_and_path_touch_remap_convention_path(monkeypatch, tmp_path):
# Path.touch() goes through os.open, not builtins/io.open.
saved = _save_patch_targets()
spec = importlib.util.spec_from_file_location("_sandbox_sitecustomize_osopen", _SHIM)
mod = importlib.util.module_from_spec(spec)
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
try:
spec.loader.exec_module(mod)
mod._notified = True
pathlib.Path("/mnt/data/touched.txt").touch()
assert os.path.isfile(os.path.join(cwd, "touched.txt"))
fd = os.open("/mnt/data/via_os_open.txt", os.O_CREAT | os.O_WRONLY, 0o600)
os.close(fd)
assert os.path.isfile(os.path.join(cwd, "via_os_open.txt"))
finally:
_restore_patch_targets(saved)
def test_path_write_read_text_remap_convention_path(monkeypatch, tmp_path):
# Path.open uses io.open on 3.11+ and the captured accessor open below 3.11.
saved = _save_patch_targets()
spec = importlib.util.spec_from_file_location("_sandbox_sitecustomize_writetext", _SHIM)
mod = importlib.util.module_from_spec(spec)
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
try:
spec.loader.exec_module(mod)
mod._notified = True
pathlib.Path("/mnt/data/note.txt").write_text("pathlib remap")
assert os.path.isfile(os.path.join(cwd, "note.txt"))
assert pathlib.Path("/mnt/data/note.txt").read_text() == "pathlib remap"
real = tmp_path / "real.txt"
pathlib.Path(str(real)).write_text("verbatim")
assert real.read_text() == "verbatim"
finally:
_restore_patch_targets(saved)
def test_write_fallback_leaves_relative_and_bytes_paths(monkeypatch, tmp_path):
mod = _load_shim()
monkeypatch.chdir(tmp_path)
assert mod._remap_open("out.txt", "w") == "out.txt"
assert mod._remap_open(b"/no/such/tree/x.bin", "w") == b"/no/such/tree/x.bin"
def test_remap_open_still_applies_prefix_remaps(monkeypatch, tmp_path):
mod = _load_shim()
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
assert mod._remap_open("/mnt/data/sub/out.txt", "w") == os.path.join(cwd, "sub", "out.txt")
assert mod._remap_open("/mnt/data/sub/out.txt", "r") == "/mnt/data/sub/out.txt"
def test_prefix_read_heals_only_when_mapped_target_exists(monkeypatch, tmp_path):
mod = _load_shim()
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
assert mod._remap_open("/mnt/data/input.csv", "r") == "/mnt/data/input.csv"
with pytest.raises(FileNotFoundError):
open(mod._remap_open("/mnt/data/input.csv", "r"))
assert mod._remap_open("/mnt/data/input.csv", "r+") == "/mnt/data/input.csv"
mapped = mod._remap_open("/mnt/data/input.csv", "w")
assert mapped == os.path.join(cwd, "input.csv")
with open(mapped, "w") as fh:
fh.write("col\n1\n")
read_target = mod._remap_open("/mnt/data/input.csv", "r")
assert read_target == os.path.join(cwd, "input.csv")
with open(read_target) as fh:
assert fh.read() == "col\n1\n"
def test_prefix_boundary_not_matched_by_similar_paths(monkeypatch, tmp_path):
# The prefix match is segment-anchored: /mnt/database is not /mnt/data.
mod = _load_shim()
monkeypatch.chdir(tmp_path)
for unrelated in ("/workspace2/file.txt", "/mnt/database/x", "/home/sandboxed/y"):
assert mod._remap(unrelated) == unrelated
assert mod._remap_open(unrelated, "r") == unrelated
def test_tmp_outputs_is_a_conditional_prefix():
mod = _load_shim()
assert "/tmp/outputs" in mod._CONDITIONAL_PREFIXES
# NOT in the always-remap set: /tmp exists on the host, so an unconditional remap
# could shadow a real /tmp/outputs the user code made.
assert "/tmp/outputs" not in mod._PREFIXES
def test_tmp_outputs_remapped_only_while_absent(monkeypatch, tmp_path):
mod = _load_shim()
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
cond = str(tmp_path / "cond_outputs")
monkeypatch.setattr(mod, "_CONDITIONAL_PREFIXES", (cond,))
assert not os.path.exists(cond)
assert mod._remap(cond + "/plot.png") == os.path.join(cwd, "plot.png")
assert mod._remap(cond) == cwd
os.makedirs(cond)
assert mod._remap(cond + "/plot.png") == cond + "/plot.png"
assert mod._remap(cond) == cond
def test_pathlib_mkdir_parents_remaps_convention_path(monkeypatch, tmp_path):
# Path.mkdir(parents, exist_ok) goes through os.mkdir and stat, so both are patched.
saved = _save_patch_targets()
spec = importlib.util.spec_from_file_location("_sandbox_sitecustomize_mkdir", _SHIM)
mod = importlib.util.module_from_spec(spec)
monkeypatch.chdir(tmp_path)
cwd = os.getcwd()
try:
spec.loader.exec_module(mod)
mod._notified = True
pathlib.Path("/mnt/data").mkdir(parents = True, exist_ok = True)
pathlib.Path("/mnt/data/plots/run1").mkdir(parents = True, exist_ok = True)
assert os.path.isdir(os.path.join(cwd, "plots", "run1"))
pathlib.Path("/mnt/data/plots/run1").mkdir(parents = True, exist_ok = True)
real_dir = tmp_path / "real_via_path"
pathlib.Path(str(real_dir)).mkdir()
assert real_dir.is_dir()
real_os = tmp_path / "real_via_os"
os.mkdir(str(real_os))
assert real_os.is_dir()
finally:
_restore_patch_targets(saved)
def test_read_of_missing_prefix_path_emits_no_notice(monkeypatch, tmp_path, capsys):
mod = _load_shim()
monkeypatch.chdir(tmp_path)
mod._notified = False
assert mod._remap_open("/mnt/data/missing.csv", "r") == "/mnt/data/missing.csv"
assert mod._notified is False
assert "does not exist" not in capsys.readouterr().err
assert mod._remap_open("/mnt/data/out.txt", "w") == os.path.join(os.getcwd(), "out.txt")
assert mod._notified is True
assert "/mnt/data does not exist in this sandbox" in capsys.readouterr().err
def test_os_open_trunc_without_creat_missing_stays_truthful(monkeypatch, tmp_path):
# O_TRUNC / O_APPEND without O_CREAT cannot create, so they are treated as reads.
saved = _save_patch_targets()
spec = importlib.util.spec_from_file_location("_sandbox_sitecustomize_trunc", _SHIM)
mod = importlib.util.module_from_spec(spec)
monkeypatch.chdir(tmp_path)
try:
spec.loader.exec_module(mod)
mod._notified = True
with pytest.raises(FileNotFoundError) as exc:
os.open("/mnt/data/missing_xyz.bin", os.O_WRONLY | os.O_TRUNC)
assert exc.value.filename == "/mnt/data/missing_xyz.bin"
assert not os.path.exists(os.path.join(os.getcwd(), "missing_xyz.bin"))
finally:
_restore_patch_targets(saved)
@pytest.mark.parametrize(
"init, namespace",
[
("__path__ = __import__('pkgutil').extend_path(__path__, __name__)\n", True),
("from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n", True),
("__import__('pkg_resources').declare_namespace(__name__)\n", True),
(
"try:\n __import__('pkg_resources').declare_namespace(__name__)\n"
"except ImportError:\n import pkgutil\n"
" __path__ = pkgutil.extend_path(__path__, __name__)\n",
True,
),
("# extend_path and declare_namespace are not used here\n", False),
("from pkgutil import extend_path\n", False),
("DOC = '__path__ = extend_path(__path__, __name__)'\n", False),
("if False:\n __import__('pkg_resources').declare_namespace(__name__)\n", False),
("def setup():\n __path__ = extend_path(__path__, __name__)\n", False),
("paths = extend_path(['x'], 'other')\n", False),
("this is not python(\n", False),
(
"try:\n import _no_such_module\n"
"except ImportError:\n __path__ = __import__('pkgutil').extend_path(__path__, __name__)\n",
False,
),
(
"try:\n import os\n"
"except ImportError:\n pass\n"
"else:\n __path__ = __import__('pkgutil').extend_path(__path__, __name__)\n",
True,
),
],
)
def test_host_packages_finder_only_treats_declared_namespaces_as_open(tmp_path, init, namespace):
"""Only a real extend_path / declare_namespace declaration opens a regular-looking package to
the chat folder's portion; text that merely mentions them does not."""
finder = _load_shim()._HostPackagesFinder
origin = tmp_path / "__init__.py"
origin.write_text(init, encoding = "utf-8")
assert finder._extends_path(str(origin)) is namespace
def _run_with_host_packages(tmp_path, folder, code):
"""Run ``code`` in a fresh interpreter with the shim installed for ``folder``."""
import subprocess
import sys
script = f"import runpy; runpy.run_path({str(_SHIM)!r})\n" + code
env = {**os.environ, "_UNSLOTH_HOST_SESSION_PACKAGES": str(folder)}
env.pop("PYTHONPATH", None)
done = subprocess.run(
[sys.executable, "-I", "-c", script],
cwd = tmp_path,
env = env,
capture_output = True,
text = True,
timeout = 60,
)
return done.stdout + done.stderr
def test_host_packages_metadata_honours_an_explicit_search_path(tmp_path):
"""An exact-name lookup scoped to explicit paths never answers from the chat folder."""
folder = tmp_path / "packages"
trusted = tmp_path / "trusted"
trusted.mkdir()
info = folder / "chatmeta-1.0.dist-info"
info.mkdir(parents = True)
(info / "METADATA").write_text(
"Metadata-Version: 2.1\nName: chatmeta\nVersion: 1.0\n", encoding = "utf-8"
)
out = _run_with_host_packages(
tmp_path,
folder,
"import importlib.metadata as m\n"
"print('DEFAULT', m.version('chatmeta'))\n"
f"print('TRUSTED', len(list(m.distributions(name = 'chatmeta', path = [{str(trusted)!r}]))))\n"
f"print('LISTED', len(list(m.distributions(name = 'chatmeta', path = [{str(folder)!r}]))))\n",
)
assert "DEFAULT 1.0" in out, out
assert "TRUSTED 0" in out, out
assert "LISTED 1" in out, out
def test_host_packages_pep420_portion_joins_the_namespace_path(tmp_path):
"""A submodule served from the chat folder's PEP 420 portion sees that portion through the
namespace's __path__ (importlib.resources, pkgutil.iter_modules); the host portion stays first."""
folder = tmp_path / "packages"
host = tmp_path / "host"
(host / "chatns").mkdir(parents = True)
(host / "chatns" / "host_mod.py").write_text("V = 'host'\n", encoding = "utf-8")
(folder / "chatns").mkdir(parents = True)
(folder / "chatns" / "data.txt").write_text("chat-data", encoding = "utf-8")
(folder / "chatns" / "chat_mod.py").write_text(
"import importlib.resources\n"
"V = importlib.resources.files('chatns').joinpath('data.txt').read_text()\n",
encoding = "utf-8",
)
out = _run_with_host_packages(
tmp_path,
folder,
f"import sys; sys.path.insert(0, {str(host)!r})\n"
"import os, pkgutil, chatns.host_mod, chatns.chat_mod\n"
"print('NS', chatns.chat_mod.V,"
" [os.path.basename(os.path.dirname(p)) for p in chatns.__path__],"
" sorted(i.name for i in pkgutil.iter_modules(chatns.__path__)))\n",
)
assert "NS chat-data ['host', 'packages'] ['chat_mod', 'host_mod']" in out, out
def test_a_raw_connect_to_a_name_resolving_to_a_metadata_host_is_refused(monkeypatch):
import socket
mod = _load_shim()
answer = {"alias.test": "169.254.169.254", "plain.test": "93.184.215.14"}
monkeypatch.setattr(
socket,
"getaddrinfo",
lambda host, *a, **k: [(socket.AF_INET, 1, 6, "", (answer[host], 0))],
)
with pytest.raises(PermissionError, match = "cloud-metadata host"):
mod._metadata_connect_hook("socket.connect", (None, ("alias.test", 80)))
assert mod._metadata_connect_hook("socket.connect", (None, ("plain.test", 80))) is None
assert mod._metadata_connect_hook("socket.connect", (None, ("127.0.0.1", 80))) is None