Repository navigation
Give unsloth studio update the same uv cache the backend already uses - #10386
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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.
…dio-update-uv-cache
… 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.
…dio-update-uv-cache
…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.
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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.
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.
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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.
There was a problem hiding this comment.
💡 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".
| if ([string]::IsNullOrEmpty($Cache) -or [System.IO.Path]::IsPathRooted($Cache)) { | ||
| return $Cache |
There was a problem hiding this comment.
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 👍 / 👎.
| # Only now: a cache that did not get through setup is not one to record. | ||
| _backfill_uv_cache_marker(env) |
There was a problem hiding this comment.
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 👍 / 👎.
| xdg = (os.environ.get("XDG_CACHE_HOME") or "").strip() | ||
| if xdg: | ||
| return Path(xdg) / "uv" |
There was a problem hiding this comment.
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 👍 / 👎.
| if recorded.endswith("\r"): | ||
| recorded = recorded[:-1] |
There was a problem hiding this comment.
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 👍 / 👎.
Summary
Three places pick a uv cache and only two of them agreed.
install.shandinstall.ps1choose one for the install itself (#10204), andstorage_roots._setup_cache_envseedscache_root()/uvfor the backend server, which is also whereinstall.shrepointsUV_CACHE_DIRjust before it autostarts Studio. An update reaches neither.unsloth studio updaterunssetup.sh/setup.ps1straight from the CLI process, nothing on that path sets the variable, and_setup_cache_envis only ever called fromstudio/backend/main.py:210andensure_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/uvor%LOCALAPPDATA%\uv\cache, and the installer's copy is never read again. The second copy also lands wherescripts/uninstall.sh:588andscripts/uninstall.ps1:861cannot reclaim it, since they only remove the Studio root.Why the update path was missed
unsloth studio updatedoes two things. The dependency work is_run_setup_script(unsloth_cli/commands/studio.py:3653), which runsbash setup.shorpowershell setup.ps1. The launcher refresh isinstall.sh --shortcuts-only, and that branch exits atinstall.sh:2029while_configure_uv_cacheis called atinstall.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_DIRare the two installers andstorage_roots.py:362. Nothing inunsloth_cli/,studio/setup.shorstudio/install_python_stack.pysets it. Worth noting thatinstall_python_stack.py:6268drops--no-cache-dirwhen it translates pip args for uv, soUV_CACHE_DIRgenuinely 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:705points the running backend at it even insharedmode, 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_envmkdirs an empty Studio cache on every server start, so asharedinstall would otherwise be sent to a cache holding nothing. Online that is one duplicate download. WithUV_OFFLINE,--offlineoroffline = truein auv.toml, where uv may read only what is already cached, it is an update that cannot resolve at all: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, sincewheels-v6carries only.msgpack/.httpon 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_DIRas--cache-dir, so a blank inherited value is a missing argument rather than an unset one (UV_CACHE_DIR= uv cache direxits 2 with "a value is required"), and uv answerscache-dir = "relcache"withrelcacheverbatim, whichsetup.shwould 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:studio(no existing uv cache)shared(existing uv cache)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_DIRfrom 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 asstorage_roots.py:373treats it.The seeding builds the child environment rather than mutating
os.environ, so a laterunsloth studioin the same process does not look like a caller override to_setup_cache_env.The staged update path needs nothing extra:
run_staged_updatespawnspython -m unsloth_cli studio updatewith a copy of the environment, and that child re-enters_run_setup_scriptand seeds itself._refresh_desktop_shortcutsis deliberately left alone. It runs the installers, which treat a presetUV_CACHE_DIRas 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=Truealone decodes with the locale codec and strict errors, andUnicodeDecodeErroris aValueError, so it would escape theOSErrorhandler), 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 asharedinstall keeping the cache that has the wheels, astudioinstall winning over a populated default, both caches cold going to the Studio one, a metadata-only default not counting as warm,uv cache dirbeing 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_interpreterasserted onspawned[0], andsubprocess.runis built onPopen, so the new probe lands in a test that fakesPopen. 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:
Run these with
UV_CACHE_DIRunset. If your shell sets it,_with_studio_uv_cachereturns at its first line and none of this code is exercised.One thing to flag for anyone running the CLI suite locally:
unsloth_cli/testshas 44 failures onmainalready, all intest_studio_secure_flag.pyand unrelated to this path.The installers record their choice too (#10410, merged into this branch)
Content alone cannot always decide. In
sharedmode the installers deliberately point the running backend at the Studio cache once the install is done (_prepare_studio_uv_cache_for_launchininstall.sh,Set-StudioUvCacheForLaunchininstall.ps1), even though the install itself used uv's own cache. One on-demand install from the server, such asstudio/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: astudioinstall loses to uv's default the moment the user runs an unrelateduv pip install.Measured with uv 0.10.7 against the same scratch install:
So
install.shandinstall.ps1write the cache they selected to<StudioHome>/cache/uv-cache-dir, and_with_studio_uv_cachereads it before falling back to inspecting both caches. Every mode records, a caller's ownUV_CACHE_DIRincluded, since that is where the install actually put its wheels; a caller keeps control regardless, because a nonblankUV_CACHE_DIRoutranks the marker at update time too. A marker is honoured only while the cache it names still holds packages, souv cache cleanis 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 | iexthe 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
sharedinstall 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.