param( [string]$ControllerUrl ) $ErrorActionPreference = "Stop" Set-Location -LiteralPath $PSScriptRoot if ($ControllerUrl) { $env:CONTROLLER_URL = $ControllerUrl } if (-not $env:PYTHONIOENCODING) { $env:PYTHONIOENCODING = "utf-8" } $logDir = Join-Path $PSScriptRoot "logs" New-Item -ItemType Directory -Force -Path $logDir | Out-Null $logFile = Join-Path $logDir "agent_start_latest.log" @( "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] SystemTest-Agent startup" "Working directory: $PWD" "CONTROLLER_URL=$env:CONTROLLER_URL" ) | Set-Content -LiteralPath $logFile -Encoding UTF8 Write-Host "Startup log: $logFile" function Write-ExitHints { param([int]$ExitCode) Write-Host "" Write-Host "SystemTest-Agent exited with code $ExitCode." Write-Host "Full log: $logFile" Write-Host "" Write-Host "Last log lines:" if (Test-Path -LiteralPath $logFile) { Get-Content -LiteralPath $logFile -Tail 80 } Write-Host "" Write-Host "Common causes:" Write-Host " 1. Controller is not reachable from this device." Write-Host " Try: .\start_agent.ps1 -ControllerUrl http://:8001" Write-Host " 2. Python 3 is not installed or not in PATH." Write-Host " 3. Local service port 18081 is already in use." } function Pause-IfNeeded { if ($env:AGENT_NO_PAUSE -ne "1") { Write-Host "" Write-Host "SystemTest-Agent stopped. Full log: $logFile" Read-Host "Press Enter to close this window" | Out-Null } } function ConvertTo-CommandArgumentString { param([string[]]$Arguments) $quoted = foreach ($argument in $Arguments) { if ($argument -match '[\s"]') { '"' + ($argument -replace '"', '\"') + '"' } else { $argument } } return ($quoted -join ' ') } function Quote-CmdArgument { param([string]$Value) return '"' + ($Value -replace '"', '\"') + '"' } function Invoke-LoggedProcess { param( [string]$FilePath, [string[]]$Arguments, [string]$LogFilePath ) $commandLine = "$(Quote-CmdArgument -Value $FilePath) $(ConvertTo-CommandArgumentString -Arguments $Arguments) >> $(Quote-CmdArgument -Value $LogFilePath) 2>&1" & $env:ComSpec /d /s /c $commandLine return $LASTEXITCODE } $pythonExe = $null $pythonArgs = @() try { if ($env:PYTHON_EXE -and (Test-Path -LiteralPath $env:PYTHON_EXE)) { $pythonExe = $env:PYTHON_EXE } if (-not $pythonExe) { $bundledPython = Join-Path $env:USERPROFILE '.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' if (Test-Path -LiteralPath $bundledPython) { $pythonExe = $bundledPython } } if (-not $pythonExe) { $pythonCommand = Get-Command python -ErrorAction SilentlyContinue if ($pythonCommand) { $pythonExe = $pythonCommand.Source } } if (-not $pythonExe) { $pythonCommand = Get-Command py -ErrorAction SilentlyContinue if ($pythonCommand) { $pythonExe = $pythonCommand.Source $pythonArgs = @('-3') } } if (-not $pythonExe) { throw "Python 3 was not found. Install Python 3, set PYTHON_EXE, or add Python to PATH." } $runArgs = @($pythonArgs) + @("-u", "-m", "app") $runLine = "Running: $pythonExe $(ConvertTo-CommandArgumentString -Arguments $runArgs)" Write-Host $runLine Add-Content -LiteralPath $logFile -Encoding UTF8 -Value $runLine $exitCode = Invoke-LoggedProcess -FilePath $pythonExe -Arguments $runArgs -LogFilePath $logFile } catch { $exitCode = 2 Write-Host ($_ | Out-String) Add-Content -LiteralPath $logFile -Encoding UTF8 -Value ($_ | Out-String) } if ($exitCode -ne 0) { Write-ExitHints -ExitCode $exitCode } Pause-IfNeeded exit $exitCode