Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Remove the reflection-emit apparatus and all four emitted types (#11193)
Daniel Hanbd612c86352b
diff --git a/.github/workflows/cross-platform-parity-ci.yml b/.github/workflows/cross-platform-parity-ci.ymlindex 69db28eaa72..c3f3dc6485e 100644--- a/.github/workflows/cross-platform-parity-ci.yml+++ b/.github/workflows/cross-platform-parity-ci.yml@@ -72,10 +72,11 @@ on:# This is the only lane that executes it, and the Windows smoke workflow it moved# from deliberately skips it, so without this line a PR editing only the test# would never run it.- - 'tests/studio/test_native_path_resolver_degrades.ps1'+ - 'tests/studio/test_path_resolver_degrades.ps1'- 'tests/python/test_windows_python_venv_hardening.py'- 'tests/python/test_windows_setup_output_encoding.py'- - 'tests/python/test_windows_installer_native_type_fallback.py'+ - 'tests/python/test_windows_installer_resolver_fallback.py'+ - 'tests/python/test_installer_helper_declaration_order.py'- 'tests/python/test_installer_embedded_nvidia_probe.py'- 'tests/python/test_pwsh_runner_encoding.py'# The deliberate unreadable-directory and traversal-limit cases for the@@ -189,10 +190,11 @@ on:# This is the only lane that executes it, and the Windows smoke workflow it moved# from deliberately skips it, so without this line a PR editing only the test# would never run it.- - 'tests/studio/test_native_path_resolver_degrades.ps1'+ - 'tests/studio/test_path_resolver_degrades.ps1'- 'tests/python/test_windows_python_venv_hardening.py'- 'tests/python/test_windows_setup_output_encoding.py'- - 'tests/python/test_windows_installer_native_type_fallback.py'+ - 'tests/python/test_windows_installer_resolver_fallback.py'+ - 'tests/python/test_installer_helper_declaration_order.py'- 'tests/python/test_installer_embedded_nvidia_probe.py'- 'tests/python/test_pwsh_runner_encoding.py'# The deliberate unreadable-directory and traversal-limit cases for the@@ -328,7 +330,8 @@ jobs:tests/python/test_cross_platform_parity.pytests/python/test_windows_python_venv_hardening.pytests/python/test_windows_setup_output_encoding.py- tests/python/test_windows_installer_native_type_fallback.py+ tests/python/test_windows_installer_resolver_fallback.py+ tests/python/test_installer_helper_declaration_order.pytests/python/test_pwsh_runner_encoding.pytests/python/test_compiler_watch_temp_scan.pytests/python/test_installer_embedded_nvidia_probe.py@@ -457,14 +460,9 @@ jobs:- name: Uninstaller ownership gate testsshell: pwshrun: pwsh -NoProfile -File tests/studio/test_uninstall_legacy_layout_gate.ps1- # Linux on purpose, and the only lane that can run it. Reflection emit succeeds on- # this engine so the types build exactly as they do on 5.1, while kernel32 and- # shell32 do not resolve, so every call through them fails the way it does on a- # Windows host whose antivirus took the helper away. That is the build side and the- # call side in one run, and it is not reproducible on a healthy Windows runner.- - name: Native path resolver degradation tests+ - name: Path resolver degradation testsif: runner.os == 'Linux'- run: pwsh -NoProfile -File tests/studio/test_native_path_resolver_degrades.ps1+ run: pwsh -NoProfile -File tests/studio/test_path_resolver_degrades.ps1- name: POSIX rollback lifecycle testsif: runner.os == 'Linux'run: sh tests/sh/test_install_rollback_lifecycle.shdiff --git a/.github/workflows/studio-windows-inference-smoke.yml b/.github/workflows/studio-windows-inference-smoke.ymlindex a24567d2020..c55930dece1 100644--- a/.github/workflows/studio-windows-inference-smoke.yml+++ b/.github/workflows/studio-windows-inference-smoke.yml@@ -188,11 +188,6 @@ jobs:if ($LASTEXITCODE) { exit $LASTEXITCODE }pwsh -NoProfile -File tests/studio/test_path_probe_access_denied.ps1if ($LASTEXITCODE) { exit $LASTEXITCODE }- # test_native_path_resolver_degrades.ps1 is deliberately NOT here. Its whole- # premise is a host where the emit succeeds and kernel32 does not resolve, so- # it asserts that the native rung answers null and the ladder reports the- # result inexact. On a healthy Windows runner those calls SUCCEED and every one- # of those assertions is false. It runs on the Linux leg of# cross-platform-parity-ci.yml, which is the host it describes.# uninstall.ps1: native uninstall must keep the shared unsloth.ico while adiff --git a/.github/workflows/windows-no-compiler-ci.yml b/.github/workflows/windows-no-compiler-ci.ymlindex 90385a4ad24..88f94088da3 100644--- a/.github/workflows/windows-no-compiler-ci.yml+++ b/.github/workflows/windows-no-compiler-ci.yml@@ -221,8 +221,9 @@ jobs:$seen.Compilers | ForEach-Object { Write-Host "COMPILER: $_" }throw ("the installer spawned $($seen.Compilers.Count) compiler process(es). This is " +"the chain that was blocked in the field: a windowless PowerShell launching " +- "csc.exe and dropping a DLL in TEMP. Declare native methods with " +- "New-StudioEmittedNativeType.")+ "csc.exe and dropping a DLL in TEMP. Nothing in the installer declares a " ++ "native method any more: reach the entry point through a child interpreter " ++ "and ctypes, as the path resolver and the GPU inventory do.")}if ($seen.TempLibraries) {$seen.TempLibraries | ForEach-Object { Write-Host "TEMP DLL: $_" }diff --git a/install.ps1 b/install.ps1index 241599b2077..e1cdb39fff2 100644--- a/install.ps1+++ b/install.ps1@@ -2168,439 +2168,28 @@ function Install-UnslothStudio {try { $defaultProfile = [Environment]::GetFolderPath("UserProfile") } catch {}$tauriProfile = if ($defaultProfile) { $defaultProfile } else { $env:USERPROFILE }- # Every native declaration in this script goes through here, never Add-Type.- #- # Add-Type on Windows PowerShell 5.1 (the interpreter the desktop app spawns) has- # no in-process compiler: -TypeDefinition and -MemberDefinition alike write C# to- # %TEMP% and run csc.exe, which security software blocks and which failed outright- # with CS2001 when %TEMP% was unusable (issue #9140). Reflection emit builds the- # same interop stubs in memory: no compiler process, no source, no DLL, empty- # assembly Location. Available on .NET Framework 4 and .NET 5+, so 5.1 and 7 take- # the same path. Which product blocked what:# tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)- #- # Throws rather than reporting: each caller wants a different answer to "the native- # side is unavailable", and the two cosmetic ones must not print the resolver's- # warning.- #- # Test-StudioCanDefineNativeTypes is the gate every caller checks first, rather than- # a try/catch, because App Control's Dynamic Code Security (policy option 19) blocks- # loading unsigned System.Reflection.Emit assemblies by usually stopping or crashing- # the parent instead of raising:- # learn.microsoft.com/en-us/windows/security/application-security/application-control/app-control-for-business/design/appcontrol-and-dotnet- # Such a machine puts PowerShell in Constrained Language, the first check and the one- # that fires in practice; the Device Guard probe covers a policy that left the- # language mode alone. When one IS active, a child process tries the emit rather than- # guessing which options the policy set.- $script:StudioCanDefineNativeTypes = $null- # Why the last probe answered as it did, so a caller can tell "the child ran and- # said no" (a policy) from "the child never answered" (failed to start, killed- # at the deadline, or lost its output). Same boolean, different facts.- $script:StudioEmitProbeOutcome = $null- function Test-StudioCanDefineNativeTypes {- if ($null -ne $script:StudioCanDefineNativeTypes) { return $script:StudioCanDefineNativeTypes }- $languageMode = "FullLanguage"- try { $languageMode = [string]$ExecutionContext.SessionState.LanguageMode } catch {}- if ($languageMode -ne "FullLanguage") {- $script:StudioCanDefineNativeTypes = $false- return $false- }- # Three outcomes, not two. Only a status that was READ and says 0 skips the- # probe; a query that threw, returned nothing, or lacked the property is- # UNKNOWN, and unknown must not mean unrestricted. Treating it as such lets- # through option 19 enforced on a host whose CIM query fails, costing a- # stopped installer; probing unnecessarily costs one short-lived process.- $known = $false- $active = $false- try {- # -OperationTimeoutSec bounds the CIM operation on a responsive target- # only: it does not interrupt DCOM connection setup, and a wedged- # provider's own timeout wins. Good for the slow case, not a hang guard.- # The installer already depends on CIM for adapter and process queries,- # so this adds no exposure; the child probe below carries the real- # deadline.- $guard = Get-CimInstance -Namespace "root\Microsoft\Windows\DeviceGuard" `- -ClassName "Win32_DeviceGuard" -OperationTimeoutSec 10 -ErrorAction Stop- # 0 off, 1 audit, 2 enforced. A null property is not a zero.- if ($guard -and $null -ne $guard.UsermodeCodeIntegrityPolicyEnforcementStatus) {- $known = $true- if ([int]$guard.UsermodeCodeIntegrityPolicyEnforcementStatus -ne 0) {- $active = $true- }- }- } catch {}- if ($known -and -not $active) {- $script:StudioCanDefineNativeTypes = $true- return $true- }- # A policy is active or unreadable, and WHICH policy decides this. Option 19- # Dynamic Code Security always blocks unsigned System.Reflection.Emit- # assemblies, with no audit mode on Windows 10 or Windows 11 before 24H2- # (enforced even in an audit policy); an audit policy WITHOUT that option- # emits fine. Win32_DeviceGuard does not report the option bit, so either- # guess costs a population: refusing sends every audit-mode machine down the- # lexical path, allowing risks the process. Ask the machine instead.- $script:StudioCanDefineNativeTypes = Test-StudioEmitInChildProcess- # One retry, only when the first attempt never reached an answer (the- # compiled version this replaces also tried twice before caching a- # negative). Otherwise one transient process failure is cached for the whole- # run as if it were a policy, sending the installer down the lexical path- # where two unequal roots compare as unknown and a second lock gets taken. A- # child that RAN and said no is not retried, so a blocked machine pays for- # one probe.- if (-not $script:StudioCanDefineNativeTypes -and- $script:StudioEmitProbeOutcome -eq "indeterminate") {- $script:StudioCanDefineNativeTypes = Test-StudioEmitInChildProcess- }- return $script:StudioCanDefineNativeTypes- }-- # The same emit, in a process that is allowed to die. A blocked dynamic load- # usually stops or crashes the parent, so doing this in-process would be the- # installer vanishing; a child that vanishes is just an answer. Silence is- # refusal, so an unspawnable probe lands on the lexical path.- function Test-StudioEmitInChildProcess {- # HostPath is for the tests, which have no policy to trigger the real path and- # cannot shadow the read-only $PSHOME. Production never passes it.- param([string]$HostPath)- # Until something below establishes otherwise.- $script:StudioEmitProbeOutcome = "indeterminate"- $probe = @'-try {- $name = New-Object System.Reflection.AssemblyName 'UnslothStudioEmitProbe'- $access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run- $assembly = $null- try { $assembly = [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly($name, $access) }- catch { $assembly = [AppDomain]::CurrentDomain.DefineDynamicAssembly($name, $access) }- $module = $assembly.DefineDynamicModule('UnslothStudioEmitProbe')- $builder = $module.DefineType('UnslothStudioEmitProbe', 'Public, Class, AutoClass, AnsiClass, BeforeFieldInit')- $method = $builder.DefinePInvokeMethod('CloseHandle', 'kernel32.dll', 'CloseHandle',- 'Public, Static, HideBySig, PinvokeImpl',- [System.Reflection.CallingConventions]::Standard, [bool], @([IntPtr]),- [System.Runtime.InteropServices.CallingConvention]::Winapi,- [System.Runtime.InteropServices.CharSet]::Ansi)- $method.SetImplementationFlags(- $method.GetMethodImplementationFlags() -bor [System.Reflection.MethodImplAttributes]::PreserveSig)- $null = $builder.CreateType()-} catch {}-# Outside the try, because CreateType can publish the type and then throw on the way-# back, and a published type works. The parent recovers from exactly that; a check-# inside the try answered no for a machine that had just succeeded.-# One line, and no closing brace in column 0: this body sits inside a here-string that-# starts at column 0 in both entrypoints, and the tests extract a function by finding the-# first line that is exactly its closing brace. A block here ends the extraction early.-if ('UnslothStudioEmitProbe' -as [type]) { Write-Output ('STUDIO_EMIT_OK ' + [string]$ExecutionContext.SessionState.LanguageMode); exit 0 }-exit 1-'@- # This host, not a guessed one: a 5.1 answer does not carry to pwsh or back.- # Both spellings of the leaf, so a non-Windows lane can execute this function- # end to end rather than leaving a Windows-only path untested.- $hostExe = $HostPath- if (-not $hostExe) {- try {- $leaves = if ($PSVersionTable.PSEdition -eq "Core") { @("pwsh.exe", "pwsh") }- else { @("powershell.exe", "powershell") }- foreach ($leaf in $leaves) {- $candidate = Join-Path $PSHOME $leaf- if (Test-Path -LiteralPath $candidate) { $hostExe = $candidate; break }- }- } catch {}- }- if (-not $hostExe) { return $false }- # A Process object rather than the call operator, for a deadline: the call- # operator waits forever, and forever is reachable (a security product- # inspecting a fresh interpreter, a wedged runtime start, a child blocking on- # shutdown). A probe meant to keep the installer alive must not hang it.- #- # BOTH streams are redirected and drained asynchronously. Draining stops a- # chatty child filling a pipe and deadlocking against the wait. Redirecting- # stderr keeps the probe out of the installer's own stderr, which the desktop- # app reads and anything the child spawns would inherit and hold open.- #- # The body is embedded in double quotes, safe only because it contains none,- # asserted by a test. See the note above.- $info = New-Object System.Diagnostics.ProcessStartInfo- $info.FileName = $hostExe- $info.Arguments = "-NoProfile -NonInteractive -Command `"$probe`""- $info.UseShellExecute = $false- $info.RedirectStandardOutput = $true- $info.RedirectStandardError = $true- $info.CreateNoWindow = $true- $child = $null- try {- $child = [System.Diagnostics.Process]::Start($info)- $reader = $child.StandardOutput.ReadToEndAsync()- $null = $child.StandardError.ReadToEndAsync()- if (-not $child.WaitForExit(20000)) {- try { $child.Kill() } catch {}- return $false- }- # Exit code AND an exact record. A marker followed by a crash is a crash:- # the question is whether this machine can emit and live. FullLanguage- # because an approved script can run in FullLanguage while a fresh inline- # command does not, and a child restricted differently from its parent- # has measured a different machine.- if ($child.ExitCode -ne 0) {- $script:StudioEmitProbeOutcome = "blocked"- return $false- }- $lines = ($reader.GetAwaiter().GetResult() -split "`r?`n")- foreach ($line in $lines) {- if ($line.Trim() -eq "STUDIO_EMIT_OK FullLanguage") {- $script:StudioEmitProbeOutcome = "ok"- return $true- }- # Emitted, but in a language mode this parent is not in: the child- # measured a different machine, which is an answer, not a miss.- if ($line.Trim() -like "STUDIO_EMIT_OK *") {- $script:StudioEmitProbeOutcome = "blocked"- return $false- }- }- # Exit 0 with no marker: the child cannot have emitted and reported- # nothing, so its output was lost rather than negative.- return $false- } catch {- return $false- } finally {- if ($child) {- # The read end goes first: a killed child can leave a grandchild- # holding the write end, and the pending async read would then keep- # this process alive past the deadline it just enforced.- try { $child.StandardOutput.Close() } catch {}- try { $child.StandardError.Close() } catch {}- try { $child.Dispose() } catch {}- }- }- }-- function New-StudioDynamicAssembly {- <#- Both spellings of "define a dynamic assembly", because the two PowerShell- hosts that run this file are on different runtimes. The static- AssemblyBuilder::DefineDynamicAssembly is documented for .NET Framework- 4.5 through 4.8.1 as well as .NET Core, so 5.1 should take the first- branch; it is tried rather than assumed because nothing here can test a- .NET Framework host and getting it wrong is invisible: the catch would- cache the resolver as unavailable and every desktop install would use the- lexical fallback, which can give a long path and its 8.3 alias different- mutex names.-- AppDomain.CurrentDomain.DefineDynamicAssembly is the .NET Framework- spelling and is absent on .NET Core, so it is the fallback; pwsh would- fail on it.- #>- param([Parameter(Mandatory = $true)][System.Reflection.AssemblyName]$AssemblyName)- $access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run- try {- return [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly(- $AssemblyName, $access)- } catch [System.Management.Automation.MethodException] {- return [AppDomain]::CurrentDomain.DefineDynamicAssembly($AssemblyName, $access)- } catch [System.Management.Automation.RuntimeException] {- # Some hosts surface a missing static as RuntimeException, not- # MethodException. Both mean "no such method here", and a real emit- # failure (Dynamic Code Security, Constrained Language) throws from the- # AppDomain call too, so the caller still sees it.- return [AppDomain]::CurrentDomain.DefineDynamicAssembly($AssemblyName, $access)- }- }- function New-StudioEmittedNativeType {- param(- [Parameter(Mandatory = $true)][string]$TypeName,- # @{ Name = "CloseHandle"; Library = "kernel32.dll"; Return = [bool]- # Args = @([IntPtr]) }- [Parameter(Mandatory = $true)][object[]]$Imports- )- $assemblyName = New-Object System.Reflection.AssemblyName $TypeName- $assembly = New-StudioDynamicAssembly -AssemblyName $assemblyName- $module = $assembly.DefineDynamicModule($TypeName)- $builder = $module.DefineType(- $TypeName, "Public, Class, AutoClass, AnsiClass, BeforeFieldInit")-- $winapi = [System.Runtime.InteropServices.CallingConvention]::Winapi- # Per import, because CharSet selects name mangling as well as marshalling:- # Unicode probes <Name>W before <Name>, Ansi probes <Name> before <Name>A.- # Every import names an export that exists exactly as written, so both- # orders arrive; matching the C# these replace keeps the metadata honest and- # tries the existing export first. Unicode is the default, since the calls- # carrying text are already spelled W.- $unicode = [System.Runtime.InteropServices.CharSet]::Unicode- $ansi = [System.Runtime.InteropServices.CharSet]::Ansi- $standard = [System.Reflection.CallingConventions]::Standard- $attributes = "Public, Static, HideBySig, PinvokeImpl"- $preserveSig = [System.Reflection.MethodImplAttributes]::PreserveSig-- foreach ($import in $Imports) {- $charSet = if ($import.ContainsKey("Ansi") -and $import.Ansi) { $ansi } else { $unicode }- $method = $builder.DefinePInvokeMethod(- $import.Name, $import.Library, $import.Name, $attributes,- $standard, $import.Return, $import.Args, $winapi, $charSet)- $method.SetImplementationFlags(- $method.GetMethodImplementationFlags() -bor $preserveSig)- # `out uint` in the C# this replaces; DefinePInvokeMethod cannot say so,- # since a by-ref type alone emits `ref` (In and Out unset). The value is- # blittable and every caller initialises it, so marshalling works either- # way, but the metadata is what a reader and any future marshalling- # change go by.- # ContainsKey, not a bare property read: most imports have no Out key and- # reading a missing one is fatal under Set-StrictMode. install.ps1 turns- # strict mode off for itself, studio/setup.ps1 inherits the caller's, so- # the guard is mirrored rather than left to one of them.- if ($import.ContainsKey("Out")) {- foreach ($position in @($import.Out)) {- if ($position) { $null = $method.DefineParameter($position, "Out", $null) }- }- }- }- $null = $builder.CreateType()- return $null -ne ($TypeName -as [type])- }-- # GetFinalPathNameByHandleW is the only exact answer for a path: it follows- # junctions, symlinks and SUBST drives, expands 8.3 aliases and reports the- # on-disk spelling, none of which GetFullPath does. Cached, since callers- # resolve dozens of paths; where unavailable, Get-StudioLexicalPath carries the- # run.- $script:StudioFinalPathNativeState = $null- # Reset with the rest: under `irm | iex` these are the caller's own.- $script:StudioNativeResolveWarned = $false$script:StudioFinalPathWarned = $false+ $script:StudioInstallIsFresh = $nullfunction Write-StudioFinalPathDegraded {param([string]$Reason)if ($script:StudioFinalPathWarned) { return }$script:StudioFinalPathWarned = $true+ if ($script:StudioInstallIsFresh -eq $true) {+ Write-Verbose "Unsloth: path identity is inexact ($Reason); first install, taking every runtime lock."+ return+ }# This used to promise "installation is unaffected", which it cannot know:# the same security software that blocks a type can be acting on the rest of# the run. Only the narrower claim is true, that the installer can continue.- Write-StudioLine "[WARN] Could not load the native path resolver ($Reason)." -ForegroundColor Yellow- Write-StudioLine " Continuing with the Python resolver when an interpreter can answer, else the" -ForegroundColor Yellow- Write-StudioLine " PowerShell one, which cannot recover a path's stored casing or expand an 8.3" -ForegroundColor Yellow- Write-StudioLine " name, so it compares paths as written." -ForegroundColor Yellow+ Write-StudioLine "[WARN] Could not resolve a path exactly ($Reason)." -ForegroundColor Yellow+ Write-StudioLine " Continuing with the lexical resolver, which cannot recover a path's" -ForegroundColor Yellow+ Write-StudioLine " stored casing or expand an 8.3 name, so paths are compared as written." -ForegroundColor Yellow+ Write-StudioLine " Unsloth will take every runtime lock it might need rather than assume" -ForegroundColor Yellow+ Write-StudioLine " two spellings are different directories." -ForegroundColor Yellow}- function Initialize-StudioFinalPathNativeType {- if ("UnslothStudioFinalPathV3" -as [type]) {- $script:StudioFinalPathNativeState = $true- return $true- }- if ($null -ne $script:StudioFinalPathNativeState) { return $script:StudioFinalPathNativeState }- # Constrained Language Mode forbids defining types at all, by emit as by- # Add-Type, so compiling would only produce a second, less honest error.- if (-not (Test-StudioCanDefineNativeTypes)) {- $script:StudioFinalPathNativeState = $false- $languageMode = "FullLanguage"- try { $languageMode = [string]$ExecutionContext.SessionState.LanguageMode } catch {}- # Three reasons, because the gate has three ways to say no and only one- # is a policy. Blaming code integrity for a probe that could not be- # spawned, or was killed at its deadline, writes a machine setting into- # the support log that the user would go looking for and not find.- $reason = if ($languageMode -ne "FullLanguage") { "PowerShell is in $languageMode" }- elseif ($script:StudioEmitProbeOutcome -eq "blocked") {- "this host enforces user-mode code integrity"- } else { "a probe process could not confirm native type support" }- Write-StudioFinalPathDegraded -Reason $reason- return $false- }- # Everything below the capability check is unchanged, so a host where the- # emit fails degrades exactly as one that could not compile already did.- #- # DefinePInvokeMethod cannot ask for SetLastError, so nothing below reads- # GetLastWin32Error. The C# it replaces threw a Win32Exception that callers- # only turned back into "use the lexical answer", so returning null loses a- # code nothing acted on.- try {- $null = New-StudioEmittedNativeType -TypeName "UnslothStudioFinalPathV3" -Imports @(- @{ Name = "CreateFileW"; Library = "kernel32.dll"; Return = [IntPtr]- Args = @([string], [uint32], [uint32], [IntPtr], [uint32], [uint32], [IntPtr]) },- @{ Name = "GetFinalPathNameByHandleW"; Library = "kernel32.dll"; Return = [uint32]- Args = @([IntPtr], [System.Text.StringBuilder], [uint32], [uint32]) },- @{ Name = "CloseHandle"; Library = "kernel32.dll"; Return = [bool]- Args = @([IntPtr])- Ansi = $true }- )- } catch {- # A throw does not mean nothing was defined: CreateType can publish the- # type and then fail on the way back, and the compiled version this- # replaces checked for that too. A published type is usable, so ask- # before caching the negative.- if ("UnslothStudioFinalPathV3" -as [type]) {- $script:StudioFinalPathNativeState = $true- return $true- }- $script:StudioFinalPathNativeState = $false- Write-StudioFinalPathDegraded -Reason (($_.Exception.Message -split "`r?`n")[0].Trim())- return $false- }- if ("UnslothStudioFinalPathV3" -as [type]) {- $script:StudioFinalPathNativeState = $true- return $true- }- $script:StudioFinalPathNativeState = $false- Write-StudioFinalPathDegraded -Reason "the native path resolver could not be defined"- return $false- }-- # GetFinalPathNameByHandleW is the only exact answer: it follows junctions,- # symlinks and SUBST drives, expands 8.3 aliases and reports the on-disk- # spelling, none of which GetFullPath does.- #- # What the compiled Resolve() did, moved out of C# so the imports above are all- # the native code there is. Same flags, same two-pass buffer growth. Null rather- # than an exception on failure: every caller already treats "no exact answer" as- # "use the lexical one".- function Get-StudioNativeFinalPath {- param([Parameter(Mandatory = $true)][string]$Path)-- $invalidHandle = [IntPtr](-1)- $fileShareAll = [uint32]7 # READ | WRITE | DELETE- $openExisting = [uint32]3- $backupSemantics = [uint32]0x02000000 # required to open a DIRECTORY-- # Every native call is guarded: an emitted stub binds its import on first CALL,- # not at definition, so a host missing the export raises here rather than above,- # and the caller must see the same null "no exact answer" a failed open gives.- # Acquisition sits INSIDE the region that closes the handle, because the- # SafeFileHandle the C# returned had a finalizer as a backstop and an IntPtr has- # none. What is left is the instant between the native return and the assignment,- # which no PowerShell arrangement can close and which costs nothing: opened with- # desired access 0 and FILE_SHARE_READ|WRITE|DELETE, even a leaked handle blocks- # no other opener and dies with the process.- $handle = $invalidHandle- try {- try {- $handle = [UnslothStudioFinalPathV3]::CreateFileW(- $Path, [uint32]0, $fileShareAll, [IntPtr]::Zero,- $openExisting, $backupSemantics, [IntPtr]::Zero)- } catch {- return $null- }- if ($handle -eq $invalidHandle -or $handle -eq [IntPtr]::Zero) { return $null }- $buffer = New-Object System.Text.StringBuilder 512- $length = [UnslothStudioFinalPathV3]::GetFinalPathNameByHandleW(- $handle, $buffer, [uint32]$buffer.Capacity, [uint32]0)- if ($length -eq 0) { return $null }- if ($length -ge $buffer.Capacity) {- $buffer = New-Object System.Text.StringBuilder ([int]$length + 1)- $length = [UnslothStudioFinalPathV3]::GetFinalPathNameByHandleW(- $handle, $buffer, [uint32]$buffer.Capacity, [uint32]0)- if ($length -eq 0) { return $null }- }- # Still short is the only answer worth trusting.- if ($length -ge $buffer.Capacity) { return $null }- return $buffer.ToString()- } catch {- return $null- } finally {- # Guarded now that the open is inside this region: failure paths reach- # here with the sentinel, which there is no reason to ask Windows to- # close.- if ($handle -ne $invalidHandle -and $handle -ne [IntPtr]::Zero) {- try { [void][UnslothStudioFinalPathV3]::CloseHandle($handle) } catch {}- }- }- }function Resolve-StudioLinkTarget {param([Parameter(Mandatory = $true)][string]$Path)@@ -2760,15 +2349,149 @@ exit 1$script:StudioEarlyPython = $null$script:StudioEarlyPythonProbedWithoutVenv = $false- function Get-StudioEarlyPython {- # This optional pre-lock probe has no ACL/ownership validation for candidates.- # Elevated or uninspectable Windows tokens retain the old resolver ladder.+ function Test-StudioSddlRightsAreWrite {+ param([string]$Rights)+ if ([string]::IsNullOrWhiteSpace($Rights)) { return $true }+ $text = "$Rights".Trim().ToUpper()+ if ($text -like "0X*") {+ $mask = [long]0+ foreach ($ch in $text.Substring(2).ToCharArray()) {+ $digit = "0123456789ABCDEF".IndexOf($ch)+ if ($digit -lt 0) { return $true }+ $mask = ($mask * 16) + $digit+ }+ return (($mask -band 0x500D0156) -ne 0)+ }+ foreach ($alias in @("GA", "GW", "WD", "WO", "SD", "DT", "FA", "FW", "KA", "KW",+ "CC", "DC", "WP", "SW")) {+ if ($text.Contains($alias)) { return $true }+ }+ return $false+ }++ function Test-StudioSddlPrincipalIsAdminOnly {+ param([string]$Principal)+ if ([string]::IsNullOrWhiteSpace($Principal)) { return $false }+ $who = "$Principal".Trim().ToUpper()+ return (@(+ "BA", "SY", "S-1-5-32-544", "S-1-5-18",+ "S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464"+ ) -contains $who)+ }++ function Test-StudioSddlWritableByNonAdmin {+ param([string]$Sddl)+ if ([string]::IsNullOrWhiteSpace($Sddl)) { return $true }+ $text = "$Sddl".Trim()+ if (-not ($text -match '^O:([A-Za-z0-9\-]+?)(?:G:|D:|S:|$)')) { return $true }+ if (-not (Test-StudioSddlPrincipalIsAdminOnly -Principal $Matches[1])) { return $true }+ $daclAt = $text.IndexOf("D:")+ if ($daclAt -lt 0) { return $true }+ $dacl = $text.Substring($daclAt)+ $saclAt = $dacl.IndexOf("S:")+ if ($saclAt -ge 0) { $dacl = $dacl.Substring(0, $saclAt) }+ $seen = 0+ foreach ($chunk in ($dacl -split '\)')) {+ $open = $chunk.IndexOf("(")+ if ($open -lt 0) { continue }+ $seen+++ $fields = $chunk.Substring($open + 1) -split ';'+ if ($fields.Count -lt 6) { return $true }+ $type = "$($fields[0])".Trim().ToUpper()+ $flags = "$($fields[1])".Trim().ToUpper()+ if (@("D", "OD", "XD") -contains $type) { continue }+ if ($flags.Contains("IO")) { continue }+ if (-not (Test-StudioSddlRightsAreWrite -Rights $fields[2])) { continue }+ if (-not (Test-StudioSddlPrincipalIsAdminOnly -Principal $fields[5])) { return $true }+ }+ # A DACL with no ACEs in it is not evidence of anything, and neither is one this did not+ # manage to split.+ if ($seen -eq 0) { return $true }+ return $false+ }++ function Test-StudioDirectoryIsAdminOnly {+ param([string]$Path)+ if ([string]::IsNullOrWhiteSpace($Path)) { return $false }+ $sddl = ""try {- $elevation = Get-ElevationState- if ($elevation -eq "true" -or- ($elevation -ne "false" -and- [Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT)) { return $null }- } catch { return $null }+ # Select-Object -ExpandProperty, not a property read: Constrained Language Mode refuses+ # property access on types outside its allowed list, and the security descriptor is one.+ $sddl = "$(Get-Acl -LiteralPath $Path -ErrorAction Stop |+ Select-Object -ExpandProperty Sddl)"+ } catch { return $false }+ if ([string]::IsNullOrWhiteSpace($sddl)) { return $false }+ return (-not (Test-StudioSddlWritableByNonAdmin -Sddl $sddl))+ }++ function Get-StudioLexicalParent {+ param([string]$Path)+ $trimmed = "$Path".TrimEnd('\', '/')+ $cut = $trimmed.LastIndexOfAny(@([char]92, [char]47))+ if ($cut -lt 0) { return "" }+ return $trimmed.Substring(0, $cut)+ }++ function Test-StudioPathUnderAdminRoot {+ param([string]$Path)+ if ([string]::IsNullOrWhiteSpace($Path)) { return $false }+ # $env: rather than [Environment]::GetEnvironmentVariable: System.Environment is not on+ # Constrained Language Mode's allowed type list, and this helper runs on hosts under it.+ $roots = @()+ foreach ($value in @($env:SystemRoot, $env:ProgramFiles, $env:ProgramW6432, ${env:ProgramFiles(x86)})) {+ if (-not [string]::IsNullOrWhiteSpace($value)) { $roots += "$value".TrimEnd('\', '/') }+ }+ $matchedRoot = ""+ foreach ($root in $roots) {+ $escapedRoot = $root -replace '([\[\]\*\?])', '`$1'+ if (("$Path" -like ($escapedRoot + "\*")) -or ("$Path" -like ($escapedRoot + "/*"))) {+ $matchedRoot = $root+ break+ }+ }+ if ([string]::IsNullOrWhiteSpace($matchedRoot)) { return $false }+ $windowsRoot = "$($env:SystemRoot)".TrimEnd('\', '/')+ if (-not [string]::IsNullOrWhiteSpace($windowsRoot)) {+ foreach ($leaf in @(+ "Temp", "Tasks", "Tracing", "Registration\CRMLog", "debug\WIA",+ "System32\Tasks", "System32\spool\drivers\color", "System32\spool\PRINTERS",+ "System32\spool\SERVERS", "System32\com\dmp", "System32\FxsTmp",+ "SysWOW64\Tasks", "SysWOW64\com\dmp", "SysWOW64\FxsTmp"+ )) {+ $writable = $windowsRoot + "\" + $leaf+ $escapedWritable = $writable -replace '([\[\]\*\?])', '`$1'+ if (("$Path" -like ($escapedWritable + "\*")) -or+ ("$Path" -like ($escapedWritable + "/*"))) { return $false }+ }+ }+ $current = Get-StudioLexicalParent -Path "$Path"+ $guard = 0+ while (-not [string]::IsNullOrWhiteSpace($current)) {+ $guard+++ if ($guard -gt 64) { return $false }+ if (-not (Test-StudioDirectoryIsAdminOnly -Path $current)) { return $false }+ if ("$current".TrimEnd('\', '/') -eq $matchedRoot) { return $true }+ $next = Get-StudioLexicalParent -Path "$current"+ if ("$next" -eq "$current") { return $false }+ $current = $next+ }+ return $false+ }++ function Test-StudioInterpreterFileIsAdminOnly {+ param([string]$Path)+ if ([string]::IsNullOrWhiteSpace($Path)) { return $false }+ try {+ $item = Get-Item -LiteralPath $Path -Force -ErrorAction Stop |+ Select-Object -Property Attributes, LinkType+ if ($null -eq $item) { return $false }+ if ("$($item.Attributes)" -match 'ReparsePoint') { return $false }+ if (-not [string]::IsNullOrWhiteSpace("$($item.LinkType)")) { return $false }+ } catch { return $false }+ return (Test-StudioDirectoryIsAdminOnly -Path $Path)+ }++ function Get-StudioEarlyPython {# A miss taken before $VenvDir existed (--tauri) is re-probed once it does.$venvDirValue = $nulltry { $venvDirValue = Get-Variable -Name VenvDir -ValueOnly -ErrorAction SilentlyContinue } catch {}@@ -2783,25 +2506,10 @@ exit 1$script:StudioEarlyPythonProbedWithoutVenv = (-not $venvKnown)if ("$($env:UNSLOTH_EARLY_PYTHON_PROBE)".Trim() -eq "0") { return $null }$candidates = @()+ $venvCandidates = @()if ($venvKnown) {- # A custom UNSLOTH_STUDIO_HOME can be a shared root, and the guard refusing a venv that is- # not Unsloth's runs later: only run one here that the guard would accept.- $venvOurs = $false- try {- $mode = Get-Variable -Name StudioRedirectMode -ValueOnly -ErrorAction Stop- $studioHomeValue = Get-Variable -Name StudioHome -ValueOnly -ErrorAction Stop- # The guard's own predicates; one not yet defined this early throws, which declines.- $venvOurs = ($mode -ne 'env') -or- (Test-Path -LiteralPath (Join-Path $venvDirValue ".unsloth-studio-owned") -PathType Leaf) -or- (Test-Path -LiteralPath (Join-Path $studioHomeValue "share\studio.conf") -PathType Leaf) -or- (Test-Path -LiteralPath (Join-Path $studioHomeValue "bin\unsloth.exe") -PathType Leaf) -or- (Test-StudioPlainFile -Path (Join-Path $studioHomeValue ".unsloth-studio-owned")) -or- (Test-UnslothCmdShimFile (Join-Path $studioHomeValue "bin\unsloth.cmd"))- } catch { $venvOurs = $false }- if ($venvOurs) {- $candidates += (Join-Path $venvDirValue "Scripts\python.exe")- $candidates += (Join-Path $venvDirValue "bin/python3")- }+ $venvCandidates = @((Join-Path $venvDirValue "Scripts\python.exe"), (Join-Path $venvDirValue "bin/python3"))+ $candidates += $venvCandidates}foreach ($name in @("python3", "python")) {try {@@ -2810,12 +2518,49 @@ exit 1}} catch {}}+ $requireAdminRoot = $false+ if ($env:OS -eq "Windows_NT") {+ try { $requireAdminRoot = Test-StudioChildScriptDirectoryElevated } catch { $requireAdminRoot = $true }+ }+ $rejectedForWritability = $falseforeach ($candidate in $candidates) {if ([string]::IsNullOrWhiteSpace($candidate)) { continue }# Test-Path throws on an unreadable dir under Stop: skip this candidate only.$isFile = $falsetry { $isFile = Test-Path -LiteralPath $candidate -PathType Leaf } catch {}if (-not $isFile) { continue }+ # The venv interpreter runs here before the destination guard does, so only where that+ # guard would let it run: the default layout, or a custom home that is already Unsloth's.+ if ($venvCandidates -contains $candidate) {+ $ours = $false+ try {+ $mode = Get-Variable -Name StudioRedirectMode -ValueOnly -ErrorAction Stop+ $studioHomeValue = Get-Variable -Name StudioHome -ValueOnly -ErrorAction Stop+ # The guard's own predicates; one not yet defined this early throws, which declines.+ $ours = ($mode -ne 'env') -or+ (Test-Path -LiteralPath (Join-Path $venvDirValue ".unsloth-studio-owned") -PathType Leaf) -or+ (Test-Path -LiteralPath (Join-Path $studioHomeValue "share\studio.conf") -PathType Leaf) -or+ (Test-Path -LiteralPath (Join-Path $studioHomeValue "bin\unsloth.exe") -PathType Leaf) -or+ (Test-StudioPlainFile -Path (Join-Path $studioHomeValue ".unsloth-studio-owned")) -or+ (Test-UnslothCmdShimFile (Join-Path $studioHomeValue "bin\unsloth.cmd"))+ } catch { $ours = $false }+ if (-not $ours) {+ if ($requireAdminRoot) { $rejectedForWritability = $true }+ continue+ }+ }+ if ($requireAdminRoot -and ($venvCandidates -notcontains $candidate)) {+ $adminOnly = $false+ try {+ $adminOnly = (Test-StudioPathUnderAdminRoot -Path $candidate) -and+ (Test-StudioInterpreterFileIsAdminOnly -Path $candidate)+ } catch { $adminOnly = $false }+ if (-not $adminOnly) {+ $rejectedForWritability = $true+ continue+ }+ }+ if ("$candidate" -match '(?i)[\\/]Microsoft[\\/]WindowsApps[\\/]') { continue }# Probe the interpreter's own directory: $PSScriptRoot is empty when the script runs from memory.$probeDir = $nulltry { $probeDir = [System.IO.Path]::GetDirectoryName($candidate) } catch {}@@ -2826,6 +2571,10 @@ exit 1return $candidate}}+ if ($rejectedForWritability) {+ Write-StudioFinalPathDegraded -Reason ("this run is elevated and the only interpreters found " ++ "are in directories a standard user can write, which an elevated run must not launch")+ }return $null}@@ -2865,8 +2614,9 @@ exit 1$psi = New-Object System.Diagnostics.ProcessStartInfo$psi.FileName = $Exe# -S as well as -I: -I still imports site, so a sitecustomize could print or hang.+ # -B: these probes can run before the install lock, so they must write no .pyc.# ArgumentList is .NET Core only; Windows PowerShell 5.1 lacks it.- $argv = @("-I", "-S", "-c", $Script) + $ScriptArgs+ $argv = @("-I", "-S", "-B", "-c", $Script) + $ScriptArgsif ($null -ne $psi.PSObject.Properties["ArgumentList"]) {foreach ($a in $argv) { $null = $psi.ArgumentList.Add($a) }} else {@@ -2900,7 +2650,147 @@ exit 1}}- # One child per distinct path (misses cached too).++ function Get-StudioSystem32Tool {+ param([Parameter(Mandatory = $true)][string]$Name)+ if ([string]::IsNullOrWhiteSpace($env:SystemRoot)) { return "" }+ $candidate = Join-Path (Join-Path $env:SystemRoot "System32") $Name+ if (-not (Test-Path -LiteralPath $candidate -PathType Leaf)) { return "" }+ return $candidate+ }++ function Invoke-StudioSystem32ToolBounded {+ param([string]$Exe, [string[]]$Arguments = @(), [int]$TimeoutMs = 3000)+ # CLM cannot construct ProcessStartInfo. A job captures the trusted system tool's+ # output through PowerShell's pipes, without redirect files in an unlabelled directory.+ if ("$($ExecutionContext.SessionState.LanguageMode)" -ne "FullLanguage") {+ $job = $null+ try {+ $seconds = ($TimeoutMs - ($TimeoutMs % 1000)) / 1000+ if (($TimeoutMs % 1000) -ne 0) { $seconds++ }+ if ($seconds -lt 1) { $seconds = 1 }+ $job = Start-Job -ScriptBlock {+ param($tool, $toolArgs)+ $lines = @(& $tool @toolArgs 2>$null)+ return @{ Output = ($lines -join "`n"); ExitCode = $LASTEXITCODE }+ } -ArgumentList $Exe, (,$Arguments)+ if (Wait-Job -Job $job -Timeout $seconds) {+ return (Receive-Job -Job $job -ErrorAction SilentlyContinue | Select-Object -Last 1)+ }+ Stop-Job -Job $job -ErrorAction SilentlyContinue+ } catch { return $null }+ finally { if ($job) { Remove-Job -Job $job -Force -ErrorAction SilentlyContinue } }+ return $null+ }+ $proc = $null+ try {+ # If policy forbids these types, decline. The caller treats an unreadable+ # token as elevated and uses the inline probe when no private directory is safe.+ $clock = [System.Diagnostics.Stopwatch]::StartNew()+ $psi = New-Object System.Diagnostics.ProcessStartInfo+ $psi.FileName = $Exe+ if ($null -ne $psi.PSObject.Properties['ArgumentList']) {+ foreach ($arg in $Arguments) { $null = $psi.ArgumentList.Add($arg) }+ } else {+ # The arguments here are paths and fixed switches; paths cannot contain quotes.+ $psi.Arguments = (@($Arguments | ForEach-Object {+ '"' + ($_ -replace '(\\+)$', '$1$1') + '"'+ }) -join ' ')+ }+ $psi.UseShellExecute = $false+ $psi.RedirectStandardOutput = $true+ $psi.RedirectStandardError = $true+ $psi.CreateNoWindow = $true+ $proc = [System.Diagnostics.Process]::Start($psi)+ $stdout = $proc.StandardOutput.ReadToEndAsync()+ $null = $proc.StandardError.ReadToEndAsync()+ if (-not $proc.WaitForExit($TimeoutMs)) {+ try { $proc.Kill() } catch { }+ return $null+ }+ $left = [Math]::Max(0, $TimeoutMs - [int]$clock.ElapsedMilliseconds)+ if (-not $stdout.Wait($left)) { return $null }+ return @{ Output = "$($stdout.Result)"; ExitCode = $proc.ExitCode }+ } catch { return $null }+ finally { if ($proc) { try { $proc.Dispose() } catch { } } }+ }++ function Test-StudioChildScriptDirectoryElevated {+ $groups = ""+ $whoami = Get-StudioSystem32Tool -Name "whoami.exe"+ if (-not $whoami) { return $true }+ try {+ $result = Invoke-StudioSystem32ToolBounded -Exe $whoami -Arguments @('/groups')+ if ($null -eq $result) { return $true }+ $groups = "$($result.Output)"+ } catch { return $true }+ if ([string]::IsNullOrWhiteSpace($groups)) { return $true }+ if ($groups -match "S-1-16-(12288|16384)") { return $true }+ if ($groups -match "S-1-16-\d+") { return $false }+ return $true+ }++ function New-StudioChildScriptDirectory {+ $tempRoot = if ($env:TEMP) { $env:TEMP } elseif ($env:TMPDIR) { $env:TMPDIR } else { "/tmp" }+ # Join-Path throws on a TEMP naming a missing drive: decline, like any other unusable root.+ try { $dir = Join-Path $tempRoot ("unsloth-child-" + [guid]::NewGuid().ToString("N")) -ErrorAction Stop } catch { return "" }+ $made = $false+ try {+ $createdPath = "$(New-Item -ItemType Directory -Path $dir -ErrorAction Stop |+ Select-Object -ExpandProperty FullName)"+ $made = (Test-Path -LiteralPath $dir -PathType Container)+ if ((-not $made) -and $createdPath) {+ Remove-Item -LiteralPath $createdPath -Recurse -Force -ErrorAction SilentlyContinue+ }+ } catch { }+ if ((-not $made) -and $dir -match '[\[\]]') {+ $escaped = $dir -replace '([\[\]])', '`$1'+ try {+ $null = New-Item -ItemType Directory -Path $escaped -ErrorAction Stop+ $made = (Test-Path -LiteralPath $dir -PathType Container)+ } catch { }+ }+ # Created by THIS call. New-Item without -Force throws on a directory that already exists,+ # which is the point: a pre-created one carrying an attacker's ACL is refused, not adopted.+ if (-not $made) { return "" }+ if ($env:OS -eq "Windows_NT") {+ # Until it is labelled a standard user can swap this directory for a junction: a link is+ # never adopted, and cleanup never recurses through one into its target.+ $isLink = { try { "$((Get-Item -LiteralPath $dir -Force -ErrorAction Stop).Attributes)" -match 'ReparsePoint' } catch { $true } }+ $labelled = $false+ $icacls = Get-StudioSystem32Tool -Name "icacls.exe"+ if (-not $icacls) {+ if (-not (& $isLink)) { try { Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue } catch { } }+ return ""+ }+ try {+ $result = Invoke-StudioSystem32ToolBounded -Exe $icacls -Arguments @($dir, '/setintegritylevel', '(OI)(CI)H')+ $labelled = ($null -ne $result -and $result.ExitCode -eq 0)+ if (-not $labelled -and $null -ne $result) {+ $readback = Invoke-StudioSystem32ToolBounded -Exe $icacls -Arguments @($dir)+ $labelled = ($null -ne $readback -and "$($readback.Output)" -match "S-1-16-12288|High Mandatory Level")+ }+ } catch { $labelled = $false }+ if ((-not $labelled) -and (Test-StudioChildScriptDirectoryElevated)) {+ if (-not (& $isLink)) { try { Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue } catch { } }+ return ""+ }+ if (& $isLink) { return "" }+ if ($labelled) {+ $planted = $true+ try {+ $planted = @(Get-ChildItem -LiteralPath $dir -Force -ErrorAction Stop).Count -ne 0+ } catch { $planted = $true }+ if ($planted) {+ if (-not (& $isLink)) { try { Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue } catch { } }+ return ""+ }+ }+ }+ return $dir+ }++ # One child per distinct path: the process scan asks for the same image paths repeatedly.$script:StudioPythonFinalPathCache = $nullfunction Get-StudioPythonFinalPath {@@ -2918,7 +2808,100 @@ exit 1return $answer}- # Shortcut icon refresh via a child interpreter when the type cannot be defined. Cosmetic: never throws.+ function Resolve-StudioFinalPathsInOneChild {+ param([string[]]$Paths = @())+ if ($null -eq $script:StudioPythonFinalPathCache) { $script:StudioPythonFinalPathCache = @{} }+ $wanted = @()+ foreach ($candidate in $Paths) {+ if ([string]::IsNullOrWhiteSpace($candidate)) { continue }+ if ($script:StudioPythonFinalPathCache.ContainsKey($candidate)) { continue }+ if ($wanted -notcontains $candidate) { $wanted += $candidate }+ }+ if ($wanted.Count -eq 0) { return }+ $exe = $null+ try { $exe = Get-StudioEarlyPython } catch {}+ if (-not $exe) { return }+ $listDir = New-StudioChildScriptDirectory+ $listFile = $null+ if ($listDir) {+ $listFile = Join-Path $listDir "paths.txt"+ try { Set-Content -LiteralPath $listFile -Value $wanted -Encoding UTF8 -ErrorAction Stop }+ catch { Remove-Item -LiteralPath $listDir -Recurse -Force -ErrorAction SilentlyContinue; $listDir = $null; $listFile = $null }+ }+ # Without a private directory the list rides the environment, in chunks under its 32767-character+ # block, so a declined directory does not fall back to one child per process.+ $batches = @()+ if ($listFile) { $batches += ,@{ Args = @($listFile); Env = $null } }+ else {+ $chunk = ""+ foreach ($candidate in $wanted) {+ if ($chunk -and ($chunk.Length + $candidate.Length + 1) -gt 30000) { $batches += ,@{ Args = @(); Env = $chunk }; $chunk = "" }+ $chunk = if ($chunk) { $chunk + "`n" + $candidate } else { $candidate }+ }+ if ($chunk) { $batches += ,@{ Args = @(); Env = $chunk } }+ }+ $probe = "import os,pathlib,sys" + [char]10 ++ "sys.exit(2) if sys.version_info < (3,8) else None" + [char]10 ++ "out=[]" + [char]10 ++ "if len(sys.argv)>1:" + [char]10 ++ " with open(sys.argv[1],'r',encoding='utf-8-sig') as fh: lines=fh.read().split('\n')" + [char]10 ++ "else:" + [char]10 ++ " lines=os.environ.get('UNSLOTH_FINAL_PATHS','').split('\n')" + [char]10 ++ "for line in lines:" + [char]10 ++ " p=line.rstrip('\r\n')" + [char]10 ++ " if not p: continue" + [char]10 ++ " try:" + [char]10 ++ " out.append(p+'|'+str(pathlib.Path(p).resolve(strict=True)))" + [char]10 ++ " except Exception:" + [char]10 ++ " out.append(p+'|')" + [char]10 ++ "sys.stdout.buffer.write('\n'.join(out).encode('utf-8'))"+ $raw = ""+ $savedPaths = $env:UNSLOTH_FINAL_PATHS+ try {+ foreach ($batch in $batches) {+ if ($null -ne $batch.Env) { $env:UNSLOTH_FINAL_PATHS = $batch.Env }+ try { $raw += "`n" + (Invoke-StudioEarlyPythonScript -Exe $exe -Script $probe -ScriptArgs $batch.Args -TimeoutMs 30000) } catch { }+ }+ } finally {+ if ($null -eq $savedPaths) { Remove-Item Env:UNSLOTH_FINAL_PATHS -ErrorAction SilentlyContinue }+ else { $env:UNSLOTH_FINAL_PATHS = $savedPaths }+ if ($listDir) { Remove-Item -LiteralPath $listDir -Recurse -Force -ErrorAction SilentlyContinue }+ }+ $answers = @{}+ $reported = @{}+ foreach ($line in ("$raw" -split "`r?`n")) {+ if ([string]::IsNullOrWhiteSpace($line)) { continue }+ # The first separator, because the pipe is not legal in a Windows path so the left+ # half cannot contain one. $requested, not $input: $input is an automatic variable.+ $split = $line.IndexOf('|')+ if ($split -lt 1) { continue }+ $requested = $line.Substring(0, $split)+ $answer = $line.Substring($split + 1)+ $reported[$requested] = $true+ if ([string]::IsNullOrWhiteSpace($answer)) { continue }+ if (-not (Split-Path -IsAbsolute $answer)) { continue }+ if (-not (Test-Path -LiteralPath $answer)) { continue }+ $answers[$requested] = $answer+ }+ foreach ($candidate in $wanted) {+ if ($answers.ContainsKey($candidate)) {+ $script:StudioPythonFinalPathCache[$candidate] = $answers[$candidate]+ } elseif ($reported.ContainsKey($candidate)) {+ # Asked, and the child said it cannot be resolved. Recorded as $null rather than+ # left absent, so the miss is not re-asked once per protected root.+ $script:StudioPythonFinalPathCache[$candidate] = $null+ }+ # Neither: the child answered, but not about this path. Left uncached, so the+ # single-path rung still gets its turn.+ }+ }++ # Tell Explorer a shortcut was rewritten, through a child interpreter. Used only where the+ # type cannot be defined in this shell, which is where the refresh silently did not happen+ # before. Returns $true when the child reported success.+ #+ # Cosmetic either way: the worst case is a stale icon on a shortcut that works. Nothing here+ # may fail the install, so every path returns rather than throws.function Invoke-StudioPythonShellIconRefresh {param([string[]]$Paths = @(), [string]$Exe = "", [switch]$DestinationValidated)if (-not ($env:OS -eq "Windows_NT")) { return $false }@@ -2980,34 +2963,12 @@ exit 1$existingPath = $parent}$exact = $false- $resolved = $null- if (Initialize-StudioFinalPathNativeType) {- try {- $resolved = Get-StudioNativeFinalPath -Path $existingPath- if ([string]::IsNullOrWhiteSpace($resolved)) { throw "no exact answer" }- $exact = $true- } catch {- # The helper was DEFINED and still could not answer: a path renamed- # between the Test-Path walk and CreateFileW, an access denial on a- # component, a volume with no drive letter. Falling back keeps the- # install alive and Exact = $false makes the runtime lock fail- # closed, but say so: the degraded warning below only fires when the- # type itself could not be built, leaving an operator with a- # silently inexact identity on a host that looks fine.- $resolved = $null- if (-not $script:StudioNativeResolveWarned) {- $script:StudioNativeResolveWarned = $true- Write-StudioLine "[WARN] Could not resolve a path with the native helper; continuing with the fallback resolvers." -ForegroundColor Yellow- }- }- }- if ([string]::IsNullOrEmpty($resolved)) {- $resolved = Get-StudioPythonFinalPath -Path $existingPath- if (-not [string]::IsNullOrWhiteSpace($resolved)) { $exact = $true }- }+ $resolved = Get-StudioPythonFinalPath -Path $existingPath+ if (-not [string]::IsNullOrWhiteSpace($resolved)) { $exact = $true }if ([string]::IsNullOrEmpty($resolved)) {$resolved = Get-StudioLexicalPath -Path $existingPath$exact = $false+ Write-StudioFinalPathDegraded -Reason "no Python interpreter was available to resolve it"}if ($resolved.StartsWith('\\?\UNC\', [System.StringComparison]::OrdinalIgnoreCase)) {$resolved = '\\' + $resolved.Substring(8)@@ -3037,6 +2998,42 @@ exit 1return (Resolve-StudioFinalPathInfo -Path $Path).Path}+ function Test-StudioSameDirectoryByProbe {+ param([string]$Left, [string]$Right)+ if ([string]::IsNullOrWhiteSpace($Left) -or [string]::IsNullOrWhiteSpace($Right)) { return $false }+ if (-not (Test-Path -LiteralPath $Left -PathType Container)) { return $false }+ if (-not (Test-Path -LiteralPath $Right -PathType Container)) { return $false }+ $name = ".unsloth-path-probe-" + [guid]::NewGuid().ToString("N")+ $probe = Join-Path $Left $name+ try { Set-Content -LiteralPath $probe -Value "" -ErrorAction Stop } catch { return $false }+ try {+ return (Test-Path -LiteralPath (Join-Path $Right $name))+ } finally {+ Remove-Item -LiteralPath $probe -Force -ErrorAction SilentlyContinue+ }+ }++ function Split-StudioExistingAncestor {+ param([string]$Path)+ if ([string]::IsNullOrWhiteSpace($Path)) { return $null }+ $current = $Path+ $tail = ""+ # Bounded: a malformed path can leave Split-Path returning something that never shortens.+ for ($hop = 0; $hop -lt 64; $hop++) {+ if ([string]::IsNullOrWhiteSpace($current)) { break }+ if (Test-Path -LiteralPath $current -PathType Container) {+ return [pscustomobject]@{ Root = $current; Tail = $tail }+ }+ $leaf = Split-Path -Leaf $current+ $parent = Split-Path -Parent $current+ if ([string]::IsNullOrWhiteSpace($parent) -or $parent -eq $current) { break }+ if ([string]::IsNullOrWhiteSpace($leaf)) { break }+ $tail = if ($tail) { Join-Path $leaf $tail } else { $leaf }+ $current = $parent+ }+ return $null+ }+# Free space for the disk-full diagnosis (#11313), answering $null rather than guessing: a# number this cannot produce is a diagnosis it does not print.# A Windows volume can be mounted at a DIRECTORY, and then the drive root is the wrong answer:@@ -3161,7 +3158,21 @@ exit 1)$_tauriOverride = $_tauriOverride.TrimEnd($_trimSeps)$_legacyTauriRoot = $_legacyTauriRoot.TrimEnd($_trimSeps)- if ($_tauriOverride -ne $_legacyTauriRoot) {+ $_tauriSameRoot = $false+ if ($_tauriOverride -eq $_legacyTauriRoot) {+ $_tauriSameRoot = $true+ } else {+ $_tauriSameRoot = Test-StudioSameDirectoryByProbe -Left $_tauriOverride -Right $_legacyTauriRoot+ }+ if (-not $_tauriSameRoot) {+ $_tauriLeft = Split-StudioExistingAncestor -Path $_tauriOverride+ $_tauriRight = Split-StudioExistingAncestor -Path $_legacyTauriRoot+ if ($_tauriLeft -and $_tauriRight -and [string]::Equals(+ $_tauriLeft.Tail, $_tauriRight.Tail, [System.StringComparison]::OrdinalIgnoreCase)) {+ $_tauriSameRoot = Test-StudioSameDirectoryByProbe -Left $_tauriLeft.Root -Right $_tauriRight.Root+ }+ }+ if (-not $_tauriSameRoot) {Write-StudioLine "ERROR: $envOverrideVar is not supported with --tauri." -ForegroundColor RedWrite-StudioLine " The desktop app uses the Windows profile .unsloth\studio root." -ForegroundColor RedWrite-StudioLine " Run install.ps1 without --tauri for custom-root shell installs," -ForegroundColor Yellow@@ -3231,6 +3242,13 @@ exit 1$StudioRedirectMode = 'default'}$VenvDir = Join-Path $StudioHome "unsloth_studio"+ # Read before anything below creates them; see Write-StudioFinalPathDegraded. Every root+ # the process scan protects counts, so a legacy-layout install is not taken for a fresh one.+ $script:StudioInstallIsFresh = -not (+ (Test-Path -LiteralPath $VenvDir) -or+ (Test-Path -LiteralPath (Join-Path $StudioHome "bin\unsloth.exe")) -or+ (Test-Path -LiteralPath (Join-Path $StudioHome ".venv")) -or+ ($env:USERPROFILE -and (Test-Path -LiteralPath (Join-Path $env:USERPROFILE "unsloth_studio"))))# Records which cache this install used, so an update reuses it rather than guessing:# Set-StudioUvCacheForLaunch repoints the backend at the Studio cache even in shared@@ -5732,34 +5750,9 @@ exit 0substep "Created Unsloth Studio shortcut"# Per-item SHChangeNotify: the global broadcast misses a rewritten same-name .lnk.try {- # Emitted, not compiled: -MemberDefinition runs csc.exe too.- # Tauri returns before here, so this was only ever on the- # console path, which deserves the same treatment. A failure- # leaves stale icons and the enclosing catch absorbs it.- # Same order as everywhere else: a published type settles it,- # and only an absent one asks the gate.- if (-not ("UnslothShellIconRefresh" -as [type])) {- if (-not (Test-StudioCanDefineNativeTypes)) {- throw "native types are unavailable on this host"- }- $null = New-StudioEmittedNativeType -TypeName "UnslothShellIconRefresh" -Imports @(- @{ Name = "SHChangeNotify"; Library = "shell32.dll"; Return = [System.Void]- Args = @([int], [uint32], [string], [IntPtr]) }- )- }- # SHCNE_UPDATEITEM (0x00002000) + SHCNF_PATHW (0x0005) per shortcut- foreach ($scPath in $createdShortcutPaths) {- try { [UnslothShellIconRefresh]::SHChangeNotify(0x00002000, 0x0005, $scPath, [System.IntPtr]::Zero) } catch {}- }- # SHCNE_ASSOCCHANGED (0x08000000) global refresh (belt-and-suspenders)- [UnslothShellIconRefresh]::SHChangeNotify(0x08000000, 0, $null, [System.IntPtr]::Zero)- } catch {- # WDAC Dynamic Code Security refused the type: same notifications via a child.- try {- $null = Invoke-StudioPythonShellIconRefresh `- -Paths $createdShortcutPaths -Exe $ManagedPythonPath -DestinationValidated:$DestinationValidated- } catch {}- }+ $null = Invoke-StudioPythonShellIconRefresh `+ -Paths $createdShortcutPaths -Exe $ManagedPythonPath -DestinationValidated:$DestinationValidated+ } catch {}if ($firstInstall -or $iconChanged) {try { & "$env:SystemRoot\System32\ie4uinit.exe" -ClearIconCache 2>$null } catch {}try { & "$env:SystemRoot\System32\ie4uinit.exe" -show 2>$null } catch {}@@ -6010,7 +6003,9 @@ exit 0# exactly; otherwise they may be aliases of one. $null is the caller's# "identity unresolved" signal and makes it take both runtime locks.if (-not $leftInfo.Exact -or -not $rightInfo.Exact) {- Write-StudioLine "[WARN] Could not resolve Unsloth path identity; using the runtime lock." -ForegroundColor Yellow+ if ($script:StudioInstallIsFresh -ne $true) {+ Write-StudioLine "[WARN] Could not resolve Unsloth path identity; using the runtime lock." -ForegroundColor Yellow+ }return $null}return $false@@ -6324,89 +6319,12 @@ exit 0# has open, so the ladder still ends at Get-Process and Win32_Process. Every# rung reports a real executable image; a command line or working directory# mentioning the path is never proof.- #- # Emitted rather than compiled, like every other native declaration here; the- # ladder itself is unchanged.- $script:StudioProcessImageNativeState = $null- function Initialize-StudioProcessImageNativeType {- if ("UnslothStudioProcessImageV1" -as [type]) {- $script:StudioProcessImageNativeState = $true- return $true- }- if ($null -ne $script:StudioProcessImageNativeState) { return $script:StudioProcessImageNativeState }- # A type this session already emitted outranks any probe. The compiled- # version carried the path helper and this one in a single type and so could- # not disagree with itself; two types can, when a session emits the path type- # and then meets a probe that now fails. Do not ask a child whether emit- # works in a process where emit demonstrably worked.- $alreadyEmitted = $null -ne ("UnslothStudioFinalPathV3" -as [type])- if (-not $alreadyEmitted -and -not (Test-StudioCanDefineNativeTypes)) {- $script:StudioProcessImageNativeState = $false- return $false- }- try {- $null = New-StudioEmittedNativeType -TypeName "UnslothStudioProcessImageV1" -Imports @(- @{ Name = "OpenProcess"; Library = "kernel32.dll"; Return = [IntPtr]- Args = @([uint32], [bool], [int])- Ansi = $true },- @{ Name = "QueryFullProcessImageNameW"; Library = "kernel32.dll"; Return = [bool]- Args = @([IntPtr], [uint32], [System.Text.StringBuilder], [uint32].MakeByRefType()) },- @{ Name = "CloseHandle"; Library = "kernel32.dll"; Return = [bool]- Args = @([IntPtr])- Ansi = $true }- )- } catch {- # Same reason as the path helper: a throw on the way out of CreateType- # can still leave the type published, and a published type works.- if ("UnslothStudioProcessImageV1" -as [type]) {- $script:StudioProcessImageNativeState = $true- return $true- }- $script:StudioProcessImageNativeState = $false- return $false- }- $script:StudioProcessImageNativeState = $null -ne ("UnslothStudioProcessImageV1" -as [type])- return $script:StudioProcessImageNativeState- }-- # The body of the compiled GetProcessImagePath, moved out of C# so the imports- # above are all the native code there is. Same flags, same buffer. Null on any- # failure, as the compiled one returned and as the caller below skips on.- function Get-StudioNativeProcessImagePath {- param([Parameter(Mandatory = $true)][int]$ProcessId)- $queryLimitedInformation = [uint32]0x1000- # Same shape as Get-StudioNativeFinalPath: the open is inside the region- # that closes it, because an IntPtr has no finalizer to fall back on.- $handle = [IntPtr]::Zero- try {- try {- $handle = [UnslothStudioProcessImageV1]::OpenProcess(- $queryLimitedInformation, $false, $ProcessId)- } catch {- return $null- }- if ($handle -eq [IntPtr]::Zero) { return $null }- $buffer = New-Object System.Text.StringBuilder 32768- [uint32]$length = $buffer.Capacity- if (-not [UnslothStudioProcessImageV1]::QueryFullProcessImageNameW(- $handle, [uint32]0, $buffer, [ref]$length)) {- return $null- }- return $buffer.ToString()- } catch {- return $null- } finally {- if ($handle -ne [IntPtr]::Zero) {- try { [void][UnslothStudioProcessImageV1]::CloseHandle($handle) } catch {}- }- }- }-$script:StudioProcessImageTable = $null- $script:StudioProcessImageWarned = $false- # PID -> image path via ctypes in a child, so no type is defined in this script.+ # PID -> image path for every visible process, via ctypes in one child, so no type is defined+ # in this script. $null leaves the WMI rung exactly as it was.$script:StudioPythonProcessImageTable = $null$script:StudioPythonProcessImageProbed = $false+ $script:StudioProcessImageWarned = $falsefunction Get-StudioPythonProcessImageTable {$exe = Get-StudioEarlyPython@@ -6455,17 +6373,31 @@ exit 0return $table}+ # Bounded out of process like Get-StudioVolumeList: without Python this rung is the common one,+ # and a degraded WMI repository never returns.+ function Get-StudioWmiProcessImageRows {+ param(+ [scriptblock]$Query = {+ Get-CimInstance -ClassName Win32_Process -ErrorAction Stop | Select-Object ProcessId, ExecutablePath+ },+ [int]$TimeoutSeconds = 15+ )+ $job = $null+ try {+ $job = Start-Job -ScriptBlock $Query+ if (Wait-Job -Job $job -Timeout $TimeoutSeconds) {+ return @(Receive-Job -Job $job -ErrorAction SilentlyContinue)+ }+ Stop-Job -Job $job -ErrorAction SilentlyContinue+ } catch {+ } finally {+ if ($job) { Remove-Job -Job $job -Force -ErrorAction SilentlyContinue }+ }+ return @()+ }+function Get-StudioProcessImagePath {param([Parameter(Mandatory = $true)][int]$ProcessId)- if (Initialize-StudioProcessImageNativeType) {- $native = Get-StudioNativeProcessImagePath -ProcessId $ProcessId- if (-not [string]::IsNullOrWhiteSpace($native)) { return $native }- return $null- }- if (-not $script:StudioProcessImageWarned) {- $script:StudioProcessImageWarned = $true- Write-StudioLine "[WARN] Scanning for running Unsloth processes without the native helper; a process this shell cannot inspect may go unnoticed." -ForegroundColor Yellow- }$process = $nulltry { $process = Get-Process -Id $ProcessId -ErrorAction Stop } catch { $process = $null }if ($process) {@@ -6482,6 +6414,11 @@ exit 0try { $script:StudioPythonProcessImageTable = Get-StudioPythonProcessImageTable } catch {$script:StudioPythonProcessImageTable = $null}+ if ($env:OS -eq "Windows_NT" -and $null -eq $script:StudioPythonProcessImageTable -and+ -not $script:StudioProcessImageWarned) {+ $script:StudioProcessImageWarned = $true+ Write-StudioLine "[WARN] Scanning for running Unsloth processes without the process-image helper; a process this shell cannot inspect may go unnoticed." -ForegroundColor Yellow+ }}if ($script:StudioPythonProcessImageTable -and$script:StudioPythonProcessImageTable.ContainsKey($ProcessId)) {@@ -6491,13 +6428,11 @@ exit 0# Queried once per run, not once per process: this is the slow rung.if ($null -eq $script:StudioProcessImageTable) {$script:StudioProcessImageTable = @{}- try {- foreach ($row in @(Get-CimInstance -ClassName Win32_Process -ErrorAction Stop)) {- if (-not [string]::IsNullOrWhiteSpace($row.ExecutablePath)) {- $script:StudioProcessImageTable[[int]$row.ProcessId] = [string]$row.ExecutablePath- }+ foreach ($row in @(Get-StudioWmiProcessImageRows)) {+ if ($row -and -not [string]::IsNullOrWhiteSpace($row.ExecutablePath)) {+ $script:StudioProcessImageTable[[int]$row.ProcessId] = [string]$row.ExecutablePath}- } catch {}+ }}if ($script:StudioProcessImageTable.ContainsKey($ProcessId)) {return $script:StudioProcessImageTable[$ProcessId]@@ -6524,9 +6459,18 @@ exit 0# Block only confirmed executable identities: a command line or working# directory that merely mentions the path is not proof of an open file.- foreach ($process in @(Get-Process -ErrorAction SilentlyContinue)) {- $executable = $null- try { $executable = Get-StudioProcessImagePath -ProcessId $process.Id } catch { continue }+ $studioScanProcesses = @(Get-Process -ErrorAction SilentlyContinue |+ Select-Object -Property Id, ProcessName)+ $studioScanImages = @{}+ foreach ($process in $studioScanProcesses) {+ $image = $null+ try { $image = Get-StudioProcessImagePath -ProcessId $process.Id } catch { continue }+ if ([string]::IsNullOrWhiteSpace($image)) { continue }+ $studioScanImages[[string]$process.Id] = $image+ }+ try { Resolve-StudioFinalPathsInOneChild -Paths @($studioScanImages.Values) } catch { }+ foreach ($process in $studioScanProcesses) {+ $executable = $studioScanImages[[string]$process.Id]if (-not $executable) { continue }try { $executable = Get-StudioFinalPath -Path $executable } catch { continue }if (Test-StudioProtectedPathMatch -Candidate $executable -ProtectedPath $resolvedPath -Exact:$Exact) {@@ -8807,9 +8751,9 @@ exit 0# has created a managed interpreter and then a venv, so asking the cache would decline# on exactly the fresh install where nvidia-smi is also most likely to be missing. The# host would take CPU wheels while holding a working NVIDIA card.- foreach ($candidate in @($VenvPython, $ManagedPythonPath)) {- if ([string]::IsNullOrWhiteSpace($candidate)) { continue }- if (Test-Path -LiteralPath $candidate -PathType Leaf) { return $candidate }+ if (-not [string]::IsNullOrWhiteSpace($VenvPython) -and+ (Test-Path -LiteralPath $VenvPython -PathType Leaf)) {+ return $VenvPython}try { return "$(Get-StudioEarlyPython)" } catch { return "" }}@@ -8832,43 +8776,9 @@ exit 0return (Join-Path $root "System32")}- # The driver's libraries as P/Invoke methods, emitted rather than compiled: the installer must- # not spawn csc.exe (New-StudioEmittedNativeType). $null when the type cannot be built. A- # missing library throws at the first call, not here.- function Get-NvidiaLibraryProbeType {- # V3: a session that emitted V2 would keep its bare-name bindings.- $name = "UnslothNvidiaProbeV3"- $existing = $name -as [type]- if ($existing) { return $existing }- # Dynamic Code Security can kill the process on an emitted load rather than throw: the- # same gate every other emitted type checks first, and no inventory when it says no.- if (-not (Test-StudioCanDefineNativeTypes)) { return $null }- $windows = ($env:OS -eq "Windows_NT")- $nvml = if ($windows) { Get-NvidiaNvmlLibraryPath } else { "libnvidia-ml.so.1" }- $cuda = if ($windows) { Join-Path (Get-NvidiaSystem32Dir) "nvcuda.dll" } else { "libcuda.so.1" }- $int = [int]; $uint = [uint32]; $refInt = [int].MakeByRefType()- $refUInt = [uint32].MakeByRefType(); $refPtr = [IntPtr].MakeByRefType()- try {- $null = New-StudioEmittedNativeType -TypeName $name -Imports @(- @{ Name = "nvmlInit_v2"; Library = $nvml; Return = $int; Args = @(); Ansi = $true },- @{ Name = "nvmlShutdown"; Library = $nvml; Return = $int; Args = @(); Ansi = $true },- @{ Name = "nvmlSystemGetCudaDriverVersion_v2"; Library = $nvml; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },- @{ Name = "nvmlDeviceGetCount_v2"; Library = $nvml; Return = $int; Args = @($refUInt); Out = @(1); Ansi = $true },- @{ Name = "nvmlDeviceGetHandleByIndex_v2"; Library = $nvml; Return = $int; Args = @($uint, $refPtr); Out = @(2); Ansi = $true },- @{ Name = "nvmlDeviceGetCudaComputeCapability"; Library = $nvml; Return = $int; Args = @([IntPtr], $refInt, $refInt); Out = @(2, 3); Ansi = $true },- @{ Name = "cuInit"; Library = $cuda; Return = $int; Args = @($uint); Ansi = $true },- @{ Name = "cuDriverGetVersion"; Library = $cuda; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },- @{ Name = "cuDeviceGetCount"; Library = $cuda; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },- @{ Name = "cuDeviceGet"; Library = $cuda; Return = $int; Args = @($refInt, $int); Out = @(1); Ansi = $true },- @{ Name = "cuDeviceGetAttribute"; Library = $cuda; Return = $int; Args = @($refInt, $int, $int); Out = @(1); Ansi = $true }- )- } catch { return $null }- return ($name -as [type])- }# The same inventory with nothing emitted: CPython's ctypes makes the identical NVML and CUDA# driver calls, and the interop leaves the scanned surface rather than moving within it.- # A second source BENEATH the emitted one, never ahead of it: "" whenever no interpreter is# available or the probe itself says nothing.# Get-NvidiaProbePythonExe is deliberately per-file. The installer has its early read-only# interpreter ladder; setup.ps1 has the venv a previous run already built.@@ -9016,17 +8926,32 @@ def main():main()'@- # Cmdlets only. Constrained Language Mode refuses New-Object ProcessStartInfo and- # [Process]::Start, and CLM is one of the two policies that make the emitted rung decline,- # so this launcher has to work on exactly the hosts that need it most.- $tempRoot = if ($env:TEMP) { $env:TEMP } elseif ($env:TMPDIR) { $env:TMPDIR } else { "/tmp" }- $stem = Join-Path $tempRoot ("unsloth-nvprobe-" + [guid]::NewGuid().ToString("N"))+ # Cmdlets only: Constrained Language Mode refuses New-Object ProcessStartInfo and+ # [Process]::Start, and this launcher has to work there.+ $probeDir = New-StudioChildScriptDirectory+ $inline = (-not $probeDir)+ if ($inline) {+ # Elevated, a shared root lets a standard user plant or swap the redirect files: decline.+ try { if (Test-StudioChildScriptDirectoryElevated) { return "" } } catch { return "" }+ # The first root that takes a file: a TEMP that declined the directory may refuse this too.+ $stem = $null+ foreach ($root in @($env:TEMP, $env:TMP, $env:LOCALAPPDATA, $env:TMPDIR, "/tmp")) {+ if (-not $root) { continue }+ try {+ $candidateStem = Join-Path $root ("unsloth-nvprobe-" + [guid]::NewGuid().ToString("N")) -ErrorAction Stop+ $null = New-Item -ItemType File -Path "$candidateStem.out" -ErrorAction Stop; $stem = $candidateStem; break+ } catch {}+ }+ if (-not $stem) { return "" }+ } else {+ $stem = Join-Path $probeDir "nvprobe"+ }$scriptFile = "$stem.py"$outFile = "$stem.out"$errFile = "$stem.err"$raw = ""try {- Set-Content -LiteralPath $scriptFile -Value $probeSource -Encoding UTF8 -ErrorAction Stop+ if (-not $inline) { Set-Content -LiteralPath $scriptFile -Value $probeSource -Encoding UTF8 -ErrorAction Stop }# Whole seconds, rounded up, without [math]::Ceiling: CLM blocks it. PowerShell's / is# floating point and [int] rounds to nearest, so 10000ms must not become 11s.$seconds = ($TimeoutMs - ($TimeoutMs % 1000)) / 1000@@ -9037,19 +8962,28 @@ main()# each native argument verbatim, so a script under "C:\Users\First Last\AppData\Local\# Temp" or a hint under "C:\Program Files\NVIDIA Corporation\NVSMI" would split on its# spaces and the child would run something else. The script arrives on stdin and the two- # library hints in the environment; the only arguments left are -I -S and a bare dash.$savedNvml = $env:UNSLOTH_NVML_HINT$savedCuda = $env:UNSLOTH_CUDA_HINT$savedSkip = $env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML+ $savedSource = $env:UNSLOTH_NVIDIA_PROBE_SOURCE$env:UNSLOTH_NVML_HINT = $nvmlHint$env:UNSLOTH_CUDA_HINT = $cudaHint# The switch reaches this child only. An inherited value must not make a first child skip NVML.if ($SkipNvml) { $env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML = "1" }else { Remove-Item Env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML -ErrorAction SilentlyContinue }try {- $proc = Start-Process -FilePath $exe -ArgumentList @("-I", "-S", "-") -NoNewWindow -PassThru `- -RedirectStandardInput $scriptFile -RedirectStandardOutput $outFile -RedirectStandardError $errFile -ErrorAction Stop+ if ($inline) {+ $env:UNSLOTH_NVIDIA_PROBE_SOURCE = $probeSource+ $proc = Start-Process -FilePath $exe -NoNewWindow -PassThru `+ -ArgumentList @("-I", "-S", "-B", "-c", "exec(__import__('os').environ['UNSLOTH_NVIDIA_PROBE_SOURCE'])") `+ -RedirectStandardOutput $outFile -RedirectStandardError $errFile -ErrorAction Stop+ } else {+ $proc = Start-Process -FilePath $exe -ArgumentList @("-I", "-S", "-B", "-") -NoNewWindow -PassThru `+ -RedirectStandardInput $scriptFile -RedirectStandardOutput $outFile -RedirectStandardError $errFile -ErrorAction Stop+ }} finally {+ if ($null -eq $savedSource) { Remove-Item Env:UNSLOTH_NVIDIA_PROBE_SOURCE -ErrorAction SilentlyContinue }+ else { $env:UNSLOTH_NVIDIA_PROBE_SOURCE = $savedSource }if ($null -eq $savedNvml) { Remove-Item Env:UNSLOTH_NVML_HINT -ErrorAction SilentlyContinue }else { $env:UNSLOTH_NVML_HINT = $savedNvml }if ($null -eq $savedCuda) { Remove-Item Env:UNSLOTH_CUDA_HINT -ErrorAction SilentlyContinue }@@ -9075,6 +9009,9 @@ main()$raw = "$(Get-Content -LiteralPath $outFile -Raw -ErrorAction SilentlyContinue)"} catch { return "" }finally {+ # The directory, not just the three files: it is ours, nothing else may be in it, and+ # leaving an empty one behind per probe would litter %TEMP% on every run.+ if ($probeDir) { Remove-Item -LiteralPath $probeDir -Recurse -Force -ErrorAction SilentlyContinue }foreach ($stale in @($scriptFile, $outFile, $errFile)) {Remove-Item -LiteralPath $stale -Force -ErrorAction SilentlyContinue}@@ -9083,89 +9020,14 @@ main()}# "source;cudaMajor;cudaMinor;cap,cap" from NVML, else the CUDA driver API; "" when neither- # answers. Versions are major*1000 + minor*10. One runspace + deadline per reader: a shared one let slow NVML starve CUDA.- # A wedged driver can block inside the library, and each deadline leaves its runspace behind.- # The Python rung below spends what the emitted rung left of ONE budget (a per-reader bound for- # NVML plus one for CUDA), so the worst-case wall clock is the emitted rung's own. Its first child- # reads NVML then the CUDA driver API under at most one per-reader bound; only when that child is- # killed at its bound (a hung NVML) and at least 2 s remain does a second child read the CUDA- # driver API alone with the rest, so a hung NVML no longer starves a healthy CUDA driver API.function Read-NvidiaLibraryRaw {param([int]$TimeoutMs = 30000)+ # One deadline for both children, as the two in-process readers had: the first gets a whole+ # bound, and only a hung NVML earns a CUDA-only child with what is left, less the 2 s kept back+ # to reap the child it may have to kill. Compared by hand: CLM refuses [math].$deadline = (Get-Date).AddMilliseconds($TimeoutMs * 2)- $native = ""- $type = Get-NvidiaLibraryProbeType- if ($type) {- $reader = {- param($T, $Which)- function Read-Nvml {- if ($T::nvmlInit_v2() -ne 0) { return "" }- try {- [uint32]$count = 0- if ($T::nvmlDeviceGetCount_v2([ref]$count) -ne 0 -or $count -eq 0) { return "" }- [int]$ver = 0- if ($T::nvmlSystemGetCudaDriverVersion_v2([ref]$ver) -ne 0 -or $ver -lt 1000) { return "" }- $caps = @()- for ([uint32]$i = 0; $i -lt $count; $i++) {- [IntPtr]$dev = [IntPtr]::Zero; [int]$major = 0; [int]$minor = 0- # One unreadable GPU voids the source: a partial list misleads the pre-Turing cap.- if ($T::nvmlDeviceGetHandleByIndex_v2($i, [ref]$dev) -ne 0) { return "" }- if ($T::nvmlDeviceGetCudaComputeCapability($dev, [ref]$major, [ref]$minor) -ne 0) { return "" }- $caps += "$major.$minor"- }- return "nvml;$([int][math]::Floor($ver / 1000));$([int][math]::Floor(($ver % 1000) / 10));$($caps -join ',')"- } finally { $null = $T::nvmlShutdown() }- }- function Read-Cuda {- # The driver API honours CUDA_VISIBLE_DEVICES; the inventory must be the physical one,- # so a hidden pre-Turing card still caps the family. cuInit reads the mask once.- $saved = $env:CUDA_VISIBLE_DEVICES- Remove-Item Env:CUDA_VISIBLE_DEVICES -ErrorAction SilentlyContinue- try { $init = $T::cuInit([uint32]0) } finally { if ($null -ne $saved) { $env:CUDA_VISIBLE_DEVICES = $saved } }- if ($init -ne 0) { return "" }- [int]$count = 0- if ($T::cuDeviceGetCount([ref]$count) -ne 0 -or $count -eq 0) { return "" }- [int]$ver = 0- if ($T::cuDriverGetVersion([ref]$ver) -ne 0 -or $ver -lt 1000) { return "" }- $caps = @()- for ($i = 0; $i -lt $count; $i++) {- [int]$dev = 0; [int]$major = 0; [int]$minor = 0- if ($T::cuDeviceGet([ref]$dev, $i) -ne 0) { return "" }- # CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR = 75, _MINOR = 76.- if ($T::cuDeviceGetAttribute([ref]$major, 75, $dev) -ne 0) { return "" }- if ($T::cuDeviceGetAttribute([ref]$minor, 76, $dev) -ne 0) { return "" }- $caps += "$major.$minor"- }- return "cuda;$([int][math]::Floor($ver / 1000));$([int][math]::Floor(($ver % 1000) / 10));$($caps -join ',')"- }- $r = ""- try { if ($Which -eq "nvml") { $r = Read-Nvml } else { $r = Read-Cuda } } catch { $r = "" }- return "$r"- }- foreach ($which in @("nvml", "cuda")) {- $ps = $null; $handle = $null; $r = ""- try {- $ps = [powershell]::Create()- $null = $ps.AddScript($reader.ToString()).AddArgument($type).AddArgument($which)- $handle = $ps.BeginInvoke()- if ($handle.AsyncWaitHandle.WaitOne($TimeoutMs)) {- $r = "$(@($ps.EndInvoke($handle)) | Select-Object -Last 1)"- }- } catch { $r = "" }- finally { if ($ps -and $handle -and $handle.IsCompleted) { $ps.Dispose() } }- if ($r) { $native = $r; break }- }- }- if ($native) { return $native }- $remainingMs = [int]($deadline - (Get-Date)).TotalMilliseconds- # Not worth a child process we cannot wait out; the emitted rung already spent the budget.- if ($remainingMs -lt 3000) { return "" }- # Each child gets at most one per-reader bound, compared by hand: CLM refuses [math]. 2 s of-# every child's share is kept back for reaping it if it hangs, so the pair ends inside the deadline.- $childMs = $remainingMs - 2000; if ($childMs -gt $TimeoutMs) { $childMs = $TimeoutMs }$raw = ""- try { $raw = Read-NvidiaLibraryRawViaPython -TimeoutMs $childMs } catch { return "" }- # Only a first child killed at its bound (a hung NVML) earns a CUDA-only child, with what is left.+ try { $raw = Read-NvidiaLibraryRawViaPython -TimeoutMs $TimeoutMs } catch { return "" }if (-not $script:NvidiaPythonProbeTimedOut) { return $raw }$remainingMs = [int]($deadline - (Get-Date)).TotalMillisecondsif ($remainingMs -lt 3000) { return "" }@@ -9183,6 +9045,10 @@ main()$script:NvidiaLibraryInventory = $nullif ("$($env:UNSLOTH_NVIDIA_LIBRARY_PROBE)".Trim() -eq "0") { return $null }try { $raw = Read-NvidiaLibraryRaw -TimeoutMs ($TimeoutSec * 1000) } catch { return $null }+ # The Python probe is the only library reader, so its opt-outs leave nvidia-smi as the only source.+ if (-not "$raw" -and ("$($env:UNSLOTH_NVIDIA_PYTHON_PROBE)".Trim() -eq "0" -or "$($env:UNSLOTH_EARLY_PYTHON_PROBE)".Trim() -eq "0")) {+ Write-StudioLine " NVIDIA driver libraries not read (Python probe disabled): a GPU without a working nvidia-smi is not detected. Set UNSLOTH_TORCH_INDEX_FAMILY=cu128 (or your CUDA wheel) to choose one." -ForegroundColor Yellow+ }$parts = "$raw".Split(";")if ($parts.Count -ne 4 -or -not $parts[3] -or [int]$parts[1] -lt 1) { return $null }$caps = @($parts[3].Split(","))diff --git a/studio/setup.ps1 b/studio/setup.ps1index 4c2e16dc69c..75a2e94d939 100644--- a/studio/setup.ps1+++ b/studio/setup.ps1@@ -1495,11 +1495,280 @@ function Get-NvidiaProbePythonExe {if (-not $VenvDir) { return "" }foreach ($leaf in @("Scripts\python.exe", "bin/python3", "bin/python")) {$candidate = Join-Path $VenvDir $leaf- if (Test-Path -LiteralPath $candidate -PathType Leaf) { return $candidate }+ if (-not (Test-Path -LiteralPath $candidate -PathType Leaf)) { continue }+ return $candidate}return ""}+function Get-StudioSystem32Tool {+ param([Parameter(Mandatory = $true)][string]$Name)+ if ([string]::IsNullOrWhiteSpace($env:SystemRoot)) { return "" }+ $candidate = Join-Path (Join-Path $env:SystemRoot "System32") $Name+ if (-not (Test-Path -LiteralPath $candidate -PathType Leaf)) { return "" }+ return $candidate+}++function Test-StudioSddlRightsAreWrite {+ param([string]$Rights)+ if ([string]::IsNullOrWhiteSpace($Rights)) { return $true }+ $text = "$Rights".Trim().ToUpper()+ if ($text -like "0X*") {+ $mask = [long]0+ foreach ($ch in $text.Substring(2).ToCharArray()) {+ $digit = "0123456789ABCDEF".IndexOf($ch)+ if ($digit -lt 0) { return $true }+ $mask = ($mask * 16) + $digit+ }+ return (($mask -band 0x500D0156) -ne 0)+ }+ foreach ($alias in @("GA", "GW", "WD", "WO", "SD", "DT", "FA", "FW", "KA", "KW",+ "CC", "DC", "WP", "SW")) {+ if ($text.Contains($alias)) { return $true }+ }+ return $false+}++function Test-StudioSddlPrincipalIsAdminOnly {+ param([string]$Principal)+ if ([string]::IsNullOrWhiteSpace($Principal)) { return $false }+ $who = "$Principal".Trim().ToUpper()+ return (@(+ "BA", "SY", "S-1-5-32-544", "S-1-5-18",+ "S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464"+ ) -contains $who)+}++function Test-StudioSddlWritableByNonAdmin {+ param([string]$Sddl)+ if ([string]::IsNullOrWhiteSpace($Sddl)) { return $true }+ $text = "$Sddl".Trim()+ if (-not ($text -match '^O:([A-Za-z0-9\-]+?)(?:G:|D:|S:|$)')) { return $true }+ if (-not (Test-StudioSddlPrincipalIsAdminOnly -Principal $Matches[1])) { return $true }+ $daclAt = $text.IndexOf("D:")+ if ($daclAt -lt 0) { return $true }+ $dacl = $text.Substring($daclAt)+ $saclAt = $dacl.IndexOf("S:")+ if ($saclAt -ge 0) { $dacl = $dacl.Substring(0, $saclAt) }+ $seen = 0+ foreach ($chunk in ($dacl -split '\)')) {+ $open = $chunk.IndexOf("(")+ if ($open -lt 0) { continue }+ $seen+++ $fields = $chunk.Substring($open + 1) -split ';'+ if ($fields.Count -lt 6) { return $true }+ $type = "$($fields[0])".Trim().ToUpper()+ $flags = "$($fields[1])".Trim().ToUpper()+ if (@("D", "OD", "XD") -contains $type) { continue }+ if ($flags.Contains("IO")) { continue }+ if (-not (Test-StudioSddlRightsAreWrite -Rights $fields[2])) { continue }+ if (-not (Test-StudioSddlPrincipalIsAdminOnly -Principal $fields[5])) { return $true }+ }+ # A DACL with no ACEs in it is not evidence of anything, and neither is one this did not+ # manage to split.+ if ($seen -eq 0) { return $true }+ return $false+}++function Test-StudioDirectoryIsAdminOnly {+ param([string]$Path)+ if ([string]::IsNullOrWhiteSpace($Path)) { return $false }+ $sddl = ""+ try {+ # Select-Object -ExpandProperty, not a property read: Constrained Language Mode refuses+ # property access on types outside its allowed list, and the security descriptor is one.+ $sddl = "$(Get-Acl -LiteralPath $Path -ErrorAction Stop |+ Select-Object -ExpandProperty Sddl)"+ } catch { return $false }+ if ([string]::IsNullOrWhiteSpace($sddl)) { return $false }+ return (-not (Test-StudioSddlWritableByNonAdmin -Sddl $sddl))+}++function Get-StudioLexicalParent {+ param([string]$Path)+ $trimmed = "$Path".TrimEnd('\', '/')+ $cut = $trimmed.LastIndexOfAny(@([char]92, [char]47))+ if ($cut -lt 0) { return "" }+ return $trimmed.Substring(0, $cut)+}++function Test-StudioPathUnderAdminRoot {+ param([string]$Path)+ if ([string]::IsNullOrWhiteSpace($Path)) { return $false }+ # $env: rather than [Environment]::GetEnvironmentVariable: System.Environment is not on+ # Constrained Language Mode's allowed type list, and this helper runs on hosts under it.+ $roots = @()+ foreach ($value in @($env:SystemRoot, $env:ProgramFiles, $env:ProgramW6432, ${env:ProgramFiles(x86)})) {+ if (-not [string]::IsNullOrWhiteSpace($value)) { $roots += "$value".TrimEnd('\', '/') }+ }+ $matchedRoot = ""+ foreach ($root in $roots) {+ $escapedRoot = $root -replace '([\[\]\*\?])', '`$1'+ if (("$Path" -like ($escapedRoot + "\*")) -or ("$Path" -like ($escapedRoot + "/*"))) {+ $matchedRoot = $root+ break+ }+ }+ if ([string]::IsNullOrWhiteSpace($matchedRoot)) { return $false }+ $windowsRoot = "$($env:SystemRoot)".TrimEnd('\', '/')+ if (-not [string]::IsNullOrWhiteSpace($windowsRoot)) {+ foreach ($leaf in @(+ "Temp", "Tasks", "Tracing", "Registration\CRMLog", "debug\WIA",+ "System32\Tasks", "System32\spool\drivers\color", "System32\spool\PRINTERS",+ "System32\spool\SERVERS", "System32\com\dmp", "System32\FxsTmp",+ "SysWOW64\Tasks", "SysWOW64\com\dmp", "SysWOW64\FxsTmp"+ )) {+ $writable = $windowsRoot + "\" + $leaf+ $escapedWritable = $writable -replace '([\[\]\*\?])', '`$1'+ if (("$Path" -like ($escapedWritable + "\*")) -or+ ("$Path" -like ($escapedWritable + "/*"))) { return $false }+ }+ }+ $current = Get-StudioLexicalParent -Path "$Path"+ $guard = 0+ while (-not [string]::IsNullOrWhiteSpace($current)) {+ $guard+++ if ($guard -gt 64) { return $false }+ if (-not (Test-StudioDirectoryIsAdminOnly -Path $current)) { return $false }+ if ("$current".TrimEnd('\', '/') -eq $matchedRoot) { return $true }+ $next = Get-StudioLexicalParent -Path "$current"+ if ("$next" -eq "$current") { return $false }+ $current = $next+ }+ return $false+}++function Invoke-StudioSystem32ToolBounded {+ param([string]$Exe, [string[]]$Arguments = @(), [int]$TimeoutMs = 3000)+ # CLM cannot construct ProcessStartInfo. A job captures the trusted system tool's+ # output through PowerShell's pipes, without redirect files in an unlabelled directory.+ if ("$($ExecutionContext.SessionState.LanguageMode)" -ne "FullLanguage") {+ $job = $null+ try {+ $seconds = ($TimeoutMs - ($TimeoutMs % 1000)) / 1000+ if (($TimeoutMs % 1000) -ne 0) { $seconds++ }+ if ($seconds -lt 1) { $seconds = 1 }+ $job = Start-Job -ScriptBlock {+ param($tool, $toolArgs)+ $lines = @(& $tool @toolArgs 2>$null)+ return @{ Output = ($lines -join "`n"); ExitCode = $LASTEXITCODE }+ } -ArgumentList $Exe, (,$Arguments)+ if (Wait-Job -Job $job -Timeout $seconds) {+ return (Receive-Job -Job $job -ErrorAction SilentlyContinue | Select-Object -Last 1)+ }+ Stop-Job -Job $job -ErrorAction SilentlyContinue+ } catch { return $null }+ finally { if ($job) { Remove-Job -Job $job -Force -ErrorAction SilentlyContinue } }+ return $null+ }+ $proc = $null+ try {+ # If policy forbids these types, decline. The caller treats an unreadable+ # token as elevated and uses the inline probe when no private directory is safe.+ $clock = [System.Diagnostics.Stopwatch]::StartNew()+ $psi = New-Object System.Diagnostics.ProcessStartInfo+ $psi.FileName = $Exe+ if ($null -ne $psi.PSObject.Properties['ArgumentList']) {+ foreach ($arg in $Arguments) { $null = $psi.ArgumentList.Add($arg) }+ } else {+ # The arguments here are paths and fixed switches; paths cannot contain quotes.+ $psi.Arguments = (@($Arguments | ForEach-Object {+ '"' + ($_ -replace '(\\+)$', '$1$1') + '"'+ }) -join ' ')+ }+ $psi.UseShellExecute = $false+ $psi.RedirectStandardOutput = $true+ $psi.RedirectStandardError = $true+ $psi.CreateNoWindow = $true+ $proc = [System.Diagnostics.Process]::Start($psi)+ $stdout = $proc.StandardOutput.ReadToEndAsync()+ $null = $proc.StandardError.ReadToEndAsync()+ if (-not $proc.WaitForExit($TimeoutMs)) {+ try { $proc.Kill() } catch { }+ return $null+ }+ $left = [Math]::Max(0, $TimeoutMs - [int]$clock.ElapsedMilliseconds)+ if (-not $stdout.Wait($left)) { return $null }+ return @{ Output = "$($stdout.Result)"; ExitCode = $proc.ExitCode }+ } catch { return $null }+ finally { if ($proc) { try { $proc.Dispose() } catch { } } }+}++function Test-StudioChildScriptDirectoryElevated {+ $groups = ""+ $whoami = Get-StudioSystem32Tool -Name "whoami.exe"+ if (-not $whoami) { return $true }+ try {+ $result = Invoke-StudioSystem32ToolBounded -Exe $whoami -Arguments @('/groups')+ if ($null -eq $result) { return $true }+ $groups = "$($result.Output)"+ } catch { return $true }+ if ([string]::IsNullOrWhiteSpace($groups)) { return $true }+ if ($groups -match "S-1-16-(12288|16384)") { return $true }+ if ($groups -match "S-1-16-\d+") { return $false }+ return $true+}++function New-StudioChildScriptDirectory {+ $tempRoot = if ($env:TEMP) { $env:TEMP } elseif ($env:TMPDIR) { $env:TMPDIR } else { "/tmp" }+ # Join-Path throws on a TEMP naming a missing drive: decline, like any other unusable root.+ try { $dir = Join-Path $tempRoot ("unsloth-child-" + [guid]::NewGuid().ToString("N")) -ErrorAction Stop } catch { return "" }+ $made = $false+ try {+ $createdPath = "$(New-Item -ItemType Directory -Path $dir -ErrorAction Stop |+ Select-Object -ExpandProperty FullName)"+ $made = (Test-Path -LiteralPath $dir -PathType Container)+ if ((-not $made) -and $createdPath) {+ Remove-Item -LiteralPath $createdPath -Recurse -Force -ErrorAction SilentlyContinue+ }+ } catch { }+ if ((-not $made) -and $dir -match '[\[\]]') {+ $escaped = $dir -replace '([\[\]])', '`$1'+ try {+ $null = New-Item -ItemType Directory -Path $escaped -ErrorAction Stop+ $made = (Test-Path -LiteralPath $dir -PathType Container)+ } catch { }+ }+ # Created by THIS call. New-Item without -Force throws on a directory that already exists,+ # which is the point: a pre-created one carrying an attacker's ACL is refused, not adopted.+ if (-not $made) { return "" }+ if ($env:OS -eq "Windows_NT") {+ # Until it is labelled a standard user can swap this directory for a junction: a link is+ # never adopted, and cleanup never recurses through one into its target.+ $isLink = { try { "$((Get-Item -LiteralPath $dir -Force -ErrorAction Stop).Attributes)" -match 'ReparsePoint' } catch { $true } }+ $labelled = $false+ $icacls = Get-StudioSystem32Tool -Name "icacls.exe"+ if (-not $icacls) {+ if (-not (& $isLink)) { try { Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue } catch { } }+ return ""+ }+ try {+ $result = Invoke-StudioSystem32ToolBounded -Exe $icacls -Arguments @($dir, '/setintegritylevel', '(OI)(CI)H')+ $labelled = ($null -ne $result -and $result.ExitCode -eq 0)+ if (-not $labelled -and $null -ne $result) {+ $readback = Invoke-StudioSystem32ToolBounded -Exe $icacls -Arguments @($dir)+ $labelled = ($null -ne $readback -and "$($readback.Output)" -match "S-1-16-12288|High Mandatory Level")+ }+ } catch { $labelled = $false }+ if ((-not $labelled) -and (Test-StudioChildScriptDirectoryElevated)) {+ if (-not (& $isLink)) { try { Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue } catch { } }+ return ""+ }+ if (& $isLink) { return "" }+ if ($labelled) {+ $planted = $true+ try {+ $planted = @(Get-ChildItem -LiteralPath $dir -Force -ErrorAction Stop).Count -ne 0+ } catch { $planted = $true }+ if ($planted) {+ if (-not (& $isLink)) { try { Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue } catch { } }+ return ""+ }+ }+ }+ return $dir+}+# ── BEGIN SHARED WITH install.ps1 (Get-NvidiaLibraryInventory) ──# Full paths only: a bare name searches PATH, where ZLUDA's nvcuda.dll and nvml.dll pass for NVIDIA.function Get-NvidiaNvmlLibraryPath {@@ -1518,43 +1787,9 @@ function Get-NvidiaSystem32Dir {return (Join-Path $root "System32")}-# The driver's libraries as P/Invoke methods, emitted rather than compiled: the installer must-# not spawn csc.exe (New-StudioEmittedNativeType). $null when the type cannot be built. A-# missing library throws at the first call, not here.-function Get-NvidiaLibraryProbeType {- # V3: a session that emitted V2 would keep its bare-name bindings.- $name = "UnslothNvidiaProbeV3"- $existing = $name -as [type]- if ($existing) { return $existing }- # Dynamic Code Security can kill the process on an emitted load rather than throw: the- # same gate every other emitted type checks first, and no inventory when it says no.- if (-not (Test-StudioCanDefineNativeTypes)) { return $null }- $windows = ($env:OS -eq "Windows_NT")- $nvml = if ($windows) { Get-NvidiaNvmlLibraryPath } else { "libnvidia-ml.so.1" }- $cuda = if ($windows) { Join-Path (Get-NvidiaSystem32Dir) "nvcuda.dll" } else { "libcuda.so.1" }- $int = [int]; $uint = [uint32]; $refInt = [int].MakeByRefType()- $refUInt = [uint32].MakeByRefType(); $refPtr = [IntPtr].MakeByRefType()- try {- $null = New-StudioEmittedNativeType -TypeName $name -Imports @(- @{ Name = "nvmlInit_v2"; Library = $nvml; Return = $int; Args = @(); Ansi = $true },- @{ Name = "nvmlShutdown"; Library = $nvml; Return = $int; Args = @(); Ansi = $true },- @{ Name = "nvmlSystemGetCudaDriverVersion_v2"; Library = $nvml; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },- @{ Name = "nvmlDeviceGetCount_v2"; Library = $nvml; Return = $int; Args = @($refUInt); Out = @(1); Ansi = $true },- @{ Name = "nvmlDeviceGetHandleByIndex_v2"; Library = $nvml; Return = $int; Args = @($uint, $refPtr); Out = @(2); Ansi = $true },- @{ Name = "nvmlDeviceGetCudaComputeCapability"; Library = $nvml; Return = $int; Args = @([IntPtr], $refInt, $refInt); Out = @(2, 3); Ansi = $true },- @{ Name = "cuInit"; Library = $cuda; Return = $int; Args = @($uint); Ansi = $true },- @{ Name = "cuDriverGetVersion"; Library = $cuda; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },- @{ Name = "cuDeviceGetCount"; Library = $cuda; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },- @{ Name = "cuDeviceGet"; Library = $cuda; Return = $int; Args = @($refInt, $int); Out = @(1); Ansi = $true },- @{ Name = "cuDeviceGetAttribute"; Library = $cuda; Return = $int; Args = @($refInt, $int, $int); Out = @(1); Ansi = $true }- )- } catch { return $null }- return ($name -as [type])-}# The same inventory with nothing emitted: CPython's ctypes makes the identical NVML and CUDA# driver calls, and the interop leaves the scanned surface rather than moving within it.-# A second source BENEATH the emitted one, never ahead of it: "" whenever no interpreter is# available or the probe itself says nothing.# Get-NvidiaProbePythonExe is deliberately per-file. The installer has its early read-only# interpreter ladder; setup.ps1 has the venv a previous run already built.@@ -1702,17 +1937,32 @@ def main():main()'@- # Cmdlets only. Constrained Language Mode refuses New-Object ProcessStartInfo and- # [Process]::Start, and CLM is one of the two policies that make the emitted rung decline,- # so this launcher has to work on exactly the hosts that need it most.- $tempRoot = if ($env:TEMP) { $env:TEMP } elseif ($env:TMPDIR) { $env:TMPDIR } else { "/tmp" }- $stem = Join-Path $tempRoot ("unsloth-nvprobe-" + [guid]::NewGuid().ToString("N"))+ # Cmdlets only: Constrained Language Mode refuses New-Object ProcessStartInfo and+ # [Process]::Start, and this launcher has to work there.+ $probeDir = New-StudioChildScriptDirectory+ $inline = (-not $probeDir)+ if ($inline) {+ # Elevated, a shared root lets a standard user plant or swap the redirect files: decline.+ try { if (Test-StudioChildScriptDirectoryElevated) { return "" } } catch { return "" }+ # The first root that takes a file: a TEMP that declined the directory may refuse this too.+ $stem = $null+ foreach ($root in @($env:TEMP, $env:TMP, $env:LOCALAPPDATA, $env:TMPDIR, "/tmp")) {+ if (-not $root) { continue }+ try {+ $candidateStem = Join-Path $root ("unsloth-nvprobe-" + [guid]::NewGuid().ToString("N")) -ErrorAction Stop+ $null = New-Item -ItemType File -Path "$candidateStem.out" -ErrorAction Stop; $stem = $candidateStem; break+ } catch {}+ }+ if (-not $stem) { return "" }+ } else {+ $stem = Join-Path $probeDir "nvprobe"+ }$scriptFile = "$stem.py"$outFile = "$stem.out"$errFile = "$stem.err"$raw = ""try {- Set-Content -LiteralPath $scriptFile -Value $probeSource -Encoding UTF8 -ErrorAction Stop+ if (-not $inline) { Set-Content -LiteralPath $scriptFile -Value $probeSource -Encoding UTF8 -ErrorAction Stop }# Whole seconds, rounded up, without [math]::Ceiling: CLM blocks it. PowerShell's / is# floating point and [int] rounds to nearest, so 10000ms must not become 11s.$seconds = ($TimeoutMs - ($TimeoutMs % 1000)) / 1000@@ -1723,19 +1973,28 @@ main()# each native argument verbatim, so a script under "C:\Users\First Last\AppData\Local\# Temp" or a hint under "C:\Program Files\NVIDIA Corporation\NVSMI" would split on its# spaces and the child would run something else. The script arrives on stdin and the two- # library hints in the environment; the only arguments left are -I -S and a bare dash.$savedNvml = $env:UNSLOTH_NVML_HINT$savedCuda = $env:UNSLOTH_CUDA_HINT$savedSkip = $env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML+ $savedSource = $env:UNSLOTH_NVIDIA_PROBE_SOURCE$env:UNSLOTH_NVML_HINT = $nvmlHint$env:UNSLOTH_CUDA_HINT = $cudaHint# The switch reaches this child only. An inherited value must not make a first child skip NVML.if ($SkipNvml) { $env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML = "1" }else { Remove-Item Env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML -ErrorAction SilentlyContinue }try {- $proc = Start-Process -FilePath $exe -ArgumentList @("-I", "-S", "-") -NoNewWindow -PassThru `- -RedirectStandardInput $scriptFile -RedirectStandardOutput $outFile -RedirectStandardError $errFile -ErrorAction Stop+ if ($inline) {+ $env:UNSLOTH_NVIDIA_PROBE_SOURCE = $probeSource+ $proc = Start-Process -FilePath $exe -NoNewWindow -PassThru `+ -ArgumentList @("-I", "-S", "-B", "-c", "exec(__import__('os').environ['UNSLOTH_NVIDIA_PROBE_SOURCE'])") `+ -RedirectStandardOutput $outFile -RedirectStandardError $errFile -ErrorAction Stop+ } else {+ $proc = Start-Process -FilePath $exe -ArgumentList @("-I", "-S", "-B", "-") -NoNewWindow -PassThru `+ -RedirectStandardInput $scriptFile -RedirectStandardOutput $outFile -RedirectStandardError $errFile -ErrorAction Stop+ }} finally {+ if ($null -eq $savedSource) { Remove-Item Env:UNSLOTH_NVIDIA_PROBE_SOURCE -ErrorAction SilentlyContinue }+ else { $env:UNSLOTH_NVIDIA_PROBE_SOURCE = $savedSource }if ($null -eq $savedNvml) { Remove-Item Env:UNSLOTH_NVML_HINT -ErrorAction SilentlyContinue }else { $env:UNSLOTH_NVML_HINT = $savedNvml }if ($null -eq $savedCuda) { Remove-Item Env:UNSLOTH_CUDA_HINT -ErrorAction SilentlyContinue }@@ -1761,6 +2020,9 @@ main()$raw = "$(Get-Content -LiteralPath $outFile -Raw -ErrorAction SilentlyContinue)"} catch { return "" }finally {+ # The directory, not just the three files: it is ours, nothing else may be in it, and+ # leaving an empty one behind per probe would litter %TEMP% on every run.+ if ($probeDir) { Remove-Item -LiteralPath $probeDir -Recurse -Force -ErrorAction SilentlyContinue }foreach ($stale in @($scriptFile, $outFile, $errFile)) {Remove-Item -LiteralPath $stale -Force -ErrorAction SilentlyContinue}@@ -1769,89 +2031,14 @@ main()}# "source;cudaMajor;cudaMinor;cap,cap" from NVML, else the CUDA driver API; "" when neither-# answers. Versions are major*1000 + minor*10. One runspace + deadline per reader: a shared one let slow NVML starve CUDA.-# A wedged driver can block inside the library, and each deadline leaves its runspace behind.-# The Python rung below spends what the emitted rung left of ONE budget (a per-reader bound for-# NVML plus one for CUDA), so the worst-case wall clock is the emitted rung's own. Its first child-# reads NVML then the CUDA driver API under at most one per-reader bound; only when that child is-# killed at its bound (a hung NVML) and at least 2 s remain does a second child read the CUDA-# driver API alone with the rest, so a hung NVML no longer starves a healthy CUDA driver API.function Read-NvidiaLibraryRaw {param([int]$TimeoutMs = 30000)+ # One deadline for both children, as the two in-process readers had: the first gets a whole+ # bound, and only a hung NVML earns a CUDA-only child with what is left, less the 2 s kept back+ # to reap the child it may have to kill. Compared by hand: CLM refuses [math].$deadline = (Get-Date).AddMilliseconds($TimeoutMs * 2)- $native = ""- $type = Get-NvidiaLibraryProbeType- if ($type) {- $reader = {- param($T, $Which)- function Read-Nvml {- if ($T::nvmlInit_v2() -ne 0) { return "" }- try {- [uint32]$count = 0- if ($T::nvmlDeviceGetCount_v2([ref]$count) -ne 0 -or $count -eq 0) { return "" }- [int]$ver = 0- if ($T::nvmlSystemGetCudaDriverVersion_v2([ref]$ver) -ne 0 -or $ver -lt 1000) { return "" }- $caps = @()- for ([uint32]$i = 0; $i -lt $count; $i++) {- [IntPtr]$dev = [IntPtr]::Zero; [int]$major = 0; [int]$minor = 0- # One unreadable GPU voids the source: a partial list misleads the pre-Turing cap.- if ($T::nvmlDeviceGetHandleByIndex_v2($i, [ref]$dev) -ne 0) { return "" }- if ($T::nvmlDeviceGetCudaComputeCapability($dev, [ref]$major, [ref]$minor) -ne 0) { return "" }- $caps += "$major.$minor"- }- return "nvml;$([int][math]::Floor($ver / 1000));$([int][math]::Floor(($ver % 1000) / 10));$($caps -join ',')"- } finally { $null = $T::nvmlShutdown() }- }- function Read-Cuda {- # The driver API honours CUDA_VISIBLE_DEVICES; the inventory must be the physical one,- # so a hidden pre-Turing card still caps the family. cuInit reads the mask once.- $saved = $env:CUDA_VISIBLE_DEVICES- Remove-Item Env:CUDA_VISIBLE_DEVICES -ErrorAction SilentlyContinue- try { $init = $T::cuInit([uint32]0) } finally { if ($null -ne $saved) { $env:CUDA_VISIBLE_DEVICES = $saved } }- if ($init -ne 0) { return "" }- [int]$count = 0- if ($T::cuDeviceGetCount([ref]$count) -ne 0 -or $count -eq 0) { return "" }- [int]$ver = 0- if ($T::cuDriverGetVersion([ref]$ver) -ne 0 -or $ver -lt 1000) { return "" }- $caps = @()- for ($i = 0; $i -lt $count; $i++) {- [int]$dev = 0; [int]$major = 0; [int]$minor = 0- if ($T::cuDeviceGet([ref]$dev, $i) -ne 0) { return "" }- # CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR = 75, _MINOR = 76.- if ($T::cuDeviceGetAttribute([ref]$major, 75, $dev) -ne 0) { return "" }- if ($T::cuDeviceGetAttribute([ref]$minor, 76, $dev) -ne 0) { return "" }- $caps += "$major.$minor"- }- return "cuda;$([int][math]::Floor($ver / 1000));$([int][math]::Floor(($ver % 1000) / 10));$($caps -join ',')"- }- $r = ""- try { if ($Which -eq "nvml") { $r = Read-Nvml } else { $r = Read-Cuda } } catch { $r = "" }- return "$r"- }- foreach ($which in @("nvml", "cuda")) {- $ps = $null; $handle = $null; $r = ""- try {- $ps = [powershell]::Create()- $null = $ps.AddScript($reader.ToString()).AddArgument($type).AddArgument($which)- $handle = $ps.BeginInvoke()- if ($handle.AsyncWaitHandle.WaitOne($TimeoutMs)) {- $r = "$(@($ps.EndInvoke($handle)) | Select-Object -Last 1)"- }- } catch { $r = "" }- finally { if ($ps -and $handle -and $handle.IsCompleted) { $ps.Dispose() } }- if ($r) { $native = $r; break }- }- }- if ($native) { return $native }- $remainingMs = [int]($deadline - (Get-Date)).TotalMilliseconds- # Not worth a child process we cannot wait out; the emitted rung already spent the budget.- if ($remainingMs -lt 3000) { return "" }- # Each child gets at most one per-reader bound, compared by hand: CLM refuses [math]. 2 s of-# every child's share is kept back for reaping it if it hangs, so the pair ends inside the deadline.- $childMs = $remainingMs - 2000; if ($childMs -gt $TimeoutMs) { $childMs = $TimeoutMs }$raw = ""- try { $raw = Read-NvidiaLibraryRawViaPython -TimeoutMs $childMs } catch { return "" }- # Only a first child killed at its bound (a hung NVML) earns a CUDA-only child, with what is left.+ try { $raw = Read-NvidiaLibraryRawViaPython -TimeoutMs $TimeoutMs } catch { return "" }if (-not $script:NvidiaPythonProbeTimedOut) { return $raw }$remainingMs = [int]($deadline - (Get-Date)).TotalMillisecondsif ($remainingMs -lt 3000) { return "" }@@ -1869,6 +2056,10 @@ function Get-NvidiaLibraryInventory {$script:NvidiaLibraryInventory = $nullif ("$($env:UNSLOTH_NVIDIA_LIBRARY_PROBE)".Trim() -eq "0") { return $null }try { $raw = Read-NvidiaLibraryRaw -TimeoutMs ($TimeoutSec * 1000) } catch { return $null }+ # The Python probe is the only library reader, so its opt-outs leave nvidia-smi as the only source.+ if (-not "$raw" -and ("$($env:UNSLOTH_NVIDIA_PYTHON_PROBE)".Trim() -eq "0" -or "$($env:UNSLOTH_EARLY_PYTHON_PROBE)".Trim() -eq "0")) {+ Write-StudioLine " NVIDIA driver libraries not read (Python probe disabled): a GPU without a working nvidia-smi is not detected. Set UNSLOTH_TORCH_INDEX_FAMILY=cu128 (or your CUDA wheel) to choose one." -ForegroundColor Yellow+ }$parts = "$raw".Split(";")if ($parts.Count -ne 4 -or -not $parts[3] -or [int]$parts[1] -lt 1) { return $null }$caps = @($parts[3].Split(","))@@ -2658,261 +2849,6 @@ function Ensure-VCRedist {# ─────────────────────────────────────────────$Rule = [string]::new([char]0x2500, 52)-# Native declarations are emitted, never compiled. Add-Type on Windows PowerShell 5.1 has no-# in-process compiler: -TypeDefinition and -MemberDefinition alike write C# to %TEMP% and run-# csc.exe, and security software blocks the result. Reflection emit builds the same stub in-# memory: no compiler process, no source, no DLL, empty assembly Location. install.ps1 carries-# the same helper for the same reason, and which product blocked what is recorded in-# tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)-# See install.ps1: App Control's Dynamic Code Security always blocks loading unsigned-# System.Reflection.Emit assemblies by usually stopping or crashing the parent rather than-# raising, so this has to be a gate and not a catch.-$script:StudioCanDefineNativeTypes = $null-# Why the last probe answered as it did, so a caller can tell "the child ran and-# said no" (a policy) from "the child never answered" (failed to start, killed-# at the deadline, or lost its output). Same boolean, different facts.-$script:StudioEmitProbeOutcome = $null-function Test-StudioCanDefineNativeTypes {- if ($null -ne $script:StudioCanDefineNativeTypes) { return $script:StudioCanDefineNativeTypes }- $languageMode = "FullLanguage"- try { $languageMode = [string]$ExecutionContext.SessionState.LanguageMode } catch {}- if ($languageMode -ne "FullLanguage") {- $script:StudioCanDefineNativeTypes = $false- return $false- }- # Read-and-zero is the only outcome that skips the probe. A query that threw, returned- # nothing, or lacked the property is UNKNOWN, and treating unknown as unrestricted lets- # option 19 through on a host whose CIM query failed. install.ps1 carries the full note.- $known = $false- $active = $false- try {- # -OperationTimeoutSec bounds the CIM operation on a responsive target only:- # it does not interrupt DCOM connection setup, and a wedged provider's own- # timeout wins. Good for the slow case, not a hang guard. The child probe- # below carries the real deadline.- $guard = Get-CimInstance -Namespace "root\Microsoft\Windows\DeviceGuard" `- -ClassName "Win32_DeviceGuard" -OperationTimeoutSec 10 -ErrorAction Stop- # 0 off, 1 audit, 2 enforced. A null property is not a zero.- if ($guard -and $null -ne $guard.UsermodeCodeIntegrityPolicyEnforcementStatus) {- $known = $true- if ([int]$guard.UsermodeCodeIntegrityPolicyEnforcementStatus -ne 0) {- $active = $true- }- }- } catch {}- if ($known -and -not $active) {- $script:StudioCanDefineNativeTypes = $true- return $true- }- # Which policy decides this, and Win32_DeviceGuard does not say. Option 19 Dynamic Code- # Security always blocks unsigned System.Reflection.Emit assemblies and is enforced even in- # an audit policy before Windows 11 24H2, while an audit policy without it emits fine. So a- # child process tries it. Same reasoning as install.ps1, which carries the full note.- $script:StudioCanDefineNativeTypes = Test-StudioEmitInChildProcess- # One retry, only when the first attempt never reached an answer (the- # compiled version this replaces also tried twice before caching a- # negative). Otherwise one transient process failure is cached for the whole- # run as if it were a policy, sending the installer down the lexical path- # where two unequal roots compare as unknown and a second lock gets taken. A- # child that RAN and said no is not retried, so a blocked machine pays for- # one probe.- if (-not $script:StudioCanDefineNativeTypes -and- $script:StudioEmitProbeOutcome -eq "indeterminate") {- $script:StudioCanDefineNativeTypes = Test-StudioEmitInChildProcess- }- return $script:StudioCanDefineNativeTypes-}--# The same emit, in a process that is allowed to die. A blocked dynamic load usually stops the-# parent, so this is asked in a child; silence is refusal.-function Test-StudioEmitInChildProcess {- # HostPath is for the tests, which have no policy to trigger the real path and- # cannot shadow the read-only $PSHOME. Production never passes it.- param([string]$HostPath)- # Until something below establishes otherwise.- $script:StudioEmitProbeOutcome = "indeterminate"- $probe = @'-try {- $name = New-Object System.Reflection.AssemblyName 'UnslothStudioEmitProbe'- $access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run- $assembly = $null- try { $assembly = [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly($name, $access) }- catch { $assembly = [AppDomain]::CurrentDomain.DefineDynamicAssembly($name, $access) }- $module = $assembly.DefineDynamicModule('UnslothStudioEmitProbe')- $builder = $module.DefineType('UnslothStudioEmitProbe', 'Public, Class, AutoClass, AnsiClass, BeforeFieldInit')- $method = $builder.DefinePInvokeMethod('CloseHandle', 'kernel32.dll', 'CloseHandle',- 'Public, Static, HideBySig, PinvokeImpl',- [System.Reflection.CallingConventions]::Standard, [bool], @([IntPtr]),- [System.Runtime.InteropServices.CallingConvention]::Winapi,- [System.Runtime.InteropServices.CharSet]::Ansi)- $method.SetImplementationFlags(- $method.GetMethodImplementationFlags() -bor [System.Reflection.MethodImplAttributes]::PreserveSig)- $null = $builder.CreateType()-} catch {}-# Outside the try, because CreateType can publish the type and then throw on the way-# back, and a published type works. The parent recovers from exactly that; a check-# inside the try answered no for a machine that had just succeeded.-# One line, and no closing brace in column 0: this body sits inside a here-string that-# starts at column 0 in both entrypoints, and the tests extract a function by finding the-# first line that is exactly its closing brace. A block here ends the extraction early.-if ('UnslothStudioEmitProbe' -as [type]) { Write-Output ('STUDIO_EMIT_OK ' + [string]$ExecutionContext.SessionState.LanguageMode); exit 0 }-exit 1-'@- # This host, not a guessed one: a 5.1 answer does not carry to pwsh or back.- # Both spellings of the leaf, so a non-Windows lane can execute this function- # end to end rather than leaving a Windows-only path untested.- $hostExe = $HostPath- if (-not $hostExe) {- try {- $leaves = if ($PSVersionTable.PSEdition -eq "Core") { @("pwsh.exe", "pwsh") }- else { @("powershell.exe", "powershell") }- foreach ($leaf in $leaves) {- $candidate = Join-Path $PSHOME $leaf- if (Test-Path -LiteralPath $candidate) { $hostExe = $candidate; break }- }- } catch {}- }- if (-not $hostExe) { return $false }- # A Process object rather than the call operator, for a deadline: the call- # operator waits forever, and forever is reachable (a security product- # inspecting a fresh interpreter, a wedged runtime start, a child blocking on- # shutdown). A probe meant to keep the installer alive must not hang it.- #- # BOTH streams are redirected and drained asynchronously. Draining stops a- # chatty child filling a pipe and deadlocking against the wait. Redirecting- # stderr keeps the probe out of the installer's own stderr, which the desktop- # app reads and anything the child spawns would inherit and hold open.- #- $info = New-Object System.Diagnostics.ProcessStartInfo- $info.FileName = $hostExe- $info.Arguments = "-NoProfile -NonInteractive -Command `"$probe`""- $info.UseShellExecute = $false- $info.RedirectStandardOutput = $true- $info.RedirectStandardError = $true- $info.CreateNoWindow = $true- $child = $null- try {- $child = [System.Diagnostics.Process]::Start($info)- $reader = $child.StandardOutput.ReadToEndAsync()- $null = $child.StandardError.ReadToEndAsync()- if (-not $child.WaitForExit(20000)) {- try { $child.Kill() } catch {}- return $false- }- # Exit code AND an exact record. A marker followed by a crash is a crash:- # the question is whether this machine can emit and live. FullLanguage- # because an approved script can run in FullLanguage while a fresh inline- # command does not, and a child restricted differently from its parent- # has measured a different machine.- if ($child.ExitCode -ne 0) {- $script:StudioEmitProbeOutcome = "blocked"- return $false- }- $lines = ($reader.GetAwaiter().GetResult() -split "`r?`n")- foreach ($line in $lines) {- if ($line.Trim() -eq "STUDIO_EMIT_OK FullLanguage") {- $script:StudioEmitProbeOutcome = "ok"- return $true- }- # Emitted, but in a language mode this parent is not in: the child- # measured a different machine, which is an answer, not a miss.- if ($line.Trim() -like "STUDIO_EMIT_OK *") {- $script:StudioEmitProbeOutcome = "blocked"- return $false- }- }- # Exit 0 with no marker: the child cannot have emitted and reported- # nothing, so its output was lost rather than negative.- return $false- } catch {- return $false- } finally {- if ($child) {- # The read end goes first: a killed child can leave a grandchild- # holding the write end, and the pending async read would then keep- # this process alive past the deadline it just enforced.- try { $child.StandardOutput.Close() } catch {}- try { $child.StandardError.Close() } catch {}- try { $child.Dispose() } catch {}- }- }-}--function New-StudioDynamicAssembly {- <#- Both spellings of "define a dynamic assembly", because the two PowerShell hosts- that run this file are on different runtimes. The static- AssemblyBuilder::DefineDynamicAssembly is documented for .NET Framework 4.5- through 4.8.1 as well as .NET Core, so 5.1 should take the first branch; it is- tried rather than assumed because nothing here can test a .NET Framework host- and getting it wrong is invisible: the catch would cache the thunk as- unavailable and every install would silently lose it.-- AppDomain.CurrentDomain.DefineDynamicAssembly is the .NET Framework spelling and- is absent on .NET Core, so it is the fallback.- #>- param([Parameter(Mandatory = $true)][System.Reflection.AssemblyName]$AssemblyName)- $access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run- try {- return [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly(- $AssemblyName, $access)- } catch [System.Management.Automation.MethodException] {- return [AppDomain]::CurrentDomain.DefineDynamicAssembly($AssemblyName, $access)- } catch [System.Management.Automation.RuntimeException] {- # Some hosts surface a missing static as RuntimeException, not- # MethodException. Both mean "no such method here", and a real emit failure- # throws from the AppDomain call too, so the caller still sees it.- return [AppDomain]::CurrentDomain.DefineDynamicAssembly($AssemblyName, $access)- }-}--function New-StudioEmittedNativeType {- param(- [Parameter(Mandatory = $true)][string]$TypeName,- [Parameter(Mandatory = $true)][object[]]$Imports- )- $assemblyName = New-Object System.Reflection.AssemblyName $TypeName- $assembly = New-StudioDynamicAssembly -AssemblyName $assemblyName- $module = $assembly.DefineDynamicModule($TypeName)- $builder = $module.DefineType(- $TypeName, "Public, Class, AutoClass, AnsiClass, BeforeFieldInit")-- $winapi = [System.Runtime.InteropServices.CallingConvention]::Winapi- # Per import, because CharSet selects name mangling as well as marshalling:- # Unicode probes <Name>W before <Name>, Ansi probes <Name> before <Name>A.- # Matching the C# these replace keeps the metadata honest and tries the existing- # export first.- $unicode = [System.Runtime.InteropServices.CharSet]::Unicode- $ansi = [System.Runtime.InteropServices.CharSet]::Ansi- $standard = [System.Reflection.CallingConventions]::Standard- $attributes = "Public, Static, HideBySig, PinvokeImpl"- $preserveSig = [System.Reflection.MethodImplAttributes]::PreserveSig-- foreach ($import in $Imports) {- $charSet = if ($import.ContainsKey("Ansi") -and $import.Ansi) { $ansi } else { $unicode }- $method = $builder.DefinePInvokeMethod(- $import.Name, $import.Library, $import.Name, $attributes,- $standard, $import.Return, $import.Args, $winapi, $charSet)- $method.SetImplementationFlags(- $method.GetMethodImplementationFlags() -bor $preserveSig)- # `out uint` in the C# this replaces; DefinePInvokeMethod cannot say so,- # since a by-ref type alone emits `ref` (In and Out unset). The value is- # blittable and every caller initialises it, so marshalling works either- # way, but the metadata is what a reader and any future marshalling- # change go by.- # ContainsKey, not a bare property read: most imports have no Out key and- # reading a missing one is fatal under Set-StrictMode. install.ps1 turns- # strict mode off for itself, studio/setup.ps1 inherits the caller's, so- # the guard is mirrored rather than left to one of them.- if ($import.ContainsKey("Out")) {- foreach ($position in @($import.Out)) {- if ($position) { $null = $method.DefineParameter($position, "Out", $null) }- }- }- }- $null = $builder.CreateType()- return $null -ne ($TypeName -as [type])-}function Enable-StudioVirtualTerminal {if ($env:NO_COLOR) { return $false }diff --git a/tests/python/test_installer_helper_declaration_order.py b/tests/python/test_installer_helper_declaration_order.pynew file mode 100644index 00000000000..a123bb5071e--- /dev/null+++ b/tests/python/test_installer_helper_declaration_order.py@@ -0,0 +1,205 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+"""A helper must be declared before the statement that reaches it.++All of install.ps1 is one function, so its nested `function` statements are ordinary statements+that run in order. A helper declared further down the file does not exist yet when an earlier+statement calls it, and the call raises CommandNotFoundException.++That is not a loud failure here. Every rung in these ladders is wrapped in try/catch precisely so+a rung that cannot run on this host is read as "declined" and the one below it is used, so a+helper placed below its own caller degrades the whole rung silently on every host: the batched+path resolver falls back to one interpreter per path, and the cmdlet launcher that exists for+Constrained Language Mode rejects every interpreter it is given. Observed exactly that way.++Declaration order is invisible in a diff and there is no runtime signal for it, which is why it is+checked statically here.+"""++from __future__ import annotations++import re+from pathlib import Path++import pytest++ROOT = Path(__file__).resolve().parents[2]+INSTALL_PS1 = ROOT / "install.ps1"++CHECKED = (+ "New-StudioChildScriptDirectory",+ "Test-StudioChildScriptDirectoryElevated",+ "Test-StudioInterpreterFileIsAdminOnly",+ "Invoke-StudioEarlyPythonScript",+ "Resolve-StudioFinalPathsInOneChild",+ "Get-StudioPythonFinalPath",+ "Get-StudioPythonProcessImageTable",+ "Read-NvidiaLibraryRawViaPython",+)++SOURCE = INSTALL_PS1.read_text(encoding = "utf-8")+++def _blank_here_strings(text: str) -> str:+ """Blank out @' ... '@ and @" ... "@ bodies, keeping every offset where it was.++ Those bodies are Python, and Python has braces of its own. Counted as PowerShell braces they+ swallow the rest of the file, and the containment tests below then read almost everything as+ being inside some helper. Found here as exactly that: 26 KB of 608 KB looked top-level.+ """+ out, inside = [], False+ for line in text.split("\n"):+ if inside:+ if line.strip() in ("'@", '"@'):+ inside = False+ out.append(line)+ else:+ out.append(" " * len(line))+ continue+ stripped = line.rstrip()+ if stripped.endswith("@'") or stripped.endswith('@"'):+ inside = True+ out.append(line)+ return "\n".join(out)+++def _strip_comments(text: str) -> str:+ """Blank out comment text, keeping every offset where it was.++ The comments in this file NAME these helpers while explaining them, and a name read out of a+ comment would make a declaration look reachable from a statement that does not call it.+ Replacing rather than deleting keeps the offsets comparable with the raw source.+ """+ out = []+ for line in text.split("\n"):+ hash_at = line.find("#")+ if hash_at == -1:+ out.append(line)+ else:+ out.append(line[:hash_at] + " " * (len(line) - hash_at))+ return "\n".join(out)+++CODE = _strip_comments(_blank_here_strings(SOURCE))+++def _nested_functions(code: str) -> dict[str, tuple[int, int]]:+ """Every `function NAME {` nested one level inside Install-UnslothStudio, with its extent.++ Brace matching rather than a scan to the next `function` keyword, which overshoots into the+ following helper and makes every containment test below wrong in the permissive direction.+ """+ found: dict[str, tuple[int, int]] = {}+ for match in re.finditer(r"(?m)^ function ([A-Za-z0-9\-]+) \{", code):+ start = match.start()+ depth, i = 0, code.index("{", match.start())+ while True:+ if code[i] == "{":+ depth += 1+ elif code[i] == "}":+ depth -= 1+ if depth == 0:+ break+ i += 1+ found[match.group(1)] = (start, i + 1)+ return found+++FUNCTIONS = _nested_functions(CODE)+++def _calls_within(start: int, end: int) -> set[str]:+ """Which of the file's own helpers this span names, ignoring the declarations inside it."""+ span = CODE[start:end]+ inner = {m.group(1) for m in re.finditer(r"(?m)^\s*function ([A-Za-z0-9\-]+) \{", span)}+ return {+ name+ for name in FUNCTIONS+ if name not in inner+ and re.search(rf"(?<![A-Za-z0-9\-]){re.escape(name)}(?![A-Za-z0-9\-])", span)+ }+++CALLS = {name: _calls_within(start, end) for name, (start, end) in FUNCTIONS.items()}+++def _guarded_within(start: int, end: int) -> set[str]:+ """Helpers this span calls only behind `Get-Command NAME -CommandType Function`."""+ return set(+ re.findall(r"Get-Command ([A-Za-z0-9\-]+) -CommandType Function", CODE[start:end])+ ) & set(FUNCTIONS)+++GUARDED = {name: _guarded_within(start, end) for name, (start, end) in FUNCTIONS.items()}+++def _top_level_spans() -> list[tuple[int, int]]:+ """Everything in the file that is not inside one of those nested declarations.++ These are the statements that run in order, so the offset of the first one that can reach a+ helper is the deadline that helper's declaration has to beat.+ """+ holes = sorted(FUNCTIONS.values())+ spans, cursor = [], 0+ for start, end in holes:+ if start > cursor:+ spans.append((cursor, start))+ cursor = max(cursor, end)+ spans.append((cursor, len(CODE)))+ return spans+++def _reaches(+ name: str,+ at: int = len(CODE),+ seen: frozenset[str] = frozenset(),+) -> set[str]:+ """Every helper that calling `name` from offset `at` can end up in, including itself.++ A guarded call runs only once its target is declared, so it is no edge before that.+ """+ if name in seen:+ return set()+ out = {name}+ for callee in CALLS.get(name, ()): # noqa: SIM118 - CALLS may not carry every name+ if callee in GUARDED.get(name, ()) and FUNCTIONS[callee][0] > at:+ continue+ out |= _reaches(callee, at, seen | {name})+ return out++[email protected]("name", CHECKED)+def test_the_helper_is_declared_before_anything_that_reaches_it(name: str):+ assert name in FUNCTIONS, f"{name} is not declared inside install.ps1"+ declared = FUNCTIONS[name][0]+ for start, end in _top_level_spans():+ if start > declared:+ continue+ guarded = _guarded_within(start, end)+ for called in sorted(_calls_within(start, end)):+ if called in guarded and FUNCTIONS[called][0] > start:+ continue+ if name in _reaches(called, start):+ line = CODE[:start].count("\n") + 1+ assert declared < start, (+ f"{name} is declared at line {CODE[:declared].count(chr(10)) + 1}, but the "+ f"statement at line {line} reaches it through {called}. PowerShell runs these "+ f"declarations in order, so the call raises CommandNotFoundException and the "+ f"rung is read as declined."+ )+++def test_the_rule_is_not_vacuous():+ """The scan really does find declarations and really does find top-level callers.++ Without this, a regex that matched nothing would leave every row above passing on an empty+ set, which is the failure mode this whole file exists to prevent.+ """+ assert len(FUNCTIONS) > 50, f"only {len(FUNCTIONS)} nested functions found"+ reached = set()+ for start, end in _top_level_spans():+ for called in _calls_within(start, end):+ reached |= _reaches(called)+ assert (+ set(CHECKED) <= reached+ ), f"not reachable from any top-level statement: {sorted(set(CHECKED) - reached)}"diff --git a/tests/python/test_windows_installer_concurrency_guard.py b/tests/python/test_windows_installer_concurrency_guard.pyindex 575e75e1be5..d398caf6082 100644--- a/tests/python/test_windows_installer_concurrency_guard.py+++ b/tests/python/test_windows_installer_concurrency_guard.py@@ -88,117 +88,153 @@ _FINAL_PATH_CHAIN = ("New-StudioPrivateTempDirectory","Initialize-StudioTempEnvironment","Write-StudioFinalPathDegraded",- "Test-StudioCanDefineNativeTypes",- "Test-StudioEmitInChildProcess",- "New-StudioDynamicAssembly",- "New-StudioEmittedNativeType",- "Initialize-StudioFinalPathNativeType",- "Get-StudioNativeFinalPath","Resolve-StudioLinkTarget","Get-StudioSubstTarget","Get-ElevationState","Get-StudioEarlyPython","Invoke-StudioEarlyPythonScript",+ "New-StudioChildScriptDirectory",+ "Test-StudioChildScriptDirectoryElevated",+ "Test-StudioPathUnderAdminRoot",+ "Test-StudioSddlRightsAreWrite",+ "Test-StudioSddlPrincipalIsAdminOnly",+ "Test-StudioSddlWritableByNonAdmin",+ "Test-StudioDirectoryIsAdminOnly",+ "Test-StudioInterpreterFileIsAdminOnly",+ "Get-StudioLexicalParent",+ "Invoke-StudioSystem32ToolBounded",+ "Get-StudioSystem32Tool","Test-StudioPlainFile","Test-UnslothCmdShimFile","Invoke-StudioEarlyPython","Get-StudioPythonFinalPath",+ "Resolve-StudioFinalPathsInOneChild","Get-StudioLexicalPath","Resolve-StudioFinalPathInfo","Get-StudioFinalPath",)+# Hosted Windows runners are elevated, and an elevated run declines a user-writable interpreter and+# takes every lock. These cases are about lock identity, so they model a standard user; the elevated+# gate itself is covered in test_windows_installer_resolver_fallback.py.+_STANDARD_USER = "function Test-StudioChildScriptDirectoryElevated { return $false }\n"++def _final_path_helpers(source: str) -> str:- return "\n".join(- _extract(rf" function {name} \{{.*?\n \}}\n", source) for name in _FINAL_PATH_CHAIN+ return (+ "\n".join(+ _extract(rf" function {name} \{{.*?\n \}}\n", source)+ for name in _FINAL_PATH_CHAIN+ )+ + "\n"+ + _STANDARD_USER)def _mutex_helpers(source: str) -> str:- return "\n".join(- _extract(rf" function {name} \{{.*?\n \}}\n", source)- for name in (- # These scripts run under -ErrorActionPreference Stop, and Test-StudioPathEqual reports through- # Write-StudioLine. Extracted, not stubbed: a stub would keep passing if the real call went wrong.- "Write-StudioLine",- "Enter-StudioNamedMutex",- # Get-StudioFinalPath is a dispatcher: it falls back to the pure PowerShell resolver (#9140).- "Test-StudioDirectoryUsable",- "Remove-StudioStalePrivateTempDirectories",- "Get-StudioPrivateTempRoots",- "New-StudioPrivateTempDirectory",- "Initialize-StudioTempEnvironment",- "Write-StudioFinalPathDegraded",- "Initialize-StudioFinalPathNativeType",- "Test-StudioCanDefineNativeTypes",- "Test-StudioEmitInChildProcess",- "New-StudioDynamicAssembly",- "New-StudioEmittedNativeType",- "Get-StudioNativeFinalPath",- "Resolve-StudioLinkTarget",- "Get-StudioSubstTarget",- "Get-ElevationState",- "Get-StudioEarlyPython",- "Invoke-StudioEarlyPythonScript",- "Test-StudioPlainFile",- "Test-UnslothCmdShimFile",- "Invoke-StudioEarlyPython",- "Get-StudioPythonFinalPath",- "Get-StudioLexicalPath",- "Resolve-StudioFinalPathInfo",- "Get-StudioFinalPath",- "Get-StudioPathHash",- "Get-StudioInstallMutexName",- "Test-StudioPathEqual",- "Get-StudioRuntimeMutexNameForSid",- "Get-StudioRuntimePathHash",- "Get-StudioRuntimeMutexNameForPath",- "Get-StudioCurrentUserSid",- "Get-StudioRuntimeMutexName",- "Get-StudioRuntimeMutexNames",- "Enter-StudioInstallMutex",- "Exit-StudioInstallMutex",+ return (+ "\n".join(+ _extract(rf" function {name} \{{.*?\n \}}\n", source)+ for name in (+ # These scripts run under -ErrorActionPreference Stop, and Test-StudioPathEqual reports through+ # Write-StudioLine. Extracted, not stubbed: a stub would keep passing if the real call went wrong.+ "Write-StudioLine",+ "Enter-StudioNamedMutex",+ # Get-StudioFinalPath is a dispatcher: it falls back to the pure PowerShell resolver (#9140).+ "Test-StudioDirectoryUsable",+ "Remove-StudioStalePrivateTempDirectories",+ "Get-StudioPrivateTempRoots",+ "New-StudioPrivateTempDirectory",+ "Initialize-StudioTempEnvironment",+ "Write-StudioFinalPathDegraded",+ "Resolve-StudioLinkTarget",+ "Get-StudioSubstTarget",+ "Get-ElevationState",+ "Get-StudioEarlyPython",+ "Invoke-StudioEarlyPythonScript",+ "New-StudioChildScriptDirectory",+ "Test-StudioChildScriptDirectoryElevated",+ "Test-StudioPathUnderAdminRoot",+ "Test-StudioSddlRightsAreWrite",+ "Test-StudioSddlPrincipalIsAdminOnly",+ "Test-StudioSddlWritableByNonAdmin",+ "Test-StudioDirectoryIsAdminOnly",+ "Test-StudioInterpreterFileIsAdminOnly",+ "Get-StudioLexicalParent",+ "Invoke-StudioSystem32ToolBounded",+ "Get-StudioSystem32Tool",+ "Test-StudioPlainFile",+ "Test-UnslothCmdShimFile",+ "Invoke-StudioEarlyPython",+ "Get-StudioPythonFinalPath",+ "Resolve-StudioFinalPathsInOneChild",+ "Get-StudioLexicalPath",+ "Resolve-StudioFinalPathInfo",+ "Get-StudioFinalPath",+ "Get-StudioPathHash",+ "Get-StudioInstallMutexName",+ "Test-StudioPathEqual",+ "Get-StudioRuntimeMutexNameForSid",+ "Get-StudioRuntimePathHash",+ "Get-StudioRuntimeMutexNameForPath",+ "Get-StudioCurrentUserSid",+ "Get-StudioRuntimeMutexName",+ "Get-StudioRuntimeMutexNames",+ "Enter-StudioInstallMutex",+ "Exit-StudioInstallMutex",+ ))+ + "\n"+ + _STANDARD_USER)def _process_helpers(source: str) -> str:- return "\n".join(- _extract(rf" function {name} \{{.*?\n \}}\n", source)- for name in (- "Write-StudioLine",- "Test-StudioDirectoryUsable",- "Remove-StudioStalePrivateTempDirectories",- "Get-StudioPrivateTempRoots",- "New-StudioPrivateTempDirectory",- "Initialize-StudioTempEnvironment",- "Write-StudioFinalPathDegraded",- "Initialize-StudioFinalPathNativeType",- "Test-StudioCanDefineNativeTypes",- "Test-StudioEmitInChildProcess",- "New-StudioDynamicAssembly",- "New-StudioEmittedNativeType",- "Get-StudioNativeFinalPath",- "Resolve-StudioLinkTarget",- "Get-StudioSubstTarget",- "Get-ElevationState",- "Get-StudioEarlyPython",- "Invoke-StudioEarlyPythonScript",- "Test-StudioPlainFile",- "Test-UnslothCmdShimFile",- "Invoke-StudioEarlyPython",- "Get-StudioPythonFinalPath",- "Get-StudioLexicalPath",- "Resolve-StudioFinalPathInfo",- "Get-StudioFinalPath",- "Test-StudioProtectedPathMatch",- "Initialize-StudioProcessImageNativeType",- "Get-StudioPythonProcessImageTable",- "Get-StudioNativeProcessImagePath",- "Get-StudioProcessImagePath",- "Get-RunningStudioVenvProcesses",+ return (+ "\n".join(+ _extract(rf" function {name} \{{.*?\n \}}\n", source)+ for name in (+ "Write-StudioLine",+ "Test-StudioDirectoryUsable",+ "Remove-StudioStalePrivateTempDirectories",+ "Get-StudioPrivateTempRoots",+ "New-StudioPrivateTempDirectory",+ "Initialize-StudioTempEnvironment",+ "Write-StudioFinalPathDegraded",+ "Resolve-StudioLinkTarget",+ "Get-StudioSubstTarget",+ "Get-ElevationState",+ "Get-StudioEarlyPython",+ "Invoke-StudioEarlyPythonScript",+ "New-StudioChildScriptDirectory",+ "Test-StudioChildScriptDirectoryElevated",+ "Test-StudioPathUnderAdminRoot",+ "Test-StudioSddlRightsAreWrite",+ "Test-StudioSddlPrincipalIsAdminOnly",+ "Test-StudioSddlWritableByNonAdmin",+ "Test-StudioDirectoryIsAdminOnly",+ "Test-StudioInterpreterFileIsAdminOnly",+ "Get-StudioLexicalParent",+ "Invoke-StudioSystem32ToolBounded",+ "Get-StudioSystem32Tool",+ "Test-StudioPlainFile",+ "Test-UnslothCmdShimFile",+ "Invoke-StudioEarlyPython",+ "Get-StudioPythonFinalPath",+ "Resolve-StudioFinalPathsInOneChild",+ "Get-StudioLexicalPath",+ "Resolve-StudioFinalPathInfo",+ "Get-StudioFinalPath",+ "Test-StudioProtectedPathMatch",+ "Get-StudioPythonProcessImageTable",+ "Get-StudioWmiProcessImageRows",+ "Get-StudioProcessImagePath",+ "Get-RunningStudioVenvProcesses",+ ))+ + "\n"+ + _STANDARD_USER)@@ -385,7 +421,7 @@ def test_installer_ignores_command_line_and_cwd_only_path_mentions():@pytest.mark.skipif(os.name != "nt" or not POWERSHELLS, reason = "Windows PowerShell is required")@pytest.mark.parametrize("shell", POWERSHELLS)-def test_versioned_native_helper_loads_after_older_installer_type(shell: str):+def test_the_resolver_survives_an_older_installer_type_in_the_session(shell: str):source = INSTALL_PS1.read_text(encoding = "utf-8")final_path_helper = _mutex_helpers(source)script = f"""@@ -398,10 +434,9 @@ public static class UnslothStudioFinalPath'@{final_path_helper}$resolved = Get-StudioFinalPath -Path $env:SystemRoot-Write-Output ([bool]("UnslothStudioFinalPathV3" -as [type]))Write-Output ([bool]($resolved -and (Test-Path -LiteralPath $resolved)))"""- assert _run_powershell(shell, script, os.environ.copy()).splitlines() == ["True", "True"]+ assert _run_powershell(shell, script, os.environ.copy()).splitlines() == ["True"]@pytest.mark.skipif(os.name != "nt" or not POWERSHELLS, reason = "Windows PowerShell is required")diff --git a/tests/python/test_windows_installer_native_type_fallback.py b/tests/python/test_windows_installer_resolver_fallback.pysimilarity index 64%rename from tests/python/test_windows_installer_native_type_fallback.pyrename to tests/python/test_windows_installer_resolver_fallback.pyindex 7fb5f0ff782..224f836fab7 100644--- a/tests/python/test_windows_installer_native_type_fallback.py+++ b/tests/python/test_windows_installer_resolver_fallback.py@@ -1,29 +1,33 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.-"""The installer must survive a host where the native path helper cannot be built.+"""The installer must survive a host where no exact path resolver can be reached.-install.ps1 resolves path identity through three kernel32 imports. It used to get+install.ps1 used to resolve path identity through three kernel32 imports. It gotthem by compiling C#, and Windows PowerShell 5.1 -- the interpreterstudio/src-tauri/src/install.rs spawns -- compiles by writing the source into%TEMP% and running csc.exe. A %TEMP% that could not hold a file made Add-Typethrow CS2001, and that exception travelled up Get-StudioPathHash to end a firstlaunch as "Could not create the Unsloth install lock" (#9140). Behavioural-antivirus then blocked the DLL the compiler produced, which is a failure no-retry can route around, so the imports are emitted with DefinePInvokeMethod now-and no compiler runs at all.--Both halves are still guarded here. That no compiler is reachable is the first-half, and this file asserts it by sabotaging Add-Type and showing nothing changes.-That a host where even the emit fails still installs is the second, and it is the-same fallback the compile failure used to land on: a lexical answer, Exact false,-and a lock that is still acquired.--These run under pwsh on any platform. Emit succeeds here while kernel32 does not-resolve, so the type builds exactly as it does on 5.1 and every call through it-fails, which covers the build side and the call side separately. Windows-PowerShell 5.1 behaviour itself is exercised by the Windows-only tests in-test_windows_installer_concurrency_guard.py.+antivirus then blocked the DLL the compiler produced, which is a failure no retry+can route around, so the imports moved to DefinePInvokeMethod and no compiler ran+at all. Reflection emit is now gone too: Skyhigh scored the apparatus as the+largest single contributor to a BehavesLike.PS.Suspicious verdict on the shipped+installer, and removing it cleared that verdict on a matched pair.++What replaced it is a child interpreter. pathlib.Path.resolve is+GetFinalPathNameByHandleW on Windows, the same system call the imports made, so an+exact answer is still exact. Beneath it is the same fallback the compile failure+used to land on: a lexical answer, Exact = $false, and a lock that is still+acquired.++Both halves are still guarded here. That no compiler is reachable is the first, and+this file asserts it by sabotaging Add-Type and showing nothing changes. That a host+with no interpreter to ask still installs is the second, reached through the+UNSLOTH_EARLY_PYTHON_PROBE kill switch.++These run under pwsh on any platform. Windows PowerShell 5.1 behaviour itself is+exercised by the Windows-only tests in test_windows_installer_concurrency_guard.py."""from __future__ import annotations@@ -71,12 +75,7 @@ SABOTAGE = ("""function Add-Type { throw "(0) : error CS2001: Source file 'a.0.cs' could not be found" }""")-# What a host that cannot emit looks like. Constrained Language Mode and App Control's-# Dynamic Code Security both come through Test-StudioCanDefineNativeTypes, so overriding-# that gate is the whole of "no native side".-# The Python rung is off too: it would answer exactly on any host with an interpreter.-NO_NATIVE = """$env:UNSLOTH_EARLY_PYTHON_PROBE = "0"-function Test-StudioCanDefineNativeTypes { return $false }"""+NO_NATIVE = '$env:UNSLOTH_EARLY_PYTHON_PROBE = "0"'def _extract(pattern: str, source: str) -> str:@@ -100,17 +99,22 @@ LOCK_CHAIN = ("Initialize-StudioTempEnvironment","Restore-StudioTempEnvironment","Write-StudioFinalPathDegraded",- "Test-StudioCanDefineNativeTypes",- "Test-StudioEmitInChildProcess",- "New-StudioDynamicAssembly",- "New-StudioEmittedNativeType",- "Initialize-StudioFinalPathNativeType",- "Get-StudioNativeFinalPath","Resolve-StudioLinkTarget","Get-StudioSubstTarget","Get-ElevationState","Get-StudioEarlyPython","Invoke-StudioEarlyPythonScript",+ "New-StudioChildScriptDirectory",+ "Test-StudioChildScriptDirectoryElevated",+ "Invoke-StudioSystem32ToolBounded",+ "Get-StudioSystem32Tool",+ "Test-StudioPathUnderAdminRoot",+ "Test-StudioSddlRightsAreWrite",+ "Test-StudioSddlPrincipalIsAdminOnly",+ "Test-StudioSddlWritableByNonAdmin",+ "Test-StudioDirectoryIsAdminOnly",+ "Test-StudioInterpreterFileIsAdminOnly",+ "Get-StudioLexicalParent","Test-StudioPlainFile","Test-UnslothCmdShimFile","Invoke-StudioEarlyPython",@@ -156,11 +160,15 @@ def _run_powershell(script: str, env: dict[str, str] | None = None) -> subprocespass+NOT_ELEVATED = "function Test-StudioChildScriptDirectoryElevated { return $false }"++def _script(body: str,*,sabotage: bool = True,names: tuple[str, ...] = LOCK_CHAIN,+ elevated: bool = False,) -> str:return "\n".join([@@ -170,6 +178,9 @@ def _script("$script:StudioStdoutRedirected = $true",_helpers(*names),SABOTAGE if sabotage else "",+ "function Test-StudioChildScriptDirectoryElevated { return $true }"+ if elevated+ else NOT_ELEVATED,body,])@@ -183,9 +194,10 @@ def _lines(result: subprocess.CompletedProcess, prefix: str) -> list[str]:def test_install_lock_is_acquired_when_the_native_helper_is_unavailable(tmp_path: Path):"""The bug from #9140, at the rung it lives on now.- It was a throwing Add-Type then and it is a host that cannot define a type at- all now, which is the same thing to every caller: no exact answer. The lock- must still be acquired, and the reason said once.+ It was a throwing Add-Type then, then a host that could not define a type at all,+ and it is a host with no interpreter to ask now. All three are the same thing to+ every caller: no exact answer. The lock must still be acquired, and the reason+ said once."""studio_home = tmp_path / "studio"studio_home.mkdir()@@ -208,18 +220,108 @@ Exit-StudioInstallMutex -Mutex $mutexr"NAME:Global\\UnslothStudioInstall-[0-9a-f]{64}", _lines(result, "NAME:")[0])# And it says why it degraded, once, without dumping anything it tried to build.- warnings = [line for line in result.stdout.splitlines() if "native path resolver" in line]+ warnings = [+ line for line in result.stdout.splitlines() if "Could not resolve a path exactly" in line+ ]assert len(warnings) == 1assert "using System" not in result.stdout+def _fresh_install_statement() -> str:+ # The real statement, not a retyped copy: it is what decides whether a run is a first install.+ source = INSTALL_PS1.read_text(encoding = "utf-8")+ return _extract(r" \$script:StudioInstallIsFresh = -not \(.*?\)\)\n", source)+++def test_the_first_install_verdict_is_taken_before_anything_is_created() -> None:+ """Read where $VenvDir is first computed, before the install lock and before any path is+ resolved: read any later and this run's own venv would make every install look like an+ update, and the warning would come back on exactly the run it was silenced for."""+ source = INSTALL_PS1.read_text(encoding = "utf-8")+ verdict = source.index(_fresh_install_statement())+ assert source.index(' $VenvDir = Join-Path $StudioHome "unsloth_studio"') < verdict+ assert verdict < source.index("$studioInstallLock = Enter-StudioInstallLock -Path $StudioHome")+ # Initialised with the rest of the resolver state, so a second `irm | iex` in one session+ # does not inherit the first run's verdict.+ assert " $script:StudioInstallIsFresh = $null\n" in source+++@requires_pwsh+ "existing, warns",+ [+ pytest.param(None, False, id = "first_install_is_quiet"),+ pytest.param("unsloth_studio", True, id = "existing_venv_warns"),+ pytest.param("bin/unsloth.exe", True, id = "existing_launcher_warns"),+ pytest.param(".venv", True, id = "legacy_venv_warns"),+ ],+)+def test_a_python_less_first_install_prints_no_degraded_warning(+ tmp_path: Path, existing: str | None, warns: bool+):+ """main resolved paths in process, so a normal first install on a host with no Python+ printed nothing. The interpreter rung has nothing to run there, and the five-line yellow+ block it printed instead was the first thing a new user (Desktop's first run included)+ saw. It stays for an existing install, where two spellings of it escaping one another's+ lock is the thing the operator is being told about. The lock is the same in every row."""+ studio_home = tmp_path / "studio"+ studio_home.mkdir()+ profile = tmp_path / "profile"+ profile.mkdir()+ if existing is not None:+ target = studio_home / existing+ if existing.endswith(".exe"):+ target.parent.mkdir(parents = True)+ target.write_text("")+ else:+ target.mkdir(parents = True)+ result = _run_powershell(+ _script(+ f"""+{NO_NATIVE}+$env:USERPROFILE = '{profile}'+$script:StudioInstallIsFresh = $null+$StudioHome = '{studio_home}'+$VenvDir = Join-Path $StudioHome "unsloth_studio"+{_fresh_install_statement()}+Write-Output "FRESH:$script:StudioInstallIsFresh"+$info = Resolve-StudioFinalPathInfo -Path '{studio_home}'+Write-Output "EXACT:$($info.Exact)"+$other = Join-Path '{tmp_path}' 'elsewhere'+Write-Output "EQUAL:$([string](Test-StudioPathEqual -Left '{studio_home}' -Right $other) -eq '')"+$mutex = Enter-StudioInstallMutex -Path '{studio_home}'+Write-Output "LOCK:$($null -ne $mutex)"+Exit-StudioInstallMutex -Mutex $mutex+""",+ sabotage = False,+ )+ )+ assert result.returncode == 0, result.stderr+ assert _lines(result, "FRESH:") == [f"FRESH:{not warns}"]+ assert _lines(result, "EXACT:") == ["EXACT:False"]+ assert _lines(result, "EQUAL:") == ["EQUAL:True"], result.stdout+ assert _lines(result, "LOCK:") == ["LOCK:True"]+ degraded = [l for l in result.stdout.splitlines() if "Could not resolve a path exactly" in l]+ identity = [+ l for l in result.stdout.splitlines() if "Could not resolve Unsloth path identity" in l+ ]+ if warns:+ assert len(degraded) == 1, result.stdout+ assert len(identity) == 1, result.stdout+ else:+ assert not degraded, result.stdout+ assert not identity, result.stdout+ assert "[WARN]" not in result.stdout, result.stdout++@requires_pwshdef test_a_dead_compiler_is_not_something_the_installer_can_notice(tmp_path: Path):"""The other half, and the reason the file is named for a fallback it no longer needs.install.ps1 reaches no compiler, so replacing Add-Type with one that always throws- has to change nothing at all: the lock is taken, the native side is built anyway,- and no degraded warning is printed. A failure here means a compile came back.+ has to change nothing at all: the lock is taken, the path resolves exactly through+ the child interpreter, and no degraded warning is printed. A failure here means a+ compile came back."""studio_home = tmp_path / "studio"studio_home.mkdir()@@ -229,14 +331,17 @@ def test_a_dead_compiler_is_not_something_the_installer_can_notice(tmp_path: Pat$mutex = Enter-StudioInstallMutex -Path '{studio_home}'Write-Output "LOCK:$($null -ne $mutex)"Exit-StudioInstallMutex -Mutex $mutex-Write-Output "TYPE:$([bool]("UnslothStudioFinalPathV3" -as [type]))"+Write-Output "EXACT:$((Resolve-StudioFinalPathInfo -Path '{studio_home}').Exact)""""))assert result.returncode == 0, result.stderrassert _lines(result, "LOCK:") == ["LOCK:True"]- assert _lines(result, "TYPE:") == ["TYPE:True"]- assert not [line for line in result.stdout.splitlines() if "native path resolver" in line]+ # Exact, because the interpreter rung answered. A compiler never entered into it.+ assert _lines(result, "EXACT:") == ["EXACT:True"]+ assert not [+ line for line in result.stdout.splitlines() if "Could not resolve a path exactly" in line+ ]@requires_pwsh@@ -263,40 +368,57 @@ Write-Output "CALLS:$global:AddTypeCalls"@requires_pwsh-def test_the_type_is_built_once_however_many_paths_are_resolved(tmp_path: Path):- """The caching the retry count used to stand in for. The install scan resolves a- path per running process, so building the type per call would be paid dozens of- times."""+def test_the_interpreter_is_looked_for_once_however_many_paths_are_resolved(tmp_path: Path):+ """The caching the retry count used to stand in for, at the rung that has it now.++ Finding an interpreter means running a probe against every candidate on PATH until+ one answers, and that search is latched. Resolving a path still costs one child, so+ five resolutions cost five children plus the ONE search: six calls, not ten. Without+ the latch every resolution pays the search again, and on a host whose first few+ candidates are slow or broken that is the expensive half.++ Five DIFFERENT paths. The same path five times is served by the per-path answer cache+ after the first child, which would make this count two and hide whether the search is+ latched at all.+ """+ paths = []+ for i in range(5):+ (tmp_path / f"p{i}").mkdir()+ paths.append(f"'{tmp_path / f'p{i}'}'")result = _run_powershell(_script(f"""-$global:Emits = 0+$global:Probes = 0# A scriptblock copy, not Get-Item function:. A FunctionInfo re-resolves by NAME when# it is invoked, so calling it from the replacement calls the replacement.-$global:RealEmit = ${{function:New-StudioEmittedNativeType}}-function New-StudioEmittedNativeType {{- param([string]$TypeName, [object[]]$Imports)- $global:Emits++- & $global:RealEmit -TypeName $TypeName -Imports $Imports+$global:RealProbe = ${{function:Invoke-StudioEarlyPython}}+function Invoke-StudioEarlyPython {{+ param([string]$Exe, [string]$Path, [int]$TimeoutMs = 10000)+ $global:Probes+++ & $global:RealProbe -Exe $Exe -Path $Path -TimeoutMs $TimeoutMs}}-foreach ($i in 1..5) {{ Get-StudioFinalPath -Path '{tmp_path}' | Out-Null }}-Write-Output "EMITS:$global:Emits"+foreach ($p in @({', '.join(paths)})) {{ Get-StudioFinalPath -Path $p | Out-Null }}+Write-Output "PROBES:$global:Probes"""",sabotage = False,))assert result.returncode == 0, result.stderr- assert _lines(result, "EMITS:") == ["EMITS:1"]+ if _lines(result, "PROBES:") == ["PROBES:0"]:+ pytest.skip("no interpreter on this host, so there is no search to latch")+ assert _lines(result, "PROBES:") == ["PROBES:6"]@requires_pwsh-def test_the_native_helper_is_built_with_no_usable_temp_at_all(tmp_path: Path):- """What replaced the private-%TEMP% retry: nothing, because nothing needs %TEMP%.-- The retry existed because compiling wrote a source file there and a %TEMP% that- could not hold one failed the install. Emit writes nothing anywhere, so the same- unusable %TEMP% that used to need a retry is now simply irrelevant, and the- variables are handed back exactly as they arrived.+def test_a_path_still_resolves_with_no_usable_temp_at_all(tmp_path: Path):+ """What replaced the private-%TEMP% retry: a ladder that cannot be ended by %TEMP%.++ The retry existed because compiling wrote a source file there, and a %TEMP% that+ could not hold one failed the install outright (#9140). The rungs left do use %TEMP%+ -- the child interpreter is handed its script through a file -- so an unusable one+ can still cost the EXACT answer. What it may no longer cost is the install: the+ ladder drops to the lexical rung, says so, and hands the caller's broken variables+ back exactly as they arrived rather than repairing them behind its back."""blocker = tmp_path / "blocker"blocker.write_text("not a directory", encoding = "utf-8")@@ -316,8 +438,6 @@ def test_the_native_helper_is_built_with_no_usable_temp_at_all(tmp_path: Path):# Skip the session-wide temp fix, so nothing but the emit itself can save this.$script:StudioTempChecked = $true$info = Resolve-StudioFinalPathInfo -Path '{studio_home}'-Write-Output "LOADED:$([bool]("UnslothStudioFinalPathV3" -as [type]))"-Write-Output "INMEMORY:$([string]::IsNullOrEmpty(("UnslothStudioFinalPathV3" -as [type]).Assembly.Location))"Write-Output "PATH:$($info.Path)"Write-Output "TMP:$env:TMP"Write-Output "TEMP:$env:TEMP"@@ -327,15 +447,10 @@ Write-Output "TEMP:$env:TEMP"env = env,)assert result.returncode == 0, result.stderr- assert _lines(result, "LOADED:") == ["LOADED:True"]- assert _lines(result, "INMEMORY:") == ["INMEMORY:True"]- assert not [line for line in result.stdout.splitlines() if "native path resolver" in line]# Handed back exactly, broken values and all: they are the caller's, not ours.assert _lines(result, "TMP:") == [f"TMP:{dead}"]assert _lines(result, "TEMP:") == [f"TEMP:{dead}"]assert list((local_app_data / "Unsloth Studio" / "temp").glob("ust-*")) == []- # The imports target kernel32, so off Windows the type builds and the CALL fails:- # it must degrade to a usable answer rather than throw.assert _lines(result, "PATH:")[0].startswith("PATH:")assert _lines(result, "PATH:") != ["PATH:"]@@ -1124,47 +1239,36 @@ def test_the_sweep_only_takes_directories_the_allocator_could_have_made(tmp_path@requires_pwsh-def test_a_native_resolver_that_throws_says_so_once(tmp_path: Path):- """Compiling and then failing to resolve is not the same as no compiler.-- The install still proceeds on the lexical answer, and Exact = $false already- makes the runtime lock fail closed, but nothing said so: the degraded warning- fires only when the COMPILE failed. That left an operator with a silently- inexact identity on a host that looks perfectly healthy.+def test_a_resolver_that_runs_and_fails_says_so_once(tmp_path: Path):+ """An interpreter that answers nothing is not the same as no interpreter.++ The install proceeds on the lexical answer either way, and Exact = $false already+ makes the runtime lock fail closed, but for a long time nothing said so: the+ degraded warning fired only where the exact rung was known to be unavailable. That+ left an operator with a silently inexact identity on a host that looks perfectly+ healthy. A child that starts, runs and returns nothing -- a sandboxed interpreter, a+ symlink loop under the requested path, an AV hook that kills it -- has to reach the+ same warning."""studio = tmp_path / "studio"studio.mkdir()result = _run_powershell(_script(f"""-# The subject here is the warning, not the answer, so the Python rung is switched off: it-# would resolve exactly on any host with an interpreter and the assertion below is about the-# degraded identity. That the rung rescues a throwing native resolver is covered in-# tests/studio/test_early_python_path_resolver.ps1.-$env:UNSLOTH_EARLY_PYTHON_PROBE = "0"-# The helper is "available" and throws anyway, which is what a rename between-# the Test-Path walk and CreateFileW looks like. The stub has to be the type the-# resolver actually calls, and it has to count its calls: a stub the resolver-# never reaches produces the same outward fallback as one that threw, so without-# the counter this test passes while proving nothing. Add-Type here is the test's-# own scaffolding; install.ps1 does not call it.-function Initialize-StudioFinalPathNativeType {{ return $true }}-Add-Type -TypeDefinition @'-public class UnslothStudioFinalPathV3 {{- public static int Calls = 0;- public static System.IntPtr CreateFileW(- string path, uint access, uint share, System.IntPtr security,- uint disposition, uint flags, System.IntPtr template) {{- Calls++;- throw new System.Exception("access is denied");- }}+# Present and useless, which is the state under test. Counted, because a stub the+# resolver never reaches produces the same outward fallback as one that answered+# nothing: without the counter this test passes while proving nothing.+$global:Calls = 0+function Get-StudioPythonFinalPath {{+ param([string]$Path)+ $global:Calls+++ return $null}}-'@foreach ($i in 1..3) {{ $null = Resolve-StudioFinalPathInfo -Path '{studio}' }}$info = Resolve-StudioFinalPathInfo -Path '{studio}'Write-Output "EXACT:$($info.Exact)"Write-Output "PATH:$($info.Path)"-Write-Output "CALLS:$([UnslothStudioFinalPathV3]::Calls)"+Write-Output "CALLS:$global:Calls"""",sabotage = False,)@@ -1172,10 +1276,10 @@ Write-Output "CALLS:$([UnslothStudioFinalPathV3]::Calls)"assert result.returncode == 0, result.stderrassert _lines(result, "EXACT:") == ["EXACT:False"]assert _lines(result, "PATH:")[0].endswith("studio")- warnings = [line for line in result.stdout.splitlines() if "native helper; continuing" in line]+ warnings = [+ line for line in result.stdout.splitlines() if "Could not resolve a path exactly" in line+ ]assert len(warnings) == 1, warnings- # The throwing resolver was really reached, four times, rather than everything- # falling back because the type was absent.assert _lines(result, "CALLS:") == ["CALLS:4"]@@ -1371,7 +1475,10 @@ def test_the_uninstall_sweep_leaves_a_live_owner_and_never_follows_a_link(tmp_pa(victim / "ust-1234-abcdef03" / "precious.txt").write_text("not ours", encoding = "utf-8")linked = tmp_path / "Linked Unsloth" / "temp"linked.parent.mkdir()- linked.symlink_to(victim, target_is_directory = True)+ try:+ linked.symlink_to(victim, target_is_directory = True)+ except (OSError, NotImplementedError):+ pytest.skip("creating a symlink requires privilege on this host")result = _run_powershell("\n".join(@@ -1498,7 +1605,10 @@ def test_a_link_high_above_another_profile_is_still_a_link(tmp_path: Path):# Three levels above the temp directory, so neither it nor its parent is a link. Only a full walk sees this.redirected = tmp_path / "redirected profile"- redirected.symlink_to(real, target_is_directory = True)+ try:+ redirected.symlink_to(real, target_is_directory = True)+ except (OSError, NotImplementedError):+ pytest.skip("creating a symlink requires privilege on this host")aliased = redirected / "localappdata" / "Unsloth Studio" / "temp"other = tmp_path / "mine" / "Unsloth Studio" / "temp"@@ -1722,623 +1832,206 @@ def test_split_path_never_pairs_literalpath_with_parent(name: str) -> None:)-SETUP_PS1 = REPO_ROOT / "studio" / "setup.ps1"+def test_the_lock_chain_defines_everything_it_reaches() -> None:+ """A helper the chain calls but the list forgets is a runtime break, not a missing test.+ These scripts run under -ErrorActionPreference Stop, so the first call to an undefined name+ ends the run, and the failure names the caller rather than the omission:-def _one_function(source: str, name: str) -> str:- match = re.search(- rf"^(?P<indent>\s*)function {name} \{{.*?\n(?P=indent)\}}\n",- source,- flags = re.DOTALL | re.MULTILINE,- )- assert match is not None, f"{name} not found"- return match.group(0)-+ Get-StudioPythonFinalPath: The term 'Get-StudioSystem32Tool' is not recognized-def _gate(source: str) -> str:- """The capability gate plus the child probe it delegates to, from either script."""- return "\n".join(- _one_function(source, name)- for name in ("Test-StudioCanDefineNativeTypes", "Test-StudioEmitInChildProcess")+ LOCK_CHAIN is hand-written and install.ps1 moves under it, so splitting a body out into a new+ helper silently breaks every case in this file. Found exactly that way on the windows-latest+ parity row, hours after the helper landed. Closing the list over what the extracted bodies+ actually call turns that into a local failure on every platform.+ """+ source = INSTALL_PS1.read_text(encoding = "utf-8")+ extracted = _helpers(*LOCK_CHAIN)+ installer_functions = set(re.findall(r"^ function ([\w-]+) \{", source, flags = re.M))+ provided = set(re.findall(r"^ function ([\w-]+) \{", extracted, flags = re.M))+ assert provided, "the helper extraction produced nothing"+ assert len(installer_functions) > 50, (+ f"only {len(installer_functions)} installer functions found, so the scan is not reading "+ "install.ps1 and this test would pass on an empty set")---# Both, because both still carry the apparatus. What changed is its only CONSUMER in-# studio/setup.ps1: a cosmetic ANSI colour thunk. A CI pre-flight measured Windows PowerShell 5.1-# attached to a real console and found the console mode already 0x7 before anything of ours ran, so-# bit 0x4, ENABLE_VIRTUAL_TERMINAL_PROCESSING, was set by the host at startup and the SetConsoleMode-# was re-setting a bit that was already set. The thunk became a read of-# $Host.UI.SupportsVirtualTerminal.-#-# The apparatus itself stays in setup.ps1 and so does its coverage here: Get-NvidiaLibraryProbeType-# emits the nvml and nvcuda imports through the same gate, so deleting the apparatus would break the-# GPU inventory. Only the tests about the CONSOLE helper specifically are install-only now.-EMIT_SCRIPTS = ["install", "setup"]-CONSOLE_HELPER_SCRIPTS = ["install"]---# Status 0 is "no policy" and answers without spawning anything. Anything else is a policy-# whose OPTIONS decide the answer, and Win32_DeviceGuard does not report them: option 19-# Dynamic Code Security always blocks unsigned System.Reflection.Emit assemblies and is-# enforced even in an audit policy before Windows 11 24H2, while an audit policy without it-# emits fine. So the gate asks a child process, and these assert that it delegates.-@requires_pwsh[email protected]("script", EMIT_SCRIPTS)[email protected]("status", ["1", "2"])[email protected]("probe,expected", [("$true", "True"), ("$false", "False")])-def test_an_active_policy_is_decided_by_the_child_probe(- script: str, status: str, probe: str, expected: str-):- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- _gate(source),- # Every parameter the real call passes has to bind, or the stub is skipped and- # the test measures the absent-provider path instead of the reported status.- "function Get-CimInstance {",- " param([string]$Namespace, [string]$ClassName, [string]$ErrorAction)",- f" [pscustomobject]@{{ UsermodeCodeIntegrityPolicyEnforcementStatus = {status} }}",- "}",- "$script:ProbeCalls = 0",- f"function Test-StudioEmitInChildProcess {{ $script:ProbeCalls++; return {probe} }}",- 'Write-Output "CAN:$(Test-StudioCanDefineNativeTypes)"',- 'Write-Output "CALLS:$script:ProbeCalls"',- ]- )+ # Whole-line comments dropped first: a helper NAMED in prose is not a call, and treating it+ # as one grows the list to satisfy a mention rather than a dependency.+ code = "\n".join(line for line in extracted.splitlines() if not line.lstrip().startswith("#"))+ called = set(re.findall(r"(?<![\w-])([A-Z][\w]*-[\w-]+)", code))+ missing = sorted((called & installer_functions) - provided)+ assert not missing, (+ f"LOCK_CHAIN extracts functions that call {missing}, which nothing here defines. "+ "Add them to LOCK_CHAIN, or the scripts in this file die on the first call.")- assert result.returncode == 0, result.stderr- assert _lines(result, "CAN:") == [f"CAN:{expected}"]- assert _lines(result, "CALLS:") == ["CALLS:1"]@requires_pwsh[email protected]("script", EMIT_SCRIPTS)-def test_no_policy_answers_without_spawning_a_probe(script: str):- """The probe costs a process. A machine with no policy is the overwhelming majority and- must not pay for it."""- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")+def test_an_elevated_run_declines_a_user_writable_interpreter(tmp_path: Path):+ """The other side of the stub above, so neither half is an assumption.++ Whatever this ladder settles on is launched WITH THE ADMINISTRATOR TOKEN when the install is+ elevated, so an interpreter in a directory the same user's medium-integrity token can write+ is a way to have arbitrary code run elevated. This host's own interpreter is exactly that, so+ the rung has to decline, the answer stops being exact, and the reason is printed rather than+ the degradation being silent.+ """+ studio_home = tmp_path / "studio"+ studio_home.mkdir()result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- _gate(source),- "function Get-CimInstance {",- " param([string]$Namespace, [string]$ClassName, [string]$ErrorAction)",- " [pscustomobject]@{ UsermodeCodeIntegrityPolicyEnforcementStatus = 0 }",- "}",- "$script:ProbeCalls = 0",- "function Test-StudioEmitInChildProcess { $script:ProbeCalls++; return $false }",- 'Write-Output "CAN:$(Test-StudioCanDefineNativeTypes)"',- 'Write-Output "CALLS:$script:ProbeCalls"',- ]+ _script(+ f"""+# The gate is Windows-only, and the point of this case is the gate. Named here so the row+# means the same thing on the Linux parity runner as on the Windows one.+$env:OS = "Windows_NT"+Write-Output "EXACT:$((Resolve-StudioFinalPathInfo -Path '{studio_home}').Exact)"+""",+ elevated = True,))assert result.returncode == 0, result.stderr- assert _lines(result, "CAN:") == ["CAN:True"]- assert _lines(result, "CALLS:") == ["CALLS:0"]---# The runtime test below catches this by executing it, but only where emit succeeds. This-# one is the invariant itself, and the one a future edit trips: one double quote added to-# the probe body is enough, and the resulting failure points nowhere near the quote.[email protected]("script", EMIT_SCRIPTS)-def test_the_probe_body_carries_no_double_quote(script: str):- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- body = re.search(r"\$probe = @'\n(.*?)\n'@", source, flags = re.DOTALL)- assert body is not None, "the probe here-string is gone"- assert '"' not in body.group(1), (- "the emit probe body gained a double quote. Windows PowerShell 5.1 appends a native "- "argument verbatim inside its own double quotes, so the first one here closes the "- "wrapper and the child runs something else entirely, silently answering no."- )---# The acceptance rules, one child at a time. $PSHOME is an ordinary variable, so a fake host-# under a temporary one exercises each rule without a policy, a Windows box or luck. Each case-# is a way a real child answers badly: printed the marker then died, ran in a language mode-# its parent did not, printed something that merely CONTAINS the marker, or never returned.-# A #!/bin/sh host, so POSIX only: Windows CreateProcess rejects a file with no-# executable format, every case would come back false through the catch, and the-# accept case would fail while the deadline case passed without waiting.[email protected](os.name == "nt", reason = "the fake host is a shell script")-@requires_pwsh[email protected]("script", EMIT_SCRIPTS)- "emits,code,expected,outcome,label",- [- ("STUDIO_EMIT_OK FullLanguage", 0, "True", "ok", "the good case"),- ("STUDIO_EMIT_OK FullLanguage", 23, "False", "blocked", "marker then a bad exit"),- (- "STUDIO_EMIT_OK ConstrainedLanguage",- 0,- "False",- "blocked",- "a child restricted differently",- ),- ("", 1, "False", "blocked", "a child that ran and refused"),- (- "NOT_STUDIO_EMIT_OK_FAILURE",- 0,- "False",- "indeterminate",- "a line that merely contains the marker",- ),- ("", 0, "False", "indeterminate", "silence"),- ],-)-def test_the_probe_only_accepts_a_clean_exact_answer(- script: str, emits: str, code: int, expected: str, outcome: str, label: str, tmp_path: Path-):- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- home = tmp_path / "fakehome"- home.mkdir()- fake = home / "pwsh"- fake.write_text(- "#!/bin/sh\n" + (f'echo "{emits}"\n' if emits else "") + f"exit {code}\n",- encoding = "utf-8",- )- fake.chmod(0o755)- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- _one_function(source, "Test-StudioEmitInChildProcess"),- f"Write-Output \"ANSWER:$(Test-StudioEmitInChildProcess -HostPath '{fake}')\"",- 'Write-Output "OUTCOME:$script:StudioEmitProbeOutcome"',- ]- )- )- assert result.returncode == 0, f"{label}: {result.stderr}"- assert _lines(result, "ANSWER:") == [f"ANSWER:{expected}"], label- # And WHY, which is not the same fact as the boolean: a child that ran and refused is a- # machine that cannot emit, a child that never answered is a failed process. The gate- # retries only the second and reports a different reason for it.- assert _lines(result, "OUTCOME:") == [f"OUTCOME:{outcome}"], label---# One transient process failure used to be indistinguishable from a policy: cached for the-# whole run, it sends the installer down the lexical path, where two unequal roots compare as-# unknown and a second runtime lock is taken, turning an unrelated install elsewhere into-# "the managed Unsloth environment is busy". The compiled version tried twice before caching-# a negative. A genuinely blocked machine still pays for exactly one probe.-@requires_pwsh[email protected]("script", EMIT_SCRIPTS)- "outcome,calls",- [("indeterminate", "2"), ("blocked", "1")],- ids = ["never-answered-is-retried", "answered-no-is-not"],-)-def test_only_a_probe_that_never_answered_is_retried(script: str, outcome: str, calls: str):- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- "$script:StudioCanDefineNativeTypes = $null",- "$script:StudioEmitProbeOutcome = $null",- "$script:ProbeCalls = 0",- # An active policy, so the gate reaches the probe on any host. Without- # this the real query answers 0 on a Windows runner and the gate returns- # before the stub below is ever called.- "function Get-CimInstance {",- " param([string]$Namespace, [string]$ClassName, [string]$ErrorAction)",- " [pscustomobject]@{ UsermodeCodeIntegrityPolicyEnforcementStatus = 1 }",- "}",- # A stub, because the gate calls the real probe through $PSHOME, which is- # read-only and cannot be pointed at a fake host. What is under test here is- # the gate's retry rule, not the child.- "function Test-StudioEmitInChildProcess {",- " $script:ProbeCalls = $script:ProbeCalls + 1",- f' $script:StudioEmitProbeOutcome = "{outcome}"',- " return $false",- "}",- _one_function(source, "Test-StudioCanDefineNativeTypes"),- "$answer = Test-StudioCanDefineNativeTypes",- 'Write-Output "ANSWER:$answer"',- 'Write-Output "CALLS:$script:ProbeCalls"',- ]- )- )- assert result.returncode == 0, result.stderr + result.stdout- assert _lines(result, "ANSWER:") == ["ANSWER:False"]- assert _lines(result, "CALLS:") == [f"CALLS:{calls}"]+ assert _lines(result, "EXACT:") == ["EXACT:False"]+ assert [+ line for line in result.stdout.splitlines() if "Could not resolve a path exactly" in line+ ], result.stdout+++_INTEGRITY_REPRO = r"""+param($Source, $FakePython)+$ErrorActionPreference = 'Stop'+$t = $null; $e = $null+$ast = [System.Management.Automation.Language.Parser]::ParseFile($Source, [ref]$t, [ref]$e)+foreach ($name in @('Get-StudioSystem32Tool', 'Invoke-StudioSystem32ToolBounded',+ 'Test-StudioChildScriptDirectoryElevated', 'New-StudioChildScriptDirectory',+ 'Get-NvidiaNvmlLibraryPath', 'Get-NvidiaSystem32Dir', 'Read-NvidiaLibraryRawViaPython')) {+ $fn = @($ast.FindAll({ param($n)+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name }, $true))+ if ($fn.Count) { Invoke-Expression $fn[0].Extent.Text }+}+if (-not (Get-Command Read-NvidiaLibraryRawViaPython -ErrorAction SilentlyContinue)) { throw "no Python rung in $Source" }+function Get-NvidiaProbePythonExe { return $FakePython }+Write-Output "ANSWER:$(Read-NvidiaLibraryRawViaPython -TimeoutMs 1000)"+"""-# The reason printed alongside the degradation. Naming a machine setting for what was really-# an unspawnable process sends whoever reads the log looking for a policy that is not there.@requires_pwsh[email protected](os.name == "nt", reason = "the fake System32 tools are POSIX shell scripts")@pytest.mark.parametrize(- "outcome,expected",+ "source, hung",[- ("blocked", "this host enforces user-mode code integrity"),- ("indeterminate", "a probe process could not confirm native type support"),+ ("install.ps1", None),+ ("install.ps1", "icacls.exe"),+ ("install.ps1", "whoami.exe"),+ ("studio/setup.ps1", "icacls.exe"),],- ids = ["a-real-refusal", "a-probe-that-never-answered"],)-def test_the_degradation_reason_says_what_was_actually_established(outcome: str, expected: str):- source = INSTALL_PS1.read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- "$script:StudioFinalPathNativeState = $null",- f'$script:StudioEmitProbeOutcome = "{outcome}"',- "function Test-StudioCanDefineNativeTypes { return $false }",- # Straight to the console, not Write-Output: the initializer's return value is- # discarded below, and that discards everything else on the success stream with- # it, including the warning this test is here to read.- "function Write-StudioLine { param($Message, $ForegroundColor)",- ' [Console]::Out.WriteLine("LINE:$Message") }',- _one_function(source, "Write-StudioFinalPathDegraded"),- _one_function(source, "Initialize-StudioFinalPathNativeType"),- "$null = Initialize-StudioFinalPathNativeType",- ]- )- )- assert result.returncode == 0, result.stderr + result.stdout- warning = [line for line in _lines(result, "LINE:") if "native path resolver" in line]- assert warning, result.stdout- assert expected in warning[0], warning[0]---# The compiled version carried the path helper and the process-image helper on ONE type, so-# they could not disagree about whether this session can emit. Two types can: a session that-# emitted the path type and came back to a probe that now fails would keep native path-# resolution and silently lose native process inspection, which is how a running Studio stops-# being seen. A type already published here outweighs any child.-@requires_pwsh-def test_an_already_emitted_type_settles_it_without_asking_a_child():- source = INSTALL_PS1.read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- "$script:StudioProcessImageNativeState = $null",- "$script:GateCalls = 0",- "function Test-StudioCanDefineNativeTypes {",- " $script:GateCalls = $script:GateCalls + 1",- " return $false",- "}",- _one_function(source, "New-StudioDynamicAssembly"),- _one_function(source, "New-StudioEmittedNativeType"),- _one_function(source, "Initialize-StudioFinalPathNativeType"),- # The real path type, emitted the way a real run emits it, so what stands in- # for the interrupted session is the same evidence that session would leave.- "$script:StudioFinalPathNativeState = $null",- "function Write-StudioFinalPathDegraded { param($Reason) }",- "$null = New-StudioEmittedNativeType -TypeName 'UnslothStudioFinalPathV3' -Imports @(",- " @{ Name = 'CloseHandle'; Library = 'kernel32.dll'; Return = [bool]",- " Args = @([IntPtr]); Ansi = $true }",- ")",- "Write-Output \"PATHTYPE:$($null -ne ('UnslothStudioFinalPathV3' -as [type]))\"",- _one_function(source, "Initialize-StudioProcessImageNativeType"),- "$ok = Initialize-StudioProcessImageNativeType",- 'Write-Output "PROCESS:$ok"',- 'Write-Output "GATE:$script:GateCalls"',- ]- )+def test_a_hung_integrity_tool_cannot_stall_the_nvidia_probe(tmp_path: Path, source, hung):+ """whoami and icacls run before the bounded child, so a utility that never exits+ (an antivirus hold, a wedged token lookup) must cost a deadline, not the install."""+ system32 = tmp_path / "Windows" / "System32"+ system32.mkdir(parents = True)+ tools = {"icacls.exe": "exit 0", "whoami.exe": 'printf "S-1-16-8192"'}+ if hung == "whoami.exe":+ # whoami is asked only once the label cannot be set.+ tools["icacls.exe"] = "exit 1"+ if hung:+ tools[hung] = "exec sleep 60"+ for name, body in tools.items():+ tool = system32 / name+ tool.write_text(f"#!/bin/sh\n{body}\n")+ tool.chmod(0o755)+ python = tmp_path / "python"+ python.write_text('#!/bin/sh\ncat >/dev/null\nprintf "nvml;12;8;8.9"\n')+ python.chmod(0o755)+ script = tmp_path / "repro.ps1"+ script.write_text(_INTEGRITY_REPRO)+ env = dict(+ os.environ,+ OS = "Windows_NT",+ SystemRoot = str(tmp_path / "Windows"),+ TEMP = str(tmp_path),+ TMP = str(tmp_path),+ TMPDIR = str(tmp_path),)- assert result.returncode == 0, result.stderr + result.stdout- assert _lines(result, "PATHTYPE:") == ["PATHTYPE:True"], result.stdout- assert _lines(result, "PROCESS:") == ["PROCESS:True"], result.stdout- assert _lines(result, "GATE:") == ["GATE:0"], (- "the process-image helper asked a child whether emit works in a process that had "- "already emitted a type"- )---# Same rule for the cosmetic helpers. The console one asked the gate before looking for its-# type, so one failed probe threw away a helper already loaded and working in this process.-# The compiled version checked the type first.-@requires_pwsh[email protected]("script", CONSOLE_HELPER_SCRIPTS)-def test_the_console_helper_keeps_a_type_it_already_has(script: str):- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- "$env:NO_COLOR = $null",- "$script:StudioStdoutRedirected = $false",- "$script:GateCalls = 0",- "function Test-StudioCanDefineNativeTypes {",- " $script:GateCalls = $script:GateCalls + 1",- " return $false",- "}",- "function Get-StudioAnsi { param($Name) return '' }",- _one_function(source, "New-StudioDynamicAssembly"),- _one_function(source, "New-StudioEmittedNativeType"),- # Published exactly as a successful earlier call in the same session would- # have left it. The calls themselves fail off Windows and are caught; what- # is under test is whether the gate is consulted at all.- "$null = New-StudioEmittedNativeType -TypeName 'StudioVTNative' -Imports @(",- " @{ Name = 'GetStdHandle'; Library = 'kernel32.dll'; Return = [IntPtr]",- " Args = @([int]); Ansi = $true }",- ")",- _one_function(source, "Enable-StudioVirtualTerminal"),- "$null = Enable-StudioVirtualTerminal",- '[Console]::Out.WriteLine("GATE:$script:GateCalls")',- ]- )- )- assert result.returncode == 0, result.stderr + result.stdout- assert _lines(result, "GATE:") == [- "GATE:0"- ], "a published console type was discarded because a child probe said no"---# A #!/bin/sh host, so POSIX only: Windows CreateProcess rejects a file with no-# executable format, every case would come back false through the catch, and the-# accept case would fail while the deadline case passed without waiting.[email protected](os.name == "nt", reason = "the fake host is a shell script")-@requires_pwsh-def test_a_child_that_never_returns_does_not_hang_the_installer(tmp_path: Path):- """The deadline. A probe that exists to keep the installer alive must not be the thing- that wedges it, and the call operator waits forever. Reachable in practice through a- security product inspecting a freshly spawned interpreter."""- source = INSTALL_PS1.read_text(encoding = "utf-8")- home = tmp_path / "fakehome"- home.mkdir()- fake = home / "pwsh"- fake.write_text("#!/bin/sh\nsleep 600\n", encoding = "utf-8")- fake.chmod(0o755)- # The wait is bounded in the script; this only has to outlast it.started = time.monotonic()- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- _one_function(source, "Test-StudioEmitInChildProcess"),- f"Write-Output \"ANSWER:$(Test-StudioEmitInChildProcess -HostPath '{fake}')\"",- ]- )+ result = run_pwsh(+ ["pwsh", "-NoProfile", "-File", str(script), str(REPO_ROOT / source), str(python)],+ env = env,+ capture_output = True,+ text = True,+ timeout = 30,)- elapsed = time.monotonic() - startedassert result.returncode == 0, result.stderr- assert _lines(result, "ANSWER:") == ["ANSWER:False"]- assert elapsed < 60, f"the probe took {elapsed:.0f}s, so the deadline is not bounding it"---# Legacy is how Windows PowerShell 5.1 ALWAYS binds a native command's arguments, and 5.1-# is the interpreter studio/src-tauri/src/install.rs spawns. It wraps the value in quotes-# and appends the body verbatim without escaping the quotes inside it, so a probe passed-# with -Command arrives as `if (UnslothStudioEmitProbe -as [type])`, a command lookup that-# throws into the probe's own catch and answers "no emit here" on every 5.1 host. pwsh can-# be put into that binder with $PSNativeCommandArgumentPassing, so this is reachable from-# Linux.-@requires_pwsh[email protected]("script", EMIT_SCRIPTS)[email protected]("binding", ["Legacy", "Standard"])-def test_the_child_probe_survives_the_5_1_argument_binder(script: str, binding: str):- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- f'$PSNativeCommandArgumentPassing = "{binding}"',- _one_function(source, "Test-StudioEmitInChildProcess"),- "$answer = Test-StudioEmitInChildProcess",- 'Write-Output "TYPE:$($answer.GetType().Name)"',- 'Write-Output "ANSWER:$answer"',- ]- )+ assert time.monotonic() - started < 20, "a hung integrity utility outlived its deadline"+ if hung is None:+ # Control: a healthy host still gets the Python answer, so the case above is not vacuous.+ assert _lines(result, "ANSWER:") == ["ANSWER:nvml;12;8;8.9"], result.stdout+++def _fake_system32(tmp_path: Path, tools: dict[str, str]) -> dict[str, str]:+ system32 = tmp_path / "Windows" / "System32"+ system32.mkdir(parents = True, exist_ok = True)+ for name, body in tools.items():+ tool = system32 / name+ tool.write_text(f"#!/bin/sh\n{body}\n")+ tool.chmod(0o755)+ return dict(+ os.environ,+ OS = "Windows_NT",+ SystemRoot = str(tmp_path / "Windows"),+ TEMP = str(tmp_path),+ TMP = str(tmp_path),+ TMPDIR = str(tmp_path),)- assert result.returncode == 0, result.stderr- # Boolean either way, since the gate calls this under ErrorActionPreference Stop.- assert _lines(result, "TYPE:") == ["TYPE:Boolean"]- # And True either way: emit works on this host, and the binder must not turn a- # working host into a refusal.- assert _lines(result, "ANSWER:") == ["ANSWER:True"]@requires_pwsh[email protected]("script", EMIT_SCRIPTS)- "provider,label",- [- ('throw "no such namespace"', "the query throws"),- ("$null", "the query returns nothing"),- ("[pscustomobject]@{ Other = 1 }", "the object has no status property"),- ],-)-def test_an_unreadable_policy_asks_the_probe_rather_than_assuming(- script: str, provider: str, label: str[email protected](os.name == "nt", reason = "the fake System32 tools are POSIX shell scripts")+def test_an_elevated_run_without_a_private_directory_does_not_probe_from_a_shared_root(+ tmp_path: Path,):- """Unknown is not zero.-- Treating an unreadable Device Guard as unrestricted lets exactly one case through: option- 19 enforced on a host whose CIM query happens to fail, where the documented outcome is a- stopped process. The cost of asking anyway is one short-lived child.- """- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- _gate(source),- "function Get-CimInstance {",- " param([string]$Namespace, [string]$ClassName, [string]$ErrorAction)",- f" {provider}",- "}",- "$script:ProbeCalls = 0",- "function Test-StudioEmitInChildProcess { $script:ProbeCalls++; return $true }",- 'Write-Output "CAN:$(Test-StudioCanDefineNativeTypes)"',- 'Write-Output "CALLS:$script:ProbeCalls"',- ]- )- )- assert result.returncode == 0, f"{label}: {result.stderr}"- assert _lines(result, "CALLS:") == ["CALLS:1"], label- assert _lines(result, "CAN:") == ["CAN:True"], label---# CharSet is not decoration on an emitted import: it picks the export the runtime looks for-# first. Unicode asks for <Name>W, Ansi asks for <Name>. Every one of these names an export-# that exists exactly as written, so a wrong charset still resolves on the second probe; it-# just stops describing the C# it replaced.-@requires_pwsh- "method,charset",- [- ("CreateFileW", "Unicode"),- ("GetFinalPathNameByHandleW", "Unicode"),- ("CloseHandle", "Ansi"),- ],-)-def test_each_import_carries_the_charset_its_declaration_had(method: str, charset: str):- source = INSTALL_PS1.read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- _helpers(- "Write-StudioLine",- "Write-StudioFinalPathDegraded",- "Test-StudioCanDefineNativeTypes",- "Test-StudioEmitInChildProcess",- "New-StudioDynamicAssembly",- "New-StudioEmittedNativeType",- "Initialize-StudioFinalPathNativeType",- ),- "$null = Initialize-StudioFinalPathNativeType",- f'$m = [UnslothStudioFinalPathV3].GetMethod("{method}")',- # The pseudo-custom attribute the runtime synthesises from the P/Invoke- # metadata, which is the metadata itself rather than a copy of the intent.- "$a = $m.GetCustomAttributes(",- " [System.Runtime.InteropServices.DllImportAttribute], $false)[0]",- 'Write-Output "CHARSET:$($a.CharSet)"',- 'Write-Output "ENTRY:$($a.EntryPoint)"',- 'Write-Output "LIB:$($a.Value)"',- ]- )- )- assert result.returncode == 0, result.stderr- assert _lines(result, "CHARSET:") == [f"CHARSET:{charset}"]- assert _lines(result, "ENTRY:") == [f"ENTRY:{method}"]- assert _lines(result, "LIB:") == ["LIB:kernel32.dll"]---# `out uint` in the C# this replaces. A by-ref type alone emits `ref`, leaving In and Out-# unset, and DefinePInvokeMethod has no argument for it, so the emitter calls DefineParameter.-# The value is blittable and callers initialise it first, so writeback works either way; this-# pins that the metadata says what the declaration said and that the Out key is wired through.-@requires_pwsh[email protected]("script", EMIT_SCRIPTS)-def test_the_emitter_still_honours_an_out_position(script: str):- """No import declares one any more, and the capability stays anyway.-- The only by-ref import was GetConsoleMode, which went with the console thunk. Six lines in the- emitter keep honouring an `Out` position, and they are worth keeping without a consumer: the- next native import that needs one would otherwise marshal silently wrong -- the call succeeds,- the out value never comes back, and nothing raises. That is the kind of bug that costs a day.-- So this probes the emitter directly with a locally invented type rather than asserting against a- declaration in the script, which is what it used to do and which no longer has anything to read.- """- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- _one_function(source, "New-StudioDynamicAssembly"),- _one_function(source, "New-StudioEmittedNativeType"),- '$null = New-StudioEmittedNativeType -TypeName "UnslothOutParamProbe" -Imports @(',- ' @{ Name = "GetConsoleMode"; Library = "kernel32.dll"; Return = [bool]',- " Args = @([IntPtr], [uint32].MakeByRefType())",- " Ansi = $true",- " Out = @(2) }",- ")",- '$p = ([type]"UnslothOutParamProbe").GetMethod("GetConsoleMode").GetParameters()[1]',- 'Write-Output "OUT:$($p.IsOut)"',- 'Write-Output "BYREF:$($p.ParameterType.IsByRef)"',- ]- )+ """The fallback writes its redirect files into %TEMP%, where a standard user can plant or swap them."""+ env = _fake_system32(tmp_path, {"icacls.exe": "exit 1", "whoami.exe": 'printf "S-1-16-12288"'})+ python = tmp_path / "python"+ python.write_text('#!/bin/sh\ncat >/dev/null\nprintf "nvml;12;8;8.9"\n')+ python.chmod(0o755)+ script = tmp_path / "repro.ps1"+ script.write_text(_INTEGRITY_REPRO)+ result = run_pwsh(+ ["pwsh", "-NoProfile", "-File", str(script), str(REPO_ROOT / "install.ps1"), str(python)],+ env = env,+ capture_output = True,+ text = True,+ timeout = 60,)assert result.returncode == 0, result.stderr- assert _lines(result, "OUT:") == ["OUT:True"]- assert _lines(result, "BYREF:") == ["BYREF:True"]- # Deliberately no assertion that some declaration in the script uses it. There is no longer such- # a declaration, and writing one purely to keep this test alive would be adding a native import- # to a file this work exists to remove native imports from.- # A declaration, not a substring: the replacement code explains in a comment which three native- # calls it replaced, and naming them is the point of that comment.- assert not re.search(r'Name\s*=\s*"GetConsoleMode"', source), (- "an import named GetConsoleMode is declared in the installer again. It was removed because "- "the host already enables virtual terminal processing before our code runs -- the console "- "mode is 0x7 before we touch it -- so the call was a no-op. If it is genuinely needed "- "again, say why here."- )+ assert _lines(result, "ANSWER:") == ["ANSWER:"], result.stdout-# Almost every import declares no Out position, and reading an absent hashtable key is fatal-# under Set-StrictMode -Version Latest, which raises PropertyNotFound rather than returning-# $null. install.ps1 sets strict mode Off for itself but setup.ps1 sets none, so the caller's-# setting runs and a profile with strict mode on would have taken out every emitted type. The-# emitter asks ContainsKey first, as it does for CharSet; this runs it under the strictest-# setting.@requires_pwsh[email protected]("script", EMIT_SCRIPTS)-def test_an_import_without_an_out_position_survives_strict_mode(script: str):- source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- "Set-StrictMode -Version Latest",- '$ErrorActionPreference = "Stop"',- _one_function(source, "New-StudioDynamicAssembly"),- _one_function(source, "New-StudioEmittedNativeType"),- # No Out key, and no Ansi key either: both reads have to be guarded.- '$ok = New-StudioEmittedNativeType -TypeName "UnslothStrictModeProbe" -Imports @(',- ' @{ Name = "CloseHandle"; Library = "kernel32.dll"; Return = [bool]',- " Args = @([IntPtr]) }",- ")",- 'Write-Output "OK:$ok"',- ][email protected](os.name == "nt", reason = "the fake System32 tools are POSIX shell scripts")[email protected]("source", ["install.ps1", "studio/setup.ps1"])+def test_a_child_directory_swapped_for_a_link_before_labelling_is_refused(tmp_path: Path, source):+ """A standard user can replace the new directory with a link until the label lands."""+ victim = tmp_path / "victim"+ victim.mkdir()+ # The label step is where the swap races in: this icacls replaces its target with a link first.+ swap = f'case "$2" in /setintegritylevel) rm -rf "$1"; ln -s "{victim}" "$1";; esac; exit 0'+ env = _fake_system32(tmp_path, {"icacls.exe": swap, "whoami.exe": 'printf "S-1-16-12288"'})+ script = tmp_path / "mkdir.ps1"+ script.write_text(+ _INTEGRITY_REPRO.split("function Get-NvidiaProbePythonExe")[0].replace(+ "if (-not (Get-Command Read-NvidiaLibraryRawViaPython",+ "if (-not (Get-Command New-StudioChildScriptDirectory",)+ + 'Write-Output "DIR:$(New-StudioChildScriptDirectory)"\n')- assert result.returncode == 0, result.stderr + result.stdout- assert _lines(result, "OK:") == ["OK:True"]---@requires_pwsh-def test_a_published_type_counts_even_when_creation_threw():- """CreateType can publish the type and then fail on the way back.-- The compiled version asked whether the type had arrived anyway before it gave up, and- dropping that turned a recoverable failure into lexical resolution for the whole run.- """- source = INSTALL_PS1.read_text(encoding = "utf-8")- result = _run_powershell(- "\n".join(- [- '$ErrorActionPreference = "Stop"',- _helpers(- "Write-StudioLine",- "Write-StudioFinalPathDegraded",- "Test-StudioCanDefineNativeTypes",- "Test-StudioEmitInChildProcess",- "New-StudioDynamicAssembly",- "New-StudioEmittedNativeType",- "Initialize-StudioFinalPathNativeType",- ),- # Publishes the type under the name the initializer wants, then throws, which- # is the shape being tested. Add-Type here is the test's own scaffolding, not- # the installer's: it is the shortest way to put a real type in the session.- "$real = ${function:New-StudioEmittedNativeType}",- "function New-StudioEmittedNativeType {",- " param([string]$TypeName, [object[]]$Imports)",- " Add-Type -TypeDefinition 'public static class UnslothStudioFinalPathV3 { }'",- ' throw "published, then failed"',- "}",- 'Write-Output "OK:$(Initialize-StudioFinalPathNativeType)"',- 'Write-Output "AGAIN:$(Initialize-StudioFinalPathNativeType)"',- ]- )+ result = run_pwsh(+ ["pwsh", "-NoProfile", "-File", str(script), str(REPO_ROOT / source), "unused"],+ env = env,+ capture_output = True,+ text = True,+ timeout = 60,)assert result.returncode == 0, result.stderr- assert _lines(result, "OK:") == ["OK:True"]- assert _lines(result, "AGAIN:") == ["AGAIN:True"]- # The degraded warning belongs to a run that really has no native side.- assert "Could not load the native path resolver" not in result.stdout+ assert _lines(result, "DIR:") == ["DIR:"], result.stdout+ assert victim.is_dir()diff --git a/tests/python/test_windows_setup_output_encoding.py b/tests/python/test_windows_setup_output_encoding.pyindex 10be2d65ec4..fbf893c1343 100644--- a/tests/python/test_windows_setup_output_encoding.py+++ b/tests/python/test_windows_setup_output_encoding.py@@ -595,16 +595,7 @@ def _console_less_probe(path: Path) -> str:r"[ \t]*try \{ \$script:StudioStdoutRedirected = \[Console\]::IsOutputRedirected \} catch \{ \}",)parts += [redirect_probe or "$script:StudioStdoutRedirected = $false", ""]- # Enable-StudioVirtualTerminal's own helpers come first, because it calls them. They were- # added when the emitted-type path was extracted, and the reconstruction that this test- # builds to represent the predecessor calls them too: without them the probe dies with an- # unrecognised command, which reads as "the predecessor disagreed" when in truth it never- # ran. Anything the sliced functions call has to be sliced with them.for name in (- "Test-StudioCanDefineNativeTypes",- "Test-StudioEmitInChildProcess",- "New-StudioDynamicAssembly",- "New-StudioEmittedNativeType","Write-StudioLine","Enable-StudioVirtualTerminal","Get-StudioAnsi",diff --git a/tests/studio/install/test_denied_llama_cpp_preflight.py b/tests/studio/install/test_denied_llama_cpp_preflight.pyindex 9de0d81b01e..310434c7e79 100644--- a/tests/studio/install/test_denied_llama_cpp_preflight.py+++ b/tests/studio/install/test_denied_llama_cpp_preflight.py@@ -306,6 +306,9 @@ def test_both_entrypoints_resolve_and_reuse_the_same_managed_directory() -> Noneassert "$LlamaCppDir =" not in phase+_LABELS_OUR_OWN_DIRECTORY = re.compile(r'icacls\.exe "\$dir"(?: /setintegritylevel\b| 2>&1)')++def test_the_installer_never_repairs_permissions_by_itself() -> None:"""Print ACL repair commands but never run them."""# Match direct, chained, captured, and delegated invocation forms.@@ -317,9 +320,33 @@ def test_the_installer_never_repairs_permissions_by_itself() -> None:code = line.split("#", 1)[0].strip()if "takeown" not in code and "icacls" not in code:continue+ if _LABELS_OUR_OWN_DIRECTORY.search(code):+ continueassert not invocation.search(code), f"{label}: {line.strip()}"+def test_the_label_exemption_is_narrow() -> None:+ """The exemption must not cover an ACL change on anything but our own new directory.++ Without this, widening it to `icacls` would read as a passing test while the rule it is+ carved out of stopped applying at all.+ """+ forbidden = (+ 'icacls.exe "$LlamaCppDir" /setintegritylevel (OI)(CI)H',+ 'icacls.exe "$dir" /grant "$env:USERNAME:(F)"',+ 'takeown.exe /f "$dir"',+ '& icacls "$StudioHome" /reset',+ )+ for line in forbidden:+ assert not _LABELS_OUR_OWN_DIRECTORY.search(line), line+ assert _LABELS_OUR_OWN_DIRECTORY.search(+ '$null = & icacls.exe "$dir" /setintegritylevel "(OI)(CI)H" 2>&1'+ )+ assert _LABELS_OUR_OWN_DIRECTORY.search(+ '$labelled = ("$(& icacls.exe "$dir" 2>&1)" -match "S-1-16-12288")'+ )++def test_setup_sh_reports_a_denied_default_home_cache() -> None:"""The POSIX prebuilt path must report a denied default cache."""block = SETUP_SH.split('substep "installing prebuilt llama.cpp..."', 1)[1]diff --git a/tests/studio/test_early_python_icon_refresh.ps1 b/tests/studio/test_early_python_icon_refresh.ps1index 3097146ad5d..bdc82a04c5c 100644--- a/tests/studio/test_early_python_icon_refresh.ps1+++ b/tests/studio/test_early_python_icon_refresh.ps1@@ -19,7 +19,13 @@ $ast = [System.Management.Automation.Language.Parser]::ParseFile($installPs1, [rif ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "install.ps1 has parse errors" }foreach ($name in @("Invoke-StudioEarlyPythonScript", "Invoke-StudioEarlyPython", "Get-StudioEarlyPython",- "Invoke-StudioPythonShellIconRefresh"+ "Invoke-StudioPythonShellIconRefresh",+ # What Get-StudioEarlyPython reaches on Windows: the elevation gate and its helpers. Off+ # Windows it never calls them, which is how a missing one once passed here and failed CI.+ "Test-StudioChildScriptDirectoryElevated", "Invoke-StudioSystem32ToolBounded", "Get-StudioSystem32Tool", "Test-StudioPathUnderAdminRoot",+ "Test-StudioSddlRightsAreWrite", "Test-StudioSddlPrincipalIsAdminOnly",+ "Test-StudioSddlWritableByNonAdmin", "Test-StudioDirectoryIsAdminOnly", "Test-StudioInterpreterFileIsAdminOnly",+ "Get-StudioLexicalParent", "Write-StudioFinalPathDegraded", "Write-StudioLine")) {$fn = $ast.FindAll({ param($n)$n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name@@ -30,29 +36,50 @@ foreach ($name in @(# A standard user: an elevated run declines this probe, and hosted Windows runners are elevated.function Get-ElevationState { return "false" }+if ($env:OS -eq "Windows_NT") { function Test-StudioChildScriptDirectoryElevated { return $false } }+# The kill switch would make discovery decline; its own cases below set it explicitly.Remove-Item Env:UNSLOTH_EARLY_PYTHON_PROBE -ErrorAction SilentlyContinue$script:StudioEarlyPythonProbed = $false$script:StudioEarlyPython = $nullif (-not (Get-StudioEarlyPython)) {- # A runnable python on PATH means a broken extraction, not a host without Python.- $onPath = $null+ # "No interpreter" is a real and supported state, so it is a skip. But it is also what a+ # BROKEN EXTRACTION looks like from here: a helper this file forgot to pull out of+ # install.ps1 makes Get-StudioEarlyPython fail, the probe finds nothing, and the suite exits 0+ # having tested nothing. That happened once and CI recorded it as a pass. Tell the two apart.+ # The documented opt-out first. UNSLOTH_EARLY_PYTHON_PROBE=0 means "do not spawn an interpreter+ # on this host", so discovery returning nothing is the switch working, not a broken extraction.+ if ("$($env:UNSLOTH_EARLY_PYTHON_PROBE)".Trim() -eq "0") {+ Write-Host " SKIP UNSLOTH_EARLY_PYTHON_PROBE=0, so this rung is switched off by request" -ForegroundColor Yellow+ exit 0+ }+ $elevatedHost = $false+ if ($env:OS -eq "Windows_NT") { try { $elevatedHost = [bool](Test-StudioChildScriptDirectoryElevated) } catch { $elevatedHost = $true } }+ $usable = $nullforeach ($n in @("python3", "python")) {- foreach ($cmd in @(Get-Command $n -All -CommandType Application -ErrorAction SilentlyContinue)) {- if ($onPath -or -not $cmd.Source) { continue }- $job = Start-Job -ArgumentList $cmd.Source -ScriptBlock {- param($exe)- $o = & $exe -I -S -c "import os,sys;sys.stdout.write(os.path.realpath('.') if sys.version_info >= (3, 8) else '')" 2>$null- [pscustomobject]@{ Out = "$o"; Code = $LASTEXITCODE }+ foreach ($src in @(Get-Command $n -All -CommandType Application -ErrorAction SilentlyContinue |+ Select-Object -ExpandProperty Source -ErrorAction SilentlyContinue)) {+ if ($usable) { break }+ if ([string]::IsNullOrWhiteSpace($src)) { continue }+ if ("$src" -match '(?i)[\\/]Microsoft[\\/]WindowsApps[\\/]') { continue }+ # The installer's own elevated rule: an elevated run refuses an interpreter a standard user+ # can replace, so such a candidate is not one discovery should have found.+ if ($elevatedHost -and -not (Test-StudioPathUnderAdminRoot -Path $src)) { continue }+ $here = Split-Path -Parent $src+ if ([string]::IsNullOrWhiteSpace($here)) { continue }+ # In a job with a deadline: a shim that starts and never exits must be a skip, not a hang.+ $job = Start-Job -ArgumentList $src, $here -ScriptBlock {+ param($exe, $dir)+ "$(& $exe -I -S -c "import pathlib,sys`nsys.exit(2) if sys.version_info < (3,8) else None`nsys.stdout.write(str(pathlib.Path(sys.argv[1]).resolve(strict=True)))" $dir 2>$null)"}- $ran = $null- if (Wait-Job $job -Timeout 30) { $ran = Receive-Job $job -ErrorAction SilentlyContinue } else { Stop-Job $job }+ $answer = ""+ if (Wait-Job $job -Timeout 30) { $answer = "$(Receive-Job $job -ErrorAction SilentlyContinue)".Trim() } else { Stop-Job $job }Remove-Job $job -Force- if ($ran -and $ran.Code -eq 0 -and -not [string]::IsNullOrWhiteSpace($ran.Out)) { $onPath = $cmd }+ if (-not [string]::IsNullOrWhiteSpace($answer)) { $usable = $src }}}- if ($onPath) {- Write-Host " FAIL Get-StudioEarlyPython found nothing, yet $($onPath.Source) is on PATH." -ForegroundColor Red+ if ($usable) {+ Write-Host " FAIL Get-StudioEarlyPython found nothing, yet $usable answers the same probe." -ForegroundColor RedWrite-Host " That is a broken extraction in this file, not a host without Python." -ForegroundColor Redexit 1}@@ -133,7 +160,17 @@ try {Check "New-StudioShortcuts passes its own interpreter to the refresh" ($shortcutFn -match '(?s)Invoke-StudioPythonShellIconRefresh[^\r\n]*[\r\n\s`]*-Paths \$createdShortcutPaths -Exe \$ManagedPythonPath')- # UNSLOTH_EARLY_PYTHON_PROBE=0 must win even over a passed interpreter (a past regression).+ Check "an elevated run still refreshes through the venv interpreter" (+ $shortcutFn -notmatch 'Test-StudioPathUnderAdminRoot' -and+ $shortcutFn -notmatch 'iconRefreshSafe')++ # The kill switch must still win, even with an interpreter handed straight in.+ #+ # UNSLOTH_EARLY_PYTHON_PROBE=0 means "do not spawn an interpreter on this host". That is a+ # statement about the host, not about how the path was obtained, and Get-StudioEarlyPython+ # honours it. Passing $ManagedPythonPath to fix the fresh-install case routed around+ # discovery and therefore around the switch, so a host that had opted out got a child process+ # anyway. Driven both ways, because the fix is one line and easy to lose.$savedProbe = $env:UNSLOTH_EARLY_PYTHON_PROBEtry {$env:UNSLOTH_EARLY_PYTHON_PROBE = "0"@@ -193,12 +230,14 @@ Check "the probe still sends the global SHCNE_ASSOCCHANGED broadcast, flushed" (Check "the probe declares SHChangeNotify's signature" ($probeText -match "SHChangeNotify\.argtypes" -and $probeText -match "LPCWSTR")-# Fallback only: when the type can be defined the installer must not reach the child.$source = Get-Content -LiteralPath $installPs1 -Raw-Check "the type-defining rung is still tried first" (- $source -match "UnslothShellIconRefresh`" -as \[type\]")-Check "the child is reached only from the catch of that rung" (- $source -match "(?s)\}\s*catch\s*\{[^}]*?Invoke-StudioPythonShellIconRefresh")+Check "no emitted shell-icon type is left to try first" ($source -notmatch "UnslothShellIconRefresh")+Check "the refresh goes straight through the child interpreter" (+ $source -match "Invoke-StudioPythonShellIconRefresh")+# Cosmetic, and it must stay that way: a failure here cannot be allowed to fail an install.+# Single-quoted, so PowerShell does not eat the `$ as an escape and quietly change the pattern.+Check "the call is still wrapped so a failure cannot fail the install" (+ $source -match '(?s)try \{[^{}]*Invoke-StudioPythonShellIconRefresh[^{}]*\} catch \{\}')if ($failures -gt 0) {Write-Host "$failures check(s) failed" -ForegroundColor Reddiff --git a/tests/studio/test_early_python_path_resolver.ps1 b/tests/studio/test_early_python_path_resolver.ps1index c74a4bd95de..4ea0ffb1411 100644--- a/tests/studio/test_early_python_path_resolver.ps1+++ b/tests/studio/test_early_python_path_resolver.ps1@@ -1,12 +1,12 @@#!/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 Python rung of Resolve-StudioFinalPathInfo, used when the native resolver answers nothing.# Run: pwsh -NoProfile -File tests/studio/test_early_python_path_resolver.ps1$ErrorActionPreference = "Stop"$root = (Resolve-Path ([System.IO.Path]::Combine($PSScriptRoot, "..", ".."))).Path$installPs1 = Join-Path $root "install.ps1"+$onWindows = $IsWindows -or $env:OS -eq "Windows_NT"$failures = 0function Check($name, $cond) {@@ -17,76 +17,97 @@ function Check($name, $cond) {$tokens = $null; $errors = $null$ast = [System.Management.Automation.Language.Parser]::ParseFile($installPs1, [ref]$tokens, [ref]$errors)if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "install.ps1 has parse errors" }+function Get-Fn($name) {+ $f = @($ast.FindAll({ param($n)+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name+ }, $true))+ if ($f.Count -lt 1) { return "" }+ return $f[0].Extent.Text+}foreach ($name in @(- "Get-ElevationState", "Get-StudioEarlyPython", "Invoke-StudioEarlyPythonScript", "Invoke-StudioEarlyPython",- "Get-StudioPythonFinalPath",+ "Get-StudioEarlyPython", "Invoke-StudioEarlyPython", "Invoke-StudioEarlyPythonScript",+ "Get-StudioPythonFinalPath", "New-StudioChildScriptDirectory", "Test-StudioChildScriptDirectoryElevated",+ "Invoke-StudioSystem32ToolBounded", "Get-StudioSystem32Tool", "Test-StudioPathUnderAdminRoot",+ "Test-StudioSddlRightsAreWrite", "Test-StudioSddlPrincipalIsAdminOnly",+ "Test-StudioSddlWritableByNonAdmin", "Test-StudioDirectoryIsAdminOnly",+ "Get-StudioLexicalParent", "Test-StudioInterpreterFileIsAdminOnly","Resolve-StudioLinkTarget", "Get-StudioSubstTarget", "Get-StudioLexicalPath",- "Resolve-StudioFinalPathInfo", "Test-StudioPlainFile", "Test-UnslothCmdShimFile"+ "Resolve-StudioFinalPathInfo", "Resolve-StudioFinalPathsInOneChild", "Write-StudioFinalPathDegraded",+ "Test-StudioPlainFile", "Test-UnslothCmdShimFile")) {- $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 install.ps1, found none" }- Invoke-Expression $fn[0].Extent.Text+ $text = Get-Fn $name+ if (-not $text) { throw "expected $name in install.ps1, found none" }+ Invoke-Expression $text}-# A failed native type is warned about before this rung runs, so the warning cannot promise-# paths compared as written: the Python rung may still answer exactly.-$degraded = @($ast.FindAll({ param($n)- $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq "Write-StudioFinalPathDegraded"-}, $true))[0].Extent.Text-Check "the native-failure warning names the Python rung it falls back to" ($degraded -match 'Python resolver')--# The native rung, forced off.-function Initialize-StudioFinalPathNativeType { return $false }-function Get-StudioNativeFinalPath { param([string]$Path) return $null }+if ($env:OS -eq "Windows_NT") { function Test-StudioChildScriptDirectoryElevated { return $false } }function Write-StudioLine { param([string]$Line, [string]$ForegroundColor = "") }-# Candidate execution happens before the install lock and destination ownership guard.-$savedElevation = ${function:Get-ElevationState}-function Get-ElevationState { return "true" }-$script:StudioEarlyPythonProbed = $false-$script:StudioEarlyPython = $null-$script:StudioEarlyPythonProbedWithoutVenv = $false-try {- Check "an elevated run declines the unvalidated early interpreter" ($null -eq (Get-StudioEarlyPython))-} finally {- ${function:Get-ElevationState} = $savedElevation+function Reset-EarlyPython {$script:StudioEarlyPythonProbed = $false$script:StudioEarlyPython = $null$script:StudioEarlyPythonProbedWithoutVenv = $false}--# The remaining cases model a normal user even on an elevated Windows CI runner.-function Get-ElevationState { return "false" }+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]) } }+function New-FakeExe($stem, $cmdBody, $shBody) {+ $p = Join-Path $tmp ($stem + $(if ($onWindows) { ".cmd" } else { ".sh" }))+ if ($onWindows) { "@echo off`r`n$cmdBody`r`n" | Set-Content -LiteralPath $p -Encoding ASCII }+ else { "#!/bin/sh`n$shBody`n" | Set-Content -LiteralPath $p; & chmod +x $p }+ return $p+}+# Returns the SDDL from $script:AclTable, else $script:AclDefault; throws while $script:AclThrows.+$aclStub = {+ param($LiteralPath, $Path, $ErrorAction)+ if ($script:AclThrows) { throw "access denied" }+ $key = "$LiteralPath".TrimEnd('\', '/')+ $sddl = $script:AclDefault+ if ($script:AclTable.ContainsKey($key)) { $sddl = $script:AclTable[$key] }+ return [pscustomobject]@{ Sddl = $sddl }+}$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("earlypy-" + [guid]::NewGuid().ToString("N"))New-Item -ItemType Directory -Force -Path $tmp | Out-Nulltry {- # The kill switch would make discovery decline; its own cases below set it explicitly.Remove-Item Env:UNSLOTH_EARLY_PYTHON_PROBE -ErrorAction SilentlyContinue- $script:StudioEarlyPythonProbed = $false- $script:StudioEarlyPython = $null+ Reset-EarlyPython$exe = Get-StudioEarlyPythonif (-not $exe) {- # A runnable python on PATH means a broken extraction, not a host without Python.- $onPath = $null+ # "No interpreter" is a supported state and a skip, but it is also what a BROKEN EXTRACTION+ # looks like (a forgotten helper makes discovery find nothing and CI records a pass). If this+ # host has a python that answers the same probe, the extraction is at fault, not the host.+ if ("$($env:UNSLOTH_EARLY_PYTHON_PROBE)".Trim() -eq "0") {+ Write-Host " SKIP UNSLOTH_EARLY_PYTHON_PROBE=0, so this rung is switched off by request" -ForegroundColor Yellow+ exit 0+ }+ $elevatedHost = $false+ if ($env:OS -eq "Windows_NT") { try { $elevatedHost = [bool](Test-StudioChildScriptDirectoryElevated) } catch { $elevatedHost = $true } }+ $usable = $nullforeach ($n in @("python3", "python")) {- foreach ($cmd in @(Get-Command $n -All -CommandType Application -ErrorAction SilentlyContinue)) {- if ($onPath -or -not $cmd.Source) { continue }- $job = Start-Job -ArgumentList $cmd.Source -ScriptBlock {- param($exe)- $o = & $exe -I -S -c "import os,sys;sys.stdout.write(os.path.realpath('.') if sys.version_info >= (3, 8) else '')" 2>$null- [pscustomobject]@{ Out = "$o"; Code = $LASTEXITCODE }+ foreach ($src in @(Get-Command $n -All -CommandType Application -ErrorAction SilentlyContinue |+ Select-Object -ExpandProperty Source -ErrorAction SilentlyContinue)) {+ if ($usable) { break }+ if ([string]::IsNullOrWhiteSpace($src)) { continue }+ if ("$src" -match '(?i)[\\/]Microsoft[\\/]WindowsApps[\\/]') { continue }+ # The installer's elevated rule: a candidate a standard user can replace is not one to find.+ if ($elevatedHost -and -not (Test-StudioPathUnderAdminRoot -Path $src)) { continue }+ $here = Split-Path -Parent $src+ if ([string]::IsNullOrWhiteSpace($here)) { continue }+ # A job with a deadline: a shim that never exits must be a skip, not a hang.+ $job = Start-Job -ArgumentList $src, $here -ScriptBlock {+ param($exe, $dir)+ "$(& $exe -I -S -c "import pathlib,sys`nsys.exit(2) if sys.version_info < (3,8) else None`nsys.stdout.write(str(pathlib.Path(sys.argv[1]).resolve(strict=True)))" $dir 2>$null)"}- $ran = $null- if (Wait-Job $job -Timeout 30) { $ran = Receive-Job $job -ErrorAction SilentlyContinue } else { Stop-Job $job }+ $answer = ""+ if (Wait-Job $job -Timeout 30) { $answer = "$(Receive-Job $job -ErrorAction SilentlyContinue)".Trim() } else { Stop-Job $job }Remove-Job $job -Force- if ($ran -and $ran.Code -eq 0 -and -not [string]::IsNullOrWhiteSpace($ran.Out)) { $onPath = $cmd }+ if (-not [string]::IsNullOrWhiteSpace($answer)) { $usable = $src }}}- if ($onPath) {- Write-Host " FAIL Get-StudioEarlyPython found nothing, yet $($onPath.Source) is on PATH." -ForegroundColor Red+ if ($usable) {+ Write-Host " FAIL Get-StudioEarlyPython found nothing, yet $usable answers the same probe." -ForegroundColor RedWrite-Host " That is a broken extraction in this file, not a host without Python." -ForegroundColor Redexit 1}@@ -100,11 +121,7 @@ try {$alias = Join-Path $tmp "alias"$madeAlias = $falsetry {- if ($IsWindows -or $env:OS -eq "Windows_NT") {- New-Item -ItemType Junction -Path $alias -Target $real -ErrorAction Stop | Out-Null- } else {- New-Item -ItemType SymbolicLink -Path $alias -Target $real -ErrorAction Stop | Out-Null- }+ New-Item -ItemType $(if ($onWindows) { "Junction" } else { "SymbolicLink" }) -Path $alias -Target $real -ErrorAction Stop | Out-Null$madeAlias = $true} catch {Write-Host " SKIP could not create a directory alias: $($_.Exception.Message)" -ForegroundColor Yellow@@ -114,31 +131,25 @@ try {# Control: the lexical rung may fold a symlink on pwsh 7, but it is never exact.$savedFinder = ${function:Get-StudioEarlyPython}function Get-StudioEarlyPython { return $null }- $inexact = Resolve-StudioFinalPathInfo -Path $alias- Check "without the rung the alias identity is inexact (bites)" ($inexact.Exact -eq $false)+ Check "without the rung the alias identity is inexact (bites)" ((Resolve-StudioFinalPathInfo -Path $alias).Exact -eq $false)${function:Get-StudioEarlyPython} = $savedFinder$viaAlias = Get-StudioPythonFinalPath -Path $alias- $viaReal = Get-StudioPythonFinalPath -Path $realCheck "Python folds the alias onto its target" (- -not [string]::IsNullOrWhiteSpace($viaAlias) -and $viaAlias -eq $viaReal)-+ -not [string]::IsNullOrWhiteSpace($viaAlias) -and $viaAlias -eq (Get-StudioPythonFinalPath -Path $real))$infoAlias = Resolve-StudioFinalPathInfo -Path $alias- $infoReal = Resolve-StudioFinalPathInfo -Path $realCheck "the resolver reports the alias as exact" ($infoAlias.Exact -eq $true)- Check "the resolver gives one identity for both spellings" ($infoAlias.Path -eq $infoReal.Path)+ Check "the resolver gives one identity for both spellings" ($infoAlias.Path -eq (Resolve-StudioFinalPathInfo -Path $real).Path)}- # No interpreter: lexical and inexact, as before. Restored afterwards so later checks use the rung.- $savedFinder2 = ${function:Get-StudioEarlyPython}+ $savedFinder = ${function:Get-StudioEarlyPython}function Get-StudioEarlyPython { return $null }$script:StudioEarlyPythonProbed = $false$script:StudioPythonFinalPathCache = $null$noPy = Resolve-StudioFinalPathInfo -Path $realCheck "with no interpreter the identity is inexact, as before" ($noPy.Exact -eq $false)- Check "with no interpreter a usable path still comes back" (- -not [string]::IsNullOrWhiteSpace($noPy.Path))- ${function:Get-StudioEarlyPython} = $savedFinder2+ Check "with no interpreter a usable path still comes back" (-not [string]::IsNullOrWhiteSpace($noPy.Path))+ ${function:Get-StudioEarlyPython} = $savedFinder$script:StudioEarlyPythonProbed = $false$script:StudioEarlyPython = $nullCheck "the interpreter is back after the no-interpreter case" ($null -ne (Get-StudioEarlyPython))@@ -149,8 +160,7 @@ try {New-Item -ItemType Directory -Force -Path $unicodeDir | Out-Null$unicodeAnswer = Get-StudioPythonFinalPath -Path $unicodeDirCheck "a non-ASCII path survives the child process" (- -not [string]::IsNullOrWhiteSpace($unicodeAnswer) -and- $unicodeAnswer.EndsWith($unicodeName))+ -not [string]::IsNullOrWhiteSpace($unicodeAnswer) -and $unicodeAnswer.EndsWith($unicodeName))# Trim() would drop the U+00A0 and name the sibling instead.$nbspName = "studio-nbsp" + [char]0x00A0@@ -158,8 +168,7 @@ try {New-Item -ItemType Directory -Force -Path (Join-Path $tmp $nbspName) | Out-Null# .NET Framework strips it on the way in, so 5.1 cannot create or name such a directory at all.if (@(Get-ChildItem -LiteralPath $tmp -Name) -ccontains $nbspName) {- $nbspAnswer = Get-StudioPythonFinalPath -Path (Join-Path $tmp $nbspName)- Check "a trailing non-breaking space is kept" ("$nbspAnswer".EndsWith($nbspName))+ Check "a trailing non-breaking space is kept" ("$(Get-StudioPythonFinalPath -Path (Join-Path $tmp $nbspName))".EndsWith($nbspName))} else {Write-Host " SKIP this host strips a trailing U+00A0 from paths" -ForegroundColor Yellow}@@ -177,22 +186,20 @@ try {$loopOk = $true} catch {}if ($loopOk) {- $loopAnswer = Get-StudioPythonFinalPath -Path $loopA- Check "a link loop is not promoted to an exact identity" ([string]::IsNullOrWhiteSpace($loopAnswer))+ Check "a link loop is not promoted to an exact identity" ([string]::IsNullOrWhiteSpace((Get-StudioPythonFinalPath -Path $loopA)))# Test-Path reports the link itself, so the walk stops AT the link.- $loopChild = Resolve-StudioFinalPathInfo -Path (Join-Path $loopA "studio")- Check "a missing path under a link loop is not exact" ($loopChild.Exact -eq $false)+ Check "a missing path under a link loop is not exact" ((Resolve-StudioFinalPathInfo -Path (Join-Path $loopA "studio")).Exact -eq $false)}$dangling = Join-Path $tmp "dangling"+ $gone = Join-Path $tmp "gone"$danglingOk = $falsetry {- New-Item -ItemType SymbolicLink -Path $dangling -Target (Join-Path $tmp "gone") -ErrorAction Stop | Out-Null+ New-Item -ItemType SymbolicLink -Path $dangling -Target $gone -ErrorAction Stop | Out-Null$danglingOk = $true} catch {# 5.1 refuses a link to a missing target: link first, then remove the target.try {- $gone = Join-Path $tmp "gone"New-Item -ItemType Directory -Force -Path $gone | Out-NullNew-Item -ItemType SymbolicLink -Path $dangling -Target $gone -ErrorAction Stop | Out-NullRemove-Item -LiteralPath $gone -Recurse -Force@@ -200,26 +207,22 @@ try {} catch {}}if ($danglingOk) {- foreach ($suffix in @("", "studio", "a\b")) {- $probePath = if ($suffix) { Join-Path $dangling $suffix } else { $dangling }- $info = Resolve-StudioFinalPathInfo -Path $probePath- Check "a dangling link is not exact (suffix '$suffix')" ($info.Exact -eq $false)+ $checkDangling = {+ param($label)+ foreach ($suffix in @("", "studio", "a\b")) {+ $probePath = if ($suffix) { Join-Path $dangling $suffix } else { $dangling }+ Check "$label (suffix '$suffix')" ((Resolve-StudioFinalPathInfo -Path $probePath).Exact -eq $false)+ }}+ & $checkDangling "a dangling link is not exact"# Where Test-Path follows the link, the stripped entry's reparse bit alone must keep it inexact.function Test-Path {param([string]$LiteralPath)if ($LiteralPath.StartsWith($dangling)) { return $false }return (Microsoft.PowerShell.Management\Test-Path -LiteralPath $LiteralPath)}- try {- foreach ($suffix in @("", "studio", "a\b")) {- $probePath = if ($suffix) { Join-Path $dangling $suffix } else { $dangling }- $info = Resolve-StudioFinalPathInfo -Path $probePath- Check "a dangling link Test-Path reports missing is not exact (suffix '$suffix')" ($info.Exact -eq $false)- }- } finally {- Remove-Item Function:Test-Path- }+ try { & $checkDangling "a dangling link Test-Path reports missing is not exact" }+ finally { Remove-Item Function:Test-Path }}# 5.1 has no ArgumentList, so arguments go through one quoted string.@@ -230,28 +233,13 @@ try {-not [string]::IsNullOrWhiteSpace($spacedAnswer) -and $spacedAnswer.EndsWith("spaces"))$src = Get-Content -Raw -LiteralPath $installPs1- Check "argument construction does not assume ArgumentList exists" (- $src -match 'PSObject\.Properties\["ArgumentList"\]')- Check "the probe refuses an interpreter older than 3.8" (- $src -match 'sys\.version_info\s*<\s*\(3,\s*8\)')-- $refuser = Join-Path $tmp $(if ($IsWindows -or $env:OS -eq "Windows_NT") { "refuse.cmd" } else { "refuse.sh" })- if ($IsWindows -or $env:OS -eq "Windows_NT") {- "@echo off`r`nexit /b 2`r`n" | Set-Content -LiteralPath $refuser -Encoding ASCII- } else {- "#!/bin/sh`nexit 2`n" | Set-Content -LiteralPath $refuser- & chmod +x $refuser- }- Check "an interpreter that exits non-zero is rejected" (- $null -eq (Invoke-StudioEarlyPython -Exe $refuser -Path $real))+ Check "argument construction does not assume ArgumentList exists" ($src -match 'PSObject\.Properties\["ArgumentList"\]')+ Check "the probe refuses an interpreter older than 3.8" ($src -match 'sys\.version_info\s*<\s*\(3,\s*8\)')- $slow = Join-Path $tmp $(if ($IsWindows -or $env:OS -eq "Windows_NT") { "slow.cmd" } else { "slow.sh" })- if ($IsWindows -or $env:OS -eq "Windows_NT") {- "@echo off`r`nping -n 30 127.0.0.1 >nul`r`n" | Set-Content -LiteralPath $slow -Encoding ASCII- } else {- "#!/bin/sh`nsleep 30`n" | Set-Content -LiteralPath $slow- & chmod +x $slow- }+ $refuser = New-FakeExe "refuse" "exit /b 2" "exit 2"+ Check "an interpreter that exits non-zero is rejected" ($null -eq (Invoke-StudioEarlyPython -Exe $refuser -Path $real))++ $slow = New-FakeExe "slow" "ping -n 30 127.0.0.1 >nul" "sleep 30"$started = [DateTime]::UtcNow$hung = Invoke-StudioEarlyPython -Exe $slow -Path $real -TimeoutMs 2000$elapsed = ([DateTime]::UtcNow - $started).TotalSeconds@@ -259,13 +247,7 @@ try {Check "a hung interpreter is killed at the deadline, not waited out" ($elapsed -lt 15)# The interpreter exits at once but leaves a child holding stdout: the read is bounded too.- $holder = Join-Path $tmp $(if ($IsWindows -or $env:OS -eq "Windows_NT") { "holder.cmd" } else { "holder.sh" })- if ($IsWindows -or $env:OS -eq "Windows_NT") {- "@echo off`r`nstart /b `"`" ping -n 20 127.0.0.1`r`nexit /b 0`r`n" | Set-Content -LiteralPath $holder -Encoding ASCII- } else {- "#!/bin/sh`nsleep 20 &`nprintf '%s' `"`$5`"`nexit 0`n" | Set-Content -LiteralPath $holder- & chmod +x $holder- }+ $holder = New-FakeExe "holder" "start /b `"`" ping -n 20 127.0.0.1`r`nexit /b 0" "sleep 20 &`nprintf '%s' `"`$5`"`nexit 0"$started = [DateTime]::UtcNow$held = Invoke-StudioEarlyPython -Exe $holder -Path $real -TimeoutMs 2000$elapsed = ([DateTime]::UtcNow - $started).TotalSeconds@@ -277,15 +259,9 @@ try {$script:StudioEarlyPythonProbed = $false$script:StudioEarlyPython = $nullRemove-Item Function:\Get-StudioEarlyPython -ErrorAction SilentlyContinue- foreach ($name in @("Get-StudioEarlyPython")) {- $fn = $ast.FindAll({ param($n)- $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name- }, $true)- Invoke-Expression $fn[0].Extent.Text- }+ Invoke-Expression (Get-Fn "Get-StudioEarlyPython")$null = Get-StudioEarlyPython- $after = @(Get-ChildItem -LiteralPath $tmp -Recurse -Force).Count- Check "finding an interpreter writes nothing" ($after -eq $before)+ Check "finding an interpreter writes nothing" (@(Get-ChildItem -LiteralPath $tmp -Recurse -Force).Count -eq $before)# -I alone still imports site, so a sitecustomize could print into the answer or hang.$exe = Get-StudioEarlyPython@@ -295,27 +271,22 @@ try {Check "-S is what turns site off" ("$isoPlus".Trim() -eq "1")# Read from the source: a planted sitecustomize is shadowed by the host's own.- $launcherFn = @($ast.FindAll({ param($n)- $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and- $n.Name -eq "Invoke-StudioEarlyPythonScript"- }, $true))[0].Extent.Text- Check "the launcher runs every probe with -S as well as -I" (- $launcherFn -match '@\("-I",\s*"-S",\s*"-c"')+ $launcherFn = Get-Fn "Invoke-StudioEarlyPythonScript"+ Check "the launcher runs every probe with -S as well as -I" ($launcherFn -match '@\("-I",\s*"-S",\s*"-B",\s*"-c"')+ Check "and with -B, so a pre-lock probe writes no bytecode" ($launcherFn -match '"-B"')+# A miss recorded before $VenvDir existed (the --tauri path) is probed again once it does.$venvHome = Join-Path $tmp "venvhome"- $venvBin = if ($IsWindows -or $env:OS -eq "Windows_NT") { "Scripts" } else { "bin" }- $venvLeaf = if ($IsWindows -or $env:OS -eq "Windows_NT") { "python.exe" } else { "python3" }+ $venvBin = if ($onWindows) { "Scripts" } else { "bin" }+ $venvLeaf = if ($onWindows) { "python.exe" } else { "python3" }New-Item -ItemType Directory -Force -Path (Join-Path $venvHome $venvBin) | Out-NullCopy-Item -LiteralPath $exe -Destination (Join-Path $venvHome (Join-Path $venvBin $venvLeaf)) -Forcefunction Get-Command { param($Name, [switch]$All, $CommandType, $ErrorAction) return @() }Remove-Variable -Name VenvDir -Scope Script -ErrorAction SilentlyContinueRemove-Variable -Name VenvDir -Scope Global -ErrorAction SilentlyContinue- $script:StudioEarlyPythonProbed = $false- $script:StudioEarlyPython = $null- $script:StudioEarlyPythonProbedWithoutVenv = $false+ Reset-EarlyPythonCheck "with no venv and no system Python the probe finds nothing" ($null -eq (Get-StudioEarlyPython))- Check "and it recorded that the venv was unknown when it looked" (- $script:StudioEarlyPythonProbedWithoutVenv -eq $true)+ Check "and it recorded that the venv was unknown when it looked" ($script:StudioEarlyPythonProbedWithoutVenv -eq $true)$global:StudioHome = $tmp$global:StudioRedirectMode = "env"$global:VenvDir = $venvHome@@ -340,15 +311,15 @@ try {$script:StudioEarlyPython = $null$global:StudioRedirectMode = "default"$found = Get-StudioEarlyPython- Check "once the venv directory is known the venv interpreter is found after all" (- -not [string]::IsNullOrWhiteSpace($found))- Check "and it is the one inside the venv, not some other copy" (- "$found" -like ("*" + $venvBin + "*"))+ Check "once the venv directory is known the venv interpreter is found after all" (-not [string]::IsNullOrWhiteSpace($found))+ Check "and it is the one inside the venv, not some other copy" ("$found" -like ("*" + $venvBin + "*"))$script:ReprobeCount = 0function Test-Path { param($LiteralPath, $PathType, $ErrorAction) $script:ReprobeCount++; return $false }$null = Get-StudioEarlyPythonCheck "a hit is not probed again" ($script:ReprobeCount -eq 0)- # An uninspectable candidate (throws under Stop) is skipped without escaping.+ # An uninspectable candidate (an unreadable directory throws under Stop) is skipped, and nothing+ # escapes into the lock-name hash. The cache is saved and restored around these throw tests.+ $savedEarly = @($script:StudioEarlyPython, $script:StudioEarlyPythonProbed, $script:StudioEarlyPythonProbedWithoutVenv)function Test-Path { param($LiteralPath, $PathType, $ErrorAction)throw [System.UnauthorizedAccessException]::new("Access to the path '$LiteralPath' is denied.") }$script:StudioEarlyPythonProbed = $false@@ -356,37 +327,363 @@ try {$threw = $nulltry { $none = Get-StudioEarlyPython } catch { $threw = $_ }Check "a candidate that cannot be inspected is skipped, not fatal" ($null -eq $threw -and $null -eq $none)- $savedFinder2 = ${function:Get-StudioEarlyPython}+ $savedFinder = ${function:Get-StudioEarlyPython}function Get-StudioEarlyPython { throw "discovery failed" }$script:StudioPythonFinalPathCache = $null$threw = $nulltry { $none = Get-StudioPythonFinalPath -Path $real } catch { $threw = $_ }Check "a discovery failure declines to the lexical rung instead of throwing" ($null -eq $threw -and $null -eq $none)- ${function:Get-StudioEarlyPython} = $savedFinder2+ ${function:Get-StudioEarlyPython} = $savedFinderRemove-Item Function:Test-Path -ErrorAction SilentlyContinue+ $script:StudioEarlyPython, $script:StudioEarlyPythonProbed, $script:StudioEarlyPythonProbedWithoutVenv = $savedEarlyRemove-Item Function:Get-Command -ErrorAction SilentlyContinueRemove-Variable -Name VenvDir, StudioHome, StudioRedirectMode -Scope Global -ErrorAction SilentlyContinue+$script:ResolveCalls = 0$savedInvoke = ${function:Invoke-StudioEarlyPython}function Invoke-StudioEarlyPython { param($Exe, $Path, $TimeoutMs) $script:ResolveCalls++; return $Path }- $savedFinder3 = ${function:Get-StudioEarlyPython}+ $savedFinder = ${function:Get-StudioEarlyPython}function Get-StudioEarlyPython { return "python3" }$script:StudioPythonFinalPathCache = $null- $null = Get-StudioPythonFinalPath -Path $real- $null = Get-StudioPythonFinalPath -Path $real- $null = Get-StudioPythonFinalPath -Path $real+ 1..3 | ForEach-Object { $null = Get-StudioPythonFinalPath -Path $real }Check "three calls for one path spawn one child" ($script:ResolveCalls -eq 1)$null = Get-StudioPythonFinalPath -Path $tmpCheck "and a different path still spawns its own" ($script:ResolveCalls -eq 2)function Invoke-StudioEarlyPython { param($Exe, $Path, $TimeoutMs) $script:ResolveCalls++; return $null }$missPath = Join-Path $tmp "no-such-thing"- $null = Get-StudioPythonFinalPath -Path $missPath- $null = Get-StudioPythonFinalPath -Path $missPath+ 1..2 | ForEach-Object { $null = Get-StudioPythonFinalPath -Path $missPath }Check "a miss is remembered as well as an answer" ($script:ResolveCalls -eq 3)${function:Invoke-StudioEarlyPython} = $savedInvoke- ${function:Get-StudioEarlyPython} = $savedFinder3+ ${function:Get-StudioEarlyPython} = $savedFinder+$script:StudioPythonFinalPathCache = $null+ $batchDir = Join-Path $tmp "batch"+ $null = New-Item -ItemType Directory -Path $batchDir -Force+ $batchPaths = @(foreach ($i in 1..12) {+ $leaf = Join-Path $batchDir "f$i.txt"+ Set-Content -LiteralPath $leaf -Value "x" -Encoding utf8+ $leaf+ })++ $script:RealInvoke = ${function:Invoke-StudioEarlyPython}+ function Invoke-StudioEarlyPython {+ param([string]$Exe, [string]$Path, [int]$TimeoutMs = 10000)+ $script:SingleCalls+++ return (& $script:RealInvoke -Exe $Exe -Path $Path -TimeoutMs $TimeoutMs)+ }+ $script:SingleCalls = 0+ foreach ($leaf in $batchPaths) { $null = Get-StudioPythonFinalPath -Path $leaf }+ Check "unbatched, every resolution starts its own child (bites)" ($script:SingleCalls -eq $batchPaths.Count)+ $script:StudioPythonFinalPathCache = $null+ $script:SingleCalls = 0+ Resolve-StudioFinalPathsInOneChild -Paths $batchPaths+ Check "the batch starts no single-path child of its own" ($script:SingleCalls -eq 0)+ foreach ($leaf in $batchPaths) { $null = Get-StudioPythonFinalPath -Path $leaf }+ Check "and every resolution after it is served without one" ($script:SingleCalls -eq 0)++ # A declined private directory still batches: the list rides the environment instead.+ $realDirFn = ${function:New-StudioChildScriptDirectory}+ function New-StudioChildScriptDirectory { return "" }+ try {+ $script:StudioPythonFinalPathCache = $null+ $script:SingleCalls = 0+ Resolve-StudioFinalPathsInOneChild -Paths $batchPaths+ foreach ($leaf in $batchPaths) { $null = Get-StudioPythonFinalPath -Path $leaf }+ Check "a declined private directory still batches, through the environment" ($script:SingleCalls -eq 0)+ Check "and leaves no list variable behind" ($null -eq $env:UNSLOTH_FINAL_PATHS)+ # Past the block limit the list splits across children, each under it and none lost.+ $script:RealScript = ${function:Invoke-StudioEarlyPythonScript}+ $script:ChunkSizes = @(); $script:ChunkPaths = 0+ function Invoke-StudioEarlyPythonScript {+ param([string]$Exe, [string]$Script, [string[]]$ScriptArgs = @(), [int]$TimeoutMs = 10000)+ $script:ChunkSizes += $env:UNSLOTH_FINAL_PATHS.Length+ $script:ChunkPaths += @($env:UNSLOTH_FINAL_PATHS -split "`n").Count+ return ""+ }+ $longPaths = @(1..300 | ForEach-Object { Join-Path $batchDir (("p{0:D3}-" -f $_) + ("x" * 140)) })+ $script:StudioPythonFinalPathCache = $null+ Resolve-StudioFinalPathsInOneChild -Paths $longPaths+ ${function:Invoke-StudioEarlyPythonScript} = $script:RealScript+ Check "a list past the block limit goes out in more than one child" ($script:ChunkSizes.Count -gt 1)+ Check "each chunk stays under 30000 characters" (@($script:ChunkSizes | Where-Object { $_ -gt 30000 }).Count -eq 0)+ Check "and every path is sent exactly once" ($script:ChunkPaths -eq $longPaths.Count)+ } finally { ${function:New-StudioChildScriptDirectory} = $realDirFn }++ # The batch must return exactly what the single-path rung returns, or it is a second identity.+ $script:StudioPythonFinalPathCache = $null+ $one = Get-StudioPythonFinalPath -Path $batchPaths[0]+ $script:StudioPythonFinalPathCache = $null+ Resolve-StudioFinalPathsInOneChild -Paths $batchPaths+ $batched = Get-StudioPythonFinalPath -Path $batchPaths[0]+ Check "the batched answer is a real path (bites)" (-not [string]::IsNullOrWhiteSpace($one))+ Check "the batch answers byte for byte what the single rung answers" ($batched -ceq $one)++ $script:StudioPythonFinalPathCache = $null+ $missing = Join-Path $batchDir "no-such-file"+ Resolve-StudioFinalPathsInOneChild -Paths @($missing)+ Check "an unresolvable path is recorded" ($script:StudioPythonFinalPathCache.ContainsKey($missing))+ Check "and recorded as no exact answer" ($null -eq $script:StudioPythonFinalPathCache[$missing])+ $script:SingleCalls = 0+ $null = Get-StudioPythonFinalPath -Path $missing+ Check "and is not re-asked" ($script:SingleCalls -eq 0)++ # A child that answers nothing, or only SOME paths, leaves the rest uncached: absence is not a miss.+ $script:RealScript = ${function:Invoke-StudioEarlyPythonScript}+ function Invoke-StudioEarlyPythonScript {+ param([string]$Exe, [string]$Script, [string[]]$ScriptArgs = @(), [int]$TimeoutMs = 10000)+ return ($script:PartialAnswer)+ }+ $script:PartialAnswer = ""+ $script:StudioPythonFinalPathCache = $null+ Resolve-StudioFinalPathsInOneChild -Paths $batchPaths+ $cachedAfterFailure = 0+ if ($null -ne $script:StudioPythonFinalPathCache) { $cachedAfterFailure = $script:StudioPythonFinalPathCache.Count }+ Check "a silent child caches nothing at all" ($cachedAfterFailure -eq 0)+ ${function:Invoke-StudioEarlyPythonScript} = $script:RealScript+ $script:SingleCalls = 0+ $null = Get-StudioPythonFinalPath -Path $batchPaths[0]+ Check "so the single-path rung still gets its turn" ($script:SingleCalls -eq 1)++ $script:StudioPythonFinalPathCache = $null+ function Invoke-StudioEarlyPythonScript {+ param([string]$Exe, [string]$Script, [string[]]$ScriptArgs = @(), [int]$TimeoutMs = 10000)+ return ($script:PartialAnswer)+ }+ $script:PartialAnswer = "$($batchPaths[0])|$($batchPaths[0])"+ Resolve-StudioFinalPathsInOneChild -Paths $batchPaths+ Check "a partial answer caches only what was answered" (+ $script:StudioPythonFinalPathCache.Count -eq 1 -and $script:StudioPythonFinalPathCache.ContainsKey($batchPaths[0]))+ Check "and leaves the unanswered paths for the single rung" (-not $script:StudioPythonFinalPathCache.ContainsKey($batchPaths[1]))+ ${function:Invoke-StudioEarlyPythonScript} = $script:RealScript++ $batchText = Get-Fn "Resolve-StudioFinalPathsInOneChild"+ Check "the reader strips a byte order mark" ($batchText -match "encoding='utf-8-sig'")+ Check "and does not read the list as plain utf-8" ($batchText -notmatch "encoding='utf-8'\)")+ # Run it: the same expression against a real BOM-prefixed file must resolve every line, and the+ # plain utf-8 control must LOSE the first one, or the check above is only spelling.+ $bomFile = Join-Path $batchDir "with-bom.txt"+ [System.IO.File]::WriteAllBytes($bomFile, [byte[]](0xEF, 0xBB, 0xBF) + [System.Text.Encoding]::UTF8.GetBytes(($batchPaths -join "`n")))+ $bomScript = @(+ "import pathlib,sys", "n=0",+ "with open(sys.argv[1],'r',encoding='utf-8-sig') as fh:",+ " for line in fh:",+ " p=line.rstrip('\r\n')",+ " if not p: continue",+ " try:",+ " pathlib.Path(p).resolve(strict=True)",+ " n+=1",+ " except Exception:",+ " pass",+ "sys.stdout.buffer.write(str(n).encode('utf-8'))"+ ) -join [char]10+ foreach ($row in @(+ @("a BOM-prefixed list resolves every line, first one included", $bomScript, $batchPaths.Count),+ @("and plain utf-8 really does lose it (bites)", ($bomScript -replace "utf-8-sig", "utf-8"), ($batchPaths.Count - 1))+ )) {+ $count = Invoke-StudioEarlyPythonScript -Exe $exe -Script $row[1] -ScriptArgs @($bomFile) -TimeoutMs 30000+ Check $row[0] ("$count".Trim() -eq "$($row[2])")+ }++ # The list goes through a file, not argv: a few hundred paths pass the 32767 character limit.+ Check "the batch hands the child a list FILE rather than an argv of paths" (+ $batchText -match 'Set-Content -LiteralPath \$listFile' -and $batchText -match 'Args = @\(\$listFile\)')+ Check "and without one, chunks under the environment block limit" (+ $batchText -match 'UNSLOTH_FINAL_PATHS' -and $batchText -match '-gt 30000')+ Check "the batch resolves with the same expression the single rung uses" ($batchText -match 'pathlib\.Path\(p\)\.resolve\(strict=True\)')+ Check "and keeps the same version gate" ($batchText -match 'sys\.version_info < \(3,8\)')+ Check "and applies the same two post-checks" (+ $batchText -match 'Split-Path -IsAbsolute \$answer' -and $batchText -match 'Test-Path -LiteralPath \$answer')++ $discoveryCode = ((Get-Fn "Get-StudioEarlyPython") -split "`r?`n" |+ Where-Object { -not ($_.TrimStart().StartsWith("#")) }) -join "`n"+ Check "the comment stripper kept the code (bites)" ($discoveryCode -match 'Get-Command')+ Check "discovery skips the WindowsApps execution aliases" ($discoveryCode -match 'WindowsApps')+ foreach ($case in @(+ @{ P = "C:\Users\a\AppData\Local\Microsoft\WindowsApps\python3.exe"; Skip = $true },+ @{ P = "C:\Users\a\AppData\Local\Microsoft\WindowsApps\python.exe"; Skip = $true },+ @{ P = "C:/Users/a/AppData/Local/Microsoft/WindowsApps/python.exe"; Skip = $true },+ @{ P = "C:\Python312\python.exe"; Skip = $false },+ @{ P = "C:\Users\a\.unsloth\studio\.venv\Scripts\python.exe"; Skip = $false },+ @{ P = "/usr/bin/python3"; Skip = $false }+ )) {+ $hit = [bool]("$($case.P)" -match '(?i)[\\/]Microsoft[\\/]WindowsApps[\\/]')+ Check "the alias filter $(if ($case.Skip) { 'skips' } else { 'keeps' }) $($case.P)" ($hit -eq $case.Skip)+ }++ $savedWinEnv = Save-Env @("ProgramFiles", "SystemRoot")+ $env:ProgramFiles = "C:\Program Files"+ $env:SystemRoot = "C:\Windows"+ $script:AclTable = @{}+ $script:AclDefault = ("O:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464G:SY" ++ "D:AI(A;;FA;;;S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464)" ++ "(A;OICIIO;GA;;;S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464)" ++ "(A;;0x1301bf;;;SY)(A;OICIIO;GA;;;SY)(A;;0x1301bf;;;BA)(A;OICIIO;GA;;;BA)" ++ "(A;;0x1200a9;;;BU)(A;OICIIO;GXGR;;;BU)(A;;0x1200a9;;;AC)(A;OICIIO;GXGR;;;AC)")+ $script:AclThrows = $false+ ${function:Get-Acl} = $aclStub+ $tempSddl = ("O:BAG:SYD:PAI(A;;FA;;;SY)(A;OICIIO;GA;;;SY)(A;;FA;;;BA)(A;OICIIO;GA;;;BA)" ++ "(A;;0x100004;;;BU)(A;;0x100002;;;BU)(A;OICIIO;GA;;;CO)")+ $ownedSddl = "O:S-1-5-21-1-2-3-1001G:SYD:PAI(A;;FA;;;SY)(A;;FA;;;BA)"+ $adminRootRows = @(+ @("the admin-root test accepts a system-wide location", "C:\Program Files\Python312\python.exe", $true),+ @("and is not fooled by a look-alike root", "C:\Program Files Evil\python.exe", $false),+ @("and rejects a per-user install", "C:\Users\me\AppData\Local\Programs\Python\python.exe", $false),+ @("and rejects an empty path", "", $false)+ )+ foreach ($r in $adminRootRows) { Check $r[0] ((Test-StudioPathUnderAdminRoot -Path $r[1]) -eq $r[2]) }++ Check "a location test alone would have accepted C:\Windows\Temp (bites)" ("C:\Windows\Temp\python.exe" -like "C:\Windows\*")+ Check "but the compatibility directories are refused outright" (+ (Test-StudioPathUnderAdminRoot -Path "C:\Windows\Temp\python.exe") -eq $false)+ foreach ($leaf in @("Tasks", "Tracing", "System32\Tasks", "System32\spool\drivers\color",+ "System32\com\dmp", "SysWOW64\FxsTmp")) {+ Check "and so is C:\Windows\$leaf" ((Test-StudioPathUnderAdminRoot -Path "C:\Windows\$leaf\python.exe") -eq $false)+ }+ $script:AclTable["C:\Windows\Sloppy"] = $tempSddl+ Check "a user-writable directory under a protected root is refused by its ACL" (+ (Test-StudioPathUnderAdminRoot -Path "C:\Windows\Sloppy\python.exe") -eq $false)+ Check "and so is a directory whose parent is user-writable, however tight its own ACL" (+ (Test-StudioPathUnderAdminRoot -Path "C:\Windows\Sloppy\mine\python.exe") -eq $false)+ $script:AclTable["C:\Program Files\Mine"] = $ownedSddl+ Check "a directory owned by a standard user is refused however tight its DACL" (+ (Test-StudioPathUnderAdminRoot -Path "C:\Program Files\Mine\python.exe") -eq $false)+ $script:AclTable.Remove("C:\Program Files\Mine")+ Check "control: the same path passes once its owner is administrators (bites)" (+ (Test-StudioPathUnderAdminRoot -Path "C:\Program Files\Mine\python.exe") -eq $true)+ # An ACL that cannot be read is not evidence that it is a safe one.+ $script:AclThrows = $true+ Check "an unreadable ACL declines rather than trusting the location" (+ (Test-StudioPathUnderAdminRoot -Path "C:\Program Files\Python312\python.exe") -eq $false)+ $script:AclThrows = $false++ foreach ($r in @(+ @("System32's own descriptor is not user-writable", $script:AclDefault, $false),+ @("Temp's is", $tempSddl, $true),+ @("an owner that is a plain user is enough", $ownedSddl, $true),+ @("a descriptor this cannot parse is treated as writable", "not a descriptor", $true),+ @("and so is an empty one", "", $true),+ @("the owner is split from the group correctly", "O:BAG:SYD:PAI(A;;FA;;;BA)", $false),+ @("an inherit-only CREATOR OWNER grant is not an effective one", "O:BAG:SYD:PAI(A;;FA;;;BA)(A;OICIIO;GA;;;CO)", $false),+ @("but the same grant without the inherit-only flag is", "O:BAG:SYD:PAI(A;;FA;;;BA)(A;OICI;GA;;;CO)", $true),+ @("a deny ACE is not a grant", "O:BAG:SYD:PAI(A;;FA;;;BA)(D;;FA;;;BU)", $false)+ )) { Check $r[0] ((Test-StudioSddlWritableByNonAdmin -Sddl $r[1]) -eq $r[2]) }+ foreach ($r in @(+ @("read and execute is not write", "0x1200a9", $false),+ @("create files is", "0x100002", $true),+ @("append data is", "0x100004", $true),+ @("a full 32-bit mask does not overflow into a Double", "0xffffffff", $true),+ @("the word forms are read too", "FA", $true),+ @("and a read-only word form is not a write", "FRFX", $false)+ )) { Check $r[0] ((Test-StudioSddlRightsAreWrite -Rights $r[1]) -eq $r[2]) }+ Remove-Item Function:Get-Acl -ErrorAction SilentlyContinue+ Restore-Env $savedWinEnv++ # Driven through the real discovery, elevated, with this host's interpreter, which is not under a+ # Windows protected root, so the rung has to decline.+ $savedOsElev = Save-Env @("OS")+ try {+ $env:OS = "Windows_NT"+ function Test-StudioChildScriptDirectoryElevated { return $true }+ Reset-EarlyPython+ $script:StudioFinalPathWarned = $false+ Check "an elevated run declines a user-writable interpreter" ([string]::IsNullOrWhiteSpace((Get-StudioEarlyPython)))+ function Test-StudioChildScriptDirectoryElevated { return $false }+ Reset-EarlyPython+ Check "control: an unelevated run still finds one" (-not [string]::IsNullOrWhiteSpace((Get-StudioEarlyPython)))+ $hostPy = (Get-Command python3 -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1).Source+ if ($savedOsElev["OS"] -ne "Windows_NT" -and $hostPy) {+ $venvRoot = Join-Path $tmp "elevated-venv"+ New-Item -ItemType Directory -Force -Path (Join-Path $venvRoot "bin") | Out-Null+ $venvPy = Join-Path $venvRoot "bin/python3"+ New-Item -ItemType SymbolicLink -Path $venvPy -Target $hostPy | Out-Null+ function Test-StudioChildScriptDirectoryElevated { return $true }+ $VenvDir = $venvRoot+ $StudioHome = Join-Path $tmp "elevated-home"+ # It runs before the destination guard, so only where that guard would let it run.+ Remove-Variable -Name StudioRedirectMode -ErrorAction SilentlyContinue+ Reset-EarlyPython+ Check "an elevated run declines the venv interpreter while the layout is unknown" (+ [string]::IsNullOrWhiteSpace((Get-StudioEarlyPython)))+ $StudioRedirectMode = 'default'+ Reset-EarlyPython+ Check "an elevated run still uses the default layout's venv interpreter" ((Get-StudioEarlyPython) -eq $venvPy)+ $StudioRedirectMode = 'env'+ Reset-EarlyPython+ Check "an elevated run declines a custom home's venv interpreter that is not Unsloth's" (+ [string]::IsNullOrWhiteSpace((Get-StudioEarlyPython)))+ Set-Content -LiteralPath (Join-Path $venvRoot ".unsloth-studio-owned") -Value ""+ Reset-EarlyPython+ Check "an elevated run uses a custom home's venv interpreter once it is Unsloth's" ((Get-StudioEarlyPython) -eq $venvPy)+ Remove-Variable -Name VenvDir, StudioHome, StudioRedirectMode -ErrorAction SilentlyContinue+ }+ } finally {+ function Test-StudioChildScriptDirectoryElevated { return $false }+ Restore-Env $savedOsElev+ $script:StudioEarlyPythonProbed = $false+ $script:StudioEarlyPython = $null+ }++ $hostPy3 = (Get-Command python3 -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1).Source+ if ($onWindows -or -not $hostPy3) {+ Write-Host " SKIP interpreter-file checks need a non-Windows host with python3" -ForegroundColor Yellow+ } else {+ $savedEnv2 = Save-Env @("ProgramFiles", "SystemRoot", "OS", "PATH")+ $pfRoot = Join-Path $tmp "pf"+ $pfBin = Join-Path $pfRoot "bin"+ New-Item -ItemType Directory -Force -Path $pfBin | Out-Null+ $wrapper = Join-Path $pfBin "python3"+ Set-Content -LiteralPath $wrapper -Value ("#!/bin/sh`nexec '" + $hostPy3 + "' `"`$@`"`n") -NoNewline+ & chmod +x $wrapper+ $script:AclTable = @{}+ $script:AclThrows = $false+ ${function:Get-Acl} = $aclStub+ $reprobe = { Reset-EarlyPython; return (Get-StudioEarlyPython) }+ $fileOk = { (Test-StudioInterpreterFileIsAdminOnly -Path $wrapper) }+ $walkOk = { (Test-StudioPathUnderAdminRoot -Path $wrapper) }+ try {+ $env:ProgramFiles = $pfRoot+ $env:SystemRoot = Join-Path $tmp "win"+ $env:OS = "Windows_NT"+ $env:PATH = $pfBin + [System.IO.Path]::PathSeparator + $savedEnv2["PATH"]+ function Test-StudioChildScriptDirectoryElevated { return $true }++ Check "the file helper accepts an administrator-only regular file" ((& $fileOk) -eq $true)+ Check "control: the directory walk alone accepts this location" ((& $walkOk) -eq $true)+ Check "control: an elevated run accepts an administrator-only file under an administrator-only root" (+ (& $reprobe) -eq $wrapper)++ $script:AclTable[$wrapper] = "O:BAG:SYD:PAI(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1301bf;;;BU)"+ Check "the directory walk still accepts it, so it cannot see the file's ACL (bites)" ((& $walkOk) -eq $true)+ Check "the file helper refuses a user-writable file under admin-only directories" ((& $fileOk) -eq $false)+ Check "an elevated run declines a PATH interpreter whose own DACL a standard user can write" (+ [string]::IsNullOrWhiteSpace((& $reprobe)))++ $script:AclTable[$wrapper] = "O:S-1-5-21-1-2-3-1001G:SYD:PAI(A;;FA;;;SY)(A;;FA;;;BA)"+ Check "the file helper refuses a file owned by a standard user" ((& $fileOk) -eq $false)+ $script:AclTable.Remove($wrapper)++ $script:AclThrows = $true+ Check "an unreadable file ACL declines" ((& $fileOk) -eq $false)+ $script:AclThrows = $false+ Check "the file helper refuses an empty path" ((Test-StudioInterpreterFileIsAdminOnly -Path "") -eq $false)+ Check "and a missing file" ((Test-StudioInterpreterFileIsAdminOnly -Path (Join-Path $pfBin "absent")) -eq $false)++ Remove-Item -LiteralPath $wrapper -Force+ New-Item -ItemType SymbolicLink -Path $wrapper -Target $hostPy3 | Out-Null+ Check "the directory walk accepts the symlinked candidate (bites)" ((& $walkOk) -eq $true)+ Check "the file helper refuses a symlinked interpreter" ((& $fileOk) -eq $false)+ Check "an elevated run declines a symlinked PATH interpreter" ([string]::IsNullOrWhiteSpace((& $reprobe)))+ function Test-StudioChildScriptDirectoryElevated { return $false }+ Check "control: an unelevated run is unaffected by the file check" (-not [string]::IsNullOrWhiteSpace((& $reprobe)))+ } finally {+ function Test-StudioChildScriptDirectoryElevated { return $false }+ Remove-Item Function:Get-Acl -ErrorAction SilentlyContinue+ Restore-Env $savedEnv2+ $script:StudioEarlyPythonProbed = $false+ $script:StudioEarlyPython = $null+ }+ }} finally {Remove-Item -LiteralPath $tmp -Recurse -Force -ErrorAction SilentlyContinue}diff --git a/tests/studio/test_early_python_process_image.ps1 b/tests/studio/test_early_python_process_image.ps1index 68962005826..22c15ef5b8e 100644--- a/tests/studio/test_early_python_process_image.ps1+++ b/tests/studio/test_early_python_process_image.ps1@@ -19,7 +19,14 @@ $ast = [System.Management.Automation.Language.Parser]::ParseFile($installPs1, [rif ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "install.ps1 has parse errors" }foreach ($name in @("Invoke-StudioEarlyPythonScript", "Invoke-StudioEarlyPython", "Get-StudioEarlyPython",- "Get-StudioPythonProcessImageTable", "Get-StudioProcessImagePath"+ "New-StudioChildScriptDirectory", "Test-StudioChildScriptDirectoryElevated",+ "Get-StudioPythonProcessImageTable", "Get-StudioWmiProcessImageRows", "Get-StudioProcessImagePath",+ # What Get-StudioEarlyPython reaches on Windows: the elevation gate and its helpers. Off+ # Windows it never calls them, which is how a missing one once passed here and failed CI.+ "Invoke-StudioSystem32ToolBounded", "Get-StudioSystem32Tool", "Test-StudioPathUnderAdminRoot",+ "Test-StudioSddlRightsAreWrite", "Test-StudioSddlPrincipalIsAdminOnly",+ "Test-StudioSddlWritableByNonAdmin", "Test-StudioDirectoryIsAdminOnly", "Test-StudioInterpreterFileIsAdminOnly",+ "Get-StudioLexicalParent", "Write-StudioFinalPathDegraded")) {$fn = $ast.FindAll({ param($n)$n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name@@ -30,10 +37,16 @@ foreach ($name in @(# A standard user: an elevated run declines this probe, and hosted Windows runners are elevated.function Get-ElevationState { return "false" }-function Initialize-StudioProcessImageNativeType { return $false }-function Get-StudioNativeProcessImagePath { param([int]$ProcessId) return $null }+if ($env:OS -eq "Windows_NT") { function Test-StudioChildScriptDirectoryElevated { return $false } }+function Get-Process { param($Id, $ErrorAction) throw "no such process" }-function Get-CimInstance { param($ClassName, $ErrorAction) throw "WMI is unavailable" }+$realWmiRows = ${function:Get-StudioWmiProcessImageRows}+Check "a WMI query that never returns is abandoned at the deadline" (+ (Measure-Command { $rows = @(& $realWmiRows -Query { Start-Sleep -Seconds 60 } -TimeoutSeconds 2) }).TotalSeconds -lt 20 -and+ $rows.Count -eq 0)+Check "control: a WMI query that answers returns its rows" (+ @(& $realWmiRows -Query { [pscustomobject]@{ ProcessId = 9; ExecutablePath = "C:\x.exe" } }).ExecutablePath -eq "C:\x.exe")+function Get-StudioWmiProcessImageRows { return @() }function Write-StudioLine { param([string]$Line, [string]$ForegroundColor = "") }function Reset-RungState {@@ -51,24 +64,43 @@ $script:StudioEarlyPythonProbed = $false$script:StudioEarlyPython = $null$exe = Get-StudioEarlyPythonif (-not $exe) {- # A runnable python on PATH means a broken extraction, not a host without Python.- $onPath = $null+ # "No interpreter" is a real and supported state, so it is a skip. But it is also what a+ # BROKEN EXTRACTION looks like from here: a helper this file forgot to pull out of+ # install.ps1 makes Get-StudioEarlyPython fail, the probe finds nothing, and the suite exits 0+ # having tested nothing. That happened once and CI recorded it as a pass. Tell the two apart.+ # The documented opt-out first. UNSLOTH_EARLY_PYTHON_PROBE=0 means "do not spawn an interpreter+ # on this host", so discovery returning nothing is the switch working, not a broken extraction.+ if ("$($env:UNSLOTH_EARLY_PYTHON_PROBE)".Trim() -eq "0") {+ Write-Host " SKIP UNSLOTH_EARLY_PYTHON_PROBE=0, so this rung is switched off by request" -ForegroundColor Yellow+ exit 0+ }+ $elevatedHost = $false+ if ($env:OS -eq "Windows_NT") { try { $elevatedHost = [bool](Test-StudioChildScriptDirectoryElevated) } catch { $elevatedHost = $true } }+ $usable = $nullforeach ($n in @("python3", "python")) {- foreach ($cmd in @(Get-Command $n -All -CommandType Application -ErrorAction SilentlyContinue)) {- if ($onPath -or -not $cmd.Source) { continue }- $job = Start-Job -ArgumentList $cmd.Source -ScriptBlock {- param($exe)- $o = & $exe -I -S -c "import os,sys;sys.stdout.write(os.path.realpath('.') if sys.version_info >= (3, 8) else '')" 2>$null- [pscustomobject]@{ Out = "$o"; Code = $LASTEXITCODE }+ foreach ($src in @(Get-Command $n -All -CommandType Application -ErrorAction SilentlyContinue |+ Select-Object -ExpandProperty Source -ErrorAction SilentlyContinue)) {+ if ($usable) { break }+ if ([string]::IsNullOrWhiteSpace($src)) { continue }+ if ("$src" -match '(?i)[\\/]Microsoft[\\/]WindowsApps[\\/]') { continue }+ # The installer's own elevated rule: an elevated run refuses an interpreter a standard user+ # can replace, so such a candidate is not one discovery should have found.+ if ($elevatedHost -and -not (Test-StudioPathUnderAdminRoot -Path $src)) { continue }+ $here = Split-Path -Parent $src+ if ([string]::IsNullOrWhiteSpace($here)) { continue }+ # In a job with a deadline: a shim that starts and never exits must be a skip, not a hang.+ $job = Start-Job -ArgumentList $src, $here -ScriptBlock {+ param($exe, $dir)+ "$(& $exe -I -S -c "import pathlib,sys`nsys.exit(2) if sys.version_info < (3,8) else None`nsys.stdout.write(str(pathlib.Path(sys.argv[1]).resolve(strict=True)))" $dir 2>$null)"}- $ran = $null- if (Wait-Job $job -Timeout 30) { $ran = Receive-Job $job -ErrorAction SilentlyContinue } else { Stop-Job $job }+ $answer = ""+ if (Wait-Job $job -Timeout 30) { $answer = "$(Receive-Job $job -ErrorAction SilentlyContinue)".Trim() } else { Stop-Job $job }Remove-Job $job -Force- if ($ran -and $ran.Code -eq 0 -and -not [string]::IsNullOrWhiteSpace($ran.Out)) { $onPath = $cmd }+ if (-not [string]::IsNullOrWhiteSpace($answer)) { $usable = $src }}}- if ($onPath) {- Write-Host " FAIL Get-StudioEarlyPython found nothing, yet $($onPath.Source) is on PATH." -ForegroundColor Red+ if ($usable) {+ Write-Host " FAIL Get-StudioEarlyPython found nothing, yet $usable answers the same probe." -ForegroundColor RedWrite-Host " That is a broken extraction in this file, not a host without Python." -ForegroundColor Redexit 1}@@ -161,13 +193,13 @@ try {Reset-RungState$savedTable = ${function:Get-StudioPythonProcessImageTable}function Get-StudioPythonProcessImageTable { throw "interpreter discovery failed" }- function Get-CimInstance { param($ClassName, $ErrorAction)+ function Get-StudioWmiProcessImageRows {return @([pscustomobject]@{ ProcessId = 55; ExecutablePath = "C:\w\python.exe" }) }$threw = $null; $viaWmi = $nulltry { $viaWmi = Get-StudioProcessImagePath -ProcessId 55 } catch { $threw = $_ }Check "a table probe that throws still reaches the WMI rung" ($null -eq $threw -and $viaWmi -eq "C:\w\python.exe")- function Get-CimInstance { param($ClassName, $ErrorAction) throw "WMI is unavailable" }+ function Get-StudioWmiProcessImageRows { return @() }${function:Get-StudioPythonProcessImageTable} = $savedTable# Validating the answer is part of the null-on-error contract: an access error declines.@@ -180,6 +212,24 @@ try {Check "an access error validating the answer declines instead of throwing" ($null -eq $threw -and $null -eq $answer)+ # Without the table only WMI is left, which the shell must say rather than scan silently.+ Reset-RungState+ $script:StudioProcessImageWarned = $false+ $script:RunnerOutput = ""+ $script:Warnings = @()+ function Write-StudioLine { param([string]$Line, [string]$ForegroundColor = "") $script:Warnings += $Line }+ $null = Get-StudioProcessImagePath -ProcessId 1+ $null = Get-StudioProcessImagePath -ProcessId 2+ Check "a missing process-image table is reported, once" (+ @($script:Warnings | Where-Object { $_ -match "may go unnoticed" }).Count -eq 1)+ Reset-RungState+ $script:StudioProcessImageWarned = $false+ $script:RunnerOutput = "77|C:\d\python.exe"+ $script:Warnings = @()+ $null = Get-StudioProcessImagePath -ProcessId 77+ Check "a working table says nothing" ($script:Warnings.Count -eq 0)+ function Write-StudioLine { param([string]$Line, [string]$ForegroundColor = "") }+$env:OS = "Linux"Reset-RungState$script:RunnerCalls = 0@@ -204,20 +254,16 @@ Check "the probe declares CloseHandle's argument type" ($probeText -match "CloseHandle\.argtypes")# 0x400 is refused by protected and cross-session processes; 0x1000 is not.Check "the probe asks for the limited-information right only" ($probeText -match "OpenProcess\(0x1000,")-# Both rungs must return the same string or Test-StudioProtectedPathMatch sees false differences.-$nativeInit = ($ast.FindAll({ param($n)- $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and- $n.Name -eq "Get-StudioNativeProcessImagePath" }, $true)[0]).Extent.Text-$nativeRight = if ($nativeInit -match '\$queryLimitedInformation\s*=\s*\[uint32\](0x[0-9a-fA-F]+)') { $Matches[1] } else { "" }-Check "the native rung asks for the same access right the probe does" (- $nativeRight -eq "0x1000" -and- $nativeInit -match "OpenProcess\(\s*\r?\n?\s*\`$queryLimitedInformation," -and+Check "the probe asks for PROCESS_QUERY_LIMITED_INFORMATION, as the native rung did" ($probeText -match "OpenProcess\(0x1000,")-Check "both pass flags 0, so both get the Win32 path form and not the device form" (- $nativeInit -match "QueryFullProcessImageNameW\(\s*\`$handle,\s*\[uint32\]0," -and+Check "it passes flags 0, so it gets the Win32 path form and not the device form" ($probeText -match "QueryFullProcessImageNameW\(h,0,")-Check "both size the buffer the same" (- $nativeInit -match "32768" -and $probeText -match "create_unicode_buffer\(32768\)")+Check "it sizes the buffer as the native rung did" (+ $probeText -match "create_unicode_buffer\(32768\)")+$installWhole = [System.IO.File]::ReadAllText($installPs1)+Check "no emitted process-image type remains" ($installWhole -notmatch "UnslothStudioProcessImageV1")+Check "no native process-image helper remains" (+ $installWhole -notmatch "Get-StudioNativeProcessImagePath|Initialize-StudioProcessImageNativeType")# EnumProcesses filling the buffer exactly may mean truncation; only a smaller count is complete.Check "the probe grows its buffer until the enumeration is provably complete" (diff --git a/tests/studio/test_install_rollback_lifecycle.ps1 b/tests/studio/test_install_rollback_lifecycle.ps1index ccc89e5486d..e83e701bf4f 100644--- a/tests/studio/test_install_rollback_lifecycle.ps1+++ b/tests/studio/test_install_rollback_lifecycle.ps1@@ -87,7 +87,7 @@ function Write-StudioLine { param([string]$Message, [string]$ForegroundColor) Wr# not recognized" inside whichever case happened to reach it first. The closure above# makes an install.ps1-defined callee unreachable by construction; this catches the rest,# including a helper that calls a sink this file forgot to stub.-$windowsOnlyCommands = @("Get-CimInstance")+$windowsOnlyCommands = @("Get-CimInstance", "Get-Acl")$unresolved = [System.Collections.Generic.List[string]]::new()foreach ($name in $extracted) {foreach ($call in $definitions[$name].Body.FindAll({ param($n)diff --git a/tests/studio/test_installer_av_shapes.py b/tests/studio/test_installer_av_shapes.pyindex a38fdd4aa13..f1b4d59ab63 100644--- a/tests/studio/test_installer_av_shapes.py+++ b/tests/studio/test_installer_av_shapes.py@@ -105,10 +105,43 @@ is real: a windowless PowerShell spawned by a GUI binary, running a compiler, wrcontent to %TEMP%. The same call also failed outright with CS2001 where %TEMP% was unusable (#9140),so this is a robustness fix as much as a detection one.-System.Reflection.Emit builds the identical interop stubs in memory: no compiler process, no source+System.Reflection.Emit built the identical interop stubs in memory: no compiler process, no sourcefile, no DLL, and an empty Assembly.Location. windows-no-compiler-ci.yml is the standing proof --4688 process auditing unioned with a live FileSystemWatcher over every temp root, with a positive-control, requiring zero compiler launches across a full install.+control, requiring zero compiler launches across a full install. That requirement has not moved; what+went away is the emit apparatus that replaced the compiler.++### And then no emit either++Emit fixed the compiler shape and did not fix the engine verdict. A controlled pair measured it: the+same install.ps1 with and without the apparatus, both first-seen so both got a fresh pass, same engine+build v2021.2.0+4045, same definitions 20260916, 74 engines reporting and zero timeouts on each.++| Sample | Size | Verdict |+|---|---|---|+| with the apparatus | 629,894 B | malicious, BehavesLike.PS.Suspicious.jr, 1 / 60 |+| without it | 594,150 B | undetected, 0 / 61 |++A 550 byte file containing nothing but a DefinePInvokeMethod call scans 0 / 62, and identical-length+comment filler in place of the apparatus also scans clean, so this is aggregate threshold scoring+rather than a construct match: the apparatus is the largest single contributor pushing the file over+the line, not something independently malicious-looking. Both halves of it were independently+sufficient, so partial removal bought nothing and only full removal could help.++Two things that did NOT move the verdict, both measured rather than assumed. A valid Authenticode+signature from a Microsoft issued CA: same family, same suffix, on a matched pair. And VirusTotal does+not parse the signature block on a .ps1 at all, so signer identity is not an input for these assets,+only for the .exe bundles. Signing is still worth having for AllSigned and RemoteSigned hosts, for+WDAC and Smart App Control, and as the artifact a false-positive submission asks for.++What replaced the four emitted types imports nothing. Path identity goes through a child interpreter+running pathlib.Path.resolve, which is GetFinalPathNameByHandleW on Windows, so the exact answer is+the same string the emitted CreateFileW / GetFinalPathNameByHandleW rung returned. The process image+lookup and the shell icon refresh call the same entry points through ctypes, and the NVIDIA inventory+reaches NVML and the CUDA driver API the same way. AMSI does not scan CPython, so the interop left the+scanned surface rather than moving within it. Where no interpreter can be reached, the path resolver+answers lexically and reports Exact = $false, which makes the caller take BOTH runtime locks: the+degradation fails closed.## No .vbs launcher@@ -671,8 +704,9 @@ def test_the_installer_never_runs_the_c_sharp_compiler(name: str) -> None:hits = re.findall(r"(?m)^[ \t]*Add-Type\b(?![^\r\n]*-AssemblyName).*", text)assert not hits, (f"{name} compiles C# again ({len(hits)} Add-Type call(s), first: {hits[0].strip()!r}). "- "Declare native methods with New-StudioEmittedNativeType instead, or with an inline "- "DefinePInvokeMethod block where the script is generated and cannot call it; "+ "Make the native call out of process instead (the early Python interpreter's ctypes, "+ "as the NVIDIA probe and path resolver do, or an in-box System32 tool), or with an "+ "inline DefinePInvokeMethod block where the script is generated; ""-MemberDefinition runs csc.exe just as -TypeDefinition does.")# Conditional, because "emits its native imports" only means anything for a file that HAS@@ -1130,15 +1164,15 @@ def test_the_compiler_window_is_cut_to_size_by_timecreated() -> None:)-def test_the_native_resolver_still_has_a_lexical_fallback() -> None:- """The point of the change is the acquisition, not the ladder: a host where emit fails must- degrade exactly as one that could not compile already did.+def test_the_path_resolver_still_has_a_lexical_fallback() -> None:+ """The point of the change is the acquisition, not the ladder: a host that cannot reach the+ exact rung must degrade exactly as one that could not compile already did."""text = _text("install.ps1")assert "Write-StudioFinalPathDegraded" in textassert "Get-StudioLexicalPath" in text- # Constrained Language Mode forbids defining types at all, by emit as by Add-Type.- assert '$languageMode -ne "FullLanguage"' in text+ assert "Get-StudioPythonFinalPath" in text+ assert "UNSLOTH_EARLY_PYTHON_PROBE" in text# ---------------------------------------------------------------------------@@ -1412,3 +1446,27 @@ def test_a_comment_never_points_at_a_file_that_is_not_here(name: str) -> None:f"{name} has a comment pointing at {missing}, which is not in this tree. Cite a PR number, ""or cite tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD), which travels with the repo.")++[email protected]("name", PS_SCRIPTS)+def test_no_comment_line_starts_with_a_bareword(name: str) -> None:+ path = REPO / name+ if not path.is_file():+ pytest.skip(f"{name} is not present")+ offenders = [+ f"{n}: {line.strip()}"+ for n, line in enumerate(path.read_text(encoding = "utf-8").splitlines(), 1)+ if re.match(r"^\s*[A-Za-z0-9_]+#", line)+ ]+ assert not offenders, (+ f"{name} has lines where a bareword runs into a '#'. PowerShell reads those as a command "+ f"named '<word>#', not as a comment, and the run dies there:\n " + "\n ".join(offenders)+ )+++def test_the_bareword_comment_rule_can_fire() -> None:+ """The pattern really does catch the shape, and really does leave ordinary lines alone."""+ assert re.match(r"^\s*[A-Za-z0-9_]+#", "f# An in-box tool")+ assert re.match(r"^\s*[A-Za-z0-9_]+#", " unction# something")+ assert not re.match(r"^\s*[A-Za-z0-9_]+#", "# An in-box tool")+ assert not re.match(r"^\s*[A-Za-z0-9_]+#", ' $x = "a#b"')diff --git a/tests/studio/test_native_path_resolver_degrades.ps1 b/tests/studio/test_native_path_resolver_degrades.ps1deleted file mode 100644index 984c9bd2f03..00000000000--- a/tests/studio/test_native_path_resolver_degrades.ps1+++ /dev/null@@ -1,298 +0,0 @@-# Behaviour of install.ps1's native helpers when the native side is not available,-# which is the state a blocked or locked-down machine is in.-#-# The change this guards replaced every Add-Type compile with reflection emit. The-# ladders around them did not move: an exact path when the native call works, the-# lexical path when it does not with $Exact false so the runtime lock fails closed,-# and for the process scan a native image path first, then Get-Process, then-# Win32_Process. This runs the real functions, parsed out of install.ps1, and checks-# that every rung still answers.-#-# POSIX is the harness on purpose. Two different failures live here and they are not-# the same: reflection emit SUCCEEDS on this engine, so the types build exactly as-# they do on 5.1, while kernel32 and shell32 do not resolve, so every call through-# them fails exactly as it does on a Windows host whose antivirus took the helper-# away. Between them that covers the build side and the call side. The Windows lanes-# cover the case where the call itself succeeds.--$ErrorActionPreference = "Stop"--# Refuse rather than assert nonsense. Half the checks below require the native calls to-# FAIL, which on a healthy Windows host they do not, so a Windows lane running this file-# would report a pile of failures that say nothing about the code. It was scheduled on a-# windows-latest job once; exiting here means the next time it happens the message names-# the reason instead.-if ($IsWindows -or $env:OS -eq "Windows_NT") {- Write-Host "SKIP: this file describes a host where kernel32 does not resolve, which Windows is not."- exit 0-}--$repoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)--# The parser, not tests/studio_setup_ps1/Get-FunctionSource.ps1, which counts braces-# without tokenizing and so runs past the end of any function holding a brace inside-# a string. Resolve-StudioLinkTarget has one ('Volume{'), and the naive walk swallowed-# the rest of the file. The parser is exact and is right here in the box.-$script:InstallAst = $null-function Get-InstallFunctionSource {- param([Parameter(Mandatory = $true)][string]$Path, [Parameter(Mandatory = $true)][string]$Name)- if ($null -eq $script:InstallAst) {- $errors = $null; $tokens = $null- $script:InstallAst = [System.Management.Automation.Language.Parser]::ParseFile(- $Path, [ref]$tokens, [ref]$errors)- if ($errors.Count -gt 0) { throw "$Path : $($errors[0].Message)" }- }- $found = $script:InstallAst.FindAll({- $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and- $args[0].Name -eq $Name- }, $true)- if ($found.Count -eq 0) { return $null }- return $found[0].Extent.Text-}--$failures = 0-function Check {- param([string]$Name, [bool]$Ok)- if ($Ok) { Write-Host " PASS $Name" -ForegroundColor Green }- else { Write-Host " FAIL $Name" -ForegroundColor Red; $script:failures++ }-}--$installPath = [System.IO.Path]::Combine($repoRoot, "install.ps1")-$text = Get-Content -LiteralPath $installPath -Raw--# ── Source contract ──-$setupPath = [System.IO.Path]::Combine($repoRoot, "studio", "setup.ps1")-foreach ($pair in @(@("install.ps1", $installPath), @("studio/setup.ps1", $setupPath))) {- $body = Get-Content -LiteralPath $pair[1] -Raw- # -MemberDefinition compiles as surely as -TypeDefinition does. -AssemblyName- # only loads an assembly that already exists and reaches no compiler, so it is- # not what this bans. Both files, unconditionally: "compiles no C#" is the claim- # that has to hold everywhere.- $compiles = [regex]::Matches($body, "(?m)^[ \t]*Add-Type\b(?![^\r\n]*-AssemblyName)")- Check "$($pair[0]) compiles no C# at all" ($compiles.Count -eq 0)-- # The rest only applies to a file that still declares native imports. studio/setup.ps1- # no longer does: its emit apparatus existed solely to colour a banner, and the console- # host turns out to enable virtual terminal processing before our code runs, so the- # whole thing went. Asserting "emits its imports" against a file with no imports would- # demand it grow some back, which is backwards.- if ($body -notmatch "DllImport|extern\s|DefinePInvokeMethod") {- Check "$($pair[0]) declares no native imports at all" $true- continue- }- Check "$($pair[0]) emits its native imports instead" ($body -match "DefinePInvokeMethod")- # Both spellings of "define a dynamic assembly". Windows PowerShell 5.1 on .NET- # Framework and pwsh on .NET Core do not agree about which one exists, and only- # one of them can be exercised from here: losing either branch would degrade- # every install on the other host with no error to see, since the caller caches- # the failure and falls through to the lexical path.- Check "$($pair[0]) tries the static DefineDynamicAssembly" (- $body -match "\[System\.Reflection\.Emit\.AssemblyBuilder\]::DefineDynamicAssembly")- Check "$($pair[0]) falls back to the AppDomain spelling" (- $body -match "\[AppDomain\]::CurrentDomain\.DefineDynamicAssembly")-}--# ── The resolver, run for real ──-# The whole ladder, not only its top rung: the claim under test is that a host-# without the native side still gets an answer, and gets told it is inexact.-$fns = @(- "Write-StudioLine", "Write-StudioFinalPathDegraded",- "Test-StudioCanDefineNativeTypes", "Test-StudioEmitInChildProcess", "New-StudioDynamicAssembly",- "New-StudioEmittedNativeType",- "Initialize-StudioFinalPathNativeType", "Get-StudioNativeFinalPath",- "Get-ElevationState", "Get-StudioEarlyPython", "Invoke-StudioEarlyPythonScript", "Invoke-StudioEarlyPython",- "Get-StudioPythonFinalPath",- "Resolve-StudioLinkTarget", "Get-StudioSubstTarget", "Get-StudioLexicalPath",- "Resolve-StudioFinalPathInfo",- "Initialize-StudioProcessImageNativeType", "Get-StudioNativeProcessImagePath",- # Reached only by the below-Get-Process scenario at the end of the harness.- "Get-StudioPythonProcessImageTable",- "Get-StudioProcessImagePath"-)-$src = @()-foreach ($fn in $fns) {- $found = Get-InstallFunctionSource -Path $installPath -Name $fn- Check "install.ps1 defines $fn" ($null -ne $found)- if ($found) { $src += $found }-}--if ($src.Count -eq $fns.Count) {- $harness = @"-`$ErrorActionPreference = "Stop"-function Write-StudioLine { param([string]`$Message, `$ForegroundColor) Write-Host `$Message }-`$script:StudioFinalPathNativeState = `$null-`$script:StudioFinalPathWarned = `$false-`$script:StudioNativeResolveWarned = `$false-`$script:StudioSubstMap = `$null-`$script:StudioProcessImageNativeState = `$null-`$script:StudioProcessImageTable = `$null-`$script:StudioProcessImageWarned = `$false-$($src -join "`n")--# Snapshot first. A runner is a shared machine, and anything else that wrote a-# matching name into the shared temp directory in the preceding minute would-# otherwise be counted as this emission's output and fail the run for it.-`$temp = [System.IO.Path]::GetTempPath()-`$patterns = @(".dll", ".cs", ".cmdline", ".err", ".out")-`$before = @(Get-ChildItem -LiteralPath `$temp -File -ErrorAction SilentlyContinue |- Where-Object { `$_.Extension -in `$patterns } | Select-Object -ExpandProperty FullName)--# 1. The type must define without a compiler, on this engine as on 5.1.-`$defined = Initialize-StudioFinalPathNativeType-Write-Host "EMIT_OK: `$defined"-Write-Host "TYPE_PRESENT: `$(`$null -ne ("UnslothStudioFinalPathV3" -as [type]))"--# 2. Nothing may be written to the temporary directory by defining it. Set-# difference against the snapshot above, not a recency window: what this asserts-# is that emission wrote nothing, and the clock cannot tell those apart.-`$after = @(Get-ChildItem -LiteralPath `$temp -File -ErrorAction SilentlyContinue |- Where-Object { `$_.Extension -in `$patterns } | Select-Object -ExpandProperty FullName)-`$fresh = @(`$after | Where-Object { `$before -notcontains `$_ })-Write-Host "TEMP_ARTIFACTS: `$(`$fresh.Count)"--# 3. The dynamic assembly must exist only in memory.-if (`$defined) {- `$asm = ("UnslothStudioFinalPathV3" -as [type]).Assembly- Write-Host "IN_MEMORY_ONLY: `$([string]::IsNullOrEmpty(`$asm.Location))"-}--# 4. A native call that cannot work must answer null, not throw. kernel32 is-# absent here, which is what a host missing the helper looks like.-`$threw = `$false-`$answer = "unset"-try { `$answer = Get-StudioNativeFinalPath -Path `$temp } catch { `$threw = `$true }-Write-Host "NATIVE_THREW: `$threw"-Write-Host "NATIVE_ANSWER_NULL: `$([string]::IsNullOrEmpty(`$answer))"--# 5. The other two emitted types must define with the same signatures the C# they-# replace declared. `out uint` has to arrive as a by-ref, and a void return has-# to stay void, or the call marshals wrongly on a host where it does run.-`$vt = New-StudioEmittedNativeType -TypeName "UnslothEmitterOutParamProbe" -Imports @(- @{ Name = "GetStdHandle"; Library = "kernel32.dll"; Return = [IntPtr]; Args = @([int]) },- @{ Name = "GetConsoleMode"; Library = "kernel32.dll"; Return = [bool]- Args = @([IntPtr], [uint32].MakeByRefType()) },- @{ Name = "SetConsoleMode"; Library = "kernel32.dll"; Return = [bool]- Args = @([IntPtr], [uint32]) })-`$icon = New-StudioEmittedNativeType -TypeName "UnslothShellIconRefresh" -Imports @(- @{ Name = "SHChangeNotify"; Library = "shell32.dll"; Return = [System.Void]- Args = @([int], [uint32], [string], [IntPtr]) })-Write-Host "VT_EMIT_OK: `$vt"-Write-Host "ICON_EMIT_OK: `$icon"-`$gcm = ("UnslothEmitterOutParamProbe" -as [type]).GetMethod("GetConsoleMode")-Write-Host "VT_BYREF_OUT: `$(`$gcm.GetParameters()[1].ParameterType.IsByRef)"-`$shcn = ("UnslothShellIconRefresh" -as [type]).GetMethod("SHChangeNotify")-Write-Host "ICON_RETURNS_VOID: `$(`$shcn.ReturnType -eq [System.Void])"--# 6. Defining a type twice must not corrupt the first one. Callers guard with-# -as [type], but a partial first attempt would otherwise leave a half-built-# type behind, which is what Add-Type's "already exists" error used to prevent.-`$redefineThrew = `$false-try { `$null = New-StudioEmittedNativeType -TypeName "UnslothEmitterOutParamProbe" -Imports @(- @{ Name = "GetStdHandle"; Library = "kernel32.dll"; Return = [IntPtr]; Args = @([int]) }) }-catch { `$redefineThrew = `$true }-Write-Host "REDEFINE_THREW: `$redefineThrew"-Write-Host "REDEFINE_TYPE_INTACT: `$(`$null -ne (("UnslothEmitterOutParamProbe" -as [type]).GetMethod("SetConsoleMode")))"--# 7. The other rung. Resolve-StudioFinalPathInfo is what every caller actually-# uses, and the point of the change is that it still answers when the native-# side cannot: a lexical path, and Exact false so the runtime lock fails closed.-# The Python rung sits between the two and would answer EXACTLY on any host with-# an interpreter, which is the opposite of the state being exercised here, so it-# is switched off for this probe. It has its own coverage in-# tests/studio/test_early_python_path_resolver.ps1.-`$env:UNSLOTH_EARLY_PYTHON_PROBE = "0"-`$script:StudioEarlyPythonProbed = `$false-`$script:StudioEarlyPython = `$null-`$info = `$null-`$ladderThrew = `$false-try { `$info = Resolve-StudioFinalPathInfo -Path `$temp } catch { `$ladderThrew = `$true; Write-Host "LADDER_ERROR: `$(`$_.Exception.Message)" }-Write-Host "LADDER_THREW: `$ladderThrew"-if (`$info) {- Write-Host "LADDER_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$info.Path))"- Write-Host "LADDER_EXACT: `$(`$info.Exact)"-}--# 8. The process-image ladder. Two states, and they are different states: the-# emit SUCCEEDS here even though kernel32 does not resolve, so this takes the-# native rung and must answer null without throwing, exactly as it did when the-# type came from a compile that succeeded and a call that then failed. Forcing-# the type unavailable is what exercises the Get-Process rung below it.-`$imgThrew = `$false-`$img = "unset"-try { `$img = Get-StudioProcessImagePath -ProcessId `$PID } catch { `$imgThrew = `$true }-Write-Host "PROCIMG_NATIVE_STATE: `$(Initialize-StudioProcessImageNativeType)"-Write-Host "PROCIMG_THREW: `$imgThrew"-Write-Host "PROCIMG_NATIVE_NULL: `$([string]::IsNullOrWhiteSpace(`$img))"--# The real initializer caches on type PRESENCE, so clearing the state variable-# would not un-define what step 8 just built. Override the guard instead: the rungs-# under test are the ones below it, and this is the only way to reach them once a-# type exists in the session.-function Initialize-StudioProcessImageNativeType { return `$false }-`$fallbackThrew = `$false-`$fallback = "unset"-try { `$fallback = Get-StudioProcessImagePath -ProcessId `$PID } catch { `$fallbackThrew = `$true }-Write-Host "PROCIMG_FALLBACK_THREW: `$fallbackThrew"-Write-Host "PROCIMG_FALLBACK_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$fallback))"-Write-Host "PROCIMG_WARNED: `$script:StudioProcessImageWarned"--# The rungs below Get-Process, which the check above never reaches because this shell can read-# its own .Path. A Get-Process with no answer stands in for an uninspectable process.-function Get-Process { param(`$Id, `$ErrorAction) return `$null }-`$script:StudioPythonProcessImageProbed = `$false-`$script:StudioPythonProcessImageTable = `$null-`$deepThrew = `$false-try { `$null = Get-StudioProcessImagePath -ProcessId `$PID } catch { `$deepThrew = `$true; Write-Host "DEEP_ERROR: `$(`$_.Exception.Message)" }-Write-Host "PROCIMG_DEEP_THREW: `$deepThrew"-"@- $file = Join-Path ([System.IO.Path]::GetTempPath()) ("uns_native_" + [guid]::NewGuid().ToString("N") + ".ps1")- Set-Content -LiteralPath $file -Value $harness -Encoding utf8- $exe = (Get-Command pwsh -ErrorAction SilentlyContinue).Source- if (-not $exe) { $exe = (Get-Command powershell).Source }- try {- $out = & $exe -NoProfile -File $file 2>&1 | Out-String- } finally {- Remove-Item -LiteralPath $file -ErrorAction SilentlyContinue- }-- # A parse failure in the generated harness would otherwise read as every check- # failing at once, with nothing saying why.- if ($out -notmatch "EMIT_OK") { Write-Host " ---- harness output ----"; Write-Host $out }- Check "the native type defines with no compiler" ($out -match "EMIT_OK: True")- Check "the emitted type is really there" ($out -match "TYPE_PRESENT: True")- Check "defining it writes nothing to the temporary directory" ($out -match "TEMP_ARTIFACTS: 0")- Check "the assembly exists only in memory" ($out -match "IN_MEMORY_ONLY: True")- # The whole point of the ladder: a native call that cannot work is not fatal.- Check "a native call that cannot work does not throw" ($out -match "NATIVE_THREW: False")- Check "a native call that cannot work answers null" ($out -match "NATIVE_ANSWER_NULL: True")- Check "the console thunk emits too" ($out -match "VT_EMIT_OK: True")- Check "the icon-refresh thunk emits too" ($out -match "ICON_EMIT_OK: True")- # These two are what a hand-written emit gets wrong: `out uint` silently- # becoming a by-value uint, and a void return becoming something else.- Check "GetConsoleMode's out parameter is by-ref" ($out -match "VT_BYREF_OUT: True")- Check "SHChangeNotify still returns void" ($out -match "ICON_RETURNS_VOID: True")- Check "defining a type twice does not throw" ($out -match "REDEFINE_THREW: False")- Check "and does not corrupt the first definition" ($out -match "REDEFINE_TYPE_INTACT: True")- # The rung that matters to callers, not just the one that failed.- Check "the full path ladder does not throw" ($out -match "LADDER_THREW: False")- Check "the full path ladder still answers" ($out -match "LADDER_ANSWERED: True")- Check "and reports the answer as inexact" ($out -match "LADDER_EXACT: False")- # The native rung is REACHED here: emit succeeds on this engine, kernel32 does- # not resolve, and null without an exception is what the caller skips on.- Check "the process image type emits too" ($out -match "PROCIMG_NATIVE_STATE: True")- Check "the process image ladder does not throw" ($out -match "PROCIMG_THREW: False")- Check "an unusable native process query answers null" ($out -match "PROCIMG_NATIVE_NULL: True")- # And with the type forced away, the rungs below it still answer, which is what- # keeps a host from finding no running processes and overwriting an open venv.- Check "the fallback rung does not throw" ($out -match "PROCIMG_FALLBACK_THREW: False")- # Catches a helper missing from the extraction list.- Check "the rungs below Get-Process do not throw either" ($out -match "PROCIMG_DEEP_THREW: False")- Check "the fallback rung still names our own image" ($out -match "PROCIMG_FALLBACK_ANSWERED: True")- Check "and says out loud that it is degraded" ($out -match "PROCIMG_WARNED: True")-}--if ($failures -gt 0) {- Write-Host "$failures check(s) failed" -ForegroundColor Red- exit 1-}-Write-Host "All checks passed" -ForegroundColor Greendiff --git a/tests/studio/test_nvidia_library_inventory.ps1 b/tests/studio/test_nvidia_library_inventory.ps1index b2f09880350..62355b6f125 100644--- a/tests/studio/test_nvidia_library_inventory.ps1+++ b/tests/studio/test_nvidia_library_inventory.ps1@@ -63,21 +63,36 @@ $setupPs1 = Join-Path $root "studio\setup.ps1"Write-Host ""Write-Host "=== shared inventory helper ==="-$blockNames = @("Get-NvidiaNvmlLibraryPath", "Get-NvidiaLibraryProbeType", "Read-NvidiaLibraryRaw", "Get-NvidiaLibraryInventory", "Get-NvidiaSystem32Dir")+$blockNames = @("New-StudioChildScriptDirectory", "Get-NvidiaNvmlLibraryPath", "Read-NvidiaLibraryRawViaPython", "Read-NvidiaLibraryRaw", "Get-NvidiaLibraryInventory", "Test-StudioChildScriptDirectoryElevated", "Get-NvidiaSystem32Dir")$installParts = @(Get-HelperSources $installPs1 $blockNames)$setupParts = @(Get-HelperSources $setupPs1 $blockNames)-$installBlock = $installParts[3]-$setupBlock = $setupParts[3]-$setupPath = $setupParts[0]-$readBlock = $setupParts[2]+$installBlock = $installParts[4]+$setupBlock = $setupParts[4]+$setupPath = $setupParts[1]+$readBlock = $setupParts[3]+$viaPythonBlock = $setupParts[2]+$probeBody = ""+$probeAt = $viaPythonBlock.IndexOf("`$probeSource = @'")+if ($probeAt -ge 0) {+ $bodyStart = $viaPythonBlock.IndexOf("`n", $probeAt) + 1+ $bodyEnd = $viaPythonBlock.IndexOf("`n'@", $bodyStart)+ if ($bodyEnd -gt $bodyStart) { $probeBody = $viaPythonBlock.Substring($bodyStart, $bodyEnd - $bodyStart) }+}+Check "the embedded probe was found" ($probeBody.Length -gt 200)# install.ps1 nests its helpers one level deeper; compare the two copies 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]))}# The installer must not spawn a C# compiler (windows-no-compiler-ci): the methods are emitted.-Check "the inventory compiles nothing" ((($setupParts -join "`n") -notmatch 'Add-Type') -and ($setupParts[1] -match 'New-StudioEmittedNativeType'))-Check "the emission is gated on the native-type capability" ($setupParts[1] -match 'if \(-not \(Test-StudioCanDefineNativeTypes\)\) \{ return \$null \}')+$joined = ($setupParts -join "`n")+Check "the inventory compiles nothing" ($joined -notmatch 'Add-Type')+Check "the inventory emits nothing" ($joined -notmatch 'New-StudioEmittedNativeType|DefinePInvokeMethod|Reflection\.Emit')+Check "the apparatus it used to need is gone from the file entirely" (+ ([System.IO.File]::ReadAllText($setupPs1) -notmatch 'New-StudioEmittedNativeType|Test-StudioCanDefineNativeTypes'))+Check "the reader goes through the Python rung" ($setupParts[2] -match 'Read-NvidiaLibraryRawViaPython')+Check "nvcuda.dll is handed to the probe by its System32 path" ($viaPythonBlock -match '\$cudaHint = if \(\$windows\) \{ Join-Path \(Get-NvidiaSystem32Dir\) "nvcuda\.dll" \}')+Invoke-Expression $setupParts[6]Invoke-Expression $setupPathInvoke-Expression $setupParts[4]$pathRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-nvml-" + [guid]::NewGuid().ToString("N"))@@ -96,17 +111,14 @@ try {$env:SystemRoot = $savedRoot; $env:ProgramFiles = $savedPfRemove-Item -LiteralPath $pathRoot -Recurse -Force -ErrorAction SilentlyContinue}-Check "nvcuda.dll is bound by its System32 path" ($setupParts[1] -match '\$cuda = if \(\$windows\) \{ Join-Path \(Get-NvidiaSystem32Dir\) "nvcuda\.dll" \}')Check "a failed driver-version read is not an inventory" (- $readBlock -match 'nvmlSystemGetCudaDriverVersion_v2\(\[ref\]\$ver\) -ne 0' -and- $readBlock -match 'cuDriverGetVersion\(\[ref\]\$ver\) -ne 0')+ $probeBody -match 'nvmlSystemGetCudaDriverVersion_v2\(ctypes\.byref\(packed\)\) != 0' -and+ $probeBody -match 'cuDriverGetVersion\(ctypes\.byref\(packed\)\) != 0')# The real libraries, in a child so the fake type below can own this session.$helperFile = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-inventory-" + [System.IO.Path]::GetRandomFileName() + ".ps1")-$emitters = @(Get-HelperSources $setupPs1 @("New-StudioDynamicAssembly", "New-StudioEmittedNativeType"))-# The capability gate is a Windows policy probe; the child stands in for it and answers yes.-$gateStub = 'function Test-StudioCanDefineNativeTypes { return $true }'-$helperBody = (@($gateStub) + $emitters + $setupParts) -join "`n"+$pythonStub = 'function Get-NvidiaProbePythonExe { $c = Get-Command python3 -ErrorAction SilentlyContinue; if (-not $c) { $c = Get-Command python -ErrorAction SilentlyContinue }; if ($c) { return $c.Source } return "" }'+$helperBody = (@($pythonStub) + $setupParts) -join "`n"Set-Content -LiteralPath $helperFile -Value ($helperBody + "`n`$inv = Get-NvidiaLibraryInventory`nif (`$inv) { `$inv | ConvertTo-Json -Compress } else { 'null' }")$pwshExe = (Get-Process -Id $PID).Path$realJson = & $pwshExe -NoProfile -File $helperFile 2>&1 | Select-Object -Last 1@@ -141,8 +153,29 @@ Check "an empty probe is no inventory" ($null -eq (Probe-Raw ""))Check "a zero driver version is no inventory" ($null -eq (Probe-Raw "nvml;0;0;8.9"))Check "no capabilities is no inventory" ($null -eq (Probe-Raw "nvml;13;0;"))Check "an unreadable capability voids the inventory" ($null -eq (Probe-Raw "nvml;13;0;N/A,8.9"))+$script:Notices = @()+function Write-StudioLine { param([string]$Line, [string]$ForegroundColor = "") $script:Notices += $Line }+$null = Probe-Raw ""+Check "an empty probe without an opt-out prints nothing" ($script:Notices.Count -eq 0)+foreach ($optOut in @("UNSLOTH_NVIDIA_PYTHON_PROBE", "UNSLOTH_EARLY_PYTHON_PROBE")) {+ $saved = [Environment]::GetEnvironmentVariable($optOut)+ try {+ [Environment]::SetEnvironmentVariable($optOut, "0")+ $script:Notices = @()+ $null = Probe-Raw ""+ Check "$optOut=0 says the libraries were not read and how to choose a wheel" (+ $script:Notices.Count -eq 1 -and $script:Notices[0] -match 'UNSLOTH_TORCH_INDEX_FAMILY')+ $script:Notices = @()+ $null = Probe-Raw "nvml;13;1;8.9"+ Check "$optOut=0 with an answer prints nothing" ($script:Notices.Count -eq 0)+ } finally { [Environment]::SetEnvironmentVariable($optOut, $saved) }+}+function Write-StudioLine { param([string]$Line, [string]$ForegroundColor = "") }+$null = Probe-Raw ""Check "one unreadable GPU voids the reader's source too" ($readBlock -notmatch '\bcontinue\b')-Check "the CUDA driver API is read with the mask lifted" ($readBlock -match 'Remove-Item Env:CUDA_VISIBLE_DEVICES' -and $readBlock -match '\$env:CUDA_VISIBLE_DEVICES = \$saved')+Check "the CUDA driver API is read with the mask lifted" (+ $probeBody -match 'os\.environ\.pop\("CUDA_VISIBLE_DEVICES", None\)' -and+ $probeBody -match 'os\.environ\["CUDA_VISIBLE_DEVICES"\] = saved')Check "setup.ps1 initialises the cache with its other script state" ((Get-Content -LiteralPath $setupPs1 -Raw) -match '(?m)^\$script:NvidiaLibraryInventoryProbed = \$false')Check "the answer is cached" ($null -eq (Get-NvidiaLibraryInventory))diff --git a/tests/studio/test_nvidia_python_probe_parity.ps1 b/tests/studio/test_nvidia_python_probe_parity.ps1index c8a3146da20..09eb278afda 100644--- a/tests/studio/test_nvidia_python_probe_parity.ps1+++ b/tests/studio/test_nvidia_python_probe_parity.ps1@@ -2,15 +2,11 @@# 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 Python rung: CPython's ctypes makes the same NVML and CUDA driver calls-# the emitted type makes, so a host that cannot emit a type (Constrained Language Mode, WDAC,-# Dynamic Code Security) still gets the CUDA version AND the per-device compute capabilities.-# Capabilities are the part WMI cannot supply: they feed $CudaArch into-# -DCMAKE_CUDA_ARCHITECTURES for the llama.cpp source build (#5854) and the pre-Turing cap.-#-# This file exists to stop three kinds of drift:-# 1. the two copies of the shared region drifting apart,-# 2. the embedded probe drifting from studio/nvidia_probe.py, which makes the same calls,-# 3. the Python rung being promoted ahead of the emitted one, or spending a second budget.+# the emitted type made, so a host that cannot emit a type (Constrained Language Mode, WDAC,+# Dynamic Code Security) still gets the CUDA version AND the per-device compute capabilities,+# which feed $CudaArch into -DCMAKE_CUDA_ARCHITECTURES (#5854) and the pre-Turing cap.+# Guards three kinds of drift: the two copies of the shared region, the embedded probe versus+# studio/nvidia_probe.py, and the rung's budget and retry shape.# Run: pwsh -NoProfile -File tests/studio/test_nvidia_python_probe_parity.ps1$ErrorActionPreference = "Stop"@@ -34,19 +30,21 @@ function Get-HelperSources($path, $names) {if ($fn.Count -lt 1) { throw "expected $name in $path, found none" }$out += $fn[0].Extent.Text}- # A single-element return unrolls to a bare string, and [0] would then be its first- # character. Callers wrap in @(), and this comment is why.+ # 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"-# The embedded Python is the text between @' and '@ inside Read-NvidiaLibraryRawViaPython.-# Both files carry other here-strings, so anchor on the function rather than the first match.-# The variable is $probeSource, not $probe: install.ps1 and setup.ps1 each already carry a-# $probe here-string for the emit probe, and reusing the name made an older suite read this one.+# The embedded Python is the $probeSource here-string inside Read-NvidiaLibraryRawViaPython. Both+# files carry other here-strings (including a $probe one), so anchor on the function and the name.function Get-EmbeddedProbe($path) {$text = [System.IO.File]::ReadAllText($path)$at = $text.IndexOf("function Read-NvidiaLibraryRawViaPython")@@ -63,12 +61,11 @@ Write-Host ""Write-Host "=== the two copies agree ==="$blockNames = @(- "Get-NvidiaNvmlLibraryPath",- "Get-NvidiaLibraryProbeType",- "Read-NvidiaLibraryRawViaPython",- "Read-NvidiaLibraryRaw",- "Get-NvidiaLibraryInventory",- "Get-NvidiaSystem32Dir"+ "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)@@ -81,8 +78,7 @@ for ($k = 0; $k -lt $blockNames.Count; $k++) {$installProbe = Get-EmbeddedProbe $installPs1$setupProbe = Get-EmbeddedProbe $setupPs1-# Stripping indentation would hide a real defect here: leading whitespace IS the Python syntax,-# so the two copies must match byte for byte, not merely after a TrimStart.+# Leading whitespace IS the Python syntax, so the two 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)@@ -91,36 +87,24 @@ Write-Host ""Write-Host "=== the embedded probe matches studio/nvidia_probe.py ==="$referenceProbe = [System.IO.File]::ReadAllText($probePy)--# Every native symbol the emitted type imports, and the two attribute numbers. If the embedded-# probe and nvidia_probe.py ever disagree on one of these, they are probing different things.-$symbols = @(+foreach ($symbol in @("nvmlInit_v2", "nvmlShutdown", "nvmlSystemGetCudaDriverVersion_v2","nvmlDeviceGetCount_v2", "nvmlDeviceGetHandleByIndex_v2", "nvmlDeviceGetCudaComputeCapability","cuInit", "cuDriverGetVersion", "cuDeviceGetCount", "cuDeviceGet", "cuDeviceGetAttribute"-)-foreach ($symbol in $symbols) {+)) {Check "both the embedded probe and nvidia_probe.py call $symbol" `(($installProbe -match [regex]::Escape($symbol)) -and ($referenceProbe -match [regex]::Escape($symbol)))- Check "the emitted type imports $symbol too, so all three agree" `- ($installParts[1] -match [regex]::Escape($symbol))}-# CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR / _MINOR. A wrong number here returns a-# plausible integer rather than an error, which is exactly the drift a test has to catch.+# CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR / _MINOR: a wrong number 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,'))--# The packed CUDA version arithmetic, shared by all three implementations.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 emitted rung unpacks the version identically" `- (($installParts[3] -match 'Floor\(\$ver / 1000\)') -and ($installParts[3] -match 'Floor\(\(\$ver % 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)'))@@ -128,47 +112,39 @@ Write-Host ""Write-Host "=== the rung stays underneath the emitted one ==="$rawBlock = & $strip $setupParts[3]-Check "the emitted rung is attempted first" `- ($rawBlock.IndexOf("Get-NvidiaLibraryProbeType") -lt $rawBlock.IndexOf("Read-NvidiaLibraryRawViaPython"))-Check "Python is only reached when the emitted rung returned nothing" `- ($rawBlock -match 'if \(\$native\) \{ return \$native \}')-# One budget, not two: a driver that wedges both emitted readers must not then buy a further-# child process. The budget is one per-reader bound for NVML plus one for CUDA.-Check "both rungs share one deadline" ($rawBlock -match '\$deadline = \(Get-Date\)\.AddMilliseconds\(\$TimeoutMs \* 2\)')-Check "each emitted reader has its own deadline" `- (($rawBlock -match 'foreach \(\$which in @\("nvml", "cuda"\)\)') -and ($rawBlock -match 'WaitOne\(\$TimeoutMs\)'))-Check "the per-reader bound defaults to 30s" ($rawBlock -match 'param\(\[int\]\$TimeoutMs = 30000\)')-# The Python rung spends what the emitted rung left, at most one per-reader bound per child: one-# child reads NVML then CUDA, and only a child killed at its bound earns a second, CUDA-only child-# with what is still left. The behaviour is driven below; these pin the shape.-Check "Python gets only what the emitted rung left, capped at one bound" `- (($rawBlock -match '\$childMs = \$remainingMs - 2000; if \(\$childMs -gt \$TimeoutMs\) \{ \$childMs = \$TimeoutMs \}') -and- ($rawBlock -match '\$raw = Read-NvidiaLibraryRawViaPython -TimeoutMs \$childMs\b'))-Check "the reader itself avoids [math], which CLM refuses" ($rawBlock -notmatch '\[math\]::Min')+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 "an exhausted budget skips the child process entirely" ($rawBlock -match 'if \(\$remainingMs -lt 3000\) \{ return "" \}')+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 banned constructs are NAMED in this rung's own comments, explaining why they are absent.-# Matching the raw text would fail on the explanation rather than on the code, so the CLM rows-# below run against comment-free source. (Found the honest way: they failed on the comments.)+# The rung's own comments NAME the banned constructs, so these rows 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')-# Constrained Language Mode is one of the two policies that make the emitted rung decline, so a-# launcher this rung cannot use on a CLM host would be absent from the population it exists for.+# Constrained Language Mode 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\]::')-# Constrained Language Mode permits property reads only on its documented allowed-type list, and-# System.Diagnostics.Process and System.IO.FileInfo are on neither. Reading .Id, .HasExited,-# .ExitCode or a path off a FileInfo throws on precisely the hosts where the emitted rung already-# declined, which is the whole population this rung serves. Handing the object to a cmdlet keeps-# the access inside compiled code. This is not hypothetical: the sibling early-python launcher-# shipped with exactly these reads and failed three checks on windows-latest under 5.1.+# CLM allows property reads only on its allowed-type list, which excludes Process and FileInfo; the+# sibling early-python launcher shipped with these reads and failed on windows-latest under 5.1.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)@@ -178,9 +154,7 @@ Check "the launcher kills by object too" ($pyCode -match 'Stop-Process -InputObjCheck "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')-# "[ref] - Casting an object to type [ref] ... is not permitted."Check "the launcher casts nothing to [ref]" ($pyCode -notmatch '\[ref\]\s*\$')-# The stripping must not be so eager that it deletes the launcher itself.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'))@@ -190,121 +164,193 @@ foreach ($file in @($installPs1, $setupPs1)) {}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')-# Windows PowerShell 5.1 appends each native argument verbatim, so anything with a space in it-# splits and the child silently runs something else. Nothing with a space may reach argv.-Check "only space-free arguments reach the command line" ($pyCode -match 'ArgumentList @\("-I", "-S", "-"\)')+# 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 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, including a MISS. On a fresh host it is first called by the-# install-lock path, finds no Python, and caches $null for the rest of the run. The inventory is-# not read until thousands of lines later, by which point this install has created a managed-# interpreter and a venv. Asking only the cache would decline on exactly the fresh install where-# nvidia-smi is also most likely absent, and the host would take CPU wheels with a working card.-$hookSrc = @(Get-HelperSources $installPs1 @("Get-NvidiaProbePythonExe"))[0]-Invoke-Expression $hookSrc-$script:EarlyCalled = $false+# Get-StudioEarlyPython memoises a MISS from the install-lock path, long before this run creates+# its venv, so asking only the cache would decline on exactly the fresh install that needs it.+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"- $fakeManaged = Join-Path $hookDir "managed-python"[System.IO.File]::WriteAllText($fakeVenv, "")- [System.IO.File]::WriteAllText($fakeManaged, "")- $VenvPython = $fakeVenv; $ManagedPythonPath = $fakeManaged+ $VenvPython = $fakeVenv$script:EarlyCalled = $falseCheck "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)- # Before the venv is built but after the managed interpreter is installed.$VenvPython = Join-Path $hookDir "not-created-yet"$script:EarlyCalled = $false- Check "the managed interpreter is next" ((Get-NvidiaProbePythonExe) -eq $fakeManaged)- Check "and it too skips the ladder" ($script:EarlyCalled -eq $false)-- # Neither present: the ladder is the fallback, which is the pre-existing behaviour.- $VenvPython = Join-Path $hookDir "nope-1"; $ManagedPythonPath = Join-Path $hookDir "nope-2"- $script:EarlyCalled = $false- Check "with neither on disk the ladder still answers" ((Get-NvidiaProbePythonExe) -eq "/early/python3")+ 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)- # A path recorded but never created must not be handed out: Test-Path is the discriminator,- # not whether the variable happens to be set.- $VenvPython = $null; $ManagedPythonPath = $null- Check "unset variables fall through rather than returning empty" (- (Get-NvidiaProbePythonExe) -eq "/early/python3")+ $VenvPython = $null+ Check "an unset variable falls through rather than returning empty" ((Get-NvidiaProbePythonExe) -eq "/early/python3")- $saved = $env:UNSLOTH_EARLY_PYTHON_PROBE- try {- $env:UNSLOTH_EARLY_PYTHON_PROBE = "0"- $VenvPython = $fakeVenv- Check "the kill switch still wins over the venv interpreter" ((Get-NvidiaProbePythonExe) -eq "")- } finally {- if ($null -eq $saved) { Remove-Item Env:UNSLOTH_EARLY_PYTHON_PROBE -ErrorAction SilentlyContinue }- else { $env:UNSLOTH_EARLY_PYTHON_PROBE = $saved }- }+ # Only ever 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 $savedEnvRemove-Item -LiteralPath $hookDir -Recurse -Force -ErrorAction SilentlyContinue}-# The ordering that makes the above reachable at all: both variables are assigned before the-# first Get-NvidiaLibraryInventory call, or the hook would read $null however it is written.$installText = [System.IO.File]::ReadAllText($installPs1)$venvAt = $installText.IndexOf('$VenvPython = Join-Path $VenvDir')-$managedAt = $installText.IndexOf('$ManagedPythonPath = (Resolve-Path')$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)-Check "the managed interpreter is named before it too" (- $managedAt -gt 0 -and $firstInventoryAt -gt $managedAt)+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 ==="-# PowerShell's / is floating point and [int] rounds to NEAREST, so the C "+999" ceiling idiom-# turns 10000ms into 11s. Run the shipped expression rather than a retyped copy of it.+# / is floating point and [int] rounds to nearest, so "+999" turns 10000ms into 11s. Run the shipped lines.$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 @(- @{ Ms = 10000; Want = 10 }, @{ Ms = 10001; Want = 11 }, @{ Ms = 9999; Want = 10 },- @{ Ms = 1; Want = 1 }, @{ Ms = 0; Want = 1 }, @{ Ms = 2500; Want = 3 }, @{ Ms = 20000; Want = 20 }-)) {- $TimeoutMs = $row.Ms- $got = & $ceilExpr- Check "$($row.Ms) ms is $($row.Want) s" ($got -eq $row.Want)+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 ==="-Invoke-Expression ($setupParts[0]) # Get-NvidiaNvmlLibraryPath-Invoke-Expression ($setupParts[5]) # Get-NvidiaSystem32Dir: Windows names the driver libraries under it-Invoke-Expression ($setupParts[2]) # Read-NvidiaLibraryRawViaPython-Invoke-Expression ($setupParts[3]) # Read-NvidiaLibraryRaw-Invoke-Expression ($setupParts[4]) # Get-NvidiaLibraryInventory-# The emitted rung declines, which is the whole point: this is what a Constrained Language Mode-# or WDAC host sees, and the capabilities below are recovered with nothing emitted.+foreach ($part in $setupParts) { Invoke-Expression $part }+# The emitted rung declines, as on a Constrained Language Mode or WDAC host.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')),+ # Accepted on the path it HANDS BACK: 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"+ # The tools are reached by their System32 path, so the label is stubbed at that call: the real+ # icacls cannot raise a label from a standard account, which would skip the check under test.+ 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 has to be "elevated", not "not elevated". It is looked up in System32,+# never on PATH, so the lookup itself is what goes missing here.+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 = $nullforeach ($name in @("python3", "python")) {$cmd = Get-Command $name -ErrorAction SilentlyContinue@@ -315,15 +361,11 @@ if (-not $python) {Write-Host " SKIP no Python on this runner; the behavioural rows need one"} else {$script:PythonExe = $python- $saved = $env:UNSLOTH_NVIDIA_PYTHON_PROBE+ $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 {- if ($null -eq $saved) { Remove-Item Env:UNSLOTH_NVIDIA_PYTHON_PROBE -ErrorAction SilentlyContinue }- else { $env:UNSLOTH_NVIDIA_PYTHON_PROBE = $saved }- }+ Check "UNSLOTH_NVIDIA_PYTHON_PROBE=0 declines before spawning anything" ((Read-NvidiaLibraryRawViaPython -TimeoutMs 5000) -eq "")+ } finally { Restore-Env $savedEnv }$answer = Read-NvidiaLibraryRawViaPython -TimeoutMs 30000# A runner without an NVIDIA driver is the common case and must answer "" rather than throw.@@ -331,22 +373,17 @@ if (-not $python) {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+)*$')- # Through the real Read-NvidiaLibraryRaw, with the emitted rung declining. Every row- # below is guarded on a non-null inventory: $null -eq $null would pass them all.+ 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 30Check "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 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);"))+ Check "the inventory names the source the raw answer named" ($null -ne $inventory -and $answer.StartsWith("$($inventory.Source);"))}- # The launcher must not leave the interpreter or its scratch files behind.$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)@@ -355,15 +392,12 @@ if (-not $python) {Write-Host ""Write-Host "=== a hung NVML earns one CUDA-only child, and nothing else does ==="-# The embedded probe honours the switch, so the retry really skips NVML.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" `- (($pyBlock -match 'if \(\$SkipNvml\) \{ \$env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML = "1" \}') -and- ($pyBlock -match 'finally \{[\s\S]*Remove-Item Env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML'))+ (($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'))-# Stubbed launcher: the real Read-NvidiaLibraryRaw (emitted rung declining, as under CLM/WDAC)-# resolves the stub through dynamic scope. A TIMEOUT outcome may also burn some of the budget.function Invoke-RawWithStub($outcomes, [int]$TimeoutMs = 30000, [int]$TimeoutSleepMs = 0) {$script:StubCalls = @()$script:StubOutcomes = [System.Collections.ArrayList]@($outcomes)@@ -387,16 +421,17 @@ $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: each child is capped at the 30s per-reader bound" `- ($script:StubCalls.Count -eq 2 -and $script:StubCalls[0].TimeoutMs -eq 30000 -and- $script:StubCalls[1].TimeoutMs -le 30000 -and $script:StubCalls[1].TimeoutMs -ge 25000)+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")-$got = Invoke-RawWithStub @("")-Check "stub: an empty answer spawns no second child" ($script:StubCalls.Count -eq 1 -and $got -eq "")-$got = Invoke-RawWithStub @("nvml;13;0;12.0")-Check "stub: an answer spawns no second child" ($script:StubCalls.Count -eq 1 -and $got -eq "nvml;13;0;12.0")-$got = Invoke-RawWithStub @("TIMEOUT", "TIMEOUT")-Check "stub: a CUDA-only child that also hangs ends the search" ($script:StubCalls.Count -eq 2 -and $got -eq "")+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, leaving under 3 s.$got = Invoke-RawWithStub @("TIMEOUT", "cuda;12;8;8.9") -TimeoutMs 1500 -TimeoutSleepMs 1200Check "stub: no CUDA-only child when under 3 s of the shared budget remain" ($script:StubCalls.Count -eq 1 -and $got -eq "")@@ -413,8 +448,8 @@ Check "stub: the CUDA-only child gets what is left of the shared budget, not a f$script:StubCalls[1].TimeoutMs -lt 5000 -and $script:StubCalls[1].TimeoutMs -ge 2000 -and $got -eq "cuda;12;8;8.9")# End to end through the real launcher, with a fake interpreter that logs the switch it sees and-# hangs like a wedged NVML unless the switch is set. POSIX sh only: a .cmd stand-in would leave its-# sleeping child holding the output file on Windows.+# hangs like a wedged NVML unless it is set. 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 {@@ -424,6 +459,11 @@ if ($IsWindows -or $env:OS -eq "Windows_NT") {$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'@@ -432,45 +472,125 @@ if ($IsWindows -or $env:OS -eq "Windows_NT") {) -join "`n"[System.IO.File]::WriteAllText($fakePy, "$fakeBody`n")& chmod +x $fakePy+ # Runs $Body in fake-interpreter $Mode with a fresh log; -Env overrides are restored after, and+ # -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- $savedSkipEnv = $env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML+ $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# A value already in this shell must reach no first child and must survive the calls.$env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML = "caller"+ $raw = { Read-NvidiaLibraryRaw -TimeoutMs 5000 }+ $viaPython = { Read-NvidiaLibraryRawViaPython -TimeoutMs 5000 }- # Shared deadline 2 x 5000 ms: the first child is killed at 5 s and reaped, leaving the retry- # its share less the 2 s kept back for reaping it.- $env:UNSLOTH_FAKE_PY_MODE = "hang"- Remove-Item -LiteralPath $fakeLog -ErrorAction SilentlyContinue- $got = Read-NvidiaLibraryRaw -TimeoutMs 5000- $seen = @(Get-Content -LiteralPath $fakeLog -ErrorAction SilentlyContinue)+ $seen = Invoke-Fake "hang" $rawCheck "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" ($got -eq "cuda;12;8;8.9")+ 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")- $env:UNSLOTH_FAKE_PY_MODE = "empty"- Remove-Item -LiteralPath $fakeLog -ErrorAction SilentlyContinue- $got = Read-NvidiaLibraryRaw -TimeoutMs 3000- $seen = @(Get-Content -LiteralPath $fakeLog -ErrorAction SilentlyContinue)- Check "live: a first child that answers empty spawns no second" ($seen.Count -eq 1 -and $got -eq "")+ $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- $env:UNSLOTH_FAKE_PY_MODE = "hang"- $null = Read-NvidiaLibraryRaw -TimeoutMs 5000+ $null = Invoke-Fake "hang" $rawCheck "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, checked last.+ $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)++ # TEMP and TMP beneath a regular file, as on a host whose temp directory is broken.+ $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)++ # TEMP and TMP on a drive that does not exist: Join-Path itself throws, in the real directory helper too.+ $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- if ($null -eq $savedSkipEnv) { Remove-Item Env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML -ErrorAction SilentlyContinue }- else { $env:UNSLOTH_NVIDIA_PROBE_SKIP_NVML = $savedSkipEnv }- Remove-Item Env:UNSLOTH_FAKE_PY_LOG, Env:UNSLOTH_FAKE_PY_MODE -ErrorAction SilentlyContinue+ Restore-Env $savedEnvRemove-Item -LiteralPath $fakeDir -Recurse -Force -ErrorAction SilentlyContinue}}+# Constrained Language Mode refuses ProcessStartInfo, so the system-tool wrapper has a job branch:+# without it the directory label and the elevation read both fail, and every probe declines.+# A host with no enforced policy refuses Start-Job from a hand-constrained session, so a trusted+# wrapper starts the job and constrains its body instead, 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"diff --git a/tests/studio/test_path_resolver_degrades.ps1 b/tests/studio/test_path_resolver_degrades.ps1new file mode 100644index 00000000000..9ed2da8cc91--- /dev/null+++ b/tests/studio/test_path_resolver_degrades.ps1@@ -0,0 +1,254 @@++$ErrorActionPreference = "Stop"++if ($IsWindows -or $env:OS -eq "Windows_NT") {+ Write-Host "SKIP: this file describes a host where the lower rungs of the ladder cannot answer, which Windows is not."+ exit 0+}++$repoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)+$installPath = [System.IO.Path]::Combine($repoRoot, "install.ps1")+$setupPath = [System.IO.Path]::Combine($repoRoot, "studio", "setup.ps1")++$errors = $null; $tokens = $null+$script:InstallAst = [System.Management.Automation.Language.Parser]::ParseFile($installPath, [ref]$tokens, [ref]$errors)+if ($errors.Count -gt 0) { throw "$installPath : $($errors[0].Message)" }+function Get-InstallFunctionSource([string]$Name) {+ $found = $script:InstallAst.FindAll({+ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $args[0].Name -eq $Name+ }, $true)+ if ($found.Count -eq 0) { return $null }+ return $found[0].Extent.Text+}++$failures = 0+function Check {+ param([string]$Name, [bool]$Ok)+ if ($Ok) { Write-Host " PASS $Name" -ForegroundColor Green }+ else { Write-Host " FAIL $Name" -ForegroundColor Red; $script:failures++ }+}++$bannedConstructs = @(+ @("compiles no C# at all", "(?m)^[ \t]*Add-Type\b(?![^\r\n]*-AssemblyName)"),+ @("emits no P/Invoke stubs", "DefinePInvokeMethod"),+ @("defines no dynamic assembly", "DefineDynamicAssembly"),+ @("declares no native import", "DllImport")+)+$bannedTypeNames = @("UnslothStudioFinalPathV3", "UnslothStudioProcessImageV1", "UnslothShellIconRefresh", "UnslothStudioConsoleModeV1")+foreach ($pair in @(@("install.ps1", $installPath), @("studio/setup.ps1", $setupPath))) {+ $body = Get-Content -LiteralPath $pair[1] -Raw+ foreach ($ban in $bannedConstructs) { Check "$($pair[0]) $($ban[0])" ([regex]::Matches($body, $ban[1]).Count -eq 0) }+ foreach ($typeName in $bannedTypeNames) {+ Check "$($pair[0]) no longer mentions $typeName" ($body -notmatch [regex]::Escape($typeName))+ }+}++$banSelfTest = @'+Add-Type -TypeDefinition "public class X {}"+$null = [AppDomain]::CurrentDomain.DefineDynamicAssembly($name, "Run")+$null = $builder.DefinePInvokeMethod("CreateFileW", "kernel32.dll")+[DllImport("kernel32.dll")]+class UnslothStudioFinalPathV3 {}+'@+foreach ($ban in $bannedConstructs) {+ Check "the '$($ban[0])' rule can fire at all" ([regex]::Matches($banSelfTest, $ban[1]).Count -gt 0)+}+Check "the deleted-type rule can fire at all" ($banSelfTest -match [regex]::Escape($bannedTypeNames[0]))++$fns = @(+ "Write-StudioFinalPathDegraded", "Resolve-StudioLinkTarget", "Get-StudioSubstTarget", "Get-StudioLexicalPath",+ "Get-StudioEarlyPython", "Invoke-StudioEarlyPythonScript", "Invoke-StudioEarlyPython", "Get-StudioPythonFinalPath",+ "Resolve-StudioFinalPathInfo", "Get-StudioPythonProcessImageTable", "Get-StudioWmiProcessImageRows", "Get-StudioProcessImagePath"+)+$src = @()+foreach ($fn in $fns) {+ $found = Get-InstallFunctionSource $fn+ Check "install.ps1 defines $fn" ($null -ne $found)+ if ($found) { $src += $found }+}++if ($src.Count -eq $fns.Count) {+ # Each step prints TAG_THREW plus TAG_* facts; the parent matches them below.+ $harness = @"+`$ErrorActionPreference = "Stop"+function Write-StudioLine { param([string]`$Message, `$ForegroundColor) Write-Host "LINE: `$Message" }+`$script:StudioFinalPathWarned = `$false+`$script:StudioSubstMap = `$null+`$script:StudioEarlyPythonProbed = `$false+`$script:StudioEarlyPython = `$null+`$script:StudioPythonProcessImageProbed = `$false+`$script:StudioPythonProcessImageTable = `$null+`$script:StudioProcessImageTable = `$null+$($src -join "`n")+function Try-Step([string]`$Tag, [scriptblock]`$Body) {+ `$r = `$null; `$threw = `$false+ try { `$r = & `$Body } catch { `$threw = `$true; Write-Host "`${Tag}_ERROR: `$(`$_.Exception.Message)" }+ Write-Host "`${Tag}_THREW: `$threw"+ return `$r+}+`$temp = [System.IO.Path]::GetTempPath().TrimEnd('/', '\')++# 1. The interpreter rung, which this host CAN reach: pathlib resolve is the same system call the+# deleted native rung made, so Exact = `$true here is earned rather than assumed.+`$py = Try-Step PY { Get-StudioPythonFinalPath -Path `$temp }+Write-Host "PY_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$py))"+Write-Host "PY_ABSOLUTE: `$(if (`$py) { [bool](Split-Path -IsAbsolute `$py) } else { `$false })"++# 2. The full ladder with an interpreter: exact, and no degradation warning.+`$exactInfo = Try-Step EXACT { Resolve-StudioFinalPathInfo -Path `$temp }+if (`$exactInfo) {+ Write-Host "EXACT_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$exactInfo.Path))"+ Write-Host "EXACT_FLAG: `$(`$exactInfo.Exact)"+}+Write-Host "EXACT_WARNED: `$script:StudioFinalPathWarned"++# 3. A not-yet-created tail (`$StudioHome before it exists): the prefix resolves exactly, the rest is appended.+`$missing = Join-Path `$temp ("uns_missing_" + [guid]::NewGuid().ToString("N"))+`$missingInfo = Try-Step MISSING { Resolve-StudioFinalPathInfo -Path `$missing }+if (`$missingInfo) {+ Write-Host "MISSING_KEEPS_LEAF: `$(`$missingInfo.Path.EndsWith((Split-Path -Leaf `$missing)))"+ Write-Host "MISSING_EXACT: `$(`$missingInfo.Exact)"+}++# 4. The Python-less host, via the kill switch. The latch and the per-path cache are cleared too,+# or step 1's answer is replayed and this checks nothing.+`$env:UNSLOTH_EARLY_PYTHON_PROBE = "0"+`$script:StudioEarlyPythonProbed = `$false+`$script:StudioEarlyPython = `$null+`$script:StudioPythonFinalPathCache = `$null+`$script:StudioFinalPathWarned = `$false+Write-Host "KILLSWITCH_DECLINES: `$(`$null -eq (Get-StudioEarlyPython))"+`$info = Try-Step LADDER { Resolve-StudioFinalPathInfo -Path `$temp }+if (`$info) {+ Write-Host "LADDER_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$info.Path))"+ Write-Host "LADDER_EXACT: `$(`$info.Exact)"+}+Write-Host "LADDER_WARNED: `$script:StudioFinalPathWarned"+Write-Host "LADDER_REPEAT_EXACT: `$((Resolve-StudioFinalPathInfo -Path `$temp).Exact)"++# 5. The process-image ladder: Get-Process answers our own image, so no lower rung is paid for.+`$img = Try-Step PROCIMG { Get-StudioProcessImagePath -ProcessId `$PID }+Write-Host "PROCIMG_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$img))"+Write-Host "PROCIMG_STAYED_ON_TOP_RUNG: `$((-not `$script:StudioPythonProcessImageProbed) -and (`$null -eq `$script:StudioProcessImageTable))"++# 6. The rungs below Get-Process, reached by making it answer nothing. On this engine the ctypes rung+# declines and Win32_Process does not exist, so the answer must be null, probed once and counted.+function Get-Process { param(`$Id, `$ErrorAction) return `$null }+`$deep = Try-Step PROCIMG_DEEP { Get-StudioProcessImagePath -ProcessId `$PID }+Write-Host "PROCIMG_DEEP_NULL: `$([string]::IsNullOrWhiteSpace(`$deep))"+Write-Host "PROCIMG_PROBED_ONCE: `$script:StudioPythonProcessImageProbed"+`$script:ProbeCallCount = 0+function Get-StudioPythonProcessImageTable { `$script:ProbeCallCount++; return `$null }+`$null = Get-StudioProcessImagePath -ProcessId `$PID+Write-Host "PROCIMG_NOT_REPROBED: `$(`$script:ProbeCallCount -eq 0)"+Write-Host "PROCIMG_TABLE_STILL_NULL: `$(`$null -eq `$script:StudioPythonProcessImageTable)"+"@+ $file = Join-Path ([System.IO.Path]::GetTempPath()) ("uns_resolver_" + [guid]::NewGuid().ToString("N") + ".ps1")+ Set-Content -LiteralPath $file -Value $harness -Encoding utf8+ $exe = (Get-Command pwsh -ErrorAction SilentlyContinue).Source+ if (-not $exe) { $exe = (Get-Command powershell).Source }+ try { $out = & $exe -NoProfile -File $file 2>&1 | Out-String }+ finally { Remove-Item -LiteralPath $file -ErrorAction SilentlyContinue }++ if ($out -notmatch "PY_THREW") { Write-Host " ---- harness output ----"; Write-Host $out }++ foreach ($row in @(+ @("PY_THREW: False", "the interpreter rung does not throw"),+ @("PY_ANSWERED: True", "the interpreter rung answers"),+ @("PY_ABSOLUTE: True", "and answers with an absolute path"),+ @("EXACT_THREW: False", "the full ladder does not throw with an interpreter present"),+ @("EXACT_ANSWERED: True", "the full ladder answers"),+ @("EXACT_FLAG: True", "and reports the answer as exact"),+ @("EXACT_WARNED: False", "an exact answer prints no degradation warning"),+ @("MISSING_THREW: False", "a not-yet-created path does not throw"),+ @("MISSING_KEEPS_LEAF: True", "and keeps the segments that do not exist yet"),+ @("MISSING_EXACT: True", "and is still exact, since its existing prefix resolved"),+ @("KILLSWITCH_DECLINES: True", "the kill switch really removes the interpreter"),+ @("LADDER_THREW: False", "the ladder without an interpreter does not throw"),+ @("LADDER_ANSWERED: True", "the ladder without an interpreter still answers"),+ @("LADDER_EXACT: False", "and reports the answer as inexact"),+ @("LADDER_WARNED: True", "and says out loud that it is degraded"),+ # The warning latches; the degraded VERDICT must not.+ @("LADDER_REPEAT_EXACT: False", "a second call is still inexact")+ )) { Check $row[1] ($out -match $row[0]) }+ Check "the degradation warning is printed once" (+ ([regex]::Matches($out, "LINE: \[WARN\] Could not resolve a path exactly")).Count -eq 1)+ foreach ($row in @(+ @("PROCIMG_THREW: False", "the process image ladder does not throw"),+ @("PROCIMG_ANSWERED: True", "the process image ladder answers our own image"),+ @("PROCIMG_STAYED_ON_TOP_RUNG: True", "and pays for no rung below the one that answered"),+ @("PROCIMG_DEEP_THREW: False", "the rungs below Get-Process do not throw either"),+ @("PROCIMG_DEEP_NULL: True", "an unanswerable process reads as unknown, not as a path"),+ @("PROCIMG_PROBED_ONCE: True", "the child interpreter is asked once per run"),+ # Counted, not inferred from the latch: a ladder that probes unconditionally also sets it.+ @("PROCIMG_NOT_REPROBED: True", "and is not asked again on the next process"),+ @("PROCIMG_TABLE_STILL_NULL: True", "and an empty result is not re-probed")+ )) { Check $row[1] ($out -match $row[0]) }+}++$probeFn = Get-InstallFunctionSource "Test-StudioSameDirectoryByProbe"+Check "install.ps1 defines Test-StudioSameDirectoryByProbe" ($null -ne $probeFn)+if ($probeFn) {+ Invoke-Expression $probeFn+ $probeRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-probe-" + [guid]::NewGuid().ToString("N"))+ $realDir = Join-Path $probeRoot "real"+ $otherDir = Join-Path $probeRoot "other"+ $absent = Join-Path $probeRoot "absent"+ $null = New-Item -ItemType Directory -Path $realDir -Force+ $null = New-Item -ItemType Directory -Path $otherDir -Force+ $aliasDir = Join-Path $probeRoot "alias"+ $madeAlias = $false+ try { $null = New-Item -ItemType SymbolicLink -Path $aliasDir -Target $realDir -ErrorAction Stop; $madeAlias = $true } catch { }+ $rows = @(+ @("two different directories are not one", $realDir, $otherDir, $false),+ @("a directory is itself", $realDir, $realDir, $true)+ )+ if ($madeAlias) { $rows += , @("two spellings that reach one directory are one", $aliasDir, $realDir, $true) }+ else { Write-Host " SKIP this host cannot create a link to test the aliased spelling" }+ # A missing side cannot be an alias of one that does exist, and must be answered without a write.+ $rows += @(+ @("a missing right side is not a match", $realDir, $absent, $false),+ @("a missing left side is not a match", $absent, $realDir, $false),+ @("an empty spelling is not a match", "", $realDir, $false)+ )+ foreach ($r in $rows) { Check $r[0] ((Test-StudioSameDirectoryByProbe -Left $r[1] -Right $r[2]) -eq $r[3]) }+ Check "the probe file is removed again" (+ @(Get-ChildItem -LiteralPath $realDir -Force -ErrorAction SilentlyContinue).Count -eq 0)+ Remove-Item -LiteralPath $probeRoot -Recurse -Force -ErrorAction SilentlyContinue+}++$ancestorFn = Get-InstallFunctionSource "Split-StudioExistingAncestor"+Check "install.ps1 defines Split-StudioExistingAncestor" ($null -ne $ancestorFn)+if ($ancestorFn) {+ Invoke-Expression $ancestorFn+ $ancRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-anc-" + [guid]::NewGuid().ToString("N"))+ $null = New-Item -ItemType Directory -Path $ancRoot -Force+ $tail = Join-Path "not-there" "either"+ $split = Split-StudioExistingAncestor -Path (Join-Path $ancRoot $tail)+ Check "the deepest existing ancestor is found" ($split -and $split.Root -eq $ancRoot)+ Check "and the part that does not exist is kept whole" ($split -and $split.Tail -eq $tail)+ $here = Split-StudioExistingAncestor -Path $ancRoot+ Check "a path that exists is its own ancestor, with nothing below it" (+ $here -and $here.Root -eq $ancRoot -and $here.Tail -eq "")+ Check "a path with no existing ancestor at all answers nothing" (+ $null -eq (Split-StudioExistingAncestor -Path (Join-Path "/nonexistent-root-xyz" "a")))+ Check "an empty path answers nothing" ($null -eq (Split-StudioExistingAncestor -Path ""))+ Remove-Item -LiteralPath $ancRoot -Recurse -Force -ErrorAction SilentlyContinue+}++$installText = [System.IO.File]::ReadAllText($installPath)+Check "the --tauri gate falls back to comparing existing ancestors" (+ $installText -match 'Split-StudioExistingAncestor -Path \$_tauriOverride')+Check "and requires the segments below them to match" ($installText -match '\$_tauriLeft\.Tail, \$_tauriRight\.Tail')+Check "the --tauri gate asks it before refusing an override" (+ $installText -match 'Test-StudioSameDirectoryByProbe -Left \$_tauriOverride -Right \$_legacyTauriRoot')++$selfText = Get-Content -LiteralPath $PSCommandPath -Raw+$afterGate = $selfText.Substring($selfText.IndexOf('if ($fail' + 'ures -gt 0) {'))+Check "no check is written after the failure gate" ($afterGate -notmatch "(?m)^\s*Check ")++if ($failures -gt 0) {+ Write-Host "$failures check(s) failed" -ForegroundColor Red+ exit 1+}+Write-Host "All checks passed" -ForegroundColor Greendiff --git a/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py b/tests/studio/test_wsl_shortcut_script_is_valid_powershell.pyindex 2ce089b7653..a9350248914 100644--- a/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py+++ b/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py@@ -275,13 +275,14 @@ def test_the_wsl_lane_arms_the_4688_half_of_the_watch() -> None:def test_the_generated_emit_carries_both_spellings_of_define_dynamic_assembly() -> None:"""The outer catch is empty, so guessing the wrong spelling costs the icon refresh silently.- `install.ps1`'s `New-StudioDynamicAssembly` tries the static- `AssemblyBuilder::DefineDynamicAssembly` and falls back to- `AppDomain.CurrentDomain.DefineDynamicAssembly`, and says why: the static form is documented for- .NET Framework 4.5 through 4.8.1, so the Windows PowerShell 5.1 host this script is launched- under should take the first branch, but nothing in this repository can run a .NET Framework host- to confirm it. The generated WSL script had only the static form under a bare `catch {}`, which- is the combination that fails invisibly. Carry the same fallback.+ `install.sh` generates this script and emits its own icon-refresh thunk in it, which is the+ last emit left in the tree: `install.ps1` no longer defines a type at all. The block tries the+ static `AssemblyBuilder::DefineDynamicAssembly` and falls back to+ `AppDomain.CurrentDomain.DefineDynamicAssembly`, and the reason is worth keeping written down:+ the static form is documented for .NET Framework 4.5 through 4.8.1, so the Windows PowerShell+ 5.1 host this script is launched under should take the first branch, but nothing in this+ repository can run a .NET Framework host to confirm it. The generated script had only the static+ form under a bare `catch {}`, which is the combination that fails invisibly. Keep both."""script = _render()assert (