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.

Studio: keep commands and modules from a second sandboxed pip install (#16018)

Nilayf28d1c802d37
diff --git a/studio/backend/core/inference/mxc_policy.py b/studio/backend/core/inference/mxc_policy.py
index feda2fbadd1..0b8f1ebefb3 100644
--- a/studio/backend/core/inference/mxc_policy.py
+++ b/studio/backend/core/inference/mxc_policy.py
@@ -409,11 +409,12 @@ def _with_session_packages(env: dict[str, str], workdir: str) -> dict[str, str]:
# Windows environment names are case-insensitive, and the inherited PATH is often spelled Path.
return next((k for k in env if k.upper() == name), name)
- env[key("PIP_TARGET")] = packages
+ env[key("PIP_PREFIX")] = packages
+ env[key("PIP_IGNORE_INSTALLED")] = "1"
env[key("PYTHONNOUSERSITE")] = "1"
# Appended, so an installed package or script cannot shadow the sandbox shim or a trusted command.
for name, value in (
- ("PYTHONPATH", packages),
+ ("PYTHONPATH", os.pathsep.join(os_sandbox.session_package_paths(packages))),
("PATH", os.path.join(packages, "Scripts")),
("PATH", os.path.join(packages, "bin")),
):
diff --git a/studio/backend/core/inference/os_sandbox.py b/studio/backend/core/inference/os_sandbox.py
index 77427120985..51da9dc1d18 100644
--- a/studio/backend/core/inference/os_sandbox.py
+++ b/studio/backend/core/inference/os_sandbox.py
@@ -15,6 +15,7 @@ import shutil
import stat
import subprocess
import sys
+import sysconfig
import threading
import time
from dataclasses import dataclass, field
@@ -35,6 +36,40 @@ PROFILE_VERSION = "unsloth-sandbox-v1"
SESSION_PACKAGES_RELPATH = ".unsloth-packages"
+def _pip_prefix_dirs() -> "tuple[tuple[str, ...], str]":
+ if sys.version_info >= (3, 10):
+ scheme = sysconfig.get_preferred_scheme("prefix")
+ # pip's own remap: the framework scheme's purelib is absolute, never under a --prefix.
+ if scheme == "osx_framework_library":
+ scheme = "posix_prefix"
+ else:
+ scheme = "nt" if os.name == "nt" else "posix_prefix"
+ # A bare separator base turns nt's "{base}/Lib" into a UNC path that relpath rejects.
+ base = os.path.join(os.path.abspath(os.sep), "prefix")
+ paths = sysconfig.get_paths(scheme, vars = {"base": base, "platbase": base})
+ # Only these: nt's stdlib/include sit on the install drive, where relpath raises.
+ purelib, platlib, scripts = (
+ os.path.relpath(paths[key], base) for key in ("purelib", "platlib", "scripts")
+ )
+ return tuple(dict.fromkeys((purelib, platlib))), scripts
+
+
+_PIP_PREFIX_LIB_DIRS, _PIP_PREFIX_SCRIPTS_DIR = _pip_prefix_dirs()
+
+
+def session_package_paths(packages: str) -> list[str]:
+ # The bare folder keeps packages from chats that installed with pip --target importable.
+ return [*(os.path.join(packages, path) for path in _PIP_PREFIX_LIB_DIRS), packages]
+
+
+def session_script_dirs(packages: str) -> list[str]:
+ # Debian's posix_local scheme puts scripts in local/bin; the bare folder serves pip --target chats.
+ legacy = "Scripts" if os.name == "nt" else "bin"
+ return list(
+ dict.fromkeys(os.path.join(packages, path) for path in (_PIP_PREFIX_SCRIPTS_DIR, legacy))
+ )
+
+
def with_session_packages(env: dict, workdir: str) -> dict:
"""Reuse existing session packages without letting their binaries shadow PATH."""
packages = os.path.join(workdir, SESSION_PACKAGES_RELPATH)
@@ -43,11 +78,9 @@ def with_session_packages(env: dict, workdir: str) -> dict:
updated = dict(env)
# Block a planted usercustomize.py; in safe mode the trusted sitecustomize shim stays first on PYTHONPATH.
updated["PYTHONNOUSERSITE"] = "1"
- # pip --target puts console scripts in Scripts on Windows and bin elsewhere.
- scripts = "Scripts" if os.name == "nt" else "bin"
for key, value in (
- ("PYTHONPATH", packages),
- ("PATH", os.path.join(packages, scripts)),
+ ("PYTHONPATH", os.pathsep.join(session_package_paths(packages))),
+ ("PATH", os.pathsep.join(session_script_dirs(packages))),
):
updated[key] = os.pathsep.join(part for part in (updated.get(key, ""), value) if part)
return updated
diff --git a/studio/backend/core/inference/sandbox_linux.py b/studio/backend/core/inference/sandbox_linux.py
index d772128fe3c..09b71911705 100644
--- a/studio/backend/core/inference/sandbox_linux.py
+++ b/studio/backend/core/inference/sandbox_linux.py
@@ -36,6 +36,8 @@ from .os_sandbox import (
editable_source_roots,
scan_workdir_for_host_channels,
model_library_roots,
+ session_package_paths,
+ session_script_dirs,
studio_state_roots,
)
@@ -707,13 +709,13 @@ def _tmpdir(plan: ToolLaunchPlan, workdir: str) -> str:
def _pythonpath(plan: ToolLaunchPlan, packages: str) -> str:
"""Appended, never prepended: a package must not shadow tools.py's sandbox_site shim."""
inherited = plan.env.get("PYTHONPATH") or ""
- return os.pathsep.join(part for part in (inherited, packages) if part)
+ return os.pathsep.join(part for part in (inherited, *session_package_paths(packages)) if part)
def _path(plan: ToolLaunchPlan, packages: str) -> str:
"""Caller's PATH plus the writable package ``bin`` LAST, so a planted binary cannot shadow a safe command."""
inherited = plan.env.get("PATH") or ""
- return os.pathsep.join(part for part in (inherited, os.path.join(packages, "bin")) if part)
+ return os.pathsep.join(part for part in (inherited, *session_script_dirs(packages)) if part)
# Bounded wait on a thread: NFS/FUSE can block isdir/scandir; an uninspected cache is not shared.
@@ -1052,9 +1054,12 @@ def prepare(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:
"TMPDIR",
_tmpdir(plan, workdir),
"--setenv",
- "PIP_TARGET",
+ "PIP_PREFIX",
packages,
"--setenv",
+ "PIP_IGNORE_INSTALLED",
+ "1",
+ "--setenv",
"PYTHONPATH",
_pythonpath(plan, packages),
"--setenv",
diff --git a/studio/backend/core/inference/sandbox_macos.py b/studio/backend/core/inference/sandbox_macos.py
index a1637aedf75..2e3bcec3ad9 100644
--- a/studio/backend/core/inference/sandbox_macos.py
+++ b/studio/backend/core/inference/sandbox_macos.py
@@ -27,6 +27,8 @@ from .os_sandbox import (
editable_source_roots,
model_library_roots,
scan_workdir_for_host_channels,
+ session_package_paths,
+ session_script_dirs,
studio_state_roots,
)
@@ -666,13 +668,16 @@ def _sandbox_environment(env: dict[str, str], workdir: str, private_tmp: str) ->
"TMP": tmpdir,
"TEMP": tmpdir,
"XDG_RUNTIME_DIR": private_tmp,
- "PIP_TARGET": packages,
+ "PIP_PREFIX": packages,
+ "PIP_IGNORE_INSTALLED": "1",
# <target>/bin LAST: it is tool-writable and must not shadow a command approval treats as safe.
"PATH": os.pathsep.join(
- part for part in (env.get("PATH") or "", posixpath.join(packages, "bin")) if part
+ part for part in (env.get("PATH") or "", *session_script_dirs(packages)) if part
),
"PYTHONPATH": os.pathsep.join(
- part for part in (env.get("PYTHONPATH") or "", packages) if part
+ part
+ for part in (env.get("PYTHONPATH") or "", *session_package_paths(packages))
+ if part
),
}
)
diff --git a/studio/backend/core/inference/sandbox_site/sitecustomize.py b/studio/backend/core/inference/sandbox_site/sitecustomize.py
index a5966a0b1b0..54e29155f71 100644
--- a/studio/backend/core/inference/sandbox_site/sitecustomize.py
+++ b/studio/backend/core/inference/sandbox_site/sitecustomize.py
@@ -239,8 +239,8 @@ except Exception: # noqa: BLE001 - a broken shim must never break user code
class _HostPackagesFinder:
_skip = frozenset(("sitecustomize", "usercustomize"))
- def __init__(self, folder):
- self._folder = folder
+ def __init__(self, folders):
+ self._folders = [folder for folder in folders.split(os.pathsep) if folder]
def find_spec(
self,
@@ -253,7 +253,7 @@ class _HostPackagesFinder:
if "." not in name:
if path is not None or name in self._skip:
return None
- return importlib.machinery.PathFinder.find_spec(name, [self._folder])
+ return importlib.machinery.PathFinder.find_spec(name, self._folders)
parent, _, _ = name.rpartition(".")
module = sys.modules.get(parent)
spec = getattr(module, "__spec__", None)
@@ -263,15 +263,18 @@ class _HostPackagesFinder:
pkgutil_style = spec.origin not in (None, "namespace")
if pkgutil_style and not self._extends_path(spec.origin):
return None
- portion = os.path.join(self._folder, *parent.split("."))
- if not os.path.isdir(portion):
+ portions = [os.path.join(folder, *parent.split(".")) for folder in self._folders]
+ portions = [portion for portion in portions if os.path.isdir(portion)]
+ if not portions:
return None
- found = importlib.machinery.PathFinder.find_spec(name, [portion])
+ found = importlib.machinery.PathFinder.find_spec(name, portions)
if found is not None:
- # The portion extend_path / PEP 420 would have collected; a _NamespacePath drops it on sys.path changes.
+ # The portions extend_path / PEP 420 would have collected; a _NamespacePath drops them on sys.path changes.
search = getattr(module, "__path__", None)
- if search is not None and hasattr(search, "append") and portion not in list(search):
- search.append(portion)
+ if search is not None and hasattr(search, "append"):
+ for portion in portions:
+ if portion not in list(search):
+ search.append(portion)
return found
_extends = {}
@@ -324,10 +327,57 @@ class _HostPackagesFinder:
import importlib.metadata
return importlib.metadata.MetadataPathFinder.find_distributions(
- importlib.metadata.DistributionFinder.Context(name = name, path = [self._folder])
+ importlib.metadata.DistributionFinder.Context(name = name, path = self._folders)
)
+# --ignore-installed never uninstalls, so a reinstall leaves stale dist-info importlib.metadata may pick;
+# RECORD is written last, so the newest RECORD per project is the finished install.
+def _prune_stale_dist_info():
+ try:
+ prefix = os.environ.get("PIP_PREFIX")
+ if not prefix:
+ return
+ root = os.path.join(os.path.normcase(os.path.abspath(prefix)), "")
+ for entry in sys.path:
+ if not entry or not os.path.normcase(os.path.abspath(entry)).startswith(root):
+ continue
+ try:
+ names = os.listdir(entry)
+ except OSError:
+ continue
+ projects = {}
+ for name in names:
+ if name.endswith(".dist-info"):
+ project = (
+ name[:-10].rpartition("-")[0].lower().replace("-", "_").replace(".", "_")
+ )
+ projects.setdefault(project, []).append(name)
+ for found in projects.values():
+ if len(found) < 2:
+ continue
+ stamped = []
+ for name in found:
+ try:
+ stamped.append(
+ (os.stat(os.path.join(entry, name, "RECORD")).st_mtime_ns, name)
+ )
+ except OSError:
+ pass
+ import shutil
+
+ for _, name in sorted(stamped)[:-1]:
+ shutil.rmtree(os.path.join(entry, name), ignore_errors = True)
+ except Exception: # noqa: BLE001 - a broken shim must never break user code
+ pass
+
+
+if os.environ.get("PIP_PREFIX"):
+ import atexit
+ _prune_stale_dist_info()
+ atexit.register(_prune_stale_dist_info)
+
+
_host_packages = os.environ.get("_UNSLOTH_HOST_SESSION_PACKAGES")
if _host_packages and not any(isinstance(f, _HostPackagesFinder) for f in sys.meta_path):
sys.meta_path.append(_HostPackagesFinder(_host_packages))
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index fe4b8979e5a..0125929bbc3 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -9090,15 +9090,23 @@ def _host_access_launch(plan, script: "str | None") -> "tuple[tuple, dict, str]"
packages = os.path.join(plan.workdir, os_sandbox.SESSION_PACKAGES_RELPATH)
if os.path.isdir(packages):
# Served by sitecustomize's last-resort finder, not sys.path, so plugin discovery never sees them.
- env["_UNSLOTH_HOST_SESSION_PACKAGES"] = packages
+ env["_UNSLOTH_HOST_SESSION_PACKAGES"] = os.pathsep.join(
+ os_sandbox.session_package_paths(packages)
+ )
env["PYTHONNOUSERSITE"] = "1"
# Chat-installed CLIs the approved call names resolve too, but only after every system executable.
# Copies of just those: a tool probing for a command it lacks never reaches the rest of the folder.
- requested = _requested_chat_clis(
- os.path.join(packages, "Scripts" if os.name == "nt" else "bin"),
- script if script is not None else " ".join(map(str, argv)),
- os.path.join(private, ".unsloth-chat-bin"),
- )
+ requested = None
+ # Reversed, so the scheme's own scripts dir wins a name clash, as it does on PATH.
+ for scripts in reversed(os_sandbox.session_script_dirs(packages)):
+ requested = (
+ _requested_chat_clis(
+ scripts,
+ script if script is not None else " ".join(map(str, argv)),
+ os.path.join(private, ".unsloth-chat-bin"),
+ )
+ or requested
+ )
if requested:
env["PATH"] = os.pathsep.join(
part for part in (env.get("PATH", ""), requested) if part
diff --git a/studio/backend/tests/test_mxc_dacl_fallback.py b/studio/backend/tests/test_mxc_dacl_fallback.py
index d66cc8ae550..a5995b04529 100644
--- a/studio/backend/tests/test_mxc_dacl_fallback.py
+++ b/studio/backend/tests/test_mxc_dacl_fallback.py
@@ -6,6 +6,7 @@ import base64
import os
import shutil
import sys
+import sysconfig
import json
from pathlib import Path
from types import SimpleNamespace
@@ -549,15 +550,20 @@ def test_unconfirmed_restore_fails_only_a_run_that_was_not_forced(
def test_mxc_workloads_install_into_and_import_from_the_session_packages(tmp_path):
- """The interpreter is read-only inside the container, so without PIP_TARGET a pip install fails there."""
+ """The interpreter is read-only inside the container, so without PIP_PREFIX a pip install fails there."""
packages = str(tmp_path / ".unsloth-packages")
env = mxc_policy._with_session_packages(
{"Path": "/windows/system32", "PYTHONPATH": "/shim"}, str(tmp_path)
)
- assert env["PIP_TARGET"] == packages
+ assert env["PIP_PREFIX"] == packages
+ assert env["PIP_IGNORE_INSTALLED"] == "1"
assert env["PYTHONNOUSERSITE"] == "1"
- assert env["PYTHONPATH"].split(os.pathsep) == ["/shim", packages]
+ pythonpath = env["PYTHONPATH"].split(os.pathsep)
+ assert pythonpath[0] == "/shim"
+ assert pythonpath[-1] == packages
+ site = sysconfig.get_path("purelib", vars = {"base": packages, "platbase": packages})
+ assert os.path.normcase(site) in map(os.path.normcase, pythonpath)
assert "PATH" not in env, "a second PATH beside Path is ambiguous on Windows"
assert env["Path"].split(os.pathsep)[0] == "/windows/system32"
assert os.path.join(packages, "Scripts") in env["Path"].split(os.pathsep)
diff --git a/studio/backend/tests/test_sandbox_linux.py b/studio/backend/tests/test_sandbox_linux.py
index d1aa3edbf6f..1e276b99070 100644
--- a/studio/backend/tests/test_sandbox_linux.py
+++ b/studio/backend/tests/test_sandbox_linux.py
@@ -291,13 +291,104 @@ def test_a_host_jvm_initialises_its_security_properties_inside_the_jail(tmp_path
assert f"user.home = {tmp_path}" in properties, properties
-def test_pip_gets_a_writable_target_inside_the_workdir(prepared):
+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_TARGET") + 1] == packages
+ 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 packages in pythonpath
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):
diff --git a/studio/backend/tests/test_sandbox_macos.py b/studio/backend/tests/test_sandbox_macos.py
index 1229cc491d9..a414c17aaf1 100644
--- a/studio/backend/tests/test_sandbox_macos.py
+++ b/studio/backend/tests/test_sandbox_macos.py
@@ -10,6 +10,7 @@ import re
import shlex
import subprocess
import sys
+import zipfile
from pathlib import Path
import pytest
@@ -237,13 +238,138 @@ def test_process_substitution_descriptors_are_readable(profile):
assert '(allow file-write* (regex #"^/dev/fd/[0-9]+$"))' in profile
-def test_pip_gets_a_writable_target_inside_the_workdir():
+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_TARGET"] == packages
- assert env["PYTHONPATH"].split(os.pathsep) == ["/shim", packages]
+ 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):
diff --git a/studio/backend/tests/test_tool_sandbox_hardening.py b/studio/backend/tests/test_tool_sandbox_hardening.py
index 64bbbc9b0e7..a26ca6f6713 100644
--- a/studio/backend/tests/test_tool_sandbox_hardening.py
+++ b/studio/backend/tests/test_tool_sandbox_hardening.py
@@ -92,6 +92,34 @@ def test_an_approved_host_call_loads_nothing_from_the_workdir(tmp_path, monkeypa
assert not os.path.exists(os.path.dirname(lines["FILE"]))
+@_POSIX
+def test_an_approved_host_call_imports_a_package_pip_installed_with_the_prefix_layout(
+ tmp_path, monkeypatch
+):
+ import sysconfig
+
+ workdir = tmp_path / "chat"
+ packages = workdir / os_sandbox.SESSION_PACKAGES_RELPATH
+ site = sysconfig.get_path("purelib", vars = {"base": str(packages), "platbase": str(packages)})
+ os.makedirs(os.path.join(site, "chatns"))
+ with open(os.path.join(site, "prefixpkg.py"), "w", encoding = "utf-8") as f:
+ f.write("VALUE = 'from-prefix-layout'\n")
+ with open(os.path.join(site, "chatns", "mod.py"), "w", encoding = "utf-8") as f:
+ f.write("VALUE = 'from-prefix-namespace'\n")
+ host_file = tmp_path / "host" / "notes.csv"
+ host_file.parent.mkdir()
+ host_file.write_text("a,b\n", encoding = "utf-8")
+ monkeypatch.setattr(tools, "_get_workdir", lambda session_id = None: str(workdir))
+ code = (
+ f"print('ROWS', open({str(host_file)!r}).read().strip())\n"
+ "import prefixpkg, chatns.mod\n"
+ "print('PKG', prefixpkg.VALUE, chatns.mod.VALUE)\n"
+ )
+ out = tools._python_exec(code, None, 60, "hardening-chat", host_access_approved = True)
+ assert "ROWS a,b" in out, out
+ assert "PKG from-prefix-layout from-prefix-namespace" in out, out
+
+
@_POSIX
def test_an_approved_terminal_call_reuses_chat_packages_without_shadowing(tmp_path, monkeypatch):
workdir = _planted_workdir(tmp_path)
diff --git a/studio/backend/tests/test_tool_sandbox_wiring.py b/studio/backend/tests/test_tool_sandbox_wiring.py
index e463d0f9872..5db907623a2 100644
--- a/studio/backend/tests/test_tool_sandbox_wiring.py
+++ b/studio/backend/tests/test_tool_sandbox_wiring.py
@@ -732,6 +732,7 @@ def test_session_package_commands_stay_on_path(monkeypatch, tmp_path, os_name, s
packages = workdir / os_sandbox.SESSION_PACKAGES_RELPATH
packages.mkdir(parents = True)
monkeypatch.setattr(os_sandbox.os, "name", os_name)
+ monkeypatch.setattr(os_sandbox, "_PIP_PREFIX_SCRIPTS_DIR", scripts)
env = os_sandbox.with_session_packages({"PATH": "/usr/bin"}, str(workdir))
assert env["PATH"].split(os.pathsep) == ["/usr/bin", str(packages / scripts)]