Spaces:
Sleeping
Sleeping
tools
Browse files- server/app.py +1 -2
- server/tools/sandbox.py +133 -0
server/app.py
CHANGED
|
@@ -397,7 +397,6 @@ def state():
|
|
| 397 |
return jsonify(current_state.model_dump()), 200
|
| 398 |
|
| 399 |
if __name__ == "__main__":
|
| 400 |
-
|
| 401 |
-
# Hugging Face Spaces default port is usually 7860
|
| 402 |
port = int(os.environ.get("PORT", 7860))
|
| 403 |
app.run(host="0.0.0.0", port=port)
|
|
|
|
| 397 |
return jsonify(current_state.model_dump()), 200
|
| 398 |
|
| 399 |
if __name__ == "__main__":
|
| 400 |
+
|
|
|
|
| 401 |
port = int(os.environ.get("PORT", 7860))
|
| 402 |
app.run(host="0.0.0.0", port=port)
|
server/tools/sandbox.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import importlib
|
| 3 |
+
import importlib.util
|
| 4 |
+
import re
|
| 5 |
+
import sys
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
# block system-level and dangerous packages only, list more if you want
|
| 9 |
+
BLOCKED_LIBS = {
|
| 10 |
+
"os", "sys", "subprocess", "shutil", # system access stuff
|
| 11 |
+
"ctypes", "cffi", # native code execution
|
| 12 |
+
"socket", "paramiko", # raw network/ssh access
|
| 13 |
+
"scapy", # packet manipulation
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def is_installed(lib: str) -> bool:
|
| 18 |
+
"""Check if a library is importable in the current environment."""
|
| 19 |
+
return importlib.util.find_spec(lib) is not None
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def pip_install(lib: str) -> dict[str, Any]:
|
| 23 |
+
"""
|
| 24 |
+
Install any PyPI library the agent needs, except blocked system packages.
|
| 25 |
+
Decision to call this belongs to the agent, not the tools.
|
| 26 |
+
"""
|
| 27 |
+
if lib in BLOCKED_LIBS:
|
| 28 |
+
return {
|
| 29 |
+
"lib": lib,
|
| 30 |
+
"success": False,
|
| 31 |
+
"already_installed": False,
|
| 32 |
+
"output": f"'{lib}' is blocked — system or unsafe package"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
if is_installed(lib):
|
| 36 |
+
return {
|
| 37 |
+
"lib": lib,
|
| 38 |
+
"success": True,
|
| 39 |
+
"already_installed": True,
|
| 40 |
+
"output": f"'{lib}' is already installed"
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
result = subprocess.run(
|
| 45 |
+
[sys.executable, "-m", "pip", "install", lib],
|
| 46 |
+
capture_output=True,
|
| 47 |
+
text=True,
|
| 48 |
+
timeout=60
|
| 49 |
+
)
|
| 50 |
+
importlib.invalidate_caches()
|
| 51 |
+
return {
|
| 52 |
+
"lib": lib,
|
| 53 |
+
"success": result.returncode == 0,
|
| 54 |
+
"already_installed": False,
|
| 55 |
+
"output": result.stdout if result.returncode == 0 else result.stderr
|
| 56 |
+
}
|
| 57 |
+
except subprocess.TimeoutExpired:
|
| 58 |
+
return {
|
| 59 |
+
"lib": lib,
|
| 60 |
+
"success": False,
|
| 61 |
+
"already_installed": False,
|
| 62 |
+
"output": "pip install timed out after 60s"
|
| 63 |
+
}
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return {
|
| 66 |
+
"lib": lib,
|
| 67 |
+
"success": False,
|
| 68 |
+
"already_installed": False,
|
| 69 |
+
"output": str(e)
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def run_tests(test_dir: str, repo_dir: str | None = None) -> dict[str, Any]:
|
| 74 |
+
"""
|
| 75 |
+
Run pytest on the given test directory/file.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
test_dir: Path to the test directory or file to run.
|
| 79 |
+
repo_dir: Working directory for pytest (defaults to test_dir parent).
|
| 80 |
+
|
| 81 |
+
Returns:
|
| 82 |
+
Dict with keys: passed, failed, errors, total, score (0.0-1.0), output.
|
| 83 |
+
"""
|
| 84 |
+
cwd = repo_dir or test_dir
|
| 85 |
+
try:
|
| 86 |
+
result = subprocess.run(
|
| 87 |
+
[sys.executable, "-m", "pytest", test_dir, "--tb=short", "-q"],
|
| 88 |
+
capture_output=True,
|
| 89 |
+
text=True,
|
| 90 |
+
timeout=120,
|
| 91 |
+
cwd=cwd,
|
| 92 |
+
)
|
| 93 |
+
stdout = result.stdout
|
| 94 |
+
stderr = result.stderr
|
| 95 |
+
|
| 96 |
+
# Parse pytest summary line, e.g.: "3 passed, 1 failed, 1 error"
|
| 97 |
+
passed_match = re.search(r"(\d+) passed", stdout)
|
| 98 |
+
failed_match = re.search(r"(\d+) failed", stdout)
|
| 99 |
+
error_match = re.search(r"(\d+) error", stdout)
|
| 100 |
+
|
| 101 |
+
passed = int(passed_match.group(1)) if passed_match else 0
|
| 102 |
+
failed = int(failed_match.group(1)) if failed_match else 0
|
| 103 |
+
errors = int(error_match.group(1)) if error_match else 0
|
| 104 |
+
|
| 105 |
+
total = passed + failed + errors
|
| 106 |
+
score = passed / total if total > 0 else 0.0
|
| 107 |
+
|
| 108 |
+
return {
|
| 109 |
+
"passed": passed,
|
| 110 |
+
"failed": failed,
|
| 111 |
+
"errors": errors,
|
| 112 |
+
"total": total,
|
| 113 |
+
"score": round(score, 4),
|
| 114 |
+
"output": stdout + stderr,
|
| 115 |
+
}
|
| 116 |
+
except subprocess.TimeoutExpired:
|
| 117 |
+
return {
|
| 118 |
+
"passed": 0,
|
| 119 |
+
"failed": 0,
|
| 120 |
+
"errors": 1,
|
| 121 |
+
"total": 1,
|
| 122 |
+
"score": 0.0,
|
| 123 |
+
"output": "pytest timed out after 120s",
|
| 124 |
+
}
|
| 125 |
+
except Exception as e:
|
| 126 |
+
return {
|
| 127 |
+
"passed": 0,
|
| 128 |
+
"failed": 0,
|
| 129 |
+
"errors": 1,
|
| 130 |
+
"total": 1,
|
| 131 |
+
"score": 0.0,
|
| 132 |
+
"output": str(e),
|
| 133 |
+
}
|