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.

feat(install): fall back to CERNET and npmmirror when package hosts are blocked or slow (#11786)

Long Yixing8c8dd864cdae
diff --git a/.github/workflows/studio-windows-inference-smoke.yml b/.github/workflows/studio-windows-inference-smoke.yml
index 33ab41c7f6a..a24567d2020 100644
--- a/.github/workflows/studio-windows-inference-smoke.yml
+++ b/.github/workflows/studio-windows-inference-smoke.yml
@@ -182,6 +182,8 @@ jobs:
if ($LASTEXITCODE) { exit $LASTEXITCODE }
pwsh -NoProfile -File tests/studio/test_node_decision.ps1
if ($LASTEXITCODE) { exit $LASTEXITCODE }
+ pwsh -NoProfile -File tests/studio/test_mirror_fallback.ps1
+ if ($LASTEXITCODE) { exit $LASTEXITCODE }
pwsh -NoProfile -File tests/studio/test_node_probe_guard.ps1
if ($LASTEXITCODE) { exit $LASTEXITCODE }
pwsh -NoProfile -File tests/studio/test_path_probe_access_denied.ps1
diff --git a/install.ps1 b/install.ps1
index e13da1e807f..50fe47c91b4 100644
--- a/install.ps1
+++ b/install.ps1
@@ -4282,6 +4282,244 @@ exit 1
return "$reason Nothing was installed."
}
+ function Test-MirrorConfigured {
+ param([ValidateSet('uv', 'pip')][string]$Tool)
+ if ($Tool -eq 'uv') {
+ if ("$env:UV_DEFAULT_INDEX$env:UV_INDEX_URL$env:UV_INDEX$env:UV_EXTRA_INDEX_URL") { return $true }
+ $pattern = '^\s*(\[\[(tool\.uv\.)?index\]\]|(pip\.)?(index|index-url|default-index|extra-index-url|no-index)\s*=)'
+ $files = @($env:UV_CONFIG_FILE, "$env:APPDATA\uv\uv.toml", "$env:ProgramData\uv\uv.toml")
+ $dir = (Get-Location -PSProvider FileSystem).ProviderPath
+ while ($dir) {
+ $pyproject = Join-Path $dir 'pyproject.toml'
+ if (Test-Path -LiteralPath (Join-Path $dir 'uv.toml') -PathType Leaf) { $files += Join-Path $dir 'uv.toml'; break }
+ if ((Test-Path -LiteralPath $pyproject -PathType Leaf) -and
+ (Select-String -LiteralPath $pyproject -Pattern '^\s*\[+tool\.uv(\.|\])' -Quiet -ErrorAction SilentlyContinue)) { $files += $pyproject; break }
+ $dir = Split-Path -Parent $dir
+ }
+ } else {
+ if ("$env:PIP_INDEX_URL$env:PIP_EXTRA_INDEX_URL$env:PIP_NO_INDEX") { return $true }
+ $pattern = '^\s*(index[-_]url|extra[-_]index[-_]url|no[-_]index)\s*[=:]'
+ $files = @($env:PIP_CONFIG_FILE, $(if ($venv = Get-Variable VenvDir -ValueOnly -ErrorAction SilentlyContinue) { Join-Path $venv 'pip.ini' }), "$env:APPDATA\pip\pip.ini", "$env:USERPROFILE\pip\pip.ini", "$env:ProgramData\pip\pip.ini")
+ }
+ foreach ($file in $files) {
+ if ($file -and (Test-Path -LiteralPath $file -PathType Leaf) -and
+ (Select-String -LiteralPath $file -Pattern $pattern -Quiet -ErrorAction SilentlyContinue)) {
+ return $true
+ }
+ }
+ return $false
+ }
+
+ function Start-MirrorProbe {
+ param([string[]]$Urls, [double]$Seconds, [long]$LastByte)
+ $state = @{ Urls = $Urls; Seconds = $Seconds; LastByte = $LastByte; Clock = [System.Diagnostics.Stopwatch]::StartNew(); Requests = @{}; Heads = @{}; InTime = @{}; Bodies = @{}; Buffers = @{} }
+ $deadline = [System.Threading.Tasks.Task]::Delay([int]($Seconds * 1000))
+ foreach ($url in $Urls) {
+ $state.Requests[$url] = [System.Net.WebRequest]::Create($url)
+ # The CERNET mirrors answer 403 to a request without a User-Agent; a fresh connection keeps every probe cold, as curl's are.
+ $state.Requests[$url].UserAgent = 'unsloth-installer'
+ $state.Requests[$url].KeepAlive = $false
+ $state.Requests[$url].AddRange(0, $LastByte)
+ $state.Heads[$url] = $state.Requests[$url].GetResponseAsync()
+ $state.InTime[$url] = [System.Threading.Tasks.Task]::WhenAny([System.Threading.Tasks.Task[]]@($state.Heads[$url], $deadline))
+ }
+ return $state
+ }
+
+ function Wait-MirrorProbe {
+ param($Probe)
+ $results = @{}
+ $measure = { param($url) @([int]$Probe.Heads[$url].Result.StatusCode, [long]($Probe.Buffers[$url].Position / $Probe.Clock.Elapsed.TotalSeconds)) }
+ try {
+ while ($true) {
+ foreach ($url in $Probe.Urls) {
+ if ($results.ContainsKey($url) -or -not $Probe.InTime[$url].IsCompleted) { continue }
+ if (-not [object]::ReferenceEquals($Probe.InTime[$url].Result, $Probe.Heads[$url])) {
+ $results[$url] = @(0, [long]0)
+ } elseif ($Probe.Heads[$url].Status -ne 'RanToCompletion') {
+ $failure = $Probe.Heads[$url].Exception.InnerException -as [System.Net.WebException]
+ $results[$url] = @($(if ($failure -and $failure.Response) { [int]$failure.Response.StatusCode } else { 0 }), [long]0)
+ } elseif (-not $Probe.Bodies.ContainsKey($url)) {
+ $Probe.Buffers[$url] = [System.IO.MemoryStream]::new([byte[]]::new($Probe.LastByte + 65537))
+ $Probe.Bodies[$url] = $Probe.Heads[$url].Result.GetResponseStream().CopyToAsync($Probe.Buffers[$url], 65536)
+ } elseif ($Probe.Bodies[$url].IsCompleted) {
+ $results[$url] = & $measure $url
+ }
+ }
+ $pending = @($Probe.Urls | Where-Object { -not $results.ContainsKey($_) } | ForEach-Object { if ($Probe.Bodies.ContainsKey($_)) { $Probe.Bodies[$_] } else { $Probe.InTime[$_] } })
+ $left = [int](($Probe.Seconds - $Probe.Clock.Elapsed.TotalSeconds) * 1000)
+ if ($pending.Count -eq 0 -or $left -le 0) { break }
+ [void][System.Threading.Tasks.Task]::WaitAny([System.Threading.Tasks.Task[]]$pending, $left)
+ }
+ foreach ($url in $Probe.Urls) {
+ if ($results.ContainsKey($url)) { continue }
+ $results[$url] = if ($Probe.Buffers.ContainsKey($url)) { & $measure $url } else { @(0, [long]0) }
+ }
+ } finally {
+ foreach ($url in @($Probe.Requests.Keys)) {
+ $Probe.Requests[$url].Abort()
+ if ($Probe.Heads[$url].Status -eq 'RanToCompletion') { $Probe.Heads[$url].Result.Dispose() }
+ }
+ }
+ return $results
+ }
+
+ function Get-MirrorDnsServers {
+ try {
+ [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | Where-Object { $_.OperationalStatus -eq 'Up' } |
+ ForEach-Object { $_.GetIPProperties().DnsAddresses } | ForEach-Object { "$_" }
+ } catch {}
+ }
+
+ # No network call: a mainland China time zone, or a resolver from a mainland public DNS or cloud, as _mirror_in_china in install.sh.
+ function Test-MirrorInChina {
+ try { if ((Get-TimeZone).Id -in 'China Standard Time', 'Asia/Shanghai', 'Asia/Chongqing', 'Asia/Chungking', 'Asia/Harbin', 'Asia/Urumqi', 'Asia/Kashgar', 'PRC') { return $true } } catch {}
+ return [bool](@(Get-MirrorDnsServers) -match '^(223\.5\.5\.5|223\.6\.6\.6|119\.29\.29\.29|114\.114\.11[45]\.11[0459]|182\.254\.116\.116|119\.28\.28\.28|180\.76\.76\.76|1\.2\.4\.8|210\.2\.4\.8|100\.100\.2\.13[68]|183\.60\.8[23]\.(19|98))$')
+ }
+
+ function Invoke-MirrorFallback {
+ param([switch]$SpareOnly)
+ $optIn = "$env:UNSLOTH_MIRROR_FALLBACK".Trim()
+ # When off, a retry state inherited from a parent is dropped so nothing downstream acts on it.
+ if ($optIn -match '^(0|false|no|off)$' -or ($optIn -notmatch '^(1|true|yes|on)$' -and -not (Test-MirrorInChina))) {
+ Remove-Item Env:_UNSLOTH_MIRROR_SPARE -ErrorAction SilentlyContinue
+ return
+ }
+ if ($env:_UNSLOTH_MIRROR_PROBED) { return }
+ if (-not $SpareOnly) { $env:_UNSLOTH_MIRROR_PROBED = '1' }
+ $cernet = 'https://tuna.mirrors.cernet.edu.cn'
+ $npmMirror = 'https://registry.npmmirror.com'
+ $pypiMirror = "$cernet/pypi/web/simple"
+ $minBps = 1MB
+ $useUv = -not (Test-MirrorConfigured -Tool uv)
+ $usePip = -not (Test-MirrorConfigured -Tool pip)
+ $uvWheel = 'packages/72/d6/207945fe69903b9794e2ef3e42608c91a59972567343a6719078d99c71f7/uv-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl'
+ $torchWheel = 'whl/cpu/torch-2.9.1%2Bcpu-cp312-cp312-manylinux_2_28_x86_64.whl'
+ $nodeTarball = 'v24.18.0/node-v24.18.0-linux-x64.tar.gz'
+ $artifact = @{
+ 'pypi' = "https://files.pythonhosted.org/$uvWheel"; 'cernet-pypi' = "$cernet/pypi/web/$uvWheel"
+ 'torch' = "https://download-r2.pytorch.org/$torchWheel"; 'cernet-torch' = "$cernet/pytorch/$torchWheel"
+ 'node' = "https://nodejs.org/dist/$nodeTarball"; 'npmmirror-node' = "$npmMirror/-/binary/node/$nodeTarball"
+ 'npm' = 'https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz'; 'npmmirror' = "$npmMirror/typescript/-/typescript-5.9.3.tgz"
+ 'astral' = 'https://releases.astral.sh/github/uv/releases/download/0.12.1/uv-x86_64-unknown-linux-gnu.tar.gz'
+ }
+ $hosts = [ordered]@{}
+ if ($useUv -or $usePip) { $hosts['pypi'] = @('pypi', 'cernet-pypi', $pypiMirror, 'https://pypi.org/simple/uv/', "$pypiMirror/uv/") }
+ if (-not "$env:UNSLOTH_PYTORCH_MIRROR$env:UNSLOTH_TORCH_INDEX_URL") {
+ $hosts['torch'] = @('torch', 'cernet-torch', "$cernet/pytorch/whl", 'https://download.pytorch.org/whl/cpu/torch/', "$cernet/pytorch/whl/cpu/torch/")
+ }
+ if (-not $env:UNSLOTH_NODE_MIRROR) { $hosts['node'] = @('node', 'npmmirror-node', "$npmMirror/-/binary/node", $null, $null) }
+ if (-not "$env:UNSLOTH_NPM_REGISTRY$env:NPM_CONFIG_REGISTRY") { $hosts['npm'] = @('npm', 'npmmirror', $npmMirror, $null, $null) }
+ if (-not "$env:UNSLOTH_UV_WHEEL_MIRROR$env:UV_DOWNLOAD_URL$env:INSTALLER_DOWNLOAD_URL$env:UV_INSTALLER_GHE_BASE_URL$env:UV_INSTALLER_GITHUB_BASE_URL") {
+ $hosts['uvbin'] = @('astral', 'cernet-pypi', "$cernet/pypi/web", $null, $null)
+ }
+ if ($hosts.Count -eq 0) { return }
+ $varsOf = {
+ param($name)
+ $to = if ($hosts.Contains($name)) { $hosts[$name][2] }
+ switch ($name) {
+ 'pypi' {
+ if ($useUv) { "UV_DEFAULT_INDEX=$to" }
+ if ($usePip) { "PIP_INDEX_URL=$to" }
+ }
+ 'unsynced' {
+ # Only for one rerun: uv's unsafe-first-match fetches every package from every index, and fails outright when one is unreachable.
+ if ($useUv) {
+ 'UV_DEFAULT_INDEX=https://pypi.org/simple'; "UV_INDEX=$pypiMirror"
+ "UV_INDEX_STRATEGY=$(if ($env:UV_INDEX_STRATEGY) { $env:UV_INDEX_STRATEGY } else { 'unsafe-first-match' })"
+ }
+ if ($usePip) { 'PIP_EXTRA_INDEX_URL=https://pypi.org/simple'; "PIP_INDEX_URL=$pypiMirror" }
+ }
+ 'torch' { "UNSLOTH_PYTORCH_MIRROR=$to" }
+ 'node' { "UNSLOTH_NODE_MIRROR=$to" }
+ 'npm' { "UNSLOTH_NPM_REGISTRY=$to" }
+ 'uvbin' { "UNSLOTH_UV_WHEEL_MIRROR=$to" }
+ }
+ }
+ $env:_UNSLOTH_MIRROR_SPARE = @($hosts.Keys | ForEach-Object { (@($_) + @(& $varsOf $_)) -join '|' }) -join ' '
+ if ($SpareOnly -or (Test-UvEnvFlag 'UV_OFFLINE')) { return }
+ $answered = @{}
+ $codeOf = { param($index, $result) if ($index -and "$($answered[$index][0])" -notmatch '^2\d\d$') { 0 } else { $result[0] } }
+ # PS 5.1 may pin TLS 1.0/1.1 (every probed host refuses it; Tls|Tls12 still fails) and queues past 2 connections per host.
+ $savedProtocol = [System.Net.ServicePointManager]::SecurityProtocol
+ $savedLimit = [System.Net.ServicePointManager]::DefaultConnectionLimit
+ if ([int]$savedProtocol -ne 0) { [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 }
+ [System.Net.ServicePointManager]::DefaultConnectionLimit = [Math]::Max($savedLimit, 16)
+ try {
+ $defaults = @($hosts.Values | ForEach-Object { $_[0] } | Select-Object -Unique)
+ $indexes = Start-MirrorProbe -Urls @($hosts.Values | ForEach-Object { $_[3] } | Where-Object { $_ }) -Seconds 4 -LastByte 1023
+ $timed = @{}
+ foreach ($name in $defaults) { $timed[$name] = (Wait-MirrorProbe (Start-MirrorProbe -Urls $artifact[$name] -Seconds 1.5 -LastByte 1048575))[$artifact[$name]] }
+ $answered = Wait-MirrorProbe $indexes
+ $slow = @($hosts.Keys | Where-Object {
+ $code = & $codeOf $hosts[$_][3] $timed[$hosts[$_][0]]
+ $code -eq 0 -or ($code -ge 300 -and $code -lt 400) -or ($code -ge 200 -and $code -lt 300 -and $timed[$hosts[$_][0]][1] -lt $minBps)
+ })
+ if ($slow.Count -eq 0) { return }
+ $sources = @($slow | ForEach-Object { $hosts[$_][1] } | Select-Object -Unique)
+ $indexes = Start-MirrorProbe -Urls @($slow | ForEach-Object { $hosts[$_][3]; $hosts[$_][4] } | Where-Object { $_ }) -Seconds 4 -LastByte 1023
+ $race = Wait-MirrorProbe (Start-MirrorProbe -Urls @($slow | ForEach-Object { $artifact[$hosts[$_][0]] }; $sources | ForEach-Object { $artifact[$_] }) -Seconds 4 -LastByte 1048575)
+ $sourceIndexes = Wait-MirrorProbe $indexes
+ foreach ($url in $sourceIndexes.Keys) { $answered[$url] = $sourceIndexes[$url] }
+ } finally {
+ [System.Net.ServicePointManager]::SecurityProtocol = $savedProtocol
+ [System.Net.ServicePointManager]::DefaultConnectionLimit = $savedLimit
+ }
+ $used = $false
+ foreach ($name in $slow) {
+ $default = $race[$artifact[$hosts[$name][0]]]
+ $mirror = $race[$artifact[$hosts[$name][1]]]
+ $how = if ("$(& $codeOf $hosts[$name][3] $default)" -match '^2\d\d$') { 'slow' } else { 'blocked' }
+ $defaultBps = if ($how -eq 'slow') { $default[1] } else { [long]0 }
+ if ("$(& $codeOf $hosts[$name][4] $mirror)" -notmatch '^2\d\d$' -or $defaultBps -ge $minBps -or $mirror[1] -le $defaultBps) { continue }
+ Set-MirrorEnv @(& $varsOf $name)
+ $env:_UNSLOTH_MIRROR_SPARE = @(-split $env:_UNSLOTH_MIRROR_SPARE | Where-Object { $_ -notlike "$name|*" }) -join ' '
+ if ($name -eq 'pypi' -and $how -eq 'slow') { $env:_UNSLOTH_MIRROR_SPARE = (@(-split $env:_UNSLOTH_MIRROR_SPARE) + ((@('unsynced') + @(& $varsOf 'unsynced')) -join '|')) -join ' ' }
+ step "mirror" "$(Get-MirrorName $name) is $how ($($defaultBps -shr 10) KB/s, mirror $($mirror[1] -shr 10) KB/s); using $($hosts[$name][2])" "Yellow"
+ $used = $true
+ }
+ if ($used) { substep "Set UNSLOTH_MIRROR_FALLBACK=0 to always use the default hosts." }
+ }
+
+ function Get-MirrorName {
+ param([string]$Name)
+ @{ pypi = 'PyPI'; unsynced = 'The PyPI mirror'; torch = 'download.pytorch.org'; node = 'nodejs.org'; npm = 'registry.npmjs.org'; uvbin = 'releases.astral.sh (uv)' }[$Name]
+ }
+
+ function Set-MirrorEnv {
+ param([string[]]$Pairs)
+ foreach ($pair in $Pairs) { Set-Item "Env:$($pair.Split('=', 2)[0])" $pair.Split('=', 2)[1] }
+ }
+
+ function Pop-MirrorSpare {
+ param([string]$Name)
+ $entry = @(-split $env:_UNSLOTH_MIRROR_SPARE | Where-Object { $_ -like "$Name|*" })
+ if (-not $entry) { return }
+ $env:_UNSLOTH_MIRROR_SPARE = @(-split $env:_UNSLOTH_MIRROR_SPARE | Where-Object { $_ -notlike "$Name|*" }) -join ' '
+ $pairs = @($entry[0].Split('|') | Select-Object -Skip 1)
+ step "mirror" "$(Get-MirrorName $Name) failed; retrying through $($pairs[0].Split('=', 2)[1])" "Yellow"
+ return $pairs
+ }
+
+ function Use-MirrorSpare {
+ param([string]$Name)
+ $pairs = @(Pop-MirrorSpare $Name)
+ Set-MirrorEnv $pairs
+ return $pairs.Count -gt 0
+ }
+
+ function Get-MirrorFailedHost {
+ param([string]$Output, [string]$Ran)
+ if ($Output -notmatch 'error sending request|timed out|network timeout|idle timeout|connection (reset|refused|closed|aborted)|network aborted|broken pipe|dns error|failed to lookup address|name resolution|nodename nor servname|network is unreachable|error decoding response body|end of file before message length|unexpected eof|tls handshake|sslerror|certificate verify failed|server error|service unavailable|bad gateway|gateway time-?out|too many requests|max retries exceeded|remotedisconnected|incompleteread|econnreset|etimedout|eidletimeout|eai_again|enotfound|econnrefused|socket hang up') {
+ if ($Output -match 'only \S+ (.* )?(is|are) available|no versions? of|not found in the package registry|could not find a version that satisfies|no matching distribution found') { 'unsynced' }
+ return
+ }
+ if ($Output -match 'download(-r2)?\.pytorch\.org') { 'torch' }
+ elseif ($Output -match 'registry\.npmjs\.org') { 'npm' }
+ elseif ($Output -match 'pypi\.org|pythonhosted\.org') { 'pypi' }
+ elseif ($Ran -and $Output -notmatch 'https?://') { $Ran }
+ }
+
# ── END SHARED WITH studio/setup.ps1 ──
# Redact index-URL credentials (userinfo + ?query= + #fragment) from captured installer
@@ -4298,8 +4536,17 @@ exit 1
function Invoke-InstallCommand {
param(
[Parameter(Mandatory = $true)][ScriptBlock]$Command,
- [string]$Label = "install command"
+ [string]$Label = "install command",
+ [switch]$NoMirror
)
+ if ($script:InstallTorchMirror) {
+ foreach ($v in $Command.Ast.FindAll({ param($n) $n -is [System.Management.Automation.Language.VariableExpressionAst] -and $n.VariablePath.IsUnqualified }, $true)) {
+ $value = $ExecutionContext.SessionState.PSVariable.GetValue($v.VariablePath.UserPath)
+ if (@($value) -like 'https://download.pytorch.org/whl*') {
+ Set-Variable -Name $v.VariablePath.UserPath -Value ($value -replace '^https://download\.pytorch\.org/whl', $script:InstallTorchMirror.TrimEnd('/'))
+ }
+ }
+ }
# A pinned index must beat an inherited uv mirror (#6898); UV_NO_CONFIG=1 blocks uv.toml.
$savedUvIndex = $null
if ($Command.ToString() -match '--default-index') {
@@ -4316,6 +4563,7 @@ exit 1
# Reset to avoid stale values from prior native commands.
$global:LASTEXITCODE = 0
Write-TauriLog "OUTPUT_CLEAR" $Label
+ $collected = [System.Text.StringBuilder]::new()
if ($script:UnslothVerbose) {
# Merge stderr into stdout so progress/warning output stays visible
# without flipping $? on successful native commands (PS 5.1 treats
@@ -4323,13 +4571,20 @@ exit 1
# Redact per record: uv echoes index URLs (credentials and all) in
# its errors, and verbose mode must not bypass the quiet path's
# redaction. ForEach-Object/Out-Host leave $LASTEXITCODE untouched.
- & $Command 2>&1 | ForEach-Object {
- Write-UvDownloadMarker "$_"
- Redact-InstallOutput "$_"
- } | Out-Host
+ if ($NoMirror -or -not $env:_UNSLOTH_MIRROR_SPARE) {
+ & $Command 2>&1 | ForEach-Object {
+ Write-UvDownloadMarker "$_"
+ Redact-InstallOutput "$_"
+ } | Out-Host
+ } else {
+ & $Command 2>&1 | ForEach-Object {
+ [void]$collected.AppendLine("$_")
+ Write-UvDownloadMarker "$_"
+ Redact-InstallOutput "$_"
+ } | Out-Host
+ }
} else {
# Streamed, not collected, so a marker reaches the app mid-download.
- $collected = [System.Text.StringBuilder]::new()
& $Command 2>&1 | ForEach-Object {
$line = "$_"
[void]$collected.AppendLine($line)
@@ -4346,7 +4601,6 @@ exit 1
} else {
Write-TauriLog "ERROR_OUTPUT" "$Label failed (exit code $exitCode)"
}
- return $exitCode
} finally {
$ErrorActionPreference = $prevEap
if ($savedUvIndex) {
@@ -4354,6 +4608,34 @@ exit 1
foreach ($n in $savedUvIndex.Keys) { if ($null -ne $savedUvIndex[$n]) { Set-Item "Env:$n" $savedUvIndex[$n] } }
}
}
+ if ($exitCode -eq 0 -or $NoMirror -or -not $env:_UNSLOTH_MIRROR_SPARE) { return $exitCode }
+ return (Invoke-InstallMirrorRetry -Code $exitCode -Command $Command -Label $Label -Output $collected.ToString())
+ }
+
+ function Invoke-InstallMirrorRetry {
+ param([int]$Code, [ScriptBlock]$Command, [string]$Label, [string]$Output)
+ $words = @(foreach ($n in $Command.Ast.FindAll({ param($n) $n -is [System.Management.Automation.Language.StringConstantExpressionAst] -or $n -is [System.Management.Automation.Language.VariableExpressionAst] }, $true)) {
+ if ($n -is [System.Management.Automation.Language.StringConstantExpressionAst]) { $n.Value } else { $ExecutionContext.SessionState.PSVariable.GetValue($n.VariablePath.UserPath) }
+ }) | ForEach-Object { "$_" }
+ $torchArg = [bool](@($words) -like 'https://download.pytorch.org/whl*')
+ $pinned = [bool](@($words) -match '^--(index-url|default-index)$')
+ $ran = if ($torchArg) { 'torch' } elseif ($pinned -or @($words) -match '^(--find-links|--no-index|--torch-backend.*|venv)$|://') { '' } else { 'pypi' }
+ $failed = Get-MirrorFailedHost -Output $Output -Ran $ran
+ $ownIndex = $pinned -or @($words) -contains '--no-index'
+ if (-not (($failed -eq 'torch' -and $torchArg) -or ($failed -eq 'pypi' -and -not $pinned) -or ($failed -eq 'unsynced' -and -not $ownIndex))) { return $Code }
+ $pairs = @(Pop-MirrorSpare $failed)
+ if (-not $pairs) { return $Code }
+ $saved = @{}
+ foreach ($pair in $pairs) { $saved[$pair.Split('=', 2)[0]] = [Environment]::GetEnvironmentVariable($pair.Split('=', 2)[0]) }
+ $savedTorch = $script:InstallTorchMirror
+ Set-MirrorEnv $pairs
+ if ($failed -eq 'torch') { $script:InstallTorchMirror = $env:UNSLOTH_PYTORCH_MIRROR }
+ $code = Invoke-InstallCommand -Command $Command -Label $Label -NoMirror
+ if ($code -ne 0) {
+ foreach ($n in $saved.Keys) { [Environment]::SetEnvironmentVariable($n, $saved[$n]) }
+ $script:InstallTorchMirror = $savedTorch
+ }
+ return $code
}
function Invoke-InstallCommandRetry {
@@ -4373,7 +4655,7 @@ exit 1
}
$attempt = 1
while ($true) {
- $code = Invoke-InstallCommand -Command $Command -Label $Label
+ $code = Invoke-InstallCommand -Command $Command -Label $Label -NoMirror:($attempt -lt $maxAttempts)
if ($code -eq 0) { return 0 }
if ($attempt -ge $maxAttempts) { return $code }
substep ("retrying ""$Label"" after transient failure (attempt $($attempt + 1)/$maxAttempts, waiting ${delay}s)...") "Yellow"
@@ -6700,6 +6982,11 @@ exit 0
if ($SkipTorch) { $InitialGpuBranch = "no_torch" }
Write-TauriDiag -GpuBranch $InitialGpuBranch -TorchIndexFamily "none" -PythonVersionForDiag $DiagPythonVersion
+ foreach ($_mirrorEnvName in @('_UNSLOTH_MIRROR_PROBED', '_UNSLOTH_MIRROR_SPARE', 'UV_INDEX', 'UV_DEFAULT_INDEX', 'UV_INDEX_STRATEGY', 'PIP_INDEX_URL', 'PIP_EXTRA_INDEX_URL', 'UNSLOTH_PYTORCH_MIRROR', 'UNSLOTH_NODE_MIRROR', 'UNSLOTH_NPM_REGISTRY', 'UNSLOTH_UV_WHEEL_MIRROR')) {
+ $script:MirrorEnvSaved[$_mirrorEnvName] = [Environment]::GetEnvironmentVariable($_mirrorEnvName)
+ }
+ Invoke-MirrorFallback
+
# ── Install uv ──
Write-TauriLog "STEP" "Installing uv package manager"
$UvMinVersion = "0.8.16"
@@ -6836,16 +7123,20 @@ exit 0
# prepend as astral's install.ps1, but it fetches a data file with a pinned
# SHA-256 instead of running remote script text in-process.
# tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
- # Bumping the version means bumping all 3 hashes:
+ # Bumping the version means bumping all 3 hashes, and each Wheel/WheelSha256 from https://pypi.org/pypi/uv/<ver>/json:
# curl -sL https://github.com/astral-sh/uv/releases/download/<ver>/uv-<arch>-pc-windows-msvc.zip.sha256
$UvPinnedVersion = "0.12.1"
$UvPinnedAssets = @{
- "x86_64" = @{ Asset = "uv-x86_64-pc-windows-msvc.zip"; Sha256 = "8FCB0CB46E1229065E344758980924E569BEF5882EF45F46FADA8FB24E06B74A" }
- "arm64" = @{ Asset = "uv-aarch64-pc-windows-msvc.zip"; Sha256 = "9BC7C18E616230FA2DC6FB24BC3AFDE18A95C2B5C9433DE747E9502C66041568" }
- "x86" = @{ Asset = "uv-i686-pc-windows-msvc.zip"; Sha256 = "9B51C33D307A8AB9E9DFD88D4AE1491761F63DE0BFFA3CEC96BEC536491C9B97" }
+ "x86_64" = @{ Asset = "uv-x86_64-pc-windows-msvc.zip"; Sha256 = "8FCB0CB46E1229065E344758980924E569BEF5882EF45F46FADA8FB24E06B74A"
+ Wheel = "packages/0d/a4/467c99c76fefa8b1259a1d382a5e49f73068f38a2d58db401504a783ed2c/uv-0.12.1-py3-none-win_amd64.whl"; WheelSha256 = "BD02F2DA212E6A983115DC64A6FC94E9256C2D60E056D6B669DE0A6025AAEC05" }
+ "arm64" = @{ Asset = "uv-aarch64-pc-windows-msvc.zip"; Sha256 = "9BC7C18E616230FA2DC6FB24BC3AFDE18A95C2B5C9433DE747E9502C66041568"
+ Wheel = "packages/68/80/ec1acbf8e22dc4866f9070c30b064728cc0da73bedc30f2fbfdc0c5901a7/uv-0.12.1-py3-none-win_arm64.whl"; WheelSha256 = "EAD7AD064F291A5DF358C3FFA8FFAB347A32BD5A75A6A068CA22254C2539A829" }
+ "x86" = @{ Asset = "uv-i686-pc-windows-msvc.zip"; Sha256 = "9B51C33D307A8AB9E9DFD88D4AE1491761F63DE0BFFA3CEC96BEC536491C9B97"
+ Wheel = "packages/fd/02/f73e4867c0748eaa3dea90cdfeb73d15bab0f04802c5c20bb37fc14918fe/uv-0.12.1-py3-none-win32.whl"; WheelSha256 = "173EE216F17D89FC39F65339D311A53584FC7DE4918D27C0F3C7EDAFABC6B54D" }
}
function Install-UvFromRelease {
+ $script:UvReleaseUnfetched = $false
$arch = Get-HostMachineArch
if (-not $UvPinnedAssets.ContainsKey($arch)) {
substep "No uv build is published for this architecture ($arch)." "Yellow"
@@ -6853,6 +7144,8 @@ exit 0
}
$asset = $UvPinnedAssets[$arch].Asset
$wanted = $UvPinnedAssets[$arch].Sha256
+ $remote = $asset
+ $wheelDir = $null
# Same destination priority as astral's installer, so an existing uv is
# replaced in place and the PATH probe further below still finds it.
@@ -6881,6 +7174,11 @@ exit 0
@("$($env:UV_INSTALLER_GHE_BASE_URL.TrimEnd('/'))/astral-sh/uv/releases/download/$UvPinnedVersion")
} elseif ($env:UV_INSTALLER_GITHUB_BASE_URL) {
@("$($env:UV_INSTALLER_GITHUB_BASE_URL.TrimEnd('/'))/astral-sh/uv/releases/download/$UvPinnedVersion")
+ } elseif ($env:UNSLOTH_UV_WHEEL_MIRROR) {
+ $remote = $UvPinnedAssets[$arch].Wheel
+ $wanted = $UvPinnedAssets[$arch].WheelSha256
+ $wheelDir = "uv-$UvPinnedVersion.data/scripts"
+ @("$($env:UNSLOTH_UV_WHEEL_MIRROR.TrimEnd('/'))")
} else {
@("https://releases.astral.sh/github/uv/releases/download/$UvPinnedVersion",
"https://github.com/astral-sh/uv/releases/download/$UvPinnedVersion")
@@ -6894,14 +7192,16 @@ exit 0
# body is a successful download by every measure Invoke-WebRequest has, and checking
# afterwards spends the only attempt on it.
$downloaded = $false
+ $script:UvReleaseUnfetched = $true
foreach ($base in $uvBase) {
substep "downloading uv $UvPinnedVersion ($arch) from $base..." "Yellow"
try {
- Invoke-WebRequest -UseBasicParsing -OutFile $zip -Uri "$base/$asset"
+ Invoke-WebRequest -UseBasicParsing -OutFile $zip -Uri "$base/$remote"
} catch {
substep "uv download failed: $($_.Exception.Message)" "Yellow"
continue
}
+ $script:UvReleaseUnfetched = $false
$actual = ""
try { $actual = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash } catch {}
if ($actual -eq $wanted) {
@@ -6914,11 +7214,11 @@ exit 0
}
if (-not $downloaded) { return $false }
- # The Windows archives are flat: uv.exe, uvx.exe, uvw.exe at the root.
Expand-Archive -LiteralPath $zip -DestinationPath $work -Force
[System.IO.Directory]::CreateDirectory($destDir) | Out-Null
+ $srcRoot = if ($wheelDir) { Join-Path $work $wheelDir } else { $work }
- $stagedUv = Join-Path $work "uv.exe"
+ $stagedUv = Join-Path $srcRoot "uv.exe"
if (-not (Test-Path -LiteralPath $stagedUv)) {
substep "uv.exe was not present in $asset." "Yellow"
return $false
@@ -6938,7 +7238,7 @@ exit 0
# install rather than leaving half a set behind quietly.
$ok = $true
foreach ($exe in @("uv.exe", "uvx.exe", "uvw.exe")) {
- $src = Join-Path $work $exe
+ $src = Join-Path $srcRoot $exe
if (-not (Test-Path -LiteralPath $src)) { continue }
$dst = Join-Path $destDir $exe
try {
@@ -7000,7 +7300,7 @@ exit 0
# winget unavailable or it didn't put uv on PATH: install the pinned
# release directly (ARM64 runners, machines without the Store).
if (-not (Test-UvVersionOk)) {
- Install-UvFromRelease | Out-Null
+ if (@(Install-UvFromRelease)[-1] -ne $true -and $script:UvReleaseUnfetched -and (Use-MirrorSpare uvbin)) { Install-UvFromRelease | Out-Null }
Refresh-SessionPath
}
}
@@ -11075,6 +11375,8 @@ sys.exit(2 if conflict else (0 if installed else 1))
# Under `irm | iex` the script scope IS the caller's session; an earlier value must not leak.
$script:WoaResolverEnvSaved = $null
+$script:MirrorEnvSaved = @{}
+$script:InstallTorchMirror = $null
$script:WoaSessionOverrides = $null
$script:TorchOverridesFile = $null
try {
@@ -11089,6 +11391,11 @@ try {
}
$script:WoaResolverEnvSaved = $null
}
+ foreach ($_mirrorEnvName in @($script:MirrorEnvSaved.Keys)) {
+ $_mirrorEnvValue = $script:MirrorEnvSaved[$_mirrorEnvName]
+ if ($null -eq $_mirrorEnvValue) { Remove-Item "Env:$_mirrorEnvName" -ErrorAction SilentlyContinue }
+ else { Set-Item "Env:$_mirrorEnvName" $_mirrorEnvValue }
+ }
# UNSLOTH_KEPT_TORCH is a process-scoped handoff, and the session outlives the installer.
Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
# The generated overrides file copies the caller's UV_OVERRIDE contents; never leave it.
diff --git a/install.sh b/install.sh
index 34c1f2deda6..c8b7a10ff52 100755
--- a/install.sh
+++ b/install.sh
@@ -213,8 +213,76 @@ _uv_download_markers() {
}
run_install_cmd() {
+ # Nothing armed (outside mainland China): the plain runner, set -e and all.
+ if [ -z "${_UNSLOTH_MIRROR_SPARE:-}" ]; then
+ _run_install_cmd_once "$@"
+ return
+ fi
+ _run_install_cmd_once "$@" || _mirror_retry_install "$?" "$@"
+}
+
+_mirror_retry_install() {
+ [ -n "${_UNSLOTH_MIRROR_SPARE:-}" ] || return "$1"
+ _mri_rc=$1
+ _mri_label=$2
+ shift 2
+ case " $* " in
+ *" https://download.pytorch.org/whl"*) _mri_ran=torch ;;
+ *" --index-url "*|*" --default-index "*|*" --find-links "*|*" --no-index "*|*"://"*|*" --torch-backend"*) _mri_ran="" ;;
+ *" uv venv "*|*" uv python install "*) _mri_ran=python ;;
+ *) _mri_ran=pypi ;;
+ esac
+ _mri_host=$(_mirror_failed_host "${_ric_log:-}" "$_mri_ran") || _mri_host=""
+ rm -f "${_ric_log:-}"
+ case "$_mri_host $* " in
+ "pypi "*" --index-url "*|"pypi "*" --default-index "*) return "$_mri_rc" ;;
+ "unsynced "*" --index-url "*|"unsynced "*" --default-index "*|"unsynced "*" --no-index "*) return "$_mri_rc" ;;
+ "torch "*https://download.pytorch.org/whl*|"pypi "*|"python "*|"unsynced "*) ;;
+ *) return "$_mri_rc" ;;
+ esac
+ _mirror_take "$_mri_host" || return "$_mri_rc"
+ (
+ for _mri_pair in $_MT_PAIRS; do export "$_mri_pair"; done
+ [ "$_mri_host" != torch ] || _ric_torch_mirror=$UNSLOTH_PYTORCH_MIRROR
+ _run_install_cmd_once "$_mri_label" "$@" || { _mri_rc=$?; rm -f "${_ric_log:-}"; exit "$_mri_rc"; }
+ ) || return
+ for _mri_pair in $_MT_PAIRS; do export "$_mri_pair"; done
+ if [ "$_mri_host" = torch ]; then
+ _ric_torch_mirror=$UNSLOTH_PYTORCH_MIRROR
+ case "${TORCH_INDEX_URL:-}" in
+ https://download.pytorch.org/whl*) TORCH_INDEX_URL=$_ric_torch_mirror${TORCH_INDEX_URL#https://download.pytorch.org/whl} ;;
+ esac
+ fi
+}
+
+_ric_run() {
+ if "$@" 2>&1; then
+ _cmd_rc=0
+ else
+ _cmd_rc=$?
+ fi
+ printf '%s' "$_cmd_rc" > "$_rcf"
+}
+
+_ric_tee() {
+ if command -v tee >/dev/null 2>&1; then tee "$1"; else cat; fi
+}
+
+_run_install_cmd_once() {
_label="$1"
shift
+ [ -z "${_ric_log:-}" ] || rm -f "$_ric_log"
+ _ric_log=""
+ if [ -n "${_ric_torch_mirror:-}" ]; then
+ _ric_n=$#
+ for _ric_arg in "$@"; do
+ case "$_ric_arg" in
+ https://download.pytorch.org/whl*) _ric_arg=$_ric_torch_mirror${_ric_arg#https://download.pytorch.org/whl} ;;
+ esac
+ set -- "$@" "$_ric_arg"
+ done
+ shift "$_ric_n"
+ fi
# For --default-index, clear inherited uv index vars so a uv.toml cannot outrank the CLI pin.
case " $* " in
*" --default-index "*) set -- env -u UV_DEFAULT_INDEX -u UV_INDEX_URL -u UV_INDEX -u UV_EXTRA_INDEX_URL -u UV_TORCH_BACKEND -u UV_FIND_LINKS -u UV_CONFIG_FILE UV_NO_CONFIG=1 "$@" ;;
@@ -223,21 +291,22 @@ run_install_cmd() {
# Stream through the redactor; the rc file carries the exit code (no pipefail in sh).
_rcf=$(mktemp)
tauri_stream_log stdout "OUTPUT_CLEAR" "$_label"
- {
- if "$@" 2>&1; then
- _cmd_rc=0
- else
- _cmd_rc=$?
- fi
- printf '%s' "$_cmd_rc" > "$_rcf"
- } | _uv_download_markers "" "$UNSLOTH_DL_MARKER_MIN_BYTES" | _redact_install_output
+ _log=""
+ if [ -n "${_UNSLOTH_MIRROR_SPARE:-}" ]; then
+ _log=$(mktemp)
+ _ric_run "$@" | _ric_tee "$_log" | _uv_download_markers "" "$UNSLOTH_DL_MARKER_MIN_BYTES" | _redact_install_output
+ else
+ _ric_run "$@" | _uv_download_markers "" "$UNSLOTH_DL_MARKER_MIN_BYTES" | _redact_install_output
+ fi
_rc=$(cat "$_rcf" 2>/dev/null || echo 1)
rm -f "$_rcf"
_rc=${_rc:-1}
if [ "$_rc" -eq 0 ] 2>/dev/null; then
+ [ -z "$_log" ] || rm -f "$_log"
tauri_clear_install_error "$_label recovered"
return 0
fi
+ _ric_log=$_log
tauri_stream_log stdout "ERROR_OUTPUT" "$_label failed (exit code $_rc)"
step "error" "$_label failed (exit code $_rc)" "$C_ERR" >&2
return "$_rc"
@@ -265,7 +334,7 @@ run_install_cmd() {
step "error" "$_label failed (exit code $_rc)" "$C_ERR" >&2
_redact_install_output "$_log" >&2
tauri_stream_log stderr "ERROR_OUTPUT" "$_label failed (exit code $_rc)"
- rm -f "$_log"
+ if [ -n "${_UNSLOTH_MIRROR_SPARE:-}" ]; then _ric_log=$_log; else rm -f "$_log"; fi
return $_rc
}
@@ -286,10 +355,11 @@ run_install_cmd_retry() {
_ricr_attempt=1
while :; do
# AND-OR (not `if`) preserves the real failure code for the rollback path.
- run_install_cmd "$@" && return 0
+ _run_install_cmd_once "$@" && return 0
_ricr_rc=$?
if [ "$_ricr_attempt" -ge "$_ricr_max" ]; then
- return "$_ricr_rc"
+ _mirror_retry_install "$_ricr_rc" "$@" && return 0
+ return $?
fi
substep "retrying \"$_ricr_label\" after transient failure (attempt $((_ricr_attempt + 1))/$_ricr_max, waiting ${_ricr_delay}s)..." "$C_WARN"
sleep "$_ricr_delay" || true
@@ -1263,6 +1333,7 @@ _cleanup_install_temporaries() {
[ -n "${_UIP_STAGE:-}" ] && rm -f "$_UIP_STAGE" 2>/dev/null || true
[ -n "${_UIP_STAGE2:-}" ] && rm -f "$_UIP_STAGE2" 2>/dev/null || true
[ -n "${_ROCM_TAG_MEMO_DIR:-}" ] && rm -rf "$_ROCM_TAG_MEMO_DIR" 2>/dev/null || true
+ [ -n "${_ric_log:-}" ] && rm -f "$_ric_log" 2>/dev/null || true
# The probe's ceiling is held by this shell, so a cancel during one would otherwise leave the
# candidate (and, under monitor mode, its whole group) running with nobody left to stop it.
if [ -n "${_UV_PROBE_TARGET:-}" ] && [ -n "${_UV_PROBE_PID:-}" ]; then
@@ -3029,6 +3100,7 @@ _maybe_reroute_strixhalo_to_2404() {
[ "${UNSLOTH_ROCM_WSL_AUTO:-0}" = "1" ] && _rr_exports="$_rr_exports; export UNSLOTH_ROCM_WSL_AUTO=1"
[ -n "${UNSLOTH_TORCH_INDEX_URL:-}" ] && _rr_exports="$_rr_exports; export UNSLOTH_TORCH_INDEX_URL=$(_rr_q "$UNSLOTH_TORCH_INDEX_URL")"
[ -n "${UNSLOTH_TORCH_INDEX_FAMILY:-}" ] && _rr_exports="$_rr_exports; export UNSLOTH_TORCH_INDEX_FAMILY=$(_rr_q "$UNSLOTH_TORCH_INDEX_FAMILY")"
+ [ -n "${UNSLOTH_MIRROR_FALLBACK:-}" ] && _rr_exports="$_rr_exports; export UNSLOTH_MIRROR_FALLBACK=$(_rr_q "$UNSLOTH_MIRROR_FALLBACK")"
[ "$_SKIP_AUTOSTART" = true ] && _rr_exports="$_rr_exports; export UNSLOTH_SKIP_AUTOSTART=1"
_rr_args=""
[ "$PACKAGE_NAME" != "unsloth" ] && _rr_args="$_rr_args --package $(_rr_q "$PACKAGE_NAME")"
@@ -3241,6 +3313,319 @@ case "$OS" in
;;
esac
+# ── BEGIN mirror fallback (kept identical in install.sh and studio/setup.sh) ──
+# Only in mainland China (or UNSLOTH_MIRROR_FALLBACK=1): swaps a default host below 1 MiB/s or unreachable for its mirror when faster; user-set sources untouched; UNSLOTH_MIRROR_FALLBACK=0 disables.
+_MIRROR_CERNET="https://tuna.mirrors.cernet.edu.cn"
+_MIRROR_PYPI="$_MIRROR_CERNET/pypi/web/simple"
+_MIRROR_NPM="https://registry.npmmirror.com"
+# GitHub-release mirrors keep only the newest Python builds, while a pinned uv asks for the builds it shipped with; npmmirror keeps every release.
+_MIRROR_PYTHON="$_MIRROR_NPM/-/binary/python-build-standalone"
+_MIRROR_MIN_BPS=1048576
+
+_mirror_probe() {
+ _mp_out=$(curl -sL -o /dev/null -r "0-$3" -w '%{http_code} %{speed_download}' --connect-timeout "$2" --max-time "$2" "$1" 2>/dev/null) || true
+ _mp_bps=${_mp_out#* }
+ _mp_bps=${_mp_bps%%.*}
+ case "$_mp_bps" in ''|*[!0-9]*) _mp_bps=0 ;; esac
+ _mp_code=${_mp_out%% *}
+ case "$_mp_code" in [0-9][0-9][0-9]) ;; *) _mp_code=000 ;; esac
+ echo "$_mp_code $_mp_bps"
+}
+
+_mirror_url() {
+ case "$1" in
+ pypi) echo "https://files.pythonhosted.org/packages/72/d6/207945fe69903b9794e2ef3e42608c91a59972567343a6719078d99c71f7/uv-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl" ;;
+ cernet-pypi) echo "$_MIRROR_CERNET/pypi/web/packages/72/d6/207945fe69903b9794e2ef3e42608c91a59972567343a6719078d99c71f7/uv-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl" ;;
+ torch) echo "https://download-r2.pytorch.org/whl/cpu/torch-2.9.1%2Bcpu-cp312-cp312-manylinux_2_28_x86_64.whl" ;;
+ cernet-torch) echo "$_MIRROR_CERNET/pytorch/whl/cpu/torch-2.9.1%2Bcpu-cp312-cp312-manylinux_2_28_x86_64.whl" ;;
+ node) echo "https://nodejs.org/dist/v24.18.0/node-v24.18.0-linux-x64.tar.gz" ;;
+ npmmirror-node) echo "$_MIRROR_NPM/-/binary/node/v24.18.0/node-v24.18.0-linux-x64.tar.gz" ;;
+ npm) echo "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz" ;;
+ npmmirror) echo "$_MIRROR_NPM/typescript/-/typescript-5.9.3.tgz" ;;
+ astral) echo "https://releases.astral.sh/github/uv/releases/download/0.12.1/uv-x86_64-unknown-linux-gnu.tar.gz" ;;
+ pypi-index) echo "https://pypi.org/simple/uv/" ;;
+ torch-index) echo "https://download.pytorch.org/whl/cpu/torch/" ;;
+ cernet-pypi-index) echo "$_MIRROR_PYPI/uv/" ;;
+ cernet-torch-index) echo "$_MIRROR_CERNET/pytorch/whl/cpu/torch/" ;;
+ esac
+}
+
+_mirror_default() {
+ case "$1" in python|uvbin) echo astral ;; *) echo "$1" ;; esac
+}
+
+_mirror_source() {
+ case "$1" in npm|python) echo npmmirror ;; node) echo npmmirror-node ;; pypi|uvbin) echo cernet-pypi ;; *) echo "cernet-$1" ;; esac
+}
+
+_mirror_index_probe() {
+ for _mip_name in "$@"; do
+ case "$_mip_name" in
+ pypi|torch|cernet-pypi|cernet-torch)
+ _mirror_probe "$(_mirror_url "$_mip_name-index")" 4 1023 > "$_mf_dir/$_mip_name-index" &
+ _mf_pids="$_mf_pids $!" ;;
+ esac
+ done
+}
+
+_mirror_index_wait() {
+ for _miw_pid in $_mf_pids; do
+ wait "$_miw_pid" || true
+ done
+ _mf_pids=""
+}
+
+_mirror_index_ok() {
+ [ -f "$_mf_dir/$1-index" ] || return 0
+ read -r _mio_code _mio_bps < "$_mf_dir/$1-index"
+ case "$_mio_code" in 2??) return 0 ;; *) return 1 ;; esac
+}
+
+_mirror_uv_project_config() {
+ _mup_dir=$PWD
+ while [ -n "$_mup_dir" ]; do
+ if [ -f "$_mup_dir/uv.toml" ]; then echo "$_mup_dir/uv.toml"; return 0; fi
+ if grep -Eqs '^[[:space:]]*\[+tool\.uv(\.|\])' "$_mup_dir/pyproject.toml"; then echo "$_mup_dir/pyproject.toml"; return 0; fi
+ [ "$_mup_dir" = / ] && return 0
+ _mup_dir=$(dirname "$_mup_dir")
+ done
+}
+
+_mirror_configured() {
+ case "$1" in
+ uv)
+ [ -n "${UV_DEFAULT_INDEX:-}${UV_INDEX_URL:-}${UV_INDEX:-}${UV_EXTRA_INDEX_URL:-}" ] && return 0
+ _mic_key='\[\[(tool\.uv\.)?index\]\]|(pip\.)?(index|index-url|default-index|extra-index-url|no-index)[[:space:]]*=' ;;
+ python)
+ [ -n "${UV_PYTHON_INSTALL_MIRROR:-}" ] && return 0
+ _mic_key='python-install-mirror[[:space:]]*=' ;;
+ pip)
+ [ -n "${PIP_INDEX_URL:-}${PIP_EXTRA_INDEX_URL:-}${PIP_NO_INDEX:-}" ] && return 0
+ _mic_key='(index[-_]url|extra[-_]index[-_]url|no[-_]index)[[:space:]]*[=:]' ;;
+ esac
+ _mic_suffix=uv/uv.toml
+ [ "$1" != pip ] || _mic_suffix=pip/pip.conf
+ if [ "$1" = pip ]; then
+ set -- "${PIP_CONFIG_FILE:-}" "${VENV_DIR:+$VENV_DIR/pip.conf}" "${XDG_CONFIG_HOME:-$HOME/.config}/pip/pip.conf" "$HOME/.pip/pip.conf" "$HOME/Library/Application Support/pip/pip.conf" /etc/xdg/pip/pip.conf /etc/pip.conf
+ else
+ set -- "${UV_CONFIG_FILE:-}" "$(_mirror_uv_project_config)" "${XDG_CONFIG_HOME:-$HOME/.config}/uv/uv.toml" /etc/xdg/uv/uv.toml /etc/uv/uv.toml
+ fi
+ _mic_xdg=${XDG_CONFIG_DIRS:-}
+ while [ -n "$_mic_xdg" ]; do
+ set -- "$@" "${_mic_xdg%%:*}/$_mic_suffix"
+ case "$_mic_xdg" in *:*) _mic_xdg=${_mic_xdg#*:} ;; *) _mic_xdg="" ;; esac
+ done
+ for _mic_file in "$@"; do
+ if [ -f "$_mic_file" ] && grep -Eq "^[[:space:]]*($_mic_key)" "$_mic_file" 2>/dev/null; then
+ return 0
+ fi
+ done
+ return 1
+}
+
+_mirror_probe_all() {
+ _mpa_dir="$1"
+ _mpa_secs="$2"
+ shift 2
+ _mpa_pids=""
+ for _mpa_name in "$@"; do
+ _mirror_probe "$(_mirror_url "$_mpa_name")" "$_mpa_secs" 1048575 > "$_mpa_dir/$_mpa_name" &
+ _mpa_pids="$_mpa_pids $!"
+ done
+ for _mpa_pid in $_mpa_pids; do
+ wait "$_mpa_pid" || true
+ done
+}
+
+_mirror_vars() {
+ case "$1" in
+ pypi)
+ [ "$_mf_uv" = false ] || echo "UV_DEFAULT_INDEX=$_MIRROR_PYPI"
+ [ "$_mf_pip" = false ] || echo "PIP_INDEX_URL=$_MIRROR_PYPI" ;;
+ unsynced)
+ # Only for one rerun: uv's unsafe-first-match fetches every package from every index, and fails outright when one is unreachable.
+ [ "$_mf_uv" = false ] || echo "UV_DEFAULT_INDEX=https://pypi.org/simple UV_INDEX=$_MIRROR_PYPI UV_INDEX_STRATEGY=${UV_INDEX_STRATEGY:-unsafe-first-match}"
+ [ "$_mf_pip" = false ] || echo "PIP_EXTRA_INDEX_URL=https://pypi.org/simple PIP_INDEX_URL=$_MIRROR_PYPI" ;;
+ torch) echo "UNSLOTH_PYTORCH_MIRROR=$_MIRROR_CERNET/pytorch/whl" ;;
+ node) echo "UNSLOTH_NODE_MIRROR=$_MIRROR_NPM/-/binary/node" ;;
+ npm) echo "UNSLOTH_NPM_REGISTRY=$_MIRROR_NPM" ;;
+ python) echo "UV_PYTHON_INSTALL_MIRROR=$_MIRROR_PYTHON" ;;
+ uvbin) echo "UNSLOTH_UV_WHEEL_MIRROR=$_MIRROR_CERNET/pypi/web" ;;
+ esac
+}
+
+_mirror_name() {
+ case "$1" in
+ pypi) echo "PyPI" ;;
+ unsynced) echo "The PyPI mirror" ;;
+ torch) echo "download.pytorch.org" ;;
+ node) echo "nodejs.org" ;;
+ npm) echo "registry.npmjs.org" ;;
+ python) echo "releases.astral.sh (Python builds)" ;;
+ uvbin) echo "releases.astral.sh (uv)" ;;
+ esac
+}
+
+_mirror_use() {
+ _mu_to=""
+ for _mu_pair in $(_mirror_vars "$1"); do
+ export "$_mu_pair"
+ [ -n "$_mu_to" ] || _mu_to=${_mu_pair#*=}
+ done
+ step "mirror" "$(_mirror_name "$1") is $2 ($(($3 / 1024)) KB/s, mirror $(($4 / 1024)) KB/s); using $_mu_to" "$C_WARN"
+ _mf_switched="$_mf_switched $1"
+ [ "$1 $2" != "pypi slow" ] || _mf_unsynced=true
+}
+
+_mirror_take() {
+ _MT_PAIRS=""
+ _mt_spare=""
+ for _mt_entry in ${_UNSLOTH_MIRROR_SPARE:-}; do
+ if [ "${_mt_entry%%|*}" = "$1" ]; then
+ _MT_PAIRS=$(printf '%s' "${_mt_entry#*|}" | tr '|' ' ')
+ else
+ _mt_spare="$_mt_spare $_mt_entry"
+ fi
+ done
+ [ -n "$_MT_PAIRS" ] || return 1
+ export _UNSLOTH_MIRROR_SPARE="${_mt_spare# }"
+ _mt_to=${_MT_PAIRS%% *}
+ step "mirror" "$(_mirror_name "$1") failed; retrying through ${_mt_to#*=}" "$C_WARN"
+}
+
+_mirror_switch() {
+ _mirror_take "$1" || return 1
+ for _ms_pair in $_MT_PAIRS; do export "$_ms_pair"; done
+}
+
+_mirror_failed_host() {
+ if ! grep -Eqi 'error sending request|timed out|network timeout|idle timeout|connection (reset|refused|closed|aborted)|network aborted|broken pipe|dns error|failed to lookup address|name resolution|nodename nor servname|network is unreachable|error decoding response body|end of file before message length|unexpected eof|tls handshake|sslerror|certificate verify failed|server error|service unavailable|bad gateway|gateway time-?out|too many requests|max retries exceeded|remotedisconnected|incompleteread|econnreset|etimedout|eidletimeout|eai_again|enotfound|econnrefused|socket hang up' "$1" 2>/dev/null; then
+ grep -Eqi 'only [^ ]+ (.* )?(is|are) available|no versions? of|not found in the package registry|could not find a version that satisfies|no matching distribution found' "$1" 2>/dev/null || return 1
+ echo unsynced
+ return 0
+ fi
+ if grep -Eq 'download(-r2)?\.pytorch\.org' "$1"; then echo torch
+ elif grep -q 'python-build-standalone' "$1"; then echo python
+ elif grep -q 'registry\.npmjs\.org' "$1"; then echo npm
+ elif grep -Eq 'pypi\.org|pythonhosted\.org' "$1"; then echo pypi
+ elif [ -n "${2:-}" ] && ! grep -Eq 'https?://' "$1"; then echo "$2"
+ else return 1
+ fi
+}
+
+# No network call: a mainland China time zone, or a resolver from a mainland public DNS or cloud (the addresses below).
+_mirror_in_china() {
+ _mcn_tz=${TZ:-}
+ [ -n "$_mcn_tz" ] || _mcn_tz=$(cat /etc/timezone 2>/dev/null) || true
+ [ -n "$_mcn_tz" ] || _mcn_tz=$(readlink /etc/localtime 2>/dev/null) || true
+ case "${_mcn_tz#:}" in
+ *Asia/Shanghai|*Asia/Chongqing|*Asia/Chungking|*Asia/Harbin|*Asia/Urumqi|*Asia/Kashgar|PRC|*/PRC) return 0 ;;
+ esac
+ grep -Eqs '^[[:space:]]*nameserver[[:space:]]+(223\.5\.5\.5|223\.6\.6\.6|119\.29\.29\.29|114\.114\.11[45]\.11[0459]|182\.254\.116\.116|119\.28\.28\.28|180\.76\.76\.76|1\.2\.4\.8|210\.2\.4\.8|100\.100\.2\.13[68]|183\.60\.8[23]\.(19|98))[[:space:]]*$' /etc/resolv.conf /run/systemd/resolve/resolv.conf
+}
+
+# Decided once per process; when off, a retry state inherited from a parent is dropped so nothing downstream acts on it.
+_mirror_enabled() {
+ if [ -z "${_mirror_on:-}" ]; then
+ case "${UNSLOTH_MIRROR_FALLBACK:-}" in
+ 0|false|False|FALSE|no|off) _mirror_on=no ;;
+ 1|true|True|TRUE|yes|on) _mirror_on=yes ;;
+ *) if _mirror_in_china; then _mirror_on=yes; else _mirror_on=no; fi ;;
+ esac
+ [ "$_mirror_on" = yes ] || unset _UNSLOTH_MIRROR_SPARE
+ fi
+ [ "$_mirror_on" = yes ]
+}
+
+_mirror_fallback() {
+ _mirror_enabled || return 0
+ [ -z "${_UNSLOTH_MIRROR_PROBED:-}" ] || return 0
+ command -v curl >/dev/null 2>&1 || return 0
+ [ "${1:-}" = spare ] || export _UNSLOTH_MIRROR_PROBED=1
+ _mf_uv=true
+ _mf_pip=true
+ _mf_switched=""
+ _mf_unsynced=false
+ _mirror_configured uv && _mf_uv=false
+ _mirror_configured pip && _mf_pip=false
+ _mf_hosts=""
+ if [ "$_mf_uv" = true ] || [ "$_mf_pip" = true ]; then
+ _mf_hosts="pypi"
+ fi
+ [ -n "${UNSLOTH_PYTORCH_MIRROR:-}${UNSLOTH_TORCH_INDEX_URL:-}" ] || _mf_hosts="$_mf_hosts torch"
+ [ -n "${UNSLOTH_NODE_MIRROR:-}" ] || _mf_hosts="$_mf_hosts node"
+ [ -n "${UNSLOTH_NPM_REGISTRY:-}${NPM_CONFIG_REGISTRY:-}${npm_config_registry:-}" ] || _mf_hosts="$_mf_hosts npm"
+ _mirror_configured python || _mf_hosts="$_mf_hosts python"
+ [ -n "${UNSLOTH_UV_WHEEL_MIRROR:-}${UV_DOWNLOAD_URL:-}${INSTALLER_DOWNLOAD_URL:-}${UV_INSTALLER_GHE_BASE_URL:-}${UV_INSTALLER_GITHUB_BASE_URL:-}" ] || _mf_hosts="$_mf_hosts uvbin"
+ [ -n "$_mf_hosts" ] || return 0
+ # UV_OFFLINE (uv's spellings) asked for no network: arm the retries, probe nothing.
+ _mf_uvo=${UV_OFFLINE:-}
+ _mf_uvo=${_mf_uvo#"${_mf_uvo%%[![:space:]]*}"}
+ _mf_uvo=${_mf_uvo%"${_mf_uvo##*[![:space:]]}"}
+ case "${1:-}/$_mf_uvo" in
+ spare/* | */1 | */[Tt] | */[Tt][Rr][Uu][Ee] | */[Yy] | */[Yy][Ee][Ss] | */[Oo][Nn]) _mirror_spare_export; return 0 ;;
+ esac
+ _mf_dir=$(mktemp -d 2>/dev/null) || return 0
+ _mf_pids=""
+ _mirror_index_probe $_mf_hosts
+ for _mf_name in $(for _mf_host in $_mf_hosts; do _mirror_default "$_mf_host"; done | sort -u); do
+ _mirror_probe "$(_mirror_url "$_mf_name")" 1.5 1048575 > "$_mf_dir/$_mf_name"
+ done
+ _mirror_index_wait
+ _mf_slow=""
+ for _mf_host in $_mf_hosts; do
+ read -r _mf_code _mf_bps < "$_mf_dir/$(_mirror_default "$_mf_host")"
+ _mirror_index_ok "$_mf_host" || _mf_code=000
+ case "$_mf_code" in
+ 000|3??) _mf_slow="$_mf_slow $_mf_host" ;;
+ 2??) [ "$_mf_bps" -ge "$_MIRROR_MIN_BPS" ] || _mf_slow="$_mf_slow $_mf_host" ;;
+ esac
+ done
+ if [ -n "$_mf_slow" ]; then
+ _mirror_index_probe $_mf_slow $(for _mf_host in $_mf_slow; do echo "cernet-$_mf_host"; done)
+ _mirror_probe_all "$_mf_dir" 4 $(for _mf_host in $_mf_slow; do _mirror_default "$_mf_host"; _mirror_source "$_mf_host"; done | sort -u)
+ _mirror_index_wait
+ for _mf_host in $_mf_slow; do
+ read -r _mf_code _mf_bps < "$_mf_dir/$(_mirror_default "$_mf_host")"
+ read -r _mf_mcode _mf_mbps < "$_mf_dir/$(_mirror_source "$_mf_host")"
+ _mirror_index_ok "$_mf_host" || _mf_code=000
+ _mirror_index_ok "cernet-$_mf_host" || _mf_mcode=000
+ case "$_mf_code" in
+ 2??) _mf_how=slow ;;
+ *) _mf_how=blocked; _mf_bps=0 ;;
+ esac
+ case "$_mf_mcode" in
+ 2??) [ "$_mf_bps" -lt "$_MIRROR_MIN_BPS" ] && [ "$_mf_mbps" -gt "$_mf_bps" ] && _mirror_use "$_mf_host" "$_mf_how" "$_mf_bps" "$_mf_mbps" ;;
+ esac
+ done
+ if [ -n "$_mf_switched" ]; then
+ substep "Set UNSLOTH_MIRROR_FALLBACK=0 to always use the default hosts."
+ fi
+ fi
+ rm -rf "$_mf_dir"
+ _mirror_spare_export
+}
+
+_mirror_spare_export() {
+ _mf_spare=""
+ for _mf_host in $_mf_hosts; do
+ case " $_mf_switched " in *" $_mf_host "*) continue ;; esac
+ _mf_entry=""
+ for _mf_pair in $(_mirror_vars "$_mf_host"); do
+ _mf_entry="$_mf_entry|$_mf_pair"
+ done
+ [ -z "$_mf_entry" ] || _mf_spare="$_mf_spare $_mf_host$_mf_entry"
+ done
+ if [ "$_mf_unsynced" = true ]; then
+ _mf_spare="$_mf_spare unsynced"
+ for _mf_pair in $(_mirror_vars unsynced); do
+ _mf_spare="$_mf_spare|$_mf_pair"
+ done
+ fi
+ export _UNSLOTH_MIRROR_SPARE="${_mf_spare# }"
+}
+# ── END mirror fallback ──
+
# ── Install uv ──
tauri_log "STEP" "Installing uv package manager"
# 0.9.3 is the first uv whose managed-Python manifest carries CPython 3.13.9. Anything older tops out at 3.13.8, which cannot import torch (see PYTHON_SKIP), so a bare "3.13" request on an older uv resolves straight to the broken patch.
@@ -3265,6 +3650,8 @@ fi
[ -n "${UV_SYSTEM_CERTS:-}" ] && export UV_SYSTEM_CERTS
[ -n "${UV_NATIVE_TLS:-}" ] && export UV_NATIVE_TLS
+_mirror_fallback
+
version_ge() {
# returns 0 if $1 >= $2
_a=$1
@@ -3345,7 +3732,7 @@ _uv_version_ok() { # uv command, floor (defaults to UV_MIN_VERSION)
}
# ── uv from a pinned release ──
-# Same archive, destination and PATH treatment as astral's installer, but it fetches a data file with a pinned SHA-256 instead of a script it runs and deletes. Mirrors Install-UvFromRelease in install.ps1. Bumping the version means bumping every hash, from https://github.com/astral-sh/uv/releases/download/<ver>/<asset>.sha256. Only the four mainstream targets are pinned; musl, armv7 and the rest fall through to the caller's existing path rather than risk a wrong triple.
+# Mirrors Install-UvFromRelease in install.ps1; bumping the version means bumping every hash (<asset>.sha256) and every _uv_pinned_wheel entry.
UV_PINNED_VERSION="0.12.1"
# Echoes the glibc minor version (the N in 2.N), or nothing when this is not a glibc host or the version cannot be read. "not musl" is not the same as "a glibc new enough to run the GNU build": astral's installer checks a minimum and drops to its musl-static archive below it, so a host we cannot positively confirm has to reach the fallback rather than take a binary that will not exec.
@@ -3404,6 +3791,30 @@ _uv_pinned_asset() {
return 0
}
+_uv_pinned_wheel() {
+ case "$1" in
+ uv-x86_64-unknown-linux-gnu.tar.gz)
+ echo "packages/72/d6/207945fe69903b9794e2ef3e42608c91a59972567343a6719078d99c71f7/uv-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl 27211df9b277f440dea438a4e525ba40250fb721ad39b8927eefc2d91f9aea15" ;;
+ uv-aarch64-unknown-linux-gnu.tar.gz)
+ echo "packages/9a/c7/29e426865c2eb8df61253dae93b953523f48c9fca1e471c7e49ff068f19a/uv-0.12.1-py3-none-manylinux_2_28_aarch64.whl b255ac23958e45f39f9c7a4cd65890df5ef46f539a3b14de03bd296bbba9cb60" ;;
+ uv-x86_64-apple-darwin.tar.gz)
+ echo "packages/fd/07/a417475380e901f4325d13b09938baab227b0c143547124b944c5bc71783/uv-0.12.1-py3-none-macosx_10_12_x86_64.whl 41b8fc2335f682312a1ca39a7b4abfd6af800992065c663582ca3e4d51cf9258" ;;
+ uv-aarch64-apple-darwin.tar.gz)
+ echo "packages/c9/68/391ff0cc3d8020e64adc43bb4e50607f744c69e792fb7623dc7c1526704b/uv-0.12.1-py3-none-macosx_11_0_arm64.whl 2e9b0b86e180abc5968b979c6e25203b32e85969abb5083ee1e8b88a5aa98a76" ;;
+ *) return 1 ;;
+ esac
+}
+
+# A wheel is a zip, which GNU tar cannot read and minimal Linux images often have no unzip for.
+_uv_unzip() {
+ if command -v unzip >/dev/null 2>&1 && unzip -qo "$1" -d "$2" >/dev/null 2>&1; then return 0; fi
+ case "$(tar --version 2>/dev/null)" in
+ *bsdtar*) tar -xf "$1" -C "$2" 2>/dev/null && return 0 ;;
+ esac
+ command -v python3 >/dev/null 2>&1 &&
+ python3 -c 'import sys, zipfile; zipfile.ZipFile(sys.argv[1]).extractall(sys.argv[2])' "$1" "$2" 2>/dev/null
+}
+
# Echoes the SHA-256 of "$1", or nothing when the host has no digest tool.
_uv_sha256() {
if command -v sha256sum >/dev/null 2>&1; then
@@ -3493,6 +3904,7 @@ _uv_probe_exec() {
}
_uv_install_pinned() {
+ _UIP_UNFETCHED=false
_uip_spec=$(_uv_pinned_asset) || return 1
[ -n "$_uip_spec" ] || return 1
_uip_asset=${_uip_spec%% *}
@@ -3525,13 +3937,23 @@ _uv_install_pinned() {
_uip_bases="${UV_INSTALLER_GHE_BASE_URL%/}/astral-sh/uv/releases/download/$UV_PINNED_VERSION"
elif [ -n "${UV_INSTALLER_GITHUB_BASE_URL:-}" ]; then
_uip_bases="${UV_INSTALLER_GITHUB_BASE_URL%/}/astral-sh/uv/releases/download/$UV_PINNED_VERSION"
+ elif [ -n "${UNSLOTH_UV_WHEEL_MIRROR:-}" ]; then
+ _uip_bases=""
+ if _uip_wheel=$(_uv_pinned_wheel "$_uip_asset"); then
+ _uip_path=${_uip_wheel% *}
+ _uip_bases="${UNSLOTH_UV_WHEEL_MIRROR%/}/${_uip_path%/*}"
+ _uip_asset=${_uip_path##*/}
+ _uip_want=${_uip_wheel##* }
+ fi
else
_uip_bases="https://releases.astral.sh/github/uv/releases/download/$UV_PINNED_VERSION
https://github.com/astral-sh/uv/releases/download/$UV_PINNED_VERSION"
fi
+ _UIP_UNFETCHED=true
for _uip_base in $_uip_bases; do
# 2>/dev/null: curl -sS prints its own errors and these attempts are speculative, so an unreachable mirror stays off the console when the install still succeeds.
if ! download "$_uip_base/$_uip_asset" "$_uip_work/$_uip_asset" 2>/dev/null; then continue; fi
+ _UIP_UNFETCHED=false
_uip_got=$(_uv_sha256 "$_uip_work/$_uip_asset")
if [ "$_uip_got" != "$_uip_want" ]; then
# Not tauri_log: [TAURI:WARN] is a marker install.sh has never emitted, and the app forwards unknown markers to its progress UI verbatim. Verbose only, since the next mirror or the fallback still runs.
@@ -3540,8 +3962,10 @@ https://github.com/astral-sh/uv/releases/download/$UV_PINNED_VERSION"
fi
continue
fi
- # The POSIX archives hold uv and uvx under a uv-<triple>/ directory.
- if ! tar -xzf "$_uip_work/$_uip_asset" -C "$_uip_work" 2>/dev/null; then continue; fi
+ case "$_uip_asset" in
+ *.whl) _uv_unzip "$_uip_work/$_uip_asset" "$_uip_work" || continue ;;
+ *) tar -xzf "$_uip_work/$_uip_asset" -C "$_uip_work" 2>/dev/null || continue ;;
+ esac
mkdir -p "$_uip_dest" 2>/dev/null || break
_uip_placed=0
# uv first, and either half failing aborts the placement: the two ship as a set, and a pinned uvx beside the host's older uv is a pairing we never built or tested. Stage both, then publish both: the renames sit next to each other so the pair is replaced as one, and a failure anywhere before them leaves the destination untouched.
@@ -3601,7 +4025,7 @@ if ! command -v uv >/dev/null 2>&1 || ! _uv_version_ok uv; then
# download() exits the shell outright when neither curl nor wget is present, which an `if` cannot catch, so probe first: a minimal image with uv copied in but no downloader must keep the install it had before the floor moved.
if command -v curl >/dev/null 2>&1 || command -v wget >/dev/null 2>&1; then
# Pinned release first: fetch a digest-checked data file rather than download-run-delete a remote script. See tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
- if _uv_install_pinned; then
+ if _uv_install_pinned || { [ "$_UIP_UNFETCHED" = true ] && _mirror_switch uvbin && _uv_install_pinned; }; then
:
else
# Unpinned hosts keep the path they have always had: a wrong triple breaks the install outright, which costs more than the fallback's score.
diff --git a/scripts/sync_shared_ps1_helpers.py b/scripts/sync_shared_ps1_helpers.py
index ddd2ff64b8c..3d887c91f3c 100644
--- a/scripts/sync_shared_ps1_helpers.py
+++ b/scripts/sync_shared_ps1_helpers.py
@@ -51,7 +51,7 @@ def dedented(source: str) -> str:
"""Drop the common indentation, which is the only difference allowed."""
lines = source.splitlines()
body = [line for line in lines if line.strip()]
- indent = min(len(line) - len(line.lstrip(" ")) for line in body)
+ indent = min((len(line) - len(line.lstrip(" ")) for line in body), default = 0)
return "\n".join(line[indent:] if line.strip() else "" for line in lines)
@@ -100,7 +100,7 @@ def main() -> int:
# run into the function keyword and hide the declaration from every
# reader of this file, this script included.
comment = dedented(install_text[leading_comment_start(install_text, start) : start])
- comment = comment.rstrip("\n") + "\n"
+ comment = comment.rstrip("\n") + "\n" if comment else ""
_, after = function_span(setup_text, previous)
setup_text = setup_text[:after] + "\n\n" + comment + wanted + setup_text[after:]
stale.append(name)
diff --git a/studio/install_node_prebuilt.py b/studio/install_node_prebuilt.py
index a483181eb7b..6451f59d73d 100644
--- a/studio/install_node_prebuilt.py
+++ b/studio/install_node_prebuilt.py
@@ -4,7 +4,8 @@
"""Cross-platform Node.js prebuilt installer for Unsloth Studio.
-Downloads an official Node.js archive from nodejs.org into an isolated
+Downloads an official Node.js archive from nodejs.org (or the dist mirror in
+``UNSLOTH_NODE_MIRROR``) into an isolated
``<UNSLOTH_HOME>/node`` and never touches the system Node/npm. Pinning Node 24+
LTS clears the Unsloth frontend build floor (Vite 8: Node ^20.19 || >=22.12,
npm >= 11) with the npm it bundles.
@@ -75,7 +76,7 @@ NODE_MIN_LTS_MAJOR = 24
NPM_MIN_MAJOR = 11
NODE_DIST_BASE = "https://nodejs.org/dist"
-NODE_DIST_INDEX = f"{NODE_DIST_BASE}/index.json"
+NODE_MIRROR_ENV = "UNSLOTH_NODE_MIRROR"
RETRYABLE_HTTP_STATUS = {408, 429, 500, 502, 503, 504}
HTTP_FETCH_ATTEMPTS = 4
@@ -174,12 +175,20 @@ def node_asset_name(version: str, host: HostInfo) -> str:
return f"{node_asset_stem(version, host)}{host.archive_ext}"
+def node_dist_base() -> str:
+ return (os.environ.get(NODE_MIRROR_ENV, "").strip() or NODE_DIST_BASE).rstrip("/")
+
+
+def node_dist_index_url() -> str:
+ return f"{node_dist_base()}/index.json"
+
+
def node_download_url(version: str, asset_name: str) -> str:
- return f"{NODE_DIST_BASE}/v{version}/{asset_name}"
+ return f"{node_dist_base()}/v{version}/{asset_name}"
def node_shasums_url(version: str) -> str:
- return f"{NODE_DIST_BASE}/v{version}/SHASUMS256.txt"
+ return f"{node_dist_base()}/v{version}/SHASUMS256.txt"
def expected_sha256_for(shasums_text: str, asset_name: str) -> str | None:
@@ -211,7 +220,7 @@ def _meets_node_floor(version: str) -> bool:
def select_node_version(index: list[dict], *, channel: str, min_major: int) -> str:
- """Pick a concrete Node version from nodejs.org index.json.
+ """Pick a concrete Node version from the Node dist index.json.
channel='lts' -> newest LTS release line whose major >= min_major.
channel='latest' -> newest release overall whose major >= min_major.
@@ -234,7 +243,7 @@ def select_node_version(index: list[dict], *, channel: str, min_major: int) -> s
best_version = version
if best_version is None:
raise PrebuiltFallback(
- f"no Node '{channel}' release found at or above major {min_major} in {NODE_DIST_INDEX}"
+ f"no Node '{channel}' release found at or above major {min_major} in {node_dist_index_url()}"
)
return best_version
@@ -1060,15 +1069,14 @@ def install_prebuilt(install_dir: Path, *, channel: str, min_major: int, force:
version = pinned_default_version(pins)
elif channel in {"lts", "latest"}:
try:
- index = fetch_json(NODE_DIST_INDEX)
+ index = fetch_json(node_dist_index_url())
except Exception as exc: # noqa: BLE001
- # nodejs.org unreachable: keep a working isolated Node instead of aborting.
if not force and existing_install_usable(install_dir, host):
log(f"Node dist index unreachable ({exc}); keeping existing isolated Node")
return EXIT_SUCCESS
raise
if not isinstance(index, list):
- raise PrebuiltFallback(f"unexpected index.json payload from {NODE_DIST_INDEX}")
+ raise PrebuiltFallback(f"unexpected index.json payload from {node_dist_index_url()}")
version = select_node_version(index, channel = channel, min_major = min_major)
else:
version = channel.lstrip("v")
diff --git a/studio/install_python_stack.py b/studio/install_python_stack.py
index 4fd089124b2..92469e7aac1 100644
--- a/studio/install_python_stack.py
+++ b/studio/install_python_stack.py
@@ -9410,7 +9410,92 @@ def pip_install_try(
if VERBOSE and result.stdout:
# pip/uv echo index URLs (credentials included) in failure output.
_safe_print(_redact_install_output(result.stdout))
- return False
+ return bool(
+ _mirror_retry(
+ args,
+ result.stdout or b"",
+ lambda *retry: pip_install_try(
+ label, *retry, req = req, constrain = constrain, force_pip = force_pip
+ ),
+ )
+ )
+
+
+_PYTORCH_DEFAULT_WHL = "https://download.pytorch.org/whl"
+_MIRROR_TRANSPORT_ERROR = re.compile(
+ r"error sending request|timed out|network timeout|connection (reset|refused|closed|aborted)|"
+ r"broken pipe|dns error|failed to lookup address|name resolution|nodename nor servname|"
+ r"network is unreachable|error decoding response body|end of file before message length|"
+ r"unexpected eof|tls handshake|sslerror|"
+ r"certificate verify failed|server error|service unavailable|bad gateway|gateway time-?out|"
+ r"too many requests|max retries exceeded|remotedisconnected|incompleteread",
+ re.IGNORECASE,
+)
+_MIRROR_HOST_NAMES = (
+ ("torch", re.compile(r"download(-r2)?\.pytorch\.org")),
+ ("pypi", re.compile(r"pypi\.org|pythonhosted\.org")),
+)
+_MIRROR_NAMES = {"torch": "download.pytorch.org", "pypi": "PyPI", "unsynced": "The PyPI mirror"}
+_MIRROR_UNSYNCED = re.compile(
+ r"only \S+ (.* )?(is|are) available|no versions? of|not found in the package registry|"
+ r"could not find a version that satisfies|no matching distribution found",
+ re.IGNORECASE,
+)
+_failed_install_output = b""
+
+
+def _mirror_retry(args: "tuple[str, ...]", output: bytes, rerun) -> "bool | None":
+ """Reruns a failed install once through the mirror of the host its output shows failing.
+
+ The installer's probe exports ``_UNSLOTH_MIRROR_SPARE`` as ``host|VAR=URL|...`` entries for
+ the hosts it left on their defaults; each gets one rerun, and later installs keep the mirror
+ only when it worked. None when there is no such host.
+ """
+ global _PYTORCH_WHL_BASE
+ if not os.environ.get("_UNSLOTH_MIRROR_SPARE", "").strip():
+ return None
+ text = output.decode("utf-8", "replace")
+ torch = any(_PYTORCH_DEFAULT_WHL in arg for arg in args)
+ if not _MIRROR_TRANSPORT_ERROR.search(text):
+ if _is_pinned_index_cmd(args) or "--no-index" in args or not _MIRROR_UNSYNCED.search(text):
+ return None
+ host = "unsynced"
+ else:
+ pinned = _is_pinned_index_cmd(args) or any(
+ arg in ("--find-links", "--no-index") or "://" in arg for arg in args
+ )
+ host = next((name for name, pattern in _MIRROR_HOST_NAMES if pattern.search(text)), None)
+ if host is None and not re.search(r"https?://", text):
+ host = "torch" if torch else "pypi"
+ if host is None or (host == "torch" and not torch) or (host == "pypi" and pinned):
+ return None
+ spare = os.environ.get("_UNSLOTH_MIRROR_SPARE", "").split()
+ entry = next((e for e in spare if e.split("|", 1)[0] == host), None)
+ if entry is None:
+ return None
+ os.environ["_UNSLOTH_MIRROR_SPARE"] = " ".join(e for e in spare if e != entry)
+ pairs = dict(pair.split("=", 1) for pair in entry.split("|")[1:])
+ _step(
+ "mirror",
+ f"{_MIRROR_NAMES[host]} failed; retrying through {next(iter(pairs.values()))}",
+ _cyan,
+ )
+ saved = ({name: os.environ.get(name) for name in pairs}, _PYTORCH_WHL_BASE)
+ os.environ.update(pairs)
+ if host == "torch" and _PYTORCH_WHL_BASE == _PYTORCH_DEFAULT_WHL:
+ _PYTORCH_WHL_BASE = pairs["UNSLOTH_PYTORCH_MIRROR"].rstrip("/")
+ ok = False
+ try:
+ ok = rerun(*(arg.replace(_PYTORCH_DEFAULT_WHL, _PYTORCH_WHL_BASE) for arg in args))
+ finally:
+ if not ok:
+ for name, value in saved[0].items():
+ if value is None:
+ os.environ.pop(name, None)
+ else:
+ os.environ[name] = value
+ _PYTORCH_WHL_BASE = saved[1]
+ return ok
def pip_install(
@@ -9420,6 +9505,24 @@ def pip_install(
constrain: bool = True,
) -> None:
"""Build and run a pip install command (uses uv when available, falls back to pip)."""
+ try:
+ _pip_install_once(label, *args, req = req, constrain = constrain)
+ except SystemExit:
+ rerun = (
+ lambda *retry: _pip_install_once(label, *retry, req = req, constrain = constrain) or True
+ )
+ if not _mirror_retry(args, _failed_install_output, rerun):
+ raise
+
+
+def _pip_install_once(
+ label: str,
+ *args: str,
+ req: Path | None = None,
+ constrain: bool = True,
+) -> None:
+ global _failed_install_output
+ _failed_install_output = b""
# Any pip operation can change which torch is installed, so the memoized
# classification must not outlive it.
_invalidate_torch_runtime_probe()
@@ -9467,6 +9570,7 @@ def pip_install(
if VERBOSE and result.stdout:
_safe_print(_redact_install_output(result.stdout))
return
+ _failed_install_output = result.stdout or b""
if _woa_overrides_are_load_bearing():
_step("error", f"{label} failed and pip cannot stand in for it", _red)
_safe_print(
@@ -9506,6 +9610,7 @@ def pip_install(
pip_label = f"{label} (pip)" if USE_UV else label
result = run(pip_label, pip_cmd, check = False, env = pip_env)
if result.returncode != 0:
+ _failed_install_output += result.stdout or b""
# Retry once, and only after clearing something pip named as
# unremovable: a blind retry of a failing install just doubles the wait.
cleared = _purge_recordless_distributions(result.stdout)
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index d47b63c5e56..ee29f8b0116 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -141,12 +141,6 @@ $DefaultLlamaSource = "https://github.com/ggml-org/llama.cpp"
$DefaultLlamaTag = "latest"
$DefaultLlamaForceCompileRef = "master"
-# UNSLOTH_NPM_REGISTRY: opt-in --registry splat past the frontend .npmrc lock (corporate proxies).
-$NpmRegistryArgs = @()
-if ($env:UNSLOTH_NPM_REGISTRY) {
- $NpmRegistryArgs = @('--registry', $env:UNSLOTH_NPM_REGISTRY)
-}
-
# Verbose can be enabled either by CLI flag or by UNSLOTH_VERBOSE=1.
$script:UnslothVerbose = ($env:UNSLOTH_VERBOSE -eq '1')
foreach ($a in $args) {
@@ -896,6 +890,244 @@ function Invoke-ManagedLlamaCppPreflight {
return "$reason Nothing was installed."
}
+function Test-MirrorConfigured {
+ param([ValidateSet('uv', 'pip')][string]$Tool)
+ if ($Tool -eq 'uv') {
+ if ("$env:UV_DEFAULT_INDEX$env:UV_INDEX_URL$env:UV_INDEX$env:UV_EXTRA_INDEX_URL") { return $true }
+ $pattern = '^\s*(\[\[(tool\.uv\.)?index\]\]|(pip\.)?(index|index-url|default-index|extra-index-url|no-index)\s*=)'
+ $files = @($env:UV_CONFIG_FILE, "$env:APPDATA\uv\uv.toml", "$env:ProgramData\uv\uv.toml")
+ $dir = (Get-Location -PSProvider FileSystem).ProviderPath
+ while ($dir) {
+ $pyproject = Join-Path $dir 'pyproject.toml'
+ if (Test-Path -LiteralPath (Join-Path $dir 'uv.toml') -PathType Leaf) { $files += Join-Path $dir 'uv.toml'; break }
+ if ((Test-Path -LiteralPath $pyproject -PathType Leaf) -and
+ (Select-String -LiteralPath $pyproject -Pattern '^\s*\[+tool\.uv(\.|\])' -Quiet -ErrorAction SilentlyContinue)) { $files += $pyproject; break }
+ $dir = Split-Path -Parent $dir
+ }
+ } else {
+ if ("$env:PIP_INDEX_URL$env:PIP_EXTRA_INDEX_URL$env:PIP_NO_INDEX") { return $true }
+ $pattern = '^\s*(index[-_]url|extra[-_]index[-_]url|no[-_]index)\s*[=:]'
+ $files = @($env:PIP_CONFIG_FILE, $(if ($venv = Get-Variable VenvDir -ValueOnly -ErrorAction SilentlyContinue) { Join-Path $venv 'pip.ini' }), "$env:APPDATA\pip\pip.ini", "$env:USERPROFILE\pip\pip.ini", "$env:ProgramData\pip\pip.ini")
+ }
+ foreach ($file in $files) {
+ if ($file -and (Test-Path -LiteralPath $file -PathType Leaf) -and
+ (Select-String -LiteralPath $file -Pattern $pattern -Quiet -ErrorAction SilentlyContinue)) {
+ return $true
+ }
+ }
+ return $false
+}
+
+function Start-MirrorProbe {
+ param([string[]]$Urls, [double]$Seconds, [long]$LastByte)
+ $state = @{ Urls = $Urls; Seconds = $Seconds; LastByte = $LastByte; Clock = [System.Diagnostics.Stopwatch]::StartNew(); Requests = @{}; Heads = @{}; InTime = @{}; Bodies = @{}; Buffers = @{} }
+ $deadline = [System.Threading.Tasks.Task]::Delay([int]($Seconds * 1000))
+ foreach ($url in $Urls) {
+ $state.Requests[$url] = [System.Net.WebRequest]::Create($url)
+ # The CERNET mirrors answer 403 to a request without a User-Agent; a fresh connection keeps every probe cold, as curl's are.
+ $state.Requests[$url].UserAgent = 'unsloth-installer'
+ $state.Requests[$url].KeepAlive = $false
+ $state.Requests[$url].AddRange(0, $LastByte)
+ $state.Heads[$url] = $state.Requests[$url].GetResponseAsync()
+ $state.InTime[$url] = [System.Threading.Tasks.Task]::WhenAny([System.Threading.Tasks.Task[]]@($state.Heads[$url], $deadline))
+ }
+ return $state
+}
+
+function Wait-MirrorProbe {
+ param($Probe)
+ $results = @{}
+ $measure = { param($url) @([int]$Probe.Heads[$url].Result.StatusCode, [long]($Probe.Buffers[$url].Position / $Probe.Clock.Elapsed.TotalSeconds)) }
+ try {
+ while ($true) {
+ foreach ($url in $Probe.Urls) {
+ if ($results.ContainsKey($url) -or -not $Probe.InTime[$url].IsCompleted) { continue }
+ if (-not [object]::ReferenceEquals($Probe.InTime[$url].Result, $Probe.Heads[$url])) {
+ $results[$url] = @(0, [long]0)
+ } elseif ($Probe.Heads[$url].Status -ne 'RanToCompletion') {
+ $failure = $Probe.Heads[$url].Exception.InnerException -as [System.Net.WebException]
+ $results[$url] = @($(if ($failure -and $failure.Response) { [int]$failure.Response.StatusCode } else { 0 }), [long]0)
+ } elseif (-not $Probe.Bodies.ContainsKey($url)) {
+ $Probe.Buffers[$url] = [System.IO.MemoryStream]::new([byte[]]::new($Probe.LastByte + 65537))
+ $Probe.Bodies[$url] = $Probe.Heads[$url].Result.GetResponseStream().CopyToAsync($Probe.Buffers[$url], 65536)
+ } elseif ($Probe.Bodies[$url].IsCompleted) {
+ $results[$url] = & $measure $url
+ }
+ }
+ $pending = @($Probe.Urls | Where-Object { -not $results.ContainsKey($_) } | ForEach-Object { if ($Probe.Bodies.ContainsKey($_)) { $Probe.Bodies[$_] } else { $Probe.InTime[$_] } })
+ $left = [int](($Probe.Seconds - $Probe.Clock.Elapsed.TotalSeconds) * 1000)
+ if ($pending.Count -eq 0 -or $left -le 0) { break }
+ [void][System.Threading.Tasks.Task]::WaitAny([System.Threading.Tasks.Task[]]$pending, $left)
+ }
+ foreach ($url in $Probe.Urls) {
+ if ($results.ContainsKey($url)) { continue }
+ $results[$url] = if ($Probe.Buffers.ContainsKey($url)) { & $measure $url } else { @(0, [long]0) }
+ }
+ } finally {
+ foreach ($url in @($Probe.Requests.Keys)) {
+ $Probe.Requests[$url].Abort()
+ if ($Probe.Heads[$url].Status -eq 'RanToCompletion') { $Probe.Heads[$url].Result.Dispose() }
+ }
+ }
+ return $results
+}
+
+function Get-MirrorDnsServers {
+ try {
+ [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | Where-Object { $_.OperationalStatus -eq 'Up' } |
+ ForEach-Object { $_.GetIPProperties().DnsAddresses } | ForEach-Object { "$_" }
+ } catch {}
+}
+
+# No network call: a mainland China time zone, or a resolver from a mainland public DNS or cloud, as _mirror_in_china in install.sh.
+function Test-MirrorInChina {
+ try { if ((Get-TimeZone).Id -in 'China Standard Time', 'Asia/Shanghai', 'Asia/Chongqing', 'Asia/Chungking', 'Asia/Harbin', 'Asia/Urumqi', 'Asia/Kashgar', 'PRC') { return $true } } catch {}
+ return [bool](@(Get-MirrorDnsServers) -match '^(223\.5\.5\.5|223\.6\.6\.6|119\.29\.29\.29|114\.114\.11[45]\.11[0459]|182\.254\.116\.116|119\.28\.28\.28|180\.76\.76\.76|1\.2\.4\.8|210\.2\.4\.8|100\.100\.2\.13[68]|183\.60\.8[23]\.(19|98))$')
+}
+
+function Invoke-MirrorFallback {
+ param([switch]$SpareOnly)
+ $optIn = "$env:UNSLOTH_MIRROR_FALLBACK".Trim()
+ # When off, a retry state inherited from a parent is dropped so nothing downstream acts on it.
+ if ($optIn -match '^(0|false|no|off)$' -or ($optIn -notmatch '^(1|true|yes|on)$' -and -not (Test-MirrorInChina))) {
+ Remove-Item Env:_UNSLOTH_MIRROR_SPARE -ErrorAction SilentlyContinue
+ return
+ }
+ if ($env:_UNSLOTH_MIRROR_PROBED) { return }
+ if (-not $SpareOnly) { $env:_UNSLOTH_MIRROR_PROBED = '1' }
+ $cernet = 'https://tuna.mirrors.cernet.edu.cn'
+ $npmMirror = 'https://registry.npmmirror.com'
+ $pypiMirror = "$cernet/pypi/web/simple"
+ $minBps = 1MB
+ $useUv = -not (Test-MirrorConfigured -Tool uv)
+ $usePip = -not (Test-MirrorConfigured -Tool pip)
+ $uvWheel = 'packages/72/d6/207945fe69903b9794e2ef3e42608c91a59972567343a6719078d99c71f7/uv-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl'
+ $torchWheel = 'whl/cpu/torch-2.9.1%2Bcpu-cp312-cp312-manylinux_2_28_x86_64.whl'
+ $nodeTarball = 'v24.18.0/node-v24.18.0-linux-x64.tar.gz'
+ $artifact = @{
+ 'pypi' = "https://files.pythonhosted.org/$uvWheel"; 'cernet-pypi' = "$cernet/pypi/web/$uvWheel"
+ 'torch' = "https://download-r2.pytorch.org/$torchWheel"; 'cernet-torch' = "$cernet/pytorch/$torchWheel"
+ 'node' = "https://nodejs.org/dist/$nodeTarball"; 'npmmirror-node' = "$npmMirror/-/binary/node/$nodeTarball"
+ 'npm' = 'https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz'; 'npmmirror' = "$npmMirror/typescript/-/typescript-5.9.3.tgz"
+ 'astral' = 'https://releases.astral.sh/github/uv/releases/download/0.12.1/uv-x86_64-unknown-linux-gnu.tar.gz'
+ }
+ $hosts = [ordered]@{}
+ if ($useUv -or $usePip) { $hosts['pypi'] = @('pypi', 'cernet-pypi', $pypiMirror, 'https://pypi.org/simple/uv/', "$pypiMirror/uv/") }
+ if (-not "$env:UNSLOTH_PYTORCH_MIRROR$env:UNSLOTH_TORCH_INDEX_URL") {
+ $hosts['torch'] = @('torch', 'cernet-torch', "$cernet/pytorch/whl", 'https://download.pytorch.org/whl/cpu/torch/', "$cernet/pytorch/whl/cpu/torch/")
+ }
+ if (-not $env:UNSLOTH_NODE_MIRROR) { $hosts['node'] = @('node', 'npmmirror-node', "$npmMirror/-/binary/node", $null, $null) }
+ if (-not "$env:UNSLOTH_NPM_REGISTRY$env:NPM_CONFIG_REGISTRY") { $hosts['npm'] = @('npm', 'npmmirror', $npmMirror, $null, $null) }
+ if (-not "$env:UNSLOTH_UV_WHEEL_MIRROR$env:UV_DOWNLOAD_URL$env:INSTALLER_DOWNLOAD_URL$env:UV_INSTALLER_GHE_BASE_URL$env:UV_INSTALLER_GITHUB_BASE_URL") {
+ $hosts['uvbin'] = @('astral', 'cernet-pypi', "$cernet/pypi/web", $null, $null)
+ }
+ if ($hosts.Count -eq 0) { return }
+ $varsOf = {
+ param($name)
+ $to = if ($hosts.Contains($name)) { $hosts[$name][2] }
+ switch ($name) {
+ 'pypi' {
+ if ($useUv) { "UV_DEFAULT_INDEX=$to" }
+ if ($usePip) { "PIP_INDEX_URL=$to" }
+ }
+ 'unsynced' {
+ # Only for one rerun: uv's unsafe-first-match fetches every package from every index, and fails outright when one is unreachable.
+ if ($useUv) {
+ 'UV_DEFAULT_INDEX=https://pypi.org/simple'; "UV_INDEX=$pypiMirror"
+ "UV_INDEX_STRATEGY=$(if ($env:UV_INDEX_STRATEGY) { $env:UV_INDEX_STRATEGY } else { 'unsafe-first-match' })"
+ }
+ if ($usePip) { 'PIP_EXTRA_INDEX_URL=https://pypi.org/simple'; "PIP_INDEX_URL=$pypiMirror" }
+ }
+ 'torch' { "UNSLOTH_PYTORCH_MIRROR=$to" }
+ 'node' { "UNSLOTH_NODE_MIRROR=$to" }
+ 'npm' { "UNSLOTH_NPM_REGISTRY=$to" }
+ 'uvbin' { "UNSLOTH_UV_WHEEL_MIRROR=$to" }
+ }
+ }
+ $env:_UNSLOTH_MIRROR_SPARE = @($hosts.Keys | ForEach-Object { (@($_) + @(& $varsOf $_)) -join '|' }) -join ' '
+ if ($SpareOnly -or (Test-UvEnvFlag 'UV_OFFLINE')) { return }
+ $answered = @{}
+ $codeOf = { param($index, $result) if ($index -and "$($answered[$index][0])" -notmatch '^2\d\d$') { 0 } else { $result[0] } }
+ # PS 5.1 may pin TLS 1.0/1.1 (every probed host refuses it; Tls|Tls12 still fails) and queues past 2 connections per host.
+ $savedProtocol = [System.Net.ServicePointManager]::SecurityProtocol
+ $savedLimit = [System.Net.ServicePointManager]::DefaultConnectionLimit
+ if ([int]$savedProtocol -ne 0) { [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 }
+ [System.Net.ServicePointManager]::DefaultConnectionLimit = [Math]::Max($savedLimit, 16)
+ try {
+ $defaults = @($hosts.Values | ForEach-Object { $_[0] } | Select-Object -Unique)
+ $indexes = Start-MirrorProbe -Urls @($hosts.Values | ForEach-Object { $_[3] } | Where-Object { $_ }) -Seconds 4 -LastByte 1023
+ $timed = @{}
+ foreach ($name in $defaults) { $timed[$name] = (Wait-MirrorProbe (Start-MirrorProbe -Urls $artifact[$name] -Seconds 1.5 -LastByte 1048575))[$artifact[$name]] }
+ $answered = Wait-MirrorProbe $indexes
+ $slow = @($hosts.Keys | Where-Object {
+ $code = & $codeOf $hosts[$_][3] $timed[$hosts[$_][0]]
+ $code -eq 0 -or ($code -ge 300 -and $code -lt 400) -or ($code -ge 200 -and $code -lt 300 -and $timed[$hosts[$_][0]][1] -lt $minBps)
+ })
+ if ($slow.Count -eq 0) { return }
+ $sources = @($slow | ForEach-Object { $hosts[$_][1] } | Select-Object -Unique)
+ $indexes = Start-MirrorProbe -Urls @($slow | ForEach-Object { $hosts[$_][3]; $hosts[$_][4] } | Where-Object { $_ }) -Seconds 4 -LastByte 1023
+ $race = Wait-MirrorProbe (Start-MirrorProbe -Urls @($slow | ForEach-Object { $artifact[$hosts[$_][0]] }; $sources | ForEach-Object { $artifact[$_] }) -Seconds 4 -LastByte 1048575)
+ $sourceIndexes = Wait-MirrorProbe $indexes
+ foreach ($url in $sourceIndexes.Keys) { $answered[$url] = $sourceIndexes[$url] }
+ } finally {
+ [System.Net.ServicePointManager]::SecurityProtocol = $savedProtocol
+ [System.Net.ServicePointManager]::DefaultConnectionLimit = $savedLimit
+ }
+ $used = $false
+ foreach ($name in $slow) {
+ $default = $race[$artifact[$hosts[$name][0]]]
+ $mirror = $race[$artifact[$hosts[$name][1]]]
+ $how = if ("$(& $codeOf $hosts[$name][3] $default)" -match '^2\d\d$') { 'slow' } else { 'blocked' }
+ $defaultBps = if ($how -eq 'slow') { $default[1] } else { [long]0 }
+ if ("$(& $codeOf $hosts[$name][4] $mirror)" -notmatch '^2\d\d$' -or $defaultBps -ge $minBps -or $mirror[1] -le $defaultBps) { continue }
+ Set-MirrorEnv @(& $varsOf $name)
+ $env:_UNSLOTH_MIRROR_SPARE = @(-split $env:_UNSLOTH_MIRROR_SPARE | Where-Object { $_ -notlike "$name|*" }) -join ' '
+ if ($name -eq 'pypi' -and $how -eq 'slow') { $env:_UNSLOTH_MIRROR_SPARE = (@(-split $env:_UNSLOTH_MIRROR_SPARE) + ((@('unsynced') + @(& $varsOf 'unsynced')) -join '|')) -join ' ' }
+ step "mirror" "$(Get-MirrorName $name) is $how ($($defaultBps -shr 10) KB/s, mirror $($mirror[1] -shr 10) KB/s); using $($hosts[$name][2])" "Yellow"
+ $used = $true
+ }
+ if ($used) { substep "Set UNSLOTH_MIRROR_FALLBACK=0 to always use the default hosts." }
+}
+
+function Get-MirrorName {
+ param([string]$Name)
+ @{ pypi = 'PyPI'; unsynced = 'The PyPI mirror'; torch = 'download.pytorch.org'; node = 'nodejs.org'; npm = 'registry.npmjs.org'; uvbin = 'releases.astral.sh (uv)' }[$Name]
+}
+
+function Set-MirrorEnv {
+ param([string[]]$Pairs)
+ foreach ($pair in $Pairs) { Set-Item "Env:$($pair.Split('=', 2)[0])" $pair.Split('=', 2)[1] }
+}
+
+function Pop-MirrorSpare {
+ param([string]$Name)
+ $entry = @(-split $env:_UNSLOTH_MIRROR_SPARE | Where-Object { $_ -like "$Name|*" })
+ if (-not $entry) { return }
+ $env:_UNSLOTH_MIRROR_SPARE = @(-split $env:_UNSLOTH_MIRROR_SPARE | Where-Object { $_ -notlike "$Name|*" }) -join ' '
+ $pairs = @($entry[0].Split('|') | Select-Object -Skip 1)
+ step "mirror" "$(Get-MirrorName $Name) failed; retrying through $($pairs[0].Split('=', 2)[1])" "Yellow"
+ return $pairs
+}
+
+function Use-MirrorSpare {
+ param([string]$Name)
+ $pairs = @(Pop-MirrorSpare $Name)
+ Set-MirrorEnv $pairs
+ return $pairs.Count -gt 0
+}
+
+function Get-MirrorFailedHost {
+ param([string]$Output, [string]$Ran)
+ if ($Output -notmatch 'error sending request|timed out|network timeout|idle timeout|connection (reset|refused|closed|aborted)|network aborted|broken pipe|dns error|failed to lookup address|name resolution|nodename nor servname|network is unreachable|error decoding response body|end of file before message length|unexpected eof|tls handshake|sslerror|certificate verify failed|server error|service unavailable|bad gateway|gateway time-?out|too many requests|max retries exceeded|remotedisconnected|incompleteread|econnreset|etimedout|eidletimeout|eai_again|enotfound|econnrefused|socket hang up') {
+ if ($Output -match 'only \S+ (.* )?(is|are) available|no versions? of|not found in the package registry|could not find a version that satisfies|no matching distribution found') { 'unsynced' }
+ return
+ }
+ if ($Output -match 'download(-r2)?\.pytorch\.org') { 'torch' }
+ elseif ($Output -match 'registry\.npmjs\.org') { 'npm' }
+ elseif ($Output -match 'pypi\.org|pythonhosted\.org') { 'pypi' }
+ elseif ($Ran -and $Output -notmatch 'https?://') { $Ran }
+}
+
# Stop every install path consistently when its destination is unreadable.
function Exit-PathAccessDenied {
param(
@@ -2509,9 +2741,17 @@ function Invoke-SetupCommand {
$global:LASTEXITCODE = 0
if ($script:UnslothVerbose -and -not $AlwaysQuiet) {
# PS 5.1 turns stderr records into $? = $false even on exit 0; redact per record.
- & $Command 2>&1 | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
+ if ($env:_UNSLOTH_MIRROR_SPARE) {
+ $lines = @()
+ & $Command 2>&1 | ForEach-Object { "$_" } | Tee-Object -Variable lines | ForEach-Object { Redact-InstallOutput $_ } | Out-Host
+ $script:SetupCommandOutput = $lines -join "`n"
+ } else {
+ & $Command 2>&1 | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
+ $script:SetupCommandOutput = ''
+ }
} else {
$output = & $Command 2>&1 | Out-String
+ $script:SetupCommandOutput = $output
if ($LASTEXITCODE -ne 0) {
Write-StudioLine (Redact-InstallOutput $output) -ForegroundColor Red
}
@@ -2623,6 +2863,18 @@ function substep {
}
}
+function Invoke-NpmMirrorRetry {
+ if (-not $env:_UNSLOTH_MIRROR_SPARE) { return $false }
+ if ((Get-MirrorFailedHost -Output $script:SetupCommandOutput -Ran npm) -ne 'npm') { return $false }
+ $pairs = @(Pop-MirrorSpare npm)
+ if (-not $pairs) { return $false }
+ $registry = $pairs[0].Split('=', 2)[1]
+ if ((Invoke-SetupCommand { npm install --registry $registry }) -ne 0) { return $false }
+ Set-MirrorEnv $pairs
+ $script:NpmRegistryArgs = @('--registry', $registry)
+ return $true
+}
+
function Show-NpmRegistryHint {
if ($env:UNSLOTH_NPM_REGISTRY) { return }
$mirror = $env:NPM_CONFIG_REGISTRY
@@ -4831,6 +5083,14 @@ Write-StudioLine ""
step "system" "prerequisites ready"
Write-StudioLine ""
+Invoke-MirrorFallback -SpareOnly
+
+# UNSLOTH_NPM_REGISTRY: opt-in --registry splat past the frontend .npmrc lock (corporate proxies).
+$NpmRegistryArgs = @()
+if ($env:UNSLOTH_NPM_REGISTRY) {
+ $NpmRegistryArgs = @('--registry', $env:UNSLOTH_NPM_REGISTRY)
+}
+
# ==========================================================================
# PHASE 2: Frontend build (skip if pip-installed -- already bundled)
# ==========================================================================
@@ -4926,6 +5186,11 @@ if ($NeedNodeForSetup) {
$NodeInstallPython = if ($ValidatedSetupPython) { $ValidatedSetupPython } else { "python" }
$nodeOut = & $NodeInstallPython "$PSScriptRoot\install_node_prebuilt.py" --install-dir $NodeDir 2>&1 | 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
+ $nodeExit = $LASTEXITCODE
+ }
if ($nodeExit -eq 3) {
Write-StudioLine $nodeOut -ForegroundColor DarkGray
step "node" "install blocked by another active Unsloth install" "Red"
@@ -5049,6 +5314,7 @@ if ($NeedFrontendBuild -and -not $IsPipInstall) {
}
if (-not $UseBun) {
$npmExit = Invoke-SetupCommand { npm install @NpmRegistryArgs }
+ if ($npmExit -ne 0 -and (Invoke-NpmMirrorRetry)) { $npmExit = 0 }
if ($npmExit -ne 0) {
Pop-Location
$ErrorActionPreference = $prevEAP_npm
@@ -5092,6 +5358,7 @@ if ((Test-Path $OxcValidatorDir) -and $NodeSource -ne "skip" -and (Get-Command n
$ErrorActionPreference = "Continue"
Push-Location $OxcValidatorDir
$oxcInstallExit = Invoke-SetupCommand { npm install @NpmRegistryArgs }
+ if ($oxcInstallExit -ne 0 -and (Invoke-NpmMirrorRetry)) { $oxcInstallExit = 0 }
if ($oxcInstallExit -ne 0) {
Pop-Location
$ErrorActionPreference = $prevEAP_oxc
@@ -6632,13 +6899,16 @@ function Assert-VenvActivated {
# prepend as astral's installer, but it fetches a data file with a pinned SHA-256 instead of
# running remote script text in-process.
# tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
-# Bumping the version means bumping all 3 hashes:
+# Bumping the version means bumping all 3 hashes, and each Wheel/WheelSha256 as in install.ps1:
# curl -sL https://github.com/astral-sh/uv/releases/download/<ver>/uv-<arch>-pc-windows-msvc.zip.sha256
$UvPinnedVersion = "0.12.1"
$UvPinnedAssets = @{
- "x86_64" = @{ Asset = "uv-x86_64-pc-windows-msvc.zip"; Sha256 = "8FCB0CB46E1229065E344758980924E569BEF5882EF45F46FADA8FB24E06B74A" }
- "arm64" = @{ Asset = "uv-aarch64-pc-windows-msvc.zip"; Sha256 = "9BC7C18E616230FA2DC6FB24BC3AFDE18A95C2B5C9433DE747E9502C66041568" }
- "x86" = @{ Asset = "uv-i686-pc-windows-msvc.zip"; Sha256 = "9B51C33D307A8AB9E9DFD88D4AE1491761F63DE0BFFA3CEC96BEC536491C9B97" }
+ "x86_64" = @{ Asset = "uv-x86_64-pc-windows-msvc.zip"; Sha256 = "8FCB0CB46E1229065E344758980924E569BEF5882EF45F46FADA8FB24E06B74A"
+ Wheel = "packages/0d/a4/467c99c76fefa8b1259a1d382a5e49f73068f38a2d58db401504a783ed2c/uv-0.12.1-py3-none-win_amd64.whl"; WheelSha256 = "BD02F2DA212E6A983115DC64A6FC94E9256C2D60E056D6B669DE0A6025AAEC05" }
+ "arm64" = @{ Asset = "uv-aarch64-pc-windows-msvc.zip"; Sha256 = "9BC7C18E616230FA2DC6FB24BC3AFDE18A95C2B5C9433DE747E9502C66041568"
+ Wheel = "packages/68/80/ec1acbf8e22dc4866f9070c30b064728cc0da73bedc30f2fbfdc0c5901a7/uv-0.12.1-py3-none-win_arm64.whl"; WheelSha256 = "EAD7AD064F291A5DF358C3FFA8FFAB347A32BD5A75A6A068CA22254C2539A829" }
+ "x86" = @{ Asset = "uv-i686-pc-windows-msvc.zip"; Sha256 = "9B51C33D307A8AB9E9DFD88D4AE1491761F63DE0BFFA3CEC96BEC536491C9B97"
+ Wheel = "packages/fd/02/f73e4867c0748eaa3dea90cdfeb73d15bab0f04802c5c20bb37fc14918fe/uv-0.12.1-py3-none-win32.whl"; WheelSha256 = "173EE216F17D89FC39F65339D311A53584FC7DE4918D27C0F3C7EDAFABC6B54D" }
}
# Not Get-HostMachineArch: it answers arm64/other for the VC++ and prebuilt probes, and "other"
@@ -6735,6 +7005,7 @@ function Get-UvInstallDir {
}
function Install-UvFromPinnedRelease {
+ $script:UvPinnedUnfetched = $false
$arch = Get-UvHostArch
if (-not $UvPinnedAssets.ContainsKey($arch)) {
Write-Output "No uv build is published for this architecture ($arch)."
@@ -6742,6 +7013,8 @@ function Install-UvFromPinnedRelease {
}
$asset = $UvPinnedAssets[$arch].Asset
$wanted = $UvPinnedAssets[$arch].Sha256
+ $remote = $asset
+ $wheelDir = $null
# astral's destination priority, so an existing uv is replaced in place and the Get-Command
# probe after Refresh-Environment still finds it.
@@ -6763,6 +7036,11 @@ function Install-UvFromPinnedRelease {
@("$($env:UV_INSTALLER_GHE_BASE_URL.TrimEnd('/'))/astral-sh/uv/releases/download/$UvPinnedVersion")
} elseif ($env:UV_INSTALLER_GITHUB_BASE_URL) {
@("$($env:UV_INSTALLER_GITHUB_BASE_URL.TrimEnd('/'))/astral-sh/uv/releases/download/$UvPinnedVersion")
+ } elseif ($env:UNSLOTH_UV_WHEEL_MIRROR) {
+ $remote = $UvPinnedAssets[$arch].Wheel
+ $wanted = $UvPinnedAssets[$arch].WheelSha256
+ $wheelDir = "uv-$UvPinnedVersion.data/scripts"
+ @("$($env:UNSLOTH_UV_WHEEL_MIRROR.TrimEnd('/'))")
} else {
@("https://releases.astral.sh/github/uv/releases/download/$UvPinnedVersion",
"https://github.com/astral-sh/uv/releases/download/$UvPinnedVersion")
@@ -6775,14 +7053,16 @@ function Install-UvFromPinnedRelease {
# Digest per mirror, as install.ps1 does: a proxy answering 200 with its own body is a
# successful download by every measure Invoke-WebRequest has.
$downloaded = $false
+ $script:UvPinnedUnfetched = $true
foreach ($base in $uvBase) {
Write-Output "downloading uv $UvPinnedVersion ($arch) from $base..."
try {
- Invoke-WebRequest -UseBasicParsing -OutFile $zip -Uri "$base/$asset"
+ Invoke-WebRequest -UseBasicParsing -OutFile $zip -Uri "$base/$remote"
} catch {
Write-Output "uv download failed: $($_.Exception.Message)"
continue
}
+ $script:UvPinnedUnfetched = $false
$actual = ""
try { $actual = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash } catch {}
if ($actual -eq $wanted) {
@@ -6795,10 +7075,10 @@ function Install-UvFromPinnedRelease {
}
if (-not $downloaded) { return $false }
- # The Windows archives are flat: uv.exe, uvx.exe, uvw.exe at the root.
Expand-Archive -LiteralPath $zip -DestinationPath $work -Force
[System.IO.Directory]::CreateDirectory($destDir) | Out-Null
- $stagedUv = Join-Path $work "uv.exe"
+ $srcRoot = if ($wheelDir) { Join-Path $work $wheelDir } else { $work }
+ $stagedUv = Join-Path $srcRoot "uv.exe"
if (-not (Test-Path -LiteralPath $stagedUv)) {
Write-Output "uv.exe was not present in $asset."
return $false
@@ -6816,7 +7096,7 @@ function Install-UvFromPinnedRelease {
# than leaving half a set behind quietly.
$haveUv = $true
foreach ($exe in @("uv.exe", "uvx.exe", "uvw.exe")) {
- $src = Join-Path $work $exe
+ $src = Join-Path $srcRoot $exe
if (-not (Test-Path -LiteralPath $src)) { continue }
$dst = Join-Path $destDir $exe
try {
@@ -6981,6 +7261,9 @@ if (Get-Command uv -ErrorAction SilentlyContinue) {
try {
$script:UvPinnedInstalled = $false
Invoke-SetupCommand { Install-UvFromPinnedRelease } | Out-Null
+ if (-not $script:UvPinnedInstalled -and $script:UvPinnedUnfetched -and (Use-MirrorSpare uvbin)) {
+ Invoke-SetupCommand { Install-UvFromPinnedRelease } | Out-Null
+ }
# The merge base ran astral's installer here, so a failed pinned install needs somewhere
# to go: with no fallback the setup drops to pip for torch, bitsandbytes and Triton, which
# is a different resolver rather than a different download. winget, not the remote script,
@@ -7439,6 +7722,8 @@ Clear-UnparseableTorchCacheEnv
if (-not $SkipPythonDeps) {
+if (-not ($env:STUDIO_LOCAL_INSTALL -eq '1' -and (Test-Path -LiteralPath (Join-Path $VenvDir 'Scripts\python.exe') -PathType Leaf))) { Invoke-MirrorFallback }
+
# Recover what a fresh shell lost, BEFORE the manifest is dropped below: recovery reads that file.
$WinArm64Venv = Test-WinArm64Venv
# Read BEFORE the re-export overwrites it: the flags install.ps1 left describe THAT index.
@@ -8431,10 +8716,24 @@ $SidecarCommonPins = @("huggingface_hub==1.8.0", "hf_xet==1.4.2")
function Fast-Install-Sidecar {
param([Parameter(ValueFromRemainingArguments=$true)]$Args_)
$savedOverride = $env:UV_OVERRIDE
+ $savedUnsynced = @{}
try {
Remove-Item Env:UV_OVERRIDE -ErrorAction SilentlyContinue
Fast-Install @Args_
+ # A pin the PyPI mirror has not synced yet: one rerun with pypi.org behind it, armed only after a slow pypi.org was switched.
+ $unsynced = @(-split "$env:_UNSLOTH_MIRROR_SPARE" | Where-Object { $_ -like 'unsynced|*' })
+ if ($LASTEXITCODE -ne 0 -and $unsynced) {
+ foreach ($pair in @($unsynced[0].Split('|') | Select-Object -Skip 1)) {
+ $name, $value = $pair.Split('=', 2)
+ $savedUnsynced[$name] = [Environment]::GetEnvironmentVariable($name)
+ Set-Item "Env:$name" $value
+ }
+ Fast-Install @Args_
+ }
} finally {
+ foreach ($name in $savedUnsynced.Keys) {
+ if ($null -eq $savedUnsynced[$name]) { Remove-Item "Env:$name" -ErrorAction SilentlyContinue } else { Set-Item "Env:$name" $savedUnsynced[$name] }
+ }
if ($null -ne $savedOverride) { $env:UV_OVERRIDE = $savedOverride }
}
}
@@ -8486,6 +8785,7 @@ function Repair-SidecarTiktoken {
# Dropping the metadata is what makes uv reinstall instead of calling the pin satisfied.
Get-ChildItem -LiteralPath $TargetDir -Directory -Filter "tiktoken-*.dist-info" -ErrorAction SilentlyContinue |
ForEach-Object { Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction SilentlyContinue }
+ Invoke-MirrorFallback
# Cleared first: an install that runs no native command would retire the sidecar on a stale
# nonzero (unresolvable uv is non-terminating under "Continue").
$global:LASTEXITCODE = 0
@@ -8557,6 +8857,7 @@ function Install-T5Sidecar {
[Parameter(Mandatory = $true)][string]$Reason
)
substep "pre-installing transformers $Version $Reason..."
+ Invoke-MirrorFallback
Assert-StudioOwnedOrAbsent -Path $TargetDir -Label "transformers $Label sidecar venv"
if (Test-Path -LiteralPath $TargetDir) { Remove-Item -LiteralPath $TargetDir -Recurse -Force }
[System.IO.Directory]::CreateDirectory($TargetDir) | Out-Null
diff --git a/studio/setup.sh b/studio/setup.sh
index 9ae1d09550b..90a9b3fae8d 100755
--- a/studio/setup.sh
+++ b/studio/setup.sh
@@ -113,6 +113,319 @@ _remove_agent_instruction_files() {
done
}
+# ── BEGIN mirror fallback (kept identical in install.sh and studio/setup.sh) ──
+# Only in mainland China (or UNSLOTH_MIRROR_FALLBACK=1): swaps a default host below 1 MiB/s or unreachable for its mirror when faster; user-set sources untouched; UNSLOTH_MIRROR_FALLBACK=0 disables.
+_MIRROR_CERNET="https://tuna.mirrors.cernet.edu.cn"
+_MIRROR_PYPI="$_MIRROR_CERNET/pypi/web/simple"
+_MIRROR_NPM="https://registry.npmmirror.com"
+# GitHub-release mirrors keep only the newest Python builds, while a pinned uv asks for the builds it shipped with; npmmirror keeps every release.
+_MIRROR_PYTHON="$_MIRROR_NPM/-/binary/python-build-standalone"
+_MIRROR_MIN_BPS=1048576
+
+_mirror_probe() {
+ _mp_out=$(curl -sL -o /dev/null -r "0-$3" -w '%{http_code} %{speed_download}' --connect-timeout "$2" --max-time "$2" "$1" 2>/dev/null) || true
+ _mp_bps=${_mp_out#* }
+ _mp_bps=${_mp_bps%%.*}
+ case "$_mp_bps" in ''|*[!0-9]*) _mp_bps=0 ;; esac
+ _mp_code=${_mp_out%% *}
+ case "$_mp_code" in [0-9][0-9][0-9]) ;; *) _mp_code=000 ;; esac
+ echo "$_mp_code $_mp_bps"
+}
+
+_mirror_url() {
+ case "$1" in
+ pypi) echo "https://files.pythonhosted.org/packages/72/d6/207945fe69903b9794e2ef3e42608c91a59972567343a6719078d99c71f7/uv-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl" ;;
+ cernet-pypi) echo "$_MIRROR_CERNET/pypi/web/packages/72/d6/207945fe69903b9794e2ef3e42608c91a59972567343a6719078d99c71f7/uv-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl" ;;
+ torch) echo "https://download-r2.pytorch.org/whl/cpu/torch-2.9.1%2Bcpu-cp312-cp312-manylinux_2_28_x86_64.whl" ;;
+ cernet-torch) echo "$_MIRROR_CERNET/pytorch/whl/cpu/torch-2.9.1%2Bcpu-cp312-cp312-manylinux_2_28_x86_64.whl" ;;
+ node) echo "https://nodejs.org/dist/v24.18.0/node-v24.18.0-linux-x64.tar.gz" ;;
+ npmmirror-node) echo "$_MIRROR_NPM/-/binary/node/v24.18.0/node-v24.18.0-linux-x64.tar.gz" ;;
+ npm) echo "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz" ;;
+ npmmirror) echo "$_MIRROR_NPM/typescript/-/typescript-5.9.3.tgz" ;;
+ astral) echo "https://releases.astral.sh/github/uv/releases/download/0.12.1/uv-x86_64-unknown-linux-gnu.tar.gz" ;;
+ pypi-index) echo "https://pypi.org/simple/uv/" ;;
+ torch-index) echo "https://download.pytorch.org/whl/cpu/torch/" ;;
+ cernet-pypi-index) echo "$_MIRROR_PYPI/uv/" ;;
+ cernet-torch-index) echo "$_MIRROR_CERNET/pytorch/whl/cpu/torch/" ;;
+ esac
+}
+
+_mirror_default() {
+ case "$1" in python|uvbin) echo astral ;; *) echo "$1" ;; esac
+}
+
+_mirror_source() {
+ case "$1" in npm|python) echo npmmirror ;; node) echo npmmirror-node ;; pypi|uvbin) echo cernet-pypi ;; *) echo "cernet-$1" ;; esac
+}
+
+_mirror_index_probe() {
+ for _mip_name in "$@"; do
+ case "$_mip_name" in
+ pypi|torch|cernet-pypi|cernet-torch)
+ _mirror_probe "$(_mirror_url "$_mip_name-index")" 4 1023 > "$_mf_dir/$_mip_name-index" &
+ _mf_pids="$_mf_pids $!" ;;
+ esac
+ done
+}
+
+_mirror_index_wait() {
+ for _miw_pid in $_mf_pids; do
+ wait "$_miw_pid" || true
+ done
+ _mf_pids=""
+}
+
+_mirror_index_ok() {
+ [ -f "$_mf_dir/$1-index" ] || return 0
+ read -r _mio_code _mio_bps < "$_mf_dir/$1-index"
+ case "$_mio_code" in 2??) return 0 ;; *) return 1 ;; esac
+}
+
+_mirror_uv_project_config() {
+ _mup_dir=$PWD
+ while [ -n "$_mup_dir" ]; do
+ if [ -f "$_mup_dir/uv.toml" ]; then echo "$_mup_dir/uv.toml"; return 0; fi
+ if grep -Eqs '^[[:space:]]*\[+tool\.uv(\.|\])' "$_mup_dir/pyproject.toml"; then echo "$_mup_dir/pyproject.toml"; return 0; fi
+ [ "$_mup_dir" = / ] && return 0
+ _mup_dir=$(dirname "$_mup_dir")
+ done
+}
+
+_mirror_configured() {
+ case "$1" in
+ uv)
+ [ -n "${UV_DEFAULT_INDEX:-}${UV_INDEX_URL:-}${UV_INDEX:-}${UV_EXTRA_INDEX_URL:-}" ] && return 0
+ _mic_key='\[\[(tool\.uv\.)?index\]\]|(pip\.)?(index|index-url|default-index|extra-index-url|no-index)[[:space:]]*=' ;;
+ python)
+ [ -n "${UV_PYTHON_INSTALL_MIRROR:-}" ] && return 0
+ _mic_key='python-install-mirror[[:space:]]*=' ;;
+ pip)
+ [ -n "${PIP_INDEX_URL:-}${PIP_EXTRA_INDEX_URL:-}${PIP_NO_INDEX:-}" ] && return 0
+ _mic_key='(index[-_]url|extra[-_]index[-_]url|no[-_]index)[[:space:]]*[=:]' ;;
+ esac
+ _mic_suffix=uv/uv.toml
+ [ "$1" != pip ] || _mic_suffix=pip/pip.conf
+ if [ "$1" = pip ]; then
+ set -- "${PIP_CONFIG_FILE:-}" "${VENV_DIR:+$VENV_DIR/pip.conf}" "${XDG_CONFIG_HOME:-$HOME/.config}/pip/pip.conf" "$HOME/.pip/pip.conf" "$HOME/Library/Application Support/pip/pip.conf" /etc/xdg/pip/pip.conf /etc/pip.conf
+ else
+ set -- "${UV_CONFIG_FILE:-}" "$(_mirror_uv_project_config)" "${XDG_CONFIG_HOME:-$HOME/.config}/uv/uv.toml" /etc/xdg/uv/uv.toml /etc/uv/uv.toml
+ fi
+ _mic_xdg=${XDG_CONFIG_DIRS:-}
+ while [ -n "$_mic_xdg" ]; do
+ set -- "$@" "${_mic_xdg%%:*}/$_mic_suffix"
+ case "$_mic_xdg" in *:*) _mic_xdg=${_mic_xdg#*:} ;; *) _mic_xdg="" ;; esac
+ done
+ for _mic_file in "$@"; do
+ if [ -f "$_mic_file" ] && grep -Eq "^[[:space:]]*($_mic_key)" "$_mic_file" 2>/dev/null; then
+ return 0
+ fi
+ done
+ return 1
+}
+
+_mirror_probe_all() {
+ _mpa_dir="$1"
+ _mpa_secs="$2"
+ shift 2
+ _mpa_pids=""
+ for _mpa_name in "$@"; do
+ _mirror_probe "$(_mirror_url "$_mpa_name")" "$_mpa_secs" 1048575 > "$_mpa_dir/$_mpa_name" &
+ _mpa_pids="$_mpa_pids $!"
+ done
+ for _mpa_pid in $_mpa_pids; do
+ wait "$_mpa_pid" || true
+ done
+}
+
+_mirror_vars() {
+ case "$1" in
+ pypi)
+ [ "$_mf_uv" = false ] || echo "UV_DEFAULT_INDEX=$_MIRROR_PYPI"
+ [ "$_mf_pip" = false ] || echo "PIP_INDEX_URL=$_MIRROR_PYPI" ;;
+ unsynced)
+ # Only for one rerun: uv's unsafe-first-match fetches every package from every index, and fails outright when one is unreachable.
+ [ "$_mf_uv" = false ] || echo "UV_DEFAULT_INDEX=https://pypi.org/simple UV_INDEX=$_MIRROR_PYPI UV_INDEX_STRATEGY=${UV_INDEX_STRATEGY:-unsafe-first-match}"
+ [ "$_mf_pip" = false ] || echo "PIP_EXTRA_INDEX_URL=https://pypi.org/simple PIP_INDEX_URL=$_MIRROR_PYPI" ;;
+ torch) echo "UNSLOTH_PYTORCH_MIRROR=$_MIRROR_CERNET/pytorch/whl" ;;
+ node) echo "UNSLOTH_NODE_MIRROR=$_MIRROR_NPM/-/binary/node" ;;
+ npm) echo "UNSLOTH_NPM_REGISTRY=$_MIRROR_NPM" ;;
+ python) echo "UV_PYTHON_INSTALL_MIRROR=$_MIRROR_PYTHON" ;;
+ uvbin) echo "UNSLOTH_UV_WHEEL_MIRROR=$_MIRROR_CERNET/pypi/web" ;;
+ esac
+}
+
+_mirror_name() {
+ case "$1" in
+ pypi) echo "PyPI" ;;
+ unsynced) echo "The PyPI mirror" ;;
+ torch) echo "download.pytorch.org" ;;
+ node) echo "nodejs.org" ;;
+ npm) echo "registry.npmjs.org" ;;
+ python) echo "releases.astral.sh (Python builds)" ;;
+ uvbin) echo "releases.astral.sh (uv)" ;;
+ esac
+}
+
+_mirror_use() {
+ _mu_to=""
+ for _mu_pair in $(_mirror_vars "$1"); do
+ export "$_mu_pair"
+ [ -n "$_mu_to" ] || _mu_to=${_mu_pair#*=}
+ done
+ step "mirror" "$(_mirror_name "$1") is $2 ($(($3 / 1024)) KB/s, mirror $(($4 / 1024)) KB/s); using $_mu_to" "$C_WARN"
+ _mf_switched="$_mf_switched $1"
+ [ "$1 $2" != "pypi slow" ] || _mf_unsynced=true
+}
+
+_mirror_take() {
+ _MT_PAIRS=""
+ _mt_spare=""
+ for _mt_entry in ${_UNSLOTH_MIRROR_SPARE:-}; do
+ if [ "${_mt_entry%%|*}" = "$1" ]; then
+ _MT_PAIRS=$(printf '%s' "${_mt_entry#*|}" | tr '|' ' ')
+ else
+ _mt_spare="$_mt_spare $_mt_entry"
+ fi
+ done
+ [ -n "$_MT_PAIRS" ] || return 1
+ export _UNSLOTH_MIRROR_SPARE="${_mt_spare# }"
+ _mt_to=${_MT_PAIRS%% *}
+ step "mirror" "$(_mirror_name "$1") failed; retrying through ${_mt_to#*=}" "$C_WARN"
+}
+
+_mirror_switch() {
+ _mirror_take "$1" || return 1
+ for _ms_pair in $_MT_PAIRS; do export "$_ms_pair"; done
+}
+
+_mirror_failed_host() {
+ if ! grep -Eqi 'error sending request|timed out|network timeout|idle timeout|connection (reset|refused|closed|aborted)|network aborted|broken pipe|dns error|failed to lookup address|name resolution|nodename nor servname|network is unreachable|error decoding response body|end of file before message length|unexpected eof|tls handshake|sslerror|certificate verify failed|server error|service unavailable|bad gateway|gateway time-?out|too many requests|max retries exceeded|remotedisconnected|incompleteread|econnreset|etimedout|eidletimeout|eai_again|enotfound|econnrefused|socket hang up' "$1" 2>/dev/null; then
+ grep -Eqi 'only [^ ]+ (.* )?(is|are) available|no versions? of|not found in the package registry|could not find a version that satisfies|no matching distribution found' "$1" 2>/dev/null || return 1
+ echo unsynced
+ return 0
+ fi
+ if grep -Eq 'download(-r2)?\.pytorch\.org' "$1"; then echo torch
+ elif grep -q 'python-build-standalone' "$1"; then echo python
+ elif grep -q 'registry\.npmjs\.org' "$1"; then echo npm
+ elif grep -Eq 'pypi\.org|pythonhosted\.org' "$1"; then echo pypi
+ elif [ -n "${2:-}" ] && ! grep -Eq 'https?://' "$1"; then echo "$2"
+ else return 1
+ fi
+}
+
+# No network call: a mainland China time zone, or a resolver from a mainland public DNS or cloud (the addresses below).
+_mirror_in_china() {
+ _mcn_tz=${TZ:-}
+ [ -n "$_mcn_tz" ] || _mcn_tz=$(cat /etc/timezone 2>/dev/null) || true
+ [ -n "$_mcn_tz" ] || _mcn_tz=$(readlink /etc/localtime 2>/dev/null) || true
+ case "${_mcn_tz#:}" in
+ *Asia/Shanghai|*Asia/Chongqing|*Asia/Chungking|*Asia/Harbin|*Asia/Urumqi|*Asia/Kashgar|PRC|*/PRC) return 0 ;;
+ esac
+ grep -Eqs '^[[:space:]]*nameserver[[:space:]]+(223\.5\.5\.5|223\.6\.6\.6|119\.29\.29\.29|114\.114\.11[45]\.11[0459]|182\.254\.116\.116|119\.28\.28\.28|180\.76\.76\.76|1\.2\.4\.8|210\.2\.4\.8|100\.100\.2\.13[68]|183\.60\.8[23]\.(19|98))[[:space:]]*$' /etc/resolv.conf /run/systemd/resolve/resolv.conf
+}
+
+# Decided once per process; when off, a retry state inherited from a parent is dropped so nothing downstream acts on it.
+_mirror_enabled() {
+ if [ -z "${_mirror_on:-}" ]; then
+ case "${UNSLOTH_MIRROR_FALLBACK:-}" in
+ 0|false|False|FALSE|no|off) _mirror_on=no ;;
+ 1|true|True|TRUE|yes|on) _mirror_on=yes ;;
+ *) if _mirror_in_china; then _mirror_on=yes; else _mirror_on=no; fi ;;
+ esac
+ [ "$_mirror_on" = yes ] || unset _UNSLOTH_MIRROR_SPARE
+ fi
+ [ "$_mirror_on" = yes ]
+}
+
+_mirror_fallback() {
+ _mirror_enabled || return 0
+ [ -z "${_UNSLOTH_MIRROR_PROBED:-}" ] || return 0
+ command -v curl >/dev/null 2>&1 || return 0
+ [ "${1:-}" = spare ] || export _UNSLOTH_MIRROR_PROBED=1
+ _mf_uv=true
+ _mf_pip=true
+ _mf_switched=""
+ _mf_unsynced=false
+ _mirror_configured uv && _mf_uv=false
+ _mirror_configured pip && _mf_pip=false
+ _mf_hosts=""
+ if [ "$_mf_uv" = true ] || [ "$_mf_pip" = true ]; then
+ _mf_hosts="pypi"
+ fi
+ [ -n "${UNSLOTH_PYTORCH_MIRROR:-}${UNSLOTH_TORCH_INDEX_URL:-}" ] || _mf_hosts="$_mf_hosts torch"
+ [ -n "${UNSLOTH_NODE_MIRROR:-}" ] || _mf_hosts="$_mf_hosts node"
+ [ -n "${UNSLOTH_NPM_REGISTRY:-}${NPM_CONFIG_REGISTRY:-}${npm_config_registry:-}" ] || _mf_hosts="$_mf_hosts npm"
+ _mirror_configured python || _mf_hosts="$_mf_hosts python"
+ [ -n "${UNSLOTH_UV_WHEEL_MIRROR:-}${UV_DOWNLOAD_URL:-}${INSTALLER_DOWNLOAD_URL:-}${UV_INSTALLER_GHE_BASE_URL:-}${UV_INSTALLER_GITHUB_BASE_URL:-}" ] || _mf_hosts="$_mf_hosts uvbin"
+ [ -n "$_mf_hosts" ] || return 0
+ # UV_OFFLINE (uv's spellings) asked for no network: arm the retries, probe nothing.
+ _mf_uvo=${UV_OFFLINE:-}
+ _mf_uvo=${_mf_uvo#"${_mf_uvo%%[![:space:]]*}"}
+ _mf_uvo=${_mf_uvo%"${_mf_uvo##*[![:space:]]}"}
+ case "${1:-}/$_mf_uvo" in
+ spare/* | */1 | */[Tt] | */[Tt][Rr][Uu][Ee] | */[Yy] | */[Yy][Ee][Ss] | */[Oo][Nn]) _mirror_spare_export; return 0 ;;
+ esac
+ _mf_dir=$(mktemp -d 2>/dev/null) || return 0
+ _mf_pids=""
+ _mirror_index_probe $_mf_hosts
+ for _mf_name in $(for _mf_host in $_mf_hosts; do _mirror_default "$_mf_host"; done | sort -u); do
+ _mirror_probe "$(_mirror_url "$_mf_name")" 1.5 1048575 > "$_mf_dir/$_mf_name"
+ done
+ _mirror_index_wait
+ _mf_slow=""
+ for _mf_host in $_mf_hosts; do
+ read -r _mf_code _mf_bps < "$_mf_dir/$(_mirror_default "$_mf_host")"
+ _mirror_index_ok "$_mf_host" || _mf_code=000
+ case "$_mf_code" in
+ 000|3??) _mf_slow="$_mf_slow $_mf_host" ;;
+ 2??) [ "$_mf_bps" -ge "$_MIRROR_MIN_BPS" ] || _mf_slow="$_mf_slow $_mf_host" ;;
+ esac
+ done
+ if [ -n "$_mf_slow" ]; then
+ _mirror_index_probe $_mf_slow $(for _mf_host in $_mf_slow; do echo "cernet-$_mf_host"; done)
+ _mirror_probe_all "$_mf_dir" 4 $(for _mf_host in $_mf_slow; do _mirror_default "$_mf_host"; _mirror_source "$_mf_host"; done | sort -u)
+ _mirror_index_wait
+ for _mf_host in $_mf_slow; do
+ read -r _mf_code _mf_bps < "$_mf_dir/$(_mirror_default "$_mf_host")"
+ read -r _mf_mcode _mf_mbps < "$_mf_dir/$(_mirror_source "$_mf_host")"
+ _mirror_index_ok "$_mf_host" || _mf_code=000
+ _mirror_index_ok "cernet-$_mf_host" || _mf_mcode=000
+ case "$_mf_code" in
+ 2??) _mf_how=slow ;;
+ *) _mf_how=blocked; _mf_bps=0 ;;
+ esac
+ case "$_mf_mcode" in
+ 2??) [ "$_mf_bps" -lt "$_MIRROR_MIN_BPS" ] && [ "$_mf_mbps" -gt "$_mf_bps" ] && _mirror_use "$_mf_host" "$_mf_how" "$_mf_bps" "$_mf_mbps" ;;
+ esac
+ done
+ if [ -n "$_mf_switched" ]; then
+ substep "Set UNSLOTH_MIRROR_FALLBACK=0 to always use the default hosts."
+ fi
+ fi
+ rm -rf "$_mf_dir"
+ _mirror_spare_export
+}
+
+_mirror_spare_export() {
+ _mf_spare=""
+ for _mf_host in $_mf_hosts; do
+ case " $_mf_switched " in *" $_mf_host "*) continue ;; esac
+ _mf_entry=""
+ for _mf_pair in $(_mirror_vars "$_mf_host"); do
+ _mf_entry="$_mf_entry|$_mf_pair"
+ done
+ [ -z "$_mf_entry" ] || _mf_spare="$_mf_spare $_mf_host$_mf_entry"
+ done
+ if [ "$_mf_unsynced" = true ]; then
+ _mf_spare="$_mf_spare unsynced"
+ for _mf_pair in $(_mirror_vars unsynced); do
+ _mf_spare="$_mf_spare|$_mf_pair"
+ done
+ fi
+ export _UNSLOTH_MIRROR_SPARE="${_mf_spare# }"
+}
+# ── END mirror fallback ──
+
# ── Corporate-mirror / proxy escape hatch for the frontend npm/bun install (#6491) ──
# studio/frontend/.npmrc pins registry=https://registry.npmjs.org/ as a supply-chain
# lock. A project-level pin overrides a corporate user's ~/.npmrc proxy, so the install
@@ -129,9 +442,16 @@ fi
# around the npm/bun installs; "" elsewhere so unrelated run_quiet calls don't capture.
_CAPTURE_LOG=""
+_npm_mirror_retry() {
+ [ "$(_mirror_failed_host "${_CAPTURE_LOG:-}" npm)" = npm ] && _mirror_take npm || return 1
+ run_quiet_no_exit "$1" npm install --no-fund --no-audit --loglevel=error --registry "${_MT_PAIRS#*=}" || return
+ export "$_MT_PAIRS"
+ _NPM_REGISTRY_ARGS=(--registry "$UNSLOTH_NPM_REGISTRY")
+}
+
# Print actionable guidance when a frontend/OXC npm/bun install fails and the registry
# lock is the likely cause (corporate firewall/proxy). No-op once the user has opted in
-# via UNSLOTH_NPM_REGISTRY. We never switch registries automatically -- we only guide.
+# via UNSLOTH_NPM_REGISTRY. This only guides; the mirror fallback does any switching.
# $1 = path to a captured install log (may be empty/missing).
_suggest_npm_registry() {
[ -n "${UNSLOTH_NPM_REGISTRY:-}" ] && return 0
@@ -1098,6 +1418,12 @@ fi
STAGE_ROOT="${UNSLOTH_STUDIO_STAGE_ROOT:-}"
RUNTIME_ROOT="${STAGE_ROOT:-$STUDIO_HOME}"
VENV_DIR="$RUNTIME_ROOT/unsloth_studio"
+if _mirror_enabled; then
+ _mirror_spare_pwd=$PWD
+ cd "$SCRIPT_DIR"
+ _mirror_fallback spare
+ cd "$_mirror_spare_pwd" 2>/dev/null || :
+fi
# Same uv cache install.sh chose, for the same reasons.
#
@@ -1759,13 +2085,18 @@ elif [ "$NODE_SOURCE" = bundled ]; then
fi
_NODE_LOG="$(mktemp)"
set +e
- if _is_verbose; then
- "$_NODE_PY" "$SCRIPT_DIR/install_node_prebuilt.py" --install-dir "$NODE_DIR" 2>&1 | tee "$_NODE_LOG"
- _NODE_STATUS=${PIPESTATUS[0]}
- else
- "$_NODE_PY" "$SCRIPT_DIR/install_node_prebuilt.py" --install-dir "$NODE_DIR" >"$_NODE_LOG" 2>&1
- _NODE_STATUS=$?
- fi
+ for _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"
+ _NODE_STATUS=${PIPESTATUS[0]}
+ else
+ "$_NODE_PY" "$SCRIPT_DIR/install_node_prebuilt.py" --install-dir "$NODE_DIR" >>"$_NODE_LOG" 2>&1
+ _NODE_STATUS=$?
+ fi
+ # A failed download gets one retry through the mirror; 3 (another install holds the lock) and 4 (permission denied) are not network failures.
+ [ "$_NODE_STATUS" -ne 0 ] && [ "$_NODE_STATUS" -ne 3 ] && [ "$_NODE_STATUS" -ne 4 ] && [ "$_node_try" = default ] || break
+ _mirror_switch node || break
+ done
set -e
if [ "$_NODE_STATUS" -eq 3 ]; then
step "node" "install blocked by another active Unsloth install" "$C_ERR"
@@ -1906,6 +2237,9 @@ if [ "$_bun_install_ok" = false ]; then
# the exact exit code. Mirrors the `|| BUILD_OK=false` idiom used below.
_npm_install_rc=0
run_quiet_no_exit "npm install" npm install --no-fund --no-audit --loglevel=error "${_NPM_REGISTRY_ARGS[@]+"${_NPM_REGISTRY_ARGS[@]}"}" || _npm_install_rc=$?
+ if [ "$_npm_install_rc" -ne 0 ] && _npm_mirror_retry "npm install"; then
+ _npm_install_rc=0
+ fi
if [ "$_npm_install_rc" -ne 0 ]; then
_suggest_npm_registry "$_FRONTEND_INSTALL_LOG"
rm -f "$_FRONTEND_INSTALL_LOG"
@@ -1945,6 +2279,9 @@ if [ -d "$_OXC_DIR" ] && [ "${NODE_SOURCE:-}" != skip ] && command -v npm &>/dev
# below is reachable; it also captures the exact exit code.
_oxc_install_rc=0
run_quiet_no_exit "npm install (oxc validator runtime)" npm install --no-fund --no-audit --loglevel=error "${_NPM_REGISTRY_ARGS[@]+"${_NPM_REGISTRY_ARGS[@]}"}" || _oxc_install_rc=$?
+ if [ "$_oxc_install_rc" -ne 0 ] && _npm_mirror_retry "npm install (oxc validator runtime)"; then
+ _oxc_install_rc=0
+ fi
_CAPTURE_LOG=""
if [ "$_oxc_install_rc" -ne 0 ]; then
_suggest_npm_registry "$_OXC_INSTALL_LOG"
@@ -2011,6 +2348,7 @@ else
fi
install_python_stack() {
+ [ "${STUDIO_LOCAL_INSTALL:-0}" = 1 ] && [ -x "$VENV_DIR/bin/python" ] || _mirror_fallback
python "$SCRIPT_DIR/install_python_stack.py"
}
@@ -2051,7 +2389,7 @@ _setup_http_get_timed() {
# Same archive and destination as astral's installer, but it fetches a data file with a
# pinned SHA-256 instead of piping remote script text into a shell. Mirrors install.sh.
# See tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
-# Bumping the version means bumping every hash:
+# Bumping the version means bumping every hash, and every _setup_uv_pinned_wheel entry:
# curl -sL https://github.com/astral-sh/uv/releases/download/<ver>/<asset>.sha256
#
# Only the four mainstream targets are pinned; the rest fall through to the existing path
@@ -2112,6 +2450,30 @@ _setup_uv_pinned_asset() {
return 0
}
+_setup_uv_pinned_wheel() {
+ case "$1" in
+ uv-x86_64-unknown-linux-gnu.tar.gz)
+ echo "packages/72/d6/207945fe69903b9794e2ef3e42608c91a59972567343a6719078d99c71f7/uv-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl 27211df9b277f440dea438a4e525ba40250fb721ad39b8927eefc2d91f9aea15" ;;
+ uv-aarch64-unknown-linux-gnu.tar.gz)
+ echo "packages/9a/c7/29e426865c2eb8df61253dae93b953523f48c9fca1e471c7e49ff068f19a/uv-0.12.1-py3-none-manylinux_2_28_aarch64.whl b255ac23958e45f39f9c7a4cd65890df5ef46f539a3b14de03bd296bbba9cb60" ;;
+ uv-x86_64-apple-darwin.tar.gz)
+ echo "packages/fd/07/a417475380e901f4325d13b09938baab227b0c143547124b944c5bc71783/uv-0.12.1-py3-none-macosx_10_12_x86_64.whl 41b8fc2335f682312a1ca39a7b4abfd6af800992065c663582ca3e4d51cf9258" ;;
+ uv-aarch64-apple-darwin.tar.gz)
+ echo "packages/c9/68/391ff0cc3d8020e64adc43bb4e50607f744c69e792fb7623dc7c1526704b/uv-0.12.1-py3-none-macosx_11_0_arm64.whl 2e9b0b86e180abc5968b979c6e25203b32e85969abb5083ee1e8b88a5aa98a76" ;;
+ *) return 1 ;;
+ esac
+}
+
+# Mirrors _uv_unzip in install.sh: GNU tar cannot read a wheel and minimal images lack unzip.
+_setup_uv_unzip() {
+ if command -v unzip >/dev/null 2>&1 && unzip -qo "$1" -d "$2" >/dev/null 2>&1; then return 0; fi
+ case "$(tar --version 2>/dev/null)" in
+ *bsdtar*) tar -xf "$1" -C "$2" 2>/dev/null && return 0 ;;
+ esac
+ command -v python3 >/dev/null 2>&1 &&
+ python3 -c 'import sys, zipfile; zipfile.ZipFile(sys.argv[1]).extractall(sys.argv[2])' "$1" "$2" 2>/dev/null
+}
+
_setup_uv_sha256() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" 2>/dev/null | awk '{print $1}'
@@ -2260,6 +2622,7 @@ _SIUP_STAGE=""
_SIUP_STAGE2=""
_setup_install_uv_pinned() {
+ _SIUP_UNFETCHED=false
_siup_spec=$(_setup_uv_pinned_asset) || return 1
[ -n "$_siup_spec" ] || return 1
_siup_asset=${_siup_spec%% *}
@@ -2293,15 +2656,28 @@ _setup_install_uv_pinned() {
_siup_bases="${UV_INSTALLER_GHE_BASE_URL%/}/astral-sh/uv/releases/download/$_SETUP_UV_PINNED_VERSION"
elif [ -n "${UV_INSTALLER_GITHUB_BASE_URL:-}" ]; then
_siup_bases="${UV_INSTALLER_GITHUB_BASE_URL%/}/astral-sh/uv/releases/download/$_SETUP_UV_PINNED_VERSION"
+ elif [ -n "${UNSLOTH_UV_WHEEL_MIRROR:-}" ]; then
+ _siup_bases=""
+ if _siup_wheel=$(_setup_uv_pinned_wheel "$_siup_asset"); then
+ _siup_path=${_siup_wheel% *}
+ _siup_bases="${UNSLOTH_UV_WHEEL_MIRROR%/}/${_siup_path%/*}"
+ _siup_asset=${_siup_path##*/}
+ _siup_want=${_siup_wheel##* }
+ fi
else
_siup_bases="https://releases.astral.sh/github/uv/releases/download/$_SETUP_UV_PINNED_VERSION
https://github.com/astral-sh/uv/releases/download/$_SETUP_UV_PINNED_VERSION"
fi
+ _SIUP_UNFETCHED=true
for _siup_base in $_siup_bases; do
_setup_http_get "$_siup_base/$_siup_asset" > "$_siup_work/$_siup_asset" 2>/dev/null || continue
[ -s "$_siup_work/$_siup_asset" ] || continue
+ _SIUP_UNFETCHED=false
[ "$(_setup_uv_sha256 "$_siup_work/$_siup_asset")" = "$_siup_want" ] || continue
- tar -xzf "$_siup_work/$_siup_asset" -C "$_siup_work" 2>/dev/null || continue
+ case "$_siup_asset" in
+ *.whl) _setup_uv_unzip "$_siup_work/$_siup_asset" "$_siup_work" || continue ;;
+ *) tar -xzf "$_siup_work/$_siup_asset" -C "$_siup_work" 2>/dev/null || continue ;;
+ esac
mkdir -p "$_siup_dest" 2>/dev/null || break
# Stage both, then publish both, as install.sh does: the renames sit next to each
# other so the pair is replaced as one.
@@ -2678,7 +3054,7 @@ elif {
step "uv" "no installed uv at $_SETUP_UV_LOOKED; installing the pinned release"
fi
_SETUP_UV_PINNED_OK=false
- if _setup_install_uv_pinned; then
+ if _setup_install_uv_pinned || { [ "$_SIUP_UNFETCHED" = true ] && _mirror_switch uvbin && _setup_install_uv_pinned; }; then
_SETUP_UV_PINNED_OK=true
elif _is_verbose; then
_setup_http_get https://astral.sh/uv/install.sh | sh
@@ -2702,7 +3078,16 @@ fast_install() {
fast_install_sidecar() (
unset UV_OVERRIDE
- fast_install "$@"
+ fast_install "$@" && return 0
+ _fis_rc=$?
+ # A pin the PyPI mirror has not synced yet: one rerun with pypi.org behind it, armed only after a slow pypi.org was switched.
+ for _fis_entry in ${_UNSLOTH_MIRROR_SPARE:-}; do
+ [ "${_fis_entry%%|*}" = unsynced ] || continue
+ for _fis_pair in $(printf '%s' "${_fis_entry#*|}" | tr '|' ' '); do export "$_fis_pair"; done
+ fast_install "$@"
+ return
+ done
+ return "$_fis_rc"
)
cd "$SCRIPT_DIR"
@@ -3162,6 +3547,7 @@ _sidecar_top_up_tiktoken() {
[ -d "$_stt_info" ] && { rm -rf "$_stt_info" || true; }
done
unset _stt_info
+ _mirror_fallback
if ! fast_install_sidecar --target "$_stt_dir" --no-deps --upgrade "tiktoken" >/dev/null 2>&1; then
if _sidecar_drop_tiktoken "$_stt_dir"; then
substep "could not install tiktoken into the $_stt_label sidecar -- Qwen tokenizers may fail"
@@ -3235,6 +3621,7 @@ _install_sidecar() {
_is_dir="$1"
_is_ver="$2"
_is_label="$3"
+ _mirror_fallback
_assert_studio_owned_or_absent "$_is_dir" "transformers $_is_label sidecar venv"
[ -d "$_is_dir" ] && rm -rf "$_is_dir"
mkdir -p "$_is_dir"
diff --git a/tests/python/test_install_python_stack.py b/tests/python/test_install_python_stack.py
index 8d8cda1c5b8..360ad9f6faf 100644
--- a/tests/python/test_install_python_stack.py
+++ b/tests/python/test_install_python_stack.py
@@ -3220,3 +3220,99 @@ class TestExpectedTorchFlavorResolution:
# reason: it is reached with the pin still in the environment.
helper = inspect.getsource(ips._recordable_torch_flavor_tag)
assert "return" in helper and "_explicit_torch_index_url()" not in helper
+
+
+PYPI_SPARE = "pypi|UV_DEFAULT_INDEX=https://m/simple|PIP_INDEX_URL=https://m/simple"
+TORCH_SPARE = "torch|UNSLOTH_PYTORCH_MIRROR=https://m/whl"
+UNSYNCED_SPARE = "unsynced|UV_DEFAULT_INDEX=https://pypi.org/simple|UV_INDEX=https://m/simple|UV_INDEX_STRATEGY=unsafe-first-match"
+TORCH_WHL = "https://download.pytorch.org/whl"
+PYPI_DOWN = b"Failed to fetch: `https://pypi.org/simple/foo/`\n Caused by: operation timed out\n"
+TORCH_DOWN = b"HTTP status server error (503) for url (https://download.pytorch.org/whl/cu128/)\n"
+NO_VERSION = b"no version of foo==9\nhint: `foo` was found on https://pypi.org/simple\n"
+PIN = ("--index-url", "https://proxy.example/simple")
+UV_STALL = (
+ b"Failed to download `torch==2.9.1`: Failed to download distribution due to network timeout.\n"
+)
+OTHER_DOWN = b"error sending request for url (https://repo.example/x/): operation timed out\n"
+PIP_RESET_NAMES_NO_HOST = (
+ b"connection broken by 'ConnectionResetError(54, 'Connection reset by peer')': /simple/\n"
+)
+
+
+class TestMirrorRetry:
+ """A transport failure on a default the installer's probe left alone reruns once through its mirror."""
+
+ @pytest.fixture(autouse = True)
+ def _spared(self, monkeypatch):
+ monkeypatch.setattr(ips, "_PYTORCH_WHL_BASE", TORCH_WHL)
+ monkeypatch.setattr(ips, "_step", lambda *a, **k: None)
+ monkeypatch.setattr(ips, "USE_UV", True)
+ with mock.patch.dict(os.environ):
+ for name in ("UV_DEFAULT_INDEX", "PIP_INDEX_URL", "UNSLOTH_PYTORCH_MIRROR"):
+ os.environ.pop(name, None)
+ os.environ["_UNSLOTH_MIRROR_SPARE"] = f"{PYPI_SPARE} {TORCH_SPARE}"
+ yield
+
+ def _runs(self, monkeypatch, ok, failure):
+ runs = []
+
+ def fake_run(cmd, **kwargs):
+ runs.append((cmd, dict(kwargs.get("env") or os.environ)))
+ passed = ok(*runs[-1])
+ output = PIP_RESET_NAMES_NO_HOST if "-m" in cmd else failure
+ return subprocess.CompletedProcess(cmd, 0 if passed else 1, b"" if passed else output)
+
+ monkeypatch.setattr(ips.subprocess, "run", fake_run)
+ return runs
+
+ @pytest.mark.parametrize("installer", ("pip_install", "pip_install_try"))
+ @pytest.mark.parametrize("failure", (PYPI_DOWN, UV_STALL))
+ def test_pypi_transport_failure_moves_to_the_mirror(self, monkeypatch, installer, failure):
+ on_mirror = lambda cmd, env: env.get("UV_DEFAULT_INDEX") == "https://m/simple"
+ runs = self._runs(monkeypatch, on_mirror, failure)
+ assert getattr(ips, installer)("deps", "foo", constrain = False) in (None, True)
+ assert "UV_DEFAULT_INDEX" not in runs[0][1] and on_mirror(*runs[-1])
+ assert os.environ["PIP_INDEX_URL"] == os.environ["UV_DEFAULT_INDEX"] == "https://m/simple"
+ assert os.environ["_UNSLOTH_MIRROR_SPARE"] == TORCH_SPARE
+
+ @pytest.mark.parametrize("installer", ("pip_install", "pip_install_try"))
+ @pytest.mark.parametrize("mirror", ("https://m/whl", None))
+ def test_torch_mirror_is_kept_only_when_it_works(self, monkeypatch, installer, mirror):
+ ok = lambda cmd, env: mirror is not None and "https://m/whl/cu128" in cmd
+ runs = self._runs(monkeypatch, ok, UV_STALL)
+ fails = installer == "pip_install" and mirror is None
+ result = None
+ with pytest.raises(SystemExit) if fails else contextlib.nullcontext():
+ result = getattr(ips, installer)(
+ "torch", "--index-url", f"{TORCH_WHL}/cu128", constrain = False
+ )
+ assert result in (None, mirror is not None)
+ assert "https://m/whl/cu128" in runs[-1][0]
+ assert os.environ["_UNSLOTH_MIRROR_SPARE"] == PYPI_SPARE
+ assert os.environ.get("UNSLOTH_PYTORCH_MIRROR") == mirror
+ assert ips._PYTORCH_WHL_BASE == (mirror or TORCH_WHL)
+
+ @pytest.mark.parametrize(
+ "failure, pin",
+ (
+ (NO_VERSION, ()),
+ (OTHER_DOWN, ()),
+ (TORCH_DOWN, ()),
+ (PYPI_DOWN, PIN),
+ (UV_STALL, ("x @ https://github.com/x/x/archive/main.zip",)),
+ ),
+ )
+ def test_other_failures_are_not_retried(self, monkeypatch, failure, pin):
+ runs = self._runs(monkeypatch, lambda cmd, env: False, failure)
+ assert not ips.pip_install_try("x", *pin, "foo", constrain = False)
+ assert len(runs) == 1
+ assert os.environ["_UNSLOTH_MIRROR_SPARE"] == f"{PYPI_SPARE} {TORCH_SPARE}"
+
+ @pytest.mark.parametrize("pin, kept", (((), True), (PIN, False), (("--no-index",), False)))
+ def test_a_release_the_mirror_lacks_reruns_with_pypi(self, monkeypatch, pin, kept):
+ spare = os.environ["_UNSLOTH_MIRROR_SPARE"] = f"{TORCH_SPARE} {UNSYNCED_SPARE}"
+ os.environ["UV_DEFAULT_INDEX"] = mirror = "https://m/simple"
+ self._runs(monkeypatch, lambda cmd, env: env.get("UV_INDEX") == mirror, NO_VERSION)
+ assert ips.pip_install_try("x", *pin, "foo", constrain = False) is kept
+ assert os.environ["UV_DEFAULT_INDEX"] == ("https://pypi.org/simple" if kept else mirror)
+ assert os.environ["_UNSLOTH_MIRROR_SPARE"] == (TORCH_SPARE if kept else spare)
diff --git a/tests/python/test_windows_arm64_wheelhouse_tags.py b/tests/python/test_windows_arm64_wheelhouse_tags.py
index 50ea9dde5b7..6f4c7b579d2 100644
--- a/tests/python/test_windows_arm64_wheelhouse_tags.py
+++ b/tests/python/test_windows_arm64_wheelhouse_tags.py
@@ -1492,7 +1492,7 @@ class TestTheSkipGateAuditsTheArm64FilteredFile:
):
callers.setdefault(node.name, []).append(ast.unparse(sub.args[0]))
assert callers == {
- "pip_install": ["req"],
+ "_pip_install_once": ["req"],
"pip_install_try": ["req"],
"_closure_record": ["req"],
"_requirements_satisfied": ["req"],
diff --git a/tests/sh/test_linux_uv_python_downloads_manual.sh b/tests/sh/test_linux_uv_python_downloads_manual.sh
index c425a8ecea8..97310077a6e 100755
--- a/tests/sh/test_linux_uv_python_downloads_manual.sh
+++ b/tests/sh/test_linux_uv_python_downloads_manual.sh
@@ -123,7 +123,8 @@ _STREAM=$(mktemp)
printf 'PYTHON_SKIP="3.13.8"; SKIP_TORCH=false\n'
printf 'step() { :; }\ntauri_log() { :; }\n'
for _f in _is_verbose tauri_stream_log tauri_clear_install_error _redact_install_output \
- _uv_download_markers run_install_cmd _macos_has_selected_install_name_tool \
+ _uv_download_markers run_install_cmd _ric_tee _ric_run _run_install_cmd_once _mirror_retry_install _mirror_failed_host \
+ _macos_has_selected_install_name_tool \
_run_uv_venv _python_skip_applies _python_request _uv_venv_requested; do
sed -n "/^$_f()/,/^}/p" "$INSTALL_SH"
done
diff --git a/tests/sh/test_macos_venv_python_preference.sh b/tests/sh/test_macos_venv_python_preference.sh
index 27445601037..fdfb6839dc1 100755
--- a/tests/sh/test_macos_venv_python_preference.sh
+++ b/tests/sh/test_macos_venv_python_preference.sh
@@ -84,7 +84,8 @@ _STREAM=$(mktemp)
printf 'C_ERR=""; TAURI_MODE=true; UNSLOTH_VERBOSE=false\n'
printf 'step() { :; }\ntauri_log() { :; }\n'
for _f in _is_verbose tauri_stream_log tauri_clear_install_error _redact_install_output \
- run_install_cmd _macos_has_selected_install_name_tool _run_uv_venv _uv_venv_arm64; do
+ run_install_cmd _ric_tee _ric_run _run_install_cmd_once _mirror_retry_install _mirror_failed_host \
+ _macos_has_selected_install_name_tool _run_uv_venv _uv_venv_arm64; do
sed -n "/^$_f()/,/^}/p" "$INSTALL_SH"
done
} > "$_STREAM"
diff --git a/tests/sh/test_mirror_fallback.sh b/tests/sh/test_mirror_fallback.sh
new file mode 100644
index 00000000000..df73e6b4c92
--- /dev/null
+++ b/tests/sh/test_mirror_fallback.sh
@@ -0,0 +1,325 @@
+#!/bin/bash
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+. "$SCRIPT_DIR/_harness.sh"
+INSTALL_SH="$SCRIPT_DIR/../../install.sh"
+SETUP_SH="$SCRIPT_DIR/../../studio/setup.sh"
+
+_block() { sed -n '/^# ── BEGIN mirror fallback/,/^# ── END mirror fallback ──$/p' "$1"; }
+
+_WORK=$(mktemp -d)
+trap 'rm -rf "$_WORK"' EXIT
+_block "$INSTALL_SH" > "$_WORK/block.sh"
+if [ "$(_block "$SETUP_SH")" = "$(cat "$_WORK/block.sh")" ]; then
+ ok "setup.sh carries the same mirror fallback block as install.sh"
+else
+ bad "setup.sh mirror fallback block drifted from install.sh"
+fi
+_heredoc_block_lines() { awk '
+ tag != "" { if ($0 == tag || (dash && $0 ~ "^\t*" tag "$")) tag = ""; else if (/^[ \t]*# ── BEGIN mirror fallback/) print NR; next }
+ !/^[ \t]*#/ && match($0, /(^|[^<])<<-?[ \t]*["\047]?[A-Za-z_][A-Za-z_0-9]*["\047]?([ \t|;&)>]|$)/) {
+ t = substr($0, RSTART, RLENGTH); sub(/^[^<]*<</, "", t); dash = (t ~ /^-/)
+ gsub(/^-?[ \t]*["\047]?|["\047]?[ \t|;&)>]*$/, "", t); tag = t }' "$1"; }
+for _f in "$INSTALL_SH" "$SETUP_SH"; do
+ assert_eq "${_f##*/} defines the mirror fallback outside every heredoc" "" "$(_heredoc_block_lines "$_f")"
+done
+
+mkdir -p "$_WORK/bin"
+cat > "$_WORK/bin/curl" <<'EOF'
+#!/bin/sh
+for _a in "$@"; do _url="$_a"; done
+echo "$_url" >> "$MOCK_LOG"
+case "$_url" in
+ https://tuna.mirrors.cernet.edu.cn/pypi/web/simple/*) _h=CERNETPYPIINDEX ;; https://tuna.mirrors.cernet.edu.cn/pytorch/whl/cpu/torch/) _h=CERNETTORCHINDEX ;;
+ https://tuna.mirrors.cernet.edu.cn/pytorch/*) _h=CERNETTORCH ;; https://registry.npmmirror.com/-/binary/node/*) _h=NPMMIRRORNODE ;;
+ https://tuna.mirrors.cernet.edu.cn/*) _h=CERNET ;; https://registry.npmmirror.com/*) _h=NPMMIRROR ;;
+ https://pypi.org/*) _h=PYPIINDEX ;; https://download.pytorch.org/*) _h=TORCHINDEX ;;
+ https://files.pythonhosted.org/*) _h=PYPI ;; https://download-r2.pytorch.org/*) _h=TORCH ;;
+ https://nodejs.org/*) _h=NODE ;; https://registry.npmjs.org/*) _h=NPM ;; *) _h=ASTRAL ;;
+esac
+eval "_r=\${MOCK_$_h:-fast}"
+if [ -f "$MOCK_LOG.$_h" ]; then _r=${_r#*|}; else : > "$MOCK_LOG.$_h"; _r=${_r%%|*}; fi
+# The mirrors redirect, so a probe that does not follow redirects or fetches the whole file is wrong.
+case " $* " in *" -sL "*" -r 0-"*) ;; *) printf '000 0.000'; exit 2 ;; esac
+case " $* " in *" --max-time 1.5 "*) [ ! -f "$MOCK_LOG.busy" ] || : > "$MOCK_LOG.overlap"; : > "$MOCK_LOG.busy"; sleep 0.05; rm -f "$MOCK_LOG.busy" ;; esac
+[ -z "${MOCK_OLD_CURL:-}" ] || case " $* " in *" 1.5 "*) exit 2 ;; esac
+case "$_r" in
+ fast) printf '206 4000000.000' ;;
+ slow) printf '206 300000.000'; exit 28 ;;
+ blocked) printf '000 0.000'; exit 7 ;;
+ *) printf '%s.000' "$_r" ;;
+esac
+EOF
+chmod +x "$_WORK/bin/curl"
+
+_VARS="UV_DEFAULT_INDEX UV_INDEX UV_INDEX_STRATEGY PIP_INDEX_URL PIP_EXTRA_INDEX_URL UNSLOTH_PYTORCH_MIRROR UNSLOTH_NODE_MIRROR UNSLOTH_NPM_REGISTRY UV_PYTHON_INSTALL_MIRROR UNSLOTH_UV_WHEEL_MIRROR"
+
+_run() {
+ _shell="$1"; shift
+ rm -f "$_WORK/curl.log".*
+ : > "$_WORK/curl.log"
+ mkdir -p "$_WORK/home"
+ case "$_shell" in
+ dash) _flags="set -e" ;;
+ *) _flags="set -euo pipefail" ;;
+ esac
+ env -i PATH="$_WORK/bin:/usr/bin:/bin" HOME="$_WORK/home" MOCK_LOG="$_WORK/curl.log" "$@" \
+ "$_shell" -c "$_flags
+C_WARN=; step() { echo \"STEP \$2\"; }; substep() { echo \"SUBSTEP \$1\"; }
+. '$_WORK/block.sh'
+_mirror_in_china() { [ \"\${MOCK_CN:-1}\" = 1 ]; }
+_mirror_fallback \${_MF_ARGS:-}
+eval \"\${_AFTER:-}\"
+for _v in $_VARS; do eval \"[ -z \\\"\\\${\$_v+x}\\\" ] || echo \\\"\$_v=\\\$\$_v\\\"\"; done"
+}
+
+M=https://tuna.mirrors.cernet.edu.cn
+for SH in dash bash; do
+ command -v "$SH" >/dev/null 2>&1 || { echo " SKIP: $SH not installed"; continue; }
+ out=$(_run "$SH")
+ assert_eq "[$SH] fast hosts export and print nothing" "" "$out"
+ out=$(_run "$SH" MOCK_CN=0 MOCK_PYPI=blocked MOCK_TORCH=blocked MOCK_NPM=blocked _AFTER='echo "SPARE=${_UNSLOTH_MIRROR_SPARE-unset} PROBED=${_UNSLOTH_MIRROR_PROBED-unset}"')
+ assert_eq "[$SH] outside China: nothing probed, switched, armed or exported" "SPARE=unset PROBED=unset" "$out$(cat "$_WORK/curl.log")"
+ assert_eq "[$SH] outside China: no retry is armed either" "SPARE=unset" "$(_run "$SH" MOCK_CN=0 _MF_ARGS=spare _AFTER='echo "SPARE=${_UNSLOTH_MIRROR_SPARE-unset}"')"
+ assert_eq "[$SH] outside China: a retry state inherited from a parent is dropped" "SPARE=unset" "$(_run "$SH" MOCK_CN=0 _MF_ARGS=spare _UNSLOTH_MIRROR_SPARE='pypi|UV_DEFAULT_INDEX=https://x' _AFTER='echo "SPARE=${_UNSLOTH_MIRROR_SPARE-unset}"')"
+ assert_eq "[$SH] UNSLOTH_MIRROR_FALLBACK=0: a retry state inherited from a parent is dropped" "SPARE=unset" "$(_run "$SH" UNSLOTH_MIRROR_FALLBACK=0 _UNSLOTH_MIRROR_SPARE='pypi|UV_DEFAULT_INDEX=https://x' _AFTER='echo "SPARE=${_UNSLOTH_MIRROR_SPARE-unset}"')"
+ assert_eq "[$SH] the location is read once per process" "1" "$(env -i PATH=/usr/bin:/bin TZ=UTC "$SH" -c ". '$_WORK/block.sh'; n=0; _mirror_in_china() { n=\$((n+1)); false; }; _mirror_fallback spare; _mirror_fallback; _mirror_fallback; echo \$n")"
+ for _off in 1 true " Yes " on T; do
+ out=$(_run "$SH" MOCK_PYPI=blocked UV_OFFLINE="$_off" _AFTER='echo "SPARE=${_UNSLOTH_MIRROR_SPARE%%|*}"')
+ assert_eq "[$SH] UV_OFFLINE='$_off': nothing probed, retries still armed" "SPARE=pypi" "$out$(cat "$_WORK/curl.log")"
+ done
+ assert_contains "[$SH] UV_OFFLINE=0 still probes" "$(_run "$SH" MOCK_PYPI=blocked UV_OFFLINE=0)" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ for _on in 1 true yes on; do
+ assert_contains "[$SH] outside China, UNSLOTH_MIRROR_FALLBACK=$_on opts in" "$(_run "$SH" MOCK_CN=0 UNSLOTH_MIRROR_FALLBACK=$_on MOCK_PYPI=blocked)" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ done
+ assert_eq "[$SH] in China, UNSLOTH_MIRROR_FALLBACK=0 still opts out" "" "$(_run "$SH" UNSLOTH_MIRROR_FALLBACK=0 MOCK_PYPI=blocked)$(cat "$_WORK/curl.log")"
+ _cn() { env -i PATH=/usr/bin:/bin "$@" "$SH" -c ". '$_WORK/block.sh'; _mirror_in_china && echo yes || echo no"; }
+ for _tz in Asia/Shanghai :Asia/Chongqing /usr/share/zoneinfo/Asia/Urumqi PRC; do
+ assert_eq "[$SH] TZ=$_tz is mainland China" "yes" "$(_cn TZ=$_tz)"
+ done
+ sed "s#/etc/resolv.conf /run/systemd/resolve/resolv.conf#$_WORK/resolv.conf#" "$_WORK/block.sh" > "$_WORK/block_dns.sh"
+ for _ns in "100.100.2.136 yes" "183.60.83.19 yes" "223.5.5.5 yes" "114.114.114.114 yes" "114.114.115.119 yes" "182.254.116.116 yes" "8.8.8.8 no" "100.100.100.100 no" "1.1.1.1 no"; do
+ printf 'search example\nnameserver %s\n' "${_ns% *}" > "$_WORK/resolv.conf"
+ assert_eq "[$SH] resolver ${_ns% *} in UTC: China=${_ns#* }" "${_ns#* }" "$(env -i PATH=/usr/bin:/bin TZ=UTC "$SH" -c ". '$_WORK/block_dns.sh'; _mirror_in_china && echo yes || echo no")"
+ done
+ if ! grep -Eqs '^[[:space:]]*nameserver[[:space:]]+(223[.]5[.]5[.]5|223[.]6[.]6[.]6|119[.]29[.]29[.]29|114[.]114[.]11[45][.]11[45]|180[.]76[.]76[.]76|1[.]2[.]4[.]8|210[.]2[.]4[.]8|100[.]100[.]2[.]13[68]|183[.]60[.]8[23][.](19|98))' /etc/resolv.conf /run/systemd/resolve/resolv.conf; then
+ for _tz in UTC America/New_York Asia/Hong_Kong Asia/Taipei Asia/Singapore; do
+ assert_eq "[$SH] TZ=$_tz is not mainland China" "no" "$(_cn TZ=$_tz)"
+ done
+ fi
+ assert_eq "[$SH] fast hosts never touch a mirror" "" "$(grep -E 'cernet|npmmirror' "$_WORK/curl.log" || true)"
+ assert_eq "[$SH] defaults are timed one at a time" "no" "$([ -f "$_WORK/curl.log.overlap" ] && echo overlap || echo no)"
+ assert_contains "[$SH] a curl that rejects the probe counts as no answer" "$(_run "$SH" MOCK_OLD_CURL=1 MOCK_PYPI=blocked)" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ out=$(_run "$SH" MOCK_PYPI=blocked)
+ assert_contains "[$SH] blocked pypi: uv default index is the mirror" "$out" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ assert_contains "[$SH] blocked pypi: pip index is the mirror" "$out" "PIP_INDEX_URL=$M/pypi/web/simple"
+ assert_not_contains "[$SH] blocked pypi: no unreachable second uv index" "$out" "UV_INDEX="
+ assert_not_contains "[$SH] blocked pypi: no unsafe-first-match against a dead index" "$out" "UV_INDEX_STRATEGY"
+ assert_not_contains "[$SH] blocked pypi: no pip extra index" "$out" "PIP_EXTRA_INDEX_URL"
+ assert_contains "[$SH] blocked pypi: says so with both speeds" "$out" "STEP PyPI is blocked (0 KB/s, mirror 3906 KB/s); using $M/pypi/web/simple"
+ assert_contains "[$SH] a switch names the opt-out" "$out" "SUBSTEP Set UNSLOTH_MIRROR_FALLBACK=0"
+ out=$(_run "$SH" MOCK_PYPI=slow _AFTER='echo "SPARE $_UNSLOTH_MIRROR_SPARE"')
+ assert_eq "[$SH] slow pypi: the mirror is the only uv and pip index" "UV_DEFAULT_INDEX=$M/pypi/web/simple PIP_INDEX_URL=$M/pypi/web/simple" "$(echo "$out" | grep -E '^(UV_|PIP_)' | paste -sd' ' -)"
+ assert_contains "[$SH] slow pypi: pypi.org, which still answers, is spared behind the mirror for what it has not synced" "$out" " unsynced|UV_DEFAULT_INDEX=https://pypi.org/simple|UV_INDEX=$M/pypi/web/simple|UV_INDEX_STRATEGY=unsafe-first-match|PIP_EXTRA_INDEX_URL=https://pypi.org/simple|PIP_INDEX_URL=$M/pypi/web/simple"
+ assert_eq "[$SH] slow pypi: raced again beside the mirror" "2" "$(grep -c files.pythonhosted "$_WORK/curl.log")"
+ assert_contains "[$SH] a user index strategy is kept" "$(_run "$SH" MOCK_PYPI=slow UV_INDEX_STRATEGY=first-index _AFTER='echo "SPARE $_UNSLOTH_MIRROR_SPARE"')" "unsynced|UV_DEFAULT_INDEX=https://pypi.org/simple|UV_INDEX=$M/pypi/web/simple|UV_INDEX_STRATEGY=first-index|"
+ assert_not_contains "[$SH] blocked pypi: nothing is spared behind the mirror" "$(_run "$SH" MOCK_PYPI=blocked _AFTER='echo "SPARE $_UNSLOTH_MIRROR_SPARE"')" "unsynced"
+ assert_eq "[$SH] a mirror slower than the slow default changes nothing" "" "$(_run "$SH" MOCK_PYPI=slow MOCK_CERNET="206 200000")"
+ out=$(_run "$SH" MOCK_PYPI=blocked MOCK_CERNET=blocked)
+ assert_eq "[$SH] a dead mirror changes and prints nothing" "" "$out"
+ assert_eq "[$SH] a default back above 1 MiB/s in the race is kept" "" "$(_run "$SH" MOCK_PYPI="slow|206 2000000" MOCK_CERNET="206 3000000")"
+ assert_contains "[$SH] a redirect that stalls counts as blocked" "$(_run "$SH" MOCK_PYPI="302 0")" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ assert_contains "[$SH] an HTTP error in the race weighs as 0 B/s" "$(_run "$SH" MOCK_PYPI="slow|404 5000000" MOCK_CERNET="206 400000")" "STEP PyPI is blocked (0 KB/s, mirror 390 KB/s)"
+ out=$(_run "$SH" MOCK_PYPI="404 0")
+ assert_eq "[$SH] a default answering an HTTP error is kept, without timing the mirror" "" "$out$(grep -F "$M" "$_WORK/curl.log" || true)"
+ out=$(_run "$SH" MOCK_TORCH=blocked MOCK_NODE=slow MOCK_NPM=blocked)
+ for _kv in "UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl" "UNSLOTH_NODE_MIRROR=https://registry.npmmirror.com/-/binary/node" "UNSLOTH_NPM_REGISTRY=https://registry.npmmirror.com"; do assert_contains "[$SH] mirror $_kv" "$out" "$_kv"; done
+ assert_not_contains "[$SH] only slow hosts switch" "$out" "PIP_INDEX_URL"
+ assert_eq "[$SH] each mirror tree is timed once" "3" "$(grep -E '^https://(tuna[.]mirrors[.]cernet[.]edu[.]cn|registry[.]npmmirror[.]com)/' "$_WORK/curl.log" | grep -cE '[.](t?gz|whl)$')"
+ out=$(_run "$SH" MOCK_TORCH=blocked MOCK_NODE=blocked MOCK_NPMMIRRORNODE=blocked)
+ assert_contains "[$SH] torch waits on CERNET's torch tree ..." "$out" "UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl"
+ assert_not_contains "[$SH] ... and node on its node tree" "$out" "UNSLOTH_NODE_MIRROR"
+ assert_contains "[$SH] an unreachable pypi.org means blocked mode" "$(_run "$SH" MOCK_PYPIINDEX=blocked)" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ assert_eq "[$SH] a pypi.org index that answers in the race is not blocked" "" "$(_run "$SH" MOCK_PYPIINDEX="blocked|fast")"
+ for _npm_env in NPM_CONFIG_REGISTRY npm_config_registry; do
+ out=$(_run "$SH" MOCK_NPM=blocked "$_npm_env=https://corp.example/npm/" _AFTER='echo "SPARE $_UNSLOTH_MIRROR_SPARE"')
+ assert_not_contains "[$SH] $_npm_env: no npm switch or retry" "$out" "UNSLOTH_NPM_REGISTRY"
+ done
+ out=$(_run "$SH" MOCK_PYPI=blocked MOCK_TORCH=blocked MOCK_ASTRAL=blocked MOCK_CERNETPYPIINDEX=blocked)
+ assert_not_contains "[$SH] a mirror whose index does not answer is not used ..." "$out" "PIP_INDEX_URL"
+ assert_contains "[$SH] ... while a mirror with its own live index is" "$out" "UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl"
+ assert_contains "[$SH] ... and the uv wheel, which skips the index, still is" "$out" "UNSLOTH_UV_WHEEL_MIRROR=$M/pypi/web"
+ assert_eq "[$SH] a mirror answering a redirect or an HTTP error is not used" "" "$(_run "$SH" MOCK_TORCH=blocked MOCK_CERNETTORCH="302 900000" MOCK_NODE=blocked MOCK_NPMMIRRORNODE="403 900000")"
+ assert_not_contains "[$SH] a dead CERNET torch index keeps torch" "$(_run "$SH" MOCK_TORCH=blocked MOCK_CERNETTORCHINDEX=blocked)" "UNSLOTH_PYTORCH_MIRROR"
+ assert_contains "[$SH] an unreachable torch index switches torch" "$(_run "$SH" MOCK_TORCHINDEX=blocked)" "UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl"
+ out=$(_run "$SH" MOCK_PYPI=slow MOCK_CERNET=slow MOCK_NPM=slow MOCK_NPMMIRROR="206 400000")
+ assert_contains "[$SH] each host is weighed against its own mirror" "$out" "UNSLOTH_NPM_REGISTRY=https://registry.npmmirror.com"
+ assert_not_contains "[$SH] ... and a mirror no faster is not used" "$out" "PIP_INDEX_URL"
+ out=$(_run "$SH" MOCK_PYPI=blocked UV_INDEX_URL=https://corp.example/simple)
+ assert_not_contains "[$SH] user uv index env: uv untouched" "$out" "UV_DEFAULT_INDEX"
+ assert_contains "[$SH] user uv index env: pip still falls back" "$out" "PIP_INDEX_URL=$M/pypi/web/simple"
+ mkdir -p "$_WORK/cfg/uv"
+ for _toml in '[[index]]\nurl = "https://corp.example/simple"' 'index = [{ url = "https://corp.example/simple", default = true }]' 'pip.index-url = "https://corp.example/simple"'; do
+ printf "$_toml\n" > "$_WORK/cfg/uv/uv.toml"
+ assert_not_contains "[$SH] uv.toml $_toml: uv untouched" "$(_run "$SH" MOCK_PYPI=blocked XDG_CONFIG_HOME="$_WORK/cfg")" "UV_DEFAULT_INDEX"
+ done
+ rm -rf "$_WORK/cfg"
+ mkdir -p "$_WORK/proj/src"
+ printf '[project]\nname = "p"\n\n[[tool.uv.index]]\nurl = "https://corp.example/simple"\ndefault = true\n' > "$_WORK/proj/pyproject.toml"
+ out=$(cd "$_WORK/proj/src" && _run "$SH" MOCK_PYPI=blocked)
+ assert_not_contains "[$SH] a parent pyproject.toml [tool.uv] index: uv untouched" "$out" "UV_DEFAULT_INDEX"
+ assert_contains "[$SH] a parent pyproject.toml [tool.uv] index: pip still falls back" "$out" "PIP_INDEX_URL=$M/pypi/web/simple"
+ printf 'native-tls = true\n' > "$_WORK/proj/src/uv.toml"
+ assert_contains "[$SH] the nearest uv.toml hides a parent pyproject.toml" "$(cd "$_WORK/proj/src" && _run "$SH" MOCK_PYPI=blocked)" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ rm -rf "$_WORK/proj"
+ mkdir -p "$_WORK/xdg a/pip" "$_WORK/xdg2/uv"
+ printf '[global]\nindex-url = https://corp.example/simple\n' > "$_WORK/xdg a/pip/pip.conf"
+ printf 'index-url = "https://corp.example/simple"\n' > "$_WORK/xdg2/uv/uv.toml"
+ out=$(_run "$SH" MOCK_PYPI=blocked XDG_CONFIG_DIRS="$_WORK/none:$_WORK/xdg a:$_WORK/xdg2")
+ assert_not_contains "[$SH] an index in an XDG_CONFIG_DIRS pip.conf: pip untouched" "$out" "PIP_INDEX_URL"
+ assert_not_contains "[$SH] an index in an XDG_CONFIG_DIRS uv.toml: uv untouched" "$out" "UV_DEFAULT_INDEX"
+ rm -rf "$_WORK/xdg a" "$_WORK/xdg2"
+ mkdir -p "$_WORK/home/.pip"
+ printf '[global]\nindex-url = https://corp.example/simple\n' > "$_WORK/home/.pip/pip.conf"
+ out=$(_run "$SH" MOCK_PYPI=blocked)
+ assert_not_contains "[$SH] pip.conf index: pip untouched" "$out" "PIP_INDEX_URL"
+ printf '[global]\ntimeout = 60\n' > "$_WORK/home/.pip/pip.conf"
+ out=$(_run "$SH" MOCK_PYPI=blocked)
+ assert_contains "[$SH] pip.conf without an index still falls back" "$out" "PIP_INDEX_URL=$M/pypi/web/simple"
+ rm -rf "$_WORK/home/.pip"
+ mkdir -p "$_WORK/venv"
+ printf '[global]\nindex-url = https://corp.example/simple\n' > "$_WORK/venv/pip.conf"
+ out=$(_run "$SH" MOCK_PYPI=blocked VENV_DIR="$_WORK/venv")
+ assert_not_contains "[$SH] the Studio venv's pip.conf index: pip untouched" "$out" "PIP_INDEX_URL"
+ assert_contains "[$SH] the Studio venv's pip.conf index: uv still falls back" "$out" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ rm -rf "$_WORK/venv"
+ out=$(_run "$SH" MOCK_NODE=blocked _MF_ARGS=spare _AFTER='echo "SPARE $_UNSLOTH_MIRROR_SPARE"')
+ assert_eq "[$SH] spare only: no host is probed" "" "$(cat "$_WORK/curl.log")"
+ assert_contains "[$SH] spare only: every host gets its retry" "$out" "SPARE pypi|UV_DEFAULT_INDEX=$M/pypi/web/simple|PIP_INDEX_URL=$M/pypi/web/simple torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl node|"
+ assert_contains "[$SH] spare only: the probe still runs later" "$(_run "$SH" MOCK_NODE=blocked _MF_ARGS=spare _AFTER=_mirror_fallback)" "UNSLOTH_NODE_MIRROR=https://registry.npmmirror.com/-/binary/node"
+ out=$(_run "$SH" MOCK_PYPI=blocked UV_DEFAULT_INDEX=https://a.example/simple PIP_INDEX_URL=https://a.example/simple)
+ assert_eq "[$SH] both tools configured: PyPI never probed" "" "$(grep -E 'files.pythonhosted|pypi.org' "$_WORK/curl.log" || true)"
+ out=$(_run "$SH" MOCK_ASTRAL=slow)
+ assert_contains "[$SH] slow releases.astral.sh: uv's Python builds come from npmmirror" "$out" "UV_PYTHON_INSTALL_MIRROR=https://registry.npmmirror.com/-/binary/python-build-standalone"
+ assert_contains "[$SH] slow releases.astral.sh: the pinned uv comes from the PyPI mirror" "$out" "UNSLOTH_UV_WHEEL_MIRROR=$M/pypi/web"
+ assert_eq "[$SH] releases.astral.sh is timed once for uv and Python" "2" "$(grep -c releases.astral.sh "$_WORK/curl.log")"
+ out=$(_run "$SH" MOCK_ASTRAL=slow MOCK_NPMMIRROR=blocked)
+ assert_not_contains "[$SH] Python builds wait on npmmirror, not CERNET" "$out" "UV_PYTHON_INSTALL_MIRROR"
+ assert_contains "[$SH] ... while uv still takes CERNET" "$out" "UNSLOTH_UV_WHEEL_MIRROR=$M/pypi/web"
+ printf 'python-install-mirror = "https://corp.example/pbs"\n' > "$_WORK/home/uv.toml"
+ out=$(_run "$SH" MOCK_ASTRAL=blocked MOCK_PYPI=blocked UV_CONFIG_FILE="$_WORK/home/uv.toml")
+ assert_not_contains "[$SH] uv.toml python-install-mirror is kept" "$out" "UV_PYTHON_INSTALL_MIRROR"
+ assert_contains "[$SH] uv.toml python-install-mirror leaves the index fallback on" "$out" "UV_DEFAULT_INDEX=$M/pypi/web/simple"
+ for _src in UV_INSTALLER_GITHUB_BASE_URL UNSLOTH_UV_WHEEL_MIRROR; do out=$(_run "$SH" MOCK_ASTRAL=blocked $_src=https://corp.example/uv)
+ assert_not_contains "[$SH] a user $_src keeps the uv source" "$out" "UNSLOTH_UV_WHEEL_MIRROR=$M"; done
+ out=$(_run "$SH" MOCK_ASTRAL=blocked UV_PYTHON_INSTALL_MIRROR=https://corp.example/pbs UNSLOTH_UV_WHEEL_MIRROR=https://corp.example/uv)
+ assert_eq "[$SH] user Python and uv sources skip the releases.astral.sh probe" "" "$(grep -F releases.astral.sh "$_WORK/curl.log" || true)"
+ out=$(_run "$SH" MOCK_TORCH=blocked UNSLOTH_TORCH_INDEX_URL=https://corp.example/whl/cu128)
+ assert_not_contains "[$SH] a pinned torch index is not overridden" "$out" "UNSLOTH_PYTORCH_MIRROR"
+ out=$(_run "$SH" MOCK_NPM=blocked UNSLOTH_NPM_REGISTRY=https://corp.example/npm/)
+ assert_contains "[$SH] a user npm registry is kept" "$out" "UNSLOTH_NPM_REGISTRY=https://corp.example/npm/"
+ assert_eq "[$SH] a user npm registry skips the npm probe" "" "$(grep -F npmjs "$_WORK/curl.log" || true)"
+ for _off in 0 false off; do
+ out=$(_run "$SH" MOCK_PYPI=blocked UNSLOTH_MIRROR_FALLBACK=$_off)
+ assert_eq "[$SH] UNSLOTH_MIRROR_FALLBACK=$_off probes nothing" "" "$(cat "$_WORK/curl.log")"
+ done
+ out=$(_run "$SH" MOCK_PYPI=blocked _UNSLOTH_MIRROR_PROBED=1)
+ assert_eq "[$SH] a parent installer's probe is not repeated" "" "$(cat "$_WORK/curl.log")"
+ out=$(_run "$SH" MOCK_TORCH=blocked PIP_INDEX_URL=https://corp.example/simple _AFTER='echo "SPARE $_UNSLOTH_MIRROR_SPARE"; _mirror_switch pypi || :; _mirror_switch pypi || echo AGAIN no')
+ assert_eq "[$SH] hosts left on their default, not the switched one, are spared with their blocked-mode vars" "SPARE pypi|UV_DEFAULT_INDEX=$M/pypi/web/simple node|UNSLOTH_NODE_MIRROR=https://registry.npmmirror.com/-/binary/node npm|UNSLOTH_NPM_REGISTRY=https://registry.npmmirror.com python|UV_PYTHON_INSTALL_MIRROR=https://registry.npmmirror.com/-/binary/python-build-standalone uvbin|UNSLOTH_UV_WHEEL_MIRROR=$M/pypi/web" "$(echo "$out" | grep '^SPARE')"
+ assert_eq "[$SH] a failed step switches its host once" "STEP PyPI failed; retrying through $M/pypi/web/simple|AGAIN no|UV_DEFAULT_INDEX=$M/pypi/web/simple" "$(echo "$out" | grep -E '^(STEP PyPI failed|AGAIN|UV_DEFAULT_INDEX)' | paste -sd'|' -)"
+done
+
+_fail_uv_timeout='error: Failed to fetch: `https://pypi.org/simple/six/`
+ Caused by: error sending request for url (https://pypi.org/simple/six/)
+ Caused by: operation timed out'
+_fail_uv_503=' Caused by: HTTP status server error (503 Service Unavailable) for url (https://download.pytorch.org/whl/cu128/torch/)'
+_fail_pip_timeout="WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError(\"HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=3.0)\")': /simple/six/"
+_fail_npm='npm error code ECONNRESET: network request to https://registry.npmjs.org/vite failed, reason: socket hang up'
+_fail_python='error: Failed to download https://releases.astral.sh/github/python-build-standalone/releases/download/20260910/cpython-3.12.12.tar.gz: error decoding response body'
+_fail_uv_stall='error: Failed to download `torch==2.9.1+cu128`: Failed to download distribution due to network timeout. Try increasing UV_HTTP_TIMEOUT (current value: 30s).'
+_fail_nover=' Caused by: Because there is no version of torch==1.0.99 [...] hint: `torch` was found on https://download.pytorch.org/whl/cu128, but not at the requested version (torch==1.0.99). A compatible version may be available on a subsequent index (e.g., https://pypi.org/simple).'
+_fail_uv_lag=' cause: Because only unsloth<=2026.9.9 is available and you require unsloth>=2026.9.10, we can conclude that your requirements are unsatisfiable.'
+_fail_pip_lag='ERROR: No matching distribution found for unsloth>=2026.9.10'
+_fail_git='error: Git operation failed: failed to fetch https://github.com/unslothai/unsloth-zoo: Connection reset by peer (os error 54)'
+_failed_host() { printf '%s\n' "$1" > "$_WORK/fail.log"; sh -c ". '$_WORK/block.sh'; _mirror_failed_host '$_WORK/fail.log' '${2:-}'" || echo none; }
+assert_eq "failed host: the default each transport failure names; unsynced for a version or package not found; none for another host" "pypi torch pypi npm python unsynced unsynced unsynced none" "$(for _o in "$_fail_uv_timeout" "$_fail_uv_503" "$_fail_pip_timeout" "$_fail_npm" "$_fail_python" "$_fail_nover" "$_fail_uv_lag" "$_fail_pip_lag" "$_fail_git"; do _failed_host "$_o"; done | paste -sd' ' -)"
+assert_eq "failed host: a stalled download names no URL, so the host that ran it; none when another URL is named" "torch none none" "$({ _failed_host "$_fail_uv_stall" torch; _failed_host "$_fail_uv_stall"; _failed_host "$_fail_git" pypi; } | paste -sd' ' -)"
+
+{ cat "$_WORK/block.sh"; for _f in run_install_cmd _mirror_retry_install _ric_tee _ric_run _run_install_cmd_once run_install_cmd_retry; do sed -n "/^$_f() {/,/^}/p" "$INSTALL_SH"; done; } > "$_WORK/retry.sh"
+mkdir -p "$_WORK/uvbin"
+cat > "$_WORK/uvbin/uv" <<'EOF'
+#!/bin/sh
+echo "RUN $* ${UV_DEFAULT_INDEX:-}${UV_INDEX:+ +$UV_INDEX}" >&3
+case "$*" in
+ *download.pytorch.org*) printf '%s\n' "$FAIL"; exit 7 ;;
+ *--default-index*) ;;
+ *) [ -n "${UV_DEFAULT_INDEX:-}" ] && { [ -z "${LAGGING:-}" ] || [ -n "${UV_INDEX:-}" ]; } || { printf '%s\n' "$FAIL"; exit 7; } ;;
+esac
+[ -z "${MIRROR_FAILS:-}" ] || { printf '%s\n' "$FAIL"; exit 8; }
+EOF
+chmod +x "$_WORK/uvbin/uv"
+_retry() {
+ _cmd=$1; shift
+ env -i PATH="$_WORK/uvbin:/usr/bin:/bin" _UNSLOTH_MIRROR_SPARE="pypi|UV_DEFAULT_INDEX=$M/pypi/web/simple torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl" UNSLOTH_INSTALL_RETRY_DELAY=0 "$@" sh -c "
+step() { echo \"STEP \$2\"; }; substep() { :; }; tauri_stream_log() { :; }; tauri_clear_install_error() { :; }; _redact_install_output() { cat \"\$@\" > /dev/null; }
+_is_verbose() { [ \"\${VERBOSE:-}\" = 1 ]; }
+_uv_download_markers() { if [ -n \"\$1\" ]; then cat >> \"\$1\"; else cat; fi; }
+. '$_WORK/retry.sh'
+TORCH_INDEX_URL=https://download.pytorch.org/whl/cu128
+\$1 3>&1; echo \"RC \$? INDEX=\${UV_DEFAULT_INDEX:-} TORCH=\$TORCH_INDEX_URL SPARE=\$_UNSLOTH_MIRROR_SPARE\"; [ -z \"\${THEN:-}\" ] || { \$THEN 3>&1; echo \"RC \$?\"; }" retry "$_cmd"
+}
+_pipe() { "$@" 2>/dev/null | grep -E '^(RUN|STEP|RC)' | paste -sd'|' -; }
+assert_eq "retry: a PyPI transport failure reruns once on the mirror, which later steps keep" "RUN pip install foo |STEP PyPI failed; retrying through $M/pypi/web/simple|RUN pip install foo $M/pypi/web/simple|RC 0 INDEX=$M/pypi/web/simple TORCH=https://download.pytorch.org/whl/cu128 SPARE=torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl" "$(_pipe _retry 'run_install_cmd deps uv pip install foo' FAIL="$_fail_uv_timeout")"
+assert_eq "retry: ... also when the output was streamed (verbose)" "RUN pip install foo |STEP PyPI failed; retrying through $M/pypi/web/simple|RUN pip install foo $M/pypi/web/simple|RC 0 INDEX=$M/pypi/web/simple TORCH=https://download.pytorch.org/whl/cu128 SPARE=torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl" "$(_pipe _retry 'run_install_cmd deps uv pip install foo' FAIL="$_fail_uv_timeout" VERBOSE=1)"
+assert_eq "retry: a version not found, with nothing spared behind the mirror, keeps the default and its spares" "RUN pip install foo |RC 7 INDEX= TORCH=https://download.pytorch.org/whl/cu128 SPARE=pypi|UV_DEFAULT_INDEX=$M/pypi/web/simple torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl" "$(_pipe _retry 'run_install_cmd deps uv pip install foo' FAIL="$_fail_nover")"
+assert_eq "retry: a failed mirror rerun restores the default for later steps" "RUN pip install foo |STEP PyPI failed; retrying through $M/pypi/web/simple|RUN pip install foo $M/pypi/web/simple|RC 8 INDEX= TORCH=https://download.pytorch.org/whl/cu128 SPARE=torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl" "$(_pipe _retry 'run_install_cmd deps uv pip install foo' FAIL="$_fail_uv_timeout" MIRROR_FAILS=1)"
+sed -n '/^fast_install_sidecar() (/,/^)$/p' "$SETUP_SH" > "$_WORK/sidecar.sh"
+_sidecar() { env -i PATH=/usr/bin:/bin UV_OVERRIDE=x "$@" bash -c "set -euo pipefail
+fast_install() { echo \"RUN \$* \${UV_INDEX:-}\${UV_OVERRIDE:-}\"; [ -n \"\${UV_INDEX:-}\" ] || return 7; }
+. '$_WORK/sidecar.sh'
+fast_install_sidecar --target d transformers==9 && echo RC 0 || echo RC \$?; echo AFTER \${UV_INDEX:-}" | paste -sd'|' -; }
+assert_eq "sidecar: nothing armed: one run, its code, UV_OVERRIDE dropped as before" "RUN --target d transformers==9 |RC 7|AFTER" "$(_sidecar)"
+assert_eq "sidecar: a pin the mirror lacks reruns once with pypi.org behind it, inside the sidecar only" "RUN --target d transformers==9 |RUN --target d transformers==9 $M/pypi/web/simple|RC 0|AFTER" "$(_sidecar _UNSLOTH_MIRROR_SPARE="torch|UNSLOTH_PYTORCH_MIRROR=x unsynced|UV_DEFAULT_INDEX=https://pypi.org/simple|UV_INDEX=$M/pypi/web/simple|UV_INDEX_STRATEGY=unsafe-first-match")"
+mkdir -p "$_WORK/tmpd"
+for _vb in "" 1; do
+ assert_eq "retry: nothing armed (outside China${_vb:+, verbose}): one run, the original code, no retry" "RUN pip install foo |RC 7 INDEX= TORCH=https://download.pytorch.org/whl/cu128 SPARE=" "$(_pipe _retry 'run_install_cmd deps uv pip install foo' FAIL="$_fail_uv_timeout" _UNSLOTH_MIRROR_SPARE= TMPDIR="$_WORK/tmpd" VERBOSE=$_vb)"
+ assert_eq "retry: nothing armed (outside China${_vb:+, verbose}): no output copy left behind" "0" "$(ls -A "$_WORK/tmpd" | wc -l | tr -d ' ')"
+done
+_abort=$(env -i PATH="$_WORK/uvbin:/usr/bin:/bin" TMPDIR="$_WORK/no-such-dir" FAIL=x sh -c "
+step() { :; }; tauri_stream_log() { :; }; tauri_clear_install_error() { :; }; _redact_install_output() { cat; }; _is_verbose() { false; }
+_uv_download_markers() { cat; }
+. '$_WORK/retry.sh'
+set -e
+run_install_cmd deps uv pip install foo 3>&1
+echo AFTER" 2>/dev/null || true)
+assert_eq "retry: nothing armed: set -e still stops the installer where it failed, as before" "" "$_abort"
+assert_eq "retry: a torch transport failure reruns on the mirror's index, and later torch steps follow" "RUN pip install torch --default-index https://download.pytorch.org/whl/cu128 |STEP download.pytorch.org failed; retrying through $M/pytorch/whl|RUN pip install torch --default-index $M/pytorch/whl/cu128 |RC 0 INDEX= TORCH=$M/pytorch/whl/cu128 SPARE=pypi|UV_DEFAULT_INDEX=$M/pypi/web/simple|RUN pip install torchvision --default-index $M/pytorch/whl/cu128 |RC 0" "$(_pipe _retry 'run_install_cmd torch uv pip install torch --default-index https://download.pytorch.org/whl/cu128' FAIL="$_fail_uv_stall" THEN='run_install_cmd tv uv pip install torchvision --default-index https://download.pytorch.org/whl/cu128')"
+assert_eq "retry: the retrying runner gives the default every attempt before the mirror (a stall naming no URL is its PyPI)" "RUN pip install foo |RUN pip install foo |STEP PyPI failed; retrying through $M/pypi/web/simple|RUN pip install foo $M/pypi/web/simple|RC 0" "$(_pipe _retry 'run_install_cmd_retry deps uv pip install foo' FAIL="$_fail_uv_stall" UNSLOTH_INSTALL_RETRIES=2 | sed 's/ INDEX=.*//')"
+assert_eq "retry: a pinned command is not moved to the PyPI mirror" "RUN pip install x --index-url https://download.pytorch.org/whl/cu128 |RC 7" "$(_pipe _retry 'run_install_cmd x uv pip install x --index-url https://download.pytorch.org/whl/cu128' FAIL="$_fail_uv_timeout" | sed 's/ INDEX=.*//')"
+assert_eq "retry: a torch failure without a torch URL to move is not retried" "RUN pip install torch --torch-backend=auto |RC 7" "$(_pipe _retry 'run_install_cmd tb uv pip install torch --torch-backend=auto' FAIL="$_fail_uv_503" | sed 's/ INDEX=.*//')"
+assert_eq "retry: ... nor a stall under a source the command picks itself" "RUN pip install torch --torch-backend=auto |RC 7" "$(_pipe _retry 'run_install_cmd tb uv pip install torch --torch-backend=auto' FAIL="$_fail_uv_stall" | sed 's/ INDEX=.*//')"
+_lag() { _lagcmd=$1; shift; _pipe _retry "run_install_cmd u uv pip install $_lagcmd" FAIL="$_fail_uv_lag" LAGGING=1 UV_DEFAULT_INDEX="$M/pypi/web/simple" _UNSLOTH_MIRROR_SPARE="torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl unsynced|UV_DEFAULT_INDEX=https://pypi.org/simple|UV_INDEX=$M/pypi/web/simple" "$@" | sed 's/ TORCH=[^ ]*//'; }
+assert_eq "retry: a release the mirror lacks reruns once with pypi.org behind it, which later steps keep" "RUN pip install unsloth $M/pypi/web/simple|STEP The PyPI mirror failed; retrying through https://pypi.org/simple|RUN pip install unsloth https://pypi.org/simple +$M/pypi/web/simple|RC 0 INDEX=https://pypi.org/simple SPARE=torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl" "$(_lag unsloth)"
+assert_eq "retry: ... also under --torch-backend, whose other packages resolve on the index" "RC 0 INDEX=https://pypi.org/simple" "$(_lag 'unsloth --torch-backend=auto' | grep -o 'RC [0-9] INDEX=[^ ]*')"
+assert_eq "retry: a command pinning its index is not given pypi.org" "RUN pip install unsloth --default-index https://corp.example/simple |RC 8 INDEX=$M/pypi/web/simple SPARE=torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl unsynced|UV_DEFAULT_INDEX=https://pypi.org/simple|UV_INDEX=$M/pypi/web/simple" "$(_lag 'unsloth --default-index https://corp.example/simple' MIRROR_FAILS=1)"
+assert_eq "retry: ... nor one ending in --no-index" "RC 7" "$(_lag 'unsloth --no-index' | grep -o 'RC [0-9]')"
+
+_npm() {
+ printf '%s\n' "$1" > "$_WORK/npm.log"; shift
+ env -i PATH=/usr/bin:/bin _UNSLOTH_MIRROR_SPARE="npm|UNSLOTH_NPM_REGISTRY=$NPMM" "$@" bash -c "
+step() { echo \"STEP \$2\"; }; run_quiet_no_exit() { shift; echo \"RUN \$*\"; [ -z \"\${MIRROR_FAILS:-}\" ]; }
+. '$_WORK/block.sh'
+$(sed -n '/^_npm_mirror_retry() {/,/^}/p' "$SETUP_SH")
+_CAPTURE_LOG='$_WORK/npm.log'; _NPM_REGISTRY_ARGS=()
+_npm_mirror_retry 'npm install'; echo \"RC \$? REG=\${UNSLOTH_NPM_REGISTRY:-} ARGS=\${_NPM_REGISTRY_ARGS[*]}\"" | paste -sd'|' -
+}
+NPMM=https://registry.npmmirror.com
+assert_eq "npm: a registry transport failure reruns once on npmmirror, which later installs keep" "STEP registry.npmjs.org failed; retrying through $NPMM|RUN npm install --no-fund --no-audit --loglevel=error --registry $NPMM|RC 0 REG=$NPMM ARGS=--registry $NPMM" "$(_npm "$_fail_npm")"
+assert_eq "npm: ... not when the rerun fails too" "STEP registry.npmjs.org failed; retrying through $NPMM|RUN npm install --no-fund --no-audit --loglevel=error --registry $NPMM|RC 1 REG= ARGS=" "$(_npm "$_fail_npm" MIRROR_FAILS=1)"
+assert_eq "npm: a dependency conflict is not retried" "RC 1 REG= ARGS=" "$(_npm 'npm error code ERESOLVE: While resolving: [email protected] from https://registry.npmjs.org/vite')"
+
+summary
diff --git a/tests/sh/test_setup_reuse_installed_uv.sh b/tests/sh/test_setup_reuse_installed_uv.sh
index d8263d14106..f7363eef1b0 100755
--- a/tests/sh/test_setup_reuse_installed_uv.sh
+++ b/tests/sh/test_setup_reuse_installed_uv.sh
@@ -281,7 +281,7 @@ done
# Source contract: the reuse sits between the PATH probe and the download, in both shells.
_probe_at=$(grep -n '^if command -v uv &>/dev/null; then$' "$SETUP_SH" | head -1 | cut -d: -f1)
_reuse_at=$(grep -n '^elif _setup_find_installed_uv; then$' "$SETUP_SH" | head -1 | cut -d: -f1)
-_install_at=$(grep -n 'if _setup_install_uv_pinned; then' "$SETUP_SH" | head -1 | cut -d: -f1)
+_install_at=$(grep -n 'if _setup_install_uv_pinned[ ;]' "$SETUP_SH" | head -1 | cut -d: -f1)
if [ -n "$_probe_at" ] && [ -n "$_reuse_at" ] && [ -n "$_install_at" ] \
&& [ "$_probe_at" -lt "$_reuse_at" ] && [ "$_reuse_at" -lt "$_install_at" ]; then
ok "setup.sh reuses an installed uv before it downloads one"
diff --git a/tests/sh/test_tauri_retry_failure_context.sh b/tests/sh/test_tauri_retry_failure_context.sh
index c687a92274a..ca88542c1d0 100755
--- a/tests/sh/test_tauri_retry_failure_context.sh
+++ b/tests/sh/test_tauri_retry_failure_context.sh
@@ -15,6 +15,7 @@ _FUNC_FILE=$(mktemp)
{
sed -n '/^run_install_cmd()/,/^}/p' "$INSTALL_SH"
sed -n '/^run_install_cmd_retry()/,/^}/p' "$INSTALL_SH"
+ for _f in _ric_tee _ric_run _run_install_cmd_once _mirror_retry_install _mirror_failed_host; do sed -n "/^$_f()/,/^}/p" "$INSTALL_SH"; done
sed -n '/^tauri_log()/,/^}/p' "$INSTALL_SH"
sed -n '/^tauri_stream_log()/,/^}/p' "$INSTALL_SH"
sed -n '/^tauri_clear_install_error()/,/^}/p' "$INSTALL_SH"
diff --git a/tests/sh/test_uv_cache_colocation.sh b/tests/sh/test_uv_cache_colocation.sh
index 18428bf7667..065727e59d9 100755
--- a/tests/sh/test_uv_cache_colocation.sh
+++ b/tests/sh/test_uv_cache_colocation.sh
@@ -114,10 +114,10 @@ assert_eq "precedes uv bootstrap" "yes" \
# Match the call that creates the venv, not the label it carries: the literal
# 'run_install_cmd "create venv" uv venv' stopped existing when #8479 moved venv
# creation behind _run_uv_venv, and a label this file cannot find reads as "the cache
-# is set too late" rather than "the grep is stale". Comment lines are dropped so the
-# prose above the helper does not answer first.
+# is set too late" rather than "the grep is stale". Comment lines and case globs are
+# dropped so the prose above the helper, or a pattern matching the call, does not answer first.
_venv_line=$(grep -nE '(^|[^[:alnum:]_"`])uv venv([[:space:]]|$)' "$INSTALL_SH" \
- | grep -vE '^[0-9]+:[[:space:]]*#' | head -1 | cut -d: -f1)
+ | grep -vE '^[0-9]+:[[:space:]]*#' | grep -v '\*" uv venv "\*' | head -1 | cut -d: -f1)
assert_eq "found the venv creation call" "yes" "$([ -n "$_venv_line" ] && echo yes || echo no)"
assert_eq "precedes venv creation" "yes" \
"$([ -n "$_set_line" ] && [ -n "$_venv_line" ] && [ "$_set_line" -lt "$_venv_line" ] && echo yes || echo no)"
diff --git a/tests/sh/test_uv_pinned_release.sh b/tests/sh/test_uv_pinned_release.sh
index c4ddad3b639..181d8099c28 100755
--- a/tests/sh/test_uv_pinned_release.sh
+++ b/tests/sh/test_uv_pinned_release.sh
@@ -29,7 +29,7 @@ else
fi
# The pinned attempt must precede the fallback, or the fallback is what actually runs.
-_pinned_at=$(grep -n 'if _uv_install_pinned; then' "$INSTALL_SH" | head -1 | cut -d: -f1)
+_pinned_at=$(grep -n 'if _uv_install_pinned[ ;]' "$INSTALL_SH" | head -1 | cut -d: -f1)
_fallback_at=$(grep -n 'download "https://astral.sh/uv/install.sh"' "$INSTALL_SH" | head -1 | cut -d: -f1)
if [ -n "$_pinned_at" ] && [ -n "$_fallback_at" ] && [ "$_pinned_at" -lt "$_fallback_at" ]; then
ok "the pinned path is tried before the astral fallback"
@@ -395,6 +395,10 @@ else
ok "a uv that cannot execute declines to the fallback"
fi
+_unfetched() ( set +e; tauri_log() { :; }; . "$WORK/uvfns.sh"; HOME="$WORK/home_noexec"; unset UNSLOTH_UV_WHEEL_MIRROR; eval "$1"; _uv_install_pinned > /dev/null 2>&1; echo "$_UIP_UNFETCHED" )
+_got=$(echo $(_unfetched '_uv_pinned_asset() { return 1; }'; _unfetched "_uv_pinned_asset() { echo 'uv-bad.tar.gz 00'; }; download() { return 6; }"; _unfetched "_uv_pinned_asset() { echo 'uv-bad.tar.gz 00'; }; download() { cp -f '$WORK/uv-bad.tar.gz' \"\$2\"; }"))
+if [ "$_got" = "false true false" ]; then ok "_UIP_UNFETCHED: only a download no source served is left for the mirror retry"; else bad "only a download no source served is left for the mirror (got: $_got)"; fi
+
# And it must not have destroyed the uv the host was already using. The rename publishes over the
# destination, so validating the new binary only after that point would leave a host whose loader
# is missing with neither its old working uv nor a usable new one, while _uv_present_before still
@@ -1109,6 +1113,39 @@ else
bad "a destination holding regex metacharacters is written once, not once per run ($_n)"
fi
+mkdir -p "$WORK/whl/uv-0.12.1.data/scripts" && cp "$WORK/src/uv-fake-triple/uv" "$WORK/src/uv-fake-triple/uvx" "$WORK/whl/uv-0.12.1.data/scripts/"
+(cd "$WORK/whl" && python3 -m zipfile -c "$WORK/uv-fake.whl" uv-0.12.1.data)
+WHEEL_SHA=$( (sha256sum "$WORK/uv-fake.whl" 2>/dev/null || shasum -a 256 "$WORK/uv-fake.whl") | awk '{print $1}')
+_sa=$(grep -n '^_SETUP_UV_PINNED_VERSION=' "$SETUP_SH" | cut -d: -f1)
+_sb=$(awk -v s="$(grep -n '^_setup_install_uv_pinned() {' "$SETUP_SH" | cut -d: -f1)" 'NR > s && /^}$/ { print NR; exit }' "$SETUP_SH")
+sed -n "${_sa},${_sb}p" "$SETUP_SH" > "$WORK/uvfns_setup.sh"
+cp "$WORK/uvfns.sh" "$WORK/uvfns_install.sh"
+for _run in install:good install:bad setup:good setup:bad; do
+ _wh="$WORK/wheel_${_run%:*}_${_run#*:}"; mkdir -p "$_wh"; _want=$WHEEL_SHA; [ "${_run#*:}" = good ] || _want=$(printf '0%.0s' $(seq 64))
+ (
+ set +e; tauri_log() { :; }
+ # shellcheck disable=SC1090
+ . "$WORK/uvfns_${_run%:*}.sh"
+ # GNU tar cannot read a zip; stand in for it where tar is bsdtar, which can.
+ tar() { case "$*" in *.whl*) return 1 ;; esac; command tar "$@"; }
+ _uv_pinned_asset() { echo "uv-fake.tar.gz $FIXTURE_SHA"; }; _setup_uv_pinned_asset() { _uv_pinned_asset; }
+ _uv_pinned_wheel() { echo "packages/ab/cd/uv-fake.whl $_want"; }; _setup_uv_pinned_wheel() { _uv_pinned_wheel; }
+ download() { echo "$1" >> "$_wh.get"; cp -f "$WORK/uv-fake.whl" "$2"; }; _setup_http_get() { download "$1" /dev/stdout; }; _setup_persist_uv_path() { :; }
+ HOME="$_wh"; UNSLOTH_UV_WHEEL_MIRROR="https://mirror.example/pypi/web/"; export HOME UNSLOTH_UV_WHEEL_MIRROR
+ unset UV_INSTALL_DIR UV_UNMANAGED_INSTALL XDG_BIN_HOME XDG_DATA_HOME UV_DOWNLOAD_URL INSTALLER_DOWNLOAD_URL UV_INSTALLER_GHE_BASE_URL UV_INSTALLER_GITHUB_BASE_URL
+ if [ "${_run%:*}" = install ]; then _uv_install_pinned; else _setup_install_uv_pinned; fi
+ ) > "$_wh.out" 2>&1 || true
+ _got=no; [ -x "$_wh/.local/bin/uv" ] && [ -x "$_wh/.local/bin/uvx" ] \
+ && [ "$(cat "$_wh.get")" = "https://mirror.example/pypi/web/packages/ab/cd/uv-fake.whl" ] && _got=yes
+ _exp=no; [ "${_run#*:}" = good ] && _exp=yes
+ assert_eq "$_run: wheel-mirror install (good digest installs from the mirror only, bad installs nothing)" "$_exp" "$_got"
+done
+_wheels() { grep -oE 'uv-[0-9.]+-py3-none-[a-z0-9_.]+\.whl [0-9a-f]{64}"' "$1" | sort; }
+assert_eq "install.sh and setup.sh pin the same four wheels of the pinned uv" "4 $(_wheels "$INSTALL_SH")" \
+ "$(_wheels "$INSTALL_SH" | grep -c "^uv-$(sed -n 's/^UV_PINNED_VERSION="\(.*\)"/\1/p' "$INSTALL_SH")-") $(_wheels "$SETUP_SH")"
+_winwheels() { grep -oE 'uv-[0-9.]+-py3-none-win[a-z0-9_]*\.whl"; WheelSha256 = "[0-9A-F]{64}' "$1" | sort; }
+assert_eq "install.ps1 and setup.ps1 pin the same three wheels of the pinned uv" "3 $(_winwheels "$SCRIPT_DIR/../../install.ps1")" "$(_winwheels "$SCRIPT_DIR/../../install.ps1" | grep -c "^uv-$(sed -n 's/^UV_PINNED_VERSION="\(.*\)"/\1/p' "$INSTALL_SH")-") $(_winwheels "$SETUP_PS1")"
+
echo
echo "passed: $PASS failed: $FAIL"
[ "$FAIL" -eq 0 ]
diff --git a/tests/studio/install/test_denied_llama_cpp_preflight.py b/tests/studio/install/test_denied_llama_cpp_preflight.py
index 9d7f278cb27..9de0d81b01e 100644
--- a/tests/studio/install/test_denied_llama_cpp_preflight.py
+++ b/tests/studio/install/test_denied_llama_cpp_preflight.py
@@ -37,6 +37,17 @@ SHARED_FUNCTIONS = (
"Get-MasterRootOverride",
"Get-ManagedLlamaCppDir",
"Invoke-ManagedLlamaCppPreflight",
+ "Test-MirrorConfigured",
+ "Start-MirrorProbe",
+ "Wait-MirrorProbe",
+ "Get-MirrorDnsServers",
+ "Test-MirrorInChina",
+ "Invoke-MirrorFallback",
+ "Get-MirrorName",
+ "Set-MirrorEnv",
+ "Pop-MirrorSpare",
+ "Use-MirrorSpare",
+ "Get-MirrorFailedHost",
)
diff --git a/tests/studio/install/test_dependency_pass_skips.py b/tests/studio/install/test_dependency_pass_skips.py
index 830df6a7d18..6888848fd71 100644
--- a/tests/studio/install/test_dependency_pass_skips.py
+++ b/tests/studio/install/test_dependency_pass_skips.py
@@ -1083,7 +1083,7 @@ def test_every_install_entry_point_is_counted() -> None:
source = STACK_PATH.read_text(encoding = "utf-8")
tree = ast.parse(source)
counted = {
- "pip_install",
+ "_pip_install_once",
"pip_install_try",
"_uninstall_distribution",
"_purge_recordless_distributions",
diff --git a/tests/studio/install/test_install_node_prebuilt_logic.py b/tests/studio/install/test_install_node_prebuilt_logic.py
index 9562355dab6..191459ffdcf 100644
--- a/tests/studio/install/test_install_node_prebuilt_logic.py
+++ b/tests/studio/install/test_install_node_prebuilt_logic.py
@@ -28,6 +28,11 @@ HostInfo = M.HostInfo
PrebuiltFallback = M.PrebuiltFallback
[email protected](autouse = True)
+def _no_ambient_node_mirror(monkeypatch):
+ monkeypatch.delenv(M.NODE_MIRROR_ENV, raising = False)
+
+
def _host(node_os: str, node_arch: str) -> HostInfo:
ext = ".zip" if node_os == "win" else ".tar.gz"
return HostInfo(
@@ -89,6 +94,27 @@ def test_shasums_url():
assert M.node_shasums_url("24.17.0") == "https://nodejs.org/dist/v24.17.0/SHASUMS256.txt"
[email protected]("value", ["https://mirror.example/node/", " https://mirror.example/node "])
+def test_node_mirror_env_rebases_every_url(monkeypatch, value):
+ monkeypatch.setenv(M.NODE_MIRROR_ENV, value)
+ assert M.node_dist_index_url() == "https://mirror.example/node/index.json"
+ assert M.node_shasums_url("24.17.0") == "https://mirror.example/node/v24.17.0/SHASUMS256.txt"
+ assert (
+ M.node_download_url("24.17.0", "node-v24.17.0-linux-x64.tar.gz")
+ == "https://mirror.example/node/v24.17.0/node-v24.17.0-linux-x64.tar.gz"
+ )
+
+
+def test_blank_node_mirror_env_keeps_nodejs_org(monkeypatch):
+ monkeypatch.setenv(M.NODE_MIRROR_ENV, " ")
+ assert M.node_dist_index_url() == "https://nodejs.org/dist/index.json"
+ assert M.node_shasums_url("24.17.0") == "https://nodejs.org/dist/v24.17.0/SHASUMS256.txt"
+ assert (
+ M.node_download_url("24.17.0", "node-v24.17.0-linux-x64.tar.gz")
+ == "https://nodejs.org/dist/v24.17.0/node-v24.17.0-linux-x64.tar.gz"
+ )
+
+
def test_binary_layout_is_host_aware():
# Windows ships node.exe + node_modules\npm at the root; Unix uses bin/ + lib/.
win = _host("win", "x64")
diff --git a/tests/studio/install/test_torch_probe_memoization.py b/tests/studio/install/test_torch_probe_memoization.py
index b8bf66a6a35..07280eb45fe 100644
--- a/tests/studio/install/test_torch_probe_memoization.py
+++ b/tests/studio/install/test_torch_probe_memoization.py
@@ -295,7 +295,7 @@ class TestMemoization:
if called & {"_build_pip_cmd", "_build_uv_cmd"}:
installers[node.name] = called
assert SCRATCH_PREFETCHERS <= set(installers), "an exempted prefetcher no longer builds"
- assert set(installers) - SCRATCH_PREFETCHERS == {"pip_install", "pip_install_try"}, (
+ assert set(installers) - SCRATCH_PREFETCHERS == {"_pip_install_once", "pip_install_try"}, (
f"a new installer entry point appeared: {sorted(installers)}. It has to drop "
"the torch classification too, or it will answer for the build it replaced"
)
diff --git a/tests/studio/test_mirror_fallback.ps1 b/tests/studio/test_mirror_fallback.ps1
new file mode 100644
index 00000000000..dcf37361e7d
--- /dev/null
+++ b/tests/studio/test_mirror_fallback.ps1
@@ -0,0 +1,263 @@
+#!/usr/bin/env pwsh
+
+$ErrorActionPreference = "Stop"
+Set-StrictMode -Version Latest # setup.ps1 runs under its caller's strict mode.
+$installPath = (Resolve-Path ([System.IO.Path]::Combine($PSScriptRoot, "..", "..", "install.ps1"))).Path
+$tokens = $null; $errors = $null
+$ast = [System.Management.Automation.Language.Parser]::ParseFile($installPath, [ref]$tokens, [ref]$errors)
+if ($errors) { throw "install.ps1 has parse errors" }
+$setupAst = [System.Management.Automation.Language.Parser]::ParseFile((Resolve-Path ([System.IO.Path]::Combine($PSScriptRoot, "..", "..", "studio", "setup.ps1"))).Path, [ref]$tokens, [ref]$errors)
+$load = 'Test-UvEnvFlag', 'Test-MirrorConfigured', 'Start-MirrorProbe', 'Wait-MirrorProbe', 'Invoke-MirrorFallback', 'Get-MirrorName', 'Set-MirrorEnv', 'Pop-MirrorSpare', 'Use-MirrorSpare', 'Get-MirrorFailedHost',
+ 'Install-UvFromRelease', 'Redact-InstallOutput', 'Invoke-InstallCommand', 'Invoke-InstallMirrorRetry', 'Invoke-InstallCommandRetry'
+foreach ($pair in @($load | ForEach-Object { , @($ast, $_) }) + (@('Install-UvFromPinnedRelease', 'Get-UvInstallDir', 'Invoke-SetupCommand', 'Invoke-NpmMirrorRetry') | ForEach-Object { , @($setupAst, $_) })) {
+ $fn = $pair[0].FindAll({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $pair[1] }, $true)
+ Invoke-Expression $fn[0].Extent.Text
+}
+
+$failures = 0
+function Check($name, $cond) {
+ if ($cond) { Write-Host " PASS $name" } else { Write-Host " FAIL $name" -ForegroundColor Red; $script:failures++ }
+}
+
+# 206 only for exactly the 1 KiB range on a fresh connection, 403 without a User-Agent (as on CERNET); /slow sends 512 bytes and stalls, /whole ignores Range, /late answers after 2 s.
+$server = @'
+import http.server, time
+class H(http.server.BaseHTTPRequestHandler):
+ def do_GET(self):
+ if self.path == "/redirect":
+ self.send_response(302); self.send_header("Location", "/ok"); self.send_header("Content-Length", "0"); self.end_headers(); return
+ if self.path == "/late": time.sleep(2)
+ ranged = self.headers.get("Range") == "bytes=0-1023" and self.headers.get("Connection", "").lower() == "close" and self.path != "/whole"
+ code = 404 if self.path == "/404" else 403 if not self.headers.get("User-Agent") else 206 if ranged else 200; size = 1024 if ranged else 1 << 20; first = 512 if self.path == "/slow" else 1
+ self.send_response(code); self.send_header("Content-Length", str(size)); self.end_headers()
+ self.wfile.write(b"x" * first); self.wfile.flush()
+ time.sleep(5 if self.path == "/slow" else 0); self.wfile.write(b"x" * (size - first))
+ def log_message(self, *a): pass
+http.server.ThreadingHTTPServer.request_queue_size = 64; s = http.server.ThreadingHTTPServer(("127.0.0.1", 0), H); print(s.server_port, flush=True); s.serve_forever()
+'@
+# Windows' python3 is often the Microsoft Store stub, which prints an install hint instead of running.
+$python = if ($env:OS -ne 'Windows_NT' -and (Get-Command python3 -ErrorAction SilentlyContinue)) { 'python3' } else { 'python' }
+$psi = New-Object System.Diagnostics.ProcessStartInfo $python, "-c `"exec(__import__('sys').stdin.read())`""
+$psi.RedirectStandardInput = $true; $psi.RedirectStandardOutput = $true; $psi.UseShellExecute = $false
+$proc = [System.Diagnostics.Process]::Start($psi)
+$proc.StandardInput.Write($server); $proc.StandardInput.Close()
+$base = "http://127.0.0.1:$($proc.StandardOutput.ReadLine())"
+try {
+ [System.Net.ServicePointManager]::DefaultConnectionLimit = 16 # as Invoke-MirrorFallback does: the batch below opens 6 connections to one host.
+ $late = Start-MirrorProbe -Urls @("$base/late") -Seconds 1 -LastByte 1023; Start-Sleep -Seconds 3; Check "probe: an answer after the deadline stays late, however late it is collected" ((Wait-MirrorProbe $late)["$base/late"][0] -eq 0)
+ $clock = [System.Diagnostics.Stopwatch]::StartNew()
+ $r = Wait-MirrorProbe ($state = Start-MirrorProbe -Urls @("$base/ok", "$base/slow", "$base/whole", "$base/late", "$base/404", "http://127.0.0.1:9/") -Seconds 1.5 -LastByte 1023)
+ Check "probe: gives up at its deadline, cancelling what has not answered" ($clock.Elapsed.TotalSeconds -lt 3 -and $r["$base/late"][0] -eq 0 -and [System.Threading.Tasks.Task]::WaitAny(@($state.Heads["$base/late"]), 200) -eq 0)
+ Check "probe: a ranged 1 KiB answer is 206 with its speed" ($r["$base/ok"][0] -eq 206 -and $r["$base/ok"][1] -gt 1024)
+ Check "probe: redirects are followed" ((Wait-MirrorProbe (Start-MirrorProbe -Urls @("$base/redirect") -Seconds 1.5 -LastByte 1023))["$base/redirect"][0] -eq 206) # alone: pwsh 7 gets resets from this server on side-by-side redirects (real mirrors measured clean)
+ Check "probe: a stalled body reports the speed so far" ($r["$base/slow"][0] -eq 206 -and $r["$base/slow"][1] -gt 200 -and $r["$base/slow"][1] -lt 600)
+ Check "probe: an answer that ignores Range is capped, not buffered whole" ($r["$base/whole"][0] -eq 200 -and $state.Buffers["$base/whole"].Position -in 1024..(1024 + 65536))
+ Check "probe: an HTTP error keeps its code" ($r["$base/404"][0] -eq 404 -and $r["$base/404"][1] -eq 0)
+ Check "probe: a refused connection is code 0" ($r["http://127.0.0.1:9/"][0] -eq 0)
+} finally { $proc.Kill() }
+
+$names = '_UNSLOTH_MIRROR_PROBED', 'UNSLOTH_MIRROR_FALLBACK', 'UV_INDEX', 'UV_DEFAULT_INDEX', 'UV_INDEX_URL', 'UV_INDEX_STRATEGY', 'PIP_INDEX_URL',
+ 'PIP_EXTRA_INDEX_URL', 'UNSLOTH_PYTORCH_MIRROR', 'UNSLOTH_NODE_MIRROR', 'UNSLOTH_NPM_REGISTRY', 'NPM_CONFIG_REGISTRY', 'UV_OFFLINE', 'UV_CONFIG_FILE', 'PIP_CONFIG_FILE',
+ 'UNSLOTH_UV_WHEEL_MIRROR', 'UV_INSTALLER_GITHUB_BASE_URL', 'UV_INSTALL_DIR', 'UV_NO_MODIFY_PATH', '_UNSLOTH_MIRROR_SPARE', 'UNSLOTH_INSTALL_RETRIES', 'UNSLOTH_INSTALL_RETRY_DELAY'
+$saved = @{}; foreach ($n in $names + 'APPDATA', 'USERPROFILE', 'ProgramData', 'PATH') { $saved[$n] = [Environment]::GetEnvironmentVariable($n) }
+$home_ = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-mirror-" + [guid]::NewGuid())
+$pipIni = Join-Path $home_ 'AppData/pip/pip.ini'
+New-Item -ItemType Directory -Force -Path (Split-Path $pipIni) | Out-Null
+$env:APPDATA = Join-Path $home_ 'AppData'; $env:USERPROFILE = $home_; $env:ProgramData = Join-Path $home_ 'ProgramData'
+function step($l, $v, $c) { $script:lines += "STEP $v" }
+function substep($m) { $script:lines += "SUBSTEP $m" }
+$M = 'https://tuna.mirrors.cernet.edu.cn'
+function Get-ProbeClass([string]$u) {
+ switch -Wildcard ($u) {
+ "$M/pypi/web/simple/*" { return 'cernetpypiindex' } "$M/pytorch/whl/cpu/torch/" { return 'cernettorchindex' } "$M/pytorch/*" { return 'cernettorch' }
+ 'https://registry.npmmirror.com/-/binary/node/*' { return 'npmmirrornode' } "$M/*" { return 'cernet' } 'https://registry.npmmirror.com/*' { return 'npmmirror' }
+ 'https://pypi.org/*' { return 'pypiindex' } 'https://download.pytorch.org/*' { return 'torchindex' } 'https://files.pythonhosted.org/*' { return 'pypi' }
+ 'https://download-r2.pytorch.org/*' { return 'torch' } 'https://nodejs.org/*' { return 'node' } 'https://registry.npmjs.org/*' { return 'npm' } default { return 'astral' }
+ }
+}
+function Start-MirrorProbe([string[]]$Urls, [double]$Seconds, [long]$LastByte) {
+ if ($Seconds -eq 1.5) { if ($script:timing -or $Urls.Count -gt 1) { $script:overlap = $true }; $script:timing = $true }
+ $script:probed += $Urls; $script:protocols += "$([System.Net.ServicePointManager]::SecurityProtocol)/$([System.Net.ServicePointManager]::DefaultConnectionLimit)"; foreach ($u in $Urls) { if (($u -match '/(simple/uv|whl/cpu/torch)/$') -ne ($LastByte -eq 1023) -or $Seconds -notin $(if ($LastByte -eq 1023) { 4 } else { 1.5, 4 })) { $script:badRange = $true } }
+ return @{ Urls = $Urls; Seconds = $Seconds }
+}
+function Wait-MirrorProbe($Probe) {
+ if ($Probe.Seconds -eq 1.5) { $script:timing = $false }
+ $out = @{}
+ foreach ($u in $Probe.Urls) {
+ $c = Get-ProbeClass $u; $r = "$($script:mock[$c])"; if (-not $r) { $r = 'fast' }
+ $r = if ($script:seen -contains $c) { $r.Split('|')[-1] } else { $script:seen += $c; $r.Split('|')[0] }
+ $out[$u] = switch ($r) { 'fast' { @(206, [long]4000000) } 'slow' { @(206, [long]300000) } 'blocked' { @(0, [long]0) } default { @([int]$r.Split(' ')[0], [long]$r.Split(' ')[1]) } }
+ }
+ return $out
+}
+$real = $ast.FindAll({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Test-MirrorInChina' }, $true)[0].Extent.Text
+Invoke-Expression $real
+function Get-TimeZone { [pscustomobject]@{ Id = $script:tz } }
+$realDns = $ast.FindAll({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Get-MirrorDnsServers' }, $true)[0].Extent.Text
+Invoke-Expression $realDns
+Check "location: the .NET resolver list reads without throwing" ($null -eq $(try { @(Get-MirrorDnsServers) | Out-Null; $null } catch { $_ }))
+function Get-MirrorDnsServers { $script:dns }
+foreach ($case in @(@('China Standard Time', $null, $true), @('Asia/Shanghai', $null, $true), @('Taipei Standard Time', $null, $false), @('UTC', $null, $false),
+ @('UTC', @('8.8.8.8', '100.100.2.136'), $true), @('UTC', @('223.5.5.5'), $true), @('UTC', @('114.114.115.119'), $true), @('UTC', @('182.254.116.116'), $true), @('UTC', @('100.100.100.100', '1.1.1.1'), $false))) {
+ $script:tz = $case[0]; $script:dns = $case[1]
+ Check "location: time zone $($case[0]), resolvers $($case[1] -join ',') -> China=$($case[2])" ((Test-MirrorInChina) -eq $case[2])
+}
+Remove-Item Function:Get-TimeZone, Function:Get-MirrorDnsServers
+function Test-MirrorInChina { $script:inChina }
+
+function Run($mock, $envs = @{}, [switch]$SpareOnly, [switch]$Abroad) {
+ foreach ($n in $names) { Remove-Item "Env:$n" -ErrorAction SilentlyContinue }
+ foreach ($k in $envs.Keys) { Set-Item "Env:$k" $envs[$k] }
+ $script:inChina = -not $Abroad
+ $script:mock = $mock; $script:seen = @(); $script:lines = @(); $script:probed = @(); $script:protocols = @(); $script:badRange = $false; $script:timing = $false; $script:overlap = $false
+ Invoke-MirrorFallback -SpareOnly:$SpareOnly
+}
+$pypiMirror = "$M/pypi/web/simple"
+try {
+ Run @{}; Check "fast hosts export and print nothing" (-not $env:UV_DEFAULT_INDEX -and -not $env:UNSLOTH_NPM_REGISTRY -and $script:lines.Count -eq 0)
+ Run @{ pypi = 'blocked'; torch = 'blocked'; npm = 'blocked' } -Abroad
+ Check "outside China: nothing probed, switched, armed or exported" ($script:probed.Count -eq 0 -and $script:lines.Count -eq 0 -and -not $env:UV_DEFAULT_INDEX -and -not $env:_UNSLOTH_MIRROR_SPARE -and -not $env:_UNSLOTH_MIRROR_PROBED)
+ Run @{} -Abroad -SpareOnly; Check "outside China: no retry is armed either" (-not $env:_UNSLOTH_MIRROR_SPARE)
+ foreach ($on in '1', 'true', ' Yes ', 'on') { Run @{ pypi = 'blocked' } @{ UV_OFFLINE = $on }; Check "UV_OFFLINE=$($on): nothing probed, retries still armed" ($script:probed.Count -eq 0 -and -not $env:UV_DEFAULT_INDEX -and $env:_UNSLOTH_MIRROR_SPARE -like 'pypi|*') }
+ Run @{ pypi = 'blocked' } @{ UV_OFFLINE = '0' }; Check "UV_OFFLINE=0 still probes" ($env:UV_DEFAULT_INDEX -eq "$M/pypi/web/simple")
+ Run @{} @{ _UNSLOTH_MIRROR_SPARE = 'pypi|UV_DEFAULT_INDEX=https://x' } -Abroad -SpareOnly; Check "outside China: a retry state inherited from a parent is dropped" (-not $env:_UNSLOTH_MIRROR_SPARE)
+ Run @{} @{ _UNSLOTH_MIRROR_SPARE = 'pypi|UV_DEFAULT_INDEX=https://x'; UNSLOTH_MIRROR_FALLBACK = '0' } -SpareOnly; Check "UNSLOTH_MIRROR_FALLBACK=0: a retry state inherited from a parent is dropped" (-not $env:_UNSLOTH_MIRROR_SPARE)
+ Run @{ pypi = 'blocked' } @{ UNSLOTH_MIRROR_FALLBACK = '1' } -Abroad; Check "outside China, UNSLOTH_MIRROR_FALLBACK=1 opts in" ($env:UV_DEFAULT_INDEX -eq "$M/pypi/web/simple")
+ Run @{ pypi = 'blocked' } @{ UNSLOTH_MIRROR_FALLBACK = '0' }; Check "in China, UNSLOTH_MIRROR_FALLBACK=0 still opts out" ($script:probed.Count -eq 0 -and -not $env:UV_DEFAULT_INDEX)
+ Check "fast hosts never touch a mirror" (-not ($script:probed -match 'cernet|npmmirror'))
+ Run @{ pypi = 'slow'; torch = 'slow'; node = 'slow'; npm = 'slow'; astral = 'slow' }; Check "defaults are timed one at a time, on 1 MiB within 1.5 s or 4 s; indexes on 1 KiB within 4 s" (-not $script:overlap -and -not $script:badRange)
+ Run @{ torch = 'blocked' }
+ Check "hosts left on their defaults keep their mirror, blocked-mode, as a spare" ($env:_UNSLOTH_MIRROR_SPARE -eq "pypi|UV_DEFAULT_INDEX=$pypiMirror|PIP_INDEX_URL=$pypiMirror node|UNSLOTH_NODE_MIRROR=https://registry.npmmirror.com/-/binary/node npm|UNSLOTH_NPM_REGISTRY=https://registry.npmmirror.com uvbin|UNSLOTH_UV_WHEEL_MIRROR=$M/pypi/web")
+ Run @{ pypi = 'blocked' }
+ Check "blocked pypi: mirror is the only uv/pip index" ($env:UV_DEFAULT_INDEX -eq $pypiMirror -and $env:PIP_INDEX_URL -eq $pypiMirror -and -not $env:UV_INDEX -and -not $env:UV_INDEX_STRATEGY -and -not $env:PIP_EXTRA_INDEX_URL)
+ Check "blocked pypi: says so with both speeds and names the opt-out" ($script:lines -contains "STEP PyPI is blocked (0 KB/s, mirror 3906 KB/s); using $pypiMirror" -and $script:lines[-1] -like 'SUBSTEP Set UNSLOTH_MIRROR_FALLBACK=0*')
+ Run @{ pypi = 'slow' }
+ Check "slow pypi: the mirror is the only uv/pip index" ($env:UV_DEFAULT_INDEX -eq $pypiMirror -and $env:PIP_INDEX_URL -eq $pypiMirror -and -not $env:UV_INDEX -and -not $env:UV_INDEX_STRATEGY -and -not $env:PIP_EXTRA_INDEX_URL)
+ Check "slow pypi: pypi.org, which still answers, is spared behind the mirror for what it has not synced" ($env:_UNSLOTH_MIRROR_SPARE -like "* unsynced|UV_DEFAULT_INDEX=https://pypi.org/simple|UV_INDEX=$pypiMirror|UV_INDEX_STRATEGY=unsafe-first-match|PIP_EXTRA_INDEX_URL=https://pypi.org/simple|PIP_INDEX_URL=$pypiMirror")
+ Check "slow pypi: the default is timed again in the race" (@($script:probed -like 'https://files.pythonhosted.org/*').Count -eq 2)
+ Run @{ pypi = 'slow' } @{ UV_INDEX_STRATEGY = 'first-index' }; Check "a user index strategy is kept" ($env:_UNSLOTH_MIRROR_SPARE -like '*|UV_INDEX_STRATEGY=first-index|*')
+ Run @{ pypi = 'slow'; cernet = '206 200000' }; Check "a mirror slower than the default is not used" (-not $env:UV_DEFAULT_INDEX -and $script:lines.Count -eq 0)
+ Run @{ pypi = 'blocked'; cernet = 'blocked' }; Check "a dead mirror changes and prints nothing" (-not $env:UV_DEFAULT_INDEX -and $script:lines.Count -eq 0)
+ Run @{ pypi = 'slow|206 2000000'; cernet = '206 3000000' }; Check "a default back above 1 MiB/s in the race is kept" (-not $env:UV_DEFAULT_INDEX -and $script:lines.Count -eq 0)
+ Run @{ pypi = '302 0' }; Check "a redirect that stalls counts as blocked" ($env:UV_DEFAULT_INDEX -eq $pypiMirror)
+ Run @{ pypi = 'slow|404 5000000'; cernet = '206 400000' }; Check "an HTTP error in the race weighs as 0 B/s" ($script:lines -contains "STEP PyPI is blocked (0 KB/s, mirror 390 KB/s); using $pypiMirror")
+ Run @{ pypi = '404 0' }; Check "a default answering an HTTP error is kept, without timing the mirror" (-not $env:UV_DEFAULT_INDEX -and -not ($script:probed -like "$M/*"))
+ Run @{ torch = 'blocked'; node = 'slow'; npm = 'blocked' }
+ Check "torch, node and npm mirrors; healthy pypi untouched" ($env:UNSLOTH_PYTORCH_MIRROR -eq "$M/pytorch/whl" -and $env:UNSLOTH_NODE_MIRROR -eq 'https://registry.npmmirror.com/-/binary/node' -and $env:UNSLOTH_NPM_REGISTRY -eq 'https://registry.npmmirror.com' -and -not $env:PIP_INDEX_URL)
+ Check "each mirror tree is timed once" (@($script:probed -match "^($([regex]::Escape($M))|https://registry\.npmmirror\.com)/.*\.(t?gz|whl)$").Count -eq 3)
+ Run @{ torch = 'blocked'; node = 'blocked'; npmmirrornode = 'blocked' }; Check "torch and node are weighed against their own mirror trees" ($env:UNSLOTH_PYTORCH_MIRROR -eq "$M/pytorch/whl" -and -not $env:UNSLOTH_NODE_MIRROR)
+ Run @{ pypiindex = 'blocked' }; Check "an unreachable pypi.org means blocked mode, with nothing spared behind the mirror" ($env:UV_DEFAULT_INDEX -eq $pypiMirror -and $env:_UNSLOTH_MIRROR_SPARE -notmatch 'unsynced')
+ Run @{ pypiindex = 'blocked|fast' }; Check "a pypi.org index that answers in the race is not blocked" (-not $env:UV_DEFAULT_INDEX -and $script:lines.Count -eq 0)
+ Run @{ npm = 'blocked' } @{ NPM_CONFIG_REGISTRY = 'https://corp.example/npm/' }; Check "NPM_CONFIG_REGISTRY: no npm switch or retry" (-not $env:UNSLOTH_NPM_REGISTRY -and $env:_UNSLOTH_MIRROR_SPARE -notmatch 'UNSLOTH_NPM_REGISTRY')
+ Run @{ torchindex = 'blocked' }; Check "an unreachable torch index switches torch" ($env:UNSLOTH_PYTORCH_MIRROR -eq "$M/pytorch/whl")
+ Run @{ pypi = 'blocked'; torch = 'blocked'; astral = 'blocked'; cernetpypiindex = 'blocked' }
+ Check "a mirror is used only while its own index answers; the uv wheel skips the index" (-not $env:PIP_INDEX_URL -and $env:UNSLOTH_PYTORCH_MIRROR -and $env:UNSLOTH_UV_WHEEL_MIRROR -eq "$M/pypi/web")
+ Run @{ torch = 'blocked'; cernettorchindex = 'blocked' }; Check "a dead CERNET torch index keeps torch" (-not $env:UNSLOTH_PYTORCH_MIRROR)
+ $limit = [System.Net.ServicePointManager]::DefaultConnectionLimit; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls; [System.Net.ServicePointManager]::DefaultConnectionLimit = 2
+ try { Run @{ pypi = 'blocked' } } finally { $protocol = "$([System.Net.ServicePointManager]::SecurityProtocol)/$([System.Net.ServicePointManager]::DefaultConnectionLimit)"; [System.Net.ServicePointManager]::SecurityProtocol = 0; [System.Net.ServicePointManager]::DefaultConnectionLimit = $limit }
+ Check "legacy TLS and 2 connections per host are raised for the probes and restored" ($protocol -eq 'Tls/2' -and -not ($script:protocols -ne 'Tls12/16'))
+ Run @{ pypi = 'blocked' } @{ UV_INDEX_URL = 'https://corp.example/simple' }; Check "user uv index env: only pip falls back" (-not $env:UV_DEFAULT_INDEX -and $env:PIP_INDEX_URL)
+ New-Item -ItemType Directory -Force -Path (Join-Path $env:APPDATA 'uv') | Out-Null
+ foreach ($toml in "[[index]]`nurl = 'https://corp.example/simple'", "index = [{ url = 'https://corp.example/simple', default = true }]", "pip.index-url = 'https://corp.example/simple'") {
+ Set-Content -Path (Join-Path $env:APPDATA 'uv/uv.toml') -Value $toml
+ Run @{ pypi = 'blocked' }; Check "uv.toml $($toml.Split("`n")[0]): only pip falls back" (-not $env:UV_DEFAULT_INDEX -and $env:PIP_INDEX_URL)
+ }
+ Remove-Item -LiteralPath (Join-Path $env:APPDATA 'uv/uv.toml')
+ $proj = Join-Path $home_ 'proj'; New-Item -ItemType Directory -Force -Path (Join-Path $proj 'src') | Out-Null
+ Set-Content -Path (Join-Path $proj 'pyproject.toml') -Value "[project]`nname = 'p'`n`n[[tool.uv.index]]`nurl = 'https://corp.example/simple'`ndefault = true"
+ Push-Location (Join-Path $proj 'src')
+ try {
+ Run @{ pypi = 'blocked' }; Check "a parent pyproject.toml [tool.uv] index: only pip falls back" (-not $env:UV_DEFAULT_INDEX -and $env:PIP_INDEX_URL)
+ Set-Content -Path (Join-Path $proj 'src/uv.toml') -Value 'native-tls = true'
+ Run @{ pypi = 'blocked' }; Check "the nearest uv.toml hides a parent pyproject.toml" ($env:UV_DEFAULT_INDEX -eq $pypiMirror)
+ } finally { Pop-Location; Remove-Item -LiteralPath $proj -Recurse -Force }
+ Set-Content -Path $pipIni -Value "[global]`nindex-url = https://corp.example/simple"
+ Run @{ pypi = 'blocked' }; Check "pip.ini index: only uv falls back" (-not $env:PIP_INDEX_URL -and $env:UV_DEFAULT_INDEX)
+ Set-Content -Path $pipIni -Value "[global]`ntimeout = 60"
+ Run @{ pypi = 'blocked' }; Check "pip.ini without an index still falls back" ($env:PIP_INDEX_URL -eq $pypiMirror)
+ $VenvDir = Join-Path $home_ 'venv'; New-Item -ItemType Directory -Force -Path $VenvDir | Out-Null
+ Set-Content -Path (Join-Path $VenvDir 'pip.ini') -Value "[global]`nindex-url = https://corp.example/simple"
+ Run @{ pypi = 'blocked' }; Check "the Studio venv's pip.ini index: only uv falls back" (-not $env:PIP_INDEX_URL -and $env:UV_DEFAULT_INDEX)
+ Remove-Variable VenvDir
+ Run @{ node = 'blocked' } -SpareOnly
+ Check "spare only: no host is probed, and every host gets its retry" ($script:probed.Count -eq 0 -and -not $env:_UNSLOTH_MIRROR_PROBED -and -not $env:UNSLOTH_NODE_MIRROR -and $env:_UNSLOTH_MIRROR_SPARE -like "pypi|UV_DEFAULT_INDEX=$pypiMirror|PIP_INDEX_URL=$pypiMirror torch|*node|UNSLOTH_NODE_MIRROR=https://registry.npmmirror.com/-/binary/node *")
+ Invoke-MirrorFallback; Check "spare only: the probe still runs later" ($env:UNSLOTH_NODE_MIRROR -eq 'https://registry.npmmirror.com/-/binary/node')
+ Run @{ torch = 'blocked' } @{ UNSLOTH_TORCH_INDEX_URL = 'https://corp.example/whl/cu128' }; Check "a pinned torch index is not overridden" (-not $env:UNSLOTH_PYTORCH_MIRROR)
+ Run @{ npm = 'blocked' } @{ UNSLOTH_NPM_REGISTRY = 'https://corp.example/npm/' }; Check "a user npm registry is kept and not probed" ($env:UNSLOTH_NPM_REGISTRY -eq 'https://corp.example/npm/' -and -not ($script:probed -like '*npm*'))
+ Run @{ pypi = 'blocked' } @{ UNSLOTH_MIRROR_FALLBACK = 'Off' }; Check "UNSLOTH_MIRROR_FALLBACK=Off probes nothing" ($script:probed.Count -eq 0)
+ Run @{ pypi = 'blocked' } @{ _UNSLOTH_MIRROR_PROBED = '1' }; Check "a parent installer's probe is not repeated" ($script:probed.Count -eq 0)
+ Run @{ astral = 'blocked' }; Check "blocked uv releases: the pinned wheel comes from the PyPI mirror" ($env:UNSLOTH_UV_WHEEL_MIRROR -eq "$M/pypi/web"); foreach ($src in 'UV_INSTALLER_GITHUB_BASE_URL', 'UNSLOTH_UV_WHEEL_MIRROR') { Run @{ astral = 'blocked' } @{ $src = 'https://corp.example/uv' }; Check "a user $src skips the uv probe" (-not ($script:probed -like '*releases.astral.sh*')) }
+ $uvDown = "error: Failed to fetch: ``https://pypi.org/simple/numpy/```n Caused by: error sending request for url (https://pypi.org/simple/numpy/)"
+ $torchDown = "error: Failed to fetch: ``https://download.pytorch.org/whl/cu128/torch/```n Caused by: operation timed out"
+ $npmDown = "npm error network request to https://registry.npmjs.org/react failed, reason: read ECONNRESET"
+ $stall = "error: Failed to download distribution`n Caused by: Failed to download: network timeout"
+ $pipDown = "WARNING: Retrying (Retry(total=0)) after connection broken by 'ReadTimeoutError(`"HTTPSConnectionPool(host='pypi.org', port=443): Read timed out.`")': /simple/numpy/"
+ $lag = " cause: Because only unsloth<=2026.9.9 is available and you require unsloth>=2026.9.10, we can conclude that your requirements are unsatisfiable."
+ $noVersion = "error: No solution found when resolving dependencies: Because nothing==9 was not found in the package registry (https://pypi.org/simple)"
+ foreach ($case in @($uvDown, '', 'pypi'), @($torchDown, 'pypi', 'torch'), @($npmDown, '', 'npm'), @($pipDown, '', 'pypi'), @('npm error code ERESOLVE', 'npm', $null), @($stall, 'pypi', 'pypi'), @($stall, '', $null), @($noVersion, 'pypi', 'unsynced'), @($lag, '', 'unsynced'), @('ERROR: No matching distribution found for unsloth>=2026.9.10', '', 'unsynced'), @("$stall (see https://example.com/)", 'pypi', $null)) {
+ Check "failed host: $($case[1]) ran, output '$($case[0].Split("`n")[-1])' -> $($case[2])" ((Get-MirrorFailedHost -Output $case[0] -Ran $case[1]) -eq $case[2])
+ }
+ function Write-TauriLog {}; function Clear-TauriInstallError {}; function Write-UvDownloadMarker {}; function Write-StudioLine {}
+ $script:UnslothVerbose = $false; $script:InstallTorchMirror = $null
+ function uv { $script:calls += , "$args"; if ("$env:UV_DEFAULT_INDEX $args" -match 'cernet') { $global:LASTEXITCODE = 0 } else { Write-Error $script:fail; $global:LASTEXITCODE = 1 } }
+ function Retry($fail, [scriptblock]$cmd, [switch]$Once) {
+ Run @{ torch = 'blocked' }; $env:UNSLOTH_INSTALL_RETRIES = '2'; $env:UNSLOTH_INSTALL_RETRY_DELAY = '0'
+ $script:fail = $fail; $script:calls = @(); $script:lines = @()
+ if ($Once) { Invoke-InstallCommand -Label t -Command $cmd } else { Invoke-InstallCommandRetry -Label t -Command $cmd }
+ }
+ $rc = Retry $uvDown { uv pip install numpy }
+ Check "pypi transport: retried through the mirror after the default's attempts, which is kept" ($rc -eq 0 -and $script:calls.Count -eq 3 -and $env:UV_DEFAULT_INDEX -eq $pypiMirror -and $script:lines -contains "STEP PyPI failed; retrying through $pypiMirror" -and $env:_UNSLOTH_MIRROR_SPARE -notlike 'pypi|*')
+ $script:UnslothVerbose = $true; $rc = Retry $stall { uv pip install numpy } -Once; $script:UnslothVerbose = $false
+ Check "a stall naming no host retries on the host that ran, in verbose mode too" ($rc -eq 0 -and $script:calls.Count -eq 2 -and $env:UV_DEFAULT_INDEX -eq $pypiMirror -and $script:lines -contains "STEP PyPI failed; retrying through $pypiMirror")
+ foreach ($own in '--torch-backend=auto', '--find-links', 'git+https://github.com/unslothai/unsloth-zoo') {
+ $rc = Retry $stall { uv pip install unsloth $own } -Once; Check "a stall in a command that picks its own source ($own) is not retried" ($rc -eq 1 -and $script:calls.Count -eq 1 -and $env:_UNSLOTH_MIRROR_SPARE -like 'pypi|*')
+ }
+ $rc = Retry $noVersion { uv pip install nothing==9 }; Check "a version not found, with nothing spared behind the mirror, keeps the spare" ($rc -eq 1 -and $script:calls.Count -eq 2 -and $env:_UNSLOTH_MIRROR_SPARE -like 'pypi|*')
+ $rc = Retry $uvDown { uv pip install numpy --default-index https://corp.example/simple }; Check "a pinned index is not moved to the PyPI mirror" ($rc -eq 1 -and $script:calls.Count -eq 2)
+ function uv { $script:calls += , "$args"; if ($env:UV_INDEX) { $global:LASTEXITCODE = 0 } else { Write-Error $script:fail; $global:LASTEXITCODE = 1 } }
+ function Lag([scriptblock]$cmd) { Run @{ pypi = 'slow' }; $script:fail = $lag; $script:calls = @(); $script:lines = @(); Invoke-InstallCommand -Label t -Command $cmd }
+ $rc = Lag { uv pip install unsloth --torch-backend=auto }
+ Check "a release the mirror lacks reruns once with pypi.org behind it, kept for later steps" ($rc -eq 0 -and $script:calls.Count -eq 2 -and $env:UV_DEFAULT_INDEX -eq 'https://pypi.org/simple' -and $env:UV_INDEX -eq $pypiMirror -and $script:lines -contains 'STEP The PyPI mirror failed; retrying through https://pypi.org/simple' -and $env:_UNSLOTH_MIRROR_SPARE -notmatch 'unsynced')
+ foreach ($own in '--no-index', '--index-url') { $rc = Lag { uv pip install unsloth $own }; Check "... not for a command with its own index ($own)" ($rc -eq 1 -and $script:calls.Count -eq 1 -and $env:_UNSLOTH_MIRROR_SPARE -match 'unsynced') }
+ function uv { $script:calls += , "$args"; Write-Error $script:fail; $global:LASTEXITCODE = 1 }
+ $rc = Retry $uvDown { uv pip install numpy } -Once
+ Check "a failed mirror rerun restores the default and spends the spare" ($rc -eq 1 -and $script:calls.Count -eq 2 -and -not $env:UV_DEFAULT_INDEX -and -not $env:PIP_INDEX_URL -and $env:_UNSLOTH_MIRROR_SPARE -notlike 'pypi|*')
+ function uv { $script:calls += , "$args"; if ("$args" -match 'download\.pytorch') { Write-Error $script:fail; $global:LASTEXITCODE = 1 } else { $global:LASTEXITCODE = 0 } }
+ $TorchIndexUrl = 'https://download.pytorch.org/whl/cu128'; $specs = @('torch', 'https://download.pytorch.org/whl/cu128/xformers.whl')
+ $env:_UNSLOTH_MIRROR_SPARE = "pypi|UV_DEFAULT_INDEX=$pypiMirror torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl"; $script:fail = $torchDown; $script:calls = @()
+ $rc = Invoke-InstallCommand -Label t -Command { uv pip install @specs --default-index $TorchIndexUrl }
+ $rc2 = Invoke-InstallCommand -Label t -Command { uv pip install torch --default-index $TorchIndexUrl }
+ Check "torch transport: the command's torch URLs move to the mirror, for later commands too" ($rc -eq 0 -and $rc2 -eq 0 -and $script:calls[1] -eq "pip install torch $M/pytorch/whl/cu128/xformers.whl --default-index $M/pytorch/whl/cu128" -and $script:calls[2] -eq "pip install torch --default-index $M/pytorch/whl/cu128" -and $TorchIndexUrl -eq 'https://download.pytorch.org/whl/cu128' -and $env:_UNSLOTH_MIRROR_SPARE -eq "pypi|UV_DEFAULT_INDEX=$pypiMirror")
+ function uv { $script:calls += , "$args"; Write-Error $script:fail; $global:LASTEXITCODE = 1 }
+ $script:InstallTorchMirror = $null; $env:_UNSLOTH_MIRROR_SPARE = "torch|UNSLOTH_PYTORCH_MIRROR=$M/pytorch/whl"; Remove-Item Env:UNSLOTH_PYTORCH_MIRROR -ErrorAction SilentlyContinue
+ $script:fail = $torchDown; $script:calls = @()
+ $rc = Invoke-InstallCommand -Label t -Command { uv pip install torch --default-index https://download.pytorch.org/whl/cu128 }
+ Check "torch transport: a failed mirror rerun leaves later torch commands on the default" ($rc -eq 1 -and $script:calls.Count -eq 2 -and -not $env:_UNSLOTH_MIRROR_SPARE -and -not $script:InstallTorchMirror -and -not $env:UNSLOTH_PYTORCH_MIRROR)
+ Check "each run starts with no torch mirror from an earlier run" (@($ast.EndBlock.Statements | Where-Object { $_.Extent.Text -eq '$script:InstallTorchMirror = $null' }).Count -eq 1)
+ function npm { $script:calls += , "$args"; if ("$args" -match $script:npmOk) { $global:LASTEXITCODE = 0 } else { Write-Error $npmDown; $global:LASTEXITCODE = 1 } }
+ foreach ($npmOk in 'npmmirror', 'never') { foreach ($verbose in $false, $true) {
+ Run @{ torch = 'blocked' }; $script:calls = @(); $NpmRegistryArgs = @(); $script:npmOk = $npmOk; $script:SetupCommandOutput = ''
+ $script:UnslothVerbose = $verbose; $ok = (Invoke-SetupCommand { npm install @NpmRegistryArgs }) -ne 0 -and (Invoke-NpmMirrorRetry); $script:UnslothVerbose = $false
+ $spent = $script:calls[-1] -eq 'install --registry https://registry.npmmirror.com' -and $env:_UNSLOTH_MIRROR_SPARE -notmatch '(^| )npm\|'
+ if ($npmOk -eq 'npmmirror') { Check "npm transport (verbose $verbose): reinstalled through npmmirror, kept for later npm installs" ($ok -and $spent -and "$NpmRegistryArgs" -eq '--registry https://registry.npmmirror.com' -and $env:UNSLOTH_NPM_REGISTRY -eq 'https://registry.npmmirror.com') }
+ else { Check "npm transport (verbose $verbose): a failed npmmirror rerun is not kept" (-not $ok -and $spent -and $NpmRegistryArgs.Count -eq 0 -and -not $env:UNSLOTH_NPM_REGISTRY) }
+ } }
+ function Get-HostMachineArch { 'x86_64' }; function Get-UvHostArch { 'x86_64' }; function Get-UvExecutableVerdict { 'ok' }; function Get-SetupUvExecutableVerdict { 'ok' }
+ function Invoke-WebRequest([switch]$UseBasicParsing, $OutFile, $Uri) { $script:fetched += $Uri; Copy-Item "$home_/uv.zip" $OutFile }
+ foreach ($exe in 'uv.exe', 'uvx.exe') { New-Item -Force -Path "$home_/whl/uv-0.12.1.data/scripts/$exe" -Value $exe | Out-Null }
+ Compress-Archive -Path "$home_/whl/uv-0.12.1.data" -DestinationPath "$home_/uv.zip"
+ $UvPinnedVersion = '0.12.1'; $wheelSha = (Get-FileHash "$home_/uv.zip").Hash
+ foreach ($fn in 'Install-UvFromRelease', 'Install-UvFromPinnedRelease') { foreach ($good in $true, $false) {
+ $dest = "$home_/bin-$fn-$good"; $env:UV_INSTALL_DIR = $dest; $env:UV_NO_MODIFY_PATH = '1'; $env:UNSLOTH_UV_WHEEL_MIRROR = 'https://m.example/pypi/web/'; $script:fetched = @()
+ $UvPinnedAssets = @{ x86_64 = @{ Asset = 'uv-x86_64-pc-windows-msvc.zip'; Sha256 = 'X'; Wheel = 'packages/ab/uv.whl'; WheelSha256 = $(if ($good) { $wheelSha } else { '0' * 64 }) } }
+ $installed = (@(& $fn)[-1] -eq $true) -and (Test-Path "$dest/uv.exe") -and (Test-Path "$dest/uvx.exe") -and "$script:fetched" -eq 'https://m.example/pypi/web/packages/ab/uv.whl'
+ Check "${fn}: wheel mirror, good digest $good" ($installed -eq $good -and ($good -or -not (Test-Path "$dest/uv.exe")))
+ Check "${fn}: a source that answered clears the flag the mirror retry needs" (-not $(if ($fn -eq 'Install-UvFromRelease') { $script:UvReleaseUnfetched } else { $script:UvPinnedUnfetched }))
+ } }
+ function Invoke-WebRequest { throw 'No such host is known.' }
+ foreach ($fn in 'Install-UvFromRelease', 'Install-UvFromPinnedRelease') { $null = & $fn; Check "${fn}: no source answering sets the flag the mirror retry needs" ($(if ($fn -eq 'Install-UvFromRelease') { $script:UvReleaseUnfetched } else { $script:UvPinnedUnfetched })) }
+} finally {
+ foreach ($n in $saved.Keys) { if ($null -eq $saved[$n]) { Remove-Item "Env:$n" -ErrorAction SilentlyContinue } else { Set-Item "Env:$n" $saved[$n] } }
+ Remove-Item -LiteralPath $home_ -Recurse -Force -ErrorAction SilentlyContinue
+}
+if ($failures) { throw "$failures mirror fallback check(s) failed" }
diff --git a/unsloth_cli/tests/test_installer_download_markers.py b/unsloth_cli/tests/test_installer_download_markers.py
index b5159ed431f..394b975af61 100644
--- a/unsloth_cli/tests/test_installer_download_markers.py
+++ b/unsloth_cli/tests/test_installer_download_markers.py
@@ -79,6 +79,7 @@ _STUBS = """
_is_verbose() { [ -n "${VERBOSE_MODE:-}" ]; }
step() { :; }; substep() { :; }; tauri_stream_log() { :; }; tauri_clear_install_error() { :; }
_redact_install_output() { cat "$@"; }
+_mirror_retry_install() { return "$1"; }
"""
@@ -95,6 +96,9 @@ def _sh_harness(child: str) -> str:
{_STUBS}
{_extract_default()}
{_extract("_uv_download_markers")}
+{_extract("_ric_tee")}
+{_extract("_ric_run")}
+{_extract("_run_install_cmd_once")}
{_extract("run_install_cmd")}
run_install_cmd "install PyTorch" sh -c '{child}'
printf 'RC=%s\\n' "$?"