Skip to content

Give unsloth studio update the same uv cache the backend already uses - #10386

Merged
danielhanchen merged 16 commits into
mainfrom
studio-update-uv-cache
Sep 7, 2026
Merged

danielhanchen merged 16 commits into
mainfrom
studio-update-uv-cache

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Three places pick a uv cache and only two of them agreed.

install.sh and install.ps1 choose one for the install itself (#10204), and storage_roots._setup_cache_env seeds cache_root()/uv for the backend server, which is also where install.sh repoints UV_CACHE_DIR just before it autostarts Studio. An update reaches neither. unsloth studio update runs setup.sh / setup.ps1 straight from the CLI process, nothing on that path sets the variable, and _setup_cache_env is only ever called from studio/backend/main.py:210 and ensure_studio_directories, so uv fell back to its own user-wide default.

On a fresh install that means the installer downloads into <StudioHome>/cache/uv, the first update downloads all of it again into ~/.cache/uv or %LOCALAPPDATA%\uv\cache, and the installer's copy is never read again. The second copy also lands where scripts/uninstall.sh:588 and scripts/uninstall.ps1:861 cannot reclaim it, since they only remove the Studio root.

Why the update path was missed

unsloth studio update does two things. The dependency work is _run_setup_script (unsloth_cli/commands/studio.py:3653), which runs bash setup.sh or powershell setup.ps1. The launcher refresh is install.sh --shortcuts-only, and that branch exits at install.sh:2029 while _configure_uv_cache is called at install.sh:2826, so the installer's selector is never reached on an update and would not help if it were: the shortcuts branch runs no uv at all.

Repo-wide, the only writers of UV_CACHE_DIR are the two installers and storage_roots.py:362. Nothing in unsloth_cli/, studio/setup.sh or studio/install_python_stack.py sets it. Worth noting that install_python_stack.py:6268 drops --no-cache-dir when it translates pip args for uv, so UV_CACHE_DIR genuinely governs what an update downloads.

The rule

The installer's own record decides, and content only fills in for installs that predate it.

With a marker naming a cache that still holds packages, that cache wins. Without one, the update prefers uv's default whenever it is warm, and the Studio cache when it is not. A warm Studio cache is deliberately not treated as decisive on its own: install.sh:705 points the running backend at it even in shared mode, so a single on-demand install from the server warms it, and an install that predates the marker cannot record its way out of the mistake, because the backfill only runs after a setup that would already have failed offline. Preferring uv's default there is what such an install has been updating from all along.

That matters because _setup_cache_env mkdirs an empty Studio cache on every server start, so a shared install would otherwise be sent to a cache holding nothing. Online that is one duplicate download. With UV_OFFLINE, --offline or offline = true in a uv.toml, where uv may read only what is already cached, it is an update that cannot resolve at all:

shared-mode install, then update
  no UV_CACHE_DIR (before)                  -> UV_OFFLINE=1 update SUCCEEDED
  UV_CACHE_DIR=<StudioHome>/cache/uv        -> UV_OFFLINE=1 update FAILED
      hint: Packages were unavailable because the network was disabled.

The fresh-install case the PR exists for is the other direction: the installer filled <StudioHome>/cache/uv, uv's own default holds nothing, and the update was downloading all of it again.

Warmth means package bytes, using the same bucket list and metadata-suffix filter as install.sh:_configure_uv_cache, since wheels-v6 carries only .msgpack/.http on uv 0.10 and would otherwise read as warm after a bare resolve.

Whichever cache wins is passed to the child as an explicit absolute path rather than left for it to resolve again. uv reads UV_CACHE_DIR as --cache-dir, so a blank inherited value is a missing argument rather than an unset one (UV_CACHE_DIR= uv cache dir exits 2 with "a value is required"), and uv answers cache-dir = "relcache" with relcache verbatim, which setup.sh would resolve from a different directory once it changes into $SCRIPT_DIR.

Measured

Real uv 0.10.7 against the merged selector, one package, --reinstall, counting files that appear in each cache tree:

installer mode before, update reads after, update reads
studio (no existing uv cache) uv default, 0 to 23 files, re-downloaded Studio cache, unchanged, reused
shared (existing uv cache) uv default, reused uv default, reused

New installs stop paying for a download they already made, and no install pays a new one.

Behaviour

Precedence is the installers' own. A nonblank UV_CACHE_DIR from the caller still wins (install.sh:626, install.ps1:1232), so a CI image that pins a cache keeps it, and blank counts as unset exactly as storage_roots.py:373 treats it.

The seeding builds the child environment rather than mutating os.environ, so a later unsloth studio in the same process does not look like a caller override to _setup_cache_env.

The staged update path needs nothing extra: run_staged_update spawns python -m unsloth_cli studio update with a copy of the environment, and that child re-enters _run_setup_script and seeds itself.

_refresh_desktop_shortcuts is deliberately left alone. It runs the installers, which treat a preset UV_CACHE_DIR as a custom override, so seeding there would change which branch the installer takes.

The probe that asks uv for its default cache is best effort. It pins UTF-8 (text=True alone decodes with the locale codec and strict errors, and UnicodeDecodeError is a ValueError, so it would escape the OSError handler), carries the repository's Windows hidden-subprocess kwargs since creation flags are not inherited, and swallows anything else: not knowing where uv would have put its cache costs the update a preference, never the update itself.

Tests

37 tests in unsloth_cli/tests/test_studio_update_uv_cache.py: the default, blank in three spellings, an explicit override, the verbose branch keeping both variables, the PowerShell spawn, no leak into the parent process, and the selection rule itself. The selection cases are a shared install keeping the cache that has the wheels, a studio install winning over a populated default, both caches cold going to the Studio one, a metadata-only default not counting as warm, uv cache dir being unavailable not stranding the update, each bucket name uv has shipped, and the probe's codec, hidden-subprocess kwargs and absolutising.

One existing test changed. test_studio_runtime_gate_powershell.py::test_the_setup_handoff_spawns_the_resolved_interpreter asserted on spawned[0], and subprocess.run is built on Popen, so the new probe lands in a test that fakes Popen. It now picks the handoff out of the spawn list, which is what it is about; the resolved-interpreter assertion is unchanged.

Verification run locally:

sh / dash / bash tests/sh/test_install_uv_cache_root.sh          81 checks each
sh / dash / bash tests/sh/test_install_rollback_lifecycle.sh     43 checks each
pytest test_studio_update_uv_cache.py + parity + hardening       183 passed, 2 skipped
all 24 tests/studio/*.ps1                                        pass
install.ps1 AST parse                                            clean
ruff 0.15.18 check, scripts/run_ruff_format.py                   clean

Run these with UV_CACHE_DIR unset. If your shell sets it, _with_studio_uv_cache returns at its first line and none of this code is exercised.

One thing to flag for anyone running the CLI suite locally: unsloth_cli/tests has 44 failures on main already, all in test_studio_secure_flag.py and unrelated to this path.

The installers record their choice too (#10410, merged into this branch)

Content alone cannot always decide. In shared mode the installers deliberately point the running backend at the Studio cache once the install is done (_prepare_studio_uv_cache_for_launch in install.sh, Set-StudioUvCacheForLaunch in install.ps1), even though the install itself used uv's own cache. One on-demand install from the server, such as studio/backend/utils/wheel_utils.py:405, then drops package bytes into the Studio cache, both caches read as warm, and the content rule prefers the wrong one. The mirror is just as reachable: a studio install loses to uv's default the moment the user runs an unrelated uv pip install.

Measured with uv 0.10.7 against the same scratch install:

install's cache: 24 files | studio cache (runtime leftovers): 61 files

content rule alone   picks <StudioHome>/cache/uv   -> UV_OFFLINE=1 update FAILED
with the marker      picks the uv default cache    -> UV_OFFLINE=1 update SUCCEEDED

So install.sh and install.ps1 write the cache they selected to <StudioHome>/cache/uv-cache-dir, and _with_studio_uv_cache reads it before falling back to inspecting both caches. Every mode records, a caller's own UV_CACHE_DIR included, since that is where the install actually put its wheels; a caller keeps control regardless, because a nonblank UV_CACHE_DIR outranks the marker at update time too. A marker is honoured only while the cache it names still holds packages, so uv cache clean is not overruled, and installs that predate the marker keep working on the content fallback, which the update then records once setup has returned 0.

The marker travels with the environment: written absolute, restored at every point a failed install puts the previous environment back, cleared the moment the new environment is committed, and reset per run because under irm | iex the script scope is the caller's session. It is never fatal: an unwritable Studio root costs the next update a preference, not the install.

Follow-up

A shared install still writes its Torch and CUDA wheels to the user-wide cache, outside the Studio root, where the uninstallers cannot reclaim them, so the storage half of #10193 stays open. Closing that means making the Studio cache the install default and offering the reuse as an opt-in, which reverses the measured trade-off #10204 landed on and is a product call rather than a bug.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 6, 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-09-07T06:54:35.124538Z a283e00 New commits
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 91f27be0d5

ℹ️ 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[bot]

This comment was marked as resolved.

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

This comment was marked as resolved.

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

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
Two items from the last review.

Both setup scripts change into their own directory before the dependency pass
(studio/setup.sh:1788), and uv discovers uv.toml and pyproject.toml from its
working directory. The probe asked from the caller's, so running an update while
standing in an unrelated project answered with that project's cache, and the
answer is then forced on the child through UV_CACHE_DIR. It now runs with the
setup script's directory as its working directory, and resolves a relative
answer against that same base.

A newline is legal in a POSIX path and uv reports one verbatim, but the marker
reader split the record into lines and kept the last fragment, so such a cache
read as absent and the update redirected uv elsewhere. The record is one path
followed by one delimiter now: strip that delimiter, keep everything else.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

… step

Six items from the last review.

setup.ps1 never changes directory: it addresses everything through PSScriptRoot
and hands install_python_stack.py the cwd it inherited, so probing from the
script's directory answered for a configuration that child never sees. The probe
now uses the directory setup will actually run uv from, which is the script's on
POSIX and the inherited one on Windows.

UV_WORKING_DIR may itself be relative, and uv resolves it after starting where
the probe started, so it is anchored to the probe's directory rather than to
wherever the update was launched.

uv reports a cache path verbatim, and stripping the reported line changed a
directory name that legitimately ends in a space. Blank lines are still dropped.

A relative UV_CACHE_DIR named a different directory in each phase of one install,
because setup.sh changes into its own directory before the dependency pass. It is
resolved once, before anything reads it, so both phases and the marker agree.
What is restored afterwards is still the caller's own spelling.

Windows PowerShell 5.1 decodes a BOM-less file with the active ANSI code page,
and the update writes this marker BOM-less UTF-8, so a non-ASCII path was read
back as mojibake and restored that way. It is read as UTF-8 explicitly.

The commit cleared two flags, and a signal between them put the previous
environment back while keeping the marker of the attempt that replaced it. One
flag decides both rollbacks now, set first and alone, and both restores consult
it.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…talls alone

Two items from the last review.

The custom branch on Windows anchored a relative UV_CACHE_DIR to $PWD alone,
ignoring UV_WORKING_DIR, and rooting it that way also stopped the marker writer
from applying its own resolution. From C:\work with UV_WORKING_DIR=subdir and
UV_CACHE_DIR=cache, uv uses C:\work\subdir\cache and the installer recorded
C:\work\cache. Both call Resolve-StudioUvCachePath now, which mirrors
_absolutize_uv_cache_dir.

An install that predates the marker cannot say which cache it used, and content
cannot tell either: the running backend is pointed at the Studio cache even in
shared mode, so one on-demand wheel warms it. Where both caches are warm and no
marker settles it, the update keeps uv's default, which is what such an install
has been updating from all along. Preferring the Studio cache there was a new way
for an offline update to fail on an install that cannot record its way out. A
Studio cache that wins on content alone still wins when uv's default is cold,
which is the case this PR exists for, and a recorded marker still settles both.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 84d19db45b

ℹ️ 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".

Comments and docstrings only, no code. Verified with comment_tools.py check and
by rerunning the suites: 238 passed, both sh suites under sh, dash and bash, and
all 24 pwsh studio tests.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Confirmed against uv 0.10.7: UV_NO_CACHE=1 makes `uv cache dir` report a
temporary directory that is discarded when the process exits. The probe
inherited that, read the throwaway as cold, and the update then seeded a
UV_CACHE_DIR that --no-cache outranks anyway and recorded it as the install's
cache. On an install predating the marker that redirected every later update to
a cache nothing had ever filled.

Neither seeds nor records now, on uv's own boolish spelling of the variable: 0
and false leave caching on, and a value uv cannot parse is one it refuses to run
on at all, which is not the same as running without a cache.
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 8700121b75

ℹ️ 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".

Comments and docstrings only, no code. Verified with comment_tools.py check and
by rerunning the suites: 246 passed, both sh suites under sh, dash and bash, and
all 24 pwsh studio tests.

@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: a283e00148

ℹ️ 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 thread install.ps1
Comment on lines +1246 to +1247
if ([string]::IsNullOrEmpty($Cache) -or [System.IO.Path]::IsPathRooted($Cache)) {
return $Cache

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 Fully qualify drive-relative Windows cache paths

When UV_CACHE_DIR is drive-relative (such as C:cache) or root-relative (such as \cache), .NET's IsPathRooted returns true even though the path is not fully qualified, so this branch records the ambiguous spelling unchanged. uv resolves such paths using the process's current drive/directory, while a later unsloth studio update can run from a different managed working directory and therefore select another cache, causing duplicate downloads or offline update failures. Fully qualify these path forms instead of treating every rooted path as absolute.

Useful? React with 👍 / 👎.

Comment on lines +3916 to +3917
# Only now: a cache that did not get through setup is not one to record.
_backfill_uv_cache_marker(env)

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 Do not backfill the marker after a no-op setup

A zero setup exit does not prove that this cache was used: both setup scripts skip the dependency pass when the installed package is already current. On an install predating the marker, if both the original Studio cache and uv's default have become warm, the fallback guesses the default; a no-op update then persists that guess here as authoritative. A later offline update follows the marker and can fail even though the original Studio cache contains the required artifacts. Backfill only when setup explicitly reports that successful uv dependency work actually used the selected cache.

Useful? React with 👍 / 👎.

Comment on lines +3686 to +3688
xdg = (os.environ.get("XDG_CACHE_HOME") or "").strip()
if xdg:
return Path(xdg) / "uv"

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 Ignore relative XDG cache roots in the fallback

When uv is unavailable to the probe and XDG_CACHE_HOME is relative, this returns the relative value even though uv follows the XDG rule that this variable must be absolute and otherwise falls back to $HOME/.cache/uv. _with_studio_uv_cache then exports the returned path as an explicit UV_CACHE_DIR, changing it from an ignored invalid XDG setting into a cache relative to the setup script's working directory; this can create cache data inside the installed package tree or fail on a non-writable installation instead of reusing uv's real default. Require an absolute XDG path before honoring it here.

Useful? React with 👍 / 👎.

Comment on lines +3770 to +3771
if recorded.endswith("\r"):
recorded = recorded[:-1]

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 a trailing carriage return in POSIX cache names

On POSIX, a cache directory may legally end in a carriage-return byte, and install.sh records it as that byte followed by its newline delimiter. After removing the newline, this unconditional second removal mistakes the path's real trailing \r for a Windows line ending, so the marker names a different directory and the warm cache is ignored; offline updates can then fail despite the recorded cache containing the required artifacts. Only consume a CRLF delimiter for markers written on Windows, or use an unambiguous marker encoding.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant