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.

35.1 KB
Raw Blame
#!/usr/bin/env pwsh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
# The NVIDIA inventory's ctypes rung serves hosts that cannot emit a type (CLM, WDAC).
# Guards drift between the two shared copies, nvidia_probe.py, and the budget/retry shape.
# Run: pwsh -NoProfile -File tests/studio/test_nvidia_python_probe_parity.ps1

$ErrorActionPreference = "Stop"
$root = (Resolve-Path ([System.IO.Path]::Combine($PSScriptRoot, "..", ".."))).Path

$failures = 0
function Check($name, $cond) {
    if ($cond) { Write-Host "  PASS  $name" }
    else { Write-Host "  FAIL  $name" -ForegroundColor Red; $script:failures++ }
}

function Get-HelperSources($path, $names) {
    $tokens = $null; $errors = $null
    $ast = [System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$tokens, [ref]$errors)
    if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "$path has parse errors" }
    $out = @()
    foreach ($name in $names) {
        $fn = $ast.FindAll({ param($n)
            $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name
        }, $true)
        if ($fn.Count -lt 1) { throw "expected $name in $path, found none" }
        $out += $fn[0].Extent.Text
    }
    # A single element unrolls to a bare string, so callers wrap in @().
    return $out
}
function Get-Helper($path, $name) { return @(Get-HelperSources $path @($name))[0] }
function Save-Env([string[]]$names) {
    $h = @{}; foreach ($n in $names) { $h[$n] = [Environment]::GetEnvironmentVariable($n) }; return $h
}
function Restore-Env($saved) { foreach ($n in $saved.Keys) { [Environment]::SetEnvironmentVariable($n, $saved[$n]) } }

$installPs1 = Join-Path $root "install.ps1"
$setupPs1 = Join-Path $root "studio\setup.ps1"
$probePy = Join-Path $root "studio\nvidia_probe.py"

# Both files carry other here-strings, so anchor on the function and the name.
function Get-EmbeddedProbe($path) {
    $text = [System.IO.File]::ReadAllText($path)
    $at = $text.IndexOf("function Read-NvidiaLibraryRawViaPython")
    if ($at -lt 0) { throw "no Read-NvidiaLibraryRawViaPython in $path" }
    $open = $text.IndexOf("`$probeSource = @'", $at)
    if ($open -lt 0) { throw "no probe here-string in $path" }
    $bodyStart = $text.IndexOf("`n", $open) + 1
    $close = $text.IndexOf("`n'@", $bodyStart)
    if ($close -lt 0) { throw "unterminated probe here-string in $path" }
    return $text.Substring($bodyStart, $close - $bodyStart)
}

Write-Host ""
Write-Host "=== the two copies agree ==="

