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: show download progress during desktop updates (#11291)
Nilayb2bd3b8974bb
diff --git a/.github/workflows/cross-platform-parity-ci.yml b/.github/workflows/cross-platform-parity-ci.ymlindex 25930cf2a3c..0aacf596391 100644--- a/.github/workflows/cross-platform-parity-ci.yml+++ b/.github/workflows/cross-platform-parity-ci.yml@@ -75,6 +75,7 @@ on:- 'tests/studio/test_path_resolver_degrades.ps1'- 'tests/python/test_windows_python_venv_hardening.py'- 'tests/python/test_windows_setup_output_encoding.py'+ - 'tests/python/test_windows_setup_download_progress.py'- 'tests/python/test_windows_installer_resolver_fallback.py'- 'tests/python/test_installer_helper_declaration_order.py'- 'tests/python/test_installer_embedded_nvidia_probe.py'@@ -195,6 +196,7 @@ on:- 'tests/studio/test_path_resolver_degrades.ps1'- 'tests/python/test_windows_python_venv_hardening.py'- 'tests/python/test_windows_setup_output_encoding.py'+ - 'tests/python/test_windows_setup_download_progress.py'- 'tests/python/test_windows_installer_resolver_fallback.py'- 'tests/python/test_installer_helper_declaration_order.py'- 'tests/python/test_installer_embedded_nvidia_probe.py'@@ -334,6 +336,7 @@ jobs:tests/python/test_cross_platform_parity.pytests/python/test_windows_python_venv_hardening.pytests/python/test_windows_setup_output_encoding.py+ tests/python/test_windows_setup_download_progress.pytests/python/test_windows_installer_resolver_fallback.pytests/python/test_installer_helper_declaration_order.pytests/python/test_pwsh_runner_encoding.pydiff --git a/studio/frontend/src/components/tauri/backend-download-progress.ts b/studio/frontend/src/components/tauri/backend-download-progress.tsnew file mode 100644index 00000000000..5c7cb302dda--- /dev/null+++ b/studio/frontend/src/components/tauri/backend-download-progress.ts@@ -0,0 +1,27 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++const DOWNLOAD_LINE = /^Downloading (.+):\s+(.+)$/;+const KNOWN_SIZE = /^(\d+(?:\.\d+)?)% \(.+\) at .+\/s$/;+const UNKNOWN_SIZE = /^.+ downloaded at .+\/s$/;++export function parseBackendDownloadProgress(line: string) {+ const match = DOWNLOAD_LINE.exec(line.trim());+ if (!match) {+ return null;+ }++ const [, file, detail] = match;+ const knownSize = KNOWN_SIZE.exec(detail);+ if (knownSize) {+ const percent = Number(knownSize[1]);+ if (percent > 100) {+ return null;+ }+ return { file, detail, percent };+ }+ if (UNKNOWN_SIZE.test(detail)) {+ return { file, detail, percent: null };+ }+ return null;+}diff --git a/studio/frontend/src/components/tauri/update-screen.tsx b/studio/frontend/src/components/tauri/update-screen.tsxindex dadf2f8b0a8..163eeb812e0 100644--- a/studio/frontend/src/components/tauri/update-screen.tsx+++ b/studio/frontend/src/components/tauri/update-screen.tsx@@ -1,6 +1,7 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+import { parseBackendDownloadProgress } from "@/components/tauri/backend-download-progress";import { DiagnosticsCopyActions } from "@/components/tauri/diagnostics-copy-actions";import { LogDetails } from "@/components/tauri/log-details";import { Button } from "@/components/ui/button";@@ -81,6 +82,12 @@ export function UpdateScreen({onCopyDiagnostics,}: UpdateScreenProps) {const isError = status === "error";+ const backendDownload =+ status === "updating-backend"+ ? parseBackendDownloadProgress(logs.at(-1) ?? "")+ : null;+ const downloadProgress =+ status === "downloading" ? progress : backendDownload?.percent;return (<div className="box-border flex h-full w-full flex-col items-center overflow-y-auto bg-background pb-6 pt-[var(--studio-startup-top-inset,0px)]">@@ -110,12 +117,31 @@ export function UpdateScreen({{statusSubtext(status, progress)}</p>- {status === "downloading" && (+ {backendDownload && (+ <div className="mt-2 w-full max-w-sm text-xs text-muted-foreground">+ <p className="break-words">+ Downloading {backendDownload.file}+ </p>+ <p>{backendDownload.detail}</p>+ </div>+ )}++ {downloadProgress != null && (<div className="mt-2 h-1.5 w-full max-w-xs overflow-hidden rounded-full bg-muted">+ <progress+ aria-label={+ backendDownload+ ? `Downloading ${backendDownload.file}`+ : "Downloading app update"+ }+ max={100}+ value={downloadProgress}+ className="sr-only"+ /><motion.divclassName="h-full rounded-full bg-primary"initial={{ width: 0 }}- animate={{ width: `${progress}%` }}+ animate={{ width: `${downloadProgress}%` }}transition={{ duration: 0.3 }}/></div>diff --git a/studio/frontend/tests/backend-download-progress.test.ts b/studio/frontend/tests/backend-download-progress.test.tsnew file mode 100644index 00000000000..e5c9b7ce12d--- /dev/null+++ b/studio/frontend/tests/backend-download-progress.test.ts@@ -0,0 +1,133 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";+import * as motion from "motion/react";+import * as React from "react";+import { renderToStaticMarkup } from "react-dom/server";+import * as jsxRuntime from "react/jsx-runtime";+import { parseBackendDownloadProgress } from "../src/components/tauri/backend-download-progress.ts";+import type * as UpdateScreenModule from "../src/components/tauri/update-screen.tsx";+import { loadWithStubs } from "./helpers/module-stubs.ts";++const { UpdateScreen } = loadWithStubs<typeof UpdateScreenModule>(+ new URL("../src/components/tauri/update-screen.tsx", import.meta.url),+ {+ "react/jsx-runtime": jsxRuntime,+ "motion/react": motion,+ "@/components/tauri/backend-download-progress": {+ parseBackendDownloadProgress,+ },+ "@/components/tauri/diagnostics-copy-actions": {+ DiagnosticsCopyActions: () => null,+ },+ "@/components/tauri/log-details": { LogDetails: () => null },+ "@/components/ui/button": { Button: () => null },+ "@/components/ui/spinner": { Spinner: () => null },+ },+);++function render(+ status: React.ComponentProps<typeof UpdateScreen>["status"],+ logs: string[],+) {+ return renderToStaticMarkup(+ React.createElement(UpdateScreen, {+ status,+ logs,+ progress: 37,+ error: null,+ onRetry() {},+ onSkipRestart() {},+ onCopyDiagnostics: async () => ({+ ok: true,+ report: "",+ source: "tauri" as const,+ }),+ }),+ );+}++test("reads the downloader's percentage, bytes and speed", () => {+ assert.deepEqual(+ parseBackendDownloadProgress(+ "Downloading llama.cpp-source.tar.gz: 77.6% (28.0 MiB/36.1 MiB) at 118.4 KiB/s",+ ),+ {+ file: "llama.cpp-source.tar.gz",+ detail: "77.6% (28.0 MiB/36.1 MiB) at 118.4 KiB/s",+ percent: 77.6,+ },+ );+});++test("unknown-length downloads show bytes without inventing a percentage", () => {+ assert.deepEqual(+ parseBackendDownloadProgress(+ "Downloading whisper.tar.gz: 25.0 MiB downloaded at 2.0 MiB/s",+ ),+ {+ file: "whisper.tar.gz",+ detail: "25.0 MiB downloaded at 2.0 MiB/s",+ percent: null,+ },+ );+});++test("completion is per file and a later step clears download progress", () => {+ const lines = [+ "Downloading node.tar.gz: 100.0% (30.0 MiB/30.0 MiB) at 2.0 MiB/s",+ " node v24.18.0",+ "Downloading llama.tar.gz: 25.0% (10.0 MiB/40.0 MiB) at 2.0 MiB/s",+ " llama.cpp prebuilt installed and validated",+ ];+ assert.deepEqual(+ lines.map((line) => parseBackendDownloadProgress(line)?.percent ?? null),+ [100, null, 25, null],+ );+});++test("unrelated output and malformed percentages cannot become progress", () => {+ for (const line of [+ "",+ "existing install detected -- validating update",+ "download failed (1/3)",+ "Downloading llama.tar.gz: preparing",+ "Downloading llama.tar.gz: 101% (41 MiB/40 MiB) at 1 MiB/s",+ "Downloading llama.tar.gz: -1% (0 MiB/40 MiB) at 1 MiB/s",+ ]) {+ assert.equal(parseBackendDownloadProgress(line), null, line);+ }+});++test("the update screen shows live backend progress without opening details", () => {+ const html = render("updating-backend", [+ "Downloading llama.tar.gz: 50.0% (20.0 MiB/40.0 MiB) at 2.0 MiB/s",+ ]);+ assert.match(html, /Downloading llama.tar.gz/);+ assert.match(html, /20.0 MiB\/40.0 MiB/);+ assert.match(html, /<progress[^>]*value="50"/);+});++test("later steps clear the backend bar and shell downloads use their own percentage", () => {+ const logs = [+ "Downloading llama.tar.gz: 100.0% (40.0 MiB/40.0 MiB) at 2.0 MiB/s",+ ];+ assert.doesNotMatch(+ render("updating-backend", [...logs, "prebuilt installed"]),+ /<progress/,+ );+ assert.doesNotMatch(render("error", logs), /<progress/);+ assert.doesNotMatch(render("installing", logs), /<progress/);+ assert.match(render("downloading", logs), /<progress[^>]*value="37"/);+ assert.doesNotMatch(render("downloading", logs), /Downloading llama/);+});++test("unknown-size downloads show their bytes and speed without a bar", () => {+ const html = render("updating-backend", [+ "Downloading whisper.tar.gz: 25.0 MiB downloaded at 2.0 MiB/s",+ ]);+ assert.match(html, /25.0 MiB downloaded at 2.0 MiB\/s/);+ assert.doesNotMatch(html, /<progress/);+});diff --git a/studio/install_node_prebuilt.py b/studio/install_node_prebuilt.pyindex 6451f59d73d..be2802efd8b 100644--- a/studio/install_node_prebuilt.py+++ b/studio/install_node_prebuilt.py@@ -45,6 +45,12 @@ from dataclasses import dataclassfrom pathlib import Pathfrom typing import Iterator+_STUDIO_DIR = str(Path(__file__).resolve().parent)+if _STUDIO_DIR not in sys.path:+ sys.path.insert(0, _STUDIO_DIR)++from prebuilt_core import DownloadProgress # noqa: E402+try:from filelock import FileLock, Timeout as FileLockTimeoutexcept ImportError:@@ -313,11 +319,23 @@ def download_file(url: str, destination: Path) -> None:) as handle:tmp_path = Path(handle.name)with urllib.request.urlopen(request, timeout = 120) as response:+ content_length = response.headers.get("Content-Length")+ total_bytes = (+ int(content_length) if content_length and content_length.isdigit() else None+ )+ progress = DownloadProgress(f"Downloading {destination.name}", total_bytes)+ if _LOG_TO_STDOUT:+ progress.stream = sys.stdout+ progress.is_tty = progress.is_tty and sys.stdout.isatty()+ downloaded_bytes = 0while True:chunk = response.read(1024 * 1024)if not chunk:breakhandle.write(chunk)+ downloaded_bytes += len(chunk)+ progress.update(downloaded_bytes)+ progress.finish(downloaded_bytes)handle.flush()os.fsync(handle.fileno())if not tmp_path.exists() or tmp_path.stat().st_size == 0:diff --git a/studio/setup.ps1 b/studio/setup.ps1index 48a4b7ee52a..7c5dda269b3 100644--- a/studio/setup.ps1+++ b/studio/setup.ps1@@ -123,6 +123,19 @@ function Write-StudioLine {}}+function Show-DownloadProgress {+ param([Parameter(ValueFromPipeline = $true)][object]$InputObject)+ process {+ $line = [string]$InputObject+ if ((@("1", "true") -contains $env:UNSLOTH_TAURI_UPDATE) -and+ ($line -like 'Downloading *: *% (*) at */s' -or+ $line -like 'Downloading *: * downloaded at */s')) {+ Write-StudioLine $line+ }+ $InputObject+ }+}+# --------------------------------------------------------------------------# Maintainer-editable defaults# Change these in the GitHub-hosted script so users get updated defaults.@@ -5668,11 +5681,11 @@ if ($NeedNodeForSetup) {substep "installing isolated Node (system Node/npm left untouched)..."# Prefer the validated handed-off/venv Python: bare `python` may be a Store stub here.$NodeInstallPython = if ($ValidatedSetupPython) { $ValidatedSetupPython } else { "python" }- $nodeOut = & $NodeInstallPython "$PSScriptRoot\install_node_prebuilt.py" --install-dir $NodeDir 2>&1 | Out-String+ $nodeOut = & $NodeInstallPython "$PSScriptRoot\install_node_prebuilt.py" --install-dir $NodeDir 2>&1 | Show-DownloadProgress | Out-String$nodeExit = $LASTEXITCODE# A failed download gets one retry through the mirror; 3 is another install holding the lock and 4 an unwritable directory, which no mirror fixes.if ($nodeExit -notin 0, 3, 4 -and (Use-MirrorSpare node)) {- $nodeOut += & $NodeInstallPython "$PSScriptRoot\install_node_prebuilt.py" --install-dir $NodeDir 2>&1 | Out-String+ $nodeOut += & $NodeInstallPython "$PSScriptRoot\install_node_prebuilt.py" --install-dir $NodeDir 2>&1 | Show-DownloadProgress | Out-String$nodeExit = $LASTEXITCODE}if ($nodeExit -eq 3) {@@ -9888,7 +9901,7 @@ if ($LocalLlamaCppLinked) {$prebuiltOutput = if (Test-Path $prebuiltLog) { Get-Content $prebuiltLog -Raw } else { "" }Remove-Item $prebuiltLog -ErrorAction SilentlyContinue} else {- $prebuiltOutput = & python @prebuiltArgs 2>&1 | Out-String+ $prebuiltOutput = & python @prebuiltArgs 2>&1 | Show-DownloadProgress | Out-String$prebuiltExit = $LASTEXITCODE}} finally {@@ -10027,7 +10040,7 @@ if ($env:WHISPER_SERVER_PATH -or $env:UNSLOTH_WHISPER_CPP_PATH) {$restoreNativeErrorPreferenceW = $true}try {- $whisperOutput = & python @whisperArgs 2>&1 | Out-String+ $whisperOutput = & python @whisperArgs 2>&1 | Show-DownloadProgress | Out-String$whisperExit = $LASTEXITCODE} finally {if ($restoreNativeErrorPreferenceW) {diff --git a/studio/setup.sh b/studio/setup.shindex 787e81229c1..32402279550 100755--- a/studio/setup.sh+++ b/studio/setup.sh@@ -96,6 +96,21 @@ _is_verbose() {[ "${UNSLOTH_VERBOSE:-0}" = "1" ]}+_filter_download_output() {+ if _is_verbose; then+ cat+ return+ fi+ local line+ while IFS= read -r line || [ -n "$line" ]; do+ case "$line" in+ "Downloading "*": "*"% ("*") at "*"/s"|"Downloading "*": "*" downloaded at "*"/s")+ printf '%s\n' "$line"+ ;;+ esac+ done+}+verbose_substep() {if _is_verbose; thensubstep "$1"@@ -2216,8 +2231,8 @@ elif [ "$NODE_SOURCE" = bundled ]; then_NODE_LOG="$(mktemp)"set +efor _node_try in default mirror; do- if _is_verbose; then- "$_NODE_PY" "$SCRIPT_DIR/install_node_prebuilt.py" --install-dir "$NODE_DIR" 2>&1 | tee -a "$_NODE_LOG"+ if _is_verbose || [ "${UNSLOTH_TAURI_UPDATE:-0}" = "1" ] || [ "${UNSLOTH_TAURI_UPDATE:-0}" = "true" ]; then+ "$_NODE_PY" "$SCRIPT_DIR/install_node_prebuilt.py" --install-dir "$NODE_DIR" 2>&1 | tee -a "$_NODE_LOG" | _filter_download_output_NODE_STATUS=${PIPESTATUS[0]}else"$_NODE_PY" "$SCRIPT_DIR/install_node_prebuilt.py" --install-dir "$NODE_DIR" >>"$_NODE_LOG" 2>&1@@ -4832,8 +4847,8 @@ elseesac_PREBUILT_LOG="$(mktemp)"set +e- if _is_verbose; then- "${_PREBUILT_CMD[@]}" 2>&1 | tee "$_PREBUILT_LOG"+ if _is_verbose || [ "${UNSLOTH_TAURI_UPDATE:-0}" = "1" ] || [ "${UNSLOTH_TAURI_UPDATE:-0}" = "true" ]; then+ "${_PREBUILT_CMD[@]}" 2>&1 | tee "$_PREBUILT_LOG" | _filter_download_output_PREBUILT_STATUS=${PIPESTATUS[0]}else"${_PREBUILT_CMD[@]}" >"$_PREBUILT_LOG" 2>&1@@ -5617,8 +5632,8 @@ elsefi_WHISPER_LOG="$(mktemp)"set +e- if _is_verbose; then- "${_WHISPER_CMD[@]}" 2>&1 | tee "$_WHISPER_LOG"+ if _is_verbose || [ "${UNSLOTH_TAURI_UPDATE:-0}" = "1" ] || [ "${UNSLOTH_TAURI_UPDATE:-0}" = "true" ]; then+ "${_WHISPER_CMD[@]}" 2>&1 | tee "$_WHISPER_LOG" | _filter_download_output_WHISPER_STATUS=${PIPESTATUS[0]}else"${_WHISPER_CMD[@]}" >"$_WHISPER_LOG" 2>&1diff --git a/studio/src-tauri/src/update.rs b/studio/src-tauri/src/update.rsindex 24a74d195f5..3a315d0f304 100644--- a/studio/src-tauri/src/update.rs+++ b/studio/src-tauri/src/update.rs@@ -69,6 +69,7 @@ fn configure_tauri_update_environment(cmd: &mut Command) {cmd.env_remove("UNSLOTH_STUDIO_HOME");cmd.env_remove("STUDIO_HOME");cmd.env("UNSLOTH_TAURI_UPDATE", "1");+ cmd.env("UNSLOTH_PROGRESS_PERCENT_STEP", "5");cmd.env("SKIP_STUDIO_FRONTEND", "1");cmd.env("UNSLOTH_DESKTOP_BACKEND_VERSION",@@ -591,7 +592,11 @@ mod tests {.get_envs().any(|(key, value)| key == OsStr::new(name) && value.is_none()));}- for (name, expected) in [("UNSLOTH_TAURI_UPDATE", "1"), ("SKIP_STUDIO_FRONTEND", "1")] {+ for (name, expected) in [+ ("UNSLOTH_TAURI_UPDATE", "1"),+ ("UNSLOTH_PROGRESS_PERCENT_STEP", "5"),+ ("SKIP_STUDIO_FRONTEND", "1"),+ ] {assert!(cmd.get_envs().any(|(key, value)| {key == OsStr::new(name) && value == Some(OsStr::new(expected))}));diff --git a/tests/python/test_windows_setup_download_progress.py b/tests/python/test_windows_setup_download_progress.pynew file mode 100644index 00000000000..7aa401114e6--- /dev/null+++ b/tests/python/test_windows_setup_download_progress.py@@ -0,0 +1,206 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++import hashlib+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+import os+from pathlib import Path+import queue+import re+import shutil+import subprocess+import sys+import threading++import pytest+++ROOT = Path(__file__).resolve().parents[2]+SHELLS = [shell for name in ("powershell", "pwsh") if (shell := shutil.which(name))]+PROGRESS = "Downloading runtime.zip: 25.0% (1.0 MiB/4.0 MiB) at 1.0 MiB/s"+UNKNOWN_PROGRESS = "Downloading runtime.zip: 25.0 MiB downloaded at 1.0 MiB/s"+DIAGNOSTICS = ["resolving release", "Downloading runtime.zip: retrying", "validating runtime"]+++def setup_fragment(component):+ source = (ROOT / "studio/setup.ps1").read_text(encoding = "utf-8")+ encoding = source[+ source.index("$_UnslothUtf8NoBom =") : source.index("function Write-StudioLine")+ ]+ helpers = "\n".join(+ re.search(rf"^function {name} \{{.*?^\}}", source, re.M | re.S).group()+ for name in ("Write-StudioLine", "Show-DownloadProgress")+ )+ if component == "node":+ block = re.search(r"\$nodeOut = & .*\n\s*\$nodeExit = \$LASTEXITCODE", source).group()+ if os.name != "nt":+ block = block.replace("$PSScriptRoot\\", "$PSScriptRoot/")+ output, status = "$nodeOut", "$nodeExit"+ else:+ name = "Prebuilt" if component == "llama" else "Whisper"+ start = source.index(f"$prevEAP{name} =")+ end = source.index(f"$ErrorActionPreference = $prevEAP{name}", start)+ block = source[start:end] + f"$ErrorActionPreference = $prevEAP{name}"+ output = "$prebuiltOutput" if component == "llama" else "$whisperOutput"+ status = "$prebuiltExit" if component == "llama" else "$whisperExit"+ return encoding + helpers, block, output, status+++def start_installer(tmp_path, shell, component, mode, child_source):+ child = tmp_path / "install_node_prebuilt.py"+ child.write_text(child_source, encoding = "utf-8")+ helpers, block, output, status = setup_fragment(component)+ script = tmp_path / "run.ps1"+ script.write_text(+ "param([string]$Python, [string]$WorkDir)\n"+ "$ErrorActionPreference = 'Stop'\n" + helpers + "\n"+ "$script:UnslothVerbose = ($env:UNSLOTH_VERBOSE -eq '1')\n"+ "$NodeInstallPython = $Python\n$NodeDir = $WorkDir\n"+ "$prebuiltArgs = @((Join-Path $WorkDir 'install_node_prebuilt.py'))\n"+ "$whisperArgs = $prebuiltArgs\n" + block + "\n"+ f"[IO.File]::WriteAllText((Join-Path $WorkDir 'captured.log'), {output})\n"+ f"exit {status}\n",+ encoding = "utf-8",+ )+ env = dict(os.environ, TEMP = str(tmp_path), RELEASE_FILE = str(tmp_path / "release"))+ env["PATH"] = str(Path(sys.executable).parent) + os.pathsep + env["PATH"]+ env["UNSLOTH_TAURI_UPDATE"] = mode if mode in ("1", "true") else "0"+ env["UNSLOTH_VERBOSE"] = "1" if mode == "verbose" else "0"+ process = subprocess.Popen(+ [+ shell,+ "-NoLogo",+ "-NoProfile",+ "-NonInteractive",+ "-ExecutionPolicy",+ "Bypass",+ "-File",+ str(script),+ sys.executable,+ str(tmp_path),+ ],+ stdout = subprocess.PIPE,+ stderr = subprocess.STDOUT,+ text = True,+ encoding = "utf-8",+ env = env,+ creationflags = subprocess.CREATE_NO_WINDOW+ if sys.platform == "win32" and mode != "verbose"+ else 0,+ )+ lines = queue.Queue()++ def read_lines():+ for line in process.stdout:+ lines.put(line.rstrip("\r\n"))++ reader = threading.Thread(target = read_lines, daemon = True)+ reader.start()+ return process, lines, reader++[email protected]("shell", SHELLS or [None])[email protected]("component", ["node", "llama", "whisper"])[email protected]("mode", ["1", "true", "quiet", "verbose"])[email protected]("exit_code", [0, 7])+def test_windows_download_output(tmp_path, shell, component, mode, exit_code):+ if shell is None:+ pytest.skip("PowerShell is unavailable")+ child = (+ "import os, sys, time\nfrom pathlib import Path\n"+ f"print({DIAGNOSTICS[0]!r}, flush=True)\n"+ f"print({PROGRESS!r}, flush=True)\n"+ f"print({DIAGNOSTICS[1]!r}, flush=True)\n"+ f"print({UNKNOWN_PROGRESS!r}, file=sys.{'stdout' if component == 'node' else 'stderr'}, flush=True)\n"+ "deadline = time.monotonic() + 30\n"+ "while not Path(os.environ['RELEASE_FILE']).exists() and time.monotonic() < deadline: time.sleep(0.01)\n"+ f"print({DIAGNOSTICS[2]!r}, flush=True)\n"+ f"raise SystemExit({exit_code})\n"+ )+ process, lines, reader = start_installer(tmp_path, shell, component, mode, child)+ received = []+ try:+ if mode in ("1", "true"):+ while PROGRESS not in received or UNKNOWN_PROGRESS not in received:+ received.append(lines.get(timeout = 15))+ assert process.poll() is None+ finally:+ (tmp_path / "release").touch()+ process.wait(timeout = 20)+ reader.join(timeout = 5)+ process.stdout.close()+ while not lines.empty():+ received.append(lines.get_nowait())+ assert process.returncode == exit_code, "\n".join(received)+ if mode in ("1", "true"):+ assert sorted(received) == sorted([PROGRESS, UNKNOWN_PROGRESS])+ elif mode == "verbose" and component == "llama":+ assert all(line in "\n".join(received) for line in DIAGNOSTICS)+ else:+ assert received == []+ captured = (tmp_path / "captured.log").read_text(encoding = "utf-8")+ assert all(line in captured for line in [PROGRESS, UNKNOWN_PROGRESS, *DIAGNOSTICS])++[email protected]("shell", SHELLS or [None])[email protected]("component", ["node", "llama", "whisper"])+def test_windows_real_download_reports_progress_while_active(+ tmp_path, shell, component, monkeypatch+):+ if shell is None:+ pytest.skip("PowerShell is unavailable")+ monkeypatch.setenv("UNSLOTH_PROGRESS_PERCENT_STEP", "5")+ payload = b"x" * (20 * 1024 * 1024)+ release = threading.Event()++ class Handler(BaseHTTPRequestHandler):+ def do_GET(self):+ self.send_response(200)+ self.send_header("Content-Length", str(len(payload)))+ self.end_headers()+ self.wfile.write(payload[: len(payload) // 5])+ self.wfile.flush()+ release.wait(30)+ self.wfile.write(payload[len(payload) // 5 :])++ def log_message(self, *args):+ pass++ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ worker = threading.Thread(target = server.serve_forever, daemon = True)+ worker.start()+ archive = tmp_path / "runtime.zip"+ child = (+ "import sys\nfrom pathlib import Path\n"+ f"sys.path.insert(0, {str(ROOT / 'studio')!r})\n"+ f"import install_{component}_prebuilt as installer\n"+ "installer._LOG_TO_STDOUT = True\n"+ "print('diagnostic before download', flush=True)\n"+ f"installer.download_file('http://127.0.0.1:{server.server_port}/archive', Path({str(archive)!r}))\n"+ "print('diagnostic after download', flush=True)\n"+ )+ process, lines, reader = start_installer(tmp_path, shell, component, "1", child)+ received = []+ try:+ while not any("20.0%" in line for line in received):+ received.append(lines.get(timeout = 15))+ assert [float(line.split(": ")[1].split("%")[0]) for line in received] == [5, 10, 15, 20]+ assert process.poll() is None+ assert not archive.exists()+ finally:+ release.set()+ process.wait(timeout = 20)+ reader.join(timeout = 5)+ process.stdout.close()+ server.shutdown()+ server.server_close()+ worker.join(timeout = 5)+ while not lines.empty():+ received.append(lines.get_nowait())+ assert process.returncode == 0, "\n".join(received)+ assert all(line.startswith("Downloading runtime.zip: ") for line in received)+ assert any("100.0%" in line for line in received)+ assert hashlib.sha256(archive.read_bytes()).digest() == hashlib.sha256(payload).digest()+ captured = (tmp_path / "captured.log").read_text(encoding = "utf-8")+ assert "diagnostic before download" in captured+ assert "diagnostic after download" in captureddiff --git a/tests/studio/install/test_install_node_prebuilt_logic.py b/tests/studio/install/test_install_node_prebuilt_logic.pyindex 191459ffdcf..e58196fa701 100644--- a/tests/studio/install/test_install_node_prebuilt_logic.py+++ b/tests/studio/install/test_install_node_prebuilt_logic.py@@ -33,6 +33,29 @@ def _no_ambient_node_mirror(monkeypatch):monkeypatch.delenv(M.NODE_MIRROR_ENV, raising = False)[email protected]("content_length", ["2097152", None])+def test_download_file_reports_progress(tmp_path, monkeypatch, capsys, content_length):+ from io import BytesIO++ payload = b"x" * (2 * 1024 * 1024)+ response = BytesIO(payload)+ response.headers = {"Content-Length": content_length}+ monkeypatch.setattr(M.urllib.request, "urlopen", lambda *args, **kwargs: response)+ monkeypatch.setattr(M, "_LOG_TO_STDOUT", True)+ destination = tmp_path / "node.zip"+ M.download_file("https://nodejs.org/node.zip", destination)+ output = capsys.readouterr()+ assert output.err == ""+ assert "Downloading node.zip:" in output.out+ if content_length:+ assert "50.0% (1.0 MiB/2.0 MiB)" in output.out+ assert "100.0% (2.0 MiB/2.0 MiB)" in output.out+ else:+ assert "2.0 MiB downloaded at" in output.out+ assert "%" not in output.out+ assert destination.read_bytes() == payload++def _host(node_os: str, node_arch: str) -> HostInfo:ext = ".zip" if node_os == "win" else ".tar.gz"return HostInfo(diff --git a/tests/studio/install/test_setup_download_progress.py b/tests/studio/install/test_setup_download_progress.pynew file mode 100644index 00000000000..277352879b4--- /dev/null+++ b/tests/studio/install/test_setup_download_progress.py@@ -0,0 +1,120 @@+# SPDX-License-Identifier: AGPL-3.0-only++import os+from pathlib import Path+import queue+import re+import shutil+import subprocess+import sys+import threading++import pytest+++ROOT = Path(__file__).resolve().parents[3]+PROGRESS = "Downloading runtime.tar.gz: 25.0% (1.0 MiB/4.0 MiB) at 1.0 MiB/s"+UNKNOWN_PROGRESS = "Downloading runtime.tar.gz: 25.0 MiB downloaded at 1.0 MiB/s"+DIAGNOSTICS = ["resolving release", "Downloading runtime.tar.gz: retrying", "validating runtime"]+pytestmark = pytest.mark.skipif(+ os.name == "nt" or shutil.which("bash") is None,+ reason = "Exercises POSIX setup.sh on macOS and Linux",+)+++def run_download(tmp_path, component, env, exit_code):+ source = (ROOT / "studio/setup.sh").read_text(encoding = "utf-8")+ start = source.index(f' _{component}_LOG="$(mktemp)"')+ end = source.index(" set -e", start) + len(" set -e")+ verbose = re.search(r"_is_verbose\(\) \{.*?\n\}", source, re.S).group()+ output_filter = re.search(r"_filter_download_output\(\) \{.*?\n\}", source, re.S).group()+ child = tmp_path / "install_node_prebuilt.py"+ child.write_text(+ "import os, sys, time\n"+ "from pathlib import Path\n"+ f"print({DIAGNOSTICS[0]!r}, file=sys.stderr, flush=True)\n"+ f"print({PROGRESS!r}, flush=True)\n"+ f"print({DIAGNOSTICS[1]!r}, flush=True)\n"+ f"print({UNKNOWN_PROGRESS!r}, file=sys.stderr, flush=True)\n"+ "while not Path(os.environ['RELEASE_FILE']).exists(): time.sleep(0.01)\n"+ f"print({DIAGNOSTICS[2]!r}, flush=True)\n"+ f"raise SystemExit({exit_code})\n"+ )+ script = tmp_path / "run.sh"+ script.write_text(+ verbose+ + "\n"+ + output_filter+ + '\n_mirror_switch() { return 1; }\n_NODE_PY="$1"\nSCRIPT_DIR="$2"\nNODE_DIR="$2"\n'+ '_PREBUILT_CMD=("$1" "$2/install_node_prebuilt.py")\n'+ '_WHISPER_CMD=("${_PREBUILT_CMD[@]}")\n'+ + source[start:end]+ + f'\ncp "$_{component}_LOG" "$2/captured.log"\n'+ + f'rm -f "$_{component}_LOG"\nexit "$_{component}_STATUS"\n'+ )+ release = tmp_path / "release"+ child_env = dict(os.environ, RELEASE_FILE = str(release), TMPDIR = str(tmp_path))+ child_env.pop("UNSLOTH_VERBOSE", None)+ child_env.pop("UNSLOTH_TAURI_UPDATE", None)+ child_env.update(env)+ process = subprocess.Popen(+ ["bash", str(script), sys.executable, str(tmp_path)],+ stdout = subprocess.PIPE,+ stderr = subprocess.STDOUT,+ text = True,+ env = child_env,+ )+ return process, release++[email protected]("component", ["NODE", "PREBUILT", "WHISPER"])+ "env",+ [{"UNSLOTH_TAURI_UPDATE": "1"}, {"UNSLOTH_TAURI_UPDATE": "true"}, {"UNSLOTH_VERBOSE": "1"}],+)[email protected]("exit_code", [0, 7])+def test_download_progress_arrives_before_the_installer_exits(tmp_path, component, env, exit_code):+ process, release = run_download(tmp_path, component, env, exit_code)+ lines = queue.Queue()++ def read_lines():+ for line in process.stdout:+ lines.put(line.strip())++ reader = threading.Thread(target = read_lines, daemon = True)+ reader.start()+ received = []+ try:+ while UNKNOWN_PROGRESS not in received:+ received.append(lines.get(timeout = 5))+ assert PROGRESS in received+ assert process.poll() is None+ finally:+ release.touch()+ process.wait(timeout = 5)+ reader.join(timeout = 5)+ process.stdout.close()+ while not lines.empty():+ received.append(lines.get_nowait())+ assert process.returncode == exit_code+ all_output = [DIAGNOSTICS[0], PROGRESS, DIAGNOSTICS[1], UNKNOWN_PROGRESS, DIAGNOSTICS[2]]+ assert received == (+ all_output if env.get("UNSLOTH_VERBOSE") == "1" else [PROGRESS, UNKNOWN_PROGRESS]+ )+ assert (tmp_path / "captured.log").read_text().splitlines() == all_output++[email protected]("component", ["NODE", "PREBUILT", "WHISPER"])+def test_terminal_update_stays_quiet(tmp_path, component):+ process, release = run_download(tmp_path, component, {}, 0)+ release.touch()+ output, _ = process.communicate(timeout = 5)+ assert process.returncode == 0+ assert output == ""+ assert (tmp_path / "captured.log").read_text().splitlines() == [+ DIAGNOSTICS[0],+ PROGRESS,+ DIAGNOSTICS[1],+ UNKNOWN_PROGRESS,+ DIAGNOSTICS[2],+ ]