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.

Cache uv's downloads, which is now the largest single cost in CI (#9271)

Daniel Han6c418978331a
diff --git a/.github/actions/install-unsloth-local/action.yml b/.github/actions/install-unsloth-local/action.yml
index 5d4017c3c6a..6938ee91db5 100644
--- a/.github/actions/install-unsloth-local/action.yml
+++ b/.github/actions/install-unsloth-local/action.yml
@@ -57,6 +57,42 @@ inputs:
runs:
using: composite
steps:
+ # This step is the single largest cost in CI: 92s median across 39 job runs
+ # in one sample, which is more total time than any test. All of it is uv
+ # resolving and DOWNLOADING the same wheels, in every job, every run, because
+ # uv's cache is per-runner and nothing carried it between them.
+ #
+ # A download cache is the safest kind there is. uv's is content-addressed by
+ # URL and hash, so a stale entry cannot serve wrong content -- the worst it can
+ # do is miss. Nothing about which packages get installed changes; they just are
+ # not fetched twice. That is the difference between this and caching the venv,
+ # which would have to reason about an editable overlay, a moving
+ # `unsloth-zoo @ git+main`, and absolute paths baked into console scripts.
+ #
+ # UV_CACHE_DIR is set explicitly rather than relying on ~/.cache/uv, so the
+ # path is the same on Linux and macOS and one cache config covers both. It is
+ # exported into the environment so install.sh, studio/setup.sh and every uv
+ # call underneath inherit it.
+ - name: Point uv's cache somewhere cacheable
+ shell: bash
+ run: |
+ echo "UV_CACHE_DIR=${{ github.workspace }}/.uv-cache" >> "$GITHUB_ENV"
+ mkdir -p "${{ github.workspace }}/.uv-cache"
+
+ # Keyed on what decides the resolution, with a prefix fallback: a near-miss
+ # still supplies almost every wheel, which is most of the win. A download cache
+ # wants restore-keys for exactly this reason, where a venv cache must not have
+ # them.
+ - name: Restore the uv download cache
+ id: uv-cache
+ uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
+ continue-on-error: true
+ with:
+ path: ${{ github.workspace }}/.uv-cache
+ key: uv-${{ runner.os }}-${{ hashFiles('studio/backend/requirements/**', 'pyproject.toml') }}
+ restore-keys: |
+ uv-${{ runner.os }}-
+
- name: Install Unsloth (--local, --no-torch)
shell: bash
env:
@@ -90,3 +126,30 @@ runs:
| while IFS= read -r line || [ -n "$line" ]; do
printf '[%4ds] %s\n' "$SECONDS" "$line"
done
+
+ # Save on main only, like every other cache in this repo, and for the reason
+ # the HF and Playwright caches record: a PR-scoped entry can only be restored
+ # by re-runs of that same PR, while every PR can restore from the default
+ # branch. Saving on PRs would spend the 50 GiB budget -- measured at 99.3% full
+ # once already -- evicting main's copy, which is the one everyone reads.
+ #
+ # `uv cache prune --ci` first: uv keeps built wheels AND unpacked source
+ # artifacts, and only the former are worth carrying between runners. Without it
+ # this key grows without bound across 39 jobs and re-opens the eviction thrash
+ # the GGUF caches were fixed for.
+ - name: Save the uv download cache
+ if: always() && github.ref == 'refs/heads/main' && steps.uv-cache.outputs.cache-hit != 'true'
+ shell: bash
+ run: |
+ # Best effort: an older uv without `prune --ci` must not fail the install
+ # that has already succeeded.
+ uv cache prune --ci 2> /dev/null || true
+ du -sh "${{ github.workspace }}/.uv-cache" 2> /dev/null || true
+
+ - name: Save the uv download cache (upload)
+ if: always() && github.ref == 'refs/heads/main' && steps.uv-cache.outputs.cache-hit != 'true'
+ uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
+ continue-on-error: true
+ with:
+ path: ${{ github.workspace }}/.uv-cache
+ key: uv-${{ runner.os }}-${{ hashFiles('studio/backend/requirements/**', 'pyproject.toml') }}
diff --git a/.github/workflows/workflow-trigger-lint.yml b/.github/workflows/workflow-trigger-lint.yml
index b7049d64e18..0a658ad7179 100644
--- a/.github/workflows/workflow-trigger-lint.yml
+++ b/.github/workflows/workflow-trigger-lint.yml
@@ -133,5 +133,6 @@ jobs:
tests/studio/test_playwright_suites_run_in_ci.py \
tests/studio/test_smoke_workflows_share_one_script.py \
tests/studio/test_stt_model_search_locator_contract.py \
+ tests/studio/test_uv_cache_discipline.py \
tests/studio/test_windows_small_checks_stay_on_their_image.py \
tests/studio/test_workflow_guards_run_unfiltered.py
diff --git a/tests/studio/test_uv_cache_discipline.py b/tests/studio/test_uv_cache_discipline.py
new file mode 100644
index 00000000000..e2022914418
--- /dev/null
+++ b/tests/studio/test_uv_cache_discipline.py
@@ -0,0 +1,146 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+"""
+The uv download cache must stay a download cache, and must never reach a
+cold-install lane.
+
+`Install Unsloth (--local, --no-torch)` is the single largest cost in CI: 92s
+median across 39 job runs in one sample, more total time than any test, and all
+of it uv re-downloading the same wheels because its cache is per-runner.
+
+Caching that is safe *because of what is cached*. uv's cache is content-addressed
+by URL and hash, so a stale entry cannot serve wrong content -- the worst it can
+do is miss. That property is the whole justification, and it is exactly what a
+later edit could take away by pointing the same cache config at the venv, or at
+`~/.unsloth`, where an editable overlay, a moving `unsloth-zoo @ git+main` and
+absolute paths in console scripts all live. These tests pin the distinction.
+
+The second invariant is the one with teeth. `clean-machine-install-ci.yml` and
+`desktop-app-clean-machine-ci.yml` exist to prove the installer works on a
+machine with nothing on it; both set their own `UV_CACHE_DIR` and delete it
+before running. If either ever adopted this action, the composite writes
+`UV_CACHE_DIR` to `$GITHUB_ENV`, which outranks a job-level `env:` for every
+later step -- so a warm cache would silently replace the cold machine those
+workflows are named after, and they would still go green.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+import yaml
+
+REPO_ROOT = Path(__file__).resolve().parents[2]
+ACTION = REPO_ROOT / ".github" / "actions" / "install-unsloth-local" / "action.yml"
+WORKFLOWS = REPO_ROOT / ".github" / "workflows"
+
+# Named, not detected: a lane whose whole point is a cold machine should have to
+# be removed from this list deliberately, in a diff someone reads.
+COLD_INSTALL_WORKFLOWS = (
+ "clean-machine-install-ci.yml",
+ "desktop-app-clean-machine-ci.yml",
+ "interrupted-install-ci.yml",
+)
+
+
+def _steps() -> list[dict]:
+ return yaml.safe_load(ACTION.read_text(encoding = "utf-8"))["runs"]["steps"]
+
+
+def _index_of(predicate) -> int:
+ for i, step in enumerate(_steps()):
+ if predicate(step):
+ return i
+ return -1
+
+
+def test_the_cache_holds_uvs_downloads_and_not_the_venv() -> None:
+ """
+ A venv cache would have to reason about the editable overlay, a moving
+ unsloth-zoo pin, and absolute paths in console scripts. A download cache
+ reasons about none of that, which is why this one is safe at all.
+ """
+ for step in _steps():
+ if "cache" not in str(step.get("uses", "")):
+ continue
+ path = str((step.get("with") or {}).get("path", ""))
+ assert ".uv-cache" in path, f"cache step points at {path!r}, not uv's download cache"
+ for forbidden in (".unsloth", "site-packages", "unsloth_studio", "venv"):
+ assert forbidden not in path, (
+ f"cache step points at {path!r}, which is an INSTALL, not a download "
+ f"cache. A restored install can be wrong; a restored download cannot."
+ )
+
+
+def test_uv_cache_dir_is_set_before_the_install_runs() -> None:
+ """Set afterwards it configures nothing, and the step would still look right."""
+ setter = _index_of(lambda s: "UV_CACHE_DIR" in str(s.get("run", "")))
+ install = _index_of(lambda s: "install.sh --local --no-torch" in str(s.get("run", "")))
+ assert setter != -1, "the action no longer points UV_CACHE_DIR anywhere"
+ assert install != -1, "the action no longer runs the local install"
+ assert setter < install, (
+ "UV_CACHE_DIR is set after the install, so the install used uv's default "
+ "cache and the restored one was never read"
+ )
+
+
+def test_the_restore_happens_before_the_install_too() -> None:
+ restore = _index_of(lambda s: "cache/restore" in str(s.get("uses", "")))
+ install = _index_of(lambda s: "install.sh --local --no-torch" in str(s.get("run", "")))
+ assert restore != -1 and restore < install
+
+
+def test_a_near_miss_still_supplies_most_wheels() -> None:
+ """
+ restore-keys is what makes this worth having on a PR whose requirements moved
+ by one line. It is correct here precisely because the entry is content-
+ addressed; the same fallback on a venv cache would be a bug.
+ """
+ restore = next(s for s in _steps() if "cache/restore" in str(s.get("uses", "")))
+ assert (restore.get("with") or {}).get("restore-keys"), (
+ "no restore-keys, so any change to requirements or pyproject drops the "
+ "cache to zero instead of to almost-full"
+ )
+
+
+def test_the_cache_is_saved_on_main_only() -> None:
+ """
+ A PR-scoped entry can only be restored by re-runs of that same PR, while every
+ PR can restore from the default branch. Saving on PRs spends a budget measured
+ at 99.3% full once already, and evicts main's copy -- the one everyone reads.
+ """
+ saves = [s for s in _steps() if "cache/save" in str(s.get("uses", ""))]
+ assert saves, "the cache is never saved, so it can never be restored either"
+ for step in saves:
+ condition = str(step.get("if", ""))
+ assert (
+ "refs/heads/main" in condition
+ ), f"a cache/save step is not gated on main: if: {condition!r}"
+
+
[email protected]("name", COLD_INSTALL_WORKFLOWS)
+def test_cold_install_lanes_never_adopt_this_action(name: str) -> None:
+ """
+ These prove the installer works on a machine with nothing on it. The composite
+ writes UV_CACHE_DIR to $GITHUB_ENV, which outranks a job-level `env:` for every
+ later step, so adopting it would hand a cold lane a warm cache and the lane
+ would still report success.
+ """
+ path = WORKFLOWS / name
+ if not path.exists():
+ pytest.skip(f"{name} no longer exists")
+ assert "install-unsloth-local" not in path.read_text(encoding = "utf-8"), (
+ f"{name} uses install-unsloth-local, which warms uv's cache. A cached "
+ f"cold-install test proves nothing and still goes green."
+ )
+
+
+def test_the_action_is_actually_used() -> None:
+ """Otherwise every assertion above guards something nothing runs."""
+ users = [
+ p.name
+ for p in WORKFLOWS.glob("*.yml")
+ if "install-unsloth-local" in p.read_text(encoding = "utf-8")
+ ]
+ assert len(users) >= 5, f"only {len(users)} workflows use the action: {users}"