Skip to content

The agent that grows with you

fix(desktop-update): relaunch the Desktop and point its shortcuts at the user home

Tekniumb03699fca590
diff --git a/scripts/desktop-update/windows.ps1 b/scripts/desktop-update/windows.ps1
index c3d5353a433..4fa7e8bed7c 100644
--- a/scripts/desktop-update/windows.ps1
+++ b/scripts/desktop-update/windows.ps1
@@ -753,6 +753,15 @@ function Wait-DesktopExit([int]$Seconds) {
return -not (Test-DesktopAlive)
}
+function Get-DesktopWorkingDirectory {
+ # The relaunched Desktop and everything it spawns (WSL/ssh helpers, IME
+ # cloud helpers that outlive it) inherit this directory. Never the exe's
+ # folder: Windows refuses to rename a directory that is a live process's
+ # cwd, so one such helper blocks every later swap of release\win-unpacked (#101789).
+ if ($env:USERPROFILE) { return $env:USERPROFILE }
+ return [Environment]::GetFolderPath('UserProfile')
+}
+
function Start-DesktopRelaunch {
# Returns $true only when a launch VERIFIABLY happened (WMI accepted and
# the pid exists, or the fallback spawn returned a live process). The
@@ -782,7 +791,7 @@ function Start-DesktopRelaunch {
# attach -- same detachment explorer.exe gives a normal launch.
$spawned = $false
try {
- $workDir = Split-Path -Parent $RelaunchExe
+ $workDir = Get-DesktopWorkingDirectory
$r = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{
CommandLine = ('"{0}"' -f $RelaunchExe)
CurrentDirectory = $workDir
@@ -889,7 +898,7 @@ function Start-DesktopRelaunch {
# a tethered Desktop beats no Desktop. Unlike the WMI and explorer rungs it inherits
# this script's environment: the stage relay is the update's, never the app's.
Remove-Item Env:HERMES_UPDATE_STATUS_FILE -ErrorAction SilentlyContinue
- $p = Start-Process -FilePath $RelaunchExe -WorkingDirectory (Split-Path -Parent $RelaunchExe) -PassThru
+ $p = Start-Process -FilePath $RelaunchExe -WorkingDirectory (Get-DesktopWorkingDirectory) -PassThru
Start-Sleep -Milliseconds 1500
if ($p -and -not $p.HasExited) { $spawned = $true }
elseif ($p) { Write-HandoffLog "WARNING: fallback relaunch exited immediately" }
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 9bcdfb29bd9..04af8453a78 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -1541,7 +1541,9 @@ function New-DesktopShortcuts {
}
$sc = $shell.CreateShortcut($lnkPath)
$sc.TargetPath = $TargetExe
- $sc.WorkingDirectory = $workDir
+ # Not the exe's folder: the Desktop's children inherit it, and a helper
+ # that outlives the app then blocks the next update's swap (#101789).
+ $sc.WorkingDirectory = $env:USERPROFILE
$sc.IconLocation = $iconLocation
$sc.Description = 'Hermes Agent'
$sc.Save()
diff --git a/tests/scripts/desktop_update/test_desktop_update_windows_relaunch_cwd.py b/tests/scripts/desktop_update/test_desktop_update_windows_relaunch_cwd.py
new file mode 100644
index 00000000000..8005ccb6f91
--- /dev/null
+++ b/tests/scripts/desktop_update/test_desktop_update_windows_relaunch_cwd.py
@@ -0,0 +1,55 @@
+"""The Desktop the Windows hand-off relaunches never runs from its own app folder.
+
+Everything the relaunched Desktop spawns without an explicit cwd (WSL/ssh helpers,
+IME cloud helpers that outlive it) inherits its working directory, and Windows
+refuses to rename a directory that is any live process's cwd. A relaunch from
+``release\\win-unpacked`` therefore let one leftover helper block every later swap
+of that folder (#101789). The relaunched process here is real: the hand-off
+creates it through WMI exactly as production does.
+"""
+from __future__ import annotations
+
+import os
+import re
+import shutil
+import subprocess
+from pathlib import Path
+
+import psutil
+import pytest
+
+ROOT = Path(__file__).resolve().parent.parent.parent.parent
+SCRIPT = ROOT / 'scripts/desktop-update/windows.ps1'
+
+
+def test_relaunched_desktop_does_not_hold_its_app_folder(tmp_path: Path) -> None:
+ home = tmp_path / 'home'
+ profile = tmp_path / 'profile'
+ app_dir = tmp_path / 'release' / 'win-unpacked'
+ for directory in (home, profile, app_dir):
+ directory.mkdir(parents=True)
+ # A console exe with no arguments waits on its own console: a stand-in Desktop
+ # that stays alive until the test kills it.
+ relaunch = app_dir / 'Hermes.exe'
+ shutil.copy2(Path(os.environ.get('SystemRoot', r'C:\Windows')) / 'System32' / 'cmd.exe', relaunch)
+
+ result = subprocess.run(
+ ['powershell', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', str(SCRIPT),
+ '-InstallRoot', str(home / 'hermes-agent'), '-NoUi', '-SelfTestMarker', '-RelaunchExe', str(relaunch)],
+ cwd=home, env={**os.environ, 'HERMES_HOME': str(home), 'HERMES_RUNTIME_DIR': str(home / 'empty-store'),
+ 'USERPROFILE': str(profile)},
+ capture_output=True, text=True, timeout=180, check=False,
+ )
+ log = (home / 'logs/desktop-update-handoff.log').read_text(encoding='utf-8-sig')
+ launched = re.search(r'desktop relaunched detached \(pid (\d+)\)', log)
+ assert result.returncode == 0 and launched, result.stdout + result.stderr + log
+ pid = int(launched.group(1))
+ try:
+ assert Path(psutil.Process(pid).cwd()).resolve() == profile.resolve()
+ # The next update's swap renames the app folder while the relaunched process lives.
+ moved = app_dir.with_name('win-unpacked.previous')
+ os.rename(app_dir, moved)
+ os.rename(moved, app_dir)
+ finally:
+ subprocess.run(['taskkill', '/T', '/F', '/PID', str(pid)], capture_output=True, check=False)