Skip to content

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

Merged
danielhanchen merged 11 commits into
unslothai:mainfrom
NilayYadav:second-pip-install-sandboxed-chat
Oct 30, 2026
Merged

danielhanchen merged 11 commits into
unslothai:mainfrom
NilayYadav:second-pip-install-sandboxed-chat

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

In a sandboxed chat (bubblewrap on Linux, Seatbelt on macOS, the Windows container), every pip install went into one shared package folder with pip's --target mode. That mode skips any top level folder that already exists and still reports success. So a later install silently lost its parts: after pip install requests, pip install gdown succeeded but gdown was then "command not found", a package sharing a namespace folder with an earlier one (such as the google packages) lost its modules, and installing idna 3.10 over 3.7 left 3.7 in use.

Sandboxed installs now use pip's prefix layout inside the same folder, which pip fills file by file, so each new package lands next to the earlier ones. Its commands stay on PATH as before, and packages that older chats installed in the old layout still import.

@NilayYadav NilayYadav self-assigned this Oct 29, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Reached Codex review convergence at NilayYadav#955 on 488cb47.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Reached Codex review convergence at NilayYadav#955 on 8201118.

pip --prefix with --ignore-installed never uninstalls, so reinstalling another version left
the old dist-info beside the new one and importlib.metadata could report the old version.
The sandbox shim now drops a project's older dist-info folders under PIP_PREFIX at interpreter
start and exit. Also mirror pip's osx_framework_library -> posix_prefix remap for prefix installs.

[skip ci]
Debian's posix_local scheme installs console scripts to local/bin, which PATH and approved
host calls never searched. Derive it from the same scheme as the library dirs, keeping the
bare bin/Scripts for packages installed with pip --target.

[skip ci]
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 30, 2026
On Windows the nt scheme's stdlib/include paths stay on the Python install drive, so taking
relpath of every entry raised ValueError at import when Studio ran from another drive.
danielhanchen added a commit that referenced this pull request Oct 30, 2026
…16108)

Since #16031 (test-launch sd.cpp binaries without the Windows
missing-DLL box), the AV shapes lint finds a new error on main. Every
PR's `Source lint` job and the
`test_lint_av_shapes.py::test_the_repository_is_clean_against_its_baseline`
test in the CPU shards fail on it, for example on #16018 and #15994:

```
studio/backend/tests/test_sd_cpp_probe_error_mode.py:221: [AV009] error: copies a Windows system binary
    line: shutil.copy(getattr(sys, "_base_executable", sys.executable), binary)
1 new error(s).
```

`test_a_real_binary_missing_its_dll_is_reported_unrunnable` copies
`python.exe` into `tmp_path` as `sd-server.exe`, away from
`pythonXY.dll`, and cuts `PATH` to System32. The result is a real PE
whose loader fails with `STATUS_DLL_NOT_FOUND`, the same failure as an
sd.cpp ROCm build without HIP. The lint's suggested replacement,
`windows_console_stub.py`, links nothing and starts fine, so it cannot
produce that failure. The copy has to stay. The lint documents this
route for that case: the finding is recorded in
`scripts/av_shapes_baseline.json` with the reason. The entry carries the
digest of that one line, so any other copy in the file is still a new
error.

I did not use `--update`. It would also have pruned many unrelated stale
entries across the baseline. This adds the one group (9 lines).

The CI message also prints an AV010 warning for
`.github/workflows/studio-tauri-smoke.yml:374` first, but warnings do
not fail the check. With only this entry added, the test passes.

## Verification

- `python scripts/lint_av_shapes.py`: `1 new error(s)` on main, `0 new
errors` here.
-
`tests/security/test_lint_av_shapes.py::test_the_repository_is_clean_against_its_baseline`:
fails on main on the AV009 line, passes here. The rest of the file
passes (19 passed).
- `--self-test` still passes, since the lint is unchanged.
@shimmyshimmer

Copy link
Copy Markdown
Member

Review record for #16018, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#639.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror e40300ba4 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/inference/sandbox_linux.py (on the mirror)

@@ -1052,9 +1053,12 @@ def prepare(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:
             "TMPDIR",
             _tmpdir(plan, workdir),
             "--setenv",
-            "PIP_TARGET",
+            "PIP_PREFIX",
             packages,

P2 Badge Derive the scripts directory from the selected prefix scheme

On Debian/Ubuntu distribution Python outside a virtualenv, the preferred prefix scheme is posix_local, so switching to PIP_PREFIX installs console scripts into .unsloth-packages/local/bin, not .unsloth-packages/bin. I reproduced this with the available /usr/bin/python3: installing a local wheel succeeded and created its command under prefix/local/bin. However, _path() still appends only packages/bin, and approved host calls likewise search only that directory in _host_access_launch(). Consequently, successfully installed commands remain unavailable in these environments. Derive the scripts path from the installation scheme alongside the library paths and use it for both PATH construction and approved-command discovery.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in fef003a on the source PR: the scripts dir now comes from the same prefix scheme as the library dirs (local/bin under posix_local), used for PATH and approved host calls, with the bare bin/Scripts kept for pip --target installs.

Round 2: reviewed mirror b166fb190 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/inference/os_sandbox.py (on the mirror)

@@ -35,6 +36,39 @@
 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 = {
+        key: os.path.relpath(path, base)
+        for key, path in sysconfig.get_paths(scheme, vars = {"base": base, "platbase": base}).items()
+    }

P1 Badge Restrict relative-path conversion to the required scheme entries

On Windows, when Studio's working directory and Python installation are on different drives, this comprehension raises ValueError before Studio can load the sandbox module. The nt scheme's stdlib, include, and platinclude entries use installed_base, which remains on Python's installation drive, whereas the synthetic base uses the current drive. I reproduced the failure using the Windows scheme and ntpath with a C: base and D: Python installation. Because _pip_prefix_dirs() runs at module import, this prevents backend initialization even though the three entries actually needed (purelib, platlib, and scripts) are valid. Convert only those required entries rather than every scheme path.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in 95ce0c6 on the source PR: only purelib, platlib and scripts go through relpath now, with a test that fails if any other scheme entry is converted.

Round 3: reviewed mirror 33be755068 (this PR's 95ce0c6e0)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 33be755068

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 33be755068 (this PR's 95ce0c6e0).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit f28d1c8 into unslothai:main Oct 30, 2026
4 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants