Skip to content

Keep uv, its cache and the uv-managed Python inside a custom UNSLOTH_STUDIO_HOME - #13373

Merged
danielhanchen merged 12 commits into
mainfrom
studio-home-keeps-uv
Oct 12, 2026
Merged

danielhanchen merged 12 commits into
mainfrom
studio-home-keeps-uv

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Part of #11327

Problem

UNSLOTH_STUDIO_HOME (and STUDIO_HOME) move the Studio install to a custom folder, but the installers still put the large uv pieces on the profile drive:

What Where it went with a custom home Size on a no-torch Linux install
uv package cache an existing shared cache (~/.cache/uv, %LOCALAPPDATA%\uv\cache) whenever one was populated 1770 MB
uv-managed Python the venv is built on uv's own store (~/.local/share/uv/python, %APPDATA%\uv\python) 108 MB
uv itself ~/.local/bin; on Windows also winget or the user PATH in the registry 54 MB

So a user who points the home at E:\Unsloth to save space on C: still gets several GB on C: (the CUDA torch wheels alone are a few GB of cache). On Windows the uv install also edited the user PATH, which env-override mode otherwise promises not to do (#5190).

Change

Opt-in: only when UNSLOTH_STUDIO_HOME (or its STUDIO_HOME alias) names a root other than the default ~/.unsloth/studio. Default installs, Desktop, Docker's explicit UV_CACHE_DIR, and UNSLOTH_HOME on its own are unchanged. At runtime the backend and unsloth studio update key on <home>/uv and <home>/python, which only the opt-in installer creates, since every launch exports UNSLOTH_STUDIO_HOME and so its presence proves nothing.

  • uv cache: the Studio cache under the home (same as --isolated-uv-cache), as long as it passes the existing usability probe; otherwise the existing selector and fallback run as before. An explicit UV_CACHE_DIR still wins.
  • uv-managed Python: UV_PYTHON_INSTALL_DIR=<home>/python unless the user set it. install.ps1 restores the variable afterwards, since under irm | iex it is the user's session.
  • uv: a uv already on PATH is still used. If one has to be installed, it goes to its own <home>/uv directory: no winget, no user PATH or shell profile edit (astral's UV_UNMANAGED_INSTALL for the script fallback in install.sh). Re-runs find it there first. An explicit UV_INSTALL_DIR / UV_UNMANAGED_INSTALL / XDG_BIN_HOME still wins.
  • Backend: on-demand installs look uv up on PATH, so for an opted-in home the backend puts <home>/uv first when it holds a uv (only uv binaries live there), and defaults UV_PYTHON_INSTALL_DIR to <home>/python. The managed engine installer's child environment passes UV_PYTHON_INSTALL_DIR through, so the ROCm engine's managed Python lands in the home as well.
  • unsloth studio update: runs setup directly. For an opted-in home it now passes UV_UNMANAGED_INSTALL=<home>/uv and UV_PYTHON_INSTALL_DIR=<home>/python: a uv already on PATH is used as before; otherwise setup version-checks the home's uv and installs or replaces it there, with no profile or user PATH edit. Without this, an update installed uv into ~/.local/bin and added a PATH line to ~/.profile.
  • Root ownership: the installers claim the custom root before creating python/ or uv/ in it, so the uninstaller still recognises a partial install. A stale private uv is reinstalled in place rather than shadowed.

The uninstaller already removes a marked custom root whole, so python/ and uv/ go with it.

Testing

Real install.sh runs, base (main) vs this branch, same script otherwise: sandboxed HOME with a populated shared uv cache, UNSLOTH_STUDIO_HOME elsewhere, clean PATH with no uv, UV_PYTHON_PREFERENCE=only-managed (a host without a suitable Python), UNSLOTH_NO_TORCH=1.

main this PR
Written under HOME (profile drive) 1932 MB (uv cache 1770, uv Python 108, uv 54) 0 MB
Written under UNSLOTH_STUDIO_HOME 2088 MB 4019 MB
venv base interpreter ~/.local/share/uv/python/cpython-3.13.14.../python3.13 <home>/python/cpython-3.13.14.../python3.13
Studio launches, /api/health 200
Second run (update) exit 0, uv not reinstalled, nothing new under HOME

unsloth studio update path on the same custom-home install, with a clean PATH: on main setup installed uv into ~/.local/bin and wrote export PATH=".../.local/bin:$PATH" to ~/.profile; with this PR nothing is written under HOME.

The backend change was checked against that install: with a clean PATH, shutil.which("uv") goes from None to <home>/uv/uv after setup_cache_env(), and UV_PYTHON_INSTALL_DIR is <home>/python.

On real Windows (windows-latest, the Windows Application Control lane, which installs with UNSLOTH_STUDIO_HOME=C:\Users\Public\issue-8490-home): on main winget installed uv 0.13.0 system-wide and printed "Path environment variable modified"; with this PR winget is skipped, the pinned uv 0.12.1 goes into the home and the PATH is left alone, and the install still passes under the AppLocker rule. The Windows installer differential lane (default home, base vs head on two runners) reports no difference, so default installs are unchanged.

.github/scripts/clean-machine-assert.sh asserted that uv lives outside the Studio home ($HOME/.local/bin), which is exactly what this changes; it now requires uv to be classified as a native binary wherever it lives.

tests/python/test_custom_studio_home_keeps_uv.py (9 tests) runs the new install.ps1 block under PowerShell and the install.sh blocks under sh, and covers the backend and update lookups. All of them fail on main. These existing suites still pass: test_cross_platform_parity.py, test_windows_uv_cache_selection.py, test_windows_uv_managed_python.py, test_windows_python_venv_hardening.py, the Windows ARM Python suites, test_setup_master_root.py, test_studio_update_uv_cache.py (684 tests), plus tests/sh/test_install_uv_cache_root.sh, test_uv_cache_adaptive_selection.sh, test_uv_pinned_release.sh and test_portable_isolated_venv.sh.

Not in this PR

@danielhanchen danielhanchen self-assigned this Oct 12, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T15:03:30.914005Z f54aef0 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 12, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 14ec194337

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +3813 to +3815
out = {**(env if env is not None else os.environ)}
if not (out.get("UV_PYTHON_INSTALL_DIR") or "").strip():
out["UV_PYTHON_INSTALL_DIR"] = str(STUDIO_HOME / "python")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep custom-home updates on the Studio uv cache

When a custom home's recorded cache is missing or has been emptied while uv's profile-default cache is warm, _with_studio_uv_cache() selects that shared cache and places it in env; this new custom-home helper copies the selected UV_CACHE_DIR unchanged. studio/setup.sh then treats it as an explicit caller value, so unsloth studio update downloads packages onto the profile drive and may backfill the shared path into the marker, defeating the isolation this change promises. The custom-home case needs to select the writable Studio cache before the warm-default fallback while still preserving a genuinely caller-supplied UV_CACHE_DIR.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: the install records the Studio cache and fills it, so update only reaches the shared default if the user empties the Studio cache while a shared one is warm. Installs from before this change keep their recorded cache on purpose.

Comment thread unsloth_cli/commands/studio.py Outdated
Comment on lines +3816 to +3818
bin_dir = STUDIO_HOME / "bin"
current = out.get("PATH", "")
if (bin_dir / ("uv.exe" if os.name == "nt" else "uv")).is_file() and str(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Direct replacement uv installs into the custom home

When the custom install has no private uv—for example, the original install reused a system uv that is later removed or no longer present on the update process's PATH—this branch does nothing beyond leaving <home>/bin off PATH. Because it also does not pass UV_INSTALL_DIR or UV_UNMANAGED_INSTALL, both setup scripts install their replacement uv into the profile-default location (and may persist that location on the user PATH), contrary to the custom-home isolation promised here. Set a private install destination for custom-home updates when the caller has not supplied one, even when the binary does not exist yet.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: it needs a system uv the install relied on to disappear later. Pointing setup's uv install at the home would mean exporting astral's install variables into every setup run, which costs more than this case is worth.

Comment on lines +1301 to +1305
if not (bin_dir / ("uv.exe" if os.name == "nt" else "uv")).is_file():
return
current = os.environ.get("PATH", "")
if str(bin_dir) not in current.split(os.pathsep):
os.environ["PATH"] = str(bin_dir) + os.pathsep + current if current else str(bin_dir)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve explicit uv destinations at backend startup

When a custom home still contains a private uv from an earlier install but the caller later selects UV_INSTALL_DIR, UV_UNMANAGED_INSTALL, or XDG_BIN_HOME and places that uv on PATH, this startup hook unconditionally prepends the stale <studio>/bin copy. The installers explicitly suppress their private destination when any of those overrides is present, so leaving the old file behind is expected; overriding PATH here makes on-demand installs run the wrong, potentially below-minimum uv instead of the caller-selected one. Apply the same explicit-destination guard before prepending the private binary.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: a stale uv in the home plus an explicit uv destination set after the install is not a path anyone takes. The home's uv is the pinned release the installer chose, so it is still a valid uv.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 14ec194337

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: c1cbfcf2eb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 61139a6e58

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

GGUF inference smoke (API, tools, vision) fails on this head (61139a6e58) in a Windows PowerShell step, right after the install-transport self-tests pass:

InvalidOperation:
Line |
  15 |          $destDir = $script:StudioPrivateUvDir
     | The variable '$script:StudioPrivateUvDir' cannot be retrieved because it has not been set.

install.ps1:7418 reads $script:StudioPrivateUvDir inside the uv install function. The variable is only assigned at line 7550, on the main install path. The step that failed runs that function on its own under Set-StrictMode, so the variable does not exist yet. Initialising it at script scope near the top of install.ps1, or reading it with a guard such as Get-Variable -Scope Script -Name StudioPrivateUvDir -ValueOnly -ErrorAction SilentlyContinue, should clear it. This is specific to this PR.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 49b800e91a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1302 to +1303
if (uv_dir / ("uv.exe" if os.name == "nt" else "uv")).is_file():
os.environ["PATH"] = _path_with_first(os.environ.get("PATH", ""), str(uv_dir))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the private uv before prepending it

When the private uv is stale or nonfunctional and its replacement download fails while a usable system uv remains on PATH, both installer paths can continue successfully, leaving the stale file in place. This startup hook checks only is_file() and then shadows the working system uv, so subsequent engine/package installs execute the known-bad private copy. Unlike the earlier explicit-destination case, the fresh evidence is the installers' tolerated refresh-failure path; validate the private binary/version before moving its directory first on PATH.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: it needs a future uv floor above the pinned release, a failed replacement download on that reinstall, and a working system uv, all at once. Even then the home's uv is the pinned release the installer chose, so on-demand installs still run; a version probe on every backend start costs more than this case is worth.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 49b800e91a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: f54aef02cb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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.

1 participant