diff --git a/src/agent/self_update.rs b/src/agent/self_update.rs index 765fa5a0..206823ce 100644 --- a/src/agent/self_update.rs +++ b/src/agent/self_update.rs @@ -518,7 +518,7 @@ fn windows_swap_script( .map(|path| powershell_literal(&path.to_string_lossy())) .unwrap_or_else(|| "$null".to_string()); format!( - "$ErrorActionPreference='Stop';$status={status};$fallback={fallback};try{{$p=[Diagnostics.Process]::GetProcessById({pid});$p.WaitForExit()}}catch [ArgumentException]{{}};try{{Move-Item -LiteralPath {staged} -Destination {exe} -Force;$psi=[Diagnostics.ProcessStartInfo]::new();$psi.FileName={exe};$psi.UseShellExecute=$false;$psi.Arguments={args};[void][Diagnostics.Process]::Start($psi);Set-Content -LiteralPath $status -Value 'swap_started' -Encoding Ascii}}catch{{$message=$_.Exception.ToString();Set-Content -LiteralPath $status -Value $message -Encoding UTF8;if($fallback -and (Test-Path -LiteralPath $fallback)){{Copy-Item -LiteralPath $fallback -Destination {exe} -Force;$psi=[Diagnostics.ProcessStartInfo]::new();$psi.FileName={exe};$psi.UseShellExecute=$false;$psi.Arguments={args};[void][Diagnostics.Process]::Start($psi)}}else{{throw}}}}" + "$ErrorActionPreference='Stop';$status={status};$fallback={fallback};$start={{$task=Get-ScheduledTask -TaskName 'CortexHeartbeatAgent' -ErrorAction SilentlyContinue;if($task){{for($i=0;$i -lt 100 -and $task.State -eq 'Running';$i++){{Start-Sleep -Milliseconds 100;$task=Get-ScheduledTask -TaskName 'CortexHeartbeatAgent'}};if($task.State -eq 'Running'){{throw 'CortexHeartbeatAgent task did not become restartable'}};Start-ScheduledTask -TaskName 'CortexHeartbeatAgent'}}else{{$psi=[Diagnostics.ProcessStartInfo]::new();$psi.FileName={exe};$psi.UseShellExecute=$false;$psi.Arguments={args};[void][Diagnostics.Process]::Start($psi)}}}};try{{$p=[Diagnostics.Process]::GetProcessById({pid});$p.WaitForExit()}}catch [ArgumentException]{{}};try{{Move-Item -LiteralPath {staged} -Destination {exe} -Force;& $start;Set-Content -LiteralPath $status -Value 'swap_started' -Encoding Ascii}}catch{{$message=$_.Exception.ToString();Set-Content -LiteralPath $status -Value $message -Encoding UTF8;if($fallback -and (Test-Path -LiteralPath $fallback)){{Copy-Item -LiteralPath $fallback -Destination {exe} -Force;& $start}}else{{throw}}}}" ) } diff --git a/src/agent/self_update_tests.rs b/src/agent/self_update_tests.rs index a2f1088c..6b91383f 100644 --- a/src/agent/self_update_tests.rs +++ b/src/agent/self_update_tests.rs @@ -81,6 +81,9 @@ fn windows_swap_script_preserves_literal_paths_and_process_arguments() { assert!(script.contains("catch{")); assert!(script.contains("cortex.bak")); assert!(script.contains(".handoff.log")); + assert!(script.contains("Get-ScheduledTask -TaskName 'CortexHeartbeatAgent'")); + assert!(script.contains("Start-ScheduledTask -TaskName 'CortexHeartbeatAgent'")); + assert!(script.contains("task did not become restartable")); } #[test]