# Kill duplicate agent wrappers; keep newest; then remove stale release dirs $ErrorActionPreference = 'Continue' $Root = 'E:\wwwroot' $keepRel = 'agent-20260913-210940' $keepHome = Join-Path $Root ("agent\releases\" + $keepRel + "\agent") Write-Output '=== stop ALL agent cmd/python under wwwroot ===' Get-CimInstance Win32_Process | ForEach-Object { $cl = $_.CommandLine if (-not $cl) { return } $hit = $false if ($cl -like '*run-agent-http.cmd*' -or $cl -like '*run-agent-worker.cmd*') { $hit = $true } if ($cl -like '*agent.http_app*' -or $cl -like '*agent.worker_app*') { $hit = $true } if ($cl -like '*wwwroot\agent*') { $hit = $true } if ($hit) { Write-Output ("stop pid=$($_.ProcessId) $($_.Name)") Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } } # also free 8910 Get-NetTCPConnection -LocalPort 8910 -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess -Unique | ForEach-Object { Stop-Process -Id $_ -Force -ErrorAction SilentlyContinue } Start-Sleep -Seconds 3 Write-Output '=== delete stale releases ===' Get-ChildItem (Join-Path $Root 'agent\releases') | Where-Object { $_.PSIsContainer } | ForEach-Object { if ($_.Name -eq $keepRel) { Write-Output ('KEEP ' + $_.Name) } else { $full = $_.FullName cmd /c ("rmdir /s /q `"$full`"") | Out-Null if (Test-Path $full) { Write-Output ('STILL ' + $_.Name) } else { Write-Output ('GONE ' + $_.Name) } } } Write-Output '=== rewrite run scripts to keepHome ===' $venvPy = Join-Path $keepHome '.venv\Scripts\python.exe' $runHttp = @( '@echo off', ('cd /d "' + $keepHome + '"'), 'set YTD_ENV=test', 'set AGENT_HTTP_PORT=8910', 'set PYTHONUTF8=1', ':loop', ('"' + $venvPy + '" -m agent.http_app >> "' + $Root + '\logs\agent-http.out.log" 2>> "' + $Root + '\logs\agent-http.err.log"'), ('echo agent exited >> "' + $Root + '\logs\agent-http.err.log"'), 'timeout /t 3 /nobreak >nul', 'goto loop' ) -join "`r`n" Set-Content -Path (Join-Path $Root 'ops\run-agent-http.cmd') -Value $runHttp -Encoding ASCII $runWorker = @( '@echo off', ('cd /d "' + $keepHome + '"'), 'set YTD_ENV=test', 'set PYTHONUTF8=1', ':loop', ('"' + $venvPy + '" -m agent.worker_app >> "' + $Root + '\logs\agent-worker.out.log" 2>> "' + $Root + '\logs\agent-worker.err.log"'), ('echo worker exited >> "' + $Root + '\logs\agent-worker.err.log"'), 'timeout /t 3 /nobreak >nul', 'goto loop' ) -join "`r`n" Set-Content -Path (Join-Path $Root 'ops\run-agent-worker.cmd') -Value $runWorker -Encoding ASCII # ensure junction $current = Join-Path $Root 'agent\current' if (Test-Path $current) { cmd /c ("rmdir `"$current`"") | Out-Null } cmd /c ("mklink /J `"$current`" `"$keepHome`"") | Out-Null Write-Output '=== start one http + one worker ===' $w1 = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{ CommandLine = ('cmd.exe /c "' + (Join-Path $Root 'ops\run-agent-http.cmd') + '"') } $w2 = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{ CommandLine = ('cmd.exe /c "' + (Join-Path $Root 'ops\run-agent-worker.cmd') + '"') } Write-Output ("http_wmi=" + $w1.ReturnValue + " worker_wmi=" + $w2.ReturnValue) Start-Sleep -Seconds 6 try { $h = Invoke-WebRequest -Uri 'http://127.0.0.1:8910/health' -UseBasicParsing -TimeoutSec 8 Write-Output ('HEALTH=' + $h.StatusCode + ' ' + $h.Content) } catch { Write-Output ('health_fail ' + $_) Get-Content (Join-Path $Root 'logs\agent-http.err.log') -Tail 30 -ErrorAction SilentlyContinue } Write-Output '=== left releases ===' Get-ChildItem (Join-Path $Root 'agent\releases') | ForEach-Object { $_.Name } Write-Output 'REPAIR_OK'