|
| 1 | +from agent_core import action |
| 2 | + |
| 3 | + |
| 4 | +@action( |
| 5 | + name="run_powershell", |
| 6 | + description="Runs a PowerShell script with Windows PowerShell, capturing stdout, stderr, and exit code. Stdin is closed (EOF) by default. IMPORTANT: For long-running scripts that don't terminate (e.g., dev servers, watch processes), you MUST set background=true. Otherwise, the script will block the entire task until timeout and may not capture any output.", |
| 7 | + platforms=["windows"], |
| 8 | + default=True, |
| 9 | + action_sets=["core"], |
| 10 | + input_schema={ |
| 11 | + "script": { |
| 12 | + "type": "string", |
| 13 | + "example": "Get-CimInstance Win32_LogicalDisk -Filter 'DriveType=3' | Select-Object DeviceID, FreeSpace, Size", |
| 14 | + "description": "The PowerShell script to run, exactly as typed at a PowerShell prompt. Can span multiple lines.", |
| 15 | + }, |
| 16 | + "timeout": { |
| 17 | + "type": "integer", |
| 18 | + "example": 600, |
| 19 | + "description": "Optional timeout (seconds). If exceeded, the process is terminated.", |
| 20 | + }, |
| 21 | + "cwd": { |
| 22 | + "type": "string", |
| 23 | + "example": "C:\\\\Users\\\\me", |
| 24 | + "description": "Optional working directory for the script.", |
| 25 | + }, |
| 26 | + "env": { |
| 27 | + "type": "object", |
| 28 | + "additionalProperties": {"type": "string"}, |
| 29 | + "example": {"MY_VAR": "123"}, |
| 30 | + "description": "Optional environment variable overrides.", |
| 31 | + }, |
| 32 | + "background": { |
| 33 | + "type": "boolean", |
| 34 | + "example": False, |
| 35 | + "description": "Set to true for long-running processes (dev servers, watchers, etc.). The script will start in the background and return immediately with the process ID. Required for commands like 'npm run dev', 'npm start', 'python -m http.server'.", |
| 36 | + }, |
| 37 | + }, |
| 38 | + output_schema={ |
| 39 | + "status": {"type": "string", "example": "success"}, |
| 40 | + "stdout": {"type": "string", "example": "Script output text"}, |
| 41 | + "stderr": {"type": "string", "example": ""}, |
| 42 | + "return_code": {"type": "integer", "example": 0}, |
| 43 | + "message": {"type": "string", "example": "Timed out after 30s."}, |
| 44 | + "pid": { |
| 45 | + "type": "integer", |
| 46 | + "example": 12345, |
| 47 | + "description": "Process ID when running in background mode.", |
| 48 | + }, |
| 49 | + }, |
| 50 | + test_payload={ |
| 51 | + "script": "Get-ChildItem C:\\\\Windows\\\\System32", |
| 52 | + "timeout": 600, |
| 53 | + "cwd": "C:\\\\Users\\\\me", |
| 54 | + "env": {"MY_VAR": "123"}, |
| 55 | + "background": False, |
| 56 | + "simulated_mode": True, |
| 57 | + }, |
| 58 | +) |
| 59 | +def powershell_exec_windows(input_data: dict) -> dict: |
| 60 | + import os |
| 61 | + import subprocess |
| 62 | + |
| 63 | + simulated_mode = input_data.get("simulated_mode", False) |
| 64 | + |
| 65 | + if simulated_mode: |
| 66 | + # Return mock result for testing |
| 67 | + return { |
| 68 | + "status": "success", |
| 69 | + "stdout": "Simulated script output", |
| 70 | + "stderr": "", |
| 71 | + "return_code": 0, |
| 72 | + "message": "", |
| 73 | + "pid": None, |
| 74 | + } |
| 75 | + |
| 76 | + script = str(input_data.get("script", "")).strip() |
| 77 | + timeout_val = input_data.get("timeout") |
| 78 | + cwd = input_data.get("cwd") |
| 79 | + env_input = input_data.get("env") or {} |
| 80 | + background = input_data.get("background", False) |
| 81 | + |
| 82 | + timeout_seconds = float(timeout_val) if timeout_val is not None else 600.0 |
| 83 | + |
| 84 | + if not script: |
| 85 | + return { |
| 86 | + "status": "error", |
| 87 | + "stdout": "", |
| 88 | + "stderr": "", |
| 89 | + "return_code": -1, |
| 90 | + "message": "script is required.", |
| 91 | + "pid": None, |
| 92 | + } |
| 93 | + |
| 94 | + if cwd and not os.path.isdir(cwd): |
| 95 | + return { |
| 96 | + "status": "error", |
| 97 | + "stdout": "", |
| 98 | + "stderr": "", |
| 99 | + "return_code": -1, |
| 100 | + "message": "Working directory does not exist.", |
| 101 | + "pid": None, |
| 102 | + } |
| 103 | + |
| 104 | + # Same environment as run_shell: the resolved Node runtime leads PATH, so |
| 105 | + # bare `node` is the major the agent-app CLI needs (app/node_runtime.py). |
| 106 | + from app import node_runtime |
| 107 | + |
| 108 | + env = node_runtime.child_env() |
| 109 | + for k, v in env_input.items(): |
| 110 | + env[str(k)] = str(v) |
| 111 | + |
| 112 | + # Windows PowerShell ships with every Windows install, so the dialect a |
| 113 | + # script is written for is the same on every machine. |
| 114 | + args = [ |
| 115 | + "powershell.exe", |
| 116 | + "-NoLogo", |
| 117 | + "-NonInteractive", |
| 118 | + "-NoProfile", |
| 119 | + "-ExecutionPolicy", |
| 120 | + "Bypass", |
| 121 | + "-Command", |
| 122 | + script, |
| 123 | + ] |
| 124 | + |
| 125 | + creation_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0) |
| 126 | + |
| 127 | + # Background mode: start process and return immediately |
| 128 | + if background: |
| 129 | + try: |
| 130 | + # Use CREATE_NEW_PROCESS_GROUP to detach from parent |
| 131 | + bg_flags = creation_flags | subprocess.CREATE_NEW_PROCESS_GROUP |
| 132 | + process = subprocess.Popen( |
| 133 | + args, |
| 134 | + stdout=subprocess.DEVNULL, |
| 135 | + stderr=subprocess.DEVNULL, |
| 136 | + stdin=subprocess.DEVNULL, |
| 137 | + cwd=cwd if cwd else None, |
| 138 | + env=env, |
| 139 | + creationflags=bg_flags, |
| 140 | + ) |
| 141 | + return { |
| 142 | + "status": "background", |
| 143 | + "stdout": "", |
| 144 | + "stderr": "", |
| 145 | + "return_code": 0, |
| 146 | + "message": f"Process started in background with PID {process.pid}", |
| 147 | + "pid": process.pid, |
| 148 | + } |
| 149 | + except Exception as e: |
| 150 | + return { |
| 151 | + "status": "error", |
| 152 | + "stdout": "", |
| 153 | + "stderr": str(e), |
| 154 | + "return_code": -1, |
| 155 | + "message": str(e), |
| 156 | + "pid": None, |
| 157 | + } |
| 158 | + |
| 159 | + # Foreground mode with proper timeout handling |
| 160 | + try: |
| 161 | + # Register with the run-cancel registry so a user force-stop can |
| 162 | + # kill this process tree while communicate() blocks the pool thread. |
| 163 | + from agent_core.core.impl.action.cancellation import ( |
| 164 | + register_process, |
| 165 | + unregister_process, |
| 166 | + was_killed, |
| 167 | + ) |
| 168 | + |
| 169 | + run_session_id = input_data.get("_session_id") or "" |
| 170 | + |
| 171 | + # Use CREATE_NEW_PROCESS_GROUP so we can kill the entire process tree |
| 172 | + fg_flags = creation_flags | subprocess.CREATE_NEW_PROCESS_GROUP |
| 173 | + process = subprocess.Popen( |
| 174 | + args, |
| 175 | + stdout=subprocess.PIPE, |
| 176 | + stderr=subprocess.PIPE, |
| 177 | + stdin=subprocess.DEVNULL, |
| 178 | + cwd=cwd if cwd else None, |
| 179 | + env=env, |
| 180 | + text=True, |
| 181 | + errors="replace", |
| 182 | + creationflags=fg_flags, |
| 183 | + ) |
| 184 | + register_process(run_session_id, process) |
| 185 | + |
| 186 | + try: |
| 187 | + stdout, stderr = process.communicate(timeout=timeout_seconds) |
| 188 | + if was_killed(process): |
| 189 | + # A user stop killed it; the exit code says nothing about |
| 190 | + # the script itself. |
| 191 | + return { |
| 192 | + "status": "error", |
| 193 | + "stdout": stdout.strip() if stdout else "", |
| 194 | + "stderr": stderr.strip() if stderr else "", |
| 195 | + "return_code": -1, |
| 196 | + "message": "Stopped by the user before the script finished.", |
| 197 | + "pid": None, |
| 198 | + } |
| 199 | + return { |
| 200 | + "status": "success" if process.returncode == 0 else "error", |
| 201 | + "stdout": stdout.strip() if stdout else "", |
| 202 | + "stderr": stderr.strip() if stderr else "", |
| 203 | + "return_code": process.returncode, |
| 204 | + "message": "", |
| 205 | + "pid": None, |
| 206 | + } |
| 207 | + except subprocess.TimeoutExpired: |
| 208 | + # Kill the entire process tree on Windows using taskkill |
| 209 | + try: |
| 210 | + subprocess.run( |
| 211 | + ["taskkill", "/F", "/T", "/PID", str(process.pid)], |
| 212 | + capture_output=True, |
| 213 | + creationflags=creation_flags, |
| 214 | + ) |
| 215 | + except Exception: |
| 216 | + pass |
| 217 | + process.kill() |
| 218 | + stdout, stderr = process.communicate() |
| 219 | + return { |
| 220 | + "status": "error", |
| 221 | + "stdout": (stdout or "").strip(), |
| 222 | + "stderr": (stderr or "").strip(), |
| 223 | + "return_code": -1, |
| 224 | + "message": f"Timed out after {timeout_seconds}s.", |
| 225 | + "pid": None, |
| 226 | + } |
| 227 | + finally: |
| 228 | + unregister_process(run_session_id, process) |
| 229 | + except Exception as e: |
| 230 | + return { |
| 231 | + "status": "error", |
| 232 | + "stdout": "", |
| 233 | + "stderr": str(e), |
| 234 | + "return_code": -1, |
| 235 | + "message": str(e), |
| 236 | + "pid": None, |
| 237 | + } |
0 commit comments