Skip to content

The agent that grows with you

fix(desktop): pin Windows update handoff cwd

fangliquanflq610b6731d440
diff --git a/scripts/desktop-update/windows.ps1 b/scripts/desktop-update/windows.ps1
index a3068ece4aa..b8784ff9aa6 100644
--- a/scripts/desktop-update/windows.ps1
+++ b/scripts/desktop-update/windows.ps1
@@ -50,7 +50,8 @@ param(
[switch]$NoMarkerCleanup,
[switch]$SelfTestUi,
[switch]$SelfTestPipeDrain,
- [switch]$SelfTestMarker
+ [switch]$SelfTestMarker,
+ [switch]$SelfTestWorkingDirectory
)
if (-not $SelfTestUi -and -not $SelfTestPipeDrain -and -not $InstallRoot) {
@@ -1178,10 +1179,35 @@ function Invoke-HermesStep([string]$Exe, [string[]]$HermesArgs, [string]$Tag) {
return @{ Code = $code; Output = $all; TreeQuiesced = (-not $stalled -or $proc.HasExited); StartedAfterJobAssignment = $true }
}
+function Set-InstallRootCurrentDirectory([string]$Root) {
+ $resolved = [System.IO.Path]::GetFullPath($Root)
+ [Environment]::CurrentDirectory = $resolved
+ return $resolved
+}
+
$finalCode = 1
$finalMsg = "update did not complete"
$script:TreeSafeToFinalize = $true
+# -SelfTestWorkingDirectory: prove StartAssigned inherits InstallRoot --------
+if ($SelfTestWorkingDirectory) {
+ try {
+ $resolvedInstallRoot = Set-InstallRootCurrentDirectory $InstallRoot
+ } catch {
+ Write-Host "WORKING-DIRECTORY SELF-TEST: FAIL cannot enter the install root ($InstallRoot)"
+ exit 3
+ }
+ $probeExe = Join-Path $PSHOME "powershell.exe"
+ $probe = Invoke-HermesStep $probeExe @("-NoProfile", "-Command", "[Environment]::CurrentDirectory") "cwd"
+ $observed = $probe.Output.Trim()
+ if ($probe.Code -ne 0 -or -not [string]::Equals($observed, $resolvedInstallRoot, [StringComparison]::OrdinalIgnoreCase)) {
+ Write-Host "WORKING-DIRECTORY SELF-TEST: FAIL expected=$resolvedInstallRoot observed=$observed code=$($probe.Code)"
+ exit 1
+ }
+ Write-Host "WORKING-DIRECTORY SELF-TEST: PASS $observed"
+ exit 0
+}
+
# ── -SelfTestUi: drive the shim to both terminal states, no update ─────────
# Manual QA for the Edge shell without a checkout or a real update. Exits
# before the marker/desktop/venv machinery — touches nothing. Off Windows
@@ -1444,6 +1470,20 @@ try {
exit 0
}
+ # StartAssigned passes a null CreateProcess currentDirectory, so children
+ # inherit the hand-off process directory rather than PowerShell's $PWD.
+ # Desktop launches us from HERMES_HOME; pin the process directory to the
+ # checkout before any update child can resolve files against the wrong tree.
+ try {
+ $resolvedInstallRoot = Set-InstallRootCurrentDirectory $InstallRoot
+ Write-HandoffLog "process cwd set to install root: $resolvedInstallRoot"
+ } catch {
+ $finalCode = 3
+ $finalMsg = "Update aborted: cannot enter the install root ($InstallRoot). Nothing was changed."
+ Write-HandoffLog $finalMsg
+ exit $finalCode
+ }
+
# Check only the interpreter here: dependency recovery belongs to update.
$pythonExe = Join-Path $InstallRoot "venv\Scripts\python.exe"
if (-not (Test-Path -LiteralPath $pythonExe -PathType Leaf)) {
diff --git a/tests/test_desktop_update_windows_cwd.py b/tests/test_desktop_update_windows_cwd.py
new file mode 100644
index 00000000000..7847da0497e
--- /dev/null
+++ b/tests/test_desktop_update_windows_cwd.py
@@ -0,0 +1,74 @@
+"""Native regression coverage for the Windows Desktop handoff cwd."""
+
+from __future__ import annotations
+
+import os
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+
+pytestmark = pytest.mark.windows_only
+
+REPO_ROOT = Path(__file__).resolve().parent.parent
+WINDOWS_UPDATE_PS1 = REPO_ROOT / "scripts" / "desktop-update" / "windows.ps1"
+
+
+def _run_cwd_self_test(
+ install_root: Path,
+ launch_cwd: Path,
+ temp_dir: Path,
+) -> subprocess.CompletedProcess[str]:
+ powershell = shutil.which("powershell.exe")
+ assert powershell, "Windows updater tests require Windows PowerShell."
+ temp_dir.mkdir(parents=True, exist_ok=True)
+ env = os.environ.copy()
+ env["TEMP"] = str(temp_dir)
+ env["TMP"] = str(temp_dir)
+ return subprocess.run(
+ [
+ powershell,
+ "-NoProfile",
+ "-ExecutionPolicy",
+ "Bypass",
+ "-File",
+ str(WINDOWS_UPDATE_PS1),
+ "-InstallRoot",
+ str(install_root),
+ "-SelfTestWorkingDirectory",
+ "-NoUi",
+ ],
+ cwd=launch_cwd,
+ env=env,
+ text=True,
+ errors="replace",
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ timeout=60,
+ check=False,
+ )
+
+
+def test_handoff_children_run_from_install_root(tmp_path: Path) -> None:
+ install_root = tmp_path / "checkout"
+ launch_cwd = tmp_path / "profile-home"
+ install_root.mkdir()
+ launch_cwd.mkdir()
+
+ result = _run_cwd_self_test(install_root, launch_cwd, tmp_path / "temp")
+
+ assert result.returncode == 0, result.stdout
+ assert "WORKING-DIRECTORY SELF-TEST: PASS" in result.stdout
+
+
+def test_handoff_fails_closed_when_install_root_cannot_be_entered(tmp_path: Path) -> None:
+ install_root = tmp_path / "missing" / "checkout"
+ launch_cwd = tmp_path / "profile-home"
+ launch_cwd.mkdir()
+
+ result = _run_cwd_self_test(install_root, launch_cwd, tmp_path / "temp")
+
+ assert result.returncode == 3, result.stdout
+ assert "cannot enter the install root" in result.stdout