$blockNames = @(
    "New-StudioChildScriptDirectory", "Get-NvidiaNvmlLibraryPath", "Read-NvidiaLibraryRawViaPython",
    "Read-NvidiaLibraryRaw", "Get-NvidiaLibraryInventory", "Test-StudioChildScriptDirectoryElevated",
    "Invoke-StudioSystem32ToolBounded", "Get-StudioSystem32Tool", "Test-StudioPathUnderAdminRoot", "Test-StudioSddlRightsAreWrite",
    "Test-StudioSddlPrincipalIsAdminOnly", "Test-StudioSddlWritableByNonAdmin",
    "Test-StudioDirectoryIsAdminOnly", "Get-StudioLexicalParent", "Get-NvidiaSystem32Dir"
)
$installParts = @(Get-HelperSources $installPs1 $blockNames)
$setupParts = @(Get-HelperSources $setupPs1 $blockNames)
# install.ps1 nests its helpers one level deeper; compare without indentation.
$strip = { param($text) ($text -split "`n" | ForEach-Object { $_.TrimStart() }) -join "`n" }
for ($k = 0; $k -lt $blockNames.Count; $k++) {
    Check "install.ps1 and setup.ps1 carry the same $($blockNames[$k])" `
        ((& $strip $installParts[$k]) -eq (& $strip $setupParts[$k]))
}

$installProbe = Get-EmbeddedProbe $installPs1
$setupProbe = Get-EmbeddedProbe $setupPs1
# Leading whitespace IS the Python syntax, so the copies must match byte for byte.
Check "the embedded Python is byte-identical in both files" ($installProbe -ceq $setupProbe)
Check "the embedded Python sits at column 0, so its indentation survives both files" `
    (($installProbe -split "`n" | Where-Object { $_ -match '^import ctypes' }).Count -eq 1)

Write-Host ""
Write-Host "=== the embedded probe matches studio/nvidia_probe.py ==="

$referenceProbe = [System.IO.File]::ReadAllText($probePy)
foreach ($symbol in @(
    "nvmlInit_v2", "nvmlShutdown", "nvmlSystemGetCudaDriverVersion_v2",
    "nvmlDeviceGetCount_v2", "nvmlDeviceGetHandleByIndex_v2", "nvmlDeviceGetCudaComputeCapability",
    "cuInit", "cuDriverGetVersion", "cuDeviceGetCount", "cuDeviceGet", "cuDeviceGetAttribute"
)) {
    Check "both the embedded probe and nvidia_probe.py call $symbol" `
        (($installProbe -match [regex]::Escape($symbol)) -and ($referenceProbe -match [regex]::Escape($symbol)))
}

# A wrong attribute number still returns a plausible integer.
Check "the embedded probe reads attribute 75 for the capability major" ($installProbe -match 'byref\(major\), 75,')
Check "the embedded probe reads attribute 76 for the capability minor" ($installProbe -match 'byref\(minor\), 76,')
Check "nvidia_probe.py reads the same two attribute numbers" `
    (($referenceProbe -match 'byref\(major\), 75,') -and ($referenceProbe -match 'byref\(minor\), 76,'))
Check "the embedded probe unpacks the version as major*1000 + minor*10" `
    ($installProbe -match 'packed // 1000, \(packed % 1000\) // 10')
Check "nvidia_probe.py unpacks the version identically" `
    ($referenceProbe -match 'packed // 1000, \(packed % 1000\) // 10')
Check "the embedded probe is stdlib-only" `
    (($installProbe -match '(?m)^import ctypes, os, sys$') -and ($installProbe -notmatch '(?m)^\s*(import|from)\s+(?!ctypes|os|sys)'))

Write-Host ""
Write-Host "=== the rung stays underneath the emitted one ==="

$rawBlock = & $strip $setupParts[3]
Check "the reader delegates straight to the Python rung" `
    ($rawBlock -match '\$raw = Read-NvidiaLibraryRawViaPython -TimeoutMs \$TimeoutMs\b')
Check "only a timed-out first child earns the CUDA-only retry" `
    (($rawBlock -match 'if \(-not \$script:NvidiaPythonProbeTimedOut\) \{ return \$raw \}') -and
     ($rawBlock -match 'Read-NvidiaLibraryRawViaPython -TimeoutMs \$childMs -SkipNvml'))
Check "the per-reader bound defaults to 30s" ($rawBlock -match 'param\(\[int\]\$TimeoutMs = 30000\)')
Check "both children share one deadline" ($rawBlock -match '\$deadline = \(Get-Date\)\.AddMilliseconds\(\$TimeoutMs \* 2\)')
Check "the retry gets what is left, less 2 s to reap it, capped at one bound" `
    ($rawBlock -match '\$childMs = \$remainingMs - 2000; if \(\$childMs -gt \$TimeoutMs\) \{ \$childMs = \$TimeoutMs \}')
Check "an exhausted budget skips the retry entirely" ($rawBlock -match 'if \(\$remainingMs -lt 3000\) \{ return "" \}')
Check "the reader itself avoids [math], which CLM refuses" ($rawBlock -notmatch '\[math\]::Min')
Check "there is no emitted rung left to attempt first" ($rawBlock -notmatch 'Get-NvidiaLibraryProbeType|\$native')
Check "a throw in the rung is an empty answer, not a failed install" ($rawBlock -match 'catch \{ return "" \}')
foreach ($file in @($installPs1, $setupPs1)) {
    $whole = [System.IO.File]::ReadAllText($file)
    Check "$(Split-Path -Leaf $file) defines no emitted native types at all" (
        $whole -notmatch 'New-StudioEmittedNativeType|New-StudioDynamicAssembly|DefinePInvokeMethod')
    Check "$(Split-Path -Leaf $file) no longer carries the capability gate they needed" (
        $whole -notmatch 'Test-StudioCanDefineNativeTypes')
}

$pyBlock = & $strip $setupParts[2]
# The rung's comments NAME the banned constructs, so read comment-free source.
$pyCode = ($pyBlock -split "`n" | Where-Object { $_.TrimStart() -notmatch '^#' }) -join "`n"
Check "the rung compiles nothing" ($pyCode -notmatch 'Add-Type')
Check "the rung emits no type" ($pyCode -notmatch 'New-StudioEmittedNativeType|DefinePInvokeMethod')
# CLM refuses these, and CLM hosts are the population this rung serves.
Check "the launcher avoids ProcessStartInfo, which CLM refuses" ($pyCode -notmatch 'ProcessStartInfo')
Check "the launcher avoids [Process]::Start, which CLM refuses" ($pyCode -notmatch '\[(System\.)?Diagnostics\.Process\]::Start|\[Process\]::Start')
Check "the launcher avoids [System.IO.Path], which CLM refuses" ($pyCode -notmatch '\[System\.IO\.Path\]')
Check "the launcher avoids [math], which CLM refuses" ($pyCode -notmatch '\[math\]::')
# CLM allows property reads only on allowed types, which excludes Process and FileInfo.
foreach ($blocked in @("\.Id\b", "\.HasExited\b", "\.ExitCode\b", "\.FullName\b", "New-TemporaryFile")) {
    Check "the launcher avoids $($blocked -replace '\\[.b]', '') , which CLM refuses on a Process or FileInfo" (
        $pyCode -notmatch $blocked)
}
Check "the launcher waits by object, not by pid" ($pyCode -match 'Wait-Process -InputObject \$proc')
Check "the launcher kills by object too" ($pyCode -match 'Stop-Process -InputObject \$proc')
Check "a killed child is waited for before its files are deleted" (
    $pyCode -match 'Stop-Process -InputObject \$proc[^\n]*\n(\s*#[^\n]*\n)?\s*Wait-Process -InputObject \$proc -Timeout \d+')
Check "the timeout is detected through an error variable" ($pyCode -match '-ErrorVariable waitError')
Check "the launcher casts nothing to [ref]" ($pyCode -notmatch '\[ref\]\s*\$')
Check "the comment-free source still contains the launcher" ($pyCode -match 'Start-Process -FilePath \$exe')
Check "the interpreter comes from a per-file hook, not from the shared region" `
    (($pyBlock -match 'Get-NvidiaProbePythonExe') -and ($pyBlock -notmatch 'function Get-NvidiaProbePythonExe'))
foreach ($file in @($installPs1, $setupPs1)) {
    Check "$(Split-Path -Leaf $file) defines its own Get-NvidiaProbePythonExe" `
        ([System.IO.File]::ReadAllText($file) -match 'function Get-NvidiaProbePythonExe')
}
Check "the probe is switchable off" ($pyBlock -match 'UNSLOTH_NVIDIA_PYTHON_PROBE')
Check "the temp files are always cleaned up" ($pyBlock -match 'finally \{[\s\S]*Remove-Item -LiteralPath \$stale')
# 5.1 appends each native argument verbatim, so nothing with a space may reach argv.
Check "only space-free arguments reach the command line" ($pyCode -match 'ArgumentList @\("-I", "-S", "-B", "-"\)')
Check "the script arrives on stdin, not as a path argument" ($pyCode -match '-RedirectStandardInput \$scriptFile')
Check "the library hints travel in the environment" `
    (($pyCode -match '\$env:UNSLOTH_NVML_HINT = \$nvmlHint') -and ($pyCode -match '\$env:UNSLOTH_CUDA_HINT = \$cudaHint'))
Check "the hint variables are restored afterwards" ($pyCode -match 'finally \{[\s\S]*Remove-Item Env:UNSLOTH_NVML_HINT')
Check "the embedded probe reads the hints from the environment" `
    (($installProbe -match 'os\.environ\.get\("UNSLOTH_NVML_HINT"') -and ($installProbe -match 'os\.environ\.get\("UNSLOTH_CUDA_HINT"'))

Write-Host ""
Write-Host "=== the installer uses the interpreter this run installed ==="

# Get-StudioEarlyPython memoises a MISS from before the venv exists.
Invoke-Expression (Get-Helper $installPs1 "Get-NvidiaProbePythonExe")
function Get-StudioEarlyPython { $script:EarlyCalled = $true; return "/early/python3" }

$hookDir = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-hook-" + [guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Path $hookDir | Out-Null
$savedEnv = Save-Env @("OS", "UNSLOTH_EARLY_PYTHON_PROBE")
try {
    $fakeVenv = Join-Path $hookDir "venv-python"
    [System.IO.File]::WriteAllText($fakeVenv, "")

    $VenvPython = $fakeVenv
    $script:EarlyCalled = $false
    Check "the venv interpreter wins when it exists" ((Get-NvidiaProbePythonExe) -eq $fakeVenv)
    Check "and the memoising ladder is not consulted at all" ($script:EarlyCalled -eq $false)

    $VenvPython = Join-Path $hookDir "not-created-yet"
    $script:EarlyCalled = $false
    Check "with no venv on disk the ladder still answers" ((Get-NvidiaProbePythonExe) -eq "/early/python3")
    Check "and the ladder really was the source" ($script:EarlyCalled -eq $true)

    $VenvPython = $null
    Check "an unset variable falls through rather than returning empty" ((Get-NvidiaProbePythonExe) -eq "/early/python3")

    # Only a New-StudioShortcuts parameter; under `irm | iex` a caller's variable would leak in.
    $ManagedPythonPath = $fakeVenv
    Check "a stray `$ManagedPythonPath is not a candidate" ((Get-NvidiaProbePythonExe) -eq "/early/python3")
    Remove-Variable -Name ManagedPythonPath -ErrorAction SilentlyContinue

    $env:OS = "Windows_NT"
    function Test-StudioChildScriptDirectoryElevated { return $true }
    function Test-StudioPathUnderAdminRoot { param([string]$Path) return $false }
    $VenvPython = $fakeVenv
    $script:EarlyCalled = $false
    Check "an elevated run still probes with the venv interpreter it already executes" ((Get-NvidiaProbePythonExe) -eq $fakeVenv)
    Check "and does not fall to the admin-root-only ladder for it" ($script:EarlyCalled -eq $false)

    $env:UNSLOTH_EARLY_PYTHON_PROBE = "0"
    Check "the kill switch still wins over the venv interpreter" ((Get-NvidiaProbePythonExe) -eq "")
    Restore-Env @{ UNSLOTH_EARLY_PYTHON_PROBE = $savedEnv["UNSLOTH_EARLY_PYTHON_PROBE"] }

    Invoke-Expression (Get-Helper $setupPs1 "Get-NvidiaProbePythonExe")
    $VenvDir = Join-Path $hookDir "venv"
    $setupVenvPy = Join-Path $VenvDir "Scripts\python.exe"
    New-Item -ItemType Directory -Force -Path (Split-Path -Parent $setupVenvPy) | Out-Null
    [System.IO.File]::WriteAllText($setupVenvPy, "")
    Check "setup.ps1: an elevated run still probes with the venv interpreter" ((Get-NvidiaProbePythonExe) -eq $setupVenvPy)
} finally {
    Remove-Item Function:Test-StudioChildScriptDirectoryElevated, Function:Test-StudioPathUnderAdminRoot -ErrorAction SilentlyContinue
    Restore-Env $savedEnv
    Remove-Item -LiteralPath $hookDir -Recurse -Force -ErrorAction SilentlyContinue
}

$installText = [System.IO.File]::ReadAllText($installPs1)
$venvAt = $installText.IndexOf('$VenvPython = Join-Path $VenvDir')
$firstInventoryAt = $installText.IndexOf('if (-not $HasNvidiaSmi -and (Get-NvidiaLibraryInventory))')
Check "the venv interpreter is named before the inventory is first read" ($venvAt -gt 0 -and $firstInventoryAt -gt $venvAt)

Write-Host ""
Write-Host "=== the timeout is whole seconds, rounded up, without [math]::Ceiling ==="

# / is floating point and [int] rounds to nearest, so "+999" turns 10000ms into 11s.
$ceilLines = @($pyBlock -split "`n" | Where-Object {
    $_ -match '^\$seconds = ' -or $_ -match '^if \(\(\$TimeoutMs % 1000\) -ne 0\)' -or $_ -match '^if \(\$seconds -lt 1\)'
})
Check "the ceiling is four lines of integer arithmetic" ($ceilLines.Count -eq 4)
$ceilExpr = [scriptblock]::Create(($ceilLines -join "`n") + "`nreturn `$seconds")
foreach ($row in @(@(10000, 10), @(10001, 11), @(9999, 10), @(1, 1), @(0, 1), @(2500, 3), @(20000, 20))) {
    $TimeoutMs = $row[0]
    Check "$($row[0]) ms is $($row[1]) s" ((& $ceilExpr) -eq $row[1])
}

Write-Host ""
Write-Host "=== behaviour, driven through the real functions ==="

foreach ($part in $setupParts) { Invoke-Expression $part }
function Get-NvidiaLibraryProbeType { return $null }
$script:PythonExe = ""
function Get-NvidiaProbePythonExe { return $script:PythonExe }

Check "no interpreter means no answer, not an error" ((Read-NvidiaLibraryRawViaPython -TimeoutMs 5000) -eq "")

foreach ($file in @($installPs1, $setupPs1)) {
    $leaf = Split-Path -Leaf $file
    $dirFn = Get-Helper $file "New-StudioChildScriptDirectory"
    $elevFn = Get-Helper $file "Test-StudioChildScriptDirectoryElevated"
    $elevCode = (($elevFn -split "`r?`n") | Where-Object { $_.Trim() -notmatch '^#' }) -join "`n"
    $toolFn = Get-Helper $file "Get-StudioSystem32Tool"
    $probePick = Get-Helper $file "Get-NvidiaProbePythonExe"
    $whole = [System.IO.File]::ReadAllText($file)
    foreach ($row in @(
        @("carries the private child-script directory", ($dirFn.Length -gt 100)),
        @("refuses a directory that already exists", (
            $dirFn -match 'New-Item -ItemType Directory[^\r\n]*-ErrorAction Stop' -and
            $dirFn -notmatch 'New-Item -ItemType Directory[^\r\n]*-Force')),
        @("raises the integrity label rather than trusting a DACL", ($dirFn -match 'icacls' -and $dirFn -match 'setintegritylevel')),
        @("reads the label back rather than assuming it took", ($dirFn -match '\$labelled' -and $dirFn -match 'High Mandatory Level')),
        @("does not depend on the English spelling of the label", ($dirFn -match '\$result.ExitCode -eq 0' -and $dirFn -match 'S-1-16-12288')),
        # A bracketed %TEMP% is a pattern that can match elsewhere.
        @("confirms the directory it returns really exists", ($dirFn -match 'Test-Path -LiteralPath \$dir -PathType Container')),
        @("cleans up a directory the pattern created elsewhere", ($dirFn -match 'Remove-Item -LiteralPath \$createdPath')),
        @("refuses a directory something was planted in before the label landed", (
            $dirFn -match 'Get-ChildItem -LiteralPath \$dir -Force' -and $dirFn -match '\$planted')),
        @("refuses an unlabelled directory when it is elevated", (
            $dirFn -match 'Test-StudioChildScriptDirectoryElevated' -and $dirFn -match 'Remove-Item[^\r\n]*\$dir')),
        # whoami, not WindowsPrincipal: the managed identity types are unreachable under CLM.
        @("asks the token for its own label with an in-box tool", ($elevCode -match 'whoami' -and $elevCode -match 'S-1-16-')),
        @("does not use the managed principal types for it", ($elevCode -notmatch 'WindowsPrincipal')),
        @("treats an unreadable token as elevated", (
            $elevCode -match 'catch \{ return \$true \}' -and $elevCode -match 'IsNullOrWhiteSpace\(\$groups\)\) \{ return \$true \}')),
        @("names the in-box tools by absolute path", (
            $dirFn -match 'Get-StudioSystem32Tool -Name "icacls\.exe"' -and $elevCode -match 'Get-StudioSystem32Tool -Name "whoami\.exe"')),
        @("does not resolve either of them through PATH", ($dirFn -notmatch '& icacls\.exe' -and $elevCode -notmatch '& whoami\.exe')),
        @("builds that path under System32", ($toolFn -match 'System32' -and $toolFn -match 'Test-Path -LiteralPath \$candidate')),
        @("does not elevation-gate the venv interpreter it already runs", (
            $probePick -notmatch 'Test-StudioChildScriptDirectoryElevated' -and $probePick -notmatch 'Test-StudioPathUnderAdminRoot')),
        @("only answers no for a label it actually read", ($elevCode -match 'S-1-16-\\d\+.*return \$false')),
        @("comment stripper kept the code (bites)", ($elevCode -match 'return')),
        # The shared root may hold the ANSWER when the private directory declines, never the program.
        @("writes no child program straight into the shared temp root", (
            ($whole -notmatch '\$stem = Join-Path \$tempRoot') -or
            ($whole -match 'if \(-not \$inline\) \{ Set-Content -LiteralPath \$scriptFile' -and
             @([regex]::Matches($whole, 'Set-Content -LiteralPath \$scriptFile')).Count -eq 1)))
    )) { Check "$leaf $($row[0])" $row[1] }
}

$bracketRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth [test] " + [guid]::NewGuid().ToString("N"))
$null = New-Item -ItemType Directory -Path $bracketRoot -Force
$savedEnv = Save-Env @("TEMP", "TMPDIR")
try {
    $env:TEMP = $bracketRoot
    $env:TMPDIR = $bracketRoot
    $bracketDir = New-StudioChildScriptDirectory
    Check "a temp root with brackets still yields a directory" (-not [string]::IsNullOrWhiteSpace($bracketDir))
    Check "and the path it returned is the directory that exists" ($bracketDir -and (Test-Path -LiteralPath $bracketDir -PathType Container))
    Check "and it is inside the bracketed root, not beside it" ("$bracketDir".StartsWith($bracketRoot))
    if ($bracketDir) { Remove-Item -LiteralPath $bracketDir -Recurse -Force -ErrorAction SilentlyContinue }
} finally {
    Restore-Env $savedEnv
    Remove-Item -LiteralPath $bracketRoot -Recurse -Force -ErrorAction SilentlyContinue
}

$savedEnv = Save-Env @("OS")
try {
    $env:OS = "Windows_NT"
    # Stubbed: the real icacls cannot raise a label from a standard account.
    function Test-StudioChildScriptDirectoryElevated { return $false }
    function Get-StudioSystem32Tool { param([string]$Name) return "C:\Windows\System32\$Name" }
    function Invoke-StudioSystem32ToolBounded { param([string]$Exe, [string[]]$Arguments, [int]$TimeoutMs) return @{ Output = ""; ExitCode = 0 } }
    function Get-ChildItem { param($LiteralPath, [switch]$Force, $ErrorAction) return @([pscustomobject]@{ Name = "early.py" }) }
    Check "a directory with something already in it is refused" ([string]::IsNullOrWhiteSpace((New-StudioChildScriptDirectory)))
} finally {
    Remove-Item Function:Get-ChildItem -ErrorAction SilentlyContinue
    foreach ($part in $setupParts[5..12]) { Invoke-Expression $part }
    Restore-Env $savedEnv
}

# With whoami absent the answer must be "elevated"; it is looked up only in System32.
try {
    function Get-StudioSystem32Tool { param([string]$Name) return "" }
    Check "an unreadable token reads as elevated" ((Test-StudioChildScriptDirectoryElevated) -eq $true)
} finally { Invoke-Expression $setupParts[7] }

$madeDir = New-StudioChildScriptDirectory
Check "the directory is created" (-not [string]::IsNullOrWhiteSpace($madeDir))
if ($madeDir) {
    Check "and it is empty, so nothing was adopted" (@(Get-ChildItem -LiteralPath $madeDir -Force -ErrorAction SilentlyContinue).Count -eq 0)
    Check "and two calls never collide" ($madeDir -ne (New-StudioChildScriptDirectory))
    Remove-Item -LiteralPath $madeDir -Recurse -Force -ErrorAction SilentlyContinue
}

$python = $null
foreach ($name in @("python3", "python")) {
    $cmd = Get-Command $name -ErrorAction SilentlyContinue
    if ($cmd) { $python = $cmd.Source; break }
}

if (-not $python) {
    Write-Host "  SKIP  no Python on this runner; the behavioural rows need one"
} else {
    $script:PythonExe = $python
    $savedEnv = Save-Env @("UNSLOTH_NVIDIA_PYTHON_PROBE")
    try {
        $env:UNSLOTH_NVIDIA_PYTHON_PROBE = "0"
        Check "UNSLOTH_NVIDIA_PYTHON_PROBE=0 declines before spawning anything" ((Read-NvidiaLibraryRawViaPython -TimeoutMs 5000) -eq "")
    } finally { Restore-Env $savedEnv }

    $answer = Read-NvidiaLibraryRawViaPython -TimeoutMs 30000
    if (-not $answer) {
        Check "no driver on this host answers empty, not an error" ($answer -eq "")
    } else {
        Check "the answer names a source the inventory understands" ($answer -match '^(nvml|cuda);')
        Check "the answer carries a version and at least one capability" ($answer -match '^(nvml|cuda);\d+;\d+;\d+\.\d+(,\d+\.\d+)*$')
        # Every row is guarded on a non-null inventory: $null -eq $null would pass them all.
        $inventory = Get-NvidiaLibraryInventory -TimeoutSec 30
        Check "the inventory parses the answer with nothing emitted" ($null -ne $inventory)
        Check "the inventory recovered the compute capabilities" `
            ($null -ne $inventory -and $inventory.ComputeCaps.Count -ge 1 -and $inventory.ComputeCaps[0] -match '^\d+\.\d+$')
        Check "the inventory device count matches the capability list" ($null -ne $inventory -and $inventory.Count -eq $inventory.ComputeCaps.Count)
        Check "the CUDA major version is plausible" ($null -ne $inventory -and $inventory.CudaMajor -ge 1)
        Check "the inventory names the source the raw answer named" ($null -ne $inventory -and $answer.StartsWith("$($inventory.Source);"))
    }

    $tempRoot = if ($env:TEMP) { $env:TEMP } elseif ($env:TMPDIR) { $env:TMPDIR } else { "/tmp" }
    $litter = @(Get-ChildItem -LiteralPath $tempRoot -Filter "unsloth-nvprobe-*" -ErrorAction SilentlyContinue)
    Check "the launcher leaves no scratch files behind" ($litter.Count -eq 0)
}

Write-Host ""
Write-Host "=== a hung NVML earns one CUDA-only child, and nothing else does ==="

Check "the embedded probe skips NVML only when the switch is 1" `
    ($installProbe -match 'if os\.environ\.get\("UNSLOTH_NVIDIA_PROBE_SKIP_NVML", ""\) != "1":')
Check "the launcher sets the switch only for a -SkipNvml child" `
    (($pyCode -match 'if \(\$SkipNvml\) \{ \$env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML = "1" \}') -and
     ($pyCode -match 'finally \{[\s\S]*Remove-Item Env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML'))

function Invoke-RawWithStub($outcomes, [int]$TimeoutMs = 30000, [int]$TimeoutSleepMs = 0) {
    $script:StubCalls = @()
    $script:StubOutcomes = [System.Collections.ArrayList]@($outcomes)
    $script:StubSleepMs = $TimeoutSleepMs
    return & {
        function Read-NvidiaLibraryRawViaPython {
            param([int]$TimeoutMs = 10000, [switch]$SkipNvml)
            $script:StubCalls += , @{ TimeoutMs = $TimeoutMs; SkipNvml = [bool]$SkipNvml }
            $next = $script:StubOutcomes[0]; $script:StubOutcomes.RemoveAt(0)
            $script:NvidiaPythonProbeTimedOut = ($next -eq "TIMEOUT")
            if ($next -eq "TIMEOUT") {
                if ($script:StubSleepMs -gt 0) { Start-Sleep -Milliseconds $script:StubSleepMs }
                return ""
            }
            return $next
        }
        Read-NvidiaLibraryRaw -TimeoutMs $TimeoutMs
    }
}
$got = Invoke-RawWithStub @("TIMEOUT", "cuda;12;8;8.9")
Check "stub: a timed-out first child is followed by exactly one more" ($script:StubCalls.Count -eq 2)
Check "stub: the first child reads NVML, the second skips it" `
    ($script:StubCalls.Count -eq 2 -and -not $script:StubCalls[0].SkipNvml -and $script:StubCalls[1].SkipNvml)
Check "stub: an instant timeout leaves the retry a whole 30s bound, capped at one" `
    ($script:StubCalls.Count -eq 2 -and $script:StubCalls[0].TimeoutMs -eq 30000 -and $script:StubCalls[1].TimeoutMs -eq 30000)
Check "stub: the CUDA-only answer is returned" ($got -eq "cuda;12;8;8.9")
foreach ($row in @(
    @("stub: an empty answer spawns no second child", @(""), 1, ""),
    @("stub: an answer spawns no second child", @("nvml;13;0;12.0"), 1, "nvml;13;0;12.0"),
    @("stub: a CUDA-only child that also hangs ends the search", @("TIMEOUT", "TIMEOUT"), 2, "")
)) {
    $got = Invoke-RawWithStub $row[1]
    Check $row[0] ($script:StubCalls.Count -eq $row[2] -and $got -eq $row[3])
}
# Shared deadline 2 x 1500 ms; the first child burns 1200 ms of it.
$got = Invoke-RawWithStub @("TIMEOUT", "cuda;12;8;8.9") -TimeoutMs 1500 -TimeoutSleepMs 1200
Check "stub: no CUDA-only child when under 3 s of the shared budget remain" ($script:StubCalls.Count -eq 1 -and $got -eq "")
# Each bound leaves 2 s to reap, so the CUDA child gets ~2800 ms. Not 3000: an early
# Start-Sleep wake on Windows gave 3001.
$got = Invoke-RawWithStub @("TIMEOUT", "cuda;12;8;8.9") -TimeoutMs 4000 -TimeoutSleepMs 3200
$secondMs = if ($script:StubCalls.Count -ge 2) { $script:StubCalls[1].TimeoutMs } else { "none" }
Check "stub: every child's bound leaves 2 s of the shared deadline to reap it (second bound: $secondMs ms)" (
    $script:StubCalls.Count -eq 2 -and $script:StubCalls[0].TimeoutMs -eq 4000 -and
    $script:StubCalls[1].TimeoutMs -le 3000 -and $script:StubCalls[1].TimeoutMs -ge 2000)
$got = Invoke-RawWithStub @("TIMEOUT", "cuda;12;8;8.9") -TimeoutMs 5000 -TimeoutSleepMs 5000
Check "stub: the CUDA-only child gets what is left of the shared budget, not a fresh bound" `
    ($script:StubCalls.Count -eq 2 -and $script:StubCalls[0].TimeoutMs -eq 5000 -and
     $script:StubCalls[1].TimeoutMs -lt 5000 -and $script:StubCalls[1].TimeoutMs -ge 2000 -and $got -eq "cuda;12;8;8.9")

# POSIX sh only: a .cmd stand-in's sleeping child would hold the output file on Windows.
if ($IsWindows -or $env:OS -eq "Windows_NT") {
    Write-Host "  SKIP  the fake-interpreter rows need a POSIX shell"
} else {
    $fakeDir = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-fakepy-" + [guid]::NewGuid().ToString("N"))
    New-Item -ItemType Directory -Path $fakeDir | Out-Null
    $fakePy = Join-Path $fakeDir "python"
    $fakeLog = Join-Path $fakeDir "calls.log"
    $fakeBody = @(
        '#!/bin/sh'
        'if [ "$UNSLOTH_FAKE_PY_MODE" = "inline" ]; then'
        '  echo "argv4=$4 src=${UNSLOTH_NVIDIA_PROBE_SOURCE:+present}" >> "$UNSLOTH_FAKE_PY_LOG"'
        '  if [ "$4" = "-c" ] && [ -n "$UNSLOTH_NVIDIA_PROBE_SOURCE" ]; then printf "nvml;12;8;8.9"; fi'
        '  exit 0'
        'fi'
        'cat > /dev/null'
        'echo "skip=${UNSLOTH_NVIDIA_PROBE_SKIP_NVML:-unset}" >> "$UNSLOTH_FAKE_PY_LOG"'
        'if [ "$UNSLOTH_NVIDIA_PROBE_SKIP_NVML" = "1" ]; then printf "cuda;12;8;8.9"; exit 0; fi'
        'if [ "$UNSLOTH_FAKE_PY_MODE" = "hang" ]; then exec sleep 30; fi'
        'exit 0'
    ) -join "`n"
    [System.IO.File]::WriteAllText($fakePy, "$fakeBody`n")
    & chmod +x $fakePy
    # -NoPrivateDir makes the private child-script directory decline (the inline path).
    function Invoke-Fake {
        param([string]$Mode, [scriptblock]$Body, [hashtable]$Env = @{}, [switch]$NoPrivateDir)
        $env:UNSLOTH_FAKE_PY_MODE = $Mode
        Remove-Item -LiteralPath $fakeLog -ErrorAction SilentlyContinue
        $savedRoots = Save-Env @($Env.Keys)
        $savedDirFn = ${function:New-StudioChildScriptDirectory}
        if ($NoPrivateDir) { ${function:New-StudioChildScriptDirectory} = { return "" } }
        try {
            foreach ($k in $Env.Keys) { [Environment]::SetEnvironmentVariable($k, $Env[$k]) }
            $script:FakeGot = & $Body
        } finally {
            ${function:New-StudioChildScriptDirectory} = $savedDirFn
            Restore-Env $savedRoots
        }
        return @(Get-Content -LiteralPath $fakeLog -ErrorAction SilentlyContinue)
    }
    $savedPy = $script:PythonExe
    $savedEnv = Save-Env @("UNSLOTH_NVIDIA_PROBE_SKIP_NVML", "UNSLOTH_FAKE_PY_LOG", "UNSLOTH_FAKE_PY_MODE")
    try {
        $script:PythonExe = $fakePy
        $env:UNSLOTH_FAKE_PY_LOG = $fakeLog
        $env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML = "caller"
        $raw = { Read-NvidiaLibraryRaw -TimeoutMs 5000 }
        $viaPython = { Read-NvidiaLibraryRawViaPython -TimeoutMs 5000 }

        $seen = Invoke-Fake "hang" $raw
        Check "live: a first child killed at its bound triggers exactly one more" ($seen.Count -eq 2)
        Check "live: the first child saw no switch, the second saw 1" (($seen -join ",") -eq "skip=unset,skip=1")
        Check "live: the CUDA-only answer is returned" ($script:FakeGot -eq "cuda;12;8;8.9")
        Check "live: the caller's switch value is restored" ($env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML -eq "caller")

        $seen = Invoke-Fake "empty" $raw
        Check "live: a first child that answers empty spawns no second" ($seen.Count -eq 1 -and $script:FakeGot -eq "")
        Check "live: the switch is still restored after a single child" ($env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML -eq "caller")

        Remove-Item Env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML -ErrorAction SilentlyContinue
        $null = Invoke-Fake "hang" $raw
        Check "live: an unset switch is left unset" ($null -eq $env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML)

        # The shared-root fallback is a standard user's; an elevated run declines it.
        $savedElevated = ${function:Test-StudioChildScriptDirectoryElevated}
        function Test-StudioChildScriptDirectoryElevated { return $false }
        $seen = Invoke-Fake "inline" $viaPython -NoPrivateDir
        Check "live: a declined private directory still yields the inventory" ($script:FakeGot -eq "nvml;12;8;8.9")
        Check "live: the program came through the environment, not a file" (($seen -join ",") -eq "argv4=-c src=present")
        Check "live: the program variable does not outlive the child" ($null -eq $env:UNSLOTH_NVIDIA_PROBE_SOURCE)

        $blocker = Join-Path $fakeDir "not-a-dir"
        Set-Content -LiteralPath $blocker -Value ""
        $altRoot = Join-Path $fakeDir "localappdata"
        New-Item -ItemType Directory -Force -Path $altRoot | Out-Null
        $brokenTemp = Join-Path $blocker "t"
        $null = Invoke-Fake "inline" $viaPython -NoPrivateDir -Env @{ TEMP = $brokenTemp; TMP = $brokenTemp; LOCALAPPDATA = $altRoot; TMPDIR = $null }
        Check "live: an unusable TEMP falls through to the next root" ($script:FakeGot -eq "nvml;12;8;8.9")
        Check "live: and leaves nothing behind there" (@(Get-ChildItem -LiteralPath $altRoot).Count -eq 0)

        $null = Invoke-Fake "inline" $viaPython -Env @{ TEMP = "Z:\UnslothReviewTemp"; TMP = "Z:\UnslothReviewTemp"; LOCALAPPDATA = $altRoot; TMPDIR = $null }
        Check "live: a TEMP on a missing drive falls through to the next root" ($script:FakeGot -eq "nvml;12;8;8.9")
        Check "live: and no stray output file lands in the working directory" (-not (Test-Path -LiteralPath ".out"))

        function Test-StudioChildScriptDirectoryElevated { return $true }
        $seen = Invoke-Fake "inline" $viaPython -NoPrivateDir
        Check "live: an elevated run declines the shared-root fallback" ($script:FakeGot -eq "" -and $seen.Count -eq 0)
        ${function:Test-StudioChildScriptDirectoryElevated} = $savedElevated
    } finally {
        $script:PythonExe = $savedPy
        Restore-Env $savedEnv
        Remove-Item -LiteralPath $fakeDir -Recurse -Force -ErrorAction SilentlyContinue
    }
}

# CLM refuses ProcessStartInfo, so the system-tool wrapper has a job branch. A trusted
# wrapper starts the job and constrains its body, as an enforced policy would.
if ($IsWindows -or $env:OS -eq "Windows_NT") {
    Write-Host "  SKIP  the constrained system-tool rows need a POSIX shell"
} else {
    $clmDir = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-clmtool-" + [guid]::NewGuid().ToString("N"))
    New-Item -ItemType Directory -Path $clmDir | Out-Null
    try {
        $tool = Join-Path $clmDir "tool"
        [System.IO.File]::WriteAllText($tool, "#!/bin/sh`nif [ `"`$1`" = hang ]; then exec sleep 30; fi`nprintf '%s|' `"`$@`"`nexit 3`n")
        & chmod +x $tool
        foreach ($file in @($installPs1, $setupPs1)) {
            $leaf = Split-Path $file -Leaf
            $script = @(
                'function Start-Job {'
                '    param([scriptblock]$ScriptBlock, [object[]]$ArgumentList)'
                '    $body = ''$ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"; & {'' + $ScriptBlock.ToString() + ''} @args'''
                '    Microsoft.PowerShell.Core\Start-Job -ScriptBlock ([scriptblock]::Create($body)) -ArgumentList $ArgumentList'
                '}'
                '$ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"'
                (Get-Helper $file "Invoke-StudioSystem32ToolBounded")
                '$r = Invoke-StudioSystem32ToolBounded -Exe $args[0] -Arguments @("a b", "c")'
                '"MODE=$($ExecutionContext.SessionState.LanguageMode)"'
                '"OUT=$($r.Output) EXIT=$($r.ExitCode)"'
                '$t0 = Get-Date'
                '$h = Invoke-StudioSystem32ToolBounded -Exe $args[0] -Arguments @("hang") -TimeoutMs 1000'
                '"HUNG=$($null -eq $h) SECS=$([int]((Get-Date) - $t0).TotalSeconds)"'
            ) -join "`n"
            $scriptPath = Join-Path $clmDir "clm_$leaf"
            [System.IO.File]::WriteAllText($scriptPath, $script)
            $out = @(& pwsh -NoProfile -File $scriptPath $tool 2>&1 | ForEach-Object { "$_" }) -join "`n"
            Check "constrained ($leaf): the session really is constrained" ($out -match 'MODE=ConstrainedLanguage')
            Check "constrained ($leaf): a system tool's output and exit code come back" ($out -match 'OUT=a b\|c\| EXIT=3')
            $secs = if ($out -match 'SECS=(\d+)') { [int]$Matches[1] } else { 99 }
            Check "constrained ($leaf): a hung system tool is abandoned near its bound" ($out -match 'HUNG=True' -and $secs -lt 10)
        }
    } finally {
        Remove-Item -LiteralPath $clmDir -Recurse -Force -ErrorAction SilentlyContinue
    }
}

Write-Host ""
if ($failures -gt 0) { Write-Host "$failures check(s) failed" -ForegroundColor Red; exit 1 }
Write-Host "All checks passed"