Skip to content

The agent that grows with you

fix(desktop-update): keep console selection from stalling the Windows hand-off

Brooklyn Nicholsond06a3b8a54ec
diff --git a/scripts/desktop-update/windows.ps1 b/scripts/desktop-update/windows.ps1
index 23cb07f5517..bb1f0a93a2a 100644
--- a/scripts/desktop-update/windows.ps1
+++ b/scripts/desktop-update/windows.ps1
@@ -83,6 +83,44 @@ try {
'@ -ErrorAction Stop
$script:Win32 = $true
} catch { $script:Win32 = $false }
+# Console selection must never hold the hand-off (#103222). The console is
+# hidden by design (wrapHandoffForDetachedConsole), but an older Desktop or a
+# manual run can leave it visible, and conhost blocks every write to it while a
+# selection is active: the child output replayed after `hermes update` exited
+# stalled on Write-Host until the user pressed Esc, and the result, marker
+# cleanup and relaunch waited behind it. QuickEdit goes off for the run (so a
+# stray click cannot start a selection) and the console echo is skipped while
+# one is active; the log file keeps every line either way.
+try {
+ Add-Type -Namespace HermesHandoff -Name ConsoleInput -MemberDefinition @'
+[StructLayout(LayoutKind.Sequential)] public struct SelectionInfo { public uint Flags; public uint Anchor; public ulong Window; }
+[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
+static extern IntPtr CreateFile(string name, uint access, uint share, IntPtr attributes, uint disposition, uint flags, IntPtr template);
+[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr handle);
+[DllImport("kernel32.dll")] static extern bool GetConsoleMode(IntPtr handle, out uint mode);
+[DllImport("kernel32.dll")] static extern bool SetConsoleMode(IntPtr handle, uint mode);
+[DllImport("kernel32.dll")] static extern bool GetConsoleSelectionInfo(out SelectionInfo info);
+
+// stdin is NUL under the Desktop spawn, so open the console input buffer itself.
+static uint? Swap(Func<uint, uint> change) {
+ IntPtr input = CreateFile("CONIN$", 0xC0000000, 3, IntPtr.Zero, 3, 0, IntPtr.Zero);
+ if (input == new IntPtr(-1)) return null;
+ try {
+ uint mode;
+ if (!GetConsoleMode(input, out mode) || !SetConsoleMode(input, change(mode))) return null;
+ return mode;
+ } finally { CloseHandle(input); }
+}
+// ENABLE_EXTENDED_FLAGS (0x80) makes conhost honour the cleared ENABLE_QUICK_EDIT_MODE (0x40).
+public static uint? DisableQuickEdit() { return Swap(mode => (mode & ~0x40u) | 0x80u); }
+public static void Restore(uint mode) { Swap(_ => mode); }
+public static bool Selecting() {
+ SelectionInfo info;
+ return GetConsoleSelectionInfo(out info) && (info.Flags & 1u) != 0; // CONSOLE_SELECTION_IN_PROGRESS
+}
+'@ -ErrorAction Stop
+ $script:ConsoleInput = $true
+} catch { $script:ConsoleInput = $false }
# Render UTF-8 glyphs (checkmarks, arrows) correctly in our own console echo
# too; the legacy conhost default OEM codepage shows them as mojibake.
try {
@@ -103,6 +141,7 @@ $script:UiStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
function Write-HandoffLog([string]$Message) {
$line = "{0:yyyy-MM-ddTHH:mm:ssK} {1}" -f (Get-Date), $Message
try { Add-Content -LiteralPath $LogPath -Value $line -Encoding UTF8 } catch {}
+ if ($script:ConsoleInput -and [HermesHandoff.ConsoleInput]::Selecting()) { return }
Write-Host $line
}
@@ -1465,6 +1504,7 @@ exit 3
exit 0
}
+$savedConsoleInputMode = if ($script:ConsoleInput) { [HermesHandoff.ConsoleInput]::DisableQuickEdit() } else { $null }
try {
New-Item -ItemType Directory -Path $LogDir -Force -ErrorAction SilentlyContinue | Out-Null
Remove-Item -LiteralPath $ResultPath -Force -ErrorAction SilentlyContinue
@@ -1720,4 +1760,5 @@ try {
Close-ProgressWindow
}
}
+ if ($null -ne $savedConsoleInputMode) { [HermesHandoff.ConsoleInput]::Restore($savedConsoleInputMode) }
}