# backend/tasks/tools/terminal_tools.py import asyncio import os async def run_command(cmd: str, timeout: int = 30) -> dict: """Run command with pre-approval check, sanitized input, real subprocess execution.""" # Pre-approval check would be handled by the executor permission_gateway try: proc = await asyncio.create_subprocess_shell( cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=os.getcwd() ) try: stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout) except asyncio.TimeoutError: proc.kill() return {"status": "error", "error": "Command timed out"} return { "status": "success" if proc.returncode == 0 else "error", "exit_code": proc.returncode, "stdout": stdout.decode('utf-8', errors='replace'), "stderr": stderr.decode('utf-8', errors='replace') } except Exception as e: return {"status": "error", "error": str(e)}