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
Tests: stop Windows tests tripping Bitdefender and the 16-bit application dialog on real machines (#12167)
Daniel Hanbe9e6afd9269
diff --git a/tests/_shared/windows_console_stub.py b/tests/_shared/windows_console_stub.pynew file mode 100644index 00000000000..5afea907342--- /dev/null+++ b/tests/_shared/windows_console_stub.py@@ -0,0 +1,50 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""A genuinely runnable Windows .exe that is not a renamed system binary.++Tests that need a real PE at a chosen name used to copy System32's where.exe there.+A Microsoft-signed tool renamed to llama-server.exe or trusted-signing-cli.exe is+itself an AV heuristic, so this builds the file the way pip builds every+Scripts\\*.exe: distlib's console launcher, a shebang naming this interpreter, and a+zipped __main__.py that exits with the requested code. The launcher reads the+shebang from its own file, so the bytes can be written anywhere and still start.+"""++from __future__ import annotations++import io+import platform+import struct+import sys+import zipfile+from pathlib import Path+++def _launcher_dir() -> Path | None:+ for name in ("pip._vendor.distlib", "distlib"):+ try:+ module = __import__(name, fromlist = ["scripts"])+ except ImportError:+ continue+ return Path(module.__file__).parent+ return None+++def console_stub_bytes(exit_code: int = 0) -> bytes | None:+ """Launcher + shebang + zip, or None where no distlib launcher ships."""+ directory = _launcher_dir()+ if directory is None:+ return None+ if platform.machine().upper() in ("ARM64", "AARCH64"):+ launcher = "t64-arm.exe"+ else:+ launcher = "t64.exe" if struct.calcsize("P") == 8 else "t32.exe"+ path = directory / launcher+ if not path.is_file():+ return None+ stream = io.BytesIO()+ with zipfile.ZipFile(stream, "w") as archive:+ archive.writestr("__main__.py", f"import sys\nsys.exit({int(exit_code)})\n")+ shebang = b'#!"' + sys.executable.encode("utf-8") + b'"\n'+ return path.read_bytes() + shebang + stream.getvalue()diff --git a/tests/python/test_windows_setup_output_encoding.py b/tests/python/test_windows_setup_output_encoding.pyindex fbf893c1343..01f4a950ced 100644--- a/tests/python/test_windows_setup_output_encoding.py+++ b/tests/python/test_windows_setup_output_encoding.py@@ -496,10 +496,20 @@ def test_rust_windows_spawns_force_utf8(rust_file: str) -> None:# the script dies with exit 1 and 2 bytes of stdout, and the user's setup log holds a PowerShell stack trace where the# banner should be.#+# DETACHED_PROCESS would reach that state from the spawn, but Windows PowerShell 5.1 started that way exits 0 without+# running a line (checked on windows-latest and windows-11-arm, whatever stdin, -File or -Command), so the detach has to+# happen inside the child. The FreeConsole prologue that does it is an Add-Type/DllImport of kernel32, which+# Bitdefender quarantines on real machines (CMD:Heur.BZC.PZQ.Boxter.542), so these runs are opt-in off CI: they run+# under GitHub Actions, or locally with UNSLOTH_TEST_CONSOLE_LESS=1.+## Everything the probe prints is sliced out of the script under test; only the FreeConsole prologue and the stderr# diagnostics are harness.CREATE_NO_WINDOW = 0x08000000+_CONSOLE_LESS_OPTED_IN = (+ os.environ.get("GITHUB_ACTIONS") == "true" or os.environ.get("UNSLOTH_TEST_CONSOLE_LESS") == "1"+)+# studio/src-tauri/src/install.rs::powershell_launch_args, minus the -File the runner appends. Not Bypass:# RemoteSigned is what the shipped spawn uses.TAURI_FLAGS = ["-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "RemoteSigned"]@@ -636,6 +646,8 @@ def _run_console_less(path: Path, source: str | None = None) -> tuple[int, bytes`source` is for the VT parity case, which runs this file's own function beside the one itreplaced. A str keeps the lru_cache above workable; a dict would not hash."""+ if not _CONSOLE_LESS_OPTED_IN:+ pytest.skip("the FreeConsole probe trips AV heuristics; set UNSLOTH_TEST_CONSOLE_LESS=1 to run it")with tempfile.TemporaryDirectory() as workdir:# A file written here has no Zone.Identifier, so RemoteSigned admits it.probe = Path(workdir) / f"{path.stem}_console_less_probe.ps1"diff --git a/tests/python/test_windows_studio_update_launcher.py b/tests/python/test_windows_studio_update_launcher.pyindex b3d6c341505..2fb0131f9d7 100644--- a/tests/python/test_windows_studio_update_launcher.py+++ b/tests/python/test_windows_studio_update_launcher.py@@ -6,7 +6,9 @@from __future__ import annotationsimport importlib.util+import errnoimport inspect+import osimport shutilimport subprocessimport sys@@ -151,6 +153,23 @@ def _successful_version_run(calls = None):return run+def _unrunnable_version_run(calls):+ """What running one of these stubs answers, without running it.++ The launchers in this file are a few bytes behind an MZ, never a real PE image. Handing one to+ CreateProcess on a Windows desktop can raise the modal "Unsupported 16-Bit Application" dialog,+ and elsewhere it fails with an exec format error. That failure is the answer the recovery path+ is exercised against, so it is raised here directly and the file is never started.+ """++ def run(argv, **kwargs):+ # The bytes it would have started, so a caller can tell which file was asked.+ calls.append((argv, Path(argv[0]).read_bytes()))+ raise OSError(errno.ENOEXEC, os.strerror(errno.ENOEXEC), argv[0])++ return run++def _update(studio, *, verify = True):studio.update(local = False, package = "unsloth", verbose = False, verify = verify)@@ -225,10 +244,14 @@ def test_setup_failure_restores_original_and_propagates(monkeypatch, studio, tmpraise RuntimeError("setup failed")monkeypatch.setattr(studio, "_run_setup_script", setup)+ calls = []+ monkeypatch.setattr(studio.subprocess, "run", _unrunnable_version_run(calls))with pytest.raises(RuntimeError, match = "setup failed"):_update(studio)+ assert calls, "the restore never asked a launcher for --version"+assert launcher.read_bytes() == ORIGINAL_LAUNCHERassert (scripts / "unsloth.exe.update-backup").read_bytes() == ORIGINAL_LAUNCHER@@ -246,8 +269,15 @@ def test_invalid_launcher_is_restored_and_update_fails(monkeypatch, studio, tmp_monkeypatch.setattr(studio, "_run_setup_script", lambda **_kwargs: launcher.write_bytes(invalid))+ calls = []+ monkeypatch.setattr(studio.subprocess, "run", _unrunnable_version_run(calls))_shared_setup_3(launcher, studio)+ # The invalid file is refused before anything runs; only the restored original is asked.+ assert calls and all(+ argv == [str(launcher), "--version"] and started == ORIGINAL_LAUNCHER+ for argv, started in calls+ )assert (scripts / "unsloth.exe.update-backup").exists()diff --git a/tests/security/test_release_desktop_signing_simulation.py b/tests/security/test_release_desktop_signing_simulation.pyindex a39ea8e40d0..bb407652f2d 100644--- a/tests/security/test_release_desktop_signing_simulation.py+++ b/tests/security/test_release_desktop_signing_simulation.py@@ -30,6 +30,7 @@ import pytestimport yamlfrom unsloth_pwsh_runner import run_pwsh+from windows_console_stub import console_stub_bytes# Only PATHEXT maps a bare name onto the `.exe` the verify step insists on, so checks needing a *passing* resolution are@@ -385,8 +386,14 @@ def test_verify_fails_when_the_binary_cannot_start(sandbox):@needs_pathextdef test_verify_fails_when_the_binary_exits_non_zero(sandbox):+ # A pip-style launcher that starts and exits 3, not a renamed System32 tool: a Microsoft+ # binary at trusted-signing-cli.exe is an AV heuristic.+ stub = console_stub_bytes(3)+ if stub is None:+ pytest.skip("no distlib console launcher to build a runnable stub from")directory = sandbox["runner_temp"] / "trusted-signing-cli"- _real_exe_on_path(directory, pathlib.Path(os.environ["SystemRoot"], "System32", "where.exe"))+ directory.mkdir(parents = True)+ (directory / "trusted-signing-cli.exe").write_bytes(stub)code, out = run_step(sandbox["verify"], {**sandbox["env"], "PATH": _path_with(directory)})diff --git a/tests/studio/install/test_keep_install_backcompat_9979.py b/tests/studio/install/test_keep_install_backcompat_9979.pyindex 0fe087ed933..af6dc0f82d8 100644--- a/tests/studio/install/test_keep_install_backcompat_9979.py+++ b/tests/studio/install/test_keep_install_backcompat_9979.py@@ -10,8 +10,11 @@ with a hand-built two-key marker. Platforms are simulated through ``HostInfo``:covers the path decisions and payload tables, not macOS dyld.Run natively on Windows too (the parity workflow's windows-latest row), where the loader-answers for real: ``_binary_image_runs`` sees an actual ``ERROR_BAD_EXE_FORMAT`` instead of-a stubbed ``run_capture``. Two things stay POSIX-only there and are skipped rather than+answers for real for every genuine image: the healthy rows start a real console launcher. The one+file that is not an image is answered with the ``ERROR_BAD_EXE_FORMAT`` the loader gives it,+without being started (see ``_windows_non_pe_is_refused_not_started``): a .exe that is not a+PE is taken for a DOS program, and on a Windows desktop that raises the modal "Unsupported+16-Bit Application" dialog. Two things stay POSIX-only there and are skipped rather thanweakened: ``os.chmod`` cannot clear an execute bit Windows does not have, and``os.access(X_OK)`` is true for any file that exists."""@@ -19,7 +22,6 @@ weakened: ``os.chmod`` cannot clear an execute bit Windows does not have, andimport importlib.utilimport jsonimport os-import shutilimport signalimport subprocessimport sys@@ -40,36 +42,33 @@ SKIP_X_OK = pytest.mark.skipif(def _windows_runnable_stub() -> bytes | None:"""Bytes of a real .exe that still starts after being copied somewhere else.- The keep path execs what it finds, so a Windows row needs a genuine PE. Copying- python.exe alone loses python3xx.dll and dies 0xC0000135, hence a System32 tool- whose imports are all KnownDLLs. Verified by running the copy, not assumed: an- unverifiable stub skips the module instead of reporting the loader's refusal as- a back-compat failure.+ The keep path execs what it finds, so a Windows row needs a genuine PE. A renamed+ System32 tool would do, but a Microsoft binary sitting at llama-server.exe is an AV+ heuristic, so this is a pip-style console launcher instead (tests/_shared).+ Verified by running the copy, not assumed: an unverifiable stub skips the module+ instead of reporting the loader's refusal as a back-compat failure."""- for name in ("where.exe", "hostname.exe"):- source = Path(os.environ.get("SystemRoot", r"C:\Windows")) / "System32" / name- if not source.is_file():- continue- with tempfile.TemporaryDirectory() as probe_dir:- copy = Path(probe_dir) / "llama-server.exe"- try:- shutil.copyfile(source, copy)- probe = subprocess.run([str(copy)], capture_output = True, timeout = 30)- except Exception:- continue- # A loader failure returns rather than raises, so an unchecked run would- # accept the very missing-DLL image this is picking a candidate to avoid,- # and every healthy fixture after it would inherit it.- if probe.returncode >= 0xC0000000:- continue- return source.read_bytes()- return None+ from windows_console_stub import console_stub_bytes++ stub = console_stub_bytes(0)+ if stub is None:+ return None+ with tempfile.TemporaryDirectory() as probe_dir:+ copy = Path(probe_dir) / "llama-server.exe"+ try:+ copy.write_bytes(stub)+ probe = subprocess.run([str(copy)], capture_output = True, timeout = 60)+ except Exception:+ return None+ # A loader failure returns rather than raises, so an unchecked run would accept the+ # very image this is meant to rule out, and every healthy fixture would inherit it.+ return stub if probe.returncode == 0 else NoneRUNNABLE_STUB = _windows_runnable_stub() if WINDOWS_HOST else Noneif WINDOWS_HOST and RUNNABLE_STUB is None:pytest.skip(- "no self-contained System32 .exe to stand in for llama-server",+ "no runnable console launcher to stand in for llama-server",allow_module_level = True,)@@ -84,6 +83,34 @@ SPEC.loader.exec_module(ILP)HostInfo = ILP.HostInfo+# ERROR_BAD_EXE_FORMAT, what CreateProcess answers for a file that is not a Windows image.+_ERROR_BAD_EXE_FORMAT = 193++[email protected](autouse = True)+def _windows_non_pe_is_refused_not_started(monkeypatch):+ """Windows only: a non-PE image gets the loader's answer without reaching CreateProcess.++ Genuine images, the runnable stub included, still go through the real ``run_capture``.+ A test that replaces ``run_capture`` itself replaces this too, as before. POSIX is left+ alone: execve on a non-ELF raises ENOEXEC with no UI, so the real answer is kept there.+ """+ if not WINDOWS_HOST:+ return+ real = ILP.run_capture++ def run_capture(command, *args, **kwargs):+ target = Path(command[0]) if command else None+ if target is not None and target.is_file():+ with open(target, "rb") as handle:+ if handle.read(2) != b"MZ":+ raise OSError(+ None, "%1 is not a valid Win32 application", str(target), _ERROR_BAD_EXE_FORMAT+ )+ return real(command, *args, **kwargs)++ monkeypatch.setattr(ILP, "run_capture", run_capture)+def _host(**kw) -> HostInfo:base = dict(@@ -215,7 +242,8 @@ def build_install(else (runnable if runnable_root is None else runnable_root))# The keep path execs these. The not-ok file has to be a bad image: ENOEXEC on- # POSIX, a non-PE on Windows, where an empty file is a valid do-nothing program.+ # POSIX, a non-PE on Windows, where an empty file is a valid do-nothing program. The+ # Windows non-PE is answered by _windows_non_pe_is_refused_not_started, never started.if WINDOWS_HOST:path.write_bytes(RUNNABLE_STUB if ok else b"not a PE image\n")else:diff --git a/tests/studio/test_install_rollback_lifecycle.ps1 b/tests/studio/test_install_rollback_lifecycle.ps1index e83e701bf4f..2aa798f9674 100644--- a/tests/studio/test_install_rollback_lifecycle.ps1+++ b/tests/studio/test_install_rollback_lifecycle.ps1@@ -374,11 +374,28 @@ try {# Arc machine to CPU wheels for having opted out of a rollback copy.[System.IO.Directory]::CreateDirectory($VenvDir) | Out-Null[System.IO.Directory]::CreateDirectory((Join-Path $VenvDir "Scripts")) | Out-Null- # Stands in for the interpreter: the probe is bounded and reads stdout, so what it runs only- # has to print True the way torch.xpu.is_available() would on an Arc machine.+ # Stands in for the interpreter. On POSIX it is a real executable and the extracted+ # Invoke-BoundedPythonProbe runs it end to end: the probe is bounded and reads stdout, so it+ # only has to print True the way torch.xpu.is_available() would on an Arc machine.+ #+ # Windows cannot do that without a real PE image. CreateProcess on a .exe that is really text+ # does not fail quietly: on an x64 desktop it raises the modal "Unsupported 16-Bit Application"+ # dialog, which blocks an unattended run on a real machine. So the file there only has to+ # exist, for the Test-Path gate in front of the probe, and the probe itself is replaced by one+ # that returns what the real helper returns for a child that printed True and exited 0. That+ # keeps the assertion below on every platform instead of skipping it where it matters most.$fakePy = Join-Path (Join-Path $VenvDir "Scripts") "python.exe"- if ($IsWindows -or $env:OS -eq "Windows_NT") {- Set-Content -LiteralPath $fakePy -Value "@echo True"+ $onWindows = ($IsWindows -or $env:OS -eq "Windows_NT")+ $probeCalls = [System.Collections.Generic.List[object]]::new()+ if ($onWindows) {+ # Never executed: nothing below starts it, and it is not a PE image.+ Set-Content -LiteralPath $fakePy -Value "placeholder, not an interpreter"+ function Invoke-BoundedPythonProbe {+ param([string]$PythonExe, [string]$Code, [int]$TimeoutSec = 30)+ $probeCalls.Add([pscustomobject]@{ PythonExe = $PythonExe; Code = $Code })+ # The real helper's shape: stdout verbatim (CRLF from a Windows python), exit 0.+ return [pscustomobject]@{ Ok = $true; Output = "True`r`n"; Error = "" }+ }} else {Set-Content -LiteralPath $fakePy -Value "#!/bin/sh`necho True"& chmod +x $fakePy@@ -388,15 +405,26 @@ try {$script:StudioNoRollback = $true$script:StudioRollbackCostsFullSize = $true$xpuThrew = $false- try { Start-StudioVenvRollback -ExistingDir $VenvDir } catch { $xpuThrew = $true }+ try {+ try { Start-StudioVenvRollback -ExistingDir $VenvDir } catch { $xpuThrew = $true }+ } finally {+ if ($onWindows) {+ # Put the extracted definition back, so no later case runs against the stand-in.+ Invoke-Expression $definitions["Invoke-BoundedPythonProbe"].Extent.Text+ }+ }Check "taking the XPU verdict never costs the discard" (-not $xpuThrew)- if ($IsWindows -or $env:OS -eq "Windows_NT") {- # A .exe that is really a batch file will not start on Windows, so only the POSIX arm- # can drive the probe end to end; here the assertion is that it is attempted and safe.- Write-Host " SKIP the stand-in interpreter cannot be executed on this platform"- } else {- Check "the XPU verdict survives the environment being discarded" (- $script:StudioPreservedXpuVerdict)+ Check "the XPU verdict survives the environment being discarded" (+ $script:StudioPreservedXpuVerdict)+ if ($onWindows) {+ # The stand-in answers True to anyone, so also pin WHAT was asked: the moved-aside tree's+ # interpreter, not the path being rebuilt, and the XPU question rather than some other one.+ Check "the verdict came from the moved-aside interpreter" (+ $probeCalls.Count -eq 1 -and+ $probeCalls[0].PythonExe -match 'unsloth_studio\.rollback\.[^\\/]+[\\/]Scripts[\\/]python\.exe$' -and+ $probeCalls[0].Code -match 'torch\.xpu\.is_available\(\)')+ Check "the extracted probe is back in place" (+ (Get-Command Invoke-BoundedPythonProbe).ScriptBlock.ToString() -match 'ProcessStartInfo')}Check "and the environment really was discarded" (-not (Test-Path -LiteralPath $VenvDir))$script:StudioPreservedXpuVerdict = $falsediff --git a/tests/studio_setup_ps1/Studio.Setup.FindInstalledUv.Tests.ps1 b/tests/studio_setup_ps1/Studio.Setup.FindInstalledUv.Tests.ps1index 88076663787..97aea5e32f6 100644--- a/tests/studio_setup_ps1/Studio.Setup.FindInstalledUv.Tests.ps1+++ b/tests/studio_setup_ps1/Studio.Setup.FindInstalledUv.Tests.ps1@@ -8,7 +8,10 @@cost 42 of a 53 s no-op update, so it is the side that most needs running.Fixtures are a real executable copied into a candidate directory, and a text file for the- one that cannot run, so both the Test-Path gate and the launch are exercised.+ one that cannot run, so both the Test-Path gate and the launch are exercised. On Windows the+ text file is never handed to CreateProcess: a .exe that is not a PE image is taken for a DOS+ program and raises the modal "Unsupported 16-Bit Application" dialog on a desktop. There+ Start-Process is shadowed for non-PE files only, to fail the way the refused launch does.#>BeforeAll {@@ -82,6 +85,43 @@ BeforeAll {@()}+ function Test-IsPeImage {+ # The two bytes CreateProcess needs before it will treat a file as a Windows image.+ param([string]$Path)+ try {+ $head = [byte[]]::new(2)+ $stream = [System.IO.File]::OpenRead($Path)+ try { $read = $stream.Read($head, 0, 2) } finally { $stream.Dispose() }+ return ($read -eq 2 -and $head[0] -eq 0x4D -and $head[1] -eq 0x5A)+ } catch {+ return $false+ }+ }++ # Windows only, installed per Describe with Set-Item function:Start-Process (see below). A+ # plain function rather than a Pester Mock: it shadows the cmdlet for the finder too, and an+ # unmatched Mock filter falls through to the real command in Pester 5 but fails the call in+ # Pester 6. Real images launch for real; a non-PE file fails as a refused launch does, with+ # Start-Process throwing, and is never started. POSIX keeps the real exec, where a text file+ # fails with ENOEXEC and no UI.+ $script:RefusedLaunches = [System.Collections.Generic.List[string]]::new()+ function Start-ProcessUnlessNonPe {+ [CmdletBinding()]+ param(+ [Parameter(Mandatory, Position = 0)][Alias('Path')][string]$FilePath,+ [string[]]$ArgumentList,+ [switch]$NoNewWindow,+ [switch]$PassThru,+ [string]$RedirectStandardOutput,+ [string]$RedirectStandardError+ )+ if (-not (Test-IsPeImage -Path $FilePath)) {+ $script:RefusedLaunches.Add($FilePath)+ throw [System.ComponentModel.Win32Exception]::new(193)+ }+ Microsoft.PowerShell.Management\Start-Process @PSBoundParameters+ }+function New-FakeUv {# ok: runs and exits 0. broken: exists, cannot launch. folder: a directory named uv.exe.# old: runs, answers below the floor (POSIX only, where the stand-in is a script).@@ -109,6 +149,10 @@ BeforeAll {}Describe "the fixtures themselves" {+ BeforeAll {+ if ($IsWindows) { Set-Item function:Start-Process ${function:Start-ProcessUnlessNonPe} }+ }+It "has a stand-in that really runs, so a finder miss cannot be blamed on the fixture" {$dir = Join-Path ([System.IO.Path]::GetTempPath()) ('fixtureprobe_' + [guid]::NewGuid().ToString('N').Substring(0, 8))try {@@ -123,6 +167,9 @@ Describe "the fixtures themselves" {$dir = Join-Path ([System.IO.Path]::GetTempPath()) ('fixtureprobe_' + [guid]::NewGuid().ToString('N').Substring(0, 8))try {$exe = New-FakeUv -Dir $dir -Kind broken+ # What makes it unlaunchable, checked on the file itself rather than only through+ # the Windows guard.+ Test-IsPeImage -Path $exe | Should -BeFalseTest-ExeAnswers -Path $exe | Should -BeFalse} finally {Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue@@ -138,6 +185,7 @@ Describe "Find-InstalledUv" {if ($IsWindows) {function Test-SetupUvVersionAtLeast { param([string]$VersionLine, [string]$Minimum) return $true }}+ if ($IsWindows) { Set-Item function:Start-Process ${function:Start-ProcessUnlessNonPe} }}BeforeEach { $script:Sandbox = New-Sandbox }AfterEach {@@ -167,6 +215,11 @@ Describe "Find-InstalledUv" {New-FakeUv -Dir $good | Out-NullFind-InstalledUv | Should -Be ([System.IO.Path]::Combine($env:USERPROFILE, '.local', 'bin'))$script:InstalledUvProbeMiss | Should -Not -BeNullOrEmpty+ if ($IsWindows) {+ # The broken uv was asked, through the guard, so the skip above is the finder's doing.+ @($script:RefusedLaunches | Where-Object { $_ -like "*broken dir*" }).Count |+ Should -BeGreaterThan 0+ }}It "does not reuse a directory that happens to be named uv.exe" {