@echo off setlocal EnableExtensions cd /d "%~dp0" if not "%~1"=="" set "CONTROLLER_URL=%~1" set "EXIT_CODE=0" if not exist "logs" mkdir "logs" >nul 2>nul set "LOG_FILE=%~dp0logs\agent_start_latest.log" if not defined PYTHONIOENCODING set "PYTHONIOENCODING=utf-8" call :set_stamp > "%LOG_FILE%" echo [%STAMP%] SystemTest-Agent startup >> "%LOG_FILE%" echo Working directory: %CD% if defined CONTROLLER_URL >> "%LOG_FILE%" echo CONTROLLER_URL=%CONTROLLER_URL% echo Startup log: "%LOG_FILE%" if defined PYTHON_EXE ( if exist "%PYTHON_EXE%" ( call :run_python "%PYTHON_EXE%" -u -m app goto :after_run ) else ( >> "%LOG_FILE%" echo PYTHON_EXE is set but was not found: %PYTHON_EXE% ) ) set "CODEX_PYTHON=%USERPROFILE%\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe" if exist "%CODEX_PYTHON%" ( call :run_python "%CODEX_PYTHON%" -u -m app goto :after_run ) call :run_python python -u -m app if not "%EXIT_CODE%"=="0" ( call :run_python py -3 -u -m app ) :after_run if not "%EXIT_CODE%"=="0" ( echo. echo SystemTest-Agent exited with code %EXIT_CODE%. echo Full log: "%LOG_FILE%" echo. echo Last log lines: call :show_log_tail echo. echo Common causes: echo 1. Controller is not reachable from this device. echo Try: start_agent.bat http://^:8001 echo 2. Python 3 is not installed or not in PATH. echo 3. Local service port 18081 is already in use. echo. ) if not "%AGENT_NO_PAUSE%"=="1" ( echo. echo SystemTest-Agent stopped. Full log: "%LOG_FILE%" echo Press any key to close this window. pause >nul ) :end endlocal & exit /b %EXIT_CODE% :run_python echo. >> "%LOG_FILE%" call :set_stamp echo [%STAMP%] Running: %* >> "%LOG_FILE%" %* >> "%LOG_FILE%" 2>&1 set "EXIT_CODE=%ERRORLEVEL%" call :set_stamp echo [%STAMP%] Exit code: %EXIT_CODE% >> "%LOG_FILE%" exit /b 0 :set_stamp for /f "delims=" %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-ddTHH:mm:ss"') do set "STAMP=%%I" if not defined STAMP set "STAMP=%TIME%" exit /b 0 :show_log_tail powershell -NoProfile -Command "if (Test-Path -LiteralPath '%LOG_FILE%') { Get-Content -LiteralPath '%LOG_FILE%' -Tail 80 -Encoding UTF8 }" exit /b 0