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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: report the real llama.cpp version for source builds (#13036)
Daniel Han56a6ecd86534
diff --git a/studio/backend/tests/test_llama_source_build_version.py b/studio/backend/tests/test_llama_source_build_version.pynew file mode 100644index 00000000000..40ec86cf185--- /dev/null+++ b/studio/backend/tests/test_llama_source_build_version.py@@ -0,0 +1,183 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""#12798: recover a --depth 1 source build's version from its bNNNN tag, on real offline shallow clones."""++from __future__ import annotations++import os+import shutil+import subprocess+import sys+from pathlib import Path++import pytest++_BACKEND = Path(__file__).resolve().parents[1]+if str(_BACKEND) not in sys.path:+ sys.path.insert(0, str(_BACKEND))++import utils.llama_cpp_update as upd # noqa: E402++pytestmark = pytest.mark.skipif(shutil.which("git") is None, reason = "git not installed")++# Answered in-process so the test runs on Windows too.+_VERSIONS: dict[str, str] = {}+_real_run = subprocess.run++[email protected](autouse = True)+def _fake_version(monkeypatch):+ def run(args, *a, **k):+ if isinstance(args, list) and args and args[0] in _VERSIONS:+ return subprocess.CompletedProcess(args, 0, stdout = "", stderr = _VERSIONS[args[0]])+ return _real_run(args, *a, **k)++ monkeypatch.setattr(upd.subprocess, "run", run)+ yield+ _VERSIONS.clear()+++LATEST_MIX = "b11408-mix-1e24fc5"+++def _git(*args, cwd = None) -> str:+ env = dict(+ os.environ,+ GIT_AUTHOR_NAME = "t",+ GIT_AUTHOR_EMAIL = "t@t",+ GIT_COMMITTER_NAME = "t",+ GIT_COMMITTER_EMAIL = "t@t",+ GIT_CONFIG_GLOBAL = os.devnull,+ GIT_CONFIG_NOSYSTEM = "1",+ )+ return subprocess.run(+ ["git", *args], cwd = cwd, env = env, check = True, capture_output = True, text = True+ ).stdout.strip()+++def _upstream(+ tmp_path: Path,+ tag: str | None,+ extra_tag: str | None = None,+) -> Path:+ repo = tmp_path / "upstream"+ repo.mkdir()+ _git("init", "-q", "-b", "master", cwd = repo)+ for i in range(3):+ (repo / "f.txt").write_text(str(i))+ _git("add", "f.txt", cwd = repo)+ _git("commit", "-q", "-m", f"c{i}", cwd = repo)+ if tag:+ _git("tag", tag, cwd = repo)+ if extra_tag:+ _git("tag", extra_tag, cwd = repo)+ return repo+++def _stub_server(root: Path, version_line: str) -> str:+ exe = root / "build" / "bin" / "llama-server"+ exe.parent.mkdir(parents = True, exist_ok = True)+ exe.write_text("")+ _VERSIONS[str(exe)] = version_line + "\n"+ return str(exe)+++def _clone_branch(tmp_path: Path, upstream: Path, ref: str) -> Path:+ # setup.sh / setup.ps1 fresh clone: git clone --depth 1 --branch <ref>+ dest = tmp_path / "llama.cpp"+ _git("clone", "-q", "--depth", "1", "--branch", ref, upstream.as_uri(), str(dest))+ return dest+++def _clone_fetch_checkout(tmp_path: Path, upstream: Path, ref: str) -> Path:+ # setup.ps1 reuse path: fetch --depth 1 origin <ref> + checkout -B unsloth-llama-build FETCH_HEAD+ dest = tmp_path / "llama.cpp"+ _git("clone", "-q", "--depth", "1", "--no-tags", upstream.as_uri(), str(dest))+ _git("-C", str(dest), "fetch", "-q", "--depth", "1", "--no-tags", "origin", ref)+ _git("-C", str(dest), "checkout", "-q", "-B", "unsloth-llama-build", "FETCH_HEAD")+ return dest+++def _short(dest: Path) -> str:+ return _git("-C", str(dest), "rev-parse", "--short=7", "HEAD")++[email protected]("layout", [_clone_branch, _clone_fetch_checkout])[email protected]("fmt", ["version: 0.6.0 (build 1, commit {c})", "version: 1 ({c})"])+def test_build_recovered_from_shallow_tag(tmp_path, layout, fmt):+ dest = layout(tmp_path, _upstream(tmp_path, "b11408"), "b11408")+ exe = _stub_server(dest, fmt.format(c = _short(dest)))+ assert upd._installed_build_number(exe) == 11408+++def test_reported_commit_mismatch_is_unknown(tmp_path):+ dest = _clone_branch(tmp_path, _upstream(tmp_path, "b11408"), "b11408")+ exe = _stub_server(dest, "version: 0.6.0 (build 1, commit deadbee)")+ assert upd._installed_build_number(exe) is None+++def test_untagged_checkout_is_unknown(tmp_path):+ dest = _clone_branch(tmp_path, _upstream(tmp_path, None), "master")+ exe = _stub_server(dest, f"version: 1 ({_short(dest)})")+ assert upd._installed_build_number(exe) is None+++def test_non_build_tag_is_unknown(tmp_path):+ dest = _clone_branch(tmp_path, _upstream(tmp_path, "v0.6.0"), "v0.6.0")+ exe = _stub_server(dest, f"version: 0.6.0 (build 1, commit {_short(dest)})")+ assert upd._installed_build_number(exe) is None+++def test_loose_tag_overrides_stale_packed_tag(tmp_path):+ # A full clone packs b11408 at HEAD; moving the tag writes a loose ref git reads instead.+ dest = tmp_path / "llama.cpp"+ _git("clone", "-q", _upstream(tmp_path, "b11408").as_uri(), str(dest))+ _git("-C", str(dest), "tag", "-f", "b11408", "HEAD~1")+ exe = _stub_server(dest, f"version: 1 ({_short(dest)})")+ assert upd._installed_build_number(exe) is None+++def test_real_build_number_does_not_read_git(tmp_path, monkeypatch):+ exe = _stub_server(tmp_path / "llama.cpp", "version: 0.6.0 (build 11420, commit abc1234)")+ monkeypatch.setattr(upd, "_checkout_tag_build", lambda *a: pytest.fail("git metadata read"))+ assert upd._installed_build_number(exe) == 11420+++def _status(monkeypatch, exe: str) -> dict:+ res = {+ "prebuilt_available": True,+ "repo": "unslothai/llama.cpp",+ "release_tag": LATEST_MIX,+ "llama_tag": "b11408",+ "asset": "app-b11408-mix-1e24fc5-linux-x64-cuda13.tar.gz",+ "install_kind": "linux-cuda",+ }+ # A pinned server path or llama.cpp dir on the host makes the tree unmanaged.+ for var in ("LLAMA_SERVER_PATH", "UNSLOTH_LLAMA_CPP_PATH"):+ monkeypatch.delenv(var, raising = False)+ monkeypatch.setattr(upd, "_find_binary", lambda: exe)+ monkeypatch.setattr(upd, "_resolve_prebuilt_for_host", lambda **_: dict(res))+ monkeypatch.setattr(upd, "latest_release_assets", lambda *a, **k: {res["asset"]: 1})+ monkeypatch.setattr(upd, "_studio_custom_path_active", lambda: False)+ monkeypatch.setattr(upd, "_active_install_is_local_link", lambda b: False)+ monkeypatch.setattr(upd, "update_checks_disabled", lambda: False)+ return upd._llama_only_status()+++ "tag, installed, offered",+ [+ ("b11408", "b11408", True), # same base: the mix still carries extra patches+ ("b11420", "b11420", False), # source newer than the prebuilt: downgrade guard+ (None, None, True), # untagged: unchanged, still offered as unknown+ ],+)+def test_source_build_status(tmp_path, monkeypatch, tag, installed, offered):+ dest = _clone_branch(tmp_path, _upstream(tmp_path, tag), tag or "master")+ exe = _stub_server(dest, f"version: 0.6.0 (build 1, commit {_short(dest)})")+ s = _status(monkeypatch, exe)+ assert s["source_build"] is True+ assert s["installed_tag"] == installed+ assert s["latest_tag"] == LATEST_MIX+ assert s["update_available"] is offereddiff --git a/studio/backend/utils/llama_cpp_update.py b/studio/backend/utils/llama_cpp_update.pyindex aba43db4cbb..29c2a478aac 100644--- a/studio/backend/utils/llama_cpp_update.py+++ b/studio/backend/utils/llama_cpp_update.py@@ -116,7 +116,7 @@ def _resolve_prebuilt_for_host(*, force_refresh: bool = False) -> Optional[dict]def _installed_build_number(binary: Optional[str]) -> Optional[int]:- """Best-effort build number from ``llama-server --version``. Current llama.cpp reports a semantic version then ``build NNNN``; older binaries put the build directly after ``version:``. None when unparseable or <= 1: a source build with no git tags reports build 1, treated as unknown (offer update)."""+ """Best-effort build number from ``llama-server --version``. Current llama.cpp reports a semantic version then ``build NNNN``; older binaries put the build directly after ``version:``. A build <= 1 (setup's ``--depth 1`` source build, #12798) is recovered from the checkout's bNNNN tag; None when that fails, treated as unknown (offer update)."""if not binary:return Nonetry:@@ -134,10 +134,74 @@ def _installed_build_number(binary: Optional[str]) -> Optional[int]:m = re.search(r"version:[^\r\n]*\bbuild\s+(\d+)\b", output)if not m:m = re.search(r"version:\s*(\d+)\b(?!\.)", output)- if not m:+ if m and int(m.group(1)) > 1:+ return int(m.group(1))+ commit = re.search(r"version:[^\r\n]*\bcommit\s+([0-9a-fA-F]{7,64})\b", output) or re.search(+ r"version:\s*\d+\s*\(([0-9a-fA-F]{7,64})\)", output+ )+ return _checkout_tag_build(binary, commit.group(1)) if commit else None+++def _checkout_tag_build(binary: str, reported_commit: str) -> Optional[int]:+ """Build number from the ``bNNNN`` tag naming the checkout's HEAD, from git files only. FETCH_HEAD is the only record setup.ps1's ``fetch --depth 1 origin bNNNN`` + ``checkout -B`` leaves. The binary's commit must prefix HEAD, so a checkout moved after the build is not misread. None on any mismatch."""+ # A loose ref replaces its packed entry, as in git.+ tags: dict[str, set[str]] = {}+ packed: dict[str, str] = {}+ try:+ git_dir = next(+ (p / ".git" for p in list(Path(binary).parents)[:5] if (p / ".git").exists()), None+ )+ if git_dir is None or not git_dir.is_dir():+ return None+ head = (git_dir / "HEAD").read_text(encoding = "utf-8").strip()+ packed_refs = git_dir / "packed-refs"+ if packed_refs.is_file():+ last_tag = None+ for line in packed_refs.read_text(encoding = "utf-8").splitlines():+ if line.startswith("^"):+ if last_tag:+ tags[last_tag].add(line[1:].strip())+ continue+ parts = line.split()+ last_tag = None+ if len(parts) == 2 and not line.startswith("#"):+ packed[parts[1]] = parts[0]+ if parts[1].startswith("refs/tags/"):+ last_tag = parts[1][len("refs/tags/") :]+ tags[last_tag] = {parts[0]}+ if head.startswith("ref:"):+ ref = head[4:].strip()+ loose = git_dir / ref+ head = (+ loose.read_text(encoding = "utf-8").strip()+ if loose.is_file()+ else packed.get(ref, "")+ )+ tags_dir = git_dir / "refs" / "tags"+ if tags_dir.is_dir():+ for f in tags_dir.iterdir():+ if f.is_file():+ tags[f.name] = {f.read_text(encoding = "utf-8").strip()}+ fetch_head = git_dir / "FETCH_HEAD"+ if fetch_head.is_file():+ for line in fetch_head.read_text(encoding = "utf-8").splitlines():+ fm = re.match(r"([0-9a-fA-F]{40,64})\t[^\t]*\ttag '([^']+)'", line)+ if fm:+ tags.setdefault(fm.group(2), set()).add(fm.group(1))+ except (OSError, UnicodeDecodeError, ValueError):+ return None+ head = head.lower()+ if not re.fullmatch(r"[0-9a-f]{40,64}", head):+ return None+ if not head.startswith(reported_commit.lower()):return None- n = int(m.group(1))- return n if n > 1 else None+ builds = [+ int(tm.group(1))+ for name, shas in tags.items()+ if head in {sha.lower() for sha in shas} and (tm := re.fullmatch(r"b([0-9]{1,9})", name))+ ]+ builds = [n for n in builds if n > 1]+ return max(builds) if builds else Nonedef get_installed_llama_version() -> Optional[str]:diff --git a/studio/backend/utils/prebuilt/freshness_flow.py b/studio/backend/utils/prebuilt/freshness_flow.pyindex d8b193e2315..9d8f717f12c 100644--- a/studio/backend/utils/prebuilt/freshness_flow.py+++ b/studio/backend/utils/prebuilt/freshness_flow.py@@ -90,7 +90,7 @@ def read_install_marker(p = Path(binary_path)marker: Optional[dict] = None# Cover all managed binary layouts (binary is 1-4 dirs deep).- for parent in p.parents[:5]:+ for parent in list(p.parents)[:5]:candidate = parent / marker_nameif candidate.is_file():try:diff --git a/studio/backend/utils/prebuilt/update_flow.py b/studio/backend/utils/prebuilt/update_flow.pyindex 62ba15fbf82..89173a25596 100644--- a/studio/backend/utils/prebuilt/update_flow.py+++ b/studio/backend/utils/prebuilt/update_flow.py@@ -185,7 +185,7 @@ def install_dir_for(binary_path: Optional[str], *, marker_name: str) -> Optionalif not binary_path:return Nonep = Path(binary_path)- for parent in p.parents[:5]:+ for parent in list(p.parents)[:5]:if (parent / marker_name).is_file():return parentreturn Nonediff --git a/studio/backend/utils/prebuilt/whisper_layout.py b/studio/backend/utils/prebuilt/whisper_layout.pyindex 7444e4e707e..c81df79bb75 100644--- a/studio/backend/utils/prebuilt/whisper_layout.py+++ b/studio/backend/utils/prebuilt/whisper_layout.py@@ -66,7 +66,7 @@ def lookup_marker(binary_path: Optional[str]) -> MarkerLookup:if slim_collision:return MarkerLookup(None, root, False, True, True)- for parent in binary.parents[:5]:+ for parent in list(binary.parents)[:5]:candidate = parent / MARKER_NAMEif candidate.is_file():marker, invalid = _parse_marker(candidate)diff --git a/studio/setup.ps1 b/studio/setup.ps1index 55016189c10..b192197238c 100644--- a/studio/setup.ps1+++ b/studio/setup.ps1@@ -10557,6 +10557,9 @@ if ($LocalLlamaCppLinked) {# -- Step A: Clone or pull llama.cpp --$UseConcreteRef = ($ResolvedSourceRef -ne "latest" -and -not [string]::IsNullOrWhiteSpace($ResolvedSourceRef))+ # --depth 1 makes llama.cpp stamp build 1 (#12798); set only once the tag is checked out.+ $TagBuildNumber = if ($ResolvedSourceRef -match '^b(\d+)$') { $Matches[1] } else { $null }+ $LlamaBuildNumber = $null# Denied must not read as "no checkout here": the fresh-clone branch ends in# a swap that recursively removes this tree and moves the temp one over it,@@ -10627,6 +10630,7 @@ if ($LocalLlamaCppLinked) {$FailedStep = "git checkout"} else {Invoke-SetupCommand -AlwaysQuiet { git -C $LlamaCppDir clean -fdx } | Out-Null+ $LlamaBuildNumber = $TagBuildNumber}}} else {@@ -10732,6 +10736,8 @@ if ($LocalLlamaCppLinked) {$BuildOk = $false$FailedStep = "git clone"if (Test-Path -LiteralPath $buildTmp) { Remove-Item -LiteralPath $buildTmp -Recurse -Force }+ } elseif ($UseConcreteRef) {+ $LlamaBuildNumber = $TagBuildNumber}}# Use temp dir for build; swap into $LlamaCppDir only after build succeeds@@ -10762,6 +10768,9 @@ if ($LocalLlamaCppLinked) {$CmakeArgs += '-DLLAMA_BUILD_EXAMPLES=OFF'$CmakeArgs += '-DLLAMA_BUILD_SERVER=ON'$CmakeArgs += '-DGGML_NATIVE=ON'+ if ($LlamaBuildNumber) {+ $CmakeArgs += "-DLLAMA_BUILD_NUMBER=$LlamaBuildNumber"+ }# HTTPS support via OpenSSLif ($OpenSslAvailable -and $OpenSslRoot) {$CmakeArgs += "-DOPENSSL_ROOT_DIR=$OpenSslRoot"diff --git a/studio/setup.sh b/studio/setup.shindex 4d6dff25501..df15e9aee22 100755--- a/studio/setup.sh+++ b/studio/setup.sh@@ -5229,6 +5229,11 @@ elseif [ "$BUILD_OK" = true ]; then# Set Release explicitly (llama.cpp only defaults to it on non-MSVC/Xcode).CMAKE_ARGS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_SERVER=ON -DGGML_NATIVE=ON $(_llama_relocatable_rpath_args)"+ # --depth 1 makes llama.cpp stamp build 1; Studio needs the tag's number (#12798).+ if [ -z "$_LLAMA_PR" ] && [ "$_RESOLVED_SOURCE_REF_KIND" != "commit" ] \+ && [[ "$_RESOLVED_SOURCE_REF" =~ ^b([0-9]+)$ ]]; then+ CMAKE_ARGS="$CMAKE_ARGS -DLLAMA_BUILD_NUMBER=${BASH_REMATCH[1]}"+ fi_TRY_METAL_CPU_FALLBACK=false_HOST_SYSTEM="$(uname -s 2>/dev/null || true)"_HOST_MACHINE="$(uname -m 2>/dev/null || true)"diff --git a/tests/studio/install/test_source_build_number_12798.py b/tests/studio/install/test_source_build_number_12798.pynew file mode 100644index 00000000000..55c90cdcf24--- /dev/null+++ b/tests/studio/install/test_source_build_number_12798.py@@ -0,0 +1,113 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""#12798: setup.sh / setup.ps1 pass -DLLAMA_BUILD_NUMBER=<N> for a bNNNN tag source build, and only then."""++import re+import shutil+import subprocess+from pathlib import Path++import pytest+from unsloth_pwsh_runner import run_pwsh++PACKAGE_ROOT = Path(__file__).resolve().parents[3]+SETUP_SH = PACKAGE_ROOT / "studio" / "setup.sh"+SETUP_PS1 = PACKAGE_ROOT / "studio" / "setup.ps1"++BASH = shutil.which("bash")+PWSH = shutil.which("pwsh")+requires_bash = pytest.mark.skipif(BASH is None, reason = "bash not available")+requires_pwsh = pytest.mark.skipif(PWSH is None, reason = "pwsh not available")++REFS = [+ ("b11408", "", "11408"),+ ("b11408", "12345", None), # a llama.cpp PR build is not the tagged release+ ("latest", "", None),+ ("master", "", None),+ ("pr-5", "", None),+ ("b11408-mix-abc", "", None),+ ("", "", None),+]+++def _sh_block() -> str:+ text = SETUP_SH.read_text(encoding = "utf-8")+ m = re.search(+ r'^[ \t]*if \[ -z "\$_LLAMA_PR" \] && \[ "\$_RESOLVED_SOURCE_REF_KIND" != "commit" \] \\\n.*?^[ \t]*fi\n',+ text,+ re.M | re.S,+ )+ assert m, "LLAMA_BUILD_NUMBER block missing from setup.sh"+ return m.group(0)+++def test_setup_sh_stamps_before_cpu_fallback_copy():+ text = SETUP_SH.read_text(encoding = "utf-8")+ base = text.index('CMAKE_ARGS="-DCMAKE_BUILD_TYPE=Release')+ stamp = text.index("-DLLAMA_BUILD_NUMBER=", base)+ copy = text.index('CPU_FALLBACK_CMAKE_ARGS="$CMAKE_ARGS"', base)+ assert base < stamp < copy+++def _run_sh_block(ref: str, pr: str, kind: str) -> str:+ script = (+ f'CMAKE_ARGS="-DBASE=1"\n_LLAMA_PR="{pr}"\n_RESOLVED_SOURCE_REF="{ref}"\n'+ f'_RESOLVED_SOURCE_REF_KIND="{kind}"\n{_sh_block()}printf "%s" "$CMAKE_ARGS"\n'+ )+ return subprocess.run([BASH, "-c", script], capture_output = True, text = True, check = True).stdout+++@requires_bash+def test_setup_sh_skips_commit_ref_that_looks_like_a_tag():+ assert _run_sh_block("b1234567", "", "commit") == "-DBASE=1"+++@requires_bash[email protected]("ref,pr,expected", REFS)+def test_setup_sh_gating(ref, pr, expected):+ out = _run_sh_block(ref, pr, "tag")+ if expected is None:+ assert out == "-DBASE=1"+ else:+ assert out == f"-DBASE=1 -DLLAMA_BUILD_NUMBER={expected}"+++def test_setup_ps1_sets_number_only_after_concrete_checkout():+ text = SETUP_PS1.read_text(encoding = "utf-8")+ assert "$LlamaBuildNumber = $null" in text+ sets = [m.start() for m in re.finditer(r"\$LlamaBuildNumber = \$TagBuildNumber", text)]+ assert len(sets) == 2+ reuse = text.index("} elseif ($UseConcreteRef) {")+ fetch_failed = text.index('substep "git fetch failed -- using existing source"', reuse)+ clean = text.index("git -C $LlamaCppDir clean -fdx", fetch_failed)+ assert fetch_failed < clean < sets[0] < text.index("} else {", sets[0])+ fresh = text.index('$cloneArgs += @("--branch", $ResolvedSourceRef)')+ assert fresh < sets[1]+ assert re.search(+ r"\} elseif \(\$UseConcreteRef\) \{\s*\$LlamaBuildNumber = \$TagBuildNumber", text[fresh:]+ )+ assert re.search(+ r"if \(\$LlamaBuildNumber\) \{\s*\$CmakeArgs \+= \"-DLLAMA_BUILD_NUMBER=\$LlamaBuildNumber\"",+ text,+ )+++@requires_pwsh+# PR builds never reach the assignment sites (checked above).[email protected]("ref,_pr,expected", [r for r in REFS if not r[1]])+def test_setup_ps1_tag_parse(ref, _pr, expected):+ line = next(+ line.strip()+ for line in SETUP_PS1.read_text(encoding = "utf-8").splitlines()+ if line.strip().startswith("$TagBuildNumber = ")+ )+ script = f"$ResolvedSourceRef = '{ref}'\n{line}\nif ($null -eq $TagBuildNumber) {{ 'NULL' }} else {{ $TagBuildNumber }}"+ out = run_pwsh(+ [PWSH, "-NoProfile", "-Command", script],+ capture_output = True,+ text = True,+ check = True,+ timeout = 120,+ ).stdout.strip()+ assert out == (expected or "NULL")