Skip to content

armgit / muxin

Public Imported from GitHub
0

All your machines, one app. For you and your AI agents: agent sessions, desktops, files, logs and GitHub across your Macs, Linux and Windows machines, with an MCP server.

Start a background runner outside the ssh session on Windows (#24)

nobody-opsc03fad31abb3
diff --git a/src/ops/mod.rs b/src/ops/mod.rs
index 2454b36..63b9ac9 100644
--- a/src/ops/mod.rs
+++ b/src/ops/mod.rs
@@ -1188,6 +1188,18 @@ Match host x
}
}
+ #[test]
+ fn windows_scripts_start_a_runner_outside_the_ssh_session() {
+ // sshd ends everything the connection that started it leaves behind when
+ // it closes, so a runner started over ssh has to come from outside that
+ // session: WMI, which is how the agent starts itself too.
+ for script in [INSTALL_PS1, CONTROL_PS1] {
+ let wmi = "Invoke-CimMethod -ClassName Win32_Process -MethodName Create";
+ assert!(script.contains(wmi), "{script}");
+ assert!(!script.contains("Start-Process -FilePath"), "{script}");
+ }
+ }
+
#[test]
fn fallback_release_has_a_checksum_for_every_package() {
let release = fallback_release();
diff --git a/src/ops/scripts/control.ps1 b/src/ops/scripts/control.ps1
index 9469e70..47df15a 100644
--- a/src/ops/scripts/control.ps1
+++ b/src/ops/scripts/control.ps1
@@ -18,6 +18,22 @@ function Get-RunnerProcesses {
Where-Object { $_.ExecutablePath -and $_.ExecutablePath.StartsWith("$OR_DIR\bin\", [StringComparison]::OrdinalIgnoreCase) }
}
+# Starts a program that outlives this session. Muxin runs this script over ssh,
+# and sshd ends everything the connection that started it leaves behind when it
+# closes, so a plain Start-Process leaves nothing running. A process made by WMI
+# isn't in that session; the agent starts itself the same way (agent/src/sys.rs).
+function Start-Detached($file, $dir) {
+ $startup = New-CimInstance -ClassName Win32_ProcessStartup -ClientOnly -Property @{ ShowWindow = [uint16]0 }
+ $cmd = 'cmd.exe /c ""' + $file + '""'
+ $r = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{
+ CommandLine = $cmd
+ CurrentDirectory = $dir
+ ProcessStartupInformation = $startup
+ }
+ if ($r.ReturnValue -ne 0) { throw "Couldn't start $file (WMI returned $($r.ReturnValue))." }
+ $r.ProcessId
+}
+
function Start-Runner {
if ($svcName) {
say "Starting service $svcName…"
@@ -26,7 +42,16 @@ function Start-Runner {
say 'Already running.'
} else {
say 'Starting run.cmd in the background (no service installed)…'
- Start-Process -FilePath (Join-Path $OR_DIR 'run.cmd') -WorkingDirectory $OR_DIR -WindowStyle Hidden
+ $id = Start-Detached (Join-Path $OR_DIR 'run.cmd') $OR_DIR
+ # It takes a moment to come up, longer on a slow machine. run.cmd runs as
+ # long as the runner does, so it ending first means the runner didn't start.
+ for ($i = 0; $i -lt 20 -and -not (Get-RunnerProcesses); $i++) {
+ if (-not (Get-Process -Id $id -ErrorAction SilentlyContinue)) {
+ throw "run.cmd stopped before the runner started. To see why, run $OR_DIR\run.cmd on the machine."
+ }
+ Start-Sleep -Milliseconds 500
+ }
+ if (-not (Get-RunnerProcesses)) { say 'Still starting; the check after this shows whether it came up.' }
}
}
diff --git a/src/ops/scripts/install.ps1 b/src/ops/scripts/install.ps1
index 73925ea..fd48437 100644
--- a/src/ops/scripts/install.ps1
+++ b/src/ops/scripts/install.ps1
@@ -8,6 +8,21 @@ $ProgressPreference = 'SilentlyContinue'
trap { Write-Error ("error: " + $_.Exception.Message); exit 1 }
function say($m) { Write-Output $m }
+# Starts a program that outlives this session. Muxin runs this script over ssh,
+# and sshd ends everything the connection that started it leaves behind when it
+# closes, so a plain Start-Process leaves nothing running. A process made by WMI
+# isn't in that session; the agent starts itself the same way (agent/src/sys.rs).
+function Start-Detached($file, $dir) {
+ $startup = New-CimInstance -ClassName Win32_ProcessStartup -ClientOnly -Property @{ ShowWindow = [uint16]0 }
+ $cmd = 'cmd.exe /c ""' + $file + '""'
+ $r = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{
+ CommandLine = $cmd
+ CurrentDirectory = $dir
+ ProcessStartupInformation = $startup
+ }
+ if ($r.ReturnValue -ne 0) { throw "Couldn't start $file (WMI returned $($r.ReturnValue))." }
+}
+
$arch = switch ($env:PROCESSOR_ARCHITECTURE) {
'AMD64' { 'x64' }
'ARM64' { 'arm64' }
@@ -116,7 +131,7 @@ foreach ($name in $OR_NAMES) {
try { & .\config.cmd @cfg } finally { Remove-Item Env:\ACTIONS_RUNNER_INPUT_TOKEN -ErrorAction SilentlyContinue }
if ($LASTEXITCODE -ne 0) { throw "config.cmd failed for $name (exit $LASTEXITCODE)" }
if (-not $OR_SERVICE) {
- Start-Process -FilePath (Join-Path $dir 'run.cmd') -WorkingDirectory $dir -WindowStyle Hidden
+ Start-Detached (Join-Path $dir 'run.cmd') $dir
say "Started $name in the background (not a service, so it won't come back after a reboot)"
}
Pop-Location