Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Delete the emit apparatus that existed to colour a banner, now that measurement says it was a no-op (#10999)

Daniel Han4becbbd24d53
diff --git a/install.ps1 b/install.ps1
index a2d774f6d7c..8dd06b2e111 100644
--- a/install.ps1
+++ b/install.ps1
@@ -2097,45 +2097,33 @@ exit 1
function Enable-StudioVirtualTerminal {
if ($env:NO_COLOR) { return $false }
- # A redirected stdout is not a console and GetConsoleMode fails on a non-console handle,
- # so the block below could only return $false anyway. install.rs spawns us with a pipe,
- # so that is the path the desktop app is on.
+ # A redirected stdout is not a console, so there is no virtual terminal to speak of.
+ # install.rs spawns us with a pipe, so that is the path the desktop app is on, and it is
+ # decided here before anything else is consulted.
if ($script:StudioStdoutRedirected) { return $false }
- # Emitted rather than compiled, for the reason New-StudioEmittedNativeType
- # gives: -MemberDefinition runs csc.exe just as -TypeDefinition does, and the
- # guard above only keeps the desktop app off it, so the console path
- # (including `irm | iex`) reached the compiler here every run.
- # Same gate as the resolver, since colour is not worth a risk that cannot be
- # caught; failure is just a plain banner.
- # The published type first, the gate only if there is nothing published: a
- # type this session already emitted proves emit works here, and asking a
- # child instead lets one failed probe throw away a usable console helper.
- if (-not ("StudioVTNative" -as [type]) -and -not (Test-StudioCanDefineNativeTypes)) {
- return $false
- }
- try {
- if (-not ("StudioVTNative" -as [type])) {
- $null = New-StudioEmittedNativeType -TypeName "StudioVTNative" -Imports @(
- @{ Name = "GetStdHandle"; Library = "kernel32.dll"; Return = [IntPtr]
- Args = @([int])
- Ansi = $true },
- @{ Name = "GetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
- Args = @([IntPtr], [uint32].MakeByRefType())
- Ansi = $true
- Out = @(2) },
- @{ Name = "SetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
- Args = @([IntPtr], [uint32])
- Ansi = $true }
- )
- }
- $h = [StudioVTNative]::GetStdHandle(-11)
- [uint32]$mode = 0
- if (-not [StudioVTNative]::GetConsoleMode($h, [ref]$mode)) { return $false }
- $mode = $mode -bor 0x0004
- return [StudioVTNative]::SetConsoleMode($h, $mode)
- } catch {
- return $false
- }
+
+ # Windows PowerShell's console host already does the GetStdHandle / GetConsoleMode /
+ # SetConsoleMode sequence this function used to do by hand, at startup, and reports
+ # the outcome through this property, and it is STRICTER than what this replaced:
+ # ConsoleHostUserInterface.TryTurnOnVirtualTerminal re-reads the mode after setting it, because
+ # older systems accept the call and ignore the flag. The deleted code trusted SetConsoleMode's
+ # return value. So the property cannot read True while VT is actually off.
+ #
+ # Measured on Windows PowerShell 5.1.26100 attached to a real console: the property answers True,
+ # the native call answers True, and the console mode read BEFORE touching it is already 0x7 --
+ # which contains 0x4, ENABLE_VIRTUAL_TERMINAL_PROCESSING. The SetConsoleMode this replaced was
+ # re-setting a bit the host had already set. It was a no-op. The measurement and the lane that
+ # produced it are in PR #10984; the record that travels with this repo is in
+ # tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD).
+ #
+ # This removes three of the script's native imports, and with them the only reason the
+ # console path (including `irm | iex`) ever reached the type emitter at all -- for colour.
+ #
+ # [bool] rather than a bare return: the property is virtual with a base of $false, so a host
+ # that does not override it answers $false already -- but a host with no UI at all yields $null,
+ # and the cast makes that $false too. The try/catch is for Set-StrictMode in a caller's profile,
+ # where reading an absent property raises PropertyNotFoundException rather than returning $null.
+ try { return [bool]$Host.UI.SupportsVirtualTerminal } catch { return $false }
}
$script:StudioVtOk = Enable-StudioVirtualTerminal
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index b7bbb19a7e4..9a7f8a9ae40 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -2079,39 +2079,30 @@ function New-StudioEmittedNativeType {
function Enable-StudioVirtualTerminal {
if ($env:NO_COLOR) { return $false }
- # A redirected stdout is not a console and GetConsoleMode fails on a non-console handle, so the
- # block below could only return $false anyway. The CLI and the desktop app both pipe us, so
- # that is the path they are on.
+ # A redirected stdout is not a console, so there is no virtual terminal to speak of. The CLI and
+ # the desktop app both pipe us, so that is the path they are on, and it is decided here before
+ # anything else is consulted.
if ($script:StudioStdoutRedirected) { return $false }
- # The published type first, the gate only if there is nothing published: a
- # type this session already emitted proves emit works here, and asking a
- # child instead lets one failed probe throw away a usable console helper.
- if (-not ("StudioVTNative" -as [type]) -and -not (Test-StudioCanDefineNativeTypes)) {
- return $false
- }
- try {
- if (-not ("StudioVTNative" -as [type])) {
- $null = New-StudioEmittedNativeType -TypeName "StudioVTNative" -Imports @(
- @{ Name = "GetStdHandle"; Library = "kernel32.dll"; Return = [IntPtr]
- Args = @([int])
- Ansi = $true },
- @{ Name = "GetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
- Args = @([IntPtr], [uint32].MakeByRefType())
- Ansi = $true
- Out = @(2) },
- @{ Name = "SetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
- Args = @([IntPtr], [uint32])
- Ansi = $true }
- )
- }
- $h = [StudioVTNative]::GetStdHandle(-11)
- [uint32]$mode = 0
- if (-not [StudioVTNative]::GetConsoleMode($h, [ref]$mode)) { return $false }
- $mode = $mode -bor 0x0004
- return [StudioVTNative]::SetConsoleMode($h, $mode)
- } catch {
- return $false
- }
+
+ # Windows PowerShell's console host already does the GetStdHandle / GetConsoleMode /
+ # SetConsoleMode sequence this function used to do by hand, at startup, and reports
+ # the outcome through this property, and it is STRICTER than what this replaced:
+ # ConsoleHostUserInterface.TryTurnOnVirtualTerminal re-reads the mode after setting it, because
+ # older systems accept the call and ignore the flag. The deleted code trusted SetConsoleMode's
+ # return value. So the property cannot read True while VT is actually off.
+ #
+ # Measured on Windows PowerShell 5.1.26100 attached to a real console: the property answers True,
+ # the native call answers True, and the console mode read BEFORE touching it is already 0x7 --
+ # which contains 0x4, ENABLE_VIRTUAL_TERMINAL_PROCESSING. The SetConsoleMode this replaced was
+ # re-setting a bit the host had already set. It was a no-op. The measurement and the lane that
+ # produced it are in PR #10984; the record that travels with this repo is in
+ # tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD).
+ #
+ # [bool] rather than a bare return: the property is virtual with a base of $false, so a host
+ # that does not override it answers $false already -- but a host with no UI at all yields $null,
+ # and the cast makes that $false too. The try/catch is for Set-StrictMode in a caller's profile,
+ # where reading an absent property raises PropertyNotFoundException rather than returning $null.
+ try { return [bool]$Host.UI.SupportsVirtualTerminal } catch { return $false }
}
$script:StudioVtOk = Enable-StudioVirtualTerminal
diff --git a/tests/python/test_windows_installer_native_type_fallback.py b/tests/python/test_windows_installer_native_type_fallback.py
index e0aa96e7f25..41765f947d3 100644
--- a/tests/python/test_windows_installer_native_type_fallback.py
+++ b/tests/python/test_windows_installer_native_type_fallback.py
@@ -1700,13 +1700,27 @@ def _gate(source: str) -> str:
)
+# 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", ["install", "setup"])
[email protected]("script", EMIT_SCRIPTS)
@pytest.mark.parametrize("status", ["1", "2"])
@pytest.mark.parametrize("probe,expected", [("$true", "True"), ("$false", "False")])
def test_an_active_policy_is_decided_by_the_child_probe(
@@ -1737,7 +1751,7 @@ def test_an_active_policy_is_decided_by_the_child_probe(
@requires_pwsh
[email protected]("script", ["install", "setup"])
[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."""
@@ -1766,7 +1780,7 @@ def test_no_policy_answers_without_spawning_a_probe(script: str):
# 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", ["install", "setup"])
[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)
@@ -1787,7 +1801,7 @@ def test_the_probe_body_carries_no_double_quote(script: str):
# accept case would fail while the deadline case passed without waiting.
@pytest.mark.skipif(os.name == "nt", reason = "the fake host is a shell script")
@requires_pwsh
[email protected]("script", ["install", "setup"])
[email protected]("script", EMIT_SCRIPTS)
@pytest.mark.parametrize(
"emits,code,expected,outcome,label",
[
@@ -1847,7 +1861,7 @@ def test_the_probe_only_accepts_a_clean_exact_answer(
# "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", ["install", "setup"])
[email protected]("script", EMIT_SCRIPTS)
@pytest.mark.parametrize(
"outcome,calls",
[("indeterminate", "2"), ("blocked", "1")],
@@ -1976,7 +1990,7 @@ def test_an_already_emitted_type_settles_it_without_asking_a_child():
# 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", ["install", "setup"])
[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(
@@ -2052,7 +2066,7 @@ def test_a_child_that_never_returns_does_not_hang_the_installer(tmp_path: Path):
# be put into that binder with $PSNativeCommandArgumentPassing, so this is reachable from
# Linux.
@requires_pwsh
[email protected]("script", ["install", "setup"])
[email protected]("script", EMIT_SCRIPTS)
@pytest.mark.parametrize("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")
@@ -2077,7 +2091,7 @@ def test_the_child_probe_survives_the_5_1_argument_binder(script: str, binding:
@requires_pwsh
[email protected]("script", ["install", "setup"])
[email protected]("script", EMIT_SCRIPTS)
@pytest.mark.parametrize(
"provider,label",
[
@@ -2168,10 +2182,19 @@ def test_each_import_carries_the_charset_its_declaration_had(method: str, charse
# 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", ["install", "setup"])
-def test_the_console_mode_parameter_is_still_declared_out(script: str):
[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")
- which = "StudioVTNative" if script == "setup" else "StudioVTNative"
result = _run_powershell(
"\n".join(
[
@@ -2193,12 +2216,17 @@ def test_the_console_mode_parameter_is_still_declared_out(script: str):
assert result.returncode == 0, result.stderr
assert _lines(result, "OUT:") == ["OUT:True"]
assert _lines(result, "BYREF:") == ["BYREF:True"]
- # And the real declaration in the script carries the key, not just the emitter's
- # ability to honour it.
- block = re.search(r'Name = "GetConsoleMode".*?\}', source, flags = re.DOTALL)
- assert block is not None and "Out = @(2)" in block.group(
- 0
- ), "GetConsoleMode lost its Out position"
+ # 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."
+ )
# Almost every import declares no Out position, and reading an absent hashtable key is fatal
@@ -2208,7 +2236,7 @@ def test_the_console_mode_parameter_is_still_declared_out(script: str):
# emitter asks ContainsKey first, as it does for CharSet; this runs it under the strictest
# setting.
@requires_pwsh
[email protected]("script", ["install", "setup"])
[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(
diff --git a/tests/python/test_windows_setup_output_encoding.py b/tests/python/test_windows_setup_output_encoding.py
index 8997d9e8c2e..10be2d65ec4 100644
--- a/tests/python/test_windows_setup_output_encoding.py
+++ b/tests/python/test_windows_setup_output_encoding.py
@@ -753,9 +753,18 @@ def test_console_less_banner_keeps_its_glyphs(path: Path) -> None:
)
-# Sliced back out to rebuild the function this replaced, so parity is measured against the real predecessor. The
-# comments go with the guard: they do not execute, but leaving them behind would make the reconstruction something
-# this test invented rather than the merge-base function.
+# Sliced back out to prove the guard is load-bearing rather than decorative. This used to rebuild the
+# predecessor for a parity comparison, which worked while the predecessor was still in the file:
+# removing the fast path left the native GetConsoleMode path behind, and the two had to agree.
+#
+# That is no longer what removing it leaves. The native path is gone, so the remainder is a bare
+# `$Host.UI.SupportsVirtualTerminal` read, and that property answers True on a redirected stream --
+# which is exactly why the early return exists and is checked BEFORE it. Comparing the two variants
+# therefore compares the shipped answer against a known-wrong one and fails by construction.
+#
+# So the contract is asserted directly on the code that ships, where it is stronger anyway: a
+# redirected stream is answered False and no escape byte reaches the pipe. Deleting the fast path
+# still fails this test, which is the regression the parity comparison existed to catch.
_VT_FAST_PATH = re.compile(
r"(?m)^[ \t]*# A redirected stdout is not a console.*?\n"
r"(?:^[ \t]*#.*\n)*"
@@ -783,30 +792,39 @@ def _vt_verdict(err: str) -> str:
@windows_only
@powershell_51_only
@pytest.mark.parametrize("path", [SETUP_PS1, INSTALL_PS1], ids = ["setup.ps1", "install.ps1"])
-def test_vt_fast_path_decides_exactly_as_the_compile_did(path: Path) -> None:
- """Skipping csc.exe must not change one byte the user sees.
+def test_a_redirected_stream_is_answered_false_and_gets_no_escape_bytes(path: Path) -> None:
+ """The branch the desktop app actually takes, asserted on the code that ships.
+
+ install.rs spawns the installer with a pipe, so `$script:StudioStdoutRedirected` is true and
+ the early return is what runs. Getting this wrong is not subtle: the Unsloth log panel is that
+ pipe, and a True here puts raw escape sequences in front of a user.
- This probe is the changed branch, not a bystander: install.rs spawns with a pipe, so
- `$script:StudioStdoutRedirected` is true here and the early return is what runs. The
- reconstructed predecessor reaches Add-Type instead, and has to land on the same verdict.
+ `$Host.UI.SupportsVirtualTerminal` does NOT answer this question on its own. It reports True on
+ a redirected stream, which is why the redirect check is consulted first and why deleting it
+ would be a user-visible regression rather than a tidy-up. That is measured below rather than
+ asserted from the source: the fast path is removed and the remainder must disagree.
"""
- new_code, new_raw, new_err = _run_console_less(path)
- old_code, old_raw, old_err = _run_console_less(
- path, source = _probe_without_the_vt_fast_path(path)
- )
- assert new_code == old_code == 0, (
- f"probe exit codes {new_code} (with the fast path) and {old_code} (without)"
- f"{_explain(path, new_code, new_raw, new_err)}"
- # Both sides. Reporting only the new run hid the whole failure once: the new run was
- # the one that passed, and the reconstruction's stderr, which named the missing
- # command, was never printed.
- f"\n reconstructed predecessor stderr:\n{old_err}"
+ code, raw, err = _run_console_less(path)
+ assert code == 0, f"the shipped probe exited {code}{_explain(path, code, raw, err)}"
+ assert _vt_verdict(err) == "False", (
+ f"a redirected stream was told it can render VT ({_vt_verdict(err)}). install.rs reads this "
+ f"pipe, so the banner would arrive with escape sequences in it."
+ + _explain(path, code, raw, err)
)
- assert _vt_verdict(new_err) == _vt_verdict(old_err) == "False", (
- f"a redirected stream cannot render VT: the fast path returned "
- f"{_vt_verdict(new_err)} where the compile returned {_vt_verdict(old_err)}"
+ assert b"\x1b" not in raw, (
+ "an escape byte reached the pipe even though the verdict was False, so something downstream "
+ "of Enable-StudioVirtualTerminal is colouring unconditionally"
+ + _explain(path, code, raw, err)
)
- assert new_raw == old_raw, (
- "the banner bytes moved. Same verdict in, same bytes out is the whole contract of "
- "this change" + _explain(path, new_code, new_raw, new_err)
+
+ # The guard is load-bearing, and this is what says so. Without it the property alone decides,
+ # and on a redirected stream it says True. If this ever stops differing, the early return has
+ # become redundant and the comment above it is wrong.
+ bare_code, _, bare_err = _run_console_less(path, source = _probe_without_the_vt_fast_path(path))
+ assert bare_code == 0, f"the fast-path-less probe exited {bare_code}:\n{bare_err}"
+ assert _vt_verdict(bare_err) == "True", (
+ f"without the redirect check the property answered {_vt_verdict(bare_err)}, not True. The "
+ f"early return in Enable-StudioVirtualTerminal is documented as load-bearing because the "
+ f"property cannot see a redirected stream; if that has changed, update the comment there "
+ f"and this test together.\n{bare_err}"
)
diff --git a/tests/studio/test_installer_av_shapes.py b/tests/studio/test_installer_av_shapes.py
index 1e4181425fe..9874bf394db 100644
--- a/tests/studio/test_installer_av_shapes.py
+++ b/tests/studio/test_installer_av_shapes.py
@@ -511,32 +511,81 @@ def test_no_new_native_imports(name: str) -> None:
@pytest.mark.parametrize("name", ("install.ps1", "studio/setup.ps1"))
def test_virtual_terminal_answers_a_redirected_stream_without_defining_a_type(name: str) -> None:
- """The answer we already know must come first, before any native work at all.
-
- Only the redirected case is decided early, and it is decided FALSE: a redirected stdout is
- not a console, GetConsoleMode fails on a non-console handle, and the native path could only
- have returned false too. Anything claiming VT here would put raw escape sequences in the
- Unsloth log panel, which is a pipe.
-
- This used to guard an Add-Type, when the redirect check was all that kept the desktop app
- off csc.exe. Nothing compiles now, so the ordering no longer matters to a scanner, but it is
- still the cheaper answer and getting it wrong still corrupts the log panel.
+ """The stronger contract this test's name always implied: nothing native happens here at all.
+
+ It used to assert an ordering -- that the redirect check came *before* the emit call -- because
+ the redirect check was the only thing keeping the desktop app off csc.exe. There is no emit call
+ now. A CI pre-flight measured Windows PowerShell 5.1 attached to a real console and found the
+ console mode already 0x7 before any of our code ran: bit 0x4,
+ ENABLE_VIRTUAL_TERMINAL_PROCESSING, is set by the host at startup. The SetConsoleMode this
+ replaced was re-setting a bit that was already set, so reading
+ $Host.UI.SupportsVirtualTerminal loses nothing.
+
+ Two things still have to hold. The redirected case must still be decided FALSE and decided
+ first: a redirected stdout is not a console, and anything claiming VT there puts raw escape
+ sequences in the Unsloth log panel, which is a pipe. And the function must stay free of native
+ work, or the three kernel32 imports come back one careful commit at a time.
"""
text = _text(name)
start = text.index("function Enable-StudioVirtualTerminal")
- call = re.compile(r"(?m)^[ \t]*\$null = New-StudioEmittedNativeType\b").search(text, start)
- assert call, f"{name} no longer emits the console thunk; update this guard"
- define_at = call.start()
- fast_path = text.index("if ($script:StudioStdoutRedirected) { return $false }", start)
- assert fast_path < define_at, (
- f"{name} builds the native console thunk before checking the stream: move the redirect "
- f"guard above it, since a redirected stream can never render VT anyway."
- )
- assert "$true" not in text[fast_path:define_at], (
- f"{name} returns something other than $false before the native work. The early answer is "
- f"only sound because a redirected stream can never render VT."
+ # To the end of the function. The next top-level construct after it is the assignment of its
+ # result, which is a stable landmark in both files.
+ end = text.index("$script:StudioVtOk = Enable-StudioVirtualTerminal", start)
+ body = text[start:end]
+
+ fast_path = body.index("if ($script:StudioStdoutRedirected) { return $false }")
+ property_read = body.index("$Host.UI.SupportsVirtualTerminal")
+ assert fast_path < property_read, (
+ f"{name} consults the host before checking whether the stream is redirected. A redirected "
+ f"stream can never render VT, so that case has to be decided first and decided false."
)
+ for banned in (
+ "New-StudioEmittedNativeType",
+ "DefinePInvokeMethod",
+ "Add-Type",
+ "GetStdHandle",
+ "SetConsoleMode",
+ "kernel32",
+ ):
+ assert banned not in _strip_comments(body), (
+ f"{name}'s Enable-StudioVirtualTerminal does native work again ({banned}). The host "
+ f"already enables virtual terminal processing at startup, measured: the console mode "
+ f"is 0x7 before we touch it. Colouring a banner is not worth three kernel32 imports."
+ )
+
+
+def _strip_comments(text: str) -> str:
+ return "\n".join(line for line in text.splitlines() if not line.strip().startswith("#"))
+
+
+def test_neither_installer_declares_a_console_mode_import() -> None:
+ """The console thunk reached zero native surface, and must not drift back.
+
+ Both scripts used to declare GetStdHandle, GetConsoleMode and SetConsoleMode for one consumer: a
+ cosmetic ANSI colour banner. A CI pre-flight measured Windows PowerShell 5.1 attached to a real
+ console and found the mode already 0x7 before anything of ours ran, so bit 0x4,
+ ENABLE_VIRTUAL_TERMINAL_PROCESSING, was already set by the host and the SetConsoleMode was
+ re-setting it. Without this test the three imports are one "just add a small helper" away from
+ coming back, and nothing else in the suite would notice: every other check here is about how a
+ native import is DECLARED rather than whether there is one.
+
+ Scoped to the console imports rather than to all of them. studio/setup.ps1 still emits the nvml
+ and nvcuda imports for Get-NvidiaLibraryProbeType, which is what the GPU inventory reads, so a
+ blanket "no native imports" assertion would be false and deleting the apparatus to satisfy it
+ would break that.
+ """
+ for name in ("install.ps1", "studio/setup.ps1"):
+ declared = _native_imports(_text(name))
+ for banned in ("GetStdHandle", "GetConsoleMode", "SetConsoleMode"):
+ assert banned not in declared, (
+ f"{name} declares {banned} again; the console mode is the host's job and "
+ f"$Host.UI.SupportsVirtualTerminal reports its outcome"
+ )
+ setup = _strip_comments(_text("studio/setup.ps1"))
+ assert "StudioVTNative" not in setup, "the emitted console thunk is back in studio/setup.ps1"
+ assert "Add-Type" not in setup, "studio/setup.ps1 compiles C# through csc.exe again"
+
@pytest.mark.parametrize("name", ALL_SCRIPTS)
def test_no_process_memory_apis(name: str) -> None:
@@ -603,13 +652,18 @@ def test_the_installer_never_runs_the_c_sharp_compiler(name: str) -> None:
"DefinePInvokeMethod block where the script is generated and cannot call it; "
"-MemberDefinition runs csc.exe just as -TypeDefinition does."
)
- # Conditional: only a script that declares a native import has an emit to still be doing.
- # scripts/uninstall.ps1 and studio/setup.sh declare none, and demanding the token of them would
- # be a guard that fails for being satisfied.
+ # Conditional, because "emits its native imports" only means anything for a file that HAS
+ # native imports. Three shipped files now declare none: scripts/uninstall.ps1 and
+ # studio/setup.sh never did, and studio/setup.ps1 stopped -- its whole emit apparatus existed
+ # to colour a banner, and the host turns out to enable virtual terminal processing before our
+ # code runs. Demanding the token of a file with zero native surface would be a guard that
+ # fails for being satisfied; test_setup_declares_no_native_imports_at_all is what keeps that
+ # zero honest.
if _native_imports(text):
- assert (
- "DefinePInvokeMethod" in text
- ), f"{name} declares native imports without emitting them; update this guard"
+ assert "DefinePInvokeMethod" in text, (
+ f"{name} declares native imports but no longer emits them. If they are compiled again "
+ f"instead, that is csc.exe on 5.1, which is the shape this whole file exists to keep out."
+ )
# The private-%TEMP% retry is gone with it: redirecting TEMP to compile again cannot beat a
# filter driver, and "blocked writing an executable to TEMP, change TEMP, write it again" is
# itself an evasion heuristic. Scoped to the resolver, since Initialize-StudioTempEnvironment
@@ -1255,3 +1309,48 @@ def test_setup_bat_steps_down_to_bypass_only_for_a_remote_script() -> None:
# The launch line, the -NoProfile asymmetry and the Unblock-File ordering are asserted by
# test_setup_bat_clears_the_mark_before_loading_under_remotesigned above; not repeated here.
+
+
+# Whole-line comments only. Both defects this guards against were whole-line, and a trailing `#`
+# cannot be told from a `#` inside a string without re-parsing, which would trade a real check for
+# a source of false alarms.
+_COMMENT_PREFIXES = {".bat": ("rem ", "::"), ".ps1": ("#",), ".sh": ("#",)}
+
+# A repo-relative path, which is a claim about THIS tree, as opposed to a PR number or a URL.
+# Anchored on the real top-level directories and required to carry a file extension, so
+# `unsloth.ai/install.ps1` (a URL) and a bare directory mention do not match.
+_REPO_PATH_IN_PROSE = re.compile(
+ r"(?<![\w./-])((?:\.github|docs|tests|scripts|studio|unsloth|unsloth_cli|unsloth_zoo)"
+ r"/[\w./-]+\.\w+)"
+)
+
+
+def _comment_lines(text: str, name: str):
+ prefixes = _COMMENT_PREFIXES[Path(name).suffix]
+ for line in text.splitlines():
+ stripped = line.strip().lower() if name.endswith(".bat") else line.strip()
+ if stripped.startswith(prefixes):
+ yield line
+
+
[email protected]("name", DOCUMENTED_SCRIPTS)
+def test_a_comment_never_points_at_a_file_that_is_not_here(name: str) -> None:
+ """A comment citing evidence must cite something a reader can actually open.
+
+ Twice now a shipped script has carried a pointer to a file that was not in the tree: first
+ `docs/windows-installer-av-shapes.md` after the doc was folded into AV_SHAPES_RECORD, then
+ `.github/workflows/windows-vt-preflight.yml`, which lives in a separate PR and therefore does
+ not exist on this branch at all. Both read as authoritative and neither could be followed, which
+ is worse than saying nothing: the justification for deleting a native call becomes unverifiable.
+ Referring to a PR number is fine and stays true; referring to a path is a claim about this tree.
+ """
+ text = (REPO / name).read_text(encoding = "utf-8")
+ cited = set()
+ for line in _comment_lines(text, name):
+ cited.update(_REPO_PATH_IN_PROSE.findall(line))
+
+ missing = sorted(p for p in cited if not (REPO / p).exists())
+ assert not missing, (
+ 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."
+ )
diff --git a/tests/studio/test_native_path_resolver_degrades.ps1 b/tests/studio/test_native_path_resolver_degrades.ps1
index 90562c23ee3..86f9888f4f1 100644
--- a/tests/studio/test_native_path_resolver_degrades.ps1
+++ b/tests/studio/test_native_path_resolver_degrades.ps1
@@ -66,9 +66,20 @@ foreach ($pair in @(@("install.ps1", $installPath), @("studio/setup.ps1", $setup
$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.
+ # 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
@@ -152,7 +163,7 @@ 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 "StudioVTNative" -Imports @(
+`$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()) },
@@ -163,7 +174,7 @@ Write-Host "NATIVE_ANSWER_NULL: `$([string]::IsNullOrEmpty(`$answer))"
Args = @([int], [uint32], [string], [IntPtr]) })
Write-Host "VT_EMIT_OK: `$vt"
Write-Host "ICON_EMIT_OK: `$icon"
-`$gcm = ("StudioVTNative" -as [type]).GetMethod("GetConsoleMode")
+`$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])"
@@ -172,11 +183,11 @@ Write-Host "ICON_RETURNS_VOID: `$(`$shcn.ReturnType -eq [System.Void])"
# -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 "StudioVTNative" -Imports @(
+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 (("StudioVTNative" -as [type]).GetMethod("SetConsoleMode")))"
+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
diff --git a/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py b/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py
index edbcba17f76..2ce089b7653 100644
--- a/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py
+++ b/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py
@@ -59,7 +59,8 @@ needs_pwsh = pytest.mark.skipif(shutil.which("pwsh") is None, reason = "needs Po
# (tests/python/test_install_uv_override_space.py:21,
# tests/studio/install/test_selection_logic.py:4185).
pytestmark = pytest.mark.skipif(
- sys.platform == "win32", reason = "renders install.sh with bash; POSIX shell installer test",
+ sys.platform == "win32",
+ reason = "renders install.sh with bash; POSIX shell installer test",
)
# Values install.sh would interpolate, chosen to be awkward: a distro name with a space, and